From 10e7816532e3f480c6eebb13e3925872d2982d81 Mon Sep 17 00:00:00 2001 From: dannybackx Date: Sat, 18 Mar 2017 16:01:34 +0100 Subject: [PATCH] Add code to query and set the current network (which access point/ssid to connect to). --- cmd/cmd.h | 1 + cmd/handlers.c | 41 +++++++++++++++++++++++++++++++++++++++++ esp-link/cgiservices.c | 1 + esp-link/cgiwifi.c | 41 ++++++++++++++++++++++++++++++++--------- esp-link/cgiwifi.h | 2 ++ esp-link/main.c | 1 + esp-link/status.c | 1 + 7 files changed, 79 insertions(+), 9 deletions(-) diff --git a/cmd/cmd.h b/cmd/cmd.h index 8471a29..0286c9a 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -65,6 +65,7 @@ typedef enum { CMD_WIFI_GET_APNAME, CMD_WIFI_SELECT_SSID, CMD_WIFI_SIGNAL_STRENGTH, // Query RSSI + CMD_WIFI_GET_SSID, // Query SSID currently connected to } CmdName; diff --git a/cmd/handlers.c b/cmd/handlers.c index 1739670..5b03a46 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -61,6 +61,7 @@ const CmdList commands[] = { {CMD_WIFI_GET_APNAME, "WIFI_GET_APNAME", cmdWifiGetApName}, {CMD_WIFI_SELECT_SSID, "WIFI_SELECT_SSID", cmdWifiSelectSSID}, {CMD_WIFI_SIGNAL_STRENGTH, "WIFI_SIGNAL_STRENGTH", cmdWifiSignalStrength}, + {CMD_WIFI_GET_SSID, "WIFI_GET_SSID", cmdWifiQuerySSID}, #ifdef MQTT {CMD_MQTT_SETUP, "MQTT_SETUP", MQTTCMD_Setup}, @@ -280,8 +281,48 @@ static void ICACHE_FLASH_ATTR cmdWifiGetApName(CmdPacket *cmd) { /* * Select a wireless network. + * This can be called in two ways : + * - with a pair of strings (SSID, password) + * - with a number and a string (index into network array, password) */ static void ICACHE_FLASH_ATTR cmdWifiSelectSSID(CmdPacket *cmd) { + CmdRequest req; + cmdRequest(&req, cmd); + int argc = cmdGetArgc(&req); + char *ssid, *pass; + + if (argc != 2) + return; + + int len = cmdArgLen(&req); + if (len == 1) { + // Assume this is the index + uint8_t ix; + cmdPopArg(&req, &ix, 1); + len = cmdArgLen(&req); + pass = (char *)os_malloc(len+2); + cmdPopArg(&req, pass, len); + pass[len] = 0; + + os_printf("SelectSSID(%d,%s)", ix, pass); + + char myssid[33]; + wifiGetApName(ix, myssid); + myssid[32] = '\0'; + connectToNetwork(myssid, pass); + } else { + ssid = os_malloc(len+2); + cmdPopArg(&req, ssid, len); + ssid[len] = 0; + + len = cmdArgLen(&req); + pass = (char *)os_malloc(len+2); + cmdPopArg(&req, pass, len); + pass[len] = 0; + + os_printf("SelectSSID(%s,%s)", ssid, pass); + connectToNetwork(ssid, pass); + } } /* diff --git a/esp-link/cgiservices.c b/esp-link/cgiservices.c index ef76287..96624b5 100644 --- a/esp-link/cgiservices.c +++ b/esp-link/cgiservices.c @@ -1,4 +1,5 @@ #include +#include "cmd.h" #include "cgiwifi.h" #include "cgi.h" #include "config.h" diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 4b1e3d1..2daf8b0 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -13,7 +13,9 @@ Cgi/template routines for the /wifi url. * ---------------------------------------------------------------------------- */ + #include +#include "cmd.h" #include "cgiwifi.h" #include "cgi.h" #include "status.h" @@ -34,7 +36,7 @@ bool mdns_started = false; // ===== wifi status change callbacks static WifiStateChangeCb wifi_state_change_cb[4]; -// Temp store for new staion config +// Temp store for new station config struct station_config stconf; // Temp store for new ap config @@ -360,6 +362,18 @@ static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) { os_timer_arm(&resetTimer, 4*RESET_TIMEOUT, 0); } +// Kick off connection to some network +void ICACHE_FLASH_ATTR connectToNetwork(char *ssid, char *pass) { + os_strncpy((char*)stconf.ssid, ssid, 32); + os_strncpy((char*)stconf.password, pass, 64); + DBG("Wifi try to connect to AP %s pw %s\n", ssid, pass); + + // Schedule disconnect/connect + os_timer_disarm(&reassTimer); + os_timer_setfn(&reassTimer, reassTimerCb, NULL); + os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it +} + // This cgi uses the routines above to connect to a specific access point with the // given ESSID using the given password. int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) { @@ -379,14 +393,8 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) { if (el > 0 && pl >= 0) { //Set to 0 if you want to disable the actual reconnecting bit - os_strncpy((char*)stconf.ssid, essid, 32); - os_strncpy((char*)stconf.password, passwd, 64); - DBG("Wifi try to connect to AP %s pw %s\n", essid, passwd); - - //Schedule disconnect/connect - os_timer_disarm(&reassTimer); - os_timer_setfn(&reassTimer, reassTimerCb, NULL); - os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it + + connectToNetwork(essid, passwd); jsonHeader(connData, 200); } else { jsonHeader(connData, 400); @@ -982,3 +990,18 @@ ICACHE_FLASH_ATTR int wifiSignalStrength(int i) { return rssi; } + +void ICACHE_FLASH_ATTR cmdWifiQuerySSID(CmdPacket *cmd) { + CmdRequest req; + cmdRequest(&req, cmd); + uint32_t callback = req.cmd->value; + + struct station_config conf; + bool res = wifi_station_get_config(&conf); + + os_printf("QuerySSID : %s\n", conf.ssid); + + cmdResponseStart(CMD_RESP_CB, callback, 1); + cmdResponseBody(conf.ssid, strlen(conf.ssid)+1); + cmdResponseEnd(); +} diff --git a/esp-link/cgiwifi.h b/esp-link/cgiwifi.h index 983974a..c142649 100644 --- a/esp-link/cgiwifi.h +++ b/esp-link/cgiwifi.h @@ -27,5 +27,7 @@ extern bool mdns_started; int wifiGetApCount(); void wifiGetApName(int, char *); int wifiSignalStrength(int); +void connectToNetwork(char *, char *); +void cmdWifiQuerySSID(CmdPacket *cmd); #endif diff --git a/esp-link/main.c b/esp-link/main.c index d9cd5f6..4124590 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -13,6 +13,7 @@ #include "httpd.h" #include "httpdespfs.h" #include "cgi.h" +#include "cmd.h" #include "cgiwifi.h" #include "cgipins.h" #include "cgitcp.h" diff --git a/esp-link/status.c b/esp-link/status.c index 6905850..a5ca8c1 100644 --- a/esp-link/status.c +++ b/esp-link/status.c @@ -3,6 +3,7 @@ #include #include "config.h" #include "serled.h" +#include "cmd.h" #include "cgiwifi.h" #ifdef MQTT