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.

134 lines
3.7 KiB

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 TELNET_ONLY_AP
  45. if (client->localIP() != WiFi.softAPIP()) {
  46. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Only local connections\n"));
  47. client->onDisconnect([](void *s, AsyncClient *c) {
  48. c->free();
  49. delete c;
  50. });
  51. client->close(true);
  52. return;
  53. }
  54. #endif
  55. for (unsigned char i = 0; i < TELNET_MAX_CLIENTS; i++) {
  56. if (!_telnetClients[i] || !_telnetClients[i]->connected()) {
  57. _telnetClients[i] = client;
  58. client->onAck([i](void *s, AsyncClient *c, size_t len, uint32_t time) {
  59. }, 0);
  60. client->onData([i](void *s, AsyncClient *c, void *data, size_t len) {
  61. _telnetData(i, data, len);
  62. }, 0);
  63. client->onDisconnect([i](void *s, AsyncClient *c) {
  64. _telnetDisconnect(i);
  65. }, 0);
  66. client->onError([i](void *s, AsyncClient *c, int8_t error) {
  67. DEBUG_MSG_P(PSTR("[TELNET] Error %s (%d) on client #%d\n"), c->errorToString(error), error, i);
  68. }, 0);
  69. client->onTimeout([i](void *s, AsyncClient *c, uint32_t time) {
  70. DEBUG_MSG_P(PSTR("[TELNET] Timeout on client #%d at %i\n"), i, time);
  71. c->close();
  72. }, 0);
  73. DEBUG_MSG_P(PSTR("[TELNET] Client #%d connected\n"), i);
  74. return;
  75. }
  76. }
  77. DEBUG_MSG_P(PSTR("[TELNET] Rejecting - Too many connections\n"));
  78. client->onDisconnect([](void *s, AsyncClient *c) {
  79. c->free();
  80. delete c;
  81. });
  82. client->close(true);
  83. }
  84. // -----------------------------------------------------------------------------
  85. // Public API
  86. // -----------------------------------------------------------------------------
  87. unsigned char telnetWrite(unsigned char ch) {
  88. char data[1] = {ch};
  89. return _telnetWrite(data, 1);
  90. }
  91. void telnetSetup() {
  92. _telnetServer = new AsyncServer(TELNET_PORT);
  93. _telnetServer->onClient([](void *s, AsyncClient* c) {
  94. _telnetNewClient(c);
  95. }, 0);
  96. _telnetServer->begin();
  97. DEBUG_MSG_P(PSTR("[TELNET] Listening on port %d\n"), TELNET_PORT);
  98. }
  99. #endif // TELNET_SUPPORT