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.

144 lines
4.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
  1. /*
  2. TELNET MODULE
  3. Copyright (C) 2017 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. // -----------------------------------------------------------------------------
  12. // Private methods
  13. // -----------------------------------------------------------------------------
  14. void _telnetDisconnect(unsigned char clientId) {
  15. _telnetClients[clientId]->free();
  16. _telnetClients[clientId] = NULL;
  17. delete _telnetClients[clientId];
  18. wifiReconnectCheck();
  19. DEBUG_MSG_P(PSTR("[TELNET] Client #%d disconnected\n"), clientId);
  20. }
  21. bool _telnetWrite(unsigned char clientId, void *data, size_t len) {
  22. if (_telnetClients[clientId] && _telnetClients[clientId]->connected()) {
  23. return (_telnetClients[clientId]->write((const char*) data, len) > 0);
  24. }
  25. return false;
  26. }
  27. unsigned char _telnetWrite(void *data, size_t len) {
  28. unsigned char count = 0;
  29. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  30. if (_telnetWrite(i, data, len)) ++count;
  31. }
  32. return count;
  33. }
  34. void _telnetData(unsigned char clientId, void *data, size_t len) {
  35. // Capture close connection
  36. char * p = (char *) data;
  37. if ((strncmp(p, "close", 5) == 0) || (strncmp(p, "quit", 4) == 0)) {
  38. _telnetClients[clientId]->close();
  39. return;
  40. }
  41. // Inject into Embedis stream
  42. settingsInject(data, len);
  43. }
  44. void _telnetNewClient(AsyncClient *client) {
  45. if (client->localIP() != WiFi.softAPIP()) {
  46. bool telnetSTA = getSetting("telnetSTA", TELNET_STA).toInt() == 1;
  47. if (!telnetSTA) {
  48. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  49. client->onDisconnect([](void *s, AsyncClient *c) {
  50. c->free();
  51. delete c;
  52. });
  53. client->close(true);
  54. return;
  55. }
  56. }
  57. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  58. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  59. _telnetClients[i] = client;
  60. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  61. }, 0);
  62. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  63. _telnetData(i, data, len);
  64. }, 0);
  65. client->onDisconnect([i](void *s, AsyncClient *c) {
  66. _telnetDisconnect(i);
  67. }, 0);
  68. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  69. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%d\n"), c->errorToString(error), error, i);
  70. }, 0);
  71. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  72. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%d at %i\n"), i, time);
  73. c->close();
  74. }, 0);
  75. DEBUG_MSG_P(PSTR("[TELNET] Client #%d connected\n"), i);
  76. wifiReconnectCheck();
  77. return;
  78. }
  79. }
  80. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  81. client->onDisconnect([](void *s, AsyncClient *c) {
  82. c->free();
  83. delete c;
  84. });
  85. client->close(true);
  86. }
  87. // -----------------------------------------------------------------------------
  88. // Public API
  89. // -----------------------------------------------------------------------------
  90. bool telnetConnected() {
  91. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  92. if (_telnetClients[i] && _telnetClients[i]->connected()) return true;
  93. }
  94. return false;
  95. }
  96. unsigned char telnetWrite(unsigned char ch) {
  97. char data[1] = {ch};
  98. return _telnetWrite(data, 1);
  99. }
  100. void telnetSetup() {
  101. _telnetServer = new AsyncServer(TELNET_PORT);
  102. _telnetServer->onClient([](void *s, AsyncClient* c) {
  103. _telnetNewClient(c);
  104. }, 0);
  105. _telnetServer->begin();
  106. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  107. }
  108. #endif // TELNET_SUPPORT