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.

187 lines
5.1 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
  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. if ((strncmp(p, "close", 5) == 0) || (strncmp(p, "quit", 4) == 0)) {
  53. _telnetClients[clientId]->close();
  54. return;
  55. }
  56. // Inject into Embedis stream
  57. settingsInject(data, len);
  58. }
  59. void _telnetNewClient(AsyncClient *client) {
  60. if (client->localIP() != WiFi.softAPIP()) {
  61. // Telnet is always available for the ESPurna Core image
  62. #ifdef ESPURNA_CORE
  63. bool telnetSTA = true;
  64. #else
  65. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  66. #endif
  67. if (!telnetSTA) {
  68. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  69. client->onDisconnect([](void *s, AsyncClient *c) {
  70. c->free();
  71. delete c;
  72. });
  73. client->close(true);
  74. return;
  75. }
  76. }
  77. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  78. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  79. _telnetClients[i] = client;
  80. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  81. }, 0);
  82. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  83. _telnetData(i, data, len);
  84. }, 0);
  85. client->onDisconnect([i](void *s, AsyncClient *c) {
  86. _telnetDisconnect(i);
  87. }, 0);
  88. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  89. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), c->errorToString(error), error, i);
  90. }, 0);
  91. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  92. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%u at %lu\n"), i, time);
  93. c->close();
  94. }, 0);
  95. DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);
  96. // If there is no terminal support automatically dump info and crash data
  97. #if TERMINAL_SUPPORT == 0
  98. info();
  99. wifiStatus();
  100. debugDumpCrashInfo();
  101. debugClearCrashInfo();
  102. #endif
  103. _telnetFirst = true;
  104. wifiReconnectCheck();
  105. return;
  106. }
  107. }
  108. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  109. client->onDisconnect([](void *s, AsyncClient *c) {
  110. c->free();
  111. delete c;
  112. });
  113. client->close(true);
  114. }
  115. // -----------------------------------------------------------------------------
  116. // Public API
  117. // -----------------------------------------------------------------------------
  118. bool telnetConnected() {
  119. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  120. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  121. }
  122. return false;
  123. }
  124. unsigned char telnetWrite(unsigned char ch) {
  125. char data[1] = {ch};
  126. return _telnetWrite(data, 1);
  127. }
  128. void telnetSetup() {
  129. _telnetServer = new AsyncServer(TELNET_PORT);
  130. _telnetServer->onClient([](void *s, AsyncClient* c) {
  131. _telnetNewClient(c);
  132. }, 0);
  133. _telnetServer->begin();
  134. #if WEB_SUPPORT
  135. wsOnSendRegister(_telnetWebSocketOnSend);
  136. wsOnReceiveRegister(_telnetWebSocketOnReceive);
  137. #endif
  138. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  139. }
  140. #endif // TELNET_SUPPORT