//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 cgiWiFiSetMode(HttpdConnData *connData) { int len; char buff[1024]; if (connData->conn==NULL) { //Connection aborted. Clean up. return HTTPD_CGI_DONE; } len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff)); if (len!=0) { os_printf("cgiWifiSetMode: %s\n", buff); #ifndef DEMO_MODE wifi_set_opmode(atoi(buff)); start_reset(); //system_restart(); #endif } httpdRedirect(connData, "/wifi"); return HTTPD_CGI_DONE; }
//Cgi that turns the Relays on or off according to the 'relayX' param in the GET data int ICACHE_FLASH_ATTR cgiGPIO(HttpdConnData *connData) { int len; char buff[128]; int gotcmd=0; if (connData->conn==NULL) { //Connection aborted. Clean up. return HTTPD_CGI_DONE; } len=httpdFindArg(connData->getArgs, "relay1", buff, sizeof(buff)); if (len>0) { currGPIO12State=atoi(buff); ioGPIO(currGPIO12State,12); gotcmd=1; //Manually switching relays means switching the thermostat off if(sysCfg.thermostat1state!=0) { sysCfg.thermostat1state=0; } } if(gotcmd==1) { if( sysCfg.relay_latching_enable) { sysCfg.relay_1_state=currGPIO12State; CFG_Save(); } httpdRedirect(connData, "relay.tpl"); return HTTPD_CGI_DONE; } else { //with no parameters returns JSON with relay state httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "text/json"); httpdHeader(connData, "Access-Control-Allow-Origin", "*"); httpdEndHeaders(connData); len=os_sprintf(buff, "{\"relay1\": %d\n,\"relay1name\":\"%s\"}\n", currGPIO12State,(char *)sysCfg.relay1name ); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; } }
int ICACHE_FLASH_ATTR ajaxConsoleBaud(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. char buff[512]; int len, status = 400; len = httpdFindArg(connData->getArgs, "rate", buff, sizeof(buff)); if (len > 0) { int rate = atoi(buff); if (rate >= 9600 && rate <= 1000000) { uart0_baud(rate); flashConfig.baud_rate = rate; status = configSave() ? 200 : 400; } } else if (connData->requestType == HTTPD_METHOD_GET) { status = 200; } jsonHeader(connData, status); os_sprintf(buff, "{\"rate\": %ld}", flashConfig.baud_rate); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; }
int ICACHE_FLASH_ATTR cgiJson(HttpdConnData *connData) { struct jsontree_context *json = (struct jsontree_context *)connData->cgiData; char buf[6]; if (connData->conn == NULL) { if (json) { free(json); } return HTTPD_CGI_DONE; } currentConnData = connData; if (json == NULL) { json = malloc(sizeof(struct jsontree_context)); jsontree_setup(json, (struct jsontree_value *)connData->cgiArg, httpdPutchar); if (httpdFindArg(connData->getArgs, "id", buf, sizeof(buf)) > 0) { json->index[JSONTREE_MAX_DEPTH - 1] = atoi(buf); } else { json->index[JSONTREE_MAX_DEPTH - 1] = 65535; } httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "application/json"); httpdEndHeaders(connData); connData->cgiData = json; } while (jsontree_print_next(json) && json->path <= json->depth) { if (connData->priv->sendBuffLen > MAX_SENDBUFF_LEN - (MAX_SENDBUFF_LEN / 4)) { return HTTPD_CGI_MORE; } } free(json); return HTTPD_CGI_DONE; }
int ICACHE_FLASH_ATTR ajaxLog(HttpdConnData *connData) { char buff[2048]; int len; // length of text in buff int start = 0; // offset onto log_wr to start sending out chars if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. jsonHeader(connData, 200); // figure out where to start in buffer based on URI param len = httpdFindArg(connData->getArgs, "start", buff, sizeof(buff)); if (len > 0) { start = atoi(buff); if (start == 0) { start = log_rd; } // show whole log } // start outputting int rd = start = start % BUF_MAX; len = os_sprintf(buff, "{\"len\":%d, \"start\":%d, \"text\": \"", (log_wr+BUF_MAX-start) % BUF_MAX, start); //int rd = (log_rd+start) % BUF_MAX; while (len < 2040 && rd != log_wr) { uint8_t c = log_buf[rd]; if (c == '\\' || c == '"') { buff[len++] = '\\'; buff[len++] = c; } else if (c < ' ') { len += os_sprintf(buff+len, "\\u%04x", c); } else { buff[len++] = c; } rd = (rd + 1) % BUF_MAX; } os_strcpy(buff+len, "\"}"); len+=2; httpdSend(connData, buff, len); return HTTPD_CGI_DONE; }
//This cgi changes the operating mode: STA / AP / STA+AP int ICACHE_FLASH_ATTR cgiWiFiSetMode(HttpdConnData *connData) { int len; char buff[1024]; if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff)); if (len!=0) { int m = atoi(buff); DBG("Wifi switching to mode %d\n", m); wifi_set_opmode(m&3); if (m == 1) { // STA-only mode, reset into STA+AP after a timeout if we don't get an IP address os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } jsonHeader(connData, 200); } else { jsonHeader(connData, 400); } return HTTPD_CGI_DONE; }
//This cgi changes the operating mode: STA / AP / STA+AP int ICACHE_FLASH_ATTR cgiWiFiSetMode(HttpdConnData *connData) { int len; char buff[1024]; int previous_mode = wifi_get_opmode(); if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff)); int next_mode = atoi(buff); if (len!=0) { if (next_mode == 2){ // moving to AP mode, so disconnect before leave STA mode wifi_station_disconnect(); } DBG("Wifi switching to mode %d\n", next_mode); wifi_set_opmode(next_mode&3); if (previous_mode == 2) { // moving to STA or STA+AP mode from AP, try to connect and set timer stconf.bssid_set = 0; wifi_station_set_config(&stconf); wifi_station_connect(); os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } if(previous_mode == 1){ // moving to AP or STA+AP from STA, so softap config call needed wifi_softap_set_config(&apconf); } jsonHeader(connData, 200); } else { jsonHeader(connData, 400); } return HTTPD_CGI_DONE; }
int ICACHE_FLASH_ATTR cgiEnv(HttpdConnData *connData) { char buff[2048]; int len=0; httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "application/json"); httpdHeader(connData, "Access-Control-Allow-Origin", "*"); httpdEndHeaders(connData); if (connData->conn==NULL) { //Connection aborted. Clean up. return HTTPD_CGI_DONE; } //os_strcpy(buff, "Unknown"); //os_strcpy(temp, "N/A"); //os_strcpy(humi, "N/A"); len=httpdFindArg(connData->getArgs, "param", buff, sizeof(buff)); if (len>0) { if(os_strcmp(buff,"temp")==0) { len=os_sprintf(buff, "%d",tempF); httpdSend(connData, buff, -1); tempF = tempF - 2; hudP = hudP + 2; os_printf("\nTemp val = %d\n",tempF); } if(os_strcmp(buff,"hud")==0) { len=os_sprintf(buff, "%d",hudP); httpdSend(connData, buff, -1); hudP = hudP - 2; tempF = tempF + 2; os_printf("\nHud val = %d\n",hudP); } } return HTTPD_CGI_DONE; }
// Change Soft-AP main settings int ICACHE_FLASH_ATTR cgiApSettingsChange(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. // No changes for Soft-AP in STA mode int mode = wifi_get_opmode(); if ( mode == 1 ){ jsonHeader(connData, 400); httpdSend(connData, "No changes allowed in STA mode", -1); return HTTPD_CGI_DONE; } char buff[96]; int len; // Check extra security measure, this must be 1 len=httpdFindArg(connData->getArgs, "100", buff, sizeof(buff)); if(len>0){ if(atoi(buff)!=1){ jsonHeader(connData, 400); return HTTPD_CGI_DONE; } } // Set new SSID len=httpdFindArg(connData->getArgs, "ap_ssid", buff, sizeof(buff)); if(checkString(buff) && len>0 && len<=32){ // STRING PREPROCESSING DONE IN CLIENT SIDE os_memset(apconf.ssid, 0, 32); os_memcpy(apconf.ssid, buff, len); apconf.ssid_len = len; }else{ jsonHeader(connData, 400); httpdSend(connData, "SSID not valid or out of range", -1); return HTTPD_CGI_DONE; } // Set new PASSWORD len=httpdFindArg(connData->getArgs, "ap_password", buff, sizeof(buff)); os_memset(apconf.password, 0, 64); if (checkString(buff) && len>7 && len<=64) { // String preprocessing done in client side, wifiap.js line 31 os_memcpy(apconf.password, buff, len); } else if (len != 0) { jsonHeader(connData, 400); httpdSend(connData, "PASSWORD not valid or out of range", -1); return HTTPD_CGI_DONE; } // Set auth mode if(len != 0){ // Set authentication mode, before password to check open settings len=httpdFindArg(connData->getArgs, "ap_authmode", buff, sizeof(buff)); if(len>0){ int value = atoi(buff); if(value >= 0 && value <= 4){ apconf.authmode = value; }else{ // If out of range set by default apconf.authmode = 4; } }else{ // Valid password but wrong auth mode, default 4 apconf.authmode = 4; } }else{ apconf.authmode = 0; } // Set max connection number len=httpdFindArg(connData->getArgs, "ap_maxconn", buff, sizeof(buff)); if(len>0){ int value = atoi(buff); if(value > 0 && value <= 4){ apconf.max_connection = value; }else{ // If out of range, set by default apconf.max_connection = 4; } } // Set beacon interval value len=httpdFindArg(connData->getArgs, "ap_beacon", buff, sizeof(buff)); if(len>0){ int value = atoi(buff); if(value >= 100 && value <= 60000){ apconf.beacon_interval = value; }else{ // If out of range, set by default apconf.beacon_interval = 100; } } // Set ssid to be hidden or not len=httpdFindArg(connData->getArgs, "ap_hidden", buff, sizeof(buff)); if(len>0){ int value = atoi(buff); if(value == 0 || value == 1){ apconf.ssid_hidden = value; }else{ // If out of range, set by default apconf.ssid_hidden = 0; } } // Store new configuration wifi_softap_set_config(&apconf); jsonHeader(connData, 200); return HTTPD_CGI_DONE; }