1 /*
2  * Copyright (c) 2017 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief WiFi L2 stack public header
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_NET_WIFI_MGMT_H_
13 #define ZEPHYR_INCLUDE_NET_WIFI_MGMT_H_
14 
15 #include <net/net_mgmt.h>
16 #include <net/wifi.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /* Management part definitions */
23 
24 #define _NET_WIFI_LAYER	NET_MGMT_LAYER_L2
25 #define _NET_WIFI_CODE	0x156
26 #define _NET_WIFI_BASE	(NET_MGMT_IFACE_BIT |			\
27 			 NET_MGMT_LAYER(_NET_WIFI_LAYER) |	\
28 			 NET_MGMT_LAYER_CODE(_NET_WIFI_CODE))
29 #define _NET_WIFI_EVENT	(_NET_WIFI_BASE | NET_MGMT_EVENT_BIT)
30 
31 enum net_request_wifi_cmd {
32 	NET_REQUEST_WIFI_CMD_SCAN = 1,
33 	NET_REQUEST_WIFI_CMD_CONNECT,
34 	NET_REQUEST_WIFI_CMD_DISCONNECT,
35 	NET_REQUEST_WIFI_CMD_AP_ENABLE,
36 	NET_REQUEST_WIFI_CMD_AP_DISABLE,
37 };
38 
39 #define NET_REQUEST_WIFI_SCAN					\
40 	(_NET_WIFI_BASE | NET_REQUEST_WIFI_CMD_SCAN)
41 
42 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_SCAN);
43 
44 #define NET_REQUEST_WIFI_CONNECT				\
45 	(_NET_WIFI_BASE | NET_REQUEST_WIFI_CMD_CONNECT)
46 
47 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_CONNECT);
48 
49 #define NET_REQUEST_WIFI_DISCONNECT				\
50 	(_NET_WIFI_BASE | NET_REQUEST_WIFI_CMD_DISCONNECT)
51 
52 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_DISCONNECT);
53 
54 #define NET_REQUEST_WIFI_AP_ENABLE				\
55 	(_NET_WIFI_BASE | NET_REQUEST_WIFI_CMD_AP_ENABLE)
56 
57 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_AP_ENABLE);
58 
59 #define NET_REQUEST_WIFI_AP_DISABLE				\
60 	(_NET_WIFI_BASE | NET_REQUEST_WIFI_CMD_AP_DISABLE)
61 
62 NET_MGMT_DEFINE_REQUEST_HANDLER(NET_REQUEST_WIFI_AP_DISABLE);
63 
64 enum net_event_wifi_cmd {
65 	NET_EVENT_WIFI_CMD_SCAN_RESULT = 1,
66 	NET_EVENT_WIFI_CMD_SCAN_DONE,
67 	NET_EVENT_WIFI_CMD_CONNECT_RESULT,
68 	NET_EVENT_WIFI_CMD_DISCONNECT_RESULT,
69 };
70 
71 #define NET_EVENT_WIFI_SCAN_RESULT				\
72 	(_NET_WIFI_EVENT | NET_EVENT_WIFI_CMD_SCAN_RESULT)
73 
74 #define NET_EVENT_WIFI_SCAN_DONE				\
75 	(_NET_WIFI_EVENT | NET_EVENT_WIFI_CMD_SCAN_DONE)
76 
77 #define NET_EVENT_WIFI_CONNECT_RESULT				\
78 	(_NET_WIFI_EVENT | NET_EVENT_WIFI_CMD_CONNECT_RESULT)
79 
80 #define NET_EVENT_WIFI_DISCONNECT_RESULT			\
81 	(_NET_WIFI_EVENT | NET_EVENT_WIFI_CMD_DISCONNECT_RESULT)
82 
83 
84 /* Each result is provided to the net_mgmt_event_callback
85  * via its info attribute (see net_mgmt.h)
86  */
87 struct wifi_scan_result {
88 	uint8_t ssid[WIFI_SSID_MAX_LEN];
89 	uint8_t ssid_length;
90 
91 	uint8_t channel;
92 	enum wifi_security_type security;
93 	int8_t rssi;
94 };
95 
96 struct wifi_connect_req_params {
97 	uint8_t *ssid;
98 	uint8_t ssid_length; /* Max 32 */
99 
100 	uint8_t *psk;
101 	uint8_t psk_length; /* Min 8 - Max 64 */
102 
103 	uint8_t channel;
104 	enum wifi_security_type security;
105 };
106 
107 struct wifi_status {
108 	int status;
109 };
110 
111 #include <net/net_if.h>
112 
113 typedef void (*scan_result_cb_t)(struct net_if *iface, int status,
114 				 struct wifi_scan_result *entry);
115 
116 struct net_wifi_mgmt_offload {
117 	/**
118 	 * Mandatory to get in first position.
119 	 * A network device should indeed provide a pointer on such
120 	 * net_if_api structure. So we make current structure pointer
121 	 * that can be casted to a net_if_api structure pointer.
122 	 */
123 	struct net_if_api iface_api;
124 
125 	/* cb parameter is the cb that should be called for each
126 	 * result by the driver. The wifi mgmt part will take care of
127 	 * raising the necessary event etc...
128 	 */
129 	int (*scan)(const struct device *dev, scan_result_cb_t cb);
130 	int (*connect)(const struct device *dev,
131 		       struct wifi_connect_req_params *params);
132 	int (*disconnect)(const struct device *dev);
133 	int (*ap_enable)(const struct device *dev,
134 			 struct wifi_connect_req_params *params);
135 	int (*ap_disable)(const struct device *dev);
136 };
137 
138 /* Make sure that the network interface API is properly setup inside
139  * Wifi mgmt offload API struct (it is the first one).
140  */
141 BUILD_ASSERT(offsetof(struct net_wifi_mgmt_offload, iface_api) == 0);
142 
143 #ifdef CONFIG_WIFI_OFFLOAD
144 
145 void wifi_mgmt_raise_connect_result_event(struct net_if *iface, int status);
146 void wifi_mgmt_raise_disconnect_result_event(struct net_if *iface, int status);
147 
148 #endif /* CONFIG_WIFI_OFFLOAD */
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif /* ZEPHYR_INCLUDE_NET_WIFI_MGMT_H_ */
155