1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  *
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <math.h>
10 
11 #ifdef AOS_NETMGR
12 #include "netmgr.h"
13 #endif
14 
15 #include "uvoice_swid.h"
16 
17 
18 #define SOUND_SSID_FLOW_DISCONNECTED	0
19 #define SOUND_SSID_FLOW_MONITOR			1
20 #define SOUND_SSID_FLOW_CONNECTING		2
21 #define SOUND_SSID_FLOW_CONNECTED		3
22 
23 static int sound_ssid_flow = SOUND_SSID_FLOW_DISCONNECTED;
24 static long long ssid_conn_time;
25 static bool uvoice_swid_initialized = false;
26 
lisen_freq_result(char * result)27 static int lisen_freq_result(char *result)
28 {
29 	char *ssid;
30 	char *passwd;
31 
32 	//printf("%s: %s\n", __func__, result);
33 
34 	if (sound_ssid_flow != SOUND_SSID_FLOW_MONITOR) {
35 		if (sound_ssid_flow == SOUND_SSID_FLOW_CONNECTING) {
36 			if (aos_now_ms() - ssid_conn_time > 10000)
37 				sound_ssid_flow = SOUND_SSID_FLOW_MONITOR;
38 			else
39 				return 0;
40 		} else {
41 			return 0;
42 		}
43 	}
44 
45 	ssid = result;
46 	passwd = strchr(result, '\n');
47 
48 	if (!passwd) {
49 		printf("%s: no enter char\n", __func__);
50 		return -1;
51 	}
52 
53 	//sound_ssid_flow = SOUND_SSID_FLOW_CONNECTING;
54 	//ssid_conn_time = aos_now_ms();
55 
56 	*passwd = '\0';
57 	passwd++;
58 	printf("ssid: %s, passwd: %s\n", ssid, passwd);
59 	return 0;
60 
61 	netmgr_ap_config_t config;
62 	memcpy(config.ssid, ssid, strlen(ssid) + 1);
63 	memcpy(config.pwd, passwd, strlen(passwd) + 1);
64 	netmgr_set_ap_config(&config);
65 	printf("connect to '%s'\n", config.ssid);
66 	netmgr_start(false);
67 
68 	return 0;
69 }
70 
lisen_freq_start(void)71 static void lisen_freq_start(void)
72 {
73 	sound_ssid_flow = SOUND_SSID_FLOW_MONITOR;
74 	uvoice_swid_start(lisen_freq_result);
75 }
76 
lisen_freq_stop(void)77 static void lisen_freq_stop(void)
78 {
79 	if (sound_ssid_flow == SOUND_SSID_FLOW_CONNECTING)
80 		sound_ssid_flow = SOUND_SSID_FLOW_CONNECTED;
81 	uvoice_swid_stop();
82 }
83 
test_swid_handle(int argc,char ** argv)84 void test_swid_handle(int argc, char **argv)
85 {
86 	if (!uvoice_swid_initialized) {
87 		uvoice_swid_init();
88 		uvoice_swid_initialized = true;
89 	}
90 
91 	if (argc >= 2) {
92 		if (!strcmp(argv[1], "off")) {
93 			lisen_freq_stop();
94 		} else {
95 			lisen_freq_start();
96 		}
97 		return;
98 	}
99 	lisen_freq_start();
100 }
101 
102