| /**@File lib_wifi6.c |
| * @Brief :about function test |
| * @details : |
| * @Author : you.chen |
| * @Date : 2022-4-6 |
| * @Version : V1.0 |
| * @copy ritght : Copyright (c) MobileTek |
| */ |
| #include <log/log.h> |
| #include <stdio.h> |
| #include <sys/types.h> |
| #include <stdlib.h> |
| #include <string.h> |
| #include "libwifi6.h" |
| #include <wpa_ctrl.h> |
| #include <string.h> |
| #include <time.h> |
| #include <pthread.h> |
| #include <sys/socket.h> |
| #include <netinet/in.h> |
| #include <arpa/inet.h> |
| #include <netdb.h> |
| #include <ifaddrs.h> |
| |
| #ifdef __cplusplus |
| extern "C" { |
| #endif |
| #ifdef __cplusplus |
| } |
| #endif |
| |
| #define MAX_CMD 128 |
| #define MAX_RET 4096 |
| #define MODE_LEN 10 |
| #define CTRL_STA 0 |
| #define CTRL_AP 1 |
| #define AP_NETWORK_0 0 |
| |
| pthread_t g_ap_watcher_pid = 0; |
| volatile int g_ap_watcher_stop_flag = 0; |
| volatile int g_ap_watcher_started_flag = 0; |
| |
| pthread_t g_sta_watcher_pid = 0; |
| volatile int g_sta_watcher_stop_flag = 0; |
| volatile int g_sta_scan_finish_flag = 1; |
| volatile int g_sta_watcher_started_flag = 0; |
| |
| void * g_ap_callback_priv = NULL; |
| AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL; |
| void * g_sta_callback_priv = NULL; |
| STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL; |
| |
| //const char * CTRL_PATH="/var/run/wpa_supplicant"; |
| const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"}; |
| //const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"}; |
| const char * cmd_list_networks = "LIST_NETWORKS"; |
| const char * cmd_save_config = "SAVE_CONFIG"; |
| const char * cmd_disconnect = "DISCONNECT"; |
| const char * cmd_remove_all = "REMOVE_NETWORK all"; |
| const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS"; |
| const char * STATE_COMPLETED = "COMPLETED"; |
| |
| struct local_wpa_ctrl{ |
| struct wpa_ctrl *ctrl; |
| pthread_mutex_t mutex; |
| }; |
| |
| static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER; |
| |
| static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0}; |
| |
| //you.chen add for tv-box start |
| volatile int g_gbw_enabled = 0; |
| char * g_gbw_mac = NULL; |
| pthread_t g_gbw_watcher_pid = 0; |
| static int startGBW(); |
| static int stopGBW(); |
| //you.chen add for tv-box end |
| |
| typedef struct __curr_status_info { |
| ap_info_s *ap; |
| char * state; |
| int net_no; |
| }curr_status_info; |
| |
| static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, |
| char *reply, size_t *reply_len, |
| void (*msg_cb)(char *msg, size_t len)) |
| { |
| int ret; |
| if (ctrl->ctrl == NULL) { |
| printf("local_wpa_ctrl_request ctrl is null\n"); |
| return -1; |
| } |
| pthread_mutex_lock(&ctrl->mutex); |
| ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb); |
| pthread_mutex_unlock(&ctrl->mutex); |
| return ret; |
| } |
| |
| static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) { |
| int repeat_cnt; |
| struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; |
| pthread_mutex_lock(&s_check_wpa_ctrl_mutex); |
| printf("inner_get_wpa_ctrl\n"); |
| for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) { |
| pthread_mutex_unlock(&s_check_wpa_ctrl_mutex); |
| // printf("wait enable finish\n"); |
| usleep(500 * 1000); |
| pthread_mutex_lock(&s_check_wpa_ctrl_mutex); |
| } |
| if (NULL == g_lynq_wpa_ctrl[index]) { |
| printf("NULL == g_lynq_wpa_ctrl[index]\n"); |
| goto out_addr; |
| } |
| if (NULL == g_lynq_wpa_ctrl[index]->ctrl) { |
| g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]); |
| if (NULL == g_lynq_wpa_ctrl[index]->ctrl) { |
| printf("wpa_ctrl_open fail\n"); |
| goto out_addr; |
| } |
| pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL); |
| } |
| lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; |
| out_addr: |
| pthread_mutex_unlock(&s_check_wpa_ctrl_mutex); |
| return lynq_wpa_ctrl; |
| } |
| |
| #define PRINT_AND_RETURN_VALUE(str,value) \ |
| {\ |
| perror((str));\ |
| return (value);\ |
| } |
| |
| #define CHECK_IDX(idx, type) do { \ |
| if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \ |
| || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \ |
| printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \ |
| return -1; \ |
| } \ |
| }while (0) |
| |
| #define CHECK_WPA_CTRL(index) int ret = 0;\ |
| size_t reply_len = MAX_RET; \ |
| char cmd_reply[MAX_RET]={0}; \ |
| struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \ |
| do{ \ |
| lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \ |
| if (NULL == lynq_wpa_ctrl) return -1; \ |
| }while(0) |
| |
| #define DO_REQUEST(cmd_str) do { \ |
| reply_len = MAX_RET;\ |
| cmd_reply[0] = '\0'; \ |
| printf("to call [%s]\n", cmd_str); \ |
| ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \ |
| if (ret != 0) { \ |
| printf("call "#cmd_str" fail %d\n", ret); \ |
| return ret; \ |
| } \ |
| cmd_reply[reply_len+1] = '\0'; \ |
| printf("cmd replay [ %s ]\n", cmd_reply); \ |
| }while(0) |
| |
| #define DO_OK_FAIL_REQUEST(cmd_str) do { \ |
| DO_REQUEST(cmd_str); \ |
| if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\ |
| printf("cmd "#cmd_str" return FAIL\n"); \ |
| return -1; \ |
| } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \ |
| printf("cmd "#cmd_str" return not OK|FAIL\n"); \ |
| return -1; \ |
| } \ |
| }while (0) |
| |
| |
| static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len); |
| |
| static void APWatcherThreadProc() { |
| size_t len = MAX_RET; |
| char msg_notify[MAX_RET]; |
| |
| struct wpa_ctrl *lynq_wpa_ctrl = NULL; |
| g_ap_watcher_stop_flag = 0; |
| |
| while (g_ap_watcher_stop_flag == 0) { |
| if (lynq_wpa_ctrl == NULL) { |
| lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change |
| if (lynq_wpa_ctrl == NULL) { |
| usleep(100*1000); |
| continue; |
| } |
| |
| wpa_ctrl_attach(lynq_wpa_ctrl); |
| g_ap_watcher_started_flag = 1; |
| } |
| |
| if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) { |
| usleep(100*1000); |
| continue; |
| } |
| memset(msg_notify, 0, MAX_RET); |
| len = MAX_RET; |
| if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) { |
| msg_notify[len+1] = '\0'; |
| printf("ap------> %s\n", msg_notify); |
| //you.chen change for tv-box start |
| if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) { |
| if (g_ap_callback_func != NULL) |
| g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT); |
| if (g_gbw_enabled == 1 && g_gbw_mac != NULL) { |
| printf("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac)); |
| if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) { |
| stopGBW(); |
| } |
| } |
| } |
| else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) { |
| if (g_ap_callback_func != NULL) |
| g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT); |
| if (g_gbw_enabled == 1 && g_gbw_mac != NULL) { |
| printf("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac)); |
| if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) { |
| startGBW(); |
| } |
| } |
| } |
| //you.chen add for tv-box end |
| } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) |
| } // end while (g_ap_watcher_stop_flag == 0) |
| if (lynq_wpa_ctrl != NULL) { |
| wpa_ctrl_detach(lynq_wpa_ctrl); |
| wpa_ctrl_close(lynq_wpa_ctrl); |
| } |
| } |
| |
| static void STAWatcherThreadProc() { |
| size_t len = MAX_RET; |
| char msg_notify[MAX_RET]; |
| char *pReason; |
| error_number_s error; |
| |
| struct wpa_ctrl *lynq_wpa_ctrl = NULL; |
| g_sta_watcher_stop_flag = 0; |
| |
| while (g_sta_watcher_stop_flag == 0) { |
| if (lynq_wpa_ctrl == NULL) { |
| lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]); |
| if (lynq_wpa_ctrl == NULL) { |
| usleep(100*1000); |
| continue; |
| } |
| |
| wpa_ctrl_attach(lynq_wpa_ctrl); |
| g_sta_watcher_started_flag = 1; |
| } |
| |
| if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) { |
| usleep(100*1000); |
| continue; |
| } |
| memset(msg_notify, 0, MAX_RET); |
| len = MAX_RET; |
| if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) { |
| msg_notify[len+1] = '\0'; |
| printf("sta ------> %s\n", msg_notify); |
| if (strstr(msg_notify, state_scan_result) != NULL) { |
| g_sta_scan_finish_flag = 1; |
| } |
| |
| if (g_sta_callback_func == NULL) { |
| continue; |
| } |
| error = -1; |
| if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) { |
| g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error); |
| } |
| else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) { |
| g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error); |
| } |
| else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) { |
| pReason = strstr(msg_notify, "reason="); |
| if (pReason != NULL) { |
| pReason += strlen("reason="); |
| if (memcmp(pReason, "CONN_FAILED", 11) == 0) { |
| error = LYNQ_TIME_OUT; |
| } |
| else if (memcmp(pReason, "WRONG_KEY", 9) == 0) { |
| error = LYNQ_PSW_ERROR; |
| } |
| g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error); |
| } |
| } |
| else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) { |
| g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error); |
| } |
| else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) { |
| g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP); |
| } |
| } |
| } |
| if (lynq_wpa_ctrl != NULL) { |
| wpa_ctrl_detach(lynq_wpa_ctrl); |
| wpa_ctrl_close(lynq_wpa_ctrl); |
| } |
| } |
| |
| int lynq_wifi_enable(void) |
| { |
| int ret = 0; |
| int i; |
| pthread_mutex_lock(&s_check_wpa_ctrl_mutex); |
| |
| if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) { |
| goto out_enable; |
| } |
| |
| printf("lynq_wifi_enable1\n"); |
| const char * cmd_check_service = |
| "state=`systemctl is-active wg870_drv_insmod.service`\n" |
| "[ \"\"$state == \"active\" ] && exit 0\n" |
| "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n"; |
| // if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) { |
| // return 0; |
| // } |
| |
| ret = system(cmd_check_service); |
| if (ret != 0) { |
| printf("service state %d\n", ret); |
| ret = -1; |
| goto out_enable; |
| } |
| |
| for (i=0; i<10; i++) { |
| if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) { |
| break; |
| } |
| usleep(300*1000); |
| } |
| |
| if (i >= 10) { |
| ret = -1; |
| goto out_enable; |
| } |
| |
| //@todo delete add temp check for socket avilable start (20220606) |
| for (i=0; i<60; i++) |
| { |
| if (system("netstat -an | grep -q DGRAM") == 0) { |
| break; |
| } |
| sleep(1); |
| } |
| |
| if (i >= 60) |
| { |
| ret = -1; |
| goto out_enable; |
| } |
| //@todo delete add temp check for socket avilable end (20220606) |
| |
| if (0 != system("ifconfig | grep -q ap0")) { |
| system("connmanctl enable wifi"); |
| usleep(300*1000); |
| system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect"); |
| system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0"); |
| usleep(300*1000); |
| system("connmanctl tether wifi on lynq 1qaz@WSX#$%^"); |
| usleep(300*1000); |
| system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect"); |
| } |
| |
| if (g_ap_watcher_pid == 0 ) { |
| ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL); |
| if(ret<0){ |
| ret = -1; |
| goto out_enable; |
| } |
| } |
| |
| if (g_sta_watcher_pid == 0 ) { |
| ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL); |
| if(ret<0){ |
| ret = -1; |
| goto out_enable; |
| } |
| } |
| |
| for (i=0; i<10; i++) { |
| usleep(300*1000); |
| if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) { |
| break; |
| } |
| } |
| |
| g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl)); |
| g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl)); |
| memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl)); |
| memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl)); |
| out_enable: |
| pthread_mutex_unlock(&s_check_wpa_ctrl_mutex); |
| return ret; |
| } |
| |
| int lynq_wifi_disable(void) |
| { |
| pthread_mutex_lock(&s_check_wpa_ctrl_mutex); |
| g_ap_watcher_stop_flag = 1; |
| g_sta_watcher_stop_flag = 1; |
| if (g_ap_watcher_pid != 0) |
| pthread_join(g_ap_watcher_pid, NULL); |
| if (g_sta_watcher_pid != 0) |
| pthread_join(g_sta_watcher_pid, NULL); |
| if (g_lynq_wpa_ctrl[0] != NULL) |
| wpa_ctrl_close(g_lynq_wpa_ctrl[0]); |
| if (g_lynq_wpa_ctrl[1] != NULL) |
| wpa_ctrl_close(g_lynq_wpa_ctrl[1]); |
| g_ap_watcher_pid = 0; |
| g_sta_watcher_pid = 0; |
| g_lynq_wpa_ctrl[0] = NULL; |
| g_lynq_wpa_ctrl[1] = NULL; |
| system("systemctl stop wg870_drv_insmod.service"); |
| pthread_mutex_unlock(&s_check_wpa_ctrl_mutex); |
| return 0; |
| } |
| |
| static inline char inner_convert_char(char in) |
| { |
| if (in >= '0' && in <= '9') |
| { |
| return in - '0'; |
| } |
| else if (in >= 'a' && in <= 'f') |
| { |
| return in - 'a' + 10; |
| } |
| else if (in >= 'A' && in <= 'F') |
| { |
| return in - 'A' + 10; |
| } |
| else |
| { |
| return '\xff'; |
| } |
| } |
| |
| static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len) |
| { |
| char *p; |
| size_t pos = 0; |
| if (NULL == out_ssid) |
| return; |
| //printf("input ssid=[%s]\n", ssid); |
| memset(out_ssid, 0, out_ssid_len); |
| if (NULL == ssid) |
| return; |
| p = strchr(ssid, '\\'); |
| if (NULL == p) |
| { |
| strncpy(out_ssid, ssid, out_ssid_len); |
| //printf(" first %s\n", out_ssid); |
| } |
| else |
| { |
| pos = p - ssid; |
| memcpy(out_ssid, ssid, pos); |
| //printf("pos %lu -- %s\n", pos, out_ssid); |
| for(; pos < out_ssid_len; pos ++) |
| { |
| if (p[0] == '\0') |
| { |
| //printf(" out %s\n", out_ssid); |
| return; |
| } |
| else if (p[0] != '\\') |
| { |
| out_ssid[pos] = p[0]; |
| p += 1; |
| } |
| else if (p[1] == 'x' || p[1] == 'X') |
| { |
| out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]); |
| p += 4; |
| } |
| else if (p[1] == '\\') |
| { |
| out_ssid[pos] = '\\'; |
| p += 2; |
| } |
| else if (p[1] == 't') |
| { |
| out_ssid[pos] = '\t'; |
| p += 2; |
| } |
| else if (p[1] == 'r') |
| { |
| out_ssid[pos] = '\r'; |
| p += 2; |
| } |
| else if (p[1] == 'n') |
| { |
| out_ssid[pos] = '\n'; |
| p += 2; |
| }//todo find a better way to convert? |
| } |
| } |
| //printf(" out %s\n", out_ssid); |
| } |
| |
| static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) { |
| int i, ssid_len; |
| char lynq_cmd_get[128]={0}; |
| |
| if (out_put == NULL) { |
| printf("output ptr is null\n"); |
| return -1; |
| } |
| if (param_name == NULL) { |
| printf("param ptr is null"); |
| return -1; |
| } |
| if (param_name[0] == '\0') { |
| printf("param is empty"); |
| return -1; |
| } |
| |
| sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name); |
| |
| CHECK_WPA_CTRL(interface); |
| |
| DO_REQUEST(lynq_cmd_get); |
| |
| if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| return -1; |
| } |
| |
| // printf("reply len %d, %08x\n", reply_len, (int)out_put); |
| if (strcmp(param_name, "ssid") == 0) |
| { |
| if (cmd_reply[0] == '\"') { |
| ssid_len = reply_len - 1; |
| memcpy(out_put, cmd_reply + 1, ssid_len); |
| if (out_put[ssid_len-1] == '\"') |
| { |
| out_put[ssid_len-1] = '\0'; |
| } |
| else |
| { |
| out_put[ssid_len] = '\0'; |
| } |
| } |
| else{ |
| ssid_len = reply_len / 2; |
| for(i=0; i<ssid_len; i++) |
| { |
| out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]); |
| } |
| out_put[ssid_len] = '\0'; |
| } |
| } |
| else |
| { |
| memcpy(out_put, cmd_reply, reply_len + 1); |
| } |
| return 0; |
| } |
| |
| static int lynq_split(char * str, int len, char delimiter, char * results[]) { |
| int ret = 0; |
| char * end = str + len - 1; |
| results[ret++] = str; |
| while(str < end) { |
| if (*str == delimiter) { |
| *str++ = '\0'; |
| results[ret++] = str; |
| continue; |
| } |
| str++; |
| } |
| if (*str == delimiter) { |
| *str = '\0'; |
| } |
| |
| return ret; |
| } |
| |
| static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len) |
| { |
| char * p; |
| int ret = 0; |
| char cmd[256]={0}; |
| if (NULL == mac || NULL == ip) |
| return -1; |
| memset(ip, 0, ip_len); |
| sprintf(cmd, "ip neigh | grep \"lladdr\" | grep \"tether\" | grep \"%s\" | head -1 | awk '{print $1}'", mac); |
| ret = exec_cmd(cmd, ip, ip_len); |
| p = strchr(ip, '\n'); |
| if (NULL != p) |
| { |
| *p = '\0'; |
| } |
| printf("inner_get_ip_by_mac %s\n", ip); |
| return ret; |
| } |
| |
| static int inner_get_hostname_by_ip(char *ip, char *hostname) { |
| struct in_addr addr ={0}; |
| struct hostent *ht; |
| |
| if (ip == NULL || *ip == '\0' || hostname == NULL) { |
| return -1; |
| } |
| |
| *hostname = '\0'; |
| if (inet_aton(ip, &addr) == 0) { |
| printf("---inet_aton fail\n"); |
| return -1; |
| } |
| |
| ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET); |
| |
| if (ht == NULL) { |
| printf("---gethostbyaddr fail\n"); |
| herror(NULL); |
| return -1; |
| } |
| |
| strcpy(hostname, ht->h_name); |
| |
| return 0; |
| } |
| |
| static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid) |
| { |
| int count, index, words_count; |
| char * split_lines[128]= {0}; |
| char * split_words[128] = {0}; |
| char local_ssid[128] = {0}; |
| const char *lynq_wifi_list_networks = "LIST_NETWORKS"; |
| |
| CHECK_WPA_CTRL(ap_sta); |
| |
| DO_REQUEST(lynq_wifi_list_networks); |
| |
| count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| |
| //@todo check ssid field to compatible |
| |
| ret = 0; |
| for(index=1; index < count; index++) { |
| words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words); |
| if (words_count > 2) { |
| inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid)); |
| if (ssid == NULL || strcmp(local_ssid, ssid) == 0) { |
| net_no_list[ret++] = atoi(split_words[0]); |
| } |
| } |
| } |
| |
| return ret; |
| } |
| |
| static int lynq_add_network(int ap_sta) { |
| size_t i=0; |
| CHECK_WPA_CTRL(ap_sta); |
| const char *lynq_wifi_add_network = "ADD_NETWORK"; |
| |
| DO_REQUEST(lynq_wifi_add_network); |
| if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| return -1; |
| } |
| |
| for(i=0;i<reply_len;i++) { |
| if(cmd_reply[i] == '\n') { |
| cmd_reply[i] = '\0'; |
| break; |
| } |
| } |
| return atoi(cmd_reply); |
| } |
| |
| static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no) |
| { |
| int count, index; |
| int net_no_list[128]; |
| |
| |
| count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL); |
| for (index=0; index < count; index++) { |
| if (net_no_list[index] == net_no) { |
| return 0; |
| } |
| } |
| |
| if (count >= 1) |
| index = net_no_list[count - 1]; |
| else |
| index = -1; |
| |
| while (index < net_no ) { |
| index = lynq_add_network(ap_sta); |
| if (index >= net_no) { // required network no created |
| return 0; |
| } |
| else if( index < 0) { |
| printf("add network fail\n"); |
| return -1; |
| } |
| } |
| |
| if (index < 0) |
| return -1; |
| |
| return 0; |
| } |
| |
| static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo |
| if (freq > 5000 && freq < 6000) { |
| return LYNQ_WIFI_5G_band; |
| } |
| else if (freq > 2000 && freq < 3000) { |
| return LYNQ_WIFI_2G_band; |
| } |
| return LYNQ_WIFI_2_and_5G_band; |
| } |
| |
| static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) { |
| if (key_mgmt != NULL) { |
| if (memcmp( key_mgmt, "NONE", 4) == 0) { |
| return LYNQ_WIFI_AUTH_OPEN; |
| } |
| else if (memcmp( key_mgmt, "WEP", 3) == 0){ |
| return LYNQ_WIFI_AUTH_WEP; |
| } |
| else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){ |
| return LYNQ_WIFI_AUTH_WPA_PSK; |
| } |
| else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){ |
| return LYNQ_WIFI_AUTH_WPA2_PSK; |
| } |
| } |
| |
| return -1; |
| } |
| |
| static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) { |
| if (flag != NULL) { |
| if (strstr( flag, "WPA2-PSK") != NULL){ |
| return LYNQ_WIFI_AUTH_WPA2_PSK; |
| } |
| else if (strstr( flag, "WPA-PSK") != NULL){ |
| return LYNQ_WIFI_AUTH_WPA_PSK; |
| } |
| else if (strstr( flag, "WEP") != NULL){ |
| return LYNQ_WIFI_AUTH_WEP; |
| } |
| else if (strstr( flag, "NONE") != NULL) { |
| return LYNQ_WIFI_AUTH_OPEN; |
| } |
| else if (strcmp( flag, "[ESS]") == 0) { |
| return LYNQ_WIFI_AUTH_OPEN; |
| } |
| } |
| |
| return -1; |
| } |
| |
| static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) { |
| switch (bw) { |
| case 10: |
| return LYNQ_WIFI_BANDWIDTH_HT10; |
| break; |
| case 20: |
| return LYNQ_WIFI_BANDWIDTH_HT20; |
| break; |
| case 40: |
| return LYNQ_WIFI_BANDWIDTH_HT40; |
| break; |
| case 80: |
| return LYNQ_WIFI_BANDWIDTH_HT80; |
| break; |
| default: |
| break; |
| } |
| |
| return -1; |
| } |
| |
| static int inner_get_status_info(int interface, curr_status_info *curr_state) { |
| int i, count; |
| char *p; |
| const char *lynq_status_cmd = "STATUS"; |
| const char * FLAG_SSID = "ssid="; |
| const char * FLAG_SBSID = "bssid="; |
| const char * FLAG_KEY_MGMT = "key_mgmt="; |
| const char * FLAG_FREQ = "freq="; |
| const char * FLAG_STATE = "wpa_state="; |
| const char * FLAG_ID = "id="; |
| const char * FLAG_IPADDR = "ip_address="; |
| char *split_lines[128] = {0}; |
| |
| CHECK_WPA_CTRL(interface); |
| |
| if (curr_state == NULL) { |
| return -1; |
| } |
| |
| DO_REQUEST(lynq_status_cmd); |
| |
| count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| |
| curr_state->net_no = -1; |
| ret = -1; |
| for(i=0; i < count; i++) { |
| if (curr_state->ap != NULL) { |
| p = strstr(split_lines[i], FLAG_SBSID); |
| if (p != NULL) { |
| strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac)); |
| ret = 0; |
| continue; |
| } |
| p = strstr(split_lines[i], FLAG_SSID); |
| if (p != NULL) { |
| inner_copy_ssid(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID), sizeof (curr_state->ap->ap_ssid)); |
| ret = 0; |
| continue; |
| } |
| p = strstr(split_lines[i], FLAG_KEY_MGMT); |
| if (p != NULL) { |
| curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT)); |
| printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p); |
| ret = 0; |
| continue; |
| } |
| p = strstr(split_lines[i], FLAG_FREQ); |
| if (p != NULL) { |
| curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ))); |
| ret = 0; |
| continue; |
| } |
| p = strstr(split_lines[i], FLAG_IPADDR); |
| if (p != NULL) { |
| strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip)); |
| ret = 0; |
| continue; |
| } |
| } // end if (ap != NULL) |
| if (curr_state->state != NULL) { |
| p = strstr(split_lines[i], FLAG_STATE); |
| if (p != NULL) { |
| strcpy(curr_state->state, p + strlen(FLAG_STATE)); |
| ret = 0; |
| continue; |
| } |
| |
| } //end else if (state != NULL) |
| if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) { |
| ret = 0; |
| curr_state->net_no = atoi(p + strlen(FLAG_ID)); |
| printf("net_no %d, -- %s\n", curr_state->net_no, p); |
| } |
| } |
| |
| return ret; |
| } |
| |
| |
| int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid) |
| { |
| char lynq_wifi_ssid_cmd[80]={0}; |
| |
| if (ap_ssid == NULL) { |
| printf("ap_ssid is null\n"); |
| return -1; |
| } |
| else { |
| printf("idx:%d ap_ssid : %s\n", idx, ap_ssid); |
| } |
| |
| if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid); |
| |
| DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd); |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| |
| return 0; |
| |
| } |
| |
| int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid) |
| { |
| CHECK_IDX(idx, CTRL_AP); |
| return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid); |
| } |
| |
| /***** |
| *frequency <------>channel |
| * |
| *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165 |
| * |
| * |
| *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825 |
| * |
| * |
| * */ |
| static int lynq_check_set_frequency(int input_frequency){ |
| int legitimate_frequency[]={2412,2417,2422,2427,2432,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825}; |
| int i; |
| int arr_len = sizeof(legitimate_frequency) / sizeof(int); |
| |
| for(i = 0; i < arr_len; i++) |
| { |
| if(input_frequency == legitimate_frequency[i]) |
| break; |
| } |
| |
| if(i == arr_len) |
| { |
| printf("input frequency is eero--->%d,please check it\n", input_frequency); |
| return -1; |
| } |
| |
| return 0; |
| } |
| int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency) |
| { |
| int check; |
| char lynq_wifi_frequency_cmd[128]={0}; |
| char lynq_cmd_mode[128]={0}; |
| char lynq_cmd_slect[128]={0}; |
| |
| //@do check input frequency |
| check = lynq_check_set_frequency(lynq_wifi_frequency); |
| if(check != 0) |
| { |
| printf("do check frequency error\n"); |
| return -1; |
| } |
| if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) |
| { |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency); |
| sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0); |
| sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0); |
| |
| DO_OK_FAIL_REQUEST(cmd_disconnect); |
| DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd); |
| DO_OK_FAIL_REQUEST(lynq_cmd_mode); |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| |
| return 0; |
| } |
| |
| int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency) |
| { |
| char lynq_frequency_str[MAX_RET] = {0}; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) { |
| return -1; |
| } |
| *lynq_wifi_frequency = atoi(lynq_frequency_str); |
| |
| return 0; |
| } |
| |
| int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth) |
| { |
| CHECK_IDX(idx, CTRL_AP); |
| switch(bandwidth){ |
| case LYNQ_WIFI_BANDWIDTH_HT10: |
| { |
| printf("bandwith [%d] not support now\n", bandwidth); |
| return -1; |
| } |
| case LYNQ_WIFI_BANDWIDTH_HT20: |
| { |
| char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6"; |
| system("wl down"); |
| if (system(lynq_cmd_bandwith) != 0 ){ |
| return -1; |
| } |
| system("wl up"); |
| break; |
| } |
| case LYNQ_WIFI_BANDWIDTH_HT40: |
| { |
| char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u"; |
| sprintf(lynq_cmd_bandwith, "wl chanspec "); |
| system("wl down"); |
| if (system(lynq_cmd_bandwith) != 0 ){ |
| return -1; |
| } |
| system("wl up"); |
| break; |
| } |
| case LYNQ_WIFI_BANDWIDTH_HT80: |
| { |
| char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80"; |
| system("wl down"); |
| if (system(lynq_cmd_bandwith) != 0 ){ |
| return -1; |
| } |
| system("wl up"); |
| break; |
| } |
| default: |
| { |
| printf("auth type [%d] not support now\n", bandwidth); |
| return -1; |
| } |
| } |
| |
| |
| return 0; |
| } |
| |
| int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth) |
| { |
| int count = 0; |
| int index = 0; |
| char *split_words[128] = {0}; |
| const char *lynq_chanspec_cmd = "DRIVER chanspec\n"; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| DO_REQUEST(lynq_chanspec_cmd); |
| |
| count = lynq_split(cmd_reply, reply_len, ' ', split_words); |
| for(;index < count; index++) { |
| if (strncmp(split_words[index], "bw", 2) != 0) { |
| continue; |
| } |
| |
| index++; |
| if (index >= count) { |
| return -1; |
| } |
| |
| printf("bw %s\n", split_words[index]); |
| *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index])); |
| return 0; |
| } |
| |
| return -1; |
| } |
| |
| int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel) |
| { |
| char lynq_cmd_channel[MAX_CMD]={0}; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| sprintf(lynq_cmd_channel, "wl channel %d", channel); |
| |
| if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { |
| return -1; |
| } |
| |
| system("wl down"); |
| if (system(lynq_cmd_channel) != 0 ){ |
| return -1; |
| } |
| system("wl up"); |
| return 0; |
| } |
| |
| int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel) |
| { |
| int count = 0; |
| int index = 0; |
| char *split_words[128] = {0}; |
| char lynq_chanspec_cmd[]="DRIVER chanspec\n"; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| DO_REQUEST(lynq_chanspec_cmd); |
| |
| count = lynq_split(cmd_reply, reply_len, ' ', split_words); |
| for(;index < count; index++) { |
| printf("---- %s\n",split_words[index]); |
| if (strncmp(split_words[index], "channel", 2) != 0) { |
| continue; |
| } |
| |
| index++; |
| if (index >= count) { |
| return -1; |
| } |
| |
| *channel = atoi(split_words[index]); |
| return 0; |
| } |
| |
| return -1; |
| } |
| |
| |
| int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth) |
| { |
| char ssid[MAX_CMD] = {0}; |
| int freq = 0; |
| char lynq_auth_cmd[64]={0}; |
| char lynq_auth_alg_cmd[64]={0}; |
| char lynq_psk_cmd[64]={0}; |
| char lynq_pairwise_cmd[64]={0}; |
| lynq_wifi_auth_s org_auth; |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) { |
| return -1; |
| } |
| |
| if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) { |
| if (org_auth == auth) { |
| return 0; |
| } |
| else { |
| if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) { |
| ssid[0] = '\0'; |
| } |
| lynq_wifi_ap_frequency_get(idx, &freq); |
| |
| DO_OK_FAIL_REQUEST(cmd_disconnect); |
| DO_OK_FAIL_REQUEST(cmd_remove_all); |
| if (ssid[0] != '\0') { |
| lynq_wifi_ap_ssid_set(idx, ssid); |
| } |
| if (freq != 0) { |
| lynq_wifi_ap_frequency_set(idx, freq); |
| } |
| } |
| } |
| |
| switch(auth){ |
| case LYNQ_WIFI_AUTH_OPEN: |
| { |
| sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0); |
| sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0); |
| |
| DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| break; |
| } |
| case LYNQ_WIFI_AUTH_WEP: |
| { |
| sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0); |
| sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0); |
| sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0); |
| |
| DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd); |
| break; |
| } |
| case LYNQ_WIFI_AUTH_WPA_PSK: |
| case LYNQ_WIFI_AUTH_WPA2_PSK: |
| { |
| if (auth == LYNQ_WIFI_AUTH_WPA_PSK) { |
| sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0); |
| sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); |
| } |
| else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) { |
| sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0); |
| sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); |
| } |
| // sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0); |
| // sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); |
| sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0); |
| |
| DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| DO_OK_FAIL_REQUEST(lynq_psk_cmd); |
| DO_OK_FAIL_REQUEST(lynq_pairwise_cmd); |
| break; |
| } |
| default: |
| { |
| printf("auth type [%d] not support now\n", auth); |
| return -1; |
| } |
| } |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| |
| return 0; |
| } |
| |
| int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth) |
| { |
| char lynq_auth_str[MAX_RET] = {0}; |
| char lynq_auth_alg_str[MAX_RET] = {0}; |
| char lynq_proto_str[MAX_RET] = {0}; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) { |
| return -1; |
| } |
| |
| if (memcmp( lynq_auth_str, "NONE", 4) == 0) { |
| if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) { |
| *auth = LYNQ_WIFI_AUTH_OPEN; |
| } |
| else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){ |
| *auth = LYNQ_WIFI_AUTH_WEP; |
| } |
| else { |
| *auth = LYNQ_WIFI_AUTH_OPEN; |
| } |
| } |
| else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) { |
| if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) { |
| *auth = -1; |
| } |
| else if (memcmp(lynq_proto_str, "RSN", 3) == 0) { |
| *auth = LYNQ_WIFI_AUTH_WPA2_PSK; |
| } |
| else { |
| *auth = LYNQ_WIFI_AUTH_WPA_PSK; |
| } |
| } |
| else { |
| *auth = -1; |
| } |
| |
| return 0; |
| } |
| |
| |
| int lynq_wifi_ap_start(lynq_wifi_index_e idx) |
| { |
| char LYNQ_WIFI_CMD[128]={0}; |
| //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all"; |
| //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf"; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| // system("connmanctl enable wifi"); |
| // system("connmanctl tether wifi on cy-test 12345678"); |
| // system("ifconfig wlan0 down"); |
| // system("ifconfig wlan0 up"); |
| // system("ifconfig wlan0 up"); |
| |
| //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd); |
| //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd); |
| |
| sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0); |
| DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD); |
| |
| return 0; |
| } |
| |
| int lynq_wifi_ap_restart(lynq_wifi_index_e idx) |
| { |
| return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1; |
| } |
| |
| int lynq_wifi_ap_stop(lynq_wifi_index_e idx) |
| { |
| char LYNQ_WIFI_CMD[128]={0}; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0); |
| |
| DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD); |
| |
| // system("connmanctl tether wifi off"); |
| |
| return 0; |
| } |
| |
| int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx) |
| { |
| char lynq_disable_cmd[128] = {0}; |
| char lynq_select_cmd[128] = {0}; |
| const char *lynq_hide_cmd = "SET HIDE_SSID 1"; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0); |
| sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0); |
| |
| DO_OK_FAIL_REQUEST(lynq_disable_cmd); |
| DO_OK_FAIL_REQUEST(lynq_hide_cmd); |
| DO_OK_FAIL_REQUEST(lynq_select_cmd); |
| |
| return 0; |
| } |
| |
| int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx) |
| { |
| char lynq_disable_cmd[128] = {0}; |
| char lynq_select_cmd[128] = {0}; |
| const char *lynq_unhide_cmd = "SET HIDE_SSID 0"; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0); |
| sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0); |
| |
| DO_OK_FAIL_REQUEST(lynq_disable_cmd); |
| DO_OK_FAIL_REQUEST(lynq_unhide_cmd); |
| DO_OK_FAIL_REQUEST(lynq_select_cmd); |
| |
| return 0; |
| } |
| |
| int lynq_ap_password_set(lynq_wifi_index_e idx,char *password) |
| { |
| int pass_len; |
| char lynq_tmp_cmd[MAX_CMD] = {0}; |
| char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0}; |
| if( password == NULL ){ |
| return -1; |
| } |
| pass_len=strlen(password); |
| lynq_wifi_auth_s auth = -1; |
| if(pass_len < 8 || pass_len >= 64){ |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| if (0 != lynq_wifi_ap_auth_get(idx, &auth)) { |
| return -1; |
| } |
| else if (auth == LYNQ_WIFI_AUTH_OPEN) { |
| return -1; |
| } |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| if (auth == LYNQ_WIFI_AUTH_WEP) { |
| sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password); |
| sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0); |
| DO_OK_FAIL_REQUEST(lynq_tmp_cmd); |
| DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd); |
| } |
| else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) { |
| sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password); |
| DO_OK_FAIL_REQUEST(lynq_tmp_cmd); |
| } |
| else { |
| return -1; |
| } |
| |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| |
| return 0; |
| } |
| |
| int lynq_ap_password_get(lynq_wifi_index_e idx, char *password) |
| { |
| FILE * fp; |
| int len, ret; |
| int count, index; |
| char *split_lines[128] = {0}; |
| char *buff, *p; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb"); |
| // fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb"); |
| if (NULL == fp) { |
| printf("open file fail\n"); |
| return -1; |
| } |
| |
| buff = alloca(MAX_RET); |
| fseek(fp, 0, SEEK_SET); |
| len = fread(buff, 1, MAX_RET, fp); |
| fclose(fp); |
| |
| for(index=0; index < len; index ++) { |
| if (memcmp(buff + index, "network={", 9) != 0) { |
| continue; |
| } |
| p = buff + index + 9; |
| for (; index < len; index ++ ) { |
| if (buff[index] != '}') { |
| continue; |
| } |
| buff[index] = '\0'; |
| break; |
| } |
| len = buff + index - p; |
| } |
| |
| count = lynq_split(p, len, '\n', split_lines); |
| |
| ret = -1; |
| for(index=0; index < count; index++) { |
| p = strstr(split_lines[index], "psk="); |
| if (p != NULL) { |
| p += 4; |
| if (*p == '\"') { |
| p++; |
| } |
| } |
| else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) { |
| p += 9; |
| if (*p == '\"') { |
| p++; |
| } |
| } |
| else { |
| continue; |
| } |
| |
| strcpy(password, p); |
| |
| while(*password != '\0') { |
| if (*password == '\"') { |
| *password = '\0'; |
| break; |
| } |
| password++; |
| } |
| ret = 0; |
| break; |
| } //end for(index=0; index < count; index++) |
| |
| return ret; |
| } |
| |
| static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) { |
| char lynq_auth_str[MAX_RET] = {0}; |
| char lynq_proto_str[MAX_RET] = {0}; |
| |
| if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) { |
| return -1; |
| } |
| |
| *auth = convert_auth_from_key_mgmt(lynq_auth_str); |
| |
| if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) { |
| if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) { |
| if (strcmp(lynq_proto_str, "RSN") == 0) { |
| *auth = LYNQ_WIFI_AUTH_WPA2_PSK; |
| } |
| } |
| } |
| return 0; |
| } |
| |
| int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password) |
| { |
| int pass_len, net_no, count, index; |
| char lynq_tmp_cmd[300]={0}; |
| int net_no_list[128]; |
| lynq_wifi_auth_s net_auth; |
| pass_len=strlen(password); |
| if(pass_len < 8 || pass_len >= 64){ |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| net_no = -1; |
| count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid); |
| |
| for (index=0; index < count; index++) { |
| net_auth = -1; |
| if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) { |
| net_no = net_no_list[index]; |
| break; |
| } |
| } |
| |
| if (net_no < 0) { |
| return -1; |
| } |
| |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password); |
| |
| DO_OK_FAIL_REQUEST(lynq_tmp_cmd); |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| |
| return 0; |
| } |
| |
| int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo |
| |
| FILE * fp; |
| int len, ret, network_len, i, ssid_len; |
| int count, index; |
| char *split_lines[128] = {0}; |
| char *buff, *p, *ssid, *ssid_end_flag; |
| char tmp_ssid[128]={0}; |
| |
| network_len = 0; |
| p = NULL; |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| if (NULL == password) { |
| printf("bad param\n"); |
| return -1; |
| } |
| |
| fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb"); |
| if (NULL == fp) { |
| printf("open file fail\n"); |
| return -1; |
| } |
| |
| buff = alloca(MAX_RET); |
| fseek(fp, 0, SEEK_SET); |
| len = fread(buff, 1, MAX_RET, fp); |
| fclose(fp); |
| |
| for(index=0; index < len; index ++) { |
| for(; index < len; index ++) { |
| if (memcmp(buff + index, "network={", 9) != 0) { |
| continue; |
| } |
| p = buff + index + 9; |
| for (; index < len; index ++ ) { |
| if (buff[index] != '}') { |
| continue; |
| } |
| buff[index] = '\0'; |
| break; |
| } |
| network_len = buff + index - p; |
| break; |
| } |
| |
| ssid = strstr(p, "ssid="); |
| if (ssid != NULL) { |
| ssid += strlen("ssid="); |
| if (ssid[0] == '\"') { |
| if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"') |
| break; |
| } |
| else{ |
| ssid_end_flag = strstr(ssid, "\n"); |
| if (ssid_end_flag != NULL) |
| { |
| ssid_len = (ssid_end_flag - ssid) / 2; |
| for(i=0; i<ssid_len; i++) |
| { |
| tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]); |
| } |
| if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0) |
| break; |
| } |
| } |
| } |
| |
| } |
| |
| if (index >= len || NULL == p || network_len <= 0) { |
| return -1; |
| } |
| |
| count = lynq_split(p, network_len, '\n', split_lines); |
| |
| ret = -1; |
| for(index=0; index < count; index++) { |
| p = strstr(split_lines[index], "psk="); |
| if (p != NULL) { |
| p += 4; |
| if (*p == '\"') { |
| p++; |
| } |
| } |
| else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) { |
| p += 9; |
| if (*p == '\"') { |
| p++; |
| } |
| } |
| else { |
| continue; |
| } |
| |
| strcpy(password, p); |
| |
| while(*password != '\0') { |
| if (*password == '\"') { |
| *password = '\0'; |
| break; |
| } |
| password++; |
| } |
| ret = 0; |
| break; |
| } //end for(index=0; index < count; index++) |
| |
| return ret; |
| } |
| |
| static int inner_set_sta_ssid(int net_no, char *sta_ssid) |
| { |
| char lynq_wifi_ssid_cmd[80]={0}; |
| |
| if (sta_ssid == NULL) { |
| printf("sta_ssid is null\n"); |
| return -1; |
| } |
| |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid); |
| |
| DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd); |
| // DO_OK_FAIL_REQUEST(cmd_save_config); |
| |
| return 0; |
| |
| } |
| |
| static int inner_sta_start_stop(int net_no, int start_flag, int save) |
| { |
| char lynq_disable_cmd[128]={0}; |
| char lynq_select_cmd[128]={0}; |
| |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| if (save != 0) { |
| if (start_flag != 0) |
| { |
| sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no); |
| DO_OK_FAIL_REQUEST(lynq_select_cmd); |
| } |
| else |
| { |
| sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no); |
| DO_OK_FAIL_REQUEST(lynq_select_cmd); |
| } |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| } |
| |
| if (start_flag == 0) { |
| sprintf(lynq_disable_cmd,"DISCONNECT"); |
| DO_OK_FAIL_REQUEST(lynq_disable_cmd); |
| } |
| else { |
| sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no); |
| DO_OK_FAIL_REQUEST(lynq_select_cmd); |
| } |
| |
| return 0; |
| } |
| |
| int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid) |
| { |
| CHECK_IDX(idx, CTRL_STA); |
| |
| curr_status_info curr_state; |
| ap_info_s ap_info; |
| curr_state.ap = &ap_info; |
| curr_state.state = NULL; |
| |
| if (0 == inner_get_status_info(CTRL_STA, &curr_state)) { |
| strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid)); |
| return 0; |
| } |
| |
| return -1; |
| } |
| |
| int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info) |
| { |
| scan_info_s *scan_list = NULL; |
| saved_ap_info_s *save_list = NULL; |
| int scan_len=0; |
| int save_len=0; |
| int best_index = -1; |
| int best_scan_index = -1; |
| int best_rssi = 0; |
| int i, j, ret; |
| |
| ret = -1; |
| |
| CHECK_IDX(idx, CTRL_STA); |
| if (info == NULL) { |
| return -1; |
| } |
| |
| curr_status_info curr_state; |
| ap_info_s ap_info; |
| char status[64]; |
| |
| memset(&ap_info, 0, sizeof (ap_info)); |
| memset(status, 0, sizeof (status)); |
| |
| curr_state.ap = &ap_info; |
| curr_state.state = status; |
| |
| if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) { |
| memcpy(&info->base_info, &ap_info, sizeof (ap_info_s)); |
| if (strcmp(status, STATE_COMPLETED) == 0) |
| { |
| info->status = LYNQ_WIFI_AP_STATUS_ENABLE; |
| } |
| else |
| { |
| info->status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| } |
| lynq_get_connect_ap_rssi(idx, &info->rssi); |
| lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw); |
| return 0; |
| } |
| |
| lynq_wifi_sta_start_scan(idx); |
| |
| if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) { |
| if (NULL != scan_list) |
| { |
| free(scan_list); |
| } |
| return -1; |
| } |
| |
| if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) { |
| if (NULL != scan_list) |
| { |
| free(scan_list); |
| } |
| if (NULL != save_list) |
| { |
| free(save_list); |
| } |
| return -1; |
| } |
| |
| for (i=0; i < save_len; i++) { |
| for (j=0; j < scan_len; j++) { |
| if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished |
| && save_list[i].base_info.auth == scan_list[j].auth) { |
| if (best_rssi == 0) { |
| best_index = i; |
| best_rssi = scan_list[j].rssi; |
| } |
| else if (best_rssi > scan_list[j].rssi) { |
| best_index = i; |
| best_scan_index = j; |
| best_rssi = scan_list[j].rssi; |
| } |
| strncpy(save_list[i].base_info.ap_mac, scan_list[j].mac, sizeof (save_list[i].base_info.ap_mac)); |
| break; |
| } |
| } |
| } |
| |
| if (best_index >= 0) { |
| memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s)); |
| inner_get_ip_by_mac( info->base_info.ap_mac, info->base_info.ap_ip, sizeof (info->base_info.ap_ip)); |
| info->status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| info->rssi = best_rssi; |
| ret = 0; |
| } |
| |
| if (NULL != scan_list) |
| { |
| free(scan_list); |
| } |
| if (NULL != save_list) |
| { |
| free(save_list); |
| } |
| |
| return ret; |
| } |
| |
| static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password) |
| { |
| char lynq_auth_cmd[64]={0}; |
| char lynq_ket_mgmt_cmd[64]={0}; |
| char lynq_pairwise_cmd[64]={0}; |
| char lynq_psk_cmd[64]={0}; |
| |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| switch(auth){ |
| case LYNQ_WIFI_AUTH_OPEN: |
| { |
| sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no); |
| |
| DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| // DO_OK_FAIL_REQUEST(cmd_save_config); |
| break; |
| } |
| case LYNQ_WIFI_AUTH_WPA_PSK: |
| case LYNQ_WIFI_AUTH_WPA2_PSK: |
| { |
| if (auth == LYNQ_WIFI_AUTH_WPA_PSK) { |
| sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no); |
| } |
| else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) { |
| sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no); |
| } |
| sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no); |
| sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no); |
| |
| DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd); |
| DO_OK_FAIL_REQUEST(lynq_pairwise_cmd); |
| |
| if (password != NULL) { |
| sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password); |
| DO_OK_FAIL_REQUEST(lynq_psk_cmd); |
| } |
| |
| // DO_OK_FAIL_REQUEST(cmd_save_config); |
| break; |
| } |
| default: |
| return -1; |
| } |
| |
| return 0; |
| } |
| |
| static int inner_get_curr_net_no(int interface) { |
| curr_status_info curr_state; |
| curr_state.ap = NULL; |
| curr_state.state = NULL; |
| |
| if (0 != inner_get_status_info(interface, &curr_state)) { |
| return -1; |
| } |
| |
| return curr_state.net_no; |
| } |
| |
| int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth) |
| { |
| int net_no; |
| CHECK_IDX(idx, CTRL_STA); |
| |
| net_no = inner_get_curr_net_no(CTRL_STA); |
| |
| if (net_no < 0) { |
| return -1; |
| } |
| |
| return inner_get_network_auth(CTRL_STA, net_no, auth); |
| } |
| |
| int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw) |
| { |
| int count, net_no, index; |
| int net_no_list[128]; |
| lynq_wifi_auth_s net_auth; |
| |
| if (ssid == NULL || *ssid == '\0') { |
| printf("bad ssid\n"); |
| return -1; |
| } |
| |
| if (LYNQ_WIFI_AUTH_OPEN != auth) { |
| if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) { |
| printf("bad password\n"); |
| return -1; |
| } |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| net_no = -1; |
| count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid); |
| |
| for (index=0; index < count; index++) { |
| net_auth = -1; |
| if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) { |
| net_no = net_no_list[index]; |
| break; |
| } |
| } |
| |
| if (net_no < 0) { |
| net_no = lynq_add_network(CTRL_STA); |
| if (net_no == -1) { |
| return -1; |
| } |
| |
| printf("net no is %d\n", net_no); |
| if (0 != inner_set_sta_ssid(net_no, ssid)) { |
| return -1; |
| } |
| } |
| |
| if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) { |
| return -1; |
| } |
| |
| return inner_sta_start_stop(net_no, 1, 1); |
| } |
| |
| int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid) |
| { |
| ap_info_s ap; |
| curr_status_info curr_state; |
| ap.ap_ssid[0] = '\0'; |
| |
| if (ssid == NULL || *ssid == '\0') { |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| curr_state.ap = ≈ |
| curr_state.state = NULL; |
| |
| if (inner_get_status_info(CTRL_STA, &curr_state) != 0) { |
| return 0; |
| } |
| |
| if (strcmp(ap.ap_ssid, ssid) != 0) { |
| return 0; |
| } |
| |
| return inner_sta_start_stop(curr_state.net_no, 0, 0); |
| } |
| |
| int lynq_wifi_sta_start(lynq_wifi_index_e idx) |
| { |
| // const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf"; |
| // const char *lynq_reconnect_cmd = "RECONNECT"; |
| const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all"; |
| const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect"; |
| // const char *lynq_first_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 remove_net all"; |
| |
| CHECK_IDX(idx, CTRL_STA); |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| system("connmanctl enable wifi"); |
| |
| if (system("ifconfig | grep -q wlan0") != 0) |
| { |
| return -1; |
| } |
| |
| // DO_OK_FAIL_REQUEST(cmd_remove_all); |
| // system(lynq_first_sta_cmd); |
| // system(lynq_reconfigure_cmd); |
| // DO_OK_FAIL_REQUEST(lynq_reconnect_cmd); |
| system(lynq_enable_sta_cmd); |
| system(lynq_reconnect_cmd); |
| // DO_OK_FAIL_REQUEST(lynq_reconnect_cmd); |
| return 0; |
| } |
| |
| int lynq_wifi_sta_stop(lynq_wifi_index_e idx) |
| { |
| // char lynq_disable_network_cmd[MAX_CMD]; |
| // curr_status_info curr_state; |
| // ap_info_s ap_info; |
| |
| const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all"; |
| CHECK_IDX(idx, CTRL_STA); |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| // curr_state.ap = &ap_info; |
| // curr_state.state = NULL; |
| |
| // if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) { |
| // return 0; |
| // } |
| |
| // sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no); |
| // DO_OK_FAIL_REQUEST(lynq_disable_network_cmd); |
| system(lynq_disable_sta_cmd); |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| |
| return 0; |
| // return system("connmanctl disable wifi"); |
| } |
| |
| //static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) { |
| // int i, count; |
| // char *p; |
| // const char * FLAG_SSID = "ssid="; |
| // const char * FLAG_SBSID = "bssid="; |
| // const char * FLAG_KEY_MGMT = "key_mgmt="; |
| // const char * FLAG_FREQ = "freq="; |
| // char lynq_sta_cmd[MAX_CMD]; |
| // char *split_lines[128] = {0}; |
| |
| // CHECK_WPA_CTRL(CTRL_AP); |
| |
| // sprintf(lynq_sta_cmd, "STA %s", bssid); |
| |
| // DO_REQUEST(lynq_sta_cmd); |
| |
| // count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| |
| // for(i=0; i < count; i++) { |
| // p = strstr(split_lines[i], FLAG_SSID); |
| // if (p != NULL) { |
| // strcpy(ap->ap_ssid, p + strlen(FLAG_SSID)); |
| // continue; |
| // } |
| // } |
| |
| // lynq_get_interface_ip(idx, ap->ap_ip); |
| // lynq_ap_password_set(idx, ap->psw); |
| |
| // return 0; |
| //} |
| |
| static int inner_get_status_info_ap (int interface, ap_info_s *ap) { |
| curr_status_info curr_state; |
| curr_state.ap = ap; |
| curr_state.state = NULL; |
| return inner_get_status_info(interface, &curr_state); |
| } |
| |
| int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len) |
| { |
| int index, line_count; |
| device_info_s *dev_info; |
| const char *lynq_first_sta_cmd = "STA-FIRST"; |
| char lynq_next_sta_cmd[MAX_CMD]; |
| char *bssid[1024] = {0}; |
| char *split_lines[128] = {0}; |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| // ap_info_s * tmp_ap; |
| // device_info_s * tmp_list; |
| if (ap == NULL || list == NULL || len == NULL) { |
| printf("bad input param"); |
| return -1; |
| } |
| |
| // ap = &tmp_ap; |
| // list = &tmp_list; |
| *ap = malloc(sizeof (ap_info_s)); |
| |
| if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) { |
| return -1; |
| } |
| |
| lynq_get_interface_ip(idx, (*ap)->ap_ip); |
| lynq_ap_password_get(idx, (*ap)->psw); |
| |
| DO_REQUEST(lynq_first_sta_cmd); |
| |
| index = 0; |
| while (reply_len > 0) { |
| if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| break; |
| } |
| line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| bssid[index] = malloc(strlen(split_lines[0]) + 1); |
| strcpy(bssid[index], split_lines[0]); |
| index++; |
| sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]); |
| reply_len = MAX_RET; |
| cmd_reply[0] = '\0'; |
| ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL); |
| if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) { |
| printf("run %s fail \n", lynq_next_sta_cmd); |
| break; |
| } |
| } |
| |
| *len = index; |
| |
| *list = malloc(sizeof(device_info_s) * (*len)); |
| for (index=0; index < *len; index++) { |
| dev_info = &(*list)[index]; |
| memset(dev_info, 0, sizeof(device_info_s)); |
| strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac)); |
| inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip)); |
| inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname); |
| dev_info->status = LYNQ_WIFI_STATUS_CONNECT; |
| free(bssid[index]); |
| } |
| |
| return 0; |
| } |
| |
| int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len) |
| { |
| int i, count, index, count_words; |
| const char *lynq_scan_result_cmd = "SCAN_RESULTS"; |
| char *split_lines[128] = {0}; |
| char *split_words[128] = {0}; |
| scan_info_s * p; |
| |
| if (list == NULL || len == NULL) { |
| return -1; |
| } |
| |
| for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++) |
| { |
| usleep(100 * 1000); |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| DO_REQUEST(lynq_scan_result_cmd); |
| |
| count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| *len = count - 1; |
| *list = malloc(sizeof (scan_info_s) * *len); |
| |
| count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header |
| for (index=0; index <count_words; index++) { |
| printf("----header: %s\n", split_words[index]); |
| } |
| |
| for(index = 1;index < count; index++) { |
| printf("---- %s\n",split_lines[index]); |
| count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words); |
| if (count_words < 4) |
| continue; |
| printf("count: %d, %s\n", count_words, split_words[0]); |
| //bssid / frequency / signal level / flags / ssid |
| p = (*list) + index - 1; |
| strcpy(p->mac, split_words[0]); |
| p->band = convert_band_from_freq(atoi(split_words[1])); |
| p->rssi = -1 * atoi( split_words[2]); |
| p->auth = convert_max_auth_from_flag(split_words[3]); |
| inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid)); |
| } |
| |
| return 0; |
| } |
| |
| int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth) |
| { |
| int count, net_no, index; |
| int net_no_list[128]; |
| lynq_wifi_auth_s net_auth; |
| char lynq_remove_cmd[MAX_CMD]; |
| |
| if (ssid == NULL || *ssid == '\0') { |
| printf("bad ssid\n"); |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| net_no = -1; |
| count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid); |
| |
| for (index=0; index < count; index++) { |
| net_auth = -1; |
| if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) { |
| net_no = net_no_list[index]; |
| break; |
| } |
| } |
| |
| if (net_no < 0) { |
| return 0; |
| } |
| |
| sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no); |
| |
| DO_OK_FAIL_REQUEST(lynq_remove_cmd); |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| |
| return 0; |
| } |
| |
| int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len) |
| { |
| int count, index; |
| int net_no_list[128]; |
| char freq[16]; |
| |
| if (list == NULL || len == NULL) { |
| printf("bad param\n"); |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| // CHECK_WPA_CTRL(CTRL_STA); |
| |
| count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL); |
| printf("count is %d\n", count); |
| |
| *list = malloc(sizeof (saved_ap_info_s) * count); |
| memset(*list, 0, sizeof (saved_ap_info_s) * count); |
| *len = count; |
| |
| for (index=0; index < count; index++) { |
| printf("to get ssid %d\n", index); |
| inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid); |
| inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac); |
| inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth); |
| if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) { |
| (*list)[index].base_info.band = convert_band_from_freq(atoi(freq)); |
| } |
| else { |
| (*list)[index].base_info.band = -1; |
| } |
| |
| lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw); |
| } |
| |
| return 0; |
| } |
| |
| int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx) |
| { |
| const char *lynq_scan_cmd = "SCAN"; |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| CHECK_WPA_CTRL(CTRL_STA); |
| |
| DO_OK_FAIL_REQUEST(lynq_scan_cmd); |
| g_sta_scan_finish_flag = 0; |
| |
| return 0; |
| } |
| |
| int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) { |
| if (cb == NULL) { |
| return -1; |
| } |
| |
| g_ap_callback_priv = priv; |
| g_ap_callback_func = cb; |
| |
| return 0; |
| } |
| |
| int lynq_unreg_ap_event_callback(void * priv) { |
| if (g_ap_callback_priv == priv) { |
| g_ap_callback_func = NULL; |
| g_ap_callback_priv = NULL; |
| return 0; |
| } |
| return -1; |
| } |
| |
| int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){ |
| if (cb == NULL) { |
| return -1; |
| } |
| |
| g_sta_callback_priv = priv; |
| g_sta_callback_func = cb; |
| |
| return 0; |
| } |
| |
| int lynq_unreg_sta_event_callback(void * priv) { |
| if (g_sta_callback_priv == priv) { |
| g_sta_callback_func = NULL; |
| g_sta_callback_priv = NULL; |
| return 0; |
| } |
| return -1; |
| } |
| |
| |
| static int inner_get_status_info_state (int interface, char *state) { |
| curr_status_info curr_state; |
| curr_state.ap = NULL; |
| curr_state.state = state; |
| return inner_get_status_info(interface, &curr_state); |
| } |
| |
| int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status) |
| { |
| char state[MAX_CMD]; |
| CHECK_IDX(idx, CTRL_AP); |
| |
| if (inner_get_status_info_state(CTRL_AP, state) != 0) { |
| *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| return 0; |
| } |
| |
| if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) { |
| *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE; |
| } |
| else { |
| *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| } |
| |
| return 0; |
| } |
| |
| int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) { |
| char state[MAX_CMD]; |
| CHECK_IDX(idx, CTRL_STA); |
| |
| if (inner_get_status_info_state(CTRL_STA, state) != 0) { |
| *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE; |
| return 0; |
| } |
| |
| if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) { |
| *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE; |
| } |
| else { |
| *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE; |
| } |
| |
| return 0; |
| } |
| |
| int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) { |
| // CHECK_IDX(idx, CTRL_AP); |
| // int ret = 0; |
| // size_t reply_len = MAX_RET; |
| // char cmd_reply[MAX_RET]={0}; |
| // const char * cmd_str = "GET country"; |
| // struct wpa_ctrl *s_lynq_wpa_ctrl = NULL; |
| // do{ |
| // if (NULL == s_lynq_wpa_ctrl) { |
| // s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd"); |
| // if (NULL == s_lynq_wpa_ctrl ) { |
| // printf("wpa_ctrl_open fail\n"); |
| // return -1; |
| // } |
| // } |
| // }while(0); |
| |
| // do { |
| // reply_len = MAX_RET; |
| // cmd_reply[0] = '\0'; |
| // printf("to call [%s]\n", cmd_str); |
| // ret = local_wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); |
| // if (ret != 0) { |
| // printf("call ##cmd_str fail %d\n", ret); |
| // return ret; |
| // } |
| // cmd_reply[reply_len+1] = '\0'; |
| // printf("cmd replay [ %s ]\n", cmd_reply); |
| // }while(0); |
| |
| FILE *fp; |
| size_t i = 0; |
| char lynq_cmd_ret[MAX_RET]={0}; |
| |
| // CHECK_IDX(idx, CTRL_AP); |
| |
| if((fp=popen("wl country","r"))==NULL) |
| { |
| perror("popen error!"); |
| return -1; |
| } |
| if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0) |
| { |
| perror("fread fail!"); |
| return -1; |
| } |
| |
| for(i=0; i < strlen(lynq_cmd_ret); i++) { |
| if (lynq_cmd_ret[i] == ' ') { |
| lynq_cmd_ret[i] = '\0'; |
| break; |
| } |
| } |
| |
| strcpy(country_code,lynq_cmd_ret); |
| printf("---country code %s\n", country_code); |
| |
| int ret=pclose(fp); |
| if(ret==-1) |
| { |
| perror("close file faild"); |
| } |
| |
| return 0; |
| } |
| |
| int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) { |
| // const char * cmd_str = "GET country"; |
| // CHECK_IDX(idx, CTRL_AP); |
| // CHECK_WPA_CTRL(CTRL_STA); |
| |
| // DO_REQUEST(cmd_str); |
| // printf("result %s\n", cmd_reply); |
| |
| if (country_code == NULL || *country_code == '\0') { |
| printf("bad country code\n"); |
| return -1; |
| } |
| |
| char lynq_country_cmd[MAX_CMD]; |
| sprintf(lynq_country_cmd, "wl country %s", country_code); |
| if (system(lynq_country_cmd) == 0) { |
| return 0; |
| } |
| |
| return -1; |
| } |
| |
| int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac) |
| { |
| if (mac == NULL) { |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| ap_info_s ap; |
| ap.ap_mac[0] = '\0'; |
| |
| if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) { |
| return -1; |
| } |
| strcpy(mac, ap.ap_mac); |
| |
| return 0; |
| } |
| |
| int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip) |
| { |
| struct ifaddrs *ifaddr_header, *ifaddr; |
| struct in_addr * ifa; |
| const char * ifaName = "wlan0"; |
| if (ip == NULL) |
| { |
| return -1; |
| } |
| |
| if (idx == 1) { |
| ifaName = "tether"; |
| } |
| else if (idx != 0) { |
| return -1; |
| } |
| |
| if (getifaddrs(&ifaddr_header) == -1) |
| { |
| perror("getifaddrs"); |
| return -1; |
| //exit(EXIT_FAILURE); |
| } |
| |
| |
| for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next) |
| { |
| if (ifaddr->ifa_addr == NULL) |
| continue; |
| if((strcmp(ifaddr->ifa_name,ifaName)==0)) |
| { |
| if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4 |
| { |
| // is a valid IP4 Address |
| ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr; |
| inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN); |
| printf("%s IP Address %s/n", ifaddr->ifa_name, ip); |
| freeifaddrs(ifaddr_header); |
| printf("ip %s\n", ip); |
| return 0; |
| } |
| } |
| } |
| freeifaddrs(ifaddr_header); |
| return -1; |
| } |
| |
| int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac) |
| { |
| int count; |
| size_t i; |
| char *split_words[128] = {0}; |
| const char *lynq_get_mac_cmd = "DRIVER MACADDR"; |
| |
| CHECK_WPA_CTRL(idx); |
| |
| DO_REQUEST(lynq_get_mac_cmd); |
| |
| if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| return -1; |
| } |
| |
| count = lynq_split(cmd_reply, reply_len, '=', split_words); |
| |
| if (count < 2) { |
| return -1; |
| } |
| |
| for (i=0; i < strlen(split_words[1]); i++ ) { |
| if (split_words[1][i] != ' ') { |
| break; |
| } |
| } |
| |
| strcpy(mac, split_words[1] + i); |
| |
| return 0; |
| } |
| |
| int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi) |
| { |
| // int count; |
| // char *split_words[128] = {0}; |
| // const char *lynq_get_rssi_cmd = "DRIVER RSSI"; |
| |
| // if (rssi == NULL) { |
| // return -1; |
| // } |
| |
| // CHECK_IDX(idx, CTRL_STA); |
| |
| // CHECK_WPA_CTRL(CTRL_STA); |
| |
| // DO_REQUEST(lynq_get_rssi_cmd); |
| |
| // if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| // return -1; |
| // } |
| |
| // count = lynq_split(cmd_reply, reply_len, ' ', split_words); |
| |
| // if (count < 2) { |
| // return -1; |
| // } |
| |
| // *rssi = atoi(split_words[1]) * -1; |
| |
| FILE *fp; |
| size_t i = 0; |
| char lynq_cmd_ret[MAX_RET]={0}; |
| |
| // CHECK_IDX(idx, CTRL_AP); |
| /*******change other cmd to get rssi******* |
| * |
| *wl rssi ---> wl -i wlan0 rssi |
| * |
| ***** change by qs.xiong 20221011*******/ |
| if((fp=popen("wl -i wlan0 rssi","r"))==NULL) |
| { |
| perror("popen error!"); |
| return -1; |
| } |
| if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0) |
| { |
| perror("fread fail!"); |
| return -1; |
| } |
| *rssi = atoi(lynq_cmd_ret) * -1; |
| /****** if got rssi is 0,means sta didn't connected any device****/ |
| if(*rssi == 0) |
| { |
| printf("sta didn't connected any ap device,please check connection\n"); |
| } |
| |
| return 0; |
| } |
| |
| int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band) |
| { |
| if (band == NULL) { |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| ap_info_s ap; |
| ap.band = -1; |
| |
| if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) { |
| return -1; |
| } |
| *band = ap.band; |
| |
| return 0; |
| } |
| |
| int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip) |
| { |
| char bssid[1024] = {0}; |
| |
| if (ip == NULL) |
| { |
| printf("invalid param"); |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_STA); |
| |
| if (lynq_get_connect_ap_mac(idx, bssid) != 0) |
| { |
| return -1; |
| } |
| |
| return inner_get_ip_by_mac(bssid, ip, 32); //better input by user |
| } |
| |
| int lynq_ap_connect_num(int sta_number) |
| { |
| char lynq_limit_cmd[32]={0}; |
| int ret; |
| if((sta_number < 1 ) && (sta_number > 15)){ |
| printf("sta_number: not in range\n",sta_number); |
| return -1; |
| } |
| sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number); |
| ret = system(lynq_limit_cmd); |
| if(ret != 0){ |
| printf("cmd of limit ap devices number error\n"); |
| } |
| return 0; |
| } |
| |
| int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode) |
| { |
| |
| char lynq_wifi_acs_cmd[128]={0}; |
| char lynq_cmd_mode[128]={0}; |
| char lynq_cmd_slect[128]={0}; |
| |
| if((acs_mode != 2) && (acs_mode != 5)){ |
| PRINT_AND_RETURN_VALUE("set acs_mode is error",-1); |
| } |
| |
| if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { |
| return -1; |
| } |
| |
| CHECK_IDX(idx, CTRL_AP); |
| |
| CHECK_WPA_CTRL(CTRL_AP); |
| |
| sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode); |
| sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0); |
| sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0); |
| |
| DO_OK_FAIL_REQUEST(cmd_disconnect); |
| DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd); |
| DO_OK_FAIL_REQUEST(lynq_cmd_mode); |
| DO_OK_FAIL_REQUEST(cmd_save_config); |
| DO_OK_FAIL_REQUEST(lynq_cmd_slect); |
| |
| return 0; |
| } |
| //you.chen add for tv-box start |
| static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) { |
| FILE *fp; |
| //printf("to exec cmd:%s\n", str_cmd); |
| if((fp=popen(str_cmd,"r"))==NULL) |
| { |
| perror("popen error!"); |
| return -1; |
| } |
| if((fread(str_cmd_ret,max_len,1,fp))<0) |
| { |
| perror("fread fail!"); |
| fclose(fp); |
| return -1; |
| } |
| fclose(fp); |
| return 0; |
| } |
| |
| static int get_netmask_length(const char* mask) |
| { |
| int masklen=0, i=0; |
| int netmask=0; |
| |
| if(mask == NULL) |
| { |
| return 0; |
| } |
| |
| struct in_addr ip_addr; |
| if( inet_aton(mask, &ip_addr) ) |
| { |
| netmask = ntohl(ip_addr.s_addr); |
| }else{ |
| netmask = 0; |
| return 0; |
| } |
| |
| while(0 == (netmask & 0x01) && i<32) |
| { |
| i++; |
| netmask = netmask>>1; |
| } |
| masklen = 32-i; |
| return masklen; |
| } |
| |
| static int get_tether_route_str(char *str_cmd_ret, size_t max_len) { |
| int mask_len; |
| char *p; |
| char tmp[64] = {0}; |
| if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0) |
| return -1; |
| p = strstr(str_cmd_ret, "Mask:"); |
| if (p == NULL) |
| return -1; |
| mask_len = get_netmask_length(p + 5); |
| if (mask_len == 0) |
| return -1; |
| p = strstr(str_cmd_ret, "inet addr:"); |
| if (p == NULL) |
| return -1; |
| strcpy(tmp, p + 10); |
| p = strstr(tmp, " "); |
| if (p != NULL) |
| *p = '\0'; |
| sprintf(str_cmd_ret, "%s/%d", tmp, mask_len); |
| return 0; |
| } |
| |
| static void GBWWatchThreadProc() { |
| int i,n, nloop, nmax, ncheckcount, nidlecount; |
| unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes; |
| unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop; |
| unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed; |
| char *results[16] = {0}; |
| char str_cmd[256] = {0}; |
| char str_cmd_ret[128] = {0}; |
| char dest_ip[32] = {0}; |
| lastAP1Bytes = lastAP2Bytes = 0; |
| lastAP1Drop = lastAP2Drop = 0; |
| lastAP1Speed = lastAP2Speed = 0; |
| setAP1Speed = 50; |
| setAP2Speed = 80; |
| nloop = 0; |
| nmax = 6; |
| ncheckcount = nidlecount = 0; |
| |
| printf("------gbw thread run\n"); |
| sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac); |
| while (dest_ip[0] == '\0') { |
| sleep(1); |
| str_cmd_ret[0] = '\0'; |
| exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)); |
| for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) { |
| if (str_cmd_ret[n] == '\n'){ |
| str_cmd_ret[n] = '\0'; |
| break; |
| } |
| } |
| if (str_cmd_ret[0] != '\0') |
| { |
| strcpy(dest_ip, str_cmd_ret); |
| } |
| } |
| |
| system("tc qdisc del dev tether root > /dev/null 2>&1"); |
| system("tc qdisc add dev tether root handle 1: htb r2q 1"); |
| system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000"); |
| if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0) |
| { |
| printf("not get tether info\n"); |
| return; |
| } |
| sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", str_cmd_ret); |
| system(str_cmd); |
| system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000"); |
| sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip); |
| //printf("----cmd:%s\n", str_cmd); |
| system(str_cmd); |
| |
| while (1) { |
| sleep(1); |
| memset(str_cmd, 0, sizeof(str_cmd)); |
| if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd))) |
| continue; |
| //printf("ap1 --- %s\n", str_cmd); |
| n = lynq_split(str_cmd, strlen(str_cmd), ' ', results); |
| if (n > 9) { |
| if (strcmp(results[1], "Sent") == 0) { |
| currAP1Bytes = atoll(results[2]); |
| } |
| if (strcmp(results[6], "(dropped") == 0) { |
| currAP1Drop = atoi(results[7]); |
| } |
| } |
| |
| memset(str_cmd, 0, sizeof(str_cmd)); |
| if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd))) |
| continue; |
| //printf("ap2 --- %s\n", str_cmd); |
| n = lynq_split(str_cmd, strlen(str_cmd), ' ', results); |
| if (n > 9) { |
| if (strcmp(results[1], "Sent") == 0) { |
| currAP2Bytes = atoll(results[2]); |
| } |
| if (strcmp(results[6], "(dropped") == 0) { |
| currAP2Drop = atoi(results[7]); |
| } |
| } |
| |
| //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop); |
| if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) { |
| lastAP1Bytes = currAP1Bytes; |
| lastAP2Bytes = currAP2Bytes; |
| continue; |
| } |
| |
| currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024; |
| currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024; |
| //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed); |
| lastAP1Speed = currAP1Speed; |
| lastAP2Speed = currAP2Speed; |
| lastAP1Bytes = currAP1Bytes; |
| lastAP2Bytes = currAP2Bytes; |
| |
| currSetAP1Speed = setAP1Speed; |
| if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) { |
| ncheckcount++; |
| if (ncheckcount > 3) { |
| ncheckcount = 0; |
| currSetAP1Speed = 5; |
| } |
| } |
| else { |
| ncheckcount = 0; |
| if (currAP1Speed < 5) |
| nidlecount++; |
| else |
| nidlecount = 0; |
| |
| } |
| |
| if (nidlecount > 60 ){ |
| currSetAP1Speed = 50; |
| } |
| |
| if (currSetAP1Speed != setAP1Speed) { |
| setAP1Speed = currSetAP1Speed; |
| sprintf(str_cmd, "tc class replace dev tether parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000", setAP1Speed, (int)(setAP1Speed*1.4)); |
| //printf("------***change speed: %s\n", str_cmd); |
| system(str_cmd); |
| } |
| } |
| } |
| |
| int enableGBW(const char* mac) { |
| int i,len; |
| char get_ipaddr_cmd[128]={0}; |
| ap_info_s *ap; |
| device_info_s * list; |
| |
| if (mac == NULL || g_gbw_enabled == 1) |
| return -1; |
| len = strlen(mac); |
| g_gbw_mac = malloc(len + 1); |
| for(i=0;i<len;i++) { |
| if (mac[i] >= 'A' && mac[i] <= 'Z') |
| { |
| g_gbw_mac[i] = 'a' + (mac[i] - 'A'); |
| } |
| else |
| g_gbw_mac[i] = mac[i]; |
| } |
| g_gbw_mac[i] = '\0'; |
| g_gbw_enabled = 1; |
| |
| sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac); |
| if (system(get_ipaddr_cmd) == 0) { |
| //startGBW(); |
| if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) { |
| for (i=0;i<len;i++) { |
| //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname); |
| if (strcmp(g_gbw_mac, list[i].sta_mac) == 0) |
| startGBW(); |
| } |
| free(ap); |
| free(list); |
| } |
| } |
| return 0; |
| } |
| |
| int disableGBW() { |
| stopGBW(); |
| free(g_gbw_mac); |
| g_gbw_mac = NULL; |
| g_gbw_enabled = 1; |
| return 0; |
| } |
| |
| static int startGBW() { |
| if (g_gbw_watcher_pid != 0) { |
| stopGBW(); |
| } |
| pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL); |
| } |
| |
| static int stopGBW() { |
| void* retval; |
| pthread_cancel(g_gbw_watcher_pid); |
| pthread_join(g_gbw_watcher_pid, &retval); |
| g_gbw_watcher_pid = 0; |
| system("tc qdisc del dev tether root"); |
| } |
| //you.chen add for tv-box end |