blob: 76c5d94c76a0fe2b322680cd463e41498f4fa7f2 [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;
1374 }
1375 hostname[0] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08001376 RLOGE("---gethostbyaddr fail\n");
you.chen35020192022-05-06 11:30:57 +08001377 herror(NULL);
1378 return -1;
1379 }
1380
1381 strcpy(hostname, ht->h_name);
1382
1383 return 0;
1384}
1385
1386static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1387{
1388 int count, index, words_count;
1389 char * split_lines[128]= {0};
1390 char * split_words[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08001391 char local_ssid[128] = {0};
you.chen35020192022-05-06 11:30:57 +08001392 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001393 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen35020192022-05-06 11:30:57 +08001394
1395 CHECK_WPA_CTRL(ap_sta);
1396
1397 DO_REQUEST(lynq_wifi_list_networks);
1398
1399 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1400
1401 //@todo check ssid field to compatible
1402
1403 ret = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001404 for(index=1; index < count; index++)
1405 {
you.chen35020192022-05-06 11:30:57 +08001406 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001407 if (words_count > 2)
1408 {
you.chend2fef3f2023-02-13 10:50:35 +08001409 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001410 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1411 {
you.chen35020192022-05-06 11:30:57 +08001412 net_no_list[ret++] = atoi(split_words[0]);
1413 }
1414 }
1415 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001416 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen35020192022-05-06 11:30:57 +08001417 return ret;
1418}
1419
1420static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001421 size_t i=0;
you.chen35020192022-05-06 11:30:57 +08001422 CHECK_WPA_CTRL(ap_sta);
1423 const char *lynq_wifi_add_network = "ADD_NETWORK";
1424
qs.xiong9fbf74e2023-03-28 13:38:22 +08001425 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen35020192022-05-06 11:30:57 +08001426 DO_REQUEST(lynq_wifi_add_network);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001427 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1428 {
1429 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen35020192022-05-06 11:30:57 +08001430 return -1;
1431 }
1432
qs.xiong9fbf74e2023-03-28 13:38:22 +08001433 for(i=0;i<reply_len;i++)
1434 {
1435 if(cmd_reply[i] == '\n')
1436 {
you.chen35020192022-05-06 11:30:57 +08001437 cmd_reply[i] = '\0';
1438 break;
1439 }
1440 }
1441 return atoi(cmd_reply);
1442}
you.chena6cd55a2022-05-08 12:20:18 +08001443
you.chen35020192022-05-06 11:30:57 +08001444static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1445{
1446 int count, index;
1447 int net_no_list[128];
1448
qs.xiong9fbf74e2023-03-28 13:38:22 +08001449 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen35020192022-05-06 11:30:57 +08001450 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001451 for (index=0; index < count; index++)
1452 {
1453 if (net_no_list[index] == net_no)
1454 {
you.chen35020192022-05-06 11:30:57 +08001455 return 0;
1456 }
1457 }
1458
1459 if (count >= 1)
1460 index = net_no_list[count - 1];
1461 else
1462 index = -1;
1463
qs.xiong9fbf74e2023-03-28 13:38:22 +08001464 while (index < net_no )
1465 {
you.chen35020192022-05-06 11:30:57 +08001466 index = lynq_add_network(ap_sta);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001467 if (index >= net_no)
1468 { // required network no created
1469 RLOGD("required network no created\n");;
you.chen35020192022-05-06 11:30:57 +08001470 return 0;
1471 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001472 else if( index < 0)
1473 {
1474 RLOGE("[lynq_check_network_number] add network fail");
you.chena6cd55a2022-05-08 12:20:18 +08001475 return -1;
1476 }
you.chen35020192022-05-06 11:30:57 +08001477 }
1478
1479 if (index < 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001480 {
1481 RLOGE("[lynq_check_network_number] network index < 0");
1482 return -1;
1483 }
1484 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen35020192022-05-06 11:30:57 +08001485 return 0;
1486}
1487
1488static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiong9fbf74e2023-03-28 13:38:22 +08001489 if (freq > 5000 && freq < 6000)
1490 {
you.chen35020192022-05-06 11:30:57 +08001491 return LYNQ_WIFI_5G_band;
1492 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001493 else if (freq > 2000 && freq < 3000)
1494 {
you.chen35020192022-05-06 11:30:57 +08001495 return LYNQ_WIFI_2G_band;
1496 }
1497 return LYNQ_WIFI_2_and_5G_band;
1498}
1499
1500static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001501 if (key_mgmt != NULL)
1502 {
1503 if (memcmp( key_mgmt, "NONE", 4) == 0)
1504 {
you.chen35020192022-05-06 11:30:57 +08001505 return LYNQ_WIFI_AUTH_OPEN;
1506 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001507 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1508 {
you.chen35020192022-05-06 11:30:57 +08001509 return LYNQ_WIFI_AUTH_WEP;
1510 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001511 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1512 {
you.chen35020192022-05-06 11:30:57 +08001513 return LYNQ_WIFI_AUTH_WPA_PSK;
1514 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001515 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1516 {
you.chen35020192022-05-06 11:30:57 +08001517 return LYNQ_WIFI_AUTH_WPA2_PSK;
1518 }
1519 }
1520
1521 return -1;
1522}
1523
1524static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001525 if (flag != NULL)
1526 {
qs.xiong46f41562023-07-11 21:06:47 +08001527 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 +08001528 {
1529 return LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong46f41562023-07-11 21:06:47 +08001530 }else if ( strstr( flag,"SAE-CCMP") != NULL || strstr(flag,"SAE-H2E") != NULL )
qs.xiong3e506812023-04-06 11:08:48 +08001531 {
1532 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1533 }else if (strstr( flag, "WPA2-PSK") != NULL)
1534 {
you.chen35020192022-05-06 11:30:57 +08001535 return LYNQ_WIFI_AUTH_WPA2_PSK;
1536 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001537 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001538 {
you.chen35020192022-05-06 11:30:57 +08001539 return LYNQ_WIFI_AUTH_WPA_PSK;
1540 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001541 else if (strstr( flag, "WEP") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001542 {
you.chen35020192022-05-06 11:30:57 +08001543 return LYNQ_WIFI_AUTH_WEP;
1544 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001545 else if (strstr( flag, "NONE") != NULL)
qs.xiong3e506812023-04-06 11:08:48 +08001546 {
you.chen35020192022-05-06 11:30:57 +08001547 return LYNQ_WIFI_AUTH_OPEN;
1548 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001549 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xiong3e506812023-04-06 11:08:48 +08001550 {
you.chend2fef3f2023-02-13 10:50:35 +08001551 return LYNQ_WIFI_AUTH_OPEN;
1552 }
qs.xiong46f41562023-07-11 21:06:47 +08001553 else
1554 {
1555 RLOGD("convert_max_auth_from_flag not-found auth mode");
1556 }
you.chen35020192022-05-06 11:30:57 +08001557 }
1558
1559 return -1;
1560}
1561
1562static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1563 switch (bw) {
1564 case 10:
1565 return LYNQ_WIFI_BANDWIDTH_HT10;
1566 break;
1567 case 20:
1568 return LYNQ_WIFI_BANDWIDTH_HT20;
1569 break;
1570 case 40:
1571 return LYNQ_WIFI_BANDWIDTH_HT40;
1572 break;
1573 case 80:
1574 return LYNQ_WIFI_BANDWIDTH_HT80;
1575 break;
1576 default:
1577 break;
1578 }
1579
1580 return -1;
1581}
1582
you.chen70f377f2023-04-14 18:17:09 +08001583static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen35020192022-05-06 11:30:57 +08001584static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1585 int i, count;
1586 char *p;
1587 const char *lynq_status_cmd = "STATUS";
1588 const char * FLAG_SSID = "ssid=";
1589 const char * FLAG_SBSID = "bssid=";
1590 const char * FLAG_KEY_MGMT = "key_mgmt=";
1591 const char * FLAG_FREQ = "freq=";
1592 const char * FLAG_STATE = "wpa_state=";
1593 const char * FLAG_ID = "id=";
you.chend2fef3f2023-02-13 10:50:35 +08001594 const char * FLAG_IPADDR = "ip_address=";
you.chen35020192022-05-06 11:30:57 +08001595 char *split_lines[128] = {0};
1596
1597 CHECK_WPA_CTRL(interface);
1598
qs.xiong9fbf74e2023-03-28 13:38:22 +08001599 if (curr_state == NULL)
1600 {
1601 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen35020192022-05-06 11:30:57 +08001602 return -1;
1603 }
1604
1605 DO_REQUEST(lynq_status_cmd);
1606
1607 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1608
1609 curr_state->net_no = -1;
1610 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001611 for(i=0; i < count; i++)
1612 {
1613 if (curr_state->ap != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001614 {
you.chen35020192022-05-06 11:30:57 +08001615 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001616 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001617 {
you.chend2fef3f2023-02-13 10:50:35 +08001618 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen35020192022-05-06 11:30:57 +08001619 ret = 0;
1620 continue;
1621 }
you.chenf58b3c92022-06-21 16:53:48 +08001622 p = strstr(split_lines[i], FLAG_SSID);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001623 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001624 {
you.chend2fef3f2023-02-13 10:50:35 +08001625 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 +08001626 ret = 0;
1627 continue;
1628 }
you.chen35020192022-05-06 11:30:57 +08001629 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001630 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001631 {
you.chen450d0172022-07-15 17:56:48 +08001632 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001633 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +08001634 ret = 0;
1635 continue;
1636 }
1637 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001638 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001639 {
you.chen35020192022-05-06 11:30:57 +08001640 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1641 ret = 0;
1642 continue;
1643 }
you.chend2fef3f2023-02-13 10:50:35 +08001644 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001645 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001646 {
you.chend2fef3f2023-02-13 10:50:35 +08001647 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1648 ret = 0;
1649 continue;
1650 }
you.chen35020192022-05-06 11:30:57 +08001651 } // end if (ap != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001652 if (curr_state->state != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001653 {
you.chen35020192022-05-06 11:30:57 +08001654 p = strstr(split_lines[i], FLAG_STATE);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001655 if (p != NULL)
you.chen70f377f2023-04-14 18:17:09 +08001656 {
you.chen35020192022-05-06 11:30:57 +08001657 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1658 ret = 0;
1659 continue;
1660 }
1661
1662 } //end else if (state != NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08001663 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chen70f377f2023-04-14 18:17:09 +08001664 {
you.chen35020192022-05-06 11:30:57 +08001665 ret = 0;
you.chen450d0172022-07-15 17:56:48 +08001666 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiong9fbf74e2023-03-28 13:38:22 +08001667 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen35020192022-05-06 11:30:57 +08001668 }
1669 }
1670
you.chen70f377f2023-04-14 18:17:09 +08001671 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1672 {
1673 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1674 }
1675
you.chen35020192022-05-06 11:30:57 +08001676 return ret;
1677}
1678
qs.xiongf1b525b2022-03-31 00:58:23 -04001679int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001680{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001681 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen35020192022-05-06 11:30:57 +08001682 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -05001683
qs.xiong9fbf74e2023-03-28 13:38:22 +08001684 if (ap_ssid == NULL)
1685 {
1686 RLOGE("Input ap_ssid is NULL");
you.chen35020192022-05-06 11:30:57 +08001687 return -1;
1688 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001689 else
1690 {
1691 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen35020192022-05-06 11:30:57 +08001692 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001693
qs.xiong9fbf74e2023-03-28 13:38:22 +08001694 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1695 {
1696 RLOGE("Do check ap network_number fail");
you.chen35020192022-05-06 11:30:57 +08001697 return -1;
1698 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001699
you.chen35020192022-05-06 11:30:57 +08001700 CHECK_IDX(idx, CTRL_AP);
1701
1702 CHECK_WPA_CTRL(CTRL_AP);
1703
1704 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1705
1706 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1707 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001708 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1709 return 0;
you.chen35020192022-05-06 11:30:57 +08001710
qs.xiong7a105ce2022-03-02 09:43:11 -05001711}
1712
you.chen35020192022-05-06 11:30:57 +08001713int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001714{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001715 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen35020192022-05-06 11:30:57 +08001716 CHECK_IDX(idx, CTRL_AP);
you.chend2fef3f2023-02-13 10:50:35 +08001717 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong7a105ce2022-03-02 09:43:11 -05001718}
1719
qs.xiongc9c79f72022-10-17 15:27:18 +08001720/*****
1721 *frequency <------>channel
1722 *
1723 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1724 *
1725 *
1726 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1727 *
1728 *
1729 * */
1730static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +08001731 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};
1732 int i;
1733 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1734
qs.xiong69a332b2022-12-02 09:58:57 +08001735 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +08001736 {
1737 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +08001738 break;
qs.xiongc9c79f72022-10-17 15:27:18 +08001739 }
qs.xiongc00b6032022-11-29 16:28:03 +08001740
1741 if(i == arr_len)
1742 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001743 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001744 return -1;
1745 }
qs.xiongc00b6032022-11-29 16:28:03 +08001746
qs.xiongc9c79f72022-10-17 15:27:18 +08001747 return 0;
1748}
qs.xiong13673462023-02-21 19:12:54 +08001749
1750static int lynq_check_frequencyby_country_code(int input_frequency)
1751{
1752 char str_cnc[]="CN";
1753 char str_dest[20]="";
1754
1755 if( lynq_get_country_code(1,str_dest) != 0 )
1756 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001757 RLOGE("get country_code error\n");
qs.xiong13673462023-02-21 19:12:54 +08001758 return -1;
1759 }
1760 if( strncmp(str_dest,str_cnc,2) != 0 )
1761 {
1762 return 0;
1763 }else if( 2473 < input_frequency && input_frequency < 5744)
1764 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001765 RLOGE("input frequency is bad\n");
qs.xiong13673462023-02-21 19:12:54 +08001766 return -1;
1767 }
1768 return 0;
1769}
qs.xiongf1b525b2022-03-31 00:58:23 -04001770int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001771{
qs.xiongc00b6032022-11-29 16:28:03 +08001772 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +08001773 char lynq_wifi_frequency_cmd[128]={0};
1774 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +08001775 char lynq_cmd_slect[128]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001776 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +08001777 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +08001778 check = lynq_check_set_frequency(lynq_wifi_frequency);
1779 if(check != 0)
1780 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001781 RLOGE("do check frequency error");
qs.xiongc9c79f72022-10-17 15:27:18 +08001782 return -1;
you.chen35020192022-05-06 11:30:57 +08001783 }
qs.xiong13673462023-02-21 19:12:54 +08001784 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
1785 if(check != 0)
1786 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001787 RLOGE("do check frequency error");
qs.xiong13673462023-02-21 19:12:54 +08001788 return -1;
1789 }
1790
qs.xiongc00b6032022-11-29 16:28:03 +08001791 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1792 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001793 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen35020192022-05-06 11:30:57 +08001794 return -1;
1795 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001796
you.chen35020192022-05-06 11:30:57 +08001797 CHECK_IDX(idx, CTRL_AP);
1798
1799 CHECK_WPA_CTRL(CTRL_AP);
1800
1801 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
1802 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
1803 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
1804
you.chen6c2dd9c2022-05-16 17:55:28 +08001805 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +08001806 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
1807 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
1808 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001809
qs.xiong9fbf74e2023-03-28 13:38:22 +08001810 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001811}
1812
qs.xiongf1b525b2022-03-31 00:58:23 -04001813int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -05001814{
you.chen35020192022-05-06 11:30:57 +08001815 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001816 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen35020192022-05-06 11:30:57 +08001817 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -04001818
qs.xiong9fbf74e2023-03-28 13:38:22 +08001819 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
1820 {
1821 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen35020192022-05-06 11:30:57 +08001822 return -1;
1823 }
1824 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -04001825
qs.xiong9fbf74e2023-03-28 13:38:22 +08001826 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001827}
1828
qs.xiongf1b525b2022-03-31 00:58:23 -04001829int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
1830{
qs.xiong9fbf74e2023-03-28 13:38:22 +08001831 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen35020192022-05-06 11:30:57 +08001832 CHECK_IDX(idx, CTRL_AP);
1833 switch(bandwidth){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001834 case LYNQ_WIFI_BANDWIDTH_HT10:
1835 {
1836 RLOGE("bandwith [%d] not support now\n", bandwidth);
1837 return -1;
1838 }
1839 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen35020192022-05-06 11:30:57 +08001840 {
1841 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
1842 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001843 if (system(lynq_cmd_bandwith) != 0 )
1844 {
1845 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001846 return -1;
1847 }
1848 system("wl up");
1849 break;
1850 }
1851 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001852 {
qs.xiong10379192023-02-21 13:19:42 +08001853 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen35020192022-05-06 11:30:57 +08001854 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1855 system("wl down");
qs.xiong9fbf74e2023-03-28 13:38:22 +08001856 if (system(lynq_cmd_bandwith) != 0 )
1857 {
1858 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen35020192022-05-06 11:30:57 +08001859 return -1;
1860 }
1861 system("wl up");
1862 break;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001863 }
you.chen35020192022-05-06 11:30:57 +08001864 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiong9fbf74e2023-03-28 13:38:22 +08001865 {
qs.xiong10379192023-02-21 13:19:42 +08001866 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen35020192022-05-06 11:30:57 +08001867 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 }
1876 default:
you.chen35020192022-05-06 11:30:57 +08001877 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08001878 RLOGE("auth type [%d] not support now\n", bandwidth);
1879 return -1;
you.chen35020192022-05-06 11:30:57 +08001880 }
1881 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001882
1883
you.chen35020192022-05-06 11:30:57 +08001884 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04001885}
you.chen35020192022-05-06 11:30:57 +08001886
qs.xiongf1b525b2022-03-31 00:58:23 -04001887int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1888{
you.chen35020192022-05-06 11:30:57 +08001889 int count = 0;
1890 int index = 0;
1891 char *split_words[128] = {0};
1892 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001893 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001894 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001895
you.chen35020192022-05-06 11:30:57 +08001896 CHECK_WPA_CTRL(CTRL_AP);
1897
1898 DO_REQUEST(lynq_chanspec_cmd);
1899
1900 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1901 for(;index < count; index++) {
1902 if (strncmp(split_words[index], "bw", 2) != 0) {
1903 continue;
1904 }
1905
1906 index++;
1907 if (index >= count) {
1908 return -1;
1909 }
1910
qs.xiong9fbf74e2023-03-28 13:38:22 +08001911 RLOGD("bw %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001912 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1913 return 0;
1914 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001915 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen35020192022-05-06 11:30:57 +08001916 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001917}
qs.xiong0fb469a2022-04-14 03:50:45 -04001918
qs.xiongf1b525b2022-03-31 00:58:23 -04001919int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001920{
you.chen35020192022-05-06 11:30:57 +08001921 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001922 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen35020192022-05-06 11:30:57 +08001923 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -04001924
you.chen35020192022-05-06 11:30:57 +08001925 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -04001926
qs.xiong9fbf74e2023-03-28 13:38:22 +08001927 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1928 {
you.chen35020192022-05-06 11:30:57 +08001929 return -1;
1930 }
1931
1932 system("wl down");
1933 if (system(lynq_cmd_channel) != 0 ){
qs.xiong9fbf74e2023-03-28 13:38:22 +08001934 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen35020192022-05-06 11:30:57 +08001935 return -1;
1936 }
1937 system("wl up");
1938 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001939}
qs.xiong0fb469a2022-04-14 03:50:45 -04001940
qs.xiongf1b525b2022-03-31 00:58:23 -04001941int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001942{
you.chen35020192022-05-06 11:30:57 +08001943 int count = 0;
1944 int index = 0;
1945 char *split_words[128] = {0};
1946 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiong9fbf74e2023-03-28 13:38:22 +08001947 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08001948 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001949
you.chen35020192022-05-06 11:30:57 +08001950 CHECK_WPA_CTRL(CTRL_AP);
1951
1952 DO_REQUEST(lynq_chanspec_cmd);
1953
1954 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiong9fbf74e2023-03-28 13:38:22 +08001955 for(;index < count; index++)
1956 {
1957 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen35020192022-05-06 11:30:57 +08001958 if (strncmp(split_words[index], "channel", 2) != 0) {
1959 continue;
1960 }
1961
1962 index++;
qs.xiong9fbf74e2023-03-28 13:38:22 +08001963 if (index >= count)
1964 {
you.chen35020192022-05-06 11:30:57 +08001965 return -1;
1966 }
1967
1968 *channel = atoi(split_words[index]);
1969 return 0;
1970 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08001971 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen35020192022-05-06 11:30:57 +08001972 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001973}
1974
1975
you.chen35020192022-05-06 11:30:57 +08001976int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001977{
you.chen6c2dd9c2022-05-16 17:55:28 +08001978 char ssid[MAX_CMD] = {0};
1979 int freq = 0;
1980 char lynq_auth_cmd[64]={0};
1981 char lynq_auth_alg_cmd[64]={0};
1982 char lynq_psk_cmd[64]={0};
1983 char lynq_pairwise_cmd[64]={0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08001984 char lynq_ieee80211_cmd[64]={0};
1985 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08001986 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001987 CHECK_IDX(idx, CTRL_AP);
1988
you.chen6c2dd9c2022-05-16 17:55:28 +08001989 CHECK_WPA_CTRL(CTRL_AP);
1990
qs.xiong9fbf74e2023-03-28 13:38:22 +08001991 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
1992 {
1993 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen35020192022-05-06 11:30:57 +08001994 return -1;
1995 }
1996
you.chen92fd5d32022-05-25 10:09:47 +08001997 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001998 if (org_auth == auth) {
qs.xiongc8d92a62023-03-29 17:36:14 +08001999 RLOGD("org_auth --- is %d\n",org_auth);
you.chen6c2dd9c2022-05-16 17:55:28 +08002000 return 0;
2001 }
2002 else {
2003 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
2004 ssid[0] = '\0';
2005 }
2006 lynq_wifi_ap_frequency_get(idx, &freq);
2007
2008 DO_OK_FAIL_REQUEST(cmd_disconnect);
2009 DO_OK_FAIL_REQUEST(cmd_remove_all);
2010 if (ssid[0] != '\0') {
2011 lynq_wifi_ap_ssid_set(idx, ssid);
2012 }
2013 if (freq != 0) {
2014 lynq_wifi_ap_frequency_set(idx, freq);
2015 }
2016 }
2017 }
you.chen35020192022-05-06 11:30:57 +08002018
qs.xiong9fbf74e2023-03-28 13:38:22 +08002019 switch(auth){
2020 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08002021 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002022 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen35020192022-05-06 11:30:57 +08002023 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002024 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002025 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002026 break;
2027 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002028 case LYNQ_WIFI_AUTH_WEP:
2029 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002030 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002031 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08002032 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08002033 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
2034
2035 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2036 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
2037 break;
2038 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002039 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiongc8d92a62023-03-29 17:36:14 +08002040 {
2041 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2042 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2043 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2044
2045 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2046 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2047 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2048 break;
2049
2050 }
you.chen35020192022-05-06 11:30:57 +08002051 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002052 {
2053 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2054 {
you.chen35020192022-05-06 11:30:57 +08002055 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
2056 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2057 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002058 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2059 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002060 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08002061 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08002062 }
2063// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2064// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
2065 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05002066
you.chen35020192022-05-06 11:30:57 +08002067 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2068 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2069 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002070 break;
2071 }
2072 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2073 {
2074 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2075 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
2076 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
2077 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2078
2079 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2080 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2081 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2082 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2083 break;
2084 }
2085 case LYNQ_WIFI_AUTH_WPA3_PSK:
2086 {
2087 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
2088 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xiong3e506812023-04-06 11:08:48 +08002089 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002090 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
2091
2092 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2093 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
2094 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2095 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2096 break;
2097 }
2098 default:
you.chen35020192022-05-06 11:30:57 +08002099 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002100 RLOGE("auth type [%d] not support now\n", auth);
2101 return -1;
you.chen35020192022-05-06 11:30:57 +08002102 }
2103 }
you.chen6c2dd9c2022-05-16 17:55:28 +08002104 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05002105
qs.xiong9fbf74e2023-03-28 13:38:22 +08002106 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002107}
2108
you.chen35020192022-05-06 11:30:57 +08002109int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05002110{
you.chen35020192022-05-06 11:30:57 +08002111 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002112 char lynq_auth_alg_str[MAX_RET] = {0};
2113 char lynq_proto_str[MAX_RET] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002114 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen35020192022-05-06 11:30:57 +08002115 CHECK_IDX(idx, CTRL_AP);
2116
qs.xiong9fbf74e2023-03-28 13:38:22 +08002117 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
2118 {
2119 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen35020192022-05-06 11:30:57 +08002120 return -1;
2121 }
2122
qs.xiong9fbf74e2023-03-28 13:38:22 +08002123 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
2124 {
2125 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
2126 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002127 RLOGD("---auth is OPEN\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002128 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiongc8d92a62023-03-29 17:36:14 +08002129 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002130 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002131 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
2132 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002133 RLOGD("---auth is WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002134 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiongc8d92a62023-03-29 17:36:14 +08002135 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002136 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002137 else
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 }
you.chen35020192022-05-06 11:30:57 +08002143 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002144 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
2145 {
2146 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
2147 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002148 RLOGE("---auth is -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002149 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08002150 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002151 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
2152 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002153 RLOGD("---auth WPA2_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002154 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002155 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002156 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002157 else
2158 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002159 RLOGD("---auth WPA_PSK\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002160 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002161 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002162 }
you.chen35020192022-05-06 11:30:57 +08002163 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002164
2165 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
2166 {
2167 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
2168 return -1;
2169 }
2170
2171 if (memcmp(lynq_auth_str,"1",1) == 0 )
2172 {
2173 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
2174 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002175 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002176 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
2177 {
2178 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
2179 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002180 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002181 }
2182 else
2183 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002184 RLOGE("---auth -- -1\n");
you.chen92fd5d32022-05-25 10:09:47 +08002185 *auth = -1;
2186 }
qs.xiong7a105ce2022-03-02 09:43:11 -05002187
you.chen6c2dd9c2022-05-16 17:55:28 +08002188 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002189}
qs.xiong1af5daf2022-03-14 09:12:12 -04002190
you.chenb95401e2023-05-12 19:39:06 +08002191static int inner_check_ap_connected(lynq_wifi_index_e idx, int retry_count)
2192{
2193 char status[64];
you.chencba13492023-05-19 13:53:43 +08002194 char LYNQ_WIFI_CMD[32]={0};
you.chenb95401e2023-05-12 19:39:06 +08002195 curr_status_info curr_state;
2196
2197 CHECK_WPA_CTRL(CTRL_AP);
2198
2199 memset(status, 0, sizeof (status));
2200
2201 curr_state.ap = NULL;
2202 curr_state.state = status;
2203
2204 printf("inner_check_ap_connected %d\n", retry_count);
2205 usleep(500*1000);
2206 if (0 == inner_get_status_info(idx, &curr_state))
2207 {
2208 if (strcmp(status, STATE_COMPLETED) == 0)
2209 {
2210 return 0;
2211 }
2212 else if (retry_count == 4) //not ok in 2s, do reconnect
2213 {
2214 DO_REQUEST("RECONNECT");
2215 return inner_check_ap_connected(idx, retry_count+1);
2216 }
you.chencba13492023-05-19 13:53:43 +08002217 else if (retry_count > 20)
you.chenb95401e2023-05-12 19:39:06 +08002218 {
2219 printf("retry 10 time\n");
2220 return -1;
2221 }
2222 else
2223 {
you.chen6d247052023-06-01 16:39:54 +08002224 if (strcmp(status, STATE_DISCONNECTED) == 0)
2225 {
2226 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2227 DO_REQUEST(LYNQ_WIFI_CMD);
2228 }
you.chenb95401e2023-05-12 19:39:06 +08002229 return inner_check_ap_connected(idx, retry_count+1);
2230 }
2231 }
2232 return -1;
2233}
qs.xiong1af5daf2022-03-14 09:12:12 -04002234
qs.xiongf1b525b2022-03-31 00:58:23 -04002235int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002236{
you.chen35020192022-05-06 11:30:57 +08002237 char LYNQ_WIFI_CMD[128]={0};
2238 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
2239 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002240 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen35020192022-05-06 11:30:57 +08002241 CHECK_IDX(idx, CTRL_AP);
2242
2243 CHECK_WPA_CTRL(CTRL_AP);
2244
you.chen0df3e7e2023-05-10 15:56:26 +08002245 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08002246 {
you.chen0df3e7e2023-05-10 15:56:26 +08002247 RLOGE("lynq_wifi_ap_start get ap name fail");
you.chenc9928582023-04-24 15:39:37 +08002248 return -1;
2249 }
you.chen35020192022-05-06 11:30:57 +08002250
2251 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
2252 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
2253
2254 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
2255 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2256
you.chenc9928582023-04-24 15:39:37 +08002257 ret = system_call_v("%s %s", start_stop_ap_script, "start");
2258 if (ret != 0)
2259 {
2260 RLOGE("lynq_wifi_ap_start excute script fail");
2261 return -1;
2262 }
2263
you.chenb95401e2023-05-12 19:39:06 +08002264 if (inner_check_ap_connected(idx, 0) != 0)
2265 {
2266 return -1;
2267 }
2268
you.chen0df3e7e2023-05-10 15:56:26 +08002269 check_tether_and_notify();
2270
qs.xiong9fbf74e2023-03-28 13:38:22 +08002271 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002272}
2273
qs.xiongf1b525b2022-03-31 00:58:23 -04002274int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002275{
you.chen35020192022-05-06 11:30:57 +08002276 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05002277}
2278
qs.xiongf1b525b2022-03-31 00:58:23 -04002279int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002280{
you.chen35020192022-05-06 11:30:57 +08002281 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04002282
you.chen35020192022-05-06 11:30:57 +08002283 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002284
you.chen35020192022-05-06 11:30:57 +08002285 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002286
you.chen35020192022-05-06 11:30:57 +08002287 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
2288
2289 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
2290
you.chenb4b121c2022-05-06 17:50:16 +08002291// system("connmanctl tether wifi off");
you.chenc9928582023-04-24 15:39:37 +08002292
2293 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
2294 if (ret != 0)
2295 {
2296 RLOGE("lynq_wifi_ap_start excute script fail");
2297 return -1;
2298 }
2299
qs.xiong9fbf74e2023-03-28 13:38:22 +08002300 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002301}
qs.xiong1af5daf2022-03-14 09:12:12 -04002302
qs.xiongf1b525b2022-03-31 00:58:23 -04002303int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002304{
you.chen35020192022-05-06 11:30:57 +08002305 char lynq_disable_cmd[128] = {0};
2306 char lynq_select_cmd[128] = {0};
2307 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002308 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen35020192022-05-06 11:30:57 +08002309 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002310
you.chen35020192022-05-06 11:30:57 +08002311 CHECK_WPA_CTRL(CTRL_AP);
you.chen35020192022-05-06 11:30:57 +08002312 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2313 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2314
2315 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2316 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
2317 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04002318
qs.xiong9fbf74e2023-03-28 13:38:22 +08002319 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002320}
2321
qs.xiongf1b525b2022-03-31 00:58:23 -04002322int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05002323{
you.chen35020192022-05-06 11:30:57 +08002324 char lynq_disable_cmd[128] = {0};
2325 char lynq_select_cmd[128] = {0};
2326 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiong9fbf74e2023-03-28 13:38:22 +08002327 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen35020192022-05-06 11:30:57 +08002328 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05002329
you.chen35020192022-05-06 11:30:57 +08002330 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002331
you.chen35020192022-05-06 11:30:57 +08002332 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
2333 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
2334
2335 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2336 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
2337 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05002338
qs.xiong9fbf74e2023-03-28 13:38:22 +08002339 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002340}
qs.xiongf1b525b2022-03-31 00:58:23 -04002341
you.chen35020192022-05-06 11:30:57 +08002342int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002343{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002344 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08002345 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002346 char lynq_wpa2_wpa3[64] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08002347 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiong9fbf74e2023-03-28 13:38:22 +08002348 RLOGD("enter lynq_ap_password_set");
2349 if( password == NULL )
2350 {
2351 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xionge7074322022-06-27 11:34:53 +08002352 return -1;
2353 }
2354 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08002355 lynq_wifi_auth_s auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002356 if(pass_len < 8 || pass_len >= 64)
2357 {
2358 RLOGE("[lynq_ap_password_set]input password len not in rage");
2359 return -1;
you.chen35020192022-05-06 11:30:57 +08002360 }
qs.xiongf1b525b2022-03-31 00:58:23 -04002361
you.chen35020192022-05-06 11:30:57 +08002362 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002363
qs.xiong9fbf74e2023-03-28 13:38:22 +08002364 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2365 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002366 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002367 return -1;
2368 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002369 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2370 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002371 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2372 return 0;
you.chen6c2dd9c2022-05-16 17:55:28 +08002373 }
2374
you.chen35020192022-05-06 11:30:57 +08002375 CHECK_WPA_CTRL(CTRL_AP);
2376
qs.xiong9fbf74e2023-03-28 13:38:22 +08002377 if (auth == LYNQ_WIFI_AUTH_WEP)
2378 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002379 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002380 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2381 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2382 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2383 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2384 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002385 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2386 {
qs.xiongc8d92a62023-03-29 17:36:14 +08002387 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 +08002388 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2389 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2390 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002391 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2392 {
2393
qs.xiongc8d92a62023-03-29 17:36:14 +08002394 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 +08002395 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiongfd771f42023-03-28 14:06:12 +08002396 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002397 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2398 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2399
2400 }
2401 else
qs.xiongc8d92a62023-03-29 17:36:14 +08002402 {
2403 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chen6c2dd9c2022-05-16 17:55:28 +08002404 return -1;
2405 }
you.chen35020192022-05-06 11:30:57 +08002406
you.chen35020192022-05-06 11:30:57 +08002407 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04002408
qs.xiong9fbf74e2023-03-28 13:38:22 +08002409 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -04002410}
2411
you.chen35020192022-05-06 11:30:57 +08002412int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04002413{
you.chen35020192022-05-06 11:30:57 +08002414 FILE * fp;
2415 int len, ret;
2416 int count, index;
2417 char *split_lines[128] = {0};
2418 char *buff, *p;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002419 RLOGD("enter lynq_ap_password_get");
qs.xiong97fa59b2022-04-07 05:41:29 -04002420
you.chen35020192022-05-06 11:30:57 +08002421 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04002422
you.chen35020192022-05-06 11:30:57 +08002423 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2424// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002425 if (NULL == fp)
2426 {
2427 RLOGE("open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002428 return -1;
2429 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002430
you.chen35020192022-05-06 11:30:57 +08002431 buff = alloca(MAX_RET);
2432 fseek(fp, 0, SEEK_SET);
2433 len = fread(buff, 1, MAX_RET, fp);
2434 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04002435
qs.xiong9fbf74e2023-03-28 13:38:22 +08002436 for(index=0; index < len; index ++)
2437 {
2438 if (memcmp(buff + index, "network={", 9) != 0)
2439 {
you.chen35020192022-05-06 11:30:57 +08002440 continue;
2441 }
2442 p = buff + index + 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002443 for (; index < len; index ++ )
2444 {
2445 if (buff[index] != '}')
2446 {
you.chen35020192022-05-06 11:30:57 +08002447 continue;
2448 }
2449 buff[index] = '\0';
2450 break;
2451 }
2452 len = buff + index - p;
2453 }
2454
2455 count = lynq_split(p, len, '\n', split_lines);
2456
2457 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002458 for(index=0; index < count; index++)
2459 {
you.chen35020192022-05-06 11:30:57 +08002460 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002461 if (p != NULL)
2462 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002463 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002464 if (*p == '\"')
2465 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002466 p++;
2467 }
you.chen35020192022-05-06 11:30:57 +08002468 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002469 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2470 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002471 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002472 if (*p == '\"')
2473 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002474 p++;
2475 }
2476 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002477 else
2478 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002479 continue;
you.chen35020192022-05-06 11:30:57 +08002480 }
2481
2482 strcpy(password, p);
2483
qs.xiong9fbf74e2023-03-28 13:38:22 +08002484 while(*password != '\0')
2485 {
2486 if (*password == '\"')
2487 {
you.chen35020192022-05-06 11:30:57 +08002488 *password = '\0';
2489 break;
2490 }
2491 password++;
2492 }
2493 ret = 0;
2494 break;
2495 } //end for(index=0; index < count; index++)
2496
2497 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05002498}
2499
you.chen35020192022-05-06 11:30:57 +08002500static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2501 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08002502 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04002503
qs.xiong9fbf74e2023-03-28 13:38:22 +08002504 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2505 {
you.chen35020192022-05-06 11:30:57 +08002506 return -1;
2507 }
2508
2509 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08002510
qs.xiong9fbf74e2023-03-28 13:38:22 +08002511 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2512 {
2513 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chen70f377f2023-04-14 18:17:09 +08002514 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002515 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chen70f377f2023-04-14 18:17:09 +08002516 {
you.chena6cd55a2022-05-08 12:20:18 +08002517 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002518 return 0;
you.chena6cd55a2022-05-08 12:20:18 +08002519 }
you.chen70f377f2023-04-14 18:17:09 +08002520 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2521 {
2522 return 0;
2523 }
you.chena6cd55a2022-05-08 12:20:18 +08002524 }
2525 }
you.chen70f377f2023-04-14 18:17:09 +08002526 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2527 {
2528 return 0;
2529 }
2530
qs.xiong9fbf74e2023-03-28 13:38:22 +08002531 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2532 {
you.chen70f377f2023-04-14 18:17:09 +08002533 RLOGE("check ieee80211w error\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002534 return -1;
2535 }
2536 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2537 {
2538
you.chen70f377f2023-04-14 18:17:09 +08002539 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2540 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002541 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2542 {
2543
2544 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiongc8d92a62023-03-29 17:36:14 +08002545 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002546 }else
2547 {
you.chen70f377f2023-04-14 18:17:09 +08002548 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002549 *auth = -1;
2550 return -1;
2551 }
you.chen35020192022-05-06 11:30:57 +08002552 return 0;
2553}
2554
2555int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05002556{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002557 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002558 int pass_len, net_no, count, index;
2559 char lynq_tmp_cmd[300]={0};
2560 int net_no_list[128];
2561 lynq_wifi_auth_s net_auth;
2562 pass_len=strlen(password);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002563 if(pass_len < 8 || pass_len >= 64)
2564 {
2565 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen35020192022-05-06 11:30:57 +08002566 return -1;
2567 }
2568
2569 CHECK_IDX(idx, CTRL_STA);
2570
2571 net_no = -1;
2572 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2573
qs.xiong9fbf74e2023-03-28 13:38:22 +08002574 for (index=0; index < count; index++)
2575 {
you.chen35020192022-05-06 11:30:57 +08002576 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002577 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2578 {
you.chen35020192022-05-06 11:30:57 +08002579 net_no = net_no_list[index];
2580 break;
2581 }
2582 }
2583
qs.xiong9fbf74e2023-03-28 13:38:22 +08002584 if (net_no < 0)
2585 {
you.chen35020192022-05-06 11:30:57 +08002586 return -1;
2587 }
2588
2589 CHECK_WPA_CTRL(CTRL_STA);
2590
2591 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2592
2593 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2594 DO_OK_FAIL_REQUEST(cmd_save_config);
2595
2596 return 0;
2597}
2598
2599int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2600
2601 FILE * fp;
you.chend2fef3f2023-02-13 10:50:35 +08002602 int len, ret, network_len, i, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002603 int count, index;
2604 char *split_lines[128] = {0};
you.chend2fef3f2023-02-13 10:50:35 +08002605 char *buff, *p, *ssid, *ssid_end_flag;
2606 char tmp_ssid[128]={0};
you.chen6d247052023-06-01 16:39:54 +08002607 RLOGD("enter lynq_sta_ssid_password_get");
you.chen35020192022-05-06 11:30:57 +08002608
you.chen755332b2022-08-06 16:59:10 +08002609 network_len = 0;
2610 p = NULL;
you.chen6d247052023-06-01 16:39:54 +08002611 buff = NULL;
you.chen755332b2022-08-06 16:59:10 +08002612
you.chen35020192022-05-06 11:30:57 +08002613 CHECK_IDX(idx, CTRL_STA);
2614
qs.xiong9fbf74e2023-03-28 13:38:22 +08002615 if (NULL == password)
2616 {
2617 RLOGE("bad param\n");
you.chen755332b2022-08-06 16:59:10 +08002618 return -1;
2619 }
2620
you.chen35020192022-05-06 11:30:57 +08002621 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002622 if (NULL == fp)
2623 {
2624 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen35020192022-05-06 11:30:57 +08002625 return -1;
2626 }
2627
you.chen6d247052023-06-01 16:39:54 +08002628 fseek(fp, 0, SEEK_END);
2629 len = ftell(fp);
2630 buff = malloc(len + 1);
2631
2632 if (buff == NULL)
2633 {
2634 RLOGE("[lynq_sta_ssid_password_get] malloc memory [%d] fail\n", len);
2635 return -1;
2636 }
2637
you.chen35020192022-05-06 11:30:57 +08002638 fseek(fp, 0, SEEK_SET);
you.chen6d247052023-06-01 16:39:54 +08002639 len = fread(buff, 1, len, fp);
you.chen35020192022-05-06 11:30:57 +08002640 fclose(fp);
2641
qs.xiong9fbf74e2023-03-28 13:38:22 +08002642 for(index=0; index < len; index ++)
2643 {
2644 for(; index < len; index ++)
you.chen6d247052023-06-01 16:39:54 +08002645 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002646 if (memcmp(buff + index, "network={", 9) != 0)
you.chen6d247052023-06-01 16:39:54 +08002647 {
you.chen35020192022-05-06 11:30:57 +08002648 continue;
2649 }
you.chen6d247052023-06-01 16:39:54 +08002650 p = buff + index + 9;
2651 for (; index < len; index ++ )
2652 {
2653 if (buff[index] != '}')
2654 {
2655 continue;
2656 }
2657 buff[index] = '\0';
2658 break;
2659 }
2660 network_len = buff + index - p;
2661 break;
you.chen35020192022-05-06 11:30:57 +08002662 }
2663
qs.xiongb3f26af2023-02-17 18:41:07 +08002664 if (p == NULL)
you.chen6d247052023-06-01 16:39:54 +08002665 {
2666 if (buff != NULL)
2667 {
2668 free(buff);
2669 }
2670
qs.xiongb3f26af2023-02-17 18:41:07 +08002671 return -1;
you.chen6d247052023-06-01 16:39:54 +08002672 }
qs.xiongb3f26af2023-02-17 18:41:07 +08002673
you.chend2fef3f2023-02-13 10:50:35 +08002674 ssid = strstr(p, "ssid=");
2675 if (ssid != NULL) {
2676 ssid += strlen("ssid=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002677 if (ssid[0] == '\"')
you.chen6d247052023-06-01 16:39:54 +08002678 {
you.chend2fef3f2023-02-13 10:50:35 +08002679 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2680 break;
2681 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002682 else
you.chen6d247052023-06-01 16:39:54 +08002683 {
you.chend2fef3f2023-02-13 10:50:35 +08002684 ssid_end_flag = strstr(ssid, "\n");
2685 if (ssid_end_flag != NULL)
2686 {
2687 ssid_len = (ssid_end_flag - ssid) / 2;
2688 for(i=0; i<ssid_len; i++)
2689 {
2690 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2691 }
2692 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2693 break;
2694 }
2695 }
you.chen35020192022-05-06 11:30:57 +08002696 }
you.chend2fef3f2023-02-13 10:50:35 +08002697
you.chen35020192022-05-06 11:30:57 +08002698 }
2699
qs.xiong9fbf74e2023-03-28 13:38:22 +08002700 if (index >= len || NULL == p || network_len <= 0)
2701 {
you.chen6d247052023-06-01 16:39:54 +08002702 if (buff != NULL)
2703 {
2704 free(buff);
2705 }
you.chen35020192022-05-06 11:30:57 +08002706 return -1;
2707 }
2708
you.chen755332b2022-08-06 16:59:10 +08002709 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08002710
2711 ret = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002712 for(index=0; index < count; index++)
2713 {
you.chen35020192022-05-06 11:30:57 +08002714 p = strstr(split_lines[index], "psk=");
qs.xiong9fbf74e2023-03-28 13:38:22 +08002715 if (p != NULL)
you.chen6d247052023-06-01 16:39:54 +08002716 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002717 p += 4;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002718 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002719 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002720 p++;
2721 }
you.chen35020192022-05-06 11:30:57 +08002722 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002723 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
you.chen6d247052023-06-01 16:39:54 +08002724 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002725 p += 9;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002726 if (*p == '\"')
you.chen6d247052023-06-01 16:39:54 +08002727 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002728 p++;
2729 }
2730 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002731 else
you.chen6d247052023-06-01 16:39:54 +08002732 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002733 continue;
you.chen35020192022-05-06 11:30:57 +08002734 }
2735
qs.xiong13673462023-02-21 19:12:54 +08002736 if (*p == '\"')
2737 p++;
2738 strncpy(password, p, 64);
you.chen35020192022-05-06 11:30:57 +08002739
qs.xiong13673462023-02-21 19:12:54 +08002740 p = password;
qs.xiong9fbf74e2023-03-28 13:38:22 +08002741 while(password - p < 64 && *password != '\0')
you.chen6d247052023-06-01 16:39:54 +08002742 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08002743 if (*password == '\"')
you.chen6d247052023-06-01 16:39:54 +08002744 {
you.chen35020192022-05-06 11:30:57 +08002745 *password = '\0';
2746 break;
2747 }
2748 password++;
2749 }
2750 ret = 0;
2751 break;
2752 } //end for(index=0; index < count; index++)
2753
you.chen6d247052023-06-01 16:39:54 +08002754 if (buff != NULL)
2755 {
2756 free(buff);
2757 }
2758
you.chen35020192022-05-06 11:30:57 +08002759 return ret;
2760}
2761
2762static int inner_set_sta_ssid(int net_no, char *sta_ssid)
2763{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002764 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04002765
qs.xiong9fbf74e2023-03-28 13:38:22 +08002766 if (sta_ssid == NULL)
2767 {
2768 RLOGE("sta_ssid is null\n");
2769 return -1;
you.chen35020192022-05-06 11:30:57 +08002770 }
2771
qs.xiong9fbf74e2023-03-28 13:38:22 +08002772 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08002773
2774 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
2775
2776 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2777// DO_OK_FAIL_REQUEST(cmd_save_config);
2778
qs.xiong9fbf74e2023-03-28 13:38:22 +08002779 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05002780
2781}
2782
you.chen35020192022-05-06 11:30:57 +08002783static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05002784{
you.chen35020192022-05-06 11:30:57 +08002785 char lynq_disable_cmd[128]={0};
2786 char lynq_select_cmd[128]={0};
2787
2788 CHECK_WPA_CTRL(CTRL_STA);
2789
qs.xiong9fbf74e2023-03-28 13:38:22 +08002790 if (save != 0)
2791 {
you.chenc29444e2022-06-07 18:01:16 +08002792 if (start_flag != 0)
2793 {
2794 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
2795 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2796 }
2797 else
2798 {
2799 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
2800 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2801 }
you.chen35020192022-05-06 11:30:57 +08002802 DO_OK_FAIL_REQUEST(cmd_save_config);
2803 }
2804
qs.xiong9fbf74e2023-03-28 13:38:22 +08002805 if (start_flag == 0)
2806 {
you.chen6c2dd9c2022-05-16 17:55:28 +08002807 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08002808 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2809 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002810 else
2811 {
you.chen35020192022-05-06 11:30:57 +08002812 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
2813 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2814 }
2815
2816 return 0;
2817}
2818
2819int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
2820{
qs.xiong9fbf74e2023-03-28 13:38:22 +08002821 RLOGD("enter lynq_sta_ssid_password_set");
you.chen35020192022-05-06 11:30:57 +08002822 CHECK_IDX(idx, CTRL_STA);
2823
you.chen6c2dd9c2022-05-16 17:55:28 +08002824 curr_status_info curr_state;
2825 ap_info_s ap_info;
2826 curr_state.ap = &ap_info;
2827 curr_state.state = NULL;
2828
qs.xiong9fbf74e2023-03-28 13:38:22 +08002829 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
2830 {
you.chend2fef3f2023-02-13 10:50:35 +08002831 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chen6c2dd9c2022-05-16 17:55:28 +08002832 return 0;
2833 }
2834
2835 return -1;
you.chen35020192022-05-06 11:30:57 +08002836}
2837
2838int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
2839{
you.chen9ac66392022-08-06 17:01:16 +08002840 scan_info_s *scan_list = NULL;
2841 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08002842 int scan_len=0;
2843 int save_len=0;
2844 int best_index = -1;
2845 int best_scan_index = -1;
2846 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08002847 int i, j, ret;
2848
2849 ret = -1;
you.chen35020192022-05-06 11:30:57 +08002850
2851 CHECK_IDX(idx, CTRL_STA);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002852 if (info == NULL)
2853 {
you.chen35020192022-05-06 11:30:57 +08002854 return -1;
2855 }
2856
2857 curr_status_info curr_state;
2858 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08002859 char status[64];
you.chen35020192022-05-06 11:30:57 +08002860
you.chen9ac66392022-08-06 17:01:16 +08002861 memset(&ap_info, 0, sizeof (ap_info));
2862 memset(status, 0, sizeof (status));
2863
2864 curr_state.ap = &ap_info;
2865 curr_state.state = status;
2866
qs.xiong9fbf74e2023-03-28 13:38:22 +08002867 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
2868 {
you.chen35020192022-05-06 11:30:57 +08002869 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08002870 if (strcmp(status, STATE_COMPLETED) == 0)
2871 {
2872 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
2873 }
2874 else
2875 {
2876 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2877 }
you.chen593621d2023-04-27 17:52:44 +08002878 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen35020192022-05-06 11:30:57 +08002879 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08002880 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002881 return 0;
2882 }
2883
you.chen9ac66392022-08-06 17:01:16 +08002884 lynq_wifi_sta_start_scan(idx);
qs.xiong3e506812023-04-06 11:08:48 +08002885 sleep(2);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002886 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
2887 {
you.chen9ac66392022-08-06 17:01:16 +08002888 if (NULL != scan_list)
2889 {
2890 free(scan_list);
2891 }
you.chen35020192022-05-06 11:30:57 +08002892 return -1;
2893 }
2894
qs.xiong9fbf74e2023-03-28 13:38:22 +08002895 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
2896 {
you.chen9ac66392022-08-06 17:01:16 +08002897 if (NULL != scan_list)
2898 {
2899 free(scan_list);
2900 }
2901 if (NULL != save_list)
2902 {
2903 free(save_list);
2904 }
you.chen35020192022-05-06 11:30:57 +08002905 return -1;
2906 }
2907
qs.xiong9fbf74e2023-03-28 13:38:22 +08002908 for (i=0; i < save_len; i++)
2909 {
2910 for (j=0; j < scan_len; j++)
2911 {
you.chen35020192022-05-06 11:30:57 +08002912 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiong9fbf74e2023-03-28 13:38:22 +08002913 && save_list[i].base_info.auth == scan_list[j].auth)
2914 {
2915 if (best_rssi == 0)
2916 {
you.chen9ac66392022-08-06 17:01:16 +08002917 best_index = i;
you.chen35020192022-05-06 11:30:57 +08002918 best_rssi = scan_list[j].rssi;
2919 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002920 else if (best_rssi > scan_list[j].rssi)
2921 {
you.chen35020192022-05-06 11:30:57 +08002922 best_index = i;
2923 best_scan_index = j;
2924 best_rssi = scan_list[j].rssi;
2925 }
you.chend2fef3f2023-02-13 10:50:35 +08002926 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 +08002927 break;
2928 }
2929 }
2930 }
2931
qs.xiong9fbf74e2023-03-28 13:38:22 +08002932 if (best_index >= 0)
2933 {
you.chen35020192022-05-06 11:30:57 +08002934 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chend2fef3f2023-02-13 10:50:35 +08002935 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 +08002936 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2937 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08002938 ret = 0;
you.chen35020192022-05-06 11:30:57 +08002939 }
2940
you.chen9ac66392022-08-06 17:01:16 +08002941 if (NULL != scan_list)
2942 {
2943 free(scan_list);
2944 }
2945 if (NULL != save_list)
2946 {
2947 free(save_list);
2948 }
2949
2950 return ret;
you.chen35020192022-05-06 11:30:57 +08002951}
2952
2953static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
2954{
qs.xiongc8d92a62023-03-29 17:36:14 +08002955 char lynq_auth_cmd[128]={0};
you.chen35020192022-05-06 11:30:57 +08002956 char lynq_ket_mgmt_cmd[64]={0};
2957 char lynq_pairwise_cmd[64]={0};
2958 char lynq_psk_cmd[64]={0};
2959
2960 CHECK_WPA_CTRL(CTRL_STA);
2961
qs.xiong9fbf74e2023-03-28 13:38:22 +08002962 switch(auth)
2963 {
2964 case LYNQ_WIFI_AUTH_OPEN:
you.chen35020192022-05-06 11:30:57 +08002965 {
2966 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002967
you.chen35020192022-05-06 11:30:57 +08002968 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002969// DO_OK_FAIL_REQUEST(cmd_save_config);
2970 break;
2971 }
2972 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08002973 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong9fbf74e2023-03-28 13:38:22 +08002974 {
2975 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2976 {
you.chen35020192022-05-06 11:30:57 +08002977 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
2978 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08002979 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2980 {
you.chena6cd55a2022-05-08 12:20:18 +08002981 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08002982 }
2983 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
2984 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04002985
you.chen35020192022-05-06 11:30:57 +08002986 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2987 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
2988 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04002989
qs.xiong9fbf74e2023-03-28 13:38:22 +08002990 if (password != NULL)
2991 {
you.chen35020192022-05-06 11:30:57 +08002992 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
2993 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002994 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen35020192022-05-06 11:30:57 +08002995 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002996
you.chen35020192022-05-06 11:30:57 +08002997// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong9fbf74e2023-03-28 13:38:22 +08002998 break;
2999 }
3000 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
3001 {
qs.xiong3e506812023-04-06 11:08:48 +08003002 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiongc8d92a62023-03-29 17:36:14 +08003003 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003004 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3005 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3006
qs.xiong3e506812023-04-06 11:08:48 +08003007 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003008 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3009 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3010 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3011
3012 break;
3013 }
3014 case LYNQ_WIFI_AUTH_WPA3_PSK:
3015 {
qs.xiong3e506812023-04-06 11:08:48 +08003016 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong03556d52023-04-17 09:45:19 +08003017 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003018 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
3019 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
3020
qs.xiong3e506812023-04-06 11:08:48 +08003021 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003022 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
3023 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
3024 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
3025
3026 break;
3027 }
3028 default:
3029 return -1;
you.chen35020192022-05-06 11:30:57 +08003030 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003031
qs.xiong9fbf74e2023-03-28 13:38:22 +08003032 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04003033}
qs.xiong7a105ce2022-03-02 09:43:11 -05003034
you.chen35020192022-05-06 11:30:57 +08003035static int inner_get_curr_net_no(int interface) {
3036 curr_status_info curr_state;
3037 curr_state.ap = NULL;
3038 curr_state.state = NULL;
3039
qs.xiong9fbf74e2023-03-28 13:38:22 +08003040 if (0 != inner_get_status_info(interface, &curr_state))
3041 {
you.chen35020192022-05-06 11:30:57 +08003042 return -1;
3043 }
3044
3045 return curr_state.net_no;
3046}
3047
3048int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05003049{
you.chen35020192022-05-06 11:30:57 +08003050 int net_no;
3051 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04003052
you.chen35020192022-05-06 11:30:57 +08003053 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05003054
qs.xiong9fbf74e2023-03-28 13:38:22 +08003055 if (net_no < 0)
3056 {
you.chen35020192022-05-06 11:30:57 +08003057 return -1;
3058 }
3059
3060 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05003061}
3062
you.chenb95401e2023-05-12 19:39:06 +08003063int 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 -05003064{
you.chen35020192022-05-06 11:30:57 +08003065 int count, net_no, index;
3066 int net_no_list[128];
qs.xiongf71b53b2023-05-03 13:12:07 +08003067 char rm_net_cmd[128];
you.chen35020192022-05-06 11:30:57 +08003068 lynq_wifi_auth_s net_auth;
you.chen70f377f2023-04-14 18:17:09 +08003069 curr_status_info curr_state;
3070 ap_info_s ap_info;
3071 char status[64];
qs.xiongf1b525b2022-03-31 00:58:23 -04003072
qs.xiong9fbf74e2023-03-28 13:38:22 +08003073 if (ssid == NULL || *ssid == '\0')
3074 {
3075 RLOGE("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003076 return -1;
3077 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003078
qs.xiong9fbf74e2023-03-28 13:38:22 +08003079 if (LYNQ_WIFI_AUTH_OPEN != auth)
3080 {
3081 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chen70f377f2023-04-14 18:17:09 +08003082 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003083 RLOGE("bad password\n");
you.chen35020192022-05-06 11:30:57 +08003084 return -1;
3085 }
3086 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003087
you.chen70f377f2023-04-14 18:17:09 +08003088
3089 pthread_mutex_lock(&s_global_check_mutex);
3090 if (s_sta_status != INNER_STA_STATUS_INIT)
3091 {
3092 s_sta_status = INNER_STA_STATUS_CANCEL;
3093 pthread_cond_signal(&s_global_check_cond);
3094 }
3095 pthread_mutex_unlock(&s_global_check_mutex);
3096
you.chen35020192022-05-06 11:30:57 +08003097 CHECK_IDX(idx, CTRL_STA);
you.chen70f377f2023-04-14 18:17:09 +08003098 CHECK_WPA_CTRL(CTRL_STA);
you.chen35020192022-05-06 11:30:57 +08003099
3100 net_no = -1;
you.chen70f377f2023-04-14 18:17:09 +08003101 memset(&ap_info, 0, sizeof (ap_info));
3102 memset(status, 0, sizeof (status));
you.chen35020192022-05-06 11:30:57 +08003103
you.chen70f377f2023-04-14 18:17:09 +08003104 curr_state.ap = &ap_info;
3105 curr_state.state = status;
3106
3107 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003108 {
you.chen70f377f2023-04-14 18:17:09 +08003109 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
3110 {
3111 net_no = curr_state.net_no;
3112 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
3113 && strcmp(ap_info.psw, psw) == 0)
3114 {
3115 RLOGD("already connected\n");
3116
3117 pthread_mutex_lock(&s_global_check_mutex);
3118 s_sta_status = INNER_STA_STATUS_CONNECTED;
3119 pthread_cond_signal(&s_global_check_cond);
3120 pthread_mutex_unlock(&s_global_check_mutex);
3121 return 0;
3122 }
you.chen35020192022-05-06 11:30:57 +08003123 }
3124 }
3125
you.chen70f377f2023-04-14 18:17:09 +08003126 if (net_no == -1)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003127 {
you.chen70f377f2023-04-14 18:17:09 +08003128 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3129
3130 for (index=0; index < count; index++)
3131 {
3132 net_auth = -1;
3133 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3134 {
3135 net_no = net_no_list[index];
3136 break;
3137 }
you.chen35020192022-05-06 11:30:57 +08003138 }
3139
you.chen70f377f2023-04-14 18:17:09 +08003140 if (net_no < 0)
3141 {
qs.xiongf71b53b2023-05-03 13:12:07 +08003142 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
3143 for ( index = 0; index < (count - STA_MAX_SAVED_AP_NUM + 1 ) ;index++) //+1 is for add new network
3144 {
3145 sprintf(rm_net_cmd,"REMOVE_NETWORK %d",net_no_list[index]);
3146 RLOGD("call cmd rm_net_cmd: %s;index is %d\n",rm_net_cmd,index);
3147 DO_OK_FAIL_REQUEST(rm_net_cmd);
3148 }
you.chen70f377f2023-04-14 18:17:09 +08003149 net_no = lynq_add_network(CTRL_STA);
3150 if (net_no == -1)
3151 {
3152 return -1;
3153 }
3154
3155 RLOGD("net no is %d\n", net_no);
3156 if (0 != inner_set_sta_ssid(net_no, ssid))
3157 {
3158 return -1;
3159 }
you.chen35020192022-05-06 11:30:57 +08003160 }
3161 }
3162
qs.xiong9fbf74e2023-03-28 13:38:22 +08003163 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
3164 {
you.chen35020192022-05-06 11:30:57 +08003165 return -1;
3166 }
3167
you.chen70f377f2023-04-14 18:17:09 +08003168
3169 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen186d3c32023-05-18 14:19:46 +08003170 system("echo \"\" > /tmp/wlan0_dhcpcd_router");
you.chen70f377f2023-04-14 18:17:09 +08003171 usleep(200*1000);
3172
3173 ret = inner_sta_start_stop(net_no, 1, 1);
3174
3175 pthread_mutex_lock(&s_global_check_mutex);
3176 s_sta_status = INNER_STA_STATUS_CONNECTING;
3177 strcpy(s_sta_current_connecting_ssid, ssid);
3178 struct timeval now;
3179 gettimeofday(&now,NULL);
you.chenb95401e2023-05-12 19:39:06 +08003180 s_sta_connect_timeout.tv_sec = now.tv_sec + timeout;
you.chen70f377f2023-04-14 18:17:09 +08003181 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
3182 pthread_cond_signal(&s_global_check_cond);
3183 pthread_mutex_unlock(&s_global_check_mutex);
3184 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05003185}
3186
you.chenb95401e2023-05-12 19:39:06 +08003187int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
3188{
3189 return lynq_wifi_sta_connect_timeout(idx, ssid, auth, psw, MAX_CONNNECT_TIME);
3190}
3191
you.chen35020192022-05-06 11:30:57 +08003192int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05003193{
you.chen35020192022-05-06 11:30:57 +08003194 ap_info_s ap;
3195 curr_status_info curr_state;
3196 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04003197
qs.xiong9fbf74e2023-03-28 13:38:22 +08003198 if (ssid == NULL || *ssid == '\0')
3199 {
3200 RLOGE("input ssid is NULL\n");
you.chen35020192022-05-06 11:30:57 +08003201 return -1;
3202 }
qs.xiong7a105ce2022-03-02 09:43:11 -05003203
you.chen35020192022-05-06 11:30:57 +08003204 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04003205
you.chen35020192022-05-06 11:30:57 +08003206 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08003207 curr_state.state = NULL;
3208
qs.xiong9fbf74e2023-03-28 13:38:22 +08003209 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
3210 {
you.chen35020192022-05-06 11:30:57 +08003211 return 0;
3212 }
qs.xiong1af5daf2022-03-14 09:12:12 -04003213
qs.xiong9fbf74e2023-03-28 13:38:22 +08003214 if (strcmp(ap.ap_ssid, ssid) != 0)
3215 {
you.chen35020192022-05-06 11:30:57 +08003216 return 0;
3217 }
3218
you.chen70f377f2023-04-14 18:17:09 +08003219 pthread_mutex_lock(&s_global_check_mutex);
3220 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
3221 pthread_mutex_unlock(&s_global_check_mutex);
you.chen35020192022-05-06 11:30:57 +08003222 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05003223}
qs.xiong97fa59b2022-04-07 05:41:29 -04003224
you.chena6cd55a2022-05-08 12:20:18 +08003225int lynq_wifi_sta_start(lynq_wifi_index_e idx)
3226{
qs.xiongad2f89d2023-01-18 13:17:41 +08003227// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
3228// const char *lynq_reconnect_cmd = "RECONNECT";
3229 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
3230 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
3231// 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 -05003232
you.chen35020192022-05-06 11:30:57 +08003233 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08003234 CHECK_WPA_CTRL(CTRL_STA);
3235
you.chenc9928582023-04-24 15:39:37 +08003236 ret = system_call_v("%s %s", start_stop_sta_script, "start");
3237 if (ret != 0)
qs.xiongad2f89d2023-01-18 13:17:41 +08003238 {
you.chenc9928582023-04-24 15:39:37 +08003239 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen35020192022-05-06 11:30:57 +08003240 return -1;
3241 }
qs.xiong9c99fa92022-03-15 08:03:26 -04003242
qs.xiongad2f89d2023-01-18 13:17:41 +08003243 system(lynq_enable_sta_cmd);
3244 system(lynq_reconnect_cmd);
3245// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08003246 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05003247}
3248
you.chen6d247052023-06-01 16:39:54 +08003249static int inner_get_status_info_state (int interface, char *state) {
3250 curr_status_info curr_state;
3251 curr_state.ap = NULL;
3252 curr_state.state = state;
3253 return inner_get_status_info(interface, &curr_state);
3254}
you.chen35020192022-05-06 11:30:57 +08003255int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04003256{
qs.xiongad2f89d2023-01-18 13:17:41 +08003257// char lynq_disable_network_cmd[MAX_CMD];
3258// curr_status_info curr_state;
3259// ap_info_s ap_info;
you.chen6d247052023-06-01 16:39:54 +08003260 int i=0;
3261 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08003262
you.chen6d247052023-06-01 16:39:54 +08003263// 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 +08003264 CHECK_IDX(idx, CTRL_STA);
3265 CHECK_WPA_CTRL(CTRL_STA);
3266
you.chen6d247052023-06-01 16:39:54 +08003267// system(lynq_disable_sta_cmd);
3268 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chena6cd55a2022-05-08 12:20:18 +08003269 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chenc9928582023-04-24 15:39:37 +08003270
3271 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
3272 if (ret != 0)
3273 {
3274 RLOGE("lynq_wifi_ap_start excute script fail");
3275 return -1;
3276 }
3277
you.chen6d247052023-06-01 16:39:54 +08003278 for (i=0; i < 30; i++) // to check if sta is realy stoped
3279 {
3280 if (inner_get_status_info_state(idx, state) != 0)
3281 {
3282 break;
3283 }
3284
3285 if (memcmp(state, STATE_DISCONNECTED, strlen (STATE_DISCONNECTED)) == 0)
3286 {
3287 break;
3288 }
3289 RLOGD("lynq_wifi_ap_start curr state %s", state);
3290 usleep(SLEEP_TIME_ON_IDLE);
3291 }
3292
you.chena6cd55a2022-05-08 12:20:18 +08003293 return 0;
3294// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04003295}
qs.xiongfcc914b2023-07-06 21:16:20 +08003296int lynq_wifi_sta_stop_net(lynq_wifi_index_e idx,int networkid)
3297{
3298 char LYNQ_DISABLE_CMD[128]={0};
3299 CHECK_IDX(idx, CTRL_STA);
3300 CHECK_WPA_CTRL(CTRL_STA);
3301 sprintf(LYNQ_DISABLE_CMD,"DISABLE_NETWORK %d",networkid);
3302 RLOGD("LYNQ_DISABLE_CMD is:%d\n",LYNQ_DISABLE_CMD);
3303 DO_OK_FAIL_REQUEST(LYNQ_DISABLE_CMD);
3304 return 0;
3305}
qs.xiong7a105ce2022-03-02 09:43:11 -05003306
you.chen35020192022-05-06 11:30:57 +08003307//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
3308// int i, count;
3309// char *p;
3310// const char * FLAG_SSID = "ssid=";
3311// const char * FLAG_SBSID = "bssid=";
3312// const char * FLAG_KEY_MGMT = "key_mgmt=";
3313// const char * FLAG_FREQ = "freq=";
3314// char lynq_sta_cmd[MAX_CMD];
3315// char *split_lines[128] = {0};
3316
3317// CHECK_WPA_CTRL(CTRL_AP);
3318
3319// sprintf(lynq_sta_cmd, "STA %s", bssid);
3320
3321// DO_REQUEST(lynq_sta_cmd);
3322
3323// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3324
3325// for(i=0; i < count; i++) {
3326// p = strstr(split_lines[i], FLAG_SSID);
3327// if (p != NULL) {
3328// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
3329// continue;
3330// }
3331// }
3332
3333// lynq_get_interface_ip(idx, ap->ap_ip);
3334// lynq_ap_password_set(idx, ap->psw);
3335
3336// return 0;
3337//}
3338
3339static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
3340 curr_status_info curr_state;
3341 curr_state.ap = ap;
3342 curr_state.state = NULL;
3343 return inner_get_status_info(interface, &curr_state);
3344}
3345
3346int 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 -04003347{
you.chend2fef3f2023-02-13 10:50:35 +08003348 int index, line_count;
3349 device_info_s *dev_info;
you.chen35020192022-05-06 11:30:57 +08003350 const char *lynq_first_sta_cmd = "STA-FIRST";
3351 char lynq_next_sta_cmd[MAX_CMD];
3352 char *bssid[1024] = {0};
you.chen35020192022-05-06 11:30:57 +08003353 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04003354
you.chen35020192022-05-06 11:30:57 +08003355 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003356
you.chen35020192022-05-06 11:30:57 +08003357 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04003358
you.chenb95401e2023-05-12 19:39:06 +08003359 // ap_info_s * tmp_ap;
3360 // device_info_s * tmp_list;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003361 if (ap == NULL || list == NULL || len == NULL)
3362 {
3363 RLOGE("bad input param");
you.chen35020192022-05-06 11:30:57 +08003364 return -1;
3365 }
3366
you.chenb95401e2023-05-12 19:39:06 +08003367 // ap = &tmp_ap;
3368 // list = &tmp_list;
you.chen35020192022-05-06 11:30:57 +08003369 *ap = malloc(sizeof (ap_info_s));
you.chenb95401e2023-05-12 19:39:06 +08003370 memset(*ap, 0, sizeof (ap_info_s));
you.chen35020192022-05-06 11:30:57 +08003371
you.chenb95401e2023-05-12 19:39:06 +08003372 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0 || (*ap)->ap_ssid[0] == '\0')
qs.xiong9fbf74e2023-03-28 13:38:22 +08003373 {
you.chenb95401e2023-05-12 19:39:06 +08003374 RLOGE("inner_get_status_info_ap !=0 or ap_ssid is empty\n");
you.chen35020192022-05-06 11:30:57 +08003375 return -1;
3376 }
3377
3378 lynq_get_interface_ip(idx, (*ap)->ap_ip);
3379 lynq_ap_password_get(idx, (*ap)->psw);
3380
you.chen35020192022-05-06 11:30:57 +08003381 DO_REQUEST(lynq_first_sta_cmd);
3382
3383 index = 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003384 while (reply_len > 0)
3385 {
3386 if (memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003387 {
you.chen35020192022-05-06 11:30:57 +08003388 break;
3389 }
3390 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3391 bssid[index] = malloc(strlen(split_lines[0]) + 1);
3392 strcpy(bssid[index], split_lines[0]);
3393 index++;
3394 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
3395 reply_len = MAX_RET;
3396 cmd_reply[0] = '\0';
you.chend2fef3f2023-02-13 10:50:35 +08003397 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 +08003398 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
you.chenb95401e2023-05-12 19:39:06 +08003399 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003400 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08003401 break;
3402 }
3403 }
3404
3405 *len = index;
3406
3407 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiong9fbf74e2023-03-28 13:38:22 +08003408 for (index=0; index < *len; index++)
3409 {
you.chend2fef3f2023-02-13 10:50:35 +08003410 dev_info = &(*list)[index];
3411 memset(dev_info, 0, sizeof(device_info_s));
3412 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
3413 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
3414 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
3415 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen35020192022-05-06 11:30:57 +08003416 free(bssid[index]);
3417 }
3418
3419 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003420}
3421
you.chen35020192022-05-06 11:30:57 +08003422int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04003423{
you.chen35020192022-05-06 11:30:57 +08003424 int i, count, index, count_words;
3425 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
3426 char *split_lines[128] = {0};
3427 char *split_words[128] = {0};
3428 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04003429
qs.xiong9fbf74e2023-03-28 13:38:22 +08003430 if (list == NULL || len == NULL)
3431 {
you.chen35020192022-05-06 11:30:57 +08003432 return -1;
3433 }
qs.xiong97fa59b2022-04-07 05:41:29 -04003434
you.chen9ac66392022-08-06 17:01:16 +08003435 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
3436 {
3437 usleep(100 * 1000);
3438 }
3439
you.chen35020192022-05-06 11:30:57 +08003440 CHECK_IDX(idx, CTRL_STA);
3441
3442 CHECK_WPA_CTRL(CTRL_STA);
3443
3444 DO_REQUEST(lynq_scan_result_cmd);
3445
3446 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3447 *len = count - 1;
3448 *list = malloc(sizeof (scan_info_s) * *len);
3449
3450 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiong9fbf74e2023-03-28 13:38:22 +08003451 for (index=0; index <count_words; index++)
3452 {
3453 RLOGD("----header: %s\n", split_words[index]);
you.chen35020192022-05-06 11:30:57 +08003454 }
3455
qs.xiong9fbf74e2023-03-28 13:38:22 +08003456 for(index = 1;index < count; index++)
3457 {
3458 RLOGD("---- %s\n",split_lines[index]);
you.chen6ed36a62023-04-27 17:51:56 +08003459 memset(split_words, 0 , sizeof (split_words));
you.chen35020192022-05-06 11:30:57 +08003460 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3461 if (count_words < 4)
3462 continue;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003463 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen35020192022-05-06 11:30:57 +08003464 //bssid / frequency / signal level / flags / ssid
3465 p = (*list) + index - 1;
3466 strcpy(p->mac, split_words[0]);
3467 p->band = convert_band_from_freq(atoi(split_words[1]));
3468 p->rssi = -1 * atoi( split_words[2]);
3469 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen6ed36a62023-04-27 17:51:56 +08003470 if (count_words == 4) // ssid hided
3471 {
3472 p->ssid[0] = '\0';
3473 }
3474 else
3475 {
3476 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3477 }
you.chen35020192022-05-06 11:30:57 +08003478 }
3479
3480 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04003481}
qs.xiong97fa59b2022-04-07 05:41:29 -04003482
you.chen35020192022-05-06 11:30:57 +08003483int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3484{
3485 int count, net_no, index;
3486 int net_no_list[128];
3487 lynq_wifi_auth_s net_auth;
3488 char lynq_remove_cmd[MAX_CMD];
3489
qs.xiong9fbf74e2023-03-28 13:38:22 +08003490 if (ssid == NULL || *ssid == '\0')
3491 {
3492 RLOGD("bad ssid\n");
you.chen35020192022-05-06 11:30:57 +08003493 return -1;
3494 }
3495
3496 CHECK_IDX(idx, CTRL_STA);
3497
3498 CHECK_WPA_CTRL(CTRL_STA);
3499
3500 net_no = -1;
3501 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3502
qs.xiong9fbf74e2023-03-28 13:38:22 +08003503 for (index=0; index < count; index++)
3504 {
you.chen35020192022-05-06 11:30:57 +08003505 net_auth = -1;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003506 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3507 {
you.chen35020192022-05-06 11:30:57 +08003508 net_no = net_no_list[index];
3509 break;
3510 }
3511 }
3512
qs.xiong9fbf74e2023-03-28 13:38:22 +08003513 if (net_no < 0)
3514 {
you.chen35020192022-05-06 11:30:57 +08003515 return 0;
3516 }
3517
3518 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3519
3520 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3521 DO_OK_FAIL_REQUEST(cmd_save_config);
3522
3523 return 0;
3524}
3525
3526int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003527{
you.chend2fef3f2023-02-13 10:50:35 +08003528 int count, index;
you.chen35020192022-05-06 11:30:57 +08003529 int net_no_list[128];
3530 char freq[16];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003531 RLOGD("enter lynq_get_sta_saved_ap api\n");
3532 if (list == NULL || len == NULL)
3533 {
3534 RLOGE("bad param,please check!");
you.chen35020192022-05-06 11:30:57 +08003535 return -1;
3536 }
3537
3538 CHECK_IDX(idx, CTRL_STA);
3539
3540// CHECK_WPA_CTRL(CTRL_STA);
3541
3542 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003543 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen35020192022-05-06 11:30:57 +08003544
you.chen057aac42023-04-13 14:06:58 +08003545 if (count < 0)
3546 {
3547 RLOGE("list network fail");
3548 return count;
3549 }
3550 else if (count == 0)
3551 {
3552 *list = NULL;
3553 *len = 0;
3554 return 0;
3555 }
3556
you.chen35020192022-05-06 11:30:57 +08003557 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08003558 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08003559 *len = count;
3560
qs.xiong9fbf74e2023-03-28 13:38:22 +08003561 for (index=0; index < count; index++)
3562 {
you.chen35020192022-05-06 11:30:57 +08003563 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen35020192022-05-06 11:30:57 +08003564 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08003565 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003566 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen057aac42023-04-13 14:06:58 +08003567 {
you.chen35020192022-05-06 11:30:57 +08003568 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3569 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003570 else
you.chen057aac42023-04-13 14:06:58 +08003571 {
you.chen35020192022-05-06 11:30:57 +08003572 (*list)[index].base_info.band = -1;
3573 }
you.chen057aac42023-04-13 14:06:58 +08003574 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen755332b2022-08-06 16:59:10 +08003575 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08003576 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003577 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen35020192022-05-06 11:30:57 +08003578 return 0;
3579}
3580
3581int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3582{
qs.xiongc8d92a62023-03-29 17:36:14 +08003583 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen35020192022-05-06 11:30:57 +08003584 const char *lynq_scan_cmd = "SCAN";
3585
3586 CHECK_IDX(idx, CTRL_STA);
3587
3588 CHECK_WPA_CTRL(CTRL_STA);
3589
you.chen0df3e7e2023-05-10 15:56:26 +08003590 if (g_sta_scan_finish_flag == 1 && s_sta_status == INNER_STA_STATUS_INIT) // temp add
3591 {
3592 RLOGD("tmp clear scanlist");
3593 system(clean_last_re);
3594 }
you.chen9ac66392022-08-06 17:01:16 +08003595 g_sta_scan_finish_flag = 0;
qs.xiongb3f26af2023-02-17 18:41:07 +08003596 DO_REQUEST(lynq_scan_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003597 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3598 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003599 return 0;
qs.xiong9fbf74e2023-03-28 13:38:22 +08003600 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3601 {
qs.xiongb3f26af2023-02-17 18:41:07 +08003602 g_sta_scan_finish_flag = 1;
3603 return -1;
3604 }
you.chen35020192022-05-06 11:30:57 +08003605
3606 return 0;
3607}
3608
3609int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003610 if (cb == NULL)
3611 {
3612 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003613 return -1;
3614 }
3615
you.chen6d247052023-06-01 16:39:54 +08003616 pthread_mutex_lock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003617 g_ap_callback_priv = priv;
3618 g_ap_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003619 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003620
you.chen6d247052023-06-01 16:39:54 +08003621 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003622 if (g_ap_watcher_pid == 0 )
3623 {
3624 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3625 {
3626 g_ap_watcher_pid = 0;
3627 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3628 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3629 return -1;
3630 }
3631 }
3632
3633 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3634 RLOGD("creat APWatcherTheradProc susccs");
3635
you.chen35020192022-05-06 11:30:57 +08003636 return 0;
3637}
3638
3639int lynq_unreg_ap_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003640 pthread_mutex_lock(&s_ap_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003641 if (g_ap_callback_priv == priv)
3642 {
you.chen35020192022-05-06 11:30:57 +08003643 g_ap_callback_func = NULL;
3644 g_ap_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003645 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003646 return 0;
3647 }
you.chen6d247052023-06-01 16:39:54 +08003648 pthread_mutex_unlock(&s_ap_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003649 return -1;
3650}
3651
3652int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiong9fbf74e2023-03-28 13:38:22 +08003653 if (cb == NULL)
3654 {
3655 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen35020192022-05-06 11:30:57 +08003656 return -1;
3657 }
3658
you.chen6d247052023-06-01 16:39:54 +08003659 pthread_mutex_lock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003660 g_sta_callback_priv = priv;
3661 g_sta_callback_func = cb;
you.chen6d247052023-06-01 16:39:54 +08003662 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003663
you.chen6d247052023-06-01 16:39:54 +08003664 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chene9d00032023-04-24 13:55:29 +08003665 if (g_sta_watcher_pid == 0 ) {
3666 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3667 {
3668 g_sta_watcher_pid = 0;
3669 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3670 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3671 return -1;
3672 }
3673 }
3674
3675 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3676 RLOGD("creat STAWatcherTheradProc susccs");
you.chen35020192022-05-06 11:30:57 +08003677 return 0;
3678}
3679
3680int lynq_unreg_sta_event_callback(void * priv) {
you.chen6d247052023-06-01 16:39:54 +08003681 pthread_mutex_lock(&s_sta_callback_mutex);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003682 if (g_sta_callback_priv == priv)
3683 {
you.chen35020192022-05-06 11:30:57 +08003684 g_sta_callback_func = NULL;
3685 g_sta_callback_priv = NULL;
you.chen6d247052023-06-01 16:39:54 +08003686 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003687 return 0;
3688 }
you.chen6d247052023-06-01 16:39:54 +08003689 pthread_mutex_unlock(&s_sta_callback_mutex);
you.chen35020192022-05-06 11:30:57 +08003690 return -1;
3691}
3692
qs.xiongfcc914b2023-07-06 21:16:20 +08003693int lynq_reg_sta_auto_event_callback(void * priv, STA_AUTO_CALLBACK_FUNC_PTR cb){
3694 if (cb == NULL)
3695 {
3696 RLOGE("lynq_reg_sta_auto_event_callback ptr is NULL,plese check!\n");
3697 return -1;
3698 }
3699 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3700 g_sta_auto_callback_priv = priv;
3701 g_sta_auto_callback_func = cb;
3702 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3703 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
3704 if (g_sta_auto_watcher_pid == 0 ) {
3705 if(pthread_create(&g_sta_auto_watcher_pid,NULL,STAAutoWatcherThreadProc,NULL) < 0) //create STAAutoWatcherThreadProc
3706 {
3707 g_sta_auto_watcher_pid = 0;
3708 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3709 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3710 return -1;
3711 }
3712 }
3713 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3714 RLOGD("creat STAWatcherTheradProc susccs");
3715 return 0;
3716}
3717int lynq_unreg_sta_auto_event_callback(void * priv) {
3718 pthread_mutex_lock(&s_sta_auto_callback_mutex);
3719 if (g_sta_auto_callback_priv == priv)
3720 {
3721 g_sta_auto_watcher_stop_flag = 1;
3722 if (g_sta_auto_watcher_pid != 0)
3723 {
3724 pthread_join(g_sta_auto_watcher_pid, NULL);
3725 }
3726 g_sta_auto_watcher_pid = 0;
3727 g_sta_auto_callback_func = NULL;
3728 g_sta_auto_callback_priv = NULL;
3729 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3730 return 0;
3731 }
3732 pthread_mutex_unlock(&s_sta_auto_callback_mutex);
3733 return -1;
3734}
you.chen35020192022-05-06 11:30:57 +08003735int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
3736{
3737 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003738 RLOGD("enter lynq_get_ap_status\n");
you.chen35020192022-05-06 11:30:57 +08003739 CHECK_IDX(idx, CTRL_AP);
3740
qs.xiong9fbf74e2023-03-28 13:38:22 +08003741 if (inner_get_status_info_state(CTRL_AP, state) != 0)
3742 {
you.chen35020192022-05-06 11:30:57 +08003743 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3744 return 0;
3745 }
3746
qs.xiong9fbf74e2023-03-28 13:38:22 +08003747 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3748 {
you.chen35020192022-05-06 11:30:57 +08003749 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
3750 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003751 else
3752 {
you.chen35020192022-05-06 11:30:57 +08003753 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3754 }
3755
3756 return 0;
3757}
3758
3759int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
3760 char state[MAX_CMD];
qs.xiong9fbf74e2023-03-28 13:38:22 +08003761 RLOGD("enter lynq_get_sta_status\n");
you.chen35020192022-05-06 11:30:57 +08003762 CHECK_IDX(idx, CTRL_STA);
3763
qs.xiong9fbf74e2023-03-28 13:38:22 +08003764 if (inner_get_status_info_state(CTRL_STA, state) != 0)
3765 {
you.chen35020192022-05-06 11:30:57 +08003766 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3767 return 0;
3768 }
3769
qs.xiong9fbf74e2023-03-28 13:38:22 +08003770 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3771 {
you.chen35020192022-05-06 11:30:57 +08003772 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
3773 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08003774 else
3775 {
you.chen35020192022-05-06 11:30:57 +08003776 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3777 }
3778
3779 return 0;
3780}
3781
3782int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
3783// CHECK_IDX(idx, CTRL_AP);
3784// int ret = 0;
3785// size_t reply_len = MAX_RET;
3786// char cmd_reply[MAX_RET]={0};
3787// const char * cmd_str = "GET country";
3788// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
3789// do{
3790// if (NULL == s_lynq_wpa_ctrl) {
3791// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
3792// if (NULL == s_lynq_wpa_ctrl ) {
3793// printf("wpa_ctrl_open fail\n");
3794// return -1;
3795// }
3796// }
3797// }while(0);
3798
3799// do {
3800// reply_len = MAX_RET;
3801// cmd_reply[0] = '\0';
3802// printf("to call [%s]\n", cmd_str);
you.chend2fef3f2023-02-13 10:50:35 +08003803// 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 +08003804// if (ret != 0) {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003805// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen35020192022-05-06 11:30:57 +08003806// return ret;
3807// }
3808// cmd_reply[reply_len+1] = '\0';
qs.xiong9fbf74e2023-03-28 13:38:22 +08003809// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen35020192022-05-06 11:30:57 +08003810// }while(0);
3811
3812 FILE *fp;
3813 size_t i = 0;
3814 char lynq_cmd_ret[MAX_RET]={0};
3815
3816// CHECK_IDX(idx, CTRL_AP);
3817
3818 if((fp=popen("wl country","r"))==NULL)
qs.xiong9fbf74e2023-03-28 13:38:22 +08003819 {
3820 perror("popen error!");
3821 return -1;
3822 }
you.chen35020192022-05-06 11:30:57 +08003823 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
3824 {
3825 perror("fread fail!");
3826 return -1;
3827 }
3828
qs.xiong9fbf74e2023-03-28 13:38:22 +08003829 for(i=0; i < strlen(lynq_cmd_ret); i++)
3830 {
3831 if (lynq_cmd_ret[i] == ' ')
3832 {
you.chen35020192022-05-06 11:30:57 +08003833 lynq_cmd_ret[i] = '\0';
3834 break;
3835 }
3836 }
3837
3838 strcpy(country_code,lynq_cmd_ret);
qs.xiong9fbf74e2023-03-28 13:38:22 +08003839 RLOGD("---country code %s\n", country_code);
you.chen35020192022-05-06 11:30:57 +08003840
3841 int ret=pclose(fp);
3842 if(ret==-1)
3843 {
3844 perror("close file faild");
3845 }
3846
3847 return 0;
3848}
3849
you.chen705a7ef2023-06-01 22:06:45 +08003850static int check_and_init_uci_config(char * country_code)
3851{
3852 FILE * fp;
3853 int is_different = 0;
3854 const char * check_uci_cmd ="uci show | grep lynq_wifi_country_code";
3855 const char * create_uci_cmd ="uci set lynq_uci.lynq_wifi_country_code='lynq_wifi_country_code'";
3856 const char * commit_uci_cmd ="uci commit";
3857 char set_country_cmd[MAX_CMD];
3858 char lynq_cmd_ret[MAX_CMD]={0};
xj3df9fd82023-06-01 20:50:02 +08003859
you.chen705a7ef2023-06-01 22:06:45 +08003860 sprintf(set_country_cmd, "uci set lynq_uci.lynq_wifi_country_code.code='%s'",country_code);
qs.xiong62034bf2023-06-01 19:23:13 +08003861
you.chen705a7ef2023-06-01 22:06:45 +08003862 if (0 != system(check_uci_cmd))
qs.xiong62034bf2023-06-01 19:23:13 +08003863 {
you.chen705a7ef2023-06-01 22:06:45 +08003864 if (0 != system(create_uci_cmd))
3865 {
3866 RLOGE("creat_uci_cmd fail");
3867 return -1;
3868 }
3869 is_different = 1;
3870 }
3871
3872 if((fp=popen("uci get lynq_uci.lynq_wifi_country_code.code","r"))==NULL)
3873 {
3874 RLOGE("popen error!");
qs.xiong62034bf2023-06-01 19:23:13 +08003875 return -1;
3876 }
3877
you.chen705a7ef2023-06-01 22:06:45 +08003878 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0 )
qs.xiong62034bf2023-06-01 19:23:13 +08003879 {
you.chen705a7ef2023-06-01 22:06:45 +08003880 RLOGE("fread fail!");
3881 fclose(fp);
3882 return -1;
qs.xiong62034bf2023-06-01 19:23:13 +08003883 }
3884
you.chen705a7ef2023-06-01 22:06:45 +08003885 if ( strncmp(lynq_cmd_ret,country_code,2) != 0 )
3886 {
3887 RLOGE("get country code for uci %s,input cpuntry code is:%s\n",lynq_cmd_ret,country_code);
3888 is_different = 1;
3889 }
3890
3891 fclose(fp);
3892
3893 if (is_different)
3894 {
3895 if ( 0 != system(set_country_cmd))
3896 {
3897 RLOGE("set_country_cmd fail");
3898 return -1;
3899 }
3900 if (0 != system(commit_uci_cmd))
3901 {
3902 RLOGE("commmit fail");
3903 }
3904 }
3905
3906 return is_different;
3907}
3908
3909int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
3910 char check_current_code[10];
3911 const char * support_country[] = {"CN", "EU"};
3912
3913 int ret,is_different, i, cc_count;
3914
3915 if (country_code == NULL || country_code[0] == '\0')
3916 {
3917 RLOGE("bad country code\n");
3918 return -1;
3919 }
3920
3921 cc_count = sizeof (support_country) / sizeof (char*);
3922 for(i=0; i < cc_count; i++)
3923 {
3924 if (strcmp(support_country[i], country_code) == 0)
3925 {
3926 break;
3927 }
3928 }
3929
3930 if (i >= cc_count)
3931 {
3932 RLOGE("unspported country code %s\n", country_code);
3933 return -1;
3934 }
3935
3936 is_different = check_and_init_uci_config(country_code);
3937 if( is_different < 0 )
3938 {
3939 RLOGE("init set uci fail\n");
3940 return -1;
3941 }
3942
3943 ret = lynq_get_country_code(idx,check_current_code);
3944 if( ret == 0 && (is_different == 1 || strcmp(check_current_code, country_code) != 0))
3945 {
3946 ret = lynq_wifi_disable();
3947 if(ret != 0 )
3948 {
3949 RLOGE("berfore set country,find bcmdhd insmod,remod fail\n");
3950 return -1;
3951 }
3952 }
3953
3954 return 0;
you.chen35020192022-05-06 11:30:57 +08003955}
3956
3957int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
3958{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003959 RLOGD("enter lynq_get_connect_ap_mac\n");
3960 if (mac == NULL)
3961 {
3962 RLOGE("input ptr is NULL,please check\n");
you.chen35020192022-05-06 11:30:57 +08003963 return -1;
3964 }
3965
3966 CHECK_IDX(idx, CTRL_STA);
3967 ap_info_s ap;
3968 ap.ap_mac[0] = '\0';
3969
qs.xiong9fbf74e2023-03-28 13:38:22 +08003970 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
3971 {
you.chen35020192022-05-06 11:30:57 +08003972 return -1;
3973 }
3974 strcpy(mac, ap.ap_mac);
3975
3976 return 0;
3977}
3978
3979int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
3980{
qs.xiong9fbf74e2023-03-28 13:38:22 +08003981 RLOGD("enter lynq_get_interface_ip\n");
you.chen9ac66392022-08-06 17:01:16 +08003982 struct ifaddrs *ifaddr_header, *ifaddr;
3983 struct in_addr * ifa;
3984 const char * ifaName = "wlan0";
3985 if (ip == NULL)
3986 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08003987 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chenf58b3c92022-06-21 16:53:48 +08003988 return -1;
you.chen9ac66392022-08-06 17:01:16 +08003989 }
you.chenf58b3c92022-06-21 16:53:48 +08003990
qs.xiong9fbf74e2023-03-28 13:38:22 +08003991 if (idx == 1)
3992 {
you.chen0df3e7e2023-05-10 15:56:26 +08003993 ifaName = inner_get_ap_interface_name();
3994 if (ifaName == NULL)
3995 {
3996 RLOGE("[lynq_get_interface_ip] ap name get fail");
3997 return -1;
3998 }
you.chen9ac66392022-08-06 17:01:16 +08003999 }
qs.xiong9fbf74e2023-03-28 13:38:22 +08004000 else if (idx != 0)
4001 {
you.chen35020192022-05-06 11:30:57 +08004002 return -1;
you.chen9ac66392022-08-06 17:01:16 +08004003 }
you.chen35020192022-05-06 11:30:57 +08004004
you.chen9ac66392022-08-06 17:01:16 +08004005 if (getifaddrs(&ifaddr_header) == -1)
4006 {
you.chen35020192022-05-06 11:30:57 +08004007 perror("getifaddrs");
4008 return -1;
4009 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08004010 }
you.chen35020192022-05-06 11:30:57 +08004011
4012
you.chen9ac66392022-08-06 17:01:16 +08004013 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
4014 {
4015 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08004016 continue;
you.chen9ac66392022-08-06 17:01:16 +08004017 if((strcmp(ifaddr->ifa_name,ifaName)==0))
4018 {
4019 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
4020 {
4021 // is a valid IP4 Address
4022 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
4023 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004024 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen9ac66392022-08-06 17:01:16 +08004025 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004026 RLOGD("ip %s\n", ip);
you.chen9ac66392022-08-06 17:01:16 +08004027 return 0;
4028 }
4029 }
4030 }
qs.xiongc4f007c2023-02-08 18:16:58 +08004031 freeifaddrs(ifaddr_header);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004032 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen9ac66392022-08-06 17:01:16 +08004033 return -1;
you.chen35020192022-05-06 11:30:57 +08004034}
4035
4036int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
4037{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004038 RLOGD("enter lynq_get_interface_mac\n");
you.chen35020192022-05-06 11:30:57 +08004039 int count;
4040 size_t i;
4041 char *split_words[128] = {0};
4042 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
4043
4044 CHECK_WPA_CTRL(idx);
4045
4046 DO_REQUEST(lynq_get_mac_cmd);
4047
qs.xiong9fbf74e2023-03-28 13:38:22 +08004048 if (memcmp(cmd_reply, "FAIL", 4) == 0)
4049 {
4050 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen35020192022-05-06 11:30:57 +08004051 return -1;
4052 }
4053
4054 count = lynq_split(cmd_reply, reply_len, '=', split_words);
4055
qs.xiong9fbf74e2023-03-28 13:38:22 +08004056 if (count < 2)
4057 {
you.chen35020192022-05-06 11:30:57 +08004058 return -1;
4059 }
4060
qs.xiong9fbf74e2023-03-28 13:38:22 +08004061 for (i=0; i < strlen(split_words[1]); i++ )
4062 {
4063 if (split_words[1][i] != ' ')
4064 {
you.chen35020192022-05-06 11:30:57 +08004065 break;
4066 }
4067 }
4068
4069 strcpy(mac, split_words[1] + i);
4070
4071 return 0;
4072}
4073
4074int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
4075{
4076// int count;
4077// char *split_words[128] = {0};
4078// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
4079
4080// if (rssi == NULL) {
4081// return -1;
4082// }
4083
4084// CHECK_IDX(idx, CTRL_STA);
4085
4086// CHECK_WPA_CTRL(CTRL_STA);
4087
4088// DO_REQUEST(lynq_get_rssi_cmd);
4089
4090// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
4091// return -1;
4092// }
4093
4094// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
4095
4096// if (count < 2) {
4097// return -1;
4098// }
4099
4100// *rssi = atoi(split_words[1]) * -1;
4101
you.chen35020192022-05-06 11:30:57 +08004102 char lynq_cmd_ret[MAX_RET]={0};
4103
qs.xiongff0ae0f2022-10-11 15:47:14 +08004104/*******change other cmd to get rssi*******
4105 *
4106 *wl rssi ---> wl -i wlan0 rssi
4107 *
4108 ***** change by qs.xiong 20221011*******/
you.chen23c4a5f2023-04-12 16:46:00 +08004109 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiong9fbf74e2023-03-28 13:38:22 +08004110 {
you.chen23c4a5f2023-04-12 16:46:00 +08004111 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen35020192022-05-06 11:30:57 +08004112 return -1;
4113 }
you.chen9f17e4d2022-06-06 17:18:18 +08004114 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004115/****** if got rssi is 0,means sta didn't connected any device****/
4116 if(*rssi == 0)
4117 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004118 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chen23c4a5f2023-04-12 16:46:00 +08004119 return -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08004120 }
you.chen35020192022-05-06 11:30:57 +08004121
4122 return 0;
4123}
4124
4125int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
4126{
qs.xiong9fbf74e2023-03-28 13:38:22 +08004127 RLOGD("enter lynq_get_connect_ap_band\n");
4128 if (band == NULL)
4129 {
you.chen35020192022-05-06 11:30:57 +08004130 return -1;
4131 }
4132
4133 CHECK_IDX(idx, CTRL_STA);
4134 ap_info_s ap;
4135 ap.band = -1;
4136
qs.xiong9fbf74e2023-03-28 13:38:22 +08004137 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
4138 {
you.chen35020192022-05-06 11:30:57 +08004139 return -1;
4140 }
4141 *band = ap.band;
4142
4143 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04004144}
you.chenf58b3c92022-06-21 16:53:48 +08004145
4146int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
4147{
you.chenb95401e2023-05-12 19:39:06 +08004148 int ret;
4149 char *p;
qs.xionge4cbf1c2023-02-28 18:22:49 +08004150 char bssid[1024] = {0};
you.chenf58b3c92022-06-21 16:53:48 +08004151
4152 if (ip == NULL)
4153 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004154 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chenf58b3c92022-06-21 16:53:48 +08004155 return -1;
4156 }
4157
4158 CHECK_IDX(idx, CTRL_STA);
4159
qs.xionge4cbf1c2023-02-28 18:22:49 +08004160 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chenf58b3c92022-06-21 16:53:48 +08004161 {
4162 return -1;
4163 }
qs.xionge4cbf1c2023-02-28 18:22:49 +08004164
you.chenb95401e2023-05-12 19:39:06 +08004165 ip[0] = '\0';
4166 ret = inner_get_ip_by_mac(bssid, ip, 32); //better input by user
4167 if (ret != 0)
4168 {
4169 RLOGE("[lynq_get_connect_ap_ip] inner_get_ip_by_mac return fail");
4170 return -1;
4171 }
4172
4173 if (ip[0] == '\0' || strchr(ip, ':') != NULL) //temp change, not ok
4174 {
4175 ip[0] = '\0';
you.chen186d3c32023-05-18 14:19:46 +08004176 ret = exec_cmd("grep \"new_router\" /tmp/wlan0_dhcpcd_router | awk '{print $2}'| tail -1", ip, 32);
you.chenb95401e2023-05-12 19:39:06 +08004177 if (ret != 0)
4178 {
4179 ip[0] = '\0';
4180 return 0;
4181 }
4182 else
4183 {
4184 p = strchr(ip, '\n');
4185 if (p != NULL)
4186 {
4187 *p = '\0';
4188 }
4189 }
4190 }
4191 return 0;
you.chenf58b3c92022-06-21 16:53:48 +08004192}
4193
qs.xiong026c5c72022-10-17 11:15:45 +08004194int lynq_ap_connect_num(int sta_number)
4195{
4196 char lynq_limit_cmd[32]={0};
4197 int ret;
qs.xiong9fbf74e2023-03-28 13:38:22 +08004198 if((sta_number < 1 ) && (sta_number > 15))
4199 {
4200 RLOGE("sta_number: not in range\n",sta_number);
qs.xiong026c5c72022-10-17 11:15:45 +08004201 return -1;
4202 }
4203 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
4204 ret = system(lynq_limit_cmd);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004205 if(ret != 0)
4206 {
4207 RLOGE("cmd of limit ap devices number error\n");
qs.xiong026c5c72022-10-17 11:15:45 +08004208 }
4209 return 0;
4210}
you.chenf58b3c92022-06-21 16:53:48 +08004211
qs.xiong77905552022-10-17 11:19:57 +08004212int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
4213{
4214
4215 char lynq_wifi_acs_cmd[128]={0};
4216 char lynq_cmd_mode[128]={0};
4217 char lynq_cmd_slect[128]={0};
4218
qs.xiong9fbf74e2023-03-28 13:38:22 +08004219 if((acs_mode != 2) && (acs_mode != 5))
4220 {
qs.xiong77905552022-10-17 11:19:57 +08004221 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
4222 }
4223
qs.xiong9fbf74e2023-03-28 13:38:22 +08004224 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
4225 {
qs.xiong77905552022-10-17 11:19:57 +08004226 return -1;
4227 }
4228
4229 CHECK_IDX(idx, CTRL_AP);
4230
4231 CHECK_WPA_CTRL(CTRL_AP);
4232
4233 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
4234 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
4235 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
4236
4237 DO_OK_FAIL_REQUEST(cmd_disconnect);
4238 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
4239 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
4240 DO_OK_FAIL_REQUEST(cmd_save_config);
4241 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
4242
4243 return 0;
4244}
you.chen0f5c6432022-11-07 18:31:14 +08004245//you.chen add for tv-box start
4246static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
4247 FILE *fp;
4248 //printf("to exec cmd:%s\n", str_cmd);
4249 if((fp=popen(str_cmd,"r"))==NULL)
4250 {
4251 perror("popen error!");
4252 return -1;
4253 }
4254 if((fread(str_cmd_ret,max_len,1,fp))<0)
4255 {
4256 perror("fread fail!");
4257 fclose(fp);
4258 return -1;
4259 }
4260 fclose(fp);
4261 return 0;
4262}
4263
4264static int get_netmask_length(const char* mask)
4265{
4266 int masklen=0, i=0;
4267 int netmask=0;
4268
4269 if(mask == NULL)
4270 {
4271 return 0;
4272 }
4273
4274 struct in_addr ip_addr;
4275 if( inet_aton(mask, &ip_addr) )
4276 {
4277 netmask = ntohl(ip_addr.s_addr);
qs.xiong9fbf74e2023-03-28 13:38:22 +08004278 }else
4279 {
you.chen0f5c6432022-11-07 18:31:14 +08004280 netmask = 0;
4281 return 0;
4282 }
4283
4284 while(0 == (netmask & 0x01) && i<32)
4285 {
4286 i++;
4287 netmask = netmask>>1;
4288 }
4289 masklen = 32-i;
4290 return masklen;
4291}
4292
4293static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
4294 int mask_len;
4295 char *p;
4296 char tmp[64] = {0};
you.chenc9928582023-04-24 15:39:37 +08004297 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
4298 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen0f5c6432022-11-07 18:31:14 +08004299 return -1;
4300 p = strstr(str_cmd_ret, "Mask:");
4301 if (p == NULL)
4302 return -1;
4303 mask_len = get_netmask_length(p + 5);
4304 if (mask_len == 0)
4305 return -1;
4306 p = strstr(str_cmd_ret, "inet addr:");
4307 if (p == NULL)
4308 return -1;
4309 strcpy(tmp, p + 10);
4310 p = strstr(tmp, " ");
4311 if (p != NULL)
4312 *p = '\0';
4313 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
4314 return 0;
4315}
4316
4317static void GBWWatchThreadProc() {
4318 int i,n, nloop, nmax, ncheckcount, nidlecount;
4319 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
4320 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
4321 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
4322 char *results[16] = {0};
4323 char str_cmd[256] = {0};
4324 char str_cmd_ret[128] = {0};
4325 char dest_ip[32] = {0};
4326 lastAP1Bytes = lastAP2Bytes = 0;
4327 lastAP1Drop = lastAP2Drop = 0;
4328 lastAP1Speed = lastAP2Speed = 0;
4329 setAP1Speed = 50;
4330 setAP2Speed = 80;
4331 nloop = 0;
4332 nmax = 6;
4333 ncheckcount = nidlecount = 0;
4334
you.chen0df3e7e2023-05-10 15:56:26 +08004335 if (inner_get_ap_interface_name() == NULL)
you.chenc9928582023-04-24 15:39:37 +08004336 {
4337 RLOGE("------gbw thread run\n");
4338 return;
4339 }
4340
qs.xiong9fbf74e2023-03-28 13:38:22 +08004341 RLOGD("------gbw thread run\n");
you.chen0f5c6432022-11-07 18:31:14 +08004342 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
4343 while (dest_ip[0] == '\0') {
4344 sleep(1);
4345 str_cmd_ret[0] = '\0';
4346 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
4347 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
4348 if (str_cmd_ret[n] == '\n'){
4349 str_cmd_ret[n] = '\0';
4350 break;
4351 }
4352 }
4353 if (str_cmd_ret[0] != '\0')
4354 {
4355 strcpy(dest_ip, str_cmd_ret);
4356 }
4357 }
4358
you.chenc9928582023-04-24 15:39:37 +08004359 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
4360 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
4361 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 +08004362 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
4363 {
qs.xiong9fbf74e2023-03-28 13:38:22 +08004364 RLOGD("not get tether info\n");
you.chen0f5c6432022-11-07 18:31:14 +08004365 return;
4366 }
you.chenc9928582023-04-24 15:39:37 +08004367 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);
4368 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);
4369 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 +08004370
4371 while (1) {
4372 sleep(1);
4373 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004374 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4375 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
4376 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004377 continue;
4378 //printf("ap1 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004379 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004380 if (n > 9) {
4381 if (strcmp(results[1], "Sent") == 0) {
4382 currAP1Bytes = atoll(results[2]);
4383 }
4384 if (strcmp(results[6], "(dropped") == 0) {
4385 currAP1Drop = atoi(results[7]);
4386 }
4387 }
4388
4389 memset(str_cmd, 0, sizeof(str_cmd));
you.chenc9928582023-04-24 15:39:37 +08004390 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
4391 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
4392 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen0f5c6432022-11-07 18:31:14 +08004393 continue;
4394 //printf("ap2 --- %s\n", str_cmd);
you.chenc9928582023-04-24 15:39:37 +08004395 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen0f5c6432022-11-07 18:31:14 +08004396 if (n > 9) {
4397 if (strcmp(results[1], "Sent") == 0) {
4398 currAP2Bytes = atoll(results[2]);
4399 }
4400 if (strcmp(results[6], "(dropped") == 0) {
4401 currAP2Drop = atoi(results[7]);
4402 }
4403 }
4404
4405 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
4406 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
4407 lastAP1Bytes = currAP1Bytes;
4408 lastAP2Bytes = currAP2Bytes;
4409 continue;
4410 }
4411
4412 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
4413 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
4414 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
4415 lastAP1Speed = currAP1Speed;
4416 lastAP2Speed = currAP2Speed;
4417 lastAP1Bytes = currAP1Bytes;
4418 lastAP2Bytes = currAP2Bytes;
4419
4420 currSetAP1Speed = setAP1Speed;
4421 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
4422 ncheckcount++;
4423 if (ncheckcount > 3) {
4424 ncheckcount = 0;
4425 currSetAP1Speed = 5;
4426 }
4427 }
4428 else {
4429 ncheckcount = 0;
4430 if (currAP1Speed < 5)
4431 nidlecount++;
4432 else
4433 nidlecount = 0;
4434
4435 }
4436
4437 if (nidlecount > 60 ){
4438 currSetAP1Speed = 50;
4439 }
4440
4441 if (currSetAP1Speed != setAP1Speed) {
4442 setAP1Speed = currSetAP1Speed;
you.chenc9928582023-04-24 15:39:37 +08004443 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
4444 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen0f5c6432022-11-07 18:31:14 +08004445 }
4446 }
4447}
4448
4449int enableGBW(const char* mac) {
4450 int i,len;
4451 char get_ipaddr_cmd[128]={0};
4452 ap_info_s *ap;
4453 device_info_s * list;
4454
4455 if (mac == NULL || g_gbw_enabled == 1)
4456 return -1;
4457 len = strlen(mac);
4458 g_gbw_mac = malloc(len + 1);
4459 for(i=0;i<len;i++) {
4460 if (mac[i] >= 'A' && mac[i] <= 'Z')
4461 {
4462 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
4463 }
4464 else
4465 g_gbw_mac[i] = mac[i];
4466 }
4467 g_gbw_mac[i] = '\0';
4468 g_gbw_enabled = 1;
4469
4470 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
4471 if (system(get_ipaddr_cmd) == 0) {
4472 //startGBW();
4473 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
4474 for (i=0;i<len;i++) {
4475 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
4476 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
4477 startGBW();
4478 }
4479 free(ap);
4480 free(list);
4481 }
4482 }
4483 return 0;
4484}
4485
4486int disableGBW() {
4487 stopGBW();
4488 free(g_gbw_mac);
4489 g_gbw_mac = NULL;
4490 g_gbw_enabled = 1;
4491 return 0;
4492}
4493
4494static int startGBW() {
4495 if (g_gbw_watcher_pid != 0) {
4496 stopGBW();
4497 }
4498 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
4499}
4500
4501static int stopGBW() {
4502 void* retval;
you.chenc9928582023-04-24 15:39:37 +08004503 char cmd[64] = {0};
you.chen0f5c6432022-11-07 18:31:14 +08004504 pthread_cancel(g_gbw_watcher_pid);
4505 pthread_join(g_gbw_watcher_pid, &retval);
4506 g_gbw_watcher_pid = 0;
you.chenc9928582023-04-24 15:39:37 +08004507 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
4508 if (s_ap_iterface_name[0] != '\0')
4509 {
4510 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
4511 system(cmd);
4512 }
you.chen0f5c6432022-11-07 18:31:14 +08004513}
4514//you.chen add for tv-box end