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.

161 lines
4.5 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. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  57. if (!telnetSTA) {
  58. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  59. client->onDisconnect([](void *s, AsyncClient *c) {
  60. c->free();
  61. delete c;
  62. });
  63. client->close(true);
  64. return;
  65. }
  66. }
  67. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  68. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  69. _telnetClients[i] = client;
  70. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  71. }, 0);
  72. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  73. _telnetData(i, data, len);
  74. }, 0);
  75. client->onDisconnect([i](void *s, AsyncClient *c) {
  76. _telnetDisconnect(i);
  77. }, 0);
  78. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  79. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%u\n"), c->errorToString(error), error, i);
  80. }, 0);
  81. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  82. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%u at %lu\n"), i, time);
  83. c->close();
  84. }, 0);
  85. DEBUG_MSG_P(PSTR("[TELNET] Client #%u connected\n"), i);
  86. _telnetFirst = true;
  87. wifiReconnectCheck();
  88. return;
  89. }
  90. }
  91. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  92. client->onDisconnect([](void *s, AsyncClient *c) {
  93. c->free();
  94. delete c;
  95. });
  96. client->close(true);
  97. }
  98. // -----------------------------------------------------------------------------
  99. // Public API
  100. // -----------------------------------------------------------------------------
  101. bool telnetConnected() {
  102. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  103. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  104. }
  105. return false;
  106. }
  107. unsigned char telnetWrite(unsigned char ch) {
  108. char data[1] = {ch};
  109. return _telnetWrite(data, 1);
  110. }
  111. void telnetSetup() {
  112. _telnetServer = new AsyncServer(TELNET_PORT);
  113. _telnetServer->onClient([](void *s, AsyncClient* c) {
  114. _telnetNewClient(c);
  115. }, 0);
  116. _telnetServer->begin();
  117. #if WEB_SUPPORT
  118. wsOnSendRegister(_telnetWebSocketOnSend);
  119. #endif
  120. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  121. }
  122. #endif // TELNET_SUPPORT