blob: 128a913f54911c05f0996c9fd0e61e5dcdb5fe0f [file] [log] [blame]
qs.xiong1af5daf2022-03-14 09:12:12 -04001/**@File lib_wifi6.c
2* @Brief :about function test
3* @details :
you.chen35020192022-05-06 11:30:57 +08004* @Author : you.chen
5* @Date : 2022-4-6
qs.xiong1af5daf2022-03-14 09:12:12 -04006* @Version : V1.0
7* @copy ritght : Copyright (c) MobileTek
8*/
9#include <log/log.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050010#include <stdio.h>
11#include <sys/types.h>
you.chen35020192022-05-06 11:30:57 +080012#include <stdlib.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050013#include <string.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050014#include "libwifi6.h"
you.chen35020192022-05-06 11:30:57 +080015#include <wpa_ctrl.h>
16#include <string.h>
17#include <time.h>
18#include <pthread.h>
19#include <sys/socket.h>
20#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <netdb.h>
23#include <ifaddrs.h>
qs.xiong7a105ce2022-03-02 09:43:11 -050024
qs.xiong1af5daf2022-03-14 09:12:12 -040025#ifdef __cplusplus
26extern "C" {
27#endif
28#ifdef __cplusplus
29}
30#endif
you.chen35020192022-05-06 11:30:57 +080031
32#define MAX_CMD 128
33#define MAX_RET 4096
qs.xiongf1b525b2022-03-31 00:58:23 -040034#define MODE_LEN 10
you.chen35020192022-05-06 11:30:57 +080035#define CTRL_STA 0
36#define CTRL_AP 1
37#define AP_NETWORK_0 0
38
39pthread_t g_ap_watcher_pid = 0;
40volatile int g_ap_watcher_stop_flag = 0;
you.chena6fa5b22022-05-18 10:28:19 +080041volatile int g_ap_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080042
43pthread_t g_sta_watcher_pid = 0;
44volatile int g_sta_watcher_stop_flag = 0;
you.chen9ac66392022-08-06 17:01:16 +080045volatile int g_sta_scan_finish_flag = 1;
you.chena6fa5b22022-05-18 10:28:19 +080046volatile int g_sta_watcher_started_flag = 0;
you.chen35020192022-05-06 11:30:57 +080047
48void * g_ap_callback_priv = NULL;
49AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL;
50void * g_sta_callback_priv = NULL;
51STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL;
52
53//const char * CTRL_PATH="/var/run/wpa_supplicant";
54const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/ap0"};
55//const char * CTRL_PATH[2] = {"/var/run/wpa_supplicant/wlan0", "/var/run/wpa_supplicant/wlan0"};
56const char * cmd_list_networks = "LIST_NETWORKS";
57const char * cmd_save_config = "SAVE_CONFIG";
you.chen6c2dd9c2022-05-16 17:55:28 +080058const char * cmd_disconnect = "DISCONNECT";
59const char * cmd_remove_all = "REMOVE_NETWORK all";
you.chen35020192022-05-06 11:30:57 +080060const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS";
you.chen9ac66392022-08-06 17:01:16 +080061const char * STATE_COMPLETED = "COMPLETED";
you.chen35020192022-05-06 11:30:57 +080062
qs.xiongfdbc58e2022-09-29 11:37:32 +080063static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0};
you.chen35020192022-05-06 11:30:57 +080064
you.chen0f5c6432022-11-07 18:31:14 +080065//you.chen add for tv-box start
66volatile int g_gbw_enabled = 0;
67char * g_gbw_mac = NULL;
68pthread_t g_gbw_watcher_pid = 0;
69static int startGBW();
70static int stopGBW();
71//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +080072
73typedef struct __curr_status_info {
74 ap_info_s *ap;
75 char * state;
76 int net_no;
77}curr_status_info;
qs.xiong97fa59b2022-04-07 05:41:29 -040078
79#define PRINT_AND_RETURN_VALUE(str,value) \
qs.xiong97fa59b2022-04-07 05:41:29 -040080{\
you.chen35020192022-05-06 11:30:57 +080081 perror((str));\
82 return (value);\
qs.xiong97fa59b2022-04-07 05:41:29 -040083}
84
you.chen35020192022-05-06 11:30:57 +080085#define CHECK_IDX(idx, type) do { \
86 if ( (idx == LYNQ_WIFI_INTERFACE_0 && type != CTRL_STA) || (idx == LYNQ_WIFI_INTERFACE_1 && type != CTRL_AP) \
87 || idx < LYNQ_WIFI_INTERFACE_0 || idx > LYNQ_WIFI_INTERFACE_1 ) { \
88 printf("not support create [%s] on interface [%d]\n", (type == CTRL_STA ? "station" : "ap"), idx); \
89 return -1; \
90 } \
91 }while (0)
92
93#define CHECK_WPA_CTRL(index) int ret = 0;\
94 size_t reply_len = MAX_RET; \
95 char cmd_reply[MAX_RET]={0}; \
qs.xiongfdbc58e2022-09-29 11:37:32 +080096 struct wpa_ctrl *lynq_wpa_ctrl = NULL; \
you.chen35020192022-05-06 11:30:57 +080097 do{ \
qs.xiongfdbc58e2022-09-29 11:37:32 +080098 if (NULL == g_lynq_wpa_ctrl[index]) { \
99 g_lynq_wpa_ctrl[index] = wpa_ctrl_open(CTRL_PATH[index]); \
100 if (NULL == g_lynq_wpa_ctrl[index]) { \
101 printf("wpa_ctrl_open fail\n"); \
102 return -1; \
103 } \
104 } \
105 lynq_wpa_ctrl = g_lynq_wpa_ctrl[index]; \
you.chen35020192022-05-06 11:30:57 +0800106 }while(0)
107
108#define DO_REQUEST(cmd_str) do { \
109 reply_len = MAX_RET;\
110 cmd_reply[0] = '\0'; \
qs.xiongfdbc58e2022-09-29 11:37:32 +0800111 printf("to call [%s]\n", cmd_str); \
112 ret = wpa_ctrl_request(lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); \
you.chen35020192022-05-06 11:30:57 +0800113 if (ret != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800114 printf("call "#cmd_str" fail %d\n", ret); \
you.chen35020192022-05-06 11:30:57 +0800115 return ret; \
116 } \
117 cmd_reply[reply_len+1] = '\0'; \
118 printf("cmd replay [ %s ]\n", cmd_reply); \
119 }while(0)
120
121#define DO_OK_FAIL_REQUEST(cmd_str) do { \
122 DO_REQUEST(cmd_str); \
123 if (reply_len >=4 && memcmp(cmd_reply, "FAIL", 4) == 0 ) {\
you.chena6cd55a2022-05-08 12:20:18 +0800124 printf("cmd "#cmd_str" return FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800125 return -1; \
126 } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \
you.chena6cd55a2022-05-08 12:20:18 +0800127 printf("cmd "#cmd_str" return not OK|FAIL\n"); \
you.chen35020192022-05-06 11:30:57 +0800128 return -1; \
129 } \
130 }while (0)
131
132
133
134static void APWatcherThreadProc() {
135 size_t len = MAX_RET;
136 char msg_notify[MAX_RET];
137
you.chen6c2dd9c2022-05-16 17:55:28 +0800138 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800139 g_ap_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800140
141 while (g_ap_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800142 if (lynq_wpa_ctrl == NULL) {
143 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_AP]); //@todo temp change
144 if (lynq_wpa_ctrl == NULL) {
145 usleep(100*1000);
146 continue;
147 }
148
149 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800150 g_ap_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800151 }
152
you.chen35020192022-05-06 11:30:57 +0800153 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
154 usleep(100*1000);
155 continue;
156 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800157 memset(msg_notify, 0, MAX_RET);
158 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800159 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
160 msg_notify[len+1] = '\0';
161 printf("ap------> %s\n", msg_notify);
you.chen0f5c6432022-11-07 18:31:14 +0800162//you.chen change for tv-box start
you.chen35020192022-05-06 11:30:57 +0800163 if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) {
you.chen0f5c6432022-11-07 18:31:14 +0800164 if (g_ap_callback_func != NULL)
165 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_DISCONNECT);
166 if (g_gbw_enabled == 1 && g_gbw_mac != NULL) {
167 printf("disconect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
168 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) {
169 stopGBW();
170 }
171 }
you.chen35020192022-05-06 11:30:57 +0800172 }
173 else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) {
you.chen0f5c6432022-11-07 18:31:14 +0800174 if (g_ap_callback_func != NULL)
175 g_ap_callback_func(g_ap_callback_priv, LYNQ_WIFI_STATUS_CONNECT);
176 if (g_gbw_enabled == 1 && g_gbw_mac != NULL) {
177 printf("conect %d, %s ,%s\n", g_gbw_enabled, g_gbw_mac, strstr(msg_notify, (const char*)g_gbw_mac));
178 if (strstr(msg_notify, (const char*)g_gbw_mac) != NULL) {
179 startGBW();
180 }
181 }
you.chen35020192022-05-06 11:30:57 +0800182 }
you.chen0f5c6432022-11-07 18:31:14 +0800183//you.chen add for tv-box end
you.chen35020192022-05-06 11:30:57 +0800184 } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len))
185 } // end while (g_ap_watcher_stop_flag == 0)
you.chen92fd5d32022-05-25 10:09:47 +0800186 if (lynq_wpa_ctrl != NULL) {
187 wpa_ctrl_detach(lynq_wpa_ctrl);
188 wpa_ctrl_close(lynq_wpa_ctrl);
189 }
qs.xiong97fa59b2022-04-07 05:41:29 -0400190}
191
you.chen35020192022-05-06 11:30:57 +0800192static void STAWatcherThreadProc() {
193 size_t len = MAX_RET;
194 char msg_notify[MAX_RET];
195 char *pReason;
196 error_number_s error;
qs.xiongf1b525b2022-03-31 00:58:23 -0400197
you.chen6c2dd9c2022-05-16 17:55:28 +0800198 struct wpa_ctrl *lynq_wpa_ctrl = NULL;
you.chen92fd5d32022-05-25 10:09:47 +0800199 g_sta_watcher_stop_flag = 0;
you.chen35020192022-05-06 11:30:57 +0800200
201 while (g_sta_watcher_stop_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800202 if (lynq_wpa_ctrl == NULL) {
203 lynq_wpa_ctrl = wpa_ctrl_open(CTRL_PATH[CTRL_STA]);
204 if (lynq_wpa_ctrl == NULL) {
205 usleep(100*1000);
206 continue;
207 }
208
209 wpa_ctrl_attach(lynq_wpa_ctrl);
you.chena6fa5b22022-05-18 10:28:19 +0800210 g_sta_watcher_started_flag = 1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800211 }
212
you.chen35020192022-05-06 11:30:57 +0800213 if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) {
214 usleep(100*1000);
215 continue;
216 }
you.chen6c2dd9c2022-05-16 17:55:28 +0800217 memset(msg_notify, 0, MAX_RET);
218 len = MAX_RET;
you.chen35020192022-05-06 11:30:57 +0800219 if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) {
220 msg_notify[len+1] = '\0';
221 printf("sta ------> %s\n", msg_notify);
222 if (strstr(msg_notify, state_scan_result) != NULL) {
223 g_sta_scan_finish_flag = 1;
224 }
225
226 if (g_sta_callback_func == NULL) {
227 continue;
228 }
229 error = -1;
230 if (strstr(msg_notify, "CTRL-EVENT-SCAN-RESULTS") != NULL) {
231 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_SCAN_RESULT, error);
232 }
233 else if (strstr(msg_notify, "CTRL-EVENT-CONNECTED") != NULL) {
234 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_CONNECT, error);
235 }
236 else if (strstr(msg_notify, "CTRL-EVENT-SSID-TEMP-DISABLED") != NULL) {
237 pReason = strstr(msg_notify, "reason=");
238 if (pReason != NULL) {
239 pReason += strlen("reason=");
you.chen92fd5d32022-05-25 10:09:47 +0800240 if (memcmp(pReason, "CONN_FAILED", 11) == 0) {
you.chen35020192022-05-06 11:30:57 +0800241 error = LYNQ_TIME_OUT;
242 }
you.chen92fd5d32022-05-25 10:09:47 +0800243 else if (memcmp(pReason, "WRONG_KEY", 9) == 0) {
you.chen35020192022-05-06 11:30:57 +0800244 error = LYNQ_PSW_ERROR;
245 }
246 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
247 }
248 }
249 else if (strstr(msg_notify, "CTRL-EVENT-DISCONNECTED") != NULL) {
250 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, error);
251 }
252 else if (strstr(msg_notify, "CTRL-EVENT-NETWORK-NOT-FOUND") != NULL) {
253 g_sta_callback_func(g_ap_callback_priv, LYNQ_WIFI_STA_STATUS_DISCONNECT, LYNQ_NOT_FIND_AP);
254 }
255 }
256 }
you.chen92fd5d32022-05-25 10:09:47 +0800257 if (lynq_wpa_ctrl != NULL) {
258 wpa_ctrl_detach(lynq_wpa_ctrl);
259 wpa_ctrl_close(lynq_wpa_ctrl);
260 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400261}
262
qs.xiong1af5daf2022-03-14 09:12:12 -0400263int lynq_wifi_enable(void)
264{
you.chen35020192022-05-06 11:30:57 +0800265 int ret = 0;
you.chen6c2dd9c2022-05-16 17:55:28 +0800266 int i;
you.chen35020192022-05-06 11:30:57 +0800267 const char * cmd_check_service =
268 "state=`systemctl is-active wg870_drv_insmod.service`\n"
269 "[ \"\"$state == \"active\" ] && exit 0\n"
270 "[ \"\"$state == \"inactive\" ] && systemctl start wg870_drv_insmod.service\n";
271// if (g_lynq_wpa_ctrl[0] != NULL && g_lynq_wpa_ctrl[1] != NULL) {
272// return 0;
273// }
qs.xiong1af5daf2022-03-14 09:12:12 -0400274
you.chen35020192022-05-06 11:30:57 +0800275 ret = system(cmd_check_service);
276 if (ret != 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800277 printf("service state %d\n", ret);
qs.xiongfdbc58e2022-09-29 11:37:32 +0800278 return -1;
you.chen35020192022-05-06 11:30:57 +0800279 }
lhfe8da902022-10-11 18:55:36 +0800280
you.chen6c2dd9c2022-05-16 17:55:28 +0800281 for (i=0; i<10; i++) {
you.chena6fa5b22022-05-18 10:28:19 +0800282 if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800283 break;
284 }
285 usleep(300*1000);
286 }
287
288 if (i >= 10) {
qs.xiongfdbc58e2022-09-29 11:37:32 +0800289 return -1;
you.chen6c2dd9c2022-05-16 17:55:28 +0800290 }
291
you.chen9f17e4d2022-06-06 17:18:18 +0800292 //@todo delete add temp check for socket avilable start (20220606)
293 for (i=0; i<60; i++)
294 {
295 if (system("netstat -an | grep -q DGRAM") == 0) {
296 break;
297 }
298 sleep(1);
299 }
300
301 if (i >= 60)
302 {
qs.xiongfdbc58e2022-09-29 11:37:32 +0800303 return -1;
you.chen9f17e4d2022-06-06 17:18:18 +0800304 }
305 //@todo delete add temp check for socket avilable end (20220606)
306
you.chena6fa5b22022-05-18 10:28:19 +0800307 if (0 != system("ifconfig | grep -q ap0")) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800308 system("connmanctl enable wifi");
you.chena6fa5b22022-05-18 10:28:19 +0800309 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800310 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800311 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0");
you.chena6fa5b22022-05-18 10:28:19 +0800312 usleep(300*1000);
you.chen6c2dd9c2022-05-16 17:55:28 +0800313 system("connmanctl tether wifi on lynq 1qaz@WSX#$%^");
you.chena6fa5b22022-05-18 10:28:19 +0800314 usleep(300*1000);
you.chen01bfac32022-06-07 10:36:00 +0800315 system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect");
you.chen6c2dd9c2022-05-16 17:55:28 +0800316 }
317
you.chen35020192022-05-06 11:30:57 +0800318 if (g_ap_watcher_pid == 0 ) {
319 ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL);
320 if(ret<0){
qs.xiongfdbc58e2022-09-29 11:37:32 +0800321 return -1;
you.chen35020192022-05-06 11:30:57 +0800322 }
323 }
324
325 if (g_sta_watcher_pid == 0 ) {
326 ret=pthread_create(&g_sta_watcher_pid,NULL,STAWatcherThreadProc,NULL);
327 if(ret<0){
qs.xiongfdbc58e2022-09-29 11:37:32 +0800328 return -1;
you.chen35020192022-05-06 11:30:57 +0800329 }
330 }
331
you.chena6fa5b22022-05-18 10:28:19 +0800332 for (i=0; i<10; i++) {
333 usleep(300*1000);
334 if (g_ap_watcher_started_flag == 1 && g_sta_watcher_started_flag == 1) {
335 break;
336 }
337 }
338
you.chen35020192022-05-06 11:30:57 +0800339 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500340}
341
qs.xiong1af5daf2022-03-14 09:12:12 -0400342int lynq_wifi_disable(void)
343{
you.chen35020192022-05-06 11:30:57 +0800344 g_ap_watcher_stop_flag = 1;
345 g_sta_watcher_stop_flag = 1;
346 if (g_ap_watcher_pid != 0)
347 pthread_join(g_ap_watcher_pid, NULL);
348 if (g_sta_watcher_pid != 0)
349 pthread_join(g_sta_watcher_pid, NULL);
350 if (g_lynq_wpa_ctrl[0] != NULL)
351 wpa_ctrl_close(g_lynq_wpa_ctrl[0]);
352 if (g_lynq_wpa_ctrl[1] != NULL)
353 wpa_ctrl_close(g_lynq_wpa_ctrl[1]);
354 g_ap_watcher_pid = 0;
355 g_sta_watcher_pid = 0;
356 g_lynq_wpa_ctrl[0] = NULL;
357 g_lynq_wpa_ctrl[1] = NULL;
358 system("systemctl stop wg870_drv_insmod.service");
qs.xiong7a105ce2022-03-02 09:43:11 -0500359 return 0;
360}
qs.xiong1af5daf2022-03-14 09:12:12 -0400361
you.chen35020192022-05-06 11:30:57 +0800362static int inner_get_param(int interface, int net_no, char* param_name, char * out_put) {
363
364 char lynq_cmd_get[128]={0};
365
366 if (out_put == NULL) {
367 printf("output ptr is null\n");
368 return -1;
369 }
370 if (param_name == NULL) {
371 printf("param ptr is null");
372 return -1;
373 }
374 if (param_name[0] == '\0') {
375 printf("param is empty");
376 return -1;
377 }
378
379 sprintf(lynq_cmd_get, "GET_NETWORK %d %s", net_no, param_name);
380
381 CHECK_WPA_CTRL(interface);
382
383 DO_REQUEST(lynq_cmd_get);
384
385 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
386 return -1;
387 }
388
you.chena6fa5b22022-05-18 10:28:19 +0800389// printf("reply len %d, %08x\n", reply_len, (int)out_put);
you.chen35020192022-05-06 11:30:57 +0800390 memcpy(out_put, cmd_reply, reply_len + 1);
391 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500392}
qs.xiong1af5daf2022-03-14 09:12:12 -0400393
you.chen35020192022-05-06 11:30:57 +0800394static int lynq_split(char * str, int len, char delimiter, char * results[]) {
395 int ret = 0;
396 char * end = str + len - 1;
397 results[ret++] = str;
398 while(str < end) {
399 if (*str == delimiter) {
400 *str++ = '\0';
401 results[ret++] = str;
402 continue;
403 }
404 str++;
405 }
406 if (*str == delimiter) {
407 *str = '\0';
408 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400409
you.chen35020192022-05-06 11:30:57 +0800410 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -0500411}
412
you.chen35020192022-05-06 11:30:57 +0800413static void trim_space(char * p, int count) {
414 char * begin = p;
415 p += count;
416 printf("%C-%C||\n", *begin, *p);
417 while (p >= begin ) {
418 if (*p == ' ') {
419 *p-- = '\0';
420 }
421 else {
422 break;
423 }
424 }
425}
426
427static int get_ip_mac_list(char * mac_list[128], char * ip_list[128]) {
428 FILE * fp;
429 int len, ret;
430 int count, count_words, index;
431 int mac_start, mac_end;
432 int ip_start, ip_end;
433 char *split_lines[128] = {0};
434 char *buff;
435 const char * ip_header = "IP address";
436 const char * mac_header = "HW address";
437 const char * zero_mac = "00:00:00:00:00:00";
438
439 fp = fopen("/proc/net/arp", "rb");
440 if (NULL == fp) {
441 printf("open file fail\n");
442 return -1;
443 }
444
445 buff = alloca(MAX_RET);
446 fseek(fp, 0, SEEK_SET);
447 len = fread(buff, 1, MAX_RET, fp);
448 fclose(fp);
449 if (len <= 0) {
450 printf("read file fail\n");
451 return -1;
452 }
453 printf("file : %s\n", buff);
454
455 count = lynq_split(buff, len, '\n', split_lines);
456 printf("----- %s\n", split_lines[0]);
457
458 mac_end = 0;
459 count_words = strlen(split_lines[0]);
460 if (strstr(split_lines[0], mac_header) != NULL) {
461 mac_start = strstr(split_lines[0], mac_header) - split_lines[0];
462 mac_end = mac_start + strlen(mac_header) + 1;
463 while (mac_end < count_words) {
464 if (split_lines[0][mac_end] != ' ') {
465 break;
466 }
467 mac_end++;
468 }
469 }
470
471 ip_end = 0;
472 if (strstr(split_lines[0], ip_header) != NULL) {
473 ip_start = strstr(split_lines[0], ip_header) - split_lines[0];
474 ip_end = ip_start + strlen(ip_header) + 1;
475 while (ip_end < count_words) {
476 if (split_lines[0][ip_end] != ' ') {
477 break;
478 }
479 ip_end++;
480 }
481 }
482
483 if (mac_end == 0 || ip_end == 0) {
484 return 0;
485 }
486
487 ret = 0;
488 for(index = 1;index < count; index++) {
489 if (memcmp(split_lines[index] + mac_start, zero_mac, strlen(zero_mac)) == 0) {
490 continue;
491 }
492 mac_list[ret] = malloc(mac_end - mac_start + 1);
493 ip_list[ret] = malloc(ip_end - ip_start + 1);
494 memcpy(mac_list[ret], split_lines[index] + mac_start, mac_end - mac_start);
495 memcpy(ip_list[ret], split_lines[index] + ip_start, ip_end - ip_start);
496 trim_space(mac_list[ret], mac_end - mac_start - 1);
497 trim_space(ip_list[ret], ip_end - ip_start - 1);
498 ret++;
499 }
500
501 return ret;
502}
503
you.chen9ac66392022-08-06 17:01:16 +0800504static void free_ip_mac_list_mem(char ** mac_list, int mac_cnt, char ** ip_list, int ip_cnt)
505{
506 int i;
507 if (mac_list != NULL && mac_cnt > 0) {
508 for(i = 0; i< mac_cnt; i++)
509 {
510 if (NULL != mac_list[i])
511 {
512 free(mac_list[i]);
513 mac_list[i] = NULL;
514 }
515 }
516 }
517 if (ip_list != NULL && ip_cnt > 0) {
518 for(i = 0; i< mac_cnt; i++)
519 {
520 if (NULL != ip_list[i])
521 {
522 free(ip_list[i]);
523 ip_list[i] = NULL;
524 }
525 }
526 }
527}
528
you.chen35020192022-05-06 11:30:57 +0800529static int get_hostname_by_ip(char *ip, char *hostname) {
530 struct in_addr addr ={0};
531 struct hostent *ht;
532
533 if (ip == NULL || *ip == '\0' || hostname == NULL) {
534 return -1;
535 }
536
537 if (inet_aton(ip, &addr) == 0) {
538 printf("---inet_aton fail\n");
539 return -1;
540 }
541
542 ht = gethostbyaddr(&addr, sizeof(struct in_addr), AF_INET);
543
544 if (ht == NULL) {
545 printf("---gethostbyaddr fail\n");
546 herror(NULL);
547 return -1;
548 }
549
550 strcpy(hostname, ht->h_name);
551
552 return 0;
553}
554
555static int lynq_get_network_number_list(lynq_wifi_index_e idx, int ap_sta, int net_no_list[], char * ssid)
556{
557 int count, index, words_count;
558 char * split_lines[128]= {0};
559 char * split_words[128] = {0};
560 const char *lynq_wifi_list_networks = "LIST_NETWORKS";
561
562 CHECK_WPA_CTRL(ap_sta);
563
564 DO_REQUEST(lynq_wifi_list_networks);
565
566 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
567
568 //@todo check ssid field to compatible
569
570 ret = 0;
571 for(index=1; index < count; index++) {
572 words_count = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
573 if (words_count > 2) {
574 if (ssid == NULL || strcmp(split_words[1], ssid) == 0) {
575 net_no_list[ret++] = atoi(split_words[0]);
576 }
577 }
578 }
579
580 return ret;
581}
582
583static int lynq_add_network(int ap_sta) {
you.chen6c2dd9c2022-05-16 17:55:28 +0800584 size_t i=0;
you.chen35020192022-05-06 11:30:57 +0800585 CHECK_WPA_CTRL(ap_sta);
586 const char *lynq_wifi_add_network = "ADD_NETWORK";
587
588 DO_REQUEST(lynq_wifi_add_network);
you.chena6cd55a2022-05-08 12:20:18 +0800589 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
you.chen35020192022-05-06 11:30:57 +0800590 return -1;
591 }
592
you.chen6c2dd9c2022-05-16 17:55:28 +0800593 for(i=0;i<reply_len;i++) {
you.chen35020192022-05-06 11:30:57 +0800594 if(cmd_reply[i] == '\n') {
595 cmd_reply[i] = '\0';
596 break;
597 }
598 }
599 return atoi(cmd_reply);
600}
you.chena6cd55a2022-05-08 12:20:18 +0800601
you.chen35020192022-05-06 11:30:57 +0800602static int lynq_check_network_number(lynq_wifi_index_e idx, int ap_sta, int net_no)
603{
604 int count, index;
605 int net_no_list[128];
606
607
608 count = lynq_get_network_number_list(idx, ap_sta, net_no_list, NULL);
609 for (index=0; index < count; index++) {
610 if (net_no_list[index] == net_no) {
611 return 0;
612 }
613 }
614
615 if (count >= 1)
616 index = net_no_list[count - 1];
617 else
618 index = -1;
619
620 while (index < net_no ) {
621 index = lynq_add_network(ap_sta);
622 if (index >= net_no) { // required network no created
623 return 0;
624 }
you.chena6cd55a2022-05-08 12:20:18 +0800625 else if( index < 0) {
626 printf("add network fail\n");
627 return -1;
628 }
you.chen35020192022-05-06 11:30:57 +0800629 }
630
631 if (index < 0)
632 return -1;
633
634 return 0;
635}
636
637static lynq_wifi_band_m convert_band_from_freq(int freq) { //@todo
638 if (freq > 5000 && freq < 6000) {
639 return LYNQ_WIFI_5G_band;
640 }
641 else if (freq > 2000 && freq < 3000) {
642 return LYNQ_WIFI_2G_band;
643 }
644 return LYNQ_WIFI_2_and_5G_band;
645}
646
647static lynq_wifi_auth_s convert_auth_from_key_mgmt(char * key_mgmt) {
648 if (key_mgmt != NULL) {
649 if (memcmp( key_mgmt, "NONE", 4) == 0) {
650 return LYNQ_WIFI_AUTH_OPEN;
651 }
652 else if (memcmp( key_mgmt, "WEP", 3) == 0){
653 return LYNQ_WIFI_AUTH_WEP;
654 }
655 else if (memcmp( key_mgmt, "WPA-PSK", 7) == 0){
656 return LYNQ_WIFI_AUTH_WPA_PSK;
657 }
658 else if (memcmp( key_mgmt, "WPA2-PSK", 8) == 0){
659 return LYNQ_WIFI_AUTH_WPA2_PSK;
660 }
661 }
662
663 return -1;
664}
665
666static lynq_wifi_auth_s convert_max_auth_from_flag(char * flag) {
667 if (flag != NULL) {
668 if (strstr( flag, "WPA2-PSK") != NULL){
669 return LYNQ_WIFI_AUTH_WPA2_PSK;
670 }
671 else if (strstr( flag, "WPA-PSK") != NULL){
672 return LYNQ_WIFI_AUTH_WPA_PSK;
673 }
674 else if (strstr( flag, "WEP") != NULL){
675 return LYNQ_WIFI_AUTH_WEP;
676 }
677 else if (strstr( flag, "NONE") != NULL) {
678 return LYNQ_WIFI_AUTH_OPEN;
679 }
680 }
681
682 return -1;
683}
684
685static lynq_wifi_bandwidth_type_m convert_bandwidth_from_bw(int bw) {
686 switch (bw) {
687 case 10:
688 return LYNQ_WIFI_BANDWIDTH_HT10;
689 break;
690 case 20:
691 return LYNQ_WIFI_BANDWIDTH_HT20;
692 break;
693 case 40:
694 return LYNQ_WIFI_BANDWIDTH_HT40;
695 break;
696 case 80:
697 return LYNQ_WIFI_BANDWIDTH_HT80;
698 break;
699 default:
700 break;
701 }
702
703 return -1;
704}
705
706static int inner_get_status_info(int interface, curr_status_info *curr_state) {
707 int i, count;
708 char *p;
709 const char *lynq_status_cmd = "STATUS";
710 const char * FLAG_SSID = "ssid=";
711 const char * FLAG_SBSID = "bssid=";
712 const char * FLAG_KEY_MGMT = "key_mgmt=";
713 const char * FLAG_FREQ = "freq=";
714 const char * FLAG_STATE = "wpa_state=";
715 const char * FLAG_ID = "id=";
716 char *split_lines[128] = {0};
717
718 CHECK_WPA_CTRL(interface);
719
720 if (curr_state == NULL) {
721 return -1;
722 }
723
724 DO_REQUEST(lynq_status_cmd);
725
726 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
727
728 curr_state->net_no = -1;
729 ret = -1;
730 for(i=0; i < count; i++) {
731 if (curr_state->ap != NULL) {
you.chen35020192022-05-06 11:30:57 +0800732 p = strstr(split_lines[i], FLAG_SBSID);
733 if (p != NULL) {
734 strcpy(curr_state->ap->ap_mac, p + strlen(FLAG_SBSID));
735 ret = 0;
736 continue;
737 }
you.chenf58b3c92022-06-21 16:53:48 +0800738 p = strstr(split_lines[i], FLAG_SSID);
739 if (p != NULL) {
740 strcpy(curr_state->ap->ap_ssid, p + strlen(FLAG_SSID));
741 ret = 0;
742 continue;
743 }
you.chen35020192022-05-06 11:30:57 +0800744 p = strstr(split_lines[i], FLAG_KEY_MGMT);
745 if (p != NULL) {
you.chen450d0172022-07-15 17:56:48 +0800746 curr_state->ap->auth = convert_auth_from_key_mgmt(p + strlen(FLAG_KEY_MGMT));
747 printf("key_mgmt %d, -- %s\n", curr_state->ap->auth, p);
you.chen35020192022-05-06 11:30:57 +0800748 ret = 0;
749 continue;
750 }
751 p = strstr(split_lines[i], FLAG_FREQ);
752 if (p != NULL) {
753 curr_state->ap->band = convert_band_from_freq(atoi( p + strlen(FLAG_FREQ)));
754 ret = 0;
755 continue;
756 }
757 } // end if (ap != NULL)
758 if (curr_state->state != NULL) {
759 p = strstr(split_lines[i], FLAG_STATE);
760 if (p != NULL) {
761 strcpy(curr_state->state, p + strlen(FLAG_STATE));
762 ret = 0;
763 continue;
764 }
765
766 } //end else if (state != NULL)
you.chena6cd55a2022-05-08 12:20:18 +0800767 if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) {
you.chen35020192022-05-06 11:30:57 +0800768 ret = 0;
you.chen450d0172022-07-15 17:56:48 +0800769 curr_state->net_no = atoi(p + strlen(FLAG_ID));
you.chen35020192022-05-06 11:30:57 +0800770 printf("net_no %d, -- %s\n", curr_state->net_no, p);
771 }
772 }
773
774 return ret;
775}
776
777
qs.xiongf1b525b2022-03-31 00:58:23 -0400778int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500779{
you.chen35020192022-05-06 11:30:57 +0800780 char lynq_wifi_ssid_cmd[80]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500781
you.chen35020192022-05-06 11:30:57 +0800782 if (ap_ssid == NULL) {
783 printf("ap_ssid is null\n");
784 return -1;
785 }
786 else {
787 printf("idx:%d ap_ssid : %s\n", idx, ap_ssid);
788 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400789
you.chen35020192022-05-06 11:30:57 +0800790 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
791 return -1;
792 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400793
you.chen35020192022-05-06 11:30:57 +0800794 CHECK_IDX(idx, CTRL_AP);
795
796 CHECK_WPA_CTRL(CTRL_AP);
797
798 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", AP_NETWORK_0, ap_ssid);
799
800 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
801 DO_OK_FAIL_REQUEST(cmd_save_config);
802
qs.xiong7a105ce2022-03-02 09:43:11 -0500803 return 0;
you.chen35020192022-05-06 11:30:57 +0800804
qs.xiong7a105ce2022-03-02 09:43:11 -0500805}
806
you.chen35020192022-05-06 11:30:57 +0800807int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -0500808{
you.chen6c2dd9c2022-05-16 17:55:28 +0800809 int len;
you.chen35020192022-05-06 11:30:57 +0800810 CHECK_IDX(idx, CTRL_AP);
you.chen6c2dd9c2022-05-16 17:55:28 +0800811 if (0 != inner_get_param(CTRL_AP, AP_NETWORK_0, "ssid", ap_ssid))
812 return -1;
813 len = strlen(ap_ssid);
814 if (ap_ssid[0] == '\"') {
815 memmove(ap_ssid, ap_ssid + 1, len - 1);
816 len -= 1;
817 }
818 if (len > 0 && ap_ssid[len-1] == '\"') {
819 ap_ssid[len-1] = '\0';
820 }
821 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500822}
823
qs.xiongc9c79f72022-10-17 15:27:18 +0800824/*****
825 *frequency <------>channel
826 *
827 *frequency 1 2 3 4 5 6 7 8 9 10 11 12 13 36 40 44 48 149 153 157 161 165
828 *
829 *
830 *channel 2412,2417,2422,2427,2532,2437,2442,2447,2452,2457,2462,2467,2472,5180,5200,5220,5240,5745,5765,5785,5805,5825
831 *
832 *
833 * */
834static int lynq_check_set_frequency(int input_frequency){
qs.xiongc00b6032022-11-29 16:28:03 +0800835 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};
836 int i;
837 int arr_len = sizeof(legitimate_frequency) / sizeof(int);
838
qs.xiong69a332b2022-12-02 09:58:57 +0800839 for(i = 0; i < arr_len; i++)
qs.xiongc00b6032022-11-29 16:28:03 +0800840 {
841 if(input_frequency == legitimate_frequency[i])
qs.xiongc9c79f72022-10-17 15:27:18 +0800842 break;
qs.xiongc9c79f72022-10-17 15:27:18 +0800843 }
qs.xiongc00b6032022-11-29 16:28:03 +0800844
845 if(i == arr_len)
846 {
847 printf("input frequency is eero--->%d,please check it\n", input_frequency);
qs.xiongc9c79f72022-10-17 15:27:18 +0800848 return -1;
849 }
qs.xiongc00b6032022-11-29 16:28:03 +0800850
qs.xiongc9c79f72022-10-17 15:27:18 +0800851 return 0;
852}
qs.xiongf1b525b2022-03-31 00:58:23 -0400853int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500854{
qs.xiongc00b6032022-11-29 16:28:03 +0800855 int check;
qs.xiongc9c79f72022-10-17 15:27:18 +0800856 char lynq_wifi_frequency_cmd[128]={0};
857 char lynq_cmd_mode[128]={0};
you.chen35020192022-05-06 11:30:57 +0800858 char lynq_cmd_slect[128]={0};
qs.xiong7a105ce2022-03-02 09:43:11 -0500859
qs.xiongc9c79f72022-10-17 15:27:18 +0800860 //@do check input frequency
qs.xiongc00b6032022-11-29 16:28:03 +0800861 check = lynq_check_set_frequency(lynq_wifi_frequency);
862 if(check != 0)
863 {
864 printf("do check frequency error\n");
qs.xiongc9c79f72022-10-17 15:27:18 +0800865 return -1;
you.chen35020192022-05-06 11:30:57 +0800866 }
qs.xiongc00b6032022-11-29 16:28:03 +0800867 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0)
868 {
you.chen35020192022-05-06 11:30:57 +0800869 return -1;
870 }
qs.xiong1af5daf2022-03-14 09:12:12 -0400871
you.chen35020192022-05-06 11:30:57 +0800872 CHECK_IDX(idx, CTRL_AP);
873
874 CHECK_WPA_CTRL(CTRL_AP);
875
876 sprintf(lynq_wifi_frequency_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, lynq_wifi_frequency);
877 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
878 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
879
you.chen6c2dd9c2022-05-16 17:55:28 +0800880 DO_OK_FAIL_REQUEST(cmd_disconnect);
you.chen35020192022-05-06 11:30:57 +0800881 DO_OK_FAIL_REQUEST(lynq_wifi_frequency_cmd);
882 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
883 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -0500884
qs.xiong1af5daf2022-03-14 09:12:12 -0400885 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -0500886}
887
qs.xiongf1b525b2022-03-31 00:58:23 -0400888int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency)
qs.xiong7a105ce2022-03-02 09:43:11 -0500889{
you.chen35020192022-05-06 11:30:57 +0800890 char lynq_frequency_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -0400891
you.chen35020192022-05-06 11:30:57 +0800892 CHECK_IDX(idx, CTRL_AP);
qs.xiongf1b525b2022-03-31 00:58:23 -0400893
you.chen35020192022-05-06 11:30:57 +0800894 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "frequency", lynq_frequency_str) != 0) {
895 return -1;
896 }
897 *lynq_wifi_frequency = atoi(lynq_frequency_str);
qs.xiongf1b525b2022-03-31 00:58:23 -0400898
899 return 0;
900}
901
qs.xiongf1b525b2022-03-31 00:58:23 -0400902int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth)
903{
you.chen35020192022-05-06 11:30:57 +0800904 CHECK_IDX(idx, CTRL_AP);
905 switch(bandwidth){
906 case LYNQ_WIFI_BANDWIDTH_HT10:
907 {
908 printf("bandwith [%d] not support now\n", bandwidth);
909 return -1;
910 }
911 case LYNQ_WIFI_BANDWIDTH_HT20:
912 {
913 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 6";
914 system("wl down");
915 if (system(lynq_cmd_bandwith) != 0 ){
916 return -1;
917 }
918 system("wl up");
919 break;
920 }
921 case LYNQ_WIFI_BANDWIDTH_HT40:
922 {
923 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 153u";
924 sprintf(lynq_cmd_bandwith, "wl chanspec ");
925 system("wl down");
926 if (system(lynq_cmd_bandwith) != 0 ){
927 return -1;
928 }
929 system("wl up");
930 break;
931 }
932 case LYNQ_WIFI_BANDWIDTH_HT80:
933 {
934 char lynq_cmd_bandwith[MAX_CMD]="wl chanspec 36/80";
935 system("wl down");
936 if (system(lynq_cmd_bandwith) != 0 ){
937 return -1;
938 }
939 system("wl up");
940 break;
941 }
942 default:
943 {
944 printf("auth type [%d] not support now\n", bandwidth);
945 return -1;
946 }
947 }
qs.xiongf1b525b2022-03-31 00:58:23 -0400948
949
you.chen35020192022-05-06 11:30:57 +0800950 return 0;
qs.xiongf1b525b2022-03-31 00:58:23 -0400951}
you.chen35020192022-05-06 11:30:57 +0800952
qs.xiongf1b525b2022-03-31 00:58:23 -0400953int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth)
954{
you.chen35020192022-05-06 11:30:57 +0800955 int count = 0;
956 int index = 0;
957 char *split_words[128] = {0};
958 const char *lynq_chanspec_cmd = "DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -0400959
you.chen35020192022-05-06 11:30:57 +0800960 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -0500961
you.chen35020192022-05-06 11:30:57 +0800962 CHECK_WPA_CTRL(CTRL_AP);
963
964 DO_REQUEST(lynq_chanspec_cmd);
965
966 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
967 for(;index < count; index++) {
968 if (strncmp(split_words[index], "bw", 2) != 0) {
969 continue;
970 }
971
972 index++;
973 if (index >= count) {
974 return -1;
975 }
976
977 printf("bw %s\n", split_words[index]);
978 *bandwidth = convert_bandwidth_from_bw(atoi(split_words[index]));
979 return 0;
980 }
981
982 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -0500983}
qs.xiong0fb469a2022-04-14 03:50:45 -0400984
qs.xiongf1b525b2022-03-31 00:58:23 -0400985int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel)
qs.xiong7a105ce2022-03-02 09:43:11 -0500986{
you.chen35020192022-05-06 11:30:57 +0800987 char lynq_cmd_channel[MAX_CMD]={0};
qs.xiong1d58e9b2022-04-14 06:17:01 -0400988
you.chen35020192022-05-06 11:30:57 +0800989 CHECK_IDX(idx, CTRL_AP);
qs.xiong1d58e9b2022-04-14 06:17:01 -0400990
you.chen35020192022-05-06 11:30:57 +0800991 sprintf(lynq_cmd_channel, "wl channel %d", channel);
qs.xiong1af5daf2022-03-14 09:12:12 -0400992
you.chen35020192022-05-06 11:30:57 +0800993 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
994 return -1;
995 }
996
997 system("wl down");
998 if (system(lynq_cmd_channel) != 0 ){
999 return -1;
1000 }
1001 system("wl up");
1002 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001003}
qs.xiong0fb469a2022-04-14 03:50:45 -04001004
qs.xiongf1b525b2022-03-31 00:58:23 -04001005int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel)
qs.xiong7a105ce2022-03-02 09:43:11 -05001006{
you.chen35020192022-05-06 11:30:57 +08001007 int count = 0;
1008 int index = 0;
1009 char *split_words[128] = {0};
1010 char lynq_chanspec_cmd[]="DRIVER chanspec\n";
qs.xiongf1b525b2022-03-31 00:58:23 -04001011
you.chen35020192022-05-06 11:30:57 +08001012 CHECK_IDX(idx, CTRL_AP);
qs.xiong1af5daf2022-03-14 09:12:12 -04001013
you.chen35020192022-05-06 11:30:57 +08001014 CHECK_WPA_CTRL(CTRL_AP);
1015
1016 DO_REQUEST(lynq_chanspec_cmd);
1017
1018 count = lynq_split(cmd_reply, reply_len, ' ', split_words);
1019 for(;index < count; index++) {
1020 printf("---- %s\n",split_words[index]);
1021 if (strncmp(split_words[index], "channel", 2) != 0) {
1022 continue;
1023 }
1024
1025 index++;
1026 if (index >= count) {
1027 return -1;
1028 }
1029
1030 *channel = atoi(split_words[index]);
1031 return 0;
1032 }
1033
1034 return -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001035}
1036
1037
you.chen35020192022-05-06 11:30:57 +08001038int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001039{
you.chen6c2dd9c2022-05-16 17:55:28 +08001040 char ssid[MAX_CMD] = {0};
1041 int freq = 0;
1042 char lynq_auth_cmd[64]={0};
1043 char lynq_auth_alg_cmd[64]={0};
1044 char lynq_psk_cmd[64]={0};
1045 char lynq_pairwise_cmd[64]={0};
1046 lynq_wifi_auth_s org_auth;
you.chen35020192022-05-06 11:30:57 +08001047 CHECK_IDX(idx, CTRL_AP);
1048
you.chen6c2dd9c2022-05-16 17:55:28 +08001049 CHECK_WPA_CTRL(CTRL_AP);
1050
you.chen35020192022-05-06 11:30:57 +08001051 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) {
1052 return -1;
1053 }
1054
you.chen92fd5d32022-05-25 10:09:47 +08001055 if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001056 if (org_auth == auth) {
1057 return 0;
1058 }
1059 else {
1060 if (0 != lynq_wifi_ap_ssid_get(idx, ssid)) {
1061 ssid[0] = '\0';
1062 }
1063 lynq_wifi_ap_frequency_get(idx, &freq);
1064
1065 DO_OK_FAIL_REQUEST(cmd_disconnect);
1066 DO_OK_FAIL_REQUEST(cmd_remove_all);
1067 if (ssid[0] != '\0') {
1068 lynq_wifi_ap_ssid_set(idx, ssid);
1069 }
1070 if (freq != 0) {
1071 lynq_wifi_ap_frequency_set(idx, freq);
1072 }
1073 }
1074 }
you.chen35020192022-05-06 11:30:57 +08001075
qs.xiong7a105ce2022-03-02 09:43:11 -05001076 switch(auth){
qs.xiongf1b525b2022-03-31 00:58:23 -04001077 case LYNQ_WIFI_AUTH_OPEN:
you.chen6c2dd9c2022-05-16 17:55:28 +08001078 {
you.chen35020192022-05-06 11:30:57 +08001079 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001080 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001081
you.chen35020192022-05-06 11:30:57 +08001082 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001083 break;
1084 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001085 case LYNQ_WIFI_AUTH_WEP:
1086 {
1087 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0);
you.chen92fd5d32022-05-25 10:09:47 +08001088 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0);
you.chen6c2dd9c2022-05-16 17:55:28 +08001089 sprintf(lynq_auth_alg_cmd,"SET_NETWORK %d auth_alg SHARED", AP_NETWORK_0);
1090
1091 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1092 DO_OK_FAIL_REQUEST(lynq_auth_alg_cmd);
1093 break;
1094 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001095 case LYNQ_WIFI_AUTH_WPA_PSK:
you.chen35020192022-05-06 11:30:57 +08001096 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiong7a105ce2022-03-02 09:43:11 -05001097 {
you.chen35020192022-05-06 11:30:57 +08001098 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1099 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", AP_NETWORK_0);
1100 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1101 }
1102 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001103 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0);
you.chena6cd55a2022-05-08 12:20:18 +08001104 sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
you.chen35020192022-05-06 11:30:57 +08001105 }
1106// sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA2", AP_NETWORK_0);
1107// sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0);
1108 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", AP_NETWORK_0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001109
you.chen35020192022-05-06 11:30:57 +08001110 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1111 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1112 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001113 break;
you.chen35020192022-05-06 11:30:57 +08001114 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001115 default:
you.chen35020192022-05-06 11:30:57 +08001116 {
1117 printf("auth type [%d] not support now\n", auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001118 return -1;
you.chen35020192022-05-06 11:30:57 +08001119 }
1120 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001121 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong7a105ce2022-03-02 09:43:11 -05001122
qs.xiong7a105ce2022-03-02 09:43:11 -05001123 return 0;
1124}
1125
you.chen35020192022-05-06 11:30:57 +08001126int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001127{
you.chen35020192022-05-06 11:30:57 +08001128 char lynq_auth_str[MAX_RET] = {0};
you.chen6c2dd9c2022-05-16 17:55:28 +08001129 char lynq_auth_alg_str[MAX_RET] = {0};
1130 char lynq_proto_str[MAX_RET] = {0};
you.chen35020192022-05-06 11:30:57 +08001131
1132 CHECK_IDX(idx, CTRL_AP);
1133
1134 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "key_mgmt", lynq_auth_str) != 0) {
1135 return -1;
1136 }
1137
1138 if (memcmp( lynq_auth_str, "NONE", 4) == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001139 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "auth_alg", lynq_auth_alg_str) != 0) {
1140 *auth = LYNQ_WIFI_AUTH_OPEN;
1141 }
1142 else if (memcmp(lynq_auth_alg_str, "SHARED", 6) == 0){
1143 *auth = LYNQ_WIFI_AUTH_WEP;
1144 }
1145 else {
1146 *auth = LYNQ_WIFI_AUTH_OPEN;
1147 }
you.chen35020192022-05-06 11:30:57 +08001148 }
you.chen92fd5d32022-05-25 10:09:47 +08001149 else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) {
you.chena6fa5b22022-05-18 10:28:19 +08001150 if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) {
you.chen92fd5d32022-05-25 10:09:47 +08001151 *auth = -1;
you.chen6c2dd9c2022-05-16 17:55:28 +08001152 }
1153 else if (memcmp(lynq_proto_str, "RSN", 3) == 0) {
1154 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1155 }
1156 else {
1157 *auth = LYNQ_WIFI_AUTH_WPA_PSK;
1158 }
you.chen35020192022-05-06 11:30:57 +08001159 }
you.chen92fd5d32022-05-25 10:09:47 +08001160 else {
1161 *auth = -1;
1162 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001163
you.chen6c2dd9c2022-05-16 17:55:28 +08001164 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001165}
qs.xiong1af5daf2022-03-14 09:12:12 -04001166
qs.xiong1af5daf2022-03-14 09:12:12 -04001167
qs.xiongf1b525b2022-03-31 00:58:23 -04001168int lynq_wifi_ap_start(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001169{
you.chen35020192022-05-06 11:30:57 +08001170 char LYNQ_WIFI_CMD[128]={0};
1171 //const char *lynq_remove_all_cmd = "REMOVE_NETWORK all";
1172 //const char *lynq_reconfig_cmd = "RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
qs.xiongf1b525b2022-03-31 00:58:23 -04001173
you.chen35020192022-05-06 11:30:57 +08001174 CHECK_IDX(idx, CTRL_AP);
1175
1176 CHECK_WPA_CTRL(CTRL_AP);
1177
1178// system("connmanctl enable wifi");
1179// system("connmanctl tether wifi on cy-test 12345678");
1180// system("ifconfig wlan0 down");
1181// system("ifconfig wlan0 up");
1182// system("ifconfig wlan0 up");
1183
1184 //DO_OK_FAIL_REQUEST(lynq_remove_all_cmd);
1185 //DO_OK_FAIL_REQUEST(lynq_reconfig_cmd);
1186
1187 sprintf(LYNQ_WIFI_CMD,"SELECT_NETWORK %d",AP_NETWORK_0);
1188 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1189
qs.xiong7a105ce2022-03-02 09:43:11 -05001190 return 0;
1191}
1192
qs.xiongf1b525b2022-03-31 00:58:23 -04001193int lynq_wifi_ap_restart(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001194{
you.chen35020192022-05-06 11:30:57 +08001195 return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1;
qs.xiong7a105ce2022-03-02 09:43:11 -05001196}
1197
qs.xiongf1b525b2022-03-31 00:58:23 -04001198int lynq_wifi_ap_stop(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001199{
you.chen35020192022-05-06 11:30:57 +08001200 char LYNQ_WIFI_CMD[128]={0};
qs.xiong1af5daf2022-03-14 09:12:12 -04001201
you.chen35020192022-05-06 11:30:57 +08001202 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001203
you.chen35020192022-05-06 11:30:57 +08001204 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001205
you.chen35020192022-05-06 11:30:57 +08001206 sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0);
1207
1208 DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD);
1209
you.chenb4b121c2022-05-06 17:50:16 +08001210// system("connmanctl tether wifi off");
you.chen35020192022-05-06 11:30:57 +08001211
qs.xiong7a105ce2022-03-02 09:43:11 -05001212 return 0;
1213}
qs.xiong1af5daf2022-03-14 09:12:12 -04001214
qs.xiongf1b525b2022-03-31 00:58:23 -04001215int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001216{
you.chen35020192022-05-06 11:30:57 +08001217 char lynq_disable_cmd[128] = {0};
1218 char lynq_select_cmd[128] = {0};
1219 const char *lynq_hide_cmd = "SET HIDE_SSID 1";
qs.xiong97fa59b2022-04-07 05:41:29 -04001220
you.chen35020192022-05-06 11:30:57 +08001221 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001222
you.chen35020192022-05-06 11:30:57 +08001223 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001224
you.chen35020192022-05-06 11:30:57 +08001225 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1226 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1227
1228 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1229 DO_OK_FAIL_REQUEST(lynq_hide_cmd);
1230 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong1af5daf2022-03-14 09:12:12 -04001231
1232 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001233}
1234
qs.xiongf1b525b2022-03-31 00:58:23 -04001235int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx)
qs.xiong7a105ce2022-03-02 09:43:11 -05001236{
you.chen35020192022-05-06 11:30:57 +08001237 char lynq_disable_cmd[128] = {0};
1238 char lynq_select_cmd[128] = {0};
1239 const char *lynq_unhide_cmd = "SET HIDE_SSID 0";
qs.xiongf1b525b2022-03-31 00:58:23 -04001240
you.chen35020192022-05-06 11:30:57 +08001241 CHECK_IDX(idx, CTRL_AP);
qs.xiong7a105ce2022-03-02 09:43:11 -05001242
you.chen35020192022-05-06 11:30:57 +08001243 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001244
you.chen35020192022-05-06 11:30:57 +08001245 sprintf(lynq_disable_cmd,"DISABLE_NETWORK %d", AP_NETWORK_0);
1246 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", AP_NETWORK_0);
1247
1248 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1249 DO_OK_FAIL_REQUEST(lynq_unhide_cmd);
1250 DO_OK_FAIL_REQUEST(lynq_select_cmd);
qs.xiong7a105ce2022-03-02 09:43:11 -05001251
1252 return 0;
1253}
qs.xiongf1b525b2022-03-31 00:58:23 -04001254
you.chen35020192022-05-06 11:30:57 +08001255int lynq_ap_password_set(lynq_wifi_index_e idx,char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001256{
qs.xiongf1b525b2022-03-31 00:58:23 -04001257 int pass_len;
you.chen6c2dd9c2022-05-16 17:55:28 +08001258 char lynq_tmp_cmd[MAX_CMD] = {0};
1259 char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0};
qs.xionge7074322022-06-27 11:34:53 +08001260 if( password == NULL ){
1261 return -1;
1262 }
1263 pass_len=strlen(password);
you.chen6c2dd9c2022-05-16 17:55:28 +08001264 lynq_wifi_auth_s auth = -1;
you.chen35020192022-05-06 11:30:57 +08001265 if(pass_len < 8 || pass_len >= 64){
qs.xiongf1b525b2022-03-31 00:58:23 -04001266 return -1;
you.chen35020192022-05-06 11:30:57 +08001267 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001268
you.chen35020192022-05-06 11:30:57 +08001269 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001270
you.chen6c2dd9c2022-05-16 17:55:28 +08001271 if (0 != lynq_wifi_ap_auth_get(idx, &auth)) {
1272 return -1;
1273 }
1274 else if (auth == LYNQ_WIFI_AUTH_OPEN) {
1275 return -1;
1276 }
1277
you.chen35020192022-05-06 11:30:57 +08001278 CHECK_WPA_CTRL(CTRL_AP);
1279
you.chen6c2dd9c2022-05-16 17:55:28 +08001280 if (auth == LYNQ_WIFI_AUTH_WEP) {
1281 sprintf(lynq_tmp_cmd,"SET_NETWORK %d wep_key0 \"%s\"",AP_NETWORK_0, password);
1282 sprintf(lynq_wep_tx_keyidx_cmd,"SET_NETWORK %d wep_tx_keyidx 0",AP_NETWORK_0);
1283 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1284 DO_OK_FAIL_REQUEST(lynq_wep_tx_keyidx_cmd);
1285 }
1286 else if (auth == LYNQ_WIFI_AUTH_WPA_PSK || auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
1287 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",AP_NETWORK_0, password);
1288 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1289 }
1290 else {
1291 return -1;
1292 }
you.chen35020192022-05-06 11:30:57 +08001293
you.chen35020192022-05-06 11:30:57 +08001294 DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001295
qs.xiongf1b525b2022-03-31 00:58:23 -04001296 return 0;
1297}
1298
you.chen35020192022-05-06 11:30:57 +08001299int lynq_ap_password_get(lynq_wifi_index_e idx, char *password)
qs.xiongf1b525b2022-03-31 00:58:23 -04001300{
you.chen35020192022-05-06 11:30:57 +08001301 FILE * fp;
1302 int len, ret;
1303 int count, index;
1304 char *split_lines[128] = {0};
1305 char *buff, *p;
qs.xiong97fa59b2022-04-07 05:41:29 -04001306
you.chen35020192022-05-06 11:30:57 +08001307 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001308
you.chen35020192022-05-06 11:30:57 +08001309 fp = fopen("/data/wifi/wg870/wpa_supplicant_ap.conf", "rb");
1310// fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1311 if (NULL == fp) {
1312 printf("open file fail\n");
1313 return -1;
1314 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001315
you.chen35020192022-05-06 11:30:57 +08001316 buff = alloca(MAX_RET);
1317 fseek(fp, 0, SEEK_SET);
1318 len = fread(buff, 1, MAX_RET, fp);
1319 fclose(fp);
qs.xiong97fa59b2022-04-07 05:41:29 -04001320
you.chen35020192022-05-06 11:30:57 +08001321 for(index=0; index < len; index ++) {
1322 if (memcmp(buff + index, "network={", 9) != 0) {
1323 continue;
1324 }
1325 p = buff + index + 9;
1326 for (; index < len; index ++ ) {
1327 if (buff[index] != '}') {
1328 continue;
1329 }
1330 buff[index] = '\0';
1331 break;
1332 }
1333 len = buff + index - p;
1334 }
1335
1336 count = lynq_split(p, len, '\n', split_lines);
1337
1338 ret = -1;
1339 for(index=0; index < count; index++) {
1340 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001341 if (p != NULL) {
1342 p += 4;
1343 if (*p == '\"') {
1344 p++;
1345 }
you.chen35020192022-05-06 11:30:57 +08001346 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001347 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1348 p += 9;
1349 if (*p == '\"') {
1350 p++;
1351 }
1352 }
1353 else {
1354 continue;
you.chen35020192022-05-06 11:30:57 +08001355 }
1356
1357 strcpy(password, p);
1358
1359 while(*password != '\0') {
1360 if (*password == '\"') {
1361 *password = '\0';
1362 break;
1363 }
1364 password++;
1365 }
1366 ret = 0;
1367 break;
1368 } //end for(index=0; index < count; index++)
1369
1370 return ret;
qs.xiong7a105ce2022-03-02 09:43:11 -05001371}
1372
you.chen35020192022-05-06 11:30:57 +08001373static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) {
1374 char lynq_auth_str[MAX_RET] = {0};
you.chena6cd55a2022-05-08 12:20:18 +08001375 char lynq_proto_str[MAX_RET] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001376
you.chen35020192022-05-06 11:30:57 +08001377 if (inner_get_param(interface, net_no, "key_mgmt", lynq_auth_str) != 0) {
1378 return -1;
1379 }
1380
1381 *auth = convert_auth_from_key_mgmt(lynq_auth_str);
you.chena6cd55a2022-05-08 12:20:18 +08001382
1383 if (*auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1384 if (inner_get_param(interface, net_no, "proto", lynq_proto_str) == 0) {
1385 if (strcmp(lynq_proto_str, "RSN") == 0) {
1386 *auth = LYNQ_WIFI_AUTH_WPA2_PSK;
1387 }
1388 }
1389 }
you.chen35020192022-05-06 11:30:57 +08001390 return 0;
1391}
1392
1393int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password)
qs.xiong7a105ce2022-03-02 09:43:11 -05001394{
you.chen35020192022-05-06 11:30:57 +08001395 int pass_len, net_no, count, index;
1396 char lynq_tmp_cmd[300]={0};
1397 int net_no_list[128];
1398 lynq_wifi_auth_s net_auth;
1399 pass_len=strlen(password);
1400 if(pass_len < 8 || pass_len >= 64){
1401 return -1;
1402 }
1403
1404 CHECK_IDX(idx, CTRL_STA);
1405
1406 net_no = -1;
1407 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ap->ap_ssid);
1408
1409 for (index=0; index < count; index++) {
1410 net_auth = -1;
1411 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == ap->auth) {
1412 net_no = net_no_list[index];
1413 break;
1414 }
1415 }
1416
1417 if (net_no < 0) {
1418 return -1;
1419 }
1420
1421 CHECK_WPA_CTRL(CTRL_STA);
1422
1423 sprintf(lynq_tmp_cmd,"SET_NETWORK %d psk \"%s\"",net_no, password);
1424
1425 DO_OK_FAIL_REQUEST(lynq_tmp_cmd);
1426 DO_OK_FAIL_REQUEST(cmd_save_config);
1427
1428 return 0;
1429}
1430
1431int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo
1432
1433 FILE * fp;
you.chen755332b2022-08-06 16:59:10 +08001434 int len, ret, network_len;
you.chen35020192022-05-06 11:30:57 +08001435 int count, index;
1436 char *split_lines[128] = {0};
1437 char *buff, *p;
1438
you.chen755332b2022-08-06 16:59:10 +08001439 network_len = 0;
1440 p = NULL;
1441
you.chen35020192022-05-06 11:30:57 +08001442 CHECK_IDX(idx, CTRL_STA);
1443
you.chen755332b2022-08-06 16:59:10 +08001444 if (NULL == password) {
1445 printf("bad param\n");
1446 return -1;
1447 }
1448
you.chen35020192022-05-06 11:30:57 +08001449 fp = fopen("/data/wifi/wg870/wpa_supplicant.conf", "rb");
1450 if (NULL == fp) {
1451 printf("open file fail\n");
1452 return -1;
1453 }
1454
1455 buff = alloca(MAX_RET);
1456 fseek(fp, 0, SEEK_SET);
1457 len = fread(buff, 1, MAX_RET, fp);
1458 fclose(fp);
1459
1460 for(index=0; index < len; index ++) {
1461 for(; index < len; index ++) {
1462 if (memcmp(buff + index, "network={", 9) != 0) {
1463 continue;
1464 }
1465 p = buff + index + 9;
1466 for (; index < len; index ++ ) {
1467 if (buff[index] != '}') {
1468 continue;
1469 }
1470 buff[index] = '\0';
1471 break;
1472 }
you.chen755332b2022-08-06 16:59:10 +08001473 network_len = buff + index - p;
1474 break;
you.chen35020192022-05-06 11:30:57 +08001475 }
1476
1477 if (strstr(p, ap->ap_ssid) != NULL) {
1478 break;
1479 }
1480 }
1481
you.chen755332b2022-08-06 16:59:10 +08001482 if (index >= len || NULL == p || network_len <= 0) {
you.chen35020192022-05-06 11:30:57 +08001483 return -1;
1484 }
1485
you.chen755332b2022-08-06 16:59:10 +08001486 count = lynq_split(p, network_len, '\n', split_lines);
you.chen35020192022-05-06 11:30:57 +08001487
1488 ret = -1;
1489 for(index=0; index < count; index++) {
1490 p = strstr(split_lines[index], "psk=");
you.chen6c2dd9c2022-05-16 17:55:28 +08001491 if (p != NULL) {
1492 p += 4;
1493 if (*p == '\"') {
1494 p++;
1495 }
you.chen35020192022-05-06 11:30:57 +08001496 }
you.chen6c2dd9c2022-05-16 17:55:28 +08001497 else if (NULL != (p = strstr(split_lines[index], "wep_key0="))) {
1498 p += 9;
1499 if (*p == '\"') {
1500 p++;
1501 }
1502 }
1503 else {
1504 continue;
you.chen35020192022-05-06 11:30:57 +08001505 }
1506
1507 strcpy(password, p);
1508
1509 while(*password != '\0') {
1510 if (*password == '\"') {
1511 *password = '\0';
1512 break;
1513 }
1514 password++;
1515 }
1516 ret = 0;
1517 break;
1518 } //end for(index=0; index < count; index++)
1519
1520 return ret;
1521}
1522
1523static int inner_set_sta_ssid(int net_no, char *sta_ssid)
1524{
qs.xiong97fa59b2022-04-07 05:41:29 -04001525 char lynq_wifi_ssid_cmd[80]={0};
qs.xiongf1b525b2022-03-31 00:58:23 -04001526
you.chen35020192022-05-06 11:30:57 +08001527 if (sta_ssid == NULL) {
1528 printf("sta_ssid is null\n");
qs.xiongf1b525b2022-03-31 00:58:23 -04001529 return -1;
you.chen35020192022-05-06 11:30:57 +08001530 }
1531
1532 CHECK_WPA_CTRL(CTRL_STA);
1533
1534 sprintf(lynq_wifi_ssid_cmd,"SET_NETWORK %d ssid \"%s\"", net_no, sta_ssid);
1535
1536 DO_OK_FAIL_REQUEST(lynq_wifi_ssid_cmd);
1537// DO_OK_FAIL_REQUEST(cmd_save_config);
1538
qs.xiong7a105ce2022-03-02 09:43:11 -05001539 return 0;
1540
1541}
1542
you.chen35020192022-05-06 11:30:57 +08001543static int inner_sta_start_stop(int net_no, int start_flag, int save)
qs.xiong7a105ce2022-03-02 09:43:11 -05001544{
you.chen35020192022-05-06 11:30:57 +08001545 char lynq_disable_cmd[128]={0};
1546 char lynq_select_cmd[128]={0};
1547
1548 CHECK_WPA_CTRL(CTRL_STA);
1549
1550 if (save != 0) {
you.chenc29444e2022-06-07 18:01:16 +08001551 if (start_flag != 0)
1552 {
1553 sprintf(lynq_select_cmd,"ENABLE_NETWORK %d", net_no);
1554 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1555 }
1556 else
1557 {
1558 sprintf(lynq_select_cmd,"DISABLE_NETWORK %d", net_no);
1559 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1560 }
you.chen35020192022-05-06 11:30:57 +08001561 DO_OK_FAIL_REQUEST(cmd_save_config);
1562 }
1563
1564 if (start_flag == 0) {
you.chen6c2dd9c2022-05-16 17:55:28 +08001565 sprintf(lynq_disable_cmd,"DISCONNECT");
you.chen35020192022-05-06 11:30:57 +08001566 DO_OK_FAIL_REQUEST(lynq_disable_cmd);
1567 }
1568 else {
1569 sprintf(lynq_select_cmd,"SELECT_NETWORK %d", net_no);
1570 DO_OK_FAIL_REQUEST(lynq_select_cmd);
1571 }
1572
1573 return 0;
1574}
1575
1576int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid)
1577{
1578 CHECK_IDX(idx, CTRL_STA);
1579
you.chen6c2dd9c2022-05-16 17:55:28 +08001580 curr_status_info curr_state;
1581 ap_info_s ap_info;
1582 curr_state.ap = &ap_info;
1583 curr_state.state = NULL;
1584
1585 if (0 == inner_get_status_info(CTRL_STA, &curr_state)) {
1586 strcpy(sta_ssid, ap_info.ap_ssid);
1587 return 0;
1588 }
1589
1590 return -1;
you.chen35020192022-05-06 11:30:57 +08001591}
1592
1593int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info)
1594{
you.chen9ac66392022-08-06 17:01:16 +08001595 scan_info_s *scan_list = NULL;
1596 saved_ap_info_s *save_list = NULL;
you.chen35020192022-05-06 11:30:57 +08001597 int scan_len=0;
1598 int save_len=0;
1599 int best_index = -1;
1600 int best_scan_index = -1;
1601 int best_rssi = 0;
you.chen9ac66392022-08-06 17:01:16 +08001602 int i, j, ret;
1603
1604 ret = -1;
you.chen35020192022-05-06 11:30:57 +08001605
1606 CHECK_IDX(idx, CTRL_STA);
1607 if (info == NULL) {
1608 return -1;
1609 }
1610
1611 curr_status_info curr_state;
1612 ap_info_s ap_info;
you.chen9ac66392022-08-06 17:01:16 +08001613 char status[64];
you.chen35020192022-05-06 11:30:57 +08001614
you.chen9ac66392022-08-06 17:01:16 +08001615 memset(&ap_info, 0, sizeof (ap_info));
1616 memset(status, 0, sizeof (status));
1617
1618 curr_state.ap = &ap_info;
1619 curr_state.state = status;
1620
1621 if (0 == inner_get_status_info(CTRL_STA, &curr_state) && curr_state.net_no >= 0) {
you.chen35020192022-05-06 11:30:57 +08001622 memcpy(&info->base_info, &ap_info, sizeof (ap_info_s));
you.chen9ac66392022-08-06 17:01:16 +08001623 if (strcmp(status, STATE_COMPLETED) == 0)
1624 {
1625 info->status = LYNQ_WIFI_AP_STATUS_ENABLE;
1626 }
1627 else
1628 {
1629 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1630 }
you.chen35020192022-05-06 11:30:57 +08001631 lynq_get_connect_ap_rssi(idx, &info->rssi);
you.chen9ac66392022-08-06 17:01:16 +08001632 lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw);
you.chen35020192022-05-06 11:30:57 +08001633 return 0;
1634 }
1635
you.chen9ac66392022-08-06 17:01:16 +08001636 lynq_wifi_sta_start_scan(idx);
1637
you.chen35020192022-05-06 11:30:57 +08001638 if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001639 if (NULL != scan_list)
1640 {
1641 free(scan_list);
1642 }
you.chen35020192022-05-06 11:30:57 +08001643 return -1;
1644 }
1645
1646 if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) {
you.chen9ac66392022-08-06 17:01:16 +08001647 if (NULL != scan_list)
1648 {
1649 free(scan_list);
1650 }
1651 if (NULL != save_list)
1652 {
1653 free(save_list);
1654 }
you.chen35020192022-05-06 11:30:57 +08001655 return -1;
1656 }
1657
1658 for (i=0; i < save_len; i++) {
1659 for (j=0; j < scan_len; j++) {
1660 if (strcmp(save_list[i].base_info.ap_ssid, scan_list[j].ssid) == 0 //@todo not finished
1661 && save_list[i].base_info.auth == scan_list[j].auth) {
1662 if (best_rssi == 0) {
you.chen9ac66392022-08-06 17:01:16 +08001663 best_index = i;
you.chen35020192022-05-06 11:30:57 +08001664 best_rssi = scan_list[j].rssi;
1665 }
1666 else if (best_rssi > scan_list[j].rssi) {
1667 best_index = i;
1668 best_scan_index = j;
1669 best_rssi = scan_list[j].rssi;
1670 }
1671 break;
1672 }
1673 }
1674 }
1675
1676 if (best_index >= 0) {
1677 memcpy(&info->base_info, &save_list[best_index].base_info, sizeof (ap_info_s));
1678 info->status = LYNQ_WIFI_AP_STATUS_DISABLE;
1679 info->rssi = best_rssi;
you.chen9ac66392022-08-06 17:01:16 +08001680 ret = 0;
you.chen35020192022-05-06 11:30:57 +08001681 }
1682
you.chen9ac66392022-08-06 17:01:16 +08001683 if (NULL != scan_list)
1684 {
1685 free(scan_list);
1686 }
1687 if (NULL != save_list)
1688 {
1689 free(save_list);
1690 }
1691
1692 return ret;
you.chen35020192022-05-06 11:30:57 +08001693}
1694
1695static int inner_set_sta_auth_psw(int net_no, lynq_wifi_auth_s auth, char *password)
1696{
1697 char lynq_auth_cmd[64]={0};
1698 char lynq_ket_mgmt_cmd[64]={0};
1699 char lynq_pairwise_cmd[64]={0};
1700 char lynq_psk_cmd[64]={0};
1701
1702 CHECK_WPA_CTRL(CTRL_STA);
1703
qs.xiong1af5daf2022-03-14 09:12:12 -04001704 switch(auth){
you.chen35020192022-05-06 11:30:57 +08001705 case LYNQ_WIFI_AUTH_OPEN:
1706 {
1707 sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001708
you.chen35020192022-05-06 11:30:57 +08001709 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1710// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiong97fa59b2022-04-07 05:41:29 -04001711 break;
1712 }
you.chen35020192022-05-06 11:30:57 +08001713 case LYNQ_WIFI_AUTH_WPA_PSK:
1714 case LYNQ_WIFI_AUTH_WPA2_PSK:
qs.xiongf1b525b2022-03-31 00:58:23 -04001715 {
you.chen35020192022-05-06 11:30:57 +08001716 if (auth == LYNQ_WIFI_AUTH_WPA_PSK) {
1717 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto WPA", net_no);
1718 }
1719 else if (auth == LYNQ_WIFI_AUTH_WPA2_PSK) {
you.chena6cd55a2022-05-08 12:20:18 +08001720 sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no);
you.chen35020192022-05-06 11:30:57 +08001721 }
1722 sprintf(lynq_ket_mgmt_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", net_no);
1723 sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise CCMP", net_no);
qs.xiong97fa59b2022-04-07 05:41:29 -04001724
you.chen35020192022-05-06 11:30:57 +08001725 DO_OK_FAIL_REQUEST(lynq_auth_cmd);
1726 DO_OK_FAIL_REQUEST(lynq_ket_mgmt_cmd);
1727 DO_OK_FAIL_REQUEST(lynq_pairwise_cmd);
qs.xiong97fa59b2022-04-07 05:41:29 -04001728
you.chen35020192022-05-06 11:30:57 +08001729 if (password != NULL) {
1730 sprintf(lynq_psk_cmd, "SET_NETWORK %d psk \"%s\"", net_no, password);
1731 DO_OK_FAIL_REQUEST(lynq_psk_cmd);
1732 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001733
you.chen35020192022-05-06 11:30:57 +08001734// DO_OK_FAIL_REQUEST(cmd_save_config);
qs.xiongf1b525b2022-03-31 00:58:23 -04001735 break;
you.chen35020192022-05-06 11:30:57 +08001736 }
qs.xiongf1b525b2022-03-31 00:58:23 -04001737 default:
1738 return -1;
you.chen35020192022-05-06 11:30:57 +08001739 }
qs.xiong97fa59b2022-04-07 05:41:29 -04001740
qs.xiongf1b525b2022-03-31 00:58:23 -04001741 return 0;
qs.xiong1af5daf2022-03-14 09:12:12 -04001742}
qs.xiong7a105ce2022-03-02 09:43:11 -05001743
you.chen35020192022-05-06 11:30:57 +08001744static int inner_get_curr_net_no(int interface) {
1745 curr_status_info curr_state;
1746 curr_state.ap = NULL;
1747 curr_state.state = NULL;
1748
1749 if (0 != inner_get_status_info(interface, &curr_state)) {
1750 return -1;
1751 }
1752
1753 return curr_state.net_no;
1754}
1755
1756int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth)
qs.xiong7a105ce2022-03-02 09:43:11 -05001757{
you.chen35020192022-05-06 11:30:57 +08001758 int net_no;
1759 CHECK_IDX(idx, CTRL_STA);
qs.xiongf1b525b2022-03-31 00:58:23 -04001760
you.chen35020192022-05-06 11:30:57 +08001761 net_no = inner_get_curr_net_no(CTRL_STA);
qs.xiong7a105ce2022-03-02 09:43:11 -05001762
you.chen35020192022-05-06 11:30:57 +08001763 if (net_no < 0) {
1764 return -1;
1765 }
1766
1767 return inner_get_network_auth(CTRL_STA, net_no, auth);
qs.xiong7a105ce2022-03-02 09:43:11 -05001768}
1769
you.chen35020192022-05-06 11:30:57 +08001770int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw)
qs.xiong7a105ce2022-03-02 09:43:11 -05001771{
you.chen35020192022-05-06 11:30:57 +08001772 int count, net_no, index;
1773 int net_no_list[128];
1774 lynq_wifi_auth_s net_auth;
qs.xiongf1b525b2022-03-31 00:58:23 -04001775
you.chen35020192022-05-06 11:30:57 +08001776 if (ssid == NULL || *ssid == '\0') {
1777 printf("bad ssid\n");
1778 return -1;
1779 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001780
you.chen35020192022-05-06 11:30:57 +08001781 if (LYNQ_WIFI_AUTH_OPEN != auth) {
1782 if (psw == NULL || strlen(psw) < 8 || strlen(psw) >= 64) {
1783 printf("bad password\n");
1784 return -1;
1785 }
1786 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001787
you.chen35020192022-05-06 11:30:57 +08001788 CHECK_IDX(idx, CTRL_STA);
1789
1790 net_no = -1;
1791 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
1792
1793 for (index=0; index < count; index++) {
1794 net_auth = -1;
1795 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
1796 net_no = net_no_list[index];
1797 break;
1798 }
1799 }
1800
1801 if (net_no < 0) {
1802 net_no = lynq_add_network(CTRL_STA);
1803 if (net_no == -1) {
1804 return -1;
1805 }
1806
1807 printf("net no is %d\n", net_no);
1808 if (0 != inner_set_sta_ssid(net_no, ssid)) {
1809 return -1;
1810 }
1811 }
1812
1813 if (0 != inner_set_sta_auth_psw(net_no, auth, psw)) {
1814 return -1;
1815 }
1816
1817 return inner_sta_start_stop(net_no, 1, 1);
qs.xiong7a105ce2022-03-02 09:43:11 -05001818}
1819
you.chen35020192022-05-06 11:30:57 +08001820int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid)
qs.xiong7a105ce2022-03-02 09:43:11 -05001821{
you.chen35020192022-05-06 11:30:57 +08001822 ap_info_s ap;
1823 curr_status_info curr_state;
1824 ap.ap_ssid[0] = '\0';
qs.xiong97fa59b2022-04-07 05:41:29 -04001825
you.chen35020192022-05-06 11:30:57 +08001826 if (ssid == NULL || *ssid == '\0') {
1827 return -1;
1828 }
qs.xiong7a105ce2022-03-02 09:43:11 -05001829
you.chen35020192022-05-06 11:30:57 +08001830 CHECK_IDX(idx, CTRL_STA);
qs.xiong97fa59b2022-04-07 05:41:29 -04001831
you.chen35020192022-05-06 11:30:57 +08001832 curr_state.ap = &ap;
you.chenb4b121c2022-05-06 17:50:16 +08001833 curr_state.state = NULL;
1834
you.chen35020192022-05-06 11:30:57 +08001835 if (inner_get_status_info(CTRL_STA, &curr_state) != 0) {
1836 return 0;
1837 }
qs.xiong1af5daf2022-03-14 09:12:12 -04001838
you.chen35020192022-05-06 11:30:57 +08001839 if (strcmp(ap.ap_ssid, ssid) != 0) {
1840 return 0;
1841 }
1842
1843 return inner_sta_start_stop(curr_state.net_no, 0, 0);
qs.xiong7a105ce2022-03-02 09:43:11 -05001844}
qs.xiong97fa59b2022-04-07 05:41:29 -04001845
you.chena6cd55a2022-05-08 12:20:18 +08001846int lynq_wifi_sta_start(lynq_wifi_index_e idx)
1847{
qs.xiongad2f89d2023-01-18 13:17:41 +08001848// const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf";
1849// const char *lynq_reconnect_cmd = "RECONNECT";
1850 const char *lynq_enable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 enable_net all";
1851 const char *lynq_reconnect_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 reconnect";
1852// const char *lynq_first_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 remove_net all";
qs.xiong7a105ce2022-03-02 09:43:11 -05001853
you.chen35020192022-05-06 11:30:57 +08001854 CHECK_IDX(idx, CTRL_STA);
you.chena6cd55a2022-05-08 12:20:18 +08001855 CHECK_WPA_CTRL(CTRL_STA);
1856
1857 system("connmanctl enable wifi");
1858
qs.xiongad2f89d2023-01-18 13:17:41 +08001859 if (system("ifconfig | grep -q wlan0") != 0)
1860 {
you.chen35020192022-05-06 11:30:57 +08001861 return -1;
1862 }
qs.xiong9c99fa92022-03-15 08:03:26 -04001863
qs.xiongad2f89d2023-01-18 13:17:41 +08001864// DO_OK_FAIL_REQUEST(cmd_remove_all);
1865// system(lynq_first_sta_cmd);
1866// system(lynq_reconfigure_cmd);
1867// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
1868 system(lynq_enable_sta_cmd);
1869 system(lynq_reconnect_cmd);
1870// DO_OK_FAIL_REQUEST(lynq_reconnect_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001871 return 0;
qs.xiong7a105ce2022-03-02 09:43:11 -05001872}
1873
you.chen35020192022-05-06 11:30:57 +08001874int lynq_wifi_sta_stop(lynq_wifi_index_e idx)
qs.xiong97fa59b2022-04-07 05:41:29 -04001875{
qs.xiongad2f89d2023-01-18 13:17:41 +08001876// char lynq_disable_network_cmd[MAX_CMD];
1877// curr_status_info curr_state;
1878// ap_info_s ap_info;
you.chen35020192022-05-06 11:30:57 +08001879
qs.xiongad2f89d2023-01-18 13:17:41 +08001880 const char * lynq_disable_sta_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disable_net all";
you.chena6cd55a2022-05-08 12:20:18 +08001881 CHECK_IDX(idx, CTRL_STA);
1882 CHECK_WPA_CTRL(CTRL_STA);
1883
qs.xiongad2f89d2023-01-18 13:17:41 +08001884// curr_state.ap = &ap_info;
1885// curr_state.state = NULL;
you.chena6cd55a2022-05-08 12:20:18 +08001886
qs.xiongad2f89d2023-01-18 13:17:41 +08001887// if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) {
1888// return 0;
1889// }
you.chena6cd55a2022-05-08 12:20:18 +08001890
qs.xiongad2f89d2023-01-18 13:17:41 +08001891// sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no);
1892// DO_OK_FAIL_REQUEST(lynq_disable_network_cmd);
1893 system(lynq_disable_sta_cmd);
you.chena6cd55a2022-05-08 12:20:18 +08001894 DO_OK_FAIL_REQUEST(cmd_save_config);
1895
1896 return 0;
1897// return system("connmanctl disable wifi");
qs.xiongf1b525b2022-03-31 00:58:23 -04001898}
qs.xiong7a105ce2022-03-02 09:43:11 -05001899
you.chen35020192022-05-06 11:30:57 +08001900//static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) {
1901// int i, count;
1902// char *p;
1903// const char * FLAG_SSID = "ssid=";
1904// const char * FLAG_SBSID = "bssid=";
1905// const char * FLAG_KEY_MGMT = "key_mgmt=";
1906// const char * FLAG_FREQ = "freq=";
1907// char lynq_sta_cmd[MAX_CMD];
1908// char *split_lines[128] = {0};
1909
1910// CHECK_WPA_CTRL(CTRL_AP);
1911
1912// sprintf(lynq_sta_cmd, "STA %s", bssid);
1913
1914// DO_REQUEST(lynq_sta_cmd);
1915
1916// count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1917
1918// for(i=0; i < count; i++) {
1919// p = strstr(split_lines[i], FLAG_SSID);
1920// if (p != NULL) {
1921// strcpy(ap->ap_ssid, p + strlen(FLAG_SSID));
1922// continue;
1923// }
1924// }
1925
1926// lynq_get_interface_ip(idx, ap->ap_ip);
1927// lynq_ap_password_set(idx, ap->psw);
1928
1929// return 0;
1930//}
1931
1932static int inner_get_status_info_ap (int interface, ap_info_s *ap) {
1933 curr_status_info curr_state;
1934 curr_state.ap = ap;
1935 curr_state.state = NULL;
1936 return inner_get_status_info(interface, &curr_state);
1937}
1938
1939int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04001940{
you.chen35020192022-05-06 11:30:57 +08001941 int ip_count, index, i, line_count;
1942 const char *lynq_first_sta_cmd = "STA-FIRST";
1943 char lynq_next_sta_cmd[MAX_CMD];
1944 char *bssid[1024] = {0};
1945 char *mac_list[128] = {0};
1946 char *ip_list[128] = {0};
1947 char *split_lines[128] = {0};
qs.xiong97fa59b2022-04-07 05:41:29 -04001948
you.chen35020192022-05-06 11:30:57 +08001949 CHECK_IDX(idx, CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001950
you.chen35020192022-05-06 11:30:57 +08001951 CHECK_WPA_CTRL(CTRL_AP);
qs.xiong97fa59b2022-04-07 05:41:29 -04001952
you.chen35020192022-05-06 11:30:57 +08001953// ap_info_s * tmp_ap;
1954// device_info_s * tmp_list;
1955 if (ap == NULL || list == NULL || len == NULL) {
1956 printf("bad input param");
1957 return -1;
1958 }
1959
1960// ap = &tmp_ap;
1961// list = &tmp_list;
1962 *ap = malloc(sizeof (ap_info_s));
1963
1964 if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) {
1965 return -1;
1966 }
1967
1968 lynq_get_interface_ip(idx, (*ap)->ap_ip);
1969 lynq_ap_password_get(idx, (*ap)->psw);
1970
1971 ip_count = get_ip_mac_list(mac_list, ip_list);
1972 printf("get count %d\n", ip_count);
1973
1974 DO_REQUEST(lynq_first_sta_cmd);
1975
1976 index = 0;
1977 while (reply_len > 0) {
1978 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
1979 break;
1980 }
1981 line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
1982 bssid[index] = malloc(strlen(split_lines[0]) + 1);
1983 strcpy(bssid[index], split_lines[0]);
1984 index++;
1985 sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]);
1986 reply_len = MAX_RET;
1987 cmd_reply[0] = '\0';
qs.xiongfdbc58e2022-09-29 11:37:32 +08001988 ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL);
you.chen9ac66392022-08-06 17:01:16 +08001989 if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) {
1990 printf("run %s fail \n", lynq_next_sta_cmd);
you.chen35020192022-05-06 11:30:57 +08001991 break;
1992 }
1993 }
1994
1995 *len = index;
1996
1997 *list = malloc(sizeof(device_info_s) * (*len));
1998 for (index=0; index < *len; index++) {
1999 strcpy((*list)[index].sta_mac, bssid[index]);
2000 for(i=0;i < ip_count; i++ ) {
2001 if (strcmp(bssid[index], mac_list[i]) == 0) {
2002 strcpy((*list)[index].sta_ip, ip_list[i]);
2003 break;
2004 }
2005 }
2006 get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname);
2007 (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT;
2008 free(bssid[index]);
2009 }
2010
you.chen9ac66392022-08-06 17:01:16 +08002011 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2012
you.chen35020192022-05-06 11:30:57 +08002013 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002014}
2015
you.chen35020192022-05-06 11:30:57 +08002016int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len)
qs.xiong97fa59b2022-04-07 05:41:29 -04002017{
you.chen35020192022-05-06 11:30:57 +08002018 int i, count, index, count_words;
2019 const char *lynq_scan_result_cmd = "SCAN_RESULTS";
2020 char *split_lines[128] = {0};
2021 char *split_words[128] = {0};
2022 scan_info_s * p;
qs.xiong97fa59b2022-04-07 05:41:29 -04002023
you.chen35020192022-05-06 11:30:57 +08002024 if (list == NULL || len == NULL) {
2025 return -1;
2026 }
qs.xiong97fa59b2022-04-07 05:41:29 -04002027
you.chen9ac66392022-08-06 17:01:16 +08002028 for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++)
2029 {
2030 usleep(100 * 1000);
2031 }
2032
you.chen35020192022-05-06 11:30:57 +08002033 CHECK_IDX(idx, CTRL_STA);
2034
2035 CHECK_WPA_CTRL(CTRL_STA);
2036
2037 DO_REQUEST(lynq_scan_result_cmd);
2038
2039 count = lynq_split(cmd_reply, reply_len, '\n', split_lines);
2040 *len = count - 1;
2041 *list = malloc(sizeof (scan_info_s) * *len);
2042
2043 count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header
2044 for (index=0; index <count_words; index++) {
2045 printf("----header: %s\n", split_words[index]);
2046 }
2047
2048 for(index = 1;index < count; index++) {
2049 printf("---- %s\n",split_lines[index]);
2050 count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words);
2051 if (count_words < 4)
2052 continue;
2053 printf("count: %d, %s\n", count_words, split_words[0]);
2054 //bssid / frequency / signal level / flags / ssid
2055 p = (*list) + index - 1;
2056 strcpy(p->mac, split_words[0]);
2057 p->band = convert_band_from_freq(atoi(split_words[1]));
2058 p->rssi = -1 * atoi( split_words[2]);
2059 p->auth = convert_max_auth_from_flag(split_words[3]);
2060 strcpy(p->ssid, split_words[4]);
2061 }
2062
2063 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002064}
qs.xiong97fa59b2022-04-07 05:41:29 -04002065
you.chen35020192022-05-06 11:30:57 +08002066int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth)
2067{
2068 int count, net_no, index;
2069 int net_no_list[128];
2070 lynq_wifi_auth_s net_auth;
2071 char lynq_remove_cmd[MAX_CMD];
2072
2073 if (ssid == NULL || *ssid == '\0') {
2074 printf("bad ssid\n");
2075 return -1;
2076 }
2077
2078 CHECK_IDX(idx, CTRL_STA);
2079
2080 CHECK_WPA_CTRL(CTRL_STA);
2081
2082 net_no = -1;
2083 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid);
2084
2085 for (index=0; index < count; index++) {
2086 net_auth = -1;
2087 if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) {
2088 net_no = net_no_list[index];
2089 break;
2090 }
2091 }
2092
2093 if (net_no < 0) {
2094 return 0;
2095 }
2096
2097 sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no);
2098
2099 DO_OK_FAIL_REQUEST(lynq_remove_cmd);
2100 DO_OK_FAIL_REQUEST(cmd_save_config);
2101
2102 return 0;
2103}
2104
2105int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len)
2106{
you.chen755332b2022-08-06 16:59:10 +08002107 int count, index, ssid_len;
you.chen35020192022-05-06 11:30:57 +08002108 int net_no_list[128];
2109 char freq[16];
you.chen755332b2022-08-06 16:59:10 +08002110 char *ssid_ptr;
you.chen35020192022-05-06 11:30:57 +08002111
2112 if (list == NULL || len == NULL) {
2113 printf("bad param\n");
2114 return -1;
2115 }
2116
2117 CHECK_IDX(idx, CTRL_STA);
2118
2119// CHECK_WPA_CTRL(CTRL_STA);
2120
2121 count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL);
2122 printf("count is %d\n", count);
2123
2124 *list = malloc(sizeof (saved_ap_info_s) * count);
you.chen755332b2022-08-06 16:59:10 +08002125 memset(*list, 0, sizeof (saved_ap_info_s) * count);
you.chen35020192022-05-06 11:30:57 +08002126 *len = count;
2127
2128 for (index=0; index < count; index++) {
2129 printf("to get ssid %d\n", index);
2130 inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid);
you.chen755332b2022-08-06 16:59:10 +08002131
2132 ssid_ptr = (*list)[index].base_info.ap_ssid;
2133 ssid_len = strlen(ssid_ptr);
2134 if (ssid_ptr[0] == '\"') {
2135 memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1);
2136 ssid_len -= 1;
2137 }
2138 if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') {
2139 ssid_ptr[ssid_len - 1] = '\0';
2140 }
2141
you.chen35020192022-05-06 11:30:57 +08002142 inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac);
you.chen35020192022-05-06 11:30:57 +08002143 inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth);
you.chen35020192022-05-06 11:30:57 +08002144 if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) {
2145 (*list)[index].base_info.band = convert_band_from_freq(atoi(freq));
2146 }
2147 else {
2148 (*list)[index].base_info.band = -1;
2149 }
2150
you.chen755332b2022-08-06 16:59:10 +08002151 lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw);
you.chen35020192022-05-06 11:30:57 +08002152 }
2153
2154 return 0;
2155}
2156
2157int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx)
2158{
2159 const char *lynq_scan_cmd = "SCAN";
2160
2161 CHECK_IDX(idx, CTRL_STA);
2162
2163 CHECK_WPA_CTRL(CTRL_STA);
2164
2165 DO_OK_FAIL_REQUEST(lynq_scan_cmd);
you.chen9ac66392022-08-06 17:01:16 +08002166 g_sta_scan_finish_flag = 0;
you.chen35020192022-05-06 11:30:57 +08002167
2168 return 0;
2169}
2170
2171int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) {
2172 if (cb == NULL) {
2173 return -1;
2174 }
2175
2176 g_ap_callback_priv = priv;
2177 g_ap_callback_func = cb;
2178
2179 return 0;
2180}
2181
2182int lynq_unreg_ap_event_callback(void * priv) {
2183 if (g_ap_callback_priv == priv) {
2184 g_ap_callback_func = NULL;
2185 g_ap_callback_priv = NULL;
2186 return 0;
2187 }
2188 return -1;
2189}
2190
2191int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){
2192 if (cb == NULL) {
2193 return -1;
2194 }
2195
2196 g_sta_callback_priv = priv;
2197 g_sta_callback_func = cb;
2198
2199 return 0;
2200}
2201
2202int lynq_unreg_sta_event_callback(void * priv) {
2203 if (g_sta_callback_priv == priv) {
2204 g_sta_callback_func = NULL;
2205 g_sta_callback_priv = NULL;
2206 return 0;
2207 }
2208 return -1;
2209}
2210
2211
2212static int inner_get_status_info_state (int interface, char *state) {
2213 curr_status_info curr_state;
2214 curr_state.ap = NULL;
2215 curr_state.state = state;
2216 return inner_get_status_info(interface, &curr_state);
2217}
2218
2219int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status)
2220{
2221 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002222 CHECK_IDX(idx, CTRL_AP);
2223
2224 if (inner_get_status_info_state(CTRL_AP, state) != 0) {
2225 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2226 return 0;
2227 }
2228
2229 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2230 *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE;
2231 }
2232 else {
2233 *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE;
2234 }
2235
2236 return 0;
2237}
2238
2239int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) {
2240 char state[MAX_CMD];
you.chen35020192022-05-06 11:30:57 +08002241 CHECK_IDX(idx, CTRL_STA);
2242
2243 if (inner_get_status_info_state(CTRL_STA, state) != 0) {
2244 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2245 return 0;
2246 }
2247
2248 if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) {
2249 *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE;
2250 }
2251 else {
2252 *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE;
2253 }
2254
2255 return 0;
2256}
2257
2258int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) {
2259// CHECK_IDX(idx, CTRL_AP);
2260// int ret = 0;
2261// size_t reply_len = MAX_RET;
2262// char cmd_reply[MAX_RET]={0};
2263// const char * cmd_str = "GET country";
2264// struct wpa_ctrl *s_lynq_wpa_ctrl = NULL;
2265// do{
2266// if (NULL == s_lynq_wpa_ctrl) {
2267// s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd");
2268// if (NULL == s_lynq_wpa_ctrl ) {
2269// printf("wpa_ctrl_open fail\n");
2270// return -1;
2271// }
2272// }
2273// }while(0);
2274
2275// do {
2276// reply_len = MAX_RET;
2277// cmd_reply[0] = '\0';
2278// printf("to call [%s]\n", cmd_str);
qs.xiongfdbc58e2022-09-29 11:37:32 +08002279// ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL);
you.chen35020192022-05-06 11:30:57 +08002280// if (ret != 0) {
2281// printf("call ##cmd_str fail %d\n", ret);
2282// return ret;
2283// }
2284// cmd_reply[reply_len+1] = '\0';
2285// printf("cmd replay [ %s ]\n", cmd_reply);
2286// }while(0);
2287
2288 FILE *fp;
2289 size_t i = 0;
2290 char lynq_cmd_ret[MAX_RET]={0};
2291
2292// CHECK_IDX(idx, CTRL_AP);
2293
2294 if((fp=popen("wl country","r"))==NULL)
2295 {
2296 perror("popen error!");
2297 return -1;
2298 }
2299 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2300 {
2301 perror("fread fail!");
2302 return -1;
2303 }
2304
2305 for(i=0; i < strlen(lynq_cmd_ret); i++) {
2306 if (lynq_cmd_ret[i] == ' ') {
2307 lynq_cmd_ret[i] = '\0';
2308 break;
2309 }
2310 }
2311
2312 strcpy(country_code,lynq_cmd_ret);
2313 printf("---country code %s\n", country_code);
2314
2315 int ret=pclose(fp);
2316 if(ret==-1)
2317 {
2318 perror("close file faild");
2319 }
2320
2321 return 0;
2322}
2323
2324int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) {
2325// const char * cmd_str = "GET country";
2326// CHECK_IDX(idx, CTRL_AP);
2327// CHECK_WPA_CTRL(CTRL_STA);
2328
2329// DO_REQUEST(cmd_str);
2330// printf("result %s\n", cmd_reply);
2331
2332 if (country_code == NULL || *country_code == '\0') {
2333 printf("bad country code\n");
2334 return -1;
2335 }
2336
2337 char lynq_country_cmd[MAX_CMD];
2338 sprintf(lynq_country_cmd, "wl country %s", country_code);
2339 if (system(lynq_country_cmd) == 0) {
2340 return 0;
2341 }
2342
2343 return -1;
2344}
2345
2346int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac)
2347{
2348 if (mac == NULL) {
2349 return -1;
2350 }
2351
2352 CHECK_IDX(idx, CTRL_STA);
2353 ap_info_s ap;
2354 ap.ap_mac[0] = '\0';
2355
2356 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2357 return -1;
2358 }
2359 strcpy(mac, ap.ap_mac);
2360
2361 return 0;
2362}
2363
2364int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip)
2365{
you.chen9ac66392022-08-06 17:01:16 +08002366 struct ifaddrs *ifaddr_header, *ifaddr;
2367 struct in_addr * ifa;
2368 const char * ifaName = "wlan0";
2369 if (ip == NULL)
2370 {
you.chenf58b3c92022-06-21 16:53:48 +08002371 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002372 }
you.chenf58b3c92022-06-21 16:53:48 +08002373
you.chen9ac66392022-08-06 17:01:16 +08002374 if (idx == 1) {
you.chen6c2dd9c2022-05-16 17:55:28 +08002375 ifaName = "tether";
you.chen9ac66392022-08-06 17:01:16 +08002376 }
2377 else if (idx != 0) {
you.chen35020192022-05-06 11:30:57 +08002378 return -1;
you.chen9ac66392022-08-06 17:01:16 +08002379 }
you.chen35020192022-05-06 11:30:57 +08002380
you.chen9ac66392022-08-06 17:01:16 +08002381 if (getifaddrs(&ifaddr_header) == -1)
2382 {
you.chen35020192022-05-06 11:30:57 +08002383 perror("getifaddrs");
2384 return -1;
2385 //exit(EXIT_FAILURE);
you.chen9ac66392022-08-06 17:01:16 +08002386 }
you.chen35020192022-05-06 11:30:57 +08002387
2388
you.chen9ac66392022-08-06 17:01:16 +08002389 for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next)
2390 {
2391 if (ifaddr->ifa_addr == NULL)
you.chen35020192022-05-06 11:30:57 +08002392 continue;
you.chen9ac66392022-08-06 17:01:16 +08002393 if((strcmp(ifaddr->ifa_name,ifaName)==0))
2394 {
2395 if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4
2396 {
2397 // is a valid IP4 Address
2398 ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr;
2399 inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN);
2400 printf("%s IP Address %s/n", ifaddr->ifa_name, ip);
2401 freeifaddrs(ifaddr_header);
2402 printf("ip %s\n", ip);
2403 return 0;
2404 }
2405 }
2406 }
2407 return -1;
you.chen35020192022-05-06 11:30:57 +08002408}
2409
2410int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac)
2411{
2412 int count;
2413 size_t i;
2414 char *split_words[128] = {0};
2415 const char *lynq_get_mac_cmd = "DRIVER MACADDR";
2416
2417 CHECK_WPA_CTRL(idx);
2418
2419 DO_REQUEST(lynq_get_mac_cmd);
2420
2421 if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2422 return -1;
2423 }
2424
2425 count = lynq_split(cmd_reply, reply_len, '=', split_words);
2426
2427 if (count < 2) {
2428 return -1;
2429 }
2430
2431 for (i=0; i < strlen(split_words[1]); i++ ) {
2432 if (split_words[1][i] != ' ') {
2433 break;
2434 }
2435 }
2436
2437 strcpy(mac, split_words[1] + i);
2438
2439 return 0;
2440}
2441
2442int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi)
2443{
2444// int count;
2445// char *split_words[128] = {0};
2446// const char *lynq_get_rssi_cmd = "DRIVER RSSI";
2447
2448// if (rssi == NULL) {
2449// return -1;
2450// }
2451
2452// CHECK_IDX(idx, CTRL_STA);
2453
2454// CHECK_WPA_CTRL(CTRL_STA);
2455
2456// DO_REQUEST(lynq_get_rssi_cmd);
2457
2458// if (memcmp(cmd_reply, "FAIL", 4) == 0) {
2459// return -1;
2460// }
2461
2462// count = lynq_split(cmd_reply, reply_len, ' ', split_words);
2463
2464// if (count < 2) {
2465// return -1;
2466// }
2467
2468// *rssi = atoi(split_words[1]) * -1;
2469
2470 FILE *fp;
2471 size_t i = 0;
2472 char lynq_cmd_ret[MAX_RET]={0};
2473
2474// CHECK_IDX(idx, CTRL_AP);
qs.xiongff0ae0f2022-10-11 15:47:14 +08002475/*******change other cmd to get rssi*******
2476 *
2477 *wl rssi ---> wl -i wlan0 rssi
2478 *
2479 ***** change by qs.xiong 20221011*******/
2480 if((fp=popen("wl -i wlan0 rssi","r"))==NULL)
you.chen35020192022-05-06 11:30:57 +08002481 {
2482 perror("popen error!");
2483 return -1;
2484 }
2485 if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0)
2486 {
2487 perror("fread fail!");
2488 return -1;
2489 }
you.chen9f17e4d2022-06-06 17:18:18 +08002490 *rssi = atoi(lynq_cmd_ret) * -1;
qs.xiongff0ae0f2022-10-11 15:47:14 +08002491/****** if got rssi is 0,means sta didn't connected any device****/
2492 if(*rssi == 0)
2493 {
2494 printf("sta didn't connected any ap device,please check connection\n");
2495 }
you.chen35020192022-05-06 11:30:57 +08002496
2497 return 0;
2498}
2499
2500int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band)
2501{
2502 if (band == NULL) {
2503 return -1;
2504 }
2505
2506 CHECK_IDX(idx, CTRL_STA);
2507 ap_info_s ap;
2508 ap.band = -1;
2509
2510 if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) {
2511 return -1;
2512 }
2513 *band = ap.band;
2514
2515 return 0;
qs.xiong97fa59b2022-04-07 05:41:29 -04002516}
you.chenf58b3c92022-06-21 16:53:48 +08002517
2518int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip)
2519{
2520 int index;
2521 int ip_count = 0;
2522 char bssid[1024] = {0};
2523 char *mac_list[128] = {0};
2524 char *ip_list[128] = {0};
2525
2526 if (ip == NULL)
2527 {
2528 printf("invalid param");
2529 return -1;
2530 }
2531
2532 CHECK_IDX(idx, CTRL_STA);
2533
2534 if (lynq_get_connect_ap_mac(idx, bssid) != 0)
2535 {
2536 return -1;
2537 }
2538
2539 ip_count = get_ip_mac_list(mac_list, ip_list);
2540
2541 for (index=0; index < ip_count; index++)
2542 {
2543 if (strcmp(bssid, mac_list[index]) == 0)
2544 {
2545 strcpy(ip, ip_list[index]);
you.chen9ac66392022-08-06 17:01:16 +08002546
2547 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2548
you.chenf58b3c92022-06-21 16:53:48 +08002549 return 0;
2550 }
2551 }
2552
2553 printf("not found\n");
you.chen9ac66392022-08-06 17:01:16 +08002554 free_ip_mac_list_mem(mac_list, 128, ip_list, 128);
2555
you.chenf58b3c92022-06-21 16:53:48 +08002556 return -1;
2557}
2558
qs.xiong026c5c72022-10-17 11:15:45 +08002559int lynq_ap_connect_num(int sta_number)
2560{
2561 char lynq_limit_cmd[32]={0};
2562 int ret;
2563 if((sta_number < 1 ) && (sta_number > 15)){
2564 printf("sta_number: not in range\n",sta_number);
2565 return -1;
2566 }
2567 sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number);
2568 ret = system(lynq_limit_cmd);
2569 if(ret != 0){
2570 printf("cmd of limit ap devices number error\n");
2571 }
2572 return 0;
2573}
you.chenf58b3c92022-06-21 16:53:48 +08002574
qs.xiong77905552022-10-17 11:19:57 +08002575int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode)
2576{
2577
2578 char lynq_wifi_acs_cmd[128]={0};
2579 char lynq_cmd_mode[128]={0};
2580 char lynq_cmd_slect[128]={0};
2581
2582 if((acs_mode != 2) && (acs_mode != 5)){
2583 PRINT_AND_RETURN_VALUE("set acs_mode is error",-1);
2584 }
2585
2586 if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) {
2587 return -1;
2588 }
2589
2590 CHECK_IDX(idx, CTRL_AP);
2591
2592 CHECK_WPA_CTRL(CTRL_AP);
2593
2594 sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode);
2595 sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0);
2596 sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0);
2597
2598 DO_OK_FAIL_REQUEST(cmd_disconnect);
2599 DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd);
2600 DO_OK_FAIL_REQUEST(lynq_cmd_mode);
2601 DO_OK_FAIL_REQUEST(cmd_save_config);
2602 DO_OK_FAIL_REQUEST(lynq_cmd_slect);
2603
2604 return 0;
2605}
you.chen0f5c6432022-11-07 18:31:14 +08002606//you.chen add for tv-box start
2607static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) {
2608 FILE *fp;
2609 //printf("to exec cmd:%s\n", str_cmd);
2610 if((fp=popen(str_cmd,"r"))==NULL)
2611 {
2612 perror("popen error!");
2613 return -1;
2614 }
2615 if((fread(str_cmd_ret,max_len,1,fp))<0)
2616 {
2617 perror("fread fail!");
2618 fclose(fp);
2619 return -1;
2620 }
2621 fclose(fp);
2622 return 0;
2623}
2624
2625static int get_netmask_length(const char* mask)
2626{
2627 int masklen=0, i=0;
2628 int netmask=0;
2629
2630 if(mask == NULL)
2631 {
2632 return 0;
2633 }
2634
2635 struct in_addr ip_addr;
2636 if( inet_aton(mask, &ip_addr) )
2637 {
2638 netmask = ntohl(ip_addr.s_addr);
2639 }else{
2640 netmask = 0;
2641 return 0;
2642 }
2643
2644 while(0 == (netmask & 0x01) && i<32)
2645 {
2646 i++;
2647 netmask = netmask>>1;
2648 }
2649 masklen = 32-i;
2650 return masklen;
2651}
2652
2653static int get_tether_route_str(char *str_cmd_ret, size_t max_len) {
2654 int mask_len;
2655 char *p;
2656 char tmp[64] = {0};
2657 if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0)
2658 return -1;
2659 p = strstr(str_cmd_ret, "Mask:");
2660 if (p == NULL)
2661 return -1;
2662 mask_len = get_netmask_length(p + 5);
2663 if (mask_len == 0)
2664 return -1;
2665 p = strstr(str_cmd_ret, "inet addr:");
2666 if (p == NULL)
2667 return -1;
2668 strcpy(tmp, p + 10);
2669 p = strstr(tmp, " ");
2670 if (p != NULL)
2671 *p = '\0';
2672 sprintf(str_cmd_ret, "%s/%d", tmp, mask_len);
2673 return 0;
2674}
2675
2676static void GBWWatchThreadProc() {
2677 int i,n, nloop, nmax, ncheckcount, nidlecount;
2678 unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes;
2679 unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop;
2680 unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed;
2681 char *results[16] = {0};
2682 char str_cmd[256] = {0};
2683 char str_cmd_ret[128] = {0};
2684 char dest_ip[32] = {0};
2685 lastAP1Bytes = lastAP2Bytes = 0;
2686 lastAP1Drop = lastAP2Drop = 0;
2687 lastAP1Speed = lastAP2Speed = 0;
2688 setAP1Speed = 50;
2689 setAP2Speed = 80;
2690 nloop = 0;
2691 nmax = 6;
2692 ncheckcount = nidlecount = 0;
2693
2694 printf("------gbw thread run\n");
2695 sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac);
2696 while (dest_ip[0] == '\0') {
2697 sleep(1);
2698 str_cmd_ret[0] = '\0';
2699 exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret));
2700 for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) {
2701 if (str_cmd_ret[n] == '\n'){
2702 str_cmd_ret[n] = '\0';
2703 break;
2704 }
2705 }
2706 if (str_cmd_ret[0] != '\0')
2707 {
2708 strcpy(dest_ip, str_cmd_ret);
2709 }
2710 }
2711
2712 system("tc qdisc del dev tether root > /dev/null 2>&1");
2713 system("tc qdisc add dev tether root handle 1: htb r2q 1");
2714 system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000");
2715 if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0)
2716 {
2717 printf("not get tether info\n");
2718 return;
2719 }
2720 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 16 u32 match ip dst %s flowid 1:1", str_cmd_ret);
2721 system(str_cmd);
2722 system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000");
2723 sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip);
2724 //printf("----cmd:%s\n", str_cmd);
2725 system(str_cmd);
2726
2727 while (1) {
2728 sleep(1);
2729 memset(str_cmd, 0, sizeof(str_cmd));
2730 if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd)))
2731 continue;
2732 //printf("ap1 --- %s\n", str_cmd);
2733 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2734 if (n > 9) {
2735 if (strcmp(results[1], "Sent") == 0) {
2736 currAP1Bytes = atoll(results[2]);
2737 }
2738 if (strcmp(results[6], "(dropped") == 0) {
2739 currAP1Drop = atoi(results[7]);
2740 }
2741 }
2742
2743 memset(str_cmd, 0, sizeof(str_cmd));
2744 if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd)))
2745 continue;
2746 //printf("ap2 --- %s\n", str_cmd);
2747 n = lynq_split(str_cmd, strlen(str_cmd), ' ', results);
2748 if (n > 9) {
2749 if (strcmp(results[1], "Sent") == 0) {
2750 currAP2Bytes = atoll(results[2]);
2751 }
2752 if (strcmp(results[6], "(dropped") == 0) {
2753 currAP2Drop = atoi(results[7]);
2754 }
2755 }
2756
2757 //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop);
2758 if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) {
2759 lastAP1Bytes = currAP1Bytes;
2760 lastAP2Bytes = currAP2Bytes;
2761 continue;
2762 }
2763
2764 currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024;
2765 currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024;
2766 //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed);
2767 lastAP1Speed = currAP1Speed;
2768 lastAP2Speed = currAP2Speed;
2769 lastAP1Bytes = currAP1Bytes;
2770 lastAP2Bytes = currAP2Bytes;
2771
2772 currSetAP1Speed = setAP1Speed;
2773 if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) {
2774 ncheckcount++;
2775 if (ncheckcount > 3) {
2776 ncheckcount = 0;
2777 currSetAP1Speed = 5;
2778 }
2779 }
2780 else {
2781 ncheckcount = 0;
2782 if (currAP1Speed < 5)
2783 nidlecount++;
2784 else
2785 nidlecount = 0;
2786
2787 }
2788
2789 if (nidlecount > 60 ){
2790 currSetAP1Speed = 50;
2791 }
2792
2793 if (currSetAP1Speed != setAP1Speed) {
2794 setAP1Speed = currSetAP1Speed;
2795 sprintf(str_cmd, "tc class replace dev tether parent 1: classid 1:1 htb rate %dMbit ceil %dMbit prio 2 quantum 3000", setAP1Speed, (int)(setAP1Speed*1.4));
2796 //printf("------***change speed: %s\n", str_cmd);
2797 system(str_cmd);
2798 }
2799 }
2800}
2801
2802int enableGBW(const char* mac) {
2803 int i,len;
2804 char get_ipaddr_cmd[128]={0};
2805 ap_info_s *ap;
2806 device_info_s * list;
2807
2808 if (mac == NULL || g_gbw_enabled == 1)
2809 return -1;
2810 len = strlen(mac);
2811 g_gbw_mac = malloc(len + 1);
2812 for(i=0;i<len;i++) {
2813 if (mac[i] >= 'A' && mac[i] <= 'Z')
2814 {
2815 g_gbw_mac[i] = 'a' + (mac[i] - 'A');
2816 }
2817 else
2818 g_gbw_mac[i] = mac[i];
2819 }
2820 g_gbw_mac[i] = '\0';
2821 g_gbw_enabled = 1;
2822
2823 sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac);
2824 if (system(get_ipaddr_cmd) == 0) {
2825 //startGBW();
2826 if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) {
2827 for (i=0;i<len;i++) {
2828 //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname);
2829 if (strcmp(g_gbw_mac, list[i].sta_mac) == 0)
2830 startGBW();
2831 }
2832 free(ap);
2833 free(list);
2834 }
2835 }
2836 return 0;
2837}
2838
2839int disableGBW() {
2840 stopGBW();
2841 free(g_gbw_mac);
2842 g_gbw_mac = NULL;
2843 g_gbw_enabled = 1;
2844 return 0;
2845}
2846
2847static int startGBW() {
2848 if (g_gbw_watcher_pid != 0) {
2849 stopGBW();
2850 }
2851 pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL);
2852}
2853
2854static int stopGBW() {
2855 void* retval;
2856 pthread_cancel(g_gbw_watcher_pid);
2857 pthread_join(g_gbw_watcher_pid, &retval);
2858 g_gbw_watcher_pid = 0;
2859 system("tc qdisc del dev tether root");
2860}
2861//you.chen add for tv-box end