From e659c99a9a3d464aad3c3b2a7879f2e8e7e37f02 Mon Sep 17 00:00:00 2001 From: dannybackx Date: Sun, 12 Mar 2017 09:58:39 +0100 Subject: [PATCH] Experimental code to query MQTT client-id, and list of network SSIDs. --- cmd/cmd.h | 6 +++++ cmd/handlers.c | 53 ++++++++++++++++++++++++++++++++++++++++++ esp-link/cgimega.c | 2 +- esp-link/cgiwifi.c | 26 +++++++++++++++++++++ esp-link/cgiwifi.h | 3 +++ esp-link/mqtt_client.c | 21 +++++++++++++++++ 6 files changed, 110 insertions(+), 1 deletion(-) diff --git a/cmd/cmd.h b/cmd/cmd.h index a0abadf..c8af296 100644 --- a/cmd/cmd.h +++ b/cmd/cmd.h @@ -43,11 +43,13 @@ typedef enum { CMD_CB_EVENTS, CMD_GET_TIME, // get current time in seconds since the unix epoch CMD_GET_WIFI_INFO, // query ip address info + CMD_SET_WIFI_INFO, // set ip address info CMD_MQTT_SETUP = 10, // set-up callbacks CMD_MQTT_PUBLISH, // publish a message CMD_MQTT_SUBSCRIBE, // subscribe to a topic CMD_MQTT_LWT, // set the last-will-topic and messge + CMD_MQTT_GET_CLIENTID, CMD_REST_SETUP = 20, // set-up callbacks CMD_REST_REQUEST, // do REST request @@ -59,6 +61,10 @@ typedef enum { CMD_SOCKET_SETUP = 40, // set-up callbacks CMD_SOCKET_SEND, // send data over UDP socket + CMD_WIFI_GET_APCOUNT = 50, /* */ + CMD_WIFI_GET_APNAME, + CMD_WIFI_SELECT_SSID, + } CmdName; typedef void (*cmdfunc_t)(CmdPacket *cmd); diff --git a/cmd/handlers.c b/cmd/handlers.c index db2826c..4490d88 100644 --- a/cmd/handlers.c +++ b/cmd/handlers.c @@ -30,8 +30,14 @@ static void cmdSync(CmdPacket *cmd); static void cmdWifiStatus(CmdPacket *cmd); static void cmdGetTime(CmdPacket *cmd); static void cmdGetWifiInfo(CmdPacket *cmd); +static void cmdSetWifiInfo(CmdPacket *cmd); static void cmdAddCallback(CmdPacket *cmd); +static void cmdWifiGetApCount(CmdPacket *cmd); +static void cmdWifiGetApName(CmdPacket *cmd); + +void cmdMqttGetClientId(CmdPacket *cmd); + // keep track of last status sent to uC so we can notify it when it changes static uint8_t lastWifiStatus = wifiIsDisconnected; // keep track of whether we have registered our cb handler with the wifi subsystem @@ -47,11 +53,17 @@ const CmdList commands[] = { {CMD_CB_ADD, "ADD_CB", cmdAddCallback}, {CMD_GET_TIME, "GET_TIME", cmdGetTime}, {CMD_GET_WIFI_INFO, "GET_WIFI_INFO", cmdGetWifiInfo}, + {CMD_SET_WIFI_INFO, "SET_WIFI_INFO", cmdSetWifiInfo}, + + {CMD_WIFI_GET_APCOUNT,"WIFI_GET_APCOUNT", cmdWifiGetApCount}, + {CMD_WIFI_GET_APNAME, "WIFI_GET_APNAME", cmdWifiGetApName}, + #ifdef MQTT {CMD_MQTT_SETUP, "MQTT_SETUP", MQTTCMD_Setup}, {CMD_MQTT_PUBLISH, "MQTT_PUB", MQTTCMD_Publish}, {CMD_MQTT_SUBSCRIBE , "MQTT_SUB", MQTTCMD_Subscribe}, {CMD_MQTT_LWT, "MQTT_LWT", MQTTCMD_Lwt}, + {CMD_MQTT_GET_CLIENTID,"MQTT_CLIENTID", cmdMqttGetClientId}, #endif #ifdef REST {CMD_REST_SETUP, "REST_SETUP", REST_Setup}, @@ -208,6 +220,11 @@ cmdGetWifiInfo(CmdPacket *cmd) { cmdResponseEnd(); } +static void ICACHE_FLASH_ATTR +cmdSetWifiInfo(CmdPacket *cmd) { + os_printf("SetWifiInfo()\n"); +} + // Command handler to add a callback to the named-callbacks list, this is for a callback to the uC static void ICACHE_FLASH_ATTR cmdAddCallback(CmdPacket *cmd) { @@ -227,3 +244,39 @@ cmdAddCallback(CmdPacket *cmd) { cmdAddCb(name, cmd->value); // save the sensor callback } + +// Query the number of wifi access points +static void ICACHE_FLASH_ATTR +cmdWifiGetApCount(CmdPacket *cmd) { + int n = wifiGetApCount(); + os_printf("WifiGetApCount : %d\n", n); + cmdResponseStart(CMD_RESP_V, n, 0); + cmdResponseEnd(); +} + +// Query the name of a wifi access point +static void ICACHE_FLASH_ATTR +cmdWifiGetApName(CmdPacket *cmd) { + CmdRequest req; + + cmdRequest(&req, cmd); + + int argc = cmdGetArgc(&req); + os_printf("cmdWifiGetApName: argc %d\n", argc); + if (argc != 1) + return; + + uint16_t i; + cmdPopArg(&req, (uint8_t*)&i, 2); + + uint32_t callback = req.cmd->value; + + char myssid[33]; + wifiGetApName(i, myssid); + myssid[32] = '\0'; + os_printf("wifiGetApName(%d) -> {%s}\n", i, myssid); + + cmdResponseStart(CMD_RESP_CB, callback, 1); + cmdResponseBody(myssid, strlen(myssid)+1); + cmdResponseEnd(); +} diff --git a/esp-link/cgimega.c b/esp-link/cgimega.c index 15eb296..3ec9e7e 100644 --- a/esp-link/cgimega.c +++ b/esp-link/cgimega.c @@ -1368,7 +1368,7 @@ static bool ICACHE_FLASH_ATTR debug() { return false; #endif -#if 1 +#if 0 if (optibootData == 0) return false; if ((0x0000F000 <= optibootData->address) && (optibootData->address <= 0x00010200)) diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 70d40dc..9e79341 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -944,3 +944,29 @@ void ICACHE_FLASH_ATTR wifiInit() { os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } + +// Access functions for cgiWifiAps +int ICACHE_FLASH_ATTR wifiGetApCount() { + return cgiWifiAps.noAps; +} + +ICACHE_FLASH_ATTR void wifiGetApName(int i, char *ptr) { + if (i < 0) + return; + if (i >= cgiWifiAps.noAps) + return; + + if (ptr != 0) + strncpy(ptr, cgiWifiAps.apData[i]->ssid, 32); + + os_printf("AP %s\n", cgiWifiAps.apData[i]->ssid); +} + +// This may not belong here : called from cmd/handlers.c +// But it's good to have similar functionality close to each other. +// This performs functions similar to cgiWiFiConnect() +int ICACHE_FLASH_ATTR wifiConnect(char *ssid, char *pass) { +// Danny + return 0; +} + diff --git a/esp-link/cgiwifi.h b/esp-link/cgiwifi.h index 667f27d..0e1805c 100644 --- a/esp-link/cgiwifi.h +++ b/esp-link/cgiwifi.h @@ -24,4 +24,7 @@ int checkString(char *str); extern uint8_t wifiState; extern bool mdns_started; +int wifiGetApCount(); +void wifiGetApName(int i, char *ptr); + #endif diff --git a/esp-link/mqtt_client.c b/esp-link/mqtt_client.c index a9c1902..682dfee 100644 --- a/esp-link/mqtt_client.c +++ b/esp-link/mqtt_client.c @@ -4,6 +4,8 @@ #include "config.h" #include "mqtt.h" +#include "cmd/cmd.h" + #ifdef MQTTCLIENT_DBG #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #else @@ -114,4 +116,23 @@ mqtt_client_on_data(MqttDataCallback dataCb) { data_cb = dataCb; } +// Command handler for MQTT information +void ICACHE_FLASH_ATTR cmdMqttGetClientId(CmdPacket *cmd) { + CmdRequest req; + + cmdRequest(&req, cmd); + if(cmd->argc != 0 || cmd->value == 0) { + cmdResponseStart(CMD_RESP_V, 0, 0); + cmdResponseEnd(); + return; + } + + uint32_t callback = req.cmd->value; + + cmdResponseStart(CMD_RESP_CB, callback, 1); + cmdResponseBody(flashConfig.mqtt_clientid, strlen(flashConfig.mqtt_clientid)+1); + cmdResponseEnd(); + + os_printf("MqttGetClientId : %s\n", flashConfig.mqtt_clientid); +} #endif // MQTT