1 /*
2  * Copyright (C) 2019-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef NETMGR_H
6 #define NETMGR_H
7 #include <sys/socket.h>
8 #include <stdbool.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include <uservice/uservice.h>
15 
16 /**
17  * @defgroup netmgr_api netmgr
18  * @{
19  */
20 
21 /** @brief  ethernet speed type */
22 typedef enum {
23     ETH_MAC_SPEED_10M,
24     ETH_MAC_SPEED_100M,
25     ETH_MAC_SPEED_1000M,
26 } eth_mac_speed_e;
27 
28 /** @brief  ethernet duplex type */
29 typedef enum {
30     ETH_MAC_DUPLEX_HALF,
31     ETH_MAC_DUPLEX_FULL,
32 } eth_mac_duplex_e;
33 
34 #define IPADDR_STR_LEN 16
35 /** @brief  this struct defines netmgr ifconfig info */
36 typedef struct netmgr_ifconfig_info {
37    bool dhcp_en;                         /**< dhcp is enabled */
38    char ip_addr[IPADDR_STR_LEN];         /**< ip address */
39    char mask[IPADDR_STR_LEN];            /**< ip address mask */
40    char gw[IPADDR_STR_LEN];              /**< gateway ip address */
41    char dns_server[IPADDR_STR_LEN];      /**< dns server address */
42    char mac[IPADDR_STR_LEN + 1];                          /**< mac address */
43    int  rssi;                            /**< rssi */
44 } netmgr_ifconfig_info_t;
45 
46 #define NETMGR_SSID_MAX_LEN         (32)
47 #define NETMGR_PWD_MAX_LEN          (64)
48 #ifdef ETH_ALEN
49 #define NETMGR_BSSID_MAX_LEN        (ETH_ALEN)
50 #else
51 #define NETMGR_BSSID_MAX_LEN        (6)
52 #endif
53 
54 /** @brief  ssid string format */
55 typedef enum {
56     NETMGR_SSID_FORMAT_UTF8,
57     NETMGR_SSID_FORMAT_GBK,
58 } netmgr_ssid_format_e;
59 
60 #define MAX_AP_CONFIG_NUM   (3)
61 
62 /** @brief  this struct defines wifi ap info */
63 typedef struct {
64     char     ssid[NETMGR_SSID_MAX_LEN+1];       /**< ssid of wifi ap */
65     uint8_t  pwd[NETMGR_PWD_MAX_LEN+1];         /**< password of wifi ap */
66     uint8_t  bssid[NETMGR_BSSID_MAX_LEN];       /**< bssid of wifi ap */
67     int8_t   ap_power;                          /**< signal strength of wifi ap */
68     uint8_t  channel;                           /**< signal channel of wifi ap */
69     uint8_t  sec_type;                          /**< details see netmgr_wifi_sec_type */
70     bool     contain_chinese;                   /**< true:contain chinese false:no chinese */
71     netmgr_ssid_format_e  ssid_format;          /**< ssid string format */
72     char     gbk_ssid[NETMGR_SSID_MAX_LEN+1];   /**< gbk ssid string */
73 } netmgr_wifi_ap_info_t;
74 
75 /** @brief  this struct defines wifi ap config */
76 typedef struct {
77     int ap_num;                                          /**< ap number of array */
78     int used_ap;                                         /**< ap that is used in the array */
79     netmgr_wifi_ap_info_t config[MAX_AP_CONFIG_NUM];     /**< The ap information array */
80 } netmgr_wifi_ap_config_t, netmgr_wifi_config_t;
81 
82 /** @brief  network type */
83 typedef enum {
84     NETMGR_TYPE_WIFI,
85     NETMGR_TYPE_GPRS,
86     NETMGR_TYPE_NBIOT,
87     NETMGR_TYPE_ETH,
88     NETMGR_TYPE_UNKNOWN,
89     NETMGR_TYPE_MAX
90 } netmgr_type_t;
91 
92 /** @brief  netmgr message type */
93 typedef enum {
94     NETMGR_MSGID_MIN = 0,
95     NETMGR_MSGID_WIFI_STATUS = NETMGR_MSGID_MIN,
96     NETMGR_MSGID_WIFI_STATUS_FROM_IMPL,
97     NETMGR_MSGID_WIFI_TRACE_FROM_IMPL,
98     NETMGR_MSGID_NETWORK_STATUS,
99     NETMGR_MSGID_ETH_STATUS_FROM_IMPL,
100     NETMGR_MSGID_MAX
101 } netmgr_msgid_t;
102 
103 /** @brief  netmgr wifi type */
104 typedef enum {
105     NETMGR_WIFI_MODE_STA = 0,      /**< WiFi station mode */
106     NETMGR_WIFI_MODE_AP,           /**< WiFi soft-AP mode */
107     NETMGR_WIFI_MODE_APSTA,        /**< WiFi station + soft-AP mode */
108     NETMGR_WIFI_MODE_P2P,          /**< WiFi P2P mode */
109     NETMGR_WIFI_MODE_UNKNOWN,         /**< null mode */
110     NETMGR_WIFI_MODE_MAX
111 } netmgr_wifi_mode_t;
112 
113 /** @brief  netmgr config struct */
114 typedef struct netmgr_config {
115     netmgr_type_t type;
116     union {
117         netmgr_wifi_config_t wifi_config;          /**< wifi config struct */
118         //netmgr_gprs_config_t gprs_config;
119         //netmgr_nbiot_config_t nbiot_config;
120         //netmgr_eth_config_t eth_config;
121     } config;
122 } netmgr_config_t;
123 
124 /** @brief  netmgr delete config */
125 typedef struct netmgr_del_config {
126     netmgr_type_t type;
127     union {
128         char ssid[NETMGR_SSID_MAX_LEN+1];      /**< wifi ssid to delete */
129     } config;
130 } netmgr_del_config_t;
131 
132 /** @brief  netmgr wifi params */
133 typedef struct netmgr_wifi_params {
134     netmgr_wifi_mode_t mode;                   /**< wifi mode, support in AP/STA mode*/
135     void* params;                              /**< netmgr wifi params*/
136 } netmgr_wifi_params_t;
137 
138 /** @brief  netmgr wifi ap params */
139 typedef struct netmgr_wifi_ap_params {
140     netmgr_wifi_mode_t mode;                   /**< wifi mode*/
141     char ssid[NETMGR_SSID_MAX_LEN+1];          /**< wifi ssid*/
142     char pwd[NETMGR_PWD_MAX_LEN+1];            /**< wifi password*/
143     char svr_ip[IPADDR_STRLEN_MAX];            /**< server ip*/
144     char gw_ip[IPADDR_STRLEN_MAX];             /**< gateway ip*/
145     char netmask_ip[IPADDR_STRLEN_MAX];        /**< netmask ip*/
146     int  beacon_interval;                      /**< beacon interval*/
147     int  hide;                                 /**< ssid hide*/
148 } netmgr_wifi_ap_params_t;
149 
150 /** @brief  netmgr wifi sta connect params */
151 typedef struct netmgr_wifi_sta_connect_params {
152     netmgr_wifi_mode_t mode;                   /**< wifi mode*/
153     char        ssid[NETMGR_SSID_MAX_LEN+1];   /**< wifi ssid to connect*/
154     char        pwd[NETMGR_PWD_MAX_LEN+1];     /**< wifi password to connect*/
155     char        bssid[NETMGR_BSSID_MAX_LEN];   /**< wifi bssid to connect*/
156     int         timeout;                       /**< wifi connect timeout*/
157 } netmgr_wifi_sta_params_t;
158 
159 /** @brief  netmgr connect params */
160 typedef struct netmgr_connect_params {
161     netmgr_type_t type;
162     union {
163         netmgr_wifi_params_t     wlan_params;     /**< wlan(AP/STA) params*/
164         netmgr_wifi_sta_params_t wifi_params;     /**< wifi sta connect params*/
165         netmgr_wifi_ap_params_t  ap_params;       /**< wifi ap start params*/
166     } params;
167 } netmgr_connect_params_t;
168 
169 #define NETMGR_WIFI_METHOD_MAX_LENGTH       (32)
170 #define NETMGR_WIFI_STATUS_MAX_LENGTH       (32)
171 #define NETMGR_WIFI_SSID_MAX_LENGTH         (32)
172 #define NETMGR_WIFI_PASSWORD_MAX_LENGTH     (64)
173 
174 /** @brief  this struct defines netmgr wifi status change info */
175 typedef struct {
176    char method[NETMGR_WIFI_METHOD_MAX_LENGTH+1];       /**< status change method */
177    int  quantity;                                      /**< signal quantity */
178    char status[NETMGR_WIFI_STATUS_MAX_LENGTH+1];       /**< current status */
179    char ssid[NETMGR_WIFI_SSID_MAX_LENGTH+1];           /**< ssid of wifi */
180    char password[NETMGR_WIFI_PASSWORD_MAX_LENGTH+1];   /**< password of wifi */
181    uint8_t reason_code;                                /**< reason of status change */
182 } netmgr_wifi_network_status_change_t;
183 
184 /** @brief  this struct defines netmgr message */
185 typedef struct {
186     netmgr_msgid_t id;                        /**< netmgr msg id */
187     union {
188         int   status;                         /**< reason of status change */
189         void *network_status_change;          /**< msg content of status change */
190         void *trace;
191     } data;
192 } netmgr_msg_t;
193 
194 /** @brief  this struct defines netmgr message callback function */
195 typedef void (*netmgr_msg_cb_t)(netmgr_msg_t* msg);
196 
197 typedef int netmgr_hdl_t;
198 
199 /** @brief  this struct defines netmgr message callback function */
200 typedef struct {
201     char     ssid[NETMGR_SSID_MAX_LEN+1];            /**< The SSID of an access point. */
202     int8_t   ap_power;                               /**< Received Signal Strength Indication, min: -110, max: 0 */
203     uint8_t  bssid[NETMGR_BSSID_MAX_LEN];            /**< The BSSID of an access point. */
204     uint8_t  channel;                                /**< The RF frequency, 1-13 */
205     uint8_t  sec_type;                               /**< details see netmgr_wifi_sec_type */
206 } netmgr_wifi_ap_list_t;
207 
208 /** @brief  netmgr connection state */
209 typedef enum netmgr_conn_state{
210     CONN_STATE_DISCONNECTING,
211     CONN_STATE_DISCONNECTED,
212     CONN_STATE_CONNECTING,
213     CONN_STATE_CONNECTED,
214     CONN_STATE_OBTAINING_IP,
215     CONN_STATE_NETWORK_CONNECTED,
216     CONN_STATE_FAILED,
217     CONN_STATE_UNKNOWN
218 } netmgr_conn_state_t;
219 
220 /**
221  * @brief net manager init.
222  *
223  * @param[in]  task    NULL for current realize.
224  *
225  * @return  0 on success, negative error on failure.
226  */
227 int netmgr_service_init(utask_t *task);
228 
229 /**
230  * @brief net manager deinit
231  */
232 void netmgr_service_deinit();
233 
234 /**
235  * @brief add netmgr dev
236  *
237  * @param  [in] name    name of netmgr dev
238  *
239  * @return  0 on success, negative error on failure.
240  */
241 int netmgr_add_dev(const char* name);
242 
243 /**
244  * @brief get netmgr handler
245  *
246  * @param  [in] name    name of netmgr dev
247  *
248  * @return  negative error on failure, others on success.
249  */
250 netmgr_hdl_t netmgr_get_dev(const char* name);
251 
252 /**
253  * @brief get netmgr dev type
254  *
255  * @param  [in] hdl     netmgr handle
256  *
257  * @return  NETMGR_TYPE_UNKNOWN on failure, others on success.
258  */
259 netmgr_type_t  get_hdl_type(netmgr_hdl_t hdl);
260 
261 /**
262  * @brief  set if config information
263  *
264  * @param  [in] hdl     netmgr handle
265  * @param  [in] info    config information
266  *
267  * @return >=0 on success, others on error
268  */
269 int netmgr_set_ifconfig(netmgr_hdl_t hdl, netmgr_ifconfig_info_t* info);
270 
271 /**
272  * @brief  get if config information
273  *
274  * @param  [in]  hdl    netmgr handle
275  * @param  [out] info   config information
276  *
277  * @return 0 on success, others on error
278  */
279 int netmgr_get_ifconfig(netmgr_hdl_t hdl, netmgr_ifconfig_info_t* info);
280 
281 /**
282  * @brief  set auto reconnect flag
283  *
284  * @param  [in] hdl     netmgr handle
285  * @param  [in] enable  enable auto reconnect
286  */
287 void netmgr_set_auto_reconnect(netmgr_hdl_t hdl, bool enable);
288 
289 /**
290  * @brief  netmgr connect network
291  *
292  * @param  [in] hdl       netmgr handle
293  * @param  [in] params    netmgr connect params
294  *
295  * @return 0 on success, others on error
296  */
297 int netmgr_connect(netmgr_hdl_t hdl, netmgr_connect_params_t* params);
298 
299 /**
300  * @brief  netmgr disconnect network
301  *
302  * @param  [in] hdl       netmgr handle
303  *
304  * @return 0 on success, others on error
305  */
306 int netmgr_disconnect(netmgr_hdl_t hdl);
307 
308 /**
309  * @brief  get state of hdl
310  *
311  * @param  [in] hdl      netmgr handle
312  *
313  * @return Please refer to #netmgr_conn_state_t
314  */
315 netmgr_conn_state_t netmgr_get_state(netmgr_hdl_t hdl);
316 
317 /**
318  * @brief  netmgr save config
319  *
320  * @param  [in] hdl       netmgr handle
321  *
322  * @return 0 on success, others on error
323  */
324 int netmgr_save_config(netmgr_hdl_t hdl);
325 
326 /**
327  * @brief  get config information
328  *
329  * @param  [in]  hdl      netmgr handle
330  * @param  [out] config   netmgr config information
331  *
332  * @return 0 on success, others on error
333  */
334 int netmgr_get_config(netmgr_hdl_t hdl, netmgr_config_t* config);
335 
336 /**
337  * @brief  delete config information
338  *
339  * @param  [in] hdl       netmgr handle
340  * @param  [in] config    netmgr config information
341  *
342  * @return 0 on success, others on error
343  */
344 int netmgr_del_config(netmgr_hdl_t, netmgr_del_config_t* config);
345 
346 /**
347  * @brief  netmgr set message callback
348  *
349  * @param  [in] hdl          netmgr handle
350  * @param  [in] cb           netmgr msg callback
351  *
352  * @return 0 on success, others on error
353  */
354 int netmgr_set_msg_cb(netmgr_hdl_t hdl, netmgr_msg_cb_t cb);
355 
356 /**
357  * @brief  netmgr delete message callback
358  *
359  * @param  [in] hdl          netmgr handle
360  * @param  [in] cb           netmgr msg callback
361  *
362  * @return 0 on success, others on error
363  */
364 int netmgr_del_msg_cb(netmgr_hdl_t hdl, netmgr_msg_cb_t cb);
365 
366 #ifdef AOS_NETWORK_WITH_WIFI
367 /**
368  * @brief  netmgr wifi get net interface
369  *
370  * @param  [in] hdl          netmgr handle
371  *
372  * @return net interface
373  */
374 void* netmgr_wifi_get_netif(netmgr_hdl_t hdl);
375 
376 /**
377  * @brief  netmgr set connect params
378  *
379  * @param  [in] hdl          netmgr handle
380  * @param  [out] ap_info     ap information
381  * @param  [in] num          number of ap got
382  * @param  [in] type         scan type
383  *
384  * @return 0 on success, others on error
385  */
386 int netmgr_wifi_scan_result(netmgr_hdl_t hdl, netmgr_wifi_ap_list_t* ap_info, int num, netmgr_wifi_scan_type_t type);
387 
388 /**
389  * @brief  netmgr wifi start monitor
390  *
391  * @param  [in] hdl         netmgr handle
392  */
393 void netmgr_wifi_start_monitor(netmgr_hdl_t hdl);
394 
395 /**
396  * @brief  netmgr wifi stop monitor
397  *
398  * @param  [in] hdl
399  */
400 void netmgr_wifi_stop_monitor(netmgr_hdl_t hdl);
401 
402 /**
403  * @brief  netmgr wifi start management monitor
404  *
405  * @param  [in] hdl        netmgr handle
406  */
407 void netmgr_wifi_start_mgnt_monitor(netmgr_hdl_t hdl);
408 
409 /**
410  * @brief  netmgr wifi stop management monitor
411  *
412  * @param  [in] hdl        netmgr handle
413  */
414 void netmgr_wifi_stop_mgnt_monitor(netmgr_hdl_t hdl);
415 
416 /**
417  * @brief  netmgr register management monitory callback
418  *
419  * @param  [in] hdl        netmgr handle
420  * @param  [in] fn         callback of management monitor
421  */
422 void netmgr_wifi_register_mgnt_monitor_cb(netmgr_hdl_t hdl, monitor_data_cb_t fn);
423 
424 /**
425  * @brief  netmgr set connect params
426  *
427  * @param  [in] hdl        netmgr handle
428  * @param  [in] buf        raw frame to send in 80211
429  * @param  [in] len        length of raw frame
430  *
431  * @return 0 on success, others on error
432  */
433 int netmgr_wifi_send_80211_raw_frame(netmgr_hdl_t hdl, uint8_t *buf, int len);
434 #endif
435 
436 /**
437  * @}
438  */
439 
440 #ifdef __cplusplus
441 }
442 #endif
443 
444 #endif
445 
446