Mirror of espurna firmware for wireless switches and more
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.

115 lines
2.9 KiB

Terminal: change command-line parser (#2247) Change the underlying command line handling: - switch to a custom parser, inspired by redis / sds - update terminalRegisterCommand signature, pass only bare minimum - clean-up `help` & `commands`. update settings `set`, `get` and `del` - allow our custom test suite to run command-line tests - clean-up Stream IO to allow us to print large things into debug stream (for example, `eeprom.dump`) - send parsing errors to the debug log As a proof of concept, introduce `TERMINAL_MQTT_SUPPORT` and `TERMINAL_WEB_API_SUPPORT` - MQTT subscribes to the `<root>/cmd/set` and sends response to the `<root>/cmd`. We can't output too much, as we don't have any large-send API. - Web API listens to the `/api/cmd?apikey=...&line=...` (or PUT, params inside the body). This one is intended as a possible replacement of the `API_SUPPORT`. Internals introduce a 'task' around the AsyncWebServerRequest object that will simulate what WiFiClient does and push data into it continuously, switching between CONT and SYS. Both are experimental. We only accept a single command and not every command is updated to use Print `ctx.output` object. We are also somewhat limited by the Print / Stream overall, perhaps I am overestimating the usefulness of Arduino compatibility to such an extent :) Web API handler can also sometimes show only part of the result, whenever the command tries to yield() by itself waiting for something. Perhaps we would need to create a custom request handler for that specific use-case.
4 years ago
  1. /*
  2. WIFI MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #pragma once
  6. #include <Arduino.h>
  7. #include <lwip/init.h>
  8. #if LWIP_VERSION_MAJOR == 1
  9. #include <netif/etharp.h>
  10. #elif LWIP_VERSION_MAJOR >= 2
  11. #include <lwip/etharp.h>
  12. #endif
  13. // (HACK) allow us to use internal lwip struct.
  14. // esp8266 re-defines enum values from tcp header... include them first
  15. #define LWIP_INTERNAL
  16. #include <ESP8266WiFi.h>
  17. #undef LWIP_INTERNAL
  18. extern "C" {
  19. #include <lwip/opt.h>
  20. #include <lwip/ip.h>
  21. #include <lwip/tcp.h>
  22. #include <lwip/inet.h> // ip_addr_t
  23. #include <lwip/err.h> // ERR_x
  24. #include <lwip/dns.h> // dns_getserver, dns_gethostbyname
  25. #include <lwip/ip_addr.h> // ip4/ip6 helpers
  26. };
  27. // ref: https://github.com/me-no-dev/ESPAsyncTCP/pull/115/files#diff-e2e636049095cc1ff920c1bfabf6dcacR8
  28. // This is missing with Core 2.3.0 and is sometimes missing from the build flags. Assume HIGH_BANDWIDTH version.
  29. #ifndef TCP_MSS
  30. #define TCP_MSS (1460)
  31. #endif
  32. namespace espurna {
  33. namespace wifi {
  34. enum class Event {
  35. Initial, // aka boot
  36. Mode, // when opmode changes
  37. StationInit, // station initialized by the connetion routine
  38. StationScan, // scanning before the connection
  39. StationConnecting, // network was selected and connection is in progress
  40. StationConnected, // successful connection
  41. StationDisconnected, // disconnected from the current network
  42. StationTimeout, // timeout after the previous connecting state
  43. StationReconnect // timeout after all connection loops failed
  44. };
  45. using EventCallback = void(*)(Event event);
  46. enum class BootMode {
  47. Disabled,
  48. Enabled,
  49. };
  50. enum class StaMode {
  51. Disabled,
  52. Enabled,
  53. };
  54. enum class ApMode {
  55. Disabled,
  56. Enabled,
  57. Fallback,
  58. };
  59. } // namespace wifi
  60. } // namespace espurna
  61. // Note that 'connected' status is *only* for the WiFi STA.
  62. // Overall connectivity depends on low-level network stack and it may be
  63. // useful to check whether relevant interfaces are up and have a routable IP
  64. // instead of exclusively depending on the WiFi API.
  65. // (e.g. when we have injected ethernet, wireguard, etc. interfaces.
  66. // esp8266 implementation specifically uses lwip, ref. `netif_list`)
  67. bool wifiConnected();
  68. // When AP is up and running
  69. bool wifiConnectable();
  70. size_t wifiApStations();
  71. IPAddress wifiApIp();
  72. // Current STA connection
  73. String wifiStaSsid();
  74. IPAddress wifiStaIp();
  75. // Request to change the current STA / AP status
  76. // Current state persists until reset or configuration reload
  77. void wifiStartAp();
  78. void wifiToggleAp();
  79. void wifiToggleSta();
  80. // Disconnects STA intefrace, will trigger reconnection
  81. void wifiDisconnect();
  82. // Toggle WiFi modem
  83. bool wifiDisabled();
  84. void wifiDisable();
  85. void wifiTurnOff();
  86. void wifiTurnOn();
  87. // Trigger fallback check for the AP
  88. void wifiApCheck();
  89. void wifiRegister(espurna::wifi::EventCallback);
  90. void wifiSetup();