blob: ed38fbf0bb5c052a4f8bad07a0d334b0e0fee398 [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.xiong9fbf74e2023-03-28 13:38:22 +080024#include "log/log.h"
you.chen70f377f2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050027
qs.xiong1af5daf2022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
you.chen35020192022-05-06 11:30:57 +080034
qs.xiong9fbf74e2023-03-28 13:38:22 +080035#undef LOG_TAG
36#define LOG_TAG "LYNQ_WIFI"
you.chen35020192022-05-06 11:30:57 +080037#define MAX_CMD 128
38#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040039#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080040#define CTRL_STA 0
41#define CTRL_AP 1
42#define AP_NETWORK_0 0
qs.xiongf71b53b2023-05-03 13:12:07 +080043#define STA_MAX_SAVED_AP_NUM 50
you.chen35020192022-05-06 11:30:57 +080044
45pthread_t g_ap_watcher_pid = 0;
46volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080047volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080048
49pthread_t g_sta_watcher_pid = 0;
50volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080051volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080052volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080053
qs.xiongfcc914b2023-07-06 21:16:20 +080054pthread_t g_sta_auto_watcher_pid = 0;
55volatile int g_sta_auto_watcher_stop_flag = 0;
56volatile int g_sta_auto_scan_finish_flag = 1;
57volatile int g_sta_auto_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080058void * g_ap_callback_priv = NULL;
59AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
60void * g_sta_callback_priv = NULL;
61STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
qs.xiongfcc914b2023-07-06 21:16:20 +080062void * g_sta_auto_callback_priv = NULL;
63STA_AUTO_CALLBACK_FUNC_PTR g_sta_auto_callback_func = NULL;
you.chen35020192022-05-06 11:30:57 +080064
65//const char * CTRL_PATH="/var/run/wpa_supplicant";
66const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
67//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
68const char * cmd_list_networks = "LIST_NETWORKS";
69const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080070const char * cmd_disconnect = "DISCONNECT";
71const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080072const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080073const char * STATE_COMPLETED = "COMPLETED";
you.chen6d247052023-06-01 16:39:54 +080074const char * STATE_DISCONNECTED = "DISCONNECTED";
you.chen35020192022-05-06 11:30:57 +080075
you.chenf711c8a2023-04-13 13:49:45 +080076const char * cmd_ping = "PING";
77const char * rsp_pong = "PONG";
78const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
79const int MAX_IDLE_COUNT = 600; // 60s
80
you.chenc9928582023-04-24 15:39:37 +080081const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
82const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
83const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
84const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
85const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
86
87static char s_ap_iterface_name[64] = {0};
88
you.chend2fef3f2023-02-13 10:50:35 +080089struct local_wpa_ctrl{
90 struct wpa_ctrl *ctrl;
91 pthread_mutex_t mutex;
92};
93
you.chen70f377f2023-04-14 18:17:09 +080094
you.chend2fef3f2023-02-13 10:50:35 +080095static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chen6d247052023-06-01 16:39:54 +080096static pthread_mutex_t s_sta_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
97static pthread_mutex_t s_ap_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
qs.xiongfcc914b2023-07-06 21:16:20 +080098// add for auto connect
99static pthread_mutex_t s_sta_auto_callback_mutex = PTHREAD_MUTEX_INITIALIZER;
you.chend2fef3f2023-02-13 10:50:35 +0800100
101static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +0800102
you.chen0f5c6432022-11-07 18:31:14 +0800103//you.chen add for tv-box start
104volatile int g_gbw_enabled = 0;
105char * g_gbw_mac = NULL;
106pthread_t g_gbw_watcher_pid = 0;
107static int startGBW();
108static int stopGBW();
109//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800110
111typedef struct __curr_status_info {
112 ap_info_s *ap;
113 char * state;
114 int net_no;
115}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -0400116
you.chen70f377f2023-04-14 18:17:09 +0800117typedef enum {
118 INNER_STA_STATUS_INIT = 0,
119 INNER_STA_STATUS_CONNECTING,
120 INNER_STA_STATUS_ASSOCIATING,
121 INNER_STA_STATUS_ASSOCIATED,
122 INNER_STA_STATUS_CONNECTED,
123 INNER_STA_STATUS_DISCONNECTING,
124 INNER_STA_STATUS_DISCONNECTED,
125 INNER_STA_STATUS_CANCEL,
126}inner_sta_status_s;
127
128static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
129static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
130static inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
131static error_number_s s_sta_error_number = -1;
132static char s_sta_current_connecting_ssid[64] = {0};
133static struct timespec s_sta_connect_timeout;
134const int MAX_CONNNECT_TIME = 15; // second
135pthread_t g_global_watcher_pid = 0;
136static int s_service_invoke_timeout_cnt=0;
137const int FAKE_MAX_INT_VALUE = 99999;
138
139static void notify_service_invoke_fail(int error)
140{
141 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
142 pthread_mutex_lock(&s_global_check_mutex);
143 if (error == -2) //timeout
144 {
145 s_service_invoke_timeout_cnt++;
146 if (s_service_invoke_timeout_cnt > 10)
147 {
148 pthread_cond_signal(&s_global_check_cond);
149 }
150 }
151 else if (error == -1)
152 {
153 // check if can connect wpa service
154 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
155 if (lynq_wpa_ctrl == NULL)
156 {
157 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
158 pthread_cond_signal(&s_global_check_cond);
159 }
160 wpa_ctrl_close(lynq_wpa_ctrl);
161 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
162 if (lynq_wpa_ctrl == NULL)
163 {
164 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
165 pthread_cond_signal(&s_global_check_cond);
166 }
167 wpa_ctrl_close(lynq_wpa_ctrl);
168 }
169
170 pthread_mutex_unlock(&s_global_check_mutex);
171}
172
you.chenc9928582023-04-24 15:39:37 +0800173static int system_call_v(const char * fmt, ...)
174{
175 char str_cmd[256] = {0};
176 va_list args;
177 va_start(args, fmt);
178 vsprintf(str_cmd, fmt, args);
179 va_end(args);
180 printf("system call----------%s\n", str_cmd);
181 return system(str_cmd);
182}
183
you.chen0df3e7e2023-05-10 15:56:26 +0800184static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
185
186static const char * inner_get_ap_interface_name()
187{
188 char * p;
189 char cmd[128]={0};
190
191 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
192 if (0 != exec_cmd(cmd, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
193 {
194 memset(s_ap_iterface_name, 0, sizeof (s_ap_iterface_name));
195 return NULL;
196 }
197 p = strchr(s_ap_iterface_name, ' ');
198 if (NULL != p)
199 {
200 *p = '\0';
201 }
202 p = strchr(s_ap_iterface_name, '\n');
203 if (NULL != p)
204 {
205 *p = '\0';
206 }
207 if (s_ap_iterface_name[0] == '\0')
208 {
209 return NULL;
210 }
211
212 return s_ap_iterface_name;
213}
214
you.chen70f377f2023-04-14 18:17:09 +0800215static void check_tether_and_notify()
216{
217 RLOGD("check_tether_and_notify called");
you.chen0df3e7e2023-05-10 15:56:26 +0800218 if (inner_get_ap_interface_name() == NULL || 0 == system_call_v("ifconfig | grep %s", s_ap_iterface_name))
you.chen70f377f2023-04-14 18:17:09 +0800219 {
220 return;
221 }
222 pthread_mutex_lock(&s_global_check_mutex);
223 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
224 pthread_cond_signal(&s_global_check_cond);
225 pthread_mutex_unlock(&s_global_check_mutex);
226}
227
you.chend2fef3f2023-02-13 10:50:35 +0800228static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
229 char *reply, size_t *reply_len,
230 void (*msg_cb)(char *msg, size_t len))
231{
232 int ret;
233 if (ctrl->ctrl == NULL) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800234 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chend2fef3f2023-02-13 10:50:35 +0800235 return -1;
236 }
237 pthread_mutex_lock(&ctrl->mutex);
238 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
239 pthread_mutex_unlock(&ctrl->mutex);
you.chen70f377f2023-04-14 18:17:09 +0800240 if (ret != 0)
241 {
242 notify_service_invoke_fail(ret);
243 }
you.chend2fef3f2023-02-13 10:50:35 +0800244 return ret;
245}
246
247static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
248 int repeat_cnt;
249 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
250 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800251 RLOGD("inner_get_wpa_ctrl\n");
you.chend2fef3f2023-02-13 10:50:35 +0800252 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
253 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
254// printf("wait enable finish\n");
255 usleep(500 * 1000);
256 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
257 }
258 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800259 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chend2fef3f2023-02-13 10:50:35 +0800260 goto out_addr;
261 }
262 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
263 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
264 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800265 RLOGE("wpa_ctrl_open fail\n");
you.chend2fef3f2023-02-13 10:50:35 +0800266 goto out_addr;
267 }
268 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
269 }
270 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
271out_addr:
272 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
273 return lynq_wpa_ctrl;
274}
275
qs.xiong97fa59b2022-04-07 05:41:29 -0400276#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -0400277{\
you.chen35020192022-05-06 11:30:57 +0800278 perror((str));\
279 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -0400280}
281
you.chen35020192022-05-06 11:30:57 +0800282#define CHECK_IDX(idx, type) do { \
283 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
284 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800285 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen35020192022-05-06 11:30:57 +0800286 return -1; \
287 } \
288 }while (0)
289
290#define CHECK_WPA_CTRL(index) int ret = 0;\
291 size_t reply_len = MAX_RET; \
292 char cmd_reply[MAX_RET]={0}; \
you.chend2fef3f2023-02-13 10:50:35 +0800293 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +0800294 do{ \
you.chend2fef3f2023-02-13 10:50:35 +0800295 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
296 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen35020192022-05-06 11:30:57 +0800297 }while(0)
298
299#define DO_REQUEST(cmd_str) do { \
300 reply_len = MAX_RET;\
301 cmd_reply[0] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800302 RLOGD("to call [%s]\n", cmd_str); \
you.chend2fef3f2023-02-13 10:50:35 +0800303 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 +0800304 if (ret != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800305 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800306 return ret; \
307 } \
308 cmd_reply[reply_len+1] = '\0'; \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800309 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen35020192022-05-06 11:30:57 +0800310 }while(0)
311
312#define DO_OK_FAIL_REQUEST(cmd_str) do { \
313 DO_REQUEST(cmd_str); \
314 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiong9fbf74e2023-03-28 13:38:22 +0800315 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800316 return -1; \
317 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiong9fbf74e2023-03-28 13:38:22 +0800318 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800319 return -1; \
320 } \
321 }while (0)
322
323
you.chenf711c8a2023-04-13 13:49:45 +0800324static int check_connection(struct wpa_ctrl * wpa_ctrl)
325{
326 size_t reply_len = MAX_RET;
327 char cmd_reply[MAX_RET]={0};
328 int ret;
329
330 RLOGD("check_connection [%p]", wpa_ctrl);
331 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
332
333 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
334 {
335 RLOGE("check_connection error: ctrl [%p], ret [%d], reply_len [%d], rsp [%s]", wpa_ctrl, ret, reply_len, cmd_reply);
you.chen70f377f2023-04-14 18:17:09 +0800336 if (ret != 0)
337 {
338 notify_service_invoke_fail(ret);
339 }
you.chenf711c8a2023-04-13 13:49:45 +0800340 return -1;
341 }
342
343 return 0;
344}
345
346/**
347 * @brief check_pending_msg
348 * @param lynq_wpa_ctrl
349 * @return 1 has msg, 0 no msg, -1 error
350 */
351static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
352{
353 int ret;
354
355 if (*pp_lynq_wpa_ctrl == NULL) // need connect
356 {
357 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
358 if (*pp_lynq_wpa_ctrl == NULL)
359 {
360 usleep(SLEEP_TIME_ON_IDLE);
361 return -1;
362 }
363
364 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
365 if (ret == 0) // attach success
366 {
367 *started_flag = 1;
368 }
369 else
370 {
you.chen70f377f2023-04-14 18:17:09 +0800371 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chenf711c8a2023-04-13 13:49:45 +0800372 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
373 *pp_lynq_wpa_ctrl = NULL;
374 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800375 notify_service_invoke_fail(-2);
you.chenf711c8a2023-04-13 13:49:45 +0800376 usleep(SLEEP_TIME_ON_IDLE);
377 return -1;
378 }
379 }
380
381 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
382 if ( ret == 0) // no pending messages
383 {
384 usleep(SLEEP_TIME_ON_IDLE);
385 *idle_count += 1;
386 if (*idle_count > MAX_IDLE_COUNT)
387 {
388 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
389 {
390 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
391 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
392 *pp_lynq_wpa_ctrl = NULL;
393 *idle_count = 0;
394 return -1;
395 }
396 *idle_count = 0;
397 }
398 return 0;
399 }
400 else if ( ret == -1) // on error
401 {
402 RLOGE("[wifi error]sta wpa_ctrl_pending");
403 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
404 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
405 *pp_lynq_wpa_ctrl = NULL;
406 *idle_count = 0;
you.chen70f377f2023-04-14 18:17:09 +0800407 notify_service_invoke_fail(ret);
you.chenf711c8a2023-04-13 13:49:45 +0800408 return -1;
409 }
410
411 *idle_count = 0;
412 return 1;
413}
414
you.chen6d247052023-06-01 16:39:54 +0800415static inline void inner_notify_ap_msg(lynq_wifi_ap_status_s status)
416{
417 pthread_mutex_lock(&s_ap_callback_mutex);
418 if (g_ap_callback_func != NULL)
419 g_ap_callback_func(g_ap_callback_priv, status);
420 pthread_mutex_unlock(&s_ap_callback_mutex);
421}
422
you.chen35020192022-05-06 11:30:57 +0800423static void APWatcherThreadProc() {
424 size_t len = MAX_RET;
425 char msg_notify[MAX_RET];
you.chenf711c8a2023-04-13 13:49:45 +0800426 int idle_count = 0;
you.chen35020192022-05-06 11:30:57 +0800427
you.chen6c2dd9c2022-05-16 17:55:28 +0800428 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800429 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800430
qs.xiong9fbf74e2023-03-28 13:38:22 +0800431 while (g_ap_watcher_stop_flag == 0)
432 {
you.chenf711c8a2023-04-13 13:49:45 +0800433 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
434 {
you.chen70f377f2023-04-14 18:17:09 +0800435 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
436 {
437 check_tether_and_notify();
438 }
439
you.chen35020192022-05-06 11:30:57 +0800440 continue;
441 }
you.chenf711c8a2023-04-13 13:49:45 +0800442
you.chen6c2dd9c2022-05-16 17:55:28 +0800443 memset(msg_notify, 0, MAX_RET);
444 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800445 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chenf711c8a2023-04-13 13:49:45 +0800446 {
you.chen35020192022-05-06 11:30:57 +0800447 msg_notify[len+1] = '\0';
qs.xiong455c30b2023-04-12 11:40:02 +0800448 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chenf711c8a2023-04-13 13:49:45 +0800449 //you.chen change for tv-box start
qs.xiong9fbf74e2023-03-28 13:38:22 +0800450 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800451 {
you.chen6d247052023-06-01 16:39:54 +0800452 inner_notify_ap_msg(LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800453 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800454 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800455 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
456 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800457 {
you.chen0f5c6432022-11-07 18:31:14 +0800458 stopGBW();
459 }
460 }
you.chen35020192022-05-06 11:30:57 +0800461 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800462 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800463 {
you.chen6d247052023-06-01 16:39:54 +0800464 inner_notify_ap_msg(LYNQ_WIFI_STATUS_CONNECT);
qs.xiong9fbf74e2023-03-28 13:38:22 +0800465 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800466 {
qs.xiong9fbf74e2023-03-28 13:38:22 +0800467 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
468 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chenf711c8a2023-04-13 13:49:45 +0800469 {
you.chen0f5c6432022-11-07 18:31:14 +0800470 startGBW();
471 }
472 }
you.chen35020192022-05-06 11:30:57 +0800473 }
you.chenf711c8a2023-04-13 13:49:45 +0800474 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800475 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
476 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800477 if (lynq_wpa_ctrl != NULL)
478 {
you.chen92fd5d32022-05-25 10:09:47 +0800479 wpa_ctrl_detach(lynq_wpa_ctrl);
480 wpa_ctrl_close(lynq_wpa_ctrl);
481 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400482}
483
you.chen70f377f2023-04-14 18:17:09 +0800484static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
485{
486 char * p;
487 const char * try_associat_flag = "Trying to associate";
488 const char * associated_flag = "Associated with ";
489
490 pthread_mutex_lock(&s_global_check_mutex);
491 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
492 {
493 pthread_mutex_unlock(&s_global_check_mutex);
494 return;
495 }
496
497 if (state == LYNQ_WIFI_STATUS_EGNORE)
498 {
499 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
500 {
501 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
502 }
503 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
504 {
505 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
506 }
507 }
508 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
509 {
510 s_sta_error_number = error_num;
511 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
512 {
513 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
514 pthread_cond_signal(&s_global_check_cond);
515 }
516 }
517 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
518 {
519 s_sta_status = INNER_STA_STATUS_CONNECTED;
520 pthread_cond_signal(&s_global_check_cond);
521 }
522 pthread_mutex_unlock(&s_global_check_mutex);
523}
524
qs.xiongf0128b12023-06-29 17:29:39 +0800525static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
526{
527 char LYNQ_DISABLE_CMD[128]={0};
528
529 CHECK_IDX(idx, CTRL_STA);
530 CHECK_WPA_CTRL(CTRL_STA);
531
532 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
533 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
534 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
535
536 return 0;
537
538}
539
540
qs.xiong455c30b2023-04-12 11:40:02 +0800541void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
542{
543 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800544 char *wpanetid;
545 char destid[3] = {0};
546 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800547 *error = LYNQ_WAIT_CONNECT_ACTIVE;
548 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
549 {
550 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
551 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
552 return;
553 }
554
555 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
556 {
557 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
558 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
559 return;
560 }
561
562 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
563 {
qs.xiongf0128b12023-06-29 17:29:39 +0800564 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
565 wpanetid = strstr(modify,"id=");
566 if ( wpanetid != NULL )
567 {
568 wpanetid +=strlen("id=");
569 memcpy(destid,wpanetid,2);
570 tmpdisid = atoi(destid);
571
572 }
qs.xiong455c30b2023-04-12 11:40:02 +0800573 pReason = strstr(modify, "reason=");
574 if (pReason != NULL)
575 {
576 pReason += strlen("reason=");
577 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
578 {
579 *error = LYNQ_TIME_OUT;
580 }
581 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
582 {
583 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800584 // tmp fix sta autoconnect connect and disconnect
585 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
586 {
587 RLOGE("stop wlan0 network %d fail",tmpdisid);
588 }
qs.xiong455c30b2023-04-12 11:40:02 +0800589 }
590 else
591 {
592 *error = LYNQ_UNSPECIFIED_REASON;
593 }
qs.xiong455c30b2023-04-12 11:40:02 +0800594 }
595 else
596 {
597 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800598 }
qs.xiongf0128b12023-06-29 17:29:39 +0800599 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
600 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800601
602 }
603
604 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
605 {
606 *error = LYNQ_NOT_FIND_AP;
607 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
608 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
609 return;
610 }
611
612
613 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
614 {
615 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
616 pReason = strstr(modify, "status_code=");
617 if (pReason != NULL)
618 {
619 pReason += strlen("status_code=");
620 if (memcmp(pReason, "17", 2) == 0)
621 {
622 *error = LYNQ_AP_UNABLE_HANDLE;
623 }
624 else if (memcmp(pReason, "1",1) == 0)
625 {
626 *error = LYNQ_UNSPECIFIED_REASON;
627 }
628 else
629 {
630 *error = LYNQ_UNSPECIFIED_REASON;
631 }
632
633 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
634 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
635 return;
636 }
637 else
638 {
639 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
640 *error = LYNQ_UNSPECIFIED_REASON;
641 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
642 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
643 return;
644 }
645 }
646
647 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
648 {
649 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
650 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
651 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
652 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
653 return;
654 }
655
656 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
657 {
658 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
659 *error = LYNQ_WAIT_CONNECT_ACTIVE;
660 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
661 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
662 return;
663 }
664
you.chen32cb31e2023-04-13 14:05:45 +0800665 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800666 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800667 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800668 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
669 return;
670
671}
672
qs.xiongfcc914b2023-07-06 21:16:20 +0800673void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
674{
675 char *pReason;
676 char *wpanetid;
677 char destid[3];
678 *error = LYNQ_WAIT_CONNECT_ACTIVE;
679 *networkid = -1;
680 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
681 {
682 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
683 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
684 return;
685 }
686 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
687 {
688 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
689 wpanetid = strstr(modify,"id=");
690 if ( wpanetid != NULL )
691 {
692 wpanetid +=strlen("id=");
693 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
694 if (memcpy(destid,wpanetid,2) != NULL)
695 {
696 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
697 *networkid = atoi(destid);
698 RLOGD("get networkid is %d\n",*networkid);
699 }
700 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
701 }
702 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
703 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
704 return;
705 }
706 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
707 {
708 wpanetid = strstr(modify,"id=");
709 if ( wpanetid != NULL )
710 {
711 wpanetid +=strlen("id=");
712 if (memcpy(destid,wpanetid,2) != NULL)
713 {
714 *networkid = atoi(destid);
715 RLOGD("get networkid is %d\n",*networkid);
716 }
717 }
718 pReason = strstr(modify, "reason=");
719 if (pReason != NULL)
720 {
721 pReason += strlen("reason=");
722 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
723 {
724 *error = LYNQ_TIME_OUT;
725 }
726 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
727 {
728 *error = LYNQ_PSW_ERROR;
729 }
730 else
731 {
732 *error = LYNQ_UNSPECIFIED_REASON;
733 }
734 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
735 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
736 return;
737 }
738 else
739 {
740 *error = LYNQ_UNSPECIFIED_REASON;
741 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
742 return;
743 }
744 }
745 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
746 {
747 wpanetid = strstr(modify,"id=");
748 if ( wpanetid != NULL )
749 {
750 wpanetid +=strlen("id=");
751 if (memcpy(destid,wpanetid,2) != NULL)
752 {
753 *networkid = atoi(destid);
754 RLOGD("get networkid is %d\n",*networkid);
755 }
756 }
757 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
758 pReason = strstr(modify, "status_code=");
759 if (pReason != NULL)
760 {
761 pReason += strlen("status_code=");
762 if (memcmp(pReason, "17", 2) == 0)
763 {
764 *error = LYNQ_AP_UNABLE_HANDLE;
765 }
766 else if (memcmp(pReason, "1",1) == 0)
767 {
768 *error = LYNQ_UNSPECIFIED_REASON;
769 }
770 else
771 {
772 *error = LYNQ_UNSPECIFIED_REASON;
773 }
774 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
775 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
776 return;
777 }
778 else
779 {
780 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
781 *error = LYNQ_UNSPECIFIED_REASON;
782 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
783 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d£¬networkid:%d\n",*state,*error,*networkid);
784 return;
785 }
786 }
787 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
788 {
789 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
790 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
791 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
792 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
793 return;
794 }
795 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
796 {
797 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
798 *error = LYNQ_WAIT_CONNECT_ACTIVE;
799 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
800 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
801 return;
802 }
803 RLOGD("EVENT : %s\n", modify);
804 *error = LYNQ_UNSPECIFIED_REASON;
805 *state = LYNQ_WIFI_STATUS_EGNORE;
806 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
807 return;
808}
you.chen70f377f2023-04-14 18:17:09 +0800809static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
810{
you.chen6d247052023-06-01 16:39:54 +0800811 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800812 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
813 {
814 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
815 g_sta_callback_func(g_sta_callback_priv, state, error);
816 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
817 }
you.chen6d247052023-06-01 16:39:54 +0800818 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800819}
qs.xiongfcc914b2023-07-06 21:16:20 +0800820static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
821{
822 pthread_mutex_lock(&s_sta_callback_mutex);
823 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
824 {
825 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
826 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
827 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
828 }
829 pthread_mutex_unlock(&s_sta_callback_mutex);
830}
you.chen70f377f2023-04-14 18:17:09 +0800831
you.chen35020192022-05-06 11:30:57 +0800832static void STAWatcherThreadProc() {
833 size_t len = MAX_RET;
834 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +0800835 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +0800836 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +0800837 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400838
you.chen6c2dd9c2022-05-16 17:55:28 +0800839 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800840 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800841
you.chen70f377f2023-04-14 18:17:09 +0800842 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +0800843 while (g_sta_watcher_stop_flag == 0)
844 {
you.chenf711c8a2023-04-13 13:49:45 +0800845 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +0800846 {
you.chen35020192022-05-06 11:30:57 +0800847 continue;
848 }
you.chenf711c8a2023-04-13 13:49:45 +0800849
you.chen6c2dd9c2022-05-16 17:55:28 +0800850 memset(msg_notify, 0, MAX_RET);
851 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800852 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +0800853 {
you.chen35020192022-05-06 11:30:57 +0800854 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +0800855 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
856 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +0800857 {
you.chen35020192022-05-06 11:30:57 +0800858 g_sta_scan_finish_flag = 1;
859 }
860
qs.xiong9fbf74e2023-03-28 13:38:22 +0800861 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +0800862 {
you.chen35020192022-05-06 11:30:57 +0800863 continue;
864 }
qs.xiong455c30b2023-04-12 11:40:02 +0800865 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +0800866 notify_connect_status(state, error);
867
868 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +0800869 {
you.chen70f377f2023-04-14 18:17:09 +0800870 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +0800871 if (last_state != state)
872 {
873 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
874 {
875 system_call_v("%s %s", sta_status_change_script, "connect");
876 }
877 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
878 {
879 system_call_v("%s %s", sta_status_change_script, "disconnect");
880 }
881 }
882
883 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +0800884 }
you.chen35020192022-05-06 11:30:57 +0800885 }
886 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800887 if (lynq_wpa_ctrl != NULL)
888 {
you.chen92fd5d32022-05-25 10:09:47 +0800889 wpa_ctrl_detach(lynq_wpa_ctrl);
890 wpa_ctrl_close(lynq_wpa_ctrl);
891 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400892}
qs.xiongfcc914b2023-07-06 21:16:20 +0800893static void STAAutoWatcherThreadProc() {
894 size_t len = MAX_RET;
895 char msg_notify[MAX_RET];
896 error_number_s error;
897 lynq_wifi_sta_status_s state, last_state = -1;
898 int idle_count = 0;
899 int networkid;
900 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
901 g_sta_auto_watcher_stop_flag = 0;
902 RLOGD("STAAutoWatcherThreadProc thread started ------");
903 while (g_sta_auto_watcher_stop_flag == 0)
904 {
905 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
906 {
907 continue;
908 }
909 memset(msg_notify, 0, MAX_RET);
910 len = MAX_RET;
911 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
912 {
913 msg_notify[len+1] = '\0';
914 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
915 if (strstr(msg_notify, state_scan_result) != NULL)
916 {
917 g_sta_auto_scan_finish_flag = 1;
918 }
919 if (g_sta_auto_callback_func == NULL)
920 {
921 continue;
922 }
923 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
924 notify_auto_connect_status(state, error,networkid);
925 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
926 {
927 inner_check_connect_error(msg_notify, state, error);
928 if (last_state != state)
929 {
930 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
931 {
932 system_call_v("%s %s", sta_status_change_script, "connect");
933 }
934 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
935 {
936 system_call_v("%s %s", sta_status_change_script, "disconnect");
937 }
938 }
939 last_state = state;
940 }
941 }
942 }
943 if (lynq_wpa_ctrl != NULL)
944 {
945 wpa_ctrl_detach(lynq_wpa_ctrl);
946 wpa_ctrl_close(lynq_wpa_ctrl);
947 }
948}
qs.xiongf1b525b2022-03-31 00:58:23 -0400949
you.chen70f377f2023-04-14 18:17:09 +0800950// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
951static void GlobalWatcherThreadProc()
952{
953 int ret, connect_timeout, service_abnormal;
954 error_number_s error_num = -1;
955 inner_sta_status_s sta_status;
956 scan_info_s *scan_list = NULL;
957 int i, scan_len=0;
958 char connecting_ssid[64];
959 struct timeval now;
960
961 RLOGD("GlobalWatcherThreadProc start to run");
962
963 while (1)
964 {
965 pthread_mutex_lock(&s_global_check_mutex);
966 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
967 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
968 {
969 pthread_mutex_unlock(&s_global_check_mutex);
970 usleep(50*1000);
971 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
972 continue;
973 }
974
975 connect_timeout = 0;
976 service_abnormal = 0;
977 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
978 {
979 while (1)
980 {
981 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
982 if (ret == ETIME)
983 {
984 connect_timeout = 1;
985 }
986 else if (ret != 0)
987 {
988 gettimeofday(&now,NULL);
989 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
990 {
991 usleep(SLEEP_TIME_ON_IDLE);
992 continue;
993 }
994 connect_timeout = 1;
995 }
996 sta_status = s_sta_status;
997 error_num = s_sta_error_number;
998 s_sta_status = INNER_STA_STATUS_INIT;
999 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1000 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1001 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1002 break;
1003 }
1004 }
1005 if (s_service_invoke_timeout_cnt > 10)
1006 {
1007 service_abnormal = 1;
1008 s_service_invoke_timeout_cnt = 0;
1009 }
1010 pthread_mutex_unlock(&s_global_check_mutex);
1011
1012 if (service_abnormal == 1)
1013 {
1014 sleep(1);
1015 RLOGE("wpa service is abnormal info app to exit");
1016 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001017
1018 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1019
you.chen70f377f2023-04-14 18:17:09 +08001020 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1021 }
1022
1023 if (sta_status == INNER_STA_STATUS_CANCEL)
1024 {
1025 continue;
1026 }
1027 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1028 {
1029 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1030 }
1031 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1032 {
1033 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1034 {
1035 for(i=0; i < scan_len;i++)
1036 {
1037 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1038 {
1039 error_num = LYNQ_AUTH_ERROR;
1040 break;
1041 }
1042 }
1043 free(scan_list);
1044 }
1045 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1046 }
1047 else if (connect_timeout == 0)
1048 {
1049 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1050 }
1051 else // wait timeout
1052 {
1053 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1054 {
1055 ; // wpa service abnormal
1056 }
1057 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1058 {
1059 RLOGD("GlobalWatcherThreadProc notify connected");
1060 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1061 }
1062 else
1063 {
1064 RLOGD("GlobalWatcherThreadProc notify timeout");
1065 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1066 }
1067 }
1068 } // while (1)
1069}
1070
qs.xiong1af5daf2022-03-14 09:12:12 -04001071int lynq_wifi_enable(void)
1072{
you.chen35020192022-05-06 11:30:57 +08001073 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001074 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001075 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001076 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1077
qs.xiong9fbf74e2023-03-28 13:38:22 +08001078 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1079 {
you.chend2fef3f2023-02-13 10:50:35 +08001080 goto out_enable;
1081 }
1082
you.chenc9928582023-04-24 15:39:37 +08001083 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001084 if (ret != 0)
1085 {
1086 //printf("service state %d\n", ret);
1087 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001088 ret = -1;
1089 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001090 }
lhfe8da902022-10-11 18:55:36 +08001091
you.chen70f377f2023-04-14 18:17:09 +08001092 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1093 {
1094 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1095 if(ret<0)
1096 {
1097 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1098 ret = -1;
1099 goto out_enable;
1100 }
1101 }
1102
you.chend2fef3f2023-02-13 10:50:35 +08001103 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1104 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1105 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1106 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1107out_enable:
1108 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001109 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001110}
1111
qs.xiong1af5daf2022-03-14 09:12:12 -04001112int lynq_wifi_disable(void)
1113{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001114 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001115 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001116 g_ap_watcher_stop_flag = 1;
1117 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001118 g_sta_auto_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001119 if (g_ap_watcher_pid != 0)
1120 pthread_join(g_ap_watcher_pid, NULL);
1121 if (g_sta_watcher_pid != 0)
1122 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001123 if (g_sta_auto_watcher_pid != 0)
1124 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001125 if (g_lynq_wpa_ctrl[0] != NULL)
1126 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1127 if (g_lynq_wpa_ctrl[1] != NULL)
1128 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
1129 g_ap_watcher_pid = 0;
1130 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001131 g_sta_auto_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001132 g_lynq_wpa_ctrl[0] = NULL;
1133 g_lynq_wpa_ctrl[1] = NULL;
1134 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001135 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1136 return 0;
1137}
1138
1139static inline char inner_convert_char(char in)
1140{
1141 if (in >= '0' && in <= '9')
1142 {
1143 return in - '0';
1144 }
1145 else if (in >= 'a' && in <= 'f')
1146 {
1147 return in - 'a' + 10;
1148 }
1149 else if (in >= 'A' && in <= 'F')
1150 {
1151 return in - 'A' + 10;
1152 }
1153 else
1154 {
1155 return '\xff';
1156 }
1157}
1158
1159static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1160{
1161 char *p;
1162 size_t pos = 0;
1163 if (NULL == out_ssid)
1164 return;
1165 //printf("input ssid=[%s]\n", ssid);
1166 memset(out_ssid, 0, out_ssid_len);
1167 if (NULL == ssid)
1168 return;
1169 p = strchr(ssid, '\\');
1170 if (NULL == p)
1171 {
1172 strncpy(out_ssid, ssid, out_ssid_len);
1173 //printf(" first %s\n", out_ssid);
1174 }
1175 else
1176 {
1177 pos = p - ssid;
1178 memcpy(out_ssid, ssid, pos);
1179 //printf("pos %lu -- %s\n", pos, out_ssid);
1180 for(; pos < out_ssid_len; pos ++)
1181 {
1182 if (p[0] == '\0')
1183 {
1184 //printf(" out %s\n", out_ssid);
1185 return;
1186 }
1187 else if (p[0] != '\\')
1188 {
1189 out_ssid[pos] = p[0];
1190 p += 1;
1191 }
1192 else if (p[1] == 'x' || p[1] == 'X')
1193 {
1194 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1195 p += 4;
1196 }
1197 else if (p[1] == '\\')
1198 {
1199 out_ssid[pos] = '\\';
1200 p += 2;
1201 }
1202 else if (p[1] == 't')
1203 {
1204 out_ssid[pos] = '\t';
1205 p += 2;
1206 }
1207 else if (p[1] == 'r')
1208 {
1209 out_ssid[pos] = '\r';
1210 p += 2;
1211 }
1212 else if (p[1] == 'n')
1213 {
1214 out_ssid[pos] = '\n';
1215 p += 2;
1216 }//todo find a better way to convert?
1217 }
1218 }
1219 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001220}
qs.xiong1af5daf2022-03-14 09:12:12 -04001221
you.chen35020192022-05-06 11:30:57 +08001222static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001223 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001224 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001225 RLOGD("enter inner_get_param");
1226 if (out_put == NULL)
1227 {
1228 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001229 return -1;
1230 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001231 if (param_name == NULL)
1232 {
1233 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001234 return -1;
1235 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001236 if (param_name[0] == '\0')
1237 {
1238 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001239 return -1;
1240 }
1241
1242 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1243
1244 CHECK_WPA_CTRL(interface);
1245
1246 DO_REQUEST(lynq_cmd_get);
1247
qs.xiong9fbf74e2023-03-28 13:38:22 +08001248 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1249 {
1250 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001251 return -1;
1252 }
1253
you.chena6fa5b22022-05-18 10:28:19 +08001254// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001255 if (strcmp(param_name, "ssid") == 0)
1256 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001257 if (cmd_reply[0] == '\"')
1258 {
you.chend2fef3f2023-02-13 10:50:35 +08001259 ssid_len = reply_len - 1;
1260 memcpy(out_put, cmd_reply + 1, ssid_len);
1261 if (out_put[ssid_len-1] == '\"')
1262 {
1263 out_put[ssid_len-1] = '\0';
1264 }
1265 else
1266 {
1267 out_put[ssid_len] = '\0';
1268 }
1269 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001270 else
1271 {
you.chend2fef3f2023-02-13 10:50:35 +08001272 ssid_len = reply_len / 2;
1273 for(i=0; i<ssid_len; i++)
1274 {
1275 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1276 }
1277 out_put[ssid_len] = '\0';
1278 }
1279 }
1280 else
1281 {
1282 memcpy(out_put, cmd_reply, reply_len + 1);
1283 }
you.chen35020192022-05-06 11:30:57 +08001284 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001285}
qs.xiong1af5daf2022-03-14 09:12:12 -04001286
you.chen35020192022-05-06 11:30:57 +08001287static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1288 int ret = 0;
1289 char * end = str + len - 1;
1290 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001291 while(str < end)
1292 {
1293 if (*str == delimiter)
1294 {
you.chen35020192022-05-06 11:30:57 +08001295 *str++ = '\0';
1296 results[ret++] = str;
1297 continue;
1298 }
1299 str++;
1300 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001301 if (*str == delimiter)
1302 {
you.chen35020192022-05-06 11:30:57 +08001303 *str = '\0';
1304 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001305
you.chen6ed36a62023-04-27 17:51:56 +08001306 results[ret] = NULL;
1307
you.chen35020192022-05-06 11:30:57 +08001308 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001309}
1310
you.chend2fef3f2023-02-13 10:50:35 +08001311static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1312{
1313 char * p;
1314 int ret = 0;
1315 char cmd[256]={0};
1316 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001317 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001318 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001319 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001320 ret = exec_cmd(cmd, ip, ip_len);
1321 p = strchr(ip, '\n');
1322 if (NULL != p)
1323 {
1324 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001325 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001326 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001327 return ret;
1328}
1329
you.chend2fef3f2023-02-13 10:50:35 +08001330static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001331 struct in_addr addr ={0};
1332 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001333 char cmd[64] = {0};
1334 char * p;
1335 int ret;
you.chen35020192022-05-06 11:30:57 +08001336
qs.xiong9fbf74e2023-03-28 13:38:22 +08001337 if (ip == NULL || *ip == '\0' || hostname == NULL)
1338 {
1339 RLOGE("ip == NULL or hostname == NULL");
1340 return -1;
you.chen35020192022-05-06 11:30:57 +08001341 }
1342
you.chend2fef3f2023-02-13 10:50:35 +08001343 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001344 if (inet_aton(ip, &addr) == 0)
1345 {
you.chen35020192022-05-06 11:30:57 +08001346 printf("---inet_aton fail\n");
1347 return -1;
1348 }
1349
1350 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1351
qs.xiong9fbf74e2023-03-28 13:38:22 +08001352 if (ht == NULL)
1353 {
you.chen186d3c32023-05-18 14:19:46 +08001354 hostname[0] = '\0';
1355 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1356 ret = exec_cmd(cmd, hostname, 32);
1357 if (ret == 0)
1358 {
1359 p = strchr(hostname, '\n');
1360 if (p != NULL)
1361 {
1362 *p = '\0';
1363 }
1364 return 0;
1365 }
1366 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001367 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001368 herror(NULL);
1369 return -1;
1370 }
1371
1372 strcpy(hostname, ht->h_name);
1373
1374 return 0;
1375}
1376
1377static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1378{
1379 int count, index, words_count;
1380 char * split_lines[128]= {0};
1381 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001382 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001383 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001384 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001385
1386 CHECK_WPA_CTRL(ap_sta);
1387
1388 DO_REQUEST(lynq_wifi_list_networks);
1389
1390 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1391
1392 //@todo check ssid field to compatible
1393
1394 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001395 for(index=1; index < count; index++)
1396 {
you.chen35020192022-05-06 11:30:57 +08001397 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001398 if (words_count > 2)
1399 {
you.chend2fef3f2023-02-13 10:50:35 +08001400 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001401 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1402 {
you.chen35020192022-05-06 11:30:57 +08001403 net_no_list[ret++] = atoi(split_words[0]);
1404 }
1405 }
1406 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001407 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001408 return ret;
1409}
1410
1411static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001412 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001413 CHECK_WPA_CTRL(ap_sta);
1414 const char *lynq_wifi_add_network = "ADD_NETWORK";
1415
qs.xiong9fbf74e2023-03-28 13:38:22 +08001416 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001417 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001418 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1419 {
1420 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001421 return -1;
1422 }
1423
qs.xiong9fbf74e2023-03-28 13:38:22 +08001424 for(i=0;i<reply_len;i++)
1425 {
1426 if(cmd_reply[i] == '\n')
1427 {
you.chen35020192022-05-06 11:30:57 +08001428 cmd_reply[i] = '\0';
1429 break;
1430 }
1431 }
1432 return atoi(cmd_reply);
1433}
you.chena6cd55a2022-05-08 12:20:18 +08001434
you.chen35020192022-05-06 11:30:57 +08001435static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1436{
1437 int count, index;
1438 int net_no_list[128];
1439
qs.xiong9fbf74e2023-03-28 13:38:22 +08001440 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001441 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001442 for (index=0; index < count; index++)
1443 {
1444 if (net_no_list[index] == net_no)
1445 {
you.chen35020192022-05-06 11:30:57 +08001446 return 0;
1447 }
1448 }
1449
1450 if (count >= 1)
1451 index = net_no_list[count - 1];
1452 else
1453 index = -1;
1454
qs.xiong9fbf74e2023-03-28 13:38:22 +08001455 while (index < net_no )
1456 {
you.chen35020192022-05-06 11:30:57 +08001457 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001458 if (index >= net_no)
1459 { // required network no created
1460 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001461 return 0;
1462 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001463 else if( index < 0)
1464 {
1465 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001466 return -1;
1467 }
you.chen35020192022-05-06 11:30:57 +08001468 }
1469
1470 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001471 {
1472 RLOGE("[lynq_check_network_number] network index < 0");
1473 return -1;
1474 }
1475 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001476 return 0;
1477}
1478
1479static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001480 if (freq > 5000 && freq < 6000)
1481 {
you.chen35020192022-05-06 11:30:57 +08001482 return LYNQ_WIFI_5G_band;
1483 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001484 else if (freq > 2000 && freq < 3000)
1485 {
you.chen35020192022-05-06 11:30:57 +08001486 return LYNQ_WIFI_2G_band;
1487 }
1488 return LYNQ_WIFI_2_and_5G_band;
1489}
1490
1491static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001492 if (key_mgmt != NULL)
1493 {
1494 if (memcmp( key_mgmt, "NONE", 4) == 0)
1495 {
you.chen35020192022-05-06 11:30:57 +08001496 return LYNQ_WIFI_AUTH_OPEN;
1497 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001498 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1499 {
you.chen35020192022-05-06 11:30:57 +08001500 return LYNQ_WIFI_AUTH_WEP;
1501 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001502 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1503 {
you.chen35020192022-05-06 11:30:57 +08001504 return LYNQ_WIFI_AUTH_WPA_PSK;
1505 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001506 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1507 {
you.chen35020192022-05-06 11:30:57 +08001508 return LYNQ_WIFI_AUTH_WPA2_PSK;
1509 }
1510 }
1511
1512 return -1;
1513}
1514
1515static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001516 if (flag != NULL)
1517 {
qs.xiong3e506812023-04-06 11:08:48 +08001518 if ( strstr(flag, "SHA256") != NULL || strstr(flag,"WPA2-SAE") != NULL || strstr(flag,"SAE-H2E") != NULL)
1519 {
1520 return LYNQ_WIFI_AUTH_WPA3_PSK;
1521 }else if ( strstr( flag,"SAE-CCMP") != NULL )
1522 {
1523 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1524 }else if (strstr( flag, "WPA2-PSK") != NULL)
1525 {
you.chen35020192022-05-06 11:30:57 +08001526 return LYNQ_WIFI_AUTH_WPA2_PSK;
1527 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001528 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001529 {
you.chen35020192022-05-06 11:30:57 +08001530 return LYNQ_WIFI_AUTH_WPA_PSK;
1531 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001532 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001533 {
you.chen35020192022-05-06 11:30:57 +08001534 return LYNQ_WIFI_AUTH_WEP;
1535 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001536 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001537 {
you.chen35020192022-05-06 11:30:57 +08001538 return LYNQ_WIFI_AUTH_OPEN;
1539 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001540 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001541 {
you.chend2fef3f2023-02-13 10:50:35 +08001542 return LYNQ_WIFI_AUTH_OPEN;
1543 }
you.chen35020192022-05-06 11:30:57 +08001544 }
1545
1546 return -1;
1547}
1548
1549static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1550 switch (bw) {
1551 case 10:
1552 return LYNQ_WIFI_BANDWIDTH_HT10;
1553 break;
1554 case 20:
1555 return LYNQ_WIFI_BANDWIDTH_HT20;
1556 break;
1557 case 40:
1558 return LYNQ_WIFI_BANDWIDTH_HT40;
1559 break;
1560 case 80:
1561 return LYNQ_WIFI_BANDWIDTH_HT80;
1562 break;
1563 default:
1564 break;
1565 }
1566
1567 return -1;
1568}
1569
you.chen70f377f2023-04-14 18:17:09 +08001570static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001571static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1572 int i, count;
1573 char *p;
1574 const char *lynq_status_cmd = "STATUS";
1575 const char * FLAG_SSID = "ssid=";
1576 const char * FLAG_SBSID = "bssid=";
1577 const char * FLAG_KEY_MGMT = "key_mgmt=";
1578 const char * FLAG_FREQ = "freq=";
1579 const char * FLAG_STATE = "wpa_state=";
1580 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001581 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001582 char *split_lines[128] = {0};
1583
1584 CHECK_WPA_CTRL(interface);
1585
qs.xiong9fbf74e2023-03-28 13:38:22 +08001586 if (curr_state == NULL)
1587 {
1588 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001589 return -1;
1590 }
1591
1592 DO_REQUEST(lynq_status_cmd);
1593
1594 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1595
1596 curr_state->net_no = -1;
1597 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001598 for(i=0; i < count; i++)
1599 {
1600 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001601 {
you.chen35020192022-05-06 11:30:57 +08001602 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001603 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001604 {
you.chend2fef3f2023-02-13 10:50:35 +08001605 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001606 ret = 0;
1607 continue;
1608 }
you.chenf58b3c92022-06-21 16:53:48 +08001609 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001610 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001611 {
you.chend2fef3f2023-02-13 10:50:35 +08001612 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 +08001613 ret = 0;
1614 continue;
1615 }
you.chen35020192022-05-06 11:30:57 +08001616 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001617 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001618 {
you.chen450d0172022-07-15 17:56:48 +08001619 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001620 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001621 ret = 0;
1622 continue;
1623 }
1624 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001625 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001626 {
you.chen35020192022-05-06 11:30:57 +08001627 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1628 ret = 0;
1629 continue;
1630 }
you.chend2fef3f2023-02-13 10:50:35 +08001631 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001632 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001633 {
you.chend2fef3f2023-02-13 10:50:35 +08001634 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1635 ret = 0;
1636 continue;
1637 }
you.chen35020192022-05-06 11:30:57 +08001638 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001639 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001640 {
you.chen35020192022-05-06 11:30:57 +08001641 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001642 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001643 {
you.chen35020192022-05-06 11:30:57 +08001644 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1645 ret = 0;
1646 continue;
1647 }
1648
1649 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001650 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001651 {
you.chen35020192022-05-06 11:30:57 +08001652 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001653 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001654 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001655 }
1656 }
1657
you.chen70f377f2023-04-14 18:17:09 +08001658 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1659 {
1660 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1661 }
1662
you.chen35020192022-05-06 11:30:57 +08001663 return ret;
1664}
1665
qs.xiongf1b525b2022-03-31 00:58:23 -04001666int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001667{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001668 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001669 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001670
qs.xiong9fbf74e2023-03-28 13:38:22 +08001671 if (ap_ssid == NULL)
1672 {
1673 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001674 return -1;
1675 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001676 else
1677 {
1678 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001679 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001680
qs.xiong9fbf74e2023-03-28 13:38:22 +08001681 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1682 {
1683 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001684 return -1;
1685 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001686
you.chen35020192022-05-06 11:30:57 +08001687 CHECK_IDX(idx, CTRL_AP);
1688
1689 CHECK_WPA_CTRL(CTRL_AP);
1690
1691 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1692
1693 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1694 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001695 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1696 return 0;
you.chen35020192022-05-06 11:30:57 +08001697
qs.xiong7a105ce2022-03-02 09:43:11 -05001698}
1699
you.chen35020192022-05-06 11:30:57 +08001700int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001701{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001702 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001703 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001704 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001705}
1706
qs.xiongc9c79f72022-10-17 15:27:18 +08001707/*****
1708 *frequency <------>channel
1709 *
1710 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1711 *
1712 *
1713 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1714 *
1715 *
1716 * */
1717static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001718 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};
1719 int i;
1720 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1721
qs.xiong69a332b2022-12-02 09:58:57 +08001722 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001723 {
1724 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001725 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001726 }
qs.xiongc00b6032022-11-29 16:28:03 +08001727
1728 if(i == arr_len)
1729 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001730 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001731 return -1;
1732 }
qs.xiongc00b6032022-11-29 16:28:03 +08001733
qs.xiongc9c79f72022-10-17 15:27:18 +08001734 return 0;
1735}
qs.xiong13673462023-02-21 19:12:54 +08001736
1737static int lynq_check_frequencyby_country_code(int input_frequency)
1738{
1739 char str_cnc[]="CN";
1740 char str_dest[20]="";
1741
1742 if( lynq_get_country_code(1,str_dest) != 0 )
1743 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001744 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001745 return -1;
1746 }
1747 if( strncmp(str_dest,str_cnc,2) != 0 )
1748 {
1749 return 0;
1750 }else if( 2473 < input_frequency && input_frequency < 5744)
1751 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001752 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08001753 return -1;
1754 }
1755 return 0;
1756}
qs.xiongf1b525b2022-03-31 00:58:23 -04001757int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001758{
qs.xiongc00b6032022-11-29 16:28:03 +08001759 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08001760 char lynq_wifi_frequency_cmd[128]={0};
1761 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08001762 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001763 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001764 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08001765 check = lynq_check_set_frequency(lynq_wifi_frequency);
1766 if(check != 0)
1767 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001768 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08001769 return -1;
you.chen35020192022-05-06 11:30:57 +08001770 }
qs.xiong13673462023-02-21 19:12:54 +08001771 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
1772 if(check != 0)
1773 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001774 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08001775 return -1;
1776 }
1777
qs.xiongc00b6032022-11-29 16:28:03 +08001778 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1779 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001780 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08001781 return -1;
1782 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001783
you.chen35020192022-05-06 11:30:57 +08001784 CHECK_IDX(idx, CTRL_AP);
1785
1786 CHECK_WPA_CTRL(CTRL_AP);
1787
1788 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
1789 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
1790 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
1791
you.chen6c2dd9c2022-05-16 17:55:28 +08001792 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08001793 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
1794 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
1795 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001796
qs.xiong9fbf74e2023-03-28 13:38:22 +08001797 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001798}
1799
qs.xiongf1b525b2022-03-31 00:58:23 -04001800int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001801{
you.chen35020192022-05-06 11:30:57 +08001802 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001803 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08001804 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04001805
qs.xiong9fbf74e2023-03-28 13:38:22 +08001806 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
1807 {
1808 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08001809 return -1;
1810 }
1811 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04001812
qs.xiong9fbf74e2023-03-28 13:38:22 +08001813 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001814}
1815
qs.xiongf1b525b2022-03-31 00:58:23 -04001816int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
1817{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001818 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08001819 CHECK_IDX(idx, CTRL_AP);
1820 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001821 case LYNQ_WIFI_BANDWIDTH_HT10:
1822 {
1823 RLOGE("bandwith [%d] not support now\n", bandwidth);
1824 return -1;
1825 }
1826 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08001827 {
1828 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
1829 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001830 if (system(lynq_cmd_bandwith) != 0 )
1831 {
1832 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001833 return -1;
1834 }
1835 system("wl up");
1836 break;
1837 }
1838 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001839 {
qs.xiong10379192023-02-21 13:19:42 +08001840 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08001841 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1842 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001843 if (system(lynq_cmd_bandwith) != 0 )
1844 {
1845 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001846 return -1;
1847 }
1848 system("wl up");
1849 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001850 }
you.chen35020192022-05-06 11:30:57 +08001851 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001852 {
qs.xiong10379192023-02-21 13:19:42 +08001853 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08001854 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001855 if (system(lynq_cmd_bandwith) != 0 )
1856 {
1857 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001858 return -1;
1859 }
1860 system("wl up");
1861 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001862 }
1863 default:
you.chen35020192022-05-06 11:30:57 +08001864 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001865 RLOGE("auth type [%d] not support now\n", bandwidth);
1866 return -1;
you.chen35020192022-05-06 11:30:57 +08001867 }
1868 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001869
1870
you.chen35020192022-05-06 11:30:57 +08001871 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001872}
you.chen35020192022-05-06 11:30:57 +08001873
qs.xiongf1b525b2022-03-31 00:58:23 -04001874int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1875{
you.chen35020192022-05-06 11:30:57 +08001876 int count = 0;
1877 int index = 0;
1878 char *split_words[128] = {0};
1879 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001880 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001881 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001882
you.chen35020192022-05-06 11:30:57 +08001883 CHECK_WPA_CTRL(CTRL_AP);
1884
1885 DO_REQUEST(lynq_chanspec_cmd);
1886
1887 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1888 for(;index < count; index++) {
1889 if (strncmp(split_words[index], "bw", 2) != 0) {
1890 continue;
1891 }
1892
1893 index++;
1894 if (index >= count) {
1895 return -1;
1896 }
1897
qs.xiong9fbf74e2023-03-28 13:38:22 +08001898 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001899 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1900 return 0;
1901 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001902 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001903 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001904}
qs.xiong0fb469a2022-04-14 03:50:45 -04001905
qs.xiongf1b525b2022-03-31 00:58:23 -04001906int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001907{
you.chen35020192022-05-06 11:30:57 +08001908 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001909 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08001910 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04001911
you.chen35020192022-05-06 11:30:57 +08001912 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04001913
qs.xiong9fbf74e2023-03-28 13:38:22 +08001914 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1915 {
you.chen35020192022-05-06 11:30:57 +08001916 return -1;
1917 }
1918
1919 system("wl down");
1920 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001921 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08001922 return -1;
1923 }
1924 system("wl up");
1925 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001926}
qs.xiong0fb469a2022-04-14 03:50:45 -04001927
qs.xiongf1b525b2022-03-31 00:58:23 -04001928int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001929{
you.chen35020192022-05-06 11:30:57 +08001930 int count = 0;
1931 int index = 0;
1932 char *split_words[128] = {0};
1933 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001934 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08001935 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001936
you.chen35020192022-05-06 11:30:57 +08001937 CHECK_WPA_CTRL(CTRL_AP);
1938
1939 DO_REQUEST(lynq_chanspec_cmd);
1940
1941 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001942 for(;index < count; index++)
1943 {
1944 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001945 if (strncmp(split_words[index], "channel", 2) != 0) {
1946 continue;
1947 }
1948
1949 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001950 if (index >= count)
1951 {
you.chen35020192022-05-06 11:30:57 +08001952 return -1;
1953 }
1954
1955 *channel = atoi(split_words[index]);
1956 return 0;
1957 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001958 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08001959 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001960}
1961
1962
you.chen35020192022-05-06 11:30:57 +08001963int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001964{
you.chen6c2dd9c2022-05-16 17:55:28 +08001965 char ssid[MAX_CMD] = {0};
1966 int freq = 0;
1967 char lynq_auth_cmd[64]={0};
1968 char lynq_auth_alg_cmd[64]={0};
1969 char lynq_psk_cmd[64]={0};
1970 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001971 char lynq_ieee80211_cmd[64]={0};
1972 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08001973 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001974 CHECK_IDX(idx, CTRL_AP);
1975
you.chen6c2dd9c2022-05-16 17:55:28 +08001976 CHECK_WPA_CTRL(CTRL_AP);
1977
qs.xiong9fbf74e2023-03-28 13:38:22 +08001978 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
1979 {
1980 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08001981 return -1;
1982 }
1983
you.chen92fd5d32022-05-25 10:09:47 +08001984 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001985 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08001986 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08001987 return 0;
1988 }
1989 else {
1990 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1991 ssid[0] = '\0';
1992 }
1993 lynq_wifi_ap_frequency_get(idx, &freq);
1994
1995 DO_OK_FAIL_REQUEST(cmd_disconnect);
1996 DO_OK_FAIL_REQUEST(cmd_remove_all);
1997 if (ssid[0] != '\0') {
1998 lynq_wifi_ap_ssid_set(idx, ssid);
1999 }
2000 if (freq != 0) {
2001 lynq_wifi_ap_frequency_set(idx, freq);
2002 }
2003 }
2004 }
you.chen35020192022-05-06 11:30:57 +08002005
qs.xiong9fbf74e2023-03-28 13:38:22 +08002006 switch(auth){
2007 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002008 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002009 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002010 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002011 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002012 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002013 break;
2014 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002015 case LYNQ_WIFI_AUTH_WEP:
2016 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002017 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002018 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002019 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002020 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2021
2022 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2023 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2024 break;
2025 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002026 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002027 {
2028 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2029 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2030 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2031
2032 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2033 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2034 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2035 break;
2036
2037 }
you.chen35020192022-05-06 11:30:57 +08002038 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002039 {
2040 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2041 {
you.chen35020192022-05-06 11:30:57 +08002042 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2043 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2044 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002045 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2046 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002047 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002048 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002049 }
2050// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2051// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2052 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002053
you.chen35020192022-05-06 11:30:57 +08002054 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2055 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2056 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002057 break;
2058 }
2059 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2060 {
2061 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2062 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2063 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2064 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2065
2066 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2067 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2068 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2069 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2070 break;
2071 }
2072 case LYNQ_WIFI_AUTH_WPA3_PSK:
2073 {
2074 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2075 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002076 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002077 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2078
2079 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2080 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2081 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2082 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2083 break;
2084 }
2085 default:
you.chen35020192022-05-06 11:30:57 +08002086 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002087 RLOGE("auth type [%d] not support now\n", auth);
2088 return -1;
you.chen35020192022-05-06 11:30:57 +08002089 }
2090 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002091 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002092
qs.xiong9fbf74e2023-03-28 13:38:22 +08002093 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002094}
2095
you.chen35020192022-05-06 11:30:57 +08002096int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002097{
you.chen35020192022-05-06 11:30:57 +08002098 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002099 char lynq_auth_alg_str[MAX_RET] = {0};
2100 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002101 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002102 CHECK_IDX(idx, CTRL_AP);
2103
qs.xiong9fbf74e2023-03-28 13:38:22 +08002104 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2105 {
2106 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002107 return -1;
2108 }
2109
qs.xiong9fbf74e2023-03-28 13:38:22 +08002110 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2111 {
2112 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2113 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002114 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002115 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002116 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002117 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002118 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2119 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002120 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002121 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002122 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002123 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002124 else
2125 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002126 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002127 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002128 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002129 }
you.chen35020192022-05-06 11:30:57 +08002130 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002131 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2132 {
2133 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2134 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002135 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002136 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002137 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002138 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2139 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002140 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002141 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002142 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002143 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002144 else
2145 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002146 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002147 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002148 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002149 }
you.chen35020192022-05-06 11:30:57 +08002150 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002151
2152 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2153 {
2154 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2155 return -1;
2156 }
2157
2158 if (memcmp(lynq_auth_str,"1",1) == 0 )
2159 {
2160 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2161 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002162 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002163 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2164 {
2165 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2166 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002167 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002168 }
2169 else
2170 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002171 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002172 *auth = -1;
2173 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002174
you.chen6c2dd9c2022-05-16 17:55:28 +08002175 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002176}
qs.xiong1af5daf2022-03-14 09:12:12 -04002177
you.chenb95401e2023-05-12 19:39:06 +08002178static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2179{
2180 char status[64];
you.chencba13492023-05-19 13:53:43 +08002181 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002182 curr_status_info curr_state;
2183
2184 CHECK_WPA_CTRL(CTRL_AP);
2185
2186 memset(status, 0, sizeof (status));
2187
2188 curr_state.ap = NULL;
2189 curr_state.state = status;
2190
2191 printf("inner_check_ap_connected %d\n", retry_count);
2192 usleep(500*1000);
2193 if (0 == inner_get_status_info(idx, &curr_state))
2194 {
2195 if (strcmp(status, STATE_COMPLETED) == 0)
2196 {
2197 return 0;
2198 }
2199 else if (retry_count == 4) //not ok in 2s, do reconnect
2200 {
2201 DO_REQUEST("RECONNECT");
2202 return inner_check_ap_connected(idx, retry_count+1);
2203 }
you.chencba13492023-05-19 13:53:43 +08002204 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002205 {
2206 printf("retry 10 time\n");
2207 return -1;
2208 }
2209 else
2210 {
you.chen6d247052023-06-01 16:39:54 +08002211 if (strcmp(status, STATE_DISCONNECTED) == 0)
2212 {
2213 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2214 DO_REQUEST(LYNQ_WIFI_CMD);
2215 }
you.chenb95401e2023-05-12 19:39:06 +08002216 return inner_check_ap_connected(idx, retry_count+1);
2217 }
2218 }
2219 return -1;
2220}
qs.xiong1af5daf2022-03-14 09:12:12 -04002221
qs.xiongf1b525b2022-03-31 00:58:23 -04002222int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002223{
you.chen35020192022-05-06 11:30:57 +08002224 char LYNQ_WIFI_CMD[128]={0};
2225 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2226 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002227 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002228 CHECK_IDX(idx, CTRL_AP);
2229
2230 CHECK_WPA_CTRL(CTRL_AP);
2231
you.chen0df3e7e2023-05-10 15:56:26 +08002232 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002233 {
you.chen0df3e7e2023-05-10 15:56:26 +08002234 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002235 return -1;
2236 }
you.chen35020192022-05-06 11:30:57 +08002237
2238 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2239 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2240
2241 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2242 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2243
you.chenc9928582023-04-24 15:39:37 +08002244 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2245 if (ret != 0)
2246 {
2247 RLOGE("lynq_wifi_ap_start excute script fail");
2248 return -1;
2249 }
2250
you.chenb95401e2023-05-12 19:39:06 +08002251 if (inner_check_ap_connected(idx, 0) != 0)
2252 {
2253 return -1;
2254 }
2255
you.chen0df3e7e2023-05-10 15:56:26 +08002256 check_tether_and_notify();
2257
qs.xiong9fbf74e2023-03-28 13:38:22 +08002258 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002259}
2260
qs.xiongf1b525b2022-03-31 00:58:23 -04002261int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002262{
you.chen35020192022-05-06 11:30:57 +08002263 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002264}
2265
qs.xiongf1b525b2022-03-31 00:58:23 -04002266int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002267{
you.chen35020192022-05-06 11:30:57 +08002268 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002269
you.chen35020192022-05-06 11:30:57 +08002270 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002271
you.chen35020192022-05-06 11:30:57 +08002272 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002273
you.chen35020192022-05-06 11:30:57 +08002274 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2275
2276 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2277
you.chenb4b121c2022-05-06 17:50:16 +08002278// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002279
2280 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2281 if (ret != 0)
2282 {
2283 RLOGE("lynq_wifi_ap_start excute script fail");
2284 return -1;
2285 }
2286
qs.xiong9fbf74e2023-03-28 13:38:22 +08002287 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002288}
qs.xiong1af5daf2022-03-14 09:12:12 -04002289
qs.xiongf1b525b2022-03-31 00:58:23 -04002290int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002291{
you.chen35020192022-05-06 11:30:57 +08002292 char lynq_disable_cmd[128] = {0};
2293 char lynq_select_cmd[128] = {0};
2294 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002295 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002296 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002297
you.chen35020192022-05-06 11:30:57 +08002298 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002299 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2300 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2301
2302 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2303 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2304 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002305
qs.xiong9fbf74e2023-03-28 13:38:22 +08002306 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002307}
2308
qs.xiongf1b525b2022-03-31 00:58:23 -04002309int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002310{
you.chen35020192022-05-06 11:30:57 +08002311 char lynq_disable_cmd[128] = {0};
2312 char lynq_select_cmd[128] = {0};
2313 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002314 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002315 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002316
you.chen35020192022-05-06 11:30:57 +08002317 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002318
you.chen35020192022-05-06 11:30:57 +08002319 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2320 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2321
2322 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2323 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2324 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002325
qs.xiong9fbf74e2023-03-28 13:38:22 +08002326 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002327}
qs.xiongf1b525b2022-03-31 00:58:23 -04002328
you.chen35020192022-05-06 11:30:57 +08002329int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002330{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002331 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002332 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002333 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002334 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002335 RLOGD("enter lynq_ap_password_set");
2336 if( password == NULL )
2337 {
2338 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002339 return -1;
2340 }
2341 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002342 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002343 if(pass_len < 8 || pass_len >= 64)
2344 {
2345 RLOGE("[lynq_ap_password_set]input password len not in rage");
2346 return -1;
you.chen35020192022-05-06 11:30:57 +08002347 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002348
you.chen35020192022-05-06 11:30:57 +08002349 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002350
qs.xiong9fbf74e2023-03-28 13:38:22 +08002351 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2352 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002353 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002354 return -1;
2355 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002356 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2357 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002358 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2359 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002360 }
2361
you.chen35020192022-05-06 11:30:57 +08002362 CHECK_WPA_CTRL(CTRL_AP);
2363
qs.xiong9fbf74e2023-03-28 13:38:22 +08002364 if (auth == LYNQ_WIFI_AUTH_WEP)
2365 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002366 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002367 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2368 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2369 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2370 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2371 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002372 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2373 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002374 RLOGD("[lynq_ap_password_set]ap auth :LYNQ_WIFI_AUTH_WPA_PSK LYNQ_WIFI_AUTH_WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002375 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2376 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2377 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002378 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2379 {
2380
qs.xiongc8d92a62023-03-29 17:36:14 +08002381 RLOGD("[lynq_ap_password_set]ap auth :LYNQ_WIFI_AUTH_WPA2_WPA3 LYNQ_WIFI_AUTH_WPA3_PSK\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002382 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002383 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002384 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2385 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2386
2387 }
2388 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002389 {
2390 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002391 return -1;
2392 }
you.chen35020192022-05-06 11:30:57 +08002393
you.chen35020192022-05-06 11:30:57 +08002394 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002395
qs.xiong9fbf74e2023-03-28 13:38:22 +08002396 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002397}
2398
you.chen35020192022-05-06 11:30:57 +08002399int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002400{
you.chen35020192022-05-06 11:30:57 +08002401 FILE * fp;
2402 int len, ret;
2403 int count, index;
2404 char *split_lines[128] = {0};
2405 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002406 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002407
you.chen35020192022-05-06 11:30:57 +08002408 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002409
you.chen35020192022-05-06 11:30:57 +08002410 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2411// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002412 if (NULL == fp)
2413 {
2414 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002415 return -1;
2416 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002417
you.chen35020192022-05-06 11:30:57 +08002418 buff = alloca(MAX_RET);
2419 fseek(fp, 0, SEEK_SET);
2420 len = fread(buff, 1, MAX_RET, fp);
2421 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002422
qs.xiong9fbf74e2023-03-28 13:38:22 +08002423 for(index=0; index < len; index ++)
2424 {
2425 if (memcmp(buff + index, "network={", 9) != 0)
2426 {
you.chen35020192022-05-06 11:30:57 +08002427 continue;
2428 }
2429 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002430 for (; index < len; index ++ )
2431 {
2432 if (buff[index] != '}')
2433 {
you.chen35020192022-05-06 11:30:57 +08002434 continue;
2435 }
2436 buff[index] = '\0';
2437 break;
2438 }
2439 len = buff + index - p;
2440 }
2441
2442 count = lynq_split(p, len, '\n', split_lines);
2443
2444 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002445 for(index=0; index < count; index++)
2446 {
you.chen35020192022-05-06 11:30:57 +08002447 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002448 if (p != NULL)
2449 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002450 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002451 if (*p == '\"')
2452 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002453 p++;
2454 }
you.chen35020192022-05-06 11:30:57 +08002455 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002456 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2457 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002458 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002459 if (*p == '\"')
2460 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002461 p++;
2462 }
2463 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002464 else
2465 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002466 continue;
you.chen35020192022-05-06 11:30:57 +08002467 }
2468
2469 strcpy(password, p);
2470
qs.xiong9fbf74e2023-03-28 13:38:22 +08002471 while(*password != '\0')
2472 {
2473 if (*password == '\"')
2474 {
you.chen35020192022-05-06 11:30:57 +08002475 *password = '\0';
2476 break;
2477 }
2478 password++;
2479 }
2480 ret = 0;
2481 break;
2482 } //end for(index=0; index < count; index++)
2483
2484 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002485}
2486
you.chen35020192022-05-06 11:30:57 +08002487static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2488 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002489 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002490
qs.xiong9fbf74e2023-03-28 13:38:22 +08002491 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2492 {
you.chen35020192022-05-06 11:30:57 +08002493 return -1;
2494 }
2495
2496 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002497
qs.xiong9fbf74e2023-03-28 13:38:22 +08002498 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2499 {
2500 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002501 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002502 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002503 {
you.chena6cd55a2022-05-08 12:20:18 +08002504 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002505 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002506 }
you.chen70f377f2023-04-14 18:17:09 +08002507 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2508 {
2509 return 0;
2510 }
you.chena6cd55a2022-05-08 12:20:18 +08002511 }
2512 }
you.chen70f377f2023-04-14 18:17:09 +08002513 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2514 {
2515 return 0;
2516 }
2517
qs.xiong9fbf74e2023-03-28 13:38:22 +08002518 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2519 {
you.chen70f377f2023-04-14 18:17:09 +08002520 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002521 return -1;
2522 }
2523 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2524 {
2525
you.chen70f377f2023-04-14 18:17:09 +08002526 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2527 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002528 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2529 {
2530
2531 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002532 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002533 }else
2534 {
you.chen70f377f2023-04-14 18:17:09 +08002535 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002536 *auth = -1;
2537 return -1;
2538 }
you.chen35020192022-05-06 11:30:57 +08002539 return 0;
2540}
2541
2542int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002543{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002544 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002545 int pass_len, net_no, count, index;
2546 char lynq_tmp_cmd[300]={0};
2547 int net_no_list[128];
2548 lynq_wifi_auth_s net_auth;
2549 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002550 if(pass_len < 8 || pass_len >= 64)
2551 {
2552 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002553 return -1;
2554 }
2555
2556 CHECK_IDX(idx, CTRL_STA);
2557
2558 net_no = -1;
2559 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2560
qs.xiong9fbf74e2023-03-28 13:38:22 +08002561 for (index=0; index < count; index++)
2562 {
you.chen35020192022-05-06 11:30:57 +08002563 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002564 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2565 {
you.chen35020192022-05-06 11:30:57 +08002566 net_no = net_no_list[index];
2567 break;
2568 }
2569 }
2570
qs.xiong9fbf74e2023-03-28 13:38:22 +08002571 if (net_no < 0)
2572 {
you.chen35020192022-05-06 11:30:57 +08002573 return -1;
2574 }
2575
2576 CHECK_WPA_CTRL(CTRL_STA);
2577
2578 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2579
2580 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2581 DO_OK_FAIL_REQUEST(cmd_save_config);
2582
2583 return 0;
2584}
2585
2586int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2587
2588 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002589 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002590 int count, index;
2591 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002592 char *buff, *p, *ssid, *ssid_end_flag;
2593 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002594 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002595
you.chen755332b2022-08-06 16:59:10 +08002596 network_len = 0;
2597 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002598 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002599
you.chen35020192022-05-06 11:30:57 +08002600 CHECK_IDX(idx, CTRL_STA);
2601
qs.xiong9fbf74e2023-03-28 13:38:22 +08002602 if (NULL == password)
2603 {
2604 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002605 return -1;
2606 }
2607
you.chen35020192022-05-06 11:30:57 +08002608 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002609 if (NULL == fp)
2610 {
2611 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002612 return -1;
2613 }
2614
you.chen6d247052023-06-01 16:39:54 +08002615 fseek(fp, 0, SEEK_END);
2616 len = ftell(fp);
2617 buff = malloc(len + 1);
2618
2619 if (buff == NULL)
2620 {
2621 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2622 return -1;
2623 }
2624
you.chen35020192022-05-06 11:30:57 +08002625 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002626 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002627 fclose(fp);
2628
qs.xiong9fbf74e2023-03-28 13:38:22 +08002629 for(index=0; index < len; index ++)
2630 {
2631 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002632 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002633 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002634 {
you.chen35020192022-05-06 11:30:57 +08002635 continue;
2636 }
you.chen6d247052023-06-01 16:39:54 +08002637 p = buff + index + 9;
2638 for (; index < len; index ++ )
2639 {
2640 if (buff[index] != '}')
2641 {
2642 continue;
2643 }
2644 buff[index] = '\0';
2645 break;
2646 }
2647 network_len = buff + index - p;
2648 break;
you.chen35020192022-05-06 11:30:57 +08002649 }
2650
qs.xiongb3f26af2023-02-17 18:41:07 +08002651 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002652 {
2653 if (buff != NULL)
2654 {
2655 free(buff);
2656 }
2657
qs.xiongb3f26af2023-02-17 18:41:07 +08002658 return -1;
you.chen6d247052023-06-01 16:39:54 +08002659 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002660
you.chend2fef3f2023-02-13 10:50:35 +08002661 ssid = strstr(p, "ssid=");
2662 if (ssid != NULL) {
2663 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002664 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002665 {
you.chend2fef3f2023-02-13 10:50:35 +08002666 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2667 break;
2668 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002669 else
you.chen6d247052023-06-01 16:39:54 +08002670 {
you.chend2fef3f2023-02-13 10:50:35 +08002671 ssid_end_flag = strstr(ssid, "\n");
2672 if (ssid_end_flag != NULL)
2673 {
2674 ssid_len = (ssid_end_flag - ssid) / 2;
2675 for(i=0; i<ssid_len; i++)
2676 {
2677 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2678 }
2679 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2680 break;
2681 }
2682 }
you.chen35020192022-05-06 11:30:57 +08002683 }
you.chend2fef3f2023-02-13 10:50:35 +08002684
you.chen35020192022-05-06 11:30:57 +08002685 }
2686
qs.xiong9fbf74e2023-03-28 13:38:22 +08002687 if (index >= len || NULL == p || network_len <= 0)
2688 {
you.chen6d247052023-06-01 16:39:54 +08002689 if (buff != NULL)
2690 {
2691 free(buff);
2692 }
you.chen35020192022-05-06 11:30:57 +08002693 return -1;
2694 }
2695
you.chen755332b2022-08-06 16:59:10 +08002696 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002697
2698 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002699 for(index=0; index < count; index++)
2700 {
you.chen35020192022-05-06 11:30:57 +08002701 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002702 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002703 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002704 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002705 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002706 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002707 p++;
2708 }
you.chen35020192022-05-06 11:30:57 +08002709 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002710 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002711 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002712 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002713 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002714 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002715 p++;
2716 }
2717 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002718 else
you.chen6d247052023-06-01 16:39:54 +08002719 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002720 continue;
you.chen35020192022-05-06 11:30:57 +08002721 }
2722
qs.xiong13673462023-02-21 19:12:54 +08002723 if (*p == '\"')
2724 p++;
2725 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002726
qs.xiong13673462023-02-21 19:12:54 +08002727 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002728 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002729 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002730 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002731 {
you.chen35020192022-05-06 11:30:57 +08002732 *password = '\0';
2733 break;
2734 }
2735 password++;
2736 }
2737 ret = 0;
2738 break;
2739 } //end for(index=0; index < count; index++)
2740
you.chen6d247052023-06-01 16:39:54 +08002741 if (buff != NULL)
2742 {
2743 free(buff);
2744 }
2745
you.chen35020192022-05-06 11:30:57 +08002746 return ret;
2747}
2748
2749static int inner_set_sta_ssid(int net_no, char *sta_ssid)
2750{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002751 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04002752
qs.xiong9fbf74e2023-03-28 13:38:22 +08002753 if (sta_ssid == NULL)
2754 {
2755 RLOGE("sta_ssid is null\n");
2756 return -1;
you.chen35020192022-05-06 11:30:57 +08002757 }
2758
qs.xiong9fbf74e2023-03-28 13:38:22 +08002759 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08002760
2761 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
2762
2763 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2764// DO_OK_FAIL_REQUEST(cmd_save_config);
2765
qs.xiong9fbf74e2023-03-28 13:38:22 +08002766 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002767
2768}
2769
you.chen35020192022-05-06 11:30:57 +08002770static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05002771{
you.chen35020192022-05-06 11:30:57 +08002772 char lynq_disable_cmd[128]={0};
2773 char lynq_select_cmd[128]={0};
2774
2775 CHECK_WPA_CTRL(CTRL_STA);
2776
qs.xiong9fbf74e2023-03-28 13:38:22 +08002777 if (save != 0)
2778 {
you.chenc29444e2022-06-07 18:01:16 +08002779 if (start_flag != 0)
2780 {
2781 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
2782 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2783 }
2784 else
2785 {
2786 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
2787 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2788 }
you.chen35020192022-05-06 11:30:57 +08002789 DO_OK_FAIL_REQUEST(cmd_save_config);
2790 }
2791
qs.xiong9fbf74e2023-03-28 13:38:22 +08002792 if (start_flag == 0)
2793 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002794 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08002795 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2796 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002797 else
2798 {
you.chen35020192022-05-06 11:30:57 +08002799 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
2800 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2801 }
2802
2803 return 0;
2804}
2805
2806int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
2807{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002808 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002809 CHECK_IDX(idx, CTRL_STA);
2810
you.chen6c2dd9c2022-05-16 17:55:28 +08002811 curr_status_info curr_state;
2812 ap_info_s ap_info;
2813 curr_state.ap = &ap_info;
2814 curr_state.state = NULL;
2815
qs.xiong9fbf74e2023-03-28 13:38:22 +08002816 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
2817 {
you.chend2fef3f2023-02-13 10:50:35 +08002818 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08002819 return 0;
2820 }
2821
2822 return -1;
you.chen35020192022-05-06 11:30:57 +08002823}
2824
2825int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
2826{
you.chen9ac66392022-08-06 17:01:16 +08002827 scan_info_s *scan_list = NULL;
2828 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08002829 int scan_len=0;
2830 int save_len=0;
2831 int best_index = -1;
2832 int best_scan_index = -1;
2833 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08002834 int i, j, ret;
2835
2836 ret = -1;
you.chen35020192022-05-06 11:30:57 +08002837
2838 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002839 if (info == NULL)
2840 {
you.chen35020192022-05-06 11:30:57 +08002841 return -1;
2842 }
2843
2844 curr_status_info curr_state;
2845 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08002846 char status[64];
you.chen35020192022-05-06 11:30:57 +08002847
you.chen9ac66392022-08-06 17:01:16 +08002848 memset(&ap_info, 0, sizeof (ap_info));
2849 memset(status, 0, sizeof (status));
2850
2851 curr_state.ap = &ap_info;
2852 curr_state.state = status;
2853
qs.xiong9fbf74e2023-03-28 13:38:22 +08002854 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
2855 {
you.chen35020192022-05-06 11:30:57 +08002856 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08002857 if (strcmp(status, STATE_COMPLETED) == 0)
2858 {
2859 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
2860 }
2861 else
2862 {
2863 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2864 }
you.chen593621d2023-04-27 17:52:44 +08002865 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08002866 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08002867 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002868 return 0;
2869 }
2870
you.chen9ac66392022-08-06 17:01:16 +08002871 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08002872 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002873 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
2874 {
you.chen9ac66392022-08-06 17:01:16 +08002875 if (NULL != scan_list)
2876 {
2877 free(scan_list);
2878 }
you.chen35020192022-05-06 11:30:57 +08002879 return -1;
2880 }
2881
qs.xiong9fbf74e2023-03-28 13:38:22 +08002882 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
2883 {
you.chen9ac66392022-08-06 17:01:16 +08002884 if (NULL != scan_list)
2885 {
2886 free(scan_list);
2887 }
2888 if (NULL != save_list)
2889 {
2890 free(save_list);
2891 }
you.chen35020192022-05-06 11:30:57 +08002892 return -1;
2893 }
2894
qs.xiong9fbf74e2023-03-28 13:38:22 +08002895 for (i=0; i < save_len; i++)
2896 {
2897 for (j=0; j < scan_len; j++)
2898 {
you.chen35020192022-05-06 11:30:57 +08002899 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08002900 && save_list[i].base_info.auth == scan_list[j].auth)
2901 {
2902 if (best_rssi == 0)
2903 {
you.chen9ac66392022-08-06 17:01:16 +08002904 best_index = i;
you.chen35020192022-05-06 11:30:57 +08002905 best_rssi = scan_list[j].rssi;
2906 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002907 else if (best_rssi > scan_list[j].rssi)
2908 {
you.chen35020192022-05-06 11:30:57 +08002909 best_index = i;
2910 best_scan_index = j;
2911 best_rssi = scan_list[j].rssi;
2912 }
you.chend2fef3f2023-02-13 10:50:35 +08002913 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 +08002914 break;
2915 }
2916 }
2917 }
2918
qs.xiong9fbf74e2023-03-28 13:38:22 +08002919 if (best_index >= 0)
2920 {
you.chen35020192022-05-06 11:30:57 +08002921 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08002922 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 +08002923 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2924 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08002925 ret = 0;
you.chen35020192022-05-06 11:30:57 +08002926 }
2927
you.chen9ac66392022-08-06 17:01:16 +08002928 if (NULL != scan_list)
2929 {
2930 free(scan_list);
2931 }
2932 if (NULL != save_list)
2933 {
2934 free(save_list);
2935 }
2936
2937 return ret;
you.chen35020192022-05-06 11:30:57 +08002938}
2939
2940static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
2941{
qs.xiongc8d92a62023-03-29 17:36:14 +08002942 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08002943 char lynq_ket_mgmt_cmd[64]={0};
2944 char lynq_pairwise_cmd[64]={0};
2945 char lynq_psk_cmd[64]={0};
2946
2947 CHECK_WPA_CTRL(CTRL_STA);
2948
qs.xiong9fbf74e2023-03-28 13:38:22 +08002949 switch(auth)
2950 {
2951 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08002952 {
2953 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002954
you.chen35020192022-05-06 11:30:57 +08002955 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002956// DO_OK_FAIL_REQUEST(cmd_save_config);
2957 break;
2958 }
2959 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08002960 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002961 {
2962 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2963 {
you.chen35020192022-05-06 11:30:57 +08002964 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
2965 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002966 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2967 {
you.chena6cd55a2022-05-08 12:20:18 +08002968 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08002969 }
2970 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
2971 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002972
you.chen35020192022-05-06 11:30:57 +08002973 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2974 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
2975 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04002976
qs.xiong9fbf74e2023-03-28 13:38:22 +08002977 if (password != NULL)
2978 {
you.chen35020192022-05-06 11:30:57 +08002979 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
2980 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002981 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08002982 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002983
you.chen35020192022-05-06 11:30:57 +08002984// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002985 break;
2986 }
2987 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2988 {
qs.xiong3e506812023-04-06 11:08:48 +08002989 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08002990 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002991 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
2992 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
2993
qs.xiong3e506812023-04-06 11:08:48 +08002994 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002995 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
2996 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2997 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2998
2999 break;
3000 }
3001 case LYNQ_WIFI_AUTH_WPA3_PSK:
3002 {
qs.xiong3e506812023-04-06 11:08:48 +08003003 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003004 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003005 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3006 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3007
qs.xiong3e506812023-04-06 11:08:48 +08003008 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003009 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3010 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3011 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3012
3013 break;
3014 }
3015 default:
3016 return -1;
you.chen35020192022-05-06 11:30:57 +08003017 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003018
qs.xiong9fbf74e2023-03-28 13:38:22 +08003019 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003020}
qs.xiong7a105ce2022-03-02 09:43:11 -05003021
you.chen35020192022-05-06 11:30:57 +08003022static int inner_get_curr_net_no(int interface) {
3023 curr_status_info curr_state;
3024 curr_state.ap = NULL;
3025 curr_state.state = NULL;
3026
qs.xiong9fbf74e2023-03-28 13:38:22 +08003027 if (0 != inner_get_status_info(interface, &curr_state))
3028 {
you.chen35020192022-05-06 11:30:57 +08003029 return -1;
3030 }
3031
3032 return curr_state.net_no;
3033}
3034
3035int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003036{
you.chen35020192022-05-06 11:30:57 +08003037 int net_no;
3038 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003039
you.chen35020192022-05-06 11:30:57 +08003040 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003041
qs.xiong9fbf74e2023-03-28 13:38:22 +08003042 if (net_no < 0)
3043 {
you.chen35020192022-05-06 11:30:57 +08003044 return -1;
3045 }
3046
3047 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003048}
3049
you.chenb95401e2023-05-12 19:39:06 +08003050int lynq_wifi_sta_connect_timeout(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw, int timeout)
qs.xiong7a105ce2022-03-02 09:43:11 -05003051{
you.chen35020192022-05-06 11:30:57 +08003052 int count, net_no, index;
3053 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003054 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003055 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003056 curr_status_info curr_state;
3057 ap_info_s ap_info;
3058 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003059
qs.xiong9fbf74e2023-03-28 13:38:22 +08003060 if (ssid == NULL || *ssid == '\0')
3061 {
3062 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003063 return -1;
3064 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003065
qs.xiong9fbf74e2023-03-28 13:38:22 +08003066 if (LYNQ_WIFI_AUTH_OPEN != auth)
3067 {
3068 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003069 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003070 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003071 return -1;
3072 }
3073 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003074
you.chen70f377f2023-04-14 18:17:09 +08003075
3076 pthread_mutex_lock(&s_global_check_mutex);
3077 if (s_sta_status != INNER_STA_STATUS_INIT)
3078 {
3079 s_sta_status = INNER_STA_STATUS_CANCEL;
3080 pthread_cond_signal(&s_global_check_cond);
3081 }
3082 pthread_mutex_unlock(&s_global_check_mutex);
3083
you.chen35020192022-05-06 11:30:57 +08003084 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003085 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003086
3087 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003088 memset(&ap_info, 0, sizeof (ap_info));
3089 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003090
you.chen70f377f2023-04-14 18:17:09 +08003091 curr_state.ap = &ap_info;
3092 curr_state.state = status;
3093
3094 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003095 {
you.chen70f377f2023-04-14 18:17:09 +08003096 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3097 {
3098 net_no = curr_state.net_no;
3099 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3100 && strcmp(ap_info.psw, psw) == 0)
3101 {
3102 RLOGD("already connected\n");
3103
3104 pthread_mutex_lock(&s_global_check_mutex);
3105 s_sta_status = INNER_STA_STATUS_CONNECTED;
3106 pthread_cond_signal(&s_global_check_cond);
3107 pthread_mutex_unlock(&s_global_check_mutex);
3108 return 0;
3109 }
you.chen35020192022-05-06 11:30:57 +08003110 }
3111 }
3112
you.chen70f377f2023-04-14 18:17:09 +08003113 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003114 {
you.chen70f377f2023-04-14 18:17:09 +08003115 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3116
3117 for (index=0; index < count; index++)
3118 {
3119 net_auth = -1;
3120 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3121 {
3122 net_no = net_no_list[index];
3123 break;
3124 }
you.chen35020192022-05-06 11:30:57 +08003125 }
3126
you.chen70f377f2023-04-14 18:17:09 +08003127 if (net_no < 0)
3128 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003129 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3130 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3131 {
3132 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3133 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3134 DO_OK_FAIL_REQUEST(rm_net_cmd);
3135 }
you.chen70f377f2023-04-14 18:17:09 +08003136 net_no = lynq_add_network(CTRL_STA);
3137 if (net_no == -1)
3138 {
3139 return -1;
3140 }
3141
3142 RLOGD("net no is %d\n", net_no);
3143 if (0 != inner_set_sta_ssid(net_no, ssid))
3144 {
3145 return -1;
3146 }
you.chen35020192022-05-06 11:30:57 +08003147 }
3148 }
3149
qs.xiong9fbf74e2023-03-28 13:38:22 +08003150 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3151 {
you.chen35020192022-05-06 11:30:57 +08003152 return -1;
3153 }
3154
you.chen70f377f2023-04-14 18:17:09 +08003155
3156 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003157 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003158 usleep(200*1000);
3159
3160 ret = inner_sta_start_stop(net_no, 1, 1);
3161
3162 pthread_mutex_lock(&s_global_check_mutex);
3163 s_sta_status = INNER_STA_STATUS_CONNECTING;
3164 strcpy(s_sta_current_connecting_ssid, ssid);
3165 struct timeval now;
3166 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003167 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003168 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3169 pthread_cond_signal(&s_global_check_cond);
3170 pthread_mutex_unlock(&s_global_check_mutex);
3171 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003172}
3173
you.chenb95401e2023-05-12 19:39:06 +08003174int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3175{
3176 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3177}
3178
you.chen35020192022-05-06 11:30:57 +08003179int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003180{
you.chen35020192022-05-06 11:30:57 +08003181 ap_info_s ap;
3182 curr_status_info curr_state;
3183 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003184
qs.xiong9fbf74e2023-03-28 13:38:22 +08003185 if (ssid == NULL || *ssid == '\0')
3186 {
3187 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003188 return -1;
3189 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003190
you.chen35020192022-05-06 11:30:57 +08003191 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003192
you.chen35020192022-05-06 11:30:57 +08003193 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003194 curr_state.state = NULL;
3195
qs.xiong9fbf74e2023-03-28 13:38:22 +08003196 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3197 {
you.chen35020192022-05-06 11:30:57 +08003198 return 0;
3199 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003200
qs.xiong9fbf74e2023-03-28 13:38:22 +08003201 if (strcmp(ap.ap_ssid, ssid) != 0)
3202 {
you.chen35020192022-05-06 11:30:57 +08003203 return 0;
3204 }
3205
you.chen70f377f2023-04-14 18:17:09 +08003206 pthread_mutex_lock(&s_global_check_mutex);
3207 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3208 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003209 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003210}
qs.xiong97fa59b2022-04-07 05:41:29 -04003211
you.chena6cd55a2022-05-08 12:20:18 +08003212int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3213{
qs.xiongad2f89d2023-01-18 13:17:41 +08003214// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3215// const char *lynq_reconnect_cmd = "RECONNECT";
3216 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3217 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3218// 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 -05003219
you.chen35020192022-05-06 11:30:57 +08003220 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003221 CHECK_WPA_CTRL(CTRL_STA);
3222
you.chenc9928582023-04-24 15:39:37 +08003223 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3224 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003225 {
you.chenc9928582023-04-24 15:39:37 +08003226 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003227 return -1;
3228 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003229
qs.xiongad2f89d2023-01-18 13:17:41 +08003230 system(lynq_enable_sta_cmd);
3231 system(lynq_reconnect_cmd);
3232// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003233 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003234}
3235
you.chen6d247052023-06-01 16:39:54 +08003236static int inner_get_status_info_state (int interface, char *state) {
3237 curr_status_info curr_state;
3238 curr_state.ap = NULL;
3239 curr_state.state = state;
3240 return inner_get_status_info(interface, &curr_state);
3241}
you.chen35020192022-05-06 11:30:57 +08003242int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003243{
qs.xiongad2f89d2023-01-18 13:17:41 +08003244// char lynq_disable_network_cmd[MAX_CMD];
3245// curr_status_info curr_state;
3246// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003247 int i=0;
3248 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003249
you.chen6d247052023-06-01 16:39:54 +08003250// 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 +08003251 CHECK_IDX(idx, CTRL_STA);
3252 CHECK_WPA_CTRL(CTRL_STA);
3253
you.chen6d247052023-06-01 16:39:54 +08003254// system(lynq_disable_sta_cmd);
3255 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003256 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003257
3258 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3259 if (ret != 0)
3260 {
3261 RLOGE("lynq_wifi_ap_start excute script fail");
3262 return -1;
3263 }
3264
you.chen6d247052023-06-01 16:39:54 +08003265 for (i=0; i < 30; i++) // to check if sta is realy stoped
3266 {
3267 if (inner_get_status_info_state(idx, state) != 0)
3268 {
3269 break;
3270 }
3271
3272 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3273 {
3274 break;
3275 }
3276 RLOGD("lynq_wifi_ap_start curr state %s", state);
3277 usleep(SLEEP_TIME_ON_IDLE);
3278 }
3279
you.chena6cd55a2022-05-08 12:20:18 +08003280 return 0;
3281// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003282}
qs.xiongfcc914b2023-07-06 21:16:20 +08003283int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3284{
3285 char LYNQ_DISABLE_CMD[128]={0};
3286 CHECK_IDX(idx, CTRL_STA);
3287 CHECK_WPA_CTRL(CTRL_STA);
3288 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3289 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3290 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3291 return 0;
3292}
qs.xiong7a105ce2022-03-02 09:43:11 -05003293
you.chen35020192022-05-06 11:30:57 +08003294//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3295// int i, count;
3296// char *p;
3297// const char * FLAG_SSID = "ssid=";
3298// const char * FLAG_SBSID = "bssid=";
3299// const char * FLAG_KEY_MGMT = "key_mgmt=";
3300// const char * FLAG_FREQ = "freq=";
3301// char lynq_sta_cmd[MAX_CMD];
3302// char *split_lines[128] = {0};
3303
3304// CHECK_WPA_CTRL(CTRL_AP);
3305
3306// sprintf(lynq_sta_cmd, "STA %s", bssid);
3307
3308// DO_REQUEST(lynq_sta_cmd);
3309
3310// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3311
3312// for(i=0; i < count; i++) {
3313// p = strstr(split_lines[i], FLAG_SSID);
3314// if (p != NULL) {
3315// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3316// continue;
3317// }
3318// }
3319
3320// lynq_get_interface_ip(idx, ap->ap_ip);
3321// lynq_ap_password_set(idx, ap->psw);
3322
3323// return 0;
3324//}
3325
3326static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3327 curr_status_info curr_state;
3328 curr_state.ap = ap;
3329 curr_state.state = NULL;
3330 return inner_get_status_info(interface, &curr_state);
3331}
3332
3333int 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 -04003334{
you.chend2fef3f2023-02-13 10:50:35 +08003335 int index, line_count;
3336 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003337 const char *lynq_first_sta_cmd = "STA-FIRST";
3338 char lynq_next_sta_cmd[MAX_CMD];
3339 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003340 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003341
you.chen35020192022-05-06 11:30:57 +08003342 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003343
you.chen35020192022-05-06 11:30:57 +08003344 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003345
you.chenb95401e2023-05-12 19:39:06 +08003346 // ap_info_s * tmp_ap;
3347 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003348 if (ap == NULL || list == NULL || len == NULL)
3349 {
3350 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003351 return -1;
3352 }
3353
you.chenb95401e2023-05-12 19:39:06 +08003354 // ap = &tmp_ap;
3355 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003356 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003357 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003358
you.chenb95401e2023-05-12 19:39:06 +08003359 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003360 {
you.chenb95401e2023-05-12 19:39:06 +08003361 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003362 return -1;
3363 }
3364
3365 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3366 lynq_ap_password_get(idx, (*ap)->psw);
3367
you.chen35020192022-05-06 11:30:57 +08003368 DO_REQUEST(lynq_first_sta_cmd);
3369
3370 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003371 while (reply_len > 0)
3372 {
3373 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003374 {
you.chen35020192022-05-06 11:30:57 +08003375 break;
3376 }
3377 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3378 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3379 strcpy(bssid[index], split_lines[0]);
3380 index++;
3381 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3382 reply_len = MAX_RET;
3383 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003384 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003385 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003386 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003387 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003388 break;
3389 }
3390 }
3391
3392 *len = index;
3393
3394 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003395 for (index=0; index < *len; index++)
3396 {
you.chend2fef3f2023-02-13 10:50:35 +08003397 dev_info = &(*list)[index];
3398 memset(dev_info, 0, sizeof(device_info_s));
3399 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3400 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3401 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3402 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003403 free(bssid[index]);
3404 }
3405
3406 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003407}
3408
you.chen35020192022-05-06 11:30:57 +08003409int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003410{
you.chen35020192022-05-06 11:30:57 +08003411 int i, count, index, count_words;
3412 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3413 char *split_lines[128] = {0};
3414 char *split_words[128] = {0};
3415 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003416
qs.xiong9fbf74e2023-03-28 13:38:22 +08003417 if (list == NULL || len == NULL)
3418 {
you.chen35020192022-05-06 11:30:57 +08003419 return -1;
3420 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003421
you.chen9ac66392022-08-06 17:01:16 +08003422 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3423 {
3424 usleep(100 * 1000);
3425 }
3426
you.chen35020192022-05-06 11:30:57 +08003427 CHECK_IDX(idx, CTRL_STA);
3428
3429 CHECK_WPA_CTRL(CTRL_STA);
3430
3431 DO_REQUEST(lynq_scan_result_cmd);
3432
3433 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3434 *len = count - 1;
3435 *list = malloc(sizeof (scan_info_s) * *len);
3436
3437 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003438 for (index=0; index <count_words; index++)
3439 {
3440 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003441 }
3442
qs.xiong9fbf74e2023-03-28 13:38:22 +08003443 for(index = 1;index < count; index++)
3444 {
3445 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003446 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003447 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3448 if (count_words < 4)
3449 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003450 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003451 //bssid / frequency / signal level / flags / ssid
3452 p = (*list) + index - 1;
3453 strcpy(p->mac, split_words[0]);
3454 p->band = convert_band_from_freq(atoi(split_words[1]));
3455 p->rssi = -1 * atoi( split_words[2]);
3456 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003457 if (count_words == 4) // ssid hided
3458 {
3459 p->ssid[0] = '\0';
3460 }
3461 else
3462 {
3463 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3464 }
you.chen35020192022-05-06 11:30:57 +08003465 }
3466
3467 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003468}
qs.xiong97fa59b2022-04-07 05:41:29 -04003469
you.chen35020192022-05-06 11:30:57 +08003470int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3471{
3472 int count, net_no, index;
3473 int net_no_list[128];
3474 lynq_wifi_auth_s net_auth;
3475 char lynq_remove_cmd[MAX_CMD];
3476
qs.xiong9fbf74e2023-03-28 13:38:22 +08003477 if (ssid == NULL || *ssid == '\0')
3478 {
3479 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003480 return -1;
3481 }
3482
3483 CHECK_IDX(idx, CTRL_STA);
3484
3485 CHECK_WPA_CTRL(CTRL_STA);
3486
3487 net_no = -1;
3488 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3489
qs.xiong9fbf74e2023-03-28 13:38:22 +08003490 for (index=0; index < count; index++)
3491 {
you.chen35020192022-05-06 11:30:57 +08003492 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003493 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3494 {
you.chen35020192022-05-06 11:30:57 +08003495 net_no = net_no_list[index];
3496 break;
3497 }
3498 }
3499
qs.xiong9fbf74e2023-03-28 13:38:22 +08003500 if (net_no < 0)
3501 {
you.chen35020192022-05-06 11:30:57 +08003502 return 0;
3503 }
3504
3505 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3506
3507 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3508 DO_OK_FAIL_REQUEST(cmd_save_config);
3509
3510 return 0;
3511}
3512
3513int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003514{
you.chend2fef3f2023-02-13 10:50:35 +08003515 int count, index;
you.chen35020192022-05-06 11:30:57 +08003516 int net_no_list[128];
3517 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003518 RLOGD("enter lynq_get_sta_saved_ap api\n");
3519 if (list == NULL || len == NULL)
3520 {
3521 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003522 return -1;
3523 }
3524
3525 CHECK_IDX(idx, CTRL_STA);
3526
3527// CHECK_WPA_CTRL(CTRL_STA);
3528
3529 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003530 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003531
you.chen057aac42023-04-13 14:06:58 +08003532 if (count < 0)
3533 {
3534 RLOGE("list network fail");
3535 return count;
3536 }
3537 else if (count == 0)
3538 {
3539 *list = NULL;
3540 *len = 0;
3541 return 0;
3542 }
3543
you.chen35020192022-05-06 11:30:57 +08003544 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003545 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003546 *len = count;
3547
qs.xiong9fbf74e2023-03-28 13:38:22 +08003548 for (index=0; index < count; index++)
3549 {
you.chen35020192022-05-06 11:30:57 +08003550 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003551 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003552 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003553 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003554 {
you.chen35020192022-05-06 11:30:57 +08003555 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3556 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003557 else
you.chen057aac42023-04-13 14:06:58 +08003558 {
you.chen35020192022-05-06 11:30:57 +08003559 (*list)[index].base_info.band = -1;
3560 }
you.chen057aac42023-04-13 14:06:58 +08003561 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003562 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003563 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003564 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003565 return 0;
3566}
3567
3568int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3569{
qs.xiongc8d92a62023-03-29 17:36:14 +08003570 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003571 const char *lynq_scan_cmd = "SCAN";
3572
3573 CHECK_IDX(idx, CTRL_STA);
3574
3575 CHECK_WPA_CTRL(CTRL_STA);
3576
you.chen0df3e7e2023-05-10 15:56:26 +08003577 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3578 {
3579 RLOGD("tmp clear scanlist");
3580 system(clean_last_re);
3581 }
you.chen9ac66392022-08-06 17:01:16 +08003582 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003583 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003584 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3585 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003586 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003587 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3588 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003589 g_sta_scan_finish_flag = 1;
3590 return -1;
3591 }
you.chen35020192022-05-06 11:30:57 +08003592
3593 return 0;
3594}
3595
3596int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003597 if (cb == NULL)
3598 {
3599 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003600 return -1;
3601 }
3602
you.chen6d247052023-06-01 16:39:54 +08003603 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003604 g_ap_callback_priv = priv;
3605 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003606 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003607
you.chen6d247052023-06-01 16:39:54 +08003608 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003609 if (g_ap_watcher_pid == 0 )
3610 {
3611 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3612 {
3613 g_ap_watcher_pid = 0;
3614 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3615 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3616 return -1;
3617 }
3618 }
3619
3620 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3621 RLOGD("creat APWatcherTheradProc susccs");
3622
you.chen35020192022-05-06 11:30:57 +08003623 return 0;
3624}
3625
3626int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003627 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003628 if (g_ap_callback_priv == priv)
3629 {
you.chen35020192022-05-06 11:30:57 +08003630 g_ap_callback_func = NULL;
3631 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003632 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003633 return 0;
3634 }
you.chen6d247052023-06-01 16:39:54 +08003635 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003636 return -1;
3637}
3638
3639int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003640 if (cb == NULL)
3641 {
3642 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003643 return -1;
3644 }
3645
you.chen6d247052023-06-01 16:39:54 +08003646 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003647 g_sta_callback_priv = priv;
3648 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003649 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003650
you.chen6d247052023-06-01 16:39:54 +08003651 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003652 if (g_sta_watcher_pid == 0 ) {
3653 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3654 {
3655 g_sta_watcher_pid = 0;
3656 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3657 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3658 return -1;
3659 }
3660 }
3661
3662 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3663 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003664 return 0;
3665}
3666
3667int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003668 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003669 if (g_sta_callback_priv == priv)
3670 {
you.chen35020192022-05-06 11:30:57 +08003671 g_sta_callback_func = NULL;
3672 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003673 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003674 return 0;
3675 }
you.chen6d247052023-06-01 16:39:54 +08003676 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003677 return -1;
3678}
3679
qs.xiongfcc914b2023-07-06 21:16:20 +08003680int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
3681 if (cb == NULL)
3682 {
3683 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
3684 return -1;
3685 }
3686 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3687 g_sta_auto_callback_priv = priv;
3688 g_sta_auto_callback_func = cb;
3689 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3690 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
3691 if (g_sta_auto_watcher_pid == 0 ) {
3692 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
3693 {
3694 g_sta_auto_watcher_pid = 0;
3695 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3696 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3697 return -1;
3698 }
3699 }
3700 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3701 RLOGD("creat STAWatcherTheradProc susccs");
3702 return 0;
3703}
3704int lynq_unreg_sta_auto_event_callback(void * priv) {
3705 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3706 if (g_sta_auto_callback_priv == priv)
3707 {
3708 g_sta_auto_watcher_stop_flag = 1;
3709 if (g_sta_auto_watcher_pid != 0)
3710 {
3711 pthread_join(g_sta_auto_watcher_pid, NULL);
3712 }
3713 g_sta_auto_watcher_pid = 0;
3714 g_sta_auto_callback_func = NULL;
3715 g_sta_auto_callback_priv = NULL;
3716 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3717 return 0;
3718 }
3719 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3720 return -1;
3721}
you.chen35020192022-05-06 11:30:57 +08003722int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
3723{
3724 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003725 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08003726 CHECK_IDX(idx, CTRL_AP);
3727
qs.xiong9fbf74e2023-03-28 13:38:22 +08003728 if (inner_get_status_info_state(CTRL_AP, state) != 0)
3729 {
you.chen35020192022-05-06 11:30:57 +08003730 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3731 return 0;
3732 }
3733
qs.xiong9fbf74e2023-03-28 13:38:22 +08003734 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3735 {
you.chen35020192022-05-06 11:30:57 +08003736 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
3737 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003738 else
3739 {
you.chen35020192022-05-06 11:30:57 +08003740 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3741 }
3742
3743 return 0;
3744}
3745
3746int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
3747 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003748 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08003749 CHECK_IDX(idx, CTRL_STA);
3750
qs.xiong9fbf74e2023-03-28 13:38:22 +08003751 if (inner_get_status_info_state(CTRL_STA, state) != 0)
3752 {
you.chen35020192022-05-06 11:30:57 +08003753 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3754 return 0;
3755 }
3756
qs.xiong9fbf74e2023-03-28 13:38:22 +08003757 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3758 {
you.chen35020192022-05-06 11:30:57 +08003759 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
3760 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003761 else
3762 {
you.chen35020192022-05-06 11:30:57 +08003763 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3764 }
3765
3766 return 0;
3767}
3768
3769int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
3770// CHECK_IDX(idx, CTRL_AP);
3771// int ret = 0;
3772// size_t reply_len = MAX_RET;
3773// char cmd_reply[MAX_RET]={0};
3774// const char * cmd_str = "GET country";
3775// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
3776// do{
3777// if (NULL == s_lynq_wpa_ctrl) {
3778// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
3779// if (NULL == s_lynq_wpa_ctrl ) {
3780// printf("wpa_ctrl_open fail\n");
3781// return -1;
3782// }
3783// }
3784// }while(0);
3785
3786// do {
3787// reply_len = MAX_RET;
3788// cmd_reply[0] = '\0';
3789// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08003790// 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 +08003791// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003792// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08003793// return ret;
3794// }
3795// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08003796// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08003797// }while(0);
3798
3799 FILE *fp;
3800 size_t i = 0;
3801 char lynq_cmd_ret[MAX_RET]={0};
3802
3803// CHECK_IDX(idx, CTRL_AP);
3804
3805 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003806 {
3807 perror("popen error!");
3808 return -1;
3809 }
you.chen35020192022-05-06 11:30:57 +08003810 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
3811 {
3812 perror("fread fail!");
3813 return -1;
3814 }
3815
qs.xiong9fbf74e2023-03-28 13:38:22 +08003816 for(i=0; i < strlen(lynq_cmd_ret); i++)
3817 {
3818 if (lynq_cmd_ret[i] == ' ')
3819 {
you.chen35020192022-05-06 11:30:57 +08003820 lynq_cmd_ret[i] = '\0';
3821 break;
3822 }
3823 }
3824
3825 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003826 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08003827
3828 int ret=pclose(fp);
3829 if(ret==-1)
3830 {
3831 perror("close file faild");
3832 }
3833
3834 return 0;
3835}
3836
you.chen705a7ef2023-06-01 22:06:45 +08003837static int check_and_init_uci_config(char * country_code)
3838{
3839 FILE * fp;
3840 int is_different = 0;
3841 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
3842 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
3843 const char * commit_uci_cmd ="uci commit";
3844 char set_country_cmd[MAX_CMD];
3845 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08003846
you.chen705a7ef2023-06-01 22:06:45 +08003847 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08003848
you.chen705a7ef2023-06-01 22:06:45 +08003849 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08003850 {
you.chen705a7ef2023-06-01 22:06:45 +08003851 if (0 != system(create_uci_cmd))
3852 {
3853 RLOGE("creat_uci_cmd fail");
3854 return -1;
3855 }
3856 is_different = 1;
3857 }
3858
3859 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
3860 {
3861 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08003862 return -1;
3863 }
3864
you.chen705a7ef2023-06-01 22:06:45 +08003865 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08003866 {
you.chen705a7ef2023-06-01 22:06:45 +08003867 RLOGE("fread fail!");
3868 fclose(fp);
3869 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08003870 }
3871
you.chen705a7ef2023-06-01 22:06:45 +08003872 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
3873 {
3874 RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
3875 is_different = 1;
3876 }
3877
3878 fclose(fp);
3879
3880 if (is_different)
3881 {
3882 if ( 0 != system(set_country_cmd))
3883 {
3884 RLOGE("set_country_cmd fail");
3885 return -1;
3886 }
3887 if (0 != system(commit_uci_cmd))
3888 {
3889 RLOGE("commmit fail");
3890 }
3891 }
3892
3893 return is_different;
3894}
3895
3896int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
3897 char check_current_code[10];
3898 const char * support_country[] = {"CN", "EU"};
3899
3900 int ret,is_different, i, cc_count;
3901
3902 if (country_code == NULL || country_code[0] == '\0')
3903 {
3904 RLOGE("bad country code\n");
3905 return -1;
3906 }
3907
3908 cc_count = sizeof (support_country) / sizeof (char*);
3909 for(i=0; i < cc_count; i++)
3910 {
3911 if (strcmp(support_country[i], country_code) == 0)
3912 {
3913 break;
3914 }
3915 }
3916
3917 if (i >= cc_count)
3918 {
3919 RLOGE("unspported country code %s\n", country_code);
3920 return -1;
3921 }
3922
3923 is_different = check_and_init_uci_config(country_code);
3924 if( is_different < 0 )
3925 {
3926 RLOGE("init set uci fail\n");
3927 return -1;
3928 }
3929
3930 ret = lynq_get_country_code(idx,check_current_code);
3931 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
3932 {
3933 ret = lynq_wifi_disable();
3934 if(ret != 0 )
3935 {
3936 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
3937 return -1;
3938 }
3939 }
3940
3941 return 0;
you.chen35020192022-05-06 11:30:57 +08003942}
3943
3944int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
3945{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003946 RLOGD("enter lynq_get_connect_ap_mac\n");
3947 if (mac == NULL)
3948 {
3949 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08003950 return -1;
3951 }
3952
3953 CHECK_IDX(idx, CTRL_STA);
3954 ap_info_s ap;
3955 ap.ap_mac[0] = '\0';
3956
qs.xiong9fbf74e2023-03-28 13:38:22 +08003957 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
3958 {
you.chen35020192022-05-06 11:30:57 +08003959 return -1;
3960 }
3961 strcpy(mac, ap.ap_mac);
3962
3963 return 0;
3964}
3965
3966int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
3967{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003968 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08003969 struct ifaddrs *ifaddr_header, *ifaddr;
3970 struct in_addr * ifa;
3971 const char * ifaName = "wlan0";
3972 if (ip == NULL)
3973 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003974 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08003975 return -1;
you.chen9ac66392022-08-06 17:01:16 +08003976 }
you.chenf58b3c92022-06-21 16:53:48 +08003977
qs.xiong9fbf74e2023-03-28 13:38:22 +08003978 if (idx == 1)
3979 {
you.chen0df3e7e2023-05-10 15:56:26 +08003980 ifaName = inner_get_ap_interface_name();
3981 if (ifaName == NULL)
3982 {
3983 RLOGE("[lynq_get_interface_ip] ap name get fail");
3984 return -1;
3985 }
you.chen9ac66392022-08-06 17:01:16 +08003986 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003987 else if (idx != 0)
3988 {
you.chen35020192022-05-06 11:30:57 +08003989 return -1;
you.chen9ac66392022-08-06 17:01:16 +08003990 }
you.chen35020192022-05-06 11:30:57 +08003991
you.chen9ac66392022-08-06 17:01:16 +08003992 if (getifaddrs(&ifaddr_header) == -1)
3993 {
you.chen35020192022-05-06 11:30:57 +08003994 perror("getifaddrs");
3995 return -1;
3996 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08003997 }
you.chen35020192022-05-06 11:30:57 +08003998
3999
you.chen9ac66392022-08-06 17:01:16 +08004000 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4001 {
4002 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004003 continue;
you.chen9ac66392022-08-06 17:01:16 +08004004 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4005 {
4006 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4007 {
4008 // is a valid IP4 Address
4009 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4010 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004011 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004012 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004013 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004014 return 0;
4015 }
4016 }
4017 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004018 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004019 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004020 return -1;
you.chen35020192022-05-06 11:30:57 +08004021}
4022
4023int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4024{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004025 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004026 int count;
4027 size_t i;
4028 char *split_words[128] = {0};
4029 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4030
4031 CHECK_WPA_CTRL(idx);
4032
4033 DO_REQUEST(lynq_get_mac_cmd);
4034
qs.xiong9fbf74e2023-03-28 13:38:22 +08004035 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4036 {
4037 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004038 return -1;
4039 }
4040
4041 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4042
qs.xiong9fbf74e2023-03-28 13:38:22 +08004043 if (count < 2)
4044 {
you.chen35020192022-05-06 11:30:57 +08004045 return -1;
4046 }
4047
qs.xiong9fbf74e2023-03-28 13:38:22 +08004048 for (i=0; i < strlen(split_words[1]); i++ )
4049 {
4050 if (split_words[1][i] != ' ')
4051 {
you.chen35020192022-05-06 11:30:57 +08004052 break;
4053 }
4054 }
4055
4056 strcpy(mac, split_words[1] + i);
4057
4058 return 0;
4059}
4060
4061int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4062{
4063// int count;
4064// char *split_words[128] = {0};
4065// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4066
4067// if (rssi == NULL) {
4068// return -1;
4069// }
4070
4071// CHECK_IDX(idx, CTRL_STA);
4072
4073// CHECK_WPA_CTRL(CTRL_STA);
4074
4075// DO_REQUEST(lynq_get_rssi_cmd);
4076
4077// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4078// return -1;
4079// }
4080
4081// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4082
4083// if (count < 2) {
4084// return -1;
4085// }
4086
4087// *rssi = atoi(split_words[1]) * -1;
4088
you.chen35020192022-05-06 11:30:57 +08004089 char lynq_cmd_ret[MAX_RET]={0};
4090
qs.xiongff0ae0f2022-10-11 15:47:14 +08004091/*******change other cmd to get rssi*******
4092 *
4093 *wl rssi ---> wl -i wlan0 rssi
4094 *
4095 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004096 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004097 {
you.chen23c4a5f2023-04-12 16:46:00 +08004098 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004099 return -1;
4100 }
you.chen9f17e4d2022-06-06 17:18:18 +08004101 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004102/****** if got rssi is 0,means sta didn't connected any device****/
4103 if(*rssi == 0)
4104 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004105 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004106 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004107 }
you.chen35020192022-05-06 11:30:57 +08004108
4109 return 0;
4110}
4111
4112int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4113{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004114 RLOGD("enter lynq_get_connect_ap_band\n");
4115 if (band == NULL)
4116 {
you.chen35020192022-05-06 11:30:57 +08004117 return -1;
4118 }
4119
4120 CHECK_IDX(idx, CTRL_STA);
4121 ap_info_s ap;
4122 ap.band = -1;
4123
qs.xiong9fbf74e2023-03-28 13:38:22 +08004124 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4125 {
you.chen35020192022-05-06 11:30:57 +08004126 return -1;
4127 }
4128 *band = ap.band;
4129
4130 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004131}
you.chenf58b3c92022-06-21 16:53:48 +08004132
4133int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4134{
you.chenb95401e2023-05-12 19:39:06 +08004135 int ret;
4136 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004137 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004138
4139 if (ip == NULL)
4140 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004141 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004142 return -1;
4143 }
4144
4145 CHECK_IDX(idx, CTRL_STA);
4146
qs.xionge4cbf1c2023-02-28 18:22:49 +08004147 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004148 {
4149 return -1;
4150 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004151
you.chenb95401e2023-05-12 19:39:06 +08004152 ip[0] = '\0';
4153 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4154 if (ret != 0)
4155 {
4156 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4157 return -1;
4158 }
4159
4160 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4161 {
4162 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004163 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004164 if (ret != 0)
4165 {
4166 ip[0] = '\0';
4167 return 0;
4168 }
4169 else
4170 {
4171 p = strchr(ip, '\n');
4172 if (p != NULL)
4173 {
4174 *p = '\0';
4175 }
4176 }
4177 }
4178 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004179}
4180
qs.xiong026c5c72022-10-17 11:15:45 +08004181int lynq_ap_connect_num(int sta_number)
4182{
4183 char lynq_limit_cmd[32]={0};
4184 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004185 if((sta_number < 1 ) && (sta_number > 15))
4186 {
4187 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004188 return -1;
4189 }
4190 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4191 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004192 if(ret != 0)
4193 {
4194 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004195 }
4196 return 0;
4197}
you.chenf58b3c92022-06-21 16:53:48 +08004198
qs.xiong77905552022-10-17 11:19:57 +08004199int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4200{
4201
4202 char lynq_wifi_acs_cmd[128]={0};
4203 char lynq_cmd_mode[128]={0};
4204 char lynq_cmd_slect[128]={0};
4205
qs.xiong9fbf74e2023-03-28 13:38:22 +08004206 if((acs_mode != 2) && (acs_mode != 5))
4207 {
qs.xiong77905552022-10-17 11:19:57 +08004208 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4209 }
4210
qs.xiong9fbf74e2023-03-28 13:38:22 +08004211 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4212 {
qs.xiong77905552022-10-17 11:19:57 +08004213 return -1;
4214 }
4215
4216 CHECK_IDX(idx, CTRL_AP);
4217
4218 CHECK_WPA_CTRL(CTRL_AP);
4219
4220 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4221 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4222 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4223
4224 DO_OK_FAIL_REQUEST(cmd_disconnect);
4225 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4226 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4227 DO_OK_FAIL_REQUEST(cmd_save_config);
4228 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4229
4230 return 0;
4231}
you.chen0f5c6432022-11-07 18:31:14 +08004232//you.chen add for tv-box start
4233static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4234 FILE *fp;
4235 //printf("to exec cmd:%s\n", str_cmd);
4236 if((fp=popen(str_cmd,"r"))==NULL)
4237 {
4238 perror("popen error!");
4239 return -1;
4240 }
4241 if((fread(str_cmd_ret,max_len,1,fp))<0)
4242 {
4243 perror("fread fail!");
4244 fclose(fp);
4245 return -1;
4246 }
4247 fclose(fp);
4248 return 0;
4249}
4250
4251static int get_netmask_length(const char* mask)
4252{
4253 int masklen=0, i=0;
4254 int netmask=0;
4255
4256 if(mask == NULL)
4257 {
4258 return 0;
4259 }
4260
4261 struct in_addr ip_addr;
4262 if( inet_aton(mask, &ip_addr) )
4263 {
4264 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004265 }else
4266 {
you.chen0f5c6432022-11-07 18:31:14 +08004267 netmask = 0;
4268 return 0;
4269 }
4270
4271 while(0 == (netmask & 0x01) && i<32)
4272 {
4273 i++;
4274 netmask = netmask>>1;
4275 }
4276 masklen = 32-i;
4277 return masklen;
4278}
4279
4280static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4281 int mask_len;
4282 char *p;
4283 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004284 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4285 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004286 return -1;
4287 p = strstr(str_cmd_ret, "Mask:");
4288 if (p == NULL)
4289 return -1;
4290 mask_len = get_netmask_length(p + 5);
4291 if (mask_len == 0)
4292 return -1;
4293 p = strstr(str_cmd_ret, "inet addr:");
4294 if (p == NULL)
4295 return -1;
4296 strcpy(tmp, p + 10);
4297 p = strstr(tmp, " ");
4298 if (p != NULL)
4299 *p = '\0';
4300 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4301 return 0;
4302}
4303
4304static void GBWWatchThreadProc() {
4305 int i,n, nloop, nmax, ncheckcount, nidlecount;
4306 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4307 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4308 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4309 char *results[16] = {0};
4310 char str_cmd[256] = {0};
4311 char str_cmd_ret[128] = {0};
4312 char dest_ip[32] = {0};
4313 lastAP1Bytes = lastAP2Bytes = 0;
4314 lastAP1Drop = lastAP2Drop = 0;
4315 lastAP1Speed = lastAP2Speed = 0;
4316 setAP1Speed = 50;
4317 setAP2Speed = 80;
4318 nloop = 0;
4319 nmax = 6;
4320 ncheckcount = nidlecount = 0;
4321
you.chen0df3e7e2023-05-10 15:56:26 +08004322 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004323 {
4324 RLOGE("------gbw thread run\n");
4325 return;
4326 }
4327
qs.xiong9fbf74e2023-03-28 13:38:22 +08004328 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004329 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4330 while (dest_ip[0] == '\0') {
4331 sleep(1);
4332 str_cmd_ret[0] = '\0';
4333 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4334 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4335 if (str_cmd_ret[n] == '\n'){
4336 str_cmd_ret[n] = '\0';
4337 break;
4338 }
4339 }
4340 if (str_cmd_ret[0] != '\0')
4341 {
4342 strcpy(dest_ip, str_cmd_ret);
4343 }
4344 }
4345
you.chenc9928582023-04-24 15:39:37 +08004346 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4347 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4348 system_call_v("tc class add dev %s parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000", s_ap_iterface_name);
you.chen0f5c6432022-11-07 18:31:14 +08004349 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4350 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004351 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004352 return;
4353 }
you.chenc9928582023-04-24 15:39:37 +08004354 system_call_v("tc filter add dev %s parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", s_ap_iterface_name, str_cmd_ret);
4355 system_call_v("tc class add dev %s parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000", s_ap_iterface_name);
4356 system_call_v("tc filter add dev %s parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", s_ap_iterface_name, dest_ip);
you.chen0f5c6432022-11-07 18:31:14 +08004357
4358 while (1) {
4359 sleep(1);
4360 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004361 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4362 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4363 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004364 continue;
4365 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004366 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004367 if (n > 9) {
4368 if (strcmp(results[1], "Sent") == 0) {
4369 currAP1Bytes = atoll(results[2]);
4370 }
4371 if (strcmp(results[6], "(dropped") == 0) {
4372 currAP1Drop = atoi(results[7]);
4373 }
4374 }
4375
4376 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004377 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4378 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4379 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004380 continue;
4381 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004382 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004383 if (n > 9) {
4384 if (strcmp(results[1], "Sent") == 0) {
4385 currAP2Bytes = atoll(results[2]);
4386 }
4387 if (strcmp(results[6], "(dropped") == 0) {
4388 currAP2Drop = atoi(results[7]);
4389 }
4390 }
4391
4392 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4393 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4394 lastAP1Bytes = currAP1Bytes;
4395 lastAP2Bytes = currAP2Bytes;
4396 continue;
4397 }
4398
4399 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4400 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4401 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4402 lastAP1Speed = currAP1Speed;
4403 lastAP2Speed = currAP2Speed;
4404 lastAP1Bytes = currAP1Bytes;
4405 lastAP2Bytes = currAP2Bytes;
4406
4407 currSetAP1Speed = setAP1Speed;
4408 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4409 ncheckcount++;
4410 if (ncheckcount > 3) {
4411 ncheckcount = 0;
4412 currSetAP1Speed = 5;
4413 }
4414 }
4415 else {
4416 ncheckcount = 0;
4417 if (currAP1Speed < 5)
4418 nidlecount++;
4419 else
4420 nidlecount = 0;
4421
4422 }
4423
4424 if (nidlecount > 60 ){
4425 currSetAP1Speed = 50;
4426 }
4427
4428 if (currSetAP1Speed != setAP1Speed) {
4429 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004430 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4431 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004432 }
4433 }
4434}
4435
4436int enableGBW(const char* mac) {
4437 int i,len;
4438 char get_ipaddr_cmd[128]={0};
4439 ap_info_s *ap;
4440 device_info_s * list;
4441
4442 if (mac == NULL || g_gbw_enabled == 1)
4443 return -1;
4444 len = strlen(mac);
4445 g_gbw_mac = malloc(len + 1);
4446 for(i=0;i<len;i++) {
4447 if (mac[i] >= 'A' && mac[i] <= 'Z')
4448 {
4449 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4450 }
4451 else
4452 g_gbw_mac[i] = mac[i];
4453 }
4454 g_gbw_mac[i] = '\0';
4455 g_gbw_enabled = 1;
4456
4457 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4458 if (system(get_ipaddr_cmd) == 0) {
4459 //startGBW();
4460 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4461 for (i=0;i<len;i++) {
4462 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4463 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4464 startGBW();
4465 }
4466 free(ap);
4467 free(list);
4468 }
4469 }
4470 return 0;
4471}
4472
4473int disableGBW() {
4474 stopGBW();
4475 free(g_gbw_mac);
4476 g_gbw_mac = NULL;
4477 g_gbw_enabled = 1;
4478 return 0;
4479}
4480
4481static int startGBW() {
4482 if (g_gbw_watcher_pid != 0) {
4483 stopGBW();
4484 }
4485 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4486}
4487
4488static int stopGBW() {
4489 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004490 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004491 pthread_cancel(g_gbw_watcher_pid);
4492 pthread_join(g_gbw_watcher_pid, &retval);
4493 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004494 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4495 if (s_ap_iterface_name[0] != '\0')
4496 {
4497 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4498 system(cmd);
4499 }
you.chen0f5c6432022-11-07 18:31:14 +08004500}
4501//you.chen add for tv-box end