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 char awss_finished = 2;
11 char awss_stop_connecting = 0;
__awss_start(void)12 int __awss_start(void)
13 {
14     char ssid[OS_MAX_SSID_LEN + 1] = { 0 },
15                                 passwd[OS_MAX_PASSWD_LEN + 1] = { 0 };
16     /*    enum AWSS_AUTH_TYPE auth = AWSS_AUTH_TYPE_INVALID;
17         enum AWSS_ENC_TYPE encry = AWSS_ENC_TYPE_INVALID;
18         uint8_t channel = 0;
19         */
20     uint8_t bssid[OS_ETH_ALEN] = { 0 };
21     uint8_t token[ZC_MAX_TOKEN_LEN] = { 0 };
22 
23     uint8_t find_token = 0;
24     int ret = -1;
25     uint8_t i;
26     awss_stop_connecting = 0;
27     awss_finished = 0;
28     /* these params is useless, keep it for compatible reason */
29     aws_start(NULL, NULL, NULL, NULL);
30 
31     ret = aws_get_ssid_passwd(&ssid[0], &passwd[0], &bssid[0], &token[0], NULL,
32                               NULL, NULL);
33     if (!ret) {
34         awss_warn("awss timeout!");
35     }
36     for (i = 0; i < ZC_MAX_TOKEN_LEN; i++) {
37         if (token[i] != 0) {
38             find_token = 1;
39             break;
40         }
41     }
42 
43     if (awss_stop_connecting) {
44         awss_finished = 1;
45         return -1;
46     }
47     aws_destroy();
48     ret = -1;
49     do {
50 #if defined(AWSS_SUPPORT_AHA)
51         char awss_notify_needed = 1;
52 #endif
53 
54         if (awss_stop_connecting || strlen(ssid) == 0) {
55             break;
56         }
57 #if defined(AWSS_SUPPORT_AHA)
58         if (strcmp(ssid, DEFAULT_SSID) == 0) {
59             awss_notify_needed = 0;
60             awss_event_post(IOTX_AWSS_CONNECT_AHA);
61         } else
62 #endif
63         {
64             awss_event_post(IOTX_AWSS_CONNECT_ROUTER);
65             AWSS_UPDATE_STATIS(AWSS_STATIS_CONN_ROUTER_IDX,
66                                AWSS_STATIS_TYPE_TIME_START);
67         }
68 
69         ret = awss_connect(ssid, passwd, bssid, ETH_ALEN,
70                            find_token != 0 ? token : NULL,
71                            find_token == 1 ? ZC_MAX_TOKEN_LEN : 0);
72         /*ret = HAL_Awss_Connect_Ap(WLAN_CONNECTION_TIMEOUT_MS, ssid, passwd,
73                                   auth, encry, bssid, channel);*/
74         if (!ret) {
75             awss_debug("awss connect ssid:%s success", ssid);
76             printf("awss connect ssid:%s success\r\n", ssid);
77             awss_event_post(IOTX_AWSS_GOT_IP);
78 
79 #if defined(AWSS_SUPPORT_AHA)
80             if (awss_notify_needed == 0) {
81 #ifndef DEV_BIND_DISABLE_NOTIFY
82                 awss_dev_bind_notify_stop();
83 #endif
84                 awss_suc_notify_stop();
85                 awss_cmp_local_init(AWSS_LC_INIT_ROUTER);
86                 awss_devinfo_notify();
87                 AWSS_UPDATE_STATIS(AWSS_STATIS_ROUTE_IDX,
88                                    AWSS_STATIS_TYPE_TIME_SUC);
89                 awss_event_post(IOTX_AWSS_SETUP_NOTIFY);
90             } else
91 #endif
92             {
93                 AWSS_UPDATE_STATIS(AWSS_STATIS_CONN_ROUTER_IDX,
94                                    AWSS_STATIS_TYPE_TIME_SUC);
95                 awss_devinfo_notify_stop();
96                 /*produce_random(aes_random, sizeof(aes_random));*/
97             }
98         } else {
99             awss_debug("awss connect ssid:%s fail", ssid);
100 #if defined(AWSS_SUPPORT_AHA)
101             if (awss_notify_needed == 0) {
102                 awss_event_post(IOTX_AWSS_CONNECT_AHA_FAIL);
103             } else {
104 #endif
105                 awss_event_post(IOTX_AWSS_CONNECT_ROUTER_FAIL);
106 #if defined(AWSS_SUPPORT_AHA)
107             }
108 #endif
109         }
110     } while (0);
111 
112     AWSS_DISP_STATIS();
113     awss_finished = 1;
114     return ret;
115 }
116 
__awss_stop(void)117 int __awss_stop(void)
118 {
119     awss_stop_connecting = 1;
120     aws_destroy();
121 #if defined(AWSS_SUPPORT_AHA)
122     awss_devinfo_notify_stop();
123 #endif
124     awss_suc_notify_stop();
125 #ifndef AWSS_DISABLE_REGISTRAR
126     awss_registrar_deinit();
127 #endif
128     if (awss_finished < 2) {
129         awss_cmp_local_deinit(1);
130     } else {
131         awss_cmp_local_deinit(0);
132     }
133 
134     while (1) {
135         if (awss_finished) {
136             break;
137         }
138         HAL_SleepMs(300);
139     }
140     aws_release_mutex();
141     awss_finished = 2;
142     return 0;
143 }
144 
145 #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
146 }
147 #endif
148