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