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.

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