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.

177 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. #endif
  97. _telnetFirst = true;
  98. wifiReconnectCheck();
  99. return;
  100. }
  101. }
  102. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  103. client->onDisconnect([](void *s, AsyncClient *c) {
  104. c->free();
  105. delete c;
  106. });
  107. client->close(true);
  108. }
  109. // -----------------------------------------------------------------------------
  110. // Public API
  111. // -----------------------------------------------------------------------------
  112. bool telnetConnected() {
  113. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  114. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  115. }
  116. return false;
  117. }
  118. unsigned char telnetWrite(unsigned char ch) {
  119. char data[1] = {ch};
  120. return _telnetWrite(data, 1);
  121. }
  122. void telnetSetup() {
  123. _telnetServer = new AsyncServer(TELNET_PORT);
  124. _telnetServer->onClient([](void *s, AsyncClient* c) {
  125. _telnetNewClient(c);
  126. }, 0);
  127. _telnetServer->begin();
  128. #if WEB_SUPPORT
  129. wsOnSendRegister(_telnetWebSocketOnSend);
  130. #endif
  131. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  132. }
  133. #endif // TELNET_SUPPORT