1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 #include "wifi_provision_internal.h"
5 
6 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
7 extern "C" {
8 #endif
9 
10 #define AWSS_PRESS_TIMEOUT_MS  (60 * 1000)
11 #define AHA_MONITOR_TIMEOUT_MS (1 * 60 * 1000)
12 
13 extern int switch_ap_done;
14 static uint8_t awss_stopped = 1;
15 static uint8_t g_user_press = 0;
16 static uint32_t g_config_press_timeout_ms = 3 * 60 * 1000;
17 static uint32_t g_channel_scan_timeout_ms = 250;
18 static uint32_t config_press_start_timestamp = 0;
19 
20 static void awss_press_timeout(void);
21 
awss_set_press_timeout_ms(unsigned int timeout_ms)22 void awss_set_press_timeout_ms(unsigned int timeout_ms)
23 {
24     if (timeout_ms < AWSS_PRESS_TIMEOUT_MS) {
25         timeout_ms = AWSS_PRESS_TIMEOUT_MS;
26     }
27     g_config_press_timeout_ms = timeout_ms;
28 }
29 
awss_get_press_timeout_ms()30 uint32_t awss_get_press_timeout_ms()
31 {
32     return g_config_press_timeout_ms;
33 }
34 
awss_update_config_press()35 void awss_update_config_press()
36 {
37     if (g_user_press && time_elapsed_ms_since(config_press_start_timestamp) >
38                             g_config_press_timeout_ms) {
39         awss_press_timeout();
40     }
41 }
42 
awss_get_channel_scan_interval_ms()43 uint32_t awss_get_channel_scan_interval_ms()
44 {
45     return g_channel_scan_timeout_ms;
46 }
47 
awss_set_channel_scan_interval_ms(uint32_t timeout_ms)48 void awss_set_channel_scan_interval_ms(uint32_t timeout_ms)
49 {
50     g_channel_scan_timeout_ms = timeout_ms;
51 }
52 
awss_success_notify(void)53 int awss_success_notify(void)
54 {
55     g_user_press = 0;
56     awss_press_timeout();
57 
58     awss_cmp_local_init(AWSS_LC_INIT_SUC);
59     awss_suc_notify_stop();
60     awss_suc_notify();
61     awss_start_connectap_monitor();
62     AWSS_DISP_STATIS();
63     return 0;
64 }
65 
66 #if defined(AWSS_SUPPORT_AHA)
awss_aha_connect_to_router()67 static char awss_aha_connect_to_router()
68 {
69     int iter = 0;
70     char dest_ap = 0;
71     char ssid[PLATFORM_MAX_SSID_LEN + 1] = { 0 };
72     int count = AHA_MONITOR_TIMEOUT_MS / 50;
73     for (iter = 0; iter < count; iter++) {
74         memset(ssid, 0, sizeof(ssid));
75         HAL_Wifi_Get_Ap_Info(ssid, NULL, NULL);
76         if (HAL_Sys_Net_Is_Ready() && strlen(ssid) > 0 &&
77             strcmp(ssid, DEFAULT_SSID)) { /* not AHA */
78             dest_ap = 1;
79             break;
80         }
81         if (awss_stopped) {
82             break;
83         }
84         HAL_SleepMs(50);
85     }
86     return dest_ap;
87 }
88 #endif
awss_start(void)89 int awss_start(void)
90 {
91     int ret = -1;
92 
93     if (awss_stopped == 0) {
94         awss_debug("awss already running\n");
95         return -1;
96     }
97 
98     awss_stopped = 0;
99     awss_event_post(IOTX_AWSS_START);
100 
101     ret = __awss_start();
102 #if defined(AWSS_SUPPORT_AHA)
103     do {
104         char ssid[PLATFORM_MAX_SSID_LEN + 1] = { 0 };
105         if (awss_stopped) {
106             break;
107         }
108 
109         if (switch_ap_done) {
110             break;
111         }
112 
113         HAL_Wifi_Get_Ap_Info(ssid, NULL, NULL);
114         if (strlen(ssid) > 0 && strcmp(ssid, DEFAULT_SSID)) { /* not AHA */
115             break;
116         }
117 
118         if (HAL_Sys_Net_Is_Ready()) {
119             char dest_ap = 0;
120 
121             awss_cmp_local_init(AWSS_LC_INIT_PAP);
122             dest_ap = awss_aha_connect_to_router();
123 
124             awss_cmp_local_deinit(0);
125 
126             if (switch_ap_done || awss_stopped) {
127                 break;
128             }
129 
130             if (dest_ap == 1) {
131                 break;
132             }
133         }
134         awss_event_post(IOTX_AWSS_ENABLE_TIMEOUT);
135         ret = __awss_start();
136     } while (1);
137 #endif
138 
139     if (awss_stopped) {
140         return -1;
141     }
142 
143     if (!ret)
144         awss_success_notify();
145 
146     awss_stopped = 1;
147 
148     return 0;
149 }
150 
awss_stop(void)151 int awss_stop(void)
152 {
153     awss_stopped = 1;
154     awss_stop_connectap_monitor();
155     g_user_press = 0;
156     awss_press_timeout();
157 
158     __awss_stop();
159     return 0;
160 }
161 
awss_press_timeout(void)162 static void awss_press_timeout(void)
163 {
164     if (g_user_press) {
165         awss_event_post(IOTX_AWSS_ENABLE_TIMEOUT);
166     }
167     g_user_press = 0;
168 }
169 
awss_config_press(void)170 int awss_config_press(void)
171 {
172     config_press_start_timestamp = os_get_time_ms();
173     awss_trace("enable awss\r\n");
174     g_user_press = 1;
175     awss_event_post(IOTX_AWSS_ENABLE);
176 
177     return 0;
178 }
179 
awss_get_config_press(void)180 uint8_t awss_get_config_press(void)
181 {
182     return g_user_press;
183 }
184 
185 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
186 }
187 #endif
188