blob: cb85035b946e2cd7c35ebc773aeeae1fa1310537 [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
you.chend2fef3f2023-02-13 10:50:35 +08001562 ssid = strstr(p, "ssid=");
1563 if (ssid != NULL) {
1564 ssid += strlen("ssid=");
1565 if (ssid[0] == '\"') {
1566 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
1567 break;
1568 }
1569 else{
1570 ssid_end_flag = strstr(ssid, "\n");
1571 if (ssid_end_flag != NULL)
1572 {
1573 ssid_len = (ssid_end_flag - ssid) / 2;
1574 for(i=0; i<ssid_len; i++)
1575 {
1576 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
1577 }
1578 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
1579 break;
1580 }
1581 }
you.chen35020192022-05-06 11:30:57 +08001582 }
you.chend2fef3f2023-02-13 10:50:35 +08001583
you.chen35020192022-05-06 11:30:57 +08001584 }
1585
you.chen755332b2022-08-06 16:59:10 +08001586 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001587 return -1;
1588 }
1589
you.chen755332b2022-08-06 16:59:10 +08001590 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001591
1592 ret = -1;
1593 for(index=0; index < count; index++) {
1594 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001595 if (p != NULL) {
1596 p += 4;
1597 if (*p == '\"') {
1598 p++;
1599 }
you.chen35020192022-05-06 11:30:57 +08001600 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001601 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1602 p += 9;
1603 if (*p == '\"') {
1604 p++;
1605 }
1606 }
1607 else {
1608 continue;
you.chen35020192022-05-06 11:30:57 +08001609 }
1610
1611 strcpy(password, p);
1612
1613 while(*password != '\0') {
1614 if (*password == '\"') {
1615 *password = '\0';
1616 break;
1617 }
1618 password++;
1619 }
1620 ret = 0;
1621 break;
1622 } //end for(index=0; index < count; index++)
1623
1624 return ret;
1625}
1626
1627static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1628{
qs.xiong97fa59b2022-04-07 05:41:29 -04001629 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001630
you.chen35020192022-05-06 11:30:57 +08001631 if (sta_ssid == NULL) {
1632 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001633 return -1;
you.chen35020192022-05-06 11:30:57 +08001634 }
1635
1636 CHECK_WPA_CTRL(CTRL_STA);
1637
1638 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1639
1640 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1641// DO_OK_FAIL_REQUEST(cmd_save_config);
1642
qs.xiong7a105ce2022-03-02 09:43:11 -05001643 return 0;
1644
1645}
1646
you.chen35020192022-05-06 11:30:57 +08001647static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001648{
you.chen35020192022-05-06 11:30:57 +08001649 char lynq_disable_cmd[128]={0};
1650 char lynq_select_cmd[128]={0};
1651
1652 CHECK_WPA_CTRL(CTRL_STA);
1653
1654 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001655 if (start_flag != 0)
1656 {
1657 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1658 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1659 }
1660 else
1661 {
1662 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1663 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1664 }
you.chen35020192022-05-06 11:30:57 +08001665 DO_OK_FAIL_REQUEST(cmd_save_config);
1666 }
1667
1668 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001669 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001670 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1671 }
1672 else {
1673 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1674 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1675 }
1676
1677 return 0;
1678}
1679
1680int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1681{
1682 CHECK_IDX(idx, CTRL_STA);
1683
you.chen6c2dd9c2022-05-16 17:55:28 +08001684 curr_status_info curr_state;
1685 ap_info_s ap_info;
1686 curr_state.ap = &ap_info;
1687 curr_state.state = NULL;
1688
1689 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
you.chend2fef3f2023-02-13 10:50:35 +08001690 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08001691 return 0;
1692 }
1693
1694 return -1;
you.chen35020192022-05-06 11:30:57 +08001695}
1696
1697int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1698{
you.chen9ac66392022-08-06 17:01:16 +08001699 scan_info_s *scan_list = NULL;
1700 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001701 int scan_len=0;
1702 int save_len=0;
1703 int best_index = -1;
1704 int best_scan_index = -1;
1705 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001706 int i, j, ret;
1707
1708 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001709
1710 CHECK_IDX(idx, CTRL_STA);
1711 if (info == NULL) {
1712 return -1;
1713 }
1714
1715 curr_status_info curr_state;
1716 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001717 char status[64];
you.chen35020192022-05-06 11:30:57 +08001718
you.chen9ac66392022-08-06 17:01:16 +08001719 memset(&ap_info, 0, sizeof (ap_info));
1720 memset(status, 0, sizeof (status));
1721
1722 curr_state.ap = &ap_info;
1723 curr_state.state = status;
1724
1725 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001726 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001727 if (strcmp(status, STATE_COMPLETED) == 0)
1728 {
1729 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1730 }
1731 else
1732 {
1733 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1734 }
you.chen35020192022-05-06 11:30:57 +08001735 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001736 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001737 return 0;
1738 }
1739
you.chen9ac66392022-08-06 17:01:16 +08001740 lynq_wifi_sta_start_scan(idx);
1741
you.chen35020192022-05-06 11:30:57 +08001742 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001743 if (NULL != scan_list)
1744 {
1745 free(scan_list);
1746 }
you.chen35020192022-05-06 11:30:57 +08001747 return -1;
1748 }
1749
1750 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001751 if (NULL != scan_list)
1752 {
1753 free(scan_list);
1754 }
1755 if (NULL != save_list)
1756 {
1757 free(save_list);
1758 }
you.chen35020192022-05-06 11:30:57 +08001759 return -1;
1760 }
1761
1762 for (i=0; i < save_len; i++) {
1763 for (j=0; j < scan_len; j++) {
1764 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1765 && save_list[i].base_info.auth == scan_list[j].auth) {
1766 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001767 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001768 best_rssi = scan_list[j].rssi;
1769 }
1770 else if (best_rssi > scan_list[j].rssi) {
1771 best_index = i;
1772 best_scan_index = j;
1773 best_rssi = scan_list[j].rssi;
1774 }
you.chend2fef3f2023-02-13 10:50:35 +08001775 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 +08001776 break;
1777 }
1778 }
1779 }
1780
1781 if (best_index >= 0) {
1782 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08001783 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 +08001784 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1785 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001786 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001787 }
1788
you.chen9ac66392022-08-06 17:01:16 +08001789 if (NULL != scan_list)
1790 {
1791 free(scan_list);
1792 }
1793 if (NULL != save_list)
1794 {
1795 free(save_list);
1796 }
1797
1798 return ret;
you.chen35020192022-05-06 11:30:57 +08001799}
1800
1801static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1802{
1803 char lynq_auth_cmd[64]={0};
1804 char lynq_ket_mgmt_cmd[64]={0};
1805 char lynq_pairwise_cmd[64]={0};
1806 char lynq_psk_cmd[64]={0};
1807
1808 CHECK_WPA_CTRL(CTRL_STA);
1809
qs.xiong1af5daf2022-03-14 09:12:12 -04001810 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001811 case LYNQ_WIFI_AUTH_OPEN:
1812 {
1813 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001814
you.chen35020192022-05-06 11:30:57 +08001815 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1816// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001817 break;
1818 }
you.chen35020192022-05-06 11:30:57 +08001819 case LYNQ_WIFI_AUTH_WPA_PSK:
1820 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001821 {
you.chen35020192022-05-06 11:30:57 +08001822 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1823 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1824 }
1825 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001826 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001827 }
1828 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1829 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001830
you.chen35020192022-05-06 11:30:57 +08001831 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1832 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1833 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001834
you.chen35020192022-05-06 11:30:57 +08001835 if (password != NULL) {
1836 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1837 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1838 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001839
you.chen35020192022-05-06 11:30:57 +08001840// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001841 break;
you.chen35020192022-05-06 11:30:57 +08001842 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001843 default:
1844 return -1;
you.chen35020192022-05-06 11:30:57 +08001845 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001846
qs.xiongf1b525b2022-03-31 00:58:23 -04001847 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001848}
qs.xiong7a105ce2022-03-02 09:43:11 -05001849
you.chen35020192022-05-06 11:30:57 +08001850static int inner_get_curr_net_no(int interface) {
1851 curr_status_info curr_state;
1852 curr_state.ap = NULL;
1853 curr_state.state = NULL;
1854
1855 if (0 != inner_get_status_info(interface, &curr_state)) {
1856 return -1;
1857 }
1858
1859 return curr_state.net_no;
1860}
1861
1862int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001863{
you.chen35020192022-05-06 11:30:57 +08001864 int net_no;
1865 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001866
you.chen35020192022-05-06 11:30:57 +08001867 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001868
you.chen35020192022-05-06 11:30:57 +08001869 if (net_no < 0) {
1870 return -1;
1871 }
1872
1873 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001874}
1875
you.chen35020192022-05-06 11:30:57 +08001876int 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 -05001877{
you.chen35020192022-05-06 11:30:57 +08001878 int count, net_no, index;
1879 int net_no_list[128];
1880 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001881
you.chen35020192022-05-06 11:30:57 +08001882 if (ssid == NULL || *ssid == '\0') {
1883 printf("bad ssid\n");
1884 return -1;
1885 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001886
you.chen35020192022-05-06 11:30:57 +08001887 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1888 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1889 printf("bad password\n");
1890 return -1;
1891 }
1892 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001893
you.chen35020192022-05-06 11:30:57 +08001894 CHECK_IDX(idx, CTRL_STA);
1895
1896 net_no = -1;
1897 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1898
1899 for (index=0; index < count; index++) {
1900 net_auth = -1;
1901 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1902 net_no = net_no_list[index];
1903 break;
1904 }
1905 }
1906
1907 if (net_no < 0) {
1908 net_no = lynq_add_network(CTRL_STA);
1909 if (net_no == -1) {
1910 return -1;
1911 }
1912
1913 printf("net no is %d\n", net_no);
1914 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1915 return -1;
1916 }
1917 }
1918
1919 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1920 return -1;
1921 }
1922
1923 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001924}
1925
you.chen35020192022-05-06 11:30:57 +08001926int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001927{
you.chen35020192022-05-06 11:30:57 +08001928 ap_info_s ap;
1929 curr_status_info curr_state;
1930 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001931
you.chen35020192022-05-06 11:30:57 +08001932 if (ssid == NULL || *ssid == '\0') {
1933 return -1;
1934 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001935
you.chen35020192022-05-06 11:30:57 +08001936 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001937
you.chen35020192022-05-06 11:30:57 +08001938 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001939 curr_state.state = NULL;
1940
you.chen35020192022-05-06 11:30:57 +08001941 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1942 return 0;
1943 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001944
you.chen35020192022-05-06 11:30:57 +08001945 if (strcmp(ap.ap_ssid, ssid) != 0) {
1946 return 0;
1947 }
1948
1949 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001950}
qs.xiong97fa59b2022-04-07 05:41:29 -04001951
you.chena6cd55a2022-05-08 12:20:18 +08001952int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1953{
qs.xiongad2f89d2023-01-18 13:17:41 +08001954// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
1955// const char *lynq_reconnect_cmd = "RECONNECT";
1956 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
1957 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
1958// 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 -05001959
you.chen35020192022-05-06 11:30:57 +08001960 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001961 CHECK_WPA_CTRL(CTRL_STA);
1962
1963 system("connmanctl enable wifi");
1964
qs.xiongad2f89d2023-01-18 13:17:41 +08001965 if (system("ifconfig | grep -q wlan0") != 0)
1966 {
you.chen35020192022-05-06 11:30:57 +08001967 return -1;
1968 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001969
qs.xiongad2f89d2023-01-18 13:17:41 +08001970// DO_OK_FAIL_REQUEST(cmd_remove_all);
1971// system(lynq_first_sta_cmd);
1972// system(lynq_reconfigure_cmd);
1973// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
1974 system(lynq_enable_sta_cmd);
1975 system(lynq_reconnect_cmd);
1976// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001977 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001978}
1979
you.chen35020192022-05-06 11:30:57 +08001980int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001981{
qs.xiongad2f89d2023-01-18 13:17:41 +08001982// char lynq_disable_network_cmd[MAX_CMD];
1983// curr_status_info curr_state;
1984// ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001985
qs.xiongad2f89d2023-01-18 13:17:41 +08001986 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 +08001987 CHECK_IDX(idx, CTRL_STA);
1988 CHECK_WPA_CTRL(CTRL_STA);
1989
qs.xiongad2f89d2023-01-18 13:17:41 +08001990// curr_state.ap = &ap_info;
1991// curr_state.state = NULL;
you.chena6cd55a2022-05-08 12:20:18 +08001992
qs.xiongad2f89d2023-01-18 13:17:41 +08001993// if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1994// return 0;
1995// }
you.chena6cd55a2022-05-08 12:20:18 +08001996
qs.xiongad2f89d2023-01-18 13:17:41 +08001997// sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1998// DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1999 system(lynq_disable_sta_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08002000 DO_OK_FAIL_REQUEST(cmd_save_config);
2001
2002 return 0;
2003// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04002004}
qs.xiong7a105ce2022-03-02 09:43:11 -05002005
you.chen35020192022-05-06 11:30:57 +08002006//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
2007// int i, count;
2008// char *p;
2009// const char * FLAG_SSID = "ssid=";
2010// const char * FLAG_SBSID = "bssid=";
2011// const char * FLAG_KEY_MGMT = "key_mgmt=";
2012// const char * FLAG_FREQ = "freq=";
2013// char lynq_sta_cmd[MAX_CMD];
2014// char *split_lines[128] = {0};
2015
2016// CHECK_WPA_CTRL(CTRL_AP);
2017
2018// sprintf(lynq_sta_cmd, "STA %s", bssid);
2019
2020// DO_REQUEST(lynq_sta_cmd);
2021
2022// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2023
2024// for(i=0; i < count; i++) {
2025// p = strstr(split_lines[i], FLAG_SSID);
2026// if (p != NULL) {
2027// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
2028// continue;
2029// }
2030// }
2031
2032// lynq_get_interface_ip(idx, ap->ap_ip);
2033// lynq_ap_password_set(idx, ap->psw);
2034
2035// return 0;
2036//}
2037
2038static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
2039 curr_status_info curr_state;
2040 curr_state.ap = ap;
2041 curr_state.state = NULL;
2042 return inner_get_status_info(interface, &curr_state);
2043}
2044
2045int 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 -04002046{
you.chend2fef3f2023-02-13 10:50:35 +08002047 int index, line_count;
2048 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08002049 const char *lynq_first_sta_cmd = "STA-FIRST";
2050 char lynq_next_sta_cmd[MAX_CMD];
2051 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08002052 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002053
you.chen35020192022-05-06 11:30:57 +08002054 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002055
you.chen35020192022-05-06 11:30:57 +08002056 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002057
you.chen35020192022-05-06 11:30:57 +08002058// ap_info_s * tmp_ap;
2059// device_info_s * tmp_list;
2060 if (ap == NULL || list == NULL || len == NULL) {
2061 printf("bad input param");
2062 return -1;
2063 }
2064
2065// ap = &tmp_ap;
2066// list = &tmp_list;
2067 *ap = malloc(sizeof (ap_info_s));
2068
2069 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
2070 return -1;
2071 }
2072
2073 lynq_get_interface_ip(idx, (*ap)->ap_ip);
2074 lynq_ap_password_get(idx, (*ap)->psw);
2075
you.chen35020192022-05-06 11:30:57 +08002076 DO_REQUEST(lynq_first_sta_cmd);
2077
2078 index = 0;
2079 while (reply_len > 0) {
2080 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2081 break;
2082 }
2083 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2084 bssid[index] = malloc(strlen(split_lines[0]) + 1);
2085 strcpy(bssid[index], split_lines[0]);
2086 index++;
2087 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
2088 reply_len = MAX_RET;
2089 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08002090 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 +08002091 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
2092 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08002093 break;
2094 }
2095 }
2096
2097 *len = index;
2098
2099 *list = malloc(sizeof(device_info_s) * (*len));
2100 for (index=0; index < *len; index++) {
you.chend2fef3f2023-02-13 10:50:35 +08002101 dev_info = &(*list)[index];
2102 memset(dev_info, 0, sizeof(device_info_s));
2103 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
2104 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
2105 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
2106 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08002107 free(bssid[index]);
2108 }
2109
2110 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002111}
2112
you.chen35020192022-05-06 11:30:57 +08002113int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002114{
you.chen35020192022-05-06 11:30:57 +08002115 int i, count, index, count_words;
2116 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2117 char *split_lines[128] = {0};
2118 char *split_words[128] = {0};
2119 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04002120
you.chen35020192022-05-06 11:30:57 +08002121 if (list == NULL || len == NULL) {
2122 return -1;
2123 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002124
you.chen9ac66392022-08-06 17:01:16 +08002125 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2126 {
2127 usleep(100 * 1000);
2128 }
2129
you.chen35020192022-05-06 11:30:57 +08002130 CHECK_IDX(idx, CTRL_STA);
2131
2132 CHECK_WPA_CTRL(CTRL_STA);
2133
2134 DO_REQUEST(lynq_scan_result_cmd);
2135
2136 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2137 *len = count - 1;
2138 *list = malloc(sizeof (scan_info_s) * *len);
2139
2140 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2141 for (index=0; index <count_words; index++) {
2142 printf("----header: %s\n", split_words[index]);
2143 }
2144
2145 for(index = 1;index < count; index++) {
2146 printf("---- %s\n",split_lines[index]);
2147 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2148 if (count_words < 4)
2149 continue;
2150 printf("count: %d, %s\n", count_words, split_words[0]);
2151 //bssid / frequency / signal level / flags / ssid
2152 p = (*list) + index - 1;
2153 strcpy(p->mac, split_words[0]);
2154 p->band = convert_band_from_freq(atoi(split_words[1]));
2155 p->rssi = -1 * atoi( split_words[2]);
2156 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chend2fef3f2023-02-13 10:50:35 +08002157 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
you.chen35020192022-05-06 11:30:57 +08002158 }
2159
2160 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002161}
qs.xiong97fa59b2022-04-07 05:41:29 -04002162
you.chen35020192022-05-06 11:30:57 +08002163int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2164{
2165 int count, net_no, index;
2166 int net_no_list[128];
2167 lynq_wifi_auth_s net_auth;
2168 char lynq_remove_cmd[MAX_CMD];
2169
2170 if (ssid == NULL || *ssid == '\0') {
2171 printf("bad ssid\n");
2172 return -1;
2173 }
2174
2175 CHECK_IDX(idx, CTRL_STA);
2176
2177 CHECK_WPA_CTRL(CTRL_STA);
2178
2179 net_no = -1;
2180 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2181
2182 for (index=0; index < count; index++) {
2183 net_auth = -1;
2184 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2185 net_no = net_no_list[index];
2186 break;
2187 }
2188 }
2189
2190 if (net_no < 0) {
2191 return 0;
2192 }
2193
2194 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2195
2196 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2197 DO_OK_FAIL_REQUEST(cmd_save_config);
2198
2199 return 0;
2200}
2201
2202int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2203{
you.chend2fef3f2023-02-13 10:50:35 +08002204 int count, index;
you.chen35020192022-05-06 11:30:57 +08002205 int net_no_list[128];
2206 char freq[16];
2207
2208 if (list == NULL || len == NULL) {
2209 printf("bad param\n");
2210 return -1;
2211 }
2212
2213 CHECK_IDX(idx, CTRL_STA);
2214
2215// CHECK_WPA_CTRL(CTRL_STA);
2216
2217 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2218 printf("count is %d\n", count);
2219
2220 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002221 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002222 *len = count;
2223
2224 for (index=0; index < count; index++) {
2225 printf("to get ssid %d\n", index);
2226 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002227 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002228 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002229 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2230 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2231 }
2232 else {
2233 (*list)[index].base_info.band = -1;
2234 }
2235
you.chen755332b2022-08-06 16:59:10 +08002236 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002237 }
2238
2239 return 0;
2240}
2241
2242int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2243{
2244 const char *lynq_scan_cmd = "SCAN";
2245
2246 CHECK_IDX(idx, CTRL_STA);
2247
2248 CHECK_WPA_CTRL(CTRL_STA);
2249
2250 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen9ac66392022-08-06 17:01:16 +08002251 g_sta_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +08002252
2253 return 0;
2254}
2255
2256int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2257 if (cb == NULL) {
2258 return -1;
2259 }
2260
2261 g_ap_callback_priv = priv;
2262 g_ap_callback_func = cb;
2263
2264 return 0;
2265}
2266
2267int lynq_unreg_ap_event_callback(void * priv) {
2268 if (g_ap_callback_priv == priv) {
2269 g_ap_callback_func = NULL;
2270 g_ap_callback_priv = NULL;
2271 return 0;
2272 }
2273 return -1;
2274}
2275
2276int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2277 if (cb == NULL) {
2278 return -1;
2279 }
2280
2281 g_sta_callback_priv = priv;
2282 g_sta_callback_func = cb;
2283
2284 return 0;
2285}
2286
2287int lynq_unreg_sta_event_callback(void * priv) {
2288 if (g_sta_callback_priv == priv) {
2289 g_sta_callback_func = NULL;
2290 g_sta_callback_priv = NULL;
2291 return 0;
2292 }
2293 return -1;
2294}
2295
2296
2297static int inner_get_status_info_state (int interface, char *state) {
2298 curr_status_info curr_state;
2299 curr_state.ap = NULL;
2300 curr_state.state = state;
2301 return inner_get_status_info(interface, &curr_state);
2302}
2303
2304int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2305{
2306 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002307 CHECK_IDX(idx, CTRL_AP);
2308
2309 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2310 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2311 return 0;
2312 }
2313
2314 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2315 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2316 }
2317 else {
2318 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2319 }
2320
2321 return 0;
2322}
2323
2324int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2325 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002326 CHECK_IDX(idx, CTRL_STA);
2327
2328 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2329 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2330 return 0;
2331 }
2332
2333 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2334 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2335 }
2336 else {
2337 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2338 }
2339
2340 return 0;
2341}
2342
2343int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2344// CHECK_IDX(idx, CTRL_AP);
2345// int ret = 0;
2346// size_t reply_len = MAX_RET;
2347// char cmd_reply[MAX_RET]={0};
2348// const char * cmd_str = "GET country";
2349// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2350// do{
2351// if (NULL == s_lynq_wpa_ctrl) {
2352// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2353// if (NULL == s_lynq_wpa_ctrl ) {
2354// printf("wpa_ctrl_open fail\n");
2355// return -1;
2356// }
2357// }
2358// }while(0);
2359
2360// do {
2361// reply_len = MAX_RET;
2362// cmd_reply[0] = '\0';
2363// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08002364// 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 +08002365// if (ret != 0) {
2366// printf("call ##cmd_str fail %d\n", ret);
2367// return ret;
2368// }
2369// cmd_reply[reply_len+1] = '\0';
2370// printf("cmd replay [ %s ]\n", cmd_reply);
2371// }while(0);
2372
2373 FILE *fp;
2374 size_t i = 0;
2375 char lynq_cmd_ret[MAX_RET]={0};
2376
2377// CHECK_IDX(idx, CTRL_AP);
2378
2379 if((fp=popen("wl country","r"))==NULL)
2380 {
2381 perror("popen error!");
2382 return -1;
2383 }
2384 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2385 {
2386 perror("fread fail!");
2387 return -1;
2388 }
2389
2390 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2391 if (lynq_cmd_ret[i] == ' ') {
2392 lynq_cmd_ret[i] = '\0';
2393 break;
2394 }
2395 }
2396
2397 strcpy(country_code,lynq_cmd_ret);
2398 printf("---country code %s\n", country_code);
2399
2400 int ret=pclose(fp);
2401 if(ret==-1)
2402 {
2403 perror("close file faild");
2404 }
2405
2406 return 0;
2407}
2408
2409int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2410// const char * cmd_str = "GET country";
2411// CHECK_IDX(idx, CTRL_AP);
2412// CHECK_WPA_CTRL(CTRL_STA);
2413
2414// DO_REQUEST(cmd_str);
2415// printf("result %s\n", cmd_reply);
2416
2417 if (country_code == NULL || *country_code == '\0') {
2418 printf("bad country code\n");
2419 return -1;
2420 }
2421
2422 char lynq_country_cmd[MAX_CMD];
2423 sprintf(lynq_country_cmd, "wl country %s", country_code);
2424 if (system(lynq_country_cmd) == 0) {
2425 return 0;
2426 }
2427
2428 return -1;
2429}
2430
2431int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2432{
2433 if (mac == NULL) {
2434 return -1;
2435 }
2436
2437 CHECK_IDX(idx, CTRL_STA);
2438 ap_info_s ap;
2439 ap.ap_mac[0] = '\0';
2440
2441 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2442 return -1;
2443 }
2444 strcpy(mac, ap.ap_mac);
2445
2446 return 0;
2447}
2448
2449int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2450{
you.chen9ac66392022-08-06 17:01:16 +08002451 struct ifaddrs *ifaddr_header, *ifaddr;
2452 struct in_addr * ifa;
2453 const char * ifaName = "wlan0";
2454 if (ip == NULL)
2455 {
you.chenf58b3c92022-06-21 16:53:48 +08002456 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002457 }
you.chenf58b3c92022-06-21 16:53:48 +08002458
you.chen9ac66392022-08-06 17:01:16 +08002459 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002460 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002461 }
2462 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002463 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002464 }
you.chen35020192022-05-06 11:30:57 +08002465
you.chen9ac66392022-08-06 17:01:16 +08002466 if (getifaddrs(&ifaddr_header) == -1)
2467 {
you.chen35020192022-05-06 11:30:57 +08002468 perror("getifaddrs");
2469 return -1;
2470 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002471 }
you.chen35020192022-05-06 11:30:57 +08002472
2473
you.chen9ac66392022-08-06 17:01:16 +08002474 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2475 {
2476 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002477 continue;
you.chen9ac66392022-08-06 17:01:16 +08002478 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2479 {
2480 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2481 {
2482 // is a valid IP4 Address
2483 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2484 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2485 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2486 freeifaddrs(ifaddr_header);
2487 printf("ip %s\n", ip);
2488 return 0;
2489 }
2490 }
2491 }
qs.xiongc4f007c2023-02-08 18:16:58 +08002492 freeifaddrs(ifaddr_header);
you.chen9ac66392022-08-06 17:01:16 +08002493 return -1;
you.chen35020192022-05-06 11:30:57 +08002494}
2495
2496int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2497{
2498 int count;
2499 size_t i;
2500 char *split_words[128] = {0};
2501 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2502
2503 CHECK_WPA_CTRL(idx);
2504
2505 DO_REQUEST(lynq_get_mac_cmd);
2506
2507 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2508 return -1;
2509 }
2510
2511 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2512
2513 if (count < 2) {
2514 return -1;
2515 }
2516
2517 for (i=0; i < strlen(split_words[1]); i++ ) {
2518 if (split_words[1][i] != ' ') {
2519 break;
2520 }
2521 }
2522
2523 strcpy(mac, split_words[1] + i);
2524
2525 return 0;
2526}
2527
2528int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2529{
2530// int count;
2531// char *split_words[128] = {0};
2532// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2533
2534// if (rssi == NULL) {
2535// return -1;
2536// }
2537
2538// CHECK_IDX(idx, CTRL_STA);
2539
2540// CHECK_WPA_CTRL(CTRL_STA);
2541
2542// DO_REQUEST(lynq_get_rssi_cmd);
2543
2544// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2545// return -1;
2546// }
2547
2548// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2549
2550// if (count < 2) {
2551// return -1;
2552// }
2553
2554// *rssi = atoi(split_words[1]) * -1;
2555
2556 FILE *fp;
2557 size_t i = 0;
2558 char lynq_cmd_ret[MAX_RET]={0};
2559
2560// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002561/*******change other cmd to get rssi*******
2562 *
2563 *wl rssi ---> wl -i wlan0 rssi
2564 *
2565 ***** change by qs.xiong 20221011*******/
2566 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002567 {
2568 perror("popen error!");
2569 return -1;
2570 }
2571 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2572 {
2573 perror("fread fail!");
2574 return -1;
2575 }
you.chen9f17e4d2022-06-06 17:18:18 +08002576 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002577/****** if got rssi is 0,means sta didn't connected any device****/
2578 if(*rssi == 0)
2579 {
2580 printf("sta didn't connected any ap device,please check connection\n");
2581 }
you.chen35020192022-05-06 11:30:57 +08002582
2583 return 0;
2584}
2585
2586int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2587{
2588 if (band == NULL) {
2589 return -1;
2590 }
2591
2592 CHECK_IDX(idx, CTRL_STA);
2593 ap_info_s ap;
2594 ap.band = -1;
2595
2596 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2597 return -1;
2598 }
2599 *band = ap.band;
2600
2601 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002602}
you.chenf58b3c92022-06-21 16:53:48 +08002603
2604int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2605{
you.chenf58b3c92022-06-21 16:53:48 +08002606 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08002607
2608 if (ip == NULL)
2609 {
2610 printf("invalid param");
2611 return -1;
2612 }
2613
2614 CHECK_IDX(idx, CTRL_STA);
2615
2616 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2617 {
2618 return -1;
2619 }
2620
you.chend2fef3f2023-02-13 10:50:35 +08002621 return inner_get_ip_by_mac(bssid, ip, 32); //better input by user
you.chenf58b3c92022-06-21 16:53:48 +08002622}
2623
qs.xiong026c5c72022-10-17 11:15:45 +08002624int lynq_ap_connect_num(int sta_number)
2625{
2626 char lynq_limit_cmd[32]={0};
2627 int ret;
2628 if((sta_number < 1 ) && (sta_number > 15)){
2629 printf("sta_number: not in range\n",sta_number);
2630 return -1;
2631 }
2632 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2633 ret = system(lynq_limit_cmd);
2634 if(ret != 0){
2635 printf("cmd of limit ap devices number error\n");
2636 }
2637 return 0;
2638}
you.chenf58b3c92022-06-21 16:53:48 +08002639
qs.xiong77905552022-10-17 11:19:57 +08002640int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
2641{
2642
2643 char lynq_wifi_acs_cmd[128]={0};
2644 char lynq_cmd_mode[128]={0};
2645 char lynq_cmd_slect[128]={0};
2646
2647 if((acs_mode != 2) && (acs_mode != 5)){
2648 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
2649 }
2650
2651 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
2652 return -1;
2653 }
2654
2655 CHECK_IDX(idx, CTRL_AP);
2656
2657 CHECK_WPA_CTRL(CTRL_AP);
2658
2659 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
2660 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2661 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2662
2663 DO_OK_FAIL_REQUEST(cmd_disconnect);
2664 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
2665 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2666 DO_OK_FAIL_REQUEST(cmd_save_config);
2667 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
2668
2669 return 0;
2670}
you.chen0f5c6432022-11-07 18:31:14 +08002671//you.chen add for tv-box start
2672static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
2673 FILE *fp;
2674 //printf("to exec cmd:%s\n", str_cmd);
2675 if((fp=popen(str_cmd,"r"))==NULL)
2676 {
2677 perror("popen error!");
2678 return -1;
2679 }
2680 if((fread(str_cmd_ret,max_len,1,fp))<0)
2681 {
2682 perror("fread fail!");
2683 fclose(fp);
2684 return -1;
2685 }
2686 fclose(fp);
2687 return 0;
2688}
2689
2690static int get_netmask_length(const char* mask)
2691{
2692 int masklen=0, i=0;
2693 int netmask=0;
2694
2695 if(mask == NULL)
2696 {
2697 return 0;
2698 }
2699
2700 struct in_addr ip_addr;
2701 if( inet_aton(mask, &ip_addr) )
2702 {
2703 netmask = ntohl(ip_addr.s_addr);
2704 }else{
2705 netmask = 0;
2706 return 0;
2707 }
2708
2709 while(0 == (netmask & 0x01) && i<32)
2710 {
2711 i++;
2712 netmask = netmask>>1;
2713 }
2714 masklen = 32-i;
2715 return masklen;
2716}
2717
2718static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
2719 int mask_len;
2720 char *p;
2721 char tmp[64] = {0};
2722 if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0)
2723 return -1;
2724 p = strstr(str_cmd_ret, "Mask:");
2725 if (p == NULL)
2726 return -1;
2727 mask_len = get_netmask_length(p + 5);
2728 if (mask_len == 0)
2729 return -1;
2730 p = strstr(str_cmd_ret, "inet addr:");
2731 if (p == NULL)
2732 return -1;
2733 strcpy(tmp, p + 10);
2734 p = strstr(tmp, " ");
2735 if (p != NULL)
2736 *p = '\0';
2737 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
2738 return 0;
2739}
2740
2741static void GBWWatchThreadProc() {
2742 int i,n, nloop, nmax, ncheckcount, nidlecount;
2743 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
2744 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
2745 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
2746 char *results[16] = {0};
2747 char str_cmd[256] = {0};
2748 char str_cmd_ret[128] = {0};
2749 char dest_ip[32] = {0};
2750 lastAP1Bytes = lastAP2Bytes = 0;
2751 lastAP1Drop = lastAP2Drop = 0;
2752 lastAP1Speed = lastAP2Speed = 0;
2753 setAP1Speed = 50;
2754 setAP2Speed = 80;
2755 nloop = 0;
2756 nmax = 6;
2757 ncheckcount = nidlecount = 0;
2758
2759 printf("------gbw thread run\n");
2760 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
2761 while (dest_ip[0] == '\0') {
2762 sleep(1);
2763 str_cmd_ret[0] = '\0';
2764 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
2765 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
2766 if (str_cmd_ret[n] == '\n'){
2767 str_cmd_ret[n] = '\0';
2768 break;
2769 }
2770 }
2771 if (str_cmd_ret[0] != '\0')
2772 {
2773 strcpy(dest_ip, str_cmd_ret);
2774 }
2775 }
2776
2777 system("tc qdisc del dev tether root > /dev/null 2>&1");
2778 system("tc qdisc add dev tether root handle 1: htb r2q 1");
2779 system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000");
2780 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
2781 {
2782 printf("not get tether info\n");
2783 return;
2784 }
2785 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);
2786 system(str_cmd);
2787 system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000");
2788 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip);
2789 //printf("----cmd:%s\n", str_cmd);
2790 system(str_cmd);
2791
2792 while (1) {
2793 sleep(1);
2794 memset(str_cmd, 0, sizeof(str_cmd));
2795 if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd)))
2796 continue;
2797 //printf("ap1 --- %s\n", str_cmd);
2798 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2799 if (n > 9) {
2800 if (strcmp(results[1], "Sent") == 0) {
2801 currAP1Bytes = atoll(results[2]);
2802 }
2803 if (strcmp(results[6], "(dropped") == 0) {
2804 currAP1Drop = atoi(results[7]);
2805 }
2806 }
2807
2808 memset(str_cmd, 0, sizeof(str_cmd));
2809 if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd)))
2810 continue;
2811 //printf("ap2 --- %s\n", str_cmd);
2812 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2813 if (n > 9) {
2814 if (strcmp(results[1], "Sent") == 0) {
2815 currAP2Bytes = atoll(results[2]);
2816 }
2817 if (strcmp(results[6], "(dropped") == 0) {
2818 currAP2Drop = atoi(results[7]);
2819 }
2820 }
2821
2822 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
2823 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
2824 lastAP1Bytes = currAP1Bytes;
2825 lastAP2Bytes = currAP2Bytes;
2826 continue;
2827 }
2828
2829 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
2830 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
2831 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
2832 lastAP1Speed = currAP1Speed;
2833 lastAP2Speed = currAP2Speed;
2834 lastAP1Bytes = currAP1Bytes;
2835 lastAP2Bytes = currAP2Bytes;
2836
2837 currSetAP1Speed = setAP1Speed;
2838 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
2839 ncheckcount++;
2840 if (ncheckcount > 3) {
2841 ncheckcount = 0;
2842 currSetAP1Speed = 5;
2843 }
2844 }
2845 else {
2846 ncheckcount = 0;
2847 if (currAP1Speed < 5)
2848 nidlecount++;
2849 else
2850 nidlecount = 0;
2851
2852 }
2853
2854 if (nidlecount > 60 ){
2855 currSetAP1Speed = 50;
2856 }
2857
2858 if (currSetAP1Speed != setAP1Speed) {
2859 setAP1Speed = currSetAP1Speed;
2860 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));
2861 //printf("------***change speed: %s\n", str_cmd);
2862 system(str_cmd);
2863 }
2864 }
2865}
2866
2867int enableGBW(const char* mac) {
2868 int i,len;
2869 char get_ipaddr_cmd[128]={0};
2870 ap_info_s *ap;
2871 device_info_s * list;
2872
2873 if (mac == NULL || g_gbw_enabled == 1)
2874 return -1;
2875 len = strlen(mac);
2876 g_gbw_mac = malloc(len + 1);
2877 for(i=0;i<len;i++) {
2878 if (mac[i] >= 'A' && mac[i] <= 'Z')
2879 {
2880 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
2881 }
2882 else
2883 g_gbw_mac[i] = mac[i];
2884 }
2885 g_gbw_mac[i] = '\0';
2886 g_gbw_enabled = 1;
2887
2888 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
2889 if (system(get_ipaddr_cmd) == 0) {
2890 //startGBW();
2891 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
2892 for (i=0;i<len;i++) {
2893 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
2894 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
2895 startGBW();
2896 }
2897 free(ap);
2898 free(list);
2899 }
2900 }
2901 return 0;
2902}
2903
2904int disableGBW() {
2905 stopGBW();
2906 free(g_gbw_mac);
2907 g_gbw_mac = NULL;
2908 g_gbw_enabled = 1;
2909 return 0;
2910}
2911
2912static int startGBW() {
2913 if (g_gbw_watcher_pid != 0) {
2914 stopGBW();
2915 }
2916 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
2917}
2918
2919static int stopGBW() {
2920 void* retval;
2921 pthread_cancel(g_gbw_watcher_pid);
2922 pthread_join(g_gbw_watcher_pid, &retval);
2923 g_gbw_watcher_pid = 0;
2924 system("tc qdisc del dev tether root");
2925}
2926//you.chen add for tv-box end