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

1588 lines
39 KiB
C++
Executable File

#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_SSID_JS_KEY "wifi_ssid"
#define WIFI_STATE_MAC_JS_KEY "wifi_mac"
#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 WLAN_BSSID_STR_RRE "Access Point: "
#define WLAN_ESSID_STR_RRE "ESSID:\""
#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;
/* TO BE FIXED */
//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_SSID,
WIFI_STATE_KEY_MAC,
WIFI_STATE_KEY_CNT,
}sWifiStateKeysEnum;
typedef enum
{
WIFI_EVT_KEY_EVT =0,
WIFI_EVT_KEY_RSSI,
WIFI_EVT_KEY_SSID,
WIFI_EVT_KEY_MAC,
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,WIFI_STATE_SSID_JS_KEY,WIFI_STATE_MAC_JS_KEY};
static char * wifiEvtKeys[WIFI_STATE_KEY_CNT] ={WIFI_STATE_EVT_JS_KEY,WIFI_STATE_RSSI_JS_KEY,WIFI_STATE_SSID_JS_KEY,WIFI_STATE_MAC_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_info(int *rssi, std::string &ssid, std::string &mac)
{
std::string shellCmd = WLAN_CONFIG_CMD_STR;
std::vector<std::string> vectorRes;
int status =0;
std::string out;
if (rssi == NULL)
return -1;
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, "warn: no rssi\r\n");
*rssi = RSSI_INVALID_NUM;
}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);
}
pos = out.find(WLAN_BSSID_STR_RRE);
if(pos == std::string::npos)
{
LOG_EX(LOG_Debug, "warn: no bssid\r\n");
mac = "";
}else
{
mac = out.substr(pos+strlen(WLAN_BSSID_STR_RRE),17);
LOG_EX(LOG_Debug, "---info: macStr is %s\r\n", mac.c_str());
}
pos = out.find(WLAN_ESSID_STR_RRE);
if(pos == std::string::npos)
{
LOG_EX(LOG_Debug, "warn: no essid\r\n");
ssid = "";
}else
{
std::string ssidStr = out.substr(pos+strlen(WLAN_ESSID_STR_RRE));
std::size_t pos_end = ssidStr.find("\n");
ssid = ssidStr.substr(0,pos_end-3);
LOG_EX(LOG_Debug, "---info: essidStr is %s\r\n", ssid.c_str());
}
}else
{
LOG_EX(LOG_Debug, "error: pipe excute error\r\n");
return -1;
}
return 0;
}
#if 0
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);
}
}
#endif
/*
*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_scan_result_json_get(char *scan_results, int len)
{
cJSON *pRoot = NULL;
cJSON *pArrayItem = NULL;
cJSON *pArrayItem2 = NULL;
char *p=NULL;
char *pCh = NULL;
const char *split1 = "\n";
const char *split2 = "\t";
LOG_EX(LOG_Debug, "Scan result: %s\n", scan_results);
pRoot = cJSON_CreateArray();
if(NULL == pRoot)
return NULL;
// Skip title
pCh = strtok(scan_results, split1);
// Skip mac
pCh = strtok(NULL, split2);
while (pCh != NULL)
{
pArrayItem = cJSON_CreateObject();
if(NULL == pArrayItem)
{
cJSON_Delete(pRoot);
return NULL;
}
// Skip Freq
pCh = strtok(NULL, split2);
// Signal level
pCh = strtok(NULL, split2);
cJSON_AddItemToObject(pArrayItem, "Strength", cJSON_CreateString(pCh));
// Flags
pCh = strtok(NULL, split2);
cJSON_AddItemToObject(pArrayItem, "Security", cJSON_CreateString(pCh));
// SSID
pCh = strtok(NULL, split1);
cJSON_AddItemToObject(pArrayItem, "Ssid", cJSON_CreateString(pCh));
// Freq, support only 2.4GHz currently.
cJSON_AddItemToObject(pArrayItem, "Frequence", cJSON_CreateString("2.4GHz"));
cJSON_AddItemToArray(pRoot, pArrayItem);
// Skip next mac
pCh = strtok(NULL, split2);
}
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(int state, int rssi, std::string ssid, std::string mac)
{
cJSON* pRoot = NULL;
char * p=NULL;
pRoot=cJSON_CreateObject();
if(NULL == pRoot)
return NULL;
cJSON_AddNumberToObject(pRoot,wifiStateKeys[WIFI_STATE_KEY_STATE],state);
cJSON_AddNumberToObject(pRoot,wifiStateKeys[WIFI_STATE_KEY_RSSI],rssi);
cJSON_AddStringToObject(pRoot,wifiStateKeys[WIFI_STATE_KEY_SSID],ssid.c_str());
cJSON_AddStringToObject(pRoot,wifiStateKeys[WIFI_STATE_KEY_MAC],mac.c_str());
p = cJSON_Print(pRoot);
if(NULL == p)
{
cJSON_Delete(pRoot);
return NULL;
}
cJSON_Delete(pRoot);
return p;
}
char * api_wifi_evt_json_get(int state, int rssi, std::string ssid, std::string mac)
{
cJSON* pRoot = NULL;
char * p=NULL;
pRoot=cJSON_CreateObject();
if(NULL == pRoot)
return NULL;
cJSON_AddNumberToObject(pRoot,wifiEvtKeys[WIFI_EVT_KEY_EVT],state);
cJSON_AddNumberToObject(pRoot,wifiEvtKeys[WIFI_EVT_KEY_RSSI],rssi);
cJSON_AddStringToObject(pRoot,wifiEvtKeys[WIFI_EVT_KEY_SSID],ssid.c_str());
cJSON_AddStringToObject(pRoot,wifiEvtKeys[WIFI_EVT_KEY_MAC],mac.c_str());
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, std::string ssid, std::string mac)
{
char * pEvtJson = NULL;
int status = 0;
pEvtJson = api_wifi_evt_json_get(state, rssi, ssid, mac);
if(NULL != pEvtJson)
{
//status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_STATE_NTF, pStateJson);
// TO BE FIXED. msg type & recver.
status = api_wifi_module_cmd_send((1<<MODULE_CLOCK_FRAME),CMD_WIFI_STATE_RESP, 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 void wifi_state_handle(struct Manager *w, int event_label)
{
int rssi = 0;
std::string ssid = "";
std::string mac = "";
LOG_EX(LOG_Debug, "app_wifi_manager event_label 0x%x, event 0x%x, state 0x%x\n", event_label, w->StaEvt.event, w->StaEvt.state);
switch(w->StaEvt.state)
{
case CONNECTING:
{
LOG_EX(LOG_Debug, "Connecting to the network......\n");
break;
}
case CONNECTED:
{
LOG_EX(LOG_Debug, "Connected to the AP\n");
start_udhcpc();
break;
}
case OBTAINING_IP:
{
LOG_EX(LOG_Debug, "Getting ip address......\n");
break;
}
case NETWORK_CONNECTED:
{
LOG_EX(LOG_Debug, "Successful network connection\n");
get_connected_ap_info(&rssi, ssid, mac);
if( 0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTED, rssi, ssid, mac))
{
LOG_EX(LOG_Debug, "Notify the wifi state error!!\r\n");
}
break;
}
case DISCONNECTED:
{
LOG_EX(LOG_Error, "Disconnected,the reason:%s\n",wmg_event_txt(w->StaEvt.event));
system("ifconfig wlan0 0.0.0.0");
if(0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_DISCONNECT, RSSI_INVALID_NUM, ssid, mac))
{
LOG_EX(LOG_Debug, "Notify the wifi state error!!\r\n");
}
break;
}
}
}
/*static void wifi_event_handle(tWIFI_EVENT wifi_event, void *buf, int event_label)
{
int rssi = 0;
std::string ssid = "";
std::string mac = "";
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;
get_connected_ap_info(&rssi, ssid, mac);
if( 0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTED, rssi, ssid, mac))
{
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, ssid, mac))
{
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 0
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;
static int wifi_off_mode = 0;
//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 mask =0;
static int wifi_connet_cnt =0;
enum wmgEvent wmg_event;
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 || 1 == wifi_off_mode)
{
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_state_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", aw_wifi_get_wifi_event());
goto END_DO;
}
}
if(NETWORK_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", aw_wifi_get_wifi_event());
goto END_DO;
}else
{
LOG_EX(LOG_Debug, "Disconnect the wifi connected ap\r\n");
}
}
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() == CONNECTING || aw_wifi_get_wifi_state() == OBTAINING_IP){
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(400000);
}
}
wmg_event = aw_wifi_get_wifi_event();
LOG_EX(LOG_Debug, "wifi module final event 0x%x.\r\n", wmg_event_txt(wmg_event));
if(aw_wifi_get_wifi_state() == NETWORK_CONNECTED)
{
rsp_code = APP_WIFI_CFG_SUCCESS;
}
else
{
switch(wmg_event)
{
case WSE_DEV_BUSING:
rsp_code = APP_WIFI_CFG_BUSY;
break;
case WSE_CMD_OR_PARAMS_ERROR:
rsp_code = APP_WIFI_CFG_PARAMS_ERR;
break;
case WSE_KEYMT_NO_SUPPORT:
rsp_code = APP_WIFI_CFG_CODEC_ERR;
break;
case WSE_NETWORK_NOT_EXIST:
rsp_code = APP_WIFI_CFG_SSID_NO_EXIT;
break;
case WSE_PASSWORD_INCORRECT:
rsp_code = APP_WIFI_CFG_PWD_INVALID;
break;
case WSE_CONNECTED_TIMEOUT:
case WSE_OBTAINED_IP_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)|(1<<MODULE_CLOCK_FRAME);
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 || 1 == wifi_off_mode)
{
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_state_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", aw_wifi_get_wifi_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(CONNECTED == aw_wifi_get_wifi_state())
status = p_wifi_interface->disconnect_ap(event_label);
}
if(CONNECTED != aw_wifi_get_wifi_state())
{
while(aw_wifi_get_wifi_state() == CONNECTING || aw_wifi_get_wifi_state() == OBTAINING_IP){
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() == CONNECTING || aw_wifi_get_wifi_state() == OBTAINING_IP)
{
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(2000000);
}
if(NETWORK_CONNECTED !=aw_wifi_get_wifi_state() )
{
std::string ssid = "";
std::string mac = "";
if(0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTING, RSSI_INVALID_NUM, ssid, mac))
{
LOG_EX(LOG_Debug, "Notify the wifi state error!!\r\n");
}
}
}
else
{
int rssi = 0;
std::string ssid = "";
std::string mac = "";
get_connected_ap_info(&rssi, ssid, mac);
if(0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTED, rssi, ssid, mac))
{
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_SCAN:
{
LOG_EX(LOG_Debug, "====================Get cmd CMD_WIFI_SCAN==========================\r\n");
char scan_results[4096] = {0};
int len = 4096;
event_label= WIFI_CONF_EVT_TAG;;
/*status = api_wifi_module_cmd_send((1<<MODULE_CLOCK_FRAME),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_state_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", aw_wifi_get_wifi_event());
busy_auto =0;
break;
}
}
while(aw_wifi_get_wifi_state() == CONNECTING || aw_wifi_get_wifi_state() == OBTAINING_IP){
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(2000000);
}
status = p_wifi_interface->get_scan_results(scan_results, &len);
if(status < 0)
{
break;
}
pResJson = (uint8_t *)api_wifi_scan_result_json_get(scan_results, len);
if(NULL != pResJson)
{
mask = 1<<MODULE_CLOCK_FRAME;
status = api_wifi_module_cmd_send(mask,CMD_WIFI_SCAN_RESP, (const char *)pResJson);
if (0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send dbus msg to clockFrame err:%d\r\n",status);
}
else
{
LOG_EX(LOG_Debug, "Wifi module is send msg : 0x%x to clockFrame module\r\n",pResJson);
}
}
else
{
LOG_EX(LOG_Debug, "Wifi module get scan results err\r\n");
}
if(NULL != pResJson)
{
free(pResJson);
pResJson = NULL;
}
break;
}
case CMD_WIFI_DISCONNECT:
{
LOG_EX(LOG_Debug, "====================Get cmd CMD_WIFI_DISCONNECT==========================\r\n");
event_label= WIFI_CONF_EVT_TAG;;
/*status = api_wifi_module_cmd_send((1<<MODULE_CLOCK_FRAME),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_state_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", aw_wifi_get_wifi_event());
busy_auto =0;
break;
}
}
while(aw_wifi_get_wifi_state() == CONNECTING || aw_wifi_get_wifi_state() == OBTAINING_IP){
LOG_EX(LOG_Debug, "wifi state busing,waiting\n");
usleep(2000000);
}
rsp_code = -1;
if(NETWORK_CONNECTED == aw_wifi_get_wifi_state())
{
rsp_code = p_wifi_interface->disconnect_ap(event_label);
if(0 != rsp_code)
{
LOG_EX(LOG_Debug, "ERROR:wifi disconnect event 0x%x\n", aw_wifi_get_wifi_event());
}else
{
LOG_EX(LOG_Debug, "Disconnect the wifi connected ap\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)
{
mask = (1<<MODULE_CLOCK_FRAME);
status = api_wifi_module_cmd_send(mask, CMD_WIFI_DISCONNECT_RESP, (const char *)pResJson);
if(0 != status)
{
LOG_EX(LOG_Debug, "Wifi module send dbus msg to clockFrame err:%d\r\n",status);
}else
{
LOG_EX(LOG_Debug, "Wifi module is send msg : 0x%x to clockFrame 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;
}
break;
}
case CMD_WIFI_STATE_REQ:
{
int rssi = 0;
std::string ssid = "";
std::string mac = "";
int wifi_state = aw_wifi_get_wifi_state();
LOG_EX(LOG_Debug, "====================Get cmd CMD_WIFI_STATE_REQ==========================\r\n");
if(NETWORK_CONNECTED == wifi_state)
{
get_connected_ap_info(&rssi, ssid, mac);
}
pStateJson = (uint8_t *)api_wifi_state_json_get(wifi_state, rssi, ssid, mac);
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_state_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event 0x%x\n", aw_wifi_get_wifi_event());
busy_auto =0;
break;
}
}
while(aw_wifi_get_wifi_state() == CONNECTING || aw_wifi_get_wifi_state() == OBTAINING_IP){
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() == CONNECTING || aw_wifi_get_wifi_state() == OBTAINING_IP)
{
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_state_handle, event_label);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Debug, "wifi on failed event L%d 0x%x\n", aw_wifi_get_wifi_event(), __LINE__);
break;
}
}
if(NETWORK_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", aw_wifi_get_wifi_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", aw_wifi_get_wifi_event(), __LINE__);
}
else
{
LOG_EX(LOG_Debug, "remove_all_networks L%d \r\n", __LINE__);
}
break;
case CMD_WIFI_ON:
status = APP_WIFI_CFG_SUCCESS;
if (wifi_off_mode == 0)
{
LOG_EX(LOG_Error, "Wifi interface already on, turn on wifi failed!\n");
status = APP_WIFI_CFG_SYS_ERR;
}
else
{
p_wifi_interface = aw_wifi_on(wifi_state_handle, WIFI_CONF_EVT_TAG);
if(p_wifi_interface == NULL){
LOG_EX(LOG_Error, "Turn on wifi failed!\n");
status = APP_WIFI_CFG_SYS_ERR;
}
else
{
wifi_off_mode = 0;
LOG_EX(LOG_Error, "Turn on wifi succeed!\n");
}
}
pResJson = (uint8_t *)api_wifi_res_json_get(status);
if(NULL != pResJson)
{
status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_ON_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;
}
break;
case CMD_WIFI_OFF:
status = APP_WIFI_CFG_SUCCESS;
if (p_wifi_interface == NULL || wifi_off_mode == 1)
{
LOG_EX(LOG_Error, "Wifi interface not on, turn off wifi failed!\n");
status = APP_WIFI_CFG_SYS_ERR;
}
else
{
if(NETWORK_CONNECTED == aw_wifi_get_wifi_state())
{
status = p_wifi_interface->disconnect_ap(WIFI_CONF_EVT_TAG);
if(0 != status)
{
LOG_EX(LOG_Error, "ERROR:wifi disconnect event 0x%x\n", aw_wifi_get_wifi_event());
status = APP_WIFI_CFG_SYS_ERR;
}else
{
LOG_EX(LOG_Debug, "Disconnect the wifi connected ap\r\n");
status = APP_WIFI_CFG_SUCCESS;
}
}
/* turn off wifi */
status = aw_wifi_off(p_wifi_interface);
if(status < 0)
{
LOG_EX(LOG_Error, "Turn off wifi failed!\n");
status = APP_WIFI_CFG_SYS_ERR;
}
else
{
LOG_EX(LOG_Error, "Turn off wifi succeed!\n");
wifi_off_mode = 1;
p_wifi_interface = NULL;
status = APP_WIFI_CFG_SUCCESS;
}
}
pResJson = (uint8_t *)api_wifi_res_json_get(status);
if(NULL != pResJson)
{
status = api_wifi_module_cmd_send((1<<MODULE_CONTROLLER),CMD_WIFI_OFF_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;
}
break;
#if 0
case CMD_WIFI_BL_CONF:
case CMD_WIFI_BL_CONF_ABORT:
case CMD_WIFI_BL_CLOUD_CHALLENGE_SIGNATURE:
case CMD_WIFI_BL_CLOUD_SHAREDKEY:
case CMD_WIFI_BL_DEV_TOKEN_BOUND:
bl_event_handle(pMsg->busCmd, pMsg->pMsg);
break;
#endif
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;
//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);
/* TO BE FIXED */
//pthread_mutex_init(&ctrl_mutex, NULL);
LOG_EX(LOG_Debug, "Start the wifi communication\n");
uv_thread_t uvSyncThread;
uv_thread_create(&uvSyncThread, DBusMsgHandleLoop, NULL);
//uv_run(pLoop, UV_RUN_DEFAULT);
RunUVLoop(pLoop);
/*while(TRUE)
{
usleep(1000);
}*/
return 0;
}