blob: d0c3c322ad88327b189f9bfdd6918f9ac65e4e5d [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 }
373
you.chen35020192022-05-06 11:30:57 +0800374 if (g_ap_watcher_pid == 0 ) {
375 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
376 if(ret<0){
you.chend2fef3f2023-02-13 10:50:35 +0800377 ret = -1;
378 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800379 }
380 }
381
382 if (g_sta_watcher_pid == 0 ) {
383 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
384 if(ret<0){
you.chend2fef3f2023-02-13 10:50:35 +0800385 ret = -1;
386 goto out_enable;
you.chen35020192022-05-06 11:30:57 +0800387 }
388 }
389
you.chena6fa5b22022-05-18 10:28:19 +0800390 for (i=0; i<10; i++) {
391 usleep(300*1000);
392 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
393 break;
394 }
395 }
396
you.chend2fef3f2023-02-13 10:50:35 +0800397 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
398 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
399 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
400 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
401out_enable:
402 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +0800403 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500404}
405
qs.xiong1af5daf2022-03-14 09:12:12 -0400406int lynq_wifi_disable(void)
407{
you.chend2fef3f2023-02-13 10:50:35 +0800408 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +0800409 g_ap_watcher_stop_flag = 1;
410 g_sta_watcher_stop_flag = 1;
411 if (g_ap_watcher_pid != 0)
412 pthread_join(g_ap_watcher_pid, NULL);
413 if (g_sta_watcher_pid != 0)
414 pthread_join(g_sta_watcher_pid, NULL);
415 if (g_lynq_wpa_ctrl[0] != NULL)
416 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
417 if (g_lynq_wpa_ctrl[1] != NULL)
418 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
419 g_ap_watcher_pid = 0;
420 g_sta_watcher_pid = 0;
421 g_lynq_wpa_ctrl[0] = NULL;
422 g_lynq_wpa_ctrl[1] = NULL;
423 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +0800424 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
425 return 0;
426}
427
428static inline char inner_convert_char(char in)
429{
430 if (in >= '0' && in <= '9')
431 {
432 return in - '0';
433 }
434 else if (in >= 'a' && in <= 'f')
435 {
436 return in - 'a' + 10;
437 }
438 else if (in >= 'A' && in <= 'F')
439 {
440 return in - 'A' + 10;
441 }
442 else
443 {
444 return '\xff';
445 }
446}
447
448static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
449{
450 char *p;
451 size_t pos = 0;
452 if (NULL == out_ssid)
453 return;
454 //printf("input ssid=[%s]\n", ssid);
455 memset(out_ssid, 0, out_ssid_len);
456 if (NULL == ssid)
457 return;
458 p = strchr(ssid, '\\');
459 if (NULL == p)
460 {
461 strncpy(out_ssid, ssid, out_ssid_len);
462 //printf(" first %s\n", out_ssid);
463 }
464 else
465 {
466 pos = p - ssid;
467 memcpy(out_ssid, ssid, pos);
468 //printf("pos %lu -- %s\n", pos, out_ssid);
469 for(; pos < out_ssid_len; pos ++)
470 {
471 if (p[0] == '\0')
472 {
473 //printf(" out %s\n", out_ssid);
474 return;
475 }
476 else if (p[0] != '\\')
477 {
478 out_ssid[pos] = p[0];
479 p += 1;
480 }
481 else if (p[1] == 'x' || p[1] == 'X')
482 {
483 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
484 p += 4;
485 }
486 else if (p[1] == '\\')
487 {
488 out_ssid[pos] = '\\';
489 p += 2;
490 }
491 else if (p[1] == 't')
492 {
493 out_ssid[pos] = '\t';
494 p += 2;
495 }
496 else if (p[1] == 'r')
497 {
498 out_ssid[pos] = '\r';
499 p += 2;
500 }
501 else if (p[1] == 'n')
502 {
503 out_ssid[pos] = '\n';
504 p += 2;
505 }//todo find a better way to convert?
506 }
507 }
508 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -0500509}
qs.xiong1af5daf2022-03-14 09:12:12 -0400510
you.chen35020192022-05-06 11:30:57 +0800511static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +0800512 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +0800513 char lynq_cmd_get[128]={0};
514
515 if (out_put == NULL) {
516 printf("output ptr is null\n");
517 return -1;
518 }
519 if (param_name == NULL) {
520 printf("param ptr is null");
521 return -1;
522 }
523 if (param_name[0] == '\0') {
524 printf("param is empty");
525 return -1;
526 }
527
528 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
529
530 CHECK_WPA_CTRL(interface);
531
532 DO_REQUEST(lynq_cmd_get);
533
534 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
535 return -1;
536 }
537
you.chena6fa5b22022-05-18 10:28:19 +0800538// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +0800539 if (strcmp(param_name, "ssid") == 0)
540 {
541 if (cmd_reply[0] == '\"') {
542 ssid_len = reply_len - 1;
543 memcpy(out_put, cmd_reply + 1, ssid_len);
544 if (out_put[ssid_len-1] == '\"')
545 {
546 out_put[ssid_len-1] = '\0';
547 }
548 else
549 {
550 out_put[ssid_len] = '\0';
551 }
552 }
553 else{
554 ssid_len = reply_len / 2;
555 for(i=0; i<ssid_len; i++)
556 {
557 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
558 }
559 out_put[ssid_len] = '\0';
560 }
561 }
562 else
563 {
564 memcpy(out_put, cmd_reply, reply_len + 1);
565 }
you.chen35020192022-05-06 11:30:57 +0800566 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500567}
qs.xiong1af5daf2022-03-14 09:12:12 -0400568
you.chen35020192022-05-06 11:30:57 +0800569static int lynq_split(char * str, int len, char delimiter, char * results[]) {
570 int ret = 0;
571 char * end = str + len - 1;
572 results[ret++] = str;
573 while(str < end) {
574 if (*str == delimiter) {
575 *str++ = '\0';
576 results[ret++] = str;
577 continue;
578 }
579 str++;
580 }
581 if (*str == delimiter) {
582 *str = '\0';
583 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400584
you.chen35020192022-05-06 11:30:57 +0800585 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500586}
587
you.chend2fef3f2023-02-13 10:50:35 +0800588static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
589{
590 char * p;
591 int ret = 0;
592 char cmd[256]={0};
593 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +0800594 return -1;
you.chend2fef3f2023-02-13 10:50:35 +0800595 memset(ip, 0, ip_len);
596 sprintf(cmd, "ip neigh | grep \"lladdr\" | grep \"tether\" | grep \"%s\" | head -1 | awk '{print $1}'", mac);
597 ret = exec_cmd(cmd, ip, ip_len);
598 p = strchr(ip, '\n');
599 if (NULL != p)
600 {
601 *p = '\0';
you.chen35020192022-05-06 11:30:57 +0800602 }
you.chend2fef3f2023-02-13 10:50:35 +0800603 printf("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +0800604 return ret;
605}
606
you.chend2fef3f2023-02-13 10:50:35 +0800607static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +0800608 struct in_addr addr ={0};
609 struct hostent *ht;
610
611 if (ip == NULL || *ip == '\0' || hostname == NULL) {
612 return -1;
613 }
614
you.chend2fef3f2023-02-13 10:50:35 +0800615 *hostname = '\0';
you.chen35020192022-05-06 11:30:57 +0800616 if (inet_aton(ip, &addr) == 0) {
617 printf("---inet_aton fail\n");
618 return -1;
619 }
620
621 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
622
623 if (ht == NULL) {
624 printf("---gethostbyaddr fail\n");
625 herror(NULL);
626 return -1;
627 }
628
629 strcpy(hostname, ht->h_name);
630
631 return 0;
632}
633
634static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
635{
636 int count, index, words_count;
637 char * split_lines[128]= {0};
638 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +0800639 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +0800640 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
641
642 CHECK_WPA_CTRL(ap_sta);
643
644 DO_REQUEST(lynq_wifi_list_networks);
645
646 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
647
648 //@todo check ssid field to compatible
649
650 ret = 0;
651 for(index=1; index < count; index++) {
652 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
653 if (words_count > 2) {
you.chend2fef3f2023-02-13 10:50:35 +0800654 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
655 if (ssid == NULL || strcmp(local_ssid, ssid) == 0) {
you.chen35020192022-05-06 11:30:57 +0800656 net_no_list[ret++] = atoi(split_words[0]);
657 }
658 }
659 }
660
661 return ret;
662}
663
664static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800665 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800666 CHECK_WPA_CTRL(ap_sta);
667 const char *lynq_wifi_add_network = "ADD_NETWORK";
668
669 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800670 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800671 return -1;
672 }
673
you.chen6c2dd9c2022-05-16 17:55:28 +0800674 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800675 if(cmd_reply[i] == '\n') {
676 cmd_reply[i] = '\0';
677 break;
678 }
679 }
680 return atoi(cmd_reply);
681}
you.chena6cd55a2022-05-08 12:20:18 +0800682
you.chen35020192022-05-06 11:30:57 +0800683static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
684{
685 int count, index;
686 int net_no_list[128];
687
688
689 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
690 for (index=0; index < count; index++) {
691 if (net_no_list[index] == net_no) {
692 return 0;
693 }
694 }
695
696 if (count >= 1)
697 index = net_no_list[count - 1];
698 else
699 index = -1;
700
701 while (index < net_no ) {
702 index = lynq_add_network(ap_sta);
703 if (index >= net_no) { // required network no created
704 return 0;
705 }
you.chena6cd55a2022-05-08 12:20:18 +0800706 else if( index < 0) {
707 printf("add network fail\n");
708 return -1;
709 }
you.chen35020192022-05-06 11:30:57 +0800710 }
711
712 if (index < 0)
713 return -1;
714
715 return 0;
716}
717
718static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
719 if (freq > 5000 && freq < 6000) {
720 return LYNQ_WIFI_5G_band;
721 }
722 else if (freq > 2000 && freq < 3000) {
723 return LYNQ_WIFI_2G_band;
724 }
725 return LYNQ_WIFI_2_and_5G_band;
726}
727
728static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
729 if (key_mgmt != NULL) {
730 if (memcmp( key_mgmt, "NONE", 4) == 0) {
731 return LYNQ_WIFI_AUTH_OPEN;
732 }
733 else if (memcmp( key_mgmt, "WEP", 3) == 0){
734 return LYNQ_WIFI_AUTH_WEP;
735 }
736 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
737 return LYNQ_WIFI_AUTH_WPA_PSK;
738 }
739 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
740 return LYNQ_WIFI_AUTH_WPA2_PSK;
741 }
742 }
743
744 return -1;
745}
746
747static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
748 if (flag != NULL) {
749 if (strstr( flag, "WPA2-PSK") != NULL){
750 return LYNQ_WIFI_AUTH_WPA2_PSK;
751 }
752 else if (strstr( flag, "WPA-PSK") != NULL){
753 return LYNQ_WIFI_AUTH_WPA_PSK;
754 }
755 else if (strstr( flag, "WEP") != NULL){
756 return LYNQ_WIFI_AUTH_WEP;
757 }
758 else if (strstr( flag, "NONE") != NULL) {
759 return LYNQ_WIFI_AUTH_OPEN;
760 }
you.chend2fef3f2023-02-13 10:50:35 +0800761 else if (strcmp( flag, "[ESS]") == 0) {
762 return LYNQ_WIFI_AUTH_OPEN;
763 }
you.chen35020192022-05-06 11:30:57 +0800764 }
765
766 return -1;
767}
768
769static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
770 switch (bw) {
771 case 10:
772 return LYNQ_WIFI_BANDWIDTH_HT10;
773 break;
774 case 20:
775 return LYNQ_WIFI_BANDWIDTH_HT20;
776 break;
777 case 40:
778 return LYNQ_WIFI_BANDWIDTH_HT40;
779 break;
780 case 80:
781 return LYNQ_WIFI_BANDWIDTH_HT80;
782 break;
783 default:
784 break;
785 }
786
787 return -1;
788}
789
790static int inner_get_status_info(int interface, curr_status_info *curr_state) {
791 int i, count;
792 char *p;
793 const char *lynq_status_cmd = "STATUS";
794 const char * FLAG_SSID = "ssid=";
795 const char * FLAG_SBSID = "bssid=";
796 const char * FLAG_KEY_MGMT = "key_mgmt=";
797 const char * FLAG_FREQ = "freq=";
798 const char * FLAG_STATE = "wpa_state=";
799 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +0800800 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +0800801 char *split_lines[128] = {0};
802
803 CHECK_WPA_CTRL(interface);
804
805 if (curr_state == NULL) {
806 return -1;
807 }
808
809 DO_REQUEST(lynq_status_cmd);
810
811 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
812
813 curr_state->net_no = -1;
814 ret = -1;
815 for(i=0; i < count; i++) {
816 if (curr_state->ap != NULL) {
you.chen35020192022-05-06 11:30:57 +0800817 p = strstr(split_lines[i], FLAG_SBSID);
818 if (p != NULL) {
you.chend2fef3f2023-02-13 10:50:35 +0800819 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +0800820 ret = 0;
821 continue;
822 }
you.chenf58b3c92022-06-21 16:53:48 +0800823 p = strstr(split_lines[i], FLAG_SSID);
824 if (p != NULL) {
you.chend2fef3f2023-02-13 10:50:35 +0800825 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 +0800826 ret = 0;
827 continue;
828 }
you.chen35020192022-05-06 11:30:57 +0800829 p = strstr(split_lines[i], FLAG_KEY_MGMT);
830 if (p != NULL) {
you.chen450d0172022-07-15 17:56:48 +0800831 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
832 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +0800833 ret = 0;
834 continue;
835 }
836 p = strstr(split_lines[i], FLAG_FREQ);
837 if (p != NULL) {
838 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
839 ret = 0;
840 continue;
841 }
you.chend2fef3f2023-02-13 10:50:35 +0800842 p = strstr(split_lines[i], FLAG_IPADDR);
843 if (p != NULL) {
844 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
845 ret = 0;
846 continue;
847 }
you.chen35020192022-05-06 11:30:57 +0800848 } // end if (ap != NULL)
849 if (curr_state->state != NULL) {
850 p = strstr(split_lines[i], FLAG_STATE);
851 if (p != NULL) {
852 strcpy(curr_state->state, p + strlen(FLAG_STATE));
853 ret = 0;
854 continue;
855 }
856
857 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800858 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800859 ret = 0;
you.chen450d0172022-07-15 17:56:48 +0800860 curr_state->net_no = atoi(p + strlen(FLAG_ID));
you.chen35020192022-05-06 11:30:57 +0800861 printf("net_no %d, -- %s\n", curr_state->net_no, p);
862 }
863 }
864
865 return ret;
866}
867
868
qs.xiongf1b525b2022-03-31 00:58:23 -0400869int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500870{
you.chen35020192022-05-06 11:30:57 +0800871 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500872
you.chen35020192022-05-06 11:30:57 +0800873 if (ap_ssid == NULL) {
874 printf("ap_ssid is null\n");
875 return -1;
876 }
877 else {
878 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
879 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400880
you.chen35020192022-05-06 11:30:57 +0800881 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
882 return -1;
883 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400884
you.chen35020192022-05-06 11:30:57 +0800885 CHECK_IDX(idx, CTRL_AP);
886
887 CHECK_WPA_CTRL(CTRL_AP);
888
889 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
890
891 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
892 DO_OK_FAIL_REQUEST(cmd_save_config);
893
qs.xiong7a105ce2022-03-02 09:43:11 -0500894 return 0;
you.chen35020192022-05-06 11:30:57 +0800895
qs.xiong7a105ce2022-03-02 09:43:11 -0500896}
897
you.chen35020192022-05-06 11:30:57 +0800898int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500899{
you.chen35020192022-05-06 11:30:57 +0800900 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +0800901 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -0500902}
903
qs.xiongc9c79f72022-10-17 15:27:18 +0800904/*****
905 *frequency <------>channel
906 *
907 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
908 *
909 *
910 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
911 *
912 *
913 * */
914static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +0800915 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};
916 int i;
917 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
918
qs.xiong69a332b2022-12-02 09:58:57 +0800919 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +0800920 {
921 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +0800922 break;
qs.xiongc9c79f72022-10-17 15:27:18 +0800923 }
qs.xiongc00b6032022-11-29 16:28:03 +0800924
925 if(i == arr_len)
926 {
927 printf("input frequency is eero--->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +0800928 return -1;
929 }
qs.xiongc00b6032022-11-29 16:28:03 +0800930
qs.xiongc9c79f72022-10-17 15:27:18 +0800931 return 0;
932}
qs.xiongf1b525b2022-03-31 00:58:23 -0400933int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500934{
qs.xiongc00b6032022-11-29 16:28:03 +0800935 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +0800936 char lynq_wifi_frequency_cmd[128]={0};
937 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +0800938 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500939
qs.xiongc9c79f72022-10-17 15:27:18 +0800940 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +0800941 check = lynq_check_set_frequency(lynq_wifi_frequency);
942 if(check != 0)
943 {
944 printf("do check frequency error\n");
qs.xiongc9c79f72022-10-17 15:27:18 +0800945 return -1;
you.chen35020192022-05-06 11:30:57 +0800946 }
qs.xiongc00b6032022-11-29 16:28:03 +0800947 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
948 {
you.chen35020192022-05-06 11:30:57 +0800949 return -1;
950 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400951
you.chen35020192022-05-06 11:30:57 +0800952 CHECK_IDX(idx, CTRL_AP);
953
954 CHECK_WPA_CTRL(CTRL_AP);
955
956 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
957 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
958 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
959
you.chen6c2dd9c2022-05-16 17:55:28 +0800960 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800961 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
962 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
963 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500964
qs.xiong1af5daf2022-03-14 09:12:12 -0400965 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500966}
967
qs.xiongf1b525b2022-03-31 00:58:23 -0400968int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500969{
you.chen35020192022-05-06 11:30:57 +0800970 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400971
you.chen35020192022-05-06 11:30:57 +0800972 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400973
you.chen35020192022-05-06 11:30:57 +0800974 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
975 return -1;
976 }
977 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400978
979 return 0;
980}
981
qs.xiongf1b525b2022-03-31 00:58:23 -0400982int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
983{
you.chen35020192022-05-06 11:30:57 +0800984 CHECK_IDX(idx, CTRL_AP);
985 switch(bandwidth){
986 case LYNQ_WIFI_BANDWIDTH_HT10:
987 {
988 printf("bandwith [%d] not support now\n", bandwidth);
989 return -1;
990 }
991 case LYNQ_WIFI_BANDWIDTH_HT20:
992 {
993 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
994 system("wl down");
995 if (system(lynq_cmd_bandwith) != 0 ){
996 return -1;
997 }
998 system("wl up");
999 break;
1000 }
1001 case LYNQ_WIFI_BANDWIDTH_HT40:
1002 {
1003 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
1004 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1005 system("wl down");
1006 if (system(lynq_cmd_bandwith) != 0 ){
1007 return -1;
1008 }
1009 system("wl up");
1010 break;
1011 }
1012 case LYNQ_WIFI_BANDWIDTH_HT80:
1013 {
1014 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
1015 system("wl down");
1016 if (system(lynq_cmd_bandwith) != 0 ){
1017 return -1;
1018 }
1019 system("wl up");
1020 break;
1021 }
1022 default:
1023 {
1024 printf("auth type [%d] not support now\n", bandwidth);
1025 return -1;
1026 }
1027 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001028
1029
you.chen35020192022-05-06 11:30:57 +08001030 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001031}
you.chen35020192022-05-06 11:30:57 +08001032
qs.xiongf1b525b2022-03-31 00:58:23 -04001033int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1034{
you.chen35020192022-05-06 11:30:57 +08001035 int count = 0;
1036 int index = 0;
1037 char *split_words[128] = {0};
1038 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001039
you.chen35020192022-05-06 11:30:57 +08001040 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001041
you.chen35020192022-05-06 11:30:57 +08001042 CHECK_WPA_CTRL(CTRL_AP);
1043
1044 DO_REQUEST(lynq_chanspec_cmd);
1045
1046 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1047 for(;index < count; index++) {
1048 if (strncmp(split_words[index], "bw", 2) != 0) {
1049 continue;
1050 }
1051
1052 index++;
1053 if (index >= count) {
1054 return -1;
1055 }
1056
1057 printf("bw %s\n", split_words[index]);
1058 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1059 return 0;
1060 }
1061
1062 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001063}
qs.xiong0fb469a2022-04-14 03:50:45 -04001064
qs.xiongf1b525b2022-03-31 00:58:23 -04001065int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001066{
you.chen35020192022-05-06 11:30:57 +08001067 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -04001068
you.chen35020192022-05-06 11:30:57 +08001069 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04001070
you.chen35020192022-05-06 11:30:57 +08001071 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04001072
you.chen35020192022-05-06 11:30:57 +08001073 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
1074 return -1;
1075 }
1076
1077 system("wl down");
1078 if (system(lynq_cmd_channel) != 0 ){
1079 return -1;
1080 }
1081 system("wl up");
1082 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001083}
qs.xiong0fb469a2022-04-14 03:50:45 -04001084
qs.xiongf1b525b2022-03-31 00:58:23 -04001085int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001086{
you.chen35020192022-05-06 11:30:57 +08001087 int count = 0;
1088 int index = 0;
1089 char *split_words[128] = {0};
1090 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001091
you.chen35020192022-05-06 11:30:57 +08001092 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001093
you.chen35020192022-05-06 11:30:57 +08001094 CHECK_WPA_CTRL(CTRL_AP);
1095
1096 DO_REQUEST(lynq_chanspec_cmd);
1097
1098 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1099 for(;index < count; index++) {
1100 printf("---- %s\n",split_words[index]);
1101 if (strncmp(split_words[index], "channel", 2) != 0) {
1102 continue;
1103 }
1104
1105 index++;
1106 if (index >= count) {
1107 return -1;
1108 }
1109
1110 *channel = atoi(split_words[index]);
1111 return 0;
1112 }
1113
1114 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001115}
1116
1117
you.chen35020192022-05-06 11:30:57 +08001118int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001119{
you.chen6c2dd9c2022-05-16 17:55:28 +08001120 char ssid[MAX_CMD] = {0};
1121 int freq = 0;
1122 char lynq_auth_cmd[64]={0};
1123 char lynq_auth_alg_cmd[64]={0};
1124 char lynq_psk_cmd[64]={0};
1125 char lynq_pairwise_cmd[64]={0};
1126 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001127 CHECK_IDX(idx, CTRL_AP);
1128
you.chen6c2dd9c2022-05-16 17:55:28 +08001129 CHECK_WPA_CTRL(CTRL_AP);
1130
you.chen35020192022-05-06 11:30:57 +08001131 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1132 return -1;
1133 }
1134
you.chen92fd5d32022-05-25 10:09:47 +08001135 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001136 if (org_auth == auth) {
1137 return 0;
1138 }
1139 else {
1140 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1141 ssid[0] = '\0';
1142 }
1143 lynq_wifi_ap_frequency_get(idx, &freq);
1144
1145 DO_OK_FAIL_REQUEST(cmd_disconnect);
1146 DO_OK_FAIL_REQUEST(cmd_remove_all);
1147 if (ssid[0] != '\0') {
1148 lynq_wifi_ap_ssid_set(idx, ssid);
1149 }
1150 if (freq != 0) {
1151 lynq_wifi_ap_frequency_set(idx, freq);
1152 }
1153 }
1154 }
you.chen35020192022-05-06 11:30:57 +08001155
qs.xiong7a105ce2022-03-02 09:43:11 -05001156 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001157 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001158 {
you.chen35020192022-05-06 11:30:57 +08001159 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001160 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001161
you.chen35020192022-05-06 11:30:57 +08001162 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001163 break;
1164 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001165 case LYNQ_WIFI_AUTH_WEP:
1166 {
1167 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001168 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001169 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1170
1171 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1172 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1173 break;
1174 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001175 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001176 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001177 {
you.chen35020192022-05-06 11:30:57 +08001178 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1179 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1180 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1181 }
1182 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001183 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001184 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001185 }
1186// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1187// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1188 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001189
you.chen35020192022-05-06 11:30:57 +08001190 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1191 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1192 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001193 break;
you.chen35020192022-05-06 11:30:57 +08001194 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001195 default:
you.chen35020192022-05-06 11:30:57 +08001196 {
1197 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001198 return -1;
you.chen35020192022-05-06 11:30:57 +08001199 }
1200 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001201 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001202
qs.xiong7a105ce2022-03-02 09:43:11 -05001203 return 0;
1204}
1205
you.chen35020192022-05-06 11:30:57 +08001206int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001207{
you.chen35020192022-05-06 11:30:57 +08001208 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001209 char lynq_auth_alg_str[MAX_RET] = {0};
1210 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001211
1212 CHECK_IDX(idx, CTRL_AP);
1213
1214 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1215 return -1;
1216 }
1217
1218 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001219 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1220 *auth = LYNQ_WIFI_AUTH_OPEN;
1221 }
1222 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1223 *auth = LYNQ_WIFI_AUTH_WEP;
1224 }
1225 else {
1226 *auth = LYNQ_WIFI_AUTH_OPEN;
1227 }
you.chen35020192022-05-06 11:30:57 +08001228 }
you.chen92fd5d32022-05-25 10:09:47 +08001229 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001230 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001231 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001232 }
1233 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1234 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1235 }
1236 else {
1237 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1238 }
you.chen35020192022-05-06 11:30:57 +08001239 }
you.chen92fd5d32022-05-25 10:09:47 +08001240 else {
1241 *auth = -1;
1242 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001243
you.chen6c2dd9c2022-05-16 17:55:28 +08001244 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001245}
qs.xiong1af5daf2022-03-14 09:12:12 -04001246
qs.xiong1af5daf2022-03-14 09:12:12 -04001247
qs.xiongf1b525b2022-03-31 00:58:23 -04001248int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001249{
you.chen35020192022-05-06 11:30:57 +08001250 char LYNQ_WIFI_CMD[128]={0};
1251 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1252 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001253
you.chen35020192022-05-06 11:30:57 +08001254 CHECK_IDX(idx, CTRL_AP);
1255
1256 CHECK_WPA_CTRL(CTRL_AP);
1257
1258// system("connmanctl enable wifi");
1259// system("connmanctl tether wifi on cy-test 12345678");
1260// system("ifconfig wlan0 down");
1261// system("ifconfig wlan0 up");
1262// system("ifconfig wlan0 up");
1263
1264 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1265 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1266
1267 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1268 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1269
qs.xiong7a105ce2022-03-02 09:43:11 -05001270 return 0;
1271}
1272
qs.xiongf1b525b2022-03-31 00:58:23 -04001273int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001274{
you.chen35020192022-05-06 11:30:57 +08001275 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001276}
1277
qs.xiongf1b525b2022-03-31 00:58:23 -04001278int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001279{
you.chen35020192022-05-06 11:30:57 +08001280 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001281
you.chen35020192022-05-06 11:30:57 +08001282 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001283
you.chen35020192022-05-06 11:30:57 +08001284 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001285
you.chen35020192022-05-06 11:30:57 +08001286 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1287
1288 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1289
you.chenb4b121c2022-05-06 17:50:16 +08001290// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001291
qs.xiong7a105ce2022-03-02 09:43:11 -05001292 return 0;
1293}
qs.xiong1af5daf2022-03-14 09:12:12 -04001294
qs.xiongf1b525b2022-03-31 00:58:23 -04001295int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001296{
you.chen35020192022-05-06 11:30:57 +08001297 char lynq_disable_cmd[128] = {0};
1298 char lynq_select_cmd[128] = {0};
1299 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001300
you.chen35020192022-05-06 11:30:57 +08001301 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001302
you.chen35020192022-05-06 11:30:57 +08001303 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001304
you.chen35020192022-05-06 11:30:57 +08001305 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1306 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1307
1308 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1309 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1310 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001311
1312 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001313}
1314
qs.xiongf1b525b2022-03-31 00:58:23 -04001315int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001316{
you.chen35020192022-05-06 11:30:57 +08001317 char lynq_disable_cmd[128] = {0};
1318 char lynq_select_cmd[128] = {0};
1319 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001320
you.chen35020192022-05-06 11:30:57 +08001321 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001322
you.chen35020192022-05-06 11:30:57 +08001323 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001324
you.chen35020192022-05-06 11:30:57 +08001325 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1326 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1327
1328 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1329 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1330 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001331
1332 return 0;
1333}
qs.xiongf1b525b2022-03-31 00:58:23 -04001334
you.chen35020192022-05-06 11:30:57 +08001335int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001336{
qs.xiongf1b525b2022-03-31 00:58:23 -04001337 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001338 char lynq_tmp_cmd[MAX_CMD] = {0};
1339 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001340 if( password == NULL ){
1341 return -1;
1342 }
1343 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001344 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001345 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001346 return -1;
you.chen35020192022-05-06 11:30:57 +08001347 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001348
you.chen35020192022-05-06 11:30:57 +08001349 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001350
you.chen6c2dd9c2022-05-16 17:55:28 +08001351 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1352 return -1;
1353 }
1354 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1355 return -1;
1356 }
1357
you.chen35020192022-05-06 11:30:57 +08001358 CHECK_WPA_CTRL(CTRL_AP);
1359
you.chen6c2dd9c2022-05-16 17:55:28 +08001360 if (auth == LYNQ_WIFI_AUTH_WEP) {
1361 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1362 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1363 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1364 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1365 }
1366 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1367 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1368 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1369 }
1370 else {
1371 return -1;
1372 }
you.chen35020192022-05-06 11:30:57 +08001373
you.chen35020192022-05-06 11:30:57 +08001374 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001375
qs.xiongf1b525b2022-03-31 00:58:23 -04001376 return 0;
1377}
1378
you.chen35020192022-05-06 11:30:57 +08001379int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001380{
you.chen35020192022-05-06 11:30:57 +08001381 FILE * fp;
1382 int len, ret;
1383 int count, index;
1384 char *split_lines[128] = {0};
1385 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001386
you.chen35020192022-05-06 11:30:57 +08001387 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001388
you.chen35020192022-05-06 11:30:57 +08001389 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1390// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1391 if (NULL == fp) {
1392 printf("open file fail\n");
1393 return -1;
1394 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001395
you.chen35020192022-05-06 11:30:57 +08001396 buff = alloca(MAX_RET);
1397 fseek(fp, 0, SEEK_SET);
1398 len = fread(buff, 1, MAX_RET, fp);
1399 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001400
you.chen35020192022-05-06 11:30:57 +08001401 for(index=0; index < len; index ++) {
1402 if (memcmp(buff + index, "network={", 9) != 0) {
1403 continue;
1404 }
1405 p = buff + index + 9;
1406 for (; index < len; index ++ ) {
1407 if (buff[index] != '}') {
1408 continue;
1409 }
1410 buff[index] = '\0';
1411 break;
1412 }
1413 len = buff + index - p;
1414 }
1415
1416 count = lynq_split(p, len, '\n', split_lines);
1417
1418 ret = -1;
1419 for(index=0; index < count; index++) {
1420 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001421 if (p != NULL) {
1422 p += 4;
1423 if (*p == '\"') {
1424 p++;
1425 }
you.chen35020192022-05-06 11:30:57 +08001426 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001427 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1428 p += 9;
1429 if (*p == '\"') {
1430 p++;
1431 }
1432 }
1433 else {
1434 continue;
you.chen35020192022-05-06 11:30:57 +08001435 }
1436
1437 strcpy(password, p);
1438
1439 while(*password != '\0') {
1440 if (*password == '\"') {
1441 *password = '\0';
1442 break;
1443 }
1444 password++;
1445 }
1446 ret = 0;
1447 break;
1448 } //end for(index=0; index < count; index++)
1449
1450 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001451}
1452
you.chen35020192022-05-06 11:30:57 +08001453static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1454 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001455 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001456
you.chen35020192022-05-06 11:30:57 +08001457 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1458 return -1;
1459 }
1460
1461 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001462
1463 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1464 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1465 if (strcmp(lynq_proto_str, "RSN") == 0) {
1466 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1467 }
1468 }
1469 }
you.chen35020192022-05-06 11:30:57 +08001470 return 0;
1471}
1472
1473int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001474{
you.chen35020192022-05-06 11:30:57 +08001475 int pass_len, net_no, count, index;
1476 char lynq_tmp_cmd[300]={0};
1477 int net_no_list[128];
1478 lynq_wifi_auth_s net_auth;
1479 pass_len=strlen(password);
1480 if(pass_len < 8 || pass_len >= 64){
1481 return -1;
1482 }
1483
1484 CHECK_IDX(idx, CTRL_STA);
1485
1486 net_no = -1;
1487 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1488
1489 for (index=0; index < count; index++) {
1490 net_auth = -1;
1491 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1492 net_no = net_no_list[index];
1493 break;
1494 }
1495 }
1496
1497 if (net_no < 0) {
1498 return -1;
1499 }
1500
1501 CHECK_WPA_CTRL(CTRL_STA);
1502
1503 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1504
1505 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1506 DO_OK_FAIL_REQUEST(cmd_save_config);
1507
1508 return 0;
1509}
1510
1511int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1512
1513 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08001514 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001515 int count, index;
1516 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001517 char *buff, *p, *ssid, *ssid_end_flag;
1518 char tmp_ssid[128]={0};
you.chen35020192022-05-06 11:30:57 +08001519
you.chen755332b2022-08-06 16:59:10 +08001520 network_len = 0;
1521 p = NULL;
1522
you.chen35020192022-05-06 11:30:57 +08001523 CHECK_IDX(idx, CTRL_STA);
1524
you.chen755332b2022-08-06 16:59:10 +08001525 if (NULL == password) {
1526 printf("bad param\n");
1527 return -1;
1528 }
1529
you.chen35020192022-05-06 11:30:57 +08001530 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1531 if (NULL == fp) {
1532 printf("open file fail\n");
1533 return -1;
1534 }
1535
1536 buff = alloca(MAX_RET);
1537 fseek(fp, 0, SEEK_SET);
1538 len = fread(buff, 1, MAX_RET, fp);
1539 fclose(fp);
1540
1541 for(index=0; index < len; index ++) {
1542 for(; index < len; index ++) {
1543 if (memcmp(buff + index, "network={", 9) != 0) {
1544 continue;
1545 }
1546 p = buff + index + 9;
1547 for (; index < len; index ++ ) {
1548 if (buff[index] != '}') {
1549 continue;
1550 }
1551 buff[index] = '\0';
1552 break;
1553 }
you.chen755332b2022-08-06 16:59:10 +08001554 network_len = buff + index - p;
1555 break;
you.chen35020192022-05-06 11:30:57 +08001556 }
1557
you.chend2fef3f2023-02-13 10:50:35 +08001558 ssid = strstr(p, "ssid=");
1559 if (ssid != NULL) {
1560 ssid += strlen("ssid=");
1561 if (ssid[0] == '\"') {
1562 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
1563 break;
1564 }
1565 else{
1566 ssid_end_flag = strstr(ssid, "\n");
1567 if (ssid_end_flag != NULL)
1568 {
1569 ssid_len = (ssid_end_flag - ssid) / 2;
1570 for(i=0; i<ssid_len; i++)
1571 {
1572 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
1573 }
1574 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
1575 break;
1576 }
1577 }
you.chen35020192022-05-06 11:30:57 +08001578 }
you.chend2fef3f2023-02-13 10:50:35 +08001579
you.chen35020192022-05-06 11:30:57 +08001580 }
1581
you.chen755332b2022-08-06 16:59:10 +08001582 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001583 return -1;
1584 }
1585
you.chen755332b2022-08-06 16:59:10 +08001586 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001587
1588 ret = -1;
1589 for(index=0; index < count; index++) {
1590 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001591 if (p != NULL) {
1592 p += 4;
1593 if (*p == '\"') {
1594 p++;
1595 }
you.chen35020192022-05-06 11:30:57 +08001596 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001597 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1598 p += 9;
1599 if (*p == '\"') {
1600 p++;
1601 }
1602 }
1603 else {
1604 continue;
you.chen35020192022-05-06 11:30:57 +08001605 }
1606
1607 strcpy(password, p);
1608
1609 while(*password != '\0') {
1610 if (*password == '\"') {
1611 *password = '\0';
1612 break;
1613 }
1614 password++;
1615 }
1616 ret = 0;
1617 break;
1618 } //end for(index=0; index < count; index++)
1619
1620 return ret;
1621}
1622
1623static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1624{
qs.xiong97fa59b2022-04-07 05:41:29 -04001625 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001626
you.chen35020192022-05-06 11:30:57 +08001627 if (sta_ssid == NULL) {
1628 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001629 return -1;
you.chen35020192022-05-06 11:30:57 +08001630 }
1631
1632 CHECK_WPA_CTRL(CTRL_STA);
1633
1634 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1635
1636 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1637// DO_OK_FAIL_REQUEST(cmd_save_config);
1638
qs.xiong7a105ce2022-03-02 09:43:11 -05001639 return 0;
1640
1641}
1642
you.chen35020192022-05-06 11:30:57 +08001643static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001644{
you.chen35020192022-05-06 11:30:57 +08001645 char lynq_disable_cmd[128]={0};
1646 char lynq_select_cmd[128]={0};
1647
1648 CHECK_WPA_CTRL(CTRL_STA);
1649
1650 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001651 if (start_flag != 0)
1652 {
1653 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1654 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1655 }
1656 else
1657 {
1658 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1659 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1660 }
you.chen35020192022-05-06 11:30:57 +08001661 DO_OK_FAIL_REQUEST(cmd_save_config);
1662 }
1663
1664 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001665 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001666 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1667 }
1668 else {
1669 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1670 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1671 }
1672
1673 return 0;
1674}
1675
1676int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1677{
1678 CHECK_IDX(idx, CTRL_STA);
1679
you.chen6c2dd9c2022-05-16 17:55:28 +08001680 curr_status_info curr_state;
1681 ap_info_s ap_info;
1682 curr_state.ap = &ap_info;
1683 curr_state.state = NULL;
1684
1685 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
you.chend2fef3f2023-02-13 10:50:35 +08001686 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08001687 return 0;
1688 }
1689
1690 return -1;
you.chen35020192022-05-06 11:30:57 +08001691}
1692
1693int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1694{
you.chen9ac66392022-08-06 17:01:16 +08001695 scan_info_s *scan_list = NULL;
1696 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001697 int scan_len=0;
1698 int save_len=0;
1699 int best_index = -1;
1700 int best_scan_index = -1;
1701 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001702 int i, j, ret;
1703
1704 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001705
1706 CHECK_IDX(idx, CTRL_STA);
1707 if (info == NULL) {
1708 return -1;
1709 }
1710
1711 curr_status_info curr_state;
1712 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001713 char status[64];
you.chen35020192022-05-06 11:30:57 +08001714
you.chen9ac66392022-08-06 17:01:16 +08001715 memset(&ap_info, 0, sizeof (ap_info));
1716 memset(status, 0, sizeof (status));
1717
1718 curr_state.ap = &ap_info;
1719 curr_state.state = status;
1720
1721 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001722 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001723 if (strcmp(status, STATE_COMPLETED) == 0)
1724 {
1725 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1726 }
1727 else
1728 {
1729 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1730 }
you.chen35020192022-05-06 11:30:57 +08001731 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001732 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001733 return 0;
1734 }
1735
you.chen9ac66392022-08-06 17:01:16 +08001736 lynq_wifi_sta_start_scan(idx);
1737
you.chen35020192022-05-06 11:30:57 +08001738 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001739 if (NULL != scan_list)
1740 {
1741 free(scan_list);
1742 }
you.chen35020192022-05-06 11:30:57 +08001743 return -1;
1744 }
1745
1746 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001747 if (NULL != scan_list)
1748 {
1749 free(scan_list);
1750 }
1751 if (NULL != save_list)
1752 {
1753 free(save_list);
1754 }
you.chen35020192022-05-06 11:30:57 +08001755 return -1;
1756 }
1757
1758 for (i=0; i < save_len; i++) {
1759 for (j=0; j < scan_len; j++) {
1760 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1761 && save_list[i].base_info.auth == scan_list[j].auth) {
1762 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001763 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001764 best_rssi = scan_list[j].rssi;
1765 }
1766 else if (best_rssi > scan_list[j].rssi) {
1767 best_index = i;
1768 best_scan_index = j;
1769 best_rssi = scan_list[j].rssi;
1770 }
you.chend2fef3f2023-02-13 10:50:35 +08001771 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 +08001772 break;
1773 }
1774 }
1775 }
1776
1777 if (best_index >= 0) {
1778 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08001779 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 +08001780 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1781 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001782 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001783 }
1784
you.chen9ac66392022-08-06 17:01:16 +08001785 if (NULL != scan_list)
1786 {
1787 free(scan_list);
1788 }
1789 if (NULL != save_list)
1790 {
1791 free(save_list);
1792 }
1793
1794 return ret;
you.chen35020192022-05-06 11:30:57 +08001795}
1796
1797static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1798{
1799 char lynq_auth_cmd[64]={0};
1800 char lynq_ket_mgmt_cmd[64]={0};
1801 char lynq_pairwise_cmd[64]={0};
1802 char lynq_psk_cmd[64]={0};
1803
1804 CHECK_WPA_CTRL(CTRL_STA);
1805
qs.xiong1af5daf2022-03-14 09:12:12 -04001806 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001807 case LYNQ_WIFI_AUTH_OPEN:
1808 {
1809 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001810
you.chen35020192022-05-06 11:30:57 +08001811 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1812// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001813 break;
1814 }
you.chen35020192022-05-06 11:30:57 +08001815 case LYNQ_WIFI_AUTH_WPA_PSK:
1816 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001817 {
you.chen35020192022-05-06 11:30:57 +08001818 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1819 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1820 }
1821 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001822 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001823 }
1824 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1825 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001826
you.chen35020192022-05-06 11:30:57 +08001827 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1828 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1829 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001830
you.chen35020192022-05-06 11:30:57 +08001831 if (password != NULL) {
1832 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1833 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1834 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001835
you.chen35020192022-05-06 11:30:57 +08001836// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001837 break;
you.chen35020192022-05-06 11:30:57 +08001838 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001839 default:
1840 return -1;
you.chen35020192022-05-06 11:30:57 +08001841 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001842
qs.xiongf1b525b2022-03-31 00:58:23 -04001843 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001844}
qs.xiong7a105ce2022-03-02 09:43:11 -05001845
you.chen35020192022-05-06 11:30:57 +08001846static int inner_get_curr_net_no(int interface) {
1847 curr_status_info curr_state;
1848 curr_state.ap = NULL;
1849 curr_state.state = NULL;
1850
1851 if (0 != inner_get_status_info(interface, &curr_state)) {
1852 return -1;
1853 }
1854
1855 return curr_state.net_no;
1856}
1857
1858int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001859{
you.chen35020192022-05-06 11:30:57 +08001860 int net_no;
1861 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001862
you.chen35020192022-05-06 11:30:57 +08001863 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001864
you.chen35020192022-05-06 11:30:57 +08001865 if (net_no < 0) {
1866 return -1;
1867 }
1868
1869 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001870}
1871
you.chen35020192022-05-06 11:30:57 +08001872int 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 -05001873{
you.chen35020192022-05-06 11:30:57 +08001874 int count, net_no, index;
1875 int net_no_list[128];
1876 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001877
you.chen35020192022-05-06 11:30:57 +08001878 if (ssid == NULL || *ssid == '\0') {
1879 printf("bad ssid\n");
1880 return -1;
1881 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001882
you.chen35020192022-05-06 11:30:57 +08001883 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1884 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1885 printf("bad password\n");
1886 return -1;
1887 }
1888 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001889
you.chen35020192022-05-06 11:30:57 +08001890 CHECK_IDX(idx, CTRL_STA);
1891
1892 net_no = -1;
1893 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1894
1895 for (index=0; index < count; index++) {
1896 net_auth = -1;
1897 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1898 net_no = net_no_list[index];
1899 break;
1900 }
1901 }
1902
1903 if (net_no < 0) {
1904 net_no = lynq_add_network(CTRL_STA);
1905 if (net_no == -1) {
1906 return -1;
1907 }
1908
1909 printf("net no is %d\n", net_no);
1910 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1911 return -1;
1912 }
1913 }
1914
1915 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1916 return -1;
1917 }
1918
1919 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001920}
1921
you.chen35020192022-05-06 11:30:57 +08001922int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001923{
you.chen35020192022-05-06 11:30:57 +08001924 ap_info_s ap;
1925 curr_status_info curr_state;
1926 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001927
you.chen35020192022-05-06 11:30:57 +08001928 if (ssid == NULL || *ssid == '\0') {
1929 return -1;
1930 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001931
you.chen35020192022-05-06 11:30:57 +08001932 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001933
you.chen35020192022-05-06 11:30:57 +08001934 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001935 curr_state.state = NULL;
1936
you.chen35020192022-05-06 11:30:57 +08001937 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1938 return 0;
1939 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001940
you.chen35020192022-05-06 11:30:57 +08001941 if (strcmp(ap.ap_ssid, ssid) != 0) {
1942 return 0;
1943 }
1944
1945 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001946}
qs.xiong97fa59b2022-04-07 05:41:29 -04001947
you.chena6cd55a2022-05-08 12:20:18 +08001948int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1949{
qs.xiongad2f89d2023-01-18 13:17:41 +08001950// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
1951// const char *lynq_reconnect_cmd = "RECONNECT";
1952 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
1953 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
1954// 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 -05001955
you.chen35020192022-05-06 11:30:57 +08001956 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001957 CHECK_WPA_CTRL(CTRL_STA);
1958
1959 system("connmanctl enable wifi");
1960
qs.xiongad2f89d2023-01-18 13:17:41 +08001961 if (system("ifconfig | grep -q wlan0") != 0)
1962 {
you.chen35020192022-05-06 11:30:57 +08001963 return -1;
1964 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001965
qs.xiongad2f89d2023-01-18 13:17:41 +08001966// DO_OK_FAIL_REQUEST(cmd_remove_all);
1967// system(lynq_first_sta_cmd);
1968// system(lynq_reconfigure_cmd);
1969// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
1970 system(lynq_enable_sta_cmd);
1971 system(lynq_reconnect_cmd);
1972// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001973 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001974}
1975
you.chen35020192022-05-06 11:30:57 +08001976int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001977{
qs.xiongad2f89d2023-01-18 13:17:41 +08001978// char lynq_disable_network_cmd[MAX_CMD];
1979// curr_status_info curr_state;
1980// ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001981
qs.xiongad2f89d2023-01-18 13:17:41 +08001982 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 +08001983 CHECK_IDX(idx, CTRL_STA);
1984 CHECK_WPA_CTRL(CTRL_STA);
1985
qs.xiongad2f89d2023-01-18 13:17:41 +08001986// curr_state.ap = &ap_info;
1987// curr_state.state = NULL;
you.chena6cd55a2022-05-08 12:20:18 +08001988
qs.xiongad2f89d2023-01-18 13:17:41 +08001989// if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1990// return 0;
1991// }
you.chena6cd55a2022-05-08 12:20:18 +08001992
qs.xiongad2f89d2023-01-18 13:17:41 +08001993// sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1994// DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1995 system(lynq_disable_sta_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001996 DO_OK_FAIL_REQUEST(cmd_save_config);
1997
1998 return 0;
1999// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04002000}
qs.xiong7a105ce2022-03-02 09:43:11 -05002001
you.chen35020192022-05-06 11:30:57 +08002002//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
2003// int i, count;
2004// char *p;
2005// const char * FLAG_SSID = "ssid=";
2006// const char * FLAG_SBSID = "bssid=";
2007// const char * FLAG_KEY_MGMT = "key_mgmt=";
2008// const char * FLAG_FREQ = "freq=";
2009// char lynq_sta_cmd[MAX_CMD];
2010// char *split_lines[128] = {0};
2011
2012// CHECK_WPA_CTRL(CTRL_AP);
2013
2014// sprintf(lynq_sta_cmd, "STA %s", bssid);
2015
2016// DO_REQUEST(lynq_sta_cmd);
2017
2018// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2019
2020// for(i=0; i < count; i++) {
2021// p = strstr(split_lines[i], FLAG_SSID);
2022// if (p != NULL) {
2023// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
2024// continue;
2025// }
2026// }
2027
2028// lynq_get_interface_ip(idx, ap->ap_ip);
2029// lynq_ap_password_set(idx, ap->psw);
2030
2031// return 0;
2032//}
2033
2034static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
2035 curr_status_info curr_state;
2036 curr_state.ap = ap;
2037 curr_state.state = NULL;
2038 return inner_get_status_info(interface, &curr_state);
2039}
2040
2041int 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 -04002042{
you.chend2fef3f2023-02-13 10:50:35 +08002043 int index, line_count;
2044 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08002045 const char *lynq_first_sta_cmd = "STA-FIRST";
2046 char lynq_next_sta_cmd[MAX_CMD];
2047 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08002048 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002049
you.chen35020192022-05-06 11:30:57 +08002050 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002051
you.chen35020192022-05-06 11:30:57 +08002052 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002053
you.chen35020192022-05-06 11:30:57 +08002054// ap_info_s * tmp_ap;
2055// device_info_s * tmp_list;
2056 if (ap == NULL || list == NULL || len == NULL) {
2057 printf("bad input param");
2058 return -1;
2059 }
2060
2061// ap = &tmp_ap;
2062// list = &tmp_list;
2063 *ap = malloc(sizeof (ap_info_s));
2064
2065 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
2066 return -1;
2067 }
2068
2069 lynq_get_interface_ip(idx, (*ap)->ap_ip);
2070 lynq_ap_password_get(idx, (*ap)->psw);
2071
you.chen35020192022-05-06 11:30:57 +08002072 DO_REQUEST(lynq_first_sta_cmd);
2073
2074 index = 0;
2075 while (reply_len > 0) {
2076 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2077 break;
2078 }
2079 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2080 bssid[index] = malloc(strlen(split_lines[0]) + 1);
2081 strcpy(bssid[index], split_lines[0]);
2082 index++;
2083 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
2084 reply_len = MAX_RET;
2085 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08002086 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 +08002087 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
2088 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08002089 break;
2090 }
2091 }
2092
2093 *len = index;
2094
2095 *list = malloc(sizeof(device_info_s) * (*len));
2096 for (index=0; index < *len; index++) {
you.chend2fef3f2023-02-13 10:50:35 +08002097 dev_info = &(*list)[index];
2098 memset(dev_info, 0, sizeof(device_info_s));
2099 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
2100 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
2101 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
2102 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08002103 free(bssid[index]);
2104 }
2105
2106 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002107}
2108
you.chen35020192022-05-06 11:30:57 +08002109int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002110{
you.chen35020192022-05-06 11:30:57 +08002111 int i, count, index, count_words;
2112 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2113 char *split_lines[128] = {0};
2114 char *split_words[128] = {0};
2115 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04002116
you.chen35020192022-05-06 11:30:57 +08002117 if (list == NULL || len == NULL) {
2118 return -1;
2119 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002120
you.chen9ac66392022-08-06 17:01:16 +08002121 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2122 {
2123 usleep(100 * 1000);
2124 }
2125
you.chen35020192022-05-06 11:30:57 +08002126 CHECK_IDX(idx, CTRL_STA);
2127
2128 CHECK_WPA_CTRL(CTRL_STA);
2129
2130 DO_REQUEST(lynq_scan_result_cmd);
2131
2132 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2133 *len = count - 1;
2134 *list = malloc(sizeof (scan_info_s) * *len);
2135
2136 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2137 for (index=0; index <count_words; index++) {
2138 printf("----header: %s\n", split_words[index]);
2139 }
2140
2141 for(index = 1;index < count; index++) {
2142 printf("---- %s\n",split_lines[index]);
2143 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2144 if (count_words < 4)
2145 continue;
2146 printf("count: %d, %s\n", count_words, split_words[0]);
2147 //bssid / frequency / signal level / flags / ssid
2148 p = (*list) + index - 1;
2149 strcpy(p->mac, split_words[0]);
2150 p->band = convert_band_from_freq(atoi(split_words[1]));
2151 p->rssi = -1 * atoi( split_words[2]);
2152 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chend2fef3f2023-02-13 10:50:35 +08002153 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
you.chen35020192022-05-06 11:30:57 +08002154 }
2155
2156 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002157}
qs.xiong97fa59b2022-04-07 05:41:29 -04002158
you.chen35020192022-05-06 11:30:57 +08002159int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2160{
2161 int count, net_no, index;
2162 int net_no_list[128];
2163 lynq_wifi_auth_s net_auth;
2164 char lynq_remove_cmd[MAX_CMD];
2165
2166 if (ssid == NULL || *ssid == '\0') {
2167 printf("bad ssid\n");
2168 return -1;
2169 }
2170
2171 CHECK_IDX(idx, CTRL_STA);
2172
2173 CHECK_WPA_CTRL(CTRL_STA);
2174
2175 net_no = -1;
2176 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2177
2178 for (index=0; index < count; index++) {
2179 net_auth = -1;
2180 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2181 net_no = net_no_list[index];
2182 break;
2183 }
2184 }
2185
2186 if (net_no < 0) {
2187 return 0;
2188 }
2189
2190 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2191
2192 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2193 DO_OK_FAIL_REQUEST(cmd_save_config);
2194
2195 return 0;
2196}
2197
2198int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2199{
you.chend2fef3f2023-02-13 10:50:35 +08002200 int count, index;
you.chen35020192022-05-06 11:30:57 +08002201 int net_no_list[128];
2202 char freq[16];
2203
2204 if (list == NULL || len == NULL) {
2205 printf("bad param\n");
2206 return -1;
2207 }
2208
2209 CHECK_IDX(idx, CTRL_STA);
2210
2211// CHECK_WPA_CTRL(CTRL_STA);
2212
2213 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2214 printf("count is %d\n", count);
2215
2216 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002217 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002218 *len = count;
2219
2220 for (index=0; index < count; index++) {
2221 printf("to get ssid %d\n", index);
2222 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08002223 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002224 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002225 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2226 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2227 }
2228 else {
2229 (*list)[index].base_info.band = -1;
2230 }
2231
you.chen755332b2022-08-06 16:59:10 +08002232 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002233 }
2234
2235 return 0;
2236}
2237
2238int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2239{
2240 const char *lynq_scan_cmd = "SCAN";
2241
2242 CHECK_IDX(idx, CTRL_STA);
2243
2244 CHECK_WPA_CTRL(CTRL_STA);
2245
2246 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen9ac66392022-08-06 17:01:16 +08002247 g_sta_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +08002248
2249 return 0;
2250}
2251
2252int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2253 if (cb == NULL) {
2254 return -1;
2255 }
2256
2257 g_ap_callback_priv = priv;
2258 g_ap_callback_func = cb;
2259
2260 return 0;
2261}
2262
2263int lynq_unreg_ap_event_callback(void * priv) {
2264 if (g_ap_callback_priv == priv) {
2265 g_ap_callback_func = NULL;
2266 g_ap_callback_priv = NULL;
2267 return 0;
2268 }
2269 return -1;
2270}
2271
2272int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2273 if (cb == NULL) {
2274 return -1;
2275 }
2276
2277 g_sta_callback_priv = priv;
2278 g_sta_callback_func = cb;
2279
2280 return 0;
2281}
2282
2283int lynq_unreg_sta_event_callback(void * priv) {
2284 if (g_sta_callback_priv == priv) {
2285 g_sta_callback_func = NULL;
2286 g_sta_callback_priv = NULL;
2287 return 0;
2288 }
2289 return -1;
2290}
2291
2292
2293static int inner_get_status_info_state (int interface, char *state) {
2294 curr_status_info curr_state;
2295 curr_state.ap = NULL;
2296 curr_state.state = state;
2297 return inner_get_status_info(interface, &curr_state);
2298}
2299
2300int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2301{
2302 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002303 CHECK_IDX(idx, CTRL_AP);
2304
2305 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2306 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2307 return 0;
2308 }
2309
2310 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2311 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2312 }
2313 else {
2314 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2315 }
2316
2317 return 0;
2318}
2319
2320int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2321 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002322 CHECK_IDX(idx, CTRL_STA);
2323
2324 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2325 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2326 return 0;
2327 }
2328
2329 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2330 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2331 }
2332 else {
2333 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2334 }
2335
2336 return 0;
2337}
2338
2339int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2340// CHECK_IDX(idx, CTRL_AP);
2341// int ret = 0;
2342// size_t reply_len = MAX_RET;
2343// char cmd_reply[MAX_RET]={0};
2344// const char * cmd_str = "GET country";
2345// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2346// do{
2347// if (NULL == s_lynq_wpa_ctrl) {
2348// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2349// if (NULL == s_lynq_wpa_ctrl ) {
2350// printf("wpa_ctrl_open fail\n");
2351// return -1;
2352// }
2353// }
2354// }while(0);
2355
2356// do {
2357// reply_len = MAX_RET;
2358// cmd_reply[0] = '\0';
2359// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08002360// 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 +08002361// if (ret != 0) {
2362// printf("call ##cmd_str fail %d\n", ret);
2363// return ret;
2364// }
2365// cmd_reply[reply_len+1] = '\0';
2366// printf("cmd replay [ %s ]\n", cmd_reply);
2367// }while(0);
2368
2369 FILE *fp;
2370 size_t i = 0;
2371 char lynq_cmd_ret[MAX_RET]={0};
2372
2373// CHECK_IDX(idx, CTRL_AP);
2374
2375 if((fp=popen("wl country","r"))==NULL)
2376 {
2377 perror("popen error!");
2378 return -1;
2379 }
2380 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2381 {
2382 perror("fread fail!");
2383 return -1;
2384 }
2385
2386 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2387 if (lynq_cmd_ret[i] == ' ') {
2388 lynq_cmd_ret[i] = '\0';
2389 break;
2390 }
2391 }
2392
2393 strcpy(country_code,lynq_cmd_ret);
2394 printf("---country code %s\n", country_code);
2395
2396 int ret=pclose(fp);
2397 if(ret==-1)
2398 {
2399 perror("close file faild");
2400 }
2401
2402 return 0;
2403}
2404
2405int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2406// const char * cmd_str = "GET country";
2407// CHECK_IDX(idx, CTRL_AP);
2408// CHECK_WPA_CTRL(CTRL_STA);
2409
2410// DO_REQUEST(cmd_str);
2411// printf("result %s\n", cmd_reply);
2412
2413 if (country_code == NULL || *country_code == '\0') {
2414 printf("bad country code\n");
2415 return -1;
2416 }
2417
2418 char lynq_country_cmd[MAX_CMD];
2419 sprintf(lynq_country_cmd, "wl country %s", country_code);
2420 if (system(lynq_country_cmd) == 0) {
2421 return 0;
2422 }
2423
2424 return -1;
2425}
2426
2427int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2428{
2429 if (mac == NULL) {
2430 return -1;
2431 }
2432
2433 CHECK_IDX(idx, CTRL_STA);
2434 ap_info_s ap;
2435 ap.ap_mac[0] = '\0';
2436
2437 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2438 return -1;
2439 }
2440 strcpy(mac, ap.ap_mac);
2441
2442 return 0;
2443}
2444
2445int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2446{
you.chen9ac66392022-08-06 17:01:16 +08002447 struct ifaddrs *ifaddr_header, *ifaddr;
2448 struct in_addr * ifa;
2449 const char * ifaName = "wlan0";
2450 if (ip == NULL)
2451 {
you.chenf58b3c92022-06-21 16:53:48 +08002452 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002453 }
you.chenf58b3c92022-06-21 16:53:48 +08002454
you.chen9ac66392022-08-06 17:01:16 +08002455 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002456 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002457 }
2458 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002459 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002460 }
you.chen35020192022-05-06 11:30:57 +08002461
you.chen9ac66392022-08-06 17:01:16 +08002462 if (getifaddrs(&ifaddr_header) == -1)
2463 {
you.chen35020192022-05-06 11:30:57 +08002464 perror("getifaddrs");
2465 return -1;
2466 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002467 }
you.chen35020192022-05-06 11:30:57 +08002468
2469
you.chen9ac66392022-08-06 17:01:16 +08002470 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2471 {
2472 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002473 continue;
you.chen9ac66392022-08-06 17:01:16 +08002474 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2475 {
2476 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2477 {
2478 // is a valid IP4 Address
2479 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2480 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2481 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2482 freeifaddrs(ifaddr_header);
2483 printf("ip %s\n", ip);
2484 return 0;
2485 }
2486 }
2487 }
qs.xiongc4f007c2023-02-08 18:16:58 +08002488 freeifaddrs(ifaddr_header);
you.chen9ac66392022-08-06 17:01:16 +08002489 return -1;
you.chen35020192022-05-06 11:30:57 +08002490}
2491
2492int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2493{
2494 int count;
2495 size_t i;
2496 char *split_words[128] = {0};
2497 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2498
2499 CHECK_WPA_CTRL(idx);
2500
2501 DO_REQUEST(lynq_get_mac_cmd);
2502
2503 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2504 return -1;
2505 }
2506
2507 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2508
2509 if (count < 2) {
2510 return -1;
2511 }
2512
2513 for (i=0; i < strlen(split_words[1]); i++ ) {
2514 if (split_words[1][i] != ' ') {
2515 break;
2516 }
2517 }
2518
2519 strcpy(mac, split_words[1] + i);
2520
2521 return 0;
2522}
2523
2524int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2525{
2526// int count;
2527// char *split_words[128] = {0};
2528// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2529
2530// if (rssi == NULL) {
2531// return -1;
2532// }
2533
2534// CHECK_IDX(idx, CTRL_STA);
2535
2536// CHECK_WPA_CTRL(CTRL_STA);
2537
2538// DO_REQUEST(lynq_get_rssi_cmd);
2539
2540// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2541// return -1;
2542// }
2543
2544// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2545
2546// if (count < 2) {
2547// return -1;
2548// }
2549
2550// *rssi = atoi(split_words[1]) * -1;
2551
2552 FILE *fp;
2553 size_t i = 0;
2554 char lynq_cmd_ret[MAX_RET]={0};
2555
2556// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002557/*******change other cmd to get rssi*******
2558 *
2559 *wl rssi ---> wl -i wlan0 rssi
2560 *
2561 ***** change by qs.xiong 20221011*******/
2562 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002563 {
2564 perror("popen error!");
2565 return -1;
2566 }
2567 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2568 {
2569 perror("fread fail!");
2570 return -1;
2571 }
you.chen9f17e4d2022-06-06 17:18:18 +08002572 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002573/****** if got rssi is 0,means sta didn't connected any device****/
2574 if(*rssi == 0)
2575 {
2576 printf("sta didn't connected any ap device,please check connection\n");
2577 }
you.chen35020192022-05-06 11:30:57 +08002578
2579 return 0;
2580}
2581
2582int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2583{
2584 if (band == NULL) {
2585 return -1;
2586 }
2587
2588 CHECK_IDX(idx, CTRL_STA);
2589 ap_info_s ap;
2590 ap.band = -1;
2591
2592 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2593 return -1;
2594 }
2595 *band = ap.band;
2596
2597 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002598}
you.chenf58b3c92022-06-21 16:53:48 +08002599
2600int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2601{
you.chenf58b3c92022-06-21 16:53:48 +08002602 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08002603
2604 if (ip == NULL)
2605 {
2606 printf("invalid param");
2607 return -1;
2608 }
2609
2610 CHECK_IDX(idx, CTRL_STA);
2611
2612 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2613 {
2614 return -1;
2615 }
2616
you.chend2fef3f2023-02-13 10:50:35 +08002617 return inner_get_ip_by_mac(bssid, ip, 32); //better input by user
you.chenf58b3c92022-06-21 16:53:48 +08002618}
2619
qs.xiong026c5c72022-10-17 11:15:45 +08002620int lynq_ap_connect_num(int sta_number)
2621{
2622 char lynq_limit_cmd[32]={0};
2623 int ret;
2624 if((sta_number < 1 ) && (sta_number > 15)){
2625 printf("sta_number: not in range\n",sta_number);
2626 return -1;
2627 }
2628 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2629 ret = system(lynq_limit_cmd);
2630 if(ret != 0){
2631 printf("cmd of limit ap devices number error\n");
2632 }
2633 return 0;
2634}
you.chenf58b3c92022-06-21 16:53:48 +08002635
qs.xiong77905552022-10-17 11:19:57 +08002636int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
2637{
2638
2639 char lynq_wifi_acs_cmd[128]={0};
2640 char lynq_cmd_mode[128]={0};
2641 char lynq_cmd_slect[128]={0};
2642
2643 if((acs_mode != 2) && (acs_mode != 5)){
2644 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
2645 }
2646
2647 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
2648 return -1;
2649 }
2650
2651 CHECK_IDX(idx, CTRL_AP);
2652
2653 CHECK_WPA_CTRL(CTRL_AP);
2654
2655 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
2656 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2657 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2658
2659 DO_OK_FAIL_REQUEST(cmd_disconnect);
2660 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
2661 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2662 DO_OK_FAIL_REQUEST(cmd_save_config);
2663 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
2664
2665 return 0;
2666}
you.chen0f5c6432022-11-07 18:31:14 +08002667//you.chen add for tv-box start
2668static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
2669 FILE *fp;
2670 //printf("to exec cmd:%s\n", str_cmd);
2671 if((fp=popen(str_cmd,"r"))==NULL)
2672 {
2673 perror("popen error!");
2674 return -1;
2675 }
2676 if((fread(str_cmd_ret,max_len,1,fp))<0)
2677 {
2678 perror("fread fail!");
2679 fclose(fp);
2680 return -1;
2681 }
2682 fclose(fp);
2683 return 0;
2684}
2685
2686static int get_netmask_length(const char* mask)
2687{
2688 int masklen=0, i=0;
2689 int netmask=0;
2690
2691 if(mask == NULL)
2692 {
2693 return 0;
2694 }
2695
2696 struct in_addr ip_addr;
2697 if( inet_aton(mask, &ip_addr) )
2698 {
2699 netmask = ntohl(ip_addr.s_addr);
2700 }else{
2701 netmask = 0;
2702 return 0;
2703 }
2704
2705 while(0 == (netmask & 0x01) && i<32)
2706 {
2707 i++;
2708 netmask = netmask>>1;
2709 }
2710 masklen = 32-i;
2711 return masklen;
2712}
2713
2714static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
2715 int mask_len;
2716 char *p;
2717 char tmp[64] = {0};
2718 if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0)
2719 return -1;
2720 p = strstr(str_cmd_ret, "Mask:");
2721 if (p == NULL)
2722 return -1;
2723 mask_len = get_netmask_length(p + 5);
2724 if (mask_len == 0)
2725 return -1;
2726 p = strstr(str_cmd_ret, "inet addr:");
2727 if (p == NULL)
2728 return -1;
2729 strcpy(tmp, p + 10);
2730 p = strstr(tmp, " ");
2731 if (p != NULL)
2732 *p = '\0';
2733 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
2734 return 0;
2735}
2736
2737static void GBWWatchThreadProc() {
2738 int i,n, nloop, nmax, ncheckcount, nidlecount;
2739 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
2740 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
2741 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
2742 char *results[16] = {0};
2743 char str_cmd[256] = {0};
2744 char str_cmd_ret[128] = {0};
2745 char dest_ip[32] = {0};
2746 lastAP1Bytes = lastAP2Bytes = 0;
2747 lastAP1Drop = lastAP2Drop = 0;
2748 lastAP1Speed = lastAP2Speed = 0;
2749 setAP1Speed = 50;
2750 setAP2Speed = 80;
2751 nloop = 0;
2752 nmax = 6;
2753 ncheckcount = nidlecount = 0;
2754
2755 printf("------gbw thread run\n");
2756 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
2757 while (dest_ip[0] == '\0') {
2758 sleep(1);
2759 str_cmd_ret[0] = '\0';
2760 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
2761 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
2762 if (str_cmd_ret[n] == '\n'){
2763 str_cmd_ret[n] = '\0';
2764 break;
2765 }
2766 }
2767 if (str_cmd_ret[0] != '\0')
2768 {
2769 strcpy(dest_ip, str_cmd_ret);
2770 }
2771 }
2772
2773 system("tc qdisc del dev tether root > /dev/null 2>&1");
2774 system("tc qdisc add dev tether root handle 1: htb r2q 1");
2775 system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000");
2776 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
2777 {
2778 printf("not get tether info\n");
2779 return;
2780 }
2781 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);
2782 system(str_cmd);
2783 system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000");
2784 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip);
2785 //printf("----cmd:%s\n", str_cmd);
2786 system(str_cmd);
2787
2788 while (1) {
2789 sleep(1);
2790 memset(str_cmd, 0, sizeof(str_cmd));
2791 if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd)))
2792 continue;
2793 //printf("ap1 --- %s\n", str_cmd);
2794 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2795 if (n > 9) {
2796 if (strcmp(results[1], "Sent") == 0) {
2797 currAP1Bytes = atoll(results[2]);
2798 }
2799 if (strcmp(results[6], "(dropped") == 0) {
2800 currAP1Drop = atoi(results[7]);
2801 }
2802 }
2803
2804 memset(str_cmd, 0, sizeof(str_cmd));
2805 if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd)))
2806 continue;
2807 //printf("ap2 --- %s\n", str_cmd);
2808 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2809 if (n > 9) {
2810 if (strcmp(results[1], "Sent") == 0) {
2811 currAP2Bytes = atoll(results[2]);
2812 }
2813 if (strcmp(results[6], "(dropped") == 0) {
2814 currAP2Drop = atoi(results[7]);
2815 }
2816 }
2817
2818 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
2819 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
2820 lastAP1Bytes = currAP1Bytes;
2821 lastAP2Bytes = currAP2Bytes;
2822 continue;
2823 }
2824
2825 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
2826 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
2827 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
2828 lastAP1Speed = currAP1Speed;
2829 lastAP2Speed = currAP2Speed;
2830 lastAP1Bytes = currAP1Bytes;
2831 lastAP2Bytes = currAP2Bytes;
2832
2833 currSetAP1Speed = setAP1Speed;
2834 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
2835 ncheckcount++;
2836 if (ncheckcount > 3) {
2837 ncheckcount = 0;
2838 currSetAP1Speed = 5;
2839 }
2840 }
2841 else {
2842 ncheckcount = 0;
2843 if (currAP1Speed < 5)
2844 nidlecount++;
2845 else
2846 nidlecount = 0;
2847
2848 }
2849
2850 if (nidlecount > 60 ){
2851 currSetAP1Speed = 50;
2852 }
2853
2854 if (currSetAP1Speed != setAP1Speed) {
2855 setAP1Speed = currSetAP1Speed;
2856 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));
2857 //printf("------***change speed: %s\n", str_cmd);
2858 system(str_cmd);
2859 }
2860 }
2861}
2862
2863int enableGBW(const char* mac) {
2864 int i,len;
2865 char get_ipaddr_cmd[128]={0};
2866 ap_info_s *ap;
2867 device_info_s * list;
2868
2869 if (mac == NULL || g_gbw_enabled == 1)
2870 return -1;
2871 len = strlen(mac);
2872 g_gbw_mac = malloc(len + 1);
2873 for(i=0;i<len;i++) {
2874 if (mac[i] >= 'A' && mac[i] <= 'Z')
2875 {
2876 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
2877 }
2878 else
2879 g_gbw_mac[i] = mac[i];
2880 }
2881 g_gbw_mac[i] = '\0';
2882 g_gbw_enabled = 1;
2883
2884 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
2885 if (system(get_ipaddr_cmd) == 0) {
2886 //startGBW();
2887 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
2888 for (i=0;i<len;i++) {
2889 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
2890 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
2891 startGBW();
2892 }
2893 free(ap);
2894 free(list);
2895 }
2896 }
2897 return 0;
2898}
2899
2900int disableGBW() {
2901 stopGBW();
2902 free(g_gbw_mac);
2903 g_gbw_mac = NULL;
2904 g_gbw_enabled = 1;
2905 return 0;
2906}
2907
2908static int startGBW() {
2909 if (g_gbw_watcher_pid != 0) {
2910 stopGBW();
2911 }
2912 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
2913}
2914
2915static int stopGBW() {
2916 void* retval;
2917 pthread_cancel(g_gbw_watcher_pid);
2918 pthread_join(g_gbw_watcher_pid, &retval);
2919 g_gbw_watcher_pid = 0;
2920 system("tc qdisc del dev tether root");
2921}
2922//you.chen add for tv-box end