1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 #if defined(WIFI_PROVISION_ENABLED)
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include "aos/kernel.h"
14 #include "ulog/ulog.h"
15 #ifdef FEATURE_UND_WITH_WIFI
16 #include "und/und.h"
17 #endif
18 
19 #include <netmgr.h>
20 
21 #include "linkkit/wrappers/wrappers_defs.h"
22 #ifdef AOS_COMP_PWRMGMT
23 #include "aos/pwrmgmt.h"
24 #endif
25 
26 #include "aos/device_core.h"
27 #include "aos/hal/wifi.h"
28 #include <lwip/ip_addr.h>
29 #include "vfsdev/wifi_dev.h"
30 #define TAG "HALAWSS"
31 #include "vfs_types.h"
32 #include "vfs_inode.h"
33 #include "netmgr.h"
34 #include "uservice/eventid.h"
35 
36 #define WIFI_SSID_KV_KEY     "wifi_ssid"
37 #define WIFI_PASSWORD_KV_KEY "wifi_password"
38 
39 awss_recv_80211_frame_cb_t g_ieee80211_handler;
40 
41 #define CONFIG_DEV_AOS
42 static void netmgr_comp_enable(const char *ssid, const char *password);
43 
get_wifi_dev()44 static void *get_wifi_dev()
45 {
46     static netdev_t *wifi_dev = NULL;
47     if (wifi_dev == NULL) {
48         vfs_inode_t *node = vfs_inode_open("/dev/wifi0");
49         wifi_dev = (netdev_t *)node->i_arg;
50     }
51     return wifi_dev;
52 }
53 
get_mac(uint8_t mac[6])54 static void get_mac(uint8_t mac[6])
55 {
56     static uint8_t s_mac[6] = { 0 };
57 
58     if (s_mac[0] == 0 && s_mac[1] == 0 && s_mac[2] == 0 && s_mac[3] == 0 &&
59         s_mac[4] == 0 && s_mac[5] == 0) {
60         netdev_t *wifi_dev = get_wifi_dev();
61         if (wifi_dev == NULL) {
62             return;
63         }
64         hal_wifi_get_mac_addr(wifi_dev, s_mac);
65     }
66 
67     memcpy(mac, s_mac, 6);
68 }
69 
monitor_data_handler(wifi_promiscuous_pkt_t * buf,wifi_promiscuous_pkt_type_t type)70 static void monitor_data_handler(wifi_promiscuous_pkt_t *buf,
71                                  wifi_promiscuous_pkt_type_t type)
72 {
73     if (!buf) {
74         return;
75     }
76     // with_fcs 1, link_type AWSS_LINK_TYPE_NONE, AWSS_LINK_TYPE_80211_RADIO
77     int with_fcs = 0;
78     unsigned char rssi = buf->rx_ctrl.rssi;
79     int link_type = AWSS_LINK_TYPE_NONE;
80     (*g_ieee80211_handler)((char *)buf->payload, buf->rx_ctrl.sig_len,
81                            link_type, with_fcs, rssi);
82 }
83 
HAL_Awss_Open_Monitor(_IN_ awss_recv_80211_frame_cb_t cb)84 void HAL_Awss_Open_Monitor(_IN_ awss_recv_80211_frame_cb_t cb)
85 {
86     netdev_t *wifi_dev = (netdev_t *)get_wifi_dev();
87     if (wifi_dev == NULL) {
88         return;
89     }
90 
91 #ifdef AOS_COMP_PWRMGMT
92     aos_pwrmgmt_lowpower_suspend(PWRMGMT_NETMGR);
93 #endif
94     g_ieee80211_handler = cb;
95 
96     hal_wifi_start_monitor(wifi_dev, monitor_data_handler);
97     HAL_Awss_Switch_Channel(6, 0, NULL);
98 }
99 
HAL_Awss_Close_Monitor(void)100 void HAL_Awss_Close_Monitor(void)
101 {
102     netdev_t *wifi_dev = (netdev_t *)get_wifi_dev();
103     if (wifi_dev == NULL) {
104         return;
105     }
106 
107     hal_wifi_stop_monitor(wifi_dev);
108 }
109 
HAL_Awss_Get_Channelscan_Interval_Ms(void)110 int HAL_Awss_Get_Channelscan_Interval_Ms(void)
111 {
112     return 250;
113 }
114 
HAL_Awss_Get_Timeout_Interval_Ms(void)115 int HAL_Awss_Get_Timeout_Interval_Ms(void)
116 {
117     return 300 * 1000;
118 }
119 
HAL_Awss_Switch_Channel(char primary_channel,char secondary_channel,uint8_t bssid[ETH_ALEN])120 void HAL_Awss_Switch_Channel(char primary_channel, char secondary_channel,
121                              uint8_t bssid[ETH_ALEN])
122 {
123     int ret = -1;
124     netdev_t *wifi_dev = (netdev_t *)get_wifi_dev();
125     if (wifi_dev == NULL) {
126         return;
127     }
128 
129     ret = hal_wifi_set_channel(wifi_dev, (int)primary_channel);
130 }
131 
HAL_Awss_Open_Ap(const char * ssid,const char * passwd,int beacon_interval,int hide)132 int HAL_Awss_Open_Ap(const char *ssid, const char *passwd, int beacon_interval,
133                      int hide)
134 {
135     wifi_config_t config;
136 
137     netdev_t *wifi_dev = (netdev_t *)get_wifi_dev();
138     if (wifi_dev == NULL) {
139         return -1;
140     }
141 
142     hal_wifi_deinit(wifi_dev);
143     memset(&config, 0, sizeof(wifi_config_t));
144 
145     if (ssid != NULL) {
146         strncpy(config.ssid, ssid, HAL_MAX_SSID_LEN);
147     }
148 
149     if (passwd != NULL) {
150         strncpy(config.password, passwd, HAL_MAX_PASSWD_LEN);
151     }
152 
153     config.mode = WIFI_MODE_AP;
154     config.ap_config.beacon_interval = beacon_interval;
155     config.ap_config.hide_ssid = hide;
156     return hal_wifi_init(wifi_dev, &config);
157 }
158 
HAL_Wifi_Send_80211_Raw_Frame(HAL_Awss_Frame_Type_t type,uint8_t * buffer,int len)159 int HAL_Wifi_Send_80211_Raw_Frame(HAL_Awss_Frame_Type_t type, uint8_t *buffer,
160                                   int len)
161 {
162     netdev_t *wifi_dev = (netdev_t *)get_wifi_dev();
163     if (wifi_dev == NULL)
164         return -1;
165     return hal_wifi_send_80211_raw_frame(wifi_dev, buffer, len);
166 }
HAL_Awss_Close_Ap()167 int HAL_Awss_Close_Ap()
168 {
169     netdev_t *wifi_dev = (netdev_t *)get_wifi_dev();
170     if (wifi_dev == NULL) {
171         return -1;
172     }
173 
174     hal_wifi_deinit(wifi_dev);
175 
176     return 0;
177 }
178 
HAL_Awss_Connect_Ap(_IN_ uint32_t connection_timeout_ms,_IN_ char ssid[HAL_MAX_SSID_LEN],_IN_ char passwd[HAL_MAX_PASSWD_LEN],_IN_OPT_ enum AWSS_AUTH_TYPE auth,_IN_OPT_ enum AWSS_ENC_TYPE encry,_IN_OPT_ uint8_t bssid[ETH_ALEN],_IN_OPT_ uint8_t channel)179 int HAL_Awss_Connect_Ap(_IN_ uint32_t connection_timeout_ms,
180                         _IN_ char ssid[HAL_MAX_SSID_LEN],
181                         _IN_ char passwd[HAL_MAX_PASSWD_LEN],
182                         _IN_OPT_ enum AWSS_AUTH_TYPE auth,
183                         _IN_OPT_ enum AWSS_ENC_TYPE encry,
184                         _IN_OPT_ uint8_t bssid[ETH_ALEN],
185                         _IN_OPT_ uint8_t channel)
186 {
187     int ret = -1;
188 
189     LOGE(TAG, "connect ap not implemented");
190 
191     printf("%s-%d ssid:%s paaswd:%s\r\n", __func__, __LINE__, ssid, passwd);
192     ret = aos_kv_set(WIFI_SSID_KV_KEY, ssid, strlen(ssid), 1);
193     if (ret) {
194         printf("aos_kv_set %s ret:%d\r\n", WIFI_SSID_KV_KEY, ret);
195     }
196     ret = aos_kv_set(WIFI_PASSWORD_KV_KEY, passwd, strlen(passwd), 1);
197     if (ret) {
198         printf("aos_kv_set %s ret:%d\r\n", WIFI_PASSWORD_KV_KEY, ret);
199     }
200     netmgr_comp_enable(ssid, passwd);
201 
202     return 0;
203 }
204 
HAL_Wifi_Get_IP(char ip_str[NETWORK_ADDR_LEN],const char * ifname)205 uint32_t HAL_Wifi_Get_IP(char ip_str[NETWORK_ADDR_LEN], const char *ifname)
206 {
207     ip_addr_t ipaddr;
208     ip_addr_t netmask;
209     ip_addr_t gw;
210     uint8_t octet[4] = { 0, 0, 0, 0 };
211 
212     netdev_t *wifi_dev = (netdev_t *)get_wifi_dev();
213     if (wifi_dev == NULL) {
214         return 0;
215     }
216 
217     /** ifconfig */
218     // hal_net_get_ipaddr(wifi_dev, &ipaddr, &netmask, &gw);
219 
220     for (int i = 0; i < 4; i++) {
221         octet[i] = (ipaddr.addr >> ((3 - i) * 8)) & 0xFF;
222     }
223     sprintf(ip_str, "%d.%d.%d.%d", octet[3], octet[2], octet[1], octet[0]);
224 
225     return 0;
226 }
227 
HAL_Wifi_Get_Mac(_OU_ char mac_str[HAL_MAC_LEN])228 char *HAL_Wifi_Get_Mac(_OU_ char mac_str[HAL_MAC_LEN])
229 {
230     uint8_t mac[6] = { 0 };
231 
232     get_mac(mac);
233 
234     snprintf(mac_str, HAL_MAC_LEN, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0],
235              mac[1], mac[2], mac[3], mac[4], mac[5]);
236 
237     return mac_str;
238 }
239 
HAL_Wifi_Get_Ap_Info(char ssid[HAL_MAX_SSID_LEN],char passwd[HAL_MAX_PASSWD_LEN],uint8_t bssid[ETH_ALEN])240 int HAL_Wifi_Get_Ap_Info(char ssid[HAL_MAX_SSID_LEN],
241                          char passwd[HAL_MAX_PASSWD_LEN],
242                          uint8_t bssid[ETH_ALEN])
243 {
244     if (ssid) {
245         int ssid_len;
246         aos_kv_get("wifi_ssid", ssid, &ssid_len);
247     }
248 
249     if (passwd) {
250         int psk_len;
251         aos_kv_get("wifi_psk", ssid, &psk_len);
252     }
253 
254     if (bssid) {
255         memset(bssid, 0, ETH_ALEN);
256     }
257 
258     return 0;
259 }
260 
261 /*
262  * @brief 检查Wi-Fi网卡、芯片或模组当前的IP地址是否有效,AliOS内部
263  *        已经对接完成
264  *
265  * @param None.
266  * @return, 0: IP地址无效; 1: IP地址有效,可以发起网络连接
267  * @see None.
268  * @note
269  *    1)该API对接有问题,可能导致AWSS一致无法退出,有可能出现设
270  *       备成功连接AP后又重新开始扫描
271  *    2)各种配网都需要对接
272  */
HAL_Sys_Net_Is_Ready()273 int HAL_Sys_Net_Is_Ready()
274 {
275     netmgr_hdl_t hdl;
276     netmgr_ifconfig_info_t info = { 0 };
277 
278     hdl = netmgr_get_dev("/dev/wifi0");
279     if (hdl < 0) {
280         return -1;
281     }
282 
283     return (netmgr_get_state(hdl) == CONN_STATE_NETWORK_CONNECTED) ? 1 : 0;
284 }
285 
wifi_event_cb(uint32_t event_id,const void * param,void * context)286 static void wifi_event_cb(uint32_t event_id, const void *param, void *context)
287 {
288     printf("wifi event cb\r\n");
289 }
290 
netmgr_comp_enable(const char * ssid,const char * password)291 static void netmgr_comp_enable(const char *ssid, const char *password)
292 {
293     netmgr_hdl_t fd;
294     netmgr_connect_params_t *params;
295     printf("netmgr test \r\n");
296     if (!ssid || !password) {
297         return;
298     }
299     event_service_init(NULL);
300 
301     netmgr_service_init(NULL);
302 
303     event_subscribe(EVENT_NETMGR_DHCP_SUCCESS, wifi_event_cb, NULL);
304 
305     printf("%s-%d ssid:%s paaswd:%s\r\n", __func__, __LINE__, ssid,
306            password);
307     params = aos_malloc(sizeof(netmgr_connect_params_t));
308 
309     fd = netmgr_get_dev("/dev/wifi0");
310     params->type = NETMGR_TYPE_WIFI;
311     memcpy(params->params.wifi_params.ssid, ssid, strlen(ssid) + 1);
312     memcpy(params->params.wifi_params.pwd, password, strlen(password) + 1);
313     netmgr_connect(fd, params);
314     aos_free(params);
315 
316     return;
317 }
318 
319 #ifdef __cplusplus
320 }
321 #endif
322 #endif
323