diff --git a/package/allwinner/wifimanager/demo/Makefile b/package/allwinner/wifimanager/demo/Makefile index e984f97c8..2a616247e 100755 --- a/package/allwinner/wifimanager/demo/Makefile +++ b/package/allwinner/wifimanager/demo/Makefile @@ -3,7 +3,8 @@ include $(TINA_BUILD_TOP)/package/netease/Makefile.common include $(TINA_BUILD_TOP)/build/nls.mk target = wifi_connect_ap_test -INCLUDES += -I$(CONFIG_PREFIX)/usr/include \ +MK_PWD = ./ +INCLUDES += -I$(CONFIG_PREFIX)/usr/include -I$(MK_PWD)/softap \ -I ../src/include CPP_SRCS = wifi_connect_ap.cpp diff --git a/package/allwinner/wifimanager/demo/wifi_get_netid.c b/package/allwinner/wifimanager/demo/wifi_get_netid.c index 22dae705e..a8c4dbb7f 100755 --- a/package/allwinner/wifimanager/demo/wifi_get_netid.c +++ b/package/allwinner/wifimanager/demo/wifi_get_netid.c @@ -245,7 +245,7 @@ int main(int argv, char *argc[]){ } - ret = p_wifi_interface->get_netid(argc[1], key_mgmt, net_id, &id_len); + ret = p_wifi_interface->get_netid(argc[1], key_mgmt, net_id); if(ret == 0){ wmg_printf(MSG_INFO,"The netid of your network is %s\n",net_id); wmg_printf(MSG_INFO,"The lenght of netid is %d\n",id_len); diff --git a/package/allwinner/wifimanager/demo/wifi_stop_restart_scan_test.c b/package/allwinner/wifimanager/demo/wifi_stop_restart_scan_test.c index 1d29abeba..833309b52 100755 --- a/package/allwinner/wifimanager/demo/wifi_stop_restart_scan_test.c +++ b/package/allwinner/wifimanager/demo/wifi_stop_restart_scan_test.c @@ -215,7 +215,7 @@ int main(int argv, char *argc[]){ wmg_printf(MSG_INFO,"********************************************\n"); wmg_printf(MSG_INFO,"==================stop scan================!\n"); wmg_printf(MSG_INFO,"********************************************\n"); - p_wifi_interface->stop_scan(); + //p_wifi_interface->stop_scan(); for(i=0;i<10;i++) { usleep(2000000); @@ -223,7 +223,7 @@ int main(int argv, char *argc[]){ wmg_printf(MSG_INFO,"********************************************\n"); wmg_printf(MSG_INFO,"==================restart scan=============!\n"); wmg_printf(MSG_INFO,"********************************************\n"); - p_wifi_interface->restart_scan(); + //p_wifi_interface->restart_scan(); } return 0; diff --git a/package/allwinner/wifimanager/smartaudio/Makefile b/package/allwinner/wifimanager/smartaudio/Makefile index a795c14ca..eebfe1902 100755 --- a/package/allwinner/wifimanager/smartaudio/Makefile +++ b/package/allwinner/wifimanager/smartaudio/Makefile @@ -4,7 +4,7 @@ include $(TINA_BUILD_TOP)/build/nls.mk target = app_wifi_manager MK_PWD = ./ -INCLUDES += -I$(CONFIG_PREFIX)/usr/include -I$(MK_PWD)/softap +INCLUDES += -I$(CONFIG_PREFIX)/usr/include -I$(MK_PWD)/softap -I$(MK_PWD)/../src/include CPP_SRCS = app_wifi_manager.cpp CPP_OBJS := $(CPP_SRCS:.cpp=.o) diff --git a/package/allwinner/wifimanager/smartaudio/app_wifi_manager.cpp b/package/allwinner/wifimanager/smartaudio/app_wifi_manager.cpp index 99c7fcce3..79df815d2 100755 --- a/package/allwinner/wifimanager/smartaudio/app_wifi_manager.cpp +++ b/package/allwinner/wifimanager/smartaudio/app_wifi_manager.cpp @@ -27,11 +27,12 @@ #include - #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" @@ -42,6 +43,8 @@ #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 @@ -66,6 +69,8 @@ typedef enum { WIFI_STATE_KEY_STATE =0, WIFI_STATE_KEY_RSSI, + WIFI_STATE_KEY_SSID, + WIFI_STATE_KEY_MAC, WIFI_STATE_KEY_CNT, }sWifiStateKeysEnum; @@ -74,6 +79,8 @@ typedef enum { WIFI_EVT_KEY_EVT =0, WIFI_EVT_KEY_RSSI, + WIFI_EVT_KEY_SSID, + WIFI_EVT_KEY_MAC, WIFI_EVT_KEY_CNT, }sWifiEvtKeysEnum; @@ -117,8 +124,9 @@ typedef enum 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}; + +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}; @@ -178,14 +186,16 @@ int pipeExcute(const char * cmd,std::vector & recVec ) return 0; } -int get_connected_ap_rssi() +int get_connected_ap_info(int *rssi, std::string &ssid, std::string &mac) { std::string shellCmd = WLAN_CONFIG_CMD_STR; std::vector vectorRes; int status =0; - int rssi = RSSI_INVALID_NUM; std::string out; + if (rssi == NULL) + return -1; + status =pipeExcute(shellCmd.c_str(),vectorRes); if(0 == status) { @@ -196,24 +206,49 @@ int get_connected_ap_rssi() if(pos == std::string::npos) { - LOG_EX(LOG_Debug, "warm: no rssi\r\n"); - status = -1; + 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()); + LOG_EX(LOG_Debug, "---info: rssiStr is %s\r\n",rssiStr.c_str()); - rssi = std::stoi(rssiStr); + *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"); - status = -1; + return -1; } - - return (0 == status)?rssi:RSSI_INVALID_NUM; + return 0; } @@ -309,20 +344,19 @@ char * api_wifi_state_json_get(char * p_key,int res) return p; } -char * api_wifi_state_json_get(char * p_key[],int res[],int size) +char * api_wifi_state_json_get(int state, int rssi, std::string ssid, std::string mac) { cJSON* pRoot = NULL; - char * p=NULL; + char * p=NULL; pRoot=cJSON_CreateObject(); - if(NULL == pRoot) return NULL; + if(NULL == pRoot) + return NULL; - - - for(int i=0;ipMsg); - if(0 == busy_conf) + if(0 == busy_conf || 1 == wifi_off_mode) { busy_conf =1; status = api_wifi_get_cfg_value(pMsg->pMsg,&p_ssid,&p_passwd); @@ -877,9 +934,8 @@ END_DO: break; case CMD_WIFI_AUTO_CONN: { - LOG_EX(LOG_Debug, "====================Get cmd CMD_WIFI_AUTO_CONN==========================\r\n"); - if(1== busy_conf) + if(1== busy_conf || 1 == wifi_off_mode) { audo_code = APP_WIFI_AUTO_BT_CONF_COLLISION; } @@ -956,7 +1012,9 @@ END_DO: if(WIFIMG_WIFI_CONNECTED !=aw_wifi_get_wifi_state() ) { - if(0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTING,RSSI_INVALID_NUM)) + 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"); } @@ -964,8 +1022,11 @@ END_DO: }else { - int rssi = get_connected_ap_rssi(); - if(0 != api_wifi_state_notify(APP_WIFI_EVT_NTF_CONNECTED,rssi)) + 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"); } @@ -990,23 +1051,19 @@ END_DO: } case CMD_WIFI_STATE_REQ: { - int stateVal[WIFI_STATE_KEY_CNT] ={0}; - int rssi =0; + int rssi = 0; + std::string ssid = ""; + std::string mac = ""; 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); + get_connected_ap_info(&rssi, ssid, mac); } - - - 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); + pStateJson = (uint8_t *)api_wifi_state_json_get(wifi_state, rssi, ssid, mac); if(NULL != pStateJson) { status = api_wifi_module_cmd_send((1<disconnect_ap(WIFI_CONF_EVT_TAG); + if(0 != status) + { + LOG_EX(LOG_Error, "ERROR:wifi disconnect event 0x%x\n", 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<busCmd, pMsg->pMsg); + break; default: break; } @@ -1141,8 +1308,6 @@ int main(int argv, char *argc[]){ 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); @@ -1152,12 +1317,13 @@ int main(int argv, char *argc[]){ 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); + LOG_EX(LOG_Debug, "Start the wifi communication\n"); uv_thread_t uvSyncThread; uv_thread_create(&uvSyncThread, DBusMsgHandleLoop, NULL); diff --git a/package/allwinner/wifimanager/src/Makefile b/package/allwinner/wifimanager/src/Makefile index 13e4b8856..c891caa57 100755 --- a/package/allwinner/wifimanager/src/Makefile +++ b/package/allwinner/wifimanager/src/Makefile @@ -10,7 +10,7 @@ LIBS += -lpthread -ldl -lrt -liconv local_LDFLAGS := $(BUILD_COMMON_LIB) $(ICONV_LDFLAGS) -liconv SRCS =wifimanager.c wifi_event.c wifi_state_machine.c network_manager.c \ - wifi.c wpa_supplicant_conf.c udhcpc_thread.c tool.c wmg_debug.c + wifi.c wpa_supplicant_conf.c udhcpc_thread.c tool.c wmg_debug.c broadlink.c install: -@mkdir -p $(CONFIG_PREFIX)/usr/lib @@ -19,12 +19,12 @@ install: #libwifimg.so ifeq ($(CONFIG_ARCH), "aarch64") - SRCS += libwpa_client_64.a + SRCS += libwpa_client_64.a libfastcon.a else ifeq ($(CONFIG_SOFT_FLOAT), y) - SRCS += libwpa_client_softft.a + SRCS += libwpa_client_softft.a libfastcon.a else - SRCS += libwpa_client.a + SRCS += libwpa_client.a libfastcon.a endif endif $(target): $(SRCS) diff --git a/package/allwinner/wifimanager/src/broadlink.c b/package/allwinner/wifimanager/src/broadlink.c new file mode 100755 index 000000000..4df4546c9 --- /dev/null +++ b/package/allwinner/wifimanager/src/broadlink.c @@ -0,0 +1,890 @@ +#ifndef PLATFORM_CPU +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(PLATFORM_R16) || defined (PLATFORM_CPU) +#include "log.h" +#include "libuv_dbus.h" +//#include "boardlink_iot.h" +#else +#include +#include +//#include +#endif +#include +#include +#include "broadlink.h" +#include "sdk.h" +#include "sdk_errno.h" + + +#define BL_IOT_PROTO (30) +#define BL_MSG_BUF_MAX (1024 * 4) + +static int g_nlSock = -1; +static unsigned char g_MsgSendBuf[BL_MSG_BUF_MAX]; +WL_INFO g_WlInfo; + +void bl_recv_msg_thread(void *pParam) +{ + unsigned char msgBuf[BL_MSG_BUF_MAX]; + unsigned char *pProbeReqPayload = NULL; + struct iovec iov = {msgBuf, BL_MSG_BUF_MAX}; + struct nlmsghdr *pMsgHdr; + struct sockaddr_nl fromAddr; + struct msghdr msgHdr = {&fromAddr, sizeof(fromAddr), &iov, 1, NULL, 0, 0}; + int ret; + int i; + + memset(&fromAddr, 0, sizeof(fromAddr)); + fromAddr.nl_family = AF_NETLINK; + fromAddr.nl_pid = 0; + fromAddr.nl_groups = 0; + + if(0 != bl_create_socket()) + { + LOG_EX(LOG_Error, "Create netlink socket error\n"); + return ; + } + + while(TRUE) + { + int len = recvmsg(g_nlSock, &msgHdr, 0); + + if(len > 0) + { + for(pMsgHdr = (struct nlmsghdr *)msgBuf; + NLMSG_OK(pMsgHdr, len); + pMsgHdr = NLMSG_NEXT(pMsgHdr, len)) + { + if(pMsgHdr->nlmsg_type == NLMSG_DONE) + { + continue; + } + else if(pMsgHdr->nlmsg_type == NLMSG_ERROR) + { + continue; + } + else + { + PBL_IOT_MSG pMsg = (PBL_IOT_MSG)NLMSG_DATA(pMsgHdr); + //LOG_EX(LOG_Debug, "Recv netlink msg type %d\n", pMsg->msgType); + if (MSG_GET_WLAN_INFO == pMsg->msgType) + { + WL_INFO *wlInfo = NULL; + wlInfo = (WL_INFO *)pMsg->msgData; + bl_set_wlan_info(wlInfo); + LOG_EX(LOG_Debug, "SSID %s, Channel %d, dev mac %x:%x:%x:%x:%x:%x, bssid mac %x:%x:%x:%x:%x:%x\n", + wlInfo->ssid, wlInfo->channel, + wlInfo->dev_mac[0], wlInfo->dev_mac[1], wlInfo->dev_mac[2], wlInfo->dev_mac[3], wlInfo->dev_mac[4], wlInfo->dev_mac[5], + wlInfo->bssid[0], wlInfo->bssid[1],wlInfo->bssid[2],wlInfo->bssid[3],wlInfo->bssid[4],wlInfo->bssid[5]); + } + else if (MSG_BYPASS_MODE == pMsg->msgType) + { + LOG_EX(LOG_Debug, "Recv BROADLINK Probe Request! Len %d\n", pMsg->msglen); + sdk_pkt_process(pMsg->msgData, pMsg->msglen); + } + } + } + } + + usleep(1000); + } + //pthread_detach(pthread_self()); +} + +int bl_create_socket(void) +{ + struct sockaddr_nl srcAddr; + + g_nlSock = socket(AF_NETLINK, SOCK_RAW, BL_IOT_PROTO); + + if(g_nlSock == -1) + { + LOG_EX(LOG_Error, "Create netlink socker error: %s\n", strerror(errno)); + return -ERR_CREATE_SOCKET; + } + + memset(&srcAddr, 0, sizeof(srcAddr)); + srcAddr.nl_family = AF_NETLINK; + srcAddr.nl_pid = getpid(); + srcAddr.nl_groups = 0; + + if(bind(g_nlSock, (struct sockaddr*)&srcAddr, sizeof(srcAddr)) < 0) + { + LOG_EX(LOG_Error, "Bind netlink socket failed: %s", strerror(errno)); + close(g_nlSock); + return -ERR_BIND_SOCKET; + } + + return 0; +} + +static int __bl_send_netlink_msg(unsigned char* pData, unsigned int len) +{ + struct iovec iov; + struct sockaddr_nl dstAddr; + struct nlmsghdr* pMsgHdr = (struct nlmsghdr*)g_MsgSendBuf; + struct msghdr msgHdr = {&dstAddr, sizeof(dstAddr), &iov, 1, NULL, 0, 0}; + + memset(&dstAddr, 0, sizeof(struct sockaddr_nl)); + dstAddr.nl_family = AF_NETLINK; + dstAddr.nl_pid = 0; + dstAddr.nl_groups = 0; + + memset(pMsgHdr, 0, BL_MSG_BUF_MAX); + pMsgHdr->nlmsg_len = len; + pMsgHdr->nlmsg_pid = getpid(); + pMsgHdr->nlmsg_flags = 0; + memcpy(NLMSG_DATA(pMsgHdr), pData, len); + + iov.iov_base = pMsgHdr; + iov.iov_len = NLMSG_SPACE(BL_MSG_BUF_MAX); + + return sendmsg(g_nlSock, &msgHdr, 0); +} + +int bl_send_msg(BL_IOT_MSG_TYPE msgType, unsigned char* pData, unsigned int len) +{ + BL_IOT_MSG blMsg; + + memset(&blMsg, 0, sizeof(BL_IOT_MSG)); + blMsg.msgType = msgType; + blMsg.msglen = len; + strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS); + + if(pData && len > 0) + { + memcpy(blMsg.msgData, pData, len); + } + return __bl_send_netlink_msg((unsigned char*)&blMsg, BL_IOT_MSG_LEN(len)); +} + +/** + * __bl_binary_to_string - Transfer binary array to string. + * For example, {0x11,0x22,0x33,0x44} will be transfered to "11223344". + * @binary: Pointer to the binary array. + * @binary_len: Length of the binary array. + * @return: Pointer to the result string. + * + * Remember to free the string space after using the string!! + */ +static char *__bl_binary_to_string(const uint8 *binary, uint16 binary_len) +{ + char *str = NULL; + int str_len = binary_len * 2 + 1; + int i; + + if (NULL == binary || 0 == binary_len) + return NULL; + + str = malloc(str_len); + if (NULL == str) + { + LOG_EX(LOG_Error, "Malloc error!\n"); + return NULL; + } + + memset(str, 0, str_len); + /* Transfer from binary to string */ + for (i=0; i= '0' && string[i*2] <= '9') + (*array)[i] = (string[i*2] - '0') * 0x10; + else if (string[i*2] >= 'a' && string[i*2] <= 'f') + (*array)[i] = (string[i*2] - 'a' + 10) * 0x10; + else + { + free(*array); + *array = NULL; + LOG_EX(LOG_Debug, "Unable to recognize charactor!\n"); + return -1; + } + + if (string[i*2+1] >= '0' && string[i*2+1] <= '9') + (*array)[i] += string[i*2+1] - '0'; + else if (string[i*2+1] >= 'a' && string[i*2+1] <= 'f') + (*array)[i] += string[i*2+1] - 'a' + 10; + else + { + free(*array); + *array = NULL; + LOG_EX(LOG_Debug, "Unable to recognize charactor!\n"); + return -1; + } + } + + return 0; +} + + +static int __bl_dbus_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 bl send module cmd err is %d\r\n",status); + } + + return status; +} + +static int __bl_get_challenge_signature(char * p_txt, uint8 **pp_challenge, uint16 *challenge_len, uint8 **pp_signature, uint16 *signature_len) +{ + cJSON *p_root =NULL; + cJSON *p_challenge_item = NULL; + cJSON *p_signature_item = NULL; + char *p_tmp = NULL; + int ret = 0; + + p_root=cJSON_Parse(p_txt); + if(NULL == p_root) + goto fail; + + /* Get cloud challenge value. */ + p_challenge_item = cJSON_GetObjectItem(p_root, "challenge"); + if(NULL == p_challenge_item) + goto fail; + + p_tmp = p_challenge_item->valuestring; + if(NULL == p_tmp) + goto fail; + + ret = __bl_string_to_binary(p_tmp, pp_challenge, challenge_len); + if (ret) + { + LOG_EX(LOG_Error, "Transfer cloud challenge error!\n"); + goto fail; + } + + /* Get cloud signature value. */ + p_signature_item = cJSON_GetObjectItem(p_root, "signature"); + if(NULL == p_signature_item) + goto fail; + + p_tmp = p_signature_item->valuestring; + if(NULL == p_tmp) + goto fail; + + ret = __bl_string_to_binary(p_tmp, pp_signature, signature_len); + if (ret) + { + LOG_EX(LOG_Error, "Transfer cloud signature error!\n"); + goto fail; + } + + cJSON_Delete(p_root); + + return 0; +fail: + if (*pp_challenge) + { + free(*pp_challenge); + *pp_challenge = NULL; + *challenge_len = 0; + } + if (*pp_signature) + { + free(*pp_signature); + *pp_signature = NULL; + *signature_len = 0; + } + if (p_root) + cJSON_Delete(p_root); + + return -1; +} + +static int __bl_get_shared_key(char * p_txt, uint8 **pp_shared_key, uint16 *shared_key_len) +{ + cJSON *p_root =NULL; + cJSON *p_shared_key_item = NULL; + char *p_tmp = NULL; + int ret = 0; + + p_root=cJSON_Parse(p_txt); + if(NULL == p_root) + goto fail; + + /* Get shared key value. */ + p_shared_key_item = cJSON_GetObjectItem(p_root, "shareKey"); + if(NULL == p_shared_key_item) + goto fail; + + p_tmp = p_shared_key_item->valuestring; + if(NULL == p_tmp) + goto fail; + + ret = __bl_string_to_binary(p_tmp, pp_shared_key, shared_key_len); + if (ret) + { + LOG_EX(LOG_Error, "Transfer shared key error!\n"); + goto fail; + } + + cJSON_Delete(p_root); + + return 0; +fail: + if (*pp_shared_key) + { + free(*pp_shared_key); + *pp_shared_key = NULL; + *shared_key_len = 0; + } + + if (p_root) + cJSON_Delete(p_root); + + return -1; +} + + + +int32 bl_printf(const char* format, ...) +{ + //LOG_EX(level, format, args...) + va_list args; + va_start(args, format); + //vprintf(fmt, args); + IHW_LOG(LOG_Debug, format, args); + va_end(args); + + return 0; +} + +int32 bl_found_device(sdk_product_info_t *product_info) +{ + LOG_EX(LOG_Debug, "Found Broadlink device!\n"); + LOG_EX(LOG_Debug, "verdor id %d!\n", product_info->vendor_id); + LOG_EX(LOG_Debug, "product id %d!\n", product_info->product_id); + LOG_EX(LOG_Debug, "mac %x:%x:%x:%x:%x:%x!\n", product_info->mac[0], product_info->mac[1], product_info->mac[2], product_info->mac[3], product_info->mac[4], product_info->mac[5]); + + if (0 != sdk_user_auth_to_start_config()) + { + LOG_EX(LOG_Debug, "sdk_user_auth_to_start_config failed!\n"); + } + else + { + LOG_EX(LOG_Debug, "sdk_user_auth_to_start_config succeed!\n"); + } + + return 0; +} + +int32 bl_challenge_post(sdk_product_info_t *product_info, uint8 *challenge, uint16 challenge_len) +{ + cJSON* pRoot = NULL; + char *pInfoJson = NULL; + char *dev_info = NULL; + char *dev_id = NULL; + char *challenge_str = NULL; + + LOG_EX(LOG_Debug, "Begin device challenge!\n"); + + pRoot = cJSON_CreateObject(); + if(NULL == pRoot) + { + LOG_EX(LOG_Error, "cJSON_CreateObject error!\n"); + return -1; + } + + /* Add device_info string to json */ + dev_info = __bl_binary_to_string(product_info->device_info, product_info->device_info_len); + if (NULL == dev_info) + { + LOG_EX(LOG_Error, "Transfer device_info error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add device_info string %s\n", dev_info); + cJSON_AddStringToObject(pRoot, "deviceInfo", dev_info); + free(dev_info); + + /* Add device_id string to json */ + dev_id = __bl_binary_to_string(product_info->device_id, product_info->device_id_len); + if (NULL == dev_id) + { + LOG_EX(LOG_Error, "Transfer device_id error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add device_id string %s\n", dev_id); + cJSON_AddStringToObject(pRoot, "deviceId", dev_id); + free(dev_id); + + /* Add device challenge string to json */ + challenge_str = __bl_binary_to_string(challenge, challenge_len); + if (NULL == challenge_str) + { + LOG_EX(LOG_Error, "Transfer device challenge error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add challenge string %s\n", challenge_str); + cJSON_AddStringToObject(pRoot, "challenge", challenge_str); + free(challenge_str); + + pInfoJson = cJSON_Print(pRoot); + if(NULL == pInfoJson) + { + cJSON_Delete(pRoot); + return -1; + } + cJSON_Delete(pRoot); + + LOG_EX(LOG_Debug, "Send device info %s\n", pInfoJson); + __bl_dbus_cmd_send(1<device_info, product_info->device_info_len); + if (NULL == dev_info) + { + LOG_EX(LOG_Error, "Transfer device_info error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add device_info string %s\n", dev_info); + cJSON_AddStringToObject(pRoot, "deviceInfo", dev_info); + free(dev_info); + + /* Add device_id string to json */ + dev_id = __bl_binary_to_string(product_info->device_id, product_info->device_id_len); + if (NULL == dev_id) + { + LOG_EX(LOG_Error, "Transfer device_id error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add device_id string %s\n", dev_id); + cJSON_AddStringToObject(pRoot, "deviceId", dev_id); + free(dev_id); + + /* Add device signature string to json */ + signature_str = __bl_binary_to_string(signature, signature_len); + if (NULL == signature_str) + { + LOG_EX(LOG_Error, "Transfer device signature error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add signature string %s\n", signature_str); + cJSON_AddStringToObject(pRoot, "signature", signature_str); + free(signature_str); + + pSignatureJson = cJSON_Print(pRoot); + if(NULL == pSignatureJson) + { + cJSON_Delete(pRoot); + return -1; + } + cJSON_Delete(pRoot); + + LOG_EX(LOG_Debug, "Send device signature %s\n", pSignatureJson); + __bl_dbus_cmd_send(1<device_info, product_info->device_info_len); + if (NULL == dev_info) + { + LOG_EX(LOG_Error, "Transfer device_info error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add device_info string %s\n", dev_info); + cJSON_AddStringToObject(pRoot, "deviceInfo", dev_info); + free(dev_info); + + /* Add device_id string to json */ + dev_id = __bl_binary_to_string(product_info->device_id, product_info->device_id_len); + if (NULL == dev_id) + { + LOG_EX(LOG_Error, "Transfer device_id error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add device_id string %s\n", dev_id); + cJSON_AddStringToObject(pRoot, "deviceId", dev_id); + free(dev_id); + + /* Add device token string to json */ + token_str = __bl_binary_to_string(token, token_len); + if (NULL == token_str) + { + LOG_EX(LOG_Error, "Transfer device token error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "add token string %s\n", token_str); + cJSON_AddStringToObject(pRoot, "tokenInfo", token_str); + free(token_str); + + pTokenJson = cJSON_Print(pRoot); + if(NULL == pTokenJson) + { + cJSON_Delete(pRoot); + return -1; + } + cJSON_Delete(pRoot); + + LOG_EX(LOG_Debug, "Send device token %s\n", pTokenJson); + __bl_dbus_cmd_send(1<channel = g_WlInfo.channel; + memcpy(usrInit->device_mac, g_WlInfo.dev_mac, ETH_ALEN); + memcpy(usrInit->bssid, g_WlInfo.bssid, ETH_ALEN); + + return 0; +} + +int bl_sdk_init() +{ + sdk_user_init_t usrInit; + int ret = 0; + + memset(&usrInit, 0, sizeof(sdk_user_init_t)); + if (0 != bl_get_wlan_info(&usrInit)) + { + LOG_EX(LOG_Error, "Get wlan info error\n"); + return -1; + } + usrInit.send_wlan_type = BL_IOT_MGMT_FRAME_TYPE_ACTION; + usrInit.fastcon_master_id[0]=BL_MASTER_ID_FOR_NETEASE; + usrInit.fastcon_master_id[1]=0; + usrInit.fastcon_master_id[2]=0; + usrInit.fastcon_master_id[3]=0; + usrInit.sdk_config.memset = memset; + usrInit.sdk_config.memcpy = memcpy; + usrInit.sdk_config.memcmp = memcmp; + usrInit.sdk_config.printf = bl_printf; + usrInit.sdk_config.found_device = bl_found_device; + usrInit.sdk_config.challenge_post = bl_challenge_post; + usrInit.sdk_config.signature_post = bl_signature_post; + usrInit.sdk_config.device_token_post = bl_device_token_post; + usrInit.sdk_config.config_result_post = bl_config_result_post; + usrInit.sdk_config.send_raw_pkt = bl_raw_pkt_send; + + if (0 != (ret = sdk_init(&usrInit))) + { + LOG_EX(LOG_Error, "Broadlink sdk init error code %d!\n", ret); + return -1; + } + LOG_EX(LOG_Debug, "Broadlink sdk init OK!\n"); + + return 0; +} + +int bl_event_handle(unsigned int cmd, const char* msg) +{ + uint8 *cloud_challenge = NULL; + uint16 cloud_challenge_len = 0; + uint8 *cloud_signature = NULL; + uint16 cloud_signature_len = 0; + + uint8 *shared_key = NULL; + uint16 shared_key_len = 0; + uint8 ssid[SSID_MAX_LEN] = {0}; + uint8 key_mgmt[KEY_MGMT_LEN] = {0}; + uint8 password[PASSWORD_MAX_LEN] = {0}; + int priority; + + int ret = 0; + + switch(cmd) + { + case CMD_WIFI_BL_CONF: + bl_enable_discovery_mode(); + bl_sdk_init(); + break; + + case CMD_WIFI_BL_CLOUD_CHALLENGE_SIGNATURE: + ret = __bl_get_challenge_signature(msg, &cloud_challenge, &cloud_challenge_len, &cloud_signature, &cloud_signature_len); + if (ret) + { + LOG_EX(LOG_Debug, "__bl_get_challenge_signature failed!\n"); + return -1; + } + + ret = sdk_challenge_and_signature_set(cloud_challenge, cloud_challenge_len, cloud_signature, cloud_signature_len); + if (ret) + LOG_EX(LOG_Error, "sdk_challenge_and_signature_set failed, error code %d!\n", ret); + else + LOG_EX(LOG_Debug, "sdk_challenge_and_signature_set succeed!\n"); + + if (cloud_challenge) + free(cloud_challenge); + + if (cloud_signature) + free(cloud_signature); + + break; + case CMD_WIFI_BL_CLOUD_SHAREDKEY: + ret = __bl_get_shared_key(msg, &shared_key, &shared_key_len); + if (ret) + { + LOG_EX(LOG_Error, "__bl_get_shared_key failed!\n"); + return -1; + } + + /* Get already connected AP's key_mgmt and ssid. */ + wpa_conf_get_apinfo_connected(key_mgmt, &priority); + strncpy(ssid, g_WlInfo.ssid, strlen(g_WlInfo.ssid)); + if (!strcmp(key_mgmt, "WPA-PSK")) + { + /* Get already connected AP's password from wpa_supplicant.conf */ + wpa_conf_get_appwd_connected(priority, password); + } + else + { + // TO BE FIXED + password[0] = '\0'; + } + + ret = sdk_wlan_params_set(shared_key, ssid, strlen(ssid), password, strlen(password)); + if (ret) + LOG_EX(LOG_Debug, "sdk_wlan_params_set failed, error code %d!\n", ret); + else + LOG_EX(LOG_Debug, "sdk_wlan_params_set succeed!\n"); + + if (shared_key) + free(shared_key); + + break; + case CMD_WIFI_BL_DEV_TOKEN_BOUND: + ret = sdk_device_token_bind_success(); + if (ret) + { + LOG_EX(LOG_Debug, "sdk_device_token_bind_success failed, error code %d!\n", ret); + } + else + { + LOG_EX(LOG_Debug, "sdk_device_token_bind_success succeed!\n"); + } + break; + case CMD_WIFI_BL_CONF_ABORT: + bl_disable_discovery_mode(); + break; + default: + break; + } + return ret; +} + +#endif diff --git a/package/allwinner/wifimanager/src/include/broadlink.h b/package/allwinner/wifimanager/src/include/broadlink.h new file mode 100755 index 000000000..09a39a6c3 --- /dev/null +++ b/package/allwinner/wifimanager/src/include/broadlink.h @@ -0,0 +1,43 @@ +#ifndef BROADLINK_IOT_H +#define BROADLINK_IOT_H +#ifndef PLATFORM_CPU +#define BL_IOT_MSG_TAGS ("BLNLINK") +#define BL_IOT_MGMT_FRAME_TYPE_ACTION 2 /* Use action frame to respond to Broadlink device. */ +#define BL_MASTER_ID_FOR_NETEASE 202 +#define BL_MAX_MSG (1024) +#define ETH_ALEN (6) + +typedef enum +{ + MSG_IN_DISCOVERY_MODE = 0, + MSG_OUT_DISCOVERY_MODE, + MSG_GET_WLAN_INFO, + MSG_BYPASS_MODE, +} BL_IOT_MSG_TYPE; + +#pragma pack (push) +#pragma pack (1) +typedef struct +{ + unsigned char msgTags[8]; + unsigned char dstMac[ETH_ALEN]; + unsigned char msgType; + unsigned int msglen; + unsigned char msgData[BL_MAX_MSG]; +} BL_IOT_MSG, *PBL_IOT_MSG; + +typedef struct +{ + unsigned char channel; + unsigned char dev_mac[ETH_ALEN]; + unsigned char bssid[ETH_ALEN]; + unsigned char ssid[32]; +} WL_INFO, *PWL_INFO; +#pragma pack (pop) + +#define BL_IOT_MSG_LEN(len) (sizeof(BL_IOT_MSG) - BL_MAX_MSG + len) + +void bl_recv_msg_thread(void *pParam); +int bl_create_socket(void); +#endif +#endif diff --git a/package/allwinner/wifimanager/src/include/network_manager.h b/package/allwinner/wifimanager/src/include/network_manager.h index 45931d70c..445d38872 100755 --- a/package/allwinner/wifimanager/src/include/network_manager.h +++ b/package/allwinner/wifimanager/src/include/network_manager.h @@ -1,6 +1,5 @@ #ifndef __NETWORK_MANAGER_H #define __NETWORK_MANAGER_H -#include "tool.h" #if __cplusplus extern "C" { @@ -11,39 +10,6 @@ extern "C" { #define KEY_WPA_PSK_INDEX 1 #define KEY_WEP_INDEX 2 -enum wpa_states { - WPA_UNKNOWN = 1024, - WPA_DISCONNECTED, - WPA_INTERFACE_DISABLED, - WPA_INACTIVE, - WPA_SCANNING, - WPA_AUTHENTICATING, - WPA_ASSOCIATING, - WPA_ASSOCIATED, - WPA_4WAY_HANDSHAKE, - WPA_GROUP_HANDSHAKE, - WPA_COMPLETED, -}; - -#define WPA_STA_MAX_SSID 32 -#define WPA_STA_MAX_BSSID 18 -#define WPA_STA_MAX_IP_ADDR 16 -#define WPA_STA_MAX_KEY_MGMT 16 -#define WPA_STA_MAX_MAC_ADDR 18 - -struct wpa_status{ - u32 id; - u8 bssid[WPA_STA_MAX_BSSID]; - u32 freq; - u8 ssid[WPA_STA_MAX_SSID]; - enum wpa_states wpa_state; - u8 ip_address[WPA_STA_MAX_IP_ADDR]; - u8 key_mgmt[WPA_STA_MAX_KEY_MGMT]; - u8 mac_address[WPA_STA_MAX_MAC_ADDR]; -}; - -void shutdown_wifi_scan_thread(); -void restart_wifi_scan_thread(); void start_wifi_scan_thread(void *args); void stop_wifi_scan_thread(); void pause_wifi_scan_thread(); @@ -51,9 +17,7 @@ void resume_wifi_scan_thread(); int update_scan_results(); int get_scan_results_inner(char *results, int *len); int get_key_mgmt(const char *ssid, int key_mgmt_info[]); -struct wpa_status * get_wpa_status(); -void print_wpa_status(struct wpa_status *wpa_sta); -void wpa_status_free(); + #if __cplusplus }; // extern "C" #endif diff --git a/package/allwinner/wifimanager/src/include/sdk.h b/package/allwinner/wifimanager/src/include/sdk.h new file mode 100755 index 000000000..db20bb7ba --- /dev/null +++ b/package/allwinner/wifimanager/src/include/sdk.h @@ -0,0 +1,213 @@ +#ifndef __SDK_H +#define __SDK_H + +/* + * 0. implement all callback's of this SDK + * 1. read wifi probe frame from driver + * 2. feed wifi probe frame to sdk_pkt_process + * 3. this sdk depends on no thirdparty libraries, and there is no internet connection with net. + */ + + +typedef unsigned char uint8; +typedef unsigned short int uint16; +typedef unsigned int uint32; + +typedef char int8; +typedef short int int16; +typedef int int32; + +#define SDK_SUCCESS (0) +#define SDK_FAIL (1) +#define SDK_NULL (0) +#define OUI_LEN (3) +#define OUI_TYPE (0) +#define OUI_TYPE_LEN (1) +#define MAC_LEN (6) +#define SSID_MAX_LEN (64) +#define PASSWORD_MAX_LEN (64) +#define KEY_MGMT_LEN (16) +#define SDK_SHAREDKEY_LEN (32) +#define SDK_FRAME_MAX_LEN (1460) +#define FASTCON_MASTER_ID_LEN (4) + +#define SDK_PACKED __attribute__((packed)) + + +typedef void* (*sdk_memset_t) ( + void* ptr, + int32 value, + uint32 num); +typedef void* (*sdk_memcpy_t) ( + void* dst, + const void* src, + uint32 num); +typedef int32 (*sdk_memcmp_t) ( + const void* ptr1, + const void* ptr2, + uint32 num); +typedef int32 (*sdk_printf_t) ( + const char* format, + ...); + +typedef struct sdk_device_info_t { + uint32 vendor_id; + uint32 product_id; + uint8 mac[MAC_LEN]; + uint8 * device_info; + uint16 device_info_len; + uint8 * device_id; + uint16 device_id_len; +}sdk_product_info_t; + +//发现设备 +typedef int32 (*sdk_found_device_t) (sdk_product_info_t *product_info); +//对应2.1 云端身份校验接口请求 +typedef int32 (*sdk_challenge_post_t)( + sdk_product_info_t *product_info, + uint8 * challenge, + uint16 challenge_len); +//对应2.2 设备身份声明接口请求 +typedef int32 (*sdk_signature_post_t)( + sdk_product_info_t *product_info, + uint8 * signature, + uint16 signature_len); +//sdk将设备控制token传给上层 +typedef int32 (*sdk_device_token_post_t)( + sdk_product_info_t *product_info, + uint8 * token, + uint16 token_len); +//上报配置结果 +//目前只支持失败结果上报。 +//成功的结果是由云端推送上线通知 +//result 0:成功 -1:失败 +typedef int32 (*sdk_config_result_post_t) ( + sdk_product_info_t *product_info, + int8 result); +//发送802.11原始报文接口 +typedef int32 (*sdk_raw_pkt_send_t)( + uint8 * frame, + uint16 frame_len); + + + + +typedef struct { + sdk_memset_t memset; + sdk_memcpy_t memcpy; + sdk_memcmp_t memcmp; + sdk_printf_t printf; + + sdk_found_device_t found_device; + sdk_challenge_post_t challenge_post; + sdk_signature_post_t signature_post; + sdk_device_token_post_t device_token_post; + sdk_config_result_post_t config_result_post; + + sdk_raw_pkt_send_t send_raw_pkt; + +} sdk_config_t; + +typedef struct { + + //gateway信息 + //包括当前路由器信道,gateway mac,bssid + uint8 channel; //cur channel + + //若mac地址为00-11-22-33-44-55,则device_mac[] = {0x00,0x11,0x22,0x33,0x44,0x55}; + uint8 device_mac[MAC_LEN]; + //AP的mac地址,格式等同device_mac + uint8 bssid[MAC_LEN]; + + //由dnakit后台分配,用于区分不同平台,固定4个字节 + uint8 fastcon_master_id[FASTCON_MASTER_ID_LEN]; + + //0:probe req,1:probe rsp,2:action + //报文选择,根据gateway 能力自行设置 + uint8 send_wlan_type; + //回调接口注册 + sdk_config_t sdk_config; +} SDK_PACKED sdk_user_init_t; + +typedef enum { + SDK_STATE_PROBE_DEVICE, + SDK_STATE_LOCK_CHANNEL, + SDK_STATE_CONFIG, + SDK_STATE_CONNECTING, + SDK_STATE_DONE, //Finish + SDK_STATE_FAIL, +} sdk_state_e; + +/** + * @brief 打印sdk编译时间 + * @return 0 打印成功 + * SDK_E_INIT_FAIL sdk未初始化 + */ +extern int32 sdk_version(void); +/** + * @brief 初始化sdk + * @param init + * @return 0 初始化成功 + * SDK_E_INIT_FAIL + */ +extern int32 sdk_init(sdk_user_init_t *init); +/** + * @brief 处理接收的802.11报文 + * @param frame 802.11标准报文格式 + * @param frame_len + * @return sdk_state_e + SDK_E_INIT_FAIL + */ +extern int32 sdk_pkt_process(uint8 *frame, int16 frame_len); +/** + * @brief 发送token bind成功报文 + * @return 0 成功 + * SDK_E_INIT_FAIL + * SDK_E_STATE + */ +extern int32 sdk_device_token_bind_success(void); +/** + * @brief ssid和password信息发送 + * @param sharedkey 二进制数组,云端返回的是十六进制字符串,需要进行转换 + * @param ssid + * @param ssid_len + * @param password + * @param password_len + * @return 0 成功 + * SDK_E_INIT_FAIL + * SDK_E_STATE + * @example 路由器ssid为"ssid",password为"password" + * 代码如下: + * @code + * uint8 ssid[] = "ssid"; + * uint8 password[] = "password"; + * sdk_wlan_params_set(sharedkey,ssid,strlen(ssid),password,strlen(password)); + * @endcode + */ +extern int32 sdk_wlan_params_set(uint8 sharedkey[SDK_SHAREDKEY_LEN],uint8 *ssid,uint16 ssid_len,uint8 *password,uint16 password_len); +/** + * @brief challenge和signature信息发送 + * @param challenge 二进制数组,云端返回的是十六进制字符串,需要进行转换 + * @param challenge_len + * @param signature 二进制数组,云端返回的是十六进制字符串,需要进行转换 + * @param signature_len + * @return 0 成功 + * SDK_E_INIT_FAIL + * SDK_E_STATE + */ +extern int32 sdk_challenge_and_signature_set(uint8 *challenge,uint16 challenge_len, uint8 *signature, uint16 signature_len); +/** + * @brief 用户确认开始配网 + * @return 0 成功 + * SDK_E_INIT_FAIL + * SDK_E_STATE + */ +extern int32 sdk_user_auth_to_start_config(void); +/** + * @brief 清除sdk状态 + * @return 0 成功 + * SDK_E_INIT_FAIL + */ +extern int32 sdk_deinit(void); + +#endif diff --git a/package/allwinner/wifimanager/src/include/sdk_errno.h b/package/allwinner/wifimanager/src/include/sdk_errno.h new file mode 100755 index 000000000..369b79779 --- /dev/null +++ b/package/allwinner/wifimanager/src/include/sdk_errno.h @@ -0,0 +1,19 @@ + +#ifndef __SDK_ERRNO_H +#define __SDK_ERRNO_H + + +#define SDK_E_INIT_FAIL (-1) //SDK初始化失败 +#define SDK_E_INVPARAMS (-2) //参数异常 +#define SDK_E_STATE (-3) //sdk状态异常 + +//only use by sdk_config_result_post_t +#define SDK_E_CONNECT_AP_TIMEOUT (-101) //连接ap超时 +#define SDK_E_PKT_SEND (-102) //发送数据包失败 +#define SDK_E_OVER_RETRY_COUNT (-103) //超过最大重试次数 +#define SDK_E_VERIFY_SIGNATURE (-104) //校验网关端身份失败 +#define SDK_E_SSID_PWD (-105) //ssid和pwd解析异常 +#define SDK_E_MSG_NOT_SUPPORT (-106) //接收到不支持的消息类型 +#define SDK_E_CONFIG_TIMEOUT (-107) //配置超时,超过10分钟配置时间 + +#endif diff --git a/package/allwinner/wifimanager/src/include/wifi_event.h b/package/allwinner/wifimanager/src/include/wifi_event.h index 35c1c4134..c017f46a8 100755 --- a/package/allwinner/wifimanager/src/include/wifi_event.h +++ b/package/allwinner/wifimanager/src/include/wifi_event.h @@ -22,7 +22,7 @@ enum WPA_EVENT{ DRIVER_STATE, EAP_FAILURE, ASSOC_REJECT, - NETWORK_NOT_FOUND, + SSID_TEMP_DISABLED, UNKNOWN, }; @@ -35,11 +35,14 @@ typedef enum{ }tWIFI_EVENT_INNER; void wifi_event_loop(tWifi_event_callback pcb); +void wifi_event_loop_stop(); +void wifi_event_loop_restart(); void start_wifi_on_check_connect_timeout(); void start_check_connect_timeout(); tWIFI_EVENT_INNER get_cur_wifi_event(); int set_cur_wifi_event(tWIFI_EVENT_INNER event); void set_scan_start_flag(); +void set_wifi_off_flag(int value); int get_scan_status(); void reset_assoc_reject_count(); int get_assoc_reject_count(); diff --git a/package/allwinner/wifimanager/src/include/wifi_intf.h b/package/allwinner/wifimanager/src/include/wifi_intf.h index 7db2bbd9a..620d2e9ac 100755 --- a/package/allwinner/wifimanager/src/include/wifi_intf.h +++ b/package/allwinner/wifimanager/src/include/wifi_intf.h @@ -4,6 +4,10 @@ #if __cplusplus extern "C" { #endif + +#include +#include + #define MAX_CALLBCAKS_COUNT 1024 typedef enum { @@ -60,9 +64,7 @@ typedef struct{ int (*remove_all_networks)(void); int (*re_config)(void); int (*list_networks)(char *reply, size_t reply_len, int event_label); - int (*get_netid)(const char *ssid, tKEY_MGMT key_mgmt, char *net_id, int *length); - int (*stop_scan)(); - int (*restart_scan)(); + int (*get_netid)(const char *ssid, tKEY_MGMT key_mgmt, char *net_id); int (*change_ap)(int event_label ); int (*enable_all_network)(void); }aw_wifi_interface_t; @@ -70,6 +72,9 @@ typedef struct{ const aw_wifi_interface_t * aw_wifi_on(tWifi_event_callback pcb, int event_label); int aw_wifi_get_wifi_state(); int aw_wifi_off(const aw_wifi_interface_t *p_wifi_interface_t); +int bl_sdk_init(); +int bl_event_handle(unsigned int cmd, const char* msg); + #if __cplusplus }; // extern "C" diff --git a/package/allwinner/wifimanager/src/libfastcon.a b/package/allwinner/wifimanager/src/libfastcon.a new file mode 100755 index 000000000..a479fd754 Binary files /dev/null and b/package/allwinner/wifimanager/src/libfastcon.a differ diff --git a/package/allwinner/wifimanager/src/network_manager.c b/package/allwinner/wifimanager/src/network_manager.c index 1472dee8f..0b64fb13f 100755 --- a/package/allwinner/wifimanager/src/network_manager.c +++ b/package/allwinner/wifimanager/src/network_manager.c @@ -5,15 +5,22 @@ #include #include +#include +#include +#include + + #include #include "wifi_event.h" #include "network_manager.h" #include "wifi_intf.h" #include "wifi.h" -#include "tool.h" +#include "broadlink.h" + + +#define MAX_SCAN_FAIL_TIME 5 #define WAITING_CLK_COUNTS 50 -#define SSID_LEN 512 #define UTF8_BYTES_MAX 4096 #define UTF8_MULTI_BYTES_MASK 0x80 #define ESCAP_CHAR_ASCII '\\' @@ -24,237 +31,23 @@ /* scan thread */ static pthread_t scan_thread_id; static int scan_running = 0; -static int scan_terminating = 0; static pthread_mutex_t scan_mutex; static pthread_cond_t scan_cond; static int scan_pause = 0; -static int scan_error = 0; -static int do_scan_flag = 1; /* run scan immediately */ static pthread_mutex_t thread_run_mutex; static pthread_cond_t thread_run_cond; -/*stop restart scan*/ -static pthread_mutex_t scan_stop_restart_mutex; -static pthread_cond_t scan_stop_restart_cond; - /* store scan results */ static char scan_results[SCAN_BUF_LEN]; static int scan_results_len = 0; static int scan_completed = 0; -enum wpa_states wpa_supplicant_state_convert(char *str) -{ - if(!strncmp(str,"DISCONNECTED",16)){ - return WPA_DISCONNECTED; - } - if(!strncmp(str,"INTERFACE_DISABLED",22)){ - return WPA_INTERFACE_DISABLED; - } - if(!strncmp(str,"INACTIVE",12)){ - return WPA_INACTIVE; - } - if(!strncmp(str,"SCANNING",12)){ - return WPA_SCANNING; - } - if(!strncmp(str,"AUTHENTICATING",18)){ - return WPA_AUTHENTICATING; - } - if(!strncmp(str,"ASSOCIATED",14)){ - return WPA_ASSOCIATED; - } - if(!strncmp(str,"4WAY_HANDSHAKE",19)){ - return WPA_4WAY_HANDSHAKE; - } - if(!strncmp(str,"GROUP_HANDSHAKE",19)){ - return WPA_GROUP_HANDSHAKE; - } - if(!strncmp(str,"COMPLETED",13)){ - return WPA_COMPLETED; - } - if(!strncmp(str,"DISCONNECTED",16)){ - return WPA_DISCONNECTED; - } - return WPA_UNKNOWN; -} -static const char * wpa_supplicant_state_txt(enum wpa_states state) -{ - switch (state) { - case WPA_DISCONNECTED: - return "DISCONNECTED"; - case WPA_INACTIVE: - return "INACTIVE"; - case WPA_INTERFACE_DISABLED: - return "INTERFACE_DISABLED"; - case WPA_SCANNING: - return "SCANNING"; - case WPA_AUTHENTICATING: - return "AUTHENTICATING"; - case WPA_ASSOCIATING: - return "ASSOCIATING"; - case WPA_ASSOCIATED: - return "ASSOCIATED"; - case WPA_4WAY_HANDSHAKE: - return "4WAY_HANDSHAKE"; - case WPA_GROUP_HANDSHAKE: - return "GROUP_HANDSHAKE"; - case WPA_COMPLETED: - return "COMPLETED"; - default: - return "UNKNOWN"; - } -} -void print_wpa_status(struct wpa_status *wpa_sta) -{ - LOG_EX(LOG_Debug, "obtained wpa_supplicant status,as follow:\n"); - LOG_EX(LOG_Debug, "==============================\n"); - if(wpa_sta->id >= 0) - LOG_EX(LOG_Debug, "id:%d\n",wpa_sta->id); - if(wpa_sta->bssid) - LOG_EX(LOG_Debug, "bssid:%s\n",wpa_sta->bssid); - if(wpa_sta->freq >=0) - LOG_EX(LOG_Debug, "freq:%d\n",wpa_sta->freq); - if(wpa_sta->ssid) - LOG_EX(LOG_Debug, "ssid:%s\n",wpa_sta->ssid); - if(wpa_sta->wpa_state) - LOG_EX(LOG_Debug, "wpa_state:%s\n",wpa_supplicant_state_txt(wpa_sta->wpa_state)); - if(wpa_sta->ip_address) - LOG_EX(LOG_Debug, "ip_address:%s\n",wpa_sta->ip_address); - if(wpa_sta->key_mgmt) - LOG_EX(LOG_Debug, "key_mgmt:%s\n",wpa_sta->key_mgmt); - if(wpa_sta->mac_address) - LOG_EX(LOG_Debug, "mac_address:%s\n",wpa_sta->mac_address); - LOG_EX(LOG_Debug, "==============================\n"); -} -static char *strstr_wpa(const char *src,const char *obj, - const char pre_str[2],int pst_len) -{ - const char *p=src; - int length; - int i=0; - if(src == NULL || obj == NULL || pre_str ==NULL){ - LOG_EX(LOG_Debug, "src or obj or pre_str is NULL"); - return NULL; - } - if(pst_len > strlen(pre_str)){ - LOG_EX(LOG_Debug, "pst_len length is illegal"); - return NULL; - } - length = strlen(obj); - for(;;p++,i++){ - p=strchr(p,*obj); - if(p == NULL){ - LOG_EX(LOG_Debug, "%s is not exist",obj); - return NULL; - } - if(strncmp(p,obj,length) == 0){ - if(i > 1 && *(p-1) != pre_str[0] && *(p-1) != pre_str[1]){ - return (char*)p; - } - } - } -} -static int search_wpa_string(const char *src,const char *obj,int max,char *get_str) -{ - int i=0; - const char *sptr = NULL; - const char *pnext = NULL; - if(obj == NULL || src == NULL){ - wmg_printf(MSG_DEBUG,"src or obj is NULL"); - return 0; - } - if(!strncmp("id=",obj,3)){ - sptr = strstr_wpa(src,obj,"su",2); - }else if(!strncmp("ssid=",obj,5)){ - sptr = strstr_wpa(src,obj,"bb",2); - }else if(!strncmp("address=",obj,8)){ - sptr = strstr_wpa(src,obj,"__",2); - }else{ - sptr = strstr(src, obj); - } +/* broadlink thread */ +static pthread_t broadlink_thread_id; +static int broadlink_thread_running = 0; - if(sptr !=NULL){ - pnext=sptr+strlen(obj); - i=0; - while(1){ - i++; - if(i >max ){ - LOG_EX(LOG_Debug, "Data overflow"); - break; - } - pnext++; - if(*pnext == '\n') - break; - } - strncpy(get_str,sptr+strlen(obj),i); - get_str[i]='\0'; - return 1; - } - return -1; -} -static struct wpa_status *gstatus; - -static struct wpa_status* wpa_status_init() -{ - int i; - if(gstatus == NULL) - gstatus = (struct wpa_status *)wgos_zalloc(sizeof(struct wpa_status)); - if(gstatus == NULL){ - LOG_EX(LOG_Debug, "MALLOC STATUS STRUCT FAILED!\n"); - return NULL; - } - gstatus->id = -1; - gstatus->freq = -1; - for(i = 0;i< WPA_STA_MAX_SSID;i++){ - gstatus->ssid[i] = '\0'; - if(i < WPA_STA_MAX_BSSID) - gstatus->bssid[i] = '\0'; - if(i < WPA_STA_MAX_IP_ADDR) - gstatus->ip_address[i] = '\0'; - if(i < WPA_STA_MAX_KEY_MGMT) - gstatus->key_mgmt[i]='\0'; - if(i < WPA_STA_MAX_MAC_ADDR) - gstatus->mac_address[i]='\0'; - } - gstatus->wpa_state = WPA_UNKNOWN; - return gstatus; -} -void wpa_status_free() -{ - if(gstatus !=NULL) - free(gstatus); -} -struct wpa_status * get_wpa_status() -{ - char reply[4096] = {0}; - char wpa_result[32]; - struct wpa_status *wpa_sta; - wpa_sta = wpa_status_init(); - wifi_command("STATUS", reply, sizeof(reply)); - if(reply !=NULL){ - if(search_wpa_string(reply,"ssid=",32,wpa_result) >0) - strncpy(wpa_sta->ssid,wpa_result,strlen(wpa_result)); - if(search_wpa_string(reply,"id=",4,wpa_result) >0) - wpa_sta->id = atoi(wpa_result); - if(search_wpa_string(reply,"freq=",6,wpa_result) >0) - wpa_sta->freq = atoi(wpa_result); - if(search_wpa_string(reply,"bssid=",18,wpa_result) >0) - strncpy(wpa_sta->bssid,wpa_result,strlen(wpa_result)); - if(search_wpa_string(reply,"key_mgmt=",16,wpa_result) >0) - strncpy(wpa_sta->key_mgmt,wpa_result,strlen(wpa_result)); - if(search_wpa_string(reply,"wpa_state=",32,wpa_result) >0) - wpa_sta->wpa_state = wpa_supplicant_state_convert(wpa_result); - if(search_wpa_string(reply,"address=",18,wpa_result) >0) - strncpy(wpa_sta->mac_address,wpa_result,strlen(wpa_result)); - if(search_wpa_string(reply,"ip_address=",16,wpa_result) >0) - strncpy(wpa_sta->ip_address,wpa_result,strlen(wpa_result)); - } - if(wmg_get_debug_level() >= MSG_DEBUG){ - print_wpa_status(wpa_sta); - } - return wpa_sta; -} int update_scan_results() { @@ -277,72 +70,13 @@ int update_scan_results() return 0; } -int remove_slash_from_scan_results() -{ - char *ptr = NULL; - char *ptr_s = NULL; - char *ftr = NULL; - - ptr_s = scan_results; - while(1) - { - ptr = strchr(ptr_s,'\"'); - if(ptr == NULL) - break; - - ptr_s = ptr; - if((*(ptr-1)) == '\\') - { - ftr = ptr; - ptr -= 1; - while(*ftr != '\0') - *(ptr++) = *(ftr++); - *ptr = '\0'; - continue; //restart new search at ptr_s after removing slash - } - else - ptr_s++; //restart new search at ptr_s++ - } - - ptr_s = scan_results; - while(1) - { - ptr = strchr(ptr_s,'\\'); - if(ptr == NULL) - break; - - ptr_s = ptr; - if((*(ptr-1)) == '\\') - { - ftr = ptr; - ptr -= 1; - while(*ftr != '\0') - *(ptr++) = *(ftr++); - *ptr = '\0'; - continue; //restart new search at ptr_s after removing slash - } - else - ptr_s++; //restart new search at ptr_s++ - } - - return 0; -} - int get_scan_results_inner(char *result, int *len) { int index = 0; char *ptr = NULL; - int ret = 0; pthread_mutex_lock(&scan_mutex); - if(0 != scan_error) - { -// prinf("%s: scan or scan_results ERROR in scan_thread!\n", __func__); - ret = -1; - } - - remove_slash_from_scan_results(); if(*len <= scan_results_len){ strncpy(result, scan_results, *len-1); index = *len -1; @@ -358,7 +92,7 @@ int get_scan_results_inner(char *result, int *len) pthread_mutex_unlock(&scan_mutex); - return ret; + return 0; } int is_network_exist(const char *ssid, tKEY_MGMT key_mgmt) @@ -461,8 +195,10 @@ int get_key_mgmt(const char *ssid, int key_mgmt_info[]) char *ptr = NULL, *pssid_start = NULL, *pssid_end = NULL; char *pst = NULL, *pend = NULL; char *pflag = NULL; - char flag[128], pssid[SSID_LEN + 1]; + char flag[128]; int len = 0, i = 0; + char * pSsidSearch = NULL; + int status=0; LOG_EX(LOG_Debug, "enter get_key_mgmt, ssid %s\n", ssid); @@ -481,47 +217,66 @@ int get_key_mgmt(const char *ssid, int key_mgmt_info[]) } //point second line of scan results - ptr++; - remove_slash_from_scan_results(); - while(1){ + status = utf8BytesExchangeEx(ssid,&pSsidSearch); + if(0 != status) + { + LOG_EX(LOG_Debug, "utf8 exchange err\r\n"); + goto ToOut; + } + + LOG_EX(LOG_Debug, "changed ssid is %s\r\n",pSsidSearch); + + ptr++; + while((pssid_start=strstr(ptr, pSsidSearch)) != NULL){ + pssid_end = pssid_start + strlen(pSsidSearch); + + /* ssid is presuffix of searched network */ + if((*pssid_end != '\n') && (*pssid_end != '\0')){ + pend = strchr(pssid_start, '\n'); + if(pend != NULL){ + ptr = pend+1; + continue; + }else{ + break; + } + } + + /* network ssid is same of searched network */ /* line end */ - pend = strchr(ptr, '\n'); - if (pend != NULL){ + pend = strchr(pssid_start, '\n'); + if(pend != NULL){ *pend = '\0'; } - /* line start */ - pst = ptr; + pst = strrchr(ptr, '\n'); + if(pst != NULL){ + pst++; + }else{ + pst = ptr; + } - /* abstract ssid */ - pssid_start = strrchr(pst, '\t') + 1; - strncpy(pssid, pssid_start, SSID_LEN); - pssid[SSID_LEN] = '\0'; + pflag = pst; + for(i=0; i<3; i++){ + pflag = strchr(pflag, '\t'); + pflag++; + } - /* find ssid in scan results */ - if(strcmp(pssid, ssid) == 0){ - pflag = pst; - for(i=0; i<3; i++){ - pflag = strchr(pflag, '\t'); - pflag++; - } - - len = pssid_start - pflag; - len = len - 1; - strncpy(flag, pflag, len); - flag[len] = '\0'; + len = pssid_start - pflag; + len = len - 1; + strncpy(flag, pflag, len); + flag[len] = '\0'; LOG_EX(LOG_Debug, "ssid %s, flag %s\n", ssid, flag); - if((strstr(flag, "WPA-PSK-") != NULL) - || (strstr(flag, "WPA2-PSK-") != NULL)){ - key_mgmt_info[KEY_WPA_PSK_INDEX] = 1; - }else if(strstr(flag, "WEP") != NULL){ - key_mgmt_info[KEY_WEP_INDEX] = 1; - }else if((strcmp(flag, "[ESS]") == 0) || (strcmp(flag, "[WPS][ESS]") == 0)){ - key_mgmt_info[KEY_NONE_INDEX] = 1; - }else{ - ; - } + + if((strstr(flag, "WPA-PSK-") != NULL) + || (strstr(flag, "WPA2-PSK-") != NULL)){ + key_mgmt_info[KEY_WPA_PSK_INDEX] = 1; + }else if(strstr(flag, "WEP") != NULL){ + key_mgmt_info[KEY_WEP_INDEX] = 1; + }else if((strcmp(flag, "[ESS]") == 0) || (strcmp(flag, "[WPS][ESS]") == 0)){ + key_mgmt_info[KEY_NONE_INDEX] = 1; + }else{ + ; } if(pend != NULL){ @@ -533,6 +288,10 @@ int get_key_mgmt(const char *ssid, int key_mgmt_info[]) } } +ToOut: + free(pSsidSearch); + pSsidSearch = NULL; + pthread_mutex_unlock(&scan_mutex); return 0; @@ -540,46 +299,103 @@ int get_key_mgmt(const char *ssid, int key_mgmt_info[]) } + +double iw_freq2float(const struct iw_freq * in) +{ + int i; + double res = (double)in->m; + + for(i = 0; i < in->e; i++) + res *= 10; + return (res); +} + +static inline int iw_get_ext(int skfd, const char *ifname, int request, struct iwreq * pwrq) +{ + /* Set device name */ + strncpy(pwrq->ifr_name, ifname, IFNAMSIZ); + /* Do the request */ + return(ioctl(skfd, request, pwrq)); +} + +int api_wifi_get_wireless_info(const char *ifname) +{ + int sock; + struct iwreq wrq; + char *mac = NULL; + + memset((char *)&wrq, 0, sizeof(struct iwreq)); + + /* Create a channel to the NET kernel. */ + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock < 0) + { + LOG_EX(LOG_Debug, "Open sock error!\n"); + return -1; + } + + /* Get wireless name */ + if(iw_get_ext(sock, ifname, SIOCGIWNAME, &wrq) < 0) + { + LOG_EX(LOG_Debug, "Get wireless name error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "Wireless name %s\n", wrq.u.name); + + /* Get frequency / channel */ + if(iw_get_ext(sock, ifname, SIOCGIWFREQ, &wrq) < 0) + { + LOG_EX(LOG_Debug, "Get wireless frequency error!\n"); + return -1; + } + LOG_EX(LOG_Debug, "Wireless frequency %lf\n", iw_freq2float(&(wrq.u.freq))); + + /* Get AP address */ + if(iw_get_ext(sock, ifname, SIOCGIWAP, &wrq) < 0) + { + LOG_EX(LOG_Debug, "Get wireless ap mac error!\n"); + return -1; + } + mac = (char *)(&(wrq.u.ap_addr)); + LOG_EX(LOG_Debug, "Wireless ap mac %x:%x:%x:%x:%x:%x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + +} + + void *wifi_scan_thread(void *args) { int ret = -1, i = 0; + int scan_fail_cnt = 0; char cmd[16] = {0}, reply[16] = {0}; struct timeval now; struct timespec outtime; while(scan_running){ - - pthread_mutex_lock(&scan_stop_restart_mutex); - while(do_scan_flag == 0){ - pthread_cond_wait(&scan_stop_restart_cond, &scan_stop_restart_mutex); - } - if(scan_terminating) - { - pthread_mutex_unlock(&scan_stop_restart_mutex); - continue; - } - pthread_mutex_unlock(&scan_stop_restart_mutex); - pthread_mutex_lock(&scan_mutex); - while(scan_pause == 1){ + if(scan_pause == 1){ pthread_cond_wait(&scan_cond, &scan_mutex); } - /* set scan start flag */ set_scan_start_flag(); - /* scan cmd */ strncpy(cmd, "SCAN", 15); ret = wifi_command(cmd, reply, sizeof(reply)); if(ret){ - scan_error = 1; pthread_mutex_unlock(&scan_mutex); - usleep(5000*1000); + scan_fail_cnt++; + usleep(500*1000); + /* fix PV1-3216, do DISCONNECT to restore driver from continuiously restart */ + if (scan_fail_cnt > MAX_SCAN_FAIL_TIME) + { + stop_connecting(); + scan_fail_cnt = 0; + } continue; } + scan_fail_cnt = 0; for(i=0;i #include -#include "tool.h" + #define IFACE_VALUE_MAX 32 static struct wpa_ctrl *ctrl_conn; static struct wpa_ctrl *monitor_conn; pthread_mutex_t ctrl_mutex; + /* socket pair used to exit from a blocking read */ static int exit_sockets[2]; @@ -479,10 +480,12 @@ int wifi_start_supplicant(int p2p_supported) exit_sockets[0] = exit_sockets[1] = -1; /* start wpa_supplicant */ - strncpy(cmd, "/etc/wifi/wifi start", 511); + strncpy(cmd, "/etc/wifi/wifi restart", 511); cmd[511] = '\0'; system(cmd); + + return 0; } @@ -492,7 +495,7 @@ int wifi_stop_supplicant(int p2p_supported) return 0; } -#define SUPPLICANT_TIMEOUT 3000000 // microseconds +#define SUPPLICANT_TIMEOUT 4000000 // microseconds #define SUPPLICANT_TIMEOUT_STEP 100000 // microseconds int wifi_connect_on_socket_path(const char *path) { @@ -536,8 +539,7 @@ int wifi_connect_on_socket_path(const char *path) int wifi_connect_to_supplicant() { static char path[PATH_MAX]; - if(get_process_state("wpa_supplicant",14) == -1) - return -1; + strncpy(primary_iface, "wlan0", IFACE_VALUE_MAX); if (access(IFACE_DIR, F_OK) == 0) { snprintf(path, sizeof(path), "%s/%s", IFACE_DIR, primary_iface); @@ -558,11 +560,11 @@ int wifi_send_command(const char *cmd, char *reply, size_t *reply_len) ret = wpa_ctrl_request(ctrl_conn, cmd, strlen(cmd), reply, reply_len, NULL); LOG_EX(LOG_Debug, "cmd is %s,size is %d,reply len %d\r\n",cmd,strlen(cmd),*reply_len); - + if (ret == -2) { LOG_EX(LOG_Debug, "'%s' command timed out.\n", cmd); /* unblocks the monitor receive socket for termination */ - TEMP_FAILURE_RETRY(write(exit_sockets[0], "T", 1)); + //TEMP_FAILURE_RETRY(write(exit_sockets[0], "T", 1)); return -2; } else if (ret < 0 || strncmp(reply, "FAIL", 4) == 0) { return -1; @@ -606,6 +608,7 @@ int wifi_wait_on_socket(char *buf, size_t buflen) char *match, *match2; if (monitor_conn == NULL) { + sleep(2); return snprintf(buf, buflen, WPA_EVENT_TERMINATING " - connection closed"); } @@ -707,6 +710,25 @@ void wifi_close_supplicant_connection() } } +void wifi_restart_supplicant() +{ + int i; + int ret; + + wifi_event_loop_stop(); + wifi_close_supplicant_connection(); + wifi_start_supplicant(0); + do{ + usleep(300000); + ret = wifi_connect_to_supplicant(); + if(!ret){ + LOG_EX(LOG_Debug, "Connected to wpa_supplicant!\n"); + break; + } + i++; + }while(ret && i<10); + wifi_event_loop_restart(); +} void wifi_clear() { @@ -730,7 +752,11 @@ int wifi_command(char const *cmd, char *reply, size_t reply_len) if ((ret =wifi_send_command(cmd, reply, &reply_len)) != 0) { LOG_EX(LOG_Debug, "wifi_send_command ret is %d %s\r\n",ret, reply); - wifi_clear(); + if (-2 == ret) + wifi_restart_supplicant(); + else + wifi_clear(); + pthread_mutex_unlock(&ctrl_mutex); return -1; } diff --git a/package/allwinner/wifimanager/src/wifi_event.c b/package/allwinner/wifimanager/src/wifi_event.c index 42b26efe6..3400fa9c5 100755 --- a/package/allwinner/wifimanager/src/wifi_event.c +++ b/package/allwinner/wifimanager/src/wifi_event.c @@ -5,14 +5,14 @@ #include #include + #include "wifi.h" #include "wifi_event.h" #include "wifi_state_machine.h" #include "wifi_intf.h" #include "wpa_supplicant_conf.h" -//tWIFI_STATE gwifi_state; -tWIFI_STATE gwifi_state = WIFIMG_WIFI_DISABLED; +tWIFI_STATE gwifi_state; extern char netid_connecting[]; extern int connecting_ap_event_label; extern int disconnect_ap_event_label; @@ -22,8 +22,10 @@ tWifi_event_callback wifi_event_callback[MAX_CALLBCAKS_COUNT] = {NULL}; int wifi_event_callback_index = 0; static pthread_t event_thread_id; +static int event_thread_running; static int wifi_event_inner = AP_DISCONNECTED; static int scan_complete = 0; +static int wifi_off_flag = 0; static int assoc_reject_count = 0; static int authentication_fail_count = 0; @@ -32,7 +34,6 @@ static void handle_event(int event, char * remainder) { char cmd[255] = {0}, reply[16]={0}; int len = NET_ID_LEN+1; tWIFI_MACHINE_STATE state; - static int network_not_found_count = 0; switch (event){ case DISCONNECTED: @@ -60,7 +61,7 @@ static void handle_event(int event, char * remainder) { } } - assoc_reject_count = 0; + //assoc_reject_count = 0; set_wifi_machine_state(L2CONNECTED_STATE); /* start udhcpcd */ @@ -71,18 +72,8 @@ static void handle_event(int event, char * remainder) { scan_complete = 1; break; - case NETWORK_NOT_FOUND: - network_not_found_count++; - LOG_EX(LOG_Debug,"NETWORK NOT FOUND %d times!\n",network_not_found_count); - if(network_not_found_count >= 3){ - sprintf(cmd, "%s", "DISCONNECT"); - wifi_command(cmd, reply, sizeof(reply)); - set_wifi_machine_state(DISCONNECTED_STATE); - set_cur_wifi_event(CONNECT_AP_TIMEOUT); - } - break; case UNKNOWN: - //printf("unknown event!\n"); + //LOG_EX(LOG_Debug, "unknown event!\n"); break; } } @@ -90,8 +81,12 @@ static void handle_event(int event, char * remainder) { static int dispatch_event(const char *event_str, int nread) { int i = 0, event = 0; - char event_name[18]; + char event_name[32]; char cmd[255] = {0}, reply[16]={0}; + char network_id[NET_ID_LEN]={0}; + static char recent_network_id[NET_ID_LEN]={0}; + int network_id_tmp; + char buf_tmp[255]; char *name_start = NULL, *name_end = NULL; char *event_data = NULL; @@ -117,6 +112,11 @@ static int dispatch_event(const char *event_str, int nread) } } + if (!strncmp(event_str, "Selecting BSS network id ", 25)){ + sscanf(event_str, "Selecting BSS network id %d!!!!", &network_id_tmp); + sprintf(recent_network_id, "%d", network_id_tmp); + } + //LOG_EX(LOG_Debug, "EVENT, not care!\n"); return 0; } @@ -124,7 +124,7 @@ static int dispatch_event(const char *event_str, int nread) name_start = (char *)((unsigned long)event_str+11); name_end = strchr(name_start, ' '); if(name_end){ - while((name_start < name_end) && (i < 18)){ + while((name_start < name_end) && (i < 31)){ event_name[i] = *name_start++; i++; } @@ -153,10 +153,10 @@ static int dispatch_event(const char *event_str, int nread) event = DRIVER_STATE; }else if(!strcmp(event_name, "EAP-FAILURE")){ event = EAP_FAILURE; - }else if(!strcmp(event_name, "NETWORK-NOT-FOUND")){ - event = NETWORK_NOT_FOUND; }else if(!strcmp(event_name, "ASSOC-REJECT")){ event = ASSOC_REJECT; + }else if(!strcmp(event_name, "SSID-TEMP-DISABLED")){ + event = SSID_TEMP_DISABLED; }else{ event = UNKNOWN; } @@ -184,22 +184,40 @@ static int dispatch_event(const char *event_str, int nread) LOG_EX(LOG_Debug, "DRIVER_STATE, no care!\n"); return 0; }else if(event == TERMINATING){ - LOG_EX(LOG_Debug, "Wpa supplicant terminated!\n"); - gwifi_state = WIFIMG_WIFI_DISABLED; - call_event_callback_function(WIFIMG_WIFI_OFF_SUCCESS, NULL, 0); - return 1; + if (get_wifi_off_flag()) + { + /* If we turn off wifi, we should update wifimanager status. */ + LOG_EX(LOG_Debug, "Wpa supplicant terminated!\n"); + gwifi_state = WIFIMG_WIFI_DISABLED; + call_event_callback_function(WIFIMG_WIFI_OFF_SUCCESS, NULL, 0); + set_wifi_off_flag(0); + return 1; + } + else + { + /* If we just restart wpa_supplicant, do nothing. */ + return 0; + } }else if(event == EAP_FAILURE){ LOG_EX(LOG_Debug, "EAP FAILURE!\n"); return 0; }else if(event == ASSOC_REJECT){ assoc_reject_count++; if(assoc_reject_count >= MAX_ASSOC_REJECT_COUNT){ + wpa_conf_add_on_priority_list_hdr(recent_network_id); + LOG_EX(LOG_Debug, "Current ap's priority lowered!\n"); /* send disconnect */ sprintf(cmd, "%s", "DISCONNECT"); wifi_command(cmd, reply, sizeof(reply)); } LOG_EX(LOG_Debug, "ASSOC REJECT!\n"); return 0; + }else if(event == SSID_TEMP_DISABLED){ + sscanf(event_str, "CTRL-EVENT-SSID-TEMP-DISABLED id=%d %s", &network_id_tmp, buf_tmp); + sprintf(network_id, "%d", network_id_tmp); + wpa_conf_add_on_priority_list_hdr(network_id); + LOG_EX(LOG_Debug, "Current ap's priority lowered!\n"); + return 0; }else{ handle_event(event, event_data); } @@ -212,8 +230,8 @@ void *event_handle_thread(void* args) char buf[EVENT_BUF_SIZE] = {0}; int nread = 0, ret = 0; - for(;;){ - nread = wifi_wait_for_event(buf, sizeof(buf)); + while(event_thread_running){ + nread = wifi_wait_for_event(buf, sizeof(buf)); if (nread > 0) { ret = dispatch_event(buf, nread); if(ret == 1){ @@ -224,16 +242,43 @@ void *event_handle_thread(void* args) } } + LOG_EX(LOG_Debug, "Wifi event_handle_thread stopped!\n"); pthread_exit(NULL); } void wifi_event_loop(tWifi_event_callback pcb) { + pthread_attr_t attr; /* initial */ wifi_event_inner = AP_DISCONNECTED; - pthread_create(&event_thread_id, NULL, event_handle_thread, NULL); + + /* Set thread attribute PTHREAD_CREATE_DETACHED, otherwise the resources + will not be reclaimed after thread exit. */ + pthread_attr_init (&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + event_thread_running = 1; + pthread_create(&event_thread_id, &attr, event_handle_thread, NULL); } +void wifi_event_loop_stop() +{ + event_thread_running = 0; + usleep(200*1000); +} + +void wifi_event_loop_restart() +{ + pthread_attr_t attr; + + /* Set thread attribute PTHREAD_CREATE_DETACHED, otherwise the resources + will not be reclaimed after thread exit. */ + pthread_attr_init (&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + event_thread_running = 1; + pthread_create(&event_thread_id, &attr, event_handle_thread, NULL); +} + + tWIFI_EVENT_INNER get_cur_wifi_event() { return wifi_event_inner; @@ -286,7 +331,10 @@ void *check_connect_timeout(void *args) call_event_callback_function(WIFIMG_CONNECT_TIMEOUT, NULL, connecting_ap_event_label); } - pthread_exit(NULL); + /* resume scan thread */ + resume_wifi_scan_thread(); + + pthread_exit(NULL); } @@ -323,9 +371,9 @@ void *wifi_on_check_connect_timeout(void *args) event = get_cur_wifi_event(); i++; - } while((state != L2CONNECTED_STATE) && (state != CONNECTED_STATE) && (state != DISCONNECTED_STATE) && (i < 100)); + } while((state != L2CONNECTED_STATE) && (state != CONNECTED_STATE) && (i < 100)); - if((state != L2CONNECTED_STATE) && (state != CONNECTED_STATE) && (state != DISCONNECTED_STATE)){ + if((state != L2CONNECTED_STATE) && (state != CONNECTED_STATE)){ /* send disconnect */ sprintf(cmd, "%s", "DISCONNECT"); wifi_command(cmd, reply, sizeof(reply)); @@ -355,6 +403,17 @@ int get_scan_status() return scan_complete; } +void set_wifi_off_flag(int value) +{ + wifi_off_flag = value; +} + +int get_wifi_off_flag() +{ + return wifi_off_flag; +} + + void reset_assoc_reject_count() { assoc_reject_count = 0; diff --git a/package/allwinner/wifimanager/src/wifimanager.c b/package/allwinner/wifimanager/src/wifimanager.c index f9a92e0e7..b696030a1 100755 --- a/package/allwinner/wifimanager/src/wifimanager.c +++ b/package/allwinner/wifimanager/src/wifimanager.c @@ -2,6 +2,8 @@ #include #include #include +#include + #include "wifi.h" #include "wifi_event.h" @@ -9,9 +11,11 @@ #include "network_manager.h" #include "wpa_supplicant_conf.h" #include "wifi_intf.h" -//#include +#include #include + + #define ENCODE_STYLE_UTF_8 "UTF-8" #define ENCODE_STYLE_GB2312 "GB2312" #define WPA_SSID_LENTH 512 @@ -19,6 +23,7 @@ #define APP_ERR_NULL -1 #define APP_ERR_NOT_MATCH -2 #define APP_ERR_INVALID_PARAMS -3 + extern int is_ip_exist(); typedef enum @@ -26,63 +31,30 @@ typedef enum WIFI_SSID_UTF_8, WIFI_SSID_GBK, }wifi_ssid_encode_e; + char netid_connecting[NET_ID_LEN+1] = {0}; int disconnecting = 0; int connecting_ap_event_label = 0; int disconnect_ap_event_label = 0; -//tWIFI_STATE gwifi_state = WIFIMG_WIFI_DISABLED; -extern tWIFI_STATE gwifi_state; +tWIFI_STATE gwifi_state = WIFIMG_WIFI_DISABLED; static tWIFI_EVENT event_code = WIFIMG_NO_NETWORK_CONNECTING; static int aw_wifi_disconnect_ap(int event_label); -static char wpa_scan_ssid[WPA_SSID_LENTH]; -static char wpa_conf_ssid[WPA_SSID_LENTH]; -static int ssid_contain_chinese = 0; static int aw_wifi_add_event_callback(tWifi_event_callback pcb) { return add_wifi_event_callback_inner(pcb); } -/* -*get wifi connection state with AP -*return value: -*1: connected with AP(connected to network IPv4) -*2: connected with AP(connected to network IPv6) -*0: disconnected with AP -*/ static int aw_wifi_is_ap_connected(char *ssid, int *len) { - int ret = 0; - tWIFI_MACHINE_STATE wifi_machine_state; - if(gwifi_state == WIFIMG_WIFI_DISABLED){ return -1; } - wifi_machine_state = get_wifi_machine_state(); - if(wifi_machine_state != CONNECTED_STATE && wifi_machine_state != DISCONNECTED_STATE){ - LOG_EX(LOG_Debug, "%s: wifi_busing, please try again later!\n", __func__); - return -1; - } - - /* pase scan thread */ - pause_wifi_scan_thread(); - - if( 4 == wpa_conf_is_ap_connected(ssid, len)) - ret = 1; - else if( 6 == wpa_conf_is_ap_connected(ssid, len)) - ret = 2; - else - ret = 0; - - /* resume scan thread */ - resume_wifi_scan_thread(); - - return ret; + return wpa_conf_is_ap_connected(ssid, len); } - static int aw_wifi_scan(int event_label) { int ret = 0; @@ -115,13 +87,8 @@ static int aw_wifi_get_scan_results(char *result, int *len) return -1; } - if(get_scan_results_inner(result, len) != 0) - { - LOG_EX(LOG_Debug, "%s: There is a scan or scan_results error, Please try scan again later!\n", __func__); - return -1; - } - else - return 0; + get_scan_results_inner(result, len); + return 0; } /* check wpa/wpa2 passwd is right */ @@ -149,145 +116,17 @@ int check_wpa_passwd(const char *passwd) return result; } -/* convert app ssid which contain chinese in utf-8 to wpa scan ssid */ -static int ssid_app_to_wpa_scan(const char *app_ssid, char *scan_ssid) -{ - unsigned char h_val = 0, l_val = 0; - int i = 0; - int chinese_in = 0; - - if(!app_ssid || !app_ssid[0]) - { - LOG_EX(LOG_Debug, "Error: app ssid is NULL!\n"); - return -1; - } - - if(!scan_ssid) - { - LOG_EX(LOG_Debug, "Error: wpa ssid buf is NULL\n"); - return -1; - } - - i = 0; - while(app_ssid[i] != '\0') - { - /* ascii code */ - if((unsigned char)app_ssid[i] <= 0x7f) - { - *(scan_ssid++) = app_ssid[i++]; - } - else /* covert to wpa ssid for chinese code */ - { - *(scan_ssid++) = '\\'; - *(scan_ssid++) = 'x'; - h_val = (app_ssid[i] & 0xf0) >> 4; - if((h_val >= 0) && (h_val <= 9)){ - *(scan_ssid++) = h_val + '0'; - }else if((h_val >= 0x0a) && (h_val <= 0x0f)){ - *(scan_ssid++) = h_val + 'a' - 0xa; - } - - l_val = app_ssid[i] & 0x0f; - if((l_val >= 0) && (l_val <= 9)){ - *(scan_ssid++) = l_val + '0'; - }else if((l_val >= 0x0a) && (l_val <= 0x0f)){ - *(scan_ssid++) = l_val + 'a' - 0xa; - } - i++; - chinese_in = 1; - } - } - *scan_ssid = '\0'; - - if(chinese_in == 1){ - return 1; - } - - return 0; -} - -/* convert app ssid which contain chinese in utf-8 to wpa conf ssid */ -static int ssid_app_to_wpa_conf(const char *app_ssid, char *conf_ssid) -{ - unsigned char h_val = 0, l_val = 0; - int i = 0; - int chinese_in = 0; - - if(!app_ssid || !app_ssid[0]) - { - LOG_EX(LOG_Debug, "Error: app ssid is NULL!\n"); - return -1; - } - - if(!conf_ssid) - { - LOG_EX(LOG_Debug, "Error: wpa ssid buf is NULL\n"); - return -1; - } - - i = 0; - while(app_ssid[i] != '\0') - { - h_val = (app_ssid[i] & 0xf0) >> 4; - if((h_val >= 0) && (h_val <= 9)){ - *(conf_ssid++) = h_val + '0'; - }else if((h_val >= 0x0a) && (h_val <= 0x0f)){ - *(conf_ssid++) = h_val + 'a' - 0xa; - } - - l_val = app_ssid[i] & 0x0f; - if((l_val >= 0) && (l_val <= 9)){ - *(conf_ssid++) = l_val + '0'; - }else if((l_val >= 0x0a) && (l_val <= 0x0f)){ - *(conf_ssid++) = l_val + 'a' - 0xa; - } - i++; - } - *conf_ssid = '\0'; - - return 0; -} - -static int connect_command_handle(char *cmd,char *net_id) -{ - int ret; - char reply[REPLY_BUF_SIZE] = {0}; - ret = wifi_command(cmd, reply, sizeof(reply)); - if(ret){ - LOG_EX(LOG_Debug, "%s failed,Remove the information just connected!",cmd); - sprintf(cmd, "REMOVE_NETWORK %s", net_id); - wifi_command(cmd, reply, sizeof(reply)); - sprintf(cmd, "%s", "SAVE_CONFIG"); - wifi_command(cmd, reply, sizeof(reply)); - ret = -1; - }else{ - LOG_EX(LOG_Debug, "%s: %s",cmd,reply); - return 0; - } -} - -static void cancel_saved_conf_handle(char *net_id) -{ - char reply[REPLY_BUF_SIZE] = {0}; - - char cmd[CMD_LEN+1] = {0}; - sprintf(cmd, "REMOVE_NETWORK %s", net_id); - cmd[CMD_LEN] = '\0'; - wifi_command(cmd, reply, sizeof(reply)); - sprintf(cmd, "%s", "SAVE_CONFIG"); - wifi_command(cmd, reply, sizeof(reply)); -} /* connect visiable network */ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char *passwd, int event_label) { int i=0, ret = -1, len = 0, max_prio = -1; char cmd[CMD_LEN+1] = {0}; char reply[REPLY_BUF_SIZE] = {0}, netid1[NET_ID_LEN+1]={0}, netid2[NET_ID_LEN+1] = {0}; - int is_exist = 0; + int is_exist = 0; tWIFI_MACHINE_STATE wifi_machine_state; const char *p_ssid = NULL; - tWIFI_MACHINE_STATE state; - tWIFI_EVENT_INNER event; + tWIFI_MACHINE_STATE state; + tWIFI_EVENT_INNER event; if(gwifi_state == WIFIMG_WIFI_DISABLED){ return -1; @@ -319,28 +158,11 @@ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char /* remove disconnecting flag */ disconnecting = 0; - /* convert app ssid to wpa scan ssid */ - ret = ssid_app_to_wpa_scan(ssid, wpa_scan_ssid); - if(ret < 0){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - }else if(ret > 0){ - ssid_contain_chinese = 1; - }else { - ssid_contain_chinese = 0; - } - - /* has no chinese code */ - if(ssid_contain_chinese == 0){ - p_ssid = ssid; - }else{ - ssid_app_to_wpa_conf(ssid, wpa_conf_ssid); - p_ssid = wpa_conf_ssid; - } + p_ssid = ssid; + /* check already exist or connected */ - len = NET_ID_LEN+1; + len = 3; is_exist = wpa_conf_is_ap_exist(p_ssid, key_mgmt, netid1, &len); /* add network */ @@ -354,14 +176,17 @@ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char goto end; } - /* set network ssid */ - if(ssid_contain_chinese == 0){ - sprintf(cmd, "SET_NETWORK %s ssid \"%s\"", netid2, p_ssid); - }else{ - sprintf(cmd, "SET_NETWORK %s ssid %s", netid2, p_ssid); - } + sprintf(cmd, "SET_NETWORK %s ssid %s", netid2, p_ssid); - if(connect_command_handle(cmd,netid2)){ + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network ssid error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); ret = -1; event_code = WIFIMG_CMD_OR_PARAMS_ERROR; goto end; @@ -371,67 +196,124 @@ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char if (key_mgmt == WIFIMG_NONE){ /* set network no passwd */ sprintf(cmd, "SET_NETWORK %s key_mgmt NONE", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network key_mgmt error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; } } else if(key_mgmt == WIFIMG_WPA_PSK || key_mgmt == WIFIMG_WPA2_PSK){ /* set network psk passwd */ sprintf(cmd,"SET_NETWORK %s key_mgmt WPA-PSK", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network key_mgmt WPA-PSK error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } ret = check_wpa_passwd(passwd); if(ret == 0){ LOG_EX(LOG_Debug, "check wpa-psk passwd is error!\n"); - cancel_saved_conf_handle(netid2); + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + cmd[CMD_LEN] = '\0'; + wifi_command(cmd, reply, sizeof(reply)); + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); ret = -1; event_code = WIFIMG_CMD_OR_PARAMS_ERROR; goto end; } sprintf(cmd, "SET_NETWORK %s psk \"%s\"", netid2, passwd); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } - + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network psk error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } } else if(key_mgmt == WIFIMG_WEP){ /* set network key_mgmt none */ - sprintf(cmd, "SET_NETWORK %s key_mgmt NONE", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } - + sprintf(cmd, "SET_NETWORK %s key_mgmt NONE", netid2); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network key_mgmt none error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } /* set network wep_key0 */ - sprintf(cmd, "SET_NETWORK %s wep_key0 %s", netid2, passwd); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } - + sprintf(cmd, "SET_NETWORK %s wep_key0 %s", netid2, passwd); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + sprintf(cmd, "SET_NETWORK %s wep_key0 \"%s\"", netid2, passwd); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network wep_key0 error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } + } /* set network auth_alg */ - sprintf(cmd, "SET_NETWORK %s auth_alg OPEN SHARED", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + sprintf(cmd, "SET_NETWORK %s auth_alg OPEN SHARED", netid2); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network auth_alg error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } - } else { + } else { LOG_EX(LOG_Debug, "Error: key mgmt not support!\n"); /* cancel saved in wpa_supplicant.conf */ - cancel_saved_conf_handle(netid2); + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); ret = -1; event_code = WIFIMG_KEY_MGMT_NOT_SUPPORT; goto end; @@ -439,41 +321,67 @@ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char /* set scan_ssid to 1 for network */ sprintf(cmd,"SET_NETWORK %s scan_ssid 1", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set scan_ssid error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } - - /* get max priority in wpa_supplicant.conf */ + /* get max priority in wpa_supplicant.conf */ max_prio = wpa_conf_get_max_priority(); /* set priority for network */ sprintf(cmd,"SET_NETWORK %s priority %d", netid2, (max_prio+1)); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set priority error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + ret = -1; + goto end; + } /* select network */ sprintf(cmd, "SELECT_NETWORK %s", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } - + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do select network error!\n"); + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } /* save config */ - sprintf(cmd, "%s", "SAVE_CONFIG"); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } - + sprintf(cmd, "%s", "SAVE_CONFIG"); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do save config error!\n"); + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } /* save netid */ strcpy(netid_connecting, netid2); @@ -488,11 +396,10 @@ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char event = get_cur_wifi_event(); /* password incorrect*/ if ((state == DISCONNECTED_STATE) && (event == PASSWORD_INCORRECT)){ - LOG_EX(LOG_Debug, "wifi_connect_ap_inner: password failed!\n"); + LOG_EX(LOG_Debug, "wifi_connect_ap_inner: password failed!\n"); break; } - if(event == CONNECT_AP_TIMEOUT) - break; + if(get_assoc_reject_count() >= MAX_ASSOC_REJECT_COUNT){ reset_assoc_reject_count(); LOG_EX(LOG_Debug, "aw_wifi_add_network: assoc reject %d times\n", MAX_ASSOC_REJECT_COUNT); @@ -514,7 +421,13 @@ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char wifi_command(cmd, reply, sizeof(reply)); /* cancel saved in wpa_supplicant.conf */ - cancel_saved_conf_handle(netid2); + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; event_code = WIFIMG_NETWORK_NOT_EXIST; goto end; @@ -525,14 +438,20 @@ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char wifi_command(cmd, reply, sizeof(reply)); /* cancel saved in wpa_supplicant.conf */ - cancel_saved_conf_handle(netid2); + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); + ret = -1; event_code = WIFIMG_PASSWORD_FAILED; goto end; }else if(event == OBTAINING_IP_TIMEOUT){ if(is_exist == 1 || is_exist == 3){ - //network is exist or connected - sprintf(cmd, "REMOVE_NETWORK %s", netid1); + //network is exist or connected + sprintf(cmd, "REMOVE_NETWORK %s", netid1); cmd[CMD_LEN] = '\0'; wifi_command(cmd, reply, sizeof(reply)); } @@ -542,13 +461,12 @@ static int aw_wifi_add_network(const char *ssid, tKEY_MGMT key_mgmt, const char wifi_command(cmd, reply, sizeof(reply)); ret = 0; }else{ - event_code = WIFIMG_NETWORK_NOT_EXIST; - cancel_saved_conf_handle(netid2); + ; } }else if(state == L2CONNECTED_STATE || state == CONNECTED_STATE){ /* connect ap l2 */ if(is_exist == 1 || is_exist == 3){ - //network is exist or connected - sprintf(cmd, "REMOVE_NETWORK %s", netid1); + //network is exist or connected + sprintf(cmd, "REMOVE_NETWORK %s", netid1); cmd[CMD_LEN] = '\0'; wifi_command(cmd, reply, sizeof(reply)); } @@ -603,24 +521,36 @@ static int aw_wifi_reconfig(void) static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const char *passwd, int event_label) { int i=0, ret = -1, len = 0, max_prio = -1; - char cmd[CMD_LEN+1] = {0}; - char reply[REPLY_BUF_SIZE] = {0}, netid1[NET_ID_LEN+1]={0}, netid2[NET_ID_LEN+1] = {0}; + int status =0; + char cmd[CMD_LEN+1] = {0}; + char reply[REPLY_BUF_SIZE] = {0}, netid1[NET_ID_LEN+1]={0}, netid2[NET_ID_LEN+1] = {0}; int is_exist = 0; tWIFI_MACHINE_STATE state; - tWIFI_EVENT_INNER event; + tWIFI_EVENT_INNER event; + char * pSsidSearch = NULL; - /* connecting */ - set_wifi_machine_state(CONNECTING_STATE); + /* connecting */ + set_wifi_machine_state(CONNECTING_STATE); - /* set connecting event label */ - connecting_ap_event_label = event_label; + /* set connecting event label */ + connecting_ap_event_label = event_label; - /* remove disconnecting flag */ - disconnecting = 0; + /* remove disconnecting flag */ + disconnecting = 0; - /* check already exist or connected */ - len = NET_ID_LEN+1; - is_exist = wpa_conf_is_ap_exist(ssid, key_mgmt, netid1, &len); + /* check already exist or connected */ + len = NET_ID_LEN+1; + status = utf8BytesExchangeEx(ssid,&pSsidSearch); + if(0 != status) + { + LOG_EX(LOG_Debug, "utf8 exchange err\r\n"); + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } + + LOG_EX(LOG_Debug, "changed ssid is %s\r\n",pSsidSearch); + is_exist = wpa_conf_is_ap_exist(pSsidSearch, key_mgmt, netid1, &len); + LOG_EX(LOG_Debug, "is_exist %d\n", is_exist); /* add network */ strncpy(cmd, "ADD_NETWORK", CMD_LEN); @@ -634,37 +564,51 @@ static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const cha } /* set network ssid */ - if(ssid_contain_chinese == 0){ - sprintf(cmd, "SET_NETWORK %s ssid \"%s\"", netid2, ssid); - }else{ - sprintf(cmd, "SET_NETWORK %s ssid %s", netid2, ssid); + sprintf(cmd, "SET_NETWORK %s ssid \"%s\"", netid2, ssid); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network ssid error!\n"); + + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; } - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } - - /* no passwd */ if (key_mgmt == WIFIMG_NONE){ /* set network no passwd */ sprintf(cmd, "SET_NETWORK %s key_mgmt NONE", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network key_mgmt error!\n"); + + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + ret = -1; + goto end; + } } else if(key_mgmt == WIFIMG_WPA_PSK || key_mgmt == WIFIMG_WPA2_PSK){ /* set network psk passwd */ sprintf(cmd,"SET_NETWORK %s key_mgmt WPA-PSK", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network key_mgmt WPA-PSK error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } ret = check_wpa_passwd(passwd); if(ret == 0){ @@ -680,38 +624,68 @@ static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const cha } sprintf(cmd, "SET_NETWORK %s psk \"%s\"", netid2, passwd); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network psk error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } } else if(key_mgmt == WIFIMG_WEP){ /* set network key_mgmt none */ - sprintf(cmd, "SET_NETWORK %s key_mgmt NONE", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + sprintf(cmd, "SET_NETWORK %s key_mgmt NONE", netid2); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network key_mgmt none error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } /* set network wep_key0 */ - sprintf(cmd, "SET_NETWORK %s wep_key0 %s", netid2, passwd); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + sprintf(cmd, "SET_NETWORK %s wep_key0 %s", netid2, passwd); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + sprintf(cmd, "SET_NETWORK %s wep_key0 \"%s\"", netid2, passwd); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network wep_key0 error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } + } /* set network auth_alg */ - sprintf(cmd, "SET_NETWORK %s auth_alg OPEN SHARED", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + sprintf(cmd, "SET_NETWORK %s auth_alg OPEN SHARED", netid2); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set network auth_alg error!\n"); + + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + ret = -1; + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; + } + } else { LOG_EX(LOG_Debug, "Error: key mgmt is not support!\n"); @@ -725,38 +699,65 @@ static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const cha } /* get max priority in wpa_supplicant.conf */ - max_prio = wpa_conf_get_max_priority(); - - /* set priority for network */ - sprintf(cmd,"SET_NETWORK %s priority %d", netid2, (max_prio+1)); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; + // max_prio = wpa_conf_get_max_priority(); + // status = wpa_conf_standardize_priority(); + + status =wpa_conf_refresh_list(50); + if(status) + { + LOG_EX(LOG_Debug, "+++++wpa reset priority err:%d\r\n",status); } + /* set priority for network */ + //sprintf(cmd,"SET_NETWORK %s priority %d", netid2, (max_prio+1)); + sprintf(cmd,"SET_NETWORK %s priority %s", netid2, netid2); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do set priority error!\n"); + + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + ret = -1; + goto end; + } /* select network */ sprintf(cmd, "SELECT_NETWORK %s", netid2); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Debug, "do select network error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + ret = -1; + goto end; + } /* save netid */ strcpy(netid_connecting, netid2); + + pause_wifi_scan_thread(); /* reconnect */ sprintf(cmd, "%s", "RECONNECT"); ret = wifi_command(cmd, reply, sizeof(reply)); - if(connect_command_handle(cmd,netid2)){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - } + if(ret){ + LOG_EX(LOG_Debug, "do reconnect network error!\n"); + /* cancel saved in wpa_supplicant.conf */ + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + + event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + ret = -1; + goto end; + } reset_assoc_reject_count(); @@ -772,6 +773,7 @@ static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const cha LOG_EX(LOG_Debug, "wifi_connect_ap_inner: password failed!\n"); break; } + if(get_assoc_reject_count() >= MAX_ASSOC_REJECT_COUNT){ reset_assoc_reject_count(); LOG_EX(LOG_Debug, "wifi_connect_ap_inner: assoc reject %d times\n", MAX_ASSOC_REJECT_COUNT); @@ -793,8 +795,12 @@ static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const cha wifi_command(cmd, reply, sizeof(reply)); /* cancel saved in wpa_supplicant.conf */ - cancel_saved_conf_handle(netid2); + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); ret = -1; LOG_EX(LOG_Debug, "connect ap inner:still connecting!\n"); @@ -807,8 +813,12 @@ static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const cha wifi_command(cmd, reply, sizeof(reply)); /* cancel saved in wpa_supplicant.conf */ - cancel_saved_conf_handle(netid2); + sprintf(cmd, "REMOVE_NETWORK %s", netid2); + wifi_command(cmd, reply, sizeof(reply)); + /* save config */ + sprintf(cmd, "%s", "SAVE_CONFIG"); + wifi_command(cmd, reply, sizeof(reply)); ret = -1; event_code = WIFIMG_PASSWORD_FAILED; @@ -816,8 +826,8 @@ static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const cha goto end; }else if(event == OBTAINING_IP_TIMEOUT){ if(is_exist == 1 || is_exist == 3){ - //network is exist or connected - sprintf(cmd, "REMOVE_NETWORK %s", netid1); + //network is exist or connected + sprintf(cmd, "REMOVE_NETWORK %s", netid1); cmd[CMD_LEN] = '\0'; wifi_command(cmd, reply, sizeof(reply)); } @@ -850,6 +860,11 @@ static int wifi_connect_ap_inner(const char *ssid, tKEY_MGMT key_mgmt, const cha } end: + free(pSsidSearch); + pSsidSearch = NULL; + + resume_wifi_scan_thread(); + //enable all networks in wpa_supplicant.conf wpa_conf_enable_all_networks(); @@ -887,34 +902,14 @@ static int aw_wifi_connect_ap_key_mgmt(const char *ssid, tKEY_MGMT key_mgmt, con goto end; } - /* convert app ssid to wpa scan ssid */ - ret = ssid_app_to_wpa_scan(ssid, wpa_scan_ssid); - if(ret < 0){ - ret = -1; - event_code = WIFIMG_CMD_OR_PARAMS_ERROR; - goto end; - }else if(ret > 0){ - ssid_contain_chinese = 1; - }else { - ssid_contain_chinese = 0; - } - - /* has no chinese code */ - if(ssid_contain_chinese == 0){ - p_ssid = ssid; - }else{ - ssid_app_to_wpa_conf(ssid, wpa_conf_ssid); - p_ssid = wpa_conf_ssid; - } - /* checking network exist at first time */ - get_key_mgmt(wpa_scan_ssid, key); + get_key_mgmt(ssid, key); /* no password */ - if (key_mgmt == WIFIMG_NONE){ + if (key_mgmt == WIFIMG_NONE){ if(key[0] == 0){ update_scan_results(); - get_key_mgmt(wpa_scan_ssid, key); + get_key_mgmt(ssid, key); if(key[0] == 0){ ret = -1; event_code = WIFIMG_NETWORK_NOT_EXIST; @@ -924,7 +919,8 @@ static int aw_wifi_connect_ap_key_mgmt(const char *ssid, tKEY_MGMT key_mgmt, con }else if(key_mgmt == WIFIMG_WPA_PSK || key_mgmt == WIFIMG_WPA2_PSK){ if(key[1] == 0){ update_scan_results(); - get_key_mgmt(wpa_scan_ssid, key); + get_key_mgmt(ssid, key); + if(key[1] == 0){ ret = -1; event_code = WIFIMG_NETWORK_NOT_EXIST; @@ -934,7 +930,7 @@ static int aw_wifi_connect_ap_key_mgmt(const char *ssid, tKEY_MGMT key_mgmt, con }else if(key_mgmt == WIFIMG_WEP){ if(key[2] == 0){ update_scan_results(); - get_key_mgmt(wpa_scan_ssid, key); + get_key_mgmt(ssid, key); if(key[2] == 0){ ret = -1; event_code = WIFIMG_NETWORK_NOT_EXIST; @@ -950,7 +946,13 @@ static int aw_wifi_connect_ap_key_mgmt(const char *ssid, tKEY_MGMT key_mgmt, con /* pause scan thread */ pause_wifi_scan_thread(); - ret = wifi_connect_ap_inner(p_ssid, key_mgmt, passwd, event_label); + /* ensure wifi disconnect */ + state = get_wifi_machine_state(); + if(state == CONNECTED_STATE){ + aw_wifi_disconnect_ap(0x7fffffff); + } + + ret = wifi_connect_ap_inner(ssid, key_mgmt, passwd, event_label); end: if(ret != 0){ @@ -964,60 +966,261 @@ end: } +static int encode_transfor(const char * from_style,const char * to_style,char *in_buf,int in_size,char * out_buf,int * out_size) +{ + int res =0; + iconv_t cid = 0; + size_t encodeSize =0; + char **pin = &in_buf; + char **pout = &out_buf; + + if(NULL == in_buf || NULL == out_buf) return -1; + + memset(out_buf,0x00,*out_size); + + cid = iconv_open(to_style,from_style); + + if(-1 == cid) + { + perror("iconv_open"); + return -1; + } + + encodeSize = iconv(cid, pin, &in_size, pout, out_size); + if(-1 == encodeSize) + { + res = -1; + } + + iconv_close(cid ); + + return 0; + +} +static int buffer_compare(char* buf1,char *buf2,int size,bool isStr) +{ + int i=0; + + if(NULL == buf1 || NULL == buf2) return APP_ERR_NULL; + if(isStr) + { + if(size > strlen(buf1) || size > strlen(buf2) ) + return APP_ERR_INVALID_PARAMS; + } + + for(i=0;i 0){ - ssid_contain_chinese = 1; - }else { - ssid_contain_chinese = 0; - } + memset(key,0x00,4*sizeof(int)); + + p_ssid = ssid; - /* has no chinese code */ - if(ssid_contain_chinese == 0){ - p_ssid = ssid; - }else{ - ssid_app_to_wpa_conf(ssid, wpa_conf_ssid); - p_ssid = wpa_conf_ssid; - } + /* checking network exist at first time */ + get_key_mgmt(p_ssid, key); - /* checking network exist at first time */ - get_key_mgmt(p_ssid, key); - - /* no password */ - if(!passwd || !passwd[0]){ + /* no password */ + if(!passwd || !passwd[0]){ if(key[0] == 0){ update_scan_results(); get_key_mgmt(p_ssid, key); @@ -1031,11 +1234,20 @@ static int aw_wifi_connect_ap(const char *ssid, const char *passwd, int event_la /* pase scan thread */ pause_wifi_scan_thread(); - ret = wifi_connect_ap_inner(p_ssid, WIFIMG_NONE, passwd, event_label); - }else{ + /* ensure disconnected */ + state = get_wifi_machine_state(); + if (state == CONNECTED_STATE){ + aw_wifi_disconnect_ap(0x7fffffff); + } + + ret = wifi_connect_ap_inner(p_ssid, WIFIMG_NONE, passwd, event_label); + }else{ + + if((key[1] == 0) && (key[2] == 0)){ update_scan_results(); - get_key_mgmt(wpa_scan_ssid, key); + get_key_mgmt(p_ssid, key); + if((key[1] == 0) && (key[2] == 0)){ ret = -1; event_code = WIFIMG_NETWORK_NOT_EXIST; @@ -1043,24 +1255,25 @@ static int aw_wifi_connect_ap(const char *ssid, const char *passwd, int event_la } } + /* pause scan thread */ pause_wifi_scan_thread(); /* wpa-psk */ if(key[1] == 1){ /* try WPA-PSK */ - ret = wifi_connect_ap_inner(p_ssid, WIFIMG_WPA_PSK, passwd, event_label); + ret = wifi_connect_ap_inner(p_ssid, WIFIMG_WPA_PSK, passwd, event_label); if(ret == 0){ goto end; } } - /* wep */ - if(key[2] == 1){ - /* try WEP */ - ret = wifi_connect_ap_inner(p_ssid, WIFIMG_WEP, passwd, event_label); - } - } + /* wep */ + if(key[2] == 1){ + /* try WEP */ + ret = wifi_connect_ap_inner(p_ssid, WIFIMG_WEP, passwd, event_label); + } + } end: if(ret != 0){ @@ -1217,7 +1430,6 @@ static int aw_wifi_remove_network(char *ssid, tKEY_MGMT key_mgmt) char cmd[CMD_LEN+1] = {0}; char reply[REPLY_BUF_SIZE] = {0}; char net_id[NET_ID_LEN+1] = {0}; - tWIFI_MACHINE_STATE wifi_machine_state; if(gwifi_state == WIFIMG_WIFI_DISABLED){ return -1; @@ -1228,12 +1440,6 @@ static int aw_wifi_remove_network(char *ssid, tKEY_MGMT key_mgmt) return -1; } - wifi_machine_state = get_wifi_machine_state(); - if(wifi_machine_state != CONNECTED_STATE && wifi_machine_state != DISCONNECTED_STATE){ - wmg_printf(MSG_INFO,"%s: wifi_busing, please try again later!\n", __func__); - return -1; - } - /* pause scan thread */ pause_wifi_scan_thread(); @@ -1274,25 +1480,14 @@ static int aw_wifi_remove_network(char *ssid, tKEY_MGMT key_mgmt) static int aw_wifi_remove_all_networks() { - int ret = -1; - tWIFI_MACHINE_STATE wifi_machine_state; - if(gwifi_state == WIFIMG_WIFI_DISABLED){ return -1; } - wifi_machine_state = get_wifi_machine_state(); - if(wifi_machine_state != CONNECTED_STATE && wifi_machine_state != DISCONNECTED_STATE){ - wmg_printf(MSG_ERROR,"%s: wifi_busing, please try again later!\n", __func__); - return -1; - } - - pause_wifi_scan_thread(); - ret = wpa_conf_remove_all_networks(); - resume_wifi_scan_thread(); - return ret; + wpa_conf_remove_all_networks(); } + static int aw_wifi_enble_all_network() { if(gwifi_state == WIFIMG_WIFI_DISABLED){ @@ -1304,7 +1499,7 @@ static int aw_wifi_enble_all_network() static int aw_wifi_connect_ap_auto(int event_label) { - int i=0, ret = -1, len = 0; + int i=0, ret =0, len = 0; char cmd[CMD_LEN+1] = {0}, reply[REPLY_BUF_SIZE] = {0}; char netid[NET_ID_LEN+1]={0}; tWIFI_MACHINE_STATE wifi_machine_state; @@ -1313,9 +1508,12 @@ static int aw_wifi_connect_ap_auto(int event_label) return -1; } + wpa_conf_enable_all_networks(); + /* pase scan thread */ pause_wifi_scan_thread(); + wifi_machine_state = get_wifi_machine_state(); if(wifi_machine_state == CONNECTED_STATE){ ret = -1; @@ -1343,13 +1541,17 @@ static int aw_wifi_connect_ap_auto(int event_label) disconnecting = 0; connecting_ap_event_label = event_label; + + pause_wifi_scan_thread(); + /* reconnected */ sprintf(cmd, "%s", "RECONNECT"); ret = wifi_command(cmd, reply, sizeof(reply)); if(ret){ - wmg_printf(MSG_ERROR,"do reconnect error!\n"); + LOG_EX(LOG_Debug, "do reconnect error!\n"); ret = -1; event_code = WIFIMG_CMD_OR_PARAMS_ERROR; + goto end; } /* check timeout */ @@ -1360,8 +1562,10 @@ end: call_event_callback_function(event_code, NULL, event_label); } + + /* resume scan thread */ - resume_wifi_scan_thread(); + if(0 != ret)resume_wifi_scan_thread(); return ret; } @@ -1585,46 +1789,28 @@ end: *Ap with certain key_mgmt exists in the .conf file:return is 0, get the *net_id as expectation; *else:return -1 */ -static int aw_wifi_get_netid(const char *ssid, tKEY_MGMT key_mgmt, char *net_id, int *length) +static int aw_wifi_get_netid(const char *ssid, tKEY_MGMT key_mgmt, char *net_id) { - int ret = -1, len = NET_ID_LEN+1; - - if(*length > (NET_ID_LEN+1)) - len = NET_ID_LEN+1; - else - len = *length; + int ret = -1, len = 3; /* pause scan thread */ pause_wifi_scan_thread(); ret = wpa_conf_is_ap_exist(ssid, key_mgmt, net_id, &len); /* resume scan thread */ - resume_wifi_scan_thread(); + resume_wifi_scan_thread(); if(ret == 1 || ret == 3){ - *length = len; return 0; }else{ return -1; } } -static int aw_wifi_stop_scan() -{ - shutdown_wifi_scan_thread(); - return 0; -} - -static int aw_wifi_restart_scan() -{ - restart_wifi_scan_thread(); - return 0; -} - static const aw_wifi_interface_t aw_wifi_interface = { aw_wifi_add_event_callback, aw_wifi_is_ap_connected, aw_wifi_scan, aw_wifi_get_scan_results, - aw_wifi_connect_ap, + aw_wifi_connect_ap_ex, aw_wifi_connect_ap_key_mgmt, aw_wifi_connect_ap_auto, aw_wifi_connect_ap_with_netid, @@ -1637,15 +1823,13 @@ static const aw_wifi_interface_t aw_wifi_interface = { aw_wifi_get_netid, aw_wifi_change_ap, aw_wifi_enble_all_network, - aw_wifi_stop_scan, - aw_wifi_restart_scan }; const aw_wifi_interface_t * aw_wifi_on(tWifi_event_callback pcb, int event_label) { - int i = 0, ret = -1, connected = 0, len = 64; - char ssid[64]; - struct wpa_status * sta; + int i = 0, ret = -1, connected = 0, len = 3; + char netid[4]; + if(gwifi_state != WIFIMG_WIFI_DISABLED){ return NULL; } @@ -1666,7 +1850,6 @@ const aw_wifi_interface_t * aw_wifi_on(tWifi_event_callback pcb, int event_label if(ret < 0){ if(pcb != NULL){ pcb(WIFIMG_WIFI_ON_FAILED, NULL, event_label); - LOG_EX(LOG_Debug, "connect wpa_supplicant failed,please check wifi driver!\n"); } return NULL; } @@ -1680,29 +1863,37 @@ const aw_wifi_interface_t * aw_wifi_on(tWifi_event_callback pcb, int event_label aw_wifi_add_event_callback(pcb); wifi_event_loop(NULL); - sta = get_wpa_status(); + /* check has network info in wpa_supplicant.conf */ - if(sta->wpa_state >= WPA_SCANNING){ + if(wpa_conf_network_info_exist() == 1){ set_wifi_machine_state(CONNECTING_STATE); /* wpa_supplicant already run by other process and connected an ap */ - connected = wpa_conf_is_ap_connected(ssid, &len); - if(connected >= 4){ + connected = wpa_conf_is_ap_connected(netid, &len); + if(connected == 1){ set_wifi_machine_state(CONNECTED_STATE); - set_cur_wifi_event(AP_CONNECTED); - call_event_callback_function(WIFIMG_NETWORK_CONNECTED, NULL, event_label); + set_cur_wifi_event(AP_CONNECTED); ret = 0; }else{ - connecting_ap_event_label = event_label; - start_wifi_on_check_connect_timeout(); + //connecting_ap_event_label = event_label; + //start_wifi_on_check_connect_timeout(); + set_wifi_machine_state(DISCONNECTED_STATE); + set_cur_wifi_event(AP_DISCONNECTED); + ret = 0; } }else{ set_wifi_machine_state(DISCONNECTED_STATE); event_code = WIFIMG_NO_NETWORK_CONNECTING; ret = -1; } + + stop_connecting(); start_wifi_scan_thread(NULL); + start_broadlink_thread(NULL); + + usleep(300*1000); + if(ret != 0){ call_event_callback_function(event_code, NULL, event_label); } @@ -1715,29 +1906,27 @@ int aw_wifi_off(const aw_wifi_interface_t *p_wifi_interface) const aw_wifi_interface_t *p_aw_wifi_intf = &aw_wifi_interface; if(p_aw_wifi_intf != p_wifi_interface){ - call_event_callback_function(WIFIMG_WIFI_OFF_FAILED, NULL, 0); + call_event_callback_function(WIFIMG_WIFI_OFF_FAILED, NULL, 0); return -1; } if(gwifi_state == WIFIMG_WIFI_DISABLED){ return 0; } - wpa_status_free(); - stop_wifi_scan_thread(); + + stop_wifi_scan_thread(); wifi_close_supplicant_connection(); wifi_stop_supplicant(0); - system("/etc/wifi/udhcpc_wlan0 stop"); - while(gwifi_state != WIFIMG_WIFI_DISABLED) - usleep(200*1000); reset_wifi_event_callback(); gwifi_state = WIFIMG_WIFI_DISABLED; + set_wifi_off_flag(1); return 0; } int aw_wifi_get_wifi_state() { int ret = -1, len = 0; - char ssid[64] = {0}; + char netid[4] = {0}; tWIFI_MACHINE_STATE machine_state; int tmp_state; @@ -1751,9 +1940,9 @@ int aw_wifi_get_wifi_state() } /* sync wifi state by wpa_supplicant */ - len = sizeof(ssid); - ret = wpa_conf_is_ap_connected(ssid, &len); - if(ret >= 4){ + len = 3; + ret = aw_wifi_is_ap_connected(netid, &len); + if(ret == 1){ tmp_state = WIFIMG_WIFI_CONNECTED; }else{ tmp_state = WIFIMG_WIFI_DISCONNECTED; diff --git a/package/allwinner/wifimanager/src/wpa_supplicant_conf.c b/package/allwinner/wifimanager/src/wpa_supplicant_conf.c index 4a0fba88b..e7233a4d6 100755 --- a/package/allwinner/wifimanager/src/wpa_supplicant_conf.c +++ b/package/allwinner/wifimanager/src/wpa_supplicant_conf.c @@ -9,6 +9,9 @@ #define WPA_DIASBLE_NETWORK_CMD "DISABLE_NETWORK" #define WPA_INTERNET_RECONNECT_SAVE 1 +static const char SUPP_CONFIG_FILE[] = "/mnt/UDISK/wifi/wpa_supplicant.conf"; + + extern int is_ip_exist(); int wpa_conf_network_info_exist() @@ -62,7 +65,8 @@ int wpa_conf_is_ap_exist(const char *ssid, tKEY_MGMT key_mgmt, char *net_id, int return -1; } - //LOG_EX(LOG_Debug, "List network:%s\r\n",reply) + LOG_EX(LOG_Debug, "List network:%s\r\n",reply); + LOG_EX(LOG_Debug, "Ssid:%s\r\n",ssid); ptr = reply; while((pssid_start=strstr(ptr, ssid)) != NULL){ @@ -186,11 +190,11 @@ int wpa_conf_ssid2netid(char *ssid, tKEY_MGMT key_mgmt, char *net_id, int *len) p_t = strchr(p_s, '\t'); if(p_t){ - int tmp = 0; + int tmp = 0; tmp = p_t - p_s; if(tmp <= NET_ID_LEN){ - strncpy(net_id, p_s, tmp); - net_id[tmp] = '\0'; + strncpy(net_id, p_s, tmp); + net_id[tmp] = '\0'; } } @@ -550,9 +554,7 @@ int wpa_conf_is_ap_connected(char *ssid, int *len) int ret = -1; char cmd[CMD_LEN+1] = {0}; char reply[REPLY_BUF_SIZE] = {0}; - char *p_c=NULL, *p_str = NULL; - char *p_s=NULL, *p_e=NULL, *p=NULL; - int is_ap_connected = 0; + char *p_c=NULL; strncpy(cmd, "LIST_NETWORKS", CMD_LEN); cmd[CMD_LEN] = '\0'; @@ -564,21 +566,15 @@ int wpa_conf_is_ap_connected(char *ssid, int *len) } LOG_EX(LOG_Debug, "----list network:%s\r\n",reply); - p_str = (char *)reply; - while((p_c=strstr(p_str, "[CURRENT]")) != NULL){ - - if(*(p_c + 9) != '\n' && *(p_c + 9) != '\0') - { - p_str = p_c+9; - continue; - } + if ((p_c=strstr(reply, "CURRENT")) != NULL){ + char *p_s=NULL, *p_e=NULL, *p=NULL; p_e = strchr(p_c, '\n'); if(p_e){ *p_e = '\0'; } - p_s = strrchr(p_str, '\n'); + p_s = strrchr(reply, '\n'); p_s++; p = strtok(p_s, "\t"); p = strtok(NULL, "\t"); @@ -587,19 +583,18 @@ int wpa_conf_is_ap_connected(char *ssid, int *len) strncpy(ssid, p, *len-1); ssid[*len-1] = '\0'; *len = strlen(ssid); - is_ap_connected = 1; - break; } } - } + /* check ip exist */ + ret = is_ip_exist(); + if(ret > 0){ + return 1; + } - /* check ip exist */ - ret = is_ip_exist(); - if(ret > 0 && is_ap_connected == 1){ - return ret; - }else{ - return is_ap_connected; + return 0; + } else { + return 0; } } @@ -663,6 +658,7 @@ int wpa_conf_get_ap_connected(char *netid, int *len) LOG_EX(LOG_Debug, "do list networks error!\n"); return -1; } + LOG_EX(LOG_Debug, "%s\n", reply); if ((p_c=strstr(reply, "CURRENT")) != NULL){ char *p_s=NULL, *p_e=NULL, *p=NULL; @@ -689,6 +685,104 @@ int wpa_conf_get_ap_connected(char *netid, int *len) } +int wpa_conf_get_apinfo_connected(char *key_mgmt, int *priority) +{ + int ret = 0; + char cmd[CMD_LEN+1] = {0}; + char reply[REPLY_BUF_SIZE] = {0}; + char netid[NET_ID_LEN+1] = {0}; + int len = NET_ID_LEN+1; + + if (netid == NULL || key_mgmt == NULL || priority == NULL) + return -1; + + if (1 != wpa_conf_get_ap_connected(netid, &len)) + return -1; + + /* Get key_mgmt */ + sprintf(cmd, "GET_NETWORK %s key_mgmt", netid); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Error, "Get network key_mgmt error!\n"); + return -1; + } + strcpy(key_mgmt, reply); + LOG_EX(LOG_Debug, "Get network key_mgmt %s\n", key_mgmt); + + /* Get priority */ + sprintf(cmd, "GET_NETWORK %s priority", netid); + ret = wifi_command(cmd, reply, sizeof(reply)); + if(ret){ + LOG_EX(LOG_Error, "Get network priority error!\n"); + return -1; + } + *priority = atoi(reply); + LOG_EX(LOG_Debug, "Get network priority %d\n", *priority); + + return 0; +} + +int wpa_conf_get_appwd_connected(int priority, char *pwd) +{ + int ret = 0; + int i; + FILE *fp = NULL; + char line[128] = {0}; + char *p_tmp = NULL; + int prio_tmp = 0; + char pwd_tmp[128] = {0}; + + if (pwd == NULL) + return -1; + + fp = fopen(SUPP_CONFIG_FILE, "r"); + if (!fp) { + LOG_EX(LOG_Debug, "Failed to fopen file: %s\n", SUPP_CONFIG_FILE); + return -1; + } + + while (fgets(line, sizeof(line), fp) != NULL) + { + if (!strncmp(line, "network={", 9)) + { + /* If priority does not exist, default to 0. */ + prio_tmp = 0; + while (fgets(line, sizeof(line), fp) != NULL + && strncmp(line, "}", 1)) + { + if ((p_tmp = strstr(line, "priority=")) != NULL) + { + p_tmp += 9; + sscanf(p_tmp, "%d\n", &prio_tmp); + } + if ((p_tmp = strstr(line, "psk=\"")) != NULL) + { + p_tmp += 5; + strncpy(pwd_tmp, p_tmp, strlen(p_tmp)); + } + } + if (prio_tmp == priority) + { + i=0; + while(pwd_tmp[i+1] != '\n') + { + pwd[i] = pwd_tmp[i]; + i++; + } + pwd[i] = '\0'; + LOG_EX(LOG_Debug, "Get network(priority %d) password succeed %s\n", priority, pwd); + fclose(fp); + return 0; + } + } + } + + LOG_EX(LOG_Debug, "Get network(priority %d) password failed!\n", priority); + fclose(fp); + return -1; +} + + int wpa_conf_enable_all_networks() { int ret = -1, len = 0; diff --git a/package/allwinner/wifimanager/udhcpc_wlan0.init b/package/allwinner/wifimanager/udhcpc_wlan0.init index ef308373c..fa56663a9 100755 --- a/package/allwinner/wifimanager/udhcpc_wlan0.init +++ b/package/allwinner/wifimanager/udhcpc_wlan0.init @@ -16,8 +16,8 @@ start_service() { then procd_set_param command $PROG -i wlan0 -h When_you_like_AW -S -T 1 else -# host_name=`cat /mnt/UDISK/bt/bt_config.xml | grep local` - procd_set_param command $PROG -i wlan0 -h "SING-PV2" -S -T 1 + host_name=`cat /mnt/UDISK/bt/bt_config.xml | grep local` + procd_set_param command $PROG -i wlan0 -h "SING-${host_name:32:4}" -S -T 1 fi procd_close_instance } diff --git a/package/firmware/ap6236/fw_bcm43436b0.bin b/package/firmware/ap6236/fw_bcm43436b0.bin index e8f122a38..40cffe0ac 100755 Binary files a/package/firmware/ap6236/fw_bcm43436b0.bin and b/package/firmware/ap6236/fw_bcm43436b0.bin differ diff --git a/package/libs/libiconv-full/Makefile b/package/libs/libiconv-full/Makefile old mode 100644 new mode 100755 index b75a4001d..e39319ef6 --- a/package/libs/libiconv-full/Makefile +++ b/package/libs/libiconv-full/Makefile @@ -49,7 +49,8 @@ define Package/iconv TITLE+= utility endef -TARGET_CFLAGS += $(FPIC) -DUSE_DOS +#TARGET_CFLAGS += $(FPIC) -DUSE_DOS +TARGET_CFLAGS += $(FPIC) CONFIGURE_ARGS += \ --enable-shared \ @@ -66,11 +67,15 @@ endef define Build/InstallDev $(INSTALL_DIR) $(1)/usr/lib/libiconv-full/include + $(INSTALL_DIR) $(1)/usr/include + $(INSTALL_DIR) $(1)/usr/include/libiconv-full $(CP) $(PKG_INSTALL_DIR)/usr/include/iconv.h $(1)/usr/lib/libiconv-full/include/ - + $(CP) $(PKG_INSTALL_DIR)/usr/include/iconv.h $(1)/usr/include/libiconv-full $(INSTALL_DIR) $(1)/usr/lib/libiconv-full/lib $(CP) $(PKG_INSTALL_DIR)/usr/lib/libcharset.{a,so*} $(1)/usr/lib/libiconv-full/lib/ $(CP) $(PKG_INSTALL_DIR)/usr/lib/libiconv.{a,so*} $(1)/usr/lib/libiconv-full/lib/ + $(INSTALL_DIR) $(1)/usr/bin + $(CP) $(PKG_INSTALL_DIR)/usr/bin/iconv $(1)/usr/bin/ endef define Package/libcharset/install