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