qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1 | /**@File lib_wifi6.c |
| 2 | * @Brief :about function test |
| 3 | * @details : |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 4 | * @Author : you.chen |
| 5 | * @Date : 2022-4-6 |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 6 | * @Version : V1.0 |
| 7 | * @copy ritght : Copyright (c) MobileTek |
| 8 | */ |
| 9 | #include <log/log.h> |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <sys/types.h> |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 12 | #include <stdlib.h> |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 13 | #include <string.h> |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 14 | #include "libwifi6.h" |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 15 | #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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 24 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | #ifdef __cplusplus |
| 29 | } |
| 30 | #endif |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 31 | |
| 32 | #define MAX_CMD 128 |
| 33 | #define MAX_RET 4096 |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 34 | #define MODE_LEN 10 |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 35 | #define CTRL_STA 0 |
| 36 | #define CTRL_AP 1 |
| 37 | #define AP_NETWORK_0 0 |
| 38 | |
| 39 | pthread_t g_ap_watcher_pid = 0; |
| 40 | volatile int g_ap_watcher_stop_flag = 0; |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 41 | volatile int g_ap_watcher_started_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 42 | |
| 43 | pthread_t g_sta_watcher_pid = 0; |
| 44 | volatile int g_sta_watcher_stop_flag = 0; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 45 | volatile int g_sta_scan_finish_flag = 1; |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 46 | volatile int g_sta_watcher_started_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 47 | |
| 48 | void * g_ap_callback_priv = NULL; |
| 49 | AP_CALLBACK_FUNC_PTR g_ap_callback_func = NULL; |
| 50 | void * g_sta_callback_priv = NULL; |
| 51 | STA_CALLBACK_FUNC_PTR g_sta_callback_func = NULL; |
| 52 | |
| 53 | //const char * CTRL_PATH="/var/run/wpa_supplicant"; |
| 54 | const 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"}; |
| 56 | const char * cmd_list_networks = "LIST_NETWORKS"; |
| 57 | const char * cmd_save_config = "SAVE_CONFIG"; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 58 | const char * cmd_disconnect = "DISCONNECT"; |
| 59 | const char * cmd_remove_all = "REMOVE_NETWORK all"; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 60 | const char * state_scan_result = "CTRL-EVENT-SCAN-RESULTS"; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 61 | const char * STATE_COMPLETED = "COMPLETED"; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 62 | |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 63 | static struct wpa_ctrl * g_lynq_wpa_ctrl[2] = {0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 64 | |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 65 | //you.chen add for tv-box start |
| 66 | volatile int g_gbw_enabled = 0; |
| 67 | char * g_gbw_mac = NULL; |
| 68 | pthread_t g_gbw_watcher_pid = 0; |
| 69 | static int startGBW(); |
| 70 | static int stopGBW(); |
| 71 | //you.chen add for tv-box end |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 72 | |
| 73 | typedef struct __curr_status_info { |
| 74 | ap_info_s *ap; |
| 75 | char * state; |
| 76 | int net_no; |
| 77 | }curr_status_info; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 78 | |
| 79 | #define PRINT_AND_RETURN_VALUE(str,value) \ |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 80 | {\ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 81 | perror((str));\ |
| 82 | return (value);\ |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 83 | } |
| 84 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 85 | #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.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 96 | struct wpa_ctrl *lynq_wpa_ctrl = NULL; \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 97 | do{ \ |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 98 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 106 | }while(0) |
| 107 | |
| 108 | #define DO_REQUEST(cmd_str) do { \ |
| 109 | reply_len = MAX_RET;\ |
| 110 | cmd_reply[0] = '\0'; \ |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 111 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 113 | if (ret != 0) { \ |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 114 | printf("call "#cmd_str" fail %d\n", ret); \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 115 | 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.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 124 | printf("cmd "#cmd_str" return FAIL\n"); \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 125 | return -1; \ |
| 126 | } else if (reply_len >=2 && memcmp(cmd_reply, "OK", 2) != 0) { \ |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 127 | printf("cmd "#cmd_str" return not OK|FAIL\n"); \ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 128 | return -1; \ |
| 129 | } \ |
| 130 | }while (0) |
| 131 | |
| 132 | |
| 133 | |
| 134 | static void APWatcherThreadProc() { |
| 135 | size_t len = MAX_RET; |
| 136 | char msg_notify[MAX_RET]; |
| 137 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 138 | struct wpa_ctrl *lynq_wpa_ctrl = NULL; |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 139 | g_ap_watcher_stop_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 140 | |
| 141 | while (g_ap_watcher_stop_flag == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 142 | 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.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 150 | g_ap_watcher_started_flag = 1; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 151 | } |
| 152 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 153 | if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) { |
| 154 | usleep(100*1000); |
| 155 | continue; |
| 156 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 157 | memset(msg_notify, 0, MAX_RET); |
| 158 | len = MAX_RET; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 159 | if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) { |
| 160 | msg_notify[len+1] = '\0'; |
| 161 | printf("ap------> %s\n", msg_notify); |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 162 | //you.chen change for tv-box start |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 163 | if (strstr(msg_notify, "AP-STA-DISCONNECTED") != NULL) { |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 164 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 172 | } |
| 173 | else if (strstr(msg_notify, "AP-STA-CONNECTED") != NULL) { |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 174 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 182 | } |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 183 | //you.chen add for tv-box end |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 184 | } // end if (!wpa_ctrl_recv(lynq_wpa_ctrl, msg_notify, &len)) |
| 185 | } // end while (g_ap_watcher_stop_flag == 0) |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 186 | if (lynq_wpa_ctrl != NULL) { |
| 187 | wpa_ctrl_detach(lynq_wpa_ctrl); |
| 188 | wpa_ctrl_close(lynq_wpa_ctrl); |
| 189 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 190 | } |
| 191 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 192 | static void STAWatcherThreadProc() { |
| 193 | size_t len = MAX_RET; |
| 194 | char msg_notify[MAX_RET]; |
| 195 | char *pReason; |
| 196 | error_number_s error; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 197 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 198 | struct wpa_ctrl *lynq_wpa_ctrl = NULL; |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 199 | g_sta_watcher_stop_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 200 | |
| 201 | while (g_sta_watcher_stop_flag == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 202 | 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.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 210 | g_sta_watcher_started_flag = 1; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 211 | } |
| 212 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 213 | if ( 0 == wpa_ctrl_pending(lynq_wpa_ctrl)) { |
| 214 | usleep(100*1000); |
| 215 | continue; |
| 216 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 217 | memset(msg_notify, 0, MAX_RET); |
| 218 | len = MAX_RET; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 219 | 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.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 240 | if (memcmp(pReason, "CONN_FAILED", 11) == 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 241 | error = LYNQ_TIME_OUT; |
| 242 | } |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 243 | else if (memcmp(pReason, "WRONG_KEY", 9) == 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 244 | 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.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 257 | if (lynq_wpa_ctrl != NULL) { |
| 258 | wpa_ctrl_detach(lynq_wpa_ctrl); |
| 259 | wpa_ctrl_close(lynq_wpa_ctrl); |
| 260 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 261 | } |
| 262 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 263 | int lynq_wifi_enable(void) |
| 264 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 265 | int ret = 0; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 266 | int i; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 267 | 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.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 274 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 275 | ret = system(cmd_check_service); |
| 276 | if (ret != 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 277 | printf("service state %d\n", ret); |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 278 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 279 | } |
lh | fe8da90 | 2022-10-11 18:55:36 +0800 | [diff] [blame] | 280 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 281 | for (i=0; i<10; i++) { |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 282 | if (system("connmanctl technologies | grep -q \"/net/connman/technology/wifi\"") == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 283 | break; |
| 284 | } |
| 285 | usleep(300*1000); |
| 286 | } |
| 287 | |
| 288 | if (i >= 10) { |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 289 | return -1; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 290 | } |
| 291 | |
you.chen | 9f17e4d | 2022-06-06 17:18:18 +0800 | [diff] [blame] | 292 | //@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.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 303 | return -1; |
you.chen | 9f17e4d | 2022-06-06 17:18:18 +0800 | [diff] [blame] | 304 | } |
| 305 | //@todo delete add temp check for socket avilable end (20220606) |
| 306 | |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 307 | if (0 != system("ifconfig | grep -q ap0")) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 308 | system("connmanctl enable wifi"); |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 309 | usleep(300*1000); |
you.chen | 01bfac3 | 2022-06-07 10:36:00 +0800 | [diff] [blame] | 310 | system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 disconnect"); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 311 | system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 DRIVER interface_create ap0"); |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 312 | usleep(300*1000); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 313 | system("connmanctl tether wifi on lynq 1qaz@WSX#$%^"); |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 314 | usleep(300*1000); |
you.chen | 01bfac3 | 2022-06-07 10:36:00 +0800 | [diff] [blame] | 315 | system("wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=ap0 disconnect"); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 316 | } |
| 317 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 318 | if (g_ap_watcher_pid == 0 ) { |
| 319 | ret=pthread_create(&g_ap_watcher_pid,NULL,APWatcherThreadProc,NULL); |
| 320 | if(ret<0){ |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 321 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 322 | } |
| 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.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 328 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 332 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 339 | return ret; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 340 | } |
| 341 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 342 | int lynq_wifi_disable(void) |
| 343 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 344 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 359 | return 0; |
| 360 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 361 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 362 | static 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.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 389 | // printf("reply len %d, %08x\n", reply_len, (int)out_put); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 390 | memcpy(out_put, cmd_reply, reply_len + 1); |
| 391 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 392 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 393 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 394 | static 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.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 409 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 410 | return ret; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 411 | } |
| 412 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 413 | static 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 | |
| 427 | static 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.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 504 | static 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 529 | static 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 | |
| 555 | static 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 | |
| 583 | static int lynq_add_network(int ap_sta) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 584 | size_t i=0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 585 | CHECK_WPA_CTRL(ap_sta); |
| 586 | const char *lynq_wifi_add_network = "ADD_NETWORK"; |
| 587 | |
| 588 | DO_REQUEST(lynq_wifi_add_network); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 589 | if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 590 | return -1; |
| 591 | } |
| 592 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 593 | for(i=0;i<reply_len;i++) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 594 | if(cmd_reply[i] == '\n') { |
| 595 | cmd_reply[i] = '\0'; |
| 596 | break; |
| 597 | } |
| 598 | } |
| 599 | return atoi(cmd_reply); |
| 600 | } |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 601 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 602 | static 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.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 625 | else if( index < 0) { |
| 626 | printf("add network fail\n"); |
| 627 | return -1; |
| 628 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | if (index < 0) |
| 632 | return -1; |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static 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 | |
| 647 | static 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 | |
| 666 | static 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 | |
| 685 | static 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 | |
| 706 | static 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 732 | 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.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 738 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 744 | p = strstr(split_lines[i], FLAG_KEY_MGMT); |
| 745 | if (p != NULL) { |
you.chen | 450d017 | 2022-07-15 17:56:48 +0800 | [diff] [blame] | 746 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 748 | 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.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 767 | if ((p = strstr(split_lines[i], FLAG_ID)) == split_lines[i]) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 768 | ret = 0; |
you.chen | 450d017 | 2022-07-15 17:56:48 +0800 | [diff] [blame] | 769 | curr_state->net_no = atoi(p + strlen(FLAG_ID)); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 770 | printf("net_no %d, -- %s\n", curr_state->net_no, p); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | return ret; |
| 775 | } |
| 776 | |
| 777 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 778 | int lynq_wifi_ap_ssid_set(lynq_wifi_index_e idx,char *ap_ssid) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 779 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 780 | char lynq_wifi_ssid_cmd[80]={0}; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 781 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 782 | 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.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 789 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 790 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { |
| 791 | return -1; |
| 792 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 793 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 794 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 803 | return 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 804 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 805 | } |
| 806 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 807 | int lynq_wifi_ap_ssid_get(lynq_wifi_index_e idx, char* ap_ssid) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 808 | { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 809 | int len; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 810 | CHECK_IDX(idx, CTRL_AP); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 811 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 822 | } |
| 823 | |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 824 | /***** |
| 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 | * */ |
| 834 | static int lynq_check_set_frequency(int input_frequency){ |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame^] | 835 | 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 | |
| 839 | for(i = 0; i <= arr_len; i++) |
| 840 | { |
| 841 | if(input_frequency == legitimate_frequency[i]) |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 842 | break; |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 843 | } |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame^] | 844 | |
| 845 | if(i == arr_len) |
| 846 | { |
| 847 | printf("input frequency is eero--->%d,please check it\n", input_frequency); |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 848 | return -1; |
| 849 | } |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame^] | 850 | |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 851 | return 0; |
| 852 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 853 | int lynq_wifi_ap_frequency_set(lynq_wifi_index_e idx,int lynq_wifi_frequency) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 854 | { |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame^] | 855 | int check; |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 856 | char lynq_wifi_frequency_cmd[128]={0}; |
| 857 | char lynq_cmd_mode[128]={0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 858 | char lynq_cmd_slect[128]={0}; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 859 | |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 860 | //@do check input frequency |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame^] | 861 | check = lynq_check_set_frequency(lynq_wifi_frequency); |
| 862 | if(check != 0) |
| 863 | { |
| 864 | printf("do check frequency error\n"); |
qs.xiong | c9c79f7 | 2022-10-17 15:27:18 +0800 | [diff] [blame] | 865 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 866 | } |
qs.xiong | c00b603 | 2022-11-29 16:28:03 +0800 | [diff] [blame^] | 867 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) |
| 868 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 869 | return -1; |
| 870 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 871 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 872 | 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.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 880 | DO_OK_FAIL_REQUEST(cmd_disconnect); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 881 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 884 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 885 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 886 | } |
| 887 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 888 | int lynq_wifi_ap_frequency_get(lynq_wifi_index_e idx,int *lynq_wifi_frequency) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 889 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 890 | char lynq_frequency_str[MAX_RET] = {0}; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 891 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 892 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 893 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 894 | 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.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 902 | int lynq_wifi_ap_bandwidth_set(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m bandwidth) |
| 903 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 904 | 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.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 948 | |
| 949 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 950 | return 0; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 951 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 952 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 953 | int lynq_wifi_ap_bandwidth_get(lynq_wifi_index_e idx,lynq_wifi_bandwidth_type_m* bandwidth) |
| 954 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 955 | int count = 0; |
| 956 | int index = 0; |
| 957 | char *split_words[128] = {0}; |
| 958 | const char *lynq_chanspec_cmd = "DRIVER chanspec\n"; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 959 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 960 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 961 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 962 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 983 | } |
qs.xiong | 0fb469a | 2022-04-14 03:50:45 -0400 | [diff] [blame] | 984 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 985 | int lynq_wifi_ap_channel_set( lynq_wifi_index_e idx,int channel) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 986 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 987 | char lynq_cmd_channel[MAX_CMD]={0}; |
qs.xiong | 1d58e9b | 2022-04-14 06:17:01 -0400 | [diff] [blame] | 988 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 989 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 1d58e9b | 2022-04-14 06:17:01 -0400 | [diff] [blame] | 990 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 991 | sprintf(lynq_cmd_channel, "wl channel %d", channel); |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 992 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 993 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1003 | } |
qs.xiong | 0fb469a | 2022-04-14 03:50:45 -0400 | [diff] [blame] | 1004 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1005 | int lynq_wifi_ap_channel_get( lynq_wifi_index_e idx,int* channel) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1006 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1007 | int count = 0; |
| 1008 | int index = 0; |
| 1009 | char *split_words[128] = {0}; |
| 1010 | char lynq_chanspec_cmd[]="DRIVER chanspec\n"; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1011 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1012 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1013 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1014 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1038 | int lynq_wifi_ap_auth_set(lynq_wifi_index_e idx, lynq_wifi_auth_s auth) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1039 | { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1040 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1047 | CHECK_IDX(idx, CTRL_AP); |
| 1048 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1049 | CHECK_WPA_CTRL(CTRL_AP); |
| 1050 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1051 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != AP_NETWORK_0) { |
| 1052 | return -1; |
| 1053 | } |
| 1054 | |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1055 | if (0 == lynq_wifi_ap_auth_get(idx, &org_auth) && org_auth != -1) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1056 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1075 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1076 | switch(auth){ |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1077 | case LYNQ_WIFI_AUTH_OPEN: |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1078 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1079 | sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0); |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1080 | sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1081 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1082 | DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1083 | break; |
| 1084 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1085 | case LYNQ_WIFI_AUTH_WEP: |
| 1086 | { |
| 1087 | sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", AP_NETWORK_0); |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1088 | sprintf(lynq_pairwise_cmd,"SET_NETWORK %d pairwise NONE", AP_NETWORK_0); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1089 | 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.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1095 | case LYNQ_WIFI_AUTH_WPA_PSK: |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1096 | case LYNQ_WIFI_AUTH_WPA2_PSK: |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1097 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1098 | 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.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1103 | sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", AP_NETWORK_0); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1104 | sprintf(lynq_psk_cmd,"SET_NETWORK %d key_mgmt WPA-PSK", AP_NETWORK_0); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1105 | } |
| 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1109 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1110 | DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| 1111 | DO_OK_FAIL_REQUEST(lynq_psk_cmd); |
| 1112 | DO_OK_FAIL_REQUEST(lynq_pairwise_cmd); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1113 | break; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1114 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1115 | default: |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1116 | { |
| 1117 | printf("auth type [%d] not support now\n", auth); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1118 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1119 | } |
| 1120 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1121 | DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1122 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1123 | return 0; |
| 1124 | } |
| 1125 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1126 | int lynq_wifi_ap_auth_get(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1127 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1128 | char lynq_auth_str[MAX_RET] = {0}; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1129 | char lynq_auth_alg_str[MAX_RET] = {0}; |
| 1130 | char lynq_proto_str[MAX_RET] = {0}; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1131 | |
| 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.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1139 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1148 | } |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1149 | else if(strcmp( lynq_auth_str, "WPA-PSK") == 0 ) { |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 1150 | if (inner_get_param(CTRL_AP, AP_NETWORK_0, "proto", lynq_proto_str) != 0) { |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1151 | *auth = -1; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1152 | } |
| 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1159 | } |
you.chen | 92fd5d3 | 2022-05-25 10:09:47 +0800 | [diff] [blame] | 1160 | else { |
| 1161 | *auth = -1; |
| 1162 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1163 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1164 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1165 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1166 | |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1167 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1168 | int lynq_wifi_ap_start(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1169 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1170 | 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.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1173 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1174 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1193 | int lynq_wifi_ap_restart(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1194 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1195 | return lynq_wifi_ap_stop(idx) == 0 ? lynq_wifi_ap_start(idx) : -1; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1196 | } |
| 1197 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1198 | int lynq_wifi_ap_stop(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1199 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1200 | char LYNQ_WIFI_CMD[128]={0}; |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1201 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1202 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1203 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1204 | CHECK_WPA_CTRL(CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1205 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1206 | sprintf(LYNQ_WIFI_CMD,"DISABLE_NETWORK %d",AP_NETWORK_0); |
| 1207 | |
| 1208 | DO_OK_FAIL_REQUEST(LYNQ_WIFI_CMD); |
| 1209 | |
you.chen | b4b121c | 2022-05-06 17:50:16 +0800 | [diff] [blame] | 1210 | // system("connmanctl tether wifi off"); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1211 | |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1212 | return 0; |
| 1213 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1214 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1215 | int lynq_wifi_ap_hide_ssid(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1216 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1217 | char lynq_disable_cmd[128] = {0}; |
| 1218 | char lynq_select_cmd[128] = {0}; |
| 1219 | const char *lynq_hide_cmd = "SET HIDE_SSID 1"; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1220 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1221 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1222 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1223 | CHECK_WPA_CTRL(CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1224 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1225 | 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.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1231 | |
| 1232 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1233 | } |
| 1234 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1235 | int lynq_wifi_ap_unhide_ssid(lynq_wifi_index_e idx) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1236 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1237 | char lynq_disable_cmd[128] = {0}; |
| 1238 | char lynq_select_cmd[128] = {0}; |
| 1239 | const char *lynq_unhide_cmd = "SET HIDE_SSID 0"; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1240 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1241 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1242 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1243 | CHECK_WPA_CTRL(CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1244 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1245 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1251 | |
| 1252 | return 0; |
| 1253 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1254 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1255 | int lynq_ap_password_set(lynq_wifi_index_e idx,char *password) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1256 | { |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1257 | int pass_len; |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1258 | char lynq_tmp_cmd[MAX_CMD] = {0}; |
| 1259 | char lynq_wep_tx_keyidx_cmd[MAX_CMD] = {0}; |
qs.xiong | e707432 | 2022-06-27 11:34:53 +0800 | [diff] [blame] | 1260 | if( password == NULL ){ |
| 1261 | return -1; |
| 1262 | } |
| 1263 | pass_len=strlen(password); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1264 | lynq_wifi_auth_s auth = -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1265 | if(pass_len < 8 || pass_len >= 64){ |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1266 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1267 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1268 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1269 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1270 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1271 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1278 | CHECK_WPA_CTRL(CTRL_AP); |
| 1279 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1280 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1293 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1294 | DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1295 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1296 | return 0; |
| 1297 | } |
| 1298 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1299 | int lynq_ap_password_get(lynq_wifi_index_e idx, char *password) |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1300 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1301 | FILE * fp; |
| 1302 | int len, ret; |
| 1303 | int count, index; |
| 1304 | char *split_lines[128] = {0}; |
| 1305 | char *buff, *p; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1306 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1307 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1308 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1309 | 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.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1315 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1316 | buff = alloca(MAX_RET); |
| 1317 | fseek(fp, 0, SEEK_SET); |
| 1318 | len = fread(buff, 1, MAX_RET, fp); |
| 1319 | fclose(fp); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1320 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1321 | 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.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1341 | if (p != NULL) { |
| 1342 | p += 4; |
| 1343 | if (*p == '\"') { |
| 1344 | p++; |
| 1345 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1346 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1347 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1355 | } |
| 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1371 | } |
| 1372 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1373 | static int inner_get_network_auth(int interface, int net_no, lynq_wifi_auth_s *auth) { |
| 1374 | char lynq_auth_str[MAX_RET] = {0}; |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1375 | char lynq_proto_str[MAX_RET] = {0}; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1376 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1377 | 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.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1382 | |
| 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1390 | return 0; |
| 1391 | } |
| 1392 | |
| 1393 | int lynq_sta_ssid_password_set(lynq_wifi_index_e idx, ap_info_s *ap, char *password) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1394 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1395 | 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 | |
| 1431 | int lynq_sta_ssid_password_get(lynq_wifi_index_e idx, ap_info_s *ap, char *password) { // @todo |
| 1432 | |
| 1433 | FILE * fp; |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1434 | int len, ret, network_len; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1435 | int count, index; |
| 1436 | char *split_lines[128] = {0}; |
| 1437 | char *buff, *p; |
| 1438 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1439 | network_len = 0; |
| 1440 | p = NULL; |
| 1441 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1442 | CHECK_IDX(idx, CTRL_STA); |
| 1443 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1444 | if (NULL == password) { |
| 1445 | printf("bad param\n"); |
| 1446 | return -1; |
| 1447 | } |
| 1448 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1449 | 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.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1473 | network_len = buff + index - p; |
| 1474 | break; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | if (strstr(p, ap->ap_ssid) != NULL) { |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1482 | if (index >= len || NULL == p || network_len <= 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1483 | return -1; |
| 1484 | } |
| 1485 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 1486 | count = lynq_split(p, network_len, '\n', split_lines); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1487 | |
| 1488 | ret = -1; |
| 1489 | for(index=0; index < count; index++) { |
| 1490 | p = strstr(split_lines[index], "psk="); |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1491 | if (p != NULL) { |
| 1492 | p += 4; |
| 1493 | if (*p == '\"') { |
| 1494 | p++; |
| 1495 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1496 | } |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1497 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1505 | } |
| 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 | |
| 1523 | static int inner_set_sta_ssid(int net_no, char *sta_ssid) |
| 1524 | { |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1525 | char lynq_wifi_ssid_cmd[80]={0}; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1526 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1527 | if (sta_ssid == NULL) { |
| 1528 | printf("sta_ssid is null\n"); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1529 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1530 | } |
| 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1539 | return 0; |
| 1540 | |
| 1541 | } |
| 1542 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1543 | static int inner_sta_start_stop(int net_no, int start_flag, int save) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1544 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1545 | 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.chen | c29444e | 2022-06-07 18:01:16 +0800 | [diff] [blame] | 1551 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1561 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 1562 | } |
| 1563 | |
| 1564 | if (start_flag == 0) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1565 | sprintf(lynq_disable_cmd,"DISCONNECT"); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1566 | 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 | |
| 1576 | int lynq_wifi_get_sta_ssid(lynq_wifi_index_e idx, char* sta_ssid) |
| 1577 | { |
| 1578 | CHECK_IDX(idx, CTRL_STA); |
| 1579 | |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 1580 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | int lynq_wifi_get_sta_available_ap(lynq_wifi_index_e idx, ap_detail_info_s *info) |
| 1594 | { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1595 | scan_info_s *scan_list = NULL; |
| 1596 | saved_ap_info_s *save_list = NULL; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1597 | 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.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1602 | int i, j, ret; |
| 1603 | |
| 1604 | ret = -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1605 | |
| 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.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1613 | char status[64]; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1614 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1615 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1622 | memcpy(&info->base_info, &ap_info, sizeof (ap_info_s)); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1623 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1631 | lynq_get_connect_ap_rssi(idx, &info->rssi); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1632 | lynq_sta_ssid_password_get(idx, & info->base_info, info->base_info.psw); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1633 | return 0; |
| 1634 | } |
| 1635 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1636 | lynq_wifi_sta_start_scan(idx); |
| 1637 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1638 | if (0 != lynq_get_scan_list(0, &scan_list, &scan_len)) { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1639 | if (NULL != scan_list) |
| 1640 | { |
| 1641 | free(scan_list); |
| 1642 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1643 | return -1; |
| 1644 | } |
| 1645 | |
| 1646 | if (0 != lynq_get_sta_saved_ap(0, &save_list, &save_len)) { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1647 | if (NULL != scan_list) |
| 1648 | { |
| 1649 | free(scan_list); |
| 1650 | } |
| 1651 | if (NULL != save_list) |
| 1652 | { |
| 1653 | free(save_list); |
| 1654 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1655 | 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.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1663 | best_index = i; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1664 | 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.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1680 | ret = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1681 | } |
| 1682 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1683 | 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.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | static 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.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1704 | switch(auth){ |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1705 | case LYNQ_WIFI_AUTH_OPEN: |
| 1706 | { |
| 1707 | sprintf(lynq_auth_cmd,"SET_NETWORK %d key_mgmt NONE", net_no); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1708 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1709 | DO_OK_FAIL_REQUEST(lynq_auth_cmd); |
| 1710 | // DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1711 | break; |
| 1712 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1713 | case LYNQ_WIFI_AUTH_WPA_PSK: |
| 1714 | case LYNQ_WIFI_AUTH_WPA2_PSK: |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1715 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1716 | 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.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1720 | sprintf(lynq_auth_cmd,"SET_NETWORK %d proto RSN", net_no); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1721 | } |
| 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.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1724 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1725 | 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.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1728 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1729 | 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.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1733 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1734 | // DO_OK_FAIL_REQUEST(cmd_save_config); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1735 | break; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1736 | } |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1737 | default: |
| 1738 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1739 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1740 | |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1741 | return 0; |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1742 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1743 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1744 | static 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 | |
| 1756 | int lynq_wifi_get_sta_auth(lynq_wifi_index_e idx, lynq_wifi_auth_s *auth) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1757 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1758 | int net_no; |
| 1759 | CHECK_IDX(idx, CTRL_STA); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1760 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1761 | net_no = inner_get_curr_net_no(CTRL_STA); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1762 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1763 | if (net_no < 0) { |
| 1764 | return -1; |
| 1765 | } |
| 1766 | |
| 1767 | return inner_get_network_auth(CTRL_STA, net_no, auth); |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1768 | } |
| 1769 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1770 | int lynq_wifi_sta_connect(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth, char *psw) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1771 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1772 | int count, net_no, index; |
| 1773 | int net_no_list[128]; |
| 1774 | lynq_wifi_auth_s net_auth; |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1775 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1776 | if (ssid == NULL || *ssid == '\0') { |
| 1777 | printf("bad ssid\n"); |
| 1778 | return -1; |
| 1779 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1780 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1781 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1787 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1788 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1818 | } |
| 1819 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1820 | int lynq_wifi_sta_disconnect(lynq_wifi_index_e idx, char *ssid) |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1821 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1822 | ap_info_s ap; |
| 1823 | curr_status_info curr_state; |
| 1824 | ap.ap_ssid[0] = '\0'; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1825 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1826 | if (ssid == NULL || *ssid == '\0') { |
| 1827 | return -1; |
| 1828 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1829 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1830 | CHECK_IDX(idx, CTRL_STA); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1831 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1832 | curr_state.ap = ≈ |
you.chen | b4b121c | 2022-05-06 17:50:16 +0800 | [diff] [blame] | 1833 | curr_state.state = NULL; |
| 1834 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1835 | if (inner_get_status_info(CTRL_STA, &curr_state) != 0) { |
| 1836 | return 0; |
| 1837 | } |
qs.xiong | 1af5daf | 2022-03-14 09:12:12 -0400 | [diff] [blame] | 1838 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1839 | 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.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1844 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1845 | |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1846 | int lynq_wifi_sta_start(lynq_wifi_index_e idx) |
| 1847 | { |
you.chen | c29444e | 2022-06-07 18:01:16 +0800 | [diff] [blame] | 1848 | const char *lynq_reconfigure_cmd = "wpa_cli -iwpa_wlan0_cmd -p/var/run/ IFNAME=wlan0 RECONFIGURE /data/wifi/wg870/wpa_supplicant.conf"; |
you.chen | 01bfac3 | 2022-06-07 10:36:00 +0800 | [diff] [blame] | 1849 | const char *lynq_reconnect_cmd = "RECONNECT"; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1850 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1851 | CHECK_IDX(idx, CTRL_STA); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1852 | CHECK_WPA_CTRL(CTRL_STA); |
| 1853 | |
| 1854 | system("connmanctl enable wifi"); |
| 1855 | |
you.chen | a6fa5b2 | 2022-05-18 10:28:19 +0800 | [diff] [blame] | 1856 | if (system("ifconfig | grep -q wlan0") != 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1857 | return -1; |
| 1858 | } |
qs.xiong | 9c99fa9 | 2022-03-15 08:03:26 -0400 | [diff] [blame] | 1859 | |
you.chen | 01bfac3 | 2022-06-07 10:36:00 +0800 | [diff] [blame] | 1860 | DO_OK_FAIL_REQUEST(cmd_remove_all); |
you.chen | c29444e | 2022-06-07 18:01:16 +0800 | [diff] [blame] | 1861 | system(lynq_reconfigure_cmd); |
you.chen | 01bfac3 | 2022-06-07 10:36:00 +0800 | [diff] [blame] | 1862 | DO_OK_FAIL_REQUEST(lynq_reconnect_cmd); |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1863 | |
| 1864 | return 0; |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1865 | } |
| 1866 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1867 | int lynq_wifi_sta_stop(lynq_wifi_index_e idx) |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1868 | { |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1869 | char lynq_disable_network_cmd[MAX_CMD]; |
| 1870 | curr_status_info curr_state; |
| 1871 | ap_info_s ap_info; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1872 | |
you.chen | a6cd55a | 2022-05-08 12:20:18 +0800 | [diff] [blame] | 1873 | CHECK_IDX(idx, CTRL_STA); |
| 1874 | CHECK_WPA_CTRL(CTRL_STA); |
| 1875 | |
| 1876 | curr_state.ap = &ap_info; |
| 1877 | curr_state.state = NULL; |
| 1878 | |
| 1879 | if (0 != inner_get_status_info(CTRL_STA, &curr_state) || curr_state.net_no < 0) { |
| 1880 | return 0; |
| 1881 | } |
| 1882 | |
| 1883 | sprintf(lynq_disable_network_cmd, "DISABLE_NETWORK %d", curr_state.net_no); |
| 1884 | DO_OK_FAIL_REQUEST(lynq_disable_network_cmd); |
| 1885 | |
| 1886 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 1887 | |
| 1888 | return 0; |
| 1889 | // return system("connmanctl disable wifi"); |
qs.xiong | f1b525b | 2022-03-31 00:58:23 -0400 | [diff] [blame] | 1890 | } |
qs.xiong | 7a105ce | 2022-03-02 09:43:11 -0500 | [diff] [blame] | 1891 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1892 | //static int inner_get_sta_info(lynq_wifi_index_e idx, const char * bssid, device_info_s *dev) { |
| 1893 | // int i, count; |
| 1894 | // char *p; |
| 1895 | // const char * FLAG_SSID = "ssid="; |
| 1896 | // const char * FLAG_SBSID = "bssid="; |
| 1897 | // const char * FLAG_KEY_MGMT = "key_mgmt="; |
| 1898 | // const char * FLAG_FREQ = "freq="; |
| 1899 | // char lynq_sta_cmd[MAX_CMD]; |
| 1900 | // char *split_lines[128] = {0}; |
| 1901 | |
| 1902 | // CHECK_WPA_CTRL(CTRL_AP); |
| 1903 | |
| 1904 | // sprintf(lynq_sta_cmd, "STA %s", bssid); |
| 1905 | |
| 1906 | // DO_REQUEST(lynq_sta_cmd); |
| 1907 | |
| 1908 | // count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| 1909 | |
| 1910 | // for(i=0; i < count; i++) { |
| 1911 | // p = strstr(split_lines[i], FLAG_SSID); |
| 1912 | // if (p != NULL) { |
| 1913 | // strcpy(ap->ap_ssid, p + strlen(FLAG_SSID)); |
| 1914 | // continue; |
| 1915 | // } |
| 1916 | // } |
| 1917 | |
| 1918 | // lynq_get_interface_ip(idx, ap->ap_ip); |
| 1919 | // lynq_ap_password_set(idx, ap->psw); |
| 1920 | |
| 1921 | // return 0; |
| 1922 | //} |
| 1923 | |
| 1924 | static int inner_get_status_info_ap (int interface, ap_info_s *ap) { |
| 1925 | curr_status_info curr_state; |
| 1926 | curr_state.ap = ap; |
| 1927 | curr_state.state = NULL; |
| 1928 | return inner_get_status_info(interface, &curr_state); |
| 1929 | } |
| 1930 | |
| 1931 | int lynq_get_ap_device_list(lynq_wifi_index_e idx, ap_info_s **ap, device_info_s ** list,int * len) |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1932 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1933 | int ip_count, index, i, line_count; |
| 1934 | const char *lynq_first_sta_cmd = "STA-FIRST"; |
| 1935 | char lynq_next_sta_cmd[MAX_CMD]; |
| 1936 | char *bssid[1024] = {0}; |
| 1937 | char *mac_list[128] = {0}; |
| 1938 | char *ip_list[128] = {0}; |
| 1939 | char *split_lines[128] = {0}; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1940 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1941 | CHECK_IDX(idx, CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1942 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1943 | CHECK_WPA_CTRL(CTRL_AP); |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 1944 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1945 | // ap_info_s * tmp_ap; |
| 1946 | // device_info_s * tmp_list; |
| 1947 | if (ap == NULL || list == NULL || len == NULL) { |
| 1948 | printf("bad input param"); |
| 1949 | return -1; |
| 1950 | } |
| 1951 | |
| 1952 | // ap = &tmp_ap; |
| 1953 | // list = &tmp_list; |
| 1954 | *ap = malloc(sizeof (ap_info_s)); |
| 1955 | |
| 1956 | if (inner_get_status_info_ap (CTRL_AP, *ap) != 0) { |
| 1957 | return -1; |
| 1958 | } |
| 1959 | |
| 1960 | lynq_get_interface_ip(idx, (*ap)->ap_ip); |
| 1961 | lynq_ap_password_get(idx, (*ap)->psw); |
| 1962 | |
| 1963 | ip_count = get_ip_mac_list(mac_list, ip_list); |
| 1964 | printf("get count %d\n", ip_count); |
| 1965 | |
| 1966 | DO_REQUEST(lynq_first_sta_cmd); |
| 1967 | |
| 1968 | index = 0; |
| 1969 | while (reply_len > 0) { |
| 1970 | if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 1971 | break; |
| 1972 | } |
| 1973 | line_count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| 1974 | bssid[index] = malloc(strlen(split_lines[0]) + 1); |
| 1975 | strcpy(bssid[index], split_lines[0]); |
| 1976 | index++; |
| 1977 | sprintf(lynq_next_sta_cmd, "STA-NEXT %s", split_lines[0]); |
| 1978 | reply_len = MAX_RET; |
| 1979 | cmd_reply[0] = '\0'; |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 1980 | ret = wpa_ctrl_request(lynq_wpa_ctrl, lynq_next_sta_cmd, strlen(lynq_next_sta_cmd), cmd_reply, &reply_len, NULL); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 1981 | if (ret != 0 || memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 1982 | printf("run %s fail \n", lynq_next_sta_cmd); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 1983 | break; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | *len = index; |
| 1988 | |
| 1989 | *list = malloc(sizeof(device_info_s) * (*len)); |
| 1990 | for (index=0; index < *len; index++) { |
| 1991 | strcpy((*list)[index].sta_mac, bssid[index]); |
| 1992 | for(i=0;i < ip_count; i++ ) { |
| 1993 | if (strcmp(bssid[index], mac_list[i]) == 0) { |
| 1994 | strcpy((*list)[index].sta_ip, ip_list[i]); |
| 1995 | break; |
| 1996 | } |
| 1997 | } |
| 1998 | get_hostname_by_ip((*list)[index].sta_ip, list[index]->hostname); |
| 1999 | (*list)[index].status = LYNQ_WIFI_STATUS_CONNECT; |
| 2000 | free(bssid[index]); |
| 2001 | } |
| 2002 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2003 | free_ip_mac_list_mem(mac_list, 128, ip_list, 128); |
| 2004 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2005 | return 0; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2006 | } |
| 2007 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2008 | int lynq_get_scan_list(lynq_wifi_index_e idx, scan_info_s ** list,int * len) |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2009 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2010 | int i, count, index, count_words; |
| 2011 | const char *lynq_scan_result_cmd = "SCAN_RESULTS"; |
| 2012 | char *split_lines[128] = {0}; |
| 2013 | char *split_words[128] = {0}; |
| 2014 | scan_info_s * p; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2015 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2016 | if (list == NULL || len == NULL) { |
| 2017 | return -1; |
| 2018 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2019 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2020 | for (i =0; i < 50 && g_sta_scan_finish_flag == 0; i++) |
| 2021 | { |
| 2022 | usleep(100 * 1000); |
| 2023 | } |
| 2024 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2025 | CHECK_IDX(idx, CTRL_STA); |
| 2026 | |
| 2027 | CHECK_WPA_CTRL(CTRL_STA); |
| 2028 | |
| 2029 | DO_REQUEST(lynq_scan_result_cmd); |
| 2030 | |
| 2031 | count = lynq_split(cmd_reply, reply_len, '\n', split_lines); |
| 2032 | *len = count - 1; |
| 2033 | *list = malloc(sizeof (scan_info_s) * *len); |
| 2034 | |
| 2035 | count_words = lynq_split(split_lines[0], strlen(split_lines[0]), '/', split_words); //@todo get with header |
| 2036 | for (index=0; index <count_words; index++) { |
| 2037 | printf("----header: %s\n", split_words[index]); |
| 2038 | } |
| 2039 | |
| 2040 | for(index = 1;index < count; index++) { |
| 2041 | printf("---- %s\n",split_lines[index]); |
| 2042 | count_words = lynq_split(split_lines[index], strlen(split_lines[index]), '\t', split_words); |
| 2043 | if (count_words < 4) |
| 2044 | continue; |
| 2045 | printf("count: %d, %s\n", count_words, split_words[0]); |
| 2046 | //bssid / frequency / signal level / flags / ssid |
| 2047 | p = (*list) + index - 1; |
| 2048 | strcpy(p->mac, split_words[0]); |
| 2049 | p->band = convert_band_from_freq(atoi(split_words[1])); |
| 2050 | p->rssi = -1 * atoi( split_words[2]); |
| 2051 | p->auth = convert_max_auth_from_flag(split_words[3]); |
| 2052 | strcpy(p->ssid, split_words[4]); |
| 2053 | } |
| 2054 | |
| 2055 | return 0; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2056 | } |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2057 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2058 | int lynq_sta_forget_ap(lynq_wifi_index_e idx, char *ssid, lynq_wifi_auth_s auth) |
| 2059 | { |
| 2060 | int count, net_no, index; |
| 2061 | int net_no_list[128]; |
| 2062 | lynq_wifi_auth_s net_auth; |
| 2063 | char lynq_remove_cmd[MAX_CMD]; |
| 2064 | |
| 2065 | if (ssid == NULL || *ssid == '\0') { |
| 2066 | printf("bad ssid\n"); |
| 2067 | return -1; |
| 2068 | } |
| 2069 | |
| 2070 | CHECK_IDX(idx, CTRL_STA); |
| 2071 | |
| 2072 | CHECK_WPA_CTRL(CTRL_STA); |
| 2073 | |
| 2074 | net_no = -1; |
| 2075 | count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, ssid); |
| 2076 | |
| 2077 | for (index=0; index < count; index++) { |
| 2078 | net_auth = -1; |
| 2079 | if (0 == inner_get_network_auth(CTRL_STA, net_no_list[index], &net_auth) && net_auth == auth) { |
| 2080 | net_no = net_no_list[index]; |
| 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | if (net_no < 0) { |
| 2086 | return 0; |
| 2087 | } |
| 2088 | |
| 2089 | sprintf(lynq_remove_cmd, "REMOVE_NETWORK %d", net_no); |
| 2090 | |
| 2091 | DO_OK_FAIL_REQUEST(lynq_remove_cmd); |
| 2092 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 2093 | |
| 2094 | return 0; |
| 2095 | } |
| 2096 | |
| 2097 | int lynq_get_sta_saved_ap(lynq_wifi_index_e idx, saved_ap_info_s ** list, int * len) |
| 2098 | { |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 2099 | int count, index, ssid_len; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2100 | int net_no_list[128]; |
| 2101 | char freq[16]; |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 2102 | char *ssid_ptr; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2103 | |
| 2104 | if (list == NULL || len == NULL) { |
| 2105 | printf("bad param\n"); |
| 2106 | return -1; |
| 2107 | } |
| 2108 | |
| 2109 | CHECK_IDX(idx, CTRL_STA); |
| 2110 | |
| 2111 | // CHECK_WPA_CTRL(CTRL_STA); |
| 2112 | |
| 2113 | count = lynq_get_network_number_list(idx, CTRL_STA, net_no_list, NULL); |
| 2114 | printf("count is %d\n", count); |
| 2115 | |
| 2116 | *list = malloc(sizeof (saved_ap_info_s) * count); |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 2117 | memset(*list, 0, sizeof (saved_ap_info_s) * count); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2118 | *len = count; |
| 2119 | |
| 2120 | for (index=0; index < count; index++) { |
| 2121 | printf("to get ssid %d\n", index); |
| 2122 | inner_get_param(CTRL_STA, net_no_list[index], "ssid", (*list)[index].base_info.ap_ssid); |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 2123 | |
| 2124 | ssid_ptr = (*list)[index].base_info.ap_ssid; |
| 2125 | ssid_len = strlen(ssid_ptr); |
| 2126 | if (ssid_ptr[0] == '\"') { |
| 2127 | memmove(ssid_ptr, ssid_ptr + 1, ssid_len - 1); |
| 2128 | ssid_len -= 1; |
| 2129 | } |
| 2130 | if (ssid_len > 0 && ssid_ptr[ssid_len - 1] == '\"') { |
| 2131 | ssid_ptr[ssid_len - 1] = '\0'; |
| 2132 | } |
| 2133 | |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2134 | inner_get_param(CTRL_STA, net_no_list[index], "bssid", (*list)[index].base_info.ap_mac); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2135 | inner_get_network_auth(CTRL_STA, net_no_list[index], &(*list)[index].base_info.auth); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2136 | if (inner_get_param(CTRL_STA, net_no_list[index], "frequency", freq) == 0) { |
| 2137 | (*list)[index].base_info.band = convert_band_from_freq(atoi(freq)); |
| 2138 | } |
| 2139 | else { |
| 2140 | (*list)[index].base_info.band = -1; |
| 2141 | } |
| 2142 | |
you.chen | 755332b | 2022-08-06 16:59:10 +0800 | [diff] [blame] | 2143 | lynq_sta_ssid_password_get(idx, & (*list)[index].base_info, (*list)[index].base_info.psw); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | return 0; |
| 2147 | } |
| 2148 | |
| 2149 | int lynq_wifi_sta_start_scan(lynq_wifi_index_e idx) |
| 2150 | { |
| 2151 | const char *lynq_scan_cmd = "SCAN"; |
| 2152 | |
| 2153 | CHECK_IDX(idx, CTRL_STA); |
| 2154 | |
| 2155 | CHECK_WPA_CTRL(CTRL_STA); |
| 2156 | |
| 2157 | DO_OK_FAIL_REQUEST(lynq_scan_cmd); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2158 | g_sta_scan_finish_flag = 0; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2159 | |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | int lynq_reg_ap_event_callback(void * priv, AP_CALLBACK_FUNC_PTR cb) { |
| 2164 | if (cb == NULL) { |
| 2165 | return -1; |
| 2166 | } |
| 2167 | |
| 2168 | g_ap_callback_priv = priv; |
| 2169 | g_ap_callback_func = cb; |
| 2170 | |
| 2171 | return 0; |
| 2172 | } |
| 2173 | |
| 2174 | int lynq_unreg_ap_event_callback(void * priv) { |
| 2175 | if (g_ap_callback_priv == priv) { |
| 2176 | g_ap_callback_func = NULL; |
| 2177 | g_ap_callback_priv = NULL; |
| 2178 | return 0; |
| 2179 | } |
| 2180 | return -1; |
| 2181 | } |
| 2182 | |
| 2183 | int lynq_reg_sta_event_callback(void * priv, STA_CALLBACK_FUNC_PTR cb){ |
| 2184 | if (cb == NULL) { |
| 2185 | return -1; |
| 2186 | } |
| 2187 | |
| 2188 | g_sta_callback_priv = priv; |
| 2189 | g_sta_callback_func = cb; |
| 2190 | |
| 2191 | return 0; |
| 2192 | } |
| 2193 | |
| 2194 | int lynq_unreg_sta_event_callback(void * priv) { |
| 2195 | if (g_sta_callback_priv == priv) { |
| 2196 | g_sta_callback_func = NULL; |
| 2197 | g_sta_callback_priv = NULL; |
| 2198 | return 0; |
| 2199 | } |
| 2200 | return -1; |
| 2201 | } |
| 2202 | |
| 2203 | |
| 2204 | static int inner_get_status_info_state (int interface, char *state) { |
| 2205 | curr_status_info curr_state; |
| 2206 | curr_state.ap = NULL; |
| 2207 | curr_state.state = state; |
| 2208 | return inner_get_status_info(interface, &curr_state); |
| 2209 | } |
| 2210 | |
| 2211 | int lynq_get_ap_status(lynq_wifi_index_e idx, lynq_wifi_ap_run_status_s * ap_status) |
| 2212 | { |
| 2213 | char state[MAX_CMD]; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2214 | CHECK_IDX(idx, CTRL_AP); |
| 2215 | |
| 2216 | if (inner_get_status_info_state(CTRL_AP, state) != 0) { |
| 2217 | *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
| 2221 | if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) { |
| 2222 | *ap_status = LYNQ_WIFI_AP_STATUS_ENABLE; |
| 2223 | } |
| 2224 | else { |
| 2225 | *ap_status = LYNQ_WIFI_AP_STATUS_DISABLE; |
| 2226 | } |
| 2227 | |
| 2228 | return 0; |
| 2229 | } |
| 2230 | |
| 2231 | int lynq_get_sta_status(lynq_wifi_index_e idx, lynq_wifi_sta_run_status_s * sta_status) { |
| 2232 | char state[MAX_CMD]; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2233 | CHECK_IDX(idx, CTRL_STA); |
| 2234 | |
| 2235 | if (inner_get_status_info_state(CTRL_STA, state) != 0) { |
| 2236 | *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE; |
| 2237 | return 0; |
| 2238 | } |
| 2239 | |
| 2240 | if (memcmp(state, STATE_COMPLETED, strlen (STATE_COMPLETED)) == 0) { |
| 2241 | *sta_status = LYNQ_WIFI_STA_STATUS_ENABLE; |
| 2242 | } |
| 2243 | else { |
| 2244 | *sta_status = LYNQ_WIFI_STA_STATUS_DISABLE; |
| 2245 | } |
| 2246 | |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
| 2250 | int lynq_get_country_code(lynq_wifi_index_e idx, char * country_code) { |
| 2251 | // CHECK_IDX(idx, CTRL_AP); |
| 2252 | // int ret = 0; |
| 2253 | // size_t reply_len = MAX_RET; |
| 2254 | // char cmd_reply[MAX_RET]={0}; |
| 2255 | // const char * cmd_str = "GET country"; |
| 2256 | // struct wpa_ctrl *s_lynq_wpa_ctrl = NULL; |
| 2257 | // do{ |
| 2258 | // if (NULL == s_lynq_wpa_ctrl) { |
| 2259 | // s_lynq_wpa_ctrl = wpa_ctrl_open("/var/run/wpa_wlan0_cmd"); |
| 2260 | // if (NULL == s_lynq_wpa_ctrl ) { |
| 2261 | // printf("wpa_ctrl_open fail\n"); |
| 2262 | // return -1; |
| 2263 | // } |
| 2264 | // } |
| 2265 | // }while(0); |
| 2266 | |
| 2267 | // do { |
| 2268 | // reply_len = MAX_RET; |
| 2269 | // cmd_reply[0] = '\0'; |
| 2270 | // printf("to call [%s]\n", cmd_str); |
qs.xiong | fdbc58e | 2022-09-29 11:37:32 +0800 | [diff] [blame] | 2271 | // ret = wpa_ctrl_request(s_lynq_wpa_ctrl, cmd_str, strlen(cmd_str), cmd_reply, &reply_len, NULL); |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2272 | // if (ret != 0) { |
| 2273 | // printf("call ##cmd_str fail %d\n", ret); |
| 2274 | // return ret; |
| 2275 | // } |
| 2276 | // cmd_reply[reply_len+1] = '\0'; |
| 2277 | // printf("cmd replay [ %s ]\n", cmd_reply); |
| 2278 | // }while(0); |
| 2279 | |
| 2280 | FILE *fp; |
| 2281 | size_t i = 0; |
| 2282 | char lynq_cmd_ret[MAX_RET]={0}; |
| 2283 | |
| 2284 | // CHECK_IDX(idx, CTRL_AP); |
| 2285 | |
| 2286 | if((fp=popen("wl country","r"))==NULL) |
| 2287 | { |
| 2288 | perror("popen error!"); |
| 2289 | return -1; |
| 2290 | } |
| 2291 | if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0) |
| 2292 | { |
| 2293 | perror("fread fail!"); |
| 2294 | return -1; |
| 2295 | } |
| 2296 | |
| 2297 | for(i=0; i < strlen(lynq_cmd_ret); i++) { |
| 2298 | if (lynq_cmd_ret[i] == ' ') { |
| 2299 | lynq_cmd_ret[i] = '\0'; |
| 2300 | break; |
| 2301 | } |
| 2302 | } |
| 2303 | |
| 2304 | strcpy(country_code,lynq_cmd_ret); |
| 2305 | printf("---country code %s\n", country_code); |
| 2306 | |
| 2307 | int ret=pclose(fp); |
| 2308 | if(ret==-1) |
| 2309 | { |
| 2310 | perror("close file faild"); |
| 2311 | } |
| 2312 | |
| 2313 | return 0; |
| 2314 | } |
| 2315 | |
| 2316 | int lynq_set_country_code(lynq_wifi_index_e idx, char * country_code) { |
| 2317 | // const char * cmd_str = "GET country"; |
| 2318 | // CHECK_IDX(idx, CTRL_AP); |
| 2319 | // CHECK_WPA_CTRL(CTRL_STA); |
| 2320 | |
| 2321 | // DO_REQUEST(cmd_str); |
| 2322 | // printf("result %s\n", cmd_reply); |
| 2323 | |
| 2324 | if (country_code == NULL || *country_code == '\0') { |
| 2325 | printf("bad country code\n"); |
| 2326 | return -1; |
| 2327 | } |
| 2328 | |
| 2329 | char lynq_country_cmd[MAX_CMD]; |
| 2330 | sprintf(lynq_country_cmd, "wl country %s", country_code); |
| 2331 | if (system(lynq_country_cmd) == 0) { |
| 2332 | return 0; |
| 2333 | } |
| 2334 | |
| 2335 | return -1; |
| 2336 | } |
| 2337 | |
| 2338 | int lynq_get_connect_ap_mac(lynq_wifi_index_e idx,char *mac) |
| 2339 | { |
| 2340 | if (mac == NULL) { |
| 2341 | return -1; |
| 2342 | } |
| 2343 | |
| 2344 | CHECK_IDX(idx, CTRL_STA); |
| 2345 | ap_info_s ap; |
| 2346 | ap.ap_mac[0] = '\0'; |
| 2347 | |
| 2348 | if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) { |
| 2349 | return -1; |
| 2350 | } |
| 2351 | strcpy(mac, ap.ap_mac); |
| 2352 | |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
| 2356 | int lynq_get_interface_ip(lynq_wifi_index_e idx, char *ip) |
| 2357 | { |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2358 | struct ifaddrs *ifaddr_header, *ifaddr; |
| 2359 | struct in_addr * ifa; |
| 2360 | const char * ifaName = "wlan0"; |
| 2361 | if (ip == NULL) |
| 2362 | { |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2363 | return -1; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2364 | } |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2365 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2366 | if (idx == 1) { |
you.chen | 6c2dd9c | 2022-05-16 17:55:28 +0800 | [diff] [blame] | 2367 | ifaName = "tether"; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2368 | } |
| 2369 | else if (idx != 0) { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2370 | return -1; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2371 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2372 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2373 | if (getifaddrs(&ifaddr_header) == -1) |
| 2374 | { |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2375 | perror("getifaddrs"); |
| 2376 | return -1; |
| 2377 | //exit(EXIT_FAILURE); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2378 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2379 | |
| 2380 | |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2381 | for (ifaddr = ifaddr_header; ifaddr != NULL; ifaddr = ifaddr->ifa_next) |
| 2382 | { |
| 2383 | if (ifaddr->ifa_addr == NULL) |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2384 | continue; |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2385 | if((strcmp(ifaddr->ifa_name,ifaName)==0)) |
| 2386 | { |
| 2387 | if (ifaddr->ifa_addr->sa_family==AF_INET) // check it is IP4 |
| 2388 | { |
| 2389 | // is a valid IP4 Address |
| 2390 | ifa=&((struct sockaddr_in *)ifaddr->ifa_addr)->sin_addr; |
| 2391 | inet_ntop(AF_INET, ifa, ip, INET_ADDRSTRLEN); |
| 2392 | printf("%s IP Address %s/n", ifaddr->ifa_name, ip); |
| 2393 | freeifaddrs(ifaddr_header); |
| 2394 | printf("ip %s\n", ip); |
| 2395 | return 0; |
| 2396 | } |
| 2397 | } |
| 2398 | } |
| 2399 | return -1; |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | int lynq_get_interface_mac(lynq_wifi_index_e idx,char *mac) |
| 2403 | { |
| 2404 | int count; |
| 2405 | size_t i; |
| 2406 | char *split_words[128] = {0}; |
| 2407 | const char *lynq_get_mac_cmd = "DRIVER MACADDR"; |
| 2408 | |
| 2409 | CHECK_WPA_CTRL(idx); |
| 2410 | |
| 2411 | DO_REQUEST(lynq_get_mac_cmd); |
| 2412 | |
| 2413 | if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 2414 | return -1; |
| 2415 | } |
| 2416 | |
| 2417 | count = lynq_split(cmd_reply, reply_len, '=', split_words); |
| 2418 | |
| 2419 | if (count < 2) { |
| 2420 | return -1; |
| 2421 | } |
| 2422 | |
| 2423 | for (i=0; i < strlen(split_words[1]); i++ ) { |
| 2424 | if (split_words[1][i] != ' ') { |
| 2425 | break; |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | strcpy(mac, split_words[1] + i); |
| 2430 | |
| 2431 | return 0; |
| 2432 | } |
| 2433 | |
| 2434 | int lynq_get_connect_ap_rssi(lynq_wifi_index_e idx,int * rssi) |
| 2435 | { |
| 2436 | // int count; |
| 2437 | // char *split_words[128] = {0}; |
| 2438 | // const char *lynq_get_rssi_cmd = "DRIVER RSSI"; |
| 2439 | |
| 2440 | // if (rssi == NULL) { |
| 2441 | // return -1; |
| 2442 | // } |
| 2443 | |
| 2444 | // CHECK_IDX(idx, CTRL_STA); |
| 2445 | |
| 2446 | // CHECK_WPA_CTRL(CTRL_STA); |
| 2447 | |
| 2448 | // DO_REQUEST(lynq_get_rssi_cmd); |
| 2449 | |
| 2450 | // if (memcmp(cmd_reply, "FAIL", 4) == 0) { |
| 2451 | // return -1; |
| 2452 | // } |
| 2453 | |
| 2454 | // count = lynq_split(cmd_reply, reply_len, ' ', split_words); |
| 2455 | |
| 2456 | // if (count < 2) { |
| 2457 | // return -1; |
| 2458 | // } |
| 2459 | |
| 2460 | // *rssi = atoi(split_words[1]) * -1; |
| 2461 | |
| 2462 | FILE *fp; |
| 2463 | size_t i = 0; |
| 2464 | char lynq_cmd_ret[MAX_RET]={0}; |
| 2465 | |
| 2466 | // CHECK_IDX(idx, CTRL_AP); |
qs.xiong | ff0ae0f | 2022-10-11 15:47:14 +0800 | [diff] [blame] | 2467 | /*******change other cmd to get rssi******* |
| 2468 | * |
| 2469 | *wl rssi ---> wl -i wlan0 rssi |
| 2470 | * |
| 2471 | ***** change by qs.xiong 20221011*******/ |
| 2472 | if((fp=popen("wl -i wlan0 rssi","r"))==NULL) |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2473 | { |
| 2474 | perror("popen error!"); |
| 2475 | return -1; |
| 2476 | } |
| 2477 | if((fread(lynq_cmd_ret,sizeof(lynq_cmd_ret),1,fp))<0) |
| 2478 | { |
| 2479 | perror("fread fail!"); |
| 2480 | return -1; |
| 2481 | } |
you.chen | 9f17e4d | 2022-06-06 17:18:18 +0800 | [diff] [blame] | 2482 | *rssi = atoi(lynq_cmd_ret) * -1; |
qs.xiong | ff0ae0f | 2022-10-11 15:47:14 +0800 | [diff] [blame] | 2483 | /****** if got rssi is 0,means sta didn't connected any device****/ |
| 2484 | if(*rssi == 0) |
| 2485 | { |
| 2486 | printf("sta didn't connected any ap device,please check connection\n"); |
| 2487 | } |
you.chen | 3502019 | 2022-05-06 11:30:57 +0800 | [diff] [blame] | 2488 | |
| 2489 | return 0; |
| 2490 | } |
| 2491 | |
| 2492 | int lynq_get_connect_ap_band(lynq_wifi_index_e idx, lynq_wifi_band_m * band) |
| 2493 | { |
| 2494 | if (band == NULL) { |
| 2495 | return -1; |
| 2496 | } |
| 2497 | |
| 2498 | CHECK_IDX(idx, CTRL_STA); |
| 2499 | ap_info_s ap; |
| 2500 | ap.band = -1; |
| 2501 | |
| 2502 | if (inner_get_status_info_ap(CTRL_STA, &ap) != 0) { |
| 2503 | return -1; |
| 2504 | } |
| 2505 | *band = ap.band; |
| 2506 | |
| 2507 | return 0; |
qs.xiong | 97fa59b | 2022-04-07 05:41:29 -0400 | [diff] [blame] | 2508 | } |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2509 | |
| 2510 | int lynq_get_connect_ap_ip(lynq_wifi_index_e idx, char *ip) |
| 2511 | { |
| 2512 | int index; |
| 2513 | int ip_count = 0; |
| 2514 | char bssid[1024] = {0}; |
| 2515 | char *mac_list[128] = {0}; |
| 2516 | char *ip_list[128] = {0}; |
| 2517 | |
| 2518 | if (ip == NULL) |
| 2519 | { |
| 2520 | printf("invalid param"); |
| 2521 | return -1; |
| 2522 | } |
| 2523 | |
| 2524 | CHECK_IDX(idx, CTRL_STA); |
| 2525 | |
| 2526 | if (lynq_get_connect_ap_mac(idx, bssid) != 0) |
| 2527 | { |
| 2528 | return -1; |
| 2529 | } |
| 2530 | |
| 2531 | ip_count = get_ip_mac_list(mac_list, ip_list); |
| 2532 | |
| 2533 | for (index=0; index < ip_count; index++) |
| 2534 | { |
| 2535 | if (strcmp(bssid, mac_list[index]) == 0) |
| 2536 | { |
| 2537 | strcpy(ip, ip_list[index]); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2538 | |
| 2539 | free_ip_mac_list_mem(mac_list, 128, ip_list, 128); |
| 2540 | |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2541 | return 0; |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | printf("not found\n"); |
you.chen | 9ac6639 | 2022-08-06 17:01:16 +0800 | [diff] [blame] | 2546 | free_ip_mac_list_mem(mac_list, 128, ip_list, 128); |
| 2547 | |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2548 | return -1; |
| 2549 | } |
| 2550 | |
qs.xiong | 026c5c7 | 2022-10-17 11:15:45 +0800 | [diff] [blame] | 2551 | int lynq_ap_connect_num(int sta_number) |
| 2552 | { |
| 2553 | char lynq_limit_cmd[32]={0}; |
| 2554 | int ret; |
| 2555 | if((sta_number < 1 ) && (sta_number > 15)){ |
| 2556 | printf("sta_number: not in range\n",sta_number); |
| 2557 | return -1; |
| 2558 | } |
| 2559 | sprintf(lynq_limit_cmd,"wl maxassoc %d", sta_number); |
| 2560 | ret = system(lynq_limit_cmd); |
| 2561 | if(ret != 0){ |
| 2562 | printf("cmd of limit ap devices number error\n"); |
| 2563 | } |
| 2564 | return 0; |
| 2565 | } |
you.chen | f58b3c9 | 2022-06-21 16:53:48 +0800 | [diff] [blame] | 2566 | |
qs.xiong | 7790555 | 2022-10-17 11:19:57 +0800 | [diff] [blame] | 2567 | int lynq_enable_acs(lynq_wifi_index_e idx,int acs_mode) |
| 2568 | { |
| 2569 | |
| 2570 | char lynq_wifi_acs_cmd[128]={0}; |
| 2571 | char lynq_cmd_mode[128]={0}; |
| 2572 | char lynq_cmd_slect[128]={0}; |
| 2573 | |
| 2574 | if((acs_mode != 2) && (acs_mode != 5)){ |
| 2575 | PRINT_AND_RETURN_VALUE("set acs_mode is error",-1); |
| 2576 | } |
| 2577 | |
| 2578 | if (lynq_check_network_number(idx, CTRL_AP, AP_NETWORK_0) != 0) { |
| 2579 | return -1; |
| 2580 | } |
| 2581 | |
| 2582 | CHECK_IDX(idx, CTRL_AP); |
| 2583 | |
| 2584 | CHECK_WPA_CTRL(CTRL_AP); |
| 2585 | |
| 2586 | sprintf(lynq_wifi_acs_cmd,"SET_NETWORK %d frequency %d", AP_NETWORK_0, acs_mode); |
| 2587 | sprintf(lynq_cmd_mode, "SET_NETWORK %d mode 2", AP_NETWORK_0); |
| 2588 | sprintf(lynq_cmd_slect, "SELECT_NETWORK %d", AP_NETWORK_0); |
| 2589 | |
| 2590 | DO_OK_FAIL_REQUEST(cmd_disconnect); |
| 2591 | DO_OK_FAIL_REQUEST(lynq_wifi_acs_cmd); |
| 2592 | DO_OK_FAIL_REQUEST(lynq_cmd_mode); |
| 2593 | DO_OK_FAIL_REQUEST(cmd_save_config); |
| 2594 | DO_OK_FAIL_REQUEST(lynq_cmd_slect); |
| 2595 | |
| 2596 | return 0; |
| 2597 | } |
you.chen | 0f5c643 | 2022-11-07 18:31:14 +0800 | [diff] [blame] | 2598 | //you.chen add for tv-box start |
| 2599 | static int exec_cmd(const char *str_cmd, char * str_cmd_ret, size_t max_len) { |
| 2600 | FILE *fp; |
| 2601 | //printf("to exec cmd:%s\n", str_cmd); |
| 2602 | if((fp=popen(str_cmd,"r"))==NULL) |
| 2603 | { |
| 2604 | perror("popen error!"); |
| 2605 | return -1; |
| 2606 | } |
| 2607 | if((fread(str_cmd_ret,max_len,1,fp))<0) |
| 2608 | { |
| 2609 | perror("fread fail!"); |
| 2610 | fclose(fp); |
| 2611 | return -1; |
| 2612 | } |
| 2613 | fclose(fp); |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
| 2617 | static int get_netmask_length(const char* mask) |
| 2618 | { |
| 2619 | int masklen=0, i=0; |
| 2620 | int netmask=0; |
| 2621 | |
| 2622 | if(mask == NULL) |
| 2623 | { |
| 2624 | return 0; |
| 2625 | } |
| 2626 | |
| 2627 | struct in_addr ip_addr; |
| 2628 | if( inet_aton(mask, &ip_addr) ) |
| 2629 | { |
| 2630 | netmask = ntohl(ip_addr.s_addr); |
| 2631 | }else{ |
| 2632 | netmask = 0; |
| 2633 | return 0; |
| 2634 | } |
| 2635 | |
| 2636 | while(0 == (netmask & 0x01) && i<32) |
| 2637 | { |
| 2638 | i++; |
| 2639 | netmask = netmask>>1; |
| 2640 | } |
| 2641 | masklen = 32-i; |
| 2642 | return masklen; |
| 2643 | } |
| 2644 | |
| 2645 | static int get_tether_route_str(char *str_cmd_ret, size_t max_len) { |
| 2646 | int mask_len; |
| 2647 | char *p; |
| 2648 | char tmp[64] = {0}; |
| 2649 | if (exec_cmd("ifconfig tether | grep Mask", str_cmd_ret, max_len) != 0) |
| 2650 | return -1; |
| 2651 | p = strstr(str_cmd_ret, "Mask:"); |
| 2652 | if (p == NULL) |
| 2653 | return -1; |
| 2654 | mask_len = get_netmask_length(p + 5); |
| 2655 | if (mask_len == 0) |
| 2656 | return -1; |
| 2657 | p = strstr(str_cmd_ret, "inet addr:"); |
| 2658 | if (p == NULL) |
| 2659 | return -1; |
| 2660 | strcpy(tmp, p + 10); |
| 2661 | p = strstr(tmp, " "); |
| 2662 | if (p != NULL) |
| 2663 | *p = '\0'; |
| 2664 | sprintf(str_cmd_ret, "%s/%d", tmp, mask_len); |
| 2665 | return 0; |
| 2666 | } |
| 2667 | |
| 2668 | static void GBWWatchThreadProc() { |
| 2669 | int i,n, nloop, nmax, ncheckcount, nidlecount; |
| 2670 | unsigned long long lastAP1Bytes, lastAP2Bytes, currAP1Bytes, currAP2Bytes; |
| 2671 | unsigned int lastAP1Drop,lastAP2Drop, currAP1Drop, currAP2Drop; |
| 2672 | unsigned int setAP1Speed, setAP2Speed, lastAP1Speed, lastAP2Speed, currAP1Speed, currAP2Speed,currSetAP1Speed; |
| 2673 | char *results[16] = {0}; |
| 2674 | char str_cmd[256] = {0}; |
| 2675 | char str_cmd_ret[128] = {0}; |
| 2676 | char dest_ip[32] = {0}; |
| 2677 | lastAP1Bytes = lastAP2Bytes = 0; |
| 2678 | lastAP1Drop = lastAP2Drop = 0; |
| 2679 | lastAP1Speed = lastAP2Speed = 0; |
| 2680 | setAP1Speed = 50; |
| 2681 | setAP2Speed = 80; |
| 2682 | nloop = 0; |
| 2683 | nmax = 6; |
| 2684 | ncheckcount = nidlecount = 0; |
| 2685 | |
| 2686 | printf("------gbw thread run\n"); |
| 2687 | sprintf(str_cmd, "ip neigh | grep %s | awk '{print $1}'", g_gbw_mac); |
| 2688 | while (dest_ip[0] == '\0') { |
| 2689 | sleep(1); |
| 2690 | str_cmd_ret[0] = '\0'; |
| 2691 | exec_cmd(str_cmd, str_cmd_ret, sizeof (str_cmd_ret)); |
| 2692 | for(n = 0; n < (int)sizeof(str_cmd_ret) && str_cmd_ret[n] != '\0'; n++) { |
| 2693 | if (str_cmd_ret[n] == '\n'){ |
| 2694 | str_cmd_ret[n] = '\0'; |
| 2695 | break; |
| 2696 | } |
| 2697 | } |
| 2698 | if (str_cmd_ret[0] != '\0') |
| 2699 | { |
| 2700 | strcpy(dest_ip, str_cmd_ret); |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | system("tc qdisc del dev tether root > /dev/null 2>&1"); |
| 2705 | system("tc qdisc add dev tether root handle 1: htb r2q 1"); |
| 2706 | system("tc class add dev tether parent 1: classid 1:1 htb rate 50Mbit ceil 70Mbit prio 2 quantum 3000"); |
| 2707 | if (get_tether_route_str(str_cmd_ret, sizeof (str_cmd_ret)) != 0) |
| 2708 | { |
| 2709 | printf("not get tether info\n"); |
| 2710 | return; |
| 2711 | } |
| 2712 | 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); |
| 2713 | system(str_cmd); |
| 2714 | system("tc class add dev tether parent 1: classid 1:2 htb rate 80Mbit ceil 100Mbit prio 0 quantum 3000000"); |
| 2715 | sprintf(str_cmd, "tc filter add dev tether parent 1: protocol ip prio 1 u32 match ip dst %s flowid 1:2", dest_ip); |
| 2716 | //printf("----cmd:%s\n", str_cmd); |
| 2717 | system(str_cmd); |
| 2718 | |
| 2719 | while (1) { |
| 2720 | sleep(1); |
| 2721 | memset(str_cmd, 0, sizeof(str_cmd)); |
| 2722 | if (0 != exec_cmd("tc -s class show dev tether classid 1:1 | grep Sent", str_cmd, sizeof (str_cmd))) |
| 2723 | continue; |
| 2724 | //printf("ap1 --- %s\n", str_cmd); |
| 2725 | n = lynq_split(str_cmd, strlen(str_cmd), ' ', results); |
| 2726 | if (n > 9) { |
| 2727 | if (strcmp(results[1], "Sent") == 0) { |
| 2728 | currAP1Bytes = atoll(results[2]); |
| 2729 | } |
| 2730 | if (strcmp(results[6], "(dropped") == 0) { |
| 2731 | currAP1Drop = atoi(results[7]); |
| 2732 | } |
| 2733 | } |
| 2734 | |
| 2735 | memset(str_cmd, 0, sizeof(str_cmd)); |
| 2736 | if (0 != exec_cmd("tc -s class show dev tether classid 1:2 | grep Sent", str_cmd, sizeof (str_cmd))) |
| 2737 | continue; |
| 2738 | //printf("ap2 --- %s\n", str_cmd); |
| 2739 | n = lynq_split(str_cmd, strlen(str_cmd), ' ', results); |
| 2740 | if (n > 9) { |
| 2741 | if (strcmp(results[1], "Sent") == 0) { |
| 2742 | currAP2Bytes = atoll(results[2]); |
| 2743 | } |
| 2744 | if (strcmp(results[6], "(dropped") == 0) { |
| 2745 | currAP2Drop = atoi(results[7]); |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | //printf("ap1 %llu- %u, ap2 %llu-%u\n", currAP1Bytes, currAP1Drop, currAP2Bytes, currAP2Drop); |
| 2750 | if (currAP1Bytes < lastAP1Bytes || currAP2Bytes < lastAP2Bytes) { |
| 2751 | lastAP1Bytes = currAP1Bytes; |
| 2752 | lastAP2Bytes = currAP2Bytes; |
| 2753 | continue; |
| 2754 | } |
| 2755 | |
| 2756 | currAP1Speed = (currAP1Bytes - lastAP1Bytes) / 128 / 1024; |
| 2757 | currAP2Speed = (currAP2Bytes - lastAP2Bytes) / 128 / 1024; |
| 2758 | //printf("ap1 speed %d mb, ap2 speed %d mb\n", currAP1Speed, currAP2Speed); |
| 2759 | lastAP1Speed = currAP1Speed; |
| 2760 | lastAP2Speed = currAP2Speed; |
| 2761 | lastAP1Bytes = currAP1Bytes; |
| 2762 | lastAP2Bytes = currAP2Bytes; |
| 2763 | |
| 2764 | currSetAP1Speed = setAP1Speed; |
| 2765 | if ((currAP2Speed < 30 && currAP2Speed > 5) && currAP1Speed > 5) { |
| 2766 | ncheckcount++; |
| 2767 | if (ncheckcount > 3) { |
| 2768 | ncheckcount = 0; |
| 2769 | currSetAP1Speed = 5; |
| 2770 | } |
| 2771 | } |
| 2772 | else { |
| 2773 | ncheckcount = 0; |
| 2774 | if (currAP1Speed < 5) |
| 2775 | nidlecount++; |
| 2776 | else |
| 2777 | nidlecount = 0; |
| 2778 | |
| 2779 | } |
| 2780 | |
| 2781 | if (nidlecount > 60 ){ |
| 2782 | currSetAP1Speed = 50; |
| 2783 | } |
| 2784 | |
| 2785 | if (currSetAP1Speed != setAP1Speed) { |
| 2786 | setAP1Speed = currSetAP1Speed; |
| 2787 | 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)); |
| 2788 | //printf("------***change speed: %s\n", str_cmd); |
| 2789 | system(str_cmd); |
| 2790 | } |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | int enableGBW(const char* mac) { |
| 2795 | int i,len; |
| 2796 | char get_ipaddr_cmd[128]={0}; |
| 2797 | ap_info_s *ap; |
| 2798 | device_info_s * list; |
| 2799 | |
| 2800 | if (mac == NULL || g_gbw_enabled == 1) |
| 2801 | return -1; |
| 2802 | len = strlen(mac); |
| 2803 | g_gbw_mac = malloc(len + 1); |
| 2804 | for(i=0;i<len;i++) { |
| 2805 | if (mac[i] >= 'A' && mac[i] <= 'Z') |
| 2806 | { |
| 2807 | g_gbw_mac[i] = 'a' + (mac[i] - 'A'); |
| 2808 | } |
| 2809 | else |
| 2810 | g_gbw_mac[i] = mac[i]; |
| 2811 | } |
| 2812 | g_gbw_mac[i] = '\0'; |
| 2813 | g_gbw_enabled = 1; |
| 2814 | |
| 2815 | sprintf(get_ipaddr_cmd, "ip neigh | grep %s", g_gbw_mac); |
| 2816 | if (system(get_ipaddr_cmd) == 0) { |
| 2817 | //startGBW(); |
| 2818 | if ( 0 ==lynq_get_ap_device_list(1, &ap, &list,&len) ) { |
| 2819 | for (i=0;i<len;i++) { |
| 2820 | //printf("--mac:%s, name:%s\n",list[i].sta_mac, list[i].hostname); |
| 2821 | if (strcmp(g_gbw_mac, list[i].sta_mac) == 0) |
| 2822 | startGBW(); |
| 2823 | } |
| 2824 | free(ap); |
| 2825 | free(list); |
| 2826 | } |
| 2827 | } |
| 2828 | return 0; |
| 2829 | } |
| 2830 | |
| 2831 | int disableGBW() { |
| 2832 | stopGBW(); |
| 2833 | free(g_gbw_mac); |
| 2834 | g_gbw_mac = NULL; |
| 2835 | g_gbw_enabled = 1; |
| 2836 | return 0; |
| 2837 | } |
| 2838 | |
| 2839 | static int startGBW() { |
| 2840 | if (g_gbw_watcher_pid != 0) { |
| 2841 | stopGBW(); |
| 2842 | } |
| 2843 | pthread_create(&g_gbw_watcher_pid,NULL,GBWWatchThreadProc,NULL); |
| 2844 | } |
| 2845 | |
| 2846 | static int stopGBW() { |
| 2847 | void* retval; |
| 2848 | pthread_cancel(g_gbw_watcher_pid); |
| 2849 | pthread_join(g_gbw_watcher_pid, &retval); |
| 2850 | g_gbw_watcher_pid = 0; |
| 2851 | system("tc qdisc del dev tether root"); |
| 2852 | } |
| 2853 | //you.chen add for tv-box end |