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