1 /*
2  * Copyright 2020-2023 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _WPL_H_
9 #define _WPL_H_
10 
11 #include "stdbool.h"
12 
13 #define WPL_WIFI_SSID_LENGTH      32
14 #define WPL_WIFI_PASSWORD_MIN_LEN 8
15 #define WPL_WIFI_PASSWORD_LENGTH  63
16 
17 /* IP Address of Wi-Fi interface in AP (Access Point) mode */
18 #ifndef WPL_WIFI_AP_IP_ADDR
19 #define WPL_WIFI_AP_IP_ADDR "192.168.1.1"
20 #endif /* WPL_WIFI_AP_IP_ADDR */
21 
22 typedef void (*linkLostCb_t)(bool linkState);
23 
24 typedef enum _wpl_ret
25 {
26     WPLRET_SUCCESS,
27     WPLRET_FAIL,
28     WPLRET_NOT_FOUND,
29     WPLRET_AUTH_FAILED,
30     WPLRET_ADDR_FAILED,
31     WPLRET_NOT_CONNECTED,
32     WPLRET_NOT_READY,
33     WPLRET_TIMEOUT,
34     WPLRET_BAD_PARAM,
35 } wpl_ret_t;
36 
37 typedef enum _wpl_security
38 {
39     /* Used when the user only knows SSID and password. This option should be used
40      * for WPA2 security and lower. */
41     WPL_SECURITY_WILDCARD,
42     /* Use WPA3 SAE security */
43     WPL_SECURITY_WPA3_SAE,
44 } wpl_security_t;
45 
46 /**
47  * @brief  Initialize Wi-Fi driver and WPL layer.
48  *         Create an internal Event structure used for WPL layer (blocking API).
49  *         Set WPL layer state to WPL_INITIALIZED.
50  *         Should be the first function called from the WPL layer API.
51  *
52  * @return WPLRET_SUCCESS Wi-Fi driver and WPL layer initialized successfully.
53  */
54 wpl_ret_t WPL_Init(void);
55 
56 /**
57  * @brief  Start Wi-Fi driver and register an application link state callback.
58  *         Set WPL layer state to WPL_STARTED.
59  *         WPL_Start should be called only after WPL_Init was successfully performed.
60  *
61  * @param  callbackFunction Function which will be called from WPL layer in order to
62  *                          notify upper application that Wi-Fi link is lost or re/established.
63  *
64  * @return WPLRET_SUCCESS Wi-Fi driver started successfully.
65  */
66 wpl_ret_t WPL_Start(linkLostCb_t callbackFunction);
67 
68 /**
69  * @brief  Stop Wi-Fi driver.
70  *         Set WPL layer state to WPL_NOT_INITIALIZED.
71  *         WPL_Stop should be called only after WPL_Start was successfully performed.
72  *
73  * @return WPLRET_SUCCESS Wi-Fi driver is stopped.
74  */
75 wpl_ret_t WPL_Stop(void);
76 
77 /**
78  * @brief  Create an AP (Access Point) network profile and start Wi-Fi AP interface based on this profile.
79  *         If AP mode is started successfully, start a DHCP server.
80  *         If everything goes well, other devices can connect to this network.
81  *         If anything goes wrong, provided network profile is deleted, AP and DHCP are stopped.
82  *         WPL_Start_AP fails if AP interface is already up.
83  *         WPL_Start_AP should be called only after WPL_Start was successfully performed.
84  *
85  * @param  ssid Name of the AP network to be created.
86  * @param  password Password of the AP network to be created.
87  * @param  chan Channel of the AP network to be created.
88  *
89  * @return WPLRET_SUCCESS Network profile created, Wi-Fi AP interface up, DHCP server running.
90  */
91 wpl_ret_t WPL_Start_AP(char *ssid, char *password, int chan);
92 
93 /**
94  * @brief  Stop DHCP server, stop Wi-Fi AP (Access Point) interface and delete AP network profile.
95  *         WPL_Stop_AP should be called only after WPL_Start_AP was successfully performed.
96  *
97  * @return WPLRET_SUCCESS DHCP server stopped, AP interface down, AP network profile deleted.
98  */
99 wpl_ret_t WPL_Stop_AP(void);
100 
101 /**
102  * @brief  Scan for nearby Wi-Fi networks.
103            Print available networks information and store them in an internal buffer in JSON fomrat.
104            The returned buffer is dynamically allocated, caller is responsible for deallocation.
105  *         WPL_Scan should be called only after WPL_Start was successfully performed.
106  *
107  * @return Pointer to buffer with scan results.
108  */
109 char *WPL_Scan(void);
110 
111 /**
112  * @brief  Create and save a new STA (Station) network profile.
113  *         This STA network profile can be used in future (WPL_RemoveNetwork / WPL_Join) calls based on its label.
114  *         WPL_AddNetwork should be called only after WPL_Start was successfully performed.
115  *
116  * @param  ssid Name of the STA network to be created.
117  * @param  password Password of the STA network to be created.
118  * @param  label Alias for the network to be added. A network may be referred by its label.
119  * @param  security Prefered security type. Refer to wpl_security_t for list of options.
120  *
121  * @return WPLRET_SUCCESS New STA network profile was successfully saved.
122  */
123 wpl_ret_t WPL_AddNetworkWithSecurity(char *ssid, char *password, char *label, wpl_security_t security);
124 
125 /**
126  * @brief  Create and save a new STA (Station) network profile.
127  *         This STA network profile can be used in future (WPL_RemoveNetwork / WPL_Join) calls based on its label.
128  *         WPL_AddNetwork should be called only after WPL_Start was successfully performed.
129  *
130  * @param  ssid Name of the STA network to be created.
131  * @param  password Password of the STA network to be created.
132  * @param  label Alias for the network to be added. A network may be referred by its label.
133  *
134  * @return WPLRET_SUCCESS New STA network profile was successfully saved.
135  */
136 wpl_ret_t WPL_AddNetwork(char *ssid, char *password, char *label);
137 
138 /**
139  * @brief  Delete a previously added STA (Station) network profile.
140  *         The profile to be deleted is referred by its label and should have been previously added using
141  * WPL_AddNetwork. WPL_RemoveNetwork should be called only after WPL_AddNetwork was successfully performed (for this
142  * network).
143  *
144  * @param  label Alias for the network to be deleted. Label was set by WPL_AddNetwork.
145  *
146  * @return WPLRET_SUCCESS The profile network is deleted.
147  */
148 wpl_ret_t WPL_RemoveNetwork(char *label);
149 
150 /**
151  * @brief  Connect to a Wi-Fi network.
152  *         Wi-Fi network is chosen by a STA network label.
153  *         The Wi-Fi network to connect to is referred by its label and should have been previously added using
154  * WPL_AddNetwork. WPL_Join should be called only after WPL_AddNetwork was successfully performed (for this network).
155  *
156  * @param  label Alias for the network to connect to. Label was set by WPL_AddNetwork.
157  *
158  * @return WPLRET_SUCCESS Device joined the Wi-Fi network using its STA interface.
159  */
160 wpl_ret_t WPL_Join(char *label);
161 
162 /**
163  * @brief  Disconnect from currently connected Wi-Fi network.
164  *         WPL_Leave should be called only after WPL_Join was successfully performed.
165  *
166  * @return WPLRET_SUCCESS Device left the Wi-Fi network it was connected to.
167  */
168 wpl_ret_t WPL_Leave(void);
169 
170 /**
171  * @brief  Get IP for AP interface or STA interface.
172  *
173  * @param  ip Pointer where IP should be stored.
174  * @param  client 0 for AP, 1 for STA.
175  *
176  * @return WPLRET_SUCCESS if the IP was successfully retrieved.
177  */
178 wpl_ret_t WPL_GetIP(char *ip, int client);
179 
180 #endif /* _WPL_H_ */
181