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