1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __ESP_SMARTCONFIG_H__
8 #define __ESP_SMARTCONFIG_H__
9 
10 #include <stdint.h>
11 #include <stdbool.h>
12 #include "esp_err.h"
13 #include "esp_event_base.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 typedef enum {
20     SC_TYPE_ESPTOUCH = 0,       /**< protocol: ESPTouch */
21     SC_TYPE_AIRKISS,            /**< protocol: AirKiss */
22     SC_TYPE_ESPTOUCH_AIRKISS,   /**< protocol: ESPTouch and AirKiss */
23     SC_TYPE_ESPTOUCH_V2,        /**< protocol: ESPTouch v2*/
24 } smartconfig_type_t;
25 
26 /** Smartconfig event declarations */
27 typedef enum {
28     SC_EVENT_SCAN_DONE,                /*!< Station smartconfig has finished to scan for APs */
29     SC_EVENT_FOUND_CHANNEL,            /*!< Station smartconfig has found the channel of the target AP */
30     SC_EVENT_GOT_SSID_PSWD,            /*!< Station smartconfig got the SSID and password */
31     SC_EVENT_SEND_ACK_DONE,            /*!< Station smartconfig has sent ACK to cellphone */
32 } smartconfig_event_t;
33 
34 /** @brief smartconfig event base declaration */
35 ESP_EVENT_DECLARE_BASE(SC_EVENT);
36 
37 /** Argument structure for SC_EVENT_GOT_SSID_PSWD event */
38 typedef struct {
39     uint8_t ssid[32];           /**< SSID of the AP. Null terminated string. */
40     uint8_t password[64];       /**< Password of the AP. Null terminated string. */
41     bool bssid_set;             /**< whether set MAC address of target AP or not. */
42     uint8_t bssid[6];           /**< MAC address of target AP. */
43     smartconfig_type_t type;    /**< Type of smartconfig(ESPTouch or AirKiss). */
44     uint8_t token;              /**< Token from cellphone which is used to send ACK to cellphone. */
45     uint8_t cellphone_ip[4];    /**< IP address of cellphone. */
46 } smartconfig_event_got_ssid_pswd_t;
47 
48 /** Configure structure for esp_smartconfig_start */
49 typedef struct {
50     bool enable_log;                /**< Enable smartconfig logs. */
51     bool esp_touch_v2_enable_crypt; /**< Enable ESPTouch v2 crypt. */
52     char *esp_touch_v2_key;         /**< ESPTouch v2 crypt key, len should be 16. */
53 } smartconfig_start_config_t;
54 
55 #define SMARTCONFIG_START_CONFIG_DEFAULT() { \
56     .enable_log = false, \
57     .esp_touch_v2_enable_crypt = false,\
58     .esp_touch_v2_key = NULL \
59 }
60 
61 /**
62   * @brief  Get the version of SmartConfig.
63   *
64   * @return
65   *     - SmartConfig version const char.
66   */
67 const char *esp_smartconfig_get_version(void);
68 
69 /**
70   * @brief     Start SmartConfig, config ESP device to connect AP. You need to broadcast information by phone APP.
71   *            Device sniffer special packets from the air that containing SSID and password of target AP.
72   *
73   * @attention 1. This API can be called in station or softAP-station mode.
74   * @attention 2. Can not call esp_smartconfig_start twice before it finish, please call
75   *               esp_smartconfig_stop first.
76   *
77   * @param     config pointer to smartconfig start configure structure
78   *
79   * @return
80   *     - ESP_OK: succeed
81   *     - others: fail
82   */
83 esp_err_t esp_smartconfig_start(const smartconfig_start_config_t *config);
84 
85 /**
86   * @brief     Stop SmartConfig, free the buffer taken by esp_smartconfig_start.
87   *
88   * @attention Whether connect to AP succeed or not, this API should be called to free
89   *            memory taken by smartconfig_start.
90   *
91   * @return
92   *     - ESP_OK: succeed
93   *     - others: fail
94   */
95 esp_err_t esp_smartconfig_stop(void);
96 
97 /**
98   * @brief     Set timeout of SmartConfig process.
99   *
100   * @attention Timing starts from SC_STATUS_FIND_CHANNEL status. SmartConfig will restart if timeout.
101   *
102   * @param     time_s  range 15s~255s, offset:45s.
103   *
104   * @return
105   *     - ESP_OK: succeed
106   *     - others: fail
107   */
108 esp_err_t esp_esptouch_set_timeout(uint8_t time_s);
109 
110 /**
111   * @brief     Set protocol type of SmartConfig.
112   *
113   * @attention If users need to set the SmartConfig type, please set it before calling
114   *            esp_smartconfig_start.
115   *
116   * @param     type  Choose from the smartconfig_type_t.
117   *
118   * @return
119   *     - ESP_OK: succeed
120   *     - others: fail
121   */
122 esp_err_t esp_smartconfig_set_type(smartconfig_type_t type);
123 
124 /**
125   * @brief     Set mode of SmartConfig. default normal mode.
126   *
127   * @attention 1. Please call it before API esp_smartconfig_start.
128   * @attention 2. Fast mode have corresponding APP(phone).
129   * @attention 3. Two mode is compatible.
130   *
131   * @param     enable  false-disable(default); true-enable;
132   *
133   * @return
134   *     - ESP_OK: succeed
135   *     - others: fail
136   */
137 esp_err_t esp_smartconfig_fast_mode(bool enable);
138 
139 /**
140   * @brief     Get reserved data of ESPTouch v2.
141   *
142   * @param     rvd_data  reserved  data
143   * @param     len length  of  reserved data
144   *
145   * @return
146   *     - ESP_OK: succeed
147   *     - others: fail
148   */
149 esp_err_t esp_smartconfig_get_rvd_data(uint8_t *rvd_data, uint8_t len);
150 
151 #ifdef __cplusplus
152 }
153 #endif
154 
155 #endif
156