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