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.

658 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. void _wifiWebSocketOnSend(JsonObject& root) {
  317. root["maxNetworks"] = WIFI_MAX_NETWORKS;
  318. root["wifiScan"] = getSetting("wifiScan", WIFI_SCAN_NETWORKS).toInt() == 1;
  319. JsonArray& wifi = root.createNestedArray("wifi");
  320. for (byte i=0; i<WIFI_MAX_NETWORKS; i++) {
  321. if (!hasSetting("wifiName", i)) break;
  322. JsonObject& network = wifi.createNestedObject();
  323. network["wifiName"] = getSetting("wifiName", i, "");
  324. network["wifiPass"] = getSetting("wifiPass", i, "");
  325. network["wifiIP"] = getSetting("wifiIP", i, "");
  326. network["wifiGW"] = getSetting("wifiGW", i, "");
  327. network["wifiMask"] = getSetting("wifiMask", i, "");
  328. network["wifiDNS"] = getSetting("wifiDNS", i, "");
  329. }
  330. }
  331. void _wifiWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  332. if (strcmp(action, "scan") == 0) _wifi_scan_client_id = client_id;
  333. }
  334. #endif
  335. bool _wifiKeyCheck(const char * key) {
  336. return (strncmp(key, "wifi", 4) == 0);
  337. }
  338. void _wifiBackwards() {
  339. // 1.14.0 - 2018-06-27
  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. delSetting("wifiGain");
  348. }
  349. // -----------------------------------------------------------------------------
  350. // INFO
  351. // -----------------------------------------------------------------------------
  352. void wifiDebug(WiFiMode_t modes) {
  353. bool footer = false;
  354. if (((modes & WIFI_STA) > 0) && ((WiFi.getMode() & WIFI_STA) > 0)) {
  355. uint8_t * bssid = WiFi.BSSID();
  356. DEBUG_MSG_P(PSTR("[WIFI] ------------------------------------- MODE STA\n"));
  357. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  358. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  359. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  360. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  361. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  362. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  363. DEBUG_MSG_P(PSTR("[WIFI] HOST http://%s.local\n"), WiFi.hostname().c_str());
  364. DEBUG_MSG_P(PSTR("[WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X\n"),
  365. bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], bssid[6]
  366. );
  367. DEBUG_MSG_P(PSTR("[WIFI] CH %d\n"), WiFi.channel());
  368. DEBUG_MSG_P(PSTR("[WIFI] RSSI %d\n"), WiFi.RSSI());
  369. footer = true;
  370. }
  371. if (((modes & WIFI_AP) > 0) && ((WiFi.getMode() & WIFI_AP) > 0)) {
  372. DEBUG_MSG_P(PSTR("[WIFI] -------------------------------------- MODE AP\n"));
  373. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), getHostname().c_str());
  374. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getPassword().c_str());
  375. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  376. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  377. footer = true;
  378. }
  379. if (WiFi.getMode() == 0) {
  380. DEBUG_MSG_P(PSTR("[WIFI] ------------------------------------- MODE OFF\n"));
  381. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  382. footer = true;
  383. }
  384. if (footer) {
  385. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  386. }
  387. }
  388. void wifiDebug() {
  389. wifiDebug(WIFI_AP_STA);
  390. }
  391. // -----------------------------------------------------------------------------
  392. // API
  393. // -----------------------------------------------------------------------------
  394. String getIP() {
  395. if (WiFi.getMode() == WIFI_AP) {
  396. return WiFi.softAPIP().toString();
  397. }
  398. return WiFi.localIP().toString();
  399. }
  400. String getNetwork() {
  401. if (WiFi.getMode() == WIFI_AP) {
  402. return jw.getAPSSID();
  403. }
  404. return WiFi.SSID();
  405. }
  406. bool wifiConnected() {
  407. return jw.connected();
  408. }
  409. void wifiDisconnect() {
  410. jw.disconnect();
  411. }
  412. void wifiStartAP(bool only) {
  413. if (only) {
  414. jw.enableSTA(false);
  415. jw.disconnect();
  416. jw.resetReconnectTimeout();
  417. }
  418. jw.enableAP(true);
  419. }
  420. void wifiStartAP() {
  421. wifiStartAP(true);
  422. }
  423. #if defined(JUSTWIFI_ENABLE_WPS)
  424. void wifiStartWPS() {
  425. jw.startWPS();
  426. }
  427. #endif // defined(JUSTWIFI_ENABLE_WPS)
  428. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  429. void wifiStartSmartConfig() {
  430. jw.startSmartConfig();
  431. }
  432. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  433. void wifiReconnectCheck() {
  434. bool connected = false;
  435. #if WEB_SUPPORT
  436. if (wsConnected()) connected = true;
  437. #endif
  438. #if TELNET_SUPPORT
  439. if (telnetConnected()) connected = true;
  440. #endif
  441. jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL);
  442. }
  443. uint8_t wifiState() {
  444. uint8_t state = 0;
  445. if (jw.connected()) state += WIFI_STATE_STA;
  446. if (jw.connectable()) state += WIFI_STATE_AP;
  447. if (_wifi_wps_running) state += WIFI_STATE_WPS;
  448. if (_wifi_smartconfig_running) state += WIFI_STATE_SMARTCONFIG;
  449. return state;
  450. }
  451. void wifiRegister(wifi_callback_f callback) {
  452. jw.subscribe(callback);
  453. }
  454. // -----------------------------------------------------------------------------
  455. // INITIALIZATION
  456. // -----------------------------------------------------------------------------
  457. void wifiSetup() {
  458. WiFi.setSleepMode(WIFI_SLEEP_MODE);
  459. _wifiBackwards();
  460. _wifiInject();
  461. _wifiConfigure();
  462. // Message callbacks
  463. wifiRegister(_wifiCallback);
  464. #if WIFI_AP_CAPTIVE
  465. wifiRegister(_wifiCaptivePortal);
  466. #endif
  467. #if DEBUG_SUPPORT
  468. wifiRegister(_wifiDebugCallback);
  469. #endif
  470. #if WEB_SUPPORT
  471. wsOnSendRegister(_wifiWebSocketOnSend);
  472. wsOnAfterParseRegister(_wifiConfigure);
  473. wsOnActionRegister(_wifiWebSocketOnAction);
  474. #endif
  475. #if TERMINAL_SUPPORT
  476. _wifiInitCommands();
  477. #endif
  478. settingsRegisterKeyCheck(_wifiKeyCheck);
  479. // Register loop
  480. espurnaRegisterLoop(wifiLoop);
  481. }
  482. void wifiLoop() {
  483. // Main wifi loop
  484. jw.loop();
  485. // Process captrive portal DNS queries if in AP mode only
  486. #if WIFI_AP_CAPTIVE
  487. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  488. _wifi_dnsServer.processNextRequest();
  489. }
  490. #endif
  491. // Do we have a pending scan?
  492. if (_wifi_scan_client_id > 0) {
  493. _wifiScan(_wifi_scan_client_id);
  494. _wifi_scan_client_id = 0;
  495. }
  496. // Check if we should disable AP
  497. static unsigned long last = 0;
  498. if (millis() - last > 60000) {
  499. last = millis();
  500. _wifiCheckAP();
  501. }
  502. }