238 lines
6.6 KiB
C
238 lines
6.6 KiB
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <errno.h>
|
||
|
#include <unistd.h>
|
||
|
#include <wifi_intf.h>
|
||
|
#include <pthread.h>
|
||
|
#include "wmg_debug.h"
|
||
|
|
||
|
static pthread_t app_scan_tid;
|
||
|
static int event = WIFIMG_NETWORK_DISCONNECTED;
|
||
|
|
||
|
static void wifi_event_handle(tWIFI_EVENT wifi_event, void *buf, int event_label)
|
||
|
{
|
||
|
wmg_printf(MSG_DEBUG,"event_label 0x%x\n", event_label);
|
||
|
|
||
|
switch(wifi_event)
|
||
|
{
|
||
|
case WIFIMG_WIFI_ON_SUCCESS:
|
||
|
{
|
||
|
wmg_printf(MSG_INFO,"WiFi on success!\n");
|
||
|
event = WIFIMG_WIFI_ON_SUCCESS;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_WIFI_ON_FAILED:
|
||
|
{
|
||
|
wmg_printf(MSG_ERROR,"WiFi on failed!\n");
|
||
|
event = WIFIMG_WIFI_ON_FAILED;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_WIFI_OFF_FAILED:
|
||
|
{
|
||
|
wmg_printf(MSG_ERROR,"wifi off failed!\n");
|
||
|
event = WIFIMG_WIFI_OFF_FAILED;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_WIFI_OFF_SUCCESS:
|
||
|
{
|
||
|
wmg_printf(MSG_INFO,"wifi off success!\n");
|
||
|
event = WIFIMG_WIFI_OFF_SUCCESS;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_NETWORK_CONNECTED:
|
||
|
{
|
||
|
wmg_printf(MSG_INFO,"WiFi connected ap!\n");
|
||
|
event = WIFIMG_NETWORK_CONNECTED;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_NETWORK_DISCONNECTED:
|
||
|
{
|
||
|
wmg_printf(MSG_INFO,"WiFi disconnected!\n");
|
||
|
event = WIFIMG_NETWORK_DISCONNECTED;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_PASSWORD_FAILED:
|
||
|
{
|
||
|
wmg_printf(MSG_ERROR,"Password authentication failed!\n");
|
||
|
event = WIFIMG_PASSWORD_FAILED;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_CONNECT_TIMEOUT:
|
||
|
{
|
||
|
wmg_printf(MSG_ERROR,"Connected timeout!\n");
|
||
|
event = WIFIMG_CONNECT_TIMEOUT;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_NO_NETWORK_CONNECTING:
|
||
|
{
|
||
|
wmg_printf(MSG_INFO,"It has no wifi auto connect when wifi on!\n");
|
||
|
event = WIFIMG_NO_NETWORK_CONNECTING;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_CMD_OR_PARAMS_ERROR:
|
||
|
{
|
||
|
wmg_printf(MSG_ERROR,"cmd or params error!\n");
|
||
|
event = WIFIMG_CMD_OR_PARAMS_ERROR;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_KEY_MGMT_NOT_SUPPORT:
|
||
|
{
|
||
|
wmg_printf(MSG_ERROR,"key mgmt is not supported!\n");
|
||
|
event = WIFIMG_KEY_MGMT_NOT_SUPPORT;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_OPT_NO_USE_EVENT:
|
||
|
{
|
||
|
wmg_printf(MSG_INFO,"operation no use!\n");
|
||
|
event = WIFIMG_OPT_NO_USE_EVENT;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_NETWORK_NOT_EXIST:
|
||
|
{
|
||
|
wmg_printf(MSG_ERROR,"network not exist!\n");
|
||
|
event = WIFIMG_NETWORK_NOT_EXIST;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
case WIFIMG_DEV_BUSING_EVENT:
|
||
|
{
|
||
|
wmg_printf(MSG_INFO,"wifi device busing!\n");
|
||
|
event = WIFIMG_DEV_BUSING_EVENT;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
default:
|
||
|
{
|
||
|
wmg_printf(MSG_DEBUG,"Other event, no care!\n");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void *app_scan_task(void *args)
|
||
|
{
|
||
|
const aw_wifi_interface_t *p_wifi = (aw_wifi_interface_t *)args;
|
||
|
char scan_results[4096];
|
||
|
int len = 0;
|
||
|
int event_label = 0;
|
||
|
|
||
|
while(1){
|
||
|
event_label++;
|
||
|
p_wifi->start_scan(event_label);
|
||
|
len = 4096;
|
||
|
p_wifi->get_scan_results(scan_results, &len);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void print_help(){
|
||
|
wmg_printf(MSG_INFO,"---------------------------------------------------------------------------------\n");
|
||
|
wmg_printf(MSG_INFO,"NAME:\n\twifi_remove_all_networks_test\n");
|
||
|
wmg_printf(MSG_INFO,"DESCRIPTION:\n\tremove all the networks in wpa_supplicant.conf.\n");
|
||
|
wmg_printf(MSG_INFO,"\nUSAGE:\n\twifi_remove_all_networks_test\n");
|
||
|
wmg_printf(MSG_INFO,"--------------------------------------MORE---------------------------------------\n");
|
||
|
wmg_printf(MSG_INFO,"The way to get help information:\n");
|
||
|
wmg_printf(MSG_INFO,"\twifi_remove_all_networks_test --help\n");
|
||
|
wmg_printf(MSG_INFO,"\twifi_remove_all_networks_test -h\n");
|
||
|
wmg_printf(MSG_INFO,"\twifi_remove_all_networks_test -H\n");
|
||
|
wmg_printf(MSG_INFO,"---------------------------------------------------------------------------------\n");
|
||
|
}
|
||
|
static int set_log_level(int argv, char *argc[])
|
||
|
{
|
||
|
if(argv >=2 && !strncmp(argc[1],"d",1)){
|
||
|
char *debug = argc[1];
|
||
|
if(strlen(debug) >=2 && debug[1] >= '0' &&
|
||
|
debug[1] <= '5'){
|
||
|
wmg_set_debug_level(debug[1] - '0');
|
||
|
return 0;
|
||
|
}else{
|
||
|
printf("Illegal level\n");
|
||
|
printf("Level range 0~5\n");
|
||
|
return -1;
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
*remove all networks in wpa_supplicant.conf
|
||
|
*
|
||
|
*/
|
||
|
int main(int argv, char *argc[]){
|
||
|
int ret = 0;
|
||
|
int times = 0, event_label = 0;
|
||
|
int wifi_state = WIFIMG_WIFI_DISABLED;
|
||
|
const aw_wifi_interface_t *p_wifi_interface = NULL;
|
||
|
|
||
|
if(argv == 2 && (!strcmp(argc[1],"--help") || !strcmp(argc[1], "-h") || !strcmp(argc[1], "-H"))){
|
||
|
print_help();
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
if(argv < 1 || argv >2){
|
||
|
wmg_printf(MSG_ERROR,"ERROR:No need other params\n");
|
||
|
print_help();
|
||
|
return -1;
|
||
|
}
|
||
|
if(set_log_level(argv,argc))
|
||
|
return -1;
|
||
|
wmg_printf(MSG_INFO,"\n*********************************\n");
|
||
|
wmg_printf(MSG_INFO,"***Start wifi remove all networks test!***\n");
|
||
|
wmg_printf(MSG_INFO,"*********************************\n");
|
||
|
|
||
|
event_label = rand();
|
||
|
p_wifi_interface = aw_wifi_on(wifi_event_handle, event_label);
|
||
|
if(p_wifi_interface == NULL){
|
||
|
wmg_printf(MSG_ERROR,"wifi on failed event 0x%x\n", event);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
while(aw_wifi_get_wifi_state() == WIFIMG_WIFI_BUSING){
|
||
|
wmg_printf(MSG_INFO,"wifi state busing,waiting\n");
|
||
|
usleep(2000000);
|
||
|
}
|
||
|
|
||
|
event_label++;
|
||
|
ret = (p_wifi_interface->disconnect_ap(event_label));
|
||
|
while(aw_wifi_get_wifi_state() == WIFIMG_WIFI_BUSING){
|
||
|
wmg_printf(MSG_INFO,"wifi state busing,waiting\n");
|
||
|
usleep(2000000);
|
||
|
}
|
||
|
while(aw_wifi_get_wifi_state() != WIFIMG_WIFI_DISCONNECTED && times < 5){
|
||
|
wmg_printf(MSG_INFO,"wifi state is not disconnected\n");
|
||
|
times++;
|
||
|
usleep(2000000);
|
||
|
}
|
||
|
if(times >= 5){
|
||
|
wmg_printf(MSG_ERROR,"wifi state can not change to disconnected,remove all networks failed\n");
|
||
|
return -1;
|
||
|
}
|
||
|
event_label++;
|
||
|
ret = (p_wifi_interface->remove_all_networks());
|
||
|
while(aw_wifi_get_wifi_state() == WIFIMG_WIFI_BUSING){
|
||
|
wmg_printf(MSG_INFO,"wifi state busing,waiting\n");
|
||
|
usleep(2000000);
|
||
|
}
|
||
|
|
||
|
if(ret == 0)
|
||
|
{
|
||
|
wmg_printf(MSG_INFO,"******************************\n");
|
||
|
wmg_printf(MSG_INFO,"Wifi remove all network test: Success!\n");
|
||
|
wmg_printf(MSG_INFO,"******************************\n");
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|