#define TAG "wifi" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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 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 & 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 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;ibusCmd); 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<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<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<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; }