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.

196 lines
5.3 KiB

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