liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1 | #include <stdio.h>
|
| 2 | #include <pthread.h>
|
| 3 | #include <time.h>
|
| 4 | #include <sys/time.h>
|
| 5 | #include <unistd.h>
|
| 6 | #include <sys/un.h>
|
| 7 | #include <poll.h>
|
| 8 | #include <sys/socket.h>
|
| 9 | #include <netinet/in.h>
|
| 10 | #include <arpa/inet.h>
|
| 11 | #include <errno.h>
|
| 12 | #include <sys/ioctl.h>
|
| 13 | #include <net/if.h>
|
| 14 | #include <string.h>
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 15 | #include <ctype.h>
|
| 16 | #include <stdlib.h>
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 17 |
|
| 18 | #include "sta_cli.h"
|
| 19 | #include "sta_ctrl.h"
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 20 | #include "mbtk_log.h"
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 21 | #include "mbtk_utils.h"
|
| 22 | #include "mbtk_str.h"
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 23 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 24 | //#include "sta_log.h"
|
| 25 | //#include "mbtk_string.h"
|
| 26 |
|
| 27 | #define STA_CLI_TIMEOUT 15000 // 15s
|
| 28 | #define UNIXSTR_PATH "/data/sta_cli_sock"
|
| 29 | #define SA struct sockaddr
|
| 30 | #define LISTENQ 1024 /* 2nd argument to listen() */
|
| 31 | #define STA_CMD_SEPARATED "#$#"
|
| 32 | #define STA_MAC_LEN 17 // xx:xx:xx:xx:xx:xx
|
| 33 | #define STA_SSID_MAX_LEN (32 * 5)
|
| 34 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 35 | bool sta_ctrl_system(const char *command);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 36 |
|
| 37 | #ifndef INFTIM
|
| 38 | #define INFTIM (-1) /* infinite poll timeout */
|
| 39 | #endif
|
| 40 |
|
| 41 | #define STA_TAG_IND "IND"
|
| 42 | #define STA_TAG_CMD "CMD"
|
| 43 | #define STA_TAG_CMD_SUCCESS "SUCCESS"
|
| 44 | #define STA_TAG_CMD_FAIL "FAIL"
|
| 45 | #define STA_TAG_CMD_FAIL_BUSY "FAIL-BUSY"
|
| 46 | #define STA_CMD_OPEN "OPEN"
|
| 47 | //#define STA_CMD_EXIT "EXIT"
|
| 48 | #define STA_CMD_CLOSE "CLOSE"
|
| 49 | #define STA_CMD_SCAN "SCAN"
|
| 50 | #define STA_CMD_STATUS "STATUS"
|
| 51 | #define STA_CMD_MIB "MIB"
|
| 52 | #define STA_CMD_RECONFIGURE "RECONFIGURE"
|
| 53 | #define STA_CMD_DISCONNECT "DISCONNECT"
|
| 54 | #define STA_CMD_RECONNECT "RECONNECT"
|
| 55 | #define STA_CMD_SAVE_CONFIG "SAVE_CONFIG"
|
| 56 | #define STA_CMD_SET_NETWORK "SET_NETWORK"
|
| 57 | #define STA_CMD_GET_NETWORK "GET_NETWORK"
|
| 58 | #define STA_CMD_REMOVE_NETWORK "REMOVE_NETWORK"
|
| 59 | #define STA_CMD_ADD_NETWORK "ADD_NETWORK"
|
| 60 | #define STA_CMD_DISABLE_NETWORK "DISABLE_NETWORK"
|
| 61 | #define STA_CMD_ENABLE_NETWORK "ENABLE_NETWORK"
|
| 62 | #define STA_CMD_SELECT_NETWORK "SELECT_NETWORK"
|
| 63 | #define STA_CMD_LIST_NETWORKS "LIST_NETWORKS"
|
| 64 | #define STA_CMD_REASSOCIATE "REASSOCIATE"
|
| 65 | #define STA_CMD_REATTACH "REATTACH"
|
| 66 |
|
| 67 | // /var/run/wpa_supplicant
|
| 68 | static sta_cli_cmd_id_enum sta_cli_cmd_id = CMD_ID_NON;
|
| 69 | static char sta_cli_cmd_reply[STA_BUF_SIZE];
|
| 70 | static pthread_cond_t cond;
|
| 71 | static pthread_mutex_t mutex;
|
| 72 | static int sta_cli_conn_fd = -1;
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 73 | //static char sta_cli_buf[STA_BUF_SIZE];
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 74 | static bool sta_should_send_connected_msg = TRUE;
|
| 75 | //static bool sta_connected = FALSE;
|
| 76 | static bool sta_disconnectting = FALSE;
|
| 77 | //static pthread_mutex_t sta_mutex;
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 | int sta_cli_ssid_get(char *ssid)
|
| 82 | {
|
| 83 | FILE *fd_tmp = NULL;
|
| 84 |
|
| 85 | fd_tmp = popen("cat /etc/wifi/sta_network.conf | grep -w 'SSID' | cut -d '=' -f 2","r");
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 86 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 87 | if(fd_tmp){
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 88 | // char buf[200] = {0};
|
| 89 | if(fgets(ssid,200,fd_tmp) == NULL)
|
| 90 | return -1;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 91 | pclose(fd_tmp);
|
| 92 | if(strlen(ssid) > 0){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 93 | LOGE("test 100:%s, len:%d\n", ssid, strlen(ssid));
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 94 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 95 | }else{// Open wpa_supplicant
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 96 | LOGE("test 101:%s\n", ssid);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 97 | }
|
| 98 | }else{
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 99 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 100 | LOGE("test 102:%s\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 101 | }
|
| 102 |
|
| 103 | return 0;
|
| 104 | }
|
| 105 |
|
| 106 | int sta_cli_psk_get(char *psk)
|
| 107 | {
|
| 108 | FILE *fd_tmp = NULL;
|
| 109 |
|
| 110 | fd_tmp = popen("cat /etc/wifi/sta_network.conf | grep -w 'PASSWORD' | cut -d '=' -f 2","r");
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 111 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 112 | if(fd_tmp){
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 113 | // char buf[200] = {0};
|
| 114 | if(fgets(psk,200,fd_tmp) == NULL)
|
| 115 | return -1;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 116 | pclose(fd_tmp);
|
| 117 | if(strlen(psk) > 0){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 118 | LOGE("test 100:%s\n", psk);
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 119 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 120 | }else{// Open wpa_supplicant
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 121 | LOGE("test 101:%s\n", psk);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 122 | }
|
| 123 | }else{
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 124 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 125 | LOGE("test 102:%s\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 126 | }
|
| 127 |
|
| 128 | return 0;
|
| 129 | }
|
| 130 |
|
| 131 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 132 | int sta_cli_ssid_set(char *ssid)
|
| 133 | {
|
| 134 |
|
| 135 | char buf[100] = {0};
|
| 136 | snprintf(buf, 100, "sed -i 's/SSID=.*$/SSID=%s/g' /etc/wifi/sta_network.conf", ssid);
|
| 137 | LOGE("set ssid:%s", buf);
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 138 | mbtk_system(buf);
|
| 139 |
|
| 140 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 141 | return 0;
|
| 142 | }
|
| 143 |
|
| 144 | int sta_cli_psk_set(char *psk)
|
| 145 | {
|
| 146 |
|
| 147 | char buf[100] = {0};
|
| 148 | snprintf(buf, 100, "sed -i 's/PASSWORD=.*$/PASSWORD=%s/g' /etc/wifi/sta_network.conf", psk);
|
| 149 | LOGE("set ssid:%s", buf);
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 150 | mbtk_system(buf);
|
| 151 |
|
| 152 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 153 | return 0;
|
| 154 | }
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 159 | #if 0
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 160 | static char*
|
| 161 | sta_cli_ssid_process
|
| 162 | (
|
| 163 | const char *ssid,
|
| 164 | char *result,
|
| 165 | int len
|
| 166 | )
|
| 167 | {
|
| 168 | bzero(result,len);
|
| 169 | int ascii = 1;
|
| 170 | int i;
|
| 171 | for (i = 0; i < strlen(ssid); i++){
|
| 172 | if (!isascii(ssid[i])){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 173 | //LOGE("0x%02x\n",(unsigned char)ssid[i]);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 174 | //return 0;
|
| 175 | ascii = 0;
|
| 176 | break;
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | if(ascii)
|
| 181 | {
|
| 182 | snprintf(result,len,
|
| 183 | "\"%s\"",ssid);
|
| 184 | }else{
|
| 185 | int pos = 0;
|
| 186 | for (i = 0; i < strlen(ssid); i++){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 187 | LOGE("0x%02x\n",(unsigned char)ssid[i]);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 188 | snprintf(result + pos,len - pos,
|
| 189 | "%02x",(unsigned char)ssid[i]);
|
| 190 | pos += 2;
|
| 191 | }
|
| 192 | }
|
| 193 | return result;
|
| 194 | }
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 195 | #endif
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 196 |
|
| 197 | static char*
|
| 198 | sta_cli_mac_get
|
| 199 | (
|
| 200 | char *ifname,
|
| 201 | char *mac,
|
| 202 | size_t mac_len
|
| 203 | )
|
| 204 | {
|
| 205 | struct ifreq ifreq;
|
| 206 | int sock;
|
| 207 | bzero(mac,mac_len);
|
| 208 |
|
| 209 | if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
|
| 210 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 211 | LOGE("socket:errno(%d)\n",errno);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 212 | return NULL;
|
| 213 | }
|
| 214 | strcpy(ifreq.ifr_name,ifname);
|
| 215 | if(ioctl(sock,SIOCGIFHWADDR,&ifreq) <0)
|
| 216 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 217 | LOGE("ioctl:errno(%d)\n",errno);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 218 | return NULL;
|
| 219 | }
|
| 220 | snprintf(mac,mac_len,
|
| 221 | "%02x%02x%02x%02x%02x%02x",
|
| 222 | (unsigned char)ifreq.ifr_hwaddr.sa_data[0],
|
| 223 | (unsigned char)ifreq.ifr_hwaddr.sa_data[1],
|
| 224 | (unsigned char)ifreq.ifr_hwaddr.sa_data[2],
|
| 225 | (unsigned char)ifreq.ifr_hwaddr.sa_data[3],
|
| 226 | (unsigned char)ifreq.ifr_hwaddr.sa_data[4],
|
| 227 | (unsigned char)ifreq.ifr_hwaddr.sa_data[5]);
|
| 228 | return mac;
|
| 229 | }
|
| 230 |
|
| 231 |
|
| 232 | static char*
|
| 233 | sta_cli_sta_name_get
|
| 234 | (
|
| 235 | char *name,
|
| 236 | size_t name_len
|
| 237 | )
|
| 238 | {
|
| 239 | bzero(name,name_len);
|
| 240 |
|
| 241 | // Get host name.
|
| 242 | FILE *stream = NULL;
|
| 243 | char host[100];
|
| 244 | bzero(host,100);
|
| 245 | stream = popen("hostname","r");
|
| 246 | if(stream != NULL)
|
| 247 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 248 | if(fgets(host, 100, stream) == NULL) {
|
| 249 | return NULL;
|
| 250 | }
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 251 | pclose(stream);
|
| 252 | int index = str_indexof(host,"\n");
|
| 253 | if(strlen(host) > 0
|
| 254 | && index != 0)
|
| 255 | {
|
| 256 | if(index > 0)
|
| 257 | {
|
| 258 | host[index] = '\0';
|
| 259 | }
|
| 260 |
|
| 261 | // index < 0
|
| 262 | // No "\n"
|
| 263 | }
|
| 264 | else // No data or empty line.
|
| 265 | {
|
| 266 | // Can not get host,set default to "MBTK".
|
| 267 | memcpy(host,"MBTK",4);
|
| 268 | }
|
| 269 | }
|
| 270 | else
|
| 271 | {
|
| 272 | // Can not get host,set default to "MBTK".
|
| 273 | memcpy(host,"MBTK",4);
|
| 274 | }
|
| 275 |
|
| 276 | // Get mac address.
|
| 277 | char mac[20];
|
| 278 | if(NULL == sta_cli_mac_get("wlan0", mac, 20))
|
| 279 | {
|
| 280 | // Can not get mac,set default to current time.
|
| 281 | time_t t;
|
| 282 | t = time(NULL);
|
| 283 | snprintf(mac,20,
|
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame] | 284 | "%lld",t);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 285 | }
|
| 286 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 287 | snprintf(name,name_len,"%s-%s",host,mac);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 288 |
|
| 289 | return name;
|
| 290 | }
|
| 291 |
|
| 292 | static void
|
| 293 | sta_cli_wpa_open_success
|
| 294 | (
|
| 295 | void
|
| 296 | )
|
| 297 | {
|
| 298 | char buf[100];
|
| 299 | int len = snprintf(buf,100,
|
| 300 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 301 | STA_TAG_CMD,
|
| 302 | STA_CMD_OPEN,
|
| 303 | STA_TAG_CMD_SUCCESS);
|
| 304 | buf[len] = '\0';
|
| 305 | if(sta_cli_conn_fd != -1){
|
| 306 | if(write(sta_cli_conn_fd,buf,len) != len){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 307 | LOGE("Send open msg to client fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 308 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 309 | LOGE("Send open msg to client success.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 310 | }
|
| 311 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 312 | LOGE("No client connected.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 313 | }
|
| 314 | sta_should_send_connected_msg = FALSE;
|
| 315 | }
|
| 316 |
|
| 317 |
|
| 318 | static void
|
| 319 | sta_cli_wpa_msg_cb
|
| 320 | (
|
| 321 | char *msg
|
| 322 | )
|
| 323 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 324 | LOGE("cmd_id = %d,[%s]\n",sta_cli_cmd_id,msg);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 325 |
|
| 326 | // if(sta_cli_conn_fd != -1){
|
| 327 | // if(write(sta_cli_conn_fd,msg,strlen(msg)) != strlen(msg)){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 328 | // LOGE("Send msg to client fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 329 | // }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 330 | // LOGE("Send msg to client success.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 331 | // }
|
| 332 | // }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 333 | // LOGE("No client connected.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 334 | // }
|
| 335 |
|
| 336 | // Send msg(CMD_OPEN_SUCCESS) to wifi_server.
|
| 337 | if(sta_should_send_connected_msg) {
|
| 338 | sta_cli_wpa_open_success();
|
| 339 | }
|
| 340 |
|
| 341 | // Connected to AP
|
| 342 | if(str_contains(msg, "CTRL-EVENT-CONNECTED")){
|
| 343 | //sta_connected = TRUE;
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 344 | char sta_name[200];
|
| 345 | sta_cli_sta_name_get(sta_name,200);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 346 | char cmd[200];
|
| 347 | int size = snprintf(cmd,200,
|
| 348 | "dhcpcd -h %s -o domain_name_servers --noipv4ll wlan0",sta_name);
|
| 349 | //dhcpcd wlan0 -t 0 -o domain_name_servers --noipv4ll -b -G
|
| 350 | cmd[size] = '\0';
|
| 351 | if(sta_ctrl_system(cmd)){
|
| 352 | char buf[100];
|
| 353 | int len = snprintf(buf,100,
|
| 354 | "%s-CONNECTED"STA_CMD_SEPARATED,
|
| 355 | STA_TAG_IND);
|
| 356 | buf[len] = '\0';
|
| 357 | if(sta_cli_conn_fd != -1){
|
| 358 | usleep(500);
|
| 359 | if(write(sta_cli_conn_fd,buf,len) != len){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 360 | LOGE("Send msg to client fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 361 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 362 | LOGE("Send msg to client success.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 363 | }
|
| 364 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 365 | LOGE("No client connected.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 366 | }
|
| 367 | }
|
| 368 | return;
|
| 369 | }
|
| 370 | #if 1
|
| 371 | else if(str_contains(msg, "CTRL-EVENT-DISCONNECTED")
|
| 372 | && !str_contains(msg, "bssid=00:00:00:00:00:00")
|
| 373 | && !sta_disconnectting)
|
| 374 | {
|
| 375 | //pthread_mutex_lock(&sta_mutex);
|
| 376 | //if(sta_connected){
|
| 377 | // Disconnected from AP
|
| 378 | char buf[100];
|
| 379 | int len = snprintf(buf,100,
|
| 380 | "%s-DISCONNECTED"STA_CMD_SEPARATED,
|
| 381 | STA_TAG_IND);
|
| 382 | buf[len] = '\0';
|
| 383 | if(sta_cli_conn_fd != -1){
|
| 384 | if(write(sta_cli_conn_fd,buf,len) != len){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 385 | LOGE("Send msg to client fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 386 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 387 | LOGE("Send msg to client success.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 388 | }
|
| 389 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 390 | LOGE("No client connected.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 391 | }
|
| 392 | //sta_connected = FALSE;
|
| 393 | //pthread_mutex_unlock(&sta_mutex);
|
| 394 | return;
|
| 395 | //}
|
| 396 | //pthread_mutex_unlock(&sta_mutex);
|
| 397 | }
|
| 398 | #endif
|
| 399 |
|
| 400 | switch(sta_cli_cmd_id)
|
| 401 | {
|
| 402 | case CMD_ID_NON:
|
| 403 | {
|
| 404 |
|
| 405 | break;
|
| 406 | }
|
| 407 | case CMD_ID_SCAN:
|
| 408 | {
|
| 409 | if(str_contains(msg, "CTRL-EVENT-SCAN-RESULTS")){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 410 | LOGE("Start resume thread.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 411 | pthread_mutex_lock(&mutex);
|
| 412 | pthread_cond_signal(&cond);
|
| 413 | //pthread_cond_broadcast(&cond);
|
| 414 | pthread_mutex_unlock(&mutex);
|
| 415 | }
|
| 416 | break;
|
| 417 | }
|
| 418 | default:
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 419 | LOGE("cmd_id[%d] unknown.\n",sta_cli_cmd_id);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 420 | break;
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | static void
|
| 425 | sta_cli_thread_pause
|
| 426 | (
|
| 427 | long time /* ms */
|
| 428 | )
|
| 429 | {
|
| 430 | struct timeval now_1;
|
| 431 | struct timespec outtime;
|
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame] | 432 | pthread_t thread_id = pthread_self();
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 433 | LOGE("Thread(%ld) pause.\n",thread_id);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 434 | pthread_mutex_lock(&mutex);
|
| 435 | gettimeofday(&now_1, NULL);
|
| 436 | outtime.tv_sec = now_1.tv_sec + time / 1000;
|
| 437 | outtime.tv_nsec = now_1.tv_usec * 1000;
|
| 438 | pthread_cond_timedwait(&cond, &mutex, &outtime);
|
| 439 | pthread_mutex_unlock(&mutex);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 440 | LOGE("Thread(%ld) resume.\n",thread_id);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 441 | }
|
| 442 |
|
| 443 | static bool
|
| 444 | sta_cli_is_empty_char(char ch)
|
| 445 | {
|
| 446 | if(/*ch == ' ' || */ch == '\r' || ch == '\n'
|
| 447 | || ch == '\t')
|
| 448 | return TRUE;
|
| 449 | else
|
| 450 | return FALSE;
|
| 451 | }
|
| 452 |
|
| 453 | static char*
|
| 454 | sta_cli_value_get
|
| 455 | (
|
| 456 | const char *data,
|
| 457 | const char *key, // "key="
|
| 458 | char *value,
|
| 459 | int len
|
| 460 | )
|
| 461 | {
|
| 462 | bzero(value,len);
|
| 463 | char *ptr = strstr(data,key);
|
| 464 | if(ptr)
|
| 465 | {
|
| 466 | ptr += strlen(key);
|
| 467 | int i = 0;
|
| 468 | while(*ptr != '\r' && *ptr != '\n')
|
| 469 | {
|
| 470 | if(i == len - 1)
|
| 471 | return NULL;
|
| 472 | value[i++] = *ptr++;
|
| 473 | }
|
| 474 | return value;
|
| 475 | }else{
|
| 476 | return NULL;
|
| 477 | }
|
| 478 | }
|
| 479 |
|
| 480 |
|
| 481 | static sta_err_enum
|
| 482 | sta_cli_cmd_scan_parse
|
| 483 | (
|
| 484 | const char *reply,
|
| 485 | char *data,
|
| 486 | size_t len
|
| 487 | )
|
| 488 | {
|
| 489 | char *data_base = data;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 490 | LOGE("SCAN:\n%s\n",reply);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 491 | bzero(data,len);
|
| 492 | const char *ptr = reply;
|
| 493 | bool start = FALSE;
|
| 494 | int count = 0; // count for ',' by every line.
|
| 495 | while(*ptr){
|
| 496 | if(!start && *ptr == '\n'){
|
| 497 | start = TRUE;
|
| 498 | ptr++;
|
| 499 | }
|
| 500 |
|
| 501 | if(start){
|
| 502 | if(sta_cli_is_empty_char(*ptr)){
|
| 503 | if(*ptr == '\r' || *ptr == '\n'){
|
| 504 | *data++ = '\r';
|
| 505 | *data++ = '\n';
|
| 506 | while(*++ptr == '\r' || *ptr == '\n')
|
| 507 | ;
|
| 508 | count = 0; // reset for next line.
|
| 509 | }else{
|
| 510 | if(count < 4) {
|
| 511 | *data++ = ',';
|
| 512 | count++;
|
| 513 | while(*++ptr == ' ' || *ptr == '\t')
|
| 514 | ;
|
| 515 | } else {
|
| 516 | *data++ = *ptr++;
|
| 517 | }
|
| 518 | }
|
| 519 | }else{
|
| 520 | *data++ = *ptr++;
|
| 521 | }
|
| 522 | }else{
|
| 523 | ptr++;
|
| 524 | }
|
| 525 | }
|
| 526 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 527 | LOGE("SCAN 0:\n%s\n",data_base);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 528 |
|
| 529 | // Delete empty ssid line.
|
| 530 | char *tmp = (char*)calloc(len,1);
|
| 531 | memcpy(tmp,data_base,strlen(data_base));
|
| 532 | bzero(data_base,len);
|
| 533 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 534 | const char *ptr_pre = tmp;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 535 | ptr = strstr(ptr_pre,"\r\n");
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 536 | LOGE("line:%s\n",ptr == NULL?"NULL":ptr);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 537 | char ssid[STA_BUF_SIZE] = {0};
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 538 | const char *p;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 539 | while(ptr)
|
| 540 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 541 | LOGE("Get line.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 542 | // Get ssid.
|
| 543 | if(*(ptr - 1) == ',') // No ssid
|
| 544 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 545 | LOGE("Delete one line.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 546 | }else{
|
| 547 | char s[STA_BUF_SIZE] = {0};
|
| 548 | p = ptr - 1;
|
| 549 | int len = 0;
|
| 550 | while(*p-- != ','){
|
| 551 | len++;
|
| 552 | }
|
| 553 | p += 2;
|
| 554 | memcpy(s,p,len);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 555 | LOGE("ssid = %s;s = %s\n",ssid,s);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 556 | if(str_contains(ssid,s))
|
| 557 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 558 | LOGE("Jump the same ssid:%s\n",s);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 559 | ptr_pre = ptr + 2;
|
| 560 | ptr = strstr(ptr_pre,"\r\n");
|
| 561 | continue;
|
| 562 | }
|
| 563 |
|
| 564 | if(strlen(ssid) > 0){
|
| 565 | ssid[strlen(ssid)] = ',';
|
| 566 | }
|
| 567 | memcpy(ssid + strlen(ssid),s,len);
|
| 568 |
|
| 569 | memcpy(data_base + strlen(data_base),ptr_pre,ptr + 2 - ptr_pre);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 570 | LOGE("Copy ssid:\"%s\"\n",s);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 571 | }
|
| 572 | ptr_pre = ptr + 2;
|
| 573 | ptr = strstr(ptr_pre,"\r\n");
|
| 574 | }
|
| 575 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 576 | LOGE("Scan parse end.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 577 |
|
| 578 | free(tmp);
|
| 579 | tmp = NULL;
|
| 580 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 581 | LOGE("SCAN 1:\n%s\n",data_base);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 582 |
|
| 583 | return STA_ERR_SUCCESS;
|
| 584 | }
|
| 585 |
|
| 586 | static sta_err_enum
|
| 587 | sta_cli_cmd_status_parse
|
| 588 | (
|
| 589 | const char *reply,
|
| 590 | char *data,
|
| 591 | size_t data_len
|
| 592 | )
|
| 593 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 594 | LOGE("STATUS:\n%s\n",reply);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 595 |
|
| 596 | bzero(data,data_len);
|
| 597 |
|
| 598 | // const char *ptr = reply;
|
| 599 | // while(*ptr){
|
| 600 | // if(*ptr == '\r' || *ptr == '\n'){
|
| 601 | // *data++ = '\r';
|
| 602 | // *data++ = '\n';
|
| 603 | // while(*++ptr == '\r' || *ptr == '\n')
|
| 604 | // ;
|
| 605 | // }else{
|
| 606 | // *data++ = *ptr++;
|
| 607 | // }
|
| 608 | // }
|
| 609 |
|
| 610 | #define BUF_SIZE 500
|
| 611 | int len = 0;
|
| 612 | char buf[BUF_SIZE];
|
| 613 | char mac_ap[STA_MAC_LEN + 1];
|
| 614 | int size;
|
| 615 | if(sta_cli_value_get(reply,
|
| 616 | "bssid=",
|
| 617 | mac_ap,
|
| 618 | STA_MAC_LEN + 1)) // Connected.
|
| 619 | {
|
| 620 | // State
|
| 621 | memcpy(data + len,"1",1);
|
| 622 | len += 1;
|
| 623 |
|
| 624 | // mac_own
|
| 625 | if(sta_cli_value_get(reply,
|
| 626 | "\naddress=",
|
| 627 | buf,
|
| 628 | BUF_SIZE))
|
| 629 | {
|
| 630 | size = snprintf(data + len,data_len - len,
|
| 631 | ",%s",buf);
|
| 632 | len += size;
|
| 633 | }else{
|
| 634 | memcpy(data + len,",",1);
|
| 635 | len += 1;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 636 | LOGE("Not get own MAC address.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 637 | }
|
| 638 |
|
| 639 | // net_id
|
| 640 | if(sta_cli_value_get(reply,
|
| 641 | "\nid=",
|
| 642 | buf,
|
| 643 | BUF_SIZE))
|
| 644 | {
|
| 645 | size = snprintf(data + len,data_len - len,
|
| 646 | ",%s",buf);
|
| 647 | len += size;
|
| 648 | }else{
|
| 649 | memcpy(data + len,",",1);
|
| 650 | len += 1;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 651 | LOGE("Not get net id.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 652 | }
|
| 653 |
|
| 654 | // ssid
|
| 655 | if(sta_cli_value_get(reply,
|
| 656 | "\nssid=",
|
| 657 | buf,
|
| 658 | BUF_SIZE))
|
| 659 | {
|
| 660 | size = snprintf(data + len,data_len - len,
|
| 661 | ",%s",buf);
|
| 662 | len += size;
|
| 663 | }else{
|
| 664 | memcpy(data + len,",",1);
|
| 665 | len += 1;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 666 | LOGE("Not get ssid.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 667 | }
|
| 668 |
|
| 669 | // freq
|
| 670 | if(sta_cli_value_get(reply,
|
| 671 | "freq=",
|
| 672 | buf,
|
| 673 | BUF_SIZE))
|
| 674 | {
|
| 675 | int freq = atoi(buf);
|
| 676 | int channel = (freq - 2412) / 5 + 1;
|
| 677 | size = snprintf(data + len,data_len - len,
|
| 678 | ",%d,%s",channel,buf);
|
| 679 | len += size;
|
| 680 | }else{
|
| 681 | memcpy(data + len,",",1);
|
| 682 | len += 1;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 683 | LOGE("Not get freq.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 684 | }
|
| 685 |
|
| 686 | // auth
|
| 687 | if(sta_cli_value_get(reply,
|
| 688 | "key_mgmt=",
|
| 689 | buf,
|
| 690 | BUF_SIZE))
|
| 691 | {
|
| 692 | // WPA/WPA2
|
| 693 | if(strncmp(buf,"WPA",3)== 0)
|
| 694 | {
|
| 695 | size = snprintf(data + len,data_len - len,
|
| 696 | ",%d",2);
|
| 697 | len += size;
|
| 698 | }else{
|
| 699 | if(sta_cli_value_get(reply,
|
| 700 | "group_cipher=",
|
| 701 | buf,
|
| 702 | BUF_SIZE))
|
| 703 | {
|
| 704 | // Open
|
| 705 | if(strncmp(buf,"NONE",4) == 0)
|
| 706 | {
|
| 707 | size = snprintf(data + len,data_len - len,
|
| 708 | ",%d",0);
|
| 709 | len += size;
|
| 710 | }else{// WEP
|
| 711 | size = snprintf(data + len,data_len - len,
|
| 712 | ",%d",1);
|
| 713 | len += size;
|
| 714 | }
|
| 715 | }else{
|
| 716 | memcpy(data + len,",",1);
|
| 717 | len += 1;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 718 | LOGE("Not get group_cipher.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 719 | }
|
| 720 | }
|
| 721 | }else{
|
| 722 | memcpy(data + len,",",1);
|
| 723 | len += 1;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 724 | LOGE("Not get key_mgmt.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 725 | }
|
| 726 |
|
| 727 | // mac_ap
|
| 728 | size = snprintf(data + len,data_len - len,
|
| 729 | ",%s",mac_ap);
|
| 730 | len += size;
|
| 731 |
|
| 732 | // ip
|
| 733 | if(sta_cli_value_get(reply,
|
| 734 | "ip_address=",
|
| 735 | buf,
|
| 736 | BUF_SIZE))
|
| 737 | {
|
| 738 | size = snprintf(data + len,data_len - len,
|
| 739 | ",%s",buf);
|
| 740 | len += size;
|
| 741 | }else{
|
| 742 | memcpy(data + len,",",1);
|
| 743 | len += 1;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 744 | LOGE("Not get IP.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 745 | }
|
| 746 | }else{
|
| 747 | memcpy(data + len,"0",1);
|
| 748 | len += 1;
|
| 749 |
|
| 750 | if(sta_cli_value_get(reply,
|
| 751 | "\naddress=",
|
| 752 | buf,
|
| 753 | BUF_SIZE))
|
| 754 | {
|
| 755 | size = snprintf(data + len,data_len - len,
|
| 756 | ",%s",buf);
|
| 757 | len += size;
|
| 758 |
|
| 759 | }else{
|
| 760 | memcpy(data + len,",",1);
|
| 761 | len += 1;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 762 | LOGE("Not get MAC address.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 763 | }
|
| 764 |
|
| 765 | memcpy(data + len,",,,,,,,",7);
|
| 766 | len += 7;
|
| 767 | }
|
| 768 |
|
| 769 | memcpy(data + len,"\r\n",2);
|
| 770 | len += 2;
|
| 771 | data[len] = '\0';
|
| 772 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 773 | LOGE("STATUS:\n%s\n",data);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 774 | #undef BUF_SIZE
|
| 775 | return STA_ERR_SUCCESS;
|
| 776 | }
|
| 777 |
|
| 778 | static sta_err_enum
|
| 779 | sta_cli_cmd_mib_parse
|
| 780 | (
|
| 781 | const char *reply,
|
| 782 | char *data,
|
| 783 | size_t len
|
| 784 | )
|
| 785 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 786 | LOGE("MIB:\n%s\n",reply);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 787 |
|
| 788 | memcpy(data,reply,strlen(reply));
|
| 789 |
|
| 790 | return STA_ERR_SUCCESS;
|
| 791 | }
|
| 792 |
|
| 793 | static sta_err_enum
|
| 794 | sta_cli_cmd_list_network_parse
|
| 795 | (
|
| 796 | const char *reply,
|
| 797 | char *data,
|
| 798 | size_t len
|
| 799 | )
|
| 800 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 801 | LOGE("LIST_NETWORK:\n%s\n",reply);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 802 |
|
| 803 | bzero(data,len);
|
| 804 | const char *ptr = reply;
|
| 805 | bool start = FALSE;
|
| 806 | bool skip = FALSE;
|
| 807 | int count = 0; // count for ',' by every line.
|
| 808 | while(*ptr){
|
| 809 | if(!start && *ptr == '\n'){
|
| 810 | start = TRUE;
|
| 811 | ptr++;
|
| 812 | }
|
| 813 |
|
| 814 | if(start){
|
| 815 | if(sta_cli_is_empty_char(*ptr)){
|
| 816 | if(*ptr == '\r' || *ptr == '\n'){
|
| 817 | *data++ = '\r';
|
| 818 | *data++ = '\n';
|
| 819 | while(*++ptr == '\r' || *ptr == '\n')
|
| 820 | ;
|
| 821 | count = 0;
|
| 822 | skip = FALSE;
|
| 823 | }else{
|
| 824 | if(count < 1) {
|
| 825 | *data++ = ',';
|
| 826 | count++;
|
| 827 | while(*++ptr == ' ' || *ptr == '\t')
|
| 828 | ;
|
| 829 | } else {
|
| 830 | skip = TRUE;
|
| 831 | ptr++;
|
| 832 | }
|
| 833 | }
|
| 834 | }else{
|
| 835 | if(!skip)
|
| 836 | *data++ = *ptr++;
|
| 837 | else
|
| 838 | ptr++;
|
| 839 | }
|
| 840 | }else{
|
| 841 | ptr++;
|
| 842 | }
|
| 843 | }
|
| 844 |
|
| 845 | //memcpy(data,reply,strlen(reply));
|
| 846 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 847 | LOGE("LIST_NETWORK:\n%s\n",data);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 848 |
|
| 849 | return STA_ERR_SUCCESS;
|
| 850 | }
|
| 851 |
|
| 852 | static sta_err_enum
|
| 853 | sta_cli_process_cmd
|
| 854 | (
|
| 855 | const char *cmd
|
| 856 | )
|
| 857 | {
|
| 858 | sta_err_enum err = sta_ctrl_cmd_process(cmd,sta_cli_cmd_reply,STA_BUF_SIZE);
|
| 859 | if(err == STA_ERR_SUCCESS){
|
| 860 | if(strncmp(cmd,STA_CMD_SCAN,strlen(STA_CMD_SCAN)) == 0
|
| 861 | && strncmp(sta_cli_cmd_reply,STA_TAG_CMD_FAIL_BUSY,strlen(STA_TAG_CMD_FAIL_BUSY)) == 0){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 862 | LOGE("\"%s\" busy.\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 863 | return STA_ERR_SUCCESS;
|
| 864 | }
|
| 865 |
|
| 866 | if(strncmp(sta_cli_cmd_reply,STA_TAG_CMD_FAIL,strlen(STA_TAG_CMD_FAIL)) == 0){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 867 | LOGE("\"%s\" fail.\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 868 | return STA_ERR_UNKNOWN;
|
| 869 | }
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 870 | LOGE("[%s]:\n%s\n",cmd,sta_cli_cmd_reply);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 871 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 872 | LOGE("[%s]:FAIL\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 873 | }
|
| 874 | return err;
|
| 875 | }
|
| 876 |
|
| 877 | static sta_err_enum
|
| 878 | sta_cli_cmd_scan
|
| 879 | (
|
| 880 | const char *cmd,
|
| 881 | char *data,
|
| 882 | size_t len
|
| 883 | )
|
| 884 | {
|
| 885 | bzero(data,len);
|
| 886 | sta_cli_cmd_id = CMD_ID_SCAN;
|
| 887 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 888 | if(STA_ERR_SUCCESS == err){
|
| 889 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0
|
| 890 | || strncmp(sta_cli_cmd_reply,STA_TAG_CMD_FAIL_BUSY,strlen(STA_TAG_CMD_FAIL_BUSY)) == 0){// Is scanning ...
|
| 891 | sta_cli_thread_pause(STA_CLI_TIMEOUT);
|
| 892 |
|
| 893 | // Scan end.
|
| 894 |
|
| 895 | sta_cli_cmd_id = CMD_ID_SCAN_RESULTS;
|
| 896 | err = sta_cli_process_cmd("SCAN_RESULTS");
|
| 897 | sta_cli_cmd_id = CMD_ID_NON;
|
| 898 | if(STA_ERR_SUCCESS == err){
|
| 899 | return sta_cli_cmd_scan_parse(sta_cli_cmd_reply,
|
| 900 | data,
|
| 901 | len);
|
| 902 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 903 | LOGE("SCAN_RESULTS cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 904 | return STA_ERR_UNKNOWN;
|
| 905 | }
|
| 906 | }else{
|
| 907 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 908 | LOGE("SCAN cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 909 | return STA_ERR_UNKNOWN;
|
| 910 | }
|
| 911 | }else{
|
| 912 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 913 | LOGE("SCAN cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 914 | return err;
|
| 915 | }
|
| 916 | }
|
| 917 |
|
| 918 |
|
| 919 | static char*
|
| 920 | sta_cli_flag_get
|
| 921 | (
|
| 922 | const char *ssid,
|
| 923 | char *flag,
|
| 924 | size_t len
|
| 925 | )
|
| 926 | {
|
| 927 | sta_err_enum err;
|
| 928 | char data[STA_BUF_SIZE];
|
| 929 | bzero(flag,len);
|
| 930 | err = sta_cli_cmd_scan("SCAN", data,STA_BUF_SIZE);
|
| 931 | if(err == STA_ERR_SUCCESS){
|
| 932 | if(strlen(data) == 0)
|
| 933 | {
|
| 934 | return NULL;
|
| 935 | }
|
| 936 | ssize_t end = str_indexof(data,ssid) - 1; // Point to ','
|
| 937 | if(end > 0)
|
| 938 | {
|
| 939 | char *ptr = data + end - 1;// Jump the ','
|
| 940 | int len = 0;
|
| 941 | while(*ptr != ',' && ptr != data)
|
| 942 | {
|
| 943 | ptr--;
|
| 944 | len++;
|
| 945 | }
|
| 946 | ptr++; // Point to flag.
|
| 947 |
|
| 948 | memcpy(flag,ptr,len);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 949 | LOGE("%s : %s\n",ssid,flag);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 950 | return flag;
|
| 951 | }
|
| 952 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 953 | LOGE("SCAN_RESULTS cmd fail.");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 954 | return NULL;
|
| 955 | }
|
| 956 | return NULL;
|
| 957 | }
|
| 958 |
|
| 959 |
|
| 960 | static sta_err_enum
|
| 961 | sta_cli_cmd_status
|
| 962 | (
|
| 963 | const char *cmd,
|
| 964 | char *data,
|
| 965 | size_t len
|
| 966 | )
|
| 967 | {
|
| 968 | bzero(data,len);
|
| 969 | sta_cli_cmd_id = CMD_ID_STATUS;
|
| 970 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 971 | sta_cli_cmd_id = CMD_ID_NON;
|
| 972 | if(STA_ERR_SUCCESS == err){
|
| 973 | return sta_cli_cmd_status_parse(sta_cli_cmd_reply,
|
| 974 | data,
|
| 975 | len);
|
| 976 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 977 | LOGE("STATUS cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 978 | return err;
|
| 979 | }
|
| 980 | }
|
| 981 |
|
| 982 | static sta_err_enum
|
| 983 | sta_cli_cmd_mib
|
| 984 | (
|
| 985 | const char *cmd,
|
| 986 | char *data,
|
| 987 | size_t len
|
| 988 | )
|
| 989 | {
|
| 990 | bzero(data,len);
|
| 991 | sta_cli_cmd_id = CMD_ID_MIB;
|
| 992 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 993 | sta_cli_cmd_id = CMD_ID_NON;
|
| 994 | if(STA_ERR_SUCCESS == err){
|
| 995 | return sta_cli_cmd_mib_parse(sta_cli_cmd_reply,
|
| 996 | data,
|
| 997 | len);
|
| 998 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 999 | LOGE("MIB cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1000 | return err;
|
| 1001 | }
|
| 1002 | }
|
| 1003 |
|
| 1004 | static sta_err_enum
|
| 1005 | sta_cli_cmd_reconfigure
|
| 1006 | (
|
| 1007 | const char *cmd
|
| 1008 | )
|
| 1009 | {
|
| 1010 | sta_cli_cmd_id = CMD_ID_RECONFIGURE;
|
| 1011 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1012 | if(STA_ERR_SUCCESS == err){
|
| 1013 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1014 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1015 | return STA_ERR_SUCCESS;
|
| 1016 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1017 | LOGE("RECONFIGURE cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1018 | return STA_ERR_UNKNOWN;
|
| 1019 | }
|
| 1020 | }else{
|
| 1021 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1022 | LOGE("RECONFIGURE cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1023 | return err;
|
| 1024 | }
|
| 1025 | }
|
| 1026 |
|
| 1027 | static sta_err_enum
|
| 1028 | sta_cli_cmd_disconnect
|
| 1029 | (
|
| 1030 | const char *cmd
|
| 1031 | )
|
| 1032 | {
|
| 1033 | sta_cli_cmd_id = CMD_ID_DISCONNECT;
|
| 1034 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1035 | if(STA_ERR_SUCCESS == err){
|
| 1036 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1037 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1038 | return STA_ERR_SUCCESS;
|
| 1039 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1040 | LOGE("DISCONNECT cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1041 | return STA_ERR_UNKNOWN;
|
| 1042 | }
|
| 1043 | }else{
|
| 1044 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1045 | LOGE("DISCONNECT cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1046 | return err;
|
| 1047 | }
|
| 1048 | }
|
| 1049 |
|
| 1050 | static sta_err_enum
|
| 1051 | sta_cli_cmd_reconnect
|
| 1052 | (
|
| 1053 | const char *cmd
|
| 1054 | )
|
| 1055 | {
|
| 1056 | sta_cli_cmd_id = CMD_ID_RECONNECT;
|
| 1057 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1058 | if(STA_ERR_SUCCESS == err){
|
| 1059 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1060 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1061 | return STA_ERR_SUCCESS;
|
| 1062 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1063 | LOGE("RECONNECT cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1064 | return STA_ERR_UNKNOWN;
|
| 1065 | }
|
| 1066 | }else{
|
| 1067 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1068 | LOGE("RECONNECT cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1069 | return err;
|
| 1070 | }
|
| 1071 | }
|
| 1072 |
|
| 1073 |
|
| 1074 | static sta_err_enum
|
| 1075 | sta_cli_cmd_save_config
|
| 1076 | (
|
| 1077 | const char *cmd
|
| 1078 | )
|
| 1079 | {
|
| 1080 | sta_cli_cmd_id = CMD_ID_SAVE_CONFIG;
|
| 1081 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1082 | if(STA_ERR_SUCCESS == err){
|
| 1083 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1084 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1085 | return STA_ERR_SUCCESS;
|
| 1086 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1087 | LOGE("SAVE_CONFIG cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1088 | return STA_ERR_UNKNOWN;
|
| 1089 | }
|
| 1090 | }else{
|
| 1091 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1092 | LOGE("SAVE_CONFIG cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1093 | return err;
|
| 1094 | }
|
| 1095 | }
|
| 1096 |
|
| 1097 | static sta_err_enum
|
| 1098 | sta_cli_cmd_set_network_process
|
| 1099 | (
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1100 | const char *c
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1101 | )
|
| 1102 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1103 | LOGE("cmd = %s\n",c);
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1104 | const char *ptr = c;
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1105 |
|
| 1106 | sta_cli_cmd_id = CMD_ID_SET_NETWORK;
|
| 1107 | sta_err_enum err = STA_ERR_SUCCESS;
|
| 1108 | char cmd[100];
|
| 1109 | int index = str_indexof(ptr,"#");
|
| 1110 | bzero(cmd,100);
|
| 1111 | if(index > 0)
|
| 1112 | {
|
| 1113 | memcpy(cmd,ptr,index);
|
| 1114 | ptr += (index + 1);
|
| 1115 | }else
|
| 1116 | {
|
| 1117 | memcpy(cmd,ptr,strlen(ptr));
|
| 1118 | ptr = NULL;
|
| 1119 | }
|
| 1120 |
|
| 1121 | while(TRUE)
|
| 1122 | {
|
| 1123 | err = sta_cli_process_cmd(c);
|
| 1124 | if(STA_ERR_SUCCESS == err){
|
| 1125 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1126 | //return STA_ERR_SUCCESS;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1127 | LOGE("Success:%s\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1128 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1129 | LOGE("Fail:%s\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1130 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1131 | return STA_ERR_UNKNOWN;
|
| 1132 | }
|
| 1133 | }else{
|
| 1134 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1135 | LOGE("Fail:%s\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1136 | return err;
|
| 1137 | }
|
| 1138 |
|
| 1139 | if(ptr == NULL)
|
| 1140 | break;
|
| 1141 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1142 | LOGE("ptr = %s",ptr);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1143 |
|
| 1144 | index = str_indexof(ptr,"#");
|
| 1145 | bzero(cmd,100);
|
| 1146 | if(index > 0)
|
| 1147 | {
|
| 1148 | memcpy(cmd,ptr,index);
|
| 1149 | ptr += (index + 1);
|
| 1150 | }else
|
| 1151 | {
|
| 1152 | memcpy(cmd,ptr,strlen(ptr));
|
| 1153 | ptr = NULL;
|
| 1154 | }
|
| 1155 | }
|
| 1156 |
|
| 1157 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1158 | return STA_ERR_SUCCESS;
|
| 1159 | }
|
| 1160 |
|
| 1161 | static sta_err_enum
|
| 1162 | sta_cli_cmd_set_network
|
| 1163 | (
|
| 1164 | const char *cmd,
|
| 1165 | const char *flag
|
| 1166 | )
|
| 1167 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1168 | LOGE("cmd = %s\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1169 | char buf[500];
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1170 | LOGE("test11\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1171 | int index = str_indexof(cmd," psk ");
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1172 | LOGE("test12\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1173 | int net_id = atoi(cmd + strlen(STA_CMD_SET_NETWORK) + 1);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1174 | LOGE("test13\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1175 | if(index > 0){ // Is set "psk"
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1176 | LOGE("test14\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1177 | char psk[64] = {0};
|
| 1178 | int start = index + 5; // " psk "
|
| 1179 | if(*(cmd + start) == '"')
|
| 1180 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1181 | LOGE("test15\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1182 | memcpy(psk,cmd + start + 1,strlen(cmd) - start - 2);
|
| 1183 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1184 | LOGE("test16\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1185 | memcpy(psk,cmd + start,strlen(cmd) - start);
|
| 1186 | }
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1187 | LOGE("psk = %s\n",psk);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1188 |
|
| 1189 | // Set to OPEN (No psk)
|
| 1190 | // SET_NETWORK <net_id> key_mgmt NONE
|
| 1191 | if(strcmp(psk,"0") == 0)
|
| 1192 | {
|
| 1193 | int size = snprintf(buf,500,
|
| 1194 | "%s %d key_mgmt NONE",
|
| 1195 | STA_CMD_SET_NETWORK,
|
| 1196 | net_id);
|
| 1197 | buf[size] = '\0';
|
| 1198 | return sta_cli_cmd_set_network_process(buf);
|
| 1199 | }
|
| 1200 |
|
| 1201 | // WEP
|
| 1202 | // key_mgmt=NONE
|
| 1203 | // auth_alg=OPEN SHARED
|
| 1204 | // wep_key0="0123456789abc"
|
| 1205 | if(flag && str_contains(flag, "WEP")
|
| 1206 | &&(strlen(psk) == 5
|
| 1207 | || strlen(psk) == 13
|
| 1208 | || strlen(psk) == 10
|
| 1209 | || strlen(psk) == 26))
|
| 1210 | {
|
| 1211 | int size = 0;
|
| 1212 | if(strlen(psk) == 5
|
| 1213 | || strlen(psk) == 13)
|
| 1214 | {
|
| 1215 | size = snprintf(buf,500,
|
| 1216 | "%s %d key_mgmt NONE#%s %d auth_alg SHARED#%s %d wep_key0 \"%s\"",
|
| 1217 | STA_CMD_SET_NETWORK,
|
| 1218 | net_id,
|
| 1219 | STA_CMD_SET_NETWORK,
|
| 1220 | net_id,
|
| 1221 | STA_CMD_SET_NETWORK,
|
| 1222 | net_id,
|
| 1223 | psk);
|
| 1224 | }else{
|
| 1225 | size = snprintf(buf,500,
|
| 1226 | "%s %d key_mgmt NONE#%s %d auth_alg SHARED#%s %d wep_key0 %s",
|
| 1227 | STA_CMD_SET_NETWORK,
|
| 1228 | net_id,
|
| 1229 | STA_CMD_SET_NETWORK,
|
| 1230 | net_id,
|
| 1231 | STA_CMD_SET_NETWORK,
|
| 1232 | net_id,
|
| 1233 | psk);
|
| 1234 | }
|
| 1235 |
|
| 1236 | buf[size] = '\0';
|
| 1237 | return sta_cli_cmd_set_network_process(buf);
|
| 1238 | }
|
| 1239 |
|
| 1240 | // Default is "WPA/WPA2"
|
| 1241 | int size = snprintf(buf,500,
|
| 1242 | "%s#%s %d key_mgmt WPA-PSK",
|
| 1243 | cmd,
|
| 1244 | STA_CMD_SET_NETWORK,
|
| 1245 | net_id);
|
| 1246 | buf[size] = '\0';
|
| 1247 | return sta_cli_cmd_set_network_process(buf);
|
| 1248 | }
|
| 1249 | else // SSID
|
| 1250 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1251 | LOGE("test21\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1252 | index = str_indexof(cmd," ssid ");
|
| 1253 | char ssid[STA_BUF_SIZE] = {0};
|
| 1254 | int start = index + 6; // " ssid "
|
| 1255 | if(*(cmd + start) == '"')
|
| 1256 | {
|
| 1257 | memcpy(ssid,cmd + start + 1,strlen(cmd) - start - 2);
|
| 1258 | }else{
|
| 1259 | memcpy(ssid,cmd + start,strlen(cmd) - start);
|
| 1260 | }
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1261 | LOGE("ssid = %s\n",ssid);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1262 |
|
| 1263 |
|
| 1264 | //char ssid_result[STA_SSID_MAX_LEN + 1];
|
| 1265 | //sta_cli_ssid_process(ssid,ssid_result,STA_SSID_MAX_LEN + 2 + 1);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1266 | // LOGE("test22, ssid_result: %s\n", ssid_result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1267 | char cmd_result[STA_BUF_SIZE];
|
| 1268 | int size = snprintf(cmd_result,STA_BUF_SIZE,
|
| 1269 | "%s %d ssid %s",
|
| 1270 | STA_CMD_SET_NETWORK,
|
| 1271 | net_id,
|
| 1272 | ssid);
|
| 1273 | cmd_result[size] = '\0';
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1274 | LOGE("cmd = %s\n",cmd_result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1275 |
|
| 1276 | return sta_cli_cmd_set_network_process(cmd);
|
| 1277 | }
|
| 1278 | }
|
| 1279 |
|
| 1280 |
|
| 1281 | static sta_err_enum
|
| 1282 | sta_cli_cmd_get_network
|
| 1283 | (
|
| 1284 | const char *cmd,
|
| 1285 | char *value,
|
| 1286 | int len
|
| 1287 | )
|
| 1288 | {
|
| 1289 | bzero(value,len);
|
| 1290 | sta_cli_cmd_id = CMD_ID_GET_NETWORK;
|
| 1291 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1292 | if(STA_ERR_SUCCESS == err){
|
| 1293 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1294 |
|
| 1295 | char *tmp = sta_cli_cmd_reply;
|
| 1296 | while(*tmp == '\"'){
|
| 1297 | tmp++;
|
| 1298 | }
|
| 1299 | // Copy with '\0'
|
| 1300 | memcpy(value,tmp,strlen(tmp) + 1);
|
| 1301 |
|
| 1302 | tmp = value + strlen(value) -1;
|
| 1303 | while(*tmp == '\"'){
|
| 1304 | tmp = '\0';
|
| 1305 | tmp--;
|
| 1306 | }
|
| 1307 |
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1308 | LOGE("GET_NETWORK:%s.\n",value);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1309 | return STA_ERR_SUCCESS;
|
| 1310 | }else{
|
| 1311 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1312 | LOGE("GET_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1313 | return err;
|
| 1314 | }
|
| 1315 | }
|
| 1316 |
|
| 1317 | static sta_err_enum
|
| 1318 | sta_cli_cmd_remove_network
|
| 1319 | (
|
| 1320 | const char *cmd
|
| 1321 | )
|
| 1322 | {
|
| 1323 | sta_cli_cmd_id = CMD_ID_REMOVE_NETWORK;
|
| 1324 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1325 | if(STA_ERR_SUCCESS == err){
|
| 1326 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1327 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1328 | return STA_ERR_SUCCESS;
|
| 1329 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1330 | LOGE("REMOVE_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1331 | return STA_ERR_UNKNOWN;
|
| 1332 | }
|
| 1333 | }else{
|
| 1334 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1335 | LOGE("REMOVE_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1336 | return err;
|
| 1337 | }
|
| 1338 | }
|
| 1339 |
|
| 1340 |
|
| 1341 | static sta_err_enum
|
| 1342 | sta_cli_cmd_add_network
|
| 1343 | (
|
| 1344 | const char *cmd
|
| 1345 | )
|
| 1346 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1347 | LOGE("cmd = %s\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1348 | sta_cli_cmd_id = CMD_ID_ADD_NETWORK;
|
| 1349 | sta_err_enum err = sta_cli_process_cmd(STA_CMD_ADD_NETWORK);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1350 | LOGE("test1\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1351 | if(STA_ERR_SUCCESS == err){
|
| 1352 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1353 | int net_id = atoi(sta_cli_cmd_reply);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1354 | LOGE("test2\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1355 | if(net_id >= 0){ // Add network success.
|
| 1356 | // Point to ssid
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1357 | LOGE("test3\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1358 | /*
|
| 1359 | const char *ptr = cmd + strlen(STA_CMD_ADD_NETWORK) + 1;
|
| 1360 |
|
| 1361 | //int index = str_indexof(ptr," ");
|
| 1362 |
|
| 1363 | char *pass_ptr = cmd + strlen(cmd);
|
| 1364 | while(*--pass_ptr != ' ')
|
| 1365 | ;
|
| 1366 | pass_ptr++; // Point to pass.
|
| 1367 |
|
| 1368 |
|
| 1369 | char ssid[STA_BUF_SIZE] = {0};
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1370 | LOGE("test4\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1371 | memcpy(ssid,ptr,pass_ptr - ptr - 1);
|
| 1372 | */
|
| 1373 |
|
| 1374 | char buf[STA_BUF_SIZE] = {'\0'};
|
| 1375 | char ssid[STA_BUF_SIZE] = {'\0'};
|
| 1376 | char psk[STA_BUF_SIZE] = {'\0'};
|
| 1377 | int len = 0;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1378 | LOGE("test5\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1379 |
|
| 1380 | sta_cli_ssid_get(ssid);
|
| 1381 | len = strlen(ssid);
|
| 1382 | ssid[len - 1] = '\0';
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1383 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1384 | sta_cli_psk_get(psk);
|
| 1385 | len = strlen(psk);
|
| 1386 | psk[len - 1] = '\0';
|
| 1387 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1388 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1389 | int size = snprintf(buf,STA_BUF_SIZE,
|
| 1390 | "%s %d ssid \"%s\"",
|
| 1391 | STA_CMD_SET_NETWORK,
|
| 1392 | net_id,
|
| 1393 | ssid);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1394 | LOGE("test6\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1395 | buf[size] = '\0';
|
| 1396 |
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 1397 |
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1398 | err = sta_cli_cmd_set_network(buf,NULL);
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1399 | LOGE("test7\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1400 | if(STA_ERR_SUCCESS == err){
|
| 1401 | char flag[50];
|
| 1402 | sta_cli_flag_get(ssid,flag,50);
|
| 1403 | size = snprintf(buf,100,
|
| 1404 | "%s %d psk \"%s\"",
|
| 1405 | STA_CMD_SET_NETWORK,
|
| 1406 | net_id,
|
| 1407 | psk);
|
| 1408 | buf[size] = '\0';
|
| 1409 | err = sta_cli_cmd_set_network(buf,flag);
|
| 1410 | if(STA_ERR_SUCCESS == err){
|
| 1411 | return STA_ERR_SUCCESS;
|
| 1412 | }
|
| 1413 | }
|
| 1414 |
|
| 1415 | if(err != STA_ERR_SUCCESS) // remove network
|
| 1416 | {
|
| 1417 | int size = snprintf(buf,STA_BUF_SIZE,
|
| 1418 | "%s %d",
|
| 1419 | STA_CMD_REMOVE_NETWORK,
|
| 1420 | net_id);
|
| 1421 | buf[size] = '\0';
|
| 1422 | sta_cli_process_cmd(buf);
|
| 1423 | }
|
| 1424 |
|
| 1425 | return err;
|
| 1426 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1427 | LOGE("ADD_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1428 | return STA_ERR_UNKNOWN;
|
| 1429 | }
|
| 1430 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1431 | LOGE("ADD_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1432 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1433 | return err;
|
| 1434 | }
|
| 1435 | }
|
| 1436 |
|
| 1437 | static sta_err_enum
|
| 1438 | sta_cli_cmd_disable_network
|
| 1439 | (
|
| 1440 | const char *cmd
|
| 1441 | )
|
| 1442 | {
|
| 1443 | sta_cli_cmd_id = CMD_ID_DISABLE_NETWORK;
|
| 1444 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1445 | if(STA_ERR_SUCCESS == err){
|
| 1446 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1447 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1448 | return STA_ERR_SUCCESS;
|
| 1449 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1450 | LOGE("DISABLE_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1451 | return STA_ERR_UNKNOWN;
|
| 1452 | }
|
| 1453 | }else{
|
| 1454 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1455 | LOGE("DISABLE_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1456 | return err;
|
| 1457 | }
|
| 1458 | }
|
| 1459 |
|
| 1460 | static sta_err_enum
|
| 1461 | sta_cli_cmd_enable_network
|
| 1462 | (
|
| 1463 | const char *cmd
|
| 1464 | )
|
| 1465 | {
|
| 1466 | sta_cli_cmd_id = CMD_ID_ENABLE_NETWORK;
|
| 1467 | sta_err_enum err = sta_cli_process_cmd("ENABLE_NETWORK 1");
|
| 1468 | if(STA_ERR_SUCCESS == err){
|
| 1469 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1470 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1471 | return STA_ERR_SUCCESS;
|
| 1472 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1473 | LOGE("ENABLE_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1474 | return STA_ERR_UNKNOWN;
|
| 1475 | }
|
| 1476 | }else{
|
| 1477 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1478 | LOGE("ENABLE_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1479 | return err;
|
| 1480 | }
|
| 1481 | }
|
| 1482 |
|
| 1483 | static sta_err_enum
|
| 1484 | sta_cli_cmd_select_network
|
| 1485 | (
|
| 1486 | const char *cmd
|
| 1487 | )
|
| 1488 | {
|
| 1489 | sta_cli_cmd_id = CMD_ID_SELECT_NETWORK;
|
| 1490 | sta_err_enum err = sta_cli_process_cmd("SELECT_NETWORK 1");
|
| 1491 | if(STA_ERR_SUCCESS == err){
|
| 1492 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1493 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1494 | return STA_ERR_SUCCESS;
|
| 1495 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1496 | LOGE("SELECT_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1497 | return STA_ERR_UNKNOWN;
|
| 1498 | }
|
| 1499 | }else{
|
| 1500 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1501 | LOGE("SELECT_NETWORK cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1502 | return err;
|
| 1503 | }
|
| 1504 | }
|
| 1505 |
|
| 1506 | static sta_err_enum
|
| 1507 | sta_cli_cmd_list_networks
|
| 1508 | (
|
| 1509 | const char *cmd,
|
| 1510 | char *data,
|
| 1511 | size_t len
|
| 1512 | )
|
| 1513 | {
|
| 1514 | bzero(data,len);
|
| 1515 | sta_cli_cmd_id = CMD_ID_LIST_NETWORKS;
|
| 1516 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1517 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1518 | if(STA_ERR_SUCCESS == err){
|
| 1519 | return sta_cli_cmd_list_network_parse(sta_cli_cmd_reply,
|
| 1520 | data,
|
| 1521 | len);
|
| 1522 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1523 | LOGE("LIST_NETWORKS cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1524 | return err;
|
| 1525 | }
|
| 1526 | }
|
| 1527 |
|
| 1528 | static sta_err_enum
|
| 1529 | sta_cli_cmd_reassociate
|
| 1530 | (
|
| 1531 | const char *cmd
|
| 1532 | )
|
| 1533 | {
|
| 1534 | sta_cli_cmd_id = CMD_ID_REASSOCIATE;
|
| 1535 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1536 | if(STA_ERR_SUCCESS == err){
|
| 1537 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1538 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1539 | return STA_ERR_SUCCESS;
|
| 1540 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1541 | LOGE("REASSOCIATE cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1542 | return STA_ERR_UNKNOWN;
|
| 1543 | }
|
| 1544 | }else{
|
| 1545 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1546 | LOGE("REASSOCIATE cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1547 | return err;
|
| 1548 | }
|
| 1549 | }
|
| 1550 |
|
| 1551 | static sta_err_enum
|
| 1552 | sta_cli_cmd_reattach
|
| 1553 | (
|
| 1554 | const char *cmd
|
| 1555 | )
|
| 1556 | {
|
| 1557 | sta_cli_cmd_id = CMD_ID_REATTACH;
|
| 1558 | sta_err_enum err = sta_cli_process_cmd(cmd);
|
| 1559 | if(STA_ERR_SUCCESS == err){
|
| 1560 | sta_cli_cmd_id = CMD_ID_NON;
|
| 1561 | if(strncmp(sta_cli_cmd_reply,"OK",2) == 0){
|
| 1562 | return STA_ERR_SUCCESS;
|
| 1563 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1564 | LOGE("REATTACH cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1565 | return STA_ERR_UNKNOWN;
|
| 1566 | }
|
| 1567 | }else{
|
| 1568 | sta_cli_cmd_id = CMD_ID_NON;
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1569 | LOGE("REATTACH cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1570 | return err;
|
| 1571 | }
|
| 1572 | }
|
| 1573 |
|
| 1574 | static sta_err_enum
|
| 1575 | sta_cli_init
|
| 1576 | (
|
| 1577 | void
|
| 1578 | )
|
| 1579 | {
|
| 1580 | sta_err_enum result = STA_ERR_SUCCESS;
|
| 1581 | if((result = sta_ctrl_driver_init(TRUE)) != STA_ERR_SUCCESS){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1582 | LOGE("Driver init fail(%d).\n",result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1583 | return result;
|
| 1584 | }
|
| 1585 |
|
| 1586 | if((result = sta_ctrl_wpa_init(
|
| 1587 | "/etc/wifi/wpa_supplicant.conf",
|
| 1588 | "wlan0",
|
| 1589 | sta_cli_wpa_msg_cb)) != STA_ERR_SUCCESS){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1590 | LOGE("wpa_supplicant init fail(%d).\n",result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1591 | return result;
|
| 1592 | }
|
| 1593 | //pthread_mutex_init(&sta_mutex,NULL);
|
| 1594 | return result;
|
| 1595 | }
|
| 1596 |
|
| 1597 | static sta_err_enum
|
| 1598 | sta_cli_deinit
|
| 1599 | (
|
| 1600 | void
|
| 1601 | )
|
| 1602 | {
|
| 1603 | sta_err_enum result = STA_ERR_SUCCESS;
|
| 1604 | if((result = sta_ctrl_wpa_deinit()) != STA_ERR_SUCCESS){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1605 | LOGE("sta_ctrl_wpa_deinit fail(%d).",result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1606 | return result;
|
| 1607 | }
|
| 1608 |
|
| 1609 | if((result = sta_ctrl_driver_init(FALSE)) != STA_ERR_SUCCESS){
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1610 | LOGE("Driver close fail(%d).\n",result);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1611 | return result;
|
| 1612 | }
|
| 1613 | //pthread_mutex_destroy(&sta_mutex);
|
| 1614 | return result;
|
| 1615 | }
|
| 1616 |
|
| 1617 | /**
|
| 1618 | * CMD-CLOSE-SUCCESS
|
| 1619 | * CMD-CLOSE-FAIL:2
|
| 1620 | *
|
| 1621 | * CMD-SCAN-XXX
|
| 1622 | *
|
| 1623 | * IND-XXX
|
| 1624 | *
|
| 1625 | */
|
| 1626 | bool sta_cli_cmd_parse
|
| 1627 | (
|
| 1628 | const char *cmd,
|
| 1629 | char *reply,
|
| 1630 | size_t reply_len
|
| 1631 | )
|
| 1632 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1633 | LOGE("cmd:%s\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1634 | bzero(reply,reply_len);
|
| 1635 | sta_err_enum err = STA_ERR_UNKNOWN;
|
| 1636 | if(strncmp(cmd,(STA_CMD_OPEN),strlen(STA_CMD_OPEN)) == 0){
|
| 1637 | // Will OPEN connection with wpa_supplicant.
|
| 1638 | err = sta_cli_init();
|
| 1639 | if(err == STA_ERR_SUCCESS){
|
| 1640 | snprintf(reply,reply_len,
|
| 1641 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1642 | STA_TAG_CMD,
|
| 1643 | cmd,
|
| 1644 | STA_TAG_CMD_SUCCESS);
|
| 1645 | sta_should_send_connected_msg = FALSE;
|
| 1646 | }else{
|
| 1647 | snprintf(reply,reply_len,
|
| 1648 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1649 | STA_TAG_CMD,
|
| 1650 | cmd,
|
| 1651 | STA_TAG_CMD_FAIL,
|
| 1652 | err);
|
| 1653 | }
|
| 1654 | }else if(strncmp(cmd,STA_CMD_CLOSE,strlen(STA_CMD_CLOSE)) == 0){
|
| 1655 | err = sta_cli_deinit();
|
| 1656 | if(err == STA_ERR_SUCCESS){
|
| 1657 | snprintf(reply,reply_len,
|
| 1658 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1659 | STA_TAG_CMD,
|
| 1660 | cmd,
|
| 1661 | STA_TAG_CMD_SUCCESS);
|
| 1662 | sta_should_send_connected_msg = TRUE;
|
| 1663 | }else{
|
| 1664 | snprintf(reply,reply_len,
|
| 1665 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1666 | STA_TAG_CMD,
|
| 1667 | cmd,
|
| 1668 | STA_TAG_CMD_FAIL,
|
| 1669 | err);
|
| 1670 | }
|
| 1671 | }else if(strncmp(cmd,STA_CMD_SCAN,strlen(STA_CMD_SCAN)) == 0){
|
| 1672 | char data[STA_BUF_SIZE];
|
| 1673 | err = sta_cli_cmd_scan(cmd, data,STA_BUF_SIZE);
|
| 1674 | if(err == STA_ERR_SUCCESS){
|
| 1675 | snprintf(reply,reply_len,
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1676 | "%s"STA_CMD_SEPARATED,
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1677 | data);
|
| 1678 | }else{
|
| 1679 | snprintf(reply,reply_len,
|
| 1680 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1681 | STA_TAG_CMD,
|
| 1682 | cmd,
|
| 1683 | STA_TAG_CMD_FAIL,
|
| 1684 | err);
|
| 1685 | }
|
| 1686 | }else if(strncmp(cmd,STA_CMD_STATUS,strlen(STA_CMD_STATUS)) == 0){
|
| 1687 | char data[STA_BUF_SIZE];
|
| 1688 | err = sta_cli_cmd_status(cmd, data,STA_BUF_SIZE);
|
| 1689 | if(err == STA_ERR_SUCCESS){
|
| 1690 | snprintf(reply,reply_len,
|
| 1691 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1692 | STA_TAG_CMD,
|
| 1693 | cmd,
|
| 1694 | data);
|
| 1695 | }else{
|
| 1696 | snprintf(reply,reply_len,
|
| 1697 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1698 | STA_TAG_CMD,
|
| 1699 | cmd,
|
| 1700 | STA_TAG_CMD_FAIL,
|
| 1701 | err);
|
| 1702 | }
|
| 1703 | }else if(strncmp(cmd,STA_CMD_MIB,strlen(STA_CMD_MIB)) == 0){
|
| 1704 | char data[STA_BUF_SIZE];
|
| 1705 | err = sta_cli_cmd_mib(cmd, data,STA_BUF_SIZE);
|
| 1706 | if(err == STA_ERR_SUCCESS){
|
| 1707 | snprintf(reply,reply_len,
|
| 1708 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1709 | STA_TAG_CMD,
|
| 1710 | cmd,
|
| 1711 | data);
|
| 1712 | }else{
|
| 1713 | snprintf(reply,reply_len,
|
| 1714 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1715 | STA_TAG_CMD,
|
| 1716 | cmd,
|
| 1717 | STA_TAG_CMD_FAIL,
|
| 1718 | err);
|
| 1719 | }
|
| 1720 | }else if(strncmp(cmd,STA_CMD_RECONFIGURE,strlen(STA_CMD_RECONFIGURE)) == 0){
|
| 1721 | err = sta_cli_cmd_reconfigure(cmd);
|
| 1722 | if(err == STA_ERR_SUCCESS){
|
| 1723 | snprintf(reply,reply_len,
|
| 1724 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1725 | STA_TAG_CMD,
|
| 1726 | cmd,
|
| 1727 | STA_TAG_CMD_SUCCESS);
|
| 1728 | }else{
|
| 1729 | snprintf(reply,reply_len,
|
| 1730 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1731 | STA_TAG_CMD,
|
| 1732 | cmd,
|
| 1733 | STA_TAG_CMD_FAIL,
|
| 1734 | err);
|
| 1735 | }
|
| 1736 | }else if(strncmp(cmd,STA_CMD_DISCONNECT,strlen(STA_CMD_DISCONNECT)) == 0){
|
| 1737 | sta_disconnectting = TRUE;
|
| 1738 | err = sta_cli_cmd_disconnect(cmd);
|
| 1739 | // Not reply(Reply after disconnecd).
|
| 1740 | #if 0
|
| 1741 | if(err == STA_ERR_SUCCESS){
|
| 1742 | snprintf(reply,reply_len,
|
| 1743 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1744 | STA_TAG_CMD,
|
| 1745 | cmd,
|
| 1746 | STA_TAG_CMD_SUCCESS);
|
| 1747 | }else{
|
| 1748 | snprintf(reply,reply_len,
|
| 1749 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1750 | STA_TAG_CMD,
|
| 1751 | cmd,
|
| 1752 | STA_TAG_CMD_FAIL,
|
| 1753 | err);
|
| 1754 | }
|
| 1755 | #endif
|
| 1756 | if(err == STA_ERR_SUCCESS)
|
| 1757 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1758 | LOGE("sta_cli_cmd_disconnect success.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1759 | //pthread_mutex_lock(&sta_mutex);
|
| 1760 | //if(sta_connected)
|
| 1761 | //{
|
| 1762 | #define GET_STATUS_MAX 5
|
| 1763 | int count = 0;
|
| 1764 | bool ok = FALSE;
|
| 1765 | usleep(500);
|
| 1766 | while(count++ < GET_STATUS_MAX)
|
| 1767 | {
|
| 1768 | sta_err_enum err = sta_cli_process_cmd(STA_CMD_STATUS);
|
| 1769 | if(STA_ERR_SUCCESS == err){
|
| 1770 | char mac_ap[STA_MAC_LEN + 1];
|
| 1771 | if(!sta_cli_value_get(sta_cli_cmd_reply,
|
| 1772 | "bssid=",
|
| 1773 | mac_ap,
|
| 1774 | STA_MAC_LEN + 1)) // Disconnected.
|
| 1775 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1776 | LOGE("Disconnected success.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1777 | snprintf(reply,reply_len,
|
| 1778 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1779 | STA_TAG_CMD,
|
| 1780 | cmd,
|
| 1781 | STA_TAG_CMD_SUCCESS);
|
| 1782 | ok = TRUE;
|
| 1783 | break;
|
| 1784 | }else{ // Connected.
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1785 | LOGE("Not disconnected.Try again(STATUS).\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1786 | usleep(500);
|
| 1787 | }
|
| 1788 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1789 | LOGE("STATUS cmd fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1790 | break;
|
| 1791 | }
|
| 1792 | }
|
| 1793 |
|
| 1794 | if(!ok) // fail
|
| 1795 | {
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1796 | LOGE("Disconnect fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1797 | snprintf(reply,reply_len,
|
| 1798 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1799 | STA_TAG_CMD,
|
| 1800 | cmd,
|
| 1801 | STA_TAG_CMD_FAIL,
|
| 1802 | STA_ERR_UNKNOWN);
|
| 1803 | }
|
| 1804 | //sta_connected = FALSE;
|
| 1805 | //}
|
| 1806 | //pthread_mutex_unlock(&sta_mutex);
|
| 1807 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 1808 | LOGE("sta_cli_cmd_disconnect fail.\n");
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 1809 | snprintf(reply,reply_len,
|
| 1810 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1811 | STA_TAG_CMD,
|
| 1812 | cmd,
|
| 1813 | STA_TAG_CMD_FAIL,
|
| 1814 | err);
|
| 1815 | }
|
| 1816 | sta_disconnectting = FALSE;
|
| 1817 | }else if(strncmp(cmd,STA_CMD_RECONNECT,strlen(STA_CMD_RECONNECT)) == 0){
|
| 1818 | err = sta_cli_cmd_reconnect(cmd);
|
| 1819 | if(err == STA_ERR_SUCCESS){
|
| 1820 | snprintf(reply,reply_len,
|
| 1821 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1822 | STA_TAG_CMD,
|
| 1823 | cmd,
|
| 1824 | STA_TAG_CMD_SUCCESS);
|
| 1825 | }else{
|
| 1826 | snprintf(reply,reply_len,
|
| 1827 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1828 | STA_TAG_CMD,
|
| 1829 | cmd,
|
| 1830 | STA_TAG_CMD_FAIL,
|
| 1831 | err);
|
| 1832 | }
|
| 1833 | }else if(strncmp(cmd,STA_CMD_SAVE_CONFIG,strlen(STA_CMD_SAVE_CONFIG)) == 0){
|
| 1834 | err = sta_cli_cmd_save_config(cmd);
|
| 1835 | if(err == STA_ERR_SUCCESS){
|
| 1836 | snprintf(reply,reply_len,
|
| 1837 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1838 | STA_TAG_CMD,
|
| 1839 | cmd,
|
| 1840 | STA_TAG_CMD_SUCCESS);
|
| 1841 | }else{
|
| 1842 | snprintf(reply,reply_len,
|
| 1843 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1844 | STA_TAG_CMD,
|
| 1845 | cmd,
|
| 1846 | STA_TAG_CMD_FAIL,
|
| 1847 | err);
|
| 1848 | }
|
| 1849 | }else if(strncmp(cmd,STA_CMD_SET_NETWORK,strlen(STA_CMD_SET_NETWORK)) == 0){
|
| 1850 | err = sta_cli_cmd_set_network(cmd,NULL);
|
| 1851 | if(err == STA_ERR_SUCCESS){
|
| 1852 | snprintf(reply,reply_len,
|
| 1853 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1854 | STA_TAG_CMD,
|
| 1855 | cmd,
|
| 1856 | STA_TAG_CMD_SUCCESS);
|
| 1857 | }else{
|
| 1858 | snprintf(reply,reply_len,
|
| 1859 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1860 | STA_TAG_CMD,
|
| 1861 | cmd,
|
| 1862 | STA_TAG_CMD_FAIL,
|
| 1863 | err);
|
| 1864 | }
|
| 1865 | }else if(strncmp(cmd,STA_CMD_GET_NETWORK,strlen(STA_CMD_GET_NETWORK)) == 0){
|
| 1866 | char data[100];
|
| 1867 | err = sta_cli_cmd_get_network(cmd,data,100);
|
| 1868 | if(err == STA_ERR_SUCCESS){
|
| 1869 | snprintf(reply,reply_len,
|
| 1870 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1871 | STA_TAG_CMD,
|
| 1872 | cmd,
|
| 1873 | data);
|
| 1874 | }else{
|
| 1875 | snprintf(reply,reply_len,
|
| 1876 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1877 | STA_TAG_CMD,
|
| 1878 | cmd,
|
| 1879 | STA_TAG_CMD_FAIL,
|
| 1880 | err);
|
| 1881 | }
|
| 1882 | }else if(strncmp(cmd,STA_CMD_REMOVE_NETWORK,strlen(STA_CMD_REMOVE_NETWORK)) == 0){
|
| 1883 | err = sta_cli_cmd_remove_network(cmd);
|
| 1884 | if(err == STA_ERR_SUCCESS){
|
| 1885 | snprintf(reply,reply_len,
|
| 1886 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1887 | STA_TAG_CMD,
|
| 1888 | cmd,
|
| 1889 | STA_TAG_CMD_SUCCESS);
|
| 1890 | }else{
|
| 1891 | snprintf(reply,reply_len,
|
| 1892 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1893 | STA_TAG_CMD,
|
| 1894 | cmd,
|
| 1895 | STA_TAG_CMD_FAIL,
|
| 1896 | err);
|
| 1897 | }
|
| 1898 | }else if(strncmp(cmd,STA_CMD_ADD_NETWORK,strlen(STA_CMD_ADD_NETWORK)) == 0){
|
| 1899 | err = sta_cli_cmd_add_network(cmd);
|
| 1900 | if(err == STA_ERR_SUCCESS){
|
| 1901 | snprintf(reply,reply_len,
|
| 1902 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1903 | STA_TAG_CMD,
|
| 1904 | cmd,
|
| 1905 | STA_TAG_CMD_SUCCESS);
|
| 1906 | }else{
|
| 1907 | snprintf(reply,reply_len,
|
| 1908 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1909 | STA_TAG_CMD,
|
| 1910 | cmd,
|
| 1911 | STA_TAG_CMD_FAIL,
|
| 1912 | err);
|
| 1913 | }
|
| 1914 | }else if(strncmp(cmd,STA_CMD_DISABLE_NETWORK,strlen(STA_CMD_DISABLE_NETWORK)) == 0){
|
| 1915 | err = sta_cli_cmd_disable_network(cmd);
|
| 1916 | if(err == STA_ERR_SUCCESS){
|
| 1917 | snprintf(reply,reply_len,
|
| 1918 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1919 | STA_TAG_CMD,
|
| 1920 | cmd,
|
| 1921 | STA_TAG_CMD_SUCCESS);
|
| 1922 | }else{
|
| 1923 | snprintf(reply,reply_len,
|
| 1924 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1925 | STA_TAG_CMD,
|
| 1926 | cmd,
|
| 1927 | STA_TAG_CMD_FAIL,
|
| 1928 | err);
|
| 1929 | }
|
| 1930 | }else if(strncmp(cmd,STA_CMD_ENABLE_NETWORK,strlen(STA_CMD_ENABLE_NETWORK)) == 0){
|
| 1931 | err = sta_cli_cmd_enable_network(cmd);
|
| 1932 | if(err == STA_ERR_SUCCESS){
|
| 1933 | snprintf(reply,reply_len,
|
| 1934 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1935 | STA_TAG_CMD,
|
| 1936 | cmd,
|
| 1937 | STA_TAG_CMD_SUCCESS);
|
| 1938 | }else{
|
| 1939 | snprintf(reply,reply_len,
|
| 1940 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1941 | STA_TAG_CMD,
|
| 1942 | cmd,
|
| 1943 | STA_TAG_CMD_FAIL,
|
| 1944 | err);
|
| 1945 | }
|
| 1946 | }else if(strncmp(cmd,STA_CMD_SELECT_NETWORK,strlen(STA_CMD_SELECT_NETWORK)) == 0){
|
| 1947 | err = sta_cli_cmd_select_network(cmd);
|
| 1948 | if(err == STA_ERR_SUCCESS){
|
| 1949 | snprintf(reply,reply_len,
|
| 1950 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1951 | STA_TAG_CMD,
|
| 1952 | cmd,
|
| 1953 | STA_TAG_CMD_SUCCESS);
|
| 1954 | }else{
|
| 1955 | snprintf(reply,reply_len,
|
| 1956 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1957 | STA_TAG_CMD,
|
| 1958 | cmd,
|
| 1959 | STA_TAG_CMD_FAIL,
|
| 1960 | err);
|
| 1961 | }
|
| 1962 | }else if(strncmp(cmd,STA_CMD_LIST_NETWORKS,strlen(STA_CMD_REASSOCIATE)) == 0){
|
| 1963 | char data[STA_BUF_SIZE];
|
| 1964 | err = sta_cli_cmd_list_networks(cmd, data,STA_BUF_SIZE);
|
| 1965 |
|
| 1966 | // char reply_tmp[STA_BUF_SIZE];
|
| 1967 | // memset(reply_tmp,0,STA_BUF_SIZE);
|
| 1968 | // char *reply_ptr = data;
|
| 1969 | // int index = 0;
|
| 1970 | // int pos = 0;
|
| 1971 | // while(*reply_ptr != '\0'){
|
| 1972 | // if(*reply_ptr == ','){
|
| 1973 | // index++;
|
| 1974 | // }
|
| 1975 |
|
| 1976 | // if(index == 3){
|
| 1977 | // if(*reply_ptr == '\n'){
|
| 1978 | // index = 0;
|
| 1979 | // reply_tmp[pos++] = '\r';
|
| 1980 | // reply_tmp[pos++] = '\n';
|
| 1981 | // }
|
| 1982 | // }else{
|
| 1983 | // reply_tmp[pos++] = *reply_ptr;
|
| 1984 | // }
|
| 1985 |
|
| 1986 | // reply_ptr++;
|
| 1987 | // }
|
| 1988 |
|
| 1989 | if(err == STA_ERR_SUCCESS){
|
| 1990 | snprintf(reply,reply_len,
|
| 1991 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 1992 | STA_TAG_CMD,
|
| 1993 | cmd,
|
| 1994 | data);
|
| 1995 | }else{
|
| 1996 | snprintf(reply,reply_len,
|
| 1997 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 1998 | STA_TAG_CMD,
|
| 1999 | cmd,
|
| 2000 | STA_TAG_CMD_FAIL,
|
| 2001 | err);
|
| 2002 | }
|
| 2003 | }else if(strncmp(cmd,STA_CMD_REASSOCIATE,strlen(STA_CMD_REASSOCIATE)) == 0){
|
| 2004 | err = sta_cli_cmd_reassociate(cmd);
|
| 2005 | if(err == STA_ERR_SUCCESS){
|
| 2006 | snprintf(reply,reply_len,
|
| 2007 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 2008 | STA_TAG_CMD,
|
| 2009 | cmd,
|
| 2010 | STA_TAG_CMD_SUCCESS);
|
| 2011 | }else{
|
| 2012 | snprintf(reply,reply_len,
|
| 2013 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 2014 | STA_TAG_CMD,
|
| 2015 | cmd,
|
| 2016 | STA_TAG_CMD_FAIL,
|
| 2017 | err);
|
| 2018 | }
|
| 2019 | }else if(strncmp(cmd,STA_CMD_REATTACH,strlen(STA_CMD_REATTACH)) == 0){
|
| 2020 | err = sta_cli_cmd_reattach(cmd);
|
| 2021 | if(err == STA_ERR_SUCCESS){
|
| 2022 | snprintf(reply,reply_len,
|
| 2023 | "%s-%s-%s"STA_CMD_SEPARATED,
|
| 2024 | STA_TAG_CMD,
|
| 2025 | cmd,
|
| 2026 | STA_TAG_CMD_SUCCESS);
|
| 2027 | }else{
|
| 2028 | snprintf(reply,reply_len,
|
| 2029 | "%s-%s-%s:%d"STA_CMD_SEPARATED,
|
| 2030 | STA_TAG_CMD,
|
| 2031 | cmd,
|
| 2032 | STA_TAG_CMD_FAIL,
|
| 2033 | err);
|
| 2034 | }
|
| 2035 | }else{
|
liuyang | 09c3d7a | 2024-10-25 10:56:51 +0800 | [diff] [blame] | 2036 | LOGE("Unknown cmd:%s\n",cmd);
|
liuyang | f01f277 | 2024-10-18 16:33:46 +0800 | [diff] [blame] | 2037 | return FALSE;
|
| 2038 | }
|
| 2039 | return TRUE;
|
| 2040 | }
|
| 2041 |
|
| 2042 |
|