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.

178 lines
4.9 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. void _telnetWebSocketOnSend(JsonObject& root) {
  16. root["telnetVisible"] = 1;
  17. root["telnetSTA"] = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  18. }
  19. void _telnetDisconnect(unsigned char clientId) {
  20. _telnetClients[clientId]->free();
  21. _telnetClients[clientId] = NULL;
  22. delete _telnetClients[clientId];
  23. wifiReconnectCheck();
  24. DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
  25. }
  26. bool _telnetWrite(unsigned char clientId, void *data, size_t len) {
  27. if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) {
  28. return (_telnetClients[clientId]->write((const char*) data, len) > 0);
  29. }
  30. return false;
  31. }
  32. unsigned char _telnetWrite(void *data, size_t len) {
  33. unsigned char count = 0;
  34. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  35. if (_telnetWrite(i, data, len)) ++count;
  36. }
  37. return count;
  38. }
  39. void _telnetData(unsigned char clientId, void *data, size_t len) {
  40. // Skip first message since it's always garbage
  41. if (_telnetFirst) {
  42. _telnetFirst = 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. // Telnet is always available for the ESPurna Core image
  57. #ifdef ESPURNA_CORE
  58. bool telnetSTA = true;
  59. #else
  60. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  61. #endif
  62. if (!telnetSTA) {
  63. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  64. client->onDisconnect([](void *s, AsyncClient *c) {
  65. c->free();
  66. delete c;
  67. });
  68. client->close(true);
  69. return;
  70. }
  71. }
  72. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  73. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  74. _telnetClients[i] = client;
  75. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  76. }, 0);
  77. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  78. _telnetData(i, data, len);
  79. }, 0);
  80. client->onDisconnect([i](void *s, AsyncClient *c) {
  81. _telnetDisconnect(i);
  82. }, 0);
  83. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  84. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), c->errorToString(error), error, i);
  85. }, 0);
  86. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  87. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%u at %lu\n"), i, time);
  88. c->close();
  89. }, 0);
  90. DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);
  91. // If there is no terminal support automatically dump info and crash data
  92. #if TERMINAL_SUPPORT == 0
  93. info();
  94. wifiStatus();
  95. debugDumpCrashInfo();
  96. debugClearCrashInfo();
  97. #endif
  98. _telnetFirst = true;
  99. wifiReconnectCheck();
  100. return;
  101. }
  102. }
  103. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  104. client->onDisconnect([](void *s, AsyncClient *c) {
  105. c->free();
  106. delete c;
  107. });
  108. client->close(true);
  109. }
  110. // -----------------------------------------------------------------------------
  111. // Public API
  112. // -----------------------------------------------------------------------------
  113. bool telnetConnected() {
  114. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  115. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  116. }
  117. return false;
  118. }
  119. unsigned char telnetWrite(unsigned char ch) {
  120. char data[1] = {ch};
  121. return _telnetWrite(data, 1);
  122. }
  123. void telnetSetup() {
  124. _telnetServer = new AsyncServer(TELNET_PORT);
  125. _telnetServer->onClient([](void *s, AsyncClient* c) {
  126. _telnetNewClient(c);
  127. }, 0);
  128. _telnetServer->begin();
  129. #if WEB_SUPPORT
  130. wsOnSendRegister(_telnetWebSocketOnSend);
  131. #endif
  132. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  133. }
  134. #endif // TELNET_SUPPORT