blob: b0dc78b9934bc637ce1557ab65642538e23a1a53 [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 }
qs.xiong31163d62023-07-11 18:54:40 +0800474 else if (strstr(msg_notify, "WoWLAN is enabled") != NULL || strstr(msg_notify, "Failed to start AP functionality") != NULL || strstr(msg_notify, "Could not connect to kernel driver") != NULL )
475 {
476 RLOGD("APWatcherThreadProc ap------> service error");
477 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
478 }
479 else
480 {
481 RLOGD("APWatcherThreadProc ap------> going on check next msg");
482 }
you.chenf711c8a2023-04-13 13:49:45 +0800483 //you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800484 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
485 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +0800486 if (lynq_wpa_ctrl != NULL)
487 {
you.chen92fd5d32022-05-25 10:09:47 +0800488 wpa_ctrl_detach(lynq_wpa_ctrl);
489 wpa_ctrl_close(lynq_wpa_ctrl);
490 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400491}
492
you.chen70f377f2023-04-14 18:17:09 +0800493static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
494{
495 char * p;
496 const char * try_associat_flag = "Trying to associate";
497 const char * associated_flag = "Associated with ";
498
499 pthread_mutex_lock(&s_global_check_mutex);
500 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
501 {
502 pthread_mutex_unlock(&s_global_check_mutex);
503 return;
504 }
505
506 if (state == LYNQ_WIFI_STATUS_EGNORE)
507 {
508 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
509 {
510 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
511 }
512 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
513 {
514 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
515 }
516 }
517 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
518 {
519 s_sta_error_number = error_num;
520 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
521 {
522 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
523 pthread_cond_signal(&s_global_check_cond);
524 }
525 }
526 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
527 {
528 s_sta_status = INNER_STA_STATUS_CONNECTED;
529 pthread_cond_signal(&s_global_check_cond);
530 }
531 pthread_mutex_unlock(&s_global_check_mutex);
532}
533
qs.xiongf0128b12023-06-29 17:29:39 +0800534static int lynq_wifi_sta_stop_network(lynq_wifi_index_e idx,int networkid)
535{
536 char LYNQ_DISABLE_CMD[128]={0};
537
538 CHECK_IDX(idx, CTRL_STA);
539 CHECK_WPA_CTRL(CTRL_STA);
540
541 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
542 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
543 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
544
545 return 0;
546
547}
548
549
qs.xiong455c30b2023-04-12 11:40:02 +0800550void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
551{
552 char *pReason;
qs.xiongf0128b12023-06-29 17:29:39 +0800553 char *wpanetid;
554 char destid[3] = {0};
555 int tmpdisid = -1;
qs.xiong455c30b2023-04-12 11:40:02 +0800556 *error = LYNQ_WAIT_CONNECT_ACTIVE;
557 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
558 {
559 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
560 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
561 return;
562 }
563
564 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
565 {
566 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
567 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
568 return;
569 }
570
571 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
572 {
qs.xiongf0128b12023-06-29 17:29:39 +0800573 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
574 wpanetid = strstr(modify,"id=");
575 if ( wpanetid != NULL )
576 {
577 wpanetid +=strlen("id=");
578 memcpy(destid,wpanetid,2);
579 tmpdisid = atoi(destid);
580
581 }
qs.xiong455c30b2023-04-12 11:40:02 +0800582 pReason = strstr(modify, "reason=");
583 if (pReason != NULL)
584 {
585 pReason += strlen("reason=");
586 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
587 {
588 *error = LYNQ_TIME_OUT;
589 }
590 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
591 {
592 *error = LYNQ_PSW_ERROR;
qs.xiongf0128b12023-06-29 17:29:39 +0800593 // tmp fix sta autoconnect connect and disconnect
594 if(tmpdisid != -1 && lynq_wifi_sta_stop_network(0,tmpdisid) != 0)
595 {
596 RLOGE("stop wlan0 network %d fail",tmpdisid);
597 }
qs.xiong455c30b2023-04-12 11:40:02 +0800598 }
599 else
600 {
601 *error = LYNQ_UNSPECIFIED_REASON;
602 }
qs.xiong455c30b2023-04-12 11:40:02 +0800603 }
604 else
605 {
606 *error = LYNQ_UNSPECIFIED_REASON;
qs.xiong455c30b2023-04-12 11:40:02 +0800607 }
qs.xiongf0128b12023-06-29 17:29:39 +0800608 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,tmpnetid:%d",*state,*error,tmpdisid);
609 return;
qs.xiong455c30b2023-04-12 11:40:02 +0800610
611 }
612
613 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
614 {
615 *error = LYNQ_NOT_FIND_AP;
616 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
617 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
618 return;
619 }
620
621
622 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
623 {
624 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
625 pReason = strstr(modify, "status_code=");
626 if (pReason != NULL)
627 {
628 pReason += strlen("status_code=");
629 if (memcmp(pReason, "17", 2) == 0)
630 {
631 *error = LYNQ_AP_UNABLE_HANDLE;
632 }
633 else if (memcmp(pReason, "1",1) == 0)
634 {
635 *error = LYNQ_UNSPECIFIED_REASON;
636 }
637 else
638 {
639 *error = LYNQ_UNSPECIFIED_REASON;
640 }
641
642 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
643 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
644 return;
645 }
646 else
647 {
648 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
649 *error = LYNQ_UNSPECIFIED_REASON;
650 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
651 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
652 return;
653 }
654 }
655
656 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
657 {
658 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
659 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
660 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
661 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
662 return;
663 }
664
665 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
666 {
667 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
668 *error = LYNQ_WAIT_CONNECT_ACTIVE;
669 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
670 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
671 return;
672 }
673
you.chen32cb31e2023-04-13 14:05:45 +0800674 RLOGD("EVENT : %s\n", modify);
qs.xiong455c30b2023-04-12 11:40:02 +0800675 *error = LYNQ_UNSPECIFIED_REASON;
you.chen32cb31e2023-04-13 14:05:45 +0800676 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiong455c30b2023-04-12 11:40:02 +0800677 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
678 return;
679
680}
681
qs.xiongfcc914b2023-07-06 21:16:20 +0800682void get_state_error_networkid(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error,int *networkid)
683{
684 char *pReason;
685 char *wpanetid;
686 char destid[3];
687 *error = LYNQ_WAIT_CONNECT_ACTIVE;
688 *networkid = -1;
689 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
690 {
691 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
692 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d,,networkid:%d\n",*state,*error,*networkid);
693 return;
694 }
695 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
696 {
697 RLOGD("[xiong]:wpanetid = strstrmodify;\n");
698 wpanetid = strstr(modify,"id=");
699 if ( wpanetid != NULL )
700 {
701 wpanetid +=strlen("id=");
702 RLOGD("[xiong]:memcpy(destid,wpanetid,0\n");
703 if (memcpy(destid,wpanetid,2) != NULL)
704 {
705 RLOGD("[xiong]:memcpy(destid,wpanetid,1\n");
706 *networkid = atoi(destid);
707 RLOGD("get networkid is %d\n",*networkid);
708 }
709 RLOGD("[xiong]:memcpy(destid,wpanetid,2\n");
710 }
711 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
712 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
713 return;
714 }
715 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
716 {
717 wpanetid = strstr(modify,"id=");
718 if ( wpanetid != NULL )
719 {
720 wpanetid +=strlen("id=");
721 if (memcpy(destid,wpanetid,2) != NULL)
722 {
723 *networkid = atoi(destid);
724 RLOGD("get networkid is %d\n",*networkid);
725 }
726 }
727 pReason = strstr(modify, "reason=");
728 if (pReason != NULL)
729 {
730 pReason += strlen("reason=");
731 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
732 {
733 *error = LYNQ_TIME_OUT;
734 }
735 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
736 {
737 *error = LYNQ_PSW_ERROR;
738 }
739 else
740 {
741 *error = LYNQ_UNSPECIFIED_REASON;
742 }
743 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
744 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
745 return;
746 }
747 else
748 {
749 *error = LYNQ_UNSPECIFIED_REASON;
750 *state = LYNQ_WIFI_STA_STATUS_CONNECT_FAIL;
751 return;
752 }
753 }
754 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
755 {
756 wpanetid = strstr(modify,"id=");
757 if ( wpanetid != NULL )
758 {
759 wpanetid +=strlen("id=");
760 if (memcpy(destid,wpanetid,2) != NULL)
761 {
762 *networkid = atoi(destid);
763 RLOGD("get networkid is %d\n",*networkid);
764 }
765 }
766 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
767 pReason = strstr(modify, "status_code=");
768 if (pReason != NULL)
769 {
770 pReason += strlen("status_code=");
771 if (memcmp(pReason, "17", 2) == 0)
772 {
773 *error = LYNQ_AP_UNABLE_HANDLE;
774 }
775 else if (memcmp(pReason, "1",1) == 0)
776 {
777 *error = LYNQ_UNSPECIFIED_REASON;
778 }
779 else
780 {
781 *error = LYNQ_UNSPECIFIED_REASON;
782 }
783 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
784 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
785 return;
786 }
787 else
788 {
789 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
790 *error = LYNQ_UNSPECIFIED_REASON;
791 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
792 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d£¬networkid:%d\n",*state,*error,*networkid);
793 return;
794 }
795 }
796 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
797 {
798 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
799 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
800 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
801 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
802 return;
803 }
804 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
805 {
806 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
807 *error = LYNQ_WAIT_CONNECT_ACTIVE;
808 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
809 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d,networkid:%d\n",*state,*error,*networkid);
810 return;
811 }
812 RLOGD("EVENT : %s\n", modify);
813 *error = LYNQ_UNSPECIFIED_REASON;
814 *state = LYNQ_WIFI_STATUS_EGNORE;
815 RLOGD("LAST : STA state:%d,error:%d,network:%d\n",*state,*error,*networkid);
816 return;
817}
you.chen70f377f2023-04-14 18:17:09 +0800818static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
819{
you.chen6d247052023-06-01 16:39:54 +0800820 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800821 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
822 {
823 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
824 g_sta_callback_func(g_sta_callback_priv, state, error);
825 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
826 }
you.chen6d247052023-06-01 16:39:54 +0800827 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen70f377f2023-04-14 18:17:09 +0800828}
qs.xiongfcc914b2023-07-06 21:16:20 +0800829static void notify_auto_connect_status(lynq_wifi_sta_status_s state, error_number_s error,int networkid)
830{
831 pthread_mutex_lock(&s_sta_callback_mutex);
832 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
833 {
834 RLOGD("STAWatcherThreadProc callback begin ------> %d %d %d\n", state, error,networkid);
835 g_sta_auto_callback_func(g_sta_auto_callback_priv, state, error,networkid);
836 RLOGD("STAAutoWatcherThreadProc callback end ------> %d %d %d\n", state, error,networkid);
837 }
838 pthread_mutex_unlock(&s_sta_callback_mutex);
839}
you.chen70f377f2023-04-14 18:17:09 +0800840
you.chen35020192022-05-06 11:30:57 +0800841static void STAWatcherThreadProc() {
842 size_t len = MAX_RET;
843 char msg_notify[MAX_RET];
you.chen35020192022-05-06 11:30:57 +0800844 error_number_s error;
you.chenc9928582023-04-24 15:39:37 +0800845 lynq_wifi_sta_status_s state, last_state = -1;
you.chenf711c8a2023-04-13 13:49:45 +0800846 int idle_count = 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400847
you.chen6c2dd9c2022-05-16 17:55:28 +0800848 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800849 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800850
you.chen70f377f2023-04-14 18:17:09 +0800851 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiong9fbf74e2023-03-28 13:38:22 +0800852 while (g_sta_watcher_stop_flag == 0)
853 {
you.chenf711c8a2023-04-13 13:49:45 +0800854 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiong455c30b2023-04-12 11:40:02 +0800855 {
you.chen35020192022-05-06 11:30:57 +0800856 continue;
857 }
you.chenf711c8a2023-04-13 13:49:45 +0800858
you.chen6c2dd9c2022-05-16 17:55:28 +0800859 memset(msg_notify, 0, MAX_RET);
860 len = MAX_RET;
qs.xiong9fbf74e2023-03-28 13:38:22 +0800861 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiong455c30b2023-04-12 11:40:02 +0800862 {
you.chen35020192022-05-06 11:30:57 +0800863 msg_notify[len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +0800864 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
865 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiong455c30b2023-04-12 11:40:02 +0800866 {
you.chen35020192022-05-06 11:30:57 +0800867 g_sta_scan_finish_flag = 1;
868 }
869
qs.xiong9fbf74e2023-03-28 13:38:22 +0800870 if (g_sta_callback_func == NULL)
qs.xiong455c30b2023-04-12 11:40:02 +0800871 {
you.chen35020192022-05-06 11:30:57 +0800872 continue;
873 }
qs.xiong455c30b2023-04-12 11:40:02 +0800874 get_state_error(msg_notify,&state,&error);
you.chen70f377f2023-04-14 18:17:09 +0800875 notify_connect_status(state, error);
876
877 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen32cb31e2023-04-13 14:05:45 +0800878 {
you.chen70f377f2023-04-14 18:17:09 +0800879 inner_check_connect_error(msg_notify, state, error);
you.chenc9928582023-04-24 15:39:37 +0800880 if (last_state != state)
881 {
882 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
883 {
884 system_call_v("%s %s", sta_status_change_script, "connect");
885 }
886 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
887 {
888 system_call_v("%s %s", sta_status_change_script, "disconnect");
889 }
890 }
891
892 last_state = state;
you.chen32cb31e2023-04-13 14:05:45 +0800893 }
you.chen35020192022-05-06 11:30:57 +0800894 }
895 }
qs.xiong9fbf74e2023-03-28 13:38:22 +0800896 if (lynq_wpa_ctrl != NULL)
897 {
you.chen92fd5d32022-05-25 10:09:47 +0800898 wpa_ctrl_detach(lynq_wpa_ctrl);
899 wpa_ctrl_close(lynq_wpa_ctrl);
900 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400901}
qs.xiongfcc914b2023-07-06 21:16:20 +0800902static void STAAutoWatcherThreadProc() {
903 size_t len = MAX_RET;
904 char msg_notify[MAX_RET];
905 error_number_s error;
906 lynq_wifi_sta_status_s state, last_state = -1;
907 int idle_count = 0;
908 int networkid;
909 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
910 g_sta_auto_watcher_stop_flag = 0;
911 RLOGD("STAAutoWatcherThreadProc thread started ------");
912 while (g_sta_auto_watcher_stop_flag == 0)
913 {
914 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_auto_watcher_started_flag) != 1)
915 {
916 continue;
917 }
918 memset(msg_notify, 0, MAX_RET);
919 len = MAX_RET;
920 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
921 {
922 msg_notify[len+1] = '\0';
923 RLOGD("STAAutoWatcherThreadProc sta ------> %s\n", msg_notify);
924 if (strstr(msg_notify, state_scan_result) != NULL)
925 {
926 g_sta_auto_scan_finish_flag = 1;
927 }
928 if (g_sta_auto_callback_func == NULL)
929 {
930 continue;
931 }
932 get_state_error_networkid(msg_notify,&state,&error,&networkid); // add net state error network function
933 notify_auto_connect_status(state, error,networkid);
934 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
935 {
936 inner_check_connect_error(msg_notify, state, error);
937 if (last_state != state)
938 {
939 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
940 {
941 system_call_v("%s %s", sta_status_change_script, "connect");
942 }
943 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
944 {
945 system_call_v("%s %s", sta_status_change_script, "disconnect");
946 }
947 }
948 last_state = state;
949 }
950 }
951 }
952 if (lynq_wpa_ctrl != NULL)
953 {
954 wpa_ctrl_detach(lynq_wpa_ctrl);
955 wpa_ctrl_close(lynq_wpa_ctrl);
956 }
957}
qs.xiongf1b525b2022-03-31 00:58:23 -0400958
you.chen70f377f2023-04-14 18:17:09 +0800959// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
960static void GlobalWatcherThreadProc()
961{
962 int ret, connect_timeout, service_abnormal;
963 error_number_s error_num = -1;
964 inner_sta_status_s sta_status;
965 scan_info_s *scan_list = NULL;
966 int i, scan_len=0;
967 char connecting_ssid[64];
968 struct timeval now;
969
970 RLOGD("GlobalWatcherThreadProc start to run");
971
972 while (1)
973 {
974 pthread_mutex_lock(&s_global_check_mutex);
975 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
976 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
977 {
978 pthread_mutex_unlock(&s_global_check_mutex);
979 usleep(50*1000);
980 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
981 continue;
982 }
983
984 connect_timeout = 0;
985 service_abnormal = 0;
986 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
987 {
988 while (1)
989 {
990 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
991 if (ret == ETIME)
992 {
993 connect_timeout = 1;
994 }
995 else if (ret != 0)
996 {
997 gettimeofday(&now,NULL);
998 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
999 {
1000 usleep(SLEEP_TIME_ON_IDLE);
1001 continue;
1002 }
1003 connect_timeout = 1;
1004 }
1005 sta_status = s_sta_status;
1006 error_num = s_sta_error_number;
1007 s_sta_status = INNER_STA_STATUS_INIT;
1008 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
1009 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
1010 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
1011 break;
1012 }
1013 }
1014 if (s_service_invoke_timeout_cnt > 10)
1015 {
1016 service_abnormal = 1;
1017 s_service_invoke_timeout_cnt = 0;
1018 }
1019 pthread_mutex_unlock(&s_global_check_mutex);
1020
1021 if (service_abnormal == 1)
1022 {
1023 sleep(1);
1024 RLOGE("wpa service is abnormal info app to exit");
1025 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
you.chen6d247052023-06-01 16:39:54 +08001026
1027 inner_notify_ap_msg(LYNQ_WIFI_SERVICE_ABNORMAL);
1028
you.chen70f377f2023-04-14 18:17:09 +08001029 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
1030 }
1031
1032 if (sta_status == INNER_STA_STATUS_CANCEL)
1033 {
1034 continue;
1035 }
1036 else if (sta_status == INNER_STA_STATUS_CONNECTED)
1037 {
1038 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1039 }
1040 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
1041 {
1042 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
1043 {
1044 for(i=0; i < scan_len;i++)
1045 {
1046 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
1047 {
1048 error_num = LYNQ_AUTH_ERROR;
1049 break;
1050 }
1051 }
1052 free(scan_list);
1053 }
1054 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1055 }
1056 else if (connect_timeout == 0)
1057 {
1058 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
1059 }
1060 else // wait timeout
1061 {
1062 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
1063 {
1064 ; // wpa service abnormal
1065 }
1066 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
1067 {
1068 RLOGD("GlobalWatcherThreadProc notify connected");
1069 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
1070 }
1071 else
1072 {
1073 RLOGD("GlobalWatcherThreadProc notify timeout");
1074 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
1075 }
1076 }
1077 } // while (1)
1078}
1079
qs.xiong1af5daf2022-03-14 09:12:12 -04001080int lynq_wifi_enable(void)
1081{
you.chen35020192022-05-06 11:30:57 +08001082 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08001083 int i;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001084 RLOGD("enter lynq_wifi_enable");
you.chend2fef3f2023-02-13 10:50:35 +08001085 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
1086
qs.xiong9fbf74e2023-03-28 13:38:22 +08001087 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
1088 {
you.chend2fef3f2023-02-13 10:50:35 +08001089 goto out_enable;
1090 }
1091
you.chenc9928582023-04-24 15:39:37 +08001092 ret = system(start_wg870_service_script);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001093 if (ret != 0)
1094 {
1095 //printf("service state %d\n", ret);
1096 RLOGE("[wifi error]service state %d",ret);
you.chend2fef3f2023-02-13 10:50:35 +08001097 ret = -1;
1098 goto out_enable;
you.chen35020192022-05-06 11:30:57 +08001099 }
lhfe8da902022-10-11 18:55:36 +08001100
you.chen70f377f2023-04-14 18:17:09 +08001101 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
1102 {
1103 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
1104 if(ret<0)
1105 {
1106 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
1107 ret = -1;
1108 goto out_enable;
1109 }
1110 }
1111
you.chend2fef3f2023-02-13 10:50:35 +08001112 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
1113 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
1114 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
1115 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
1116out_enable:
1117 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001118 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001119}
1120
qs.xiong1af5daf2022-03-14 09:12:12 -04001121int lynq_wifi_disable(void)
1122{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001123 RLOGD("enter lynq_wifi_disable");
you.chend2fef3f2023-02-13 10:50:35 +08001124 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen35020192022-05-06 11:30:57 +08001125 g_ap_watcher_stop_flag = 1;
1126 g_sta_watcher_stop_flag = 1;
qs.xiongfcc914b2023-07-06 21:16:20 +08001127 g_sta_auto_watcher_stop_flag = 1;
you.chen35020192022-05-06 11:30:57 +08001128 if (g_ap_watcher_pid != 0)
1129 pthread_join(g_ap_watcher_pid, NULL);
1130 if (g_sta_watcher_pid != 0)
1131 pthread_join(g_sta_watcher_pid, NULL);
qs.xiongfcc914b2023-07-06 21:16:20 +08001132 if (g_sta_auto_watcher_pid != 0)
1133 pthread_join(g_sta_auto_watcher_pid, NULL);
you.chen35020192022-05-06 11:30:57 +08001134 if (g_lynq_wpa_ctrl[0] != NULL)
1135 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
1136 if (g_lynq_wpa_ctrl[1] != NULL)
1137 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
1138 g_ap_watcher_pid = 0;
1139 g_sta_watcher_pid = 0;
qs.xiongfcc914b2023-07-06 21:16:20 +08001140 g_sta_auto_watcher_pid = 0;
you.chen35020192022-05-06 11:30:57 +08001141 g_lynq_wpa_ctrl[0] = NULL;
1142 g_lynq_wpa_ctrl[1] = NULL;
1143 system("systemctl stop wg870_drv_insmod.service");
you.chend2fef3f2023-02-13 10:50:35 +08001144 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
1145 return 0;
1146}
1147
1148static inline char inner_convert_char(char in)
1149{
1150 if (in >= '0' && in <= '9')
1151 {
1152 return in - '0';
1153 }
1154 else if (in >= 'a' && in <= 'f')
1155 {
1156 return in - 'a' + 10;
1157 }
1158 else if (in >= 'A' && in <= 'F')
1159 {
1160 return in - 'A' + 10;
1161 }
1162 else
1163 {
1164 return '\xff';
1165 }
1166}
1167
1168static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
1169{
1170 char *p;
1171 size_t pos = 0;
1172 if (NULL == out_ssid)
1173 return;
1174 //printf("input ssid=[%s]\n", ssid);
1175 memset(out_ssid, 0, out_ssid_len);
1176 if (NULL == ssid)
1177 return;
1178 p = strchr(ssid, '\\');
1179 if (NULL == p)
1180 {
1181 strncpy(out_ssid, ssid, out_ssid_len);
1182 //printf(" first %s\n", out_ssid);
1183 }
1184 else
1185 {
1186 pos = p - ssid;
1187 memcpy(out_ssid, ssid, pos);
1188 //printf("pos %lu -- %s\n", pos, out_ssid);
1189 for(; pos < out_ssid_len; pos ++)
1190 {
1191 if (p[0] == '\0')
1192 {
1193 //printf(" out %s\n", out_ssid);
1194 return;
1195 }
1196 else if (p[0] != '\\')
1197 {
1198 out_ssid[pos] = p[0];
1199 p += 1;
1200 }
1201 else if (p[1] == 'x' || p[1] == 'X')
1202 {
1203 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
1204 p += 4;
1205 }
1206 else if (p[1] == '\\')
1207 {
1208 out_ssid[pos] = '\\';
1209 p += 2;
1210 }
1211 else if (p[1] == 't')
1212 {
1213 out_ssid[pos] = '\t';
1214 p += 2;
1215 }
1216 else if (p[1] == 'r')
1217 {
1218 out_ssid[pos] = '\r';
1219 p += 2;
1220 }
1221 else if (p[1] == 'n')
1222 {
1223 out_ssid[pos] = '\n';
1224 p += 2;
1225 }//todo find a better way to convert?
1226 }
1227 }
1228 //printf(" out %s\n", out_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001229}
qs.xiong1af5daf2022-03-14 09:12:12 -04001230
you.chen35020192022-05-06 11:30:57 +08001231static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chend2fef3f2023-02-13 10:50:35 +08001232 int i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08001233 char lynq_cmd_get[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001234 RLOGD("enter inner_get_param");
1235 if (out_put == NULL)
1236 {
1237 RLOGE("output ptr is null");
you.chen35020192022-05-06 11:30:57 +08001238 return -1;
1239 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001240 if (param_name == NULL)
1241 {
1242 RLOGE("param ptr is null");
you.chen35020192022-05-06 11:30:57 +08001243 return -1;
1244 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001245 if (param_name[0] == '\0')
1246 {
1247 RLOGE("param is empty");
you.chen35020192022-05-06 11:30:57 +08001248 return -1;
1249 }
1250
1251 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
1252
1253 CHECK_WPA_CTRL(interface);
1254
1255 DO_REQUEST(lynq_cmd_get);
1256
qs.xiong9fbf74e2023-03-28 13:38:22 +08001257 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1258 {
1259 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen35020192022-05-06 11:30:57 +08001260 return -1;
1261 }
1262
you.chena6fa5b22022-05-18 10:28:19 +08001263// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chend2fef3f2023-02-13 10:50:35 +08001264 if (strcmp(param_name, "ssid") == 0)
1265 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001266 if (cmd_reply[0] == '\"')
1267 {
you.chend2fef3f2023-02-13 10:50:35 +08001268 ssid_len = reply_len - 1;
1269 memcpy(out_put, cmd_reply + 1, ssid_len);
1270 if (out_put[ssid_len-1] == '\"')
1271 {
1272 out_put[ssid_len-1] = '\0';
1273 }
1274 else
1275 {
1276 out_put[ssid_len] = '\0';
1277 }
1278 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001279 else
1280 {
you.chend2fef3f2023-02-13 10:50:35 +08001281 ssid_len = reply_len / 2;
1282 for(i=0; i<ssid_len; i++)
1283 {
1284 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
1285 }
1286 out_put[ssid_len] = '\0';
1287 }
1288 }
1289 else
1290 {
1291 memcpy(out_put, cmd_reply, reply_len + 1);
1292 }
you.chen35020192022-05-06 11:30:57 +08001293 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001294}
qs.xiong1af5daf2022-03-14 09:12:12 -04001295
you.chen35020192022-05-06 11:30:57 +08001296static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1297 int ret = 0;
1298 char * end = str + len - 1;
1299 results[ret++] = str;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001300 while(str < end)
1301 {
1302 if (*str == delimiter)
1303 {
you.chen35020192022-05-06 11:30:57 +08001304 *str++ = '\0';
1305 results[ret++] = str;
1306 continue;
1307 }
1308 str++;
1309 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001310 if (*str == delimiter)
1311 {
you.chen35020192022-05-06 11:30:57 +08001312 *str = '\0';
1313 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001314
you.chen6ed36a62023-04-27 17:51:56 +08001315 results[ret] = NULL;
1316
you.chen35020192022-05-06 11:30:57 +08001317 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001318}
1319
you.chend2fef3f2023-02-13 10:50:35 +08001320static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1321{
1322 char * p;
1323 int ret = 0;
1324 char cmd[256]={0};
1325 if (NULL == mac || NULL == ip)
you.chen35020192022-05-06 11:30:57 +08001326 return -1;
you.chend2fef3f2023-02-13 10:50:35 +08001327 memset(ip, 0, ip_len);
qs.xiong08971432023-07-05 19:17:34 +08001328 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | awk '{print $1}' | grep -v \":\" | head -1", mac);
you.chend2fef3f2023-02-13 10:50:35 +08001329 ret = exec_cmd(cmd, ip, ip_len);
1330 p = strchr(ip, '\n');
1331 if (NULL != p)
1332 {
1333 *p = '\0';
you.chen35020192022-05-06 11:30:57 +08001334 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001335 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen35020192022-05-06 11:30:57 +08001336 return ret;
1337}
1338
you.chend2fef3f2023-02-13 10:50:35 +08001339static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen35020192022-05-06 11:30:57 +08001340 struct in_addr addr ={0};
1341 struct hostent *ht;
you.chen186d3c32023-05-18 14:19:46 +08001342 char cmd[64] = {0};
1343 char * p;
1344 int ret;
you.chen35020192022-05-06 11:30:57 +08001345
qs.xiong9fbf74e2023-03-28 13:38:22 +08001346 if (ip == NULL || *ip == '\0' || hostname == NULL)
1347 {
1348 RLOGE("ip == NULL or hostname == NULL");
1349 return -1;
you.chen35020192022-05-06 11:30:57 +08001350 }
1351
you.chend2fef3f2023-02-13 10:50:35 +08001352 *hostname = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001353 if (inet_aton(ip, &addr) == 0)
1354 {
you.chen35020192022-05-06 11:30:57 +08001355 printf("---inet_aton fail\n");
1356 return -1;
1357 }
1358
1359 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1360
qs.xiong9fbf74e2023-03-28 13:38:22 +08001361 if (ht == NULL)
1362 {
you.chen186d3c32023-05-18 14:19:46 +08001363 hostname[0] = '\0';
1364 sprintf(cmd, "grep -F '%s' /run/wg870/ap0.lease | awk '{print $4}' | tail -1", ip);
1365 ret = exec_cmd(cmd, hostname, 32);
1366 if (ret == 0)
1367 {
1368 p = strchr(hostname, '\n');
1369 if (p != NULL)
1370 {
1371 *p = '\0';
1372 }
1373 return 0;
qs.xiong6d64b542023-07-26 17:09:22 +08001374 }else{
1375 usleep( 10 * 1000);
1376 ret = exec_cmd(cmd, hostname, 32);
1377 if( ret == 0)
1378 {
1379 p= strchr(hostname, '\n');
1380 if(p != NULL )
1381 {
1382 *p = '\0';
1383 }
1384 return 0;
1385 }
you.chen186d3c32023-05-18 14:19:46 +08001386 }
1387 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001388 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001389 herror(NULL);
1390 return -1;
1391 }
1392
1393 strcpy(hostname, ht->h_name);
1394
1395 return 0;
1396}
1397
1398static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1399{
1400 int count, index, words_count;
1401 char * split_lines[128]= {0};
1402 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001403 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001404 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001405 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001406
1407 CHECK_WPA_CTRL(ap_sta);
1408
1409 DO_REQUEST(lynq_wifi_list_networks);
1410
1411 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1412
1413 //@todo check ssid field to compatible
1414
1415 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001416 for(index=1; index < count; index++)
1417 {
you.chen35020192022-05-06 11:30:57 +08001418 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001419 if (words_count > 2)
1420 {
you.chend2fef3f2023-02-13 10:50:35 +08001421 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001422 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1423 {
you.chen35020192022-05-06 11:30:57 +08001424 net_no_list[ret++] = atoi(split_words[0]);
1425 }
1426 }
1427 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001428 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001429 return ret;
1430}
1431
1432static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001433 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001434 CHECK_WPA_CTRL(ap_sta);
1435 const char *lynq_wifi_add_network = "ADD_NETWORK";
1436
qs.xiong9fbf74e2023-03-28 13:38:22 +08001437 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001438 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001439 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1440 {
1441 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001442 return -1;
1443 }
1444
qs.xiong9fbf74e2023-03-28 13:38:22 +08001445 for(i=0;i<reply_len;i++)
1446 {
1447 if(cmd_reply[i] == '\n')
1448 {
you.chen35020192022-05-06 11:30:57 +08001449 cmd_reply[i] = '\0';
1450 break;
1451 }
1452 }
1453 return atoi(cmd_reply);
1454}
you.chena6cd55a2022-05-08 12:20:18 +08001455
you.chen35020192022-05-06 11:30:57 +08001456static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1457{
1458 int count, index;
1459 int net_no_list[128];
1460
qs.xiong9fbf74e2023-03-28 13:38:22 +08001461 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001462 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001463 for (index=0; index < count; index++)
1464 {
1465 if (net_no_list[index] == net_no)
1466 {
you.chen35020192022-05-06 11:30:57 +08001467 return 0;
1468 }
1469 }
1470
1471 if (count >= 1)
1472 index = net_no_list[count - 1];
1473 else
1474 index = -1;
1475
qs.xiong9fbf74e2023-03-28 13:38:22 +08001476 while (index < net_no )
1477 {
you.chen35020192022-05-06 11:30:57 +08001478 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001479 if (index >= net_no)
1480 { // required network no created
1481 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001482 return 0;
1483 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001484 else if( index < 0)
1485 {
1486 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001487 return -1;
1488 }
you.chen35020192022-05-06 11:30:57 +08001489 }
1490
1491 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001492 {
1493 RLOGE("[lynq_check_network_number] network index < 0");
1494 return -1;
1495 }
1496 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001497 return 0;
1498}
1499
1500static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001501 if (freq > 5000 && freq < 6000)
1502 {
you.chen35020192022-05-06 11:30:57 +08001503 return LYNQ_WIFI_5G_band;
1504 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001505 else if (freq > 2000 && freq < 3000)
1506 {
you.chen35020192022-05-06 11:30:57 +08001507 return LYNQ_WIFI_2G_band;
1508 }
1509 return LYNQ_WIFI_2_and_5G_band;
1510}
1511
1512static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001513 if (key_mgmt != NULL)
1514 {
1515 if (memcmp( key_mgmt, "NONE", 4) == 0)
1516 {
you.chen35020192022-05-06 11:30:57 +08001517 return LYNQ_WIFI_AUTH_OPEN;
1518 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001519 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1520 {
you.chen35020192022-05-06 11:30:57 +08001521 return LYNQ_WIFI_AUTH_WEP;
1522 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001523 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1524 {
you.chen35020192022-05-06 11:30:57 +08001525 return LYNQ_WIFI_AUTH_WPA_PSK;
1526 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001527 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1528 {
you.chen35020192022-05-06 11:30:57 +08001529 return LYNQ_WIFI_AUTH_WPA2_PSK;
1530 }
1531 }
1532
1533 return -1;
1534}
1535
1536static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001537 if (flag != NULL)
1538 {
qs.xiong46f41562023-07-11 21:06:47 +08001539 if ( strstr(flag, "SHA256") != NULL || strstr(flag,"WPA2-SAE") != NULL || ( strstr(flag,"SAE-H2E") != NULL && strstr(flag,"WPS") == NULL ) )
qs.xiong3e506812023-04-06 11:08:48 +08001540 {
1541 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001542 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001543 {
1544 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1545 }else if (strstr( flag, "WPA2-PSK") != NULL)
1546 {
you.chen35020192022-05-06 11:30:57 +08001547 return LYNQ_WIFI_AUTH_WPA2_PSK;
1548 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001549 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001550 {
you.chen35020192022-05-06 11:30:57 +08001551 return LYNQ_WIFI_AUTH_WPA_PSK;
1552 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001553 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001554 {
you.chen35020192022-05-06 11:30:57 +08001555 return LYNQ_WIFI_AUTH_WEP;
1556 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001557 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001558 {
you.chen35020192022-05-06 11:30:57 +08001559 return LYNQ_WIFI_AUTH_OPEN;
1560 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001561 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001562 {
you.chend2fef3f2023-02-13 10:50:35 +08001563 return LYNQ_WIFI_AUTH_OPEN;
1564 }
qs.xiong46f41562023-07-11 21:06:47 +08001565 else
1566 {
1567 RLOGD("convert_max_auth_from_flag not-found auth mode");
1568 }
you.chen35020192022-05-06 11:30:57 +08001569 }
1570
1571 return -1;
1572}
1573
1574static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1575 switch (bw) {
1576 case 10:
1577 return LYNQ_WIFI_BANDWIDTH_HT10;
1578 break;
1579 case 20:
1580 return LYNQ_WIFI_BANDWIDTH_HT20;
1581 break;
1582 case 40:
1583 return LYNQ_WIFI_BANDWIDTH_HT40;
1584 break;
1585 case 80:
1586 return LYNQ_WIFI_BANDWIDTH_HT80;
1587 break;
1588 default:
1589 break;
1590 }
1591
1592 return -1;
1593}
1594
you.chen70f377f2023-04-14 18:17:09 +08001595static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001596static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1597 int i, count;
1598 char *p;
1599 const char *lynq_status_cmd = "STATUS";
1600 const char * FLAG_SSID = "ssid=";
1601 const char * FLAG_SBSID = "bssid=";
1602 const char * FLAG_KEY_MGMT = "key_mgmt=";
1603 const char * FLAG_FREQ = "freq=";
1604 const char * FLAG_STATE = "wpa_state=";
1605 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001606 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001607 char *split_lines[128] = {0};
1608
1609 CHECK_WPA_CTRL(interface);
1610
qs.xiong9fbf74e2023-03-28 13:38:22 +08001611 if (curr_state == NULL)
1612 {
1613 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001614 return -1;
1615 }
1616
1617 DO_REQUEST(lynq_status_cmd);
1618
1619 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1620
1621 curr_state->net_no = -1;
1622 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001623 for(i=0; i < count; i++)
1624 {
1625 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001626 {
you.chen35020192022-05-06 11:30:57 +08001627 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001628 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001629 {
you.chend2fef3f2023-02-13 10:50:35 +08001630 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001631 ret = 0;
1632 continue;
1633 }
you.chenf58b3c92022-06-21 16:53:48 +08001634 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001635 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001636 {
you.chend2fef3f2023-02-13 10:50:35 +08001637 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 +08001638 ret = 0;
1639 continue;
1640 }
you.chen35020192022-05-06 11:30:57 +08001641 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001642 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001643 {
you.chen450d0172022-07-15 17:56:48 +08001644 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001645 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001646 ret = 0;
1647 continue;
1648 }
1649 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001650 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001651 {
you.chen35020192022-05-06 11:30:57 +08001652 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1653 ret = 0;
1654 continue;
1655 }
you.chend2fef3f2023-02-13 10:50:35 +08001656 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001657 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001658 {
you.chend2fef3f2023-02-13 10:50:35 +08001659 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1660 ret = 0;
1661 continue;
1662 }
you.chen35020192022-05-06 11:30:57 +08001663 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001664 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001665 {
you.chen35020192022-05-06 11:30:57 +08001666 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001667 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001668 {
you.chen35020192022-05-06 11:30:57 +08001669 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1670 ret = 0;
1671 continue;
1672 }
1673
1674 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001675 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001676 {
you.chen35020192022-05-06 11:30:57 +08001677 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001678 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001679 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001680 }
1681 }
1682
you.chen70f377f2023-04-14 18:17:09 +08001683 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1684 {
1685 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1686 }
1687
you.chen35020192022-05-06 11:30:57 +08001688 return ret;
1689}
1690
qs.xiongf1b525b2022-03-31 00:58:23 -04001691int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001692{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001693 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001694 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001695
qs.xiong9fbf74e2023-03-28 13:38:22 +08001696 if (ap_ssid == NULL)
1697 {
1698 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001699 return -1;
1700 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001701 else
1702 {
1703 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001704 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001705
qs.xiong9fbf74e2023-03-28 13:38:22 +08001706 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1707 {
1708 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001709 return -1;
1710 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001711
you.chen35020192022-05-06 11:30:57 +08001712 CHECK_IDX(idx, CTRL_AP);
1713
1714 CHECK_WPA_CTRL(CTRL_AP);
1715
1716 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1717
1718 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1719 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001720 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1721 return 0;
you.chen35020192022-05-06 11:30:57 +08001722
qs.xiong7a105ce2022-03-02 09:43:11 -05001723}
1724
you.chen35020192022-05-06 11:30:57 +08001725int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001726{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001727 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001728 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001729 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001730}
1731
qs.xiongc9c79f72022-10-17 15:27:18 +08001732/*****
1733 *frequency <------>channel
1734 *
1735 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1736 *
1737 *
1738 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1739 *
1740 *
1741 * */
1742static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001743 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};
1744 int i;
1745 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1746
qs.xiong69a332b2022-12-02 09:58:57 +08001747 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001748 {
1749 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001750 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001751 }
qs.xiongc00b6032022-11-29 16:28:03 +08001752
1753 if(i == arr_len)
1754 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001755 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001756 return -1;
1757 }
qs.xiongc00b6032022-11-29 16:28:03 +08001758
qs.xiongc9c79f72022-10-17 15:27:18 +08001759 return 0;
1760}
qs.xiong13673462023-02-21 19:12:54 +08001761
1762static int lynq_check_frequencyby_country_code(int input_frequency)
1763{
1764 char str_cnc[]="CN";
1765 char str_dest[20]="";
1766
1767 if( lynq_get_country_code(1,str_dest) != 0 )
1768 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001769 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001770 return -1;
1771 }
1772 if( strncmp(str_dest,str_cnc,2) != 0 )
1773 {
1774 return 0;
1775 }else if( 2473 < input_frequency && input_frequency < 5744)
1776 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001777 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08001778 return -1;
1779 }
1780 return 0;
1781}
qs.xiongf1b525b2022-03-31 00:58:23 -04001782int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001783{
qs.xiongc00b6032022-11-29 16:28:03 +08001784 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08001785 char lynq_wifi_frequency_cmd[128]={0};
1786 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08001787 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001788 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001789 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08001790 check = lynq_check_set_frequency(lynq_wifi_frequency);
1791 if(check != 0)
1792 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001793 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08001794 return -1;
you.chen35020192022-05-06 11:30:57 +08001795 }
qs.xiong13673462023-02-21 19:12:54 +08001796 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
1797 if(check != 0)
1798 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001799 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08001800 return -1;
1801 }
1802
qs.xiongc00b6032022-11-29 16:28:03 +08001803 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1804 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001805 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08001806 return -1;
1807 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001808
you.chen35020192022-05-06 11:30:57 +08001809 CHECK_IDX(idx, CTRL_AP);
1810
1811 CHECK_WPA_CTRL(CTRL_AP);
1812
1813 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
1814 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
1815 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
1816
you.chen6c2dd9c2022-05-16 17:55:28 +08001817 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08001818 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
1819 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
1820 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001821
qs.xiong9fbf74e2023-03-28 13:38:22 +08001822 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001823}
1824
qs.xiongf1b525b2022-03-31 00:58:23 -04001825int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001826{
you.chen35020192022-05-06 11:30:57 +08001827 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001828 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08001829 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04001830
qs.xiong9fbf74e2023-03-28 13:38:22 +08001831 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
1832 {
1833 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08001834 return -1;
1835 }
1836 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04001837
qs.xiong9fbf74e2023-03-28 13:38:22 +08001838 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001839}
1840
qs.xiongf1b525b2022-03-31 00:58:23 -04001841int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
1842{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001843 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08001844 CHECK_IDX(idx, CTRL_AP);
1845 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001846 case LYNQ_WIFI_BANDWIDTH_HT10:
1847 {
1848 RLOGE("bandwith [%d] not support now\n", bandwidth);
1849 return -1;
1850 }
1851 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08001852 {
1853 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
1854 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;
1862 }
1863 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001864 {
qs.xiong10379192023-02-21 13:19:42 +08001865 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08001866 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1867 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001868 if (system(lynq_cmd_bandwith) != 0 )
1869 {
1870 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001871 return -1;
1872 }
1873 system("wl up");
1874 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001875 }
you.chen35020192022-05-06 11:30:57 +08001876 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001877 {
qs.xiong10379192023-02-21 13:19:42 +08001878 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08001879 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001880 if (system(lynq_cmd_bandwith) != 0 )
1881 {
1882 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001883 return -1;
1884 }
1885 system("wl up");
1886 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001887 }
1888 default:
you.chen35020192022-05-06 11:30:57 +08001889 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001890 RLOGE("auth type [%d] not support now\n", bandwidth);
1891 return -1;
you.chen35020192022-05-06 11:30:57 +08001892 }
1893 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001894
1895
you.chen35020192022-05-06 11:30:57 +08001896 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001897}
you.chen35020192022-05-06 11:30:57 +08001898
qs.xiongf1b525b2022-03-31 00:58:23 -04001899int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1900{
you.chen35020192022-05-06 11:30:57 +08001901 int count = 0;
1902 int index = 0;
1903 char *split_words[128] = {0};
1904 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001905 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001906 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001907
you.chen35020192022-05-06 11:30:57 +08001908 CHECK_WPA_CTRL(CTRL_AP);
1909
1910 DO_REQUEST(lynq_chanspec_cmd);
1911
1912 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1913 for(;index < count; index++) {
1914 if (strncmp(split_words[index], "bw", 2) != 0) {
1915 continue;
1916 }
1917
1918 index++;
1919 if (index >= count) {
1920 return -1;
1921 }
1922
qs.xiong9fbf74e2023-03-28 13:38:22 +08001923 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001924 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1925 return 0;
1926 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001927 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001928 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001929}
qs.xiong0fb469a2022-04-14 03:50:45 -04001930
qs.xiongf1b525b2022-03-31 00:58:23 -04001931int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001932{
you.chen35020192022-05-06 11:30:57 +08001933 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001934 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08001935 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04001936
you.chen35020192022-05-06 11:30:57 +08001937 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04001938
qs.xiong9fbf74e2023-03-28 13:38:22 +08001939 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1940 {
you.chen35020192022-05-06 11:30:57 +08001941 return -1;
1942 }
1943
1944 system("wl down");
1945 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001946 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08001947 return -1;
1948 }
1949 system("wl up");
1950 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001951}
qs.xiong0fb469a2022-04-14 03:50:45 -04001952
qs.xiongf1b525b2022-03-31 00:58:23 -04001953int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001954{
you.chen35020192022-05-06 11:30:57 +08001955 int count = 0;
1956 int index = 0;
1957 char *split_words[128] = {0};
1958 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001959 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08001960 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001961
you.chen35020192022-05-06 11:30:57 +08001962 CHECK_WPA_CTRL(CTRL_AP);
1963
1964 DO_REQUEST(lynq_chanspec_cmd);
1965
1966 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001967 for(;index < count; index++)
1968 {
1969 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001970 if (strncmp(split_words[index], "channel", 2) != 0) {
1971 continue;
1972 }
1973
1974 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001975 if (index >= count)
1976 {
you.chen35020192022-05-06 11:30:57 +08001977 return -1;
1978 }
1979
1980 *channel = atoi(split_words[index]);
1981 return 0;
1982 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001983 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08001984 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001985}
1986
1987
you.chen35020192022-05-06 11:30:57 +08001988int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001989{
you.chen6c2dd9c2022-05-16 17:55:28 +08001990 char ssid[MAX_CMD] = {0};
1991 int freq = 0;
1992 char lynq_auth_cmd[64]={0};
1993 char lynq_auth_alg_cmd[64]={0};
1994 char lynq_psk_cmd[64]={0};
1995 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001996 char lynq_ieee80211_cmd[64]={0};
1997 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08001998 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001999 CHECK_IDX(idx, CTRL_AP);
2000
you.chen6c2dd9c2022-05-16 17:55:28 +08002001 CHECK_WPA_CTRL(CTRL_AP);
2002
qs.xiong9fbf74e2023-03-28 13:38:22 +08002003 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
2004 {
2005 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08002006 return -1;
2007 }
2008
you.chen92fd5d32022-05-25 10:09:47 +08002009 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002010 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08002011 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002012 return 0;
2013 }
2014 else {
2015 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2016 ssid[0] = '\0';
2017 }
2018 lynq_wifi_ap_frequency_get(idx, &freq);
2019
2020 DO_OK_FAIL_REQUEST(cmd_disconnect);
2021 DO_OK_FAIL_REQUEST(cmd_remove_all);
2022 if (ssid[0] != '\0') {
2023 lynq_wifi_ap_ssid_set(idx, ssid);
2024 }
2025 if (freq != 0) {
2026 lynq_wifi_ap_frequency_set(idx, freq);
2027 }
2028 }
2029 }
you.chen35020192022-05-06 11:30:57 +08002030
qs.xiong9fbf74e2023-03-28 13:38:22 +08002031 switch(auth){
2032 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002033 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002034 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002035 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002036 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002037 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002038 break;
2039 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002040 case LYNQ_WIFI_AUTH_WEP:
2041 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002042 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002043 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002044 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002045 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2046
2047 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2048 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2049 break;
2050 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002051 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002052 {
2053 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2054 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2055 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2056
2057 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2058 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2059 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2060 break;
2061
2062 }
you.chen35020192022-05-06 11:30:57 +08002063 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002064 {
2065 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2066 {
you.chen35020192022-05-06 11:30:57 +08002067 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2068 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2069 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002070 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2071 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002072 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002073 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002074 }
2075// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2076// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2077 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002078
you.chen35020192022-05-06 11:30:57 +08002079 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2080 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2081 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002082 break;
2083 }
2084 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2085 {
2086 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2087 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2088 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2089 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2090
2091 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2092 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2093 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2094 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2095 break;
2096 }
2097 case LYNQ_WIFI_AUTH_WPA3_PSK:
2098 {
2099 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2100 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002101 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002102 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2103
2104 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2105 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2106 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2107 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2108 break;
2109 }
2110 default:
you.chen35020192022-05-06 11:30:57 +08002111 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002112 RLOGE("auth type [%d] not support now\n", auth);
2113 return -1;
you.chen35020192022-05-06 11:30:57 +08002114 }
2115 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002116 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002117
qs.xiong9fbf74e2023-03-28 13:38:22 +08002118 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002119}
2120
you.chen35020192022-05-06 11:30:57 +08002121int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002122{
you.chen35020192022-05-06 11:30:57 +08002123 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002124 char lynq_auth_alg_str[MAX_RET] = {0};
2125 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002126 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002127 CHECK_IDX(idx, CTRL_AP);
2128
qs.xiong9fbf74e2023-03-28 13:38:22 +08002129 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2130 {
2131 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002132 return -1;
2133 }
2134
qs.xiong9fbf74e2023-03-28 13:38:22 +08002135 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2136 {
2137 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2138 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002139 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002140 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002141 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002142 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002143 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2144 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002145 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002146 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002147 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002148 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002149 else
2150 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002151 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002152 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002153 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002154 }
you.chen35020192022-05-06 11:30:57 +08002155 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002156 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2157 {
2158 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2159 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002160 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002161 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002162 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002163 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2164 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002165 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002166 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002167 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002168 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002169 else
2170 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002171 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002172 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002173 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002174 }
you.chen35020192022-05-06 11:30:57 +08002175 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002176
2177 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2178 {
2179 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2180 return -1;
2181 }
2182
2183 if (memcmp(lynq_auth_str,"1",1) == 0 )
2184 {
2185 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2186 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002187 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002188 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2189 {
2190 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2191 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002192 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002193 }
2194 else
2195 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002196 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002197 *auth = -1;
2198 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002199
you.chen6c2dd9c2022-05-16 17:55:28 +08002200 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002201}
qs.xiong1af5daf2022-03-14 09:12:12 -04002202
you.chenb95401e2023-05-12 19:39:06 +08002203static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2204{
2205 char status[64];
you.chencba13492023-05-19 13:53:43 +08002206 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002207 curr_status_info curr_state;
2208
2209 CHECK_WPA_CTRL(CTRL_AP);
2210
2211 memset(status, 0, sizeof (status));
2212
2213 curr_state.ap = NULL;
2214 curr_state.state = status;
2215
2216 printf("inner_check_ap_connected %d\n", retry_count);
2217 usleep(500*1000);
2218 if (0 == inner_get_status_info(idx, &curr_state))
2219 {
2220 if (strcmp(status, STATE_COMPLETED) == 0)
2221 {
2222 return 0;
2223 }
2224 else if (retry_count == 4) //not ok in 2s, do reconnect
2225 {
2226 DO_REQUEST("RECONNECT");
2227 return inner_check_ap_connected(idx, retry_count+1);
2228 }
you.chencba13492023-05-19 13:53:43 +08002229 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002230 {
2231 printf("retry 10 time\n");
2232 return -1;
2233 }
2234 else
2235 {
you.chen6d247052023-06-01 16:39:54 +08002236 if (strcmp(status, STATE_DISCONNECTED) == 0)
2237 {
2238 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2239 DO_REQUEST(LYNQ_WIFI_CMD);
2240 }
you.chenb95401e2023-05-12 19:39:06 +08002241 return inner_check_ap_connected(idx, retry_count+1);
2242 }
2243 }
2244 return -1;
2245}
qs.xiong1af5daf2022-03-14 09:12:12 -04002246
qs.xiongf1b525b2022-03-31 00:58:23 -04002247int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002248{
you.chen35020192022-05-06 11:30:57 +08002249 char LYNQ_WIFI_CMD[128]={0};
2250 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2251 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002252 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002253 CHECK_IDX(idx, CTRL_AP);
2254
2255 CHECK_WPA_CTRL(CTRL_AP);
2256
you.chen0df3e7e2023-05-10 15:56:26 +08002257 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002258 {
you.chen0df3e7e2023-05-10 15:56:26 +08002259 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002260 return -1;
2261 }
you.chen35020192022-05-06 11:30:57 +08002262
2263 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2264 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2265
2266 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2267 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2268
you.chenc9928582023-04-24 15:39:37 +08002269 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2270 if (ret != 0)
2271 {
2272 RLOGE("lynq_wifi_ap_start excute script fail");
2273 return -1;
2274 }
2275
you.chenb95401e2023-05-12 19:39:06 +08002276 if (inner_check_ap_connected(idx, 0) != 0)
2277 {
2278 return -1;
2279 }
2280
you.chen0df3e7e2023-05-10 15:56:26 +08002281 check_tether_and_notify();
2282
qs.xiong9fbf74e2023-03-28 13:38:22 +08002283 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002284}
2285
qs.xiongf1b525b2022-03-31 00:58:23 -04002286int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002287{
you.chen35020192022-05-06 11:30:57 +08002288 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002289}
2290
qs.xiongf1b525b2022-03-31 00:58:23 -04002291int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002292{
you.chen35020192022-05-06 11:30:57 +08002293 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002294
you.chen35020192022-05-06 11:30:57 +08002295 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002296
you.chen35020192022-05-06 11:30:57 +08002297 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002298
you.chen35020192022-05-06 11:30:57 +08002299 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2300
2301 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2302
you.chenb4b121c2022-05-06 17:50:16 +08002303// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002304
2305 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2306 if (ret != 0)
2307 {
2308 RLOGE("lynq_wifi_ap_start excute script fail");
2309 return -1;
2310 }
2311
qs.xiong9fbf74e2023-03-28 13:38:22 +08002312 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002313}
qs.xiong1af5daf2022-03-14 09:12:12 -04002314
qs.xiongf1b525b2022-03-31 00:58:23 -04002315int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002316{
you.chen35020192022-05-06 11:30:57 +08002317 char lynq_disable_cmd[128] = {0};
2318 char lynq_select_cmd[128] = {0};
2319 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002320 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002321 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002322
you.chen35020192022-05-06 11:30:57 +08002323 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002324 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2325 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2326
2327 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2328 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2329 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002330
qs.xiong9fbf74e2023-03-28 13:38:22 +08002331 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002332}
2333
qs.xiongf1b525b2022-03-31 00:58:23 -04002334int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002335{
you.chen35020192022-05-06 11:30:57 +08002336 char lynq_disable_cmd[128] = {0};
2337 char lynq_select_cmd[128] = {0};
2338 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002339 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002340 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002341
you.chen35020192022-05-06 11:30:57 +08002342 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002343
you.chen35020192022-05-06 11:30:57 +08002344 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2345 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2346
2347 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2348 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2349 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002350
qs.xiong9fbf74e2023-03-28 13:38:22 +08002351 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002352}
qs.xiongf1b525b2022-03-31 00:58:23 -04002353
you.chen35020192022-05-06 11:30:57 +08002354int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002355{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002356 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002357 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002358 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002359 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002360 RLOGD("enter lynq_ap_password_set");
2361 if( password == NULL )
2362 {
2363 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002364 return -1;
2365 }
2366 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002367 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002368 if(pass_len < 8 || pass_len >= 64)
2369 {
2370 RLOGE("[lynq_ap_password_set]input password len not in rage");
2371 return -1;
you.chen35020192022-05-06 11:30:57 +08002372 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002373
you.chen35020192022-05-06 11:30:57 +08002374 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002375
qs.xiong9fbf74e2023-03-28 13:38:22 +08002376 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2377 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002378 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002379 return -1;
2380 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002381 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2382 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002383 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2384 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002385 }
2386
you.chen35020192022-05-06 11:30:57 +08002387 CHECK_WPA_CTRL(CTRL_AP);
2388
qs.xiong9fbf74e2023-03-28 13:38:22 +08002389 if (auth == LYNQ_WIFI_AUTH_WEP)
2390 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002391 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002392 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2393 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2394 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2395 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2396 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002397 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2398 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002399 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 +08002400 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2401 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2402 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002403 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2404 {
2405
qs.xiongc8d92a62023-03-29 17:36:14 +08002406 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 +08002407 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002408 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002409 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2410 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2411
2412 }
2413 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002414 {
2415 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002416 return -1;
2417 }
you.chen35020192022-05-06 11:30:57 +08002418
you.chen35020192022-05-06 11:30:57 +08002419 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002420
qs.xiong9fbf74e2023-03-28 13:38:22 +08002421 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002422}
2423
you.chen35020192022-05-06 11:30:57 +08002424int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002425{
you.chen35020192022-05-06 11:30:57 +08002426 FILE * fp;
2427 int len, ret;
2428 int count, index;
2429 char *split_lines[128] = {0};
2430 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002431 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002432
you.chen35020192022-05-06 11:30:57 +08002433 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002434
you.chen35020192022-05-06 11:30:57 +08002435 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2436// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002437 if (NULL == fp)
2438 {
2439 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002440 return -1;
2441 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002442
you.chen35020192022-05-06 11:30:57 +08002443 buff = alloca(MAX_RET);
2444 fseek(fp, 0, SEEK_SET);
2445 len = fread(buff, 1, MAX_RET, fp);
2446 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002447
qs.xiong9fbf74e2023-03-28 13:38:22 +08002448 for(index=0; index < len; index ++)
2449 {
2450 if (memcmp(buff + index, "network={", 9) != 0)
2451 {
you.chen35020192022-05-06 11:30:57 +08002452 continue;
2453 }
2454 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002455 for (; index < len; index ++ )
2456 {
2457 if (buff[index] != '}')
2458 {
you.chen35020192022-05-06 11:30:57 +08002459 continue;
2460 }
2461 buff[index] = '\0';
2462 break;
2463 }
2464 len = buff + index - p;
2465 }
2466
2467 count = lynq_split(p, len, '\n', split_lines);
2468
2469 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002470 for(index=0; index < count; index++)
2471 {
you.chen35020192022-05-06 11:30:57 +08002472 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002473 if (p != NULL)
2474 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002475 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002476 if (*p == '\"')
2477 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002478 p++;
2479 }
you.chen35020192022-05-06 11:30:57 +08002480 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002481 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2482 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002483 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002484 if (*p == '\"')
2485 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002486 p++;
2487 }
2488 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002489 else
2490 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002491 continue;
you.chen35020192022-05-06 11:30:57 +08002492 }
2493
2494 strcpy(password, p);
2495
qs.xiong9fbf74e2023-03-28 13:38:22 +08002496 while(*password != '\0')
2497 {
2498 if (*password == '\"')
2499 {
you.chen35020192022-05-06 11:30:57 +08002500 *password = '\0';
2501 break;
2502 }
2503 password++;
2504 }
2505 ret = 0;
2506 break;
2507 } //end for(index=0; index < count; index++)
2508
2509 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002510}
2511
you.chen35020192022-05-06 11:30:57 +08002512static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2513 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002514 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002515
qs.xiong9fbf74e2023-03-28 13:38:22 +08002516 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2517 {
you.chen35020192022-05-06 11:30:57 +08002518 return -1;
2519 }
2520
2521 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002522
qs.xiong9fbf74e2023-03-28 13:38:22 +08002523 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2524 {
2525 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002526 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002527 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002528 {
you.chena6cd55a2022-05-08 12:20:18 +08002529 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002530 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002531 }
you.chen70f377f2023-04-14 18:17:09 +08002532 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2533 {
2534 return 0;
2535 }
you.chena6cd55a2022-05-08 12:20:18 +08002536 }
2537 }
you.chen70f377f2023-04-14 18:17:09 +08002538 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2539 {
2540 return 0;
2541 }
2542
qs.xiong9fbf74e2023-03-28 13:38:22 +08002543 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2544 {
you.chen70f377f2023-04-14 18:17:09 +08002545 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002546 return -1;
2547 }
2548 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2549 {
2550
you.chen70f377f2023-04-14 18:17:09 +08002551 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2552 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002553 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2554 {
2555
2556 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002557 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002558 }else
2559 {
you.chen70f377f2023-04-14 18:17:09 +08002560 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002561 *auth = -1;
2562 return -1;
2563 }
you.chen35020192022-05-06 11:30:57 +08002564 return 0;
2565}
2566
2567int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002568{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002569 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002570 int pass_len, net_no, count, index;
2571 char lynq_tmp_cmd[300]={0};
2572 int net_no_list[128];
2573 lynq_wifi_auth_s net_auth;
2574 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002575 if(pass_len < 8 || pass_len >= 64)
2576 {
2577 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002578 return -1;
2579 }
2580
2581 CHECK_IDX(idx, CTRL_STA);
2582
2583 net_no = -1;
2584 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2585
qs.xiong9fbf74e2023-03-28 13:38:22 +08002586 for (index=0; index < count; index++)
2587 {
you.chen35020192022-05-06 11:30:57 +08002588 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002589 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2590 {
you.chen35020192022-05-06 11:30:57 +08002591 net_no = net_no_list[index];
2592 break;
2593 }
2594 }
2595
qs.xiong9fbf74e2023-03-28 13:38:22 +08002596 if (net_no < 0)
2597 {
you.chen35020192022-05-06 11:30:57 +08002598 return -1;
2599 }
2600
2601 CHECK_WPA_CTRL(CTRL_STA);
2602
2603 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2604
2605 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2606 DO_OK_FAIL_REQUEST(cmd_save_config);
2607
2608 return 0;
2609}
2610
2611int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2612
2613 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002614 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002615 int count, index;
2616 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002617 char *buff, *p, *ssid, *ssid_end_flag;
2618 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002619 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002620
you.chen755332b2022-08-06 16:59:10 +08002621 network_len = 0;
2622 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002623 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002624
you.chen35020192022-05-06 11:30:57 +08002625 CHECK_IDX(idx, CTRL_STA);
2626
qs.xiong9fbf74e2023-03-28 13:38:22 +08002627 if (NULL == password)
2628 {
2629 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002630 return -1;
2631 }
2632
you.chen35020192022-05-06 11:30:57 +08002633 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002634 if (NULL == fp)
2635 {
2636 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002637 return -1;
2638 }
2639
you.chen6d247052023-06-01 16:39:54 +08002640 fseek(fp, 0, SEEK_END);
2641 len = ftell(fp);
2642 buff = malloc(len + 1);
2643
2644 if (buff == NULL)
2645 {
2646 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2647 return -1;
2648 }
2649
you.chen35020192022-05-06 11:30:57 +08002650 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002651 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002652 fclose(fp);
2653
qs.xiong9fbf74e2023-03-28 13:38:22 +08002654 for(index=0; index < len; index ++)
2655 {
2656 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002657 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002658 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002659 {
you.chen35020192022-05-06 11:30:57 +08002660 continue;
2661 }
you.chen6d247052023-06-01 16:39:54 +08002662 p = buff + index + 9;
2663 for (; index < len; index ++ )
2664 {
2665 if (buff[index] != '}')
2666 {
2667 continue;
2668 }
2669 buff[index] = '\0';
2670 break;
2671 }
2672 network_len = buff + index - p;
2673 break;
you.chen35020192022-05-06 11:30:57 +08002674 }
2675
qs.xiongb3f26af2023-02-17 18:41:07 +08002676 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002677 {
2678 if (buff != NULL)
2679 {
2680 free(buff);
2681 }
2682
qs.xiongb3f26af2023-02-17 18:41:07 +08002683 return -1;
you.chen6d247052023-06-01 16:39:54 +08002684 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002685
you.chend2fef3f2023-02-13 10:50:35 +08002686 ssid = strstr(p, "ssid=");
2687 if (ssid != NULL) {
2688 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002689 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002690 {
you.chend2fef3f2023-02-13 10:50:35 +08002691 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2692 break;
2693 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002694 else
you.chen6d247052023-06-01 16:39:54 +08002695 {
you.chend2fef3f2023-02-13 10:50:35 +08002696 ssid_end_flag = strstr(ssid, "\n");
2697 if (ssid_end_flag != NULL)
2698 {
2699 ssid_len = (ssid_end_flag - ssid) / 2;
2700 for(i=0; i<ssid_len; i++)
2701 {
2702 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2703 }
2704 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2705 break;
2706 }
2707 }
you.chen35020192022-05-06 11:30:57 +08002708 }
you.chend2fef3f2023-02-13 10:50:35 +08002709
you.chen35020192022-05-06 11:30:57 +08002710 }
2711
qs.xiong9fbf74e2023-03-28 13:38:22 +08002712 if (index >= len || NULL == p || network_len <= 0)
2713 {
you.chen6d247052023-06-01 16:39:54 +08002714 if (buff != NULL)
2715 {
2716 free(buff);
2717 }
you.chen35020192022-05-06 11:30:57 +08002718 return -1;
2719 }
2720
you.chen755332b2022-08-06 16:59:10 +08002721 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002722
2723 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002724 for(index=0; index < count; index++)
2725 {
you.chen35020192022-05-06 11:30:57 +08002726 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002727 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002728 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002729 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002730 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002731 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002732 p++;
2733 }
you.chen35020192022-05-06 11:30:57 +08002734 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002735 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002736 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002737 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002738 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002739 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002740 p++;
2741 }
2742 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002743 else
you.chen6d247052023-06-01 16:39:54 +08002744 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002745 continue;
you.chen35020192022-05-06 11:30:57 +08002746 }
2747
qs.xiong13673462023-02-21 19:12:54 +08002748 if (*p == '\"')
2749 p++;
2750 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002751
qs.xiong13673462023-02-21 19:12:54 +08002752 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002753 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002754 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002755 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002756 {
you.chen35020192022-05-06 11:30:57 +08002757 *password = '\0';
2758 break;
2759 }
2760 password++;
2761 }
2762 ret = 0;
2763 break;
2764 } //end for(index=0; index < count; index++)
2765
you.chen6d247052023-06-01 16:39:54 +08002766 if (buff != NULL)
2767 {
2768 free(buff);
2769 }
2770
you.chen35020192022-05-06 11:30:57 +08002771 return ret;
2772}
2773
2774static int inner_set_sta_ssid(int net_no, char *sta_ssid)
2775{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002776 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04002777
qs.xiong9fbf74e2023-03-28 13:38:22 +08002778 if (sta_ssid == NULL)
2779 {
2780 RLOGE("sta_ssid is null\n");
2781 return -1;
you.chen35020192022-05-06 11:30:57 +08002782 }
2783
qs.xiong9fbf74e2023-03-28 13:38:22 +08002784 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08002785
2786 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
2787
2788 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2789// DO_OK_FAIL_REQUEST(cmd_save_config);
2790
qs.xiong9fbf74e2023-03-28 13:38:22 +08002791 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002792
2793}
2794
you.chen35020192022-05-06 11:30:57 +08002795static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05002796{
you.chen35020192022-05-06 11:30:57 +08002797 char lynq_disable_cmd[128]={0};
2798 char lynq_select_cmd[128]={0};
2799
2800 CHECK_WPA_CTRL(CTRL_STA);
2801
qs.xiong9fbf74e2023-03-28 13:38:22 +08002802 if (save != 0)
2803 {
you.chenc29444e2022-06-07 18:01:16 +08002804 if (start_flag != 0)
2805 {
2806 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
2807 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2808 }
2809 else
2810 {
2811 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
2812 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2813 }
you.chen35020192022-05-06 11:30:57 +08002814 DO_OK_FAIL_REQUEST(cmd_save_config);
2815 }
2816
qs.xiong9fbf74e2023-03-28 13:38:22 +08002817 if (start_flag == 0)
2818 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002819 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08002820 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2821 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002822 else
2823 {
you.chen35020192022-05-06 11:30:57 +08002824 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
2825 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2826 }
2827
2828 return 0;
2829}
2830
2831int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
2832{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002833 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002834 CHECK_IDX(idx, CTRL_STA);
2835
you.chen6c2dd9c2022-05-16 17:55:28 +08002836 curr_status_info curr_state;
2837 ap_info_s ap_info;
2838 curr_state.ap = &ap_info;
2839 curr_state.state = NULL;
2840
qs.xiong9fbf74e2023-03-28 13:38:22 +08002841 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
2842 {
you.chend2fef3f2023-02-13 10:50:35 +08002843 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08002844 return 0;
2845 }
2846
2847 return -1;
you.chen35020192022-05-06 11:30:57 +08002848}
2849
2850int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
2851{
you.chen9ac66392022-08-06 17:01:16 +08002852 scan_info_s *scan_list = NULL;
2853 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08002854 int scan_len=0;
2855 int save_len=0;
2856 int best_index = -1;
2857 int best_scan_index = -1;
2858 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08002859 int i, j, ret;
2860
2861 ret = -1;
you.chen35020192022-05-06 11:30:57 +08002862
2863 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002864 if (info == NULL)
2865 {
you.chen35020192022-05-06 11:30:57 +08002866 return -1;
2867 }
2868
2869 curr_status_info curr_state;
2870 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08002871 char status[64];
you.chen35020192022-05-06 11:30:57 +08002872
you.chen9ac66392022-08-06 17:01:16 +08002873 memset(&ap_info, 0, sizeof (ap_info));
2874 memset(status, 0, sizeof (status));
2875
2876 curr_state.ap = &ap_info;
2877 curr_state.state = status;
2878
qs.xiong9fbf74e2023-03-28 13:38:22 +08002879 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
2880 {
you.chen35020192022-05-06 11:30:57 +08002881 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08002882 if (strcmp(status, STATE_COMPLETED) == 0)
2883 {
2884 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
2885 }
2886 else
2887 {
2888 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2889 }
you.chen593621d2023-04-27 17:52:44 +08002890 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08002891 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08002892 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002893 return 0;
2894 }
2895
you.chen9ac66392022-08-06 17:01:16 +08002896 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08002897 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002898 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
2899 {
you.chen9ac66392022-08-06 17:01:16 +08002900 if (NULL != scan_list)
2901 {
2902 free(scan_list);
2903 }
you.chen35020192022-05-06 11:30:57 +08002904 return -1;
2905 }
2906
qs.xiong9fbf74e2023-03-28 13:38:22 +08002907 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
2908 {
you.chen9ac66392022-08-06 17:01:16 +08002909 if (NULL != scan_list)
2910 {
2911 free(scan_list);
2912 }
2913 if (NULL != save_list)
2914 {
2915 free(save_list);
2916 }
you.chen35020192022-05-06 11:30:57 +08002917 return -1;
2918 }
2919
qs.xiong9fbf74e2023-03-28 13:38:22 +08002920 for (i=0; i < save_len; i++)
2921 {
2922 for (j=0; j < scan_len; j++)
2923 {
you.chen35020192022-05-06 11:30:57 +08002924 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08002925 && save_list[i].base_info.auth == scan_list[j].auth)
2926 {
2927 if (best_rssi == 0)
2928 {
you.chen9ac66392022-08-06 17:01:16 +08002929 best_index = i;
you.chen35020192022-05-06 11:30:57 +08002930 best_rssi = scan_list[j].rssi;
2931 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002932 else if (best_rssi > scan_list[j].rssi)
2933 {
you.chen35020192022-05-06 11:30:57 +08002934 best_index = i;
2935 best_scan_index = j;
2936 best_rssi = scan_list[j].rssi;
2937 }
you.chend2fef3f2023-02-13 10:50:35 +08002938 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 +08002939 break;
2940 }
2941 }
2942 }
2943
qs.xiong9fbf74e2023-03-28 13:38:22 +08002944 if (best_index >= 0)
2945 {
you.chen35020192022-05-06 11:30:57 +08002946 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08002947 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 +08002948 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2949 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08002950 ret = 0;
you.chen35020192022-05-06 11:30:57 +08002951 }
2952
you.chen9ac66392022-08-06 17:01:16 +08002953 if (NULL != scan_list)
2954 {
2955 free(scan_list);
2956 }
2957 if (NULL != save_list)
2958 {
2959 free(save_list);
2960 }
2961
2962 return ret;
you.chen35020192022-05-06 11:30:57 +08002963}
2964
2965static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
2966{
qs.xiongc8d92a62023-03-29 17:36:14 +08002967 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08002968 char lynq_ket_mgmt_cmd[64]={0};
2969 char lynq_pairwise_cmd[64]={0};
2970 char lynq_psk_cmd[64]={0};
2971
2972 CHECK_WPA_CTRL(CTRL_STA);
2973
qs.xiong9fbf74e2023-03-28 13:38:22 +08002974 switch(auth)
2975 {
2976 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08002977 {
2978 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002979
you.chen35020192022-05-06 11:30:57 +08002980 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002981// DO_OK_FAIL_REQUEST(cmd_save_config);
2982 break;
2983 }
2984 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08002985 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002986 {
2987 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2988 {
you.chen35020192022-05-06 11:30:57 +08002989 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
2990 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002991 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2992 {
you.chena6cd55a2022-05-08 12:20:18 +08002993 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08002994 }
2995 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
2996 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002997
you.chen35020192022-05-06 11:30:57 +08002998 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2999 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3000 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04003001
qs.xiong9fbf74e2023-03-28 13:38:22 +08003002 if (password != NULL)
3003 {
you.chen35020192022-05-06 11:30:57 +08003004 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3005 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003006 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08003007 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003008
you.chen35020192022-05-06 11:30:57 +08003009// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003010 break;
3011 }
3012 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3013 {
qs.xiong3e506812023-04-06 11:08:48 +08003014 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003015 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003016 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3017 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3018
qs.xiong3e506812023-04-06 11:08:48 +08003019 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003020 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3021 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3022 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3023
3024 break;
3025 }
3026 case LYNQ_WIFI_AUTH_WPA3_PSK:
3027 {
qs.xiong3e506812023-04-06 11:08:48 +08003028 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003029 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003030 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3031 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3032
qs.xiong3e506812023-04-06 11:08:48 +08003033 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003034 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3035 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3036 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3037
3038 break;
3039 }
3040 default:
3041 return -1;
you.chen35020192022-05-06 11:30:57 +08003042 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003043
qs.xiong9fbf74e2023-03-28 13:38:22 +08003044 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003045}
qs.xiong7a105ce2022-03-02 09:43:11 -05003046
you.chen35020192022-05-06 11:30:57 +08003047static int inner_get_curr_net_no(int interface) {
3048 curr_status_info curr_state;
3049 curr_state.ap = NULL;
3050 curr_state.state = NULL;
3051
qs.xiong9fbf74e2023-03-28 13:38:22 +08003052 if (0 != inner_get_status_info(interface, &curr_state))
3053 {
you.chen35020192022-05-06 11:30:57 +08003054 return -1;
3055 }
3056
3057 return curr_state.net_no;
3058}
3059
3060int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003061{
you.chen35020192022-05-06 11:30:57 +08003062 int net_no;
3063 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003064
you.chen35020192022-05-06 11:30:57 +08003065 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003066
qs.xiong9fbf74e2023-03-28 13:38:22 +08003067 if (net_no < 0)
3068 {
you.chen35020192022-05-06 11:30:57 +08003069 return -1;
3070 }
3071
3072 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003073}
3074
you.chenb95401e2023-05-12 19:39:06 +08003075int 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 -05003076{
you.chen35020192022-05-06 11:30:57 +08003077 int count, net_no, index;
3078 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003079 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003080 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003081 curr_status_info curr_state;
3082 ap_info_s ap_info;
3083 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003084
qs.xiong9fbf74e2023-03-28 13:38:22 +08003085 if (ssid == NULL || *ssid == '\0')
3086 {
3087 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003088 return -1;
3089 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003090
qs.xiong9fbf74e2023-03-28 13:38:22 +08003091 if (LYNQ_WIFI_AUTH_OPEN != auth)
3092 {
3093 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003094 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003095 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003096 return -1;
3097 }
3098 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003099
you.chen70f377f2023-04-14 18:17:09 +08003100
3101 pthread_mutex_lock(&s_global_check_mutex);
3102 if (s_sta_status != INNER_STA_STATUS_INIT)
3103 {
3104 s_sta_status = INNER_STA_STATUS_CANCEL;
3105 pthread_cond_signal(&s_global_check_cond);
3106 }
3107 pthread_mutex_unlock(&s_global_check_mutex);
3108
you.chen35020192022-05-06 11:30:57 +08003109 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003110 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003111
3112 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003113 memset(&ap_info, 0, sizeof (ap_info));
3114 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003115
you.chen70f377f2023-04-14 18:17:09 +08003116 curr_state.ap = &ap_info;
3117 curr_state.state = status;
3118
3119 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003120 {
you.chen70f377f2023-04-14 18:17:09 +08003121 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3122 {
3123 net_no = curr_state.net_no;
3124 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3125 && strcmp(ap_info.psw, psw) == 0)
3126 {
3127 RLOGD("already connected\n");
3128
3129 pthread_mutex_lock(&s_global_check_mutex);
3130 s_sta_status = INNER_STA_STATUS_CONNECTED;
3131 pthread_cond_signal(&s_global_check_cond);
3132 pthread_mutex_unlock(&s_global_check_mutex);
3133 return 0;
3134 }
you.chen35020192022-05-06 11:30:57 +08003135 }
3136 }
3137
you.chen70f377f2023-04-14 18:17:09 +08003138 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003139 {
you.chen70f377f2023-04-14 18:17:09 +08003140 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3141
3142 for (index=0; index < count; index++)
3143 {
3144 net_auth = -1;
3145 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3146 {
3147 net_no = net_no_list[index];
3148 break;
3149 }
you.chen35020192022-05-06 11:30:57 +08003150 }
3151
you.chen70f377f2023-04-14 18:17:09 +08003152 if (net_no < 0)
3153 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003154 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3155 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3156 {
3157 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3158 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3159 DO_OK_FAIL_REQUEST(rm_net_cmd);
3160 }
you.chen70f377f2023-04-14 18:17:09 +08003161 net_no = lynq_add_network(CTRL_STA);
3162 if (net_no == -1)
3163 {
3164 return -1;
3165 }
3166
3167 RLOGD("net no is %d\n", net_no);
3168 if (0 != inner_set_sta_ssid(net_no, ssid))
3169 {
3170 return -1;
3171 }
you.chen35020192022-05-06 11:30:57 +08003172 }
3173 }
3174
qs.xiong9fbf74e2023-03-28 13:38:22 +08003175 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3176 {
you.chen35020192022-05-06 11:30:57 +08003177 return -1;
3178 }
3179
you.chen70f377f2023-04-14 18:17:09 +08003180
3181 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003182 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003183 usleep(200*1000);
3184
3185 ret = inner_sta_start_stop(net_no, 1, 1);
3186
3187 pthread_mutex_lock(&s_global_check_mutex);
3188 s_sta_status = INNER_STA_STATUS_CONNECTING;
3189 strcpy(s_sta_current_connecting_ssid, ssid);
3190 struct timeval now;
3191 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003192 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003193 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3194 pthread_cond_signal(&s_global_check_cond);
3195 pthread_mutex_unlock(&s_global_check_mutex);
3196 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003197}
3198
you.chenb95401e2023-05-12 19:39:06 +08003199int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3200{
3201 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3202}
3203
you.chen35020192022-05-06 11:30:57 +08003204int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003205{
you.chen35020192022-05-06 11:30:57 +08003206 ap_info_s ap;
3207 curr_status_info curr_state;
3208 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003209
qs.xiong9fbf74e2023-03-28 13:38:22 +08003210 if (ssid == NULL || *ssid == '\0')
3211 {
3212 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003213 return -1;
3214 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003215
you.chen35020192022-05-06 11:30:57 +08003216 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003217
you.chen35020192022-05-06 11:30:57 +08003218 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003219 curr_state.state = NULL;
3220
qs.xiong9fbf74e2023-03-28 13:38:22 +08003221 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3222 {
you.chen35020192022-05-06 11:30:57 +08003223 return 0;
3224 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003225
qs.xiong9fbf74e2023-03-28 13:38:22 +08003226 if (strcmp(ap.ap_ssid, ssid) != 0)
3227 {
you.chen35020192022-05-06 11:30:57 +08003228 return 0;
3229 }
3230
you.chen70f377f2023-04-14 18:17:09 +08003231 pthread_mutex_lock(&s_global_check_mutex);
3232 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3233 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003234 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003235}
qs.xiong97fa59b2022-04-07 05:41:29 -04003236
you.chena6cd55a2022-05-08 12:20:18 +08003237int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3238{
qs.xiongad2f89d2023-01-18 13:17:41 +08003239// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3240// const char *lynq_reconnect_cmd = "RECONNECT";
3241 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3242 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3243// 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 -05003244
you.chen35020192022-05-06 11:30:57 +08003245 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003246 CHECK_WPA_CTRL(CTRL_STA);
3247
you.chenc9928582023-04-24 15:39:37 +08003248 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3249 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003250 {
you.chenc9928582023-04-24 15:39:37 +08003251 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003252 return -1;
3253 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003254
qs.xiongad2f89d2023-01-18 13:17:41 +08003255 system(lynq_enable_sta_cmd);
3256 system(lynq_reconnect_cmd);
3257// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003258 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003259}
3260
you.chen6d247052023-06-01 16:39:54 +08003261static int inner_get_status_info_state (int interface, char *state) {
3262 curr_status_info curr_state;
3263 curr_state.ap = NULL;
3264 curr_state.state = state;
3265 return inner_get_status_info(interface, &curr_state);
3266}
you.chen35020192022-05-06 11:30:57 +08003267int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003268{
qs.xiongad2f89d2023-01-18 13:17:41 +08003269// char lynq_disable_network_cmd[MAX_CMD];
3270// curr_status_info curr_state;
3271// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003272 int i=0;
3273 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003274
you.chen6d247052023-06-01 16:39:54 +08003275// 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 +08003276 CHECK_IDX(idx, CTRL_STA);
3277 CHECK_WPA_CTRL(CTRL_STA);
3278
you.chen6d247052023-06-01 16:39:54 +08003279// system(lynq_disable_sta_cmd);
3280 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003281 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003282
3283 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3284 if (ret != 0)
3285 {
3286 RLOGE("lynq_wifi_ap_start excute script fail");
3287 return -1;
3288 }
3289
you.chen6d247052023-06-01 16:39:54 +08003290 for (i=0; i < 30; i++) // to check if sta is realy stoped
3291 {
3292 if (inner_get_status_info_state(idx, state) != 0)
3293 {
3294 break;
3295 }
3296
3297 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3298 {
3299 break;
3300 }
3301 RLOGD("lynq_wifi_ap_start curr state %s", state);
3302 usleep(SLEEP_TIME_ON_IDLE);
3303 }
3304
you.chena6cd55a2022-05-08 12:20:18 +08003305 return 0;
3306// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003307}
qs.xiongfcc914b2023-07-06 21:16:20 +08003308int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3309{
3310 char LYNQ_DISABLE_CMD[128]={0};
3311 CHECK_IDX(idx, CTRL_STA);
3312 CHECK_WPA_CTRL(CTRL_STA);
3313 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3314 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3315 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3316 return 0;
3317}
qs.xiong7a105ce2022-03-02 09:43:11 -05003318
you.chen35020192022-05-06 11:30:57 +08003319//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3320// int i, count;
3321// char *p;
3322// const char * FLAG_SSID = "ssid=";
3323// const char * FLAG_SBSID = "bssid=";
3324// const char * FLAG_KEY_MGMT = "key_mgmt=";
3325// const char * FLAG_FREQ = "freq=";
3326// char lynq_sta_cmd[MAX_CMD];
3327// char *split_lines[128] = {0};
3328
3329// CHECK_WPA_CTRL(CTRL_AP);
3330
3331// sprintf(lynq_sta_cmd, "STA %s", bssid);
3332
3333// DO_REQUEST(lynq_sta_cmd);
3334
3335// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3336
3337// for(i=0; i < count; i++) {
3338// p = strstr(split_lines[i], FLAG_SSID);
3339// if (p != NULL) {
3340// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3341// continue;
3342// }
3343// }
3344
3345// lynq_get_interface_ip(idx, ap->ap_ip);
3346// lynq_ap_password_set(idx, ap->psw);
3347
3348// return 0;
3349//}
3350
3351static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3352 curr_status_info curr_state;
3353 curr_state.ap = ap;
3354 curr_state.state = NULL;
3355 return inner_get_status_info(interface, &curr_state);
3356}
3357
3358int 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 -04003359{
you.chend2fef3f2023-02-13 10:50:35 +08003360 int index, line_count;
3361 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003362 const char *lynq_first_sta_cmd = "STA-FIRST";
3363 char lynq_next_sta_cmd[MAX_CMD];
3364 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003365 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003366
you.chen35020192022-05-06 11:30:57 +08003367 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003368
you.chen35020192022-05-06 11:30:57 +08003369 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003370
you.chenb95401e2023-05-12 19:39:06 +08003371 // ap_info_s * tmp_ap;
3372 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003373 if (ap == NULL || list == NULL || len == NULL)
3374 {
3375 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003376 return -1;
3377 }
3378
you.chenb95401e2023-05-12 19:39:06 +08003379 // ap = &tmp_ap;
3380 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003381 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003382 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003383
you.chenb95401e2023-05-12 19:39:06 +08003384 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003385 {
you.chenb95401e2023-05-12 19:39:06 +08003386 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003387 return -1;
3388 }
3389
3390 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3391 lynq_ap_password_get(idx, (*ap)->psw);
3392
you.chen35020192022-05-06 11:30:57 +08003393 DO_REQUEST(lynq_first_sta_cmd);
3394
3395 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003396 while (reply_len > 0)
3397 {
3398 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003399 {
you.chen35020192022-05-06 11:30:57 +08003400 break;
3401 }
3402 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3403 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3404 strcpy(bssid[index], split_lines[0]);
3405 index++;
3406 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3407 reply_len = MAX_RET;
3408 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003409 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 +08003410 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003411 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003412 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003413 break;
3414 }
3415 }
3416
3417 *len = index;
3418
3419 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003420 for (index=0; index < *len; index++)
3421 {
you.chend2fef3f2023-02-13 10:50:35 +08003422 dev_info = &(*list)[index];
3423 memset(dev_info, 0, sizeof(device_info_s));
3424 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3425 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3426 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3427 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003428 free(bssid[index]);
3429 }
3430
3431 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003432}
3433
you.chen35020192022-05-06 11:30:57 +08003434int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003435{
you.chen35020192022-05-06 11:30:57 +08003436 int i, count, index, count_words;
3437 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3438 char *split_lines[128] = {0};
3439 char *split_words[128] = {0};
3440 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003441
qs.xiong9fbf74e2023-03-28 13:38:22 +08003442 if (list == NULL || len == NULL)
3443 {
you.chen35020192022-05-06 11:30:57 +08003444 return -1;
3445 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003446
you.chen9ac66392022-08-06 17:01:16 +08003447 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3448 {
3449 usleep(100 * 1000);
3450 }
3451
you.chen35020192022-05-06 11:30:57 +08003452 CHECK_IDX(idx, CTRL_STA);
3453
3454 CHECK_WPA_CTRL(CTRL_STA);
3455
3456 DO_REQUEST(lynq_scan_result_cmd);
3457
3458 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3459 *len = count - 1;
3460 *list = malloc(sizeof (scan_info_s) * *len);
3461
3462 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003463 for (index=0; index <count_words; index++)
3464 {
3465 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003466 }
3467
qs.xiong9fbf74e2023-03-28 13:38:22 +08003468 for(index = 1;index < count; index++)
3469 {
3470 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003471 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003472 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3473 if (count_words < 4)
3474 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003475 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003476 //bssid / frequency / signal level / flags / ssid
3477 p = (*list) + index - 1;
3478 strcpy(p->mac, split_words[0]);
3479 p->band = convert_band_from_freq(atoi(split_words[1]));
3480 p->rssi = -1 * atoi( split_words[2]);
3481 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003482 if (count_words == 4) // ssid hided
3483 {
3484 p->ssid[0] = '\0';
3485 }
3486 else
3487 {
3488 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3489 }
you.chen35020192022-05-06 11:30:57 +08003490 }
3491
3492 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003493}
qs.xiong97fa59b2022-04-07 05:41:29 -04003494
you.chen35020192022-05-06 11:30:57 +08003495int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3496{
3497 int count, net_no, index;
3498 int net_no_list[128];
3499 lynq_wifi_auth_s net_auth;
3500 char lynq_remove_cmd[MAX_CMD];
3501
qs.xiong9fbf74e2023-03-28 13:38:22 +08003502 if (ssid == NULL || *ssid == '\0')
3503 {
3504 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003505 return -1;
3506 }
3507
3508 CHECK_IDX(idx, CTRL_STA);
3509
3510 CHECK_WPA_CTRL(CTRL_STA);
3511
3512 net_no = -1;
3513 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3514
qs.xiong9fbf74e2023-03-28 13:38:22 +08003515 for (index=0; index < count; index++)
3516 {
you.chen35020192022-05-06 11:30:57 +08003517 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003518 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3519 {
you.chen35020192022-05-06 11:30:57 +08003520 net_no = net_no_list[index];
3521 break;
3522 }
3523 }
3524
qs.xiong9fbf74e2023-03-28 13:38:22 +08003525 if (net_no < 0)
3526 {
you.chen35020192022-05-06 11:30:57 +08003527 return 0;
3528 }
3529
3530 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3531
3532 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3533 DO_OK_FAIL_REQUEST(cmd_save_config);
3534
3535 return 0;
3536}
3537
3538int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003539{
you.chend2fef3f2023-02-13 10:50:35 +08003540 int count, index;
you.chen35020192022-05-06 11:30:57 +08003541 int net_no_list[128];
3542 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003543 RLOGD("enter lynq_get_sta_saved_ap api\n");
3544 if (list == NULL || len == NULL)
3545 {
3546 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003547 return -1;
3548 }
3549
3550 CHECK_IDX(idx, CTRL_STA);
3551
3552// CHECK_WPA_CTRL(CTRL_STA);
3553
3554 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003555 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003556
you.chen057aac42023-04-13 14:06:58 +08003557 if (count < 0)
3558 {
3559 RLOGE("list network fail");
3560 return count;
3561 }
3562 else if (count == 0)
3563 {
3564 *list = NULL;
3565 *len = 0;
3566 return 0;
3567 }
3568
you.chen35020192022-05-06 11:30:57 +08003569 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003570 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003571 *len = count;
3572
qs.xiong9fbf74e2023-03-28 13:38:22 +08003573 for (index=0; index < count; index++)
3574 {
you.chen35020192022-05-06 11:30:57 +08003575 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003576 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003577 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003578 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003579 {
you.chen35020192022-05-06 11:30:57 +08003580 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3581 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003582 else
you.chen057aac42023-04-13 14:06:58 +08003583 {
you.chen35020192022-05-06 11:30:57 +08003584 (*list)[index].base_info.band = -1;
3585 }
you.chen057aac42023-04-13 14:06:58 +08003586 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003587 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003588 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003589 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003590 return 0;
3591}
3592
3593int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3594{
qs.xiongc8d92a62023-03-29 17:36:14 +08003595 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003596 const char *lynq_scan_cmd = "SCAN";
3597
3598 CHECK_IDX(idx, CTRL_STA);
3599
3600 CHECK_WPA_CTRL(CTRL_STA);
3601
you.chen0df3e7e2023-05-10 15:56:26 +08003602 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3603 {
3604 RLOGD("tmp clear scanlist");
3605 system(clean_last_re);
3606 }
you.chen9ac66392022-08-06 17:01:16 +08003607 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003608 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003609 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3610 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003611 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003612 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3613 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003614 g_sta_scan_finish_flag = 1;
3615 return -1;
3616 }
you.chen35020192022-05-06 11:30:57 +08003617
3618 return 0;
3619}
3620
3621int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003622 if (cb == NULL)
3623 {
3624 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003625 return -1;
3626 }
3627
you.chen6d247052023-06-01 16:39:54 +08003628 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003629 g_ap_callback_priv = priv;
3630 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003631 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003632
you.chen6d247052023-06-01 16:39:54 +08003633 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003634 if (g_ap_watcher_pid == 0 )
3635 {
3636 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3637 {
3638 g_ap_watcher_pid = 0;
3639 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3640 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3641 return -1;
3642 }
3643 }
3644
3645 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3646 RLOGD("creat APWatcherTheradProc susccs");
3647
you.chen35020192022-05-06 11:30:57 +08003648 return 0;
3649}
3650
3651int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003652 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003653 if (g_ap_callback_priv == priv)
3654 {
you.chen35020192022-05-06 11:30:57 +08003655 g_ap_callback_func = NULL;
3656 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003657 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003658 return 0;
3659 }
you.chen6d247052023-06-01 16:39:54 +08003660 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003661 return -1;
3662}
3663
3664int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003665 if (cb == NULL)
3666 {
3667 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003668 return -1;
3669 }
3670
you.chen6d247052023-06-01 16:39:54 +08003671 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003672 g_sta_callback_priv = priv;
3673 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003674 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003675
you.chen6d247052023-06-01 16:39:54 +08003676 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003677 if (g_sta_watcher_pid == 0 ) {
3678 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3679 {
3680 g_sta_watcher_pid = 0;
3681 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3682 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3683 return -1;
3684 }
3685 }
3686
3687 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3688 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003689 return 0;
3690}
3691
3692int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003693 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003694 if (g_sta_callback_priv == priv)
3695 {
you.chen35020192022-05-06 11:30:57 +08003696 g_sta_callback_func = NULL;
3697 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003698 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003699 return 0;
3700 }
you.chen6d247052023-06-01 16:39:54 +08003701 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003702 return -1;
3703}
3704
qs.xiongfcc914b2023-07-06 21:16:20 +08003705int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
3706 if (cb == NULL)
3707 {
3708 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
3709 return -1;
3710 }
3711 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3712 g_sta_auto_callback_priv = priv;
3713 g_sta_auto_callback_func = cb;
3714 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3715 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
3716 if (g_sta_auto_watcher_pid == 0 ) {
3717 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
3718 {
3719 g_sta_auto_watcher_pid = 0;
3720 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3721 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3722 return -1;
3723 }
3724 }
3725 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3726 RLOGD("creat STAWatcherTheradProc susccs");
3727 return 0;
3728}
3729int lynq_unreg_sta_auto_event_callback(void * priv) {
3730 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3731 if (g_sta_auto_callback_priv == priv)
3732 {
3733 g_sta_auto_watcher_stop_flag = 1;
3734 if (g_sta_auto_watcher_pid != 0)
3735 {
3736 pthread_join(g_sta_auto_watcher_pid, NULL);
3737 }
3738 g_sta_auto_watcher_pid = 0;
3739 g_sta_auto_callback_func = NULL;
3740 g_sta_auto_callback_priv = NULL;
3741 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3742 return 0;
3743 }
3744 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3745 return -1;
3746}
you.chen35020192022-05-06 11:30:57 +08003747int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
3748{
3749 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003750 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08003751 CHECK_IDX(idx, CTRL_AP);
3752
qs.xiong9fbf74e2023-03-28 13:38:22 +08003753 if (inner_get_status_info_state(CTRL_AP, state) != 0)
3754 {
you.chen35020192022-05-06 11:30:57 +08003755 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3756 return 0;
3757 }
3758
qs.xiong9fbf74e2023-03-28 13:38:22 +08003759 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3760 {
you.chen35020192022-05-06 11:30:57 +08003761 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
3762 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003763 else
3764 {
you.chen35020192022-05-06 11:30:57 +08003765 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3766 }
3767
3768 return 0;
3769}
3770
3771int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
3772 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003773 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08003774 CHECK_IDX(idx, CTRL_STA);
3775
qs.xiong9fbf74e2023-03-28 13:38:22 +08003776 if (inner_get_status_info_state(CTRL_STA, state) != 0)
3777 {
you.chen35020192022-05-06 11:30:57 +08003778 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3779 return 0;
3780 }
3781
qs.xiong9fbf74e2023-03-28 13:38:22 +08003782 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3783 {
you.chen35020192022-05-06 11:30:57 +08003784 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
3785 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003786 else
3787 {
you.chen35020192022-05-06 11:30:57 +08003788 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3789 }
3790
3791 return 0;
3792}
3793
3794int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
3795// CHECK_IDX(idx, CTRL_AP);
3796// int ret = 0;
3797// size_t reply_len = MAX_RET;
3798// char cmd_reply[MAX_RET]={0};
3799// const char * cmd_str = "GET country";
3800// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
3801// do{
3802// if (NULL == s_lynq_wpa_ctrl) {
3803// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
3804// if (NULL == s_lynq_wpa_ctrl ) {
3805// printf("wpa_ctrl_open fail\n");
3806// return -1;
3807// }
3808// }
3809// }while(0);
3810
3811// do {
3812// reply_len = MAX_RET;
3813// cmd_reply[0] = '\0';
3814// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08003815// 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 +08003816// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003817// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08003818// return ret;
3819// }
3820// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08003821// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08003822// }while(0);
3823
3824 FILE *fp;
3825 size_t i = 0;
3826 char lynq_cmd_ret[MAX_RET]={0};
3827
3828// CHECK_IDX(idx, CTRL_AP);
3829
3830 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003831 {
3832 perror("popen error!");
3833 return -1;
3834 }
you.chen35020192022-05-06 11:30:57 +08003835 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
3836 {
3837 perror("fread fail!");
3838 return -1;
3839 }
3840
qs.xiong9fbf74e2023-03-28 13:38:22 +08003841 for(i=0; i < strlen(lynq_cmd_ret); i++)
3842 {
3843 if (lynq_cmd_ret[i] == ' ')
3844 {
you.chen35020192022-05-06 11:30:57 +08003845 lynq_cmd_ret[i] = '\0';
3846 break;
3847 }
3848 }
3849
3850 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003851 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08003852
3853 int ret=pclose(fp);
3854 if(ret==-1)
3855 {
3856 perror("close file faild");
3857 }
3858
3859 return 0;
3860}
3861
you.chen705a7ef2023-06-01 22:06:45 +08003862static int check_and_init_uci_config(char * country_code)
3863{
3864 FILE * fp;
3865 int is_different = 0;
3866 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
3867 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
3868 const char * commit_uci_cmd ="uci commit";
3869 char set_country_cmd[MAX_CMD];
3870 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08003871
you.chen705a7ef2023-06-01 22:06:45 +08003872 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08003873
you.chen705a7ef2023-06-01 22:06:45 +08003874 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08003875 {
you.chen705a7ef2023-06-01 22:06:45 +08003876 if (0 != system(create_uci_cmd))
3877 {
3878 RLOGE("creat_uci_cmd fail");
3879 return -1;
3880 }
3881 is_different = 1;
3882 }
3883
3884 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
3885 {
3886 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08003887 return -1;
3888 }
3889
you.chen705a7ef2023-06-01 22:06:45 +08003890 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08003891 {
you.chen705a7ef2023-06-01 22:06:45 +08003892 RLOGE("fread fail!");
3893 fclose(fp);
3894 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08003895 }
3896
you.chen705a7ef2023-06-01 22:06:45 +08003897 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
3898 {
3899 RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
3900 is_different = 1;
3901 }
3902
3903 fclose(fp);
3904
3905 if (is_different)
3906 {
3907 if ( 0 != system(set_country_cmd))
3908 {
3909 RLOGE("set_country_cmd fail");
3910 return -1;
3911 }
3912 if (0 != system(commit_uci_cmd))
3913 {
3914 RLOGE("commmit fail");
3915 }
3916 }
3917
3918 return is_different;
3919}
3920
3921int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
3922 char check_current_code[10];
3923 const char * support_country[] = {"CN", "EU"};
3924
3925 int ret,is_different, i, cc_count;
3926
3927 if (country_code == NULL || country_code[0] == '\0')
3928 {
3929 RLOGE("bad country code\n");
3930 return -1;
3931 }
3932
3933 cc_count = sizeof (support_country) / sizeof (char*);
3934 for(i=0; i < cc_count; i++)
3935 {
3936 if (strcmp(support_country[i], country_code) == 0)
3937 {
3938 break;
3939 }
3940 }
3941
3942 if (i >= cc_count)
3943 {
3944 RLOGE("unspported country code %s\n", country_code);
3945 return -1;
3946 }
3947
3948 is_different = check_and_init_uci_config(country_code);
3949 if( is_different < 0 )
3950 {
3951 RLOGE("init set uci fail\n");
3952 return -1;
3953 }
3954
3955 ret = lynq_get_country_code(idx,check_current_code);
3956 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
3957 {
3958 ret = lynq_wifi_disable();
3959 if(ret != 0 )
3960 {
3961 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
3962 return -1;
3963 }
3964 }
3965
3966 return 0;
you.chen35020192022-05-06 11:30:57 +08003967}
3968
3969int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
3970{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003971 RLOGD("enter lynq_get_connect_ap_mac\n");
3972 if (mac == NULL)
3973 {
3974 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08003975 return -1;
3976 }
3977
3978 CHECK_IDX(idx, CTRL_STA);
3979 ap_info_s ap;
3980 ap.ap_mac[0] = '\0';
3981
qs.xiong9fbf74e2023-03-28 13:38:22 +08003982 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
3983 {
you.chen35020192022-05-06 11:30:57 +08003984 return -1;
3985 }
3986 strcpy(mac, ap.ap_mac);
3987
3988 return 0;
3989}
3990
3991int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
3992{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003993 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08003994 struct ifaddrs *ifaddr_header, *ifaddr;
3995 struct in_addr * ifa;
3996 const char * ifaName = "wlan0";
3997 if (ip == NULL)
3998 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003999 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08004000 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004001 }
you.chenf58b3c92022-06-21 16:53:48 +08004002
qs.xiong9fbf74e2023-03-28 13:38:22 +08004003 if (idx == 1)
4004 {
you.chen0df3e7e2023-05-10 15:56:26 +08004005 ifaName = inner_get_ap_interface_name();
4006 if (ifaName == NULL)
4007 {
4008 RLOGE("[lynq_get_interface_ip] ap name get fail");
4009 return -1;
4010 }
you.chen9ac66392022-08-06 17:01:16 +08004011 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004012 else if (idx != 0)
4013 {
you.chen35020192022-05-06 11:30:57 +08004014 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004015 }
you.chen35020192022-05-06 11:30:57 +08004016
you.chen9ac66392022-08-06 17:01:16 +08004017 if (getifaddrs(&ifaddr_header) == -1)
4018 {
you.chen35020192022-05-06 11:30:57 +08004019 perror("getifaddrs");
4020 return -1;
4021 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004022 }
you.chen35020192022-05-06 11:30:57 +08004023
4024
you.chen9ac66392022-08-06 17:01:16 +08004025 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4026 {
4027 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004028 continue;
you.chen9ac66392022-08-06 17:01:16 +08004029 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4030 {
4031 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4032 {
4033 // is a valid IP4 Address
4034 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4035 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004036 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004037 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004038 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004039 return 0;
4040 }
4041 }
4042 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004043 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004044 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004045 return -1;
you.chen35020192022-05-06 11:30:57 +08004046}
4047
4048int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4049{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004050 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004051 int count;
4052 size_t i;
4053 char *split_words[128] = {0};
4054 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4055
4056 CHECK_WPA_CTRL(idx);
4057
4058 DO_REQUEST(lynq_get_mac_cmd);
4059
qs.xiong9fbf74e2023-03-28 13:38:22 +08004060 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4061 {
4062 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004063 return -1;
4064 }
4065
4066 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4067
qs.xiong9fbf74e2023-03-28 13:38:22 +08004068 if (count < 2)
4069 {
you.chen35020192022-05-06 11:30:57 +08004070 return -1;
4071 }
4072
qs.xiong9fbf74e2023-03-28 13:38:22 +08004073 for (i=0; i < strlen(split_words[1]); i++ )
4074 {
4075 if (split_words[1][i] != ' ')
4076 {
you.chen35020192022-05-06 11:30:57 +08004077 break;
4078 }
4079 }
4080
4081 strcpy(mac, split_words[1] + i);
4082
4083 return 0;
4084}
4085
4086int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4087{
4088// int count;
4089// char *split_words[128] = {0};
4090// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4091
4092// if (rssi == NULL) {
4093// return -1;
4094// }
4095
4096// CHECK_IDX(idx, CTRL_STA);
4097
4098// CHECK_WPA_CTRL(CTRL_STA);
4099
4100// DO_REQUEST(lynq_get_rssi_cmd);
4101
4102// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4103// return -1;
4104// }
4105
4106// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4107
4108// if (count < 2) {
4109// return -1;
4110// }
4111
4112// *rssi = atoi(split_words[1]) * -1;
4113
you.chen35020192022-05-06 11:30:57 +08004114 char lynq_cmd_ret[MAX_RET]={0};
4115
qs.xiongff0ae0f2022-10-11 15:47:14 +08004116/*******change other cmd to get rssi*******
4117 *
4118 *wl rssi ---> wl -i wlan0 rssi
4119 *
4120 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004121 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004122 {
you.chen23c4a5f2023-04-12 16:46:00 +08004123 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004124 return -1;
4125 }
you.chen9f17e4d2022-06-06 17:18:18 +08004126 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004127/****** if got rssi is 0,means sta didn't connected any device****/
4128 if(*rssi == 0)
4129 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004130 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004131 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004132 }
you.chen35020192022-05-06 11:30:57 +08004133
4134 return 0;
4135}
4136
4137int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4138{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004139 RLOGD("enter lynq_get_connect_ap_band\n");
4140 if (band == NULL)
4141 {
you.chen35020192022-05-06 11:30:57 +08004142 return -1;
4143 }
4144
4145 CHECK_IDX(idx, CTRL_STA);
4146 ap_info_s ap;
4147 ap.band = -1;
4148
qs.xiong9fbf74e2023-03-28 13:38:22 +08004149 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4150 {
you.chen35020192022-05-06 11:30:57 +08004151 return -1;
4152 }
4153 *band = ap.band;
4154
4155 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004156}
you.chenf58b3c92022-06-21 16:53:48 +08004157
4158int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4159{
you.chenb95401e2023-05-12 19:39:06 +08004160 int ret;
4161 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004162 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004163
4164 if (ip == NULL)
4165 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004166 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004167 return -1;
4168 }
4169
4170 CHECK_IDX(idx, CTRL_STA);
4171
qs.xionge4cbf1c2023-02-28 18:22:49 +08004172 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004173 {
4174 return -1;
4175 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004176
you.chenb95401e2023-05-12 19:39:06 +08004177 ip[0] = '\0';
4178 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4179 if (ret != 0)
4180 {
4181 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4182 return -1;
4183 }
4184
4185 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4186 {
4187 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004188 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004189 if (ret != 0)
4190 {
4191 ip[0] = '\0';
4192 return 0;
4193 }
4194 else
4195 {
4196 p = strchr(ip, '\n');
4197 if (p != NULL)
4198 {
4199 *p = '\0';
4200 }
4201 }
4202 }
4203 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004204}
4205
qs.xiong026c5c72022-10-17 11:15:45 +08004206int lynq_ap_connect_num(int sta_number)
4207{
4208 char lynq_limit_cmd[32]={0};
4209 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004210 if((sta_number < 1 ) && (sta_number > 15))
4211 {
4212 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004213 return -1;
4214 }
4215 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4216 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004217 if(ret != 0)
4218 {
4219 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004220 }
4221 return 0;
4222}
you.chenf58b3c92022-06-21 16:53:48 +08004223
qs.xiong77905552022-10-17 11:19:57 +08004224int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4225{
4226
4227 char lynq_wifi_acs_cmd[128]={0};
4228 char lynq_cmd_mode[128]={0};
4229 char lynq_cmd_slect[128]={0};
4230
qs.xiong9fbf74e2023-03-28 13:38:22 +08004231 if((acs_mode != 2) && (acs_mode != 5))
4232 {
qs.xiong77905552022-10-17 11:19:57 +08004233 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4234 }
4235
qs.xiong9fbf74e2023-03-28 13:38:22 +08004236 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4237 {
qs.xiong77905552022-10-17 11:19:57 +08004238 return -1;
4239 }
4240
4241 CHECK_IDX(idx, CTRL_AP);
4242
4243 CHECK_WPA_CTRL(CTRL_AP);
4244
4245 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4246 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4247 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4248
4249 DO_OK_FAIL_REQUEST(cmd_disconnect);
4250 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4251 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4252 DO_OK_FAIL_REQUEST(cmd_save_config);
4253 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4254
4255 return 0;
4256}
you.chen0f5c6432022-11-07 18:31:14 +08004257//you.chen add for tv-box start
4258static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4259 FILE *fp;
4260 //printf("to exec cmd:%s\n", str_cmd);
4261 if((fp=popen(str_cmd,"r"))==NULL)
4262 {
4263 perror("popen error!");
4264 return -1;
4265 }
4266 if((fread(str_cmd_ret,max_len,1,fp))<0)
4267 {
4268 perror("fread fail!");
4269 fclose(fp);
4270 return -1;
4271 }
4272 fclose(fp);
4273 return 0;
4274}
4275
4276static int get_netmask_length(const char* mask)
4277{
4278 int masklen=0, i=0;
4279 int netmask=0;
4280
4281 if(mask == NULL)
4282 {
4283 return 0;
4284 }
4285
4286 struct in_addr ip_addr;
4287 if( inet_aton(mask, &ip_addr) )
4288 {
4289 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004290 }else
4291 {
you.chen0f5c6432022-11-07 18:31:14 +08004292 netmask = 0;
4293 return 0;
4294 }
4295
4296 while(0 == (netmask & 0x01) && i<32)
4297 {
4298 i++;
4299 netmask = netmask>>1;
4300 }
4301 masklen = 32-i;
4302 return masklen;
4303}
4304
4305static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4306 int mask_len;
4307 char *p;
4308 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004309 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4310 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004311 return -1;
4312 p = strstr(str_cmd_ret, "Mask:");
4313 if (p == NULL)
4314 return -1;
4315 mask_len = get_netmask_length(p + 5);
4316 if (mask_len == 0)
4317 return -1;
4318 p = strstr(str_cmd_ret, "inet addr:");
4319 if (p == NULL)
4320 return -1;
4321 strcpy(tmp, p + 10);
4322 p = strstr(tmp, " ");
4323 if (p != NULL)
4324 *p = '\0';
4325 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4326 return 0;
4327}
4328
4329static void GBWWatchThreadProc() {
4330 int i,n, nloop, nmax, ncheckcount, nidlecount;
4331 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4332 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4333 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4334 char *results[16] = {0};
4335 char str_cmd[256] = {0};
4336 char str_cmd_ret[128] = {0};
4337 char dest_ip[32] = {0};
4338 lastAP1Bytes = lastAP2Bytes = 0;
4339 lastAP1Drop = lastAP2Drop = 0;
4340 lastAP1Speed = lastAP2Speed = 0;
4341 setAP1Speed = 50;
4342 setAP2Speed = 80;
4343 nloop = 0;
4344 nmax = 6;
4345 ncheckcount = nidlecount = 0;
4346
you.chen0df3e7e2023-05-10 15:56:26 +08004347 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004348 {
4349 RLOGE("------gbw thread run\n");
4350 return;
4351 }
4352
qs.xiong9fbf74e2023-03-28 13:38:22 +08004353 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004354 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4355 while (dest_ip[0] == '\0') {
4356 sleep(1);
4357 str_cmd_ret[0] = '\0';
4358 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4359 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4360 if (str_cmd_ret[n] == '\n'){
4361 str_cmd_ret[n] = '\0';
4362 break;
4363 }
4364 }
4365 if (str_cmd_ret[0] != '\0')
4366 {
4367 strcpy(dest_ip, str_cmd_ret);
4368 }
4369 }
4370
you.chenc9928582023-04-24 15:39:37 +08004371 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4372 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4373 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 +08004374 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4375 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004376 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004377 return;
4378 }
you.chenc9928582023-04-24 15:39:37 +08004379 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);
4380 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);
4381 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 +08004382
4383 while (1) {
4384 sleep(1);
4385 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004386 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4387 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4388 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004389 continue;
4390 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004391 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004392 if (n > 9) {
4393 if (strcmp(results[1], "Sent") == 0) {
4394 currAP1Bytes = atoll(results[2]);
4395 }
4396 if (strcmp(results[6], "(dropped") == 0) {
4397 currAP1Drop = atoi(results[7]);
4398 }
4399 }
4400
4401 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004402 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4403 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4404 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004405 continue;
4406 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004407 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004408 if (n > 9) {
4409 if (strcmp(results[1], "Sent") == 0) {
4410 currAP2Bytes = atoll(results[2]);
4411 }
4412 if (strcmp(results[6], "(dropped") == 0) {
4413 currAP2Drop = atoi(results[7]);
4414 }
4415 }
4416
4417 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4418 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4419 lastAP1Bytes = currAP1Bytes;
4420 lastAP2Bytes = currAP2Bytes;
4421 continue;
4422 }
4423
4424 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4425 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4426 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4427 lastAP1Speed = currAP1Speed;
4428 lastAP2Speed = currAP2Speed;
4429 lastAP1Bytes = currAP1Bytes;
4430 lastAP2Bytes = currAP2Bytes;
4431
4432 currSetAP1Speed = setAP1Speed;
4433 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4434 ncheckcount++;
4435 if (ncheckcount > 3) {
4436 ncheckcount = 0;
4437 currSetAP1Speed = 5;
4438 }
4439 }
4440 else {
4441 ncheckcount = 0;
4442 if (currAP1Speed < 5)
4443 nidlecount++;
4444 else
4445 nidlecount = 0;
4446
4447 }
4448
4449 if (nidlecount > 60 ){
4450 currSetAP1Speed = 50;
4451 }
4452
4453 if (currSetAP1Speed != setAP1Speed) {
4454 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004455 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4456 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004457 }
4458 }
4459}
4460
4461int enableGBW(const char* mac) {
4462 int i,len;
4463 char get_ipaddr_cmd[128]={0};
4464 ap_info_s *ap;
4465 device_info_s * list;
4466
4467 if (mac == NULL || g_gbw_enabled == 1)
4468 return -1;
4469 len = strlen(mac);
4470 g_gbw_mac = malloc(len + 1);
4471 for(i=0;i<len;i++) {
4472 if (mac[i] >= 'A' && mac[i] <= 'Z')
4473 {
4474 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4475 }
4476 else
4477 g_gbw_mac[i] = mac[i];
4478 }
4479 g_gbw_mac[i] = '\0';
4480 g_gbw_enabled = 1;
4481
4482 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4483 if (system(get_ipaddr_cmd) == 0) {
4484 //startGBW();
4485 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4486 for (i=0;i<len;i++) {
4487 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4488 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4489 startGBW();
4490 }
4491 free(ap);
4492 free(list);
4493 }
4494 }
4495 return 0;
4496}
4497
4498int disableGBW() {
4499 stopGBW();
4500 free(g_gbw_mac);
4501 g_gbw_mac = NULL;
4502 g_gbw_enabled = 1;
4503 return 0;
4504}
4505
4506static int startGBW() {
4507 if (g_gbw_watcher_pid != 0) {
4508 stopGBW();
4509 }
4510 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4511}
4512
4513static int stopGBW() {
4514 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004515 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004516 pthread_cancel(g_gbw_watcher_pid);
4517 pthread_join(g_gbw_watcher_pid, &retval);
4518 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004519 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4520 if (s_ap_iterface_name[0] != '\0')
4521 {
4522 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4523 system(cmd);
4524 }
you.chen0f5c6432022-11-07 18:31:14 +08004525}
4526//you.chen add for tv-box end