qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1 | /**@File lib_wifi6.c |
| 2 | * @Brief :about function test |
| 3 | * @details : |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 4 | * @Author : you.chen |
| 5 | * @Date : 2022-4-6 |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 6 | * @Version : V1.0 |
| 7 | * @copy ritght : Copyright (c) MobileTek |
| 8 | */ |
| 9 | #include <log/log.h> |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <sys/types.h> |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 12 | #include <stdlib.h> |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 13 | #include <string.h> |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 14 | #include "libwifi6.h" |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 15 | #include <wpa_ctrl.h> |
| 16 | #include <string.h> |
| 17 | #include <time.h> |
| 18 | #include <pthread.h> |
| 19 | #include <sys/socket.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <arpa/inet.h> |
| 22 | #include <netdb.h> |
| 23 | #include <ifaddrs.h> |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 24 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | #ifdef __cplusplus |
| 29 | } |
| 30 | #endif |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 31 | |
| 32 | #define MAX_CMD 128 |
| 33 | #define MAX_RET 4096 |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 34 | #define MODE_LEN 10 |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 35 | #define CTRL_STA 0 |
| 36 | #define CTRL_AP 1 |
| 37 | #define AP_NETWORK_0 0 |
| 38 | |
| 39 | pthread_t g_ap_watcher_pid = 0; |
| 40 | volatile int g_ap_watcher_stop_flag = 0; |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 41 | volatile int g_ap_watcher_started_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 42 | |
| 43 | pthread_t g_sta_watcher_pid = 0; |
| 44 | volatile int g_sta_watcher_stop_flag = 0; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 45 | volatile int g_sta_scan_finish_flag = 1; |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 46 | volatile int g_sta_watcher_started_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 47 | |
| 48 | void * g_ap_callback_priv = NULL; |
| 49 | AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL; |
| 50 | void * g_sta_callback_priv = NULL; |
| 51 | STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL; |
| 52 | |
| 53 | //const char * CTRL_PATH="/var/run/wpa_supplicant"; |
| 54 | const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"}; |
| 55 | //const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"}; |
| 56 | const char * cmd_list_networks = "LIST_NETWORKS"; |
| 57 | const char * cmd_save_config = "SAVE_CONFIG"; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 58 | const char * cmd_disconnect = "DISCONNECT"; |
| 59 | const char * cmd_remove_all = "REMOVE_NETWORK all"; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 60 | const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS"; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 61 | const char * STATE_COMPLETED = "COMPLETED"; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 62 | |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 63 | struct local_wpa_ctrl{ |
| 64 | struct wpa_ctrl *ctrl; |
| 65 | pthread_mutex_t mutex; |
| 66 | }; |
| 67 | |
| 68 | static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 69 | |
| 70 | static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 71 | |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 72 | //you.chen add for tv-box start |
| 73 | volatile int g_gbw_enabled = 0; |
| 74 | char * g_gbw_mac = NULL; |
| 75 | pthread_t g_gbw_watcher_pid = 0; |
| 76 | static int startGBW(); |
| 77 | static int stopGBW(); |
| 78 | //you.chen add for tv-box end |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 79 | |
| 80 | typedef struct __curr_status_info { |
| 81 | ap_info_s *ap; |
| 82 | char * state; |
| 83 | int net_no; |
| 84 | }curr_status_info; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 85 | |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 86 | static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, |
| 87 | char *reply, size_t *reply_len, |
| 88 | void (*msg_cb)(char *msg, size_t len)) |
| 89 | { |
| 90 | int ret; |
| 91 | if (ctrl->ctrl == NULL) { |
| 92 | printf("local_wpa_ctrl_request ctrl is null\n"); |
| 93 | return -1; |
| 94 | } |
| 95 | pthread_mutex_lock(&ctrl->mutex); |
| 96 | ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb); |
| 97 | pthread_mutex_unlock(&ctrl->mutex); |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) { |
| 102 | int repeat_cnt; |
| 103 | struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; |
| 104 | pthread_mutex_lock(&s_check_wpa_ctrl_mutex); |
| 105 | printf("inner_get_wpa_ctrl\n"); |
| 106 | for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) { |
| 107 | pthread_mutex_unlock(&s_check_wpa_ctrl_mutex); |
| 108 | // printf("wait enable finish\n"); |
| 109 | usleep(500 * 1000); |
| 110 | pthread_mutex_lock(&s_check_wpa_ctrl_mutex); |
| 111 | } |
| 112 | if (NULL == g_lynq_wpa_ctrl[index]) { |
| 113 | printf("NULL == g_lynq_wpa_ctrl[index]\n"); |
| 114 | goto out_addr; |
| 115 | } |
| 116 | if (NULL == g_lynq_wpa_ctrl[index]->ctrl) { |
| 117 | g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]); |
| 118 | if (NULL == g_lynq_wpa_ctrl[index]->ctrl) { |
| 119 | printf("wpa_ctrl_open fail\n"); |
| 120 | goto out_addr; |
| 121 | } |
| 122 | pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL); |
| 123 | } |
| 124 | lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; |
| 125 | out_addr: |
| 126 | pthread_mutex_unlock(&s_check_wpa_ctrl_mutex); |
| 127 | return lynq_wpa_ctrl; |
| 128 | } |
| 129 | |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 130 | #define PRINT_AND_RETURN_VALUE(str,value) \ |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 131 | {\ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 132 | perror((str));\ |
| 133 | return (value);\ |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 134 | } |
| 135 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 136 | #define CHECK_IDX(idx, type) do { \ |
| 137 | if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \ |
| 138 | || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \ |
| 139 | printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \ |
| 140 | return -1; \ |
| 141 | } \ |
| 142 | }while (0) |
| 143 | |
| 144 | #define CHECK_WPA_CTRL(index) int ret = 0;\ |
| 145 | size_t reply_len = MAX_RET; \ |
| 146 | char cmd_reply[MAX_RET]={0}; \ |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 147 | struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 148 | do{ \ |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 149 | lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \ |
| 150 | if (NULL == lynq_wpa_ctrl) return -1; \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 151 | }while(0) |
| 152 | |
| 153 | #define DO_REQUEST(cmd_str) do { \ |
| 154 | reply_len = MAX_RET;\ |
| 155 | cmd_reply[0] = '\0'; \ |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 156 | printf("to call [%s]\n", cmd_str); \ |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 157 | ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 158 | if (ret != 0) { \ |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 159 | printf("call "#cmd_str" fail %d\n", ret); \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 160 | return ret; \ |
| 161 | } \ |
| 162 | cmd_reply[reply_len+1] = '\0'; \ |
| 163 | printf("cmd replay [ %s ]\n", cmd_reply); \ |
| 164 | }while(0) |
| 165 | |
| 166 | #define DO_OK_FAIL_REQUEST(cmd_str) do { \ |
| 167 | DO_REQUEST(cmd_str); \ |
| 168 | if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\ |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 169 | printf("cmd "#cmd_str" return FAIL\n"); \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 170 | return -1; \ |
| 171 | } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \ |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 172 | printf("cmd "#cmd_str" return not OK|FAIL\n"); \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 173 | return -1; \ |
| 174 | } \ |
| 175 | }while (0) |
| 176 | |
| 177 | |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 178 | static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 179 | |
| 180 | static void APWatcherThreadProc() { |
| 181 | size_t len = MAX_RET; |
| 182 | char msg_notify[MAX_RET]; |
| 183 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 184 | struct wpa_ctrl *lynq_wpa_ctrl = NULL; |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 185 | g_ap_watcher_stop_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 186 | |
| 187 | while (g_ap_watcher_stop_flag == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 188 | if (lynq_wpa_ctrl == NULL) { |
| 189 | lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change |
| 190 | if (lynq_wpa_ctrl == NULL) { |
| 191 | usleep(100*1000); |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | wpa_ctrl_attach(lynq_wpa_ctrl); |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 196 | g_ap_watcher_started_flag = 1; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 197 | } |
| 198 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 199 | if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) { |
| 200 | usleep(100*1000); |
| 201 | continue; |
| 202 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 203 | memset(msg_notify, 0, MAX_RET); |
| 204 | len = MAX_RET; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 205 | if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) { |
| 206 | msg_notify[len+1] = '\0'; |
| 207 | printf("ap------> %s\n", msg_notify); |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 208 | //you.chen change for tv-box start |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 209 | if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) { |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 210 | if (g_ap_callback_func != NULL) |
| 211 | g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT); |
| 212 | if (g_gbw_enabled == 1 && g_gbw_mac != NULL) { |
| 213 | printf("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac)); |
| 214 | if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) { |
| 215 | stopGBW(); |
| 216 | } |
| 217 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 218 | } |
| 219 | else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) { |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 220 | if (g_ap_callback_func != NULL) |
| 221 | g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT); |
| 222 | if (g_gbw_enabled == 1 && g_gbw_mac != NULL) { |
| 223 | printf("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac)); |
| 224 | if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) { |
| 225 | startGBW(); |
| 226 | } |
| 227 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 228 | } |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 229 | //you.chen add for tv-box end |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 230 | } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) |
| 231 | } // end while (g_ap_watcher_stop_flag == 0) |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 232 | if (lynq_wpa_ctrl != NULL) { |
| 233 | wpa_ctrl_detach(lynq_wpa_ctrl); |
| 234 | wpa_ctrl_close(lynq_wpa_ctrl); |
| 235 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 236 | } |
| 237 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 238 | static void STAWatcherThreadProc() { |
| 239 | size_t len = MAX_RET; |
| 240 | char msg_notify[MAX_RET]; |
| 241 | char *pReason; |
| 242 | error_number_s error; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 243 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 244 | struct wpa_ctrl *lynq_wpa_ctrl = NULL; |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 245 | g_sta_watcher_stop_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 246 | |
| 247 | while (g_sta_watcher_stop_flag == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 248 | if (lynq_wpa_ctrl == NULL) { |
| 249 | lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]); |
| 250 | if (lynq_wpa_ctrl == NULL) { |
| 251 | usleep(100*1000); |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | wpa_ctrl_attach(lynq_wpa_ctrl); |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 256 | g_sta_watcher_started_flag = 1; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 257 | } |
| 258 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 259 | if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) { |
| 260 | usleep(100*1000); |
| 261 | continue; |
| 262 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 263 | memset(msg_notify, 0, MAX_RET); |
| 264 | len = MAX_RET; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 265 | if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) { |
| 266 | msg_notify[len+1] = '\0'; |
| 267 | printf("sta ------> %s\n", msg_notify); |
| 268 | if (strstr(msg_notify, state_scan_result) != NULL) { |
| 269 | g_sta_scan_finish_flag = 1; |
| 270 | } |
| 271 | |
| 272 | if (g_sta_callback_func == NULL) { |
| 273 | continue; |
| 274 | } |
| 275 | error = -1; |
| 276 | if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) { |
| 277 | g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error); |
| 278 | } |
| 279 | else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) { |
| 280 | g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error); |
| 281 | } |
| 282 | else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) { |
| 283 | pReason = strstr(msg_notify, "reason="); |
| 284 | if (pReason != NULL) { |
| 285 | pReason += strlen("reason="); |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 286 | if (memcmp(pReason, "CONN_FAILED", 11) == 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 287 | error = LYNQ_TIME_OUT; |
| 288 | } |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 289 | else if (memcmp(pReason, "WRONG_KEY", 9) == 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 290 | error = LYNQ_PSW_ERROR; |
| 291 | } |
| 292 | g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error); |
| 293 | } |
| 294 | } |
| 295 | else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) { |
| 296 | g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error); |
| 297 | } |
| 298 | else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) { |
| 299 | g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP); |
| 300 | } |
| 301 | } |
| 302 | } |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 303 | if (lynq_wpa_ctrl != NULL) { |
| 304 | wpa_ctrl_detach(lynq_wpa_ctrl); |
| 305 | wpa_ctrl_close(lynq_wpa_ctrl); |
| 306 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 307 | } |
| 308 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 309 | int lynq_wifi_enable(void) |
| 310 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 311 | int ret = 0; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 312 | int i; |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 313 | pthread_mutex_lock(&s_check_wpa_ctrl_mutex); |
| 314 | |
| 315 | if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) { |
| 316 | goto out_enable; |
| 317 | } |
| 318 | |
| 319 | printf("lynq_wifi_enable1\n"); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 320 | const char * cmd_check_service = |
| 321 | "state=`systemctl is-active wg870_drv_insmod.service`\n" |
| 322 | "[ \"\"$state == \"active\" ] && exit 0\n" |
| 323 | "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n"; |
| 324 | // if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) { |
| 325 | // return 0; |
| 326 | // } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 327 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 328 | ret = system(cmd_check_service); |
| 329 | if (ret != 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 330 | printf("service state %d\n", ret); |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 331 | ret = -1; |
| 332 | goto out_enable; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 333 | } |
lh | fe8da90 | 2022-10-11 18:55:36 +0800 | [diff] [blame] | 334 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 335 | for (i=0; i<10; i++) { |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 336 | if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 337 | break; |
| 338 | } |
| 339 | usleep(300*1000); |
| 340 | } |
| 341 | |
| 342 | if (i >= 10) { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 343 | ret = -1; |
| 344 | goto out_enable; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 345 | } |
| 346 | |
you.chen | 9f17e4d | 2022-06-06 17:18:18 +0800 | [diff] [blame] | 347 | //@todo delete add temp check for socket avilable start (20220606) |
| 348 | for (i=0; i<60; i++) |
| 349 | { |
| 350 | if (system("netstat -an | grep -q DGRAM") == 0) { |
| 351 | break; |
| 352 | } |
| 353 | sleep(1); |
| 354 | } |
| 355 | |
| 356 | if (i >= 60) |
| 357 | { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 358 | ret = -1; |
| 359 | goto out_enable; |
you.chen | 9f17e4d | 2022-06-06 17:18:18 +0800 | [diff] [blame] | 360 | } |
| 361 | //@todo delete add temp check for socket avilable end (20220606) |
| 362 | |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 363 | if (0 != system("ifconfig | grep -q ap0")) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 364 | system("connmanctl enable wifi"); |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 365 | usleep(300*1000); |
you.chen | 01bfac3 | 2022-06-07 10:36:00 +0800 | [diff] [blame] | 366 | system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect"); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 367 | system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0"); |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 368 | usleep(300*1000); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 369 | system("connmanctl tether wifi on lynq 1qaz@WSX#$%^"); |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 370 | usleep(300*1000); |
you.chen | 01bfac3 | 2022-06-07 10:36:00 +0800 | [diff] [blame] | 371 | system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect"); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 372 | } |
qs.xiong | 55f3276 | 2023-02-16 15:59:45 +0800 | [diff] [blame] | 373 | ret = system("wl -i wlan0 PM 0"); |
| 374 | if( ret != 0) |
| 375 | { |
| 376 | printf("Set sta PM work mode to 0 fail,ret is%d",ret); |
| 377 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 378 | if (g_ap_watcher_pid == 0 ) { |
| 379 | ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL); |
| 380 | if(ret<0){ |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 381 | ret = -1; |
| 382 | goto out_enable; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | if (g_sta_watcher_pid == 0 ) { |
| 387 | ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL); |
| 388 | if(ret<0){ |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 389 | ret = -1; |
| 390 | goto out_enable; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 394 | for (i=0; i<10; i++) { |
| 395 | usleep(300*1000); |
| 396 | if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) { |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 401 | g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl)); |
| 402 | g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl)); |
| 403 | memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl)); |
| 404 | memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl)); |
| 405 | out_enable: |
| 406 | pthread_mutex_unlock(&s_check_wpa_ctrl_mutex); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 407 | return ret; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 408 | } |
| 409 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 410 | int lynq_wifi_disable(void) |
| 411 | { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 412 | pthread_mutex_lock(&s_check_wpa_ctrl_mutex); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 413 | g_ap_watcher_stop_flag = 1; |
| 414 | g_sta_watcher_stop_flag = 1; |
| 415 | if (g_ap_watcher_pid != 0) |
| 416 | pthread_join(g_ap_watcher_pid, NULL); |
| 417 | if (g_sta_watcher_pid != 0) |
| 418 | pthread_join(g_sta_watcher_pid, NULL); |
| 419 | if (g_lynq_wpa_ctrl[0] != NULL) |
| 420 | wpa_ctrl_close(g_lynq_wpa_ctrl[0]); |
| 421 | if (g_lynq_wpa_ctrl[1] != NULL) |
| 422 | wpa_ctrl_close(g_lynq_wpa_ctrl[1]); |
| 423 | g_ap_watcher_pid = 0; |
| 424 | g_sta_watcher_pid = 0; |
| 425 | g_lynq_wpa_ctrl[0] = NULL; |
| 426 | g_lynq_wpa_ctrl[1] = NULL; |
| 427 | system("systemctl stop wg870_drv_insmod.service"); |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 428 | pthread_mutex_unlock(&s_check_wpa_ctrl_mutex); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static inline char inner_convert_char(char in) |
| 433 | { |
| 434 | if (in >= '0' && in <= '9') |
| 435 | { |
| 436 | return in - '0'; |
| 437 | } |
| 438 | else if (in >= 'a' && in <= 'f') |
| 439 | { |
| 440 | return in - 'a' + 10; |
| 441 | } |
| 442 | else if (in >= 'A' && in <= 'F') |
| 443 | { |
| 444 | return in - 'A' + 10; |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | return '\xff'; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len) |
| 453 | { |
| 454 | char *p; |
| 455 | size_t pos = 0; |
| 456 | if (NULL == out_ssid) |
| 457 | return; |
| 458 | //printf("input ssid=[%s]\n", ssid); |
| 459 | memset(out_ssid, 0, out_ssid_len); |
| 460 | if (NULL == ssid) |
| 461 | return; |
| 462 | p = strchr(ssid, '\\'); |
| 463 | if (NULL == p) |
| 464 | { |
| 465 | strncpy(out_ssid, ssid, out_ssid_len); |
| 466 | //printf(" first %s\n", out_ssid); |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | pos = p - ssid; |
| 471 | memcpy(out_ssid, ssid, pos); |
| 472 | //printf("pos %lu -- %s\n", pos, out_ssid); |
| 473 | for(; pos < out_ssid_len; pos ++) |
| 474 | { |
| 475 | if (p[0] == '\0') |
| 476 | { |
| 477 | //printf(" out %s\n", out_ssid); |
| 478 | return; |
| 479 | } |
| 480 | else if (p[0] != '\\') |
| 481 | { |
| 482 | out_ssid[pos] = p[0]; |
| 483 | p += 1; |
| 484 | } |
| 485 | else if (p[1] == 'x' || p[1] == 'X') |
| 486 | { |
| 487 | out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]); |
| 488 | p += 4; |
| 489 | } |
| 490 | else if (p[1] == '\\') |
| 491 | { |
| 492 | out_ssid[pos] = '\\'; |
| 493 | p += 2; |
| 494 | } |
| 495 | else if (p[1] == 't') |
| 496 | { |
| 497 | out_ssid[pos] = '\t'; |
| 498 | p += 2; |
| 499 | } |
| 500 | else if (p[1] == 'r') |
| 501 | { |
| 502 | out_ssid[pos] = '\r'; |
| 503 | p += 2; |
| 504 | } |
| 505 | else if (p[1] == 'n') |
| 506 | { |
| 507 | out_ssid[pos] = '\n'; |
| 508 | p += 2; |
| 509 | }//todo find a better way to convert? |
| 510 | } |
| 511 | } |
| 512 | //printf(" out %s\n", out_ssid); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 513 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 514 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 515 | static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 516 | int i, ssid_len; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 517 | char lynq_cmd_get[128]={0}; |
| 518 | |
| 519 | if (out_put == NULL) { |
| 520 | printf("output ptr is null\n"); |
| 521 | return -1; |
| 522 | } |
| 523 | if (param_name == NULL) { |
| 524 | printf("param ptr is null"); |
| 525 | return -1; |
| 526 | } |
| 527 | if (param_name[0] == '\0') { |
| 528 | printf("param is empty"); |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name); |
| 533 | |
| 534 | CHECK_WPA_CTRL(interface); |
| 535 | |
| 536 | DO_REQUEST(lynq_cmd_get); |
| 537 | |
| 538 | if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 539 | return -1; |
| 540 | } |
| 541 | |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 542 | // printf("reply len %d, %08x\n", reply_len, (int)out_put); |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 543 | if (strcmp(param_name, "ssid") == 0) |
| 544 | { |
| 545 | if (cmd_reply[0] == '\"') { |
| 546 | ssid_len = reply_len - 1; |
| 547 | memcpy(out_put, cmd_reply + 1, ssid_len); |
| 548 | if (out_put[ssid_len-1] == '\"') |
| 549 | { |
| 550 | out_put[ssid_len-1] = '\0'; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | out_put[ssid_len] = '\0'; |
| 555 | } |
| 556 | } |
| 557 | else{ |
| 558 | ssid_len = reply_len / 2; |
| 559 | for(i=0; i<ssid_len; i++) |
| 560 | { |
| 561 | out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]); |
| 562 | } |
| 563 | out_put[ssid_len] = '\0'; |
| 564 | } |
| 565 | } |
| 566 | else |
| 567 | { |
| 568 | memcpy(out_put, cmd_reply, reply_len + 1); |
| 569 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 570 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 571 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 572 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 573 | static int lynq_split(char * str, int len, char delimiter, char * results[]) { |
| 574 | int ret = 0; |
| 575 | char * end = str + len - 1; |
| 576 | results[ret++] = str; |
| 577 | while(str < end) { |
| 578 | if (*str == delimiter) { |
| 579 | *str++ = '\0'; |
| 580 | results[ret++] = str; |
| 581 | continue; |
| 582 | } |
| 583 | str++; |
| 584 | } |
| 585 | if (*str == delimiter) { |
| 586 | *str = '\0'; |
| 587 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 588 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 589 | return ret; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 590 | } |
| 591 | |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 592 | static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len) |
| 593 | { |
| 594 | char * p; |
| 595 | int ret = 0; |
| 596 | char cmd[256]={0}; |
| 597 | if (NULL == mac || NULL == ip) |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 598 | return -1; |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 599 | memset(ip, 0, ip_len); |
qs.xiong | 1367346 | 2023-02-21 19:12:54 +0800 | [diff] [blame] | 600 | sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | head -1 | awk '{print $1}'", mac); |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 601 | ret = exec_cmd(cmd, ip, ip_len); |
| 602 | p = strchr(ip, '\n'); |
| 603 | if (NULL != p) |
| 604 | { |
| 605 | *p = '\0'; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 606 | } |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 607 | printf("inner_get_ip_by_mac %s\n", ip); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 608 | return ret; |
| 609 | } |
| 610 | |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 611 | static int inner_get_hostname_by_ip(char *ip, char *hostname) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 612 | struct in_addr addr ={0}; |
| 613 | struct hostent *ht; |
| 614 | |
| 615 | if (ip == NULL || *ip == '\0' || hostname == NULL) { |
| 616 | return -1; |
| 617 | } |
| 618 | |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 619 | *hostname = '\0'; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 620 | if (inet_aton(ip, &addr) == 0) { |
| 621 | printf("---inet_aton fail\n"); |
| 622 | return -1; |
| 623 | } |
| 624 | |
| 625 | ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET); |
| 626 | |
| 627 | if (ht == NULL) { |
| 628 | printf("---gethostbyaddr fail\n"); |
| 629 | herror(NULL); |
| 630 | return -1; |
| 631 | } |
| 632 | |
| 633 | strcpy(hostname, ht->h_name); |
| 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid) |
| 639 | { |
| 640 | int count, index, words_count; |
| 641 | char * split_lines[128]= {0}; |
| 642 | char * split_words[128] = {0}; |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 643 | char local_ssid[128] = {0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 644 | const char *lynq_wifi_list_networks = "LIST_NETWORKS"; |
| 645 | |
| 646 | CHECK_WPA_CTRL(ap_sta); |
| 647 | |
| 648 | DO_REQUEST(lynq_wifi_list_networks); |
| 649 | |
| 650 | count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| 651 | |
| 652 | //@todo check ssid field to compatible |
| 653 | |
| 654 | ret = 0; |
| 655 | for(index=1; index < count; index++) { |
| 656 | words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words); |
| 657 | if (words_count > 2) { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 658 | inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid)); |
| 659 | if (ssid == NULL || strcmp(local_ssid, ssid) == 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 660 | net_no_list[ret++] = atoi(split_words[0]); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | static int lynq_add_network(int ap_sta) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 669 | size_t i=0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 670 | CHECK_WPA_CTRL(ap_sta); |
| 671 | const char *lynq_wifi_add_network = "ADD_NETWORK"; |
| 672 | |
| 673 | DO_REQUEST(lynq_wifi_add_network); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 674 | if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 675 | return -1; |
| 676 | } |
| 677 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 678 | for(i=0;i<reply_len;i++) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 679 | if(cmd_reply[i] == '\n') { |
| 680 | cmd_reply[i] = '\0'; |
| 681 | break; |
| 682 | } |
| 683 | } |
| 684 | return atoi(cmd_reply); |
| 685 | } |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 686 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 687 | static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no) |
| 688 | { |
| 689 | int count, index; |
| 690 | int net_no_list[128]; |
| 691 | |
| 692 | |
| 693 | count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL); |
| 694 | for (index=0; index < count; index++) { |
| 695 | if (net_no_list[index] == net_no) { |
| 696 | return 0; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | if (count >= 1) |
| 701 | index = net_no_list[count - 1]; |
| 702 | else |
| 703 | index = -1; |
| 704 | |
| 705 | while (index < net_no ) { |
| 706 | index = lynq_add_network(ap_sta); |
| 707 | if (index >= net_no) { // required network no created |
| 708 | return 0; |
| 709 | } |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 710 | else if( index < 0) { |
| 711 | printf("add network fail\n"); |
| 712 | return -1; |
| 713 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | if (index < 0) |
| 717 | return -1; |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo |
| 723 | if (freq > 5000 && freq < 6000) { |
| 724 | return LYNQ_WIFI_5G_band; |
| 725 | } |
| 726 | else if (freq > 2000 && freq < 3000) { |
| 727 | return LYNQ_WIFI_2G_band; |
| 728 | } |
| 729 | return LYNQ_WIFI_2_and_5G_band; |
| 730 | } |
| 731 | |
| 732 | static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) { |
| 733 | if (key_mgmt != NULL) { |
| 734 | if (memcmp( key_mgmt, "NONE", 4) == 0) { |
| 735 | return LYNQ_WIFI_AUTH_OPEN; |
| 736 | } |
| 737 | else if (memcmp( key_mgmt, "WEP", 3) == 0){ |
| 738 | return LYNQ_WIFI_AUTH_WEP; |
| 739 | } |
| 740 | else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){ |
| 741 | return LYNQ_WIFI_AUTH_WPA_PSK; |
| 742 | } |
| 743 | else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){ |
| 744 | return LYNQ_WIFI_AUTH_WPA2_PSK; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | return -1; |
| 749 | } |
| 750 | |
| 751 | static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) { |
| 752 | if (flag != NULL) { |
| 753 | if (strstr( flag, "WPA2-PSK") != NULL){ |
| 754 | return LYNQ_WIFI_AUTH_WPA2_PSK; |
| 755 | } |
| 756 | else if (strstr( flag, "WPA-PSK") != NULL){ |
| 757 | return LYNQ_WIFI_AUTH_WPA_PSK; |
| 758 | } |
| 759 | else if (strstr( flag, "WEP") != NULL){ |
| 760 | return LYNQ_WIFI_AUTH_WEP; |
| 761 | } |
| 762 | else if (strstr( flag, "NONE") != NULL) { |
| 763 | return LYNQ_WIFI_AUTH_OPEN; |
| 764 | } |
qs.xiong | d215fa3 | 2023-02-18 16:16:26 +0800 | [diff] [blame] | 765 | else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0) { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 766 | return LYNQ_WIFI_AUTH_OPEN; |
| 767 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) { |
| 774 | switch (bw) { |
| 775 | case 10: |
| 776 | return LYNQ_WIFI_BANDWIDTH_HT10; |
| 777 | break; |
| 778 | case 20: |
| 779 | return LYNQ_WIFI_BANDWIDTH_HT20; |
| 780 | break; |
| 781 | case 40: |
| 782 | return LYNQ_WIFI_BANDWIDTH_HT40; |
| 783 | break; |
| 784 | case 80: |
| 785 | return LYNQ_WIFI_BANDWIDTH_HT80; |
| 786 | break; |
| 787 | default: |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | static int inner_get_status_info(int interface, curr_status_info *curr_state) { |
| 795 | int i, count; |
| 796 | char *p; |
| 797 | const char *lynq_status_cmd = "STATUS"; |
| 798 | const char * FLAG_SSID = "ssid="; |
| 799 | const char * FLAG_SBSID = "bssid="; |
| 800 | const char * FLAG_KEY_MGMT = "key_mgmt="; |
| 801 | const char * FLAG_FREQ = "freq="; |
| 802 | const char * FLAG_STATE = "wpa_state="; |
| 803 | const char * FLAG_ID = "id="; |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 804 | const char * FLAG_IPADDR = "ip_address="; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 805 | char *split_lines[128] = {0}; |
| 806 | |
| 807 | CHECK_WPA_CTRL(interface); |
| 808 | |
| 809 | if (curr_state == NULL) { |
| 810 | return -1; |
| 811 | } |
| 812 | |
| 813 | DO_REQUEST(lynq_status_cmd); |
| 814 | |
| 815 | count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| 816 | |
| 817 | curr_state->net_no = -1; |
| 818 | ret = -1; |
| 819 | for(i=0; i < count; i++) { |
| 820 | if (curr_state->ap != NULL) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 821 | p = strstr(split_lines[i], FLAG_SBSID); |
| 822 | if (p != NULL) { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 823 | strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac)); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 824 | ret = 0; |
| 825 | continue; |
| 826 | } |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 827 | p = strstr(split_lines[i], FLAG_SSID); |
| 828 | if (p != NULL) { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 829 | inner_copy_ssid(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID), sizeof (curr_state->ap->ap_ssid)); |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 830 | ret = 0; |
| 831 | continue; |
| 832 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 833 | p = strstr(split_lines[i], FLAG_KEY_MGMT); |
| 834 | if (p != NULL) { |
you.chen | 450d017 | 2022-07-15 17:56:48 +0800 | [diff] [blame] | 835 | curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT)); |
| 836 | printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 837 | ret = 0; |
| 838 | continue; |
| 839 | } |
| 840 | p = strstr(split_lines[i], FLAG_FREQ); |
| 841 | if (p != NULL) { |
| 842 | curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ))); |
| 843 | ret = 0; |
| 844 | continue; |
| 845 | } |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 846 | p = strstr(split_lines[i], FLAG_IPADDR); |
| 847 | if (p != NULL) { |
| 848 | strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip)); |
| 849 | ret = 0; |
| 850 | continue; |
| 851 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 852 | } // end if (ap != NULL) |
| 853 | if (curr_state->state != NULL) { |
| 854 | p = strstr(split_lines[i], FLAG_STATE); |
| 855 | if (p != NULL) { |
| 856 | strcpy(curr_state->state, p + strlen(FLAG_STATE)); |
| 857 | ret = 0; |
| 858 | continue; |
| 859 | } |
| 860 | |
| 861 | } //end else if (state != NULL) |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 862 | if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 863 | ret = 0; |
you.chen | 450d017 | 2022-07-15 17:56:48 +0800 | [diff] [blame] | 864 | curr_state->net_no = atoi(p + strlen(FLAG_ID)); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 865 | printf("net_no %d, -- %s\n", curr_state->net_no, p); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | return ret; |
| 870 | } |
| 871 | |
| 872 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 873 | int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 874 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 875 | char lynq_wifi_ssid_cmd[80]={0}; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 876 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 877 | if (ap_ssid == NULL) { |
| 878 | printf("ap_ssid is null\n"); |
| 879 | return -1; |
| 880 | } |
| 881 | else { |
| 882 | printf("idx:%d ap_ssid : %s\n", idx, ap_ssid); |
| 883 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 884 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 885 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { |
| 886 | return -1; |
| 887 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 888 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 889 | CHECK_IDX(idx, CTRL_AP); |
| 890 | |
| 891 | CHECK_WPA_CTRL(CTRL_AP); |
| 892 | |
| 893 | sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid); |
| 894 | |
| 895 | DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd); |
| 896 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 897 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 898 | return 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 899 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 900 | } |
| 901 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 902 | int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 903 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 904 | CHECK_IDX(idx, CTRL_AP); |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 905 | return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 906 | } |
| 907 | |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 908 | /***** |
| 909 | *frequency <------>channel |
| 910 | * |
| 911 | *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165 |
| 912 | * |
| 913 | * |
| 914 | *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825 |
| 915 | * |
| 916 | * |
| 917 | * */ |
| 918 | static int lynq_check_set_frequency(int input_frequency){ |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame] | 919 | 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}; |
| 920 | int i; |
| 921 | int arr_len = sizeof(legitimate_frequency) / sizeof(int); |
| 922 | |
qs.xiong | 69a332b | 2022-12-02 09:58:57 +0800 | [diff] [blame] | 923 | for(i = 0; i < arr_len; i++) |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame] | 924 | { |
| 925 | if(input_frequency == legitimate_frequency[i]) |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 926 | break; |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 927 | } |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame] | 928 | |
| 929 | if(i == arr_len) |
| 930 | { |
| 931 | printf("input frequency is eero--->%d,please check it\n", input_frequency); |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 932 | return -1; |
| 933 | } |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame] | 934 | |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 935 | return 0; |
| 936 | } |
qs.xiong | 1367346 | 2023-02-21 19:12:54 +0800 | [diff] [blame] | 937 | |
| 938 | static int lynq_check_frequencyby_country_code(int input_frequency) |
| 939 | { |
| 940 | char str_cnc[]="CN"; |
| 941 | char str_dest[20]=""; |
| 942 | |
| 943 | if( lynq_get_country_code(1,str_dest) != 0 ) |
| 944 | { |
| 945 | printf("get country_code error\n"); |
| 946 | return -1; |
| 947 | } |
| 948 | if( strncmp(str_dest,str_cnc,2) != 0 ) |
| 949 | { |
| 950 | return 0; |
| 951 | }else if( 2473 < input_frequency && input_frequency < 5744) |
| 952 | { |
| 953 | printf("input frequency is bad\n"); |
| 954 | return -1; |
| 955 | } |
| 956 | return 0; |
| 957 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 958 | int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 959 | { |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame] | 960 | int check; |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 961 | char lynq_wifi_frequency_cmd[128]={0}; |
| 962 | char lynq_cmd_mode[128]={0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 963 | char lynq_cmd_slect[128]={0}; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 964 | |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 965 | //@do check input frequency |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame] | 966 | check = lynq_check_set_frequency(lynq_wifi_frequency); |
| 967 | if(check != 0) |
| 968 | { |
| 969 | printf("do check frequency error\n"); |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 970 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 971 | } |
qs.xiong | 1367346 | 2023-02-21 19:12:54 +0800 | [diff] [blame] | 972 | check = lynq_check_frequencyby_country_code(lynq_wifi_frequency); |
| 973 | if(check != 0) |
| 974 | { |
| 975 | printf("do check frequency error\n"); |
| 976 | return -1; |
| 977 | } |
| 978 | |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame] | 979 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) |
| 980 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 981 | return -1; |
| 982 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 983 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 984 | CHECK_IDX(idx, CTRL_AP); |
| 985 | |
| 986 | CHECK_WPA_CTRL(CTRL_AP); |
| 987 | |
| 988 | sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency); |
| 989 | sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0); |
| 990 | sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0); |
| 991 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 992 | DO_OK_FAIL_REQUEST(cmd_disconnect); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 993 | DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd); |
| 994 | DO_OK_FAIL_REQUEST(lynq_cmd_mode); |
| 995 | DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 996 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 997 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 998 | } |
| 999 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1000 | int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1001 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1002 | char lynq_frequency_str[MAX_RET] = {0}; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1003 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1004 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1005 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1006 | if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) { |
| 1007 | return -1; |
| 1008 | } |
| 1009 | *lynq_wifi_frequency = atoi(lynq_frequency_str); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1010 | |
| 1011 | return 0; |
| 1012 | } |
| 1013 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1014 | int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth) |
| 1015 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1016 | CHECK_IDX(idx, CTRL_AP); |
| 1017 | switch(bandwidth){ |
| 1018 | case LYNQ_WIFI_BANDWIDTH_HT10: |
| 1019 | { |
| 1020 | printf("bandwith [%d] not support now\n", bandwidth); |
| 1021 | return -1; |
| 1022 | } |
| 1023 | case LYNQ_WIFI_BANDWIDTH_HT20: |
| 1024 | { |
| 1025 | char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6"; |
| 1026 | system("wl down"); |
| 1027 | if (system(lynq_cmd_bandwith) != 0 ){ |
| 1028 | return -1; |
| 1029 | } |
| 1030 | system("wl up"); |
| 1031 | break; |
| 1032 | } |
| 1033 | case LYNQ_WIFI_BANDWIDTH_HT40: |
| 1034 | { |
qs.xiong | 1037919 | 2023-02-21 13:19:42 +0800 | [diff] [blame] | 1035 | char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40"; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1036 | sprintf(lynq_cmd_bandwith, "wl chanspec "); |
| 1037 | system("wl down"); |
| 1038 | if (system(lynq_cmd_bandwith) != 0 ){ |
| 1039 | return -1; |
| 1040 | } |
| 1041 | system("wl up"); |
| 1042 | break; |
| 1043 | } |
| 1044 | case LYNQ_WIFI_BANDWIDTH_HT80: |
| 1045 | { |
qs.xiong | 1037919 | 2023-02-21 13:19:42 +0800 | [diff] [blame] | 1046 | char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80"; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1047 | system("wl down"); |
| 1048 | if (system(lynq_cmd_bandwith) != 0 ){ |
| 1049 | return -1; |
| 1050 | } |
| 1051 | system("wl up"); |
| 1052 | break; |
| 1053 | } |
| 1054 | default: |
| 1055 | { |
| 1056 | printf("auth type [%d] not support now\n", bandwidth); |
| 1057 | return -1; |
| 1058 | } |
| 1059 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1060 | |
| 1061 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1062 | return 0; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1063 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1064 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1065 | int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth) |
| 1066 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1067 | int count = 0; |
| 1068 | int index = 0; |
| 1069 | char *split_words[128] = {0}; |
| 1070 | const char *lynq_chanspec_cmd = "DRIVER chanspec\n"; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1071 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1072 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1073 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1074 | CHECK_WPA_CTRL(CTRL_AP); |
| 1075 | |
| 1076 | DO_REQUEST(lynq_chanspec_cmd); |
| 1077 | |
| 1078 | count = lynq_split(cmd_reply, reply_len, ' ', split_words); |
| 1079 | for(;index < count; index++) { |
| 1080 | if (strncmp(split_words[index], "bw", 2) != 0) { |
| 1081 | continue; |
| 1082 | } |
| 1083 | |
| 1084 | index++; |
| 1085 | if (index >= count) { |
| 1086 | return -1; |
| 1087 | } |
| 1088 | |
| 1089 | printf("bw %s\n", split_words[index]); |
| 1090 | *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index])); |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | return -1; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1095 | } |
qs.xiong | 0fb469a | 2022-04-14 03:50:45 -0400 | [diff] [blame] | 1096 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1097 | int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1098 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1099 | char lynq_cmd_channel[MAX_CMD]={0}; |
qs.xiong | 1d58e9b | 2022-04-14 06:17:01 -0400 | [diff] [blame] | 1100 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1101 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 1d58e9b | 2022-04-14 06:17:01 -0400 | [diff] [blame] | 1102 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1103 | sprintf(lynq_cmd_channel, "wl channel %d", channel); |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1104 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1105 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { |
| 1106 | return -1; |
| 1107 | } |
| 1108 | |
| 1109 | system("wl down"); |
| 1110 | if (system(lynq_cmd_channel) != 0 ){ |
| 1111 | return -1; |
| 1112 | } |
| 1113 | system("wl up"); |
| 1114 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1115 | } |
qs.xiong | 0fb469a | 2022-04-14 03:50:45 -0400 | [diff] [blame] | 1116 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1117 | int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1118 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1119 | int count = 0; |
| 1120 | int index = 0; |
| 1121 | char *split_words[128] = {0}; |
| 1122 | char lynq_chanspec_cmd[]="DRIVER chanspec\n"; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1123 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1124 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1125 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1126 | CHECK_WPA_CTRL(CTRL_AP); |
| 1127 | |
| 1128 | DO_REQUEST(lynq_chanspec_cmd); |
| 1129 | |
| 1130 | count = lynq_split(cmd_reply, reply_len, ' ', split_words); |
| 1131 | for(;index < count; index++) { |
| 1132 | printf("---- %s\n",split_words[index]); |
| 1133 | if (strncmp(split_words[index], "channel", 2) != 0) { |
| 1134 | continue; |
| 1135 | } |
| 1136 | |
| 1137 | index++; |
| 1138 | if (index >= count) { |
| 1139 | return -1; |
| 1140 | } |
| 1141 | |
| 1142 | *channel = atoi(split_words[index]); |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | return -1; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1150 | int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1151 | { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1152 | char ssid[MAX_CMD] = {0}; |
| 1153 | int freq = 0; |
| 1154 | char lynq_auth_cmd[64]={0}; |
| 1155 | char lynq_auth_alg_cmd[64]={0}; |
| 1156 | char lynq_psk_cmd[64]={0}; |
| 1157 | char lynq_pairwise_cmd[64]={0}; |
| 1158 | lynq_wifi_auth_s org_auth; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1159 | CHECK_IDX(idx, CTRL_AP); |
| 1160 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1161 | CHECK_WPA_CTRL(CTRL_AP); |
| 1162 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1163 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) { |
| 1164 | return -1; |
| 1165 | } |
| 1166 | |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1167 | if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1168 | if (org_auth == auth) { |
| 1169 | return 0; |
| 1170 | } |
| 1171 | else { |
| 1172 | if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) { |
| 1173 | ssid[0] = '\0'; |
| 1174 | } |
| 1175 | lynq_wifi_ap_frequency_get(idx, &freq); |
| 1176 | |
| 1177 | DO_OK_FAIL_REQUEST(cmd_disconnect); |
| 1178 | DO_OK_FAIL_REQUEST(cmd_remove_all); |
| 1179 | if (ssid[0] != '\0') { |
| 1180 | lynq_wifi_ap_ssid_set(idx, ssid); |
| 1181 | } |
| 1182 | if (freq != 0) { |
| 1183 | lynq_wifi_ap_frequency_set(idx, freq); |
| 1184 | } |
| 1185 | } |
| 1186 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1187 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1188 | switch(auth){ |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1189 | case LYNQ_WIFI_AUTH_OPEN: |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1190 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1191 | sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0); |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1192 | sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1193 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1194 | DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1195 | break; |
| 1196 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1197 | case LYNQ_WIFI_AUTH_WEP: |
| 1198 | { |
| 1199 | sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0); |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1200 | sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1201 | sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0); |
| 1202 | |
| 1203 | DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| 1204 | DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd); |
| 1205 | break; |
| 1206 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1207 | case LYNQ_WIFI_AUTH_WPA_PSK: |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1208 | case LYNQ_WIFI_AUTH_WPA2_PSK: |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1209 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1210 | if (auth == LYNQ_WIFI_AUTH_WPA_PSK) { |
| 1211 | sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0); |
| 1212 | sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); |
| 1213 | } |
| 1214 | else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1215 | sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1216 | sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1217 | } |
| 1218 | // sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0); |
| 1219 | // sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); |
| 1220 | sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1221 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1222 | DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| 1223 | DO_OK_FAIL_REQUEST(lynq_psk_cmd); |
| 1224 | DO_OK_FAIL_REQUEST(lynq_pairwise_cmd); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1225 | break; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1226 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1227 | default: |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1228 | { |
| 1229 | printf("auth type [%d] not support now\n", auth); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1230 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1231 | } |
| 1232 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1233 | DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1234 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1235 | return 0; |
| 1236 | } |
| 1237 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1238 | int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1239 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1240 | char lynq_auth_str[MAX_RET] = {0}; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1241 | char lynq_auth_alg_str[MAX_RET] = {0}; |
| 1242 | char lynq_proto_str[MAX_RET] = {0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1243 | |
| 1244 | CHECK_IDX(idx, CTRL_AP); |
| 1245 | |
| 1246 | if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) { |
| 1247 | return -1; |
| 1248 | } |
| 1249 | |
| 1250 | if (memcmp( lynq_auth_str, "NONE", 4) == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1251 | if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) { |
| 1252 | *auth = LYNQ_WIFI_AUTH_OPEN; |
| 1253 | } |
| 1254 | else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){ |
| 1255 | *auth = LYNQ_WIFI_AUTH_WEP; |
| 1256 | } |
| 1257 | else { |
| 1258 | *auth = LYNQ_WIFI_AUTH_OPEN; |
| 1259 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1260 | } |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1261 | else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) { |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 1262 | if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) { |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1263 | *auth = -1; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1264 | } |
| 1265 | else if (memcmp(lynq_proto_str, "RSN", 3) == 0) { |
| 1266 | *auth = LYNQ_WIFI_AUTH_WPA2_PSK; |
| 1267 | } |
| 1268 | else { |
| 1269 | *auth = LYNQ_WIFI_AUTH_WPA_PSK; |
| 1270 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1271 | } |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1272 | else { |
| 1273 | *auth = -1; |
| 1274 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1275 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1276 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1277 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1278 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1279 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1280 | int lynq_wifi_ap_start(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1281 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1282 | char LYNQ_WIFI_CMD[128]={0}; |
| 1283 | //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all"; |
| 1284 | //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf"; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1285 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1286 | CHECK_IDX(idx, CTRL_AP); |
| 1287 | |
| 1288 | CHECK_WPA_CTRL(CTRL_AP); |
| 1289 | |
| 1290 | // system("connmanctl enable wifi"); |
| 1291 | // system("connmanctl tether wifi on cy-test 12345678"); |
| 1292 | // system("ifconfig wlan0 down"); |
| 1293 | // system("ifconfig wlan0 up"); |
| 1294 | // system("ifconfig wlan0 up"); |
| 1295 | |
| 1296 | //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd); |
| 1297 | //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd); |
| 1298 | |
| 1299 | sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0); |
| 1300 | DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD); |
| 1301 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1302 | return 0; |
| 1303 | } |
| 1304 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1305 | int lynq_wifi_ap_restart(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1306 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1307 | return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1308 | } |
| 1309 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1310 | int lynq_wifi_ap_stop(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1311 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1312 | char LYNQ_WIFI_CMD[128]={0}; |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1313 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1314 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1315 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1316 | CHECK_WPA_CTRL(CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1317 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1318 | sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0); |
| 1319 | |
| 1320 | DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD); |
| 1321 | |
you.chen | b4b121c | 2022-05-06 17:50:16 +0800 | [diff] [blame] | 1322 | // system("connmanctl tether wifi off"); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1323 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1324 | return 0; |
| 1325 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1326 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1327 | int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1328 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1329 | char lynq_disable_cmd[128] = {0}; |
| 1330 | char lynq_select_cmd[128] = {0}; |
| 1331 | const char *lynq_hide_cmd = "SET HIDE_SSID 1"; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1332 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1333 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1334 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1335 | CHECK_WPA_CTRL(CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1336 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1337 | sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0); |
| 1338 | sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0); |
| 1339 | |
| 1340 | DO_OK_FAIL_REQUEST(lynq_disable_cmd); |
| 1341 | DO_OK_FAIL_REQUEST(lynq_hide_cmd); |
| 1342 | DO_OK_FAIL_REQUEST(lynq_select_cmd); |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1343 | |
| 1344 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1345 | } |
| 1346 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1347 | int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1348 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1349 | char lynq_disable_cmd[128] = {0}; |
| 1350 | char lynq_select_cmd[128] = {0}; |
| 1351 | const char *lynq_unhide_cmd = "SET HIDE_SSID 0"; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1352 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1353 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1354 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1355 | CHECK_WPA_CTRL(CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1356 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1357 | sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0); |
| 1358 | sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0); |
| 1359 | |
| 1360 | DO_OK_FAIL_REQUEST(lynq_disable_cmd); |
| 1361 | DO_OK_FAIL_REQUEST(lynq_unhide_cmd); |
| 1362 | DO_OK_FAIL_REQUEST(lynq_select_cmd); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1363 | |
| 1364 | return 0; |
| 1365 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1366 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1367 | int lynq_ap_password_set(lynq_wifi_index_e idx,char *password) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1368 | { |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1369 | int pass_len; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1370 | char lynq_tmp_cmd[MAX_CMD] = {0}; |
| 1371 | char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0}; |
qs.xiong | e707432 | 2022-06-27 11:34:53 +0800 | [diff] [blame] | 1372 | if( password == NULL ){ |
| 1373 | return -1; |
| 1374 | } |
| 1375 | pass_len=strlen(password); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1376 | lynq_wifi_auth_s auth = -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1377 | if(pass_len < 8 || pass_len >= 64){ |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1378 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1379 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1380 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1381 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1382 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1383 | if (0 != lynq_wifi_ap_auth_get(idx, &auth)) { |
| 1384 | return -1; |
| 1385 | } |
| 1386 | else if (auth == LYNQ_WIFI_AUTH_OPEN) { |
| 1387 | return -1; |
| 1388 | } |
| 1389 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1390 | CHECK_WPA_CTRL(CTRL_AP); |
| 1391 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1392 | if (auth == LYNQ_WIFI_AUTH_WEP) { |
| 1393 | sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password); |
| 1394 | sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0); |
| 1395 | DO_OK_FAIL_REQUEST(lynq_tmp_cmd); |
| 1396 | DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd); |
| 1397 | } |
| 1398 | else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) { |
| 1399 | sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password); |
| 1400 | DO_OK_FAIL_REQUEST(lynq_tmp_cmd); |
| 1401 | } |
| 1402 | else { |
| 1403 | return -1; |
| 1404 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1405 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1406 | DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1407 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1408 | return 0; |
| 1409 | } |
| 1410 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1411 | int lynq_ap_password_get(lynq_wifi_index_e idx, char *password) |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1412 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1413 | FILE * fp; |
| 1414 | int len, ret; |
| 1415 | int count, index; |
| 1416 | char *split_lines[128] = {0}; |
| 1417 | char *buff, *p; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1418 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1419 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1420 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1421 | fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb"); |
| 1422 | // fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb"); |
| 1423 | if (NULL == fp) { |
| 1424 | printf("open file fail\n"); |
| 1425 | return -1; |
| 1426 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1427 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1428 | buff = alloca(MAX_RET); |
| 1429 | fseek(fp, 0, SEEK_SET); |
| 1430 | len = fread(buff, 1, MAX_RET, fp); |
| 1431 | fclose(fp); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1432 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1433 | for(index=0; index < len; index ++) { |
| 1434 | if (memcmp(buff + index, "network={", 9) != 0) { |
| 1435 | continue; |
| 1436 | } |
| 1437 | p = buff + index + 9; |
| 1438 | for (; index < len; index ++ ) { |
| 1439 | if (buff[index] != '}') { |
| 1440 | continue; |
| 1441 | } |
| 1442 | buff[index] = '\0'; |
| 1443 | break; |
| 1444 | } |
| 1445 | len = buff + index - p; |
| 1446 | } |
| 1447 | |
| 1448 | count = lynq_split(p, len, '\n', split_lines); |
| 1449 | |
| 1450 | ret = -1; |
| 1451 | for(index=0; index < count; index++) { |
| 1452 | p = strstr(split_lines[index], "psk="); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1453 | if (p != NULL) { |
| 1454 | p += 4; |
| 1455 | if (*p == '\"') { |
| 1456 | p++; |
| 1457 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1458 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1459 | else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) { |
| 1460 | p += 9; |
| 1461 | if (*p == '\"') { |
| 1462 | p++; |
| 1463 | } |
| 1464 | } |
| 1465 | else { |
| 1466 | continue; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | strcpy(password, p); |
| 1470 | |
| 1471 | while(*password != '\0') { |
| 1472 | if (*password == '\"') { |
| 1473 | *password = '\0'; |
| 1474 | break; |
| 1475 | } |
| 1476 | password++; |
| 1477 | } |
| 1478 | ret = 0; |
| 1479 | break; |
| 1480 | } //end for(index=0; index < count; index++) |
| 1481 | |
| 1482 | return ret; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1483 | } |
| 1484 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1485 | static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) { |
| 1486 | char lynq_auth_str[MAX_RET] = {0}; |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1487 | char lynq_proto_str[MAX_RET] = {0}; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1488 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1489 | if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) { |
| 1490 | return -1; |
| 1491 | } |
| 1492 | |
| 1493 | *auth = convert_auth_from_key_mgmt(lynq_auth_str); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1494 | |
| 1495 | if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) { |
| 1496 | if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) { |
| 1497 | if (strcmp(lynq_proto_str, "RSN") == 0) { |
| 1498 | *auth = LYNQ_WIFI_AUTH_WPA2_PSK; |
| 1499 | } |
| 1500 | } |
| 1501 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1502 | return 0; |
| 1503 | } |
| 1504 | |
| 1505 | int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1506 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1507 | int pass_len, net_no, count, index; |
| 1508 | char lynq_tmp_cmd[300]={0}; |
| 1509 | int net_no_list[128]; |
| 1510 | lynq_wifi_auth_s net_auth; |
| 1511 | pass_len=strlen(password); |
| 1512 | if(pass_len < 8 || pass_len >= 64){ |
| 1513 | return -1; |
| 1514 | } |
| 1515 | |
| 1516 | CHECK_IDX(idx, CTRL_STA); |
| 1517 | |
| 1518 | net_no = -1; |
| 1519 | count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid); |
| 1520 | |
| 1521 | for (index=0; index < count; index++) { |
| 1522 | net_auth = -1; |
| 1523 | if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) { |
| 1524 | net_no = net_no_list[index]; |
| 1525 | break; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | if (net_no < 0) { |
| 1530 | return -1; |
| 1531 | } |
| 1532 | |
| 1533 | CHECK_WPA_CTRL(CTRL_STA); |
| 1534 | |
| 1535 | sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password); |
| 1536 | |
| 1537 | DO_OK_FAIL_REQUEST(lynq_tmp_cmd); |
| 1538 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 1539 | |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo |
| 1544 | |
| 1545 | FILE * fp; |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 1546 | int len, ret, network_len, i, ssid_len; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1547 | int count, index; |
| 1548 | char *split_lines[128] = {0}; |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 1549 | char *buff, *p, *ssid, *ssid_end_flag; |
| 1550 | char tmp_ssid[128]={0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1551 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1552 | network_len = 0; |
| 1553 | p = NULL; |
| 1554 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1555 | CHECK_IDX(idx, CTRL_STA); |
| 1556 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1557 | if (NULL == password) { |
| 1558 | printf("bad param\n"); |
| 1559 | return -1; |
| 1560 | } |
| 1561 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1562 | fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb"); |
| 1563 | if (NULL == fp) { |
| 1564 | printf("open file fail\n"); |
| 1565 | return -1; |
| 1566 | } |
| 1567 | |
| 1568 | buff = alloca(MAX_RET); |
| 1569 | fseek(fp, 0, SEEK_SET); |
| 1570 | len = fread(buff, 1, MAX_RET, fp); |
| 1571 | fclose(fp); |
| 1572 | |
| 1573 | for(index=0; index < len; index ++) { |
| 1574 | for(; index < len; index ++) { |
| 1575 | if (memcmp(buff + index, "network={", 9) != 0) { |
| 1576 | continue; |
| 1577 | } |
| 1578 | p = buff + index + 9; |
| 1579 | for (; index < len; index ++ ) { |
| 1580 | if (buff[index] != '}') { |
| 1581 | continue; |
| 1582 | } |
| 1583 | buff[index] = '\0'; |
| 1584 | break; |
| 1585 | } |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1586 | network_len = buff + index - p; |
| 1587 | break; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1588 | } |
| 1589 | |
qs.xiong | b3f26af | 2023-02-17 18:41:07 +0800 | [diff] [blame] | 1590 | if (p == NULL) |
| 1591 | return -1; |
| 1592 | |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 1593 | ssid = strstr(p, "ssid="); |
| 1594 | if (ssid != NULL) { |
| 1595 | ssid += strlen("ssid="); |
| 1596 | if (ssid[0] == '\"') { |
| 1597 | if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"') |
| 1598 | break; |
| 1599 | } |
| 1600 | else{ |
| 1601 | ssid_end_flag = strstr(ssid, "\n"); |
| 1602 | if (ssid_end_flag != NULL) |
| 1603 | { |
| 1604 | ssid_len = (ssid_end_flag - ssid) / 2; |
| 1605 | for(i=0; i<ssid_len; i++) |
| 1606 | { |
| 1607 | tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]); |
| 1608 | } |
| 1609 | if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0) |
| 1610 | break; |
| 1611 | } |
| 1612 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1613 | } |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 1614 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1615 | } |
| 1616 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1617 | if (index >= len || NULL == p || network_len <= 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1618 | return -1; |
| 1619 | } |
| 1620 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1621 | count = lynq_split(p, network_len, '\n', split_lines); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1622 | |
| 1623 | ret = -1; |
| 1624 | for(index=0; index < count; index++) { |
| 1625 | p = strstr(split_lines[index], "psk="); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1626 | if (p != NULL) { |
| 1627 | p += 4; |
| 1628 | if (*p == '\"') { |
| 1629 | p++; |
| 1630 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1631 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1632 | else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) { |
| 1633 | p += 9; |
| 1634 | if (*p == '\"') { |
| 1635 | p++; |
| 1636 | } |
| 1637 | } |
| 1638 | else { |
| 1639 | continue; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1640 | } |
| 1641 | |
qs.xiong | 1367346 | 2023-02-21 19:12:54 +0800 | [diff] [blame] | 1642 | if (*p == '\"') |
| 1643 | p++; |
| 1644 | strncpy(password, p, 64); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1645 | |
qs.xiong | 1367346 | 2023-02-21 19:12:54 +0800 | [diff] [blame] | 1646 | p = password; |
| 1647 | while(password - p < 64 && *password != '\0') { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1648 | if (*password == '\"') { |
| 1649 | *password = '\0'; |
| 1650 | break; |
| 1651 | } |
| 1652 | password++; |
| 1653 | } |
| 1654 | ret = 0; |
| 1655 | break; |
| 1656 | } //end for(index=0; index < count; index++) |
| 1657 | |
| 1658 | return ret; |
| 1659 | } |
| 1660 | |
| 1661 | static int inner_set_sta_ssid(int net_no, char *sta_ssid) |
| 1662 | { |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1663 | char lynq_wifi_ssid_cmd[80]={0}; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1664 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1665 | if (sta_ssid == NULL) { |
| 1666 | printf("sta_ssid is null\n"); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1667 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | CHECK_WPA_CTRL(CTRL_STA); |
| 1671 | |
| 1672 | sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid); |
| 1673 | |
| 1674 | DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd); |
| 1675 | // DO_OK_FAIL_REQUEST(cmd_save_config); |
| 1676 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1677 | return 0; |
| 1678 | |
| 1679 | } |
| 1680 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1681 | static int inner_sta_start_stop(int net_no, int start_flag, int save) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1682 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1683 | char lynq_disable_cmd[128]={0}; |
| 1684 | char lynq_select_cmd[128]={0}; |
| 1685 | |
| 1686 | CHECK_WPA_CTRL(CTRL_STA); |
| 1687 | |
| 1688 | if (save != 0) { |
you.chen | c29444e | 2022-06-07 18:01:16 +0800 | [diff] [blame] | 1689 | if (start_flag != 0) |
| 1690 | { |
| 1691 | sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no); |
| 1692 | DO_OK_FAIL_REQUEST(lynq_select_cmd); |
| 1693 | } |
| 1694 | else |
| 1695 | { |
| 1696 | sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no); |
| 1697 | DO_OK_FAIL_REQUEST(lynq_select_cmd); |
| 1698 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1699 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 1700 | } |
| 1701 | |
| 1702 | if (start_flag == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1703 | sprintf(lynq_disable_cmd,"DISCONNECT"); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1704 | DO_OK_FAIL_REQUEST(lynq_disable_cmd); |
| 1705 | } |
| 1706 | else { |
| 1707 | sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no); |
| 1708 | DO_OK_FAIL_REQUEST(lynq_select_cmd); |
| 1709 | } |
| 1710 | |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
| 1714 | int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid) |
| 1715 | { |
| 1716 | CHECK_IDX(idx, CTRL_STA); |
| 1717 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1718 | curr_status_info curr_state; |
| 1719 | ap_info_s ap_info; |
| 1720 | curr_state.ap = &ap_info; |
| 1721 | curr_state.state = NULL; |
| 1722 | |
| 1723 | if (0 == inner_get_status_info(CTRL_STA, &curr_state)) { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 1724 | strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid)); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1725 | return 0; |
| 1726 | } |
| 1727 | |
| 1728 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info) |
| 1732 | { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1733 | scan_info_s *scan_list = NULL; |
| 1734 | saved_ap_info_s *save_list = NULL; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1735 | int scan_len=0; |
| 1736 | int save_len=0; |
| 1737 | int best_index = -1; |
| 1738 | int best_scan_index = -1; |
| 1739 | int best_rssi = 0; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1740 | int i, j, ret; |
| 1741 | |
| 1742 | ret = -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1743 | |
| 1744 | CHECK_IDX(idx, CTRL_STA); |
| 1745 | if (info == NULL) { |
| 1746 | return -1; |
| 1747 | } |
| 1748 | |
| 1749 | curr_status_info curr_state; |
| 1750 | ap_info_s ap_info; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1751 | char status[64]; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1752 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1753 | memset(&ap_info, 0, sizeof (ap_info)); |
| 1754 | memset(status, 0, sizeof (status)); |
| 1755 | |
| 1756 | curr_state.ap = &ap_info; |
| 1757 | curr_state.state = status; |
| 1758 | |
| 1759 | if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1760 | memcpy(&info->base_info, &ap_info, sizeof (ap_info_s)); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1761 | if (strcmp(status, STATE_COMPLETED) == 0) |
| 1762 | { |
| 1763 | info->status = LYNQ_WIFI_AP_STATUS_ENABLE; |
| 1764 | } |
| 1765 | else |
| 1766 | { |
| 1767 | info->status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| 1768 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1769 | lynq_get_connect_ap_rssi(idx, &info->rssi); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1770 | lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1771 | return 0; |
| 1772 | } |
| 1773 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1774 | lynq_wifi_sta_start_scan(idx); |
| 1775 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1776 | if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1777 | if (NULL != scan_list) |
| 1778 | { |
| 1779 | free(scan_list); |
| 1780 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1781 | return -1; |
| 1782 | } |
| 1783 | |
| 1784 | if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1785 | if (NULL != scan_list) |
| 1786 | { |
| 1787 | free(scan_list); |
| 1788 | } |
| 1789 | if (NULL != save_list) |
| 1790 | { |
| 1791 | free(save_list); |
| 1792 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1793 | return -1; |
| 1794 | } |
| 1795 | |
| 1796 | for (i=0; i < save_len; i++) { |
| 1797 | for (j=0; j < scan_len; j++) { |
| 1798 | if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished |
| 1799 | && save_list[i].base_info.auth == scan_list[j].auth) { |
| 1800 | if (best_rssi == 0) { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1801 | best_index = i; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1802 | best_rssi = scan_list[j].rssi; |
| 1803 | } |
| 1804 | else if (best_rssi > scan_list[j].rssi) { |
| 1805 | best_index = i; |
| 1806 | best_scan_index = j; |
| 1807 | best_rssi = scan_list[j].rssi; |
| 1808 | } |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 1809 | strncpy(save_list[i].base_info.ap_mac, scan_list[j].mac, sizeof (save_list[i].base_info.ap_mac)); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1810 | break; |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | if (best_index >= 0) { |
| 1816 | memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s)); |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 1817 | inner_get_ip_by_mac( info->base_info.ap_mac, info->base_info.ap_ip, sizeof (info->base_info.ap_ip)); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1818 | info->status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| 1819 | info->rssi = best_rssi; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1820 | ret = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1821 | } |
| 1822 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1823 | if (NULL != scan_list) |
| 1824 | { |
| 1825 | free(scan_list); |
| 1826 | } |
| 1827 | if (NULL != save_list) |
| 1828 | { |
| 1829 | free(save_list); |
| 1830 | } |
| 1831 | |
| 1832 | return ret; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password) |
| 1836 | { |
| 1837 | char lynq_auth_cmd[64]={0}; |
| 1838 | char lynq_ket_mgmt_cmd[64]={0}; |
| 1839 | char lynq_pairwise_cmd[64]={0}; |
| 1840 | char lynq_psk_cmd[64]={0}; |
| 1841 | |
| 1842 | CHECK_WPA_CTRL(CTRL_STA); |
| 1843 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1844 | switch(auth){ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1845 | case LYNQ_WIFI_AUTH_OPEN: |
| 1846 | { |
| 1847 | sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1848 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1849 | DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| 1850 | // DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1851 | break; |
| 1852 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1853 | case LYNQ_WIFI_AUTH_WPA_PSK: |
| 1854 | case LYNQ_WIFI_AUTH_WPA2_PSK: |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1855 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1856 | if (auth == LYNQ_WIFI_AUTH_WPA_PSK) { |
| 1857 | sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no); |
| 1858 | } |
| 1859 | else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) { |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1860 | sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1861 | } |
| 1862 | sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no); |
| 1863 | sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1864 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1865 | DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| 1866 | DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd); |
| 1867 | DO_OK_FAIL_REQUEST(lynq_pairwise_cmd); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1868 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1869 | if (password != NULL) { |
| 1870 | sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password); |
| 1871 | DO_OK_FAIL_REQUEST(lynq_psk_cmd); |
| 1872 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1873 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1874 | // DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1875 | break; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1876 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1877 | default: |
| 1878 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1879 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1880 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1881 | return 0; |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1882 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1883 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1884 | static int inner_get_curr_net_no(int interface) { |
| 1885 | curr_status_info curr_state; |
| 1886 | curr_state.ap = NULL; |
| 1887 | curr_state.state = NULL; |
| 1888 | |
| 1889 | if (0 != inner_get_status_info(interface, &curr_state)) { |
| 1890 | return -1; |
| 1891 | } |
| 1892 | |
| 1893 | return curr_state.net_no; |
| 1894 | } |
| 1895 | |
| 1896 | int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1897 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1898 | int net_no; |
| 1899 | CHECK_IDX(idx, CTRL_STA); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1900 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1901 | net_no = inner_get_curr_net_no(CTRL_STA); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1902 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1903 | if (net_no < 0) { |
| 1904 | return -1; |
| 1905 | } |
| 1906 | |
| 1907 | return inner_get_network_auth(CTRL_STA, net_no, auth); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1908 | } |
| 1909 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1910 | int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1911 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1912 | int count, net_no, index; |
| 1913 | int net_no_list[128]; |
| 1914 | lynq_wifi_auth_s net_auth; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1915 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1916 | if (ssid == NULL || *ssid == '\0') { |
| 1917 | printf("bad ssid\n"); |
| 1918 | return -1; |
| 1919 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1920 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1921 | if (LYNQ_WIFI_AUTH_OPEN != auth) { |
| 1922 | if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) { |
| 1923 | printf("bad password\n"); |
| 1924 | return -1; |
| 1925 | } |
| 1926 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1927 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1928 | CHECK_IDX(idx, CTRL_STA); |
| 1929 | |
| 1930 | net_no = -1; |
| 1931 | count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid); |
| 1932 | |
| 1933 | for (index=0; index < count; index++) { |
| 1934 | net_auth = -1; |
| 1935 | if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) { |
| 1936 | net_no = net_no_list[index]; |
| 1937 | break; |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | if (net_no < 0) { |
| 1942 | net_no = lynq_add_network(CTRL_STA); |
| 1943 | if (net_no == -1) { |
| 1944 | return -1; |
| 1945 | } |
| 1946 | |
| 1947 | printf("net no is %d\n", net_no); |
| 1948 | if (0 != inner_set_sta_ssid(net_no, ssid)) { |
| 1949 | return -1; |
| 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) { |
| 1954 | return -1; |
| 1955 | } |
| 1956 | |
| 1957 | return inner_sta_start_stop(net_no, 1, 1); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1958 | } |
| 1959 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1960 | int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1961 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1962 | ap_info_s ap; |
| 1963 | curr_status_info curr_state; |
| 1964 | ap.ap_ssid[0] = '\0'; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1965 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1966 | if (ssid == NULL || *ssid == '\0') { |
| 1967 | return -1; |
| 1968 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1969 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1970 | CHECK_IDX(idx, CTRL_STA); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1971 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1972 | curr_state.ap = ≈ |
you.chen | b4b121c | 2022-05-06 17:50:16 +0800 | [diff] [blame] | 1973 | curr_state.state = NULL; |
| 1974 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1975 | if (inner_get_status_info(CTRL_STA, &curr_state) != 0) { |
| 1976 | return 0; |
| 1977 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1978 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1979 | if (strcmp(ap.ap_ssid, ssid) != 0) { |
| 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | return inner_sta_start_stop(curr_state.net_no, 0, 0); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1984 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1985 | |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1986 | int lynq_wifi_sta_start(lynq_wifi_index_e idx) |
| 1987 | { |
qs.xiong | ad2f89d | 2023-01-18 13:17:41 +0800 | [diff] [blame] | 1988 | // const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf"; |
| 1989 | // const char *lynq_reconnect_cmd = "RECONNECT"; |
| 1990 | const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all"; |
| 1991 | const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect"; |
| 1992 | // const char *lynq_first_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 remove_net all"; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1993 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1994 | CHECK_IDX(idx, CTRL_STA); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1995 | CHECK_WPA_CTRL(CTRL_STA); |
| 1996 | |
| 1997 | system("connmanctl enable wifi"); |
| 1998 | |
qs.xiong | ad2f89d | 2023-01-18 13:17:41 +0800 | [diff] [blame] | 1999 | if (system("ifconfig | grep -q wlan0") != 0) |
| 2000 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2001 | return -1; |
| 2002 | } |
qs.xiong | 9c99fa9 | 2022-03-15 08:03:26 -0400 | [diff] [blame] | 2003 | |
qs.xiong | ad2f89d | 2023-01-18 13:17:41 +0800 | [diff] [blame] | 2004 | // DO_OK_FAIL_REQUEST(cmd_remove_all); |
| 2005 | // system(lynq_first_sta_cmd); |
| 2006 | // system(lynq_reconfigure_cmd); |
| 2007 | // DO_OK_FAIL_REQUEST(lynq_reconnect_cmd); |
| 2008 | system(lynq_enable_sta_cmd); |
| 2009 | system(lynq_reconnect_cmd); |
| 2010 | // DO_OK_FAIL_REQUEST(lynq_reconnect_cmd); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 2011 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 2012 | } |
| 2013 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2014 | int lynq_wifi_sta_stop(lynq_wifi_index_e idx) |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2015 | { |
qs.xiong | ad2f89d | 2023-01-18 13:17:41 +0800 | [diff] [blame] | 2016 | // char lynq_disable_network_cmd[MAX_CMD]; |
| 2017 | // curr_status_info curr_state; |
| 2018 | // ap_info_s ap_info; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2019 | |
qs.xiong | ad2f89d | 2023-01-18 13:17:41 +0800 | [diff] [blame] | 2020 | const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all"; |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 2021 | CHECK_IDX(idx, CTRL_STA); |
| 2022 | CHECK_WPA_CTRL(CTRL_STA); |
| 2023 | |
qs.xiong | ad2f89d | 2023-01-18 13:17:41 +0800 | [diff] [blame] | 2024 | // curr_state.ap = &ap_info; |
| 2025 | // curr_state.state = NULL; |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 2026 | |
qs.xiong | ad2f89d | 2023-01-18 13:17:41 +0800 | [diff] [blame] | 2027 | // if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) { |
| 2028 | // return 0; |
| 2029 | // } |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 2030 | |
qs.xiong | ad2f89d | 2023-01-18 13:17:41 +0800 | [diff] [blame] | 2031 | // sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no); |
| 2032 | // DO_OK_FAIL_REQUEST(lynq_disable_network_cmd); |
| 2033 | system(lynq_disable_sta_cmd); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 2034 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 2035 | |
| 2036 | return 0; |
| 2037 | // return system("connmanctl disable wifi"); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 2038 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 2039 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2040 | //static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) { |
| 2041 | // int i, count; |
| 2042 | // char *p; |
| 2043 | // const char * FLAG_SSID = "ssid="; |
| 2044 | // const char * FLAG_SBSID = "bssid="; |
| 2045 | // const char * FLAG_KEY_MGMT = "key_mgmt="; |
| 2046 | // const char * FLAG_FREQ = "freq="; |
| 2047 | // char lynq_sta_cmd[MAX_CMD]; |
| 2048 | // char *split_lines[128] = {0}; |
| 2049 | |
| 2050 | // CHECK_WPA_CTRL(CTRL_AP); |
| 2051 | |
| 2052 | // sprintf(lynq_sta_cmd, "STA %s", bssid); |
| 2053 | |
| 2054 | // DO_REQUEST(lynq_sta_cmd); |
| 2055 | |
| 2056 | // count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| 2057 | |
| 2058 | // for(i=0; i < count; i++) { |
| 2059 | // p = strstr(split_lines[i], FLAG_SSID); |
| 2060 | // if (p != NULL) { |
| 2061 | // strcpy(ap->ap_ssid, p + strlen(FLAG_SSID)); |
| 2062 | // continue; |
| 2063 | // } |
| 2064 | // } |
| 2065 | |
| 2066 | // lynq_get_interface_ip(idx, ap->ap_ip); |
| 2067 | // lynq_ap_password_set(idx, ap->psw); |
| 2068 | |
| 2069 | // return 0; |
| 2070 | //} |
| 2071 | |
| 2072 | static int inner_get_status_info_ap (int interface, ap_info_s *ap) { |
| 2073 | curr_status_info curr_state; |
| 2074 | curr_state.ap = ap; |
| 2075 | curr_state.state = NULL; |
| 2076 | return inner_get_status_info(interface, &curr_state); |
| 2077 | } |
| 2078 | |
| 2079 | int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len) |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2080 | { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 2081 | int index, line_count; |
| 2082 | device_info_s *dev_info; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2083 | const char *lynq_first_sta_cmd = "STA-FIRST"; |
| 2084 | char lynq_next_sta_cmd[MAX_CMD]; |
| 2085 | char *bssid[1024] = {0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2086 | char *split_lines[128] = {0}; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2087 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2088 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2089 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2090 | CHECK_WPA_CTRL(CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2091 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2092 | // ap_info_s * tmp_ap; |
| 2093 | // device_info_s * tmp_list; |
| 2094 | if (ap == NULL || list == NULL || len == NULL) { |
| 2095 | printf("bad input param"); |
| 2096 | return -1; |
| 2097 | } |
| 2098 | |
| 2099 | // ap = &tmp_ap; |
| 2100 | // list = &tmp_list; |
| 2101 | *ap = malloc(sizeof (ap_info_s)); |
| 2102 | |
| 2103 | if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) { |
| 2104 | return -1; |
| 2105 | } |
| 2106 | |
| 2107 | lynq_get_interface_ip(idx, (*ap)->ap_ip); |
| 2108 | lynq_ap_password_get(idx, (*ap)->psw); |
| 2109 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2110 | DO_REQUEST(lynq_first_sta_cmd); |
| 2111 | |
| 2112 | index = 0; |
| 2113 | while (reply_len > 0) { |
| 2114 | if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 2115 | break; |
| 2116 | } |
| 2117 | line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| 2118 | bssid[index] = malloc(strlen(split_lines[0]) + 1); |
| 2119 | strcpy(bssid[index], split_lines[0]); |
| 2120 | index++; |
| 2121 | sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]); |
| 2122 | reply_len = MAX_RET; |
| 2123 | cmd_reply[0] = '\0'; |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 2124 | ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2125 | if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 2126 | printf("run %s fail \n", lynq_next_sta_cmd); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2127 | break; |
| 2128 | } |
| 2129 | } |
| 2130 | |
| 2131 | *len = index; |
| 2132 | |
| 2133 | *list = malloc(sizeof(device_info_s) * (*len)); |
| 2134 | for (index=0; index < *len; index++) { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 2135 | dev_info = &(*list)[index]; |
| 2136 | memset(dev_info, 0, sizeof(device_info_s)); |
| 2137 | strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac)); |
| 2138 | inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip)); |
| 2139 | inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname); |
| 2140 | dev_info->status = LYNQ_WIFI_STATUS_CONNECT; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2141 | free(bssid[index]); |
| 2142 | } |
| 2143 | |
| 2144 | return 0; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2145 | } |
| 2146 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2147 | int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len) |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2148 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2149 | int i, count, index, count_words; |
| 2150 | const char *lynq_scan_result_cmd = "SCAN_RESULTS"; |
| 2151 | char *split_lines[128] = {0}; |
| 2152 | char *split_words[128] = {0}; |
| 2153 | scan_info_s * p; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2154 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2155 | if (list == NULL || len == NULL) { |
| 2156 | return -1; |
| 2157 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2158 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2159 | for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++) |
| 2160 | { |
| 2161 | usleep(100 * 1000); |
| 2162 | } |
| 2163 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2164 | CHECK_IDX(idx, CTRL_STA); |
| 2165 | |
| 2166 | CHECK_WPA_CTRL(CTRL_STA); |
| 2167 | |
| 2168 | DO_REQUEST(lynq_scan_result_cmd); |
| 2169 | |
| 2170 | count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| 2171 | *len = count - 1; |
| 2172 | *list = malloc(sizeof (scan_info_s) * *len); |
| 2173 | |
| 2174 | count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header |
| 2175 | for (index=0; index <count_words; index++) { |
| 2176 | printf("----header: %s\n", split_words[index]); |
| 2177 | } |
| 2178 | |
| 2179 | for(index = 1;index < count; index++) { |
| 2180 | printf("---- %s\n",split_lines[index]); |
| 2181 | count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words); |
| 2182 | if (count_words < 4) |
| 2183 | continue; |
| 2184 | printf("count: %d, %s\n", count_words, split_words[0]); |
| 2185 | //bssid / frequency / signal level / flags / ssid |
| 2186 | p = (*list) + index - 1; |
| 2187 | strcpy(p->mac, split_words[0]); |
| 2188 | p->band = convert_band_from_freq(atoi(split_words[1])); |
| 2189 | p->rssi = -1 * atoi( split_words[2]); |
| 2190 | p->auth = convert_max_auth_from_flag(split_words[3]); |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 2191 | inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid)); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | return 0; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2195 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2196 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2197 | int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth) |
| 2198 | { |
| 2199 | int count, net_no, index; |
| 2200 | int net_no_list[128]; |
| 2201 | lynq_wifi_auth_s net_auth; |
| 2202 | char lynq_remove_cmd[MAX_CMD]; |
| 2203 | |
| 2204 | if (ssid == NULL || *ssid == '\0') { |
| 2205 | printf("bad ssid\n"); |
| 2206 | return -1; |
| 2207 | } |
| 2208 | |
| 2209 | CHECK_IDX(idx, CTRL_STA); |
| 2210 | |
| 2211 | CHECK_WPA_CTRL(CTRL_STA); |
| 2212 | |
| 2213 | net_no = -1; |
| 2214 | count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid); |
| 2215 | |
| 2216 | for (index=0; index < count; index++) { |
| 2217 | net_auth = -1; |
| 2218 | if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) { |
| 2219 | net_no = net_no_list[index]; |
| 2220 | break; |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | if (net_no < 0) { |
| 2225 | return 0; |
| 2226 | } |
| 2227 | |
| 2228 | sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no); |
| 2229 | |
| 2230 | DO_OK_FAIL_REQUEST(lynq_remove_cmd); |
| 2231 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 2232 | |
| 2233 | return 0; |
| 2234 | } |
| 2235 | |
| 2236 | int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len) |
| 2237 | { |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 2238 | int count, index; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2239 | int net_no_list[128]; |
| 2240 | char freq[16]; |
| 2241 | |
| 2242 | if (list == NULL || len == NULL) { |
| 2243 | printf("bad param\n"); |
| 2244 | return -1; |
| 2245 | } |
| 2246 | |
| 2247 | CHECK_IDX(idx, CTRL_STA); |
| 2248 | |
| 2249 | // CHECK_WPA_CTRL(CTRL_STA); |
| 2250 | |
| 2251 | count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL); |
| 2252 | printf("count is %d\n", count); |
| 2253 | |
| 2254 | *list = malloc(sizeof (saved_ap_info_s) * count); |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 2255 | memset(*list, 0, sizeof (saved_ap_info_s) * count); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2256 | *len = count; |
| 2257 | |
| 2258 | for (index=0; index < count; index++) { |
| 2259 | printf("to get ssid %d\n", index); |
| 2260 | inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2261 | inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2262 | inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2263 | if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) { |
| 2264 | (*list)[index].base_info.band = convert_band_from_freq(atoi(freq)); |
| 2265 | } |
| 2266 | else { |
| 2267 | (*list)[index].base_info.band = -1; |
| 2268 | } |
| 2269 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 2270 | lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | return 0; |
| 2274 | } |
| 2275 | |
| 2276 | int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx) |
| 2277 | { |
| 2278 | const char *lynq_scan_cmd = "SCAN"; |
| 2279 | |
| 2280 | CHECK_IDX(idx, CTRL_STA); |
| 2281 | |
| 2282 | CHECK_WPA_CTRL(CTRL_STA); |
| 2283 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2284 | g_sta_scan_finish_flag = 0; |
qs.xiong | b3f26af | 2023-02-17 18:41:07 +0800 | [diff] [blame] | 2285 | DO_REQUEST(lynq_scan_cmd); |
| 2286 | if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 ) { |
| 2287 | return 0; |
| 2288 | } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { |
| 2289 | g_sta_scan_finish_flag = 1; |
| 2290 | return -1; |
| 2291 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2292 | |
| 2293 | return 0; |
| 2294 | } |
| 2295 | |
| 2296 | int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) { |
| 2297 | if (cb == NULL) { |
| 2298 | return -1; |
| 2299 | } |
| 2300 | |
| 2301 | g_ap_callback_priv = priv; |
| 2302 | g_ap_callback_func = cb; |
| 2303 | |
| 2304 | return 0; |
| 2305 | } |
| 2306 | |
| 2307 | int lynq_unreg_ap_event_callback(void * priv) { |
| 2308 | if (g_ap_callback_priv == priv) { |
| 2309 | g_ap_callback_func = NULL; |
| 2310 | g_ap_callback_priv = NULL; |
| 2311 | return 0; |
| 2312 | } |
| 2313 | return -1; |
| 2314 | } |
| 2315 | |
| 2316 | int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){ |
| 2317 | if (cb == NULL) { |
| 2318 | return -1; |
| 2319 | } |
| 2320 | |
| 2321 | g_sta_callback_priv = priv; |
| 2322 | g_sta_callback_func = cb; |
| 2323 | |
| 2324 | return 0; |
| 2325 | } |
| 2326 | |
| 2327 | int lynq_unreg_sta_event_callback(void * priv) { |
| 2328 | if (g_sta_callback_priv == priv) { |
| 2329 | g_sta_callback_func = NULL; |
| 2330 | g_sta_callback_priv = NULL; |
| 2331 | return 0; |
| 2332 | } |
| 2333 | return -1; |
| 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | static int inner_get_status_info_state (int interface, char *state) { |
| 2338 | curr_status_info curr_state; |
| 2339 | curr_state.ap = NULL; |
| 2340 | curr_state.state = state; |
| 2341 | return inner_get_status_info(interface, &curr_state); |
| 2342 | } |
| 2343 | |
| 2344 | int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status) |
| 2345 | { |
| 2346 | char state[MAX_CMD]; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2347 | CHECK_IDX(idx, CTRL_AP); |
| 2348 | |
| 2349 | if (inner_get_status_info_state(CTRL_AP, state) != 0) { |
| 2350 | *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| 2351 | return 0; |
| 2352 | } |
| 2353 | |
| 2354 | if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) { |
| 2355 | *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE; |
| 2356 | } |
| 2357 | else { |
| 2358 | *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| 2359 | } |
| 2360 | |
| 2361 | return 0; |
| 2362 | } |
| 2363 | |
| 2364 | int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) { |
| 2365 | char state[MAX_CMD]; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2366 | CHECK_IDX(idx, CTRL_STA); |
| 2367 | |
| 2368 | if (inner_get_status_info_state(CTRL_STA, state) != 0) { |
| 2369 | *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE; |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
| 2373 | if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) { |
| 2374 | *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE; |
| 2375 | } |
| 2376 | else { |
| 2377 | *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE; |
| 2378 | } |
| 2379 | |
| 2380 | return 0; |
| 2381 | } |
| 2382 | |
| 2383 | int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) { |
| 2384 | // CHECK_IDX(idx, CTRL_AP); |
| 2385 | // int ret = 0; |
| 2386 | // size_t reply_len = MAX_RET; |
| 2387 | // char cmd_reply[MAX_RET]={0}; |
| 2388 | // const char * cmd_str = "GET country"; |
| 2389 | // struct wpa_ctrl *s_lynq_wpa_ctrl = NULL; |
| 2390 | // do{ |
| 2391 | // if (NULL == s_lynq_wpa_ctrl) { |
| 2392 | // s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd"); |
| 2393 | // if (NULL == s_lynq_wpa_ctrl ) { |
| 2394 | // printf("wpa_ctrl_open fail\n"); |
| 2395 | // return -1; |
| 2396 | // } |
| 2397 | // } |
| 2398 | // }while(0); |
| 2399 | |
| 2400 | // do { |
| 2401 | // reply_len = MAX_RET; |
| 2402 | // cmd_reply[0] = '\0'; |
| 2403 | // printf("to call [%s]\n", cmd_str); |
you.chen | d2fef3f | 2023-02-13 10:50:35 +0800 | [diff] [blame] | 2404 | // ret = local_wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2405 | // if (ret != 0) { |
| 2406 | // printf("call ##cmd_str fail %d\n", ret); |
| 2407 | // return ret; |
| 2408 | // } |
| 2409 | // cmd_reply[reply_len+1] = '\0'; |
| 2410 | // printf("cmd replay [ %s ]\n", cmd_reply); |
| 2411 | // }while(0); |
| 2412 | |
| 2413 | FILE *fp; |
| 2414 | size_t i = 0; |
| 2415 | char lynq_cmd_ret[MAX_RET]={0}; |
| 2416 | |
| 2417 | // CHECK_IDX(idx, CTRL_AP); |
| 2418 | |
| 2419 | if((fp=popen("wl country","r"))==NULL) |
| 2420 | { |
| 2421 | perror("popen error!"); |
| 2422 | return -1; |
| 2423 | } |
| 2424 | if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0) |
| 2425 | { |
| 2426 | perror("fread fail!"); |
| 2427 | return -1; |
| 2428 | } |
| 2429 | |
| 2430 | for(i=0; i < strlen(lynq_cmd_ret); i++) { |
| 2431 | if (lynq_cmd_ret[i] == ' ') { |
| 2432 | lynq_cmd_ret[i] = '\0'; |
| 2433 | break; |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | strcpy(country_code,lynq_cmd_ret); |
| 2438 | printf("---country code %s\n", country_code); |
| 2439 | |
| 2440 | int ret=pclose(fp); |
| 2441 | if(ret==-1) |
| 2442 | { |
| 2443 | perror("close file faild"); |
| 2444 | } |
| 2445 | |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
| 2449 | int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) { |
| 2450 | // const char * cmd_str = "GET country"; |
| 2451 | // CHECK_IDX(idx, CTRL_AP); |
| 2452 | // CHECK_WPA_CTRL(CTRL_STA); |
| 2453 | |
| 2454 | // DO_REQUEST(cmd_str); |
| 2455 | // printf("result %s\n", cmd_reply); |
| 2456 | |
| 2457 | if (country_code == NULL || *country_code == '\0') { |
| 2458 | printf("bad country code\n"); |
| 2459 | return -1; |
| 2460 | } |
| 2461 | |
| 2462 | char lynq_country_cmd[MAX_CMD]; |
| 2463 | sprintf(lynq_country_cmd, "wl country %s", country_code); |
| 2464 | if (system(lynq_country_cmd) == 0) { |
| 2465 | return 0; |
| 2466 | } |
| 2467 | |
| 2468 | return -1; |
| 2469 | } |
| 2470 | |
| 2471 | int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac) |
| 2472 | { |
| 2473 | if (mac == NULL) { |
| 2474 | return -1; |
| 2475 | } |
| 2476 | |
| 2477 | CHECK_IDX(idx, CTRL_STA); |
| 2478 | ap_info_s ap; |
| 2479 | ap.ap_mac[0] = '\0'; |
| 2480 | |
| 2481 | if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) { |
| 2482 | return -1; |
| 2483 | } |
| 2484 | strcpy(mac, ap.ap_mac); |
| 2485 | |
| 2486 | return 0; |
| 2487 | } |
| 2488 | |
| 2489 | int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip) |
| 2490 | { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2491 | struct ifaddrs *ifaddr_header, *ifaddr; |
| 2492 | struct in_addr * ifa; |
| 2493 | const char * ifaName = "wlan0"; |
| 2494 | if (ip == NULL) |
| 2495 | { |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2496 | return -1; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2497 | } |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2498 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2499 | if (idx == 1) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 2500 | ifaName = "tether"; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2501 | } |
| 2502 | else if (idx != 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2503 | return -1; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2504 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2505 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2506 | if (getifaddrs(&ifaddr_header) == -1) |
| 2507 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2508 | perror("getifaddrs"); |
| 2509 | return -1; |
| 2510 | //exit(EXIT_FAILURE); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2511 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2512 | |
| 2513 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2514 | for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next) |
| 2515 | { |
| 2516 | if (ifaddr->ifa_addr == NULL) |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2517 | continue; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2518 | if((strcmp(ifaddr->ifa_name,ifaName)==0)) |
| 2519 | { |
| 2520 | if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4 |
| 2521 | { |
| 2522 | // is a valid IP4 Address |
| 2523 | ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr; |
| 2524 | inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN); |
| 2525 | printf("%s IP Address %s/n", ifaddr->ifa_name, ip); |
| 2526 | freeifaddrs(ifaddr_header); |
| 2527 | printf("ip %s\n", ip); |
| 2528 | return 0; |
| 2529 | } |
| 2530 | } |
| 2531 | } |
qs.xiong | c4f007c | 2023-02-08 18:16:58 +0800 | [diff] [blame] | 2532 | freeifaddrs(ifaddr_header); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2533 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac) |
| 2537 | { |
| 2538 | int count; |
| 2539 | size_t i; |
| 2540 | char *split_words[128] = {0}; |
| 2541 | const char *lynq_get_mac_cmd = "DRIVER MACADDR"; |
| 2542 | |
| 2543 | CHECK_WPA_CTRL(idx); |
| 2544 | |
| 2545 | DO_REQUEST(lynq_get_mac_cmd); |
| 2546 | |
| 2547 | if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 2548 | return -1; |
| 2549 | } |
| 2550 | |
| 2551 | count = lynq_split(cmd_reply, reply_len, '=', split_words); |
| 2552 | |
| 2553 | if (count < 2) { |
| 2554 | return -1; |
| 2555 | } |
| 2556 | |
| 2557 | for (i=0; i < strlen(split_words[1]); i++ ) { |
| 2558 | if (split_words[1][i] != ' ') { |
| 2559 | break; |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | strcpy(mac, split_words[1] + i); |
| 2564 | |
| 2565 | return 0; |
| 2566 | } |
| 2567 | |
| 2568 | int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi) |
| 2569 | { |
| 2570 | // int count; |
| 2571 | // char *split_words[128] = {0}; |
| 2572 | // const char *lynq_get_rssi_cmd = "DRIVER RSSI"; |
| 2573 | |
| 2574 | // if (rssi == NULL) { |
| 2575 | // return -1; |
| 2576 | // } |
| 2577 | |
| 2578 | // CHECK_IDX(idx, CTRL_STA); |
| 2579 | |
| 2580 | // CHECK_WPA_CTRL(CTRL_STA); |
| 2581 | |
| 2582 | // DO_REQUEST(lynq_get_rssi_cmd); |
| 2583 | |
| 2584 | // if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 2585 | // return -1; |
| 2586 | // } |
| 2587 | |
| 2588 | // count = lynq_split(cmd_reply, reply_len, ' ', split_words); |
| 2589 | |
| 2590 | // if (count < 2) { |
| 2591 | // return -1; |
| 2592 | // } |
| 2593 | |
| 2594 | // *rssi = atoi(split_words[1]) * -1; |
| 2595 | |
| 2596 | FILE *fp; |
| 2597 | size_t i = 0; |
| 2598 | char lynq_cmd_ret[MAX_RET]={0}; |
| 2599 | |
| 2600 | // CHECK_IDX(idx, CTRL_AP); |
qs.xiong | ff0ae0f | 2022-10-11 15:47:14 +0800 | [diff] [blame] | 2601 | /*******change other cmd to get rssi******* |
| 2602 | * |
| 2603 | *wl rssi ---> wl -i wlan0 rssi |
| 2604 | * |
| 2605 | ***** change by qs.xiong 20221011*******/ |
| 2606 | if((fp=popen("wl -i wlan0 rssi","r"))==NULL) |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2607 | { |
| 2608 | perror("popen error!"); |
| 2609 | return -1; |
| 2610 | } |
| 2611 | if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0) |
| 2612 | { |
| 2613 | perror("fread fail!"); |
| 2614 | return -1; |
| 2615 | } |
you.chen | 9f17e4d | 2022-06-06 17:18:18 +0800 | [diff] [blame] | 2616 | *rssi = atoi(lynq_cmd_ret) * -1; |
qs.xiong | ff0ae0f | 2022-10-11 15:47:14 +0800 | [diff] [blame] | 2617 | /****** if got rssi is 0,means sta didn't connected any device****/ |
| 2618 | if(*rssi == 0) |
| 2619 | { |
| 2620 | printf("sta didn't connected any ap device,please check connection\n"); |
| 2621 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2622 | |
| 2623 | return 0; |
| 2624 | } |
| 2625 | |
| 2626 | int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band) |
| 2627 | { |
| 2628 | if (band == NULL) { |
| 2629 | return -1; |
| 2630 | } |
| 2631 | |
| 2632 | CHECK_IDX(idx, CTRL_STA); |
| 2633 | ap_info_s ap; |
| 2634 | ap.band = -1; |
| 2635 | |
| 2636 | if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) { |
| 2637 | return -1; |
| 2638 | } |
| 2639 | *band = ap.band; |
| 2640 | |
| 2641 | return 0; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2642 | } |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2643 | |
| 2644 | int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip) |
| 2645 | { |
qs.xiong | e4cbf1c | 2023-02-28 18:22:49 +0800 | [diff] [blame^] | 2646 | char bssid[1024] = {0}; |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2647 | |
| 2648 | if (ip == NULL) |
| 2649 | { |
| 2650 | printf("invalid param"); |
| 2651 | return -1; |
| 2652 | } |
| 2653 | |
| 2654 | CHECK_IDX(idx, CTRL_STA); |
| 2655 | |
qs.xiong | e4cbf1c | 2023-02-28 18:22:49 +0800 | [diff] [blame^] | 2656 | if (lynq_get_connect_ap_mac(idx, bssid) != 0) |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2657 | { |
| 2658 | return -1; |
| 2659 | } |
qs.xiong | e4cbf1c | 2023-02-28 18:22:49 +0800 | [diff] [blame^] | 2660 | |
| 2661 | return inner_get_ip_by_mac(bssid, ip, 32); //better input by user |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2662 | } |
| 2663 | |
qs.xiong | 026c5c7 | 2022-10-17 11:15:45 +0800 | [diff] [blame] | 2664 | int lynq_ap_connect_num(int sta_number) |
| 2665 | { |
| 2666 | char lynq_limit_cmd[32]={0}; |
| 2667 | int ret; |
| 2668 | if((sta_number < 1 ) && (sta_number > 15)){ |
| 2669 | printf("sta_number: not in range\n",sta_number); |
| 2670 | return -1; |
| 2671 | } |
| 2672 | sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number); |
| 2673 | ret = system(lynq_limit_cmd); |
| 2674 | if(ret != 0){ |
| 2675 | printf("cmd of limit ap devices number error\n"); |
| 2676 | } |
| 2677 | return 0; |
| 2678 | } |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2679 | |
qs.xiong | 7790555 | 2022-10-17 11:19:57 +0800 | [diff] [blame] | 2680 | int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode) |
| 2681 | { |
| 2682 | |
| 2683 | char lynq_wifi_acs_cmd[128]={0}; |
| 2684 | char lynq_cmd_mode[128]={0}; |
| 2685 | char lynq_cmd_slect[128]={0}; |
| 2686 | |
| 2687 | if((acs_mode != 2) && (acs_mode != 5)){ |
| 2688 | PRINT_AND_RETURN_VALUE("set acs_mode is error",-1); |
| 2689 | } |
| 2690 | |
| 2691 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { |
| 2692 | return -1; |
| 2693 | } |
| 2694 | |
| 2695 | CHECK_IDX(idx, CTRL_AP); |
| 2696 | |
| 2697 | CHECK_WPA_CTRL(CTRL_AP); |
| 2698 | |
| 2699 | sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode); |
| 2700 | sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0); |
| 2701 | sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0); |
| 2702 | |
| 2703 | DO_OK_FAIL_REQUEST(cmd_disconnect); |
| 2704 | DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd); |
| 2705 | DO_OK_FAIL_REQUEST(lynq_cmd_mode); |
| 2706 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 2707 | DO_OK_FAIL_REQUEST(lynq_cmd_slect); |
| 2708 | |
| 2709 | return 0; |
| 2710 | } |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 2711 | //you.chen add for tv-box start |
| 2712 | static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) { |
| 2713 | FILE *fp; |
| 2714 | //printf("to exec cmd:%s\n", str_cmd); |
| 2715 | if((fp=popen(str_cmd,"r"))==NULL) |
| 2716 | { |
| 2717 | perror("popen error!"); |
| 2718 | return -1; |
| 2719 | } |
| 2720 | if((fread(str_cmd_ret,max_len,1,fp))<0) |
| 2721 | { |
| 2722 | perror("fread fail!"); |
| 2723 | fclose(fp); |
| 2724 | return -1; |
| 2725 | } |
| 2726 | fclose(fp); |
| 2727 | return 0; |
| 2728 | } |
| 2729 | |
| 2730 | static int get_netmask_length(const char* mask) |
| 2731 | { |
| 2732 | int masklen=0, i=0; |
| 2733 | int netmask=0; |
| 2734 | |
| 2735 | if(mask == NULL) |
| 2736 | { |
| 2737 | return 0; |
| 2738 | } |
| 2739 | |
| 2740 | struct in_addr ip_addr; |
| 2741 | if( inet_aton(mask, &ip_addr) ) |
| 2742 | { |
| 2743 | netmask = ntohl(ip_addr.s_addr); |
| 2744 | }else{ |
| 2745 | netmask = 0; |
| 2746 | return 0; |
| 2747 | } |
| 2748 | |
| 2749 | while(0 == (netmask & 0x01) && i<32) |
| 2750 | { |
| 2751 | i++; |
| 2752 | netmask = netmask>>1; |
| 2753 | } |
| 2754 | masklen = 32-i; |
| 2755 | return masklen; |
| 2756 | } |
| 2757 | |
| 2758 | static int get_tether_route_str(char *str_cmd_ret, size_t max_len) { |
| 2759 | int mask_len; |
| 2760 | char *p; |
| 2761 | char tmp[64] = {0}; |
| 2762 | if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0) |
| 2763 | return -1; |
| 2764 | p = strstr(str_cmd_ret, "Mask:"); |
| 2765 | if (p == NULL) |
| 2766 | return -1; |
| 2767 | mask_len = get_netmask_length(p + 5); |
| 2768 | if (mask_len == 0) |
| 2769 | return -1; |
| 2770 | p = strstr(str_cmd_ret, "inet addr:"); |
| 2771 | if (p == NULL) |
| 2772 | return -1; |
| 2773 | strcpy(tmp, p + 10); |
| 2774 | p = strstr(tmp, " "); |
| 2775 | if (p != NULL) |
| 2776 | *p = '\0'; |
| 2777 | sprintf(str_cmd_ret, "%s/%d", tmp, mask_len); |
| 2778 | return 0; |
| 2779 | } |
| 2780 | |
| 2781 | static void GBWWatchThreadProc() { |
| 2782 | int i,n, nloop, nmax, ncheckcount, nidlecount; |
| 2783 | unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes; |
| 2784 | unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop; |
| 2785 | unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed; |
| 2786 | char *results[16] = {0}; |
| 2787 | char str_cmd[256] = {0}; |
| 2788 | char str_cmd_ret[128] = {0}; |
| 2789 | char dest_ip[32] = {0}; |
| 2790 | lastAP1Bytes = lastAP2Bytes = 0; |
| 2791 | lastAP1Drop = lastAP2Drop = 0; |
| 2792 | lastAP1Speed = lastAP2Speed = 0; |
| 2793 | setAP1Speed = 50; |
| 2794 | setAP2Speed = 80; |
| 2795 | nloop = 0; |
| 2796 | nmax = 6; |
| 2797 | ncheckcount = nidlecount = 0; |
| 2798 | |
| 2799 | printf("------gbw thread run\n"); |
| 2800 | sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac); |
| 2801 | while (dest_ip[0] == '\0') { |
| 2802 | sleep(1); |
| 2803 | str_cmd_ret[0] = '\0'; |
| 2804 | exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)); |
| 2805 | for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) { |
| 2806 | if (str_cmd_ret[n] == '\n'){ |
| 2807 | str_cmd_ret[n] = '\0'; |
| 2808 | break; |
| 2809 | } |
| 2810 | } |
| 2811 | if (str_cmd_ret[0] != '\0') |
| 2812 | { |
| 2813 | strcpy(dest_ip, str_cmd_ret); |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | system("tc qdisc del dev tether root > /dev/null 2>&1"); |
| 2818 | system("tc qdisc add dev tether root handle 1: htb r2q 1"); |
| 2819 | system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000"); |
| 2820 | if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0) |
| 2821 | { |
| 2822 | printf("not get tether info\n"); |
| 2823 | return; |
| 2824 | } |
| 2825 | 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); |
| 2826 | system(str_cmd); |
| 2827 | system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000"); |
| 2828 | sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip); |
| 2829 | //printf("----cmd:%s\n", str_cmd); |
| 2830 | system(str_cmd); |
| 2831 | |
| 2832 | while (1) { |
| 2833 | sleep(1); |
| 2834 | memset(str_cmd, 0, sizeof(str_cmd)); |
| 2835 | if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd))) |
| 2836 | continue; |
| 2837 | //printf("ap1 --- %s\n", str_cmd); |
| 2838 | n = lynq_split(str_cmd, strlen(str_cmd), ' ', results); |
| 2839 | if (n > 9) { |
| 2840 | if (strcmp(results[1], "Sent") == 0) { |
| 2841 | currAP1Bytes = atoll(results[2]); |
| 2842 | } |
| 2843 | if (strcmp(results[6], "(dropped") == 0) { |
| 2844 | currAP1Drop = atoi(results[7]); |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | memset(str_cmd, 0, sizeof(str_cmd)); |
| 2849 | if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd))) |
| 2850 | continue; |
| 2851 | //printf("ap2 --- %s\n", str_cmd); |
| 2852 | n = lynq_split(str_cmd, strlen(str_cmd), ' ', results); |
| 2853 | if (n > 9) { |
| 2854 | if (strcmp(results[1], "Sent") == 0) { |
| 2855 | currAP2Bytes = atoll(results[2]); |
| 2856 | } |
| 2857 | if (strcmp(results[6], "(dropped") == 0) { |
| 2858 | currAP2Drop = atoi(results[7]); |
| 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop); |
| 2863 | if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) { |
| 2864 | lastAP1Bytes = currAP1Bytes; |
| 2865 | lastAP2Bytes = currAP2Bytes; |
| 2866 | continue; |
| 2867 | } |
| 2868 | |
| 2869 | currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024; |
| 2870 | currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024; |
| 2871 | //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed); |
| 2872 | lastAP1Speed = currAP1Speed; |
| 2873 | lastAP2Speed = currAP2Speed; |
| 2874 | lastAP1Bytes = currAP1Bytes; |
| 2875 | lastAP2Bytes = currAP2Bytes; |
| 2876 | |
| 2877 | currSetAP1Speed = setAP1Speed; |
| 2878 | if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) { |
| 2879 | ncheckcount++; |
| 2880 | if (ncheckcount > 3) { |
| 2881 | ncheckcount = 0; |
| 2882 | currSetAP1Speed = 5; |
| 2883 | } |
| 2884 | } |
| 2885 | else { |
| 2886 | ncheckcount = 0; |
| 2887 | if (currAP1Speed < 5) |
| 2888 | nidlecount++; |
| 2889 | else |
| 2890 | nidlecount = 0; |
| 2891 | |
| 2892 | } |
| 2893 | |
| 2894 | if (nidlecount > 60 ){ |
| 2895 | currSetAP1Speed = 50; |
| 2896 | } |
| 2897 | |
| 2898 | if (currSetAP1Speed != setAP1Speed) { |
| 2899 | setAP1Speed = currSetAP1Speed; |
| 2900 | 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)); |
| 2901 | //printf("------***change speed: %s\n", str_cmd); |
| 2902 | system(str_cmd); |
| 2903 | } |
| 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | int enableGBW(const char* mac) { |
| 2908 | int i,len; |
| 2909 | char get_ipaddr_cmd[128]={0}; |
| 2910 | ap_info_s *ap; |
| 2911 | device_info_s * list; |
| 2912 | |
| 2913 | if (mac == NULL || g_gbw_enabled == 1) |
| 2914 | return -1; |
| 2915 | len = strlen(mac); |
| 2916 | g_gbw_mac = malloc(len + 1); |
| 2917 | for(i=0;i<len;i++) { |
| 2918 | if (mac[i] >= 'A' && mac[i] <= 'Z') |
| 2919 | { |
| 2920 | g_gbw_mac[i] = 'a' + (mac[i] - 'A'); |
| 2921 | } |
| 2922 | else |
| 2923 | g_gbw_mac[i] = mac[i]; |
| 2924 | } |
| 2925 | g_gbw_mac[i] = '\0'; |
| 2926 | g_gbw_enabled = 1; |
| 2927 | |
| 2928 | sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac); |
| 2929 | if (system(get_ipaddr_cmd) == 0) { |
| 2930 | //startGBW(); |
| 2931 | if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) { |
| 2932 | for (i=0;i<len;i++) { |
| 2933 | //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname); |
| 2934 | if (strcmp(g_gbw_mac, list[i].sta_mac) == 0) |
| 2935 | startGBW(); |
| 2936 | } |
| 2937 | free(ap); |
| 2938 | free(list); |
| 2939 | } |
| 2940 | } |
| 2941 | return 0; |
| 2942 | } |
| 2943 | |
| 2944 | int disableGBW() { |
| 2945 | stopGBW(); |
| 2946 | free(g_gbw_mac); |
| 2947 | g_gbw_mac = NULL; |
| 2948 | g_gbw_enabled = 1; |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
| 2952 | static int startGBW() { |
| 2953 | if (g_gbw_watcher_pid != 0) { |
| 2954 | stopGBW(); |
| 2955 | } |
| 2956 | pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL); |
| 2957 | } |
| 2958 | |
| 2959 | static int stopGBW() { |
| 2960 | void* retval; |
| 2961 | pthread_cancel(g_gbw_watcher_pid); |
| 2962 | pthread_join(g_gbw_watcher_pid, &retval); |
| 2963 | g_gbw_watcher_pid = 0; |
| 2964 | system("tc qdisc del dev tether root"); |
| 2965 | } |
| 2966 | //you.chen add for tv-box end |