Handle part of TVE's input already.

pull/289/head
dannybackx 8 years ago
parent 99cb5732df
commit 23d5c1ef7a
  1. 70
      cmd/handlers.c
  2. 59
      esp-link/cgiwifi.c
  3. 20
      esp-link/mqtt_client.c

@ -19,6 +19,8 @@
#endif #endif
#include <ip_addr.h> #include <ip_addr.h>
#include "config.h"
#ifdef CMD_DBG #ifdef CMD_DBG
#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) #define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0)
#else #else
@ -93,7 +95,7 @@ CmdCallback callbacks[MAX_CALLBACKS]; // cleared in cmdSync
uint32_t ICACHE_FLASH_ATTR uint32_t ICACHE_FLASH_ATTR
cmdAddCb(char* name, uint32_t cb) { cmdAddCb(char* name, uint32_t cb) {
for (uint8_t i = 0; i < MAX_CALLBACKS; i++) { for (uint8_t i = 0; i < MAX_CALLBACKS; i++) {
//os_printf("cmdAddCb: index %d name=%s cb=%p\n", i, callbacks[i].name, //DBG("cmdAddCb: index %d name=%s cb=%p\n", i, callbacks[i].name,
// (void *)callbacks[i].callback); // (void *)callbacks[i].callback);
// find existing callback or add to the end // find existing callback or add to the end
if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0 || callbacks[i].name[0] == '\0') { if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0 || callbacks[i].name[0] == '\0') {
@ -110,7 +112,7 @@ cmdAddCb(char* name, uint32_t cb) {
CmdCallback* ICACHE_FLASH_ATTR CmdCallback* ICACHE_FLASH_ATTR
cmdGetCbByName(char* name) { cmdGetCbByName(char* name) {
for (uint8_t i = 0; i < MAX_CALLBACKS; i++) { for (uint8_t i = 0; i < MAX_CALLBACKS; i++) {
//os_printf("cmdGetCbByName: index %d name=%s cb=%p\n", i, callbacks[i].name, //DBG("cmdGetCbByName: index %d name=%s cb=%p\n", i, callbacks[i].name,
// (void *)callbacks[i].callback); // (void *)callbacks[i].callback);
// if callback doesn't exist or it's null // if callback doesn't exist or it's null
if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0) { if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0) {
@ -118,7 +120,7 @@ cmdGetCbByName(char* name) {
return &callbacks[i]; return &callbacks[i];
} }
} }
os_printf("cmdGetCbByName: cb %s not found\n", name); DBG("cmdGetCbByName: cb %s not found\n", name);
return 0; return 0;
} }
@ -249,7 +251,7 @@ cmdAddCallback(CmdPacket *cmd) {
// Query the number of wifi access points // Query the number of wifi access points
static void ICACHE_FLASH_ATTR cmdWifiGetApCount(CmdPacket *cmd) { static void ICACHE_FLASH_ATTR cmdWifiGetApCount(CmdPacket *cmd) {
int n = wifiGetApCount(); int n = wifiGetApCount();
os_printf("WifiGetApCount : %d\n", n); DBG("WifiGetApCount : %d\n", n);
cmdResponseStart(CMD_RESP_V, n, 0); cmdResponseStart(CMD_RESP_V, n, 0);
cmdResponseEnd(); cmdResponseEnd();
} }
@ -261,7 +263,7 @@ static void ICACHE_FLASH_ATTR cmdWifiGetApName(CmdPacket *cmd) {
cmdRequest(&req, cmd); cmdRequest(&req, cmd);
int argc = cmdGetArgc(&req); int argc = cmdGetArgc(&req);
os_printf("cmdWifiGetApName: argc %d\n", argc); DBG("cmdWifiGetApName: argc %d\n", argc);
if (argc != 1) if (argc != 1)
return; return;
@ -273,7 +275,7 @@ static void ICACHE_FLASH_ATTR cmdWifiGetApName(CmdPacket *cmd) {
char myssid[33]; char myssid[33];
wifiGetApName(i, myssid); wifiGetApName(i, myssid);
myssid[32] = '\0'; myssid[32] = '\0';
os_printf("wifiGetApName(%d) -> {%s}\n", i, myssid); DBG("wifiGetApName(%d) -> {%s}\n", i, myssid);
cmdResponseStart(CMD_RESP_CB, callback, 1); cmdResponseStart(CMD_RESP_CB, callback, 1);
cmdResponseBody(myssid, strlen(myssid)+1); cmdResponseBody(myssid, strlen(myssid)+1);
@ -301,18 +303,18 @@ static void ICACHE_FLASH_ATTR cmdWifiSelectSSID(CmdPacket *cmd) {
uint8_t ix; uint8_t ix;
cmdPopArg(&req, &ix, 1); cmdPopArg(&req, &ix, 1);
len = cmdArgLen(&req); len = cmdArgLen(&req);
pass = (char *)os_malloc(len+2); pass = (char *)os_malloc(len+1);
cmdPopArg(&req, pass, len); cmdPopArg(&req, pass, len);
pass[len] = 0; pass[len] = 0;
os_printf("SelectSSID(%d,%s)", ix, pass); DBG("SelectSSID(%d,%s)", ix, pass);
char myssid[33]; char myssid[33];
wifiGetApName(ix, myssid); wifiGetApName(ix, myssid);
myssid[32] = '\0'; myssid[32] = '\0';
connectToNetwork(myssid, pass); connectToNetwork(myssid, pass);
} else { } else {
ssid = os_malloc(len+2); ssid = os_malloc(len+1);
cmdPopArg(&req, ssid, len); cmdPopArg(&req, ssid, len);
ssid[len] = 0; ssid[len] = 0;
@ -321,7 +323,7 @@ static void ICACHE_FLASH_ATTR cmdWifiSelectSSID(CmdPacket *cmd) {
cmdPopArg(&req, pass, len); cmdPopArg(&req, pass, len);
pass[len] = 0; pass[len] = 0;
os_printf("SelectSSID(%s,%s)", ssid, pass); DBG("SelectSSID(%s,%s)", ssid, pass);
connectToNetwork(ssid, pass); connectToNetwork(ssid, pass);
} }
} }
@ -331,7 +333,7 @@ static void ICACHE_FLASH_ATTR cmdWifiSelectSSID(CmdPacket *cmd) {
* DHCP or so but set our own. * DHCP or so but set our own.
*/ */
static void ICACHE_FLASH_ATTR cmdSetWifiInfo(CmdPacket *cmd) { static void ICACHE_FLASH_ATTR cmdSetWifiInfo(CmdPacket *cmd) {
os_printf("SetWifiInfo()\n"); DBG("SetWifiInfo()\n");
} }
static void ICACHE_FLASH_ATTR cmdWifiSignalStrength(CmdPacket *cmd) { static void ICACHE_FLASH_ATTR cmdWifiSignalStrength(CmdPacket *cmd) {
@ -341,18 +343,58 @@ static void ICACHE_FLASH_ATTR cmdWifiSignalStrength(CmdPacket *cmd) {
int argc = cmdGetArgc(&req); int argc = cmdGetArgc(&req);
if (argc != 1) { if (argc != 1) {
os_printf("cmdWifiSignalStrength: argc %d\n", argc); DBG("cmdWifiSignalStrength: argc %d\n", argc);
return; return;
} }
char x; char x;
cmdPopArg(&req, (uint8_t*)&x, 1); cmdPopArg(&req, (uint8_t*)&x, 1);
int i = x; int i = x;
os_printf("cmdWifiSignalStrength: argc %d, ", argc); DBG("cmdWifiSignalStrength: argc %d, ", argc);
os_printf("i %d\n", i); DBG("i %d\n", i);
int rssi = wifiSignalStrength(i); int rssi = wifiSignalStrength(i);
cmdResponseStart(CMD_RESP_V, rssi, 0); cmdResponseStart(CMD_RESP_V, rssi, 0);
cmdResponseEnd(); cmdResponseEnd();
} }
//
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);
if (res) {
// #warning handle me
} else {
}
DBG("QuerySSID : %s\n", conf.ssid);
cmdResponseStart(CMD_RESP_CB, callback, 1);
cmdResponseBody(conf.ssid, strlen((char *)conf.ssid)+1);
cmdResponseEnd();
}
// 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);
}

@ -232,12 +232,10 @@ static void ICACHE_FLASH_ATTR scanStartCb(void *arg) {
static int ICACHE_FLASH_ATTR cgiWiFiStartScan(HttpdConnData *connData) { static int ICACHE_FLASH_ATTR cgiWiFiStartScan(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
jsonHeader(connData, 200); jsonHeader(connData, 200);
if (!cgiWifiAps.scanInProgress) {
cgiWifiAps.scanInProgress = 1; // Don't duplicate code, reuse the function below
os_timer_disarm(&scanTimer); cmdWifiStartScan(0);
os_timer_setfn(&scanTimer, scanStartCb, NULL);
os_timer_arm(&scanTimer, 200, 0);
}
return HTTPD_CGI_DONE; return HTTPD_CGI_DONE;
} }
@ -257,7 +255,7 @@ static int ICACHE_FLASH_ATTR cgiWiFiGetScan(HttpdConnData *connData) {
const int chunk = 1460/64; // ssid is up to 32 chars const int chunk = 1460/64; // ssid is up to 32 chars
int len = 0; int len = 0;
os_printf("GET scan: cgiData=%d noAps=%d\n", (int)connData->cgiData, cgiWifiAps.noAps); DBG("GET scan: cgiData=%d noAps=%d\n", (int)connData->cgiData, cgiWifiAps.noAps);
// handle continuation call, connData->cgiData-1 is the position in the scan results where we // handle continuation call, connData->cgiData-1 is the position in the scan results where we
// we need to continue sending from (using -1 'cause 0 means it's the first call) // we need to continue sending from (using -1 'cause 0 means it's the first call)
@ -444,12 +442,12 @@ static bool ICACHE_FLASH_ATTR parse_ip(char *buff, ip_addr_t *ip_ptr) {
static void ICACHE_FLASH_ATTR debugIP() { static void ICACHE_FLASH_ATTR debugIP() {
struct ip_info info; struct ip_info info;
if (wifi_get_ip_info(0, &info)) { if (wifi_get_ip_info(0, &info)) {
os_printf("\"ip\": \"%d.%d.%d.%d\"\n", IP2STR(&info.ip.addr)); DBG("\"ip\": \"%d.%d.%d.%d\"\n", IP2STR(&info.ip.addr));
os_printf("\"netmask\": \"%d.%d.%d.%d\"\n", IP2STR(&info.netmask.addr)); DBG("\"netmask\": \"%d.%d.%d.%d\"\n", IP2STR(&info.netmask.addr));
os_printf("\"gateway\": \"%d.%d.%d.%d\"\n", IP2STR(&info.gw.addr)); DBG("\"gateway\": \"%d.%d.%d.%d\"\n", IP2STR(&info.gw.addr));
os_printf("\"hostname\": \"%s\"\n", wifi_station_get_hostname()); DBG("\"hostname\": \"%s\"\n", wifi_station_get_hostname());
} else { } else {
os_printf("\"ip\": \"-none-\"\n"); DBG("\"ip\": \"-none-\"\n");
} }
} }
#endif #endif
@ -581,7 +579,7 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) {
if (checkString(buff) && len>7 && len<=64) { if (checkString(buff) && len>7 && len<=64) {
// String preprocessing done in client side, wifiap.js line 31 // String preprocessing done in client side, wifiap.js line 31
os_memcpy(apconf.password, buff, len); os_memcpy(apconf.password, buff, len);
os_printf("Setting AP password len=%d\n", len); DBG("Setting AP password len=%d\n", len);
} else if (len != 0) { } else if (len != 0) {
jsonHeader(connData, 400); jsonHeader(connData, 400);
httpdSend(connData, "PASSWORD not valid or out of range", -1); httpdSend(connData, "PASSWORD not valid or out of range", -1);
@ -597,18 +595,18 @@ int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) {
apconf.authmode = value; apconf.authmode = value;
} else { } else {
// If out of range set by default // If out of range set by default
os_printf("Forcing AP authmode to WPA_WPA2_PSK\n"); DBG("Forcing AP authmode to WPA_WPA2_PSK\n");
apconf.authmode = 4; apconf.authmode = 4;
} }
} else { } else {
// Valid password but wrong auth mode, default 4 // Valid password but wrong auth mode, default 4
os_printf("Forcing AP authmode to WPA_WPA2_PSK\n"); DBG("Forcing AP authmode to WPA_WPA2_PSK\n");
apconf.authmode = 4; apconf.authmode = 4;
} }
} else { } else {
apconf.authmode = 0; apconf.authmode = 0;
} }
os_printf("Setting AP authmode=%d\n", apconf.authmode); DBG("Setting AP authmode=%d\n", apconf.authmode);
// Set max connection number // Set max connection number
len=httpdFindArg(connData->getArgs, "ap_maxconn", buff, sizeof(buff)); len=httpdFindArg(connData->getArgs, "ap_maxconn", buff, sizeof(buff));
if(len>0){ if(len>0){
@ -965,13 +963,14 @@ void ICACHE_FLASH_ATTR wifiInit() {
os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0);
} }
// Access functions for cgiWifiAps // Access functions for cgiWifiAps : query the number of entries in the table
int ICACHE_FLASH_ATTR wifiGetApCount() { int ICACHE_FLASH_ATTR wifiGetApCount() {
if (cgiWifiAps.scanInProgress) if (cgiWifiAps.scanInProgress)
return 0; return 0;
return cgiWifiAps.noAps; return cgiWifiAps.noAps;
} }
// Access functions for cgiWifiAps : returns the name of a network, i is the index into the array, return stored in memory pointed to by ptr.
ICACHE_FLASH_ATTR void wifiGetApName(int i, char *ptr) { ICACHE_FLASH_ATTR void wifiGetApName(int i, char *ptr) {
if (i < 0) if (i < 0)
return; return;
@ -981,17 +980,10 @@ ICACHE_FLASH_ATTR void wifiGetApName(int i, char *ptr) {
if (ptr != 0) if (ptr != 0)
strncpy(ptr, cgiWifiAps.apData[i]->ssid, 32); strncpy(ptr, cgiWifiAps.apData[i]->ssid, 32);
os_printf("AP %s\n", cgiWifiAps.apData[i]->ssid); DBG("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;
} }
// Access functions for cgiWifiAps : returns the signal strength of network (i is index into array). Return current network strength for negative i.
ICACHE_FLASH_ATTR int wifiSignalStrength(int i) { ICACHE_FLASH_ATTR int wifiSignalStrength(int i) {
sint8 rssi; sint8 rssi;
@ -1004,18 +996,3 @@ ICACHE_FLASH_ATTR int wifiSignalStrength(int i) {
return rssi; 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((char *)conf.ssid)+1);
cmdResponseEnd();
}

@ -115,24 +115,4 @@ void ICACHE_FLASH_ATTR
mqtt_client_on_data(MqttDataCallback dataCb) { mqtt_client_on_data(MqttDataCallback dataCb) {
data_cb = 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 #endif // MQTT

Loading…
Cancel
Save