Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.4 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. TELNET MODULE
  3. Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Parts of the code have been borrowed from Thomas Sarlandie's NetServer
  5. (https://github.com/sarfata/kbox-firmware/tree/master/src/esp)
  6. */
  7. #if TELNET_SUPPORT
  8. #include <ESPAsyncTCP.h>
  9. AsyncServer * _telnetServer;
  10. AsyncClient * _telnetClients[TELNET_MAX_CLIENTS];
  11. // -----------------------------------------------------------------------------
  12. // Private methods
  13. // -----------------------------------------------------------------------------
  14. void _telnetWebSocketOnSend(JsonObject& root) {
  15. root["telnetVisible"] = 1;
  16. root["telnetSTA"] = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  17. }
  18. void _telnetDisconnect(unsigned char clientId) {
  19. _telnetClients[clientId]->free();
  20. _telnetClients[clientId] = NULL;
  21. delete _telnetClients[clientId];
  22. wifiReconnectCheck();
  23. DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
  24. }
  25. bool _telnetWrite(unsigned char clientId, void *data, size_t len) {
  26. if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) {
  27. return (_telnetClients[clientId]->write((const char*) data, len) > 0);
  28. }
  29. return false;
  30. }
  31. unsigned char _telnetWrite(void *data, size_t len) {
  32. unsigned char count = 0;
  33. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  34. if (_telnetWrite(i, data, len)) ++count;
  35. }
  36. return count;
  37. }
  38. void _telnetData(unsigned char clientId, void *data, size_t len) {
  39. // Skip first message since it's always garbage
  40. static bool first = true;
  41. if (first) {
  42. first = false;
  43. return;
  44. }
  45. // Capture close connection
  46. char * p = (char *) data;
  47. if ((strncmp(p, "close", 5) == 0) || (strncmp(p, "quit", 4) == 0)) {
  48. _telnetClients[clientId]->close();
  49. return;
  50. }
  51. // Inject into Embedis stream
  52. settingsInject(data, len);
  53. }
  54. void _telnetNewClient(AsyncClient *client) {
  55. if (client->localIP() != WiFi.softAPIP()) {
  56. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  57. if (!telnetSTA) {
  58. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  59. client->onDisconnect([](void *s, AsyncClient *c) {
  60. c->free();
  61. delete c;
  62. });
  63. client->close(true);
  64. return;
  65. }
  66. }
  67. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  68. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  69. _telnetClients[i] = client;
  70. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  71. }, 0);
  72. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  73. _telnetData(i, data, len);
  74. }, 0);
  75. client->onDisconnect([i](void *s, AsyncClient *c) {
  76. _telnetDisconnect(i);
  77. }, 0);
  78. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  79. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), c->errorToString(error), error, i);
  80. }, 0);
  81. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  82. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%u at %lu\n"), i, time);
  83. c->close();
  84. }, 0);
  85. DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);
  86. wifiReconnectCheck();
  87. return;
  88. }
  89. }
  90. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  91. client->onDisconnect([](void *s, AsyncClient *c) {
  92. c->free();
  93. delete c;
  94. });
  95. client->close(true);
  96. }
  97. // -----------------------------------------------------------------------------
  98. // Public API
  99. // -----------------------------------------------------------------------------
  100. bool telnetConnected() {
  101. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  102. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  103. }
  104. return false;
  105. }
  106. unsigned char telnetWrite(unsigned char ch) {
  107. char data[1] = {ch};
  108. return _telnetWrite(data, 1);
  109. }
  110. void telnetSetup() {
  111. _telnetServer = new AsyncServer(TELNET_PORT);
  112. _telnetServer->onClient([](void *s, AsyncClient* c) {
  113. _telnetNewClient(c);
  114. }, 0);
  115. _telnetServer->begin();
  116. #if WEB_SUPPORT
  117. wsOnSendRegister(_telnetWebSocketOnSend);
  118. #endif
  119. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  120. }
  121. #endif // TELNET_SUPPORT