blob: 9529e2ce15747d679ebdce49705cfa505e166ce9 [file] [log] [blame]
qs.xiong799dab02022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen87ff5172022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong799dab02022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen87ff5172022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050013#include <string.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen87ff5172022-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.xiongf1e48bb2023-03-28 13:38:22 +080024#include "log/log.h"
you.chenf9d718d2023-04-14 18:17:09 +080025#include <sys/time.h>
26#include <asm/errno.h>
qs.xiong8d42bb92022-03-02 09:43:11 -050027
qs.xiong799dab02022-03-14 09:12:12 -040028#ifdef __cplusplus
29extern "C" {
30#endif
31#ifdef __cplusplus
32}
33#endif
you.chen87ff5172022-05-06 11:30:57 +080034
qs.xiongf1e48bb2023-03-28 13:38:22 +080035#undef LOG_TAG
36#define LOG_TAG "LYNQ_WIFI"
you.chen87ff5172022-05-06 11:30:57 +080037#define MAX_CMD 128
38#define MAX_RET 4096
qs.xiongd189c542022-03-31 00:58:23 -040039#define MODE_LEN 10
you.chen87ff5172022-05-06 11:30:57 +080040#define CTRL_STA 0
41#define CTRL_AP 1
42#define AP_NETWORK_0 0
43
44pthread_t g_ap_watcher_pid = 0;
45volatile int g_ap_watcher_stop_flag = 0;
you.chen6a9361d2022-05-18 10:28:19 +080046volatile int g_ap_watcher_started_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +080047
48pthread_t g_sta_watcher_pid = 0;
49volatile int g_sta_watcher_stop_flag = 0;
you.chen61c7aee2022-08-06 17:01:16 +080050volatile int g_sta_scan_finish_flag = 1;
you.chen6a9361d2022-05-18 10:28:19 +080051volatile int g_sta_watcher_started_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +080052
53void * g_ap_callback_priv = NULL;
54AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
55void * g_sta_callback_priv = NULL;
56STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
57
58//const char * CTRL_PATH="/var/run/wpa_supplicant";
59const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
60//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
61const char * cmd_list_networks = "LIST_NETWORKS";
62const char * cmd_save_config = "SAVE_CONFIG";
you.chenc7357f22022-05-16 17:55:28 +080063const char * cmd_disconnect = "DISCONNECT";
64const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen87ff5172022-05-06 11:30:57 +080065const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen61c7aee2022-08-06 17:01:16 +080066const char * STATE_COMPLETED = "COMPLETED";
you.chen87ff5172022-05-06 11:30:57 +080067
you.chen92668da2023-04-13 13:49:45 +080068const char * cmd_ping = "PING";
69const char * rsp_pong = "PONG";
70const int SLEEP_TIME_ON_IDLE = 100 * 1000; // usecond
71const int MAX_IDLE_COUNT = 600; // 60s
72
you.chencd882682023-04-24 15:39:37 +080073const char * start_wg870_service_script = "/etc/wg870/scripts/start_wg870_service.sh";
74const char * get_interface_name_script = "/etc/wg870/scripts/get_interface_name.sh";
75const char * start_stop_sta_script = "/etc/wg870/scripts/start_stop_sta.sh";
76const char * start_stop_ap_script = "/etc/wg870/scripts/start_stop_ap.sh";
77const char * sta_status_change_script = "/etc/wg870/scripts/sta_status_change.sh";
78
79static char s_ap_iterface_name[64] = {0};
80
you.chened802ab2023-02-13 10:50:35 +080081struct local_wpa_ctrl{
82 struct wpa_ctrl *ctrl;
83 pthread_mutex_t mutex;
84};
85
you.chenf9d718d2023-04-14 18:17:09 +080086
you.chened802ab2023-02-13 10:50:35 +080087static pthread_mutex_t s_check_wpa_ctrl_mutex = PTHREAD_MUTEX_INITIALIZER;
88
89static struct local_wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen87ff5172022-05-06 11:30:57 +080090
you.chen2ef1c552022-11-07 18:31:14 +080091//you.chen add for tv-box start
92volatile int g_gbw_enabled = 0;
93char * g_gbw_mac = NULL;
94pthread_t g_gbw_watcher_pid = 0;
95static int startGBW();
96static int stopGBW();
97//you.chen add for tv-box end
you.chen87ff5172022-05-06 11:30:57 +080098
99typedef struct __curr_status_info {
100 ap_info_s *ap;
101 char * state;
102 int net_no;
103}curr_status_info;
qs.xiong99b48d62022-04-07 05:41:29 -0400104
you.chenf9d718d2023-04-14 18:17:09 +0800105typedef enum {
106 INNER_STA_STATUS_INIT = 0,
107 INNER_STA_STATUS_CONNECTING,
108 INNER_STA_STATUS_ASSOCIATING,
109 INNER_STA_STATUS_ASSOCIATED,
110 INNER_STA_STATUS_CONNECTED,
111 INNER_STA_STATUS_DISCONNECTING,
112 INNER_STA_STATUS_DISCONNECTED,
113 INNER_STA_STATUS_CANCEL,
114}inner_sta_status_s;
115
116static pthread_cond_t s_global_check_cond = PTHREAD_COND_INITIALIZER;
117static pthread_mutex_t s_global_check_mutex = PTHREAD_MUTEX_INITIALIZER;
118static inner_sta_status_s s_sta_status = INNER_STA_STATUS_INIT;
119static error_number_s s_sta_error_number = -1;
120static char s_sta_current_connecting_ssid[64] = {0};
121static struct timespec s_sta_connect_timeout;
122const int MAX_CONNNECT_TIME = 15; // second
123pthread_t g_global_watcher_pid = 0;
124static int s_service_invoke_timeout_cnt=0;
125const int FAKE_MAX_INT_VALUE = 99999;
126
127static void notify_service_invoke_fail(int error)
128{
129 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
130 pthread_mutex_lock(&s_global_check_mutex);
131 if (error == -2) //timeout
132 {
133 s_service_invoke_timeout_cnt++;
134 if (s_service_invoke_timeout_cnt > 10)
135 {
136 pthread_cond_signal(&s_global_check_cond);
137 }
138 }
139 else if (error == -1)
140 {
141 // check if can connect wpa service
142 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[0]);
143 if (lynq_wpa_ctrl == NULL)
144 {
145 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
146 pthread_cond_signal(&s_global_check_cond);
147 }
148 wpa_ctrl_close(lynq_wpa_ctrl);
149 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[1]);
150 if (lynq_wpa_ctrl == NULL)
151 {
152 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
153 pthread_cond_signal(&s_global_check_cond);
154 }
155 wpa_ctrl_close(lynq_wpa_ctrl);
156 }
157
158 pthread_mutex_unlock(&s_global_check_mutex);
159}
160
you.chencd882682023-04-24 15:39:37 +0800161static int system_call_v(const char * fmt, ...)
162{
163 char str_cmd[256] = {0};
164 va_list args;
165 va_start(args, fmt);
166 vsprintf(str_cmd, fmt, args);
167 va_end(args);
168 printf("system call----------%s\n", str_cmd);
169 return system(str_cmd);
170}
171
you.chenf9d718d2023-04-14 18:17:09 +0800172static void check_tether_and_notify()
173{
174 RLOGD("check_tether_and_notify called");
you.chencd882682023-04-24 15:39:37 +0800175 if (s_ap_iterface_name[0] != '\0' && 0 == system_call_v("ifconfig | grep %s", s_ap_iterface_name))
you.chenf9d718d2023-04-14 18:17:09 +0800176 {
177 return;
178 }
179 pthread_mutex_lock(&s_global_check_mutex);
180 s_service_invoke_timeout_cnt = FAKE_MAX_INT_VALUE;
181 pthread_cond_signal(&s_global_check_cond);
182 pthread_mutex_unlock(&s_global_check_mutex);
183}
184
you.chened802ab2023-02-13 10:50:35 +0800185static int local_wpa_ctrl_request(struct local_wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,
186 char *reply, size_t *reply_len,
187 void (*msg_cb)(char *msg, size_t len))
188{
189 int ret;
190 if (ctrl->ctrl == NULL) {
qs.xiongf1e48bb2023-03-28 13:38:22 +0800191 RLOGE("local_wpa_ctrl_request ctrl is null\n");
you.chened802ab2023-02-13 10:50:35 +0800192 return -1;
193 }
194 pthread_mutex_lock(&ctrl->mutex);
195 ret = wpa_ctrl_request(ctrl->ctrl, cmd, cmd_len, reply, reply_len, msg_cb);
196 pthread_mutex_unlock(&ctrl->mutex);
you.chenf9d718d2023-04-14 18:17:09 +0800197 if (ret != 0)
198 {
199 notify_service_invoke_fail(ret);
200 }
you.chened802ab2023-02-13 10:50:35 +0800201 return ret;
202}
203
204static struct local_wpa_ctrl * inner_get_wpa_ctrl(int index) {
205 int repeat_cnt;
206 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL;
207 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
qs.xiongf1e48bb2023-03-28 13:38:22 +0800208 RLOGD("inner_get_wpa_ctrl\n");
you.chened802ab2023-02-13 10:50:35 +0800209 for (repeat_cnt = 0; repeat_cnt < 5 && NULL == g_lynq_wpa_ctrl[index]; repeat_cnt++) {
210 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
211// printf("wait enable finish\n");
212 usleep(500 * 1000);
213 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
214 }
215 if (NULL == g_lynq_wpa_ctrl[index]) {
qs.xiongf1e48bb2023-03-28 13:38:22 +0800216 RLOGE("NULL == g_lynq_wpa_ctrl[index]");
you.chened802ab2023-02-13 10:50:35 +0800217 goto out_addr;
218 }
219 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
220 g_lynq_wpa_ctrl[index]->ctrl = wpa_ctrl_open(CTRL_PATH[index]);
221 if (NULL == g_lynq_wpa_ctrl[index]->ctrl) {
qs.xiongf1e48bb2023-03-28 13:38:22 +0800222 RLOGE("wpa_ctrl_open fail\n");
you.chened802ab2023-02-13 10:50:35 +0800223 goto out_addr;
224 }
225 pthread_mutex_init(&g_lynq_wpa_ctrl[index]->mutex, NULL);
226 }
227 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index];
228out_addr:
229 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
230 return lynq_wpa_ctrl;
231}
232
qs.xiong99b48d62022-04-07 05:41:29 -0400233#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong99b48d62022-04-07 05:41:29 -0400234{\
you.chen87ff5172022-05-06 11:30:57 +0800235 perror((str));\
236 return (value);\
qs.xiong99b48d62022-04-07 05:41:29 -0400237}
238
you.chen87ff5172022-05-06 11:30:57 +0800239#define CHECK_IDX(idx, type) do { \
240 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
241 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
qs.xiongf1e48bb2023-03-28 13:38:22 +0800242 RLOGE("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
you.chen87ff5172022-05-06 11:30:57 +0800243 return -1; \
244 } \
245 }while (0)
246
247#define CHECK_WPA_CTRL(index) int ret = 0;\
248 size_t reply_len = MAX_RET; \
249 char cmd_reply[MAX_RET]={0}; \
you.chened802ab2023-02-13 10:50:35 +0800250 struct local_wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen87ff5172022-05-06 11:30:57 +0800251 do{ \
you.chened802ab2023-02-13 10:50:35 +0800252 lynq_wpa_ctrl = inner_get_wpa_ctrl(index); \
253 if (NULL == lynq_wpa_ctrl) return -1; \
you.chen87ff5172022-05-06 11:30:57 +0800254 }while(0)
255
256#define DO_REQUEST(cmd_str) do { \
257 reply_len = MAX_RET;\
258 cmd_reply[0] = '\0'; \
qs.xiongf1e48bb2023-03-28 13:38:22 +0800259 RLOGD("to call [%s]\n", cmd_str); \
you.chened802ab2023-02-13 10:50:35 +0800260 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen87ff5172022-05-06 11:30:57 +0800261 if (ret != 0) { \
qs.xiongf1e48bb2023-03-28 13:38:22 +0800262 RLOGE("call "#cmd_str" fail %d\n", ret); \
you.chen87ff5172022-05-06 11:30:57 +0800263 return ret; \
264 } \
265 cmd_reply[reply_len+1] = '\0'; \
qs.xiongf1e48bb2023-03-28 13:38:22 +0800266 RLOGD("cmd replay [ %s ]\n", cmd_reply); \
you.chen87ff5172022-05-06 11:30:57 +0800267 }while(0)
268
269#define DO_OK_FAIL_REQUEST(cmd_str) do { \
270 DO_REQUEST(cmd_str); \
271 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
qs.xiongf1e48bb2023-03-28 13:38:22 +0800272 RLOGE("cmd "#cmd_str" return FAIL\n"); \
you.chen87ff5172022-05-06 11:30:57 +0800273 return -1; \
274 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
qs.xiongf1e48bb2023-03-28 13:38:22 +0800275 RLOGE("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen87ff5172022-05-06 11:30:57 +0800276 return -1; \
277 } \
278 }while (0)
279
280
you.chened802ab2023-02-13 10:50:35 +0800281static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len);
you.chen87ff5172022-05-06 11:30:57 +0800282
you.chen92668da2023-04-13 13:49:45 +0800283static int check_connection(struct wpa_ctrl * wpa_ctrl)
284{
285 size_t reply_len = MAX_RET;
286 char cmd_reply[MAX_RET]={0};
287 int ret;
288
289 RLOGD("check_connection [%p]", wpa_ctrl);
290 ret = wpa_ctrl_request(wpa_ctrl, cmd_ping, strlen(cmd_ping), cmd_reply, &reply_len, NULL);
291
292 if (ret != 0 || reply_len < 4 || memcmp(cmd_reply, rsp_pong, 4) != 0)
293 {
294 RLOGE("check_connection error: ctrl [%p], ret [%d], reply_len [%d], rsp [%s]", wpa_ctrl, ret, reply_len, cmd_reply);
you.chenf9d718d2023-04-14 18:17:09 +0800295 if (ret != 0)
296 {
297 notify_service_invoke_fail(ret);
298 }
you.chen92668da2023-04-13 13:49:45 +0800299 return -1;
300 }
301
302 return 0;
303}
304
305/**
306 * @brief check_pending_msg
307 * @param lynq_wpa_ctrl
308 * @return 1 has msg, 0 no msg, -1 error
309 */
310static int check_pending_msg(struct wpa_ctrl ** pp_lynq_wpa_ctrl, int type, int* idle_count, int *started_flag)
311{
312 int ret;
313
314 if (*pp_lynq_wpa_ctrl == NULL) // need connect
315 {
316 *pp_lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[type]); //@todo temp change
317 if (*pp_lynq_wpa_ctrl == NULL)
318 {
319 usleep(SLEEP_TIME_ON_IDLE);
320 return -1;
321 }
322
323 ret = wpa_ctrl_attach(*pp_lynq_wpa_ctrl);
324 if (ret == 0) // attach success
325 {
326 *started_flag = 1;
327 }
328 else
329 {
you.chenf9d718d2023-04-14 18:17:09 +0800330 RLOGE("[wifi error]sta watch thread wpa_ctrl_attach fail");
you.chen92668da2023-04-13 13:49:45 +0800331 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
332 *pp_lynq_wpa_ctrl = NULL;
333 *idle_count = 0;
you.chenf9d718d2023-04-14 18:17:09 +0800334 notify_service_invoke_fail(-2);
you.chen92668da2023-04-13 13:49:45 +0800335 usleep(SLEEP_TIME_ON_IDLE);
336 return -1;
337 }
338 }
339
340 ret = wpa_ctrl_pending(*pp_lynq_wpa_ctrl);
341 if ( ret == 0) // no pending messages
342 {
343 usleep(SLEEP_TIME_ON_IDLE);
344 *idle_count += 1;
345 if (*idle_count > MAX_IDLE_COUNT)
346 {
347 if (check_connection(*pp_lynq_wpa_ctrl) != 0)
348 {
349 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
350 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
351 *pp_lynq_wpa_ctrl = NULL;
352 *idle_count = 0;
353 return -1;
354 }
355 *idle_count = 0;
356 }
357 return 0;
358 }
359 else if ( ret == -1) // on error
360 {
361 RLOGE("[wifi error]sta wpa_ctrl_pending");
362 wpa_ctrl_detach(*pp_lynq_wpa_ctrl);
363 wpa_ctrl_close(*pp_lynq_wpa_ctrl);
364 *pp_lynq_wpa_ctrl = NULL;
365 *idle_count = 0;
you.chenf9d718d2023-04-14 18:17:09 +0800366 notify_service_invoke_fail(ret);
you.chen92668da2023-04-13 13:49:45 +0800367 return -1;
368 }
369
370 *idle_count = 0;
371 return 1;
372}
373
you.chen87ff5172022-05-06 11:30:57 +0800374static void APWatcherThreadProc() {
375 size_t len = MAX_RET;
376 char msg_notify[MAX_RET];
you.chen92668da2023-04-13 13:49:45 +0800377 int idle_count = 0;
you.chen87ff5172022-05-06 11:30:57 +0800378
you.chenc7357f22022-05-16 17:55:28 +0800379 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen01276462022-05-25 10:09:47 +0800380 g_ap_watcher_stop_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +0800381
qs.xiongf1e48bb2023-03-28 13:38:22 +0800382 while (g_ap_watcher_stop_flag == 0)
383 {
you.chen92668da2023-04-13 13:49:45 +0800384 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_AP, &idle_count, &g_ap_watcher_started_flag) != 1)
385 {
you.chenf9d718d2023-04-14 18:17:09 +0800386 if (g_ap_callback_func != NULL && idle_count == MAX_IDLE_COUNT - 100 )
387 {
388 check_tether_and_notify();
389 }
390
you.chen87ff5172022-05-06 11:30:57 +0800391 continue;
392 }
you.chen92668da2023-04-13 13:49:45 +0800393
you.chenc7357f22022-05-16 17:55:28 +0800394 memset(msg_notify, 0, MAX_RET);
395 len = MAX_RET;
qs.xiongf1e48bb2023-03-28 13:38:22 +0800396 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
you.chen92668da2023-04-13 13:49:45 +0800397 {
you.chen87ff5172022-05-06 11:30:57 +0800398 msg_notify[len+1] = '\0';
qs.xiongaf960232023-04-12 11:40:02 +0800399 RLOGD("APWatcherThreadProc ap------> %s\n", msg_notify);
you.chen92668da2023-04-13 13:49:45 +0800400 //you.chen change for tv-box start
qs.xiongf1e48bb2023-03-28 13:38:22 +0800401 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL)
you.chen92668da2023-04-13 13:49:45 +0800402 {
you.chen2ef1c552022-11-07 18:31:14 +0800403 if (g_ap_callback_func != NULL)
404 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
qs.xiongf1e48bb2023-03-28 13:38:22 +0800405 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chen92668da2023-04-13 13:49:45 +0800406 {
qs.xiongf1e48bb2023-03-28 13:38:22 +0800407 RLOGD("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
408 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chen92668da2023-04-13 13:49:45 +0800409 {
you.chen2ef1c552022-11-07 18:31:14 +0800410 stopGBW();
411 }
412 }
you.chen87ff5172022-05-06 11:30:57 +0800413 }
qs.xiongf1e48bb2023-03-28 13:38:22 +0800414 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL)
you.chen92668da2023-04-13 13:49:45 +0800415 {
you.chen2ef1c552022-11-07 18:31:14 +0800416 if (g_ap_callback_func != NULL)
417 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
qs.xiongf1e48bb2023-03-28 13:38:22 +0800418 if (g_gbw_enabled == 1 && g_gbw_mac != NULL)
you.chen92668da2023-04-13 13:49:45 +0800419 {
qs.xiongf1e48bb2023-03-28 13:38:22 +0800420 RLOGD("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
421 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL)
you.chen92668da2023-04-13 13:49:45 +0800422 {
you.chen2ef1c552022-11-07 18:31:14 +0800423 startGBW();
424 }
425 }
you.chen87ff5172022-05-06 11:30:57 +0800426 }
you.chen92668da2023-04-13 13:49:45 +0800427 //you.chen add for tv-box end
you.chen87ff5172022-05-06 11:30:57 +0800428 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
429 } // end while (g_ap_watcher_stop_flag == 0)
qs.xiongf1e48bb2023-03-28 13:38:22 +0800430 if (lynq_wpa_ctrl != NULL)
431 {
you.chen01276462022-05-25 10:09:47 +0800432 wpa_ctrl_detach(lynq_wpa_ctrl);
433 wpa_ctrl_close(lynq_wpa_ctrl);
434 }
qs.xiong99b48d62022-04-07 05:41:29 -0400435}
436
you.chenf9d718d2023-04-14 18:17:09 +0800437static void inner_check_connect_error(const char * event_msg, lynq_wifi_sta_status_s state, error_number_s error_num)
438{
439 char * p;
440 const char * try_associat_flag = "Trying to associate";
441 const char * associated_flag = "Associated with ";
442
443 pthread_mutex_lock(&s_global_check_mutex);
444 if (s_sta_status < INNER_STA_STATUS_CONNECTING || s_sta_status >= INNER_STA_STATUS_CONNECTED) //not in connecting stage, egnore
445 {
446 pthread_mutex_unlock(&s_global_check_mutex);
447 return;
448 }
449
450 if (state == LYNQ_WIFI_STATUS_EGNORE)
451 {
452 if (strstr(event_msg, try_associat_flag) != NULL && strstr(event_msg, s_sta_current_connecting_ssid) != NULL) //associating request ssid
453 {
454 s_sta_status = INNER_STA_STATUS_ASSOCIATING;
455 }
456 else if (s_sta_status == INNER_STA_STATUS_ASSOCIATING && (p=strstr(event_msg, associated_flag)) != NULL)
457 {
458 s_sta_status = INNER_STA_STATUS_ASSOCIATED;
459 }
460 }
461 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
462 {
463 s_sta_error_number = error_num;
464 if (s_sta_status >= INNER_STA_STATUS_ASSOCIATING && strstr(event_msg, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL && error_num != LYNQ_WAIT_CONNECT_ACTIVE)
465 {
466 s_sta_status = INNER_STA_STATUS_DISCONNECTED;
467 pthread_cond_signal(&s_global_check_cond);
468 }
469 }
470 else if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
471 {
472 s_sta_status = INNER_STA_STATUS_CONNECTED;
473 pthread_cond_signal(&s_global_check_cond);
474 }
475 pthread_mutex_unlock(&s_global_check_mutex);
476}
477
qs.xiongaf960232023-04-12 11:40:02 +0800478void get_state_error(const char* modify, lynq_wifi_sta_status_s* state, error_number_s* error)
479{
480 char *pReason;
481 *error = LYNQ_WAIT_CONNECT_ACTIVE;
482 if (strstr(modify, "CTRL-EVENT-SCAN-RESULTS") != NULL)
483 {
484 *state = LYNQ_WIFI_STA_STATUS_SCAN_RESULT;
485 RLOGD("CTRL-EVENT-SCAN-RESULTS state:%d,error:%d\n",*state,*error);
486 return;
487 }
488
489 if (strstr(modify, "CTRL-EVENT-CONNECTED") != NULL)
490 {
491 *state = LYNQ_WIFI_STA_STATUS_CONNECT;
492 RLOGD("CTRL-EVENT-CONNECTED state:%d,error:%d\n",*state,*error);
493 return;
494 }
495
496 if (strstr(modify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL)
497 {
498 pReason = strstr(modify, "reason=");
499 if (pReason != NULL)
500 {
501 pReason += strlen("reason=");
502 if (memcmp(pReason, "CONN_FAILED", 11) == 0)
503 {
504 *error = LYNQ_TIME_OUT;
505 }
506 else if (memcmp(pReason, "WRONG_KEY", 9) == 0)
507 {
508 *error = LYNQ_PSW_ERROR;
509 }
510 else
511 {
512 *error = LYNQ_UNSPECIFIED_REASON;
513 }
514 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
515 RLOGD("CTRL-EVENT-SSID-TEMP-DISABLED state:%d,error:%d\n",*state,*error);
516 return;
517 }
518 else
519 {
520 *error = LYNQ_UNSPECIFIED_REASON;
521 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
522 return;
523 }
524
525 }
526
527 if (strstr(modify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL)
528 {
529 *error = LYNQ_NOT_FIND_AP;
530 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
531 RLOGD("CTRL-EVENT-NETWORK-NOT-FOUND state:%d,error:%d\n",*state,*error);
532 return;
533 }
534
535
536 if (strstr(modify, "CTRL-EVENT-ASSOC-REJECT") != NULL)
537 {
538 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT\n");
539 pReason = strstr(modify, "status_code=");
540 if (pReason != NULL)
541 {
542 pReason += strlen("status_code=");
543 if (memcmp(pReason, "17", 2) == 0)
544 {
545 *error = LYNQ_AP_UNABLE_HANDLE;
546 }
547 else if (memcmp(pReason, "1",1) == 0)
548 {
549 *error = LYNQ_UNSPECIFIED_REASON;
550 }
551 else
552 {
553 *error = LYNQ_UNSPECIFIED_REASON;
554 }
555
556 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
557 RLOGD("CTRL-EVENT-ASSOC-REJECT BUT NOT STATUS_CODE NOT 1 or 17 state:%d,error:%d\n",*state,*error);
558 return;
559 }
560 else
561 {
562 RLOGD("FIND CTRL EVENT : CTRL-EVENT-ASSOC-REJECT BUT NOT FOUND STATUS_CODE\n");
563 *error = LYNQ_UNSPECIFIED_REASON;
564 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
565 RLOGD("CTRL-EVENT-ASSOC-REJECT state:%d,error:%d\n",*state,*error);
566 return;
567 }
568 }
569
570 if (strstr(modify, "CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER") != NULL)
571 {
572 RLOGD("FIND CTRL EVENT : CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER\n");
573 *error = LYNQ_AUTHENTICATION_NO_LONGER_VALID;
574 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
575 RLOGD("CTRL-EVENT-SAE-UNKNOWN-PASSWORD-IDENTIFIER state:%d,error:%d\n",*state,*error);
576 return;
577 }
578
579 if (strstr(modify, "CTRL-EVENT-DISCONNECTED") != NULL)
580 {
581 RLOGD("FIND CTRL EVENT : CTRL-EVENT-DISCONNECTED\n");
582 *error = LYNQ_WAIT_CONNECT_ACTIVE;
583 *state = LYNQ_WIFI_STA_STATUS_DISCONNECT;
584 RLOGD("CTRL-EVENT-DISCONNECTED state:%d,error:%d\n",*state,*error);
585 return;
586 }
587
you.chen4ca39eb2023-04-13 14:05:45 +0800588 RLOGD("EVENT : %s\n", modify);
qs.xiongaf960232023-04-12 11:40:02 +0800589 *error = LYNQ_UNSPECIFIED_REASON;
you.chen4ca39eb2023-04-13 14:05:45 +0800590 *state = LYNQ_WIFI_STATUS_EGNORE;
qs.xiongaf960232023-04-12 11:40:02 +0800591 RLOGD("LAST : STA state:%d,error:%d\n",*state,*error);
592 return;
593
594}
595
you.chenf9d718d2023-04-14 18:17:09 +0800596static void notify_connect_status(lynq_wifi_sta_status_s state, error_number_s error)
597{
598 if (g_sta_callback_func != NULL && state != LYNQ_WIFI_STATUS_EGNORE)
599 {
600 RLOGD("STAWatcherThreadProc callback begin ------> %d %d\n", state, error);
601 g_sta_callback_func(g_sta_callback_priv, state, error);
602 RLOGD("STAWatcherThreadProc callback end ------> %d %d\n", state, error);
603 }
604}
605
you.chen87ff5172022-05-06 11:30:57 +0800606static void STAWatcherThreadProc() {
607 size_t len = MAX_RET;
608 char msg_notify[MAX_RET];
you.chen87ff5172022-05-06 11:30:57 +0800609 error_number_s error;
you.chencd882682023-04-24 15:39:37 +0800610 lynq_wifi_sta_status_s state, last_state = -1;
you.chen92668da2023-04-13 13:49:45 +0800611 int idle_count = 0;
qs.xiongd189c542022-03-31 00:58:23 -0400612
you.chenc7357f22022-05-16 17:55:28 +0800613 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen01276462022-05-25 10:09:47 +0800614 g_sta_watcher_stop_flag = 0;
you.chen87ff5172022-05-06 11:30:57 +0800615
you.chenf9d718d2023-04-14 18:17:09 +0800616 RLOGD("STAWatcherThreadProc thread started ------");
qs.xiongf1e48bb2023-03-28 13:38:22 +0800617 while (g_sta_watcher_stop_flag == 0)
618 {
you.chen92668da2023-04-13 13:49:45 +0800619 if (check_pending_msg(&lynq_wpa_ctrl, CTRL_STA, &idle_count, &g_sta_watcher_started_flag) != 1)
qs.xiongaf960232023-04-12 11:40:02 +0800620 {
you.chen87ff5172022-05-06 11:30:57 +0800621 continue;
622 }
you.chen92668da2023-04-13 13:49:45 +0800623
you.chenc7357f22022-05-16 17:55:28 +0800624 memset(msg_notify, 0, MAX_RET);
625 len = MAX_RET;
qs.xiongf1e48bb2023-03-28 13:38:22 +0800626 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
qs.xiongaf960232023-04-12 11:40:02 +0800627 {
you.chen87ff5172022-05-06 11:30:57 +0800628 msg_notify[len+1] = '\0';
qs.xiongf1e48bb2023-03-28 13:38:22 +0800629 RLOGD("STAWatcherThreadProc sta ------> %s\n", msg_notify);
630 if (strstr(msg_notify, state_scan_result) != NULL)
qs.xiongaf960232023-04-12 11:40:02 +0800631 {
you.chen87ff5172022-05-06 11:30:57 +0800632 g_sta_scan_finish_flag = 1;
633 }
634
qs.xiongf1e48bb2023-03-28 13:38:22 +0800635 if (g_sta_callback_func == NULL)
qs.xiongaf960232023-04-12 11:40:02 +0800636 {
you.chen87ff5172022-05-06 11:30:57 +0800637 continue;
638 }
qs.xiongaf960232023-04-12 11:40:02 +0800639 get_state_error(msg_notify,&state,&error);
you.chenf9d718d2023-04-14 18:17:09 +0800640 notify_connect_status(state, error);
641
642 if (state != LYNQ_WIFI_STA_STATUS_SCAN_RESULT)
you.chen4ca39eb2023-04-13 14:05:45 +0800643 {
you.chenf9d718d2023-04-14 18:17:09 +0800644 inner_check_connect_error(msg_notify, state, error);
you.chencd882682023-04-24 15:39:37 +0800645 if (last_state != state)
646 {
647 if (state == LYNQ_WIFI_STA_STATUS_CONNECT)
648 {
649 system_call_v("%s %s", sta_status_change_script, "connect");
650 }
651 else if (state == LYNQ_WIFI_STA_STATUS_DISCONNECT)
652 {
653 system_call_v("%s %s", sta_status_change_script, "disconnect");
654 }
655 }
656
657 last_state = state;
you.chen4ca39eb2023-04-13 14:05:45 +0800658 }
you.chen87ff5172022-05-06 11:30:57 +0800659 }
660 }
qs.xiongf1e48bb2023-03-28 13:38:22 +0800661 if (lynq_wpa_ctrl != NULL)
662 {
you.chen01276462022-05-25 10:09:47 +0800663 wpa_ctrl_detach(lynq_wpa_ctrl);
664 wpa_ctrl_close(lynq_wpa_ctrl);
665 }
qs.xiongd189c542022-03-31 00:58:23 -0400666}
667
you.chenf9d718d2023-04-14 18:17:09 +0800668// this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
669static void GlobalWatcherThreadProc()
670{
671 int ret, connect_timeout, service_abnormal;
672 error_number_s error_num = -1;
673 inner_sta_status_s sta_status;
674 scan_info_s *scan_list = NULL;
675 int i, scan_len=0;
676 char connecting_ssid[64];
677 struct timeval now;
678
679 RLOGD("GlobalWatcherThreadProc start to run");
680
681 while (1)
682 {
683 pthread_mutex_lock(&s_global_check_mutex);
684 pthread_cond_wait(&s_global_check_cond,&s_global_check_mutex);
685 if (s_sta_status == INNER_STA_STATUS_CONNECTED)
686 {
687 pthread_mutex_unlock(&s_global_check_mutex);
688 usleep(50*1000);
689 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
690 continue;
691 }
692
693 connect_timeout = 0;
694 service_abnormal = 0;
695 if (s_sta_status >= INNER_STA_STATUS_CONNECTING && s_sta_status < INNER_STA_STATUS_CONNECTED)
696 {
697 while (1)
698 {
699 ret = pthread_cond_timedwait(&s_global_check_cond, &s_global_check_mutex, &s_sta_connect_timeout);
700 if (ret == ETIME)
701 {
702 connect_timeout = 1;
703 }
704 else if (ret != 0)
705 {
706 gettimeofday(&now,NULL);
707 if (now.tv_sec < s_sta_connect_timeout.tv_sec) //time not arrive
708 {
709 usleep(SLEEP_TIME_ON_IDLE);
710 continue;
711 }
712 connect_timeout = 1;
713 }
714 sta_status = s_sta_status;
715 error_num = s_sta_error_number;
716 s_sta_status = INNER_STA_STATUS_INIT;
717 strcpy(connecting_ssid, s_sta_current_connecting_ssid);
718 memset(&s_sta_connect_timeout, 0, sizeof (s_sta_connect_timeout));
719 memset(&s_sta_current_connecting_ssid, 0, sizeof (s_sta_current_connecting_ssid));
720 break;
721 }
722 }
723 if (s_service_invoke_timeout_cnt > 10)
724 {
725 service_abnormal = 1;
726 s_service_invoke_timeout_cnt = 0;
727 }
728 pthread_mutex_unlock(&s_global_check_mutex);
729
730 if (service_abnormal == 1)
731 {
732 sleep(1);
733 RLOGE("wpa service is abnormal info app to exit");
734 notify_connect_status(LYNQ_WIFI_STA_SERVICE_ABNORMAL, -1);
735 if (g_ap_callback_func != NULL)
736 {
737 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_SERVICE_ABNORMAL);
738 }
739 sleep(FAKE_MAX_INT_VALUE); // wait process to exit
740 }
741
742 if (sta_status == INNER_STA_STATUS_CANCEL)
743 {
744 continue;
745 }
746 else if (sta_status == INNER_STA_STATUS_CONNECTED)
747 {
748 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
749 }
750 else if (connect_timeout == 0 && error_num == LYNQ_NOT_FIND_AP) // not found ap maybe mismatch auth
751 {
752 if (0 == lynq_get_scan_list(0, &scan_list, &scan_len) && NULL != scan_list) // if not found, but scan result exist, maybe auth error
753 {
754 for(i=0; i < scan_len;i++)
755 {
756 if (strcmp(scan_list[i].ssid, connecting_ssid) == 0)
757 {
758 error_num = LYNQ_AUTH_ERROR;
759 break;
760 }
761 }
762 free(scan_list);
763 }
764 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
765 }
766 else if (connect_timeout == 0)
767 {
768 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, error_num);
769 }
770 else // wait timeout
771 {
772 if (0 != lynq_get_sta_status(LYNQ_WIFI_INTERFACE_0, &sta_status)) // get status fail
773 {
774 ; // wpa service abnormal
775 }
776 else if (sta_status == LYNQ_WIFI_STA_STATUS_ENABLE) // connect ok
777 {
778 RLOGD("GlobalWatcherThreadProc notify connected");
779 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT, 0);
780 }
781 else
782 {
783 RLOGD("GlobalWatcherThreadProc notify timeout");
784 notify_connect_status(LYNQ_WIFI_STA_STATUS_CONNECT_FAIL, LYNQ_TIME_OUT);
785 }
786 }
787 } // while (1)
788}
789
qs.xiong799dab02022-03-14 09:12:12 -0400790int lynq_wifi_enable(void)
791{
you.chen87ff5172022-05-06 11:30:57 +0800792 int ret = 0;
you.chenc7357f22022-05-16 17:55:28 +0800793 int i;
qs.xiongf1e48bb2023-03-28 13:38:22 +0800794 RLOGD("enter lynq_wifi_enable");
you.chened802ab2023-02-13 10:50:35 +0800795 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
796
qs.xiongf1e48bb2023-03-28 13:38:22 +0800797 if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL)
798 {
you.chened802ab2023-02-13 10:50:35 +0800799 goto out_enable;
800 }
801
you.chencd882682023-04-24 15:39:37 +0800802 ret = system(start_wg870_service_script);
qs.xiongf1e48bb2023-03-28 13:38:22 +0800803 if (ret != 0)
804 {
805 //printf("service state %d\n", ret);
806 RLOGE("[wifi error]service state %d",ret);
you.chened802ab2023-02-13 10:50:35 +0800807 ret = -1;
808 goto out_enable;
you.chen87ff5172022-05-06 11:30:57 +0800809 }
lhb29aec32022-10-11 18:55:36 +0800810
you.chenf9d718d2023-04-14 18:17:09 +0800811 if (g_global_watcher_pid == 0 ) // this thread will not exit when lynq_wifi_disable called,to avoid dead lock,take care
812 {
813 ret=pthread_create(&g_global_watcher_pid,NULL,GlobalWatcherThreadProc,NULL);
814 if(ret<0)
815 {
816 RLOGE("[wifi error]creat GlobalWatcherThreadProc fail");
817 ret = -1;
818 goto out_enable;
819 }
820 }
821
you.chened802ab2023-02-13 10:50:35 +0800822 g_lynq_wpa_ctrl[0] = malloc(sizeof (struct local_wpa_ctrl));
823 g_lynq_wpa_ctrl[1] = malloc(sizeof (struct local_wpa_ctrl));
824 memset(g_lynq_wpa_ctrl[0], 0 , sizeof(struct local_wpa_ctrl));
825 memset(g_lynq_wpa_ctrl[1], 0 , sizeof(struct local_wpa_ctrl));
826out_enable:
827 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
you.chen87ff5172022-05-06 11:30:57 +0800828 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -0500829}
830
qs.xiong799dab02022-03-14 09:12:12 -0400831int lynq_wifi_disable(void)
832{
qs.xiongf1e48bb2023-03-28 13:38:22 +0800833 RLOGD("enter lynq_wifi_disable");
you.chened802ab2023-02-13 10:50:35 +0800834 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen87ff5172022-05-06 11:30:57 +0800835 g_ap_watcher_stop_flag = 1;
836 g_sta_watcher_stop_flag = 1;
837 if (g_ap_watcher_pid != 0)
838 pthread_join(g_ap_watcher_pid, NULL);
839 if (g_sta_watcher_pid != 0)
840 pthread_join(g_sta_watcher_pid, NULL);
841 if (g_lynq_wpa_ctrl[0] != NULL)
842 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
843 if (g_lynq_wpa_ctrl[1] != NULL)
844 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
845 g_ap_watcher_pid = 0;
846 g_sta_watcher_pid = 0;
847 g_lynq_wpa_ctrl[0] = NULL;
848 g_lynq_wpa_ctrl[1] = NULL;
849 system("systemctl stop wg870_drv_insmod.service");
you.chened802ab2023-02-13 10:50:35 +0800850 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
851 return 0;
852}
853
854static inline char inner_convert_char(char in)
855{
856 if (in >= '0' && in <= '9')
857 {
858 return in - '0';
859 }
860 else if (in >= 'a' && in <= 'f')
861 {
862 return in - 'a' + 10;
863 }
864 else if (in >= 'A' && in <= 'F')
865 {
866 return in - 'A' + 10;
867 }
868 else
869 {
870 return '\xff';
871 }
872}
873
874static inline void inner_copy_ssid(char * out_ssid, const char * ssid, size_t out_ssid_len)
875{
876 char *p;
877 size_t pos = 0;
878 if (NULL == out_ssid)
879 return;
880 //printf("input ssid=[%s]\n", ssid);
881 memset(out_ssid, 0, out_ssid_len);
882 if (NULL == ssid)
883 return;
884 p = strchr(ssid, '\\');
885 if (NULL == p)
886 {
887 strncpy(out_ssid, ssid, out_ssid_len);
888 //printf(" first %s\n", out_ssid);
889 }
890 else
891 {
892 pos = p - ssid;
893 memcpy(out_ssid, ssid, pos);
894 //printf("pos %lu -- %s\n", pos, out_ssid);
895 for(; pos < out_ssid_len; pos ++)
896 {
897 if (p[0] == '\0')
898 {
899 //printf(" out %s\n", out_ssid);
900 return;
901 }
902 else if (p[0] != '\\')
903 {
904 out_ssid[pos] = p[0];
905 p += 1;
906 }
907 else if (p[1] == 'x' || p[1] == 'X')
908 {
909 out_ssid[pos] = inner_convert_char(p[2]) << 4 | inner_convert_char(p[3]);
910 p += 4;
911 }
912 else if (p[1] == '\\')
913 {
914 out_ssid[pos] = '\\';
915 p += 2;
916 }
917 else if (p[1] == 't')
918 {
919 out_ssid[pos] = '\t';
920 p += 2;
921 }
922 else if (p[1] == 'r')
923 {
924 out_ssid[pos] = '\r';
925 p += 2;
926 }
927 else if (p[1] == 'n')
928 {
929 out_ssid[pos] = '\n';
930 p += 2;
931 }//todo find a better way to convert?
932 }
933 }
934 //printf(" out %s\n", out_ssid);
qs.xiong8d42bb92022-03-02 09:43:11 -0500935}
qs.xiong799dab02022-03-14 09:12:12 -0400936
you.chen87ff5172022-05-06 11:30:57 +0800937static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
you.chened802ab2023-02-13 10:50:35 +0800938 int i, ssid_len;
you.chen87ff5172022-05-06 11:30:57 +0800939 char lynq_cmd_get[128]={0};
qs.xiongf1e48bb2023-03-28 13:38:22 +0800940 RLOGD("enter inner_get_param");
941 if (out_put == NULL)
942 {
943 RLOGE("output ptr is null");
you.chen87ff5172022-05-06 11:30:57 +0800944 return -1;
945 }
qs.xiongf1e48bb2023-03-28 13:38:22 +0800946 if (param_name == NULL)
947 {
948 RLOGE("param ptr is null");
you.chen87ff5172022-05-06 11:30:57 +0800949 return -1;
950 }
qs.xiongf1e48bb2023-03-28 13:38:22 +0800951 if (param_name[0] == '\0')
952 {
953 RLOGE("param is empty");
you.chen87ff5172022-05-06 11:30:57 +0800954 return -1;
955 }
956
957 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
958
959 CHECK_WPA_CTRL(interface);
960
961 DO_REQUEST(lynq_cmd_get);
962
qs.xiongf1e48bb2023-03-28 13:38:22 +0800963 if (memcmp(cmd_reply, "FAIL", 4) == 0)
964 {
965 RLOGE("wpa_supplicant return cmd_reply is FAIL");
you.chen87ff5172022-05-06 11:30:57 +0800966 return -1;
967 }
968
you.chen6a9361d2022-05-18 10:28:19 +0800969// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chened802ab2023-02-13 10:50:35 +0800970 if (strcmp(param_name, "ssid") == 0)
971 {
qs.xiongf1e48bb2023-03-28 13:38:22 +0800972 if (cmd_reply[0] == '\"')
973 {
you.chened802ab2023-02-13 10:50:35 +0800974 ssid_len = reply_len - 1;
975 memcpy(out_put, cmd_reply + 1, ssid_len);
976 if (out_put[ssid_len-1] == '\"')
977 {
978 out_put[ssid_len-1] = '\0';
979 }
980 else
981 {
982 out_put[ssid_len] = '\0';
983 }
984 }
qs.xiongf1e48bb2023-03-28 13:38:22 +0800985 else
986 {
you.chened802ab2023-02-13 10:50:35 +0800987 ssid_len = reply_len / 2;
988 for(i=0; i<ssid_len; i++)
989 {
990 out_put[i] = inner_convert_char(cmd_reply[i*2]) << 4 | inner_convert_char(cmd_reply[i*2 + 1]);
991 }
992 out_put[ssid_len] = '\0';
993 }
994 }
995 else
996 {
997 memcpy(out_put, cmd_reply, reply_len + 1);
998 }
you.chen87ff5172022-05-06 11:30:57 +0800999 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001000}
qs.xiong799dab02022-03-14 09:12:12 -04001001
you.chen87ff5172022-05-06 11:30:57 +08001002static int lynq_split(char * str, int len, char delimiter, char * results[]) {
1003 int ret = 0;
1004 char * end = str + len - 1;
1005 results[ret++] = str;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001006 while(str < end)
1007 {
1008 if (*str == delimiter)
1009 {
you.chen87ff5172022-05-06 11:30:57 +08001010 *str++ = '\0';
1011 results[ret++] = str;
1012 continue;
1013 }
1014 str++;
1015 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001016 if (*str == delimiter)
1017 {
you.chen87ff5172022-05-06 11:30:57 +08001018 *str = '\0';
1019 }
qs.xiong799dab02022-03-14 09:12:12 -04001020
you.chen7bf12432023-04-27 17:51:56 +08001021 results[ret] = NULL;
1022
you.chen87ff5172022-05-06 11:30:57 +08001023 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -05001024}
1025
you.chened802ab2023-02-13 10:50:35 +08001026static int inner_get_ip_by_mac(const char * mac, char * ip, int ip_len)
1027{
1028 char * p;
1029 int ret = 0;
1030 char cmd[256]={0};
1031 if (NULL == mac || NULL == ip)
you.chen87ff5172022-05-06 11:30:57 +08001032 return -1;
you.chened802ab2023-02-13 10:50:35 +08001033 memset(ip, 0, ip_len);
qs.xiong9c1020c2023-02-21 19:12:54 +08001034 sprintf(cmd, "ip n s | grep \"lladdr\" | grep \"%s\" | head -1 | awk '{print $1}'", mac);
you.chened802ab2023-02-13 10:50:35 +08001035 ret = exec_cmd(cmd, ip, ip_len);
1036 p = strchr(ip, '\n');
1037 if (NULL != p)
1038 {
1039 *p = '\0';
you.chen87ff5172022-05-06 11:30:57 +08001040 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001041 RLOGD("inner_get_ip_by_mac %s\n", ip);
you.chen87ff5172022-05-06 11:30:57 +08001042 return ret;
1043}
1044
you.chened802ab2023-02-13 10:50:35 +08001045static int inner_get_hostname_by_ip(char *ip, char *hostname) {
you.chen87ff5172022-05-06 11:30:57 +08001046 struct in_addr addr ={0};
1047 struct hostent *ht;
1048
qs.xiongf1e48bb2023-03-28 13:38:22 +08001049 if (ip == NULL || *ip == '\0' || hostname == NULL)
1050 {
1051 RLOGE("ip == NULL or hostname == NULL");
1052 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001053 }
1054
you.chened802ab2023-02-13 10:50:35 +08001055 *hostname = '\0';
qs.xiongf1e48bb2023-03-28 13:38:22 +08001056 if (inet_aton(ip, &addr) == 0)
1057 {
you.chen87ff5172022-05-06 11:30:57 +08001058 printf("---inet_aton fail\n");
1059 return -1;
1060 }
1061
1062 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
1063
qs.xiongf1e48bb2023-03-28 13:38:22 +08001064 if (ht == NULL)
1065 {
1066 RLOGE("---gethostbyaddr fail\n");
you.chen87ff5172022-05-06 11:30:57 +08001067 herror(NULL);
1068 return -1;
1069 }
1070
1071 strcpy(hostname, ht->h_name);
1072
1073 return 0;
1074}
1075
1076static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
1077{
1078 int count, index, words_count;
1079 char * split_lines[128]= {0};
1080 char * split_words[128] = {0};
you.chened802ab2023-02-13 10:50:35 +08001081 char local_ssid[128] = {0};
you.chen87ff5172022-05-06 11:30:57 +08001082 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
qs.xiongf1e48bb2023-03-28 13:38:22 +08001083 RLOGD("[lynq_get_network_number_list] enter lynq_get_network_number_list api");
you.chen87ff5172022-05-06 11:30:57 +08001084
1085 CHECK_WPA_CTRL(ap_sta);
1086
1087 DO_REQUEST(lynq_wifi_list_networks);
1088
1089 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1090
1091 //@todo check ssid field to compatible
1092
1093 ret = 0;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001094 for(index=1; index < count; index++)
1095 {
you.chen87ff5172022-05-06 11:30:57 +08001096 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001097 if (words_count > 2)
1098 {
you.chened802ab2023-02-13 10:50:35 +08001099 inner_copy_ssid(local_ssid, split_words[1], sizeof (local_ssid));
qs.xiongf1e48bb2023-03-28 13:38:22 +08001100 if (ssid == NULL || strcmp(local_ssid, ssid) == 0)
1101 {
you.chen87ff5172022-05-06 11:30:57 +08001102 net_no_list[ret++] = atoi(split_words[0]);
1103 }
1104 }
1105 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001106 RLOGD("[lynq_get_network_number_list] lynq_get_network_number_list return ok");
you.chen87ff5172022-05-06 11:30:57 +08001107 return ret;
1108}
1109
1110static int lynq_add_network(int ap_sta) {
you.chenc7357f22022-05-16 17:55:28 +08001111 size_t i=0;
you.chen87ff5172022-05-06 11:30:57 +08001112 CHECK_WPA_CTRL(ap_sta);
1113 const char *lynq_wifi_add_network = "ADD_NETWORK";
1114
qs.xiongf1e48bb2023-03-28 13:38:22 +08001115 RLOGD("[lynq_add_network] enter lynq_add_network");
you.chen87ff5172022-05-06 11:30:57 +08001116 DO_REQUEST(lynq_wifi_add_network);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001117 if (memcmp(cmd_reply, "FAIL", 4) == 0)
1118 {
1119 RLOGE("[wifi error]lynq_add_network cmd_reply FAIL");
you.chen87ff5172022-05-06 11:30:57 +08001120 return -1;
1121 }
1122
qs.xiongf1e48bb2023-03-28 13:38:22 +08001123 for(i=0;i<reply_len;i++)
1124 {
1125 if(cmd_reply[i] == '\n')
1126 {
you.chen87ff5172022-05-06 11:30:57 +08001127 cmd_reply[i] = '\0';
1128 break;
1129 }
1130 }
1131 return atoi(cmd_reply);
1132}
you.chen5e363602022-05-08 12:20:18 +08001133
you.chen87ff5172022-05-06 11:30:57 +08001134static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
1135{
1136 int count, index;
1137 int net_no_list[128];
1138
qs.xiongf1e48bb2023-03-28 13:38:22 +08001139 RLOGD("[lynq_check_network_number] enter lynq_check_network_number api");
you.chen87ff5172022-05-06 11:30:57 +08001140 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001141 for (index=0; index < count; index++)
1142 {
1143 if (net_no_list[index] == net_no)
1144 {
you.chen87ff5172022-05-06 11:30:57 +08001145 return 0;
1146 }
1147 }
1148
1149 if (count >= 1)
1150 index = net_no_list[count - 1];
1151 else
1152 index = -1;
1153
qs.xiongf1e48bb2023-03-28 13:38:22 +08001154 while (index < net_no )
1155 {
you.chen87ff5172022-05-06 11:30:57 +08001156 index = lynq_add_network(ap_sta);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001157 if (index >= net_no)
1158 { // required network no created
1159 RLOGD("required network no created\n");;
you.chen87ff5172022-05-06 11:30:57 +08001160 return 0;
1161 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001162 else if( index < 0)
1163 {
1164 RLOGE("[lynq_check_network_number] add network fail");
you.chen5e363602022-05-08 12:20:18 +08001165 return -1;
1166 }
you.chen87ff5172022-05-06 11:30:57 +08001167 }
1168
1169 if (index < 0)
qs.xiongf1e48bb2023-03-28 13:38:22 +08001170 {
1171 RLOGE("[lynq_check_network_number] network index < 0");
1172 return -1;
1173 }
1174 RLOGD("[lynq_check_network_number] work finished &state is ok");
you.chen87ff5172022-05-06 11:30:57 +08001175 return 0;
1176}
1177
1178static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
qs.xiongf1e48bb2023-03-28 13:38:22 +08001179 if (freq > 5000 && freq < 6000)
1180 {
you.chen87ff5172022-05-06 11:30:57 +08001181 return LYNQ_WIFI_5G_band;
1182 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001183 else if (freq > 2000 && freq < 3000)
1184 {
you.chen87ff5172022-05-06 11:30:57 +08001185 return LYNQ_WIFI_2G_band;
1186 }
1187 return LYNQ_WIFI_2_and_5G_band;
1188}
1189
1190static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001191 if (key_mgmt != NULL)
1192 {
1193 if (memcmp( key_mgmt, "NONE", 4) == 0)
1194 {
you.chen87ff5172022-05-06 11:30:57 +08001195 return LYNQ_WIFI_AUTH_OPEN;
1196 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001197 else if (memcmp( key_mgmt, "WEP", 3) == 0)
1198 {
you.chen87ff5172022-05-06 11:30:57 +08001199 return LYNQ_WIFI_AUTH_WEP;
1200 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001201 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0)
1202 {
you.chen87ff5172022-05-06 11:30:57 +08001203 return LYNQ_WIFI_AUTH_WPA_PSK;
1204 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001205 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0)
1206 {
you.chen87ff5172022-05-06 11:30:57 +08001207 return LYNQ_WIFI_AUTH_WPA2_PSK;
1208 }
1209 }
1210
1211 return -1;
1212}
1213
1214static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001215 if (flag != NULL)
1216 {
qs.xionge2dc2e02023-04-06 11:08:48 +08001217 if ( strstr(flag, "SHA256") != NULL || strstr(flag,"WPA2-SAE") != NULL || strstr(flag,"SAE-H2E") != NULL)
1218 {
1219 return LYNQ_WIFI_AUTH_WPA3_PSK;
1220 }else if ( strstr( flag,"SAE-CCMP") != NULL )
1221 {
1222 return LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
1223 }else if (strstr( flag, "WPA2-PSK") != NULL)
1224 {
you.chen87ff5172022-05-06 11:30:57 +08001225 return LYNQ_WIFI_AUTH_WPA2_PSK;
1226 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001227 else if (strstr( flag, "WPA-PSK") != NULL)
qs.xionge2dc2e02023-04-06 11:08:48 +08001228 {
you.chen87ff5172022-05-06 11:30:57 +08001229 return LYNQ_WIFI_AUTH_WPA_PSK;
1230 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001231 else if (strstr( flag, "WEP") != NULL)
qs.xionge2dc2e02023-04-06 11:08:48 +08001232 {
you.chen87ff5172022-05-06 11:30:57 +08001233 return LYNQ_WIFI_AUTH_WEP;
1234 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001235 else if (strstr( flag, "NONE") != NULL)
qs.xionge2dc2e02023-04-06 11:08:48 +08001236 {
you.chen87ff5172022-05-06 11:30:57 +08001237 return LYNQ_WIFI_AUTH_OPEN;
1238 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001239 else if (strcmp( flag, "[ESS]") == 0 || strcmp( flag,"[WPS][ESS]") == 0)
qs.xionge2dc2e02023-04-06 11:08:48 +08001240 {
you.chened802ab2023-02-13 10:50:35 +08001241 return LYNQ_WIFI_AUTH_OPEN;
1242 }
you.chen87ff5172022-05-06 11:30:57 +08001243 }
1244
1245 return -1;
1246}
1247
1248static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
1249 switch (bw) {
1250 case 10:
1251 return LYNQ_WIFI_BANDWIDTH_HT10;
1252 break;
1253 case 20:
1254 return LYNQ_WIFI_BANDWIDTH_HT20;
1255 break;
1256 case 40:
1257 return LYNQ_WIFI_BANDWIDTH_HT40;
1258 break;
1259 case 80:
1260 return LYNQ_WIFI_BANDWIDTH_HT80;
1261 break;
1262 default:
1263 break;
1264 }
1265
1266 return -1;
1267}
1268
you.chenf9d718d2023-04-14 18:17:09 +08001269static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth);
you.chen87ff5172022-05-06 11:30:57 +08001270static int inner_get_status_info(int interface, curr_status_info *curr_state) {
1271 int i, count;
1272 char *p;
1273 const char *lynq_status_cmd = "STATUS";
1274 const char * FLAG_SSID = "ssid=";
1275 const char * FLAG_SBSID = "bssid=";
1276 const char * FLAG_KEY_MGMT = "key_mgmt=";
1277 const char * FLAG_FREQ = "freq=";
1278 const char * FLAG_STATE = "wpa_state=";
1279 const char * FLAG_ID = "id=";
you.chened802ab2023-02-13 10:50:35 +08001280 const char * FLAG_IPADDR = "ip_address=";
you.chen87ff5172022-05-06 11:30:57 +08001281 char *split_lines[128] = {0};
1282
1283 CHECK_WPA_CTRL(interface);
1284
qs.xiongf1e48bb2023-03-28 13:38:22 +08001285 if (curr_state == NULL)
1286 {
1287 RLOGE("[wifi error][inner_get_status_info]curr_state is NULL");
you.chen87ff5172022-05-06 11:30:57 +08001288 return -1;
1289 }
1290
1291 DO_REQUEST(lynq_status_cmd);
1292
1293 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1294
1295 curr_state->net_no = -1;
1296 ret = -1;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001297 for(i=0; i < count; i++)
1298 {
1299 if (curr_state->ap != NULL)
you.chenf9d718d2023-04-14 18:17:09 +08001300 {
you.chen87ff5172022-05-06 11:30:57 +08001301 p = strstr(split_lines[i], FLAG_SBSID);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001302 if (p != NULL)
you.chenf9d718d2023-04-14 18:17:09 +08001303 {
you.chened802ab2023-02-13 10:50:35 +08001304 strncpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID), sizeof(curr_state->ap->ap_mac));
you.chen87ff5172022-05-06 11:30:57 +08001305 ret = 0;
1306 continue;
1307 }
you.chendad3f9f2022-06-21 16:53:48 +08001308 p = strstr(split_lines[i], FLAG_SSID);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001309 if (p != NULL)
you.chenf9d718d2023-04-14 18:17:09 +08001310 {
you.chened802ab2023-02-13 10:50:35 +08001311 inner_copy_ssid(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID), sizeof (curr_state->ap->ap_ssid));
you.chendad3f9f2022-06-21 16:53:48 +08001312 ret = 0;
1313 continue;
1314 }
you.chen87ff5172022-05-06 11:30:57 +08001315 p = strstr(split_lines[i], FLAG_KEY_MGMT);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001316 if (p != NULL)
you.chenf9d718d2023-04-14 18:17:09 +08001317 {
you.chenb98d1cf2022-07-15 17:56:48 +08001318 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
qs.xiongf1e48bb2023-03-28 13:38:22 +08001319 RLOGD("inner_get_status_info: key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen87ff5172022-05-06 11:30:57 +08001320 ret = 0;
1321 continue;
1322 }
1323 p = strstr(split_lines[i], FLAG_FREQ);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001324 if (p != NULL)
you.chenf9d718d2023-04-14 18:17:09 +08001325 {
you.chen87ff5172022-05-06 11:30:57 +08001326 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
1327 ret = 0;
1328 continue;
1329 }
you.chened802ab2023-02-13 10:50:35 +08001330 p = strstr(split_lines[i], FLAG_IPADDR);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001331 if (p != NULL)
you.chenf9d718d2023-04-14 18:17:09 +08001332 {
you.chened802ab2023-02-13 10:50:35 +08001333 strncpy(curr_state->ap->ap_ip, p + strlen(FLAG_IPADDR), sizeof(curr_state->ap->ap_ip));
1334 ret = 0;
1335 continue;
1336 }
you.chen87ff5172022-05-06 11:30:57 +08001337 } // end if (ap != NULL)
qs.xiongf1e48bb2023-03-28 13:38:22 +08001338 if (curr_state->state != NULL)
you.chenf9d718d2023-04-14 18:17:09 +08001339 {
you.chen87ff5172022-05-06 11:30:57 +08001340 p = strstr(split_lines[i], FLAG_STATE);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001341 if (p != NULL)
you.chenf9d718d2023-04-14 18:17:09 +08001342 {
you.chen87ff5172022-05-06 11:30:57 +08001343 strcpy(curr_state->state, p + strlen(FLAG_STATE));
1344 ret = 0;
1345 continue;
1346 }
1347
1348 } //end else if (state != NULL)
qs.xiongf1e48bb2023-03-28 13:38:22 +08001349 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i])
you.chenf9d718d2023-04-14 18:17:09 +08001350 {
you.chen87ff5172022-05-06 11:30:57 +08001351 ret = 0;
you.chenb98d1cf2022-07-15 17:56:48 +08001352 curr_state->net_no = atoi(p + strlen(FLAG_ID));
qs.xiongf1e48bb2023-03-28 13:38:22 +08001353 RLOGD("inner_get_status_info:net_no %d, -- %s\n", curr_state->net_no, p);
you.chen87ff5172022-05-06 11:30:57 +08001354 }
1355 }
1356
you.chenf9d718d2023-04-14 18:17:09 +08001357 if (ret == 0 && curr_state->ap != NULL && curr_state->net_no >= 0) // auth may not right when add wpa3
1358 {
1359 inner_get_network_auth(interface, curr_state->net_no, &curr_state->ap->auth);
1360 }
1361
you.chen87ff5172022-05-06 11:30:57 +08001362 return ret;
1363}
1364
qs.xiongd189c542022-03-31 00:58:23 -04001365int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -05001366{
qs.xiongf1e48bb2023-03-28 13:38:22 +08001367 RLOGD("enter lynq_wifi_ap_ssid_set");
you.chen87ff5172022-05-06 11:30:57 +08001368 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong8d42bb92022-03-02 09:43:11 -05001369
qs.xiongf1e48bb2023-03-28 13:38:22 +08001370 if (ap_ssid == NULL)
1371 {
1372 RLOGE("Input ap_ssid is NULL");
you.chen87ff5172022-05-06 11:30:57 +08001373 return -1;
1374 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001375 else
1376 {
1377 RLOGD("[lynq_wifi_ap_ssid_set]idx:%d ap_ssid : %s\n", idx, ap_ssid);
you.chen87ff5172022-05-06 11:30:57 +08001378 }
qs.xiong799dab02022-03-14 09:12:12 -04001379
qs.xiongf1e48bb2023-03-28 13:38:22 +08001380 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1381 {
1382 RLOGE("Do check ap network_number fail");
you.chen87ff5172022-05-06 11:30:57 +08001383 return -1;
1384 }
qs.xiong799dab02022-03-14 09:12:12 -04001385
you.chen87ff5172022-05-06 11:30:57 +08001386 CHECK_IDX(idx, CTRL_AP);
1387
1388 CHECK_WPA_CTRL(CTRL_AP);
1389
1390 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
1391
1392 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1393 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001394 RLOGD("[lynq_wifi_ap_ssid_set] set ssid sucss");
1395 return 0;
you.chen87ff5172022-05-06 11:30:57 +08001396
qs.xiong8d42bb92022-03-02 09:43:11 -05001397}
1398
you.chen87ff5172022-05-06 11:30:57 +08001399int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -05001400{
qs.xiongf1e48bb2023-03-28 13:38:22 +08001401 RLOGD("enter lynq_wifi_ap_ssid_get");
you.chen87ff5172022-05-06 11:30:57 +08001402 CHECK_IDX(idx, CTRL_AP);
you.chened802ab2023-02-13 10:50:35 +08001403 return inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid);
qs.xiong8d42bb92022-03-02 09:43:11 -05001404}
1405
qs.xiongaed02ac2022-10-17 15:27:18 +08001406/*****
1407 *frequency <------>channel
1408 *
1409 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
1410 *
1411 *
1412 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
1413 *
1414 *
1415 * */
1416static int lynq_check_set_frequency(int input_frequency){
qs.xiongf962eef2022-11-29 16:28:03 +08001417 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};
1418 int i;
1419 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
1420
qs.xiong3163d0f2022-12-02 09:58:57 +08001421 for(i = 0; i < arr_len; i++)
qs.xiongf962eef2022-11-29 16:28:03 +08001422 {
1423 if(input_frequency == legitimate_frequency[i])
qs.xiongaed02ac2022-10-17 15:27:18 +08001424 break;
qs.xiongaed02ac2022-10-17 15:27:18 +08001425 }
qs.xiongf962eef2022-11-29 16:28:03 +08001426
1427 if(i == arr_len)
1428 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001429 RLOGE("[lynq_check_set_frequency]input frequency is --->%d,please check it\n", input_frequency);
qs.xiongaed02ac2022-10-17 15:27:18 +08001430 return -1;
1431 }
qs.xiongf962eef2022-11-29 16:28:03 +08001432
qs.xiongaed02ac2022-10-17 15:27:18 +08001433 return 0;
1434}
qs.xiong9c1020c2023-02-21 19:12:54 +08001435
1436static int lynq_check_frequencyby_country_code(int input_frequency)
1437{
1438 char str_cnc[]="CN";
1439 char str_dest[20]="";
1440
1441 if( lynq_get_country_code(1,str_dest) != 0 )
1442 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001443 RLOGE("get country_code error\n");
qs.xiong9c1020c2023-02-21 19:12:54 +08001444 return -1;
1445 }
1446 if( strncmp(str_dest,str_cnc,2) != 0 )
1447 {
1448 return 0;
1449 }else if( 2473 < input_frequency && input_frequency < 5744)
1450 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001451 RLOGE("input frequency is bad\n");
qs.xiong9c1020c2023-02-21 19:12:54 +08001452 return -1;
1453 }
1454 return 0;
1455}
qs.xiongd189c542022-03-31 00:58:23 -04001456int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong8d42bb92022-03-02 09:43:11 -05001457{
qs.xiongf962eef2022-11-29 16:28:03 +08001458 int check;
qs.xiongaed02ac2022-10-17 15:27:18 +08001459 char lynq_wifi_frequency_cmd[128]={0};
1460 char lynq_cmd_mode[128]={0};
you.chen87ff5172022-05-06 11:30:57 +08001461 char lynq_cmd_slect[128]={0};
qs.xiongf1e48bb2023-03-28 13:38:22 +08001462 RLOGD("enter lynq_wifi_ap_frequency_set and input frequency is:%d", lynq_wifi_frequency);
qs.xiongaed02ac2022-10-17 15:27:18 +08001463 //@do check input frequency
qs.xiongf962eef2022-11-29 16:28:03 +08001464 check = lynq_check_set_frequency(lynq_wifi_frequency);
1465 if(check != 0)
1466 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001467 RLOGE("do check frequency error");
qs.xiongaed02ac2022-10-17 15:27:18 +08001468 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001469 }
qs.xiong9c1020c2023-02-21 19:12:54 +08001470 check = lynq_check_frequencyby_country_code(lynq_wifi_frequency);
1471 if(check != 0)
1472 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001473 RLOGE("do check frequency error");
qs.xiong9c1020c2023-02-21 19:12:54 +08001474 return -1;
1475 }
1476
qs.xiongf962eef2022-11-29 16:28:03 +08001477 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1478 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001479 RLOGE("[set ap frequecny][lynq_check_network_number] error");
you.chen87ff5172022-05-06 11:30:57 +08001480 return -1;
1481 }
qs.xiong799dab02022-03-14 09:12:12 -04001482
you.chen87ff5172022-05-06 11:30:57 +08001483 CHECK_IDX(idx, CTRL_AP);
1484
1485 CHECK_WPA_CTRL(CTRL_AP);
1486
1487 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
1488 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
1489 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
1490
you.chenc7357f22022-05-16 17:55:28 +08001491 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen87ff5172022-05-06 11:30:57 +08001492 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
1493 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
1494 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong8d42bb92022-03-02 09:43:11 -05001495
qs.xiongf1e48bb2023-03-28 13:38:22 +08001496 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001497}
1498
qs.xiongd189c542022-03-31 00:58:23 -04001499int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong8d42bb92022-03-02 09:43:11 -05001500{
you.chen87ff5172022-05-06 11:30:57 +08001501 char lynq_frequency_str[MAX_RET] = {0};
qs.xiongf1e48bb2023-03-28 13:38:22 +08001502 RLOGD("enter lynq_wifi_ap_frequency_get and input idx is %d",idx);
you.chen87ff5172022-05-06 11:30:57 +08001503 CHECK_IDX(idx, CTRL_AP);
qs.xiongd189c542022-03-31 00:58:23 -04001504
qs.xiongf1e48bb2023-03-28 13:38:22 +08001505 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0)
1506 {
1507 RLOGE("[wifi error][lynq_wifi_ap_frequency_get]get frequency from device fail");
you.chen87ff5172022-05-06 11:30:57 +08001508 return -1;
1509 }
1510 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongd189c542022-03-31 00:58:23 -04001511
qs.xiongf1e48bb2023-03-28 13:38:22 +08001512 return 0;
qs.xiongd189c542022-03-31 00:58:23 -04001513}
1514
qs.xiongd189c542022-03-31 00:58:23 -04001515int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
1516{
qs.xiongf1e48bb2023-03-28 13:38:22 +08001517 RLOGD("enter lynq_wifi_ap_bandwidth_set");
you.chen87ff5172022-05-06 11:30:57 +08001518 CHECK_IDX(idx, CTRL_AP);
1519 switch(bandwidth){
qs.xiongf1e48bb2023-03-28 13:38:22 +08001520 case LYNQ_WIFI_BANDWIDTH_HT10:
1521 {
1522 RLOGE("bandwith [%d] not support now\n", bandwidth);
1523 return -1;
1524 }
1525 case LYNQ_WIFI_BANDWIDTH_HT20:
you.chen87ff5172022-05-06 11:30:57 +08001526 {
1527 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
1528 system("wl down");
qs.xiongf1e48bb2023-03-28 13:38:22 +08001529 if (system(lynq_cmd_bandwith) != 0 )
1530 {
1531 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen87ff5172022-05-06 11:30:57 +08001532 return -1;
1533 }
1534 system("wl up");
1535 break;
1536 }
1537 case LYNQ_WIFI_BANDWIDTH_HT40:
qs.xiongf1e48bb2023-03-28 13:38:22 +08001538 {
qs.xiongdb04cdb2023-02-21 13:19:42 +08001539 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/40";
you.chen87ff5172022-05-06 11:30:57 +08001540 sprintf(lynq_cmd_bandwith, "wl chanspec ");
1541 system("wl down");
qs.xiongf1e48bb2023-03-28 13:38:22 +08001542 if (system(lynq_cmd_bandwith) != 0 )
1543 {
1544 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen87ff5172022-05-06 11:30:57 +08001545 return -1;
1546 }
1547 system("wl up");
1548 break;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001549 }
you.chen87ff5172022-05-06 11:30:57 +08001550 case LYNQ_WIFI_BANDWIDTH_HT80:
qs.xiongf1e48bb2023-03-28 13:38:22 +08001551 {
qs.xiongdb04cdb2023-02-21 13:19:42 +08001552 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 149/80";
you.chen87ff5172022-05-06 11:30:57 +08001553 system("wl down");
qs.xiongf1e48bb2023-03-28 13:38:22 +08001554 if (system(lynq_cmd_bandwith) != 0 )
1555 {
1556 RLOGE("lynq_wifi_ap_bandwidth_set erro");
you.chen87ff5172022-05-06 11:30:57 +08001557 return -1;
1558 }
1559 system("wl up");
1560 break;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001561 }
1562 default:
you.chen87ff5172022-05-06 11:30:57 +08001563 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001564 RLOGE("auth type [%d] not support now\n", bandwidth);
1565 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001566 }
1567 }
qs.xiongd189c542022-03-31 00:58:23 -04001568
1569
you.chen87ff5172022-05-06 11:30:57 +08001570 return 0;
qs.xiongd189c542022-03-31 00:58:23 -04001571}
you.chen87ff5172022-05-06 11:30:57 +08001572
qs.xiongd189c542022-03-31 00:58:23 -04001573int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
1574{
you.chen87ff5172022-05-06 11:30:57 +08001575 int count = 0;
1576 int index = 0;
1577 char *split_words[128] = {0};
1578 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1e48bb2023-03-28 13:38:22 +08001579 RLOGD("enter lynq_wifi_ap_bandwidth_get");
you.chen87ff5172022-05-06 11:30:57 +08001580 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -05001581
you.chen87ff5172022-05-06 11:30:57 +08001582 CHECK_WPA_CTRL(CTRL_AP);
1583
1584 DO_REQUEST(lynq_chanspec_cmd);
1585
1586 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1587 for(;index < count; index++) {
1588 if (strncmp(split_words[index], "bw", 2) != 0) {
1589 continue;
1590 }
1591
1592 index++;
1593 if (index >= count) {
1594 return -1;
1595 }
1596
qs.xiongf1e48bb2023-03-28 13:38:22 +08001597 RLOGD("bw %s\n", split_words[index]);
you.chen87ff5172022-05-06 11:30:57 +08001598 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
1599 return 0;
1600 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001601 RLOGE("[wifi error]lynq_wifi_ap_bandwidth_get");
you.chen87ff5172022-05-06 11:30:57 +08001602 return -1;
qs.xiong8d42bb92022-03-02 09:43:11 -05001603}
qs.xiong7cc23cb2022-04-14 03:50:45 -04001604
qs.xiongd189c542022-03-31 00:58:23 -04001605int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong8d42bb92022-03-02 09:43:11 -05001606{
you.chen87ff5172022-05-06 11:30:57 +08001607 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiongf1e48bb2023-03-28 13:38:22 +08001608 RLOGD("enter lynq_wifi_ap_channel_set and input channel is %d",channel);
you.chen87ff5172022-05-06 11:30:57 +08001609 CHECK_IDX(idx, CTRL_AP);
qs.xiong6a886062022-04-14 06:17:01 -04001610
you.chen87ff5172022-05-06 11:30:57 +08001611 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong799dab02022-03-14 09:12:12 -04001612
qs.xiongf1e48bb2023-03-28 13:38:22 +08001613 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
1614 {
you.chen87ff5172022-05-06 11:30:57 +08001615 return -1;
1616 }
1617
1618 system("wl down");
1619 if (system(lynq_cmd_channel) != 0 ){
qs.xiongf1e48bb2023-03-28 13:38:22 +08001620 RLOGE("lynq_wifi_ap_channel_set erro");
you.chen87ff5172022-05-06 11:30:57 +08001621 return -1;
1622 }
1623 system("wl up");
1624 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001625}
qs.xiong7cc23cb2022-04-14 03:50:45 -04001626
qs.xiongd189c542022-03-31 00:58:23 -04001627int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong8d42bb92022-03-02 09:43:11 -05001628{
you.chen87ff5172022-05-06 11:30:57 +08001629 int count = 0;
1630 int index = 0;
1631 char *split_words[128] = {0};
1632 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1e48bb2023-03-28 13:38:22 +08001633 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen87ff5172022-05-06 11:30:57 +08001634 CHECK_IDX(idx, CTRL_AP);
qs.xiong799dab02022-03-14 09:12:12 -04001635
you.chen87ff5172022-05-06 11:30:57 +08001636 CHECK_WPA_CTRL(CTRL_AP);
1637
1638 DO_REQUEST(lynq_chanspec_cmd);
1639
1640 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001641 for(;index < count; index++)
1642 {
1643 RLOGD("[lynq_wifi_ap_channel_get]---- %s\n",split_words[index]);
you.chen87ff5172022-05-06 11:30:57 +08001644 if (strncmp(split_words[index], "channel", 2) != 0) {
1645 continue;
1646 }
1647
1648 index++;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001649 if (index >= count)
1650 {
you.chen87ff5172022-05-06 11:30:57 +08001651 return -1;
1652 }
1653
1654 *channel = atoi(split_words[index]);
1655 return 0;
1656 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001657 RLOGE("[lynq_wifi_ap_channel_get] function fail");
you.chen87ff5172022-05-06 11:30:57 +08001658 return -1;
qs.xiong8d42bb92022-03-02 09:43:11 -05001659}
1660
1661
you.chen87ff5172022-05-06 11:30:57 +08001662int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong8d42bb92022-03-02 09:43:11 -05001663{
you.chenc7357f22022-05-16 17:55:28 +08001664 char ssid[MAX_CMD] = {0};
1665 int freq = 0;
1666 char lynq_auth_cmd[64]={0};
1667 char lynq_auth_alg_cmd[64]={0};
1668 char lynq_psk_cmd[64]={0};
1669 char lynq_pairwise_cmd[64]={0};
qs.xiongf1e48bb2023-03-28 13:38:22 +08001670 char lynq_ieee80211_cmd[64]={0};
1671 RLOGD("enter lynq_wifi_ap_auth_set and input idx is:%d,auth is:%d",idx,auth);
you.chenc7357f22022-05-16 17:55:28 +08001672 lynq_wifi_auth_s org_auth;
you.chen87ff5172022-05-06 11:30:57 +08001673 CHECK_IDX(idx, CTRL_AP);
1674
you.chenc7357f22022-05-16 17:55:28 +08001675 CHECK_WPA_CTRL(CTRL_AP);
1676
qs.xiongf1e48bb2023-03-28 13:38:22 +08001677 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0)
1678 {
1679 RLOGE("[wifi error][lynq_wifi_ap_auth_set] check network fail\n");
you.chen87ff5172022-05-06 11:30:57 +08001680 return -1;
1681 }
1682
you.chen01276462022-05-25 10:09:47 +08001683 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chenc7357f22022-05-16 17:55:28 +08001684 if (org_auth == auth) {
qs.xiong045f60b2023-03-29 17:36:14 +08001685 RLOGD("org_auth --- is %d\n",org_auth);
you.chenc7357f22022-05-16 17:55:28 +08001686 return 0;
1687 }
1688 else {
1689 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1690 ssid[0] = '\0';
1691 }
1692 lynq_wifi_ap_frequency_get(idx, &freq);
1693
1694 DO_OK_FAIL_REQUEST(cmd_disconnect);
1695 DO_OK_FAIL_REQUEST(cmd_remove_all);
1696 if (ssid[0] != '\0') {
1697 lynq_wifi_ap_ssid_set(idx, ssid);
1698 }
1699 if (freq != 0) {
1700 lynq_wifi_ap_frequency_set(idx, freq);
1701 }
1702 }
1703 }
you.chen87ff5172022-05-06 11:30:57 +08001704
qs.xiongf1e48bb2023-03-28 13:38:22 +08001705 switch(auth){
1706 case LYNQ_WIFI_AUTH_OPEN:
you.chenc7357f22022-05-16 17:55:28 +08001707 {
qs.xiong045f60b2023-03-29 17:36:14 +08001708 RLOGD("auth == is LYNQ_WIFI_AUTH_OPEN\n");
you.chen87ff5172022-05-06 11:30:57 +08001709 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen01276462022-05-25 10:09:47 +08001710 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen87ff5172022-05-06 11:30:57 +08001711 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001712 break;
1713 }
you.chenc7357f22022-05-16 17:55:28 +08001714 case LYNQ_WIFI_AUTH_WEP:
1715 {
qs.xiong045f60b2023-03-29 17:36:14 +08001716 RLOGD("auth == is LYNQ_WIFI_AUTH_WEP\n");
you.chenc7357f22022-05-16 17:55:28 +08001717 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen01276462022-05-25 10:09:47 +08001718 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chenc7357f22022-05-16 17:55:28 +08001719 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1720
1721 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1722 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1723 break;
1724 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001725 case LYNQ_WIFI_AUTH_WPA_PSK:
qs.xiong045f60b2023-03-29 17:36:14 +08001726 {
1727 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1728 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1729 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
1730
1731 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1732 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1733 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
1734 break;
1735
1736 }
you.chen87ff5172022-05-06 11:30:57 +08001737 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1e48bb2023-03-28 13:38:22 +08001738 {
1739 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
1740 {
you.chen87ff5172022-05-06 11:30:57 +08001741 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1742 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1743 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001744 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
1745 {
you.chenc7357f22022-05-16 17:55:28 +08001746 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chen5e363602022-05-08 12:20:18 +08001747 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen87ff5172022-05-06 11:30:57 +08001748 }
1749// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1750// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1751 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong8d42bb92022-03-02 09:43:11 -05001752
you.chen87ff5172022-05-06 11:30:57 +08001753 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1754 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1755 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001756 break;
1757 }
1758 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
1759 {
1760 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1761 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 1", AP_NETWORK_0);
1762 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK SAE", AP_NETWORK_0);
1763 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
1764
1765 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1766 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
1767 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1768 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
1769 break;
1770 }
1771 case LYNQ_WIFI_AUTH_WPA3_PSK:
1772 {
1773 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1774 sprintf(lynq_ieee80211_cmd,"SET_NETWORK %d ieee80211w 2", AP_NETWORK_0);
qs.xionge2dc2e02023-04-06 11:08:48 +08001775 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt SAE", AP_NETWORK_0);
qs.xiongf1e48bb2023-03-28 13:38:22 +08001776 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
1777
1778 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1779 DO_OK_FAIL_REQUEST(lynq_ieee80211_cmd);
1780 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1781 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
1782 break;
1783 }
1784 default:
you.chen87ff5172022-05-06 11:30:57 +08001785 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08001786 RLOGE("auth type [%d] not support now\n", auth);
1787 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001788 }
1789 }
you.chenc7357f22022-05-16 17:55:28 +08001790 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong8d42bb92022-03-02 09:43:11 -05001791
qs.xiongf1e48bb2023-03-28 13:38:22 +08001792 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001793}
1794
you.chen87ff5172022-05-06 11:30:57 +08001795int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong8d42bb92022-03-02 09:43:11 -05001796{
you.chen87ff5172022-05-06 11:30:57 +08001797 char lynq_auth_str[MAX_RET] = {0};
you.chenc7357f22022-05-16 17:55:28 +08001798 char lynq_auth_alg_str[MAX_RET] = {0};
1799 char lynq_proto_str[MAX_RET] = {0};
qs.xiongf1e48bb2023-03-28 13:38:22 +08001800 RLOGD("enter lynq_wifi_ap_auth_get");
you.chen87ff5172022-05-06 11:30:57 +08001801 CHECK_IDX(idx, CTRL_AP);
1802
qs.xiongf1e48bb2023-03-28 13:38:22 +08001803 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0)
1804 {
1805 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network fail");
you.chen87ff5172022-05-06 11:30:57 +08001806 return -1;
1807 }
1808
qs.xiongf1e48bb2023-03-28 13:38:22 +08001809 if (memcmp( lynq_auth_str, "NONE", 4) == 0)
1810 {
1811 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0)
1812 {
qs.xiong045f60b2023-03-29 17:36:14 +08001813 RLOGD("---auth is OPEN\n");
you.chenc7357f22022-05-16 17:55:28 +08001814 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiong045f60b2023-03-29 17:36:14 +08001815 return 0;
you.chenc7357f22022-05-16 17:55:28 +08001816 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001817 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0)
1818 {
qs.xiong045f60b2023-03-29 17:36:14 +08001819 RLOGD("---auth is WEP\n");
you.chenc7357f22022-05-16 17:55:28 +08001820 *auth = LYNQ_WIFI_AUTH_WEP;
qs.xiong045f60b2023-03-29 17:36:14 +08001821 return 0;
you.chenc7357f22022-05-16 17:55:28 +08001822 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001823 else
1824 {
qs.xiong045f60b2023-03-29 17:36:14 +08001825 RLOGD("---auth is OPEN\n");
you.chenc7357f22022-05-16 17:55:28 +08001826 *auth = LYNQ_WIFI_AUTH_OPEN;
qs.xiong045f60b2023-03-29 17:36:14 +08001827 return 0;
you.chenc7357f22022-05-16 17:55:28 +08001828 }
you.chen87ff5172022-05-06 11:30:57 +08001829 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001830 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 )
1831 {
1832 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0)
1833 {
qs.xiong045f60b2023-03-29 17:36:14 +08001834 RLOGE("---auth is -1\n");
you.chen01276462022-05-25 10:09:47 +08001835 *auth = -1;
you.chenc7357f22022-05-16 17:55:28 +08001836 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001837 else if (memcmp(lynq_proto_str, "RSN", 3) == 0)
1838 {
qs.xiong045f60b2023-03-29 17:36:14 +08001839 RLOGD("---auth WPA2_PSK\n");
you.chenc7357f22022-05-16 17:55:28 +08001840 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiong045f60b2023-03-29 17:36:14 +08001841 return 0;
you.chenc7357f22022-05-16 17:55:28 +08001842 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001843 else
1844 {
qs.xiong045f60b2023-03-29 17:36:14 +08001845 RLOGD("---auth WPA_PSK\n");
you.chenc7357f22022-05-16 17:55:28 +08001846 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
qs.xiong045f60b2023-03-29 17:36:14 +08001847 return 0;
you.chenc7357f22022-05-16 17:55:28 +08001848 }
you.chen87ff5172022-05-06 11:30:57 +08001849 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08001850
1851 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "ieee80211w", lynq_auth_str) != 0)
1852 {
1853 RLOGE("[wifi error][lynq_wifi_ap_auth_get] check network auth ieee80211w fail");
1854 return -1;
1855 }
1856
1857 if (memcmp(lynq_auth_str,"1",1) == 0 )
1858 {
1859 RLOGD("auth : LYNQ_WIFI_AUTH_WPA2_WPA3_PSK\n");
1860 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
qs.xiong045f60b2023-03-29 17:36:14 +08001861 return 0;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001862 }else if (memcmp(lynq_auth_str,"2",1) == 0 )
1863 {
1864 RLOGD("auth : LYNQ_WIFI_AUTH_WPA3_PSK\n");
1865 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong045f60b2023-03-29 17:36:14 +08001866 return 0;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001867 }
1868 else
1869 {
qs.xiong045f60b2023-03-29 17:36:14 +08001870 RLOGE("---auth -- -1\n");
you.chen01276462022-05-25 10:09:47 +08001871 *auth = -1;
1872 }
qs.xiong8d42bb92022-03-02 09:43:11 -05001873
you.chenc7357f22022-05-16 17:55:28 +08001874 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001875}
qs.xiong799dab02022-03-14 09:12:12 -04001876
qs.xiong799dab02022-03-14 09:12:12 -04001877
qs.xiongd189c542022-03-31 00:58:23 -04001878int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001879{
you.chen87ff5172022-05-06 11:30:57 +08001880 char LYNQ_WIFI_CMD[128]={0};
1881 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1882 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1e48bb2023-03-28 13:38:22 +08001883 RLOGD("enter lynq_wifi_ap_channel_get");
you.chen87ff5172022-05-06 11:30:57 +08001884 CHECK_IDX(idx, CTRL_AP);
1885
1886 CHECK_WPA_CTRL(CTRL_AP);
1887
you.chencd882682023-04-24 15:39:37 +08001888 sprintf(LYNQ_WIFI_CMD, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
1889 if (0 != exec_cmd(LYNQ_WIFI_CMD, s_ap_iterface_name, sizeof(s_ap_iterface_name)) || s_ap_iterface_name[0] == '\0')
1890 {
1891 return -1;
1892 }
you.chen87ff5172022-05-06 11:30:57 +08001893
1894 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1895 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1896
1897 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1898 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1899
you.chenf9d718d2023-04-14 18:17:09 +08001900 check_tether_and_notify();
1901
you.chencd882682023-04-24 15:39:37 +08001902 ret = system_call_v("%s %s", start_stop_ap_script, "start");
1903 if (ret != 0)
1904 {
1905 RLOGE("lynq_wifi_ap_start excute script fail");
1906 return -1;
1907 }
1908
qs.xiongf1e48bb2023-03-28 13:38:22 +08001909 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001910}
1911
qs.xiongd189c542022-03-31 00:58:23 -04001912int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001913{
you.chen87ff5172022-05-06 11:30:57 +08001914 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong8d42bb92022-03-02 09:43:11 -05001915}
1916
qs.xiongd189c542022-03-31 00:58:23 -04001917int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001918{
you.chen87ff5172022-05-06 11:30:57 +08001919 char LYNQ_WIFI_CMD[128]={0};
qs.xiong799dab02022-03-14 09:12:12 -04001920
you.chen87ff5172022-05-06 11:30:57 +08001921 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001922
you.chen87ff5172022-05-06 11:30:57 +08001923 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001924
you.chen87ff5172022-05-06 11:30:57 +08001925 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1926
1927 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1928
you.chenefc7f3f2022-05-06 17:50:16 +08001929// system("connmanctl tether wifi off");
you.chencd882682023-04-24 15:39:37 +08001930
1931 ret = system_call_v("%s %s", start_stop_ap_script, "stop");
1932 if (ret != 0)
1933 {
1934 RLOGE("lynq_wifi_ap_start excute script fail");
1935 return -1;
1936 }
1937
qs.xiongf1e48bb2023-03-28 13:38:22 +08001938 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001939}
qs.xiong799dab02022-03-14 09:12:12 -04001940
qs.xiongd189c542022-03-31 00:58:23 -04001941int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001942{
you.chen87ff5172022-05-06 11:30:57 +08001943 char lynq_disable_cmd[128] = {0};
1944 char lynq_select_cmd[128] = {0};
1945 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiongf1e48bb2023-03-28 13:38:22 +08001946 RLOGD("enter lynq_wifi_ap_hide_ssid");
you.chen87ff5172022-05-06 11:30:57 +08001947 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -05001948
you.chen87ff5172022-05-06 11:30:57 +08001949 CHECK_WPA_CTRL(CTRL_AP);
you.chen87ff5172022-05-06 11:30:57 +08001950 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1951 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1952
1953 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1954 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1955 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong799dab02022-03-14 09:12:12 -04001956
qs.xiongf1e48bb2023-03-28 13:38:22 +08001957 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001958}
1959
qs.xiongd189c542022-03-31 00:58:23 -04001960int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong8d42bb92022-03-02 09:43:11 -05001961{
you.chen87ff5172022-05-06 11:30:57 +08001962 char lynq_disable_cmd[128] = {0};
1963 char lynq_select_cmd[128] = {0};
1964 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1e48bb2023-03-28 13:38:22 +08001965 RLOGD("enter lynq_wifi_ap_unhide_ssid");
you.chen87ff5172022-05-06 11:30:57 +08001966 CHECK_IDX(idx, CTRL_AP);
qs.xiong8d42bb92022-03-02 09:43:11 -05001967
you.chen87ff5172022-05-06 11:30:57 +08001968 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04001969
you.chen87ff5172022-05-06 11:30:57 +08001970 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1971 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1972
1973 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1974 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1975 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong8d42bb92022-03-02 09:43:11 -05001976
qs.xiongf1e48bb2023-03-28 13:38:22 +08001977 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05001978}
qs.xiongd189c542022-03-31 00:58:23 -04001979
you.chen87ff5172022-05-06 11:30:57 +08001980int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong8d42bb92022-03-02 09:43:11 -05001981{
qs.xiongf1e48bb2023-03-28 13:38:22 +08001982 int pass_len;
you.chenc7357f22022-05-16 17:55:28 +08001983 char lynq_tmp_cmd[MAX_CMD] = {0};
qs.xiongf1e48bb2023-03-28 13:38:22 +08001984 char lynq_wpa2_wpa3[64] = {0};
you.chenc7357f22022-05-16 17:55:28 +08001985 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xiongf1e48bb2023-03-28 13:38:22 +08001986 RLOGD("enter lynq_ap_password_set");
1987 if( password == NULL )
1988 {
1989 RLOGE("[lynq_ap_password_set]input password is NULL");
qs.xiong6ad4d7d2022-06-27 11:34:53 +08001990 return -1;
1991 }
1992 pass_len=strlen(password);
you.chenc7357f22022-05-16 17:55:28 +08001993 lynq_wifi_auth_s auth = -1;
qs.xiongf1e48bb2023-03-28 13:38:22 +08001994 if(pass_len < 8 || pass_len >= 64)
1995 {
1996 RLOGE("[lynq_ap_password_set]input password len not in rage");
1997 return -1;
you.chen87ff5172022-05-06 11:30:57 +08001998 }
qs.xiongd189c542022-03-31 00:58:23 -04001999
you.chen87ff5172022-05-06 11:30:57 +08002000 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04002001
qs.xiongf1e48bb2023-03-28 13:38:22 +08002002 if (0 != lynq_wifi_ap_auth_get(idx, &auth))
2003 {
qs.xiong045f60b2023-03-29 17:36:14 +08002004 RLOGE("[lynq_ap_password_set] get ap auth info error\n");
you.chenc7357f22022-05-16 17:55:28 +08002005 return -1;
2006 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002007 else if (auth == LYNQ_WIFI_AUTH_OPEN)
2008 {
qs.xiong045f60b2023-03-29 17:36:14 +08002009 RLOGD("ap auth :LYNQ_WIFI_AUTH_OPEN\n");
2010 return 0;
you.chenc7357f22022-05-16 17:55:28 +08002011 }
2012
you.chen87ff5172022-05-06 11:30:57 +08002013 CHECK_WPA_CTRL(CTRL_AP);
2014
qs.xiongf1e48bb2023-03-28 13:38:22 +08002015 if (auth == LYNQ_WIFI_AUTH_WEP)
2016 {
qs.xiong045f60b2023-03-29 17:36:14 +08002017 RLOGD("[lynq_ap_password_set]ap auth : LYNQ_WIFI_AUTH_WEP\n");
you.chenc7357f22022-05-16 17:55:28 +08002018 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
2019 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
2020 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2021 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
2022 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002023 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2024 {
qs.xiong045f60b2023-03-29 17:36:14 +08002025 RLOGD("[lynq_ap_password_set]ap auth :LYNQ_WIFI_AUTH_WPA_PSK LYNQ_WIFI_AUTH_WPA2_PSK\n");
you.chenc7357f22022-05-16 17:55:28 +08002026 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
2027 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2028 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002029 else if (auth == LYNQ_WIFI_AUTH_WPA2_WPA3_PSK || auth == LYNQ_WIFI_AUTH_WPA3_PSK)
2030 {
2031
qs.xiong045f60b2023-03-29 17:36:14 +08002032 RLOGD("[lynq_ap_password_set]ap auth :LYNQ_WIFI_AUTH_WPA2_WPA3 LYNQ_WIFI_AUTH_WPA3_PSK\n");
qs.xiongf1e48bb2023-03-28 13:38:22 +08002033 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
qs.xiong083d8072023-03-28 14:06:12 +08002034 sprintf(lynq_wpa2_wpa3,"SET_NETWORK %d sae_password \"%s\"",AP_NETWORK_0, password);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002035 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2036 DO_OK_FAIL_REQUEST(lynq_wpa2_wpa3);
2037
2038 }
2039 else
qs.xiong045f60b2023-03-29 17:36:14 +08002040 {
2041 RLOGD("[lynq_ap_password_set]ap auth :get ap auth error\n");
you.chenc7357f22022-05-16 17:55:28 +08002042 return -1;
2043 }
you.chen87ff5172022-05-06 11:30:57 +08002044
you.chen87ff5172022-05-06 11:30:57 +08002045 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong99b48d62022-04-07 05:41:29 -04002046
qs.xiongf1e48bb2023-03-28 13:38:22 +08002047 return 0;
qs.xiongd189c542022-03-31 00:58:23 -04002048}
2049
you.chen87ff5172022-05-06 11:30:57 +08002050int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongd189c542022-03-31 00:58:23 -04002051{
you.chen87ff5172022-05-06 11:30:57 +08002052 FILE * fp;
2053 int len, ret;
2054 int count, index;
2055 char *split_lines[128] = {0};
2056 char *buff, *p;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002057 RLOGD("enter lynq_ap_password_get");
qs.xiong99b48d62022-04-07 05:41:29 -04002058
you.chen87ff5172022-05-06 11:30:57 +08002059 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04002060
you.chen87ff5172022-05-06 11:30:57 +08002061 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
2062// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiongf1e48bb2023-03-28 13:38:22 +08002063 if (NULL == fp)
2064 {
2065 RLOGE("open file fail\n");
you.chen87ff5172022-05-06 11:30:57 +08002066 return -1;
2067 }
qs.xiong99b48d62022-04-07 05:41:29 -04002068
you.chen87ff5172022-05-06 11:30:57 +08002069 buff = alloca(MAX_RET);
2070 fseek(fp, 0, SEEK_SET);
2071 len = fread(buff, 1, MAX_RET, fp);
2072 fclose(fp);
qs.xiong99b48d62022-04-07 05:41:29 -04002073
qs.xiongf1e48bb2023-03-28 13:38:22 +08002074 for(index=0; index < len; index ++)
2075 {
2076 if (memcmp(buff + index, "network={", 9) != 0)
2077 {
you.chen87ff5172022-05-06 11:30:57 +08002078 continue;
2079 }
2080 p = buff + index + 9;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002081 for (; index < len; index ++ )
2082 {
2083 if (buff[index] != '}')
2084 {
you.chen87ff5172022-05-06 11:30:57 +08002085 continue;
2086 }
2087 buff[index] = '\0';
2088 break;
2089 }
2090 len = buff + index - p;
2091 }
2092
2093 count = lynq_split(p, len, '\n', split_lines);
2094
2095 ret = -1;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002096 for(index=0; index < count; index++)
2097 {
you.chen87ff5172022-05-06 11:30:57 +08002098 p = strstr(split_lines[index], "psk=");
qs.xiongf1e48bb2023-03-28 13:38:22 +08002099 if (p != NULL)
2100 {
you.chenc7357f22022-05-16 17:55:28 +08002101 p += 4;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002102 if (*p == '\"')
2103 {
you.chenc7357f22022-05-16 17:55:28 +08002104 p++;
2105 }
you.chen87ff5172022-05-06 11:30:57 +08002106 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002107 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2108 {
you.chenc7357f22022-05-16 17:55:28 +08002109 p += 9;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002110 if (*p == '\"')
2111 {
you.chenc7357f22022-05-16 17:55:28 +08002112 p++;
2113 }
2114 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002115 else
2116 {
you.chenc7357f22022-05-16 17:55:28 +08002117 continue;
you.chen87ff5172022-05-06 11:30:57 +08002118 }
2119
2120 strcpy(password, p);
2121
qs.xiongf1e48bb2023-03-28 13:38:22 +08002122 while(*password != '\0')
2123 {
2124 if (*password == '\"')
2125 {
you.chen87ff5172022-05-06 11:30:57 +08002126 *password = '\0';
2127 break;
2128 }
2129 password++;
2130 }
2131 ret = 0;
2132 break;
2133 } //end for(index=0; index < count; index++)
2134
2135 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -05002136}
2137
you.chen87ff5172022-05-06 11:30:57 +08002138static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
2139 char lynq_auth_str[MAX_RET] = {0};
you.chen5e363602022-05-08 12:20:18 +08002140 char lynq_proto_str[MAX_RET] = {0};
qs.xiong99b48d62022-04-07 05:41:29 -04002141
qs.xiongf1e48bb2023-03-28 13:38:22 +08002142 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0)
2143 {
you.chen87ff5172022-05-06 11:30:57 +08002144 return -1;
2145 }
2146
2147 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chen5e363602022-05-08 12:20:18 +08002148
qs.xiongf1e48bb2023-03-28 13:38:22 +08002149 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK)
2150 {
2151 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0)
you.chenf9d718d2023-04-14 18:17:09 +08002152 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08002153 if (strcmp(lynq_proto_str, "RSN") == 0)
you.chenf9d718d2023-04-14 18:17:09 +08002154 {
you.chen5e363602022-05-08 12:20:18 +08002155 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
qs.xiong045f60b2023-03-29 17:36:14 +08002156 return 0;
you.chen5e363602022-05-08 12:20:18 +08002157 }
you.chenf9d718d2023-04-14 18:17:09 +08002158 else if (strcmp(lynq_proto_str, "WPA") == 0) // need compare when wpa3 supported
2159 {
2160 return 0;
2161 }
you.chen5e363602022-05-08 12:20:18 +08002162 }
2163 }
you.chenf9d718d2023-04-14 18:17:09 +08002164 else if (*auth == LYNQ_WIFI_AUTH_OPEN || *auth == LYNQ_WIFI_AUTH_WEP)
2165 {
2166 return 0;
2167 }
2168
qs.xiongf1e48bb2023-03-28 13:38:22 +08002169 if (inner_get_param(interface, net_no,"ieee80211w",lynq_auth_str) !=0)
2170 {
you.chenf9d718d2023-04-14 18:17:09 +08002171 RLOGE("check ieee80211w error\n");
qs.xiongf1e48bb2023-03-28 13:38:22 +08002172 return -1;
2173 }
2174 if ( strncmp(lynq_auth_str,"1",1) == 0 )
2175 {
2176
you.chenf9d718d2023-04-14 18:17:09 +08002177 *auth = LYNQ_WIFI_AUTH_WPA2_WPA3_PSK;
2178 return 0;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002179 }else if( strncmp(lynq_auth_str,"2",1) == 0 )
2180 {
2181
2182 *auth = LYNQ_WIFI_AUTH_WPA3_PSK;
qs.xiong045f60b2023-03-29 17:36:14 +08002183 return 0;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002184 }else
2185 {
you.chenf9d718d2023-04-14 18:17:09 +08002186 RLOGE("check ieee80211w error, not 1 or 2\n");
qs.xiongf1e48bb2023-03-28 13:38:22 +08002187 *auth = -1;
2188 return -1;
2189 }
you.chen87ff5172022-05-06 11:30:57 +08002190 return 0;
2191}
2192
2193int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong8d42bb92022-03-02 09:43:11 -05002194{
qs.xiongf1e48bb2023-03-28 13:38:22 +08002195 RLOGD("enter lynq_sta_ssid_password_set");
you.chen87ff5172022-05-06 11:30:57 +08002196 int pass_len, net_no, count, index;
2197 char lynq_tmp_cmd[300]={0};
2198 int net_no_list[128];
2199 lynq_wifi_auth_s net_auth;
2200 pass_len=strlen(password);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002201 if(pass_len < 8 || pass_len >= 64)
2202 {
2203 RLOGE("[lynq_sta_ssid_password_set]input psw error");
you.chen87ff5172022-05-06 11:30:57 +08002204 return -1;
2205 }
2206
2207 CHECK_IDX(idx, CTRL_STA);
2208
2209 net_no = -1;
2210 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
2211
qs.xiongf1e48bb2023-03-28 13:38:22 +08002212 for (index=0; index < count; index++)
2213 {
you.chen87ff5172022-05-06 11:30:57 +08002214 net_auth = -1;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002215 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth)
2216 {
you.chen87ff5172022-05-06 11:30:57 +08002217 net_no = net_no_list[index];
2218 break;
2219 }
2220 }
2221
qs.xiongf1e48bb2023-03-28 13:38:22 +08002222 if (net_no < 0)
2223 {
you.chen87ff5172022-05-06 11:30:57 +08002224 return -1;
2225 }
2226
2227 CHECK_WPA_CTRL(CTRL_STA);
2228
2229 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
2230
2231 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
2232 DO_OK_FAIL_REQUEST(cmd_save_config);
2233
2234 return 0;
2235}
2236
2237int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
2238
2239 FILE * fp;
you.chened802ab2023-02-13 10:50:35 +08002240 int len, ret, network_len, i, ssid_len;
you.chen87ff5172022-05-06 11:30:57 +08002241 int count, index;
2242 char *split_lines[128] = {0};
you.chened802ab2023-02-13 10:50:35 +08002243 char *buff, *p, *ssid, *ssid_end_flag;
2244 char tmp_ssid[128]={0};
qs.xiongf1e48bb2023-03-28 13:38:22 +08002245 RLOGD("enter lynq_sta_ssid_password_get");
you.chen87ff5172022-05-06 11:30:57 +08002246
you.chen0c938042022-08-06 16:59:10 +08002247 network_len = 0;
2248 p = NULL;
2249
you.chen87ff5172022-05-06 11:30:57 +08002250 CHECK_IDX(idx, CTRL_STA);
2251
qs.xiongf1e48bb2023-03-28 13:38:22 +08002252 if (NULL == password)
2253 {
2254 RLOGE("bad param\n");
you.chen0c938042022-08-06 16:59:10 +08002255 return -1;
2256 }
2257
you.chen87ff5172022-05-06 11:30:57 +08002258 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
qs.xiongf1e48bb2023-03-28 13:38:22 +08002259 if (NULL == fp)
2260 {
2261 RLOGE("[lynq_sta_ssid_password_get] open file fail\n");
you.chen87ff5172022-05-06 11:30:57 +08002262 return -1;
2263 }
2264
2265 buff = alloca(MAX_RET);
2266 fseek(fp, 0, SEEK_SET);
2267 len = fread(buff, 1, MAX_RET, fp);
2268 fclose(fp);
2269
qs.xiongf1e48bb2023-03-28 13:38:22 +08002270 for(index=0; index < len; index ++)
2271 {
2272 for(; index < len; index ++)
2273 {
2274 if (memcmp(buff + index, "network={", 9) != 0)
2275 {
you.chen87ff5172022-05-06 11:30:57 +08002276 continue;
2277 }
2278 p = buff + index + 9;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002279 for (; index < len; index ++ )
2280 {
2281 if (buff[index] != '}')
2282 {
you.chen87ff5172022-05-06 11:30:57 +08002283 continue;
2284 }
2285 buff[index] = '\0';
2286 break;
2287 }
you.chen0c938042022-08-06 16:59:10 +08002288 network_len = buff + index - p;
2289 break;
you.chen87ff5172022-05-06 11:30:57 +08002290 }
2291
qs.xiongc6cab7c2023-02-17 18:41:07 +08002292 if (p == NULL)
2293 return -1;
2294
you.chened802ab2023-02-13 10:50:35 +08002295 ssid = strstr(p, "ssid=");
2296 if (ssid != NULL) {
2297 ssid += strlen("ssid=");
qs.xiongf1e48bb2023-03-28 13:38:22 +08002298 if (ssid[0] == '\"')
2299 {
you.chened802ab2023-02-13 10:50:35 +08002300 if (memcmp(ssid + 1, ap->ap_ssid, strlen(ap->ap_ssid)) == 0 && ssid[strlen(ap->ap_ssid) + 1] == '\"')
2301 break;
2302 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002303 else
2304 {
you.chened802ab2023-02-13 10:50:35 +08002305 ssid_end_flag = strstr(ssid, "\n");
2306 if (ssid_end_flag != NULL)
2307 {
2308 ssid_len = (ssid_end_flag - ssid) / 2;
2309 for(i=0; i<ssid_len; i++)
2310 {
2311 tmp_ssid[i] = inner_convert_char(ssid[i*2]) << 4 | inner_convert_char(ssid[i*2 + 1]);
2312 }
2313 if (memcmp(tmp_ssid, ap->ap_ssid, ssid_len) == 0)
2314 break;
2315 }
2316 }
you.chen87ff5172022-05-06 11:30:57 +08002317 }
you.chened802ab2023-02-13 10:50:35 +08002318
you.chen87ff5172022-05-06 11:30:57 +08002319 }
2320
qs.xiongf1e48bb2023-03-28 13:38:22 +08002321 if (index >= len || NULL == p || network_len <= 0)
2322 {
you.chen87ff5172022-05-06 11:30:57 +08002323 return -1;
2324 }
2325
you.chen0c938042022-08-06 16:59:10 +08002326 count = lynq_split(p, network_len, '\n', split_lines);
you.chen87ff5172022-05-06 11:30:57 +08002327
2328 ret = -1;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002329 for(index=0; index < count; index++)
2330 {
you.chen87ff5172022-05-06 11:30:57 +08002331 p = strstr(split_lines[index], "psk=");
qs.xiongf1e48bb2023-03-28 13:38:22 +08002332 if (p != NULL)
2333 {
you.chenc7357f22022-05-16 17:55:28 +08002334 p += 4;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002335 if (*p == '\"')
2336 {
you.chenc7357f22022-05-16 17:55:28 +08002337 p++;
2338 }
you.chen87ff5172022-05-06 11:30:57 +08002339 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002340 else if (NULL != (p = strstr(split_lines[index], "wep_key0=")))
2341 {
you.chenc7357f22022-05-16 17:55:28 +08002342 p += 9;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002343 if (*p == '\"')
2344 {
you.chenc7357f22022-05-16 17:55:28 +08002345 p++;
2346 }
2347 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002348 else
2349 {
you.chenc7357f22022-05-16 17:55:28 +08002350 continue;
you.chen87ff5172022-05-06 11:30:57 +08002351 }
2352
qs.xiong9c1020c2023-02-21 19:12:54 +08002353 if (*p == '\"')
2354 p++;
2355 strncpy(password, p, 64);
you.chen87ff5172022-05-06 11:30:57 +08002356
qs.xiong9c1020c2023-02-21 19:12:54 +08002357 p = password;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002358 while(password - p < 64 && *password != '\0')
2359 {
2360 if (*password == '\"')
2361 {
you.chen87ff5172022-05-06 11:30:57 +08002362 *password = '\0';
2363 break;
2364 }
2365 password++;
2366 }
2367 ret = 0;
2368 break;
2369 } //end for(index=0; index < count; index++)
2370
2371 return ret;
2372}
2373
2374static int inner_set_sta_ssid(int net_no, char *sta_ssid)
2375{
qs.xiongf1e48bb2023-03-28 13:38:22 +08002376 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongd189c542022-03-31 00:58:23 -04002377
qs.xiongf1e48bb2023-03-28 13:38:22 +08002378 if (sta_ssid == NULL)
2379 {
2380 RLOGE("sta_ssid is null\n");
2381 return -1;
you.chen87ff5172022-05-06 11:30:57 +08002382 }
2383
qs.xiongf1e48bb2023-03-28 13:38:22 +08002384 CHECK_WPA_CTRL(CTRL_STA);
you.chen87ff5172022-05-06 11:30:57 +08002385
2386 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
2387
2388 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
2389// DO_OK_FAIL_REQUEST(cmd_save_config);
2390
qs.xiongf1e48bb2023-03-28 13:38:22 +08002391 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05002392
2393}
2394
you.chen87ff5172022-05-06 11:30:57 +08002395static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong8d42bb92022-03-02 09:43:11 -05002396{
you.chen87ff5172022-05-06 11:30:57 +08002397 char lynq_disable_cmd[128]={0};
2398 char lynq_select_cmd[128]={0};
2399
2400 CHECK_WPA_CTRL(CTRL_STA);
2401
qs.xiongf1e48bb2023-03-28 13:38:22 +08002402 if (save != 0)
2403 {
you.chen948e0032022-06-07 18:01:16 +08002404 if (start_flag != 0)
2405 {
2406 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
2407 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2408 }
2409 else
2410 {
2411 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
2412 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2413 }
you.chen87ff5172022-05-06 11:30:57 +08002414 DO_OK_FAIL_REQUEST(cmd_save_config);
2415 }
2416
qs.xiongf1e48bb2023-03-28 13:38:22 +08002417 if (start_flag == 0)
2418 {
you.chenc7357f22022-05-16 17:55:28 +08002419 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen87ff5172022-05-06 11:30:57 +08002420 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
2421 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002422 else
2423 {
you.chen87ff5172022-05-06 11:30:57 +08002424 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
2425 DO_OK_FAIL_REQUEST(lynq_select_cmd);
2426 }
2427
2428 return 0;
2429}
2430
2431int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
2432{
qs.xiongf1e48bb2023-03-28 13:38:22 +08002433 RLOGD("enter lynq_sta_ssid_password_set");
you.chen87ff5172022-05-06 11:30:57 +08002434 CHECK_IDX(idx, CTRL_STA);
2435
you.chenc7357f22022-05-16 17:55:28 +08002436 curr_status_info curr_state;
2437 ap_info_s ap_info;
2438 curr_state.ap = &ap_info;
2439 curr_state.state = NULL;
2440
qs.xiongf1e48bb2023-03-28 13:38:22 +08002441 if (0 == inner_get_status_info(CTRL_STA, &curr_state))
2442 {
you.chened802ab2023-02-13 10:50:35 +08002443 strncpy(sta_ssid, ap_info.ap_ssid, sizeof (ap_info.ap_ssid));
you.chenc7357f22022-05-16 17:55:28 +08002444 return 0;
2445 }
2446
2447 return -1;
you.chen87ff5172022-05-06 11:30:57 +08002448}
2449
2450int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
2451{
you.chen61c7aee2022-08-06 17:01:16 +08002452 scan_info_s *scan_list = NULL;
2453 saved_ap_info_s *save_list = NULL;
you.chen87ff5172022-05-06 11:30:57 +08002454 int scan_len=0;
2455 int save_len=0;
2456 int best_index = -1;
2457 int best_scan_index = -1;
2458 int best_rssi = 0;
you.chen61c7aee2022-08-06 17:01:16 +08002459 int i, j, ret;
2460
2461 ret = -1;
you.chen87ff5172022-05-06 11:30:57 +08002462
2463 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002464 if (info == NULL)
2465 {
you.chen87ff5172022-05-06 11:30:57 +08002466 return -1;
2467 }
2468
2469 curr_status_info curr_state;
2470 ap_info_s ap_info;
you.chen61c7aee2022-08-06 17:01:16 +08002471 char status[64];
you.chen87ff5172022-05-06 11:30:57 +08002472
you.chen61c7aee2022-08-06 17:01:16 +08002473 memset(&ap_info, 0, sizeof (ap_info));
2474 memset(status, 0, sizeof (status));
2475
2476 curr_state.ap = &ap_info;
2477 curr_state.state = status;
2478
qs.xiongf1e48bb2023-03-28 13:38:22 +08002479 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
2480 {
you.chen87ff5172022-05-06 11:30:57 +08002481 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen61c7aee2022-08-06 17:01:16 +08002482 if (strcmp(status, STATE_COMPLETED) == 0)
2483 {
2484 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
2485 }
2486 else
2487 {
2488 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2489 }
you.chenc32dc912023-04-27 17:52:44 +08002490 lynq_get_connect_ap_ip(idx, info->base_info.ap_ip);
you.chen87ff5172022-05-06 11:30:57 +08002491 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen61c7aee2022-08-06 17:01:16 +08002492 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen87ff5172022-05-06 11:30:57 +08002493 return 0;
2494 }
2495
you.chen61c7aee2022-08-06 17:01:16 +08002496 lynq_wifi_sta_start_scan(idx);
qs.xionge2dc2e02023-04-06 11:08:48 +08002497 sleep(2);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002498 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len))
2499 {
you.chen61c7aee2022-08-06 17:01:16 +08002500 if (NULL != scan_list)
2501 {
2502 free(scan_list);
2503 }
you.chen87ff5172022-05-06 11:30:57 +08002504 return -1;
2505 }
2506
qs.xiongf1e48bb2023-03-28 13:38:22 +08002507 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len))
2508 {
you.chen61c7aee2022-08-06 17:01:16 +08002509 if (NULL != scan_list)
2510 {
2511 free(scan_list);
2512 }
2513 if (NULL != save_list)
2514 {
2515 free(save_list);
2516 }
you.chen87ff5172022-05-06 11:30:57 +08002517 return -1;
2518 }
2519
qs.xiongf1e48bb2023-03-28 13:38:22 +08002520 for (i=0; i < save_len; i++)
2521 {
2522 for (j=0; j < scan_len; j++)
2523 {
you.chen87ff5172022-05-06 11:30:57 +08002524 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
qs.xiongf1e48bb2023-03-28 13:38:22 +08002525 && save_list[i].base_info.auth == scan_list[j].auth)
2526 {
2527 if (best_rssi == 0)
2528 {
you.chen61c7aee2022-08-06 17:01:16 +08002529 best_index = i;
you.chen87ff5172022-05-06 11:30:57 +08002530 best_rssi = scan_list[j].rssi;
2531 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002532 else if (best_rssi > scan_list[j].rssi)
2533 {
you.chen87ff5172022-05-06 11:30:57 +08002534 best_index = i;
2535 best_scan_index = j;
2536 best_rssi = scan_list[j].rssi;
2537 }
you.chened802ab2023-02-13 10:50:35 +08002538 strncpy(save_list[i].base_info.ap_mac, scan_list[j].mac, sizeof (save_list[i].base_info.ap_mac));
you.chen87ff5172022-05-06 11:30:57 +08002539 break;
2540 }
2541 }
2542 }
2543
qs.xiongf1e48bb2023-03-28 13:38:22 +08002544 if (best_index >= 0)
2545 {
you.chen87ff5172022-05-06 11:30:57 +08002546 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
you.chened802ab2023-02-13 10:50:35 +08002547 inner_get_ip_by_mac( info->base_info.ap_mac, info->base_info.ap_ip, sizeof (info->base_info.ap_ip));
you.chen87ff5172022-05-06 11:30:57 +08002548 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
2549 info->rssi = best_rssi;
you.chen61c7aee2022-08-06 17:01:16 +08002550 ret = 0;
you.chen87ff5172022-05-06 11:30:57 +08002551 }
2552
you.chen61c7aee2022-08-06 17:01:16 +08002553 if (NULL != scan_list)
2554 {
2555 free(scan_list);
2556 }
2557 if (NULL != save_list)
2558 {
2559 free(save_list);
2560 }
2561
2562 return ret;
you.chen87ff5172022-05-06 11:30:57 +08002563}
2564
2565static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
2566{
qs.xiong045f60b2023-03-29 17:36:14 +08002567 char lynq_auth_cmd[128]={0};
you.chen87ff5172022-05-06 11:30:57 +08002568 char lynq_ket_mgmt_cmd[64]={0};
2569 char lynq_pairwise_cmd[64]={0};
2570 char lynq_psk_cmd[64]={0};
2571
2572 CHECK_WPA_CTRL(CTRL_STA);
2573
qs.xiongf1e48bb2023-03-28 13:38:22 +08002574 switch(auth)
2575 {
2576 case LYNQ_WIFI_AUTH_OPEN:
you.chen87ff5172022-05-06 11:30:57 +08002577 {
2578 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong99b48d62022-04-07 05:41:29 -04002579
you.chen87ff5172022-05-06 11:30:57 +08002580 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002581// DO_OK_FAIL_REQUEST(cmd_save_config);
2582 break;
2583 }
2584 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen87ff5172022-05-06 11:30:57 +08002585 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1e48bb2023-03-28 13:38:22 +08002586 {
2587 if (auth == LYNQ_WIFI_AUTH_WPA_PSK)
2588 {
you.chen87ff5172022-05-06 11:30:57 +08002589 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
2590 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08002591 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK)
2592 {
you.chen5e363602022-05-08 12:20:18 +08002593 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen87ff5172022-05-06 11:30:57 +08002594 }
2595 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
2596 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong99b48d62022-04-07 05:41:29 -04002597
you.chen87ff5172022-05-06 11:30:57 +08002598 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
2599 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
2600 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong99b48d62022-04-07 05:41:29 -04002601
qs.xiongf1e48bb2023-03-28 13:38:22 +08002602 if (password != NULL)
2603 {
you.chen87ff5172022-05-06 11:30:57 +08002604 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
2605 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002606 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
you.chen87ff5172022-05-06 11:30:57 +08002607 }
qs.xiong99b48d62022-04-07 05:41:29 -04002608
you.chen87ff5172022-05-06 11:30:57 +08002609// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002610 break;
2611 }
2612 case LYNQ_WIFI_AUTH_WPA2_WPA3_PSK:
2613 {
qs.xionge2dc2e02023-04-06 11:08:48 +08002614 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 1",net_no);
qs.xiong045f60b2023-03-29 17:36:14 +08002615 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE WPA-PSK",net_no);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002616 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
2617 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
2618
qs.xionge2dc2e02023-04-06 11:08:48 +08002619 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002620 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
2621 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2622 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2623
2624 break;
2625 }
2626 case LYNQ_WIFI_AUTH_WPA3_PSK:
2627 {
qs.xionge2dc2e02023-04-06 11:08:48 +08002628 sprintf(lynq_auth_cmd,"SET_NETWORK %d ieee80211w 2",net_no);
qs.xiong2e4b05a2023-04-17 09:45:19 +08002629 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt SAE",net_no);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002630 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
2631 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
2632
qs.xionge2dc2e02023-04-06 11:08:48 +08002633 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002634 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
2635 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
2636 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
2637
2638 break;
2639 }
2640 default:
2641 return -1;
you.chen87ff5172022-05-06 11:30:57 +08002642 }
qs.xiong99b48d62022-04-07 05:41:29 -04002643
qs.xiongf1e48bb2023-03-28 13:38:22 +08002644 return 0;
qs.xiong799dab02022-03-14 09:12:12 -04002645}
qs.xiong8d42bb92022-03-02 09:43:11 -05002646
you.chen87ff5172022-05-06 11:30:57 +08002647static int inner_get_curr_net_no(int interface) {
2648 curr_status_info curr_state;
2649 curr_state.ap = NULL;
2650 curr_state.state = NULL;
2651
qs.xiongf1e48bb2023-03-28 13:38:22 +08002652 if (0 != inner_get_status_info(interface, &curr_state))
2653 {
you.chen87ff5172022-05-06 11:30:57 +08002654 return -1;
2655 }
2656
2657 return curr_state.net_no;
2658}
2659
2660int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong8d42bb92022-03-02 09:43:11 -05002661{
you.chen87ff5172022-05-06 11:30:57 +08002662 int net_no;
2663 CHECK_IDX(idx, CTRL_STA);
qs.xiongd189c542022-03-31 00:58:23 -04002664
you.chen87ff5172022-05-06 11:30:57 +08002665 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong8d42bb92022-03-02 09:43:11 -05002666
qs.xiongf1e48bb2023-03-28 13:38:22 +08002667 if (net_no < 0)
2668 {
you.chen87ff5172022-05-06 11:30:57 +08002669 return -1;
2670 }
2671
2672 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong8d42bb92022-03-02 09:43:11 -05002673}
2674
you.chen87ff5172022-05-06 11:30:57 +08002675int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
qs.xiong8d42bb92022-03-02 09:43:11 -05002676{
you.chen87ff5172022-05-06 11:30:57 +08002677 int count, net_no, index;
2678 int net_no_list[128];
2679 lynq_wifi_auth_s net_auth;
you.chenf9d718d2023-04-14 18:17:09 +08002680 curr_status_info curr_state;
2681 ap_info_s ap_info;
2682 char status[64];
qs.xiongd189c542022-03-31 00:58:23 -04002683
qs.xiongf1e48bb2023-03-28 13:38:22 +08002684 if (ssid == NULL || *ssid == '\0')
2685 {
2686 RLOGE("bad ssid\n");
you.chen87ff5172022-05-06 11:30:57 +08002687 return -1;
2688 }
qs.xiong8d42bb92022-03-02 09:43:11 -05002689
qs.xiongf1e48bb2023-03-28 13:38:22 +08002690 if (LYNQ_WIFI_AUTH_OPEN != auth)
2691 {
2692 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64)
you.chenf9d718d2023-04-14 18:17:09 +08002693 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08002694 RLOGE("bad password\n");
you.chen87ff5172022-05-06 11:30:57 +08002695 return -1;
2696 }
2697 }
qs.xiong8d42bb92022-03-02 09:43:11 -05002698
you.chenf9d718d2023-04-14 18:17:09 +08002699
2700 pthread_mutex_lock(&s_global_check_mutex);
2701 if (s_sta_status != INNER_STA_STATUS_INIT)
2702 {
2703 s_sta_status = INNER_STA_STATUS_CANCEL;
2704 pthread_cond_signal(&s_global_check_cond);
2705 }
2706 pthread_mutex_unlock(&s_global_check_mutex);
2707
you.chen87ff5172022-05-06 11:30:57 +08002708 CHECK_IDX(idx, CTRL_STA);
you.chenf9d718d2023-04-14 18:17:09 +08002709 CHECK_WPA_CTRL(CTRL_STA);
you.chen87ff5172022-05-06 11:30:57 +08002710
2711 net_no = -1;
you.chenf9d718d2023-04-14 18:17:09 +08002712 memset(&ap_info, 0, sizeof (ap_info));
2713 memset(status, 0, sizeof (status));
you.chen87ff5172022-05-06 11:30:57 +08002714
you.chenf9d718d2023-04-14 18:17:09 +08002715 curr_state.ap = &ap_info;
2716 curr_state.state = status;
2717
2718 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0)
qs.xiongf1e48bb2023-03-28 13:38:22 +08002719 {
you.chenf9d718d2023-04-14 18:17:09 +08002720 if (strcmp(status, STATE_COMPLETED) == 0 && strcmp(ap_info.ap_ssid, ssid) ==0 && ap_info.auth == auth)
2721 {
2722 net_no = curr_state.net_no;
2723 if (0 == lynq_sta_ssid_password_get(idx, &ap_info, ap_info.psw)
2724 && strcmp(ap_info.psw, psw) == 0)
2725 {
2726 RLOGD("already connected\n");
2727
2728 pthread_mutex_lock(&s_global_check_mutex);
2729 s_sta_status = INNER_STA_STATUS_CONNECTED;
2730 pthread_cond_signal(&s_global_check_cond);
2731 pthread_mutex_unlock(&s_global_check_mutex);
2732 return 0;
2733 }
you.chen87ff5172022-05-06 11:30:57 +08002734 }
2735 }
2736
you.chenf9d718d2023-04-14 18:17:09 +08002737 if (net_no == -1)
qs.xiongf1e48bb2023-03-28 13:38:22 +08002738 {
you.chenf9d718d2023-04-14 18:17:09 +08002739 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2740
2741 for (index=0; index < count; index++)
2742 {
2743 net_auth = -1;
2744 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
2745 {
2746 net_no = net_no_list[index];
2747 break;
2748 }
you.chen87ff5172022-05-06 11:30:57 +08002749 }
2750
you.chenf9d718d2023-04-14 18:17:09 +08002751 if (net_no < 0)
2752 {
2753 net_no = lynq_add_network(CTRL_STA);
2754 if (net_no == -1)
2755 {
2756 return -1;
2757 }
2758
2759 RLOGD("net no is %d\n", net_no);
2760 if (0 != inner_set_sta_ssid(net_no, ssid))
2761 {
2762 return -1;
2763 }
you.chen87ff5172022-05-06 11:30:57 +08002764 }
2765 }
2766
qs.xiongf1e48bb2023-03-28 13:38:22 +08002767 if (0 != inner_set_sta_auth_psw(net_no, auth, psw))
2768 {
you.chen87ff5172022-05-06 11:30:57 +08002769 return -1;
2770 }
2771
you.chenf9d718d2023-04-14 18:17:09 +08002772
2773 DO_OK_FAIL_REQUEST(cmd_disconnect);
2774 usleep(200*1000);
2775
2776 ret = inner_sta_start_stop(net_no, 1, 1);
2777
2778 pthread_mutex_lock(&s_global_check_mutex);
2779 s_sta_status = INNER_STA_STATUS_CONNECTING;
2780 strcpy(s_sta_current_connecting_ssid, ssid);
2781 struct timeval now;
2782 gettimeofday(&now,NULL);
2783 s_sta_connect_timeout.tv_sec = now.tv_sec + MAX_CONNNECT_TIME;
2784 s_sta_connect_timeout.tv_nsec = now.tv_usec*1000;
2785 pthread_cond_signal(&s_global_check_cond);
2786 pthread_mutex_unlock(&s_global_check_mutex);
2787 return ret;
qs.xiong8d42bb92022-03-02 09:43:11 -05002788}
2789
you.chen87ff5172022-05-06 11:30:57 +08002790int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong8d42bb92022-03-02 09:43:11 -05002791{
you.chen87ff5172022-05-06 11:30:57 +08002792 ap_info_s ap;
2793 curr_status_info curr_state;
2794 ap.ap_ssid[0] = '\0';
qs.xiong99b48d62022-04-07 05:41:29 -04002795
qs.xiongf1e48bb2023-03-28 13:38:22 +08002796 if (ssid == NULL || *ssid == '\0')
2797 {
2798 RLOGE("input ssid is NULL\n");
you.chen87ff5172022-05-06 11:30:57 +08002799 return -1;
2800 }
qs.xiong8d42bb92022-03-02 09:43:11 -05002801
you.chen87ff5172022-05-06 11:30:57 +08002802 CHECK_IDX(idx, CTRL_STA);
qs.xiong99b48d62022-04-07 05:41:29 -04002803
you.chen87ff5172022-05-06 11:30:57 +08002804 curr_state.ap = &ap;
you.chenefc7f3f2022-05-06 17:50:16 +08002805 curr_state.state = NULL;
2806
qs.xiongf1e48bb2023-03-28 13:38:22 +08002807 if (inner_get_status_info(CTRL_STA, &curr_state) != 0)
2808 {
you.chen87ff5172022-05-06 11:30:57 +08002809 return 0;
2810 }
qs.xiong799dab02022-03-14 09:12:12 -04002811
qs.xiongf1e48bb2023-03-28 13:38:22 +08002812 if (strcmp(ap.ap_ssid, ssid) != 0)
2813 {
you.chen87ff5172022-05-06 11:30:57 +08002814 return 0;
2815 }
2816
you.chenf9d718d2023-04-14 18:17:09 +08002817 pthread_mutex_lock(&s_global_check_mutex);
2818 s_sta_status = INNER_STA_STATUS_DISCONNECTING;
2819 pthread_mutex_unlock(&s_global_check_mutex);
you.chen87ff5172022-05-06 11:30:57 +08002820 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong8d42bb92022-03-02 09:43:11 -05002821}
qs.xiong99b48d62022-04-07 05:41:29 -04002822
you.chen5e363602022-05-08 12:20:18 +08002823int lynq_wifi_sta_start(lynq_wifi_index_e idx)
2824{
qs.xiongfa7be312023-01-18 13:17:41 +08002825// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
2826// const char *lynq_reconnect_cmd = "RECONNECT";
2827 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
2828 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
2829// const char *lynq_first_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 remove_net all";
qs.xiong8d42bb92022-03-02 09:43:11 -05002830
you.chen87ff5172022-05-06 11:30:57 +08002831 CHECK_IDX(idx, CTRL_STA);
you.chen5e363602022-05-08 12:20:18 +08002832 CHECK_WPA_CTRL(CTRL_STA);
2833
you.chencd882682023-04-24 15:39:37 +08002834 ret = system_call_v("%s %s", start_stop_sta_script, "start");
2835 if (ret != 0)
qs.xiongfa7be312023-01-18 13:17:41 +08002836 {
you.chencd882682023-04-24 15:39:37 +08002837 RLOGE("lynq_wifi_ap_start excute script fail");
you.chen87ff5172022-05-06 11:30:57 +08002838 return -1;
2839 }
qs.xionge5164a82022-03-15 08:03:26 -04002840
qs.xiongfa7be312023-01-18 13:17:41 +08002841 system(lynq_enable_sta_cmd);
2842 system(lynq_reconnect_cmd);
2843// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chen5e363602022-05-08 12:20:18 +08002844 return 0;
qs.xiong8d42bb92022-03-02 09:43:11 -05002845}
2846
you.chen87ff5172022-05-06 11:30:57 +08002847int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong99b48d62022-04-07 05:41:29 -04002848{
qs.xiongfa7be312023-01-18 13:17:41 +08002849// char lynq_disable_network_cmd[MAX_CMD];
2850// curr_status_info curr_state;
2851// ap_info_s ap_info;
you.chen87ff5172022-05-06 11:30:57 +08002852
qs.xiongfa7be312023-01-18 13:17:41 +08002853 const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all";
you.chen5e363602022-05-08 12:20:18 +08002854 CHECK_IDX(idx, CTRL_STA);
2855 CHECK_WPA_CTRL(CTRL_STA);
2856
qs.xiongfa7be312023-01-18 13:17:41 +08002857 system(lynq_disable_sta_cmd);
you.chen5e363602022-05-08 12:20:18 +08002858 DO_OK_FAIL_REQUEST(cmd_save_config);
you.chencd882682023-04-24 15:39:37 +08002859
2860 ret = system_call_v("%s %s", start_stop_sta_script, "stop");
2861 if (ret != 0)
2862 {
2863 RLOGE("lynq_wifi_ap_start excute script fail");
2864 return -1;
2865 }
2866
you.chen5e363602022-05-08 12:20:18 +08002867 return 0;
2868// return system("connmanctl disable wifi");
qs.xiongd189c542022-03-31 00:58:23 -04002869}
qs.xiong8d42bb92022-03-02 09:43:11 -05002870
you.chen87ff5172022-05-06 11:30:57 +08002871//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
2872// int i, count;
2873// char *p;
2874// const char * FLAG_SSID = "ssid=";
2875// const char * FLAG_SBSID = "bssid=";
2876// const char * FLAG_KEY_MGMT = "key_mgmt=";
2877// const char * FLAG_FREQ = "freq=";
2878// char lynq_sta_cmd[MAX_CMD];
2879// char *split_lines[128] = {0};
2880
2881// CHECK_WPA_CTRL(CTRL_AP);
2882
2883// sprintf(lynq_sta_cmd, "STA %s", bssid);
2884
2885// DO_REQUEST(lynq_sta_cmd);
2886
2887// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2888
2889// for(i=0; i < count; i++) {
2890// p = strstr(split_lines[i], FLAG_SSID);
2891// if (p != NULL) {
2892// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
2893// continue;
2894// }
2895// }
2896
2897// lynq_get_interface_ip(idx, ap->ap_ip);
2898// lynq_ap_password_set(idx, ap->psw);
2899
2900// return 0;
2901//}
2902
2903static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
2904 curr_status_info curr_state;
2905 curr_state.ap = ap;
2906 curr_state.state = NULL;
2907 return inner_get_status_info(interface, &curr_state);
2908}
2909
2910int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len)
qs.xiong99b48d62022-04-07 05:41:29 -04002911{
you.chened802ab2023-02-13 10:50:35 +08002912 int index, line_count;
2913 device_info_s *dev_info;
you.chen87ff5172022-05-06 11:30:57 +08002914 const char *lynq_first_sta_cmd = "STA-FIRST";
2915 char lynq_next_sta_cmd[MAX_CMD];
2916 char *bssid[1024] = {0};
you.chen87ff5172022-05-06 11:30:57 +08002917 char *split_lines[128] = {0};
qs.xiong99b48d62022-04-07 05:41:29 -04002918
you.chen87ff5172022-05-06 11:30:57 +08002919 CHECK_IDX(idx, CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04002920
you.chen87ff5172022-05-06 11:30:57 +08002921 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong99b48d62022-04-07 05:41:29 -04002922
you.chen87ff5172022-05-06 11:30:57 +08002923// ap_info_s * tmp_ap;
2924// device_info_s * tmp_list;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002925 if (ap == NULL || list == NULL || len == NULL)
2926 {
2927 RLOGE("bad input param");
you.chen87ff5172022-05-06 11:30:57 +08002928 return -1;
2929 }
2930
2931// ap = &tmp_ap;
2932// list = &tmp_list;
2933 *ap = malloc(sizeof (ap_info_s));
2934
qs.xiongf1e48bb2023-03-28 13:38:22 +08002935 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0)
2936 {
2937 RLOGE("inner_get_status_info_ap !=0\n");
you.chen87ff5172022-05-06 11:30:57 +08002938 return -1;
2939 }
2940
2941 lynq_get_interface_ip(idx, (*ap)->ap_ip);
2942 lynq_ap_password_get(idx, (*ap)->psw);
2943
you.chen87ff5172022-05-06 11:30:57 +08002944 DO_REQUEST(lynq_first_sta_cmd);
2945
2946 index = 0;
qs.xiongf1e48bb2023-03-28 13:38:22 +08002947 while (reply_len > 0)
2948 {
2949 if (memcmp(cmd_reply, "FAIL", 4) == 0)
2950 {
you.chen87ff5172022-05-06 11:30:57 +08002951 break;
2952 }
2953 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2954 bssid[index] = malloc(strlen(split_lines[0]) + 1);
2955 strcpy(bssid[index], split_lines[0]);
2956 index++;
2957 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
2958 reply_len = MAX_RET;
2959 cmd_reply[0] = '\0';
you.chened802ab2023-02-13 10:50:35 +08002960 ret = local_wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
qs.xiongf1e48bb2023-03-28 13:38:22 +08002961 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0)
2962 {
2963 RLOGD("run %s fail \n", lynq_next_sta_cmd);
you.chen87ff5172022-05-06 11:30:57 +08002964 break;
2965 }
2966 }
2967
2968 *len = index;
2969
2970 *list = malloc(sizeof(device_info_s) * (*len));
qs.xiongf1e48bb2023-03-28 13:38:22 +08002971 for (index=0; index < *len; index++)
2972 {
you.chened802ab2023-02-13 10:50:35 +08002973 dev_info = &(*list)[index];
2974 memset(dev_info, 0, sizeof(device_info_s));
2975 strncpy(dev_info->sta_mac, bssid[index], sizeof (dev_info->sta_mac));
2976 inner_get_ip_by_mac(dev_info->sta_mac, dev_info->sta_ip, sizeof (dev_info->sta_ip));
2977 inner_get_hostname_by_ip(dev_info->sta_ip, dev_info->hostname);
2978 dev_info->status = LYNQ_WIFI_STATUS_CONNECT;
you.chen87ff5172022-05-06 11:30:57 +08002979 free(bssid[index]);
2980 }
2981
2982 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04002983}
2984
you.chen87ff5172022-05-06 11:30:57 +08002985int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong99b48d62022-04-07 05:41:29 -04002986{
you.chen87ff5172022-05-06 11:30:57 +08002987 int i, count, index, count_words;
2988 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2989 char *split_lines[128] = {0};
2990 char *split_words[128] = {0};
2991 scan_info_s * p;
qs.xiong99b48d62022-04-07 05:41:29 -04002992
qs.xiongf1e48bb2023-03-28 13:38:22 +08002993 if (list == NULL || len == NULL)
2994 {
you.chen87ff5172022-05-06 11:30:57 +08002995 return -1;
2996 }
qs.xiong99b48d62022-04-07 05:41:29 -04002997
you.chen61c7aee2022-08-06 17:01:16 +08002998 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2999 {
3000 usleep(100 * 1000);
3001 }
3002
you.chen87ff5172022-05-06 11:30:57 +08003003 CHECK_IDX(idx, CTRL_STA);
3004
3005 CHECK_WPA_CTRL(CTRL_STA);
3006
3007 DO_REQUEST(lynq_scan_result_cmd);
3008
3009 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
3010 *len = count - 1;
3011 *list = malloc(sizeof (scan_info_s) * *len);
3012
3013 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
qs.xiongf1e48bb2023-03-28 13:38:22 +08003014 for (index=0; index <count_words; index++)
3015 {
3016 RLOGD("----header: %s\n", split_words[index]);
you.chen87ff5172022-05-06 11:30:57 +08003017 }
3018
qs.xiongf1e48bb2023-03-28 13:38:22 +08003019 for(index = 1;index < count; index++)
3020 {
3021 RLOGD("---- %s\n",split_lines[index]);
you.chen7bf12432023-04-27 17:51:56 +08003022 memset(split_words, 0 , sizeof (split_words));
you.chen87ff5172022-05-06 11:30:57 +08003023 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
3024 if (count_words < 4)
3025 continue;
qs.xiongf1e48bb2023-03-28 13:38:22 +08003026 RLOGD("count: %d, %s\n", count_words, split_words[0]);
you.chen87ff5172022-05-06 11:30:57 +08003027 //bssid / frequency / signal level / flags / ssid
3028 p = (*list) + index - 1;
3029 strcpy(p->mac, split_words[0]);
3030 p->band = convert_band_from_freq(atoi(split_words[1]));
3031 p->rssi = -1 * atoi( split_words[2]);
3032 p->auth = convert_max_auth_from_flag(split_words[3]);
you.chen7bf12432023-04-27 17:51:56 +08003033 if (count_words == 4) // ssid hided
3034 {
3035 p->ssid[0] = '\0';
3036 }
3037 else
3038 {
3039 inner_copy_ssid(p->ssid, split_words[4], sizeof (p->ssid));
3040 }
you.chen87ff5172022-05-06 11:30:57 +08003041 }
3042
3043 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04003044}
qs.xiong99b48d62022-04-07 05:41:29 -04003045
you.chen87ff5172022-05-06 11:30:57 +08003046int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
3047{
3048 int count, net_no, index;
3049 int net_no_list[128];
3050 lynq_wifi_auth_s net_auth;
3051 char lynq_remove_cmd[MAX_CMD];
3052
qs.xiongf1e48bb2023-03-28 13:38:22 +08003053 if (ssid == NULL || *ssid == '\0')
3054 {
3055 RLOGD("bad ssid\n");
you.chen87ff5172022-05-06 11:30:57 +08003056 return -1;
3057 }
3058
3059 CHECK_IDX(idx, CTRL_STA);
3060
3061 CHECK_WPA_CTRL(CTRL_STA);
3062
3063 net_no = -1;
3064 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
3065
qs.xiongf1e48bb2023-03-28 13:38:22 +08003066 for (index=0; index < count; index++)
3067 {
you.chen87ff5172022-05-06 11:30:57 +08003068 net_auth = -1;
qs.xiongf1e48bb2023-03-28 13:38:22 +08003069 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth)
3070 {
you.chen87ff5172022-05-06 11:30:57 +08003071 net_no = net_no_list[index];
3072 break;
3073 }
3074 }
3075
qs.xiongf1e48bb2023-03-28 13:38:22 +08003076 if (net_no < 0)
3077 {
you.chen87ff5172022-05-06 11:30:57 +08003078 return 0;
3079 }
3080
3081 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
3082
3083 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
3084 DO_OK_FAIL_REQUEST(cmd_save_config);
3085
3086 return 0;
3087}
3088
3089int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
qs.xiongf1e48bb2023-03-28 13:38:22 +08003090{
you.chened802ab2023-02-13 10:50:35 +08003091 int count, index;
you.chen87ff5172022-05-06 11:30:57 +08003092 int net_no_list[128];
3093 char freq[16];
qs.xiongf1e48bb2023-03-28 13:38:22 +08003094 RLOGD("enter lynq_get_sta_saved_ap api\n");
3095 if (list == NULL || len == NULL)
3096 {
3097 RLOGE("bad param,please check!");
you.chen87ff5172022-05-06 11:30:57 +08003098 return -1;
3099 }
3100
3101 CHECK_IDX(idx, CTRL_STA);
3102
3103// CHECK_WPA_CTRL(CTRL_STA);
3104
3105 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003106 RLOGD("[lynq_get_sta_saved_ap]count is %d\n", count);
you.chen87ff5172022-05-06 11:30:57 +08003107
you.chen8418a4e2023-04-13 14:06:58 +08003108 if (count < 0)
3109 {
3110 RLOGE("list network fail");
3111 return count;
3112 }
3113 else if (count == 0)
3114 {
3115 *list = NULL;
3116 *len = 0;
3117 return 0;
3118 }
3119
you.chen87ff5172022-05-06 11:30:57 +08003120 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen0c938042022-08-06 16:59:10 +08003121 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen87ff5172022-05-06 11:30:57 +08003122 *len = count;
3123
qs.xiongf1e48bb2023-03-28 13:38:22 +08003124 for (index=0; index < count; index++)
3125 {
you.chen87ff5172022-05-06 11:30:57 +08003126 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen87ff5172022-05-06 11:30:57 +08003127 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen87ff5172022-05-06 11:30:57 +08003128 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003129 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0)
you.chen8418a4e2023-04-13 14:06:58 +08003130 {
you.chen87ff5172022-05-06 11:30:57 +08003131 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
3132 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08003133 else
you.chen8418a4e2023-04-13 14:06:58 +08003134 {
you.chen87ff5172022-05-06 11:30:57 +08003135 (*list)[index].base_info.band = -1;
3136 }
you.chen8418a4e2023-04-13 14:06:58 +08003137 RLOGD("[lynq_get_sta_saved_ap][inner_get_param]to get psw");
you.chen0c938042022-08-06 16:59:10 +08003138 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen87ff5172022-05-06 11:30:57 +08003139 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08003140 RLOGD("[lynq_get_sta_saved_ap] return ok");
you.chen87ff5172022-05-06 11:30:57 +08003141 return 0;
3142}
3143
3144int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
3145{
qs.xiong045f60b2023-03-29 17:36:14 +08003146 const char *clean_last_re ="wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 bss_flush";
you.chen87ff5172022-05-06 11:30:57 +08003147 const char *lynq_scan_cmd = "SCAN";
3148
3149 CHECK_IDX(idx, CTRL_STA);
3150
3151 CHECK_WPA_CTRL(CTRL_STA);
3152
you.chenf9d718d2023-04-14 18:17:09 +08003153// system(clean_last_re); // youchen @ 2023-04-14 temp delete ,next time to fix the orginal bug
you.chen61c7aee2022-08-06 17:01:16 +08003154 g_sta_scan_finish_flag = 0;
qs.xiongc6cab7c2023-02-17 18:41:07 +08003155 DO_REQUEST(lynq_scan_cmd);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003156 if (reply_len >=9 && memcmp(cmd_reply, "FAIL-BUSY", 9) == 0 )
3157 {
qs.xiongc6cab7c2023-02-17 18:41:07 +08003158 return 0;
qs.xiongf1e48bb2023-03-28 13:38:22 +08003159 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0)
3160 {
qs.xiongc6cab7c2023-02-17 18:41:07 +08003161 g_sta_scan_finish_flag = 1;
3162 return -1;
3163 }
you.chen87ff5172022-05-06 11:30:57 +08003164
3165 return 0;
3166}
3167
3168int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
qs.xiongf1e48bb2023-03-28 13:38:22 +08003169 if (cb == NULL)
3170 {
3171 RLOGE("lynq_reg_ap_event_callback ptr is NULL,plese check!\n");
you.chen87ff5172022-05-06 11:30:57 +08003172 return -1;
3173 }
3174
you.chen603c58a2023-04-24 13:55:29 +08003175 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen87ff5172022-05-06 11:30:57 +08003176 g_ap_callback_priv = priv;
3177 g_ap_callback_func = cb;
3178
you.chen603c58a2023-04-24 13:55:29 +08003179 if (g_ap_watcher_pid == 0 )
3180 {
3181 if(pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL) < 0)
3182 {
3183 g_ap_watcher_pid = 0;
3184 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3185 RLOGE("[wifi error]creat APWatcherThreadProc fail");
3186 return -1;
3187 }
3188 }
3189
3190 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3191 RLOGD("creat APWatcherTheradProc susccs");
3192
you.chen87ff5172022-05-06 11:30:57 +08003193 return 0;
3194}
3195
3196int lynq_unreg_ap_event_callback(void * priv) {
qs.xiongf1e48bb2023-03-28 13:38:22 +08003197 if (g_ap_callback_priv == priv)
3198 {
you.chen87ff5172022-05-06 11:30:57 +08003199 g_ap_callback_func = NULL;
3200 g_ap_callback_priv = NULL;
3201 return 0;
3202 }
3203 return -1;
3204}
3205
3206int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
qs.xiongf1e48bb2023-03-28 13:38:22 +08003207 if (cb == NULL)
3208 {
3209 RLOGE("lynq_reg_sta_event_callback ptr is NULL,plese check!\n");
you.chen87ff5172022-05-06 11:30:57 +08003210 return -1;
3211 }
3212
you.chen603c58a2023-04-24 13:55:29 +08003213 pthread_mutex_lock(&s_check_wpa_ctrl_mutex);
you.chen87ff5172022-05-06 11:30:57 +08003214 g_sta_callback_priv = priv;
3215 g_sta_callback_func = cb;
3216
you.chen603c58a2023-04-24 13:55:29 +08003217 if (g_sta_watcher_pid == 0 ) {
3218 if(pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL) < 0)
3219 {
3220 g_sta_watcher_pid = 0;
3221 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3222 RLOGE("[wifi error]creat STAWatcherThreadProc fail");
3223 return -1;
3224 }
3225 }
3226
3227 pthread_mutex_unlock(&s_check_wpa_ctrl_mutex);
3228 RLOGD("creat STAWatcherTheradProc susccs");
you.chen87ff5172022-05-06 11:30:57 +08003229 return 0;
3230}
3231
3232int lynq_unreg_sta_event_callback(void * priv) {
qs.xiongf1e48bb2023-03-28 13:38:22 +08003233 if (g_sta_callback_priv == priv)
3234 {
you.chen87ff5172022-05-06 11:30:57 +08003235 g_sta_callback_func = NULL;
3236 g_sta_callback_priv = NULL;
3237 return 0;
3238 }
3239 return -1;
3240}
3241
3242
3243static int inner_get_status_info_state (int interface, char *state) {
3244 curr_status_info curr_state;
3245 curr_state.ap = NULL;
3246 curr_state.state = state;
3247 return inner_get_status_info(interface, &curr_state);
3248}
3249
3250int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
3251{
3252 char state[MAX_CMD];
qs.xiongf1e48bb2023-03-28 13:38:22 +08003253 RLOGD("enter lynq_get_ap_status\n");
you.chen87ff5172022-05-06 11:30:57 +08003254 CHECK_IDX(idx, CTRL_AP);
3255
qs.xiongf1e48bb2023-03-28 13:38:22 +08003256 if (inner_get_status_info_state(CTRL_AP, state) != 0)
3257 {
you.chen87ff5172022-05-06 11:30:57 +08003258 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3259 return 0;
3260 }
3261
qs.xiongf1e48bb2023-03-28 13:38:22 +08003262 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3263 {
you.chen87ff5172022-05-06 11:30:57 +08003264 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
3265 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08003266 else
3267 {
you.chen87ff5172022-05-06 11:30:57 +08003268 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
3269 }
3270
3271 return 0;
3272}
3273
3274int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
3275 char state[MAX_CMD];
qs.xiongf1e48bb2023-03-28 13:38:22 +08003276 RLOGD("enter lynq_get_sta_status\n");
you.chen87ff5172022-05-06 11:30:57 +08003277 CHECK_IDX(idx, CTRL_STA);
3278
qs.xiongf1e48bb2023-03-28 13:38:22 +08003279 if (inner_get_status_info_state(CTRL_STA, state) != 0)
3280 {
you.chen87ff5172022-05-06 11:30:57 +08003281 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3282 return 0;
3283 }
3284
qs.xiongf1e48bb2023-03-28 13:38:22 +08003285 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0)
3286 {
you.chen87ff5172022-05-06 11:30:57 +08003287 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
3288 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08003289 else
3290 {
you.chen87ff5172022-05-06 11:30:57 +08003291 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
3292 }
3293
3294 return 0;
3295}
3296
3297int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
3298// CHECK_IDX(idx, CTRL_AP);
3299// int ret = 0;
3300// size_t reply_len = MAX_RET;
3301// char cmd_reply[MAX_RET]={0};
3302// const char * cmd_str = "GET country";
3303// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
3304// do{
3305// if (NULL == s_lynq_wpa_ctrl) {
3306// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
3307// if (NULL == s_lynq_wpa_ctrl ) {
3308// printf("wpa_ctrl_open fail\n");
3309// return -1;
3310// }
3311// }
3312// }while(0);
3313
3314// do {
3315// reply_len = MAX_RET;
3316// cmd_reply[0] = '\0';
3317// printf("to call [%s]\n", cmd_str);
you.chened802ab2023-02-13 10:50:35 +08003318// ret = local_wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
you.chen87ff5172022-05-06 11:30:57 +08003319// if (ret != 0) {
qs.xiongf1e48bb2023-03-28 13:38:22 +08003320// RLOGE("call ##cmd_str fail %d\n", ret);
you.chen87ff5172022-05-06 11:30:57 +08003321// return ret;
3322// }
3323// cmd_reply[reply_len+1] = '\0';
qs.xiongf1e48bb2023-03-28 13:38:22 +08003324// RLOGD("cmd replay [ %s ]\n", cmd_reply);
you.chen87ff5172022-05-06 11:30:57 +08003325// }while(0);
3326
3327 FILE *fp;
3328 size_t i = 0;
3329 char lynq_cmd_ret[MAX_RET]={0};
3330
3331// CHECK_IDX(idx, CTRL_AP);
3332
3333 if((fp=popen("wl country","r"))==NULL)
qs.xiongf1e48bb2023-03-28 13:38:22 +08003334 {
3335 perror("popen error!");
3336 return -1;
3337 }
you.chen87ff5172022-05-06 11:30:57 +08003338 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
3339 {
3340 perror("fread fail!");
3341 return -1;
3342 }
3343
qs.xiongf1e48bb2023-03-28 13:38:22 +08003344 for(i=0; i < strlen(lynq_cmd_ret); i++)
3345 {
3346 if (lynq_cmd_ret[i] == ' ')
3347 {
you.chen87ff5172022-05-06 11:30:57 +08003348 lynq_cmd_ret[i] = '\0';
3349 break;
3350 }
3351 }
3352
3353 strcpy(country_code,lynq_cmd_ret);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003354 RLOGD("---country code %s\n", country_code);
you.chen87ff5172022-05-06 11:30:57 +08003355
3356 int ret=pclose(fp);
3357 if(ret==-1)
3358 {
3359 perror("close file faild");
3360 }
3361
3362 return 0;
3363}
3364
3365int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
3366// const char * cmd_str = "GET country";
3367// CHECK_IDX(idx, CTRL_AP);
3368// CHECK_WPA_CTRL(CTRL_STA);
3369
3370// DO_REQUEST(cmd_str);
3371// printf("result %s\n", cmd_reply);
3372
qs.xiongf1e48bb2023-03-28 13:38:22 +08003373 if (country_code == NULL || *country_code == '\0')
3374 {
3375 RLOGD("bad country code\n");
you.chen87ff5172022-05-06 11:30:57 +08003376 return -1;
3377 }
3378
3379 char lynq_country_cmd[MAX_CMD];
3380 sprintf(lynq_country_cmd, "wl country %s", country_code);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003381 if (system(lynq_country_cmd) == 0)
3382 {
you.chen87ff5172022-05-06 11:30:57 +08003383 return 0;
3384 }
3385
3386 return -1;
3387}
3388
3389int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
3390{
qs.xiongf1e48bb2023-03-28 13:38:22 +08003391 RLOGD("enter lynq_get_connect_ap_mac\n");
3392 if (mac == NULL)
3393 {
3394 RLOGE("input ptr is NULL,please check\n");
you.chen87ff5172022-05-06 11:30:57 +08003395 return -1;
3396 }
3397
3398 CHECK_IDX(idx, CTRL_STA);
3399 ap_info_s ap;
3400 ap.ap_mac[0] = '\0';
3401
qs.xiongf1e48bb2023-03-28 13:38:22 +08003402 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
3403 {
you.chen87ff5172022-05-06 11:30:57 +08003404 return -1;
3405 }
3406 strcpy(mac, ap.ap_mac);
3407
3408 return 0;
3409}
3410
3411int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
3412{
qs.xiongf1e48bb2023-03-28 13:38:22 +08003413 RLOGD("enter lynq_get_interface_ip\n");
you.chen61c7aee2022-08-06 17:01:16 +08003414 struct ifaddrs *ifaddr_header, *ifaddr;
3415 struct in_addr * ifa;
3416 const char * ifaName = "wlan0";
3417 if (ip == NULL)
3418 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08003419 RLOGE("[lynq_get_interface_ip]input erro,input is NULL ptr,please check\n");
you.chendad3f9f2022-06-21 16:53:48 +08003420 return -1;
you.chen61c7aee2022-08-06 17:01:16 +08003421 }
you.chendad3f9f2022-06-21 16:53:48 +08003422
qs.xiongf1e48bb2023-03-28 13:38:22 +08003423 if (idx == 1)
3424 {
you.chencd882682023-04-24 15:39:37 +08003425 ifaName = s_ap_iterface_name;
you.chen61c7aee2022-08-06 17:01:16 +08003426 }
qs.xiongf1e48bb2023-03-28 13:38:22 +08003427 else if (idx != 0)
3428 {
you.chen87ff5172022-05-06 11:30:57 +08003429 return -1;
you.chen61c7aee2022-08-06 17:01:16 +08003430 }
you.chen87ff5172022-05-06 11:30:57 +08003431
you.chen61c7aee2022-08-06 17:01:16 +08003432 if (getifaddrs(&ifaddr_header) == -1)
3433 {
you.chen87ff5172022-05-06 11:30:57 +08003434 perror("getifaddrs");
3435 return -1;
3436 //exit(EXIT_FAILURE);
you.chen61c7aee2022-08-06 17:01:16 +08003437 }
you.chen87ff5172022-05-06 11:30:57 +08003438
3439
you.chen61c7aee2022-08-06 17:01:16 +08003440 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
3441 {
3442 if (ifaddr->ifa_addr == NULL)
you.chen87ff5172022-05-06 11:30:57 +08003443 continue;
you.chen61c7aee2022-08-06 17:01:16 +08003444 if((strcmp(ifaddr->ifa_name,ifaName)==0))
3445 {
3446 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
3447 {
3448 // is a valid IP4 Address
3449 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
3450 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003451 RLOGD("[lynq_get_interface_ip]:%s IP Address %s/n", ifaddr->ifa_name, ip);
you.chen61c7aee2022-08-06 17:01:16 +08003452 freeifaddrs(ifaddr_header);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003453 RLOGD("ip %s\n", ip);
you.chen61c7aee2022-08-06 17:01:16 +08003454 return 0;
3455 }
3456 }
3457 }
qs.xiong84d87a42023-02-08 18:16:58 +08003458 freeifaddrs(ifaddr_header);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003459 RLOGE("[lynq_get_interface_ip] can't find interface | other erro\n");
you.chen61c7aee2022-08-06 17:01:16 +08003460 return -1;
you.chen87ff5172022-05-06 11:30:57 +08003461}
3462
3463int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
3464{
qs.xiongf1e48bb2023-03-28 13:38:22 +08003465 RLOGD("enter lynq_get_interface_mac\n");
you.chen87ff5172022-05-06 11:30:57 +08003466 int count;
3467 size_t i;
3468 char *split_words[128] = {0};
3469 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
3470
3471 CHECK_WPA_CTRL(idx);
3472
3473 DO_REQUEST(lynq_get_mac_cmd);
3474
qs.xiongf1e48bb2023-03-28 13:38:22 +08003475 if (memcmp(cmd_reply, "FAIL", 4) == 0)
3476 {
3477 RLOGE("[lynq_get_interface_mac]do request cmd --DRIVER MACADDR-- reply FAIL\n");
you.chen87ff5172022-05-06 11:30:57 +08003478 return -1;
3479 }
3480
3481 count = lynq_split(cmd_reply, reply_len, '=', split_words);
3482
qs.xiongf1e48bb2023-03-28 13:38:22 +08003483 if (count < 2)
3484 {
you.chen87ff5172022-05-06 11:30:57 +08003485 return -1;
3486 }
3487
qs.xiongf1e48bb2023-03-28 13:38:22 +08003488 for (i=0; i < strlen(split_words[1]); i++ )
3489 {
3490 if (split_words[1][i] != ' ')
3491 {
you.chen87ff5172022-05-06 11:30:57 +08003492 break;
3493 }
3494 }
3495
3496 strcpy(mac, split_words[1] + i);
3497
3498 return 0;
3499}
3500
3501int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
3502{
3503// int count;
3504// char *split_words[128] = {0};
3505// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
3506
3507// if (rssi == NULL) {
3508// return -1;
3509// }
3510
3511// CHECK_IDX(idx, CTRL_STA);
3512
3513// CHECK_WPA_CTRL(CTRL_STA);
3514
3515// DO_REQUEST(lynq_get_rssi_cmd);
3516
3517// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
3518// return -1;
3519// }
3520
3521// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
3522
3523// if (count < 2) {
3524// return -1;
3525// }
3526
3527// *rssi = atoi(split_words[1]) * -1;
3528
you.chen87ff5172022-05-06 11:30:57 +08003529 char lynq_cmd_ret[MAX_RET]={0};
3530
qs.xiongc28ae382022-10-11 15:47:14 +08003531/*******change other cmd to get rssi*******
3532 *
3533 *wl rssi ---> wl -i wlan0 rssi
3534 *
3535 ***** change by qs.xiong 20221011*******/
you.chenbf5d0ee2023-04-12 16:46:00 +08003536 if (0 != exec_cmd("wl -i wlan0 rssi", lynq_cmd_ret, MAX_RET))
qs.xiongf1e48bb2023-03-28 13:38:22 +08003537 {
you.chenbf5d0ee2023-04-12 16:46:00 +08003538 RLOGE("[lynq_get_connect_ap_rssi] exec cmd [ wl -i wlan0 rssi ] fail");
you.chen87ff5172022-05-06 11:30:57 +08003539 return -1;
3540 }
you.chenfcea58c2022-06-06 17:18:18 +08003541 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongc28ae382022-10-11 15:47:14 +08003542/****** if got rssi is 0,means sta didn't connected any device****/
3543 if(*rssi == 0)
3544 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08003545 RLOGE("[lynq_get_connect_ap_rssi]sta didn't connected any ap device,please check connection\n");
you.chenbf5d0ee2023-04-12 16:46:00 +08003546 return -1;
qs.xiongc28ae382022-10-11 15:47:14 +08003547 }
you.chen87ff5172022-05-06 11:30:57 +08003548
3549 return 0;
3550}
3551
3552int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
3553{
qs.xiongf1e48bb2023-03-28 13:38:22 +08003554 RLOGD("enter lynq_get_connect_ap_band\n");
3555 if (band == NULL)
3556 {
you.chen87ff5172022-05-06 11:30:57 +08003557 return -1;
3558 }
3559
3560 CHECK_IDX(idx, CTRL_STA);
3561 ap_info_s ap;
3562 ap.band = -1;
3563
qs.xiongf1e48bb2023-03-28 13:38:22 +08003564 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0)
3565 {
you.chen87ff5172022-05-06 11:30:57 +08003566 return -1;
3567 }
3568 *band = ap.band;
3569
3570 return 0;
qs.xiong99b48d62022-04-07 05:41:29 -04003571}
you.chendad3f9f2022-06-21 16:53:48 +08003572
3573int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
3574{
qs.xiongc719c3f2023-02-28 18:22:49 +08003575 char bssid[1024] = {0};
you.chendad3f9f2022-06-21 16:53:48 +08003576
3577 if (ip == NULL)
3578 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08003579 RLOGE("[lynq_get_connect_ap_ip]invalid param ptr ip,input ptr is NULL\n");
you.chendad3f9f2022-06-21 16:53:48 +08003580 return -1;
3581 }
3582
3583 CHECK_IDX(idx, CTRL_STA);
3584
qs.xiongc719c3f2023-02-28 18:22:49 +08003585 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
you.chendad3f9f2022-06-21 16:53:48 +08003586 {
3587 return -1;
3588 }
qs.xiongc719c3f2023-02-28 18:22:49 +08003589
3590 return inner_get_ip_by_mac(bssid, ip, 32); //better input by user
you.chendad3f9f2022-06-21 16:53:48 +08003591}
3592
qs.xiongd5c6fd32022-10-17 11:15:45 +08003593int lynq_ap_connect_num(int sta_number)
3594{
3595 char lynq_limit_cmd[32]={0};
3596 int ret;
qs.xiongf1e48bb2023-03-28 13:38:22 +08003597 if((sta_number < 1 ) && (sta_number > 15))
3598 {
3599 RLOGE("sta_number: not in range\n",sta_number);
qs.xiongd5c6fd32022-10-17 11:15:45 +08003600 return -1;
3601 }
3602 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
3603 ret = system(lynq_limit_cmd);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003604 if(ret != 0)
3605 {
3606 RLOGE("cmd of limit ap devices number error\n");
qs.xiongd5c6fd32022-10-17 11:15:45 +08003607 }
3608 return 0;
3609}
you.chendad3f9f2022-06-21 16:53:48 +08003610
qs.xiong330f5a12022-10-17 11:19:57 +08003611int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
3612{
3613
3614 char lynq_wifi_acs_cmd[128]={0};
3615 char lynq_cmd_mode[128]={0};
3616 char lynq_cmd_slect[128]={0};
3617
qs.xiongf1e48bb2023-03-28 13:38:22 +08003618 if((acs_mode != 2) && (acs_mode != 5))
3619 {
qs.xiong330f5a12022-10-17 11:19:57 +08003620 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
3621 }
3622
qs.xiongf1e48bb2023-03-28 13:38:22 +08003623 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
3624 {
qs.xiong330f5a12022-10-17 11:19:57 +08003625 return -1;
3626 }
3627
3628 CHECK_IDX(idx, CTRL_AP);
3629
3630 CHECK_WPA_CTRL(CTRL_AP);
3631
3632 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
3633 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
3634 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
3635
3636 DO_OK_FAIL_REQUEST(cmd_disconnect);
3637 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
3638 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
3639 DO_OK_FAIL_REQUEST(cmd_save_config);
3640 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
3641
3642 return 0;
3643}
you.chen2ef1c552022-11-07 18:31:14 +08003644//you.chen add for tv-box start
3645static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
3646 FILE *fp;
3647 //printf("to exec cmd:%s\n", str_cmd);
3648 if((fp=popen(str_cmd,"r"))==NULL)
3649 {
3650 perror("popen error!");
3651 return -1;
3652 }
3653 if((fread(str_cmd_ret,max_len,1,fp))<0)
3654 {
3655 perror("fread fail!");
3656 fclose(fp);
3657 return -1;
3658 }
3659 fclose(fp);
3660 return 0;
3661}
3662
3663static int get_netmask_length(const char* mask)
3664{
3665 int masklen=0, i=0;
3666 int netmask=0;
3667
3668 if(mask == NULL)
3669 {
3670 return 0;
3671 }
3672
3673 struct in_addr ip_addr;
3674 if( inet_aton(mask, &ip_addr) )
3675 {
3676 netmask = ntohl(ip_addr.s_addr);
qs.xiongf1e48bb2023-03-28 13:38:22 +08003677 }else
3678 {
you.chen2ef1c552022-11-07 18:31:14 +08003679 netmask = 0;
3680 return 0;
3681 }
3682
3683 while(0 == (netmask & 0x01) && i<32)
3684 {
3685 i++;
3686 netmask = netmask>>1;
3687 }
3688 masklen = 32-i;
3689 return masklen;
3690}
3691
3692static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
3693 int mask_len;
3694 char *p;
3695 char tmp[64] = {0};
you.chencd882682023-04-24 15:39:37 +08003696 sprintf(tmp, "ifconfig %s | grep Mask", s_ap_iterface_name);
3697 if (exec_cmd(tmp, str_cmd_ret, max_len) != 0)
you.chen2ef1c552022-11-07 18:31:14 +08003698 return -1;
3699 p = strstr(str_cmd_ret, "Mask:");
3700 if (p == NULL)
3701 return -1;
3702 mask_len = get_netmask_length(p + 5);
3703 if (mask_len == 0)
3704 return -1;
3705 p = strstr(str_cmd_ret, "inet addr:");
3706 if (p == NULL)
3707 return -1;
3708 strcpy(tmp, p + 10);
3709 p = strstr(tmp, " ");
3710 if (p != NULL)
3711 *p = '\0';
3712 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
3713 return 0;
3714}
3715
3716static void GBWWatchThreadProc() {
3717 int i,n, nloop, nmax, ncheckcount, nidlecount;
3718 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
3719 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
3720 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
3721 char *results[16] = {0};
3722 char str_cmd[256] = {0};
3723 char str_cmd_ret[128] = {0};
3724 char dest_ip[32] = {0};
3725 lastAP1Bytes = lastAP2Bytes = 0;
3726 lastAP1Drop = lastAP2Drop = 0;
3727 lastAP1Speed = lastAP2Speed = 0;
3728 setAP1Speed = 50;
3729 setAP2Speed = 80;
3730 nloop = 0;
3731 nmax = 6;
3732 ncheckcount = nidlecount = 0;
3733
you.chencd882682023-04-24 15:39:37 +08003734 if (s_ap_iterface_name[0] == '\0')
3735 {
3736 RLOGE("------gbw thread run\n");
3737 return;
3738 }
3739
qs.xiongf1e48bb2023-03-28 13:38:22 +08003740 RLOGD("------gbw thread run\n");
you.chen2ef1c552022-11-07 18:31:14 +08003741 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
3742 while (dest_ip[0] == '\0') {
3743 sleep(1);
3744 str_cmd_ret[0] = '\0';
3745 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
3746 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
3747 if (str_cmd_ret[n] == '\n'){
3748 str_cmd_ret[n] = '\0';
3749 break;
3750 }
3751 }
3752 if (str_cmd_ret[0] != '\0')
3753 {
3754 strcpy(dest_ip, str_cmd_ret);
3755 }
3756 }
3757
you.chencd882682023-04-24 15:39:37 +08003758 system_call_v("tc qdisc del dev %s root > /dev/null 2>&1", s_ap_iterface_name);
3759 system_call_v("tc qdisc add dev %s root handle 1: htb r2q 1", s_ap_iterface_name);
3760 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.chen2ef1c552022-11-07 18:31:14 +08003761 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
3762 {
qs.xiongf1e48bb2023-03-28 13:38:22 +08003763 RLOGD("not get tether info\n");
you.chen2ef1c552022-11-07 18:31:14 +08003764 return;
3765 }
you.chencd882682023-04-24 15:39:37 +08003766 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);
3767 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);
3768 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.chen2ef1c552022-11-07 18:31:14 +08003769
3770 while (1) {
3771 sleep(1);
3772 memset(str_cmd, 0, sizeof(str_cmd));
you.chencd882682023-04-24 15:39:37 +08003773 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
3774 sprintf(str_cmd, "tc -s class show dev %s classid 1:1 | grep Sent", s_ap_iterface_name);
3775 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen2ef1c552022-11-07 18:31:14 +08003776 continue;
3777 //printf("ap1 --- %s\n", str_cmd);
you.chencd882682023-04-24 15:39:37 +08003778 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen2ef1c552022-11-07 18:31:14 +08003779 if (n > 9) {
3780 if (strcmp(results[1], "Sent") == 0) {
3781 currAP1Bytes = atoll(results[2]);
3782 }
3783 if (strcmp(results[6], "(dropped") == 0) {
3784 currAP1Drop = atoi(results[7]);
3785 }
3786 }
3787
3788 memset(str_cmd, 0, sizeof(str_cmd));
you.chencd882682023-04-24 15:39:37 +08003789 memset(str_cmd_ret, 0, sizeof(str_cmd_ret));
3790 sprintf(str_cmd, "tc -s class show dev %s classid 1:2 | grep Sent", s_ap_iterface_name);
3791 if (0 != exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)))
you.chen2ef1c552022-11-07 18:31:14 +08003792 continue;
3793 //printf("ap2 --- %s\n", str_cmd);
you.chencd882682023-04-24 15:39:37 +08003794 n = lynq_split(str_cmd_ret, strlen(str_cmd_ret), ' ', results);
you.chen2ef1c552022-11-07 18:31:14 +08003795 if (n > 9) {
3796 if (strcmp(results[1], "Sent") == 0) {
3797 currAP2Bytes = atoll(results[2]);
3798 }
3799 if (strcmp(results[6], "(dropped") == 0) {
3800 currAP2Drop = atoi(results[7]);
3801 }
3802 }
3803
3804 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
3805 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
3806 lastAP1Bytes = currAP1Bytes;
3807 lastAP2Bytes = currAP2Bytes;
3808 continue;
3809 }
3810
3811 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
3812 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
3813 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
3814 lastAP1Speed = currAP1Speed;
3815 lastAP2Speed = currAP2Speed;
3816 lastAP1Bytes = currAP1Bytes;
3817 lastAP2Bytes = currAP2Bytes;
3818
3819 currSetAP1Speed = setAP1Speed;
3820 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
3821 ncheckcount++;
3822 if (ncheckcount > 3) {
3823 ncheckcount = 0;
3824 currSetAP1Speed = 5;
3825 }
3826 }
3827 else {
3828 ncheckcount = 0;
3829 if (currAP1Speed < 5)
3830 nidlecount++;
3831 else
3832 nidlecount = 0;
3833
3834 }
3835
3836 if (nidlecount > 60 ){
3837 currSetAP1Speed = 50;
3838 }
3839
3840 if (currSetAP1Speed != setAP1Speed) {
3841 setAP1Speed = currSetAP1Speed;
you.chencd882682023-04-24 15:39:37 +08003842 system_call_v(str_cmd, "tc class replace dev %s parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000",
3843 s_ap_iterface_name, setAP1Speed, (int)(setAP1Speed*1.4));
you.chen2ef1c552022-11-07 18:31:14 +08003844 }
3845 }
3846}
3847
3848int enableGBW(const char* mac) {
3849 int i,len;
3850 char get_ipaddr_cmd[128]={0};
3851 ap_info_s *ap;
3852 device_info_s * list;
3853
3854 if (mac == NULL || g_gbw_enabled == 1)
3855 return -1;
3856 len = strlen(mac);
3857 g_gbw_mac = malloc(len + 1);
3858 for(i=0;i<len;i++) {
3859 if (mac[i] >= 'A' && mac[i] <= 'Z')
3860 {
3861 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
3862 }
3863 else
3864 g_gbw_mac[i] = mac[i];
3865 }
3866 g_gbw_mac[i] = '\0';
3867 g_gbw_enabled = 1;
3868
3869 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
3870 if (system(get_ipaddr_cmd) == 0) {
3871 //startGBW();
3872 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
3873 for (i=0;i<len;i++) {
3874 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
3875 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
3876 startGBW();
3877 }
3878 free(ap);
3879 free(list);
3880 }
3881 }
3882 return 0;
3883}
3884
3885int disableGBW() {
3886 stopGBW();
3887 free(g_gbw_mac);
3888 g_gbw_mac = NULL;
3889 g_gbw_enabled = 1;
3890 return 0;
3891}
3892
3893static int startGBW() {
3894 if (g_gbw_watcher_pid != 0) {
3895 stopGBW();
3896 }
3897 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
3898}
3899
3900static int stopGBW() {
3901 void* retval;
you.chencd882682023-04-24 15:39:37 +08003902 char cmd[64] = {0};
you.chen2ef1c552022-11-07 18:31:14 +08003903 pthread_cancel(g_gbw_watcher_pid);
3904 pthread_join(g_gbw_watcher_pid, &retval);
3905 g_gbw_watcher_pid = 0;
you.chencd882682023-04-24 15:39:37 +08003906 sprintf(cmd, "%s %d", get_interface_name_script, LYNQ_WIFI_INTERFACE_1);
3907 if (s_ap_iterface_name[0] != '\0')
3908 {
3909 sprintf(cmd, "tc qdisc del dev %s root", s_ap_iterface_name);
3910 system(cmd);
3911 }
you.chen2ef1c552022-11-07 18:31:14 +08003912}
3913//you.chen add for tv-box end