lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <stdio.h>
|
| 2 | #include <stdlib.h>
|
| 3 | #include <stdbool.h>
|
| 4 | #include <stdio.h>
|
| 5 | #include <string.h>
|
| 6 | #include <errno.h>
|
| 7 | #include <unistd.h>
|
| 8 | #include <sys/types.h>
|
| 9 | #include <sys/time.h>
|
| 10 | #include <getopt.h>
|
| 11 | #include <time.h>
|
| 12 | #include <wifi_msg.h>
|
| 13 | #include "softap_api.h"
|
| 14 |
|
| 15 |
|
| 16 | #include "wifi_sta_ctrl.h"
|
| 17 | #include "wifi_util.h"
|
| 18 | /*
|
| 19 | static char * g_short_string = "u:b:s:d:h";
|
| 20 |
|
| 21 | static struct option g_long_options[] = {
|
| 22 | {"upgrade", required_argument, NULL, 'u'},
|
| 23 | {"boot", required_argument, NULL, 'b'},
|
| 24 | {"status", required_argument, NULL, 's'},
|
| 25 | {"debug", required_argument, NULL, 'd'},
|
| 26 | {"help", no_argument, NULL, 'h'},
|
| 27 | };
|
| 28 |
|
| 29 | static option_handle_t g_option_handle[] = {
|
| 30 | {'u', excute_command_upgrade},
|
| 31 | {'b', excute_command_boot},
|
| 32 | {'s', excute_command_status},
|
| 33 | {'d', excute_command_debug},
|
| 34 | {'h', excute_command_help}
|
| 35 | };
|
| 36 |
|
| 37 | */
|
| 38 |
|
| 39 | int msg_queue_id = 0;
|
| 40 | scan_result_t g_scan_spot_list[60];
|
| 41 | int g_spot_cur_num = 0;
|
| 42 | void usage (void)
|
| 43 | {
|
| 44 | printf ("cmd list: "
|
| 45 | " open open wifi\n"
|
| 46 | " close close wifi\n"
|
| 47 | " results display the scan results\n"
|
| 48 | " mmi display scan list to simulate watch mmi\n"
|
| 49 | " help dispaly this message\n"
|
| 50 | " scan send scan cmd\n"
|
| 51 | " conn connect the ap specified by the index of scan list ,and need input the password\n"
|
| 52 | " forget disconnect and forget the ap\n"
|
| 53 | " dump dump the content of the global variable in wlan_manager\n"
|
| 54 | " cmd you can send a wpa_supplicant cmd directly\n");
|
| 55 | }
|
| 56 |
|
| 57 | static scan_result_t* parse_spot_string(scan_result_t *spot, char *spot_string)
|
| 58 | {
|
| 59 | char *splited_string = NULL;
|
| 60 | char *save = NULL;
|
| 61 | const char *delimiter = ",";
|
| 62 | scan_content_t scan_content = WIFI_STATION_AP_CONTENT_UNDEFINED;
|
| 63 | char spot_string_tmp[256] = {0};
|
| 64 |
|
| 65 | if (spot_string == NULL) {
|
| 66 | return NULL;
|
| 67 | }
|
| 68 |
|
| 69 | safe_strcpy(spot_string_tmp, spot_string, 256);
|
| 70 |
|
| 71 | //·Ö¸î×Ö·û´®£¬´æ´¢µ½½á¹¹Ìå
|
| 72 | splited_string = strtok_r(spot_string_tmp, delimiter, &save);
|
| 73 |
|
| 74 | // wf_log("%s", splited_string);
|
| 75 | while (splited_string) {
|
| 76 | scan_content++;
|
| 77 | switch (scan_content) {
|
| 78 | case WIFI_STATION_AP_MAC:
|
| 79 | safe_strcpy(spot->mac, splited_string, WIFI_STATION_MAC_LEN);
|
| 80 | break;
|
| 81 | case WIFI_STATION_AP_SSID:
|
| 82 | safe_strcpy(spot->ssid, splited_string, WIFI_STATION_SSID_LEN);
|
| 83 | break;
|
| 84 | case WIFI_STATION_AP_AUTH_MODE:
|
| 85 | safe_strcpy(spot->auth_mode, splited_string, WIFI_STATION_AUTH_MODE_LEN);
|
| 86 | break;
|
| 87 | case WIFI_STATION_AP_ENCRYPT_TYPE:
|
| 88 | safe_strcpy(spot->encrypt_type, splited_string, WIFI_STATION_ENCRYPT_TYPE_LEN);
|
| 89 | break;
|
| 90 | case WIFI_STATION_AP_CHANNEL:
|
| 91 | spot->channel = atoi(splited_string);
|
| 92 | break;
|
| 93 | case WIFI_STATION_AP_SIGNAL:
|
| 94 | spot->signal = atoi(splited_string);
|
| 95 | break;
|
| 96 | case WIFI_STATION_AP_CONNECT_STATUS:
|
| 97 | spot->connect_status = atoi(splited_string);
|
| 98 | break;
|
| 99 | case WIFI_STATION_AP_NETWORK_ID:
|
| 100 | spot->network_id = atoi(splited_string);
|
| 101 | break;
|
| 102 | default:
|
| 103 | break;
|
| 104 | }
|
| 105 |
|
| 106 | splited_string = strtok_r(NULL, delimiter, &save);
|
| 107 | // wf_log("%s", splited_string);
|
| 108 | }
|
| 109 |
|
| 110 | if (scan_content != WIFI_STATION_AP_NETWORK_ID) {
|
| 111 | return NULL;
|
| 112 | }
|
| 113 |
|
| 114 | return spot;
|
| 115 |
|
| 116 | }
|
| 117 |
|
| 118 | static void parse_scan_result(char *aplist,char* aplist1)
|
| 119 | {
|
| 120 | char *splited_string = NULL;
|
| 121 | char *save = NULL;
|
| 122 | const char *delimiter = ";";
|
| 123 | char spot_string_tmp[2048+2] = {0};// nv ×¿ÉÒÔ´æ´¢1024 byte
|
| 124 | int index = 0;
|
| 125 | if (aplist == NULL) {
|
| 126 | return;
|
| 127 | }
|
| 128 | if(strlen(aplist)>0){
|
| 129 | safe_strcpy(spot_string_tmp, aplist, sizeof(spot_string_tmp));
|
| 130 | if(strlen(aplist1)>0){
|
| 131 | strcat(spot_string_tmp, ";");
|
| 132 | strcat(spot_string_tmp, aplist1);
|
| 133 | }
|
| 134 | }
|
| 135 | else{
|
| 136 | return;
|
| 137 | }
|
| 138 |
|
| 139 | //·Ö¸î×Ö·û´®£¬´æ´¢µ½½á¹¹Ìå
|
| 140 | splited_string = strtok_r(spot_string_tmp, delimiter, &save);
|
| 141 | // wf_log("profile=%s", splited_string);
|
| 142 | while (splited_string) {
|
| 143 | parse_spot_string(&g_scan_spot_list[index], splited_string);
|
| 144 | index++;
|
| 145 | splited_string = strtok_r(NULL, delimiter, &save);
|
| 146 | // wf_log("profile=%s", splited_string);
|
| 147 | }
|
| 148 |
|
| 149 | g_spot_cur_num = index;
|
| 150 |
|
| 151 | }
|
| 152 |
|
| 153 | static void set_spot_state(msg_act_spot_t *mas)
|
| 154 | {
|
| 155 | int i = 0;
|
| 156 | spot_action_t action = mas->act;
|
| 157 | for(i = 0; i< g_spot_cur_num; i++ ){
|
| 158 | if( strcmp(g_scan_spot_list[i].mac, mas->mac) ==0){
|
| 159 | if(SAVE_ACTION == action){
|
| 160 | g_scan_spot_list[i].network_id = mas->network_id;
|
| 161 | }
|
| 162 | else if(DEL_ACTION == action)
|
| 163 | g_scan_spot_list[i].network_id = -1;
|
| 164 | else if(CONNECTING_ACTION == action)
|
| 165 | g_scan_spot_list[i].connect_status = CONNECTING_STATUS;
|
| 166 | else if(DHCPING_ACTION == action)
|
| 167 | g_scan_spot_list[i].connect_status = DHCPING_STATUS;
|
| 168 | else if(CONNECTED_ACTION == action)
|
| 169 | g_scan_spot_list[i].connect_status = CONNECTED_STATUS;
|
| 170 | else if(DISCONNECT_ACTION == action)
|
| 171 | g_scan_spot_list[i].connect_status = DISCONNECT_STATUS;
|
| 172 |
|
| 173 | break;
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 |
|
| 179 | static void process_msg(MSG_BUF *pMsg)
|
| 180 | {
|
| 181 | switch (pMsg->usMsgCmd) {
|
| 182 | case MSG_CMD_WIFI_STATION_INDICATE_SCAN_RESULTS:
|
| 183 | {
|
| 184 | if(pMsg->usDataLen ==0){
|
| 185 | char aplist[1025]={0};
|
| 186 | char aplist1[1025]={0};
|
| 187 | sc_cfg_get("EX_APLIST", aplist, 1024);
|
| 188 | sc_cfg_get("EX_APLIST1", aplist1, 1024);
|
| 189 | parse_scan_result(aplist,aplist1);
|
| 190 | }
|
| 191 | else
|
| 192 | {
|
| 193 | msg_act_spot_t *mas = (msg_act_spot_t *)pMsg->aucDataBuf;
|
| 194 | set_spot_state(mas);
|
| 195 | }
|
| 196 | break;
|
| 197 | }
|
| 198 | default:
|
| 199 | break;
|
| 200 | }
|
| 201 |
|
| 202 | }
|
| 203 |
|
| 204 | void dump_spot_list()
|
| 205 | {
|
| 206 | int i = 0;
|
| 207 | printf("%-8s%-24s%-32s%-16s%-8s%-8s%-8s\n","Index","MAC","SSID","CHANNEL","SIGNAL","ID","State");
|
| 208 | for(i = 0; i< g_spot_cur_num; i++){
|
| 209 | scan_result_t *spot = &g_scan_spot_list[i];
|
| 210 | printf("%-8d%-24s%-32s%-16d%-8d%-8d%-8s\n",i, spot->mac, spot->ssid,spot->channel,
|
| 211 | spot->signal,spot->network_id, spot->connect_status==CONNECTED_STATUS?"O":"x");
|
| 212 | }
|
| 213 | }
|
| 214 | void dump_conn_ap(spot_t *conn_ap)
|
| 215 | {
|
| 216 | printf("mac=%s\n",conn_ap->mac);
|
| 217 | printf("ssid=%s\n",conn_ap->ssid);
|
| 218 | printf("auth_mode=%s\n",conn_ap->auth_mode);
|
| 219 | printf("encrypt_type=%s\n",conn_ap->encrypt_type);
|
| 220 | printf("password=%s\n",conn_ap->password);
|
| 221 | printf("network_id=%d\n",conn_ap->network_id);
|
| 222 | }
|
| 223 | int mmi_display_loop = 0;
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 | static void *mmi_loop (void *param)
|
| 228 | {
|
| 229 | MSG_BUF wlanMsg ={0};
|
| 230 | char name[32] = {0};
|
| 231 | int ret = -1;
|
| 232 |
|
| 233 | strcpy (name, (char*) param);
|
| 234 | prctl (PR_SET_NAME, name, 0, 0, 0);
|
| 235 |
|
| 236 | while (1) {
|
| 237 | if(0 == mmi_display_loop)break;
|
| 238 | dump_spot_list();
|
| 239 | sleep(1);
|
| 240 | printf("\033[2J");//clear screen
|
| 241 | printf("\033[0;0H");//locate cursor to head
|
| 242 | }
|
| 243 |
|
| 244 | return NULL;
|
| 245 | }
|
| 246 | void mmi_display()
|
| 247 | {
|
| 248 | mmi_display_loop = 1;
|
| 249 |
|
| 250 | wf_create_thread ("mmi_loop", mmi_loop);
|
| 251 |
|
| 252 | while(1){
|
| 253 | char cmd;
|
| 254 | scanf("%c", &cmd);
|
| 255 | if(cmd == 'q'){
|
| 256 | mmi_display_loop = 0;
|
| 257 | break;
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | return;
|
| 262 | }
|
| 263 |
|
| 264 | static void *station_loop (void *param)
|
| 265 | {
|
| 266 | MSG_BUF wlanMsg ={0};
|
| 267 | char name[32] = {0};
|
| 268 | int ret = -1;
|
| 269 |
|
| 270 | strcpy (name, (char*) param);
|
| 271 | prctl (PR_SET_NAME, name, 0, 0, 0);
|
| 272 |
|
| 273 | while (1) {
|
| 274 | memset (&wlanMsg, 0, sizeof (MSG_BUF));
|
| 275 | ret = msgrcv (msg_queue_id, &wlanMsg, sizeof (MSG_BUF) - sizeof (LONG), 0, 0);
|
| 276 | if (ret == -1) {
|
| 277 | continue;
|
| 278 | }
|
| 279 | process_msg (&wlanMsg);
|
| 280 |
|
| 281 | }
|
| 282 |
|
| 283 | return NULL;
|
| 284 | }
|
| 285 |
|
| 286 |
|
| 287 | int main (int argc, char *argv[])
|
| 288 | {
|
| 289 | int c=0;
|
| 290 | int iRet = -1;
|
| 291 | char cmd[128]={0};
|
| 292 |
|
| 293 | msg_queue_id = wf_create_msg_qid (MODULE_ID_WLAN_SERVER);
|
| 294 | wf_create_thread ("wlan-mmi", station_loop);
|
| 295 | printf("rda_mmi v1.0\n"
|
| 296 | "please use results, conn, disc three cmd to control it\n\n");
|
| 297 |
|
| 298 | for (;;) {
|
| 299 |
|
| 300 | printf(">>");
|
| 301 | scanf("%s", cmd);
|
| 302 |
|
| 303 | if(!strcmp(cmd, "open")){
|
| 304 | wf_msg_to_self(MSG_CMD_WIFI_STATION_OPEN, 0, NULL);
|
| 305 | }
|
| 306 | else if(!strcmp(cmd, "close")){
|
| 307 | wf_msg_to_self(MSG_CMD_WIFI_STATION_CLOSE, 0, NULL);
|
| 308 | }
|
| 309 | else if(!strcmp(cmd, "results")){
|
| 310 | dump_spot_list();
|
| 311 | }
|
| 312 | else if(!strcmp(cmd, "scan")){
|
| 313 | wf_msg_to_self(MSG_CMD_WIFI_STATION_SCAN, 0, NULL);
|
| 314 | }
|
| 315 | else if(!strcmp(cmd, "conn")){
|
| 316 | int num = 0;
|
| 317 | char pwd[WIFI_STATION_SPOT_PASSWORD_LEN]={0};
|
| 318 | printf("please enter the ssid num\n num=");
|
| 319 | scanf("%d", &num);
|
| 320 | if(g_scan_spot_list[num].network_id == -1){
|
| 321 | printf("\nplease enter the pwd\n pwd=");
|
| 322 | scanf("%s", pwd);
|
| 323 | }
|
| 324 | spot_t conn_ap ={0};
|
| 325 | strcpy(conn_ap.mac, g_scan_spot_list[num].mac);
|
| 326 | strcpy(conn_ap.ssid, g_scan_spot_list[num].ssid);
|
| 327 | strcpy(conn_ap.auth_mode, g_scan_spot_list[num].auth_mode);
|
| 328 | strcpy(conn_ap.encrypt_type, g_scan_spot_list[num].encrypt_type);
|
| 329 | if(g_scan_spot_list[num].network_id >=0)
|
| 330 | conn_ap.network_id = g_scan_spot_list[num].network_id;
|
| 331 | else{
|
| 332 | strcpy(conn_ap.password, pwd);
|
| 333 | conn_ap.network_id = -1;
|
| 334 | }
|
| 335 | dump_conn_ap(&conn_ap);
|
| 336 | wf_msg_to_self(MSG_CMD_WIFI_STATION_CONNECT, sizeof(conn_ap), &conn_ap);
|
| 337 |
|
| 338 | }
|
| 339 | else if(!strcmp(cmd, "forget")){
|
| 340 | int num = 0;
|
| 341 | printf("please enter the ssid num\n num=");
|
| 342 | scanf("%d", &num);
|
| 343 | spot_t conn_ap ={0};
|
| 344 | strcpy(conn_ap.mac, g_scan_spot_list[num].mac);
|
| 345 | strcpy(conn_ap.ssid, g_scan_spot_list[num].ssid);
|
| 346 | conn_ap.network_id = g_scan_spot_list[num].network_id;
|
| 347 |
|
| 348 | wf_msg_to_self(MSG_CMD_WIFI_STATION_DISCONNECT, sizeof(conn_ap), &conn_ap);
|
| 349 | }
|
| 350 | else if(!strcmp(cmd, "dump")){
|
| 351 | wf_msg_to_self(MSG_CMD_WIFI_STATION_DEBUG_DUMP, 0, NULL);
|
| 352 | }
|
| 353 | else if(!strcmp(cmd, "mmi")){
|
| 354 | mmi_display();
|
| 355 | }
|
| 356 | else if(!strcmp(cmd, "help")){
|
| 357 | usage();
|
| 358 | }
|
| 359 | else if(!strcmp(cmd, "exit") || !strcmp(cmd, "q")){
|
| 360 | exit(0);
|
| 361 | }
|
| 362 | else if(!strcmp(cmd, "cmd")){
|
| 363 | char cmd[128]={0};
|
| 364 | printf("please enter wpa_supplicant cmd:\n");
|
| 365 | scanf("%s", cmd);
|
| 366 | wf_msg_to_self(MSG_CMD_WIFI_STATION_WPA_CMD, strlen(cmd)+1, cmd);
|
| 367 | }
|
| 368 | printf("done\n");
|
| 369 | }
|
| 370 |
|
| 371 |
|
| 372 | return 0;
|
| 373 | }
|