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.

323 lines
7.1 KiB

6 years ago
6 years ago
8 years ago
6 years ago
8 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. ESPurna
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "config/all.h"
  16. #include <functional>
  17. #include <algorithm>
  18. #include <limits>
  19. #include <vector>
  20. #include <memory>
  21. #include "board.h"
  22. #include "compat.h"
  23. #include "storage_eeprom.h"
  24. #include "gpio.h"
  25. #include "settings.h"
  26. #include "system.h"
  27. #include "terminal.h"
  28. #include "utils.h"
  29. #include "wifi.h"
  30. #include "alexa.h"
  31. #include "api.h"
  32. #include "broker.h"
  33. #include "button.h"
  34. #include "debug.h"
  35. #include "domoticz.h"
  36. #include "homeassistant.h"
  37. #include "i2c.h"
  38. #include "ir.h"
  39. #include "led.h"
  40. #include "mqtt.h"
  41. #include "ntp.h"
  42. #include "ota.h"
  43. #include "relay.h"
  44. #include "rfm69.h"
  45. #include "rpc.h"
  46. #include "rpnrules.h"
  47. #include "rtcmem.h"
  48. #include "sensor.h"
  49. #include "thermostat.h"
  50. #include "tuya.h"
  51. #include "web.h"
  52. #include "ws.h"
  53. #include "libs/URL.h"
  54. #include "libs/HeapStats.h"
  55. using void_callback_f = void (*)();
  56. std::vector<void_callback_f> _loop_callbacks;
  57. std::vector<void_callback_f> _reload_callbacks;
  58. bool _reload_config = false;
  59. unsigned long _loop_delay = 0;
  60. // -----------------------------------------------------------------------------
  61. // GENERAL CALLBACKS
  62. // -----------------------------------------------------------------------------
  63. void espurnaRegisterLoop(void_callback_f callback) {
  64. _loop_callbacks.push_back(callback);
  65. }
  66. void espurnaRegisterReload(void_callback_f callback) {
  67. _reload_callbacks.push_back(callback);
  68. }
  69. void espurnaReload() {
  70. _reload_config = true;
  71. }
  72. void _espurnaReload() {
  73. for (const auto& callback : _reload_callbacks) {
  74. callback();
  75. }
  76. }
  77. unsigned long espurnaLoopDelay() {
  78. return _loop_delay;
  79. }
  80. // -----------------------------------------------------------------------------
  81. // BOOTING
  82. // -----------------------------------------------------------------------------
  83. void setup() {
  84. // -------------------------------------------------------------------------
  85. // Basic modules, will always run
  86. // -------------------------------------------------------------------------
  87. // Cache initial free heap value
  88. setInitialFreeHeap();
  89. // Init logging module
  90. #if DEBUG_SUPPORT
  91. debugSetup();
  92. #endif
  93. // Init GPIO functions
  94. gpioSetup();
  95. // Init RTCMEM
  96. rtcmemSetup();
  97. // Init EEPROM
  98. eepromSetup();
  99. // Init persistance
  100. settingsSetup();
  101. // Configure logger and crash recorder
  102. #if DEBUG_SUPPORT
  103. debugConfigureBoot();
  104. crashSetup();
  105. #endif
  106. // Return bogus free heap value for broken devices
  107. // XXX: device is likely to trigger other bugs! tread carefuly
  108. wtfHeap(getSetting<int>("wtfHeap", 0));
  109. // Init Serial, SPIFFS and system check
  110. systemSetup();
  111. // Init terminal features
  112. #if TERMINAL_SUPPORT
  113. terminalSetup();
  114. #endif
  115. // Hostname & board name initialization
  116. if (getSetting("hostname").length() == 0) {
  117. setDefaultHostname();
  118. }
  119. setBoardName();
  120. // Show welcome message and system configuration
  121. info(true);
  122. wifiSetup();
  123. #if OTA_ARDUINOOTA_SUPPORT
  124. arduinoOtaSetup();
  125. #endif
  126. #if TELNET_SUPPORT
  127. telnetSetup();
  128. #endif
  129. #if OTA_CLIENT != OTA_CLIENT_NONE
  130. otaClientSetup();
  131. #endif
  132. // -------------------------------------------------------------------------
  133. // Check if system is stable
  134. // -------------------------------------------------------------------------
  135. #if SYSTEM_CHECK_ENABLED
  136. if (!systemCheck()) return;
  137. #endif
  138. // -------------------------------------------------------------------------
  139. // Next modules will be only loaded if system is flagged as stable
  140. // -------------------------------------------------------------------------
  141. // Init webserver required before any module that uses API
  142. #if WEB_SUPPORT
  143. webSetup();
  144. wsSetup();
  145. #if DEBUG_WEB_SUPPORT
  146. debugWebSetup();
  147. #endif
  148. #if OTA_WEB_SUPPORT
  149. otaWebSetup();
  150. #endif
  151. #endif
  152. #if API_SUPPORT
  153. apiSetup();
  154. #endif
  155. // lightSetup must be called before relaySetup
  156. #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
  157. lightSetup();
  158. #endif
  159. #if RELAY_SUPPORT
  160. relaySetup();
  161. #endif
  162. #if BUTTON_SUPPORT
  163. buttonSetup();
  164. #endif
  165. #if ENCODER_SUPPORT && (LIGHT_PROVIDER != LIGHT_PROVIDER_NONE)
  166. encoderSetup();
  167. #endif
  168. #if LED_SUPPORT
  169. ledSetup();
  170. #endif
  171. #if MQTT_SUPPORT
  172. mqttSetup();
  173. #endif
  174. #if MDNS_SERVER_SUPPORT
  175. mdnsServerSetup();
  176. #endif
  177. #if MDNS_CLIENT_SUPPORT
  178. mdnsClientSetup();
  179. #endif
  180. #if LLMNR_SUPPORT
  181. llmnrSetup();
  182. #endif
  183. #if NETBIOS_SUPPORT
  184. netbiosSetup();
  185. #endif
  186. #if SSDP_SUPPORT
  187. ssdpSetup();
  188. #endif
  189. #if NTP_SUPPORT
  190. ntpSetup();
  191. #endif
  192. #if I2C_SUPPORT
  193. i2cSetup();
  194. #endif
  195. #if RF_SUPPORT
  196. rfbSetup();
  197. #endif
  198. #if ALEXA_SUPPORT
  199. alexaSetup();
  200. #endif
  201. #if NOFUSS_SUPPORT
  202. nofussSetup();
  203. #endif
  204. #if SENSOR_SUPPORT
  205. sensorSetup();
  206. #endif
  207. #if INFLUXDB_SUPPORT
  208. idbSetup();
  209. #endif
  210. #if THINGSPEAK_SUPPORT
  211. tspkSetup();
  212. #endif
  213. #if RFM69_SUPPORT
  214. rfm69Setup();
  215. #endif
  216. #if IR_SUPPORT
  217. irSetup();
  218. #endif
  219. #if DOMOTICZ_SUPPORT
  220. domoticzSetup();
  221. #endif
  222. #if HOMEASSISTANT_SUPPORT
  223. haSetup();
  224. #endif
  225. #if SCHEDULER_SUPPORT
  226. schSetup();
  227. #endif
  228. #if RPN_RULES_SUPPORT
  229. rpnSetup();
  230. #endif
  231. #if UART_MQTT_SUPPORT
  232. uartmqttSetup();
  233. #endif
  234. #ifdef FOXEL_LIGHTFOX_DUAL
  235. lightfoxSetup();
  236. #endif
  237. #if THERMOSTAT_SUPPORT
  238. thermostatSetup();
  239. #endif
  240. #if THERMOSTAT_DISPLAY_SUPPORT
  241. displaySetup();
  242. #endif
  243. #if TUYA_SUPPORT
  244. tuyaSetup();
  245. #endif
  246. // 3rd party code hook
  247. #if USE_EXTRA
  248. extraSetup();
  249. #endif
  250. // Prepare configuration for version 2.0
  251. migrate();
  252. // Set up delay() after loop callbacks are finished
  253. // Note: should be after settingsSetup()
  254. _loop_delay = constrain(
  255. getSetting("loopDelay", LOOP_DELAY_TIME), 0, 300
  256. );
  257. saveSettings();
  258. }
  259. void loop() {
  260. // Reload config before running any callbacks
  261. if (_reload_config) {
  262. _espurnaReload();
  263. _reload_config = false;
  264. }
  265. // Call registered loop callbacks
  266. for (unsigned char i = 0; i < _loop_callbacks.size(); i++) {
  267. (_loop_callbacks[i])();
  268. }
  269. // Power saving delay
  270. if (_loop_delay) delay(_loop_delay);
  271. }