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.

650 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
7 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(getSetting("hostname").c_str());
  26. #if USE_PASSWORD
  27. jw.setSoftAP(getSetting("hostname").c_str(), getSetting("adminPass", ADMIN_PASS).c_str());
  28. #else
  29. jw.setSoftAP(getSetting("hostname").c_str());
  30. #endif
  31. jw.setConnectTimeout(WIFI_CONNECT_TIMEOUT);
  32. wifiReconnectCheck();
  33. jw.enableAPFallback(true);
  34. jw.cleanNetworks();
  35. _wifi_ap_mode = getSetting("apmode", 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 (getSetting("ssid" + String(i)).length() == 0) break;
  45. if (getSetting("ip" + String(i)).length() == 0) {
  46. jw.addNetwork(
  47. getSetting("ssid" + String(i)).c_str(),
  48. getSetting("pass" + String(i)).c_str()
  49. );
  50. } else {
  51. jw.addNetwork(
  52. getSetting("ssid" + String(i)).c_str(),
  53. getSetting("pass" + String(i)).c_str(),
  54. getSetting("ip" + String(i)).c_str(),
  55. getSetting("gw" + String(i)).c_str(),
  56. getSetting("mask" + String(i)).c_str(),
  57. getSetting("dns" + String(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("ssid", i)) {
  120. delSetting("ssid", i);
  121. break;
  122. }
  123. // Delete empty values
  124. if (!hasSetting("pass", i)) delSetting("pass", i);
  125. if (!hasSetting("ip", i)) delSetting("ip", i);
  126. if (!hasSetting("gw", i)) delSetting("gw", i);
  127. if (!hasSetting("mask", i)) delSetting("mask", i);
  128. if (!hasSetting("dns", i)) delSetting("dns", i);
  129. ++i;
  130. }
  131. // Delete all other settings
  132. while (i < WIFI_MAX_NETWORKS) {
  133. changed = hasSetting("ssid", i);
  134. delSetting("ssid", i);
  135. delSetting("pass", i);
  136. delSetting("ip", i);
  137. delSetting("gw", i);
  138. delSetting("mask", i);
  139. delSetting("dns", i);
  140. ++i;
  141. }
  142. return changed;
  143. }
  144. // Inject hardcoded networks
  145. void _wifiInject() {
  146. if (strlen(WIFI1_SSID)) {
  147. if (!hasSetting("ssid", 0)) {
  148. setSetting("ssid", 0, WIFI1_SSID);
  149. setSetting("pass", 0, WIFI1_PASS);
  150. setSetting("ip", 0, WIFI1_IP);
  151. setSetting("gw", 0, WIFI1_GW);
  152. setSetting("mask", 0, WIFI1_MASK);
  153. setSetting("dns", 0, WIFI1_DNS);
  154. }
  155. if (strlen(WIFI2_SSID)) {
  156. if (!hasSetting("ssid", 1)) {
  157. setSetting("ssid", 1, WIFI2_SSID);
  158. setSetting("pass", 1, WIFI2_PASS);
  159. setSetting("ip", 1, WIFI2_IP);
  160. setSetting("gw", 1, WIFI2_GW);
  161. setSetting("mask", 1, WIFI2_MASK);
  162. setSetting("dns", 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("ssid", count)) break;
  185. if (ssid.equals(getSetting("ssid", 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("ssid", count, ssid);
  191. setSetting("pass", 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. if (strncmp(key, "ssid", 4) == 0) return true;
  319. if (strncmp(key, "pass", 4) == 0) return true;
  320. if (strncmp(key, "ip", 2) == 0) return true;
  321. if (strncmp(key, "gw", 2) == 0) return true;
  322. if (strncmp(key, "mask", 4) == 0) return true;
  323. if (strncmp(key, "dns", 3) == 0) return true;
  324. return false;
  325. }
  326. void _wifiWebSocketOnSend(JsonObject& root) {
  327. root["maxNetworks"] = WIFI_MAX_NETWORKS;
  328. root["wifiScan"] = getSetting("wifiScan", WIFI_SCAN_NETWORKS).toInt() == 1;
  329. JsonArray& wifi = root.createNestedArray("wifi");
  330. for (byte i=0; i<WIFI_MAX_NETWORKS; i++) {
  331. if (!hasSetting("ssid", i)) break;
  332. JsonObject& network = wifi.createNestedObject();
  333. network["ssid"] = getSetting("ssid", i, "");
  334. network["pass"] = getSetting("pass", i, "");
  335. network["ip"] = getSetting("ip", i, "");
  336. network["gw"] = getSetting("gw", i, "");
  337. network["mask"] = getSetting("mask", i, "");
  338. network["dns"] = getSetting("dns", i, "");
  339. }
  340. }
  341. void _wifiWebSocketOnAction(uint32_t client_id, const char * action, JsonObject& data) {
  342. if (strcmp(action, "scan") == 0) _wifi_scan_client_id = client_id;
  343. }
  344. #endif
  345. // -----------------------------------------------------------------------------
  346. // INFO
  347. // -----------------------------------------------------------------------------
  348. void wifiDebug(WiFiMode_t modes) {
  349. bool footer = false;
  350. if (((modes & WIFI_STA) > 0) && ((WiFi.getMode() & WIFI_STA) > 0)) {
  351. uint8_t * bssid = WiFi.BSSID();
  352. DEBUG_MSG_P(PSTR("[WIFI] ------------------------------------- MODE STA\n"));
  353. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), WiFi.SSID().c_str());
  354. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.localIP().toString().c_str());
  355. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.macAddress().c_str());
  356. DEBUG_MSG_P(PSTR("[WIFI] GW %s\n"), WiFi.gatewayIP().toString().c_str());
  357. DEBUG_MSG_P(PSTR("[WIFI] DNS %s\n"), WiFi.dnsIP().toString().c_str());
  358. DEBUG_MSG_P(PSTR("[WIFI] MASK %s\n"), WiFi.subnetMask().toString().c_str());
  359. DEBUG_MSG_P(PSTR("[WIFI] HOST http://%s.local\n"), WiFi.hostname().c_str());
  360. DEBUG_MSG_P(PSTR("[WIFI] BSSID %02X:%02X:%02X:%02X:%02X:%02X\n"),
  361. bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5], bssid[6]
  362. );
  363. DEBUG_MSG_P(PSTR("[WIFI] CH %d\n"), WiFi.channel());
  364. DEBUG_MSG_P(PSTR("[WIFI] RSSI %d\n"), WiFi.RSSI());
  365. footer = true;
  366. }
  367. if (((modes & WIFI_AP) > 0) && ((WiFi.getMode() & WIFI_AP) > 0)) {
  368. DEBUG_MSG_P(PSTR("[WIFI] -------------------------------------- MODE AP\n"));
  369. DEBUG_MSG_P(PSTR("[WIFI] SSID %s\n"), getSetting("hostname").c_str());
  370. DEBUG_MSG_P(PSTR("[WIFI] PASS %s\n"), getSetting("adminPass", ADMIN_PASS).c_str());
  371. DEBUG_MSG_P(PSTR("[WIFI] IP %s\n"), WiFi.softAPIP().toString().c_str());
  372. DEBUG_MSG_P(PSTR("[WIFI] MAC %s\n"), WiFi.softAPmacAddress().c_str());
  373. footer = true;
  374. }
  375. if (WiFi.getMode() == 0) {
  376. DEBUG_MSG_P(PSTR("[WIFI] ------------------------------------- MODE OFF\n"));
  377. DEBUG_MSG_P(PSTR("[WIFI] No connection\n"));
  378. footer = true;
  379. }
  380. if (footer) {
  381. DEBUG_MSG_P(PSTR("[WIFI] ----------------------------------------------\n"));
  382. }
  383. }
  384. void wifiDebug() {
  385. wifiDebug(WIFI_AP_STA);
  386. }
  387. // -----------------------------------------------------------------------------
  388. // API
  389. // -----------------------------------------------------------------------------
  390. String getIP() {
  391. if (WiFi.getMode() == WIFI_AP) {
  392. return WiFi.softAPIP().toString();
  393. }
  394. return WiFi.localIP().toString();
  395. }
  396. String getNetwork() {
  397. if (WiFi.getMode() == WIFI_AP) {
  398. return jw.getAPSSID();
  399. }
  400. return WiFi.SSID();
  401. }
  402. bool wifiConnected() {
  403. return jw.connected();
  404. }
  405. void wifiDisconnect() {
  406. jw.disconnect();
  407. }
  408. void wifiStartAP(bool only) {
  409. if (only) {
  410. jw.enableSTA(false);
  411. jw.disconnect();
  412. jw.resetReconnectTimeout();
  413. }
  414. jw.enableAP(true);
  415. }
  416. void wifiStartAP() {
  417. wifiStartAP(true);
  418. }
  419. #if defined(JUSTWIFI_ENABLE_WPS)
  420. void wifiStartWPS() {
  421. jw.startWPS();
  422. }
  423. #endif // defined(JUSTWIFI_ENABLE_WPS)
  424. #if defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  425. void wifiStartSmartConfig() {
  426. jw.startSmartConfig();
  427. }
  428. #endif // defined(JUSTWIFI_ENABLE_SMARTCONFIG)
  429. void wifiReconnectCheck() {
  430. bool connected = false;
  431. #if WEB_SUPPORT
  432. if (wsConnected()) connected = true;
  433. #endif
  434. #if TELNET_SUPPORT
  435. if (telnetConnected()) connected = true;
  436. #endif
  437. jw.setReconnectTimeout(connected ? 0 : WIFI_RECONNECT_INTERVAL);
  438. }
  439. uint8_t wifiState() {
  440. uint8_t state = 0;
  441. if (jw.connected()) state += WIFI_STATE_STA;
  442. if (jw.connectable()) state += WIFI_STATE_AP;
  443. if (_wifi_wps_running) state += WIFI_STATE_WPS;
  444. if (_wifi_smartconfig_running) state += WIFI_STATE_SMARTCONFIG;
  445. return state;
  446. }
  447. void wifiRegister(wifi_callback_f callback) {
  448. jw.subscribe(callback);
  449. }
  450. // -----------------------------------------------------------------------------
  451. // INITIALIZATION
  452. // -----------------------------------------------------------------------------
  453. void wifiSetup() {
  454. WiFi.setSleepMode(WIFI_SLEEP_MODE);
  455. _wifiInject();
  456. _wifiConfigure();
  457. // Message callbacks
  458. wifiRegister(_wifiCallback);
  459. #if WIFI_AP_CAPTIVE
  460. wifiRegister(_wifiCaptivePortal);
  461. #endif
  462. #if DEBUG_SUPPORT
  463. wifiRegister(_wifiDebugCallback);
  464. #endif
  465. #if WEB_SUPPORT
  466. wsOnSendRegister(_wifiWebSocketOnSend);
  467. wsOnReceiveRegister(_wifiWebSocketOnReceive);
  468. wsOnAfterParseRegister(_wifiConfigure);
  469. wsOnActionRegister(_wifiWebSocketOnAction);
  470. #endif
  471. #if TERMINAL_SUPPORT
  472. _wifiInitCommands();
  473. #endif
  474. // Register loop
  475. espurnaRegisterLoop(wifiLoop);
  476. }
  477. void wifiLoop() {
  478. // Main wifi loop
  479. jw.loop();
  480. // Process captrive portal DNS queries if in AP mode only
  481. #if WIFI_AP_CAPTIVE
  482. if ((WiFi.getMode() & WIFI_AP) == WIFI_AP) {
  483. _wifi_dnsServer.processNextRequest();
  484. }
  485. #endif
  486. // Do we have a pending scan?
  487. if (_wifi_scan_client_id > 0) {
  488. _wifiScan(_wifi_scan_client_id);
  489. _wifi_scan_client_id = 0;
  490. }
  491. // Check if we should disable AP
  492. static unsigned long last = 0;
  493. if (millis() - last > 60000) {
  494. last = millis();
  495. _wifiCheckAP();
  496. }
  497. }