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