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