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.

75 lines
1.7 KiB

  1. /*
  2. TELNET MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #include "espurna.h"
  7. #include <Arduino.h>
  8. #include <Schedule.h>
  9. #include <memory>
  10. #include <list>
  11. #if TELNET_SERVER == TELNET_SERVER_ASYNC
  12. #include <ESPAsyncTCP.h>
  13. struct AsyncBufferedClient {
  14. public:
  15. constexpr static const size_t BUFFERS_MAX = 5;
  16. using buffer_t = std::vector<uint8_t>;
  17. AsyncBufferedClient(AsyncClient* client);
  18. size_t write(char c);
  19. size_t write(const char* data, size_t size=0);
  20. void flush();
  21. size_t available();
  22. bool connect(const char *host, uint16_t port);
  23. void close(bool now = false);
  24. bool connected();
  25. private:
  26. void _addBuffer();
  27. static void _trySend(AsyncBufferedClient* client);
  28. static void _s_onAck(void* client_ptr, AsyncClient*, size_t, uint32_t);
  29. static void _s_onPoll(void* client_ptr, AsyncClient* client);
  30. std::unique_ptr<AsyncClient> _client;
  31. std::list<buffer_t> _buffers;
  32. };
  33. using TTelnetServer = AsyncServer;
  34. #if TELNET_SERVER_ASYNC_BUFFERED
  35. using TTelnetClient = AsyncBufferedClient;
  36. #else
  37. using TTelnetClient = AsyncClient;
  38. #endif // TELNET_SERVER_ASYNC_BUFFERED
  39. #elif TELNET_SERVER == TELNET_SERVER_WIFISERVER
  40. using TTelnetServer = WiFiServer;
  41. using TTelnetClient = WiFiClient;
  42. #else
  43. #error "TELNET_SERVER value was not properly set"
  44. #endif
  45. constexpr unsigned char TELNET_IAC = 0xFF;
  46. constexpr unsigned char TELNET_XEOF = 0xEC;
  47. bool telnetConnected();
  48. unsigned char telnetWrite(unsigned char ch);
  49. bool telnetDebugSend(const char* prefix, const char* data);
  50. void telnetSetup();