Exemplo n.º 1
0
void wifi_startup(){

  config_wifi();            // This function sets up the wifi sensor for operation.  Basic housekeeping.
  wifi_auth_mode(2);        // Set the authentication mode.
                            // 0 is none or WPA // 1 is Open network // 2 is Shared with WEP
  setDHCP(1);               // Get us an IP number
  set_wpa_psk();            // Calculates your psk
  Receive(true);            // Housekeeping
  Receive(true);            // Housekeeping
  connect_to_ssid();        // Connects to the wifi network you've designated
  PlaySound(soundBeepBeep); // After connection, announce to the world you're connected.
  Receive(true);            // Housekeeping.
}
Exemplo n.º 2
0
void _setConfigurations(cJSON *root, s_config *config, char *http_packet)
{
	cJSON *valueSetObj= cJSON_CreateObject();
	cJSON *attribute;
	cJSON *key;
	cJSON *item;
	int array_size;
	int i;
	char cmd[MAX_BUF];
	char result[MAX_BUF];
	char ErrMesg[B_5_BUF];
	cJSON *transaction_id = cJSON_GetObjectItem(root, "transaction_id");
	char *conf_version;
	char *radio_index;
	char *profile_index;
	char *status;
	int flag =0;
	if(!transaction_id) {
		debug(LOG_ERR, "Can not find transaction_id parameter: %s", cJSON_GetErrorPtr());
		create_http_json(valueSetObj, NULL, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{transaction_id}", config->sn, http_packet);
		return;

	}
	cJSON *valueSet = cJSON_GetObjectItem(root, "valueSet");
	if(!valueSet) {
		debug(LOG_ERR, "Get valueSet faild[%s]", cJSON_GetErrorPtr());
		create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{valueSet}", config->sn, http_packet);
		return;
	}
	/*
	if((key = cJSON_GetObjectItem(valueSet, "conf_version")) == NULL || (conf_version = key->valuestring) == NULL) {
		create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{conf_version}", config->sn, http_packet);
		return;
	}
	*/
	ErrMesg[0]=0;
	//setDeviceInfo//
	{
		if((attribute = cJSON_GetObjectItem(valueSet, "setDeviceInfo")) != NULL) {
			if((key = cJSON_GetObjectItem(attribute, "apname")) != NULL && key->valuestring != NULL) {
					/*snprintf(cmd, MAX_BUF, "config apname %s\n", key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_apname(key->valuestring, result, sizeof(result)/sizeof(result[0]));
				}
		}
	}
	//setRadioInfo//
	{
		if((attribute = cJSON_GetObjectItem(valueSet, "setRadioInfo")) != NULL) {
			array_size = cJSON_GetArraySize(attribute);
			debug(LOG_DEBUG, "Array size of paras is %d",array_size);
 			for(i=0; i< array_size; i++) {
				item = cJSON_GetArrayItem(attribute, i);
				//debug(LOG_DEBUG, "%s\n",item->valuestring);
				if((key = cJSON_GetObjectItem(item, "radio_index")) == NULL || (radio_index = radio_keywords[parse_radio_keywords(key->valuestring)].value) == NULL) {
					create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{radio_index}", config->sn, http_packet);
					return;
				}
				/*
				if((key = cJSON_GetObjectItem(item, "radio")) != NULL && key->valuestring != NULL) {
					snprintf(cmd, MAX_BUF, "config interface wlan %s radio %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					//debug(LOG_INFO, "result is %d", strlen(result));
				}
				*/
				if((key = cJSON_GetObjectItem(item, "mode")) != NULL && key->valuestring != NULL) {
					/*snprintf(cmd, MAX_BUF, "config interface wlan %s mode %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
			
					set_mode(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				
				if((key = cJSON_GetObjectItem(item, "power")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s power %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_power(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				
				if((key = cJSON_GetObjectItem(item, "channel")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s channel %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_channel(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				if((key = cJSON_GetObjectItem(item, "channelwidth")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s channelwidth %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_channelwidth(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
	
				}
				if((key = cJSON_GetObjectItem(item, "max-wireless-clients")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s max-wireless-clients %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_max_assoc(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				
				if((key = cJSON_GetObjectItem(item, "client-isolation")) != NULL && key->valuestring != NULL) {
					/*
					snprintf(cmd, MAX_BUF, "config interface wlan %s client-isolation %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
					*/
					set_ap_isolate(atoi(radio_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
					
				}
				/*
				if((key = cJSON_GetObjectItem(item, "rate")) != NULL && key->valuestring != NULL) {
					snprintf(cmd, MAX_BUF, "config interface wlan %s rate %s\n", cJSON_GetObjectItem(item, "radio_index")->valuestring, key->valuestring);
					
					if(set_config(cmd, result)) 
						strcpy(ErrMesg, result);
				}	
				*/
 			}
		}
	}
	//setSSIDInfo//
	{
		if((attribute = cJSON_GetObjectItem(valueSet, "setSSIDInfo")) != NULL) {
			array_size = cJSON_GetArraySize(attribute);
			debug(LOG_DEBUG, "Array size of paras is %d",array_size);
 			for(i=0; i< array_size; i++) {
				item = cJSON_GetArrayItem(attribute, i);
				if((key = cJSON_GetObjectItem(item, "radio_index")) == NULL || (radio_index = radio_keywords[parse_radio_keywords(key->valuestring)].value) == NULL) {
					create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{radio_index}", config->sn, http_packet);
					return;
				}
				if((key = cJSON_GetObjectItem(item, "profile_index")) == NULL || (profile_index = key->valuestring) == NULL) {
					create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{profile_index}", config->sn, http_packet);
					return;
				}
				if((key = cJSON_GetObjectItem(item, "status")) == NULL || (status = key->valuestring) == NULL) {
					create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "3", "Missing parameter:{status}", config->sn, http_packet);
					return;
				}
				if(atoi(profile_index)>0) {
					char vifs[128];
					char viname[32];
					sprintf(viname, "wl%d.%d", atoi(radio_index), atoi(profile_index));
					read_vifs(atoi(radio_index), vifs, sizeof(vifs)/sizeof(vifs[0]));
	
					if(strstr(vifs, viname) && atoi(status)==0) {
						del_virtual_interface(atoi(radio_index), atoi(profile_index));
					}
					else if(!strstr(vifs, viname) && atoi(status)==1) {
						add_virtual_interface(atoi(radio_index), atoi(profile_index));
					}
					else if((!strstr(vifs, viname) && atoi(status)==0) || atoi(profile_index)>3) {
						continue;
					}
					
				}
				if((key = cJSON_GetObjectItem(item, "hide-network-name")) != NULL && key->valuestring != NULL) {
					set_status(atoi(radio_index), atoi(profile_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);	
				}
				if((key = cJSON_GetObjectItem(item, "ssid")) != NULL && key->valuestring != NULL) {
					set_ssid(atoi(radio_index), atoi(profile_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
						
				}
				if((key = cJSON_GetObjectItem(item, "authentication")) != NULL && key->valuestring != NULL) {
					
					set_security_mode(atoi(radio_index), atoi(profile_index), authentication_keywords[parse_authentication_keywords(key->valuestring)].value, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				
				if((key = cJSON_GetObjectItem(item, "encryption")) != NULL && key->valuestring != NULL) {
					set_crypto(atoi(radio_index), atoi(profile_index), encryption_keywords[parse_encryption_keywords(key->valuestring)].value, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}

				if((key = cJSON_GetObjectItem(item, "presharedkey")) != NULL && key->valuestring != NULL) {
					set_wpa_psk(atoi(radio_index), atoi(profile_index), key->valuestring, result, sizeof(result)/sizeof(result[0]));
					strcpy(ErrMesg, result);
				}
				flag =1;
 			}
			
		}
		
 	}
 	{
 		//apply_setting();
 		/*
 		int result;
		pthread_t tid_init_service = 0;
		config->upgrade_lock = 1;
 		debug(LOG_INFO, "create a new thread (thread_init_service)");
		result = pthread_create(&tid_init_service, NULL, (void *)thread_init_service, NULL);
		if (result != 0) {
			debug(LOG_ERR, "FATAL: Failed to create a new thread (init_service) - exiting");
			exit(1);
		}*/
		system("nvram commit");
		
 	}
 
	if(strlen(ErrMesg)) {
		create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "failed", "1000", ErrMesg, config->sn, http_packet);
	}
	else {
		create_http_json(valueSetObj, transaction_id->valuestring, RESPONSE, SETCONFIGURATIONS, "success", "0", NULL, config->sn, http_packet);
	}
	if(flag) {
		safe_encrypt_http_send(config->httpfd, http_packet, strlen(http_packet), 0); 
		shutdown(config->httpfd, SHUT_RDWR);
		system("reboot");
	}
		
			
}