SmartAudio/package/allwinner/smartlinkd/demo/smartlinkd_long_time_test.c

313 lines
7.9 KiB
C
Executable File

#define TAG "smartlinkd-demo"
#include <stdlib.h>
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <tina_log.h>
#include "aw_smartlinkd_connect.h"
#include <wifi_intf.h>
#include <wmg_debug.h>
#include <wifi_udhcpc.h>
#define SMARTLINK_TEST_TIMES 10
int debug_enable = 1;
int success_times = 0;
int fail_times = 0;
/*
param used to wifi connect test ,add by Kirin 2016/11/22
*/
const aw_wifi_interface_t *p_wifi_interface = NULL;
tKEY_MGMT key_mgmt;
int event_label = 0;
static int connect_ap_finished_flag = 0;
enum wmgState state;
static void wifi_state_handle(struct Manager *w, int event_label)
{
wmg_printf(MSG_DEBUG,"event_label 0x%x\n", event_label);
switch(w->StaEvt.state)
{
case CONNECTING:
{
wmg_printf(MSG_INFO,"Connecting to the network......\n");
state = CONNECTING;
break;
}
case CONNECTED:
{
wmg_printf(MSG_INFO,"Connected to the AP\n");
state = CONNECTED;
start_udhcpc();
break;
}
case OBTAINING_IP:
{
wmg_printf(MSG_INFO,"Getting ip address......\n");
state = OBTAINING_IP;
break;
}
case NETWORK_CONNECTED:
{
wmg_printf(MSG_DEBUG,"Successful network connection\n");
state = NETWORK_CONNECTED;
break;
}
case DISCONNECTED:
{
wmg_printf(MSG_ERROR,"Disconnected,the reason:%s\n",wmg_event_txt(w->StaEvt.event));
state = DISCONNECTED;
break;
}
}
}
int onRead(char* buf,int length)
{
pid_t pid;
char ssid[256] = {0};
char passwd[256] = {0};
int status;
if(length == THREAD_INIT){
printf("if(length == THREAD_INIT)...\n");
}
else if(length == -1){ //RST
printf("else if(length == -1)...\n");
}else if(length == 0){ //EOF
printf("server close the connection...\n");
//exit(0);
return THREAD_EXIT;
}else {
printf("length: %d\n",length);
//printf_info((struct _cmd *)buf);
struct _cmd* c = (struct _cmd *)buf;
if(c->cmd == AW_SMARTLINKD_FAILED){
printf("smartlinkd_demo:response failed\n");
return THREAD_CONTINUE;
}
if(c->info.protocol == AW_SMARTLINKD_PROTO_FAIL){
printf("proto scan fail");
connect_ap_finished_flag = 1;
return THREAD_EXIT;
}
printf("cmd: %d\n",c->cmd);
printf("pcol: %d\n",c->info.protocol);
printf("ssid: %s\n",c->info.base_info.ssid);
printf("pasd: %s\n",c->info.base_info.password);
printf("security: %d\n",c->info.base_info.security);
if(c->info.protocol == AW_SMARTLINKD_PROTO_AKISS)
printf("radm: %d\n",c->info.airkiss_random);
if(c->info.protocol == AW_SMARTLINKD_PROTO_COOEE){
printf("ip: %s\n",c->info.ip_info.ip);
printf("port: %d\n",c->info.ip_info.port);
}
if(c->info.protocol == AW_SMARTLINKD_PROTO_ADT){
printf("adt get: %s\n",c->info.adt_str);
}
printf("\n:*********************************\n");
printf("***Starting to connect ap!***\n");
printf("*********************************\n");
if ((pid = fork()) < 0)
{
printf("smartlinkd_demo: fork 2 error\n");
}
else if(pid == 0)
{
event_label = rand();
p_wifi_interface = aw_wifi_on(wifi_state_handle, event_label);
if(p_wifi_interface == NULL){
printf("wifi on failed event \n");
return -1;
}
if(state == CONNECTED){
wmg_printf(MSG_INFO,"auto connected Successful !!!!\n");
wmg_printf(MSG_INFO,"==================================\n");
}
if(c->info.protocol == AW_SMARTLINKD_PROTO_COOEE || c->info.protocol == AW_SMARTLINKD_PROTO_AKISS)
{
//judge the encryption method of wifi
if(c->info.base_info.security ==0)
key_mgmt = 0;
else if(c->info.base_info.security ==1)
key_mgmt = 3;
else if(c->info.base_info.security ==2)
key_mgmt = 1;
else if(c->info.base_info.security ==3)
key_mgmt = 2;
/*
WLAN_SECURITY_NONE = 0,
WLAN_SECURITY_WEP,
WLAN_SECURITY_WPA,
WLAN_SECURITY_WPA2,
typedef enum {
WIFIMG_NONE = 0,
WIFIMG_WPA_PSK,
WIFIMG_WPA2_PSK,
WIFIMG_WEP,
}tKEY_MGMT;
*/
event_label++;
p_wifi_interface->add_network(c->info.base_info.ssid,key_mgmt,c->info.base_info.password,event_label);
}
else
{
convert_adt_str_to_info(ssid, passwd, c->info.adt_str);
event_label++;
p_wifi_interface->connect_ap(ssid, passwd, event_label);
}
if(state == NETWORK_CONNECTED)
exit(0);
else
exit(1);
}
if(waitpid(pid,&status,0) != pid)
{
printf("waitpid 2 error!\n");
}
if(WIFEXITED(status))
{
if(0 == WEXITSTATUS(status))
{
success_times++;
printf("*****************************************\n");
printf("Smartlink demo:wifi connect ap: Successful!\n");
printf("*****************************************\n");
}
else
{
fail_times++;
printf("*****************************************\n");
printf("Smartlink demo:wifi connect ap: failed!\n");
printf("*****************************************\n");
}
}
connect_ap_finished_flag = 1;
return THREAD_EXIT;
}
return THREAD_CONTINUE;
}
static void help_message( )
{
}
int main(int argc, char* argv[])
{
int proto = 0;
pid_t pid;
int ret = 0;
int test_times = 0;
int i = 0;
char prt_buf[256] = {0};
if(argc < 2)
{
printf("ERROR: parameter error!\n");
help_message();
return -1;
}
if(2 == argc && (!strcmp(argv[1],"-h") || !strcmp(argv[1],"-help")))
{
help_message();
return 0;
}
printf("#########start smartlinkd#########\n");
printf("#########check wifi statue#########\n");
system("ifconfig wlan0 down");
while( 0 == check_wlan_interface()){
printf("wating for wlan interface to be down...\n");
sleep(1);
}
printf("#########check wifi statue ok#########\n");
system("ifconfig wlan0 up");
if(argc > 1){
proto = atoi(argv[1]);
}
if(argv[2] && NULL != argv[2])
{
test_times = atoi(argv[2]);
printf("===================Total test times(with setup): %d!===================\n", test_times);
}
else
{
test_times = SMARTLINK_TEST_TIMES;
printf("===================Total test times(with default): %d!===================\n", test_times);
}
success_times = 0;
fail_times = 0;
for(i = 0; i < test_times; i++)
{
printf("===================The %drd time!===================\n", i+1);
//aw_smartlinkd_prepare();
if(aw_smartlinkd_init(0,onRead) == 0){
if(proto == 0){
printf("#########start airkiss#########\n");
aw_startairkiss();
}
else if(proto == 1){
printf("#########start cooee#########\n");
aw_startcooee();
}
else if(proto == 2){
printf("#########start adt#########\n");
aw_startadt();
}
else if(proto ==3){
printf("#########start composite#########\n");
aw_startcomposite(AW_SMARTLINKD_PROTO_COOEE|AW_SMARTLINKD_PROTO_ADT);
}
}
while(connect_ap_finished_flag == 0)
sleep(1);
connect_ap_finished_flag = 0;
printf("===================The %drd time test finished!===================\n", i+1);
}
sprintf(prt_buf,"echo \"Test Times:%d, Success Times:%d, Failed Times:%d\" > /etc/smartlinkd_test_results",i,success_times,fail_times);
system(prt_buf);
if(success_times == test_times)
{
sprintf(prt_buf,"echo Congratulations! >> /etc/smartlinkd_test_results");
system(prt_buf);
}
return 0;
}