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.

655 lines
18 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
6 years ago
  1. /*
  2. WIFI MODULE
  3. Copyright (C) 2016-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. Module key prefix: wifi
  5. */
  6. #include "JustWifi.h"
  7. #include <Ticker.h>
  8. uint32_t _wifi_scan_client_id = 0;
  9. bool _wifi_wps_running = false;
  10. bool _wifi_smartconfig_running = false;
  11. uint8_t _wifi_ap_mode = WIFI_AP_FALLBACK;
  12. // -----------------------------------------------------------------------------
  13. // PRIVATE
  14. // -----------------------------------------------------------------------------
  15. void _wifiCheckAP() {
  16. if ((WIFI_AP_FALLBACK == _wifi_ap_mode) &&
  17. (jw.connected()) &&
  18. ((WiFi.getMode() & WIFI_AP) > 0) &&
  19. (WiFi.softAPgetStationNum() == 0)
  20. ) {
  21. jw.enableAP(false);
  22. }
  23. }
  24. void _wifiConfigure() {
  25. jw.setHostname(getHostname().c_str());
  26. #if USE_PASSWORD
  27. jw.setSoftAP(getHostname().c_str(), getPassword().c_str());
  28. #else
  29. jw.setSoftAP(getHostname().c_str());
  30. #endif
  31. jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
  32. wifiReconnectCheck();
  33. jw.enableAPFallback(true);
  34. jw.cleanNetworks();
  35. _wifi_ap_mode = getSetting("wifiMode", WIFI_AP_FALLBACK).toInt();
  36. // If system is flagged unstable we do not init wifi networks
  37. #if SYSTEM_CHECK_ENABLED
  38. if (!systemCheck()) return;
  39. #endif
  40. // Clean settings
  41. _wifiClean(WIFI_MAX_NETWORKS);
  42. int i;
  43. for (i = 0; i< WIFI_MAX_NETWORKS; i++) {
  44. if (!hasSetting("wifiName", i)) break;
  45. if (hasSetting("wifiIP", i)) {
  46. jw.addNetwork(
  47. getSetting("wifiName", i, "").c_str(),
  48. getSetting("wifiPass", i, "").c_str(),
  49. getSetting("wifiIP", i, "").c_str(),
  50. getSetting("wifiGW", i, "").c_str(),
  51. getSetting("wifiMask", i, "").c_str(),
  52. getSetting("wifiDNS", i, "").c_str()
  53. );
  54. } else {
  55. jw.addNetwork(
  56. getSetting("wifiName", i, "").c_str(),
  57. getSetting("wifiPass", i, "").c_str()
  58. );
  59. }
  60. }
  61. jw.enableScan(getSetting("wifiScan", WIFI_SCAN_NETWORKS).toInt() == 1);
  62. }
  63. void _wifiScan(uint32_t client_id = 0) {
  64. DEBUG_MSG_P(PSTR("[WIFI] Start scanning\n"));
  65. #if WEB_SUPPORT
  66. String output;
  67. #endif
  68. unsigned char result = WiFi.scanNetworks();
  69. if (result == WIFI_SCAN_FAILED) {
  70. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  71. #if WEB_SUPPORT
  72. output = String("Failed scan");
  73. #endif
  74. } else if (result == 0) {
  75. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  76. #if WEB_SUPPORT
  77. output = String("No networks found");
  78. #endif
  79. } else {
  80. DEBUG_MSG_P(PSTR("[WIFI] %d networks found:\n"), result);
  81. // Populate defined networks with scan data
  82. for (int8_t i = 0; i < result; ++i) {
  83. String ssid_scan;
  84. int32_t rssi_scan;
  85. uint8_t sec_scan;
  86. uint8_t* BSSID_scan;
  87. int32_t chan_scan;
  88. bool hidden_scan;
  89. char buffer[128];
  90. WiFi.getNetworkInfo(i, ssid_scan, sec_scan, rssi_scan, BSSID_scan, chan_scan, hidden_scan);
  91. snprintf_P(buffer, sizeof(buffer),
  92. PSTR("BSSID: %02X:%02X:%02X:%02X:%02X:%02X SEC: %s RSSI: %3d CH: %2d SSID: %s"),
  93. BSSID_scan[1], BSSID_scan[2], BSSID_scan[3], BSSID_scan[4], BSSID_scan[5], BSSID_scan[6],
  94. (sec_scan != ENC_TYPE_NONE ? "YES" : "NO "),
  95. rssi_scan,
  96. chan_scan,
  97. (char *) ssid_scan.c_str()
  98. );
  99. DEBUG_MSG_P(PSTR("[WIFI] > %s\n"), buffer);
  100. #if WEB_SUPPORT
  101. if (client_id > 0) output = output + String(buffer) + String("<br />");
  102. #endif
  103. }
  104. }
  105. #if WEB_SUPPORT
  106. if (client_id > 0) {
  107. output = String("{\"scanResult\": \"") + output + String("\"}");
  108. wsSend(client_id, output.c_str());
  109. }
  110. #endif
  111. WiFi.scanDelete();
  112. }
  113. bool _wifiClean(unsigned char num) {
  114. bool changed = false;
  115. int i = 0;
  116. // Clean defined settings
  117. while (i < num) {
  118. // Skip on first non-defined setting
  119. if (!hasSetting("wifiName", i)) {
  120. delSetting("wifiName", i);
  121. break;
  122. }
  123. // Delete empty values
  124. if (!hasSetting("wifiPass", i)) delSetting("wifiPass", i);
  125. if (!hasSetting("wifiIP", i)) delSetting("wifiIP", i);
  126. if (!hasSetting("wifiGW", i)) delSetting("wifiGW", i);
  127. if (!hasSetting("wifiMask", i)) delSetting("wifiMask", i);
  128. if (!hasSetting("wifiDNS", i)) delSetting("wifiDNS", i);
  129. ++i;
  130. }
  131. // Delete all other settings
  132. while (i < WIFI_MAX_NETWORKS) {
  133. changed = hasSetting("wifiName", i);
  134. delSetting("wifiName", i);
  135. delSetting("wifiPass", i);
  136. delSetting("wifiIP", i);
  137. delSetting("wifiGW", i);
  138. delSetting("wifiMask", i);
  139. delSetting("wifiDNS", i);
  140. ++i;
  141. }
  142. return changed;
  143. }
  144. // Inject hardcoded networks
  145. void _wifiInject() {
  146. if (strlen(WIFI1_SSID)) {
  147. if (!hasSetting("wifiName", 0)) {
  148. setSetting("wifiName", 0, WIFI1_SSID);
  149. setSetting("wifiPass", 0, WIFI1_PASS);
  150. setSetting("wifiIP", 0, WIFI1_IP);
  151. setSetting("wifiGW", 0, WIFI1_GW);
  152. setSetting("wifiMask", 0, WIFI1_MASK);
  153. setSetting("wifiDNS", 0, WIFI1_DNS);
  154. }
  155. if (strlen(WIFI2_SSID)) {
  156. if (!hasSetting("wifiName", 1)) {
  157. setSetting("wifiName", 1, WIFI2_SSID);
  158. setSetting("wifiPass", 1, WIFI2_PASS);
  159. setSetting("wifiIP", 1, WIFI2_IP);
  160. setSetting("wifiGW", 1, WIFI2_GW);
  161. setSetting("wifiMask", 1, WIFI2_MASK);
  162. setSetting("wifiDNS", 1, WIFI2_DNS);
  163. }
  164. }
  165. }
  166. }
  167. void _wifiCallback(justwifi_messages_t code, char * parameter) {
  168. if (MESSAGE_WPS_START == code) {
  169. _wifi_wps_running = true;
  170. }
  171. if (MESSAGE_SMARTCONFIG_START == code) {
  172. _wifi_smartconfig_running = true;
  173. }
  174. if (MESSAGE_WPS_ERROR == code || MESSAGE_SMARTCONFIG_ERROR == code) {
  175. _wifi_wps_running = false;
  176. _wifi_smartconfig_running = false;
  177. }
  178. if (MESSAGE_WPS_SUCCESS == code || MESSAGE_SMARTCONFIG_SUCCESS == code) {
  179. String ssid = WiFi.SSID();
  180. String pass = WiFi.psk();
  181. // Look for the same SSID
  182. uint8_t count = 0;
  183. while (count < WIFI_MAX_NETWORKS) {
  184. if (!hasSetting("wifiName", count)) break;
  185. if (ssid.equals(getSetting("wifiName", count, ""))) break;
  186. count++;
  187. }
  188. // If we have reached the max we overwrite the first one
  189. if (WIFI_MAX_NETWORKS == count) count = 0;
  190. setSetting("wifiName", count, ssid);
  191. setSetting("wifiPass", count, pass);
  192. _wifi_wps_running = false;
  193. _wifi_smartconfig_running = false;
  194. }
  195. }
  196. #if WIFI_AP_CAPTIVE
  197. #include "DNSServer.h"
  198. DNSServer _wifi_dnsServer;
  199. void _wifiCaptivePortal(justwifi_messages_t code, char * parameter) {
  200. if (MESSAGE_ACCESSPOINT_CREATED == code) {
  201. _wifi_dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
  202. _wifi_dnsServer.start(53, "*", WiFi.softAPIP());
  203. DEBUG_MSG_P(PSTR("[WIFI] Captive portal enabled\n"));
  204. }
  205. if (MESSAGE_CONNECTED == code) {
  206. _wifi_dnsServer.stop();
  207. DEBUG_MSG_P(PSTR("[WIFI] Captive portal disabled\n"));
  208. }
  209. }
  210. #endif // WIFI_AP_CAPTIVE
  211. #if DEBUG_SUPPORT
  212. void _wifiDebugCallback(justwifi_messages_t code, char * parameter) {
  213. // -------------------------------------------------------------------------
  214. if (code == MESSAGE_SCANNING) {
  215. DEBUG_MSG_P(PSTR("[WIFI] Scanning\n"));
  216. }
  217. if (code == MESSAGE_SCAN_FAILED) {
  218. DEBUG_MSG_P(PSTR("[WIFI] Scan failed\n"));
  219. }
  220. if (code == MESSAGE_NO_NETWORKS) {
  221. DEBUG_MSG_P(PSTR("[WIFI] No networks found\n"));
  222. }
  223. if (code == MESSAGE_NO_KNOWN_NETWORKS) {
  224. DEBUG_MSG_P(PSTR("[WIFI] No known networks found\n"));
  225. }
  226. if (code == MESSAGE_FOUND_NETWORK) {
  227. DEBUG_MSG_P(PSTR("[WIFI] %s\n"), parameter);
  228. }
  229. // -------------------------------------------------------------------------
  230. if (code == MESSAGE_CONNECTING) {
  231. DEBUG_MSG_P(PSTR("[WIFI] Connecting to %s\n"), parameter);
  232. }
  233. if (code == MESSAGE_CONNECT_WAITING) {
  234. // too much noise
  235. }
  236. if (code == MESSAGE_CONNECT_FAILED) {
  237. DEBUG_MSG_P(PSTR("[WIFI] Could not connect to %s\n"), parameter);
  238. }
  239. if (code == MESSAGE_CONNECTED) {
  240. wifiDebug(WIFI_STA);
  241. }
  242. if (code == MESSAGE_DISCONNECTED) {
  243. DEBUG_MSG_P(PSTR("[WIFI] Disconnected\n"));
  244. }
  245. // -------------------------------------------------------------------------
  246. if (code == MESSAGE_ACCESSPOINT_CREATING) {
  247. DEBUG_MSG_P(PSTR("[WIFI] Creating access point\n"));
  248. }
  249. if (code == MESSAGE_ACCESSPOINT_CREATED) {
  250. wifiDebug(WIFI_AP);
  251. }
  252. if (code == MESSAGE_ACCESSPOINT_FAILED) {
  253. DEBUG_MSG_P(PSTR("[WIFI] Could not create access point\n"));
  254. }
  255. if (code == MESSAGE_ACCESSPOINT_DESTROYED) {
  256. DEBUG_MSG_P(PSTR("[WIFI] Access point destroyed\n"));
  257. }
  258. // -------------------------------------------------------------------------
  259. if (code == MESSAGE_WPS_START) {
  260. DEBUG_MSG_P(PSTR("[WIFI] WPS started\n"));
  261. }
  262. if (code == MESSAGE_WPS_SUCCESS) {
  263. DEBUG_MSG_P(PSTR("[WIFI] WPS succeded!\n"));
  264. }
  265. if (code == MESSAGE_WPS_ERROR) {
  266. DEBUG_MSG_P(PSTR("[WIFI] WPS failed\n"));
  267. }
  268. // ------------------------------------------------------------------------
  269. if (code == MESSAGE_SMARTCONFIG_START) {
  270. DEBUG_MSG_P(PSTR("[WIFI] Smart Config started\n"));
  271. }
  272. if (code == MESSAGE_SMARTCONFIG_SUCCESS) {
  273. DEBUG_MSG_P(PSTR("[WIFI] Smart Config succeded!\n"));
  274. }
  275. if (code == MESSAGE_SMARTCONFIG_ERROR) {
  276. DEBUG_MSG_P(PSTR("[WIFI] Smart Config failed\n"));
  277. }
  278. }
  279. #endif // DEBUG_SUPPORT
  280. // -----------------------------------------------------------------------------
  281. // SETTINGS
  282. // -----------------------------------------------------------------------------
  283. #if TERMINAL_SUPPORT
  284. void _wifiInitCommands() {
  285. settingsRegisterCommand(F("WIFI.RESET"), [](Embedis* e) {
  286. _wifiConfigure();
  287. wifiDisconnect();
  288. DEBUG_MSG_P(PSTR("+OK\n"));
  289. });
  290. settingsRegisterCommand(F("WIFI.AP"), [](Embedis* e) {
  291. wifiStartAP();
  292. DEBUG_MSG_P(PSTR("+OK\n"));
  293. });
  294. #if defined(JUSTWIFI_ENABLE_WPS)
  295. settingsRegisterCommand(F("WIFI.WPS"), [](Embedis* e) {
  296. wifiStartWPS();
  297. DEBUG_MSG_P(PSTR("+OK\n"));
  298. });
  299. #endif // defined(JUSTWIFI_ENABLE_WPS)
  300. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  301. settingsRegisterCommand(F("WIFI.SMARTCONFIG"), [](Embedis* e) {
  302. wifiStartSmartConfig();
  303. DEBUG_MSG_P(PSTR("+OK\n"));
  304. });
  305. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  306. settingsRegisterCommand(F("WIFI.SCAN"), [](Embedis* e) {
  307. _wifiScan();
  308. DEBUG_MSG_P(PSTR("+OK\n"));
  309. });
  310. }
  311. #endif
  312. // -----------------------------------------------------------------------------
  313. // WEB
  314. // -----------------------------------------------------------------------------
  315. #if WEB_SUPPORT
  316. bool _wifiWebSocketOnReceive(const char * key, JsonVariant& value) {
  317. if (strncmp(key, "wifi", 4) == 0) return true;
  318. return false;
  319. }
  320. void _wifiWebSocketOnSend(JsonObject& root) {
  321. root["maxNetworks"] = WIFI_MAX_NETWORKS;
  322. root["wifiScan"] = getSetting("wifiScan", WIFI_SCAN_NETWORKS).toInt() == 1;
  323. JsonArray& wifi = root.createNestedArray("wifi");
  324. for (byte i=0; i<WIFI_MAX_NETWORKS; i++) {
  325. if (!hasSetting("wifiName", i)) break;
  326. JsonObject& network = wifi.createNestedObject();
  327. network["wifiName"] = getSetting("wifiName", i, "");
  328. network["wifiPass"] = getSetting("wifiPass", i, "");
  329. network["wifiIP"] = getSetting("wifiIP", i, "");
  330. network["wifiGW"] = getSetting("wifiGW", i, "");
  331. network["wifiMask"] = getSetting("wifiMask", i, "");
  332. network["wifiDNS"] = getSetting("wifiDNS", i, "");
  333. }
  334. }
  335. void _wifiWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  336. if (strcmp(action, "scan") == 0) _wifi_scan_client_id = client_id;
  337. }
  338. #endif
  339. void _wifiBackwards() {
  340. moveSettings("ssid", "wifiName");
  341. moveSettings("pass", "wifiPass");
  342. moveSettings("ip", "wifiIP");
  343. moveSettings("gw", "wifiGW");
  344. moveSettings("mask", "wifiMask");
  345. moveSettings("dns", "wifiDNS");
  346. moveSetting("apmode", "wifiMode");
  347. }
  348. // -----------------------------------------------------------------------------
  349. // INFO
  350. // -----------------------------------------------------------------------------
  351. void wifiDebug(WiFiMode_t modes) {
  352. bool footer = false;
  353. if (((modes & WIFI_STA) > 0) && ((WiFi.getMode() & WIFI_STA) > 0)) {
  354. uint8_t * bssid = WiFi.BSSID();
  355. DEBUG_MSG_P(PSTR("[WIFI] ------------------------------------- MODE STA\n"));
  356. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  357. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  358. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  359. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  360. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  361. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  362. DEBUG_MSG_P(PSTR("[WIFI] HOST http://%s.local\n"), WiFi.hostname().c_str());
  363. DEBUG_MSG_P(PSTR("[WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X\n"),
  364. bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], bssid[6]
  365. );
  366. DEBUG_MSG_P(PSTR("[WIFI] CH %d\n"), WiFi.channel());
  367. DEBUG_MSG_P(PSTR("[WIFI] RSSI %d\n"), WiFi.RSSI());
  368. footer = true;
  369. }
  370. if (((modes & WIFI_AP) > 0) && ((WiFi.getMode() & WIFI_AP) > 0)) {
  371. DEBUG_MSG_P(PSTR("[WIFI] -------------------------------------- MODE AP\n"));
  372. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), getHostname().c_str());
  373. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getPassword().c_str());
  374. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  375. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  376. footer = true;
  377. }
  378. if (WiFi.getMode() == 0) {
  379. DEBUG_MSG_P(PSTR("[WIFI] ------------------------------------- MODE OFF\n"));
  380. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  381. footer = true;
  382. }
  383. if (footer) {
  384. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  385. }
  386. }
  387. void wifiDebug() {
  388. wifiDebug(WIFI_AP_STA);
  389. }
  390. // -----------------------------------------------------------------------------
  391. // API
  392. // -----------------------------------------------------------------------------
  393. String getIP() {
  394. if (WiFi.getMode() == WIFI_AP) {
  395. return WiFi.softAPIP().toString();
  396. }
  397. return WiFi.localIP().toString();
  398. }
  399. String getNetwork() {
  400. if (WiFi.getMode() == WIFI_AP) {
  401. return jw.getAPSSID();
  402. }
  403. return WiFi.SSID();
  404. }
  405. bool wifiConnected() {
  406. return jw.connected();
  407. }
  408. void wifiDisconnect() {
  409. jw.disconnect();
  410. }
  411. void wifiStartAP(bool only) {
  412. if (only) {
  413. jw.enableSTA(false);
  414. jw.disconnect();
  415. jw.resetReconnectTimeout();
  416. }
  417. jw.enableAP(true);
  418. }
  419. void wifiStartAP() {
  420. wifiStartAP(true);
  421. }
  422. #if defined(JUSTWIFI_ENABLE_WPS)
  423. void wifiStartWPS() {
  424. jw.startWPS();
  425. }
  426. #endif // defined(JUSTWIFI_ENABLE_WPS)
  427. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  428. void wifiStartSmartConfig() {
  429. jw.startSmartConfig();
  430. }
  431. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  432. void wifiReconnectCheck() {
  433. bool connected = false;
  434. #if WEB_SUPPORT
  435. if (wsConnected()) connected = true;
  436. #endif
  437. #if TELNET_SUPPORT
  438. if (telnetConnected()) connected = true;
  439. #endif
  440. jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL);
  441. }
  442. uint8_t wifiState() {
  443. uint8_t state = 0;
  444. if (jw.connected()) state += WIFI_STATE_STA;
  445. if (jw.connectable()) state += WIFI_STATE_AP;
  446. if (_wifi_wps_running) state += WIFI_STATE_WPS;
  447. if (_wifi_smartconfig_running) state += WIFI_STATE_SMARTCONFIG;
  448. return state;
  449. }
  450. void wifiRegister(wifi_callback_f callback) {
  451. jw.subscribe(callback);
  452. }
  453. // -----------------------------------------------------------------------------
  454. // INITIALIZATION
  455. // -----------------------------------------------------------------------------
  456. void wifiSetup() {
  457. WiFi.setSleepMode(WIFI_SLEEP_MODE);
  458. _wifiBackwards();
  459. _wifiInject();
  460. _wifiConfigure();
  461. // Message callbacks
  462. wifiRegister(_wifiCallback);
  463. #if WIFI_AP_CAPTIVE
  464. wifiRegister(_wifiCaptivePortal);
  465. #endif
  466. #if DEBUG_SUPPORT
  467. wifiRegister(_wifiDebugCallback);
  468. #endif
  469. #if WEB_SUPPORT
  470. wsOnSendRegister(_wifiWebSocketOnSend);
  471. wsOnReceiveRegister(_wifiWebSocketOnReceive);
  472. wsOnAfterParseRegister(_wifiConfigure);
  473. wsOnActionRegister(_wifiWebSocketOnAction);
  474. #endif
  475. #if TERMINAL_SUPPORT
  476. _wifiInitCommands();
  477. #endif
  478. // Register loop
  479. espurnaRegisterLoop(wifiLoop);
  480. }
  481. void wifiLoop() {
  482. // Main wifi loop
  483. jw.loop();
  484. // Process captrive portal DNS queries if in AP mode only
  485. #if WIFI_AP_CAPTIVE
  486. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  487. _wifi_dnsServer.processNextRequest();
  488. }
  489. #endif
  490. // Do we have a pending scan?
  491. if (_wifi_scan_client_id > 0) {
  492. _wifiScan(_wifi_scan_client_id);
  493. _wifi_scan_client_id = 0;
  494. }
  495. // Check if we should disable AP
  496. static unsigned long last = 0;
  497. if (millis() - last > 60000) {
  498. last = millis();
  499. _wifiCheckAP();
  500. }
  501. }