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.

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