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

294 lines
7.7 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 <wmg_debug.h>
#include <wifi_udhcpc.h>
/*
wifi connect test
include <wifi_intf.h>
add by Kirin 2016/11/22
it should link to libwifimg.so
*/
#include <wifi_intf.h>
int debug_enable = 1;
/*
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 \n");
return -1;
}
if(state == NETWORK_CONNECTED){
wmg_printf(MSG_INFO,"auto connected Successful !!!!\n");
wmg_printf(MSG_INFO,"==================================\n");
}
printf("c->info.protocol === %d \n",c->info.protocol);
if(c->info.protocol == AW_SMARTLINKD_PROTO_XRSC ||
c->info.protocol == AW_SMARTLINKD_PROTO_COOEE ||
c->info.protocol == AW_SMARTLINKD_PROTO_AKISS)
{
printf("c->info.base_info.security == %d \n",c->info.base_info.security);
//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;
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))
{
printf("*****************************************\n");
printf("Smartlink demo:wifi connect ap: Successful!\n");
printf("*****************************************\n");
}
else
{
printf("*****************************************\n");
printf("Smartlink demo:wifi connect ap: failed!\n");
printf("*****************************************\n");
}
}
connect_ap_finished_flag = 1;
return THREAD_EXIT;
}
return THREAD_CONTINUE;
}
void print_help()
{
printf("---------------------------------------------------------------------------------\n");
printf("example: smartlinkd_demo V\n\n");
printf("V:take the following value\n");
printf("\t0-airkiss\n");
printf("\t1-cooee ----- Only support Broadcom module\n");
printf("\t2-soundwave\n");
printf("\t3-softap ----- Only support Broadcom module\n");
printf("\t4-xrsc ----- Only support XR819 module\n");
printf("\t5-composite\n");
printf("---------------------------------------------------------------------------------\n");
}
int check_params(int num){
if(num != 2 ){
printf("ERROR: params more or less!\n");
return -1;
}
return 0;
}
int main(int argc, char* argv[])
{
int proto = 0;
pid_t pid;
int ret = 0;
if(check_params(argc)){
print_help();
return -1;
}
if(argc == 2 && (!strcmp(argv[1],"--help") || !strcmp(argv[1], "-h") || !strcmp(argv[1], "-H"))){
print_help();
return -1;
}
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(check_wlan_interface()){
printf("!!!!!!!!!WLAN is not ready\n");
return 0;
}
if(argc > 1){
proto = atoi(argv[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("#########smartlink softap#########\n");
system("smartlinkd_softap");
connect_ap_finished_flag=1;
}
else if(proto ==4){
printf("#######xr819 xsrc##############\n");
aw_startxrsc();
}
else if(proto == 5){
printf("#########start composite#########\n");
aw_startcomposite(AW_SMARTLINKD_PROTO_COOEE|AW_SMARTLINKD_PROTO_ADT);
}
}
while(connect_ap_finished_flag == 0)
sleep(1);
return 0;
}