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.

205 lines
5.5 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
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. Module key prefix: tel
  7. */
  8. #if TELNET_SUPPORT
  9. #include <ESPAsyncTCP.h>
  10. AsyncServer * _telnetServer;
  11. AsyncClient * _telnetClients[TELNET_MAX_CLIENTS];
  12. bool _telnetFirst = true;
  13. // -----------------------------------------------------------------------------
  14. // Private methods
  15. // -----------------------------------------------------------------------------
  16. #if WEB_SUPPORT
  17. bool _telnetWebSocketOnReceive(const char * key, JsonVariant& value) {
  18. return (strncmp(key, "telt", 3) == 0);
  19. }
  20. void _telnetWebSocketOnSend(JsonObject& root) {
  21. root["telVisible"] = 1;
  22. root["telSTA"] = getSetting("telSTA", TELNET_STA).toInt() == 1;
  23. }
  24. #endif
  25. void _telnetDisconnect(unsigned char clientId) {
  26. _telnetClients[clientId]->free();
  27. _telnetClients[clientId] = NULL;
  28. delete _telnetClients[clientId];
  29. wifiReconnectCheck();
  30. DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
  31. }
  32. bool _telnetWrite(unsigned char clientId, void *data, size_t len) {
  33. if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) {
  34. return (_telnetClients[clientId]->write((const char*) data, len) > 0);
  35. }
  36. return false;
  37. }
  38. unsigned char _telnetWrite(void *data, size_t len) {
  39. unsigned char count = 0;
  40. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  41. if (_telnetWrite(i, data, len)) ++count;
  42. }
  43. return count;
  44. }
  45. void _telnetData(unsigned char clientId, void *data, size_t len) {
  46. // Skip first message since it's always garbage
  47. if (_telnetFirst) {
  48. _telnetFirst = false;
  49. return;
  50. }
  51. // Capture close connection
  52. char * p = (char *) data;
  53. // C-d is sent as two bytes (sometimes repeating)
  54. if (len >= 2) {
  55. if ((p[0] == 0xFF) && (p[1] == 0xEC)) {
  56. _telnetClients[clientId]->close(true);
  57. return;
  58. }
  59. }
  60. if ((strncmp(p, "close", 5) == 0) || (strncmp(p, "quit", 4) == 0)) {
  61. _telnetClients[clientId]->close();
  62. return;
  63. }
  64. // Inject into Embedis stream
  65. settingsInject(data, len);
  66. }
  67. void _telnetNewClient(AsyncClient *client) {
  68. if (client->localIP() != WiFi.softAPIP()) {
  69. // Telnet is always available for the ESPurna Core image
  70. #ifdef ESPURNA_CORE
  71. bool telnetSTA = true;
  72. #else
  73. bool telnetSTA = getSetting("telSTA", TELNET_STA).toInt() == 1;
  74. #endif
  75. if (!telnetSTA) {
  76. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  77. client->onDisconnect([](void *s, AsyncClient *c) {
  78. c->free();
  79. delete c;
  80. });
  81. client->close(true);
  82. return;
  83. }
  84. }
  85. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  86. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  87. _telnetClients[i] = client;
  88. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  89. }, 0);
  90. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  91. _telnetData(i, data, len);
  92. }, 0);
  93. client->onDisconnect([i](void *s, AsyncClient *c) {
  94. _telnetDisconnect(i);
  95. }, 0);
  96. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  97. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), c->errorToString(error), error, i);
  98. }, 0);
  99. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  100. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%u at %lu\n"), i, time);
  101. c->close();
  102. }, 0);
  103. DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);
  104. // If there is no terminal support automatically dump info and crash data
  105. #if TERMINAL_SUPPORT == 0
  106. info();
  107. wifiDebug();
  108. debugDumpCrashInfo();
  109. debugClearCrashInfo();
  110. #endif
  111. _telnetFirst = true;
  112. wifiReconnectCheck();
  113. return;
  114. }
  115. }
  116. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  117. client->onDisconnect([](void *s, AsyncClient *c) {
  118. c->free();
  119. delete c;
  120. });
  121. client->close(true);
  122. }
  123. void _telnetBackwards() {
  124. moveSetting("telnetSTA", "telSTA"); // 1.13.1 -- 2018-06-26
  125. }
  126. // -----------------------------------------------------------------------------
  127. // Public API
  128. // -----------------------------------------------------------------------------
  129. bool telnetConnected() {
  130. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  131. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  132. }
  133. return false;
  134. }
  135. unsigned char telnetWrite(unsigned char ch) {
  136. char data[1] = {ch};
  137. return _telnetWrite(data, 1);
  138. }
  139. void telnetSetup() {
  140. // Backwards compatibility
  141. _telnetBackwards();
  142. _telnetServer = new AsyncServer(TELNET_PORT);
  143. _telnetServer->onClient([](void *s, AsyncClient* c) {
  144. _telnetNewClient(c);
  145. }, 0);
  146. _telnetServer->begin();
  147. #if WEB_SUPPORT
  148. wsOnSendRegister(_telnetWebSocketOnSend);
  149. wsOnReceiveRegister(_telnetWebSocketOnReceive);
  150. #endif
  151. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  152. }
  153. #endif // TELNET_SUPPORT