1 /*
2  * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __ESP_WPS_H__
8 #define __ESP_WPS_H__
9 
10 #include <stdint.h>
11 #include <stdbool.h>
12 #include "esp_err.h"
13 #include "esp_wifi_crypto_types.h"
14 #include "esp_compiler.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /** \defgroup WiFi_APIs WiFi Related APIs
21   * @brief WiFi APIs
22   */
23 
24 /** @addtogroup WiFi_APIs
25   * @{
26   */
27 
28 /** @addtogroup WPS_APIs
29   * @{
30   */
31 
32 #define ESP_ERR_WIFI_REGISTRAR   (ESP_ERR_WIFI_BASE + 51)  /*!< WPS registrar is not supported */
33 #define ESP_ERR_WIFI_WPS_TYPE    (ESP_ERR_WIFI_BASE + 52)  /*!< WPS type error */
34 #define ESP_ERR_WIFI_WPS_SM      (ESP_ERR_WIFI_BASE + 53)  /*!< WPS state machine is not initialized */
35 
36 /**
37  * @brief Enumeration of WPS (Wi-Fi Protected Setup) types.
38  */
39 typedef enum wps_type {
40     WPS_TYPE_DISABLE = 0,   /**< WPS is disabled */
41     WPS_TYPE_PBC,           /**< WPS Push Button Configuration method */
42     WPS_TYPE_PIN,           /**< WPS PIN (Personal Identification Number) method */
43     WPS_TYPE_MAX            /**< Maximum value for WPS type enumeration */
44 } wps_type_t;
45 
46 #define WPS_MAX_MANUFACTURER_LEN 65  /**< Maximum length of the manufacturer name in WPS information */
47 #define WPS_MAX_MODEL_NUMBER_LEN 33  /**< Maximum length of the model number in WPS information */
48 #define WPS_MAX_MODEL_NAME_LEN   33  /**< Maximum length of the model name in WPS information */
49 #define WPS_MAX_DEVICE_NAME_LEN  33  /**< Maximum length of the device name in WPS information */
50 
51 /**
52  * @brief Structure representing WPS factory information for ESP device.
53  *
54  * This structure holds various strings representing factory information for a device, such as the manufacturer,
55  * model number, model name, and device name. Each string is a null-terminated character array. If any of the
56  * strings are empty, the default values are used.
57  */
58 typedef struct {
59     char manufacturer[WPS_MAX_MANUFACTURER_LEN]; /*!< Manufacturer of the device. If empty, the default manufacturer is used. */
60     char model_number[WPS_MAX_MODEL_NUMBER_LEN]; /*!< Model number of the device. If empty, the default model number is used. */
61     char model_name[WPS_MAX_MODEL_NAME_LEN];     /*!< Model name of the device. If empty, the default model name is used. */
62     char device_name[WPS_MAX_DEVICE_NAME_LEN];   /*!< Device name. If empty, the default device name is used. */
63 } wps_factory_information_t;
64 
65 #define PIN_LEN 9 /*!< The length of the WPS PIN (Personal Identification Number). */
66 /**
67  * @brief Structure representing configuration settings for WPS (Wi-Fi Protected Setup).
68  *
69  * This structure encapsulates various configuration settings for WPS, including the WPS type (PBC or PIN),
70  * factory information that will be shown in the WPS Information Element (IE), and a PIN if the WPS type is
71  * set to PIN.
72  */
73 typedef struct {
74     wps_type_t wps_type;  /*!< The type of WPS to be used (PBC or PIN). */
75     wps_factory_information_t factory_info; /*!< Factory information to be shown in the WPS Information Element (IE). Vendor can choose to display their own information. */
76     char pin[PIN_LEN];   /*!< WPS PIN (Personal Identification Number) used when wps_type is set to WPS_TYPE_PIN. */
77 } esp_wps_config_t;
78 
79 /**
80  * @def WPS_CONFIG_INIT_DEFAULT(type)
81  * @brief Initialize a default WPS configuration structure with specified WPS type.
82  *
83  * This macro initializes a `esp_wps_config_t` structure with default values for the specified WPS type.
84  * It sets the WPS type, factory information (including default manufacturer, model number, model name, and device name),
85  * and a default PIN value if applicable.
86  *
87  * @param type The WPS type to be used (PBC or PIN).
88  * @return An initialized `esp_wps_config_t` structure with the specified WPS type and default values.
89  */
90 #define WPS_CONFIG_INIT_DEFAULT(type) { \
91     .wps_type = type, \
92     .factory_info = {   \
93         ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(manufacturer, "ESPRESSIF")  \
94         ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(model_number, CONFIG_IDF_TARGET)  \
95         ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(model_name, "ESPRESSIF IOT")  \
96         ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(device_name, "ESP DEVICE")  \
97     },  \
98     ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(pin, "00000000") \
99 }
100 
101 /**
102   * @brief     Enable Wi-Fi WPS function.
103   *
104   * @param     config : WPS config to be used in connection
105   *
106   * @return
107   *          - ESP_OK : succeed
108   *          - ESP_ERR_WIFI_WPS_TYPE : wps type is invalid
109   *          - ESP_ERR_WIFI_WPS_MODE : wifi is not in station mode or sniffer mode is on
110   *          - ESP_FAIL : wps initialization fails
111   */
112 esp_err_t esp_wifi_wps_enable(const esp_wps_config_t *config);
113 
114 /**
115   * @brief  Disable Wi-Fi WPS function and release resource it taken.
116   *
117   * @return
118   *          - ESP_OK : succeed
119   *          - ESP_ERR_WIFI_WPS_MODE : wifi is not in station mode or sniffer mode is on
120   */
121 esp_err_t esp_wifi_wps_disable(void);
122 
123 /**
124   * @brief     Start WPS session.
125   *
126   * @attention WPS can only be used when station is enabled. WPS needs to be enabled first for using this API.
127   *
128   * @param     timeout_ms : deprecated: This argument's value will have not effect in functionality of API.
129   *                         The argument will be removed in future.
130   *                         The app should start WPS and register for WIFI events to get the status.
131   *                         WPS status is updated through WPS events. See wifi_event_t enum for more info.
132   *
133   * @return
134   *          - ESP_OK : succeed
135   *          - ESP_ERR_WIFI_WPS_TYPE : wps type is invalid
136   *          - ESP_ERR_WIFI_WPS_MODE : wifi is not in station mode or sniffer mode is on
137   *          - ESP_ERR_WIFI_WPS_SM : wps state machine is not initialized
138   *          - ESP_FAIL : wps initialization fails
139   */
140 esp_err_t esp_wifi_wps_start(int timeout_ms);
141 
142 /**
143   * @brief     Enable Wi-Fi AP WPS function.
144   *
145   * @attention WPS can only be used when softAP is enabled.
146   *
147   * @param     config: wps configuration to be used.
148   *
149   * @return
150   *          - ESP_OK : succeed
151   *          - ESP_ERR_WIFI_WPS_TYPE : wps type is invalid
152   *          - ESP_ERR_WIFI_WPS_MODE : wifi is not in station mode or sniffer mode is on
153   *          - ESP_FAIL : wps initialization fails
154   */
155 esp_err_t esp_wifi_ap_wps_enable(const esp_wps_config_t *config);
156 
157 /**
158   * @brief  Disable Wi-Fi SoftAP WPS function and release resource it taken.
159   *
160   * @return
161   *          - ESP_OK : succeed
162   *          - ESP_ERR_WIFI_WPS_MODE : wifi is not in station mode or sniffer mode is on
163   */
164 esp_err_t esp_wifi_ap_wps_disable(void);
165 
166 /**
167   * @brief     WPS starts to work.
168   *
169   * @attention WPS can only be used when softAP is enabled.
170   *
171   * @param  pin : Pin to be used in case of WPS mode is pin.
172   *               If Pin is not provided, device will use the pin generated/provided
173   *               during esp_wifi_ap_wps_enable() and reported in WIFI_EVENT_AP_WPS_RG_PIN
174   *
175   * @return
176   *          - ESP_OK : succeed
177   *          - ESP_ERR_WIFI_WPS_TYPE : wps type is invalid
178   *          - ESP_ERR_WIFI_WPS_MODE : wifi is not in station mode or sniffer mode is on
179   *          - ESP_ERR_WIFI_WPS_SM : wps state machine is not initialized
180   *          - ESP_FAIL : wps initialization fails
181   */
182 esp_err_t esp_wifi_ap_wps_start(const unsigned char *pin);
183 
184 
185 /**
186   * @}
187   */
188 
189 /**
190   * @}
191   */
192 
193 #ifdef __cplusplus
194 }
195 #endif
196 
197 #endif /* __ESP_WPS_H__ */
198