blob: dce290a65493e74e57e306f7b4cc4321e03e0efa [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#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.xiong7a105ce2022-03-02 09:43:11 -050024
qs.xiong1af5daf2022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chen35020192022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080035#define CTRL_STA 0
36#define CTRL_AP 1
37#define AP_NETWORK_0 0
38
39pthread_t g_ap_watcher_pid = 0;
40volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080041volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080042
43pthread_t g_sta_watcher_pid = 0;
44volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080045volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080046volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080047
48void * g_ap_callback_priv = NULL;
49AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
50void * g_sta_callback_priv = NULL;
51STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
52
53//const char * CTRL_PATH="/var/run/wpa_supplicant";
54const 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"};
56const char * cmd_list_networks = "LIST_NETWORKS";
57const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080058const char * cmd_disconnect = "DISCONNECT";
59const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080060const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080061const char * STATE_COMPLETED = "COMPLETED";
you.chen35020192022-05-06 11:30:57 +080062
you.chend2fef3f2023-02-13 10:50:35 +080063struct local_wpa_ctrl{
64 struct wpa_ctrl *ctrl;
65 pthread_mutex_t mutex;
66};
67
68static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
69
70static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +080071
you.chen0f5c6432022-11-07 18:31:14 +080072//you.chen add for tv-box start
73volatile int g_gbw_enabled = 0;
74char * g_gbw_mac = NULL;
75pthread_t g_gbw_watcher_pid = 0;
76static int startGBW();
77static int stopGBW();
78//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +080079
80typedef struct __curr_status_info {
81 ap_info_s *ap;
82 char * state;
83 int net_no;
84}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040085
you.chend2fef3f2023-02-13 10:50:35 +080086static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
87 char *reply, size_t *reply_len,
88 void (*msg_cb)(char *msg, size_t len))
89{
90 int ret;
91 if (ctrl->ctrl == NULL) {
92 printf("local_wpa_ctrl_request ctrl is null\n");
93 return -1;
94 }
95 pthread_mutex_lock(&ctrl->mutex);
96 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
97 pthread_mutex_unlock(&ctrl->mutex);
98 return ret;
99}
100
101static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
102 int repeat_cnt;
103 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
104 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
105 printf("inner_get_wpa_ctrl\n");
106 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
107 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
108// printf("wait enable finish\n");
109 usleep(500 * 1000);
110 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
111 }
112 if (NULL == g_lynq_wpa_ctrl[index]) {
113 printf("NULL == g_lynq_wpa_ctrl[index]\n");
114 goto out_addr;
115 }
116 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
117 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
118 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
119 printf("wpa_ctrl_open fail\n");
120 goto out_addr;
121 }
122 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
123 }
124 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
125out_addr:
126 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
127 return lynq_wpa_ctrl;
128}
129
qs.xiong97fa59b2022-04-07 05:41:29 -0400130#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400131{\
you.chen35020192022-05-06 11:30:57 +0800132 perror((str));\
133 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400134}
135
you.chen35020192022-05-06 11:30:57 +0800136#define CHECK_IDX(idx, type) do { \
137 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
138 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
139 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
140 return -1; \
141 } \
142 }while (0)
143
144#define CHECK_WPA_CTRL(index) int ret = 0;\
145 size_t reply_len = MAX_RET; \
146 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800147 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800148 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800149 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
150 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800151 }while(0)
152
153#define DO_REQUEST(cmd_str) do { \
154 reply_len = MAX_RET;\
155 cmd_reply[0] = '\0'; \
qs.xiongfdbc58e2022-09-29 11:37:32 +0800156 printf("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800157 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen35020192022-05-06 11:30:57 +0800158 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800159 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800160 return ret; \
161 } \
162 cmd_reply[reply_len+1] = '\0'; \
163 printf("cmd replay [ %s ]\n", cmd_reply); \
164 }while(0)
165
166#define DO_OK_FAIL_REQUEST(cmd_str) do { \
167 DO_REQUEST(cmd_str); \
168 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800169 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800170 return -1; \
171 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800172 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800173 return -1; \
174 } \
175 }while (0)
176
177
you.chend2fef3f2023-02-13 10:50:35 +0800178static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
you.chen35020192022-05-06 11:30:57 +0800179
180static void APWatcherThreadProc() {
181 size_t len = MAX_RET;
182 char msg_notify[MAX_RET];
183
you.chen6c2dd9c2022-05-16 17:55:28 +0800184 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800185 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800186
187 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800188 if (lynq_wpa_ctrl == NULL) {
189 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
190 if (lynq_wpa_ctrl == NULL) {
191 usleep(100*1000);
192 continue;
193 }
194
195 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800196 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800197 }
198
you.chen35020192022-05-06 11:30:57 +0800199 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
200 usleep(100*1000);
201 continue;
202 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800203 memset(msg_notify, 0, MAX_RET);
204 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800205 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
206 msg_notify[len+1] = '\0';
207 printf("ap------> %s\n", msg_notify);
you.chen0f5c6432022-11-07 18:31:14 +0800208//you.chen change for tv-box start
you.chen35020192022-05-06 11:30:57 +0800209 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
you.chen0f5c6432022-11-07 18:31:14 +0800210 if (g_ap_callback_func != NULL)
211 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
212 if (g_gbw_enabled == 1 && g_gbw_mac != NULL) {
213 printf("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
214 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) {
215 stopGBW();
216 }
217 }
you.chen35020192022-05-06 11:30:57 +0800218 }
219 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
you.chen0f5c6432022-11-07 18:31:14 +0800220 if (g_ap_callback_func != NULL)
221 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
222 if (g_gbw_enabled == 1 && g_gbw_mac != NULL) {
223 printf("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
224 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) {
225 startGBW();
226 }
227 }
you.chen35020192022-05-06 11:30:57 +0800228 }
you.chen0f5c6432022-11-07 18:31:14 +0800229//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800230 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
231 } // end while (g_ap_watcher_stop_flag == 0)
you.chen92fd5d32022-05-25 10:09:47 +0800232 if (lynq_wpa_ctrl != NULL) {
233 wpa_ctrl_detach(lynq_wpa_ctrl);
234 wpa_ctrl_close(lynq_wpa_ctrl);
235 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400236}
237
you.chen35020192022-05-06 11:30:57 +0800238static void STAWatcherThreadProc() {
239 size_t len = MAX_RET;
240 char msg_notify[MAX_RET];
241 char *pReason;
242 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400243
you.chen6c2dd9c2022-05-16 17:55:28 +0800244 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800245 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800246
247 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800248 if (lynq_wpa_ctrl == NULL) {
249 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
250 if (lynq_wpa_ctrl == NULL) {
251 usleep(100*1000);
252 continue;
253 }
254
255 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800256 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800257 }
258
you.chen35020192022-05-06 11:30:57 +0800259 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
260 usleep(100*1000);
261 continue;
262 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800263 memset(msg_notify, 0, MAX_RET);
264 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800265 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
266 msg_notify[len+1] = '\0';
267 printf("sta ------> %s\n", msg_notify);
268 if (strstr(msg_notify, state_scan_result) != NULL) {
269 g_sta_scan_finish_flag = 1;
270 }
271
272 if (g_sta_callback_func == NULL) {
273 continue;
274 }
275 error = -1;
276 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
277 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
278 }
279 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
280 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
281 }
282 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
283 pReason = strstr(msg_notify, "reason=");
284 if (pReason != NULL) {
285 pReason += strlen("reason=");
you.chen92fd5d32022-05-25 10:09:47 +0800286 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen35020192022-05-06 11:30:57 +0800287 error = LYNQ_TIME_OUT;
288 }
you.chen92fd5d32022-05-25 10:09:47 +0800289 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen35020192022-05-06 11:30:57 +0800290 error = LYNQ_PSW_ERROR;
291 }
292 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
293 }
294 }
295 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
296 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
297 }
298 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
299 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
300 }
301 }
302 }
you.chen92fd5d32022-05-25 10:09:47 +0800303 if (lynq_wpa_ctrl != NULL) {
304 wpa_ctrl_detach(lynq_wpa_ctrl);
305 wpa_ctrl_close(lynq_wpa_ctrl);
306 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400307}
308
qs.xiong1af5daf2022-03-14 09:12:12 -0400309int lynq_wifi_enable(void)
310{
you.chen35020192022-05-06 11:30:57 +0800311 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800312 int i;
you.chend2fef3f2023-02-13 10:50:35 +0800313 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
314
315 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
316 goto out_enable;
317 }
318
319 printf("lynq_wifi_enable1\n");
you.chen35020192022-05-06 11:30:57 +0800320 const char * cmd_check_service =
321 "state=`systemctl is-active wg870_drv_insmod.service`\n"
322 "[ \"\"$state == \"active\" ] && exit 0\n"
323 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
324// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
325// return 0;
326// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400327
you.chen35020192022-05-06 11:30:57 +0800328 ret = system(cmd_check_service);
329 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800330 printf("service state %d\n", ret);
you.chend2fef3f2023-02-13 10:50:35 +0800331 ret = -1;
332 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800333 }
lhfe8da902022-10-11 18:55:36 +0800334
you.chen6c2dd9c2022-05-16 17:55:28 +0800335 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800336 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800337 break;
338 }
339 usleep(300*1000);
340 }
341
342 if (i >= 10) {
you.chend2fef3f2023-02-13 10:50:35 +0800343 ret = -1;
344 goto out_enable;
you.chen6c2dd9c2022-05-16 17:55:28 +0800345 }
346
you.chen9f17e4d2022-06-06 17:18:18 +0800347 //@todo delete add temp check for socket avilable start (20220606)
348 for (i=0; i<60; i++)
349 {
350 if (system("netstat -an | grep -q DGRAM") == 0) {
351 break;
352 }
353 sleep(1);
354 }
355
356 if (i >= 60)
357 {
you.chend2fef3f2023-02-13 10:50:35 +0800358 ret = -1;
359 goto out_enable;
you.chen9f17e4d2022-06-06 17:18:18 +0800360 }
361 //@todo delete add temp check for socket avilable end (20220606)
362
you.chena6fa5b22022-05-18 10:28:19 +0800363 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800364 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800365 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800366 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800367 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800368 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800369 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800370 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800371 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800372 }
qs.xiong55f32762023-02-16 15:59:45 +0800373 ret = system("wl -i wlan0 PM 0");
374 if( ret != 0)
375 {
376 printf("Set sta PM work mode to 0 fail,ret is%d",ret);
377 }
you.chen35020192022-05-06 11:30:57 +0800378 if (g_ap_watcher_pid == 0 ) {
379 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
380 if(ret<0){
you.chend2fef3f2023-02-13 10:50:35 +0800381 ret = -1;
382 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800383 }
384 }
385
386 if (g_sta_watcher_pid == 0 ) {
387 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
388 if(ret<0){
you.chend2fef3f2023-02-13 10:50:35 +0800389 ret = -1;
390 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800391 }
392 }
393
you.chena6fa5b22022-05-18 10:28:19 +0800394 for (i=0; i<10; i++) {
395 usleep(300*1000);
396 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
397 break;
398 }
399 }
400
you.chend2fef3f2023-02-13 10:50:35 +0800401 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
402 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
403 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
404 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
405out_enable:
406 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +0800407 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500408}
409
qs.xiong1af5daf2022-03-14 09:12:12 -0400410int lynq_wifi_disable(void)
411{
you.chend2fef3f2023-02-13 10:50:35 +0800412 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +0800413 g_ap_watcher_stop_flag = 1;
414 g_sta_watcher_stop_flag = 1;
415 if (g_ap_watcher_pid != 0)
416 pthread_join(g_ap_watcher_pid, NULL);
417 if (g_sta_watcher_pid != 0)
418 pthread_join(g_sta_watcher_pid, NULL);
419 if (g_lynq_wpa_ctrl[0] != NULL)
420 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
421 if (g_lynq_wpa_ctrl[1] != NULL)
422 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
423 g_ap_watcher_pid = 0;
424 g_sta_watcher_pid = 0;
425 g_lynq_wpa_ctrl[0] = NULL;
426 g_lynq_wpa_ctrl[1] = NULL;
427 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +0800428 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
429 return 0;
430}
431
432static inline char inner_convert_char(char in)
433{
434 if (in >= '0' && in <= '9')
435 {
436 return in - '0';
437 }
438 else if (in >= 'a' && in <= 'f')
439 {
440 return in - 'a' + 10;
441 }
442 else if (in >= 'A' && in <= 'F')
443 {
444 return in - 'A' + 10;
445 }
446 else
447 {
448 return '\xff';
449 }
450}
451
452static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
453{
454 char *p;
455 size_t pos = 0;
456 if (NULL == out_ssid)
457 return;
458 //printf("input ssid=[%s]\n", ssid);
459 memset(out_ssid, 0, out_ssid_len);
460 if (NULL == ssid)
461 return;
462 p = strchr(ssid, '\\');
463 if (NULL == p)
464 {
465 strncpy(out_ssid, ssid, out_ssid_len);
466 //printf(" first %s\n", out_ssid);
467 }
468 else
469 {
470 pos = p - ssid;
471 memcpy(out_ssid, ssid, pos);
472 //printf("pos %lu -- %s\n", pos, out_ssid);
473 for(; pos < out_ssid_len; pos ++)
474 {
475 if (p[0] == '\0')
476 {
477 //printf(" out %s\n", out_ssid);
478 return;
479 }
480 else if (p[0] != '\\')
481 {
482 out_ssid[pos] = p[0];
483 p += 1;
484 }
485 else if (p[1] == 'x' || p[1] == 'X')
486 {
487 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
488 p += 4;
489 }
490 else if (p[1] == '\\')
491 {
492 out_ssid[pos] = '\\';
493 p += 2;
494 }
495 else if (p[1] == 't')
496 {
497 out_ssid[pos] = '\t';
498 p += 2;
499 }
500 else if (p[1] == 'r')
501 {
502 out_ssid[pos] = '\r';
503 p += 2;
504 }
505 else if (p[1] == 'n')
506 {
507 out_ssid[pos] = '\n';
508 p += 2;
509 }//todo find a better way to convert?
510 }
511 }
512 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -0500513}
qs.xiong1af5daf2022-03-14 09:12:12 -0400514
you.chen35020192022-05-06 11:30:57 +0800515static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +0800516 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +0800517 char lynq_cmd_get[128]={0};
518
519 if (out_put == NULL) {
520 printf("output ptr is null\n");
521 return -1;
522 }
523 if (param_name == NULL) {
524 printf("param ptr is null");
525 return -1;
526 }
527 if (param_name[0] == '\0') {
528 printf("param is empty");
529 return -1;
530 }
531
532 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
533
534 CHECK_WPA_CTRL(interface);
535
536 DO_REQUEST(lynq_cmd_get);
537
538 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
539 return -1;
540 }
541
you.chena6fa5b22022-05-18 10:28:19 +0800542// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +0800543 if (strcmp(param_name, "ssid") == 0)
544 {
545 if (cmd_reply[0] == '\"') {
546 ssid_len = reply_len - 1;
547 memcpy(out_put, cmd_reply + 1, ssid_len);
548 if (out_put[ssid_len-1] == '\"')
549 {
550 out_put[ssid_len-1] = '\0';
551 }
552 else
553 {
554 out_put[ssid_len] = '\0';
555 }
556 }
557 else{
558 ssid_len = reply_len / 2;
559 for(i=0; i<ssid_len; i++)
560 {
561 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
562 }
563 out_put[ssid_len] = '\0';
564 }
565 }
566 else
567 {
568 memcpy(out_put, cmd_reply, reply_len + 1);
569 }
you.chen35020192022-05-06 11:30:57 +0800570 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500571}
qs.xiong1af5daf2022-03-14 09:12:12 -0400572
you.chen35020192022-05-06 11:30:57 +0800573static int lynq_split(char * str, int len, char delimiter, char * results[]) {
574 int ret = 0;
575 char * end = str + len - 1;
576 results[ret++] = str;
577 while(str < end) {
578 if (*str == delimiter) {
579 *str++ = '\0';
580 results[ret++] = str;
581 continue;
582 }
583 str++;
584 }
585 if (*str == delimiter) {
586 *str = '\0';
587 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400588
you.chen35020192022-05-06 11:30:57 +0800589 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500590}
591
you.chend2fef3f2023-02-13 10:50:35 +0800592static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
593{
594 char * p;
595 int ret = 0;
596 char cmd[256]={0};
597 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +0800598 return -1;
you.chend2fef3f2023-02-13 10:50:35 +0800599 memset(ip, 0, ip_len);
600 sprintf(cmd, "ip neigh | grep \"lladdr\" | grep \"tether\" | grep \"%s\" | head -1 | awk '{print $1}'", mac);
601 ret = exec_cmd(cmd, ip, ip_len);
602 p = strchr(ip, '\n');
603 if (NULL != p)
604 {
605 *p = '\0';
you.chen35020192022-05-06 11:30:57 +0800606 }
you.chend2fef3f2023-02-13 10:50:35 +0800607 printf("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +0800608 return ret;
609}
610
you.chend2fef3f2023-02-13 10:50:35 +0800611static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +0800612 struct in_addr addr ={0};
613 struct hostent *ht;
614
615 if (ip == NULL || *ip == '\0' || hostname == NULL) {
616 return -1;
617 }
618
you.chend2fef3f2023-02-13 10:50:35 +0800619 *hostname = '\0';
you.chen35020192022-05-06 11:30:57 +0800620 if (inet_aton(ip, &addr) == 0) {
621 printf("---inet_aton fail\n");
622 return -1;
623 }
624
625 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
626
627 if (ht == NULL) {
628 printf("---gethostbyaddr fail\n");
629 herror(NULL);
630 return -1;
631 }
632
633 strcpy(hostname, ht->h_name);
634
635 return 0;
636}
637
638static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
639{
640 int count, index, words_count;
641 char * split_lines[128]= {0};
642 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +0800643 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +0800644 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
645
646 CHECK_WPA_CTRL(ap_sta);
647
648 DO_REQUEST(lynq_wifi_list_networks);
649
650 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
651
652 //@todo check ssid field to compatible
653
654 ret = 0;
655 for(index=1; index < count; index++) {
656 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
657 if (words_count > 2) {
you.chend2fef3f2023-02-13 10:50:35 +0800658 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
659 if (ssid == NULL || strcmp(local_ssid, ssid) == 0) {
you.chen35020192022-05-06 11:30:57 +0800660 net_no_list[ret++] = atoi(split_words[0]);
661 }
662 }
663 }
664
665 return ret;
666}
667
668static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800669 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800670 CHECK_WPA_CTRL(ap_sta);
671 const char *lynq_wifi_add_network = "ADD_NETWORK";
672
673 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800674 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800675 return -1;
676 }
677
you.chen6c2dd9c2022-05-16 17:55:28 +0800678 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800679 if(cmd_reply[i] == '\n') {
680 cmd_reply[i] = '\0';
681 break;
682 }
683 }
684 return atoi(cmd_reply);
685}
you.chena6cd55a2022-05-08 12:20:18 +0800686
you.chen35020192022-05-06 11:30:57 +0800687static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
688{
689 int count, index;
690 int net_no_list[128];
691
692
693 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
694 for (index=0; index < count; index++) {
695 if (net_no_list[index] == net_no) {
696 return 0;
697 }
698 }
699
700 if (count >= 1)
701 index = net_no_list[count - 1];
702 else
703 index = -1;
704
705 while (index < net_no ) {
706 index = lynq_add_network(ap_sta);
707 if (index >= net_no) { // required network no created
708 return 0;
709 }
you.chena6cd55a2022-05-08 12:20:18 +0800710 else if( index < 0) {
711 printf("add network fail\n");
712 return -1;
713 }
you.chen35020192022-05-06 11:30:57 +0800714 }
715
716 if (index < 0)
717 return -1;
718
719 return 0;
720}
721
722static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
723 if (freq > 5000 && freq < 6000) {
724 return LYNQ_WIFI_5G_band;
725 }
726 else if (freq > 2000 && freq < 3000) {
727 return LYNQ_WIFI_2G_band;
728 }
729 return LYNQ_WIFI_2_and_5G_band;
730}
731
732static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
733 if (key_mgmt != NULL) {
734 if (memcmp( key_mgmt, "NONE", 4) == 0) {
735 return LYNQ_WIFI_AUTH_OPEN;
736 }
737 else if (memcmp( key_mgmt, "WEP", 3) == 0){
738 return LYNQ_WIFI_AUTH_WEP;
739 }
740 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
741 return LYNQ_WIFI_AUTH_WPA_PSK;
742 }
743 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
744 return LYNQ_WIFI_AUTH_WPA2_PSK;
745 }
746 }
747
748 return -1;
749}
750
751static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
752 if (flag != NULL) {
753 if (strstr( flag, "WPA2-PSK") != NULL){
754 return LYNQ_WIFI_AUTH_WPA2_PSK;
755 }
756 else if (strstr( flag, "WPA-PSK") != NULL){
757 return LYNQ_WIFI_AUTH_WPA_PSK;
758 }
759 else if (strstr( flag, "WEP") != NULL){
760 return LYNQ_WIFI_AUTH_WEP;
761 }
762 else if (strstr( flag, "NONE") != NULL) {
763 return LYNQ_WIFI_AUTH_OPEN;
764 }
you.chend2fef3f2023-02-13 10:50:35 +0800765 else if (strcmp( flag, "[ESS]") == 0) {
766 return LYNQ_WIFI_AUTH_OPEN;
767 }
you.chen35020192022-05-06 11:30:57 +0800768 }
769
770 return -1;
771}
772
773static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
774 switch (bw) {
775 case 10:
776 return LYNQ_WIFI_BANDWIDTH_HT10;
777 break;
778 case 20:
779 return LYNQ_WIFI_BANDWIDTH_HT20;
780 break;
781 case 40:
782 return LYNQ_WIFI_BANDWIDTH_HT40;
783 break;
784 case 80:
785 return LYNQ_WIFI_BANDWIDTH_HT80;
786 break;
787 default:
788 break;
789 }
790
791 return -1;
792}
793
794static int inner_get_status_info(int interface, curr_status_info *curr_state) {
795 int i, count;
796 char *p;
797 const char *lynq_status_cmd = "STATUS";
798 const char * FLAG_SSID = "ssid=";
799 const char * FLAG_SBSID = "bssid=";
800 const char * FLAG_KEY_MGMT = "key_mgmt=";
801 const char * FLAG_FREQ = "freq=";
802 const char * FLAG_STATE = "wpa_state=";
803 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +0800804 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +0800805 char *split_lines[128] = {0};
806
807 CHECK_WPA_CTRL(interface);
808
809 if (curr_state == NULL) {
810 return -1;
811 }
812
813 DO_REQUEST(lynq_status_cmd);
814
815 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
816
817 curr_state->net_no = -1;
818 ret = -1;
819 for(i=0; i < count; i++) {
820 if (curr_state->ap != NULL) {
you.chen35020192022-05-06 11:30:57 +0800821 p = strstr(split_lines[i], FLAG_SBSID);
822 if (p != NULL) {
you.chend2fef3f2023-02-13 10:50:35 +0800823 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +0800824 ret = 0;
825 continue;
826 }
you.chenf58b3c92022-06-21 16:53:48 +0800827 p = strstr(split_lines[i], FLAG_SSID);
828 if (p != NULL) {
you.chend2fef3f2023-02-13 10:50:35 +0800829 inner_copy_ssid(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID), sizeof (curr_state->ap->ap_ssid));
you.chenf58b3c92022-06-21 16:53:48 +0800830 ret = 0;
831 continue;
832 }
you.chen35020192022-05-06 11:30:57 +0800833 p = strstr(split_lines[i], FLAG_KEY_MGMT);
834 if (p != NULL) {
you.chen450d0172022-07-15 17:56:48 +0800835 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
836 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +0800837 ret = 0;
838 continue;
839 }
840 p = strstr(split_lines[i], FLAG_FREQ);
841 if (p != NULL) {
842 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
843 ret = 0;
844 continue;
845 }
you.chend2fef3f2023-02-13 10:50:35 +0800846 p = strstr(split_lines[i], FLAG_IPADDR);
847 if (p != NULL) {
848 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
849 ret = 0;
850 continue;
851 }
you.chen35020192022-05-06 11:30:57 +0800852 } // end if (ap != NULL)
853 if (curr_state->state != NULL) {
854 p = strstr(split_lines[i], FLAG_STATE);
855 if (p != NULL) {
856 strcpy(curr_state->state, p + strlen(FLAG_STATE));
857 ret = 0;
858 continue;
859 }
860
861 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800862 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800863 ret = 0;
you.chen450d0172022-07-15 17:56:48 +0800864 curr_state->net_no = atoi(p + strlen(FLAG_ID));
you.chen35020192022-05-06 11:30:57 +0800865 printf("net_no %d, -- %s\n", curr_state->net_no, p);
866 }
867 }
868
869 return ret;
870}
871
872
qs.xiongf1b525b2022-03-31 00:58:23 -0400873int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500874{
you.chen35020192022-05-06 11:30:57 +0800875 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500876
you.chen35020192022-05-06 11:30:57 +0800877 if (ap_ssid == NULL) {
878 printf("ap_ssid is null\n");
879 return -1;
880 }
881 else {
882 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
883 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400884
you.chen35020192022-05-06 11:30:57 +0800885 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
886 return -1;
887 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400888
you.chen35020192022-05-06 11:30:57 +0800889 CHECK_IDX(idx, CTRL_AP);
890
891 CHECK_WPA_CTRL(CTRL_AP);
892
893 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
894
895 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
896 DO_OK_FAIL_REQUEST(cmd_save_config);
897
qs.xiong7a105ce2022-03-02 09:43:11 -0500898 return 0;
you.chen35020192022-05-06 11:30:57 +0800899
qs.xiong7a105ce2022-03-02 09:43:11 -0500900}
901
you.chen35020192022-05-06 11:30:57 +0800902int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500903{
you.chen35020192022-05-06 11:30:57 +0800904 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +0800905 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -0500906}
907
qs.xiongc9c79f72022-10-17 15:27:18 +0800908/*****
909 *frequency <------>channel
910 *
911 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
912 *
913 *
914 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
915 *
916 *
917 * */
918static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +0800919 int legitimate_frequency[]={2412,2417,2422,2427,2432,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825};
920 int i;
921 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
922
qs.xiong69a332b2022-12-02 09:58:57 +0800923 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +0800924 {
925 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +0800926 break;
qs.xiongc9c79f72022-10-17 15:27:18 +0800927 }
qs.xiongc00b6032022-11-29 16:28:03 +0800928
929 if(i == arr_len)
930 {
931 printf("input frequency is eero--->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +0800932 return -1;
933 }
qs.xiongc00b6032022-11-29 16:28:03 +0800934
qs.xiongc9c79f72022-10-17 15:27:18 +0800935 return 0;
936}
qs.xiongf1b525b2022-03-31 00:58:23 -0400937int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500938{
qs.xiongc00b6032022-11-29 16:28:03 +0800939 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +0800940 char lynq_wifi_frequency_cmd[128]={0};
941 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +0800942 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500943
qs.xiongc9c79f72022-10-17 15:27:18 +0800944 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +0800945 check = lynq_check_set_frequency(lynq_wifi_frequency);
946 if(check != 0)
947 {
948 printf("do check frequency error\n");
qs.xiongc9c79f72022-10-17 15:27:18 +0800949 return -1;
you.chen35020192022-05-06 11:30:57 +0800950 }
qs.xiongc00b6032022-11-29 16:28:03 +0800951 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
952 {
you.chen35020192022-05-06 11:30:57 +0800953 return -1;
954 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400955
you.chen35020192022-05-06 11:30:57 +0800956 CHECK_IDX(idx, CTRL_AP);
957
958 CHECK_WPA_CTRL(CTRL_AP);
959
960 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
961 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
962 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
963
you.chen6c2dd9c2022-05-16 17:55:28 +0800964 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800965 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
966 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
967 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500968
qs.xiong1af5daf2022-03-14 09:12:12 -0400969 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500970}
971
qs.xiongf1b525b2022-03-31 00:58:23 -0400972int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500973{
you.chen35020192022-05-06 11:30:57 +0800974 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400975
you.chen35020192022-05-06 11:30:57 +0800976 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400977
you.chen35020192022-05-06 11:30:57 +0800978 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
979 return -1;
980 }
981 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400982
983 return 0;
984}
985
qs.xiongf1b525b2022-03-31 00:58:23 -0400986int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
987{
you.chen35020192022-05-06 11:30:57 +0800988 CHECK_IDX(idx, CTRL_AP);
989 switch(bandwidth){
990 case LYNQ_WIFI_BANDWIDTH_HT10:
991 {
992 printf("bandwith [%d] not support now\n", bandwidth);
993 return -1;
994 }
995 case LYNQ_WIFI_BANDWIDTH_HT20:
996 {
997 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
998 system("wl down");
999 if (system(lynq_cmd_bandwith) != 0 ){
1000 return -1;
1001 }
1002 system("wl up");
1003 break;
1004 }
1005 case LYNQ_WIFI_BANDWIDTH_HT40:
1006 {
1007 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
1008 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1009 system("wl down");
1010 if (system(lynq_cmd_bandwith) != 0 ){
1011 return -1;
1012 }
1013 system("wl up");
1014 break;
1015 }
1016 case LYNQ_WIFI_BANDWIDTH_HT80:
1017 {
1018 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
1019 system("wl down");
1020 if (system(lynq_cmd_bandwith) != 0 ){
1021 return -1;
1022 }
1023 system("wl up");
1024 break;
1025 }
1026 default:
1027 {
1028 printf("auth type [%d] not support now\n", bandwidth);
1029 return -1;
1030 }
1031 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001032
1033
you.chen35020192022-05-06 11:30:57 +08001034 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001035}
you.chen35020192022-05-06 11:30:57 +08001036
qs.xiongf1b525b2022-03-31 00:58:23 -04001037int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1038{
you.chen35020192022-05-06 11:30:57 +08001039 int count = 0;
1040 int index = 0;
1041 char *split_words[128] = {0};
1042 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001043
you.chen35020192022-05-06 11:30:57 +08001044 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001045
you.chen35020192022-05-06 11:30:57 +08001046 CHECK_WPA_CTRL(CTRL_AP);
1047
1048 DO_REQUEST(lynq_chanspec_cmd);
1049
1050 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1051 for(;index < count; index++) {
1052 if (strncmp(split_words[index], "bw", 2) != 0) {
1053 continue;
1054 }
1055
1056 index++;
1057 if (index >= count) {
1058 return -1;
1059 }
1060
1061 printf("bw %s\n", split_words[index]);
1062 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1063 return 0;
1064 }
1065
1066 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001067}
qs.xiong0fb469a2022-04-14 03:50:45 -04001068
qs.xiongf1b525b2022-03-31 00:58:23 -04001069int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001070{
you.chen35020192022-05-06 11:30:57 +08001071 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -04001072
you.chen35020192022-05-06 11:30:57 +08001073 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04001074
you.chen35020192022-05-06 11:30:57 +08001075 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04001076
you.chen35020192022-05-06 11:30:57 +08001077 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
1078 return -1;
1079 }
1080
1081 system("wl down");
1082 if (system(lynq_cmd_channel) != 0 ){
1083 return -1;
1084 }
1085 system("wl up");
1086 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001087}
qs.xiong0fb469a2022-04-14 03:50:45 -04001088
qs.xiongf1b525b2022-03-31 00:58:23 -04001089int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001090{
you.chen35020192022-05-06 11:30:57 +08001091 int count = 0;
1092 int index = 0;
1093 char *split_words[128] = {0};
1094 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001095
you.chen35020192022-05-06 11:30:57 +08001096 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001097
you.chen35020192022-05-06 11:30:57 +08001098 CHECK_WPA_CTRL(CTRL_AP);
1099
1100 DO_REQUEST(lynq_chanspec_cmd);
1101
1102 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1103 for(;index < count; index++) {
1104 printf("---- %s\n",split_words[index]);
1105 if (strncmp(split_words[index], "channel", 2) != 0) {
1106 continue;
1107 }
1108
1109 index++;
1110 if (index >= count) {
1111 return -1;
1112 }
1113
1114 *channel = atoi(split_words[index]);
1115 return 0;
1116 }
1117
1118 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001119}
1120
1121
you.chen35020192022-05-06 11:30:57 +08001122int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001123{
you.chen6c2dd9c2022-05-16 17:55:28 +08001124 char ssid[MAX_CMD] = {0};
1125 int freq = 0;
1126 char lynq_auth_cmd[64]={0};
1127 char lynq_auth_alg_cmd[64]={0};
1128 char lynq_psk_cmd[64]={0};
1129 char lynq_pairwise_cmd[64]={0};
1130 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001131 CHECK_IDX(idx, CTRL_AP);
1132
you.chen6c2dd9c2022-05-16 17:55:28 +08001133 CHECK_WPA_CTRL(CTRL_AP);
1134
you.chen35020192022-05-06 11:30:57 +08001135 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1136 return -1;
1137 }
1138
you.chen92fd5d32022-05-25 10:09:47 +08001139 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001140 if (org_auth == auth) {
1141 return 0;
1142 }
1143 else {
1144 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1145 ssid[0] = '\0';
1146 }
1147 lynq_wifi_ap_frequency_get(idx, &freq);
1148
1149 DO_OK_FAIL_REQUEST(cmd_disconnect);
1150 DO_OK_FAIL_REQUEST(cmd_remove_all);
1151 if (ssid[0] != '\0') {
1152 lynq_wifi_ap_ssid_set(idx, ssid);
1153 }
1154 if (freq != 0) {
1155 lynq_wifi_ap_frequency_set(idx, freq);
1156 }
1157 }
1158 }
you.chen35020192022-05-06 11:30:57 +08001159
qs.xiong7a105ce2022-03-02 09:43:11 -05001160 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001161 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001162 {
you.chen35020192022-05-06 11:30:57 +08001163 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001164 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001165
you.chen35020192022-05-06 11:30:57 +08001166 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001167 break;
1168 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001169 case LYNQ_WIFI_AUTH_WEP:
1170 {
1171 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001172 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001173 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1174
1175 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1176 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1177 break;
1178 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001179 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001180 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001181 {
you.chen35020192022-05-06 11:30:57 +08001182 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1183 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1184 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1185 }
1186 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001187 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001188 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001189 }
1190// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1191// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1192 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001193
you.chen35020192022-05-06 11:30:57 +08001194 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1195 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1196 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001197 break;
you.chen35020192022-05-06 11:30:57 +08001198 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001199 default:
you.chen35020192022-05-06 11:30:57 +08001200 {
1201 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001202 return -1;
you.chen35020192022-05-06 11:30:57 +08001203 }
1204 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001205 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001206
qs.xiong7a105ce2022-03-02 09:43:11 -05001207 return 0;
1208}
1209
you.chen35020192022-05-06 11:30:57 +08001210int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001211{
you.chen35020192022-05-06 11:30:57 +08001212 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001213 char lynq_auth_alg_str[MAX_RET] = {0};
1214 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001215
1216 CHECK_IDX(idx, CTRL_AP);
1217
1218 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1219 return -1;
1220 }
1221
1222 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001223 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1224 *auth = LYNQ_WIFI_AUTH_OPEN;
1225 }
1226 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1227 *auth = LYNQ_WIFI_AUTH_WEP;
1228 }
1229 else {
1230 *auth = LYNQ_WIFI_AUTH_OPEN;
1231 }
you.chen35020192022-05-06 11:30:57 +08001232 }
you.chen92fd5d32022-05-25 10:09:47 +08001233 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001234 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001235 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001236 }
1237 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1238 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1239 }
1240 else {
1241 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1242 }
you.chen35020192022-05-06 11:30:57 +08001243 }
you.chen92fd5d32022-05-25 10:09:47 +08001244 else {
1245 *auth = -1;
1246 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001247
you.chen6c2dd9c2022-05-16 17:55:28 +08001248 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001249}
qs.xiong1af5daf2022-03-14 09:12:12 -04001250
qs.xiong1af5daf2022-03-14 09:12:12 -04001251
qs.xiongf1b525b2022-03-31 00:58:23 -04001252int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001253{
you.chen35020192022-05-06 11:30:57 +08001254 char LYNQ_WIFI_CMD[128]={0};
1255 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1256 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001257
you.chen35020192022-05-06 11:30:57 +08001258 CHECK_IDX(idx, CTRL_AP);
1259
1260 CHECK_WPA_CTRL(CTRL_AP);
1261
1262// system("connmanctl enable wifi");
1263// system("connmanctl tether wifi on cy-test 12345678");
1264// system("ifconfig wlan0 down");
1265// system("ifconfig wlan0 up");
1266// system("ifconfig wlan0 up");
1267
1268 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1269 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1270
1271 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1272 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1273
qs.xiong7a105ce2022-03-02 09:43:11 -05001274 return 0;
1275}
1276
qs.xiongf1b525b2022-03-31 00:58:23 -04001277int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001278{
you.chen35020192022-05-06 11:30:57 +08001279 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001280}
1281
qs.xiongf1b525b2022-03-31 00:58:23 -04001282int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001283{
you.chen35020192022-05-06 11:30:57 +08001284 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001285
you.chen35020192022-05-06 11:30:57 +08001286 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001287
you.chen35020192022-05-06 11:30:57 +08001288 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001289
you.chen35020192022-05-06 11:30:57 +08001290 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1291
1292 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1293
you.chenb4b121c2022-05-06 17:50:16 +08001294// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001295
qs.xiong7a105ce2022-03-02 09:43:11 -05001296 return 0;
1297}
qs.xiong1af5daf2022-03-14 09:12:12 -04001298
qs.xiongf1b525b2022-03-31 00:58:23 -04001299int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001300{
you.chen35020192022-05-06 11:30:57 +08001301 char lynq_disable_cmd[128] = {0};
1302 char lynq_select_cmd[128] = {0};
1303 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001304
you.chen35020192022-05-06 11:30:57 +08001305 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001306
you.chen35020192022-05-06 11:30:57 +08001307 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001308
you.chen35020192022-05-06 11:30:57 +08001309 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1310 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1311
1312 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1313 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1314 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001315
1316 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001317}
1318
qs.xiongf1b525b2022-03-31 00:58:23 -04001319int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001320{
you.chen35020192022-05-06 11:30:57 +08001321 char lynq_disable_cmd[128] = {0};
1322 char lynq_select_cmd[128] = {0};
1323 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001324
you.chen35020192022-05-06 11:30:57 +08001325 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001326
you.chen35020192022-05-06 11:30:57 +08001327 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001328
you.chen35020192022-05-06 11:30:57 +08001329 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1330 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1331
1332 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1333 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1334 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001335
1336 return 0;
1337}
qs.xiongf1b525b2022-03-31 00:58:23 -04001338
you.chen35020192022-05-06 11:30:57 +08001339int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001340{
qs.xiongf1b525b2022-03-31 00:58:23 -04001341 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001342 char lynq_tmp_cmd[MAX_CMD] = {0};
1343 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001344 if( password == NULL ){
1345 return -1;
1346 }
1347 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001348 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001349 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001350 return -1;
you.chen35020192022-05-06 11:30:57 +08001351 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001352
you.chen35020192022-05-06 11:30:57 +08001353 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001354
you.chen6c2dd9c2022-05-16 17:55:28 +08001355 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1356 return -1;
1357 }
1358 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1359 return -1;
1360 }
1361
you.chen35020192022-05-06 11:30:57 +08001362 CHECK_WPA_CTRL(CTRL_AP);
1363
you.chen6c2dd9c2022-05-16 17:55:28 +08001364 if (auth == LYNQ_WIFI_AUTH_WEP) {
1365 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1366 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1367 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1368 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1369 }
1370 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1371 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1372 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1373 }
1374 else {
1375 return -1;
1376 }
you.chen35020192022-05-06 11:30:57 +08001377
you.chen35020192022-05-06 11:30:57 +08001378 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001379
qs.xiongf1b525b2022-03-31 00:58:23 -04001380 return 0;
1381}
1382
you.chen35020192022-05-06 11:30:57 +08001383int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001384{
you.chen35020192022-05-06 11:30:57 +08001385 FILE * fp;
1386 int len, ret;
1387 int count, index;
1388 char *split_lines[128] = {0};
1389 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001390
you.chen35020192022-05-06 11:30:57 +08001391 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001392
you.chen35020192022-05-06 11:30:57 +08001393 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1394// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1395 if (NULL == fp) {
1396 printf("open file fail\n");
1397 return -1;
1398 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001399
you.chen35020192022-05-06 11:30:57 +08001400 buff = alloca(MAX_RET);
1401 fseek(fp, 0, SEEK_SET);
1402 len = fread(buff, 1, MAX_RET, fp);
1403 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001404
you.chen35020192022-05-06 11:30:57 +08001405 for(index=0; index < len; index ++) {
1406 if (memcmp(buff + index, "network={", 9) != 0) {
1407 continue;
1408 }
1409 p = buff + index + 9;
1410 for (; index < len; index ++ ) {
1411 if (buff[index] != '}') {
1412 continue;
1413 }
1414 buff[index] = '\0';
1415 break;
1416 }
1417 len = buff + index - p;
1418 }
1419
1420 count = lynq_split(p, len, '\n', split_lines);
1421
1422 ret = -1;
1423 for(index=0; index < count; index++) {
1424 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001425 if (p != NULL) {
1426 p += 4;
1427 if (*p == '\"') {
1428 p++;
1429 }
you.chen35020192022-05-06 11:30:57 +08001430 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001431 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1432 p += 9;
1433 if (*p == '\"') {
1434 p++;
1435 }
1436 }
1437 else {
1438 continue;
you.chen35020192022-05-06 11:30:57 +08001439 }
1440
1441 strcpy(password, p);
1442
1443 while(*password != '\0') {
1444 if (*password == '\"') {
1445 *password = '\0';
1446 break;
1447 }
1448 password++;
1449 }
1450 ret = 0;
1451 break;
1452 } //end for(index=0; index < count; index++)
1453
1454 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001455}
1456
you.chen35020192022-05-06 11:30:57 +08001457static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1458 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001459 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001460
you.chen35020192022-05-06 11:30:57 +08001461 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1462 return -1;
1463 }
1464
1465 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001466
1467 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1468 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1469 if (strcmp(lynq_proto_str, "RSN") == 0) {
1470 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1471 }
1472 }
1473 }
you.chen35020192022-05-06 11:30:57 +08001474 return 0;
1475}
1476
1477int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001478{
you.chen35020192022-05-06 11:30:57 +08001479 int pass_len, net_no, count, index;
1480 char lynq_tmp_cmd[300]={0};
1481 int net_no_list[128];
1482 lynq_wifi_auth_s net_auth;
1483 pass_len=strlen(password);
1484 if(pass_len < 8 || pass_len >= 64){
1485 return -1;
1486 }
1487
1488 CHECK_IDX(idx, CTRL_STA);
1489
1490 net_no = -1;
1491 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1492
1493 for (index=0; index < count; index++) {
1494 net_auth = -1;
1495 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1496 net_no = net_no_list[index];
1497 break;
1498 }
1499 }
1500
1501 if (net_no < 0) {
1502 return -1;
1503 }
1504
1505 CHECK_WPA_CTRL(CTRL_STA);
1506
1507 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1508
1509 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1510 DO_OK_FAIL_REQUEST(cmd_save_config);
1511
1512 return 0;
1513}
1514
1515int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1516
1517 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08001518 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001519 int count, index;
1520 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001521 char *buff, *p, *ssid, *ssid_end_flag;
1522 char tmp_ssid[128]={0};
you.chen35020192022-05-06 11:30:57 +08001523
you.chen755332b2022-08-06 16:59:10 +08001524 network_len = 0;
1525 p = NULL;
1526
you.chen35020192022-05-06 11:30:57 +08001527 CHECK_IDX(idx, CTRL_STA);
1528
you.chen755332b2022-08-06 16:59:10 +08001529 if (NULL == password) {
1530 printf("bad param\n");
1531 return -1;
1532 }
1533
you.chen35020192022-05-06 11:30:57 +08001534 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1535 if (NULL == fp) {
1536 printf("open file fail\n");
1537 return -1;
1538 }
1539
1540 buff = alloca(MAX_RET);
1541 fseek(fp, 0, SEEK_SET);
1542 len = fread(buff, 1, MAX_RET, fp);
1543 fclose(fp);
1544
1545 for(index=0; index < len; index ++) {
1546 for(; index < len; index ++) {
1547 if (memcmp(buff + index, "network={", 9) != 0) {
1548 continue;
1549 }
1550 p = buff + index + 9;
1551 for (; index < len; index ++ ) {
1552 if (buff[index] != '}') {
1553 continue;
1554 }
1555 buff[index] = '\0';
1556 break;
1557 }
you.chen755332b2022-08-06 16:59:10 +08001558 network_len = buff + index - p;
1559 break;
you.chen35020192022-05-06 11:30:57 +08001560 }
1561
qs.xiongb3f26af2023-02-17 18:41:07 +08001562 if (p == NULL)
1563 return -1;
1564
you.chend2fef3f2023-02-13 10:50:35 +08001565 ssid = strstr(p, "ssid=");
1566 if (ssid != NULL) {
1567 ssid += strlen("ssid=");
1568 if (ssid[0] == '\"') {
1569 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
1570 break;
1571 }
1572 else{
1573 ssid_end_flag = strstr(ssid, "\n");
1574 if (ssid_end_flag != NULL)
1575 {
1576 ssid_len = (ssid_end_flag - ssid) / 2;
1577 for(i=0; i<ssid_len; i++)
1578 {
1579 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
1580 }
1581 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
1582 break;
1583 }
1584 }
you.chen35020192022-05-06 11:30:57 +08001585 }
you.chend2fef3f2023-02-13 10:50:35 +08001586
you.chen35020192022-05-06 11:30:57 +08001587 }
1588
you.chen755332b2022-08-06 16:59:10 +08001589 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001590 return -1;
1591 }
1592
you.chen755332b2022-08-06 16:59:10 +08001593 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001594
1595 ret = -1;
1596 for(index=0; index < count; index++) {
1597 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001598 if (p != NULL) {
1599 p += 4;
1600 if (*p == '\"') {
1601 p++;
1602 }
you.chen35020192022-05-06 11:30:57 +08001603 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001604 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1605 p += 9;
1606 if (*p == '\"') {
1607 p++;
1608 }
1609 }
1610 else {
1611 continue;
you.chen35020192022-05-06 11:30:57 +08001612 }
1613
1614 strcpy(password, p);
1615
1616 while(*password != '\0') {
1617 if (*password == '\"') {
1618 *password = '\0';
1619 break;
1620 }
1621 password++;
1622 }
1623 ret = 0;
1624 break;
1625 } //end for(index=0; index < count; index++)
1626
1627 return ret;
1628}
1629
1630static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1631{
qs.xiong97fa59b2022-04-07 05:41:29 -04001632 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001633
you.chen35020192022-05-06 11:30:57 +08001634 if (sta_ssid == NULL) {
1635 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001636 return -1;
you.chen35020192022-05-06 11:30:57 +08001637 }
1638
1639 CHECK_WPA_CTRL(CTRL_STA);
1640
1641 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1642
1643 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1644// DO_OK_FAIL_REQUEST(cmd_save_config);
1645
qs.xiong7a105ce2022-03-02 09:43:11 -05001646 return 0;
1647
1648}
1649
you.chen35020192022-05-06 11:30:57 +08001650static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001651{
you.chen35020192022-05-06 11:30:57 +08001652 char lynq_disable_cmd[128]={0};
1653 char lynq_select_cmd[128]={0};
1654
1655 CHECK_WPA_CTRL(CTRL_STA);
1656
1657 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001658 if (start_flag != 0)
1659 {
1660 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1661 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1662 }
1663 else
1664 {
1665 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1666 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1667 }
you.chen35020192022-05-06 11:30:57 +08001668 DO_OK_FAIL_REQUEST(cmd_save_config);
1669 }
1670
1671 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001672 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001673 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1674 }
1675 else {
1676 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1677 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1678 }
1679
1680 return 0;
1681}
1682
1683int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1684{
1685 CHECK_IDX(idx, CTRL_STA);
1686
you.chen6c2dd9c2022-05-16 17:55:28 +08001687 curr_status_info curr_state;
1688 ap_info_s ap_info;
1689 curr_state.ap = &ap_info;
1690 curr_state.state = NULL;
1691
1692 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
you.chend2fef3f2023-02-13 10:50:35 +08001693 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08001694 return 0;
1695 }
1696
1697 return -1;
you.chen35020192022-05-06 11:30:57 +08001698}
1699
1700int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1701{
you.chen9ac66392022-08-06 17:01:16 +08001702 scan_info_s *scan_list = NULL;
1703 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001704 int scan_len=0;
1705 int save_len=0;
1706 int best_index = -1;
1707 int best_scan_index = -1;
1708 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001709 int i, j, ret;
1710
1711 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001712
1713 CHECK_IDX(idx, CTRL_STA);
1714 if (info == NULL) {
1715 return -1;
1716 }
1717
1718 curr_status_info curr_state;
1719 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001720 char status[64];
you.chen35020192022-05-06 11:30:57 +08001721
you.chen9ac66392022-08-06 17:01:16 +08001722 memset(&ap_info, 0, sizeof (ap_info));
1723 memset(status, 0, sizeof (status));
1724
1725 curr_state.ap = &ap_info;
1726 curr_state.state = status;
1727
1728 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001729 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001730 if (strcmp(status, STATE_COMPLETED) == 0)
1731 {
1732 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1733 }
1734 else
1735 {
1736 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1737 }
you.chen35020192022-05-06 11:30:57 +08001738 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001739 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001740 return 0;
1741 }
1742
you.chen9ac66392022-08-06 17:01:16 +08001743 lynq_wifi_sta_start_scan(idx);
1744
you.chen35020192022-05-06 11:30:57 +08001745 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001746 if (NULL != scan_list)
1747 {
1748 free(scan_list);
1749 }
you.chen35020192022-05-06 11:30:57 +08001750 return -1;
1751 }
1752
1753 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001754 if (NULL != scan_list)
1755 {
1756 free(scan_list);
1757 }
1758 if (NULL != save_list)
1759 {
1760 free(save_list);
1761 }
you.chen35020192022-05-06 11:30:57 +08001762 return -1;
1763 }
1764
1765 for (i=0; i < save_len; i++) {
1766 for (j=0; j < scan_len; j++) {
1767 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1768 && save_list[i].base_info.auth == scan_list[j].auth) {
1769 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001770 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001771 best_rssi = scan_list[j].rssi;
1772 }
1773 else if (best_rssi > scan_list[j].rssi) {
1774 best_index = i;
1775 best_scan_index = j;
1776 best_rssi = scan_list[j].rssi;
1777 }
you.chend2fef3f2023-02-13 10:50:35 +08001778 strncpy(save_list[i].base_info.ap_mac, scan_list[j].mac, sizeof (save_list[i].base_info.ap_mac));
you.chen35020192022-05-06 11:30:57 +08001779 break;
1780 }
1781 }
1782 }
1783
1784 if (best_index >= 0) {
1785 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08001786 inner_get_ip_by_mac( info->base_info.ap_mac, info->base_info.ap_ip, sizeof (info->base_info.ap_ip));
you.chen35020192022-05-06 11:30:57 +08001787 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1788 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001789 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001790 }
1791
you.chen9ac66392022-08-06 17:01:16 +08001792 if (NULL != scan_list)
1793 {
1794 free(scan_list);
1795 }
1796 if (NULL != save_list)
1797 {
1798 free(save_list);
1799 }
1800
1801 return ret;
you.chen35020192022-05-06 11:30:57 +08001802}
1803
1804static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1805{
1806 char lynq_auth_cmd[64]={0};
1807 char lynq_ket_mgmt_cmd[64]={0};
1808 char lynq_pairwise_cmd[64]={0};
1809 char lynq_psk_cmd[64]={0};
1810
1811 CHECK_WPA_CTRL(CTRL_STA);
1812
qs.xiong1af5daf2022-03-14 09:12:12 -04001813 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001814 case LYNQ_WIFI_AUTH_OPEN:
1815 {
1816 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001817
you.chen35020192022-05-06 11:30:57 +08001818 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1819// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001820 break;
1821 }
you.chen35020192022-05-06 11:30:57 +08001822 case LYNQ_WIFI_AUTH_WPA_PSK:
1823 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001824 {
you.chen35020192022-05-06 11:30:57 +08001825 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1826 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1827 }
1828 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001829 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001830 }
1831 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1832 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001833
you.chen35020192022-05-06 11:30:57 +08001834 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1835 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1836 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001837
you.chen35020192022-05-06 11:30:57 +08001838 if (password != NULL) {
1839 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1840 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1841 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001842
you.chen35020192022-05-06 11:30:57 +08001843// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001844 break;
you.chen35020192022-05-06 11:30:57 +08001845 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001846 default:
1847 return -1;
you.chen35020192022-05-06 11:30:57 +08001848 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001849
qs.xiongf1b525b2022-03-31 00:58:23 -04001850 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001851}
qs.xiong7a105ce2022-03-02 09:43:11 -05001852
you.chen35020192022-05-06 11:30:57 +08001853static int inner_get_curr_net_no(int interface) {
1854 curr_status_info curr_state;
1855 curr_state.ap = NULL;
1856 curr_state.state = NULL;
1857
1858 if (0 != inner_get_status_info(interface, &curr_state)) {
1859 return -1;
1860 }
1861
1862 return curr_state.net_no;
1863}
1864
1865int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001866{
you.chen35020192022-05-06 11:30:57 +08001867 int net_no;
1868 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001869
you.chen35020192022-05-06 11:30:57 +08001870 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001871
you.chen35020192022-05-06 11:30:57 +08001872 if (net_no < 0) {
1873 return -1;
1874 }
1875
1876 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001877}
1878
you.chen35020192022-05-06 11:30:57 +08001879int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
qs.xiong7a105ce2022-03-02 09:43:11 -05001880{
you.chen35020192022-05-06 11:30:57 +08001881 int count, net_no, index;
1882 int net_no_list[128];
1883 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001884
you.chen35020192022-05-06 11:30:57 +08001885 if (ssid == NULL || *ssid == '\0') {
1886 printf("bad ssid\n");
1887 return -1;
1888 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001889
you.chen35020192022-05-06 11:30:57 +08001890 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1891 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1892 printf("bad password\n");
1893 return -1;
1894 }
1895 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001896
you.chen35020192022-05-06 11:30:57 +08001897 CHECK_IDX(idx, CTRL_STA);
1898
1899 net_no = -1;
1900 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1901
1902 for (index=0; index < count; index++) {
1903 net_auth = -1;
1904 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1905 net_no = net_no_list[index];
1906 break;
1907 }
1908 }
1909
1910 if (net_no < 0) {
1911 net_no = lynq_add_network(CTRL_STA);
1912 if (net_no == -1) {
1913 return -1;
1914 }
1915
1916 printf("net no is %d\n", net_no);
1917 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1918 return -1;
1919 }
1920 }
1921
1922 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1923 return -1;
1924 }
1925
1926 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001927}
1928
you.chen35020192022-05-06 11:30:57 +08001929int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001930{
you.chen35020192022-05-06 11:30:57 +08001931 ap_info_s ap;
1932 curr_status_info curr_state;
1933 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001934
you.chen35020192022-05-06 11:30:57 +08001935 if (ssid == NULL || *ssid == '\0') {
1936 return -1;
1937 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001938
you.chen35020192022-05-06 11:30:57 +08001939 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001940
you.chen35020192022-05-06 11:30:57 +08001941 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001942 curr_state.state = NULL;
1943
you.chen35020192022-05-06 11:30:57 +08001944 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1945 return 0;
1946 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001947
you.chen35020192022-05-06 11:30:57 +08001948 if (strcmp(ap.ap_ssid, ssid) != 0) {
1949 return 0;
1950 }
1951
1952 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001953}
qs.xiong97fa59b2022-04-07 05:41:29 -04001954
you.chena6cd55a2022-05-08 12:20:18 +08001955int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1956{
qs.xiongad2f89d2023-01-18 13:17:41 +08001957// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
1958// const char *lynq_reconnect_cmd = "RECONNECT";
1959 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
1960 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
1961// const char *lynq_first_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 remove_net all";
qs.xiong7a105ce2022-03-02 09:43:11 -05001962
you.chen35020192022-05-06 11:30:57 +08001963 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001964 CHECK_WPA_CTRL(CTRL_STA);
1965
1966 system("connmanctl enable wifi");
1967
qs.xiongad2f89d2023-01-18 13:17:41 +08001968 if (system("ifconfig | grep -q wlan0") != 0)
1969 {
you.chen35020192022-05-06 11:30:57 +08001970 return -1;
1971 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001972
qs.xiongad2f89d2023-01-18 13:17:41 +08001973// DO_OK_FAIL_REQUEST(cmd_remove_all);
1974// system(lynq_first_sta_cmd);
1975// system(lynq_reconfigure_cmd);
1976// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
1977 system(lynq_enable_sta_cmd);
1978 system(lynq_reconnect_cmd);
1979// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001980 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001981}
1982
you.chen35020192022-05-06 11:30:57 +08001983int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001984{
qs.xiongad2f89d2023-01-18 13:17:41 +08001985// char lynq_disable_network_cmd[MAX_CMD];
1986// curr_status_info curr_state;
1987// ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001988
qs.xiongad2f89d2023-01-18 13:17:41 +08001989 const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all";
you.chena6cd55a2022-05-08 12:20:18 +08001990 CHECK_IDX(idx, CTRL_STA);
1991 CHECK_WPA_CTRL(CTRL_STA);
1992
qs.xiongad2f89d2023-01-18 13:17:41 +08001993// curr_state.ap = &ap_info;
1994// curr_state.state = NULL;
you.chena6cd55a2022-05-08 12:20:18 +08001995
qs.xiongad2f89d2023-01-18 13:17:41 +08001996// if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1997// return 0;
1998// }
you.chena6cd55a2022-05-08 12:20:18 +08001999
qs.xiongad2f89d2023-01-18 13:17:41 +08002000// sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
2001// DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
2002 system(lynq_disable_sta_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08002003 DO_OK_FAIL_REQUEST(cmd_save_config);
2004
2005 return 0;
2006// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04002007}
qs.xiong7a105ce2022-03-02 09:43:11 -05002008
you.chen35020192022-05-06 11:30:57 +08002009//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
2010// int i, count;
2011// char *p;
2012// const char * FLAG_SSID = "ssid=";
2013// const char * FLAG_SBSID = "bssid=";
2014// const char * FLAG_KEY_MGMT = "key_mgmt=";
2015// const char * FLAG_FREQ = "freq=";
2016// char lynq_sta_cmd[MAX_CMD];
2017// char *split_lines[128] = {0};
2018
2019// CHECK_WPA_CTRL(CTRL_AP);
2020
2021// sprintf(lynq_sta_cmd, "STA %s", bssid);
2022
2023// DO_REQUEST(lynq_sta_cmd);
2024
2025// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2026
2027// for(i=0; i < count; i++) {
2028// p = strstr(split_lines[i], FLAG_SSID);
2029// if (p != NULL) {
2030// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
2031// continue;
2032// }
2033// }
2034
2035// lynq_get_interface_ip(idx, ap->ap_ip);
2036// lynq_ap_password_set(idx, ap->psw);
2037
2038// return 0;
2039//}
2040
2041static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
2042 curr_status_info curr_state;
2043 curr_state.ap = ap;
2044 curr_state.state = NULL;
2045 return inner_get_status_info(interface, &curr_state);
2046}
2047
2048int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002049{
you.chend2fef3f2023-02-13 10:50:35 +08002050 int index, line_count;
2051 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08002052 const char *lynq_first_sta_cmd = "STA-FIRST";
2053 char lynq_next_sta_cmd[MAX_CMD];
2054 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08002055 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002056
you.chen35020192022-05-06 11:30:57 +08002057 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002058
you.chen35020192022-05-06 11:30:57 +08002059 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002060
you.chen35020192022-05-06 11:30:57 +08002061// ap_info_s * tmp_ap;
2062// device_info_s * tmp_list;
2063 if (ap == NULL || list == NULL || len == NULL) {
2064 printf("bad input param");
2065 return -1;
2066 }
2067
2068// ap = &tmp_ap;
2069// list = &tmp_list;
2070 *ap = malloc(sizeof (ap_info_s));
2071
2072 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
2073 return -1;
2074 }
2075
2076 lynq_get_interface_ip(idx, (*ap)->ap_ip);
2077 lynq_ap_password_get(idx, (*ap)->psw);
2078
you.chen35020192022-05-06 11:30:57 +08002079 DO_REQUEST(lynq_first_sta_cmd);
2080
2081 index = 0;
2082 while (reply_len > 0) {
2083 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2084 break;
2085 }
2086 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2087 bssid[index] = malloc(strlen(split_lines[0]) + 1);
2088 strcpy(bssid[index], split_lines[0]);
2089 index++;
2090 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
2091 reply_len = MAX_RET;
2092 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08002093 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
you.chen9ac66392022-08-06 17:01:16 +08002094 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
2095 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08002096 break;
2097 }
2098 }
2099
2100 *len = index;
2101
2102 *list = malloc(sizeof(device_info_s) * (*len));
2103 for (index=0; index < *len; index++) {
you.chend2fef3f2023-02-13 10:50:35 +08002104 dev_info = &(*list)[index];
2105 memset(dev_info, 0, sizeof(device_info_s));
2106 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
2107 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
2108 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
2109 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08002110 free(bssid[index]);
2111 }
2112
2113 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002114}
2115
you.chen35020192022-05-06 11:30:57 +08002116int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002117{
you.chen35020192022-05-06 11:30:57 +08002118 int i, count, index, count_words;
2119 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2120 char *split_lines[128] = {0};
2121 char *split_words[128] = {0};
2122 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04002123
you.chen35020192022-05-06 11:30:57 +08002124 if (list == NULL || len == NULL) {
2125 return -1;
2126 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002127
you.chen9ac66392022-08-06 17:01:16 +08002128 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2129 {
2130 usleep(100 * 1000);
2131 }
2132
you.chen35020192022-05-06 11:30:57 +08002133 CHECK_IDX(idx, CTRL_STA);
2134
2135 CHECK_WPA_CTRL(CTRL_STA);
2136
2137 DO_REQUEST(lynq_scan_result_cmd);
2138
2139 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2140 *len = count - 1;
2141 *list = malloc(sizeof (scan_info_s) * *len);
2142
2143 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2144 for (index=0; index <count_words; index++) {
2145 printf("----header: %s\n", split_words[index]);
2146 }
2147
2148 for(index = 1;index < count; index++) {
2149 printf("---- %s\n",split_lines[index]);
2150 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2151 if (count_words < 4)
2152 continue;
2153 printf("count: %d, %s\n", count_words, split_words[0]);
2154 //bssid / frequency / signal level / flags / ssid
2155 p = (*list) + index - 1;
2156 strcpy(p->mac, split_words[0]);
2157 p->band = convert_band_from_freq(atoi(split_words[1]));
2158 p->rssi = -1 * atoi( split_words[2]);
2159 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chend2fef3f2023-02-13 10:50:35 +08002160 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
you.chen35020192022-05-06 11:30:57 +08002161 }
2162
2163 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002164}
qs.xiong97fa59b2022-04-07 05:41:29 -04002165
you.chen35020192022-05-06 11:30:57 +08002166int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2167{
2168 int count, net_no, index;
2169 int net_no_list[128];
2170 lynq_wifi_auth_s net_auth;
2171 char lynq_remove_cmd[MAX_CMD];
2172
2173 if (ssid == NULL || *ssid == '\0') {
2174 printf("bad ssid\n");
2175 return -1;
2176 }
2177
2178 CHECK_IDX(idx, CTRL_STA);
2179
2180 CHECK_WPA_CTRL(CTRL_STA);
2181
2182 net_no = -1;
2183 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2184
2185 for (index=0; index < count; index++) {
2186 net_auth = -1;
2187 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2188 net_no = net_no_list[index];
2189 break;
2190 }
2191 }
2192
2193 if (net_no < 0) {
2194 return 0;
2195 }
2196
2197 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2198
2199 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2200 DO_OK_FAIL_REQUEST(cmd_save_config);
2201
2202 return 0;
2203}
2204
2205int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2206{
you.chend2fef3f2023-02-13 10:50:35 +08002207 int count, index;
you.chen35020192022-05-06 11:30:57 +08002208 int net_no_list[128];
2209 char freq[16];
2210
2211 if (list == NULL || len == NULL) {
2212 printf("bad param\n");
2213 return -1;
2214 }
2215
2216 CHECK_IDX(idx, CTRL_STA);
2217
2218// CHECK_WPA_CTRL(CTRL_STA);
2219
2220 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2221 printf("count is %d\n", count);
2222
2223 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002224 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002225 *len = count;
2226
2227 for (index=0; index < count; index++) {
2228 printf("to get ssid %d\n", index);
2229 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002230 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002231 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002232 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2233 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2234 }
2235 else {
2236 (*list)[index].base_info.band = -1;
2237 }
2238
you.chen755332b2022-08-06 16:59:10 +08002239 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002240 }
2241
2242 return 0;
2243}
2244
2245int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2246{
2247 const char *lynq_scan_cmd = "SCAN";
2248
2249 CHECK_IDX(idx, CTRL_STA);
2250
2251 CHECK_WPA_CTRL(CTRL_STA);
2252
you.chen9ac66392022-08-06 17:01:16 +08002253 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08002254 DO_REQUEST(lynq_scan_cmd);
2255 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 ) {
2256 return 0;
2257 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) {
2258 g_sta_scan_finish_flag = 1;
2259 return -1;
2260 }
you.chen35020192022-05-06 11:30:57 +08002261
2262 return 0;
2263}
2264
2265int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2266 if (cb == NULL) {
2267 return -1;
2268 }
2269
2270 g_ap_callback_priv = priv;
2271 g_ap_callback_func = cb;
2272
2273 return 0;
2274}
2275
2276int lynq_unreg_ap_event_callback(void * priv) {
2277 if (g_ap_callback_priv == priv) {
2278 g_ap_callback_func = NULL;
2279 g_ap_callback_priv = NULL;
2280 return 0;
2281 }
2282 return -1;
2283}
2284
2285int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2286 if (cb == NULL) {
2287 return -1;
2288 }
2289
2290 g_sta_callback_priv = priv;
2291 g_sta_callback_func = cb;
2292
2293 return 0;
2294}
2295
2296int lynq_unreg_sta_event_callback(void * priv) {
2297 if (g_sta_callback_priv == priv) {
2298 g_sta_callback_func = NULL;
2299 g_sta_callback_priv = NULL;
2300 return 0;
2301 }
2302 return -1;
2303}
2304
2305
2306static int inner_get_status_info_state (int interface, char *state) {
2307 curr_status_info curr_state;
2308 curr_state.ap = NULL;
2309 curr_state.state = state;
2310 return inner_get_status_info(interface, &curr_state);
2311}
2312
2313int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2314{
2315 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002316 CHECK_IDX(idx, CTRL_AP);
2317
2318 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2319 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2320 return 0;
2321 }
2322
2323 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2324 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2325 }
2326 else {
2327 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2328 }
2329
2330 return 0;
2331}
2332
2333int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2334 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002335 CHECK_IDX(idx, CTRL_STA);
2336
2337 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2338 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2339 return 0;
2340 }
2341
2342 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2343 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2344 }
2345 else {
2346 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2347 }
2348
2349 return 0;
2350}
2351
2352int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2353// CHECK_IDX(idx, CTRL_AP);
2354// int ret = 0;
2355// size_t reply_len = MAX_RET;
2356// char cmd_reply[MAX_RET]={0};
2357// const char * cmd_str = "GET country";
2358// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2359// do{
2360// if (NULL == s_lynq_wpa_ctrl) {
2361// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2362// if (NULL == s_lynq_wpa_ctrl ) {
2363// printf("wpa_ctrl_open fail\n");
2364// return -1;
2365// }
2366// }
2367// }while(0);
2368
2369// do {
2370// reply_len = MAX_RET;
2371// cmd_reply[0] = '\0';
2372// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08002373// ret = local_wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
you.chen35020192022-05-06 11:30:57 +08002374// if (ret != 0) {
2375// printf("call ##cmd_str fail %d\n", ret);
2376// return ret;
2377// }
2378// cmd_reply[reply_len+1] = '\0';
2379// printf("cmd replay [ %s ]\n", cmd_reply);
2380// }while(0);
2381
2382 FILE *fp;
2383 size_t i = 0;
2384 char lynq_cmd_ret[MAX_RET]={0};
2385
2386// CHECK_IDX(idx, CTRL_AP);
2387
2388 if((fp=popen("wl country","r"))==NULL)
2389 {
2390 perror("popen error!");
2391 return -1;
2392 }
2393 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2394 {
2395 perror("fread fail!");
2396 return -1;
2397 }
2398
2399 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2400 if (lynq_cmd_ret[i] == ' ') {
2401 lynq_cmd_ret[i] = '\0';
2402 break;
2403 }
2404 }
2405
2406 strcpy(country_code,lynq_cmd_ret);
2407 printf("---country code %s\n", country_code);
2408
2409 int ret=pclose(fp);
2410 if(ret==-1)
2411 {
2412 perror("close file faild");
2413 }
2414
2415 return 0;
2416}
2417
2418int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2419// const char * cmd_str = "GET country";
2420// CHECK_IDX(idx, CTRL_AP);
2421// CHECK_WPA_CTRL(CTRL_STA);
2422
2423// DO_REQUEST(cmd_str);
2424// printf("result %s\n", cmd_reply);
2425
2426 if (country_code == NULL || *country_code == '\0') {
2427 printf("bad country code\n");
2428 return -1;
2429 }
2430
2431 char lynq_country_cmd[MAX_CMD];
2432 sprintf(lynq_country_cmd, "wl country %s", country_code);
2433 if (system(lynq_country_cmd) == 0) {
2434 return 0;
2435 }
2436
2437 return -1;
2438}
2439
2440int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2441{
2442 if (mac == NULL) {
2443 return -1;
2444 }
2445
2446 CHECK_IDX(idx, CTRL_STA);
2447 ap_info_s ap;
2448 ap.ap_mac[0] = '\0';
2449
2450 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2451 return -1;
2452 }
2453 strcpy(mac, ap.ap_mac);
2454
2455 return 0;
2456}
2457
2458int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2459{
you.chen9ac66392022-08-06 17:01:16 +08002460 struct ifaddrs *ifaddr_header, *ifaddr;
2461 struct in_addr * ifa;
2462 const char * ifaName = "wlan0";
2463 if (ip == NULL)
2464 {
you.chenf58b3c92022-06-21 16:53:48 +08002465 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002466 }
you.chenf58b3c92022-06-21 16:53:48 +08002467
you.chen9ac66392022-08-06 17:01:16 +08002468 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002469 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002470 }
2471 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002472 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002473 }
you.chen35020192022-05-06 11:30:57 +08002474
you.chen9ac66392022-08-06 17:01:16 +08002475 if (getifaddrs(&ifaddr_header) == -1)
2476 {
you.chen35020192022-05-06 11:30:57 +08002477 perror("getifaddrs");
2478 return -1;
2479 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002480 }
you.chen35020192022-05-06 11:30:57 +08002481
2482
you.chen9ac66392022-08-06 17:01:16 +08002483 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2484 {
2485 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002486 continue;
you.chen9ac66392022-08-06 17:01:16 +08002487 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2488 {
2489 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2490 {
2491 // is a valid IP4 Address
2492 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2493 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2494 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2495 freeifaddrs(ifaddr_header);
2496 printf("ip %s\n", ip);
2497 return 0;
2498 }
2499 }
2500 }
qs.xiongc4f007c2023-02-08 18:16:58 +08002501 freeifaddrs(ifaddr_header);
you.chen9ac66392022-08-06 17:01:16 +08002502 return -1;
you.chen35020192022-05-06 11:30:57 +08002503}
2504
2505int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2506{
2507 int count;
2508 size_t i;
2509 char *split_words[128] = {0};
2510 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2511
2512 CHECK_WPA_CTRL(idx);
2513
2514 DO_REQUEST(lynq_get_mac_cmd);
2515
2516 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2517 return -1;
2518 }
2519
2520 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2521
2522 if (count < 2) {
2523 return -1;
2524 }
2525
2526 for (i=0; i < strlen(split_words[1]); i++ ) {
2527 if (split_words[1][i] != ' ') {
2528 break;
2529 }
2530 }
2531
2532 strcpy(mac, split_words[1] + i);
2533
2534 return 0;
2535}
2536
2537int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2538{
2539// int count;
2540// char *split_words[128] = {0};
2541// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2542
2543// if (rssi == NULL) {
2544// return -1;
2545// }
2546
2547// CHECK_IDX(idx, CTRL_STA);
2548
2549// CHECK_WPA_CTRL(CTRL_STA);
2550
2551// DO_REQUEST(lynq_get_rssi_cmd);
2552
2553// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2554// return -1;
2555// }
2556
2557// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2558
2559// if (count < 2) {
2560// return -1;
2561// }
2562
2563// *rssi = atoi(split_words[1]) * -1;
2564
2565 FILE *fp;
2566 size_t i = 0;
2567 char lynq_cmd_ret[MAX_RET]={0};
2568
2569// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002570/*******change other cmd to get rssi*******
2571 *
2572 *wl rssi ---> wl -i wlan0 rssi
2573 *
2574 ***** change by qs.xiong 20221011*******/
2575 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002576 {
2577 perror("popen error!");
2578 return -1;
2579 }
2580 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2581 {
2582 perror("fread fail!");
2583 return -1;
2584 }
you.chen9f17e4d2022-06-06 17:18:18 +08002585 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002586/****** if got rssi is 0,means sta didn't connected any device****/
2587 if(*rssi == 0)
2588 {
2589 printf("sta didn't connected any ap device,please check connection\n");
2590 }
you.chen35020192022-05-06 11:30:57 +08002591
2592 return 0;
2593}
2594
2595int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2596{
2597 if (band == NULL) {
2598 return -1;
2599 }
2600
2601 CHECK_IDX(idx, CTRL_STA);
2602 ap_info_s ap;
2603 ap.band = -1;
2604
2605 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2606 return -1;
2607 }
2608 *band = ap.band;
2609
2610 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002611}
you.chenf58b3c92022-06-21 16:53:48 +08002612
2613int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2614{
you.chenf58b3c92022-06-21 16:53:48 +08002615 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08002616
2617 if (ip == NULL)
2618 {
2619 printf("invalid param");
2620 return -1;
2621 }
2622
2623 CHECK_IDX(idx, CTRL_STA);
2624
2625 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2626 {
2627 return -1;
2628 }
2629
you.chend2fef3f2023-02-13 10:50:35 +08002630 return inner_get_ip_by_mac(bssid, ip, 32); //better input by user
you.chenf58b3c92022-06-21 16:53:48 +08002631}
2632
qs.xiong026c5c72022-10-17 11:15:45 +08002633int lynq_ap_connect_num(int sta_number)
2634{
2635 char lynq_limit_cmd[32]={0};
2636 int ret;
2637 if((sta_number < 1 ) && (sta_number > 15)){
2638 printf("sta_number: not in range\n",sta_number);
2639 return -1;
2640 }
2641 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2642 ret = system(lynq_limit_cmd);
2643 if(ret != 0){
2644 printf("cmd of limit ap devices number error\n");
2645 }
2646 return 0;
2647}
you.chenf58b3c92022-06-21 16:53:48 +08002648
qs.xiong77905552022-10-17 11:19:57 +08002649int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
2650{
2651
2652 char lynq_wifi_acs_cmd[128]={0};
2653 char lynq_cmd_mode[128]={0};
2654 char lynq_cmd_slect[128]={0};
2655
2656 if((acs_mode != 2) && (acs_mode != 5)){
2657 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
2658 }
2659
2660 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
2661 return -1;
2662 }
2663
2664 CHECK_IDX(idx, CTRL_AP);
2665
2666 CHECK_WPA_CTRL(CTRL_AP);
2667
2668 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
2669 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2670 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2671
2672 DO_OK_FAIL_REQUEST(cmd_disconnect);
2673 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
2674 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2675 DO_OK_FAIL_REQUEST(cmd_save_config);
2676 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
2677
2678 return 0;
2679}
you.chen0f5c6432022-11-07 18:31:14 +08002680//you.chen add for tv-box start
2681static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
2682 FILE *fp;
2683 //printf("to exec cmd:%s\n", str_cmd);
2684 if((fp=popen(str_cmd,"r"))==NULL)
2685 {
2686 perror("popen error!");
2687 return -1;
2688 }
2689 if((fread(str_cmd_ret,max_len,1,fp))<0)
2690 {
2691 perror("fread fail!");
2692 fclose(fp);
2693 return -1;
2694 }
2695 fclose(fp);
2696 return 0;
2697}
2698
2699static int get_netmask_length(const char* mask)
2700{
2701 int masklen=0, i=0;
2702 int netmask=0;
2703
2704 if(mask == NULL)
2705 {
2706 return 0;
2707 }
2708
2709 struct in_addr ip_addr;
2710 if( inet_aton(mask, &ip_addr) )
2711 {
2712 netmask = ntohl(ip_addr.s_addr);
2713 }else{
2714 netmask = 0;
2715 return 0;
2716 }
2717
2718 while(0 == (netmask & 0x01) && i<32)
2719 {
2720 i++;
2721 netmask = netmask>>1;
2722 }
2723 masklen = 32-i;
2724 return masklen;
2725}
2726
2727static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
2728 int mask_len;
2729 char *p;
2730 char tmp[64] = {0};
2731 if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0)
2732 return -1;
2733 p = strstr(str_cmd_ret, "Mask:");
2734 if (p == NULL)
2735 return -1;
2736 mask_len = get_netmask_length(p + 5);
2737 if (mask_len == 0)
2738 return -1;
2739 p = strstr(str_cmd_ret, "inet addr:");
2740 if (p == NULL)
2741 return -1;
2742 strcpy(tmp, p + 10);
2743 p = strstr(tmp, " ");
2744 if (p != NULL)
2745 *p = '\0';
2746 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
2747 return 0;
2748}
2749
2750static void GBWWatchThreadProc() {
2751 int i,n, nloop, nmax, ncheckcount, nidlecount;
2752 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
2753 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
2754 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
2755 char *results[16] = {0};
2756 char str_cmd[256] = {0};
2757 char str_cmd_ret[128] = {0};
2758 char dest_ip[32] = {0};
2759 lastAP1Bytes = lastAP2Bytes = 0;
2760 lastAP1Drop = lastAP2Drop = 0;
2761 lastAP1Speed = lastAP2Speed = 0;
2762 setAP1Speed = 50;
2763 setAP2Speed = 80;
2764 nloop = 0;
2765 nmax = 6;
2766 ncheckcount = nidlecount = 0;
2767
2768 printf("------gbw thread run\n");
2769 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
2770 while (dest_ip[0] == '\0') {
2771 sleep(1);
2772 str_cmd_ret[0] = '\0';
2773 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
2774 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
2775 if (str_cmd_ret[n] == '\n'){
2776 str_cmd_ret[n] = '\0';
2777 break;
2778 }
2779 }
2780 if (str_cmd_ret[0] != '\0')
2781 {
2782 strcpy(dest_ip, str_cmd_ret);
2783 }
2784 }
2785
2786 system("tc qdisc del dev tether root > /dev/null 2>&1");
2787 system("tc qdisc add dev tether root handle 1: htb r2q 1");
2788 system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000");
2789 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
2790 {
2791 printf("not get tether info\n");
2792 return;
2793 }
2794 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", str_cmd_ret);
2795 system(str_cmd);
2796 system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000");
2797 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip);
2798 //printf("----cmd:%s\n", str_cmd);
2799 system(str_cmd);
2800
2801 while (1) {
2802 sleep(1);
2803 memset(str_cmd, 0, sizeof(str_cmd));
2804 if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd)))
2805 continue;
2806 //printf("ap1 --- %s\n", str_cmd);
2807 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2808 if (n > 9) {
2809 if (strcmp(results[1], "Sent") == 0) {
2810 currAP1Bytes = atoll(results[2]);
2811 }
2812 if (strcmp(results[6], "(dropped") == 0) {
2813 currAP1Drop = atoi(results[7]);
2814 }
2815 }
2816
2817 memset(str_cmd, 0, sizeof(str_cmd));
2818 if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd)))
2819 continue;
2820 //printf("ap2 --- %s\n", str_cmd);
2821 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2822 if (n > 9) {
2823 if (strcmp(results[1], "Sent") == 0) {
2824 currAP2Bytes = atoll(results[2]);
2825 }
2826 if (strcmp(results[6], "(dropped") == 0) {
2827 currAP2Drop = atoi(results[7]);
2828 }
2829 }
2830
2831 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
2832 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
2833 lastAP1Bytes = currAP1Bytes;
2834 lastAP2Bytes = currAP2Bytes;
2835 continue;
2836 }
2837
2838 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
2839 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
2840 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
2841 lastAP1Speed = currAP1Speed;
2842 lastAP2Speed = currAP2Speed;
2843 lastAP1Bytes = currAP1Bytes;
2844 lastAP2Bytes = currAP2Bytes;
2845
2846 currSetAP1Speed = setAP1Speed;
2847 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
2848 ncheckcount++;
2849 if (ncheckcount > 3) {
2850 ncheckcount = 0;
2851 currSetAP1Speed = 5;
2852 }
2853 }
2854 else {
2855 ncheckcount = 0;
2856 if (currAP1Speed < 5)
2857 nidlecount++;
2858 else
2859 nidlecount = 0;
2860
2861 }
2862
2863 if (nidlecount > 60 ){
2864 currSetAP1Speed = 50;
2865 }
2866
2867 if (currSetAP1Speed != setAP1Speed) {
2868 setAP1Speed = currSetAP1Speed;
2869 sprintf(str_cmd, "tc class replace dev tether parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000", setAP1Speed, (int)(setAP1Speed*1.4));
2870 //printf("------***change speed: %s\n", str_cmd);
2871 system(str_cmd);
2872 }
2873 }
2874}
2875
2876int enableGBW(const char* mac) {
2877 int i,len;
2878 char get_ipaddr_cmd[128]={0};
2879 ap_info_s *ap;
2880 device_info_s * list;
2881
2882 if (mac == NULL || g_gbw_enabled == 1)
2883 return -1;
2884 len = strlen(mac);
2885 g_gbw_mac = malloc(len + 1);
2886 for(i=0;i<len;i++) {
2887 if (mac[i] >= 'A' && mac[i] <= 'Z')
2888 {
2889 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
2890 }
2891 else
2892 g_gbw_mac[i] = mac[i];
2893 }
2894 g_gbw_mac[i] = '\0';
2895 g_gbw_enabled = 1;
2896
2897 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
2898 if (system(get_ipaddr_cmd) == 0) {
2899 //startGBW();
2900 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
2901 for (i=0;i<len;i++) {
2902 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
2903 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
2904 startGBW();
2905 }
2906 free(ap);
2907 free(list);
2908 }
2909 }
2910 return 0;
2911}
2912
2913int disableGBW() {
2914 stopGBW();
2915 free(g_gbw_mac);
2916 g_gbw_mac = NULL;
2917 g_gbw_enabled = 1;
2918 return 0;
2919}
2920
2921static int startGBW() {
2922 if (g_gbw_watcher_pid != 0) {
2923 stopGBW();
2924 }
2925 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
2926}
2927
2928static int stopGBW() {
2929 void* retval;
2930 pthread_cancel(g_gbw_watcher_pid);
2931 pthread_join(g_gbw_watcher_pid, &retval);
2932 g_gbw_watcher_pid = 0;
2933 system("tc qdisc del dev tether root");
2934}
2935//you.chen add for tv-box end