1 /*
2  * Copyright (C) 2019-2020 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __NETMGR_SERVICE_H_
6 #define __NETMGR_SERVICE_H_
7 
8 #include <aos/aos.h>
9 
10 #include <uservice/uservice.h>
11 #include <uservice/event.h>
12 
13 #include <netmgr.h>
14 #include <vfsdev/wifi_dev.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define API_NET_INIT        0
21 #define API_NET_GET_HDL     1
22 //#define API_NET_MACCONFIG   2
23 #define API_NET_IPCONFIG    3
24 #define API_NET_START       5
25 #define API_NET_STOP        6
26 #define API_NET_SUBSCRIBE   7
27 #define API_NET_INFO        8
28 #define API_NET_RESET       9
29 #define API_NET_DHCP_CHECK  10
30 
31 #define API_WIFI_CONFIG_SSID_PSK    11
32 
33 #define API_ETH_CONFIG      21
34 
35 #define API_GPRS_CONFIG     31
36 
37 #define API_WIFI_END        99
38 
39 /* kv key */
40 #define KV_WIFI_EN          "wifi_en"
41 #define KV_WIFI_SSID        "wifi_ssid"
42 #define KV_WIFI_PSK         "wifi_psk"
43 
44 #define KV_ETH_EN           "eth_en"
45 #define KV_ETH_DUPLEX       "eth_dup"
46 #define KV_ETH_SPEED        "eth_speed"
47 
48 #define KV_DHCP_EN          "dhcp_en"
49 #define KV_IP_ADDR          "ip_addr"
50 #define KV_IP_NETMASK       "netmask"
51 #define KV_IP_GW            "gw"
52 
53 #define KV_NBIOT_EN         "nbiot_en"
54 
55 #define KV_GPRS_EN          "gprs_en"
56 
57 #define WIFI_SSID_MAX_LEN 32
58 #define WIFI_PSK_MAX_LEN 64
59 
60 #define WIFI_CHANNEL_MAX 14
61 #define WIFI_CHANNEL_ANY 255
62 
63 typedef struct wifi_ssid_psk_s {
64     char ssid[WIFI_SSID_MAX_LEN + 1];
65     int ssid_length; /* Max 32 */
66 
67     char psk[WIFI_PSK_MAX_LEN + 1];
68     int psk_length; /* Min 8 - Max 64 */
69 
70     uint8_t channel;
71 } wifi_ssid_psk_t;
72 
73 typedef struct {
74     wifi_ssid_psk_t ssid_psk;
75 } wifi_setting_t;
76 
77 typedef struct eth_setting_s {
78     eth_mac_speed_e speed;
79     eth_mac_duplex_e duplex;
80 } eth_setting_t;
81 
82 typedef struct gprs_setting_s {
83     int foo;
84 } gprs_setting_t;
85 
86 typedef struct {
87     int dhcp_en;
88     char *ipaddr;
89     char *netmask;
90     char *gw;
91 } ip_setting_t;
92 
93 //members in node reflect data in nvram
94 #define NETMGR_NAME_LEN (16)
95 typedef struct netmgr_dev_s {
96     slist_t     next;
97 
98     char name[NETMGR_NAME_LEN];
99     union {
100         wifi_setting_t wifi_config;
101         eth_setting_t eth_config;
102         gprs_setting_t gprs_config;
103     } config;
104 
105     uint8_t dhcp_en;
106     ip_addr_t ipaddr;
107     ip_addr_t netmask;
108     ip_addr_t gw;
109 
110     int (*provision)(struct netmgr_dev_s *node);
111     int (*unprovision)(struct netmgr_dev_s *node);
112     int (*info)(struct netmgr_dev_s *node);
113     int (*reset)(struct netmgr_dev_s *node);
114 
115     aos_dev_t *dev;
116 
117     /* status */
118     int is_gotip;
119 
120     /* link status */
121     int is_linkup;
122 } netmgr_dev_t;
123 
124 struct netmgr_uservice;
125 typedef int (*netmgr_srv_func)(struct netmgr_uservice *netmgr, rpc_t *rpc);
126 typedef struct netmgr_srv_s {
127     slist_t     next;
128     int cmd_id;
129     netmgr_srv_func func;
130 } netmgr_srv_t;
131 
132 struct netmgr_uservice {
133     uservice_t *srv;
134     utask_t *task;
135     int      inited;
136 
137     slist_t dev_list;           /* struct netmgr_dev_t*/
138     slist_t srv_list;           /* struct netmgr_srv_t*/
139 
140     //rpc_t *current_cmd;
141 };
142 
143 /**
144  * @brief  handle event of the netmgr uservice
145  * @param  [in] event_id
146  * @param  [in] param
147  * @param  [in] context
148  * @return
149  */
150 void netmgr_event_cb(uint32_t event_id, const void *param, void *context);
151 
152 /**
153  * @brief  start dhcp by the network name
154  * @param  [in] netmgr
155  * @param  [in] name : network name
156  * @return 0/-1
157  */
158 int netmgr_start_dhcp(struct netmgr_uservice *netmgr, const char *name);
159 
160 /**
161  * @brief  find netmgr dev from netdev list by the network name
162  * @param  [in] list : netmgr dev list
163  * @param  [in] name
164  * @return NULL on error
165  */
166 netmgr_dev_t *netmgr_find_dev(slist_t *list, const char *name);
167 
168 /**
169  * @brief  regist service function
170  * @param  [in] cmd_id : command id of the netmgr uservice
171  * @param  [in] func : callback
172  * @return 0/-1
173  */
174 int netmgr_reg_srv_func(int cmd_id, netmgr_srv_func func);
175 
176 /**
177  * @brief  unregist service function
178  * @param  [in] cmd_id : command id of the netmgr uservice
179  * @param  [in] func : callback
180  * @return 0/-1
181  */
182 int netmgr_unreg_srv_func(int cmd_id, netmgr_srv_func func);
183 
184 /**
185  * @brief  subscribe event of the netmgr uservice
186  * @param  [in] cmd_id : EVENT_WIFI_LINK_UP, etc
187  * @return
188  */
189 void netmgr_subscribe(int cmd_id);
190 
191 /**
192  * @brief  unsubscribe event of the netmgr uservice
193  * @param  [in] cmd_id : EVENT_WIFI_LINK_UP, etc
194  * @return
195  */
196 void netmgr_unsubscribe(int cmd_id);
197 
198 /**
199  * @brief  set gotip flag by network name
200  * @param  [in] name
201  * @param  [in] gotip
202  * @return -1 on error
203  */
204 int netmgr_set_gotip(const char *name, int gotip);
205 
206 /**
207  * @brief  set link layer up flag by network name
208  * @param  [in] name
209  * @param  [in] linkup
210  * @return -1 on error
211  */
212 int netmgr_set_linkup(const char *name, int linkup);
213 
214 /**
215  * @brief  create & init the netmgr uservice
216  * @param  [in] task
217  * @return
218  */
219 void netmgr_service_init(utask_t *task);
220 
221 /**
222  * @brief  destroy & uninit the netmgr uservice
223  * @return
224  */
225 void netmgr_service_deinit();
226 
227 #ifdef CONFIG_KV_SMART
228 char *netmgr_kv_get(const char *key);
229 int netmgr_kv_getint(const char *key);
230 #endif
231 
232 extern struct netmgr_uservice netmgr_svc;
233 
234 #ifdef __cplusplus
235 }
236 #endif
237 
238 #endif
239