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.

128 lines
3.5 KiB

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