SmartAudio/package/allwinner/wifimanager/smartaudio/app_wifi_manager.cpp

1174 lines
26 KiB
C++
Raw Normal View History

2018-07-17 07:44:16 +00:00
#define TAG "wifi"
#include <tina_log.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <wifi_intf.h>
#include <pthread.h>
#include <sys/time.h>
#include <uv.h>
#include <dbus/dbus.h>
#include <errno.h>
#include <cjson/s2j.h>
#include <uvdbus/log.h>
#include <uvdbus/libuv_dbus.h>
#include <uvdbus/crypto.h>
#include <uvdbus/json_struct.h>
#include <uvdbus/config_engine.h>
#include <string>
#include <vector>
#include <cstddef>
#include <algorithm>
#define PIPE_RET_SIZE 1024
#define WIFI_STATE_JS_KEY "wifi_state"
#define WIFI_STATE_RSSI_JS_KEY "wifi_rssi"
#define WIFI_STATE_EVT_JS_KEY "wifi_evt"
#define RES_CODDE_STR "resCode"
#define SSID_JS_KEY "ssid"
#define PASSWD_JS_KEY "pwd"
#define PASSWD_ACCOUNT "account"
#define WLAN_CONFIG_CMD_STR "iwconfig"
#define WLAN_RSSI_STR_RRE "Signal level="
#define WIFI_CONF_EVT_TAG 0xf000
#define WIFI_AUTO_EVT_TAG 0xf100
#define WIFI_CHANGE_AP_EVT_TAG 0xf200
static pthread_t app_scan_tid;
static int event = WIFIMG_NETWORK_DISCONNECTED;
extern pthread_mutex_t ctrl_mutex;
#define APP_WIFI_SUCCES 0
#define APP_WIFI_NULL -1
#define APP_WIFI_OVER -2
#define APP_WIFI_JSON_INVALID -3
#define RSSI_INVALID_NUM (-9999)
typedef enum
{
WIFI_STATE_KEY_STATE =0,
WIFI_STATE_KEY_RSSI,
WIFI_STATE_KEY_CNT,
}sWifiStateKeysEnum;
typedef enum
{
WIFI_EVT_KEY_EVT =0,
WIFI_EVT_KEY_RSSI,
WIFI_EVT_KEY_CNT,
}sWifiEvtKeysEnum;
typedef enum
{
APP_WIFI_CFG_SUCCESS =0,
APP_WIFI_CFG_SYS_ERR = 0xf001,
APP_WIFI_CFG_FMT_INVALID,
APP_WIFI_CFG_JSON_INVALID,
APP_WIFI_CFG_PARAMS_ERR,
APP_WIFI_CFG_SSID_NO_EXIT,
APP_WIFI_CFG_PWD_INVALID,
APP_WIFI_CFG_BUSY,
APP_WIFI_CFG_TIMEOUT,
APP_WIFI_CFG_WIFI_MD_ERR,
APP_WIFI_CFG_CODEC_ERR,
APP_WIFI_CFG_OTHER,
APP_WIFI_CFG_SYS_CFG_BUSY,
APP_WIFI_CFG_SERVER_ERR,
APP_WIFI_CFG_STATE_INVALID,
}app_wifi_cfg_code_edef;
typedef enum
{
APP_WIFI_AUTO_SET_SUCCESS,
APP_WIFI_AUTO_SET_BUSY,
APP_WIFI_AUTO_BT_CONF_COLLISION,
APP_WIFI_AUTO_OTHER_INVALID,
}app_wifi_auto_resp_code_edef;
typedef enum
{
APP_WIFI_EVT_NTF_CONNECTED =0,
APP_WIFI_EVT_NTF_DISCONNECT,
APP_WIFI_EVT_NTF_CONNECTING,
APP_WIFI_EVT_NTF_MAX,
}app_wifi_state_notify_edef;
int api_wifi_state_notify(int state);
static char * wifiStateKeys[WIFI_STATE_KEY_CNT] ={WIFI_STATE_JS_KEY,WIFI_STATE_RSSI_JS_KEY};
static char * wifiEvtKeys[WIFI_STATE_KEY_CNT] ={WIFI_STATE_EVT_JS_KEY,WIFI_STATE_RSSI_JS_KEY};
std::string VectorStrCompose(std::vector<std::string> recVec)
{
std::string ret;
if(0 == recVec.size()) return "";
for(auto str = recVec.begin(); str !=recVec.end();str++)
{
ret+= *str;
}
return ret;
}
int pipeExcute(const char * cmd,std::vector<std::string> & recVec )
{
if(NULL == cmd) return -1;
recVec.clear();
FILE * pPipe = popen(cmd, "r");
LOG_EX(LOG_Debug, "INFO: cmd is %s\r\n",cmd);
if (NULL==pPipe) {
return -1;
}
char ret[PIPE_RET_SIZE];
memset(ret,0x00,PIPE_RET_SIZE);
while (fgets(ret, sizeof(ret), pPipe) != NULL)
{
recVec.push_back(ret);
}
if(0 != pclose(pPipe))
{
recVec.push_back(strerror(errno));
if(0 != errno)
{
LOG_EX(LOG_Debug, "ERROR: pipe close error = %d\r\n",errno);
}
}
LOG_EX(LOG_Debug, "info: cmd excute complete\r\n");
return 0;
}
int get_connected_ap_rssi()
{
std::string shellCmd = WLAN_CONFIG_CMD_STR;
std::vector<std::string> vectorRes;
int status =0;
int rssi = RSSI_INVALID_NUM;
std::string out;
status =pipeExcute(shellCmd.c_str(),vectorRes);
if(0 == status)
{
out = VectorStrCompose(vectorRes);
LOG_EX(LOG_Debug, "info:cmd is %s, res is \r\n %s \r\n",shellCmd.c_str(),out.c_str());
std::size_t pos = out.find(WLAN_RSSI_STR_RRE);
if(pos == std::string::npos)
{
LOG_EX(LOG_Debug, "warm: no rssi\r\n");
status = -1;
}else
{
std::string rssiStr = out.substr(pos+strlen(WLAN_RSSI_STR_RRE),4);
LOG_EX(LOG_Debug, "---info: rssiStr is %s \r\n",rssiStr.c_str());
rssi = std::stoi(rssiStr);
}
}else
{
LOG_EX(LOG_Debug, "error: pipe excute error\r\n");
status = -1;
}
return (0 == status)?rssi:RSSI_INVALID_NUM;
}
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);
}
}
/*
*argc[1] ap ssid
*argc[2] ap passwd
*/
DBusConnection *pBus;
static const char* g_pDBusInterfaceName = NULL;
static uv_timer_t timerCurPosMsg;
int api_wifi_module_cmd_send(int mask, uint32_t cmd, const char* pContext)
{
int status =0;
status = DBusBoardcastCommand(NULL,
mask,
cmd,
pContext);
if(0 != status )
{
LOG_EX(LOG_Debug, "nevsps send module cmd err is %d\r\n",status);
}
return status;
}
char * api_wifi_res_json_get(int res)
{
cJSON* pRoot = NULL;
char * p=NULL;
pRoot=cJSON_CreateObject();
if(NULL == pRoot) return NULL;
cJSON_AddNumberToObject(pRoot,RES_CODDE_STR,res);
p = cJSON_Print(pRoot);
if(NULL == p)
{
cJSON_Delete(pRoot);
return NULL;
}
cJSON_Delete(pRoot);
return p;
}
char * api_wifi_state_json_get(char * p_key,int res)
{
cJSON* pRoot = NULL;
char * p=NULL;
pRoot=cJSON_CreateObject();
if(NULL == pRoot) return NULL;
cJSON_AddNumberToObject(pRoot,p_key,res);
p = cJSON_Print(pRoot);
if(NULL == p)
{
cJSON_Delete(pRoot);
return NULL;
}
cJSON_Delete(pRoot);
return p;
}
char * api_wifi_state_json_get(char * p_key[],int res[],int size)
{
cJSON* pRoot = NULL;
char * p=NULL;
pRoot=cJSON_CreateObject();
if(NULL == pRoot) return NULL;
for(int i=0;i<size;i++)
{
cJSON_AddNumberToObject(pRoot,p_key[i],res[i]);
}
p = cJSON_Print(pRoot);
if(NULL == p)
{
cJSON_Delete(pRoot);
return NULL;
}
cJSON_Delete(pRoot);
return p;
}
int api_wifi_state_notify(int state)
{
char * pStateJson = NULL;
int status = 0;
pStateJson = api_wifi_state_json_get(WIFI_STATE_EVT_JS_KEY,state);
if(NULL != pStateJson)
{
//status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_STATE_NTF, pStateJson);
status = api_wifi_module_cmd_send(0xffffffff,CMD_WIFI_STATE_NTF, pStateJson);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send wifi state notify msg to control er:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send wifi state notify msg : 0x%x to control module\r\n",state);
}
}else
{
status = APP_WIFI_NULL;
}
if(NULL != pStateJson)
{
free(pStateJson);
pStateJson =NULL;
}
return status;
}
int api_wifi_state_notify(int state,int rssi)
{
char * pEvtJson = NULL;
int status = 0;
int evtVal[WIFI_STATE_KEY_CNT] ={0};
evtVal[WIFI_EVT_KEY_EVT] = state;
evtVal[WIFI_EVT_KEY_RSSI] = rssi;
pEvtJson = api_wifi_state_json_get(wifiEvtKeys,evtVal,WIFI_STATE_KEY_CNT);
if(NULL != pEvtJson)
{
//status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_STATE_NTF, pStateJson);
status = api_wifi_module_cmd_send(0xffffffff,CMD_WIFI_STATE_NTF, pEvtJson);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send wifi state notify msg to control er:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send wifi state notify msg : 0x%x to control module\r\n",state);
}
}else
{
status = APP_WIFI_NULL;
}
if(NULL != pEvtJson)
{
free(pEvtJson);
pEvtJson =NULL;
}
return status;
}
static int ret_flag =0;
static void wifi_event_handle(tWIFI_EVENT wifi_event, void *buf, int event_label)
{
int rssi =0;
LOG_EX(LOG_Debug, "app_wifi_manager event_label 0x%x,event 0x%x\n", event_label,wifi_event);
switch(wifi_event)
{
case WIFIMG_WIFI_ON_SUCCESS:
{
LOG_EX(LOG_Debug, "++++++WiFi on success!\n");
event = WIFIMG_WIFI_ON_SUCCESS;
break;
}
case WIFIMG_WIFI_ON_FAILED:
{
LOG_EX(LOG_Debug, "++++++WiFi on failed!\n");
event = WIFIMG_WIFI_ON_FAILED;
break;
}
case WIFIMG_WIFI_OFF_FAILED:
{
LOG_EX(LOG_Debug, "++++++wifi off failed!\n");
event = WIFIMG_WIFI_OFF_FAILED;
break;
}
case WIFIMG_WIFI_OFF_SUCCESS:
{
LOG_EX(LOG_Debug, "+++++wifi off success!\n");
event = WIFIMG_WIFI_OFF_SUCCESS;
break;
}
case WIFIMG_NETWORK_CONNECTED:
{
LOG_EX(LOG_Debug, "++++++WiFi connected ap!\n");
event = WIFIMG_NETWORK_CONNECTED;
rssi = get_connected_ap_rssi();
if( 0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTED,rssi))
{
LOG_EX(LOG_Debug, "Notify the wifi state error!!\r\n");
}
break;
}
case WIFIMG_NETWORK_DISCONNECTED:
{
LOG_EX(LOG_Debug, "++++++WiFi disconnected!\n");
event = WIFIMG_NETWORK_DISCONNECTED;
system("ifconfig wlan0 0.0.0.0");
if(0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_DISCONNECT,RSSI_INVALID_NUM))
{
LOG_EX(LOG_Debug, "Notify the wifi state error!!\r\n");
}
break;
}
case WIFIMG_PASSWORD_FAILED:
{
//if(1== ret_flag) break;
LOG_EX(LOG_Debug, "++++++Password authentication failed!\n");
event = WIFIMG_PASSWORD_FAILED;
ret_flag =1;
break;
}
case WIFIMG_CONNECT_TIMEOUT:
{
//if(1== ret_flag) break;
LOG_EX(LOG_Debug, "++++++Connected timeout!\n");
event = WIFIMG_CONNECT_TIMEOUT;
ret_flag =1;
break;
}
case WIFIMG_NO_NETWORK_CONNECTING:
{
LOG_EX(LOG_Debug, "++++++It has no wifi auto connect when wifi on!\n");
event = WIFIMG_NO_NETWORK_CONNECTING;
break;
}
case WIFIMG_CMD_OR_PARAMS_ERROR:
{
//if(1== ret_flag) break;
LOG_EX(LOG_Debug, "+++++cmd or params error!\n");
event = WIFIMG_CMD_OR_PARAMS_ERROR;
ret_flag =1;
break;
}
case WIFIMG_KEY_MGMT_NOT_SUPPORT:
{
//if(1== ret_flag) break;
LOG_EX(LOG_Debug, "++++++key mgmt is not supported!\n");
event = WIFIMG_KEY_MGMT_NOT_SUPPORT;
ret_flag =1;
break;
}
case WIFIMG_OPT_NO_USE_EVENT:
{
LOG_EX(LOG_Debug, "++++++operation no use!\n");
event = WIFIMG_OPT_NO_USE_EVENT;
break;
}
case WIFIMG_NETWORK_NOT_EXIST:
{
//if(1== ret_flag) break;
LOG_EX(LOG_Debug, "++++++network not exist!\n");
event = WIFIMG_NETWORK_NOT_EXIST;
ret_flag =1;
break;
}
case WIFIMG_DEV_BUSING_EVENT:
{
LOG_EX(LOG_Debug, "+++++wifi device busing!\n");
event = WIFIMG_DEV_BUSING_EVENT;
break;
}
default:
{
LOG_EX(LOG_Debug, "+++++Other event, no care!\n");
}
}
}
int api_wifi_get_cfg_value(char * p_txt,char** pp_ssid,char** pp_pswd)
{
cJSON * p_root =NULL;
cJSON *p_sd_item = NULL;
cJSON *p_ps_item = NULL;
p_root=cJSON_Parse(p_txt);
if(NULL == p_root)
{
return APP_WIFI_JSON_INVALID;
}
p_sd_item = cJSON_GetObjectItem(p_root,SSID_JS_KEY);
if(NULL == p_sd_item)
{
cJSON_Delete(p_root);
return APP_WIFI_NULL;
}
*pp_ssid= cJSON_Print(p_sd_item);
if(NULL == *pp_ssid)
{
cJSON_Delete(p_root);
return APP_WIFI_NULL;
}
p_ps_item = cJSON_GetObjectItem(p_root,PASSWD_JS_KEY);
if(NULL == p_ps_item)
{
cJSON_Delete(p_root);
return APP_WIFI_NULL;
}
*pp_pswd = cJSON_Print(p_ps_item);
if(NULL == *pp_pswd)
{
cJSON_Delete(p_root);
return APP_WIFI_NULL;
}
cJSON_Delete(p_root);
return 0;
}
void DBusOnKeyEvent(uint16_t uType, uint16_t uKey, int32_t iValue){
}
int api_wifi_get_str_raw(char **p_str_brace)
{
int size =0;
char * p_ret = NULL;
if(NULL == *p_str_brace) return APP_WIFI_NULL;
size = strlen(*p_str_brace);
p_ret= (char *)malloc(size);
if(NULL == p_ret)
{
return APP_WIFI_NULL;
}
memset(p_ret,0x00,size);
memcpy(p_ret,(*p_str_brace+1),size -2);
free(*p_str_brace);
*p_str_brace = p_ret;
return 0;
}
#define WIFI_INFO_BASE64 1
PDBUS_MSG_PACK DBusOnMessage(uv_loop_t *pLoop, DBusConnection *pConn,
PDBUS_MSG_PACK pMsg) {
int event_label = 0;;
static const aw_wifi_interface_t *p_wifi_interface = NULL;
//cJSON * root = NULL;
int status =0;
uint8_t * pResJson = NULL;
uint8_t * pStateJson = NULL;
char * p_ssid =NULL;
char * p_passwd = NULL;
#if WIFI_INFO_BASE64
char * pBaseSsid = NULL;
char * pBasePwd = NULL;
int ssidSize =0;
int pwdSize =0;
#endif
int rsp_code =APP_WIFI_CFG_SUCCESS;
int audo_code =0;
static int busy_conf =0;
static int busy_auto =0;
int wifi_state = WIFIMG_WIFI_DISCONNECTED;
int mask =0;
static int wifi_connet_cnt =0;
LOG_EX(LOG_Debug, "GetDbusCmd:0x%x\r\n",pMsg->busCmd);
if (!pMsg || !pLoop || !pConn) {
return NULL;
}
switch (pMsg->busCmd) {
case CMD_WIFI_CONF:
LOG_EX(LOG_Debug, "====================Get cmd CMD_WIFI_CONF==========================\r\n");
LOG_EX(LOG_Debug, "wifi module get cfg msg is %s\r\n",pMsg->pMsg);
if(0 == busy_conf)
{
busy_conf =1;
status = api_wifi_get_cfg_value(pMsg->pMsg,&p_ssid,&p_passwd);
if(APP_WIFI_JSON_INVALID == status)
{
rsp_code = APP_WIFI_CFG_JSON_INVALID;
}else if(APP_WIFI_NULL == status)
{
rsp_code = APP_WIFI_CFG_FMT_INVALID;
}
if(APP_WIFI_CFG_SUCCESS== rsp_code)
{
LOG_EX(LOG_Debug, "+++++++Now wifi config cnt is %d\r\n",++wifi_connet_cnt);
LOG_EX(LOG_Debug, "ssid is %s,pd is %s\r\n",p_ssid,p_passwd);
status = api_wifi_get_str_raw(&p_ssid);
if(0 != status)
{
LOG_EX(LOG_Debug, "ERROR:get ssid str raw err");
goto END_DO;
}
status = api_wifi_get_str_raw(&p_passwd);
if(0 != status)
{
LOG_EX(LOG_Debug, "get passwd str raw err");
goto END_DO;
}
#if WIFI_INFO_BASE64
pBaseSsid = (char *)EvpBase64DecodeNoAlignV2((const char *)p_ssid, &ssidSize);
if(0 == ssidSize || NULL == pBaseSsid)
{
LOG_EX(LOG_Debug, "ERROR: ssid base64 error\r\n");
goto END_DO;
}
pBasePwd = (char *)EvpBase64DecodeNoAlignV2((const char *)p_passwd, &pwdSize);
LOG_EX(LOG_Debug, "INFO:pBasePwd is %d, pwdSize is %d\r\n",pBasePwd,pwdSize);
if(0 == pwdSize && NULL == pBasePwd)
{
LOG_EX(LOG_Debug, "warm: pswd null!!!\r\n");
}
free(p_ssid);
p_ssid = NULL;
free(p_passwd);
p_passwd = NULL;
p_ssid = pBaseSsid;
p_passwd = pBasePwd;
pBasePwd = NULL;
pBaseSsid = NULL;
#endif
LOG_EX(LOG_Debug, "New ssid is %s,new pd is %s\r\n",p_ssid,p_passwd);
event_label = WIFI_CONF_EVT_TAG;
if(NULL == p_wifi_interface)
{
p_wifi_interface = aw_wifi_on(wifi_event_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", event);
goto END_DO;
}
}
if(WIFIMG_WIFI_CONNECTED == aw_wifi_get_wifi_state())
{
status = p_wifi_interface->disconnect_ap(event_label);
if(0 != status)
{
LOG_EX(LOG_Debug, "ERROR:wifi disconnect event 0x%x\n", event);
goto END_DO;
}else
{
LOG_EX(LOG_Debug, "Disconnect the wifi connected ap\r\n");
}
}
ret_flag =0;
event_label++;
LOG_EX(LOG_Debug, "INFO: ---- Now connecting ap\r\n");
status = p_wifi_interface->connect_ap(p_ssid,p_passwd, event_label);
while(aw_wifi_get_wifi_state() == WIFIMG_WIFI_BUSING){
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(400000);
}
}
LOG_EX(LOG_Debug, "wifi module final event 0x%x.\r\n", event);
switch(event)
{
case WIFIMG_NETWORK_CONNECTED:
rsp_code = APP_WIFI_CFG_SUCCESS;
break;
case WIFIMG_DEV_BUSING_EVENT:
rsp_code = APP_WIFI_CFG_BUSY;
break;
case WIFIMG_NO_NETWORK_CONNECTING:
case WIFIMG_CMD_OR_PARAMS_ERROR:
rsp_code = APP_WIFI_CFG_PARAMS_ERR;
break;
case WIFIMG_KEY_MGMT_NOT_SUPPORT:
rsp_code = APP_WIFI_CFG_CODEC_ERR;
break;
case WIFIMG_NETWORK_NOT_EXIST:
rsp_code = APP_WIFI_CFG_SSID_NO_EXIT;
break;
case WIFIMG_PASSWORD_FAILED:
rsp_code = APP_WIFI_CFG_PWD_INVALID;
break;
case WIFIMG_NETWORK_DISCONNECTED:
case WIFIMG_CONNECT_TIMEOUT:
rsp_code = APP_WIFI_CFG_TIMEOUT;
break;
default:
rsp_code = APP_WIFI_CFG_OTHER;
break;
}
busy_conf =0;
}else
{
rsp_code = APP_WIFI_CFG_SYS_CFG_BUSY;
LOG_EX(LOG_Debug, "wifi module cfg busy!!!!\r\n");
}
LOG_EX(LOG_Debug, "wifi module CMD_WIFI_CONF rsp_code 0x%x.\r\n", rsp_code);
pResJson = (uint8_t *)api_wifi_res_json_get(rsp_code);
if(NULL != pResJson)
{
if(0 == rsp_code)mask = 1<<MODULE_CONTROLLER;
else mask = (1<<MODULE_BT)|(1<<MODULE_CONTROLLER);
status = api_wifi_module_cmd_send(mask,CMD_WIFI_CONF_RESP, (const char *)pResJson);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send dbus msg to bt er:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send msg : 0x%x to bt module\r\n",rsp_code);
}
}else
{
LOG_EX(LOG_Debug, "Wifi module get res json err\r\n");
}
END_DO:
busy_conf =0;
if(NULL != pResJson)
{
free(pResJson);
pResJson = NULL;
}
#if WIFI_INFO_BASE64
free(pBasePwd);
pBasePwd = NULL;
free(pBaseSsid);
pBasePwd = NULL;
#endif
free(p_ssid);
p_ssid = NULL;
free(p_passwd);
p_passwd = NULL;
break;
case CMD_WIFI_AUTO_CONN:
{
LOG_EX(LOG_Debug, "====================Get cmd CMD_WIFI_AUTO_CONN==========================\r\n");
if(1== busy_conf)
{
audo_code = APP_WIFI_AUTO_BT_CONF_COLLISION;
}
else if(1 == busy_auto){
audo_code = APP_WIFI_AUTO_SET_BUSY;
} else
{
audo_code = APP_WIFI_AUTO_SET_SUCCESS;
}
pResJson = (uint8_t *)api_wifi_res_json_get(audo_code);
if(NULL != pResJson)
{
status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_AUTO_CONN_RESP,(const char *)pResJson);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send dbus msg to control er:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send wifi_auto msg : 0x%x to control module\r\n",rsp_code);
}
}else
{
LOG_EX(LOG_Debug, "Wifi module get res json err\r\n");
}
if(NULL != pResJson)
{
free(pResJson);
pResJson = NULL;
}
if(0 == busy_auto && APP_WIFI_AUTO_SET_SUCCESS == audo_code)
{
busy_auto =1;
event_label = WIFI_AUTO_EVT_TAG;
if(NULL == p_wifi_interface)
{
p_wifi_interface = aw_wifi_on(wifi_event_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", event);
busy_auto =0;
break;
}
#if 0
status = p_wifi_interface->enable_all_network();
if(0 != status)
{
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", event);
busy_auto =0;
break;
}
#endif
if(WIFIMG_WIFI_CONNECTED == aw_wifi_get_wifi_state())
status = p_wifi_interface->disconnect_ap(event_label);
}
if(WIFIMG_WIFI_CONNECTED != aw_wifi_get_wifi_state())
{
while(aw_wifi_get_wifi_state() == WIFIMG_WIFI_BUSING){
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(2000000);
}
status = p_wifi_interface->connect_ap_auto(event_label);
while(aw_wifi_get_wifi_state() == WIFIMG_WIFI_BUSING)
{
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(2000000);
}
if(WIFIMG_WIFI_CONNECTED !=aw_wifi_get_wifi_state() )
{
if(0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTING,RSSI_INVALID_NUM))
{
LOG_EX(LOG_Debug, "Notify the wifi state error!!\r\n");
}
}
}else
{
int rssi = get_connected_ap_rssi();
if(0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTED,rssi))
{
LOG_EX(LOG_Debug, "Notify the wifi state error!!\r\n");
}
}
busy_auto =0;
status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_AUTO_CONN_OUT, NULL);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send dbus msg to control er:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send wifi_auto out to control module\r\n");
}
}
break;
}
case CMD_WIFI_STATE_REQ:
{
int stateVal[WIFI_STATE_KEY_CNT] ={0};
int rssi =0;
LOG_EX(LOG_Debug, "====================Get cmd CMD_WIFI_STATE_REQ==========================\r\n");
wifi_state = aw_wifi_get_wifi_state();
if(WIFIMG_WIFI_CONNECTED == wifi_state)
{
rssi = get_connected_ap_rssi();
LOG_EX(LOG_Debug, "---info: rssi is %d\r\n",rssi);
}
stateVal[WIFI_STATE_KEY_STATE] = wifi_state;
stateVal[WIFI_STATE_KEY_RSSI] = rssi;
pStateJson = (uint8_t *)api_wifi_state_json_get(wifiStateKeys,stateVal,WIFI_STATE_KEY_CNT);
if(NULL != pStateJson)
{
status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_STATE_RESP,(const char *) pStateJson);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send dbus msg to control er:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send wifi state msg : 0x%x to control module\r\n",wifi_state);
}
}else
{
LOG_EX(LOG_Debug, "Wifi module get res json err\r\n");
}
if(NULL != pStateJson)
{
free(pStateJson);
pStateJson = NULL;
}
break;
}
case CMD_WIFI_CHANGE_VALID_AP:
LOG_EX(LOG_Debug, "====================Get cmd CMD_WIFI_CHANGE_VALID_AP==========================\r\n");
event_label= WIFI_CHANGE_AP_EVT_TAG;;
status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_CHANGE_VALID_AP_RESP, NULL);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send dbus msg to control er:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send wifi_auto msg to control module\r\n");
}
if(NULL == p_wifi_interface)
{
p_wifi_interface = aw_wifi_on(wifi_event_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", event);
busy_auto =0;
break;
}
}
while(aw_wifi_get_wifi_state() == WIFIMG_WIFI_BUSING){
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(2000000);
}
status = p_wifi_interface->change_ap(event_label);
while(aw_wifi_get_wifi_state() == WIFIMG_WIFI_BUSING)
{
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(2000000);
}
status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_CHANGE_VALID_AP_OUT, NULL);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send dbus msg to control er:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send change ap out msg to control module\r\n");
}
break;
case CMD_WIFI_UNBIND_ACCOUNT:
LOG_EX(LOG_Debug, "recv DBUS MSG: CMD_WIFI_UNBIND_ACCOUNT\r\n");
if(NULL == p_wifi_interface)
{
p_wifi_interface = aw_wifi_on(wifi_event_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event L%d 0x%x\n", event, __LINE__);
break;
}
}
if(WIFIMG_WIFI_CONNECTED == aw_wifi_get_wifi_state())
{
status = p_wifi_interface->disconnect_ap(event_label);
if(0 != status)
{
LOG_EX(LOG_Debug, "ERROR:wifi disconnect event 0x%x L%d\n", event, __LINE__);
}
else
{
LOG_EX(LOG_Debug, "Disconnect the wifi connected ap L%d \r\n", __LINE__);
}
}
status = p_wifi_interface->remove_all_networks();
if(0 != status)
{
LOG_EX(LOG_Debug, "ERROR:remove_all_networks event 0x%x L%d\n", event, __LINE__);
}
else
{
LOG_EX(LOG_Debug, "remove_all_networks L%d \r\n", __LINE__);
}
break;
default:
break;
}
return NULL;
}
static void DBusMsgHandleLoop(void* param){
while(1)
{
PDBUS_MSG_PACK pMsg = DBusGetMessage();
PLIBUV_DBUS_PARAMS pInfo = DBusLibuvGetRuntime();
if(pMsg && pInfo)
DBusOnMessage(pInfo->pLoop, pInfo->pBus, pMsg);
if(pMsg)
DBusMsgCleanup(pMsg);
usleep(100);
}
return;
}
int main(int argv, char *argc[]){
int ret = 0;
DBusConnection* pBus = NULL;
uv_loop_t* pLoop = uv_default_loop();
int fd;
g_pDBusInterfaceName = g_pModInfoTable[MODULE_WIFI].modAliase;
LOG_EX(LOG_Debug, "Start the wifi communication\n");
//pBus = DBusWithLibuvInit(pLoop, g_pDBusInterfaceName, DBusOnMessage, NULL, NULL, &ret);
pBus = DBusWithLibuvInit(pLoop, g_pDBusInterfaceName, NULL, NULL, NULL, &ret);
if(pBus == NULL)
{
fprintf(stderr, "%s(%d): DBusWithLibuvInit Error: %d\n", g_pDBusInterfaceName, MODULE_PLAYER, ret);
return 0;
}
/* init a uv timer to report cur position */
uv_timer_init(pLoop, &timerCurPosMsg);
pthread_mutex_init(&ctrl_mutex, NULL);
uv_thread_t uvSyncThread;
uv_thread_create(&uvSyncThread, DBusMsgHandleLoop, NULL);
//uv_run(pLoop, UV_RUN_DEFAULT);
RunUVLoop(pLoop);
/*while(TRUE)
{
usleep(1000);
}*/
return 0;
}