示例#1
0
int http_menu_api_get(http_connection *c) {
   CGI_MENU_DBG("http_menu_api_get\n");
   //wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
      return HTTPD_CGI_MORE;

   // set cache to 1 hour
   http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
   http_SET_HEADER(c,HTTP_CACHE_CONTROL,HTTP_DEFAULT_CACHE);
   http_response_OK(c);

   //create json
   cJSON *root, *array;
   root = cJSON_CreateObject();
   cJSON_AddItemToObject(root, "menu", array = cJSON_CreateArray());
   cJSON_AddStringToObject(array,"Index","Index");
   cJSON_AddStringToObject(array,"Index","/index.html");
   cJSON_AddStringToObject(array,"Wifi","Wifi");
   cJSON_AddStringToObject(array,"Wifi","/wifi.html");
   cJSON_AddStringToObject(array,"Console","Console");
   cJSON_AddStringToObject(array,"Console","/console.html");
   cJSON_AddStringToObject(array,"RFM OTA","RFM OTA");
   cJSON_AddStringToObject(array,"RFM OTA","/fw.html");

    cJSON_AddStringToObject(root,"version", esp_rfm69_version);

    http_write_json(c,root);

    //delete json struct
    cJSON_Delete(root);

    return HTTPD_CGI_DONE;
}
示例#2
0
//TODO move to own file
int ICACHE_FLASH_ATTR http_dht_api_read(http_connection *c) {


	NODE_DBG("http_dht_api_read");
		
	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE; 	

	//write headers
	http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);	
	http_response_OK(c);

	dht_data data = dht22_read();

	write_json_object_start(c);
	write_json_pair_float(c,"temp",data.temp);
	write_json_list_separator(c);
	write_json_pair_float(c,"hum",data.hum);	
	write_json_object_end(c);

			
	return HTTPD_CGI_DONE;


}
示例#3
0
//TODO move to own file
int ICACHE_FLASH_ATTR http_dht_api_read(http_connection *c) {


	NODE_DBG("http_dht_api_read");
		
	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE; 	

	//write headers
	http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);	
	http_response_OK(c);

	sensor_data data;
	sensors_get_data(&data);
		
	//create json
	cJSON *root = cJSON_CreateObject();
	cJSON_AddNumberToObject(root,"temp",data.dht22.temp);
	cJSON_AddNumberToObject(root,"hum",data.dht22.hum);

	//write json
	http_write_json(c,root);

	//delete json struct
	cJSON_Delete(root);
				
	return HTTPD_CGI_DONE;


}
示例#4
0
int http_wifi_api_get_status(http_connection *c)
{
    CGI_WIFI_DBG("wifi get_status\n");

    //wait for whole body
    if(c->state <HTTPD_STATE_BODY_END) {
        return HTTPD_CGI_MORE;
    }

    api_cgi_status * status = c->cgi.data;

    //first call, send headers
    if(status==NULL)
    {
        status = (api_cgi_status*)os_malloc(sizeof(api_cgi_status));
        status->state=1;
        c->cgi.data=status;

        http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
        http_response_OK(c);

        return HTTPD_CGI_MORE;
    }
    else if(status->state==1)
    {
        wifi_station_get_config(&wifi_status.station_config);
        uint8_t c_status = wifi_station_get_connect_status();

        //return JSON
        cJSON *root = cJSON_CreateObject();
        cJSON_AddBoolToObject(root,"scanning",wifi_status.scanning);
        cJSON_AddStringToObject(root,"ssid",(const char *)wifi_status.station_config.ssid);
        cJSON_AddNumberToObject(root,"mode",wifi_get_opmode());
        cJSON_AddNumberToObject(root,"station_status",c_status);

        //got ip
        if(c_status==5)
        {
            struct ip_info ip;
            wifi_get_ip_info(0x0,&ip);
            char *ip_str = (char*)ipaddr_ntoa(&ip.ip);
            cJSON_AddStringToObject(root,"ip",ip_str);
        }
        else {
            cJSON_AddStringToObject(root,"ip","");
        }

        http_write_json(c,root);
        cJSON_Delete(root);

        status->state=99;
        return HTTPD_CGI_MORE;
    }
    else
    {
        os_free(c->cgi.data);
        return HTTPD_CGI_DONE;
    }
}
示例#5
0
int http_console_api_clear(http_connection *c)
{
	CGI_CONS_DBG("api_clear\n");
	http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
	http_response_OK(c);
	console_rd = console_wr = console_pos = 0;
	return HTTPD_CGI_DONE;
}
示例#6
0
int ICACHE_FLASH_ATTR http_relay_api_toggle(http_connection *c) {


	NODE_DBG("http_wifi_api_get_status");	
	

	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE; 

	//parse json
	cJSON * root = cJSON_Parse(c->body.data);
	if(root==NULL) goto badrequest;
	
	cJSON * relay = cJSON_GetObjectItem(root,"relay");
	if(relay==NULL) goto badrequest;


	int relayNumber = relay->valueint;
	cJSON_Delete(root);	

	if(relayNumber<0 || relayNumber >=relay_count()){
		http_response_BAD_REQUEST(c);
		NODE_DBG("Wrong relay");		
		return HTTPD_CGI_DONE;
	}
	else{
		//valid relay

		unsigned status = relay_get_state(relayNumber);
		status = relay_toggle_state(relayNumber);

		//write headers
		http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);	
		http_response_OK(c);


		//create json
		root = cJSON_CreateObject();
		cJSON_AddNumberToObject(root,"relay",relayNumber);
		cJSON_AddNumberToObject(root,"state",status);

		http_write_json(c,root);

		//delete json struct
		cJSON_Delete(root);

		return HTTPD_CGI_DONE;

	}	

badrequest:
	http_response_BAD_REQUEST(c);
	return HTTPD_CGI_DONE;

}
示例#7
0
int ICACHE_FLASH_ATTR http_relay_api_toggle(http_connection *c) {


	NODE_DBG("http_wifi_api_get_status");	
	

	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE; 

	
	int ret = json_count_token(c->body.data,c->body.len);
	
	jsonPair fields[1]={
		{
			.key="relay",
			.value=NULL
		}
	};

	if(json_parse(&fields[0],1,c->body.data,c->body.len)){
		
		int relayNumber = atoi(fields[0].value);	

		if(relayNumber<0 || relayNumber >=relay_count()){
			http_response_BAD_REQUEST(c);
			NODE_DBG("Wrong relay");		
			return HTTPD_CGI_DONE;
		}

		unsigned status = relay_get_state(relayNumber);
		status = relay_toggle_state(relayNumber);

		//write headers
		http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);	
		http_response_OK(c);

		write_json_object_start(c);
		write_json_pair_int(c,"relay",relayNumber);
		write_json_list_separator(c);
		write_json_pair_int(c,"state",status);
		write_json_object_end(c);

		return HTTPD_CGI_DONE;

	}
	else{
		http_response_BAD_REQUEST(c);		
		return HTTPD_CGI_DONE;
	}

	http_response_BAD_REQUEST(c);		
	return HTTPD_CGI_DONE;


}
示例#8
0
//cgi that adds CORS ( Cross Origin Resource Sharing ) to our server
int ICACHE_FLASH_ATTR cgi_cors(http_connection *connData) {
	
	http_server_config *config = (http_server_config*)connData->cgi.argument;;
	if(config==NULL)
		return HTTPD_CGI_NEXT_RULE;

	if(!config->enable_cors)
		return HTTPD_CGI_NEXT_RULE;


	if(connData->state==HTTPD_STATE_ON_URL){

		//save cors headers 
		http_set_save_header(connData,HTTP_ACCESS_CONTROL_REQUEST_HEADERS);
		http_set_save_header(connData,HTTP_ACCESS_CONTROL_REQUEST_METHOD);
		
		return HTTPD_CGI_NEXT_RULE;
	}
	
	if(connData->state==HTTPD_STATE_HEADERS_END){

		//SET CORS Allow Origin for every request
		http_SET_HEADER(connData,HTTP_ACCESS_CONTROL_ALLOW_ORIGIN,"*");

		header * allow_headers = http_get_header(connData,HTTP_ACCESS_CONTROL_REQUEST_HEADERS);
		header * allow_methods = http_get_header(connData,HTTP_ACCESS_CONTROL_REQUEST_METHOD);
		
		if(allow_headers!=NULL)
			http_SET_HEADER(connData,HTTP_ACCESS_CONTROL_ALLOW_HEADERS,allow_headers->value);
		if(allow_methods!=NULL)
			http_SET_HEADER(connData,HTTP_ACCESS_CONTROL_ALLOW_METHODS,allow_methods->value);

		// Browsers will send an OPTIONS pre-flight request when posting data on a cross-domain situation
		// If that's the case here, we can safely return 200 OK with our CORS headers set
		if(connData->parser.method==HTTP_OPTIONS)
		{
			http_response_OK(connData);
			return HTTPD_CGI_DONE;
		}
	}

	return HTTPD_CGI_NEXT_RULE;
}
示例#9
0
int ICACHE_FLASH_ATTR http_relay_api_status(http_connection *c) {


	NODE_DBG("http_wifi_api_get_status");
		
	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE; 	

	//write headers
	http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);	
	http_response_OK(c);

	write_json_object_start(c);
	write_json_key(c,"relays");
	write_json_object_separator(c);
	write_json_array_start(c);

	int i;
	for(i=0;i<relay_count();i++){

		if(i>0)
			write_json_list_separator(c);

		write_json_object_start(c);
		write_json_pair_int(c,"relay",i);
		write_json_list_separator(c);
		write_json_pair_int(c,"state",relay_get_state(i));
		write_json_object_end(c);

		
	}
	
	write_json_array_end(c);
	write_json_object_end(c);
	

			
	return HTTPD_CGI_DONE;


}
示例#10
0
int http_wifi_api_disconnect(http_connection *c)
{
    CGI_WIFI_DBG("http_wifi_disconnect\n");

    //wait for whole body
    if (c->state <HTTPD_STATE_BODY_END) {
        return HTTPD_CGI_MORE;
    }

    //reset wifi cfg
    strcpy(wifi_status.station_config.ssid,"");
    strcpy(wifi_status.station_config.password,"");
    wifi_status.station_config.bssid_set=0;

    wifi_station_set_config(&wifi_status.station_config);
    wifi_station_disconnect();
    wifi_station_set_auto_connect(false);

    http_response_OK(c);
    return HTTPD_CGI_DONE;
}
示例#11
0
int ICACHE_FLASH_ATTR http_wifi_api_disconnect(http_connection *c){

	NODE_DBG("http_wifi_disconnect");

	
	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE; 
	

	//reset wifi cfg
	strcpy(wifi_status.station_config.ssid,"");
	strcpy(wifi_status.station_config.password,"");
	wifi_status.station_config.bssid_set=0;

	wifi_station_disconnect();
	wifi_station_set_config(&wifi_status.station_config);

	http_response_OK(c);
	return HTTPD_CGI_DONE;
}
示例#12
0
int ICACHE_FLASH_ATTR http_relay_api_status(http_connection *c) {


	NODE_DBG("http_wifi_api_get_status");
		
	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE; 	

	//write headers
	http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);	
	http_response_OK(c);

	cJSON *root, *relays, *fld;

	root = cJSON_CreateObject(); 
	cJSON_AddItemToObject(root, "relays", relays = cJSON_CreateArray());
	
	int i;
	for(i=0;i<relay_count();i++){

		cJSON_AddItemToArray(relays,fld=cJSON_CreateObject());
		cJSON_AddNumberToObject(fld,"relay",i);
		cJSON_AddNumberToObject(fld,"state",relay_get_state(i));
		
	}

	http_write_json(c,root);	

	//delete json struct
	cJSON_Delete(root);

	return HTTPD_CGI_DONE;


}
示例#13
0
int http_wifi_api_get_info(http_connection *c)
{
    CGI_WIFI_DBG("http_wifi_api_get_info\n");

    //wait for whole body
    if(c->state <HTTPD_STATE_BODY_END) {
        return HTTPD_CGI_MORE;
    }

    api_cgi_status * status = c->cgi.data;

    if(status==NULL) { //first call, send headers

        status = (api_cgi_status*)os_malloc(sizeof(api_cgi_status));
        status->state=1;
        c->cgi.data=status;

        http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
        http_response_OK(c);

        return HTTPD_CGI_MORE;
    }
    else if(status->state==1)
    {
        //json data
        char mac[20];
        wifi_station_get_config(&wifi_status.station_config);
        char *wifistatus = "unknown";
        uint8_t c_status = wifi_station_get_connect_status();
        if (c_status >= 0 && c_status < sizeof(connStatuses)) wifistatus = connStatuses[c_status];
        int p = wifi_get_phy_mode();
        char *phy = wifiPhy[p&3];
        sint8 rssi = wifi_station_get_rssi();
        if (rssi > 0) rssi = 0;
        uint8_t op = wifi_get_opmode() & 0x3;
        char *warn = wifiWarn[op];
        uint8 mac_addr[6];
        wifi_get_macaddr(0, mac_addr);
        uint8_t chan = wifi_get_channel();

        char *hostname = wifi_station_get_hostname();
        os_sprintf(mac,"%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1],
                   mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);

        cJSON *root = cJSON_CreateObject();
        cJSON_AddNumberToObject(root,"mode",wifi_get_opmode());
        cJSON_AddStringToObject(root,"modechange", MODECHANGE);
        cJSON_AddStringToObject(root,"ssid", (const char *)wifi_status.station_config.ssid);
        cJSON_AddStringToObject(root,"status", (const char *)wifistatus);
        cJSON_AddStringToObject(root,"phy", (const char *)phy);
        cJSON_AddNumberToObject(root,"rssi", rssi);
        cJSON_AddStringToObject(root,"warn", (const char *)warn);
        cJSON_AddStringToObject(root,"mac", (const char *)mac);
        cJSON_AddNumberToObject(root,"chan", chan);
        cJSON_AddStringToObject(root,"hostname", (const char *)hostname);
        cJSON_AddStringToObject(root,"domain", (const char *)INTERFACE_DOMAIN);

        // got ip
        if(c_status==5)
        {
            struct ip_info ip;
            wifi_get_ip_info(0x0,&ip);
            char *ip_str = (char*)ipaddr_ntoa(&ip.ip);
            cJSON_AddStringToObject(root,"ip",ip_str);
        }
        else {
            cJSON_AddStringToObject(root,"ip","");
        }

        http_write_json(c,root);
        cJSON_Delete(root);

        status->state=99;
        return HTTPD_CGI_MORE;
    }
    else
    {
        os_free(c->cgi.data);
        return HTTPD_CGI_DONE;
    }
}
示例#14
0
int http_wifi_api_check_internet(http_connection *c)
{
    CGI_WIFI_DBG("check_internet\n");

    //wait for whole body
    if(c->state <HTTPD_STATE_BODY_END) {
        return HTTPD_CGI_MORE;
    }

    api_cgi_check_internet_status * status = (api_cgi_check_internet_status *)c->cgi.data;
    //first call, send headers
    if(status==NULL)
    {
        CGI_WIFI_DBG("check_internet begin\n");
        status = (api_cgi_check_internet_status*)os_malloc(sizeof(api_cgi_check_internet_status));
        status->state=1;
        c->cgi.data=status;

        http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
        http_response_OK(c);

        status->http_client = http_client_new(http_wifi_api_check_internet_cb);
        http_client_GET(status->http_client,"http://www.msftncsi.com/ncsi.txt");

        status->http_client->reverse = c; //mark reverse so we can find on callback
        c->reverse=&status->http_client; //reverse other way around

        return HTTPD_CGI_MORE;
    }
    else if (status->state==1)
    {
        // just signal we aren't finished
        CGI_WIFI_DBG("check_internet 1\n");
        return HTTPD_CGI_MORE;
    }
    else if(status->state==2)
    {
        //DNS FOUND
        CGI_WIFI_DBG("check_internet 2\n");

        status->state=99;

        //create json
        cJSON *root = cJSON_CreateObject();
        cJSON_AddNumberToObject(root,"status",1);

        http_write_json(c,root);
        //delete json struct
        cJSON_Delete(root);

        return HTTPD_CGI_MORE;
    }
    else if (status->state==3)
    {
        //DNS NOT FOUND
        status->state=99;

        //create json
        cJSON *root = cJSON_CreateObject();
        cJSON_AddNumberToObject(root,"status",0);

        http_write_json(c,root);
        //delete json struct
        cJSON_Delete(root);

        return HTTPD_CGI_MORE;
    }
    else
    {
        CGI_WIFI_DBG("check_internet done\n");
        os_free(c->cgi.data);
        return HTTPD_CGI_DONE;
    }
}
示例#15
0
int http_wifi_api_connect_ap(http_connection *c)
{
    CGI_WIFI_DBG("http_wifi_api_connect_ap\n");

    //wait for whole body
    if(c->state <HTTPD_STATE_BODY_END) {
        return HTTPD_CGI_MORE;
    }

    api_cgi_connect_status * status = c->cgi.data;

    if(status==NULL)
    {
        // CGI_WIFI_DBG("http_wifi_api_connect_ap status NULL\n");

        status = (api_cgi_connect_status*)os_malloc(sizeof(api_cgi_connect_status));
        status->state=1;
        c->cgi.data=status;

        //parse json and validate
        cJSON * root = cJSON_Parse(c->body.data);
        if(root==NULL) goto badrequest;

        cJSON * ssid = cJSON_GetObjectItem(root,"ssid");
        if(ssid==NULL) goto badrequest;
        else if(ssid->type != cJSON_String) goto badrequest;

        cJSON * pwd = cJSON_GetObjectItem(root,"pwd");
        if(pwd==NULL) goto badrequest;
        else if(pwd->type!=cJSON_String) goto badrequest;

        //parse ok
        strncpy(status->ssid,ssid->valuestring,32);
        strncpy(status->pwd,pwd->valuestring,64);

        //set timer to connect
        os_timer_disarm(&status->timer);
        os_timer_setfn(&status->timer, (os_timer_func_t *)http_execute_cgi, c);
        os_timer_arm(&status->timer, 10, 0);

        return HTTPD_CGI_MORE;
    }
    else if (status->state==1)
    {
        CGI_WIFI_DBG("connect_ap status %d\n",status->state);
        //try connect

        if(strlen(status->ssid)>32 || strlen(status->pwd)>64)
            goto badrequest;

        CGI_WIFI_DBG("ap connect ssid: %s, pwd: %s\n",status->ssid,status->pwd);

        strcpy(wifi_status.station_config.ssid,status->ssid);
        strcpy(wifi_status.station_config.password,status->pwd);
        wifi_status.station_config.bssid_set=0;

        wifi_station_disconnect();
        wifi_station_set_config(&wifi_status.station_config);
        wifi_station_connect();

        //set timer to check status
        os_timer_disarm(&status->timer);
        os_timer_setfn(&status->timer, (os_timer_func_t *)http_execute_cgi, c);
        os_timer_arm(&status->timer, 500, 0);

        status->state=2;

        return HTTPD_CGI_MORE;
    }
    else if (status->state==2)
    {
        CGI_WIFI_DBG("connect_ap status %d\n",status->state);
        uint8_t c_status = wifi_station_get_connect_status();

        CGI_WIFI_DBG("wifi sta status %d\n",c_status);
        if (c_status>=2 && c_status <= 4 )
        {
            wifi_station_disconnect();
            strcpy(wifi_status.station_config.ssid,"");
            strcpy(wifi_status.station_config.password,"");
            wifi_station_set_config(&wifi_status.station_config);
        }

        if (c_status==1)
        {
            //set timer to check status
            os_timer_disarm(&status->timer);
            os_timer_setfn(&status->timer, (os_timer_func_t *)http_execute_cgi, c);
            os_timer_arm(&status->timer, 500, 0);
            return HTTPD_CGI_MORE;
        }
        else
        {
            //write headers
            http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
            http_response_OK(c);

            //create json
            cJSON *root = cJSON_CreateObject();
            cJSON_AddNumberToObject(root,"status",c_status);

            //got ip
            if(c_status==5)
            {
                struct ip_info ip;
                wifi_get_ip_info(0x0,&ip);
                char *ip_str = (char*)ipaddr_ntoa(&ip.ip);
                cJSON_AddStringToObject(root,"ip",ip_str);
            }
            else
            {
                cJSON_AddStringToObject(root,"ip","");
            }

            http_write_json(c,root);

            //delete json struct
            cJSON_Delete(root);

            status->state=99;

            return HTTPD_CGI_MORE;
        }
    }
    else //status=99
    {
        CGI_WIFI_DBG("connect_ap status %d\n",status->state);
        //clean
        os_free(c->cgi.data);
        return HTTPD_CGI_DONE;
    }

badrequest:
    http_response_BAD_REQUEST(c);
    status->state=99;
    return HTTPD_CGI_MORE;

    //shut up compiler
    return HTTPD_CGI_DONE;
}
示例#16
0
int http_wifi_api_scan(http_connection *c)
{
    //wait for whole body
    if(c->state <HTTPD_STATE_BODY_END) {
        return HTTPD_CGI_MORE;
    }

    api_cgi_scan_status * status = c->cgi.data;
    //first call, create status
    if(status==NULL)
    {
        //create status
        status = (api_cgi_scan_status*)os_malloc(sizeof(api_cgi_scan_status));
        status->state=1;
        status->ap_index=0;
        c->cgi.data=status;

        //if not already scanning, request scan
        if(!wifi_status.scanning)
        {
            // CGI_WIFI_DBG("Starting scan\n");
            wifi_station_scan(NULL,http_wifi_api_scan_callback);
            wifi_status.scanning=1;
        }

        //write headers
        http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
        http_response_OK(c);

        //set state to 1 - waiting
        status->state=1;

        return HTTPD_CGI_MORE;
    }
    else
    {
        if (wifi_status.scanning)
        {
            // CGI_WIFI_DBG("Waiting scan done\n");

            //set timer to check again
            os_timer_disarm(&status->timer);
            os_timer_setfn(&status->timer, (os_timer_func_t *)http_execute_cgi, c);
            os_timer_arm(&status->timer, 500, 0);

            return HTTPD_CGI_MORE;
        }
        else if (status->state==1)
        {
            //clear timer
            os_timer_disarm(&status->timer);

            CGI_WIFI_DBG("Scan complete %d\n",status->ap_index);

            //create json
            cJSON *root = cJSON_CreateObject();
            cJSON_AddNumberToObject(root,"ap_count",wifi_status.scan_result.ap_count);

            cJSON * array;
            cJSON * item;
            cJSON_AddItemToObject(root, "ap", array = cJSON_CreateArray());

            int i;
            for(i=0; i< wifi_status.scan_result.ap_count; i++)
            {
                cJSON_AddItemToArray(array,item=cJSON_CreateObject());
                cJSON_AddStringToObject(item,"ssid",(const char *)wifi_status.scan_result.ap[i]->ssid);
                cJSON_AddNumberToObject(item,"rssi",wifi_status.scan_result.ap[i]->rssi);
                cJSON_AddNumberToObject(item,"enc",wifi_status.scan_result.ap[i]->enc);
                cJSON_AddNumberToObject(item,"channel",wifi_status.scan_result.ap[i]->channel);
            }

            http_write_json(c,root);

            //delete json struct
            cJSON_Delete(root);

            status->state=99;
            return HTTPD_CGI_MORE;
        }
        else  //free resources
        {
            CGI_WIFI_DBG("Freeing alloced memory\n");
            os_free(c->cgi.data);

            return HTTPD_CGI_DONE;
        }
    }
}
示例#17
0
int ICACHE_FLASH_ATTR http_wifi_api_scan(http_connection *c) {
		
	NODE_DBG("http_wifi_api_scan");

	
	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE; 


	api_cgi_scan_status * status = c->cgi.data;
	if(status==NULL){ //first call, create status

		//create status
		status = (api_cgi_scan_status*)os_malloc(sizeof(api_cgi_scan_status));
		status->state=1;
		status->ap_index=0;
		c->cgi.data=status;

		if(!wifi_status.scanning){
			//if not already scanning, request scan

			NODE_DBG("Starting scan");

			wifi_station_scan(NULL,http_wifi_api_scan_callback);

			wifi_status.scanning=1;

		}

		//write headers
		http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);	
		http_response_OK(c);			

		//set state to 1 - waiting
		status->state=1;
		
		return HTTPD_CGI_MORE;

	}
	else{		

		if(wifi_status.scanning){
			NODE_DBG("Waiting scan done");

			//set timer to check again
			os_timer_disarm(&status->timer);
			os_timer_setfn(&status->timer, http_execute_cgi, c);
			os_timer_arm(&status->timer, 500, 0);

			return HTTPD_CGI_MORE;
		}			
		else if(status->state==1){	
			
			//clear timer
			os_timer_disarm(&status->timer);

			NODE_DBG("Scan complete %d",status->ap_index);

			//create json
			cJSON *root = cJSON_CreateObject();
			cJSON_AddNumberToObject(root,"ap_count",wifi_status.scan_result.ap_count);

			cJSON * array;
			cJSON * item;
			cJSON_AddItemToObject(root, "ap", array = cJSON_CreateArray());


			//check max count on query string
			char *query=http_url_get_query_param(c,"max");
			int max = INT_MAX;
			if(query!=NULL)		
				max = atoi(query);

			int i;
			for(i=0;i< wifi_status.scan_result.ap_count && i<max;i++){

				cJSON_AddItemToArray(array,item=cJSON_CreateObject());
				cJSON_AddStringToObject(item,"ssid",(const char *)wifi_status.scan_result.ap[i]->ssid);
				cJSON_AddNumberToObject(item,"rssi",wifi_status.scan_result.ap[i]->rssi);
				cJSON_AddNumberToObject(item,"enc",wifi_status.scan_result.ap[i]->enc);
				cJSON_AddNumberToObject(item,"channel",wifi_status.scan_result.ap[i]->channel);

			}

			http_write_json(c,root);	

			//delete json struct
			cJSON_Delete(root);

			status->state=99; 		
			return HTTPD_CGI_MORE;
						
			
		}
		else{ //free resources

			NODE_DBG("Freeing alloced memory");
			os_free(c->cgi.data); 		

			return HTTPD_CGI_DONE;
		}

	}

}
示例#18
0
int http_console_api(http_connection *c)
{
	CGI_CONS_DBG("http_console_api\n");

	//wait for whole body
	if(c->state <HTTPD_STATE_BODY_END)
		return HTTPD_CGI_MORE;

	http_SET_HEADER(c,HTTP_CONTENT_TYPE,JSON_CONTENT_TYPE);
	// http_response_OK(c);

	//parse json and validate
   cJSON * root = cJSON_Parse(c->body.data);
   if(root==NULL) goto badrequest;

	cJSON * first = NULL;
	unsigned i;
	for (i=0; i < CONS_CMDS_CT; i++)
	{
		if (cJSON_HasObjectItem(root, console_json[i]))
		{
			first = cJSON_GetObjectItem(root,console_json[i]);
			break;
		}
	}

	if ((first==NULL) || i == CONS_CMDS_CT) { goto badrequest; }
	if ((first->type != cJSON_String) && (i != cons_start_t) && (i != cons_to_node_t)) { goto badrequest; }

	int tx = 0;
	int btn_val = 99;

	cJSON * cmd_ct = NULL;
	if (i > 0)
	{
		cmd_ct = cJSON_GetObjectItem(root,"ctcnt");
		if (cmd_ct==NULL) { goto badrequest; }
		if (cmd_ct->type != cJSON_Number) { goto badrequest; }
		btn_val = cmd_ct->valueint;
	}

	CGI_CONS_DBG("BTN Value: %d index: %d\n", btn_val, i);

	switch (i)
	{
		case cons_start_t:
		{
			char buff[256];
			if (first->type != cJSON_Number)
				{ goto badrequest; }

			int start = first->valueint;
			int len = 0; // length of text in buff
			int console_len = (console_wr+BUF_MAX-console_rd) % BUF_MAX; // num chars in console_buf

			if (start < console_pos) 							{ start = 0; }
			else if (start >= console_pos+console_len) 	{ start = console_len; }
			else { start = start - console_pos; }
			int rd = (console_rd+start) % BUF_MAX;
			while (len < BUF_MAX && rd != console_wr)
			{
				uint8_t c = console_buf[rd];
				buff[len++] = c;
				rd = (rd + 1) % BUF_MAX;
			}
			buff[len] = '\0'; //null terminate

			cJSON_AddNumberToObject(root,"len",console_len-start);
			cJSON *newSt = cJSON_CreateNumber(console_pos+start);
			cJSON_ReplaceItemInObject(root, "start", newSt);
			cJSON_AddStringToObject(root,"text", (const char *)buff);

			http_write_json(c,root);
			cJSON_Delete(root);
			return HTTPD_CGI_DONE;
		} break;

		case cons_pwr_t:
		{
			sendTxBuff[0] = (uint8_t) (49-btn_val);
         tx = 1;
			NODE_DBG("sendtxbuff[0]==%02x\n", sendTxBuff[0]);
			NODE_DBG("Sending %d bytes to node %d\n",tx, 99);
			generateRfmTxMsg(99, sendTxBuff, tx, false, false);
      } break;

		case cons_to_node_t:
		{
			/* Get requested payload */
			cJSON * dataJson = cJSON_GetObjectItem(root,"data");
		   if(dataJson==NULL) { goto badrequest; }
		   else if (dataJson->type != cJSON_String) { goto badrequest; }
			bool reqAck = false;

			/* Get ack checkbox value */
			cJSON * ackJson = cJSON_GetObjectItem(root,"ack");
			if ((ackJson!=NULL) && (ackJson->type == cJSON_Number))
				{ reqAck = (bool) ackJson->valueint; }

			int len, nodeid;
			len = strlen(dataJson->valuestring);
			NODE_DBG("send len = %d\n", len);
			nodeid = first->valueint;

			if ((len > 0) && (nodeid>0) && (nodeid < 255))
			{
				CGI_CONS_DBG("Sending %s to node %d\n", dataJson->valuestring, nodeid);
				generateRfmTxMsg((uint8_t)nodeid, dataJson->valuestring, len, reqAck, false);
			}

		} break;

		case cons_disp_t:
		{
			rfm_toggle_all_output();
		} break;

	} /* End cons_method_t Switch() */
	http_response_OK(c);
	cJSON_Delete(root);
	return HTTPD_CGI_DONE;

badrequest:
	http_response_BAD_REQUEST(c);
	cJSON_Delete(root);
	return HTTPD_CGI_DONE;

	//shut up compiler
	return HTTPD_CGI_DONE;
}
示例#19
0
//cgi for static file serving
int ICACHE_FLASH_ATTR cgi_file_system(http_connection *connData) {

	cgi_fs_state *state = (cgi_fs_state *)connData->cgi.data;

	int len;
	char buff[HTTP_BUFFER_SIZE];		
	
	//wait for body end state
	if(connData->state<HTTPD_STATE_BODY_END)
		return HTTPD_CGI_NEXT_RULE; 	

	if (state==NULL) {
		//First call to this cgi. Open the file so we can read it.
		
		RO_FILE *f;
		if( ! connData->url_parsed.field_set & (1<<UF_PATH) ){
			//there's no PATH on the url ( WTF? ), so not found
			return HTTPD_CGI_NOTFOUND;
		}

		char * path = http_url_get_path(connData);
		
		f=f_open(path);		

		if (f==NULL) {
			return HTTPD_CGI_NOTFOUND;
		}

		NODE_DBG("File %s opened",path);

		state = (cgi_fs_state*)os_zalloc(sizeof(cgi_fs_state));
		state->f = f;
		connData->cgi.data=state; //save state for next time
		
		//set headers
		http_SET_HEADER(connData,HTTP_CONTENT_TYPE,http_get_mime(connData->url));	
		http_SET_HEADER(connData,HTTP_CACHE_CONTROL,HTTP_DEFAULT_CACHE);
		http_SET_CONTENT_LENGTH(connData,f->file->size);

		if(f->file->gzip)
			http_SET_HEADER(connData,HTTP_CONTENT_ENCODING,"gzip");

		http_response_OK(connData);		
		
		
		return HTTPD_CGI_MORE;
	}
	else{
		//file found, transmit data

		len=f_read(state->f, buff, HTTP_BUFFER_SIZE);
		if (len>0){
			NODE_DBG("Sending %d bytes of data",len);
			http_nwrite(connData,(const char*)buff,len);
			//http_response_transmit(connData);
		} 
		if (state->f->eof) {
			NODE_DBG("End of file reached");
			//We're done.
			f_close(state->f);
			os_free(state);
			return HTTPD_CGI_DONE;
		} else {
			//not done yet
			return HTTPD_CGI_MORE;
		}

	}

	
}