Skip to content

Commit d3a974f

Browse files
committed
feat(smartconfig): refactor smartconfig callback to use esp event
Consistent with esp-idf(branch: release/v4.2, commit id: c5bb6c4)
1 parent dda5062 commit d3a974f

File tree

13 files changed

+339
-296
lines changed

13 files changed

+339
-296
lines changed

components/esp8266/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ else()
3232
"source/crc.c"
3333
"source/phy_init.c"
3434
"source/reset_reason.c"
35+
"source/smartconfig.c"
36+
"source/smartconfig_ack.c"
3537
"source/startup.c"
3638
"source/system_api.c"
3739
"source/task_wdt.c"

components/esp8266/include/esp_private/wifi.h

+42-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "esp_wifi_types.h"
1919
#include "esp_event.h"
2020
#include "esp_wifi.h"
21+
#include "esp_smartconfig.h"
2122

2223
#ifdef __cplusplus
2324
extern "C" {
@@ -39,7 +40,7 @@ typedef enum {
3940
WIFI_LOG_DEBUG, /*can be set in menuconfig*/
4041
WIFI_LOG_VERBOSE, /*can be set in menuconfig*/
4142
} wifi_log_level_t;
42-
43+
4344
/**
4445
* @brief WiFi log submodule definition
4546
*
@@ -81,7 +82,7 @@ typedef enum {
8182
* - ESP_ERR_NO_MEM: out of memory
8283
* - others: refer to error code esp_err.h
8384
*/
84-
esp_err_t esp_wifi_init_internal(const wifi_init_config_t *config);
85+
esp_err_t esp_wifi_init_internal(const wifi_init_config_t* config);
8586

8687
/**
8788
* @brief Deinitialize Wi-Fi Driver
@@ -117,14 +118,14 @@ wifi_rx_pbuf_mem_type_t esp_wifi_get_rx_pbuf_mem_type(void);
117118
int8_t esp_wifi_get_ap_rssi(void);
118119

119120
/**
120-
* @brief The RX callback function when receive probe request packet.
121+
* @brief The RX callback function when receive probe request packet.
121122
* When probe request packet is received, the callback function will be called.
122123
*
123124
* @param frame Data of received probe request.
124125
* @param len length of received probe request.
125126
* @param rssi rssi of received probe request.
126127
*/
127-
typedef void (*wifi_sta_rx_probe_req_t)(const uint8_t *frame, int len, int rssi);
128+
typedef void (*wifi_sta_rx_probe_req_t)(const uint8_t* frame, int len, int rssi);
128129

129130
/**
130131
* @brief Register the RX callback function when receive probe request.
@@ -159,14 +160,14 @@ void esp_wifi_internal_free_rx_buffer(void* buffer);
159160
* - ERR_IF : WiFi driver error
160161
* - ERR_ARG : Invalid argument
161162
*/
162-
int esp_wifi_internal_tx(wifi_interface_t wifi_if, void *buffer, uint16_t len);
163+
int esp_wifi_internal_tx(wifi_interface_t wifi_if, void* buffer, uint16_t len);
163164

164165
/**
165166
* @brief The WiFi RX callback function
166167
*
167168
* Each time the WiFi need to forward the packets to high layer, the callback function will be called
168169
*/
169-
typedef esp_err_t (*wifi_rxcb_t)(void *buffer, uint16_t len, void *eb);
170+
typedef esp_err_t (*wifi_rxcb_t)(void* buffer, uint16_t len, void* eb);
170171

171172
/**
172173
* @brief Set the WiFi RX callback
@@ -183,7 +184,7 @@ typedef esp_err_t (*wifi_rxcb_t)(void *buffer, uint16_t len, void *eb);
183184
esp_err_t esp_wifi_internal_reg_rxcb(wifi_interface_t ifx, wifi_rxcb_t fn);
184185

185186
/**
186-
* @brief Set current WiFi log level
187+
* @brief Set current WiFi log level
187188
*
188189
* @param level Log level.
189190
*
@@ -207,29 +208,57 @@ esp_err_t esp_wifi_internal_set_log_level(wifi_log_level_t level);
207208
esp_err_t esp_wifi_internal_set_log_mod(uint32_t submodule);
208209

209210
/**
210-
* @brief Get current WiFi log info
211+
* @brief Get current WiFi log info
211212
*
212213
* @param log_level the return log level.
213214
* @param log_mod the return log module and submodule
214215
*
215216
* @return
216217
* - ESP_OK: succeed
217218
*/
218-
esp_err_t esp_wifi_internal_get_log(wifi_log_level_t *log_level, uint32_t *log_mod);
219+
esp_err_t esp_wifi_internal_get_log(wifi_log_level_t* log_level, uint32_t* log_mod);
219220

220221
/**
221222
* @brief get wifi power management config.
222-
*
223+
*
223224
* @param ps_config power management config
224225
*/
225-
void esp_wifi_set_pm_config(esp_pm_config_t *pm_config);
226+
void esp_wifi_set_pm_config(esp_pm_config_t* pm_config);
226227

227228
/**
228229
* @brief set wifi power management config.
229-
*
230+
*
230231
* @param ps_config power management config
231232
*/
232-
void esp_wifi_get_pm_config(esp_pm_config_t *pm_config);
233+
void esp_wifi_get_pm_config(esp_pm_config_t* pm_config);
234+
235+
/**
236+
* @brief Start SmartConfig, config ESP device to connect AP. You need to broadcast information by phone APP.
237+
* Device sniffer special packets from the air that containing SSID and password of target AP.
238+
*
239+
* @attention 1. This API can be called in station or softAP-station mode.
240+
* @attention 2. Can not call esp_smartconfig_start twice before it finish, please call
241+
* esp_smartconfig_stop first.
242+
*
243+
* @param config pointer to smartconfig start configure structure
244+
*
245+
* @return
246+
* - ESP_OK: succeed
247+
* - others: fail
248+
*/
249+
esp_err_t esp_smartconfig_internal_start(const smartconfig_start_config_t* config);
250+
251+
/**
252+
* @brief Stop SmartConfig, free the buffer taken by esp_smartconfig_start.
253+
*
254+
* @attention Whether connect to AP succeed or not, this API should be called to free
255+
* memory taken by smartconfig_start.
256+
*
257+
* @return
258+
* - ESP_OK: succeed
259+
* - others: fail
260+
*/
261+
esp_err_t esp_smartconfig_internal_stop(void);
233262

234263
#ifdef __cplusplus
235264
}

components/esp8266/include/esp_smartconfig.h

+35-25
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,72 @@
1818
#include <stdint.h>
1919
#include <stdbool.h>
2020
#include "esp_err.h"
21+
#include "esp_event_base.h"
2122

2223
#ifdef __cplusplus
2324
extern "C" {
2425
#endif
2526

26-
typedef enum {
27-
SC_STATUS_WAIT = 0, /**< Waiting to start connect */
28-
SC_STATUS_FIND_CHANNEL, /**< Finding target channel */
29-
SC_STATUS_GETTING_SSID_PSWD, /**< Getting SSID and password of target AP */
30-
SC_STATUS_LINK, /**< Connecting to target AP */
31-
SC_STATUS_LINK_OVER, /**< Connected to AP successfully */
32-
} smartconfig_status_t;
33-
3427
typedef enum {
3528
SC_TYPE_ESPTOUCH = 0, /**< protocol: ESPTouch */
3629
SC_TYPE_AIRKISS, /**< protocol: AirKiss */
3730
SC_TYPE_ESPTOUCH_AIRKISS, /**< protocol: ESPTouch and AirKiss */
3831
} smartconfig_type_t;
3932

40-
/**
41-
* @brief The callback of SmartConfig, executed when smart-config status changed.
42-
*
43-
* @param status Status of SmartConfig:
44-
* - SC_STATUS_GETTING_SSID_PSWD : pdata is a pointer of smartconfig_type_t, means config type.
45-
* - SC_STATUS_LINK : pdata is a pointer of struct station_config.
46-
* - SC_STATUS_LINK_OVER : pdata is a pointer of phone's IP address(4 bytes) if pdata unequal NULL.
47-
* - otherwise : parameter void *pdata is NULL.
48-
* @param pdata According to the different status have different values.
49-
*
50-
*/
51-
typedef void (*sc_callback_t)(smartconfig_status_t status, void *pdata);
33+
/** Smartconfig event declarations */
34+
typedef enum {
35+
SC_EVENT_SCAN_DONE, /*!< ESP32 station smartconfig has finished to scan for APs */
36+
SC_EVENT_FOUND_CHANNEL, /*!< ESP32 station smartconfig has found the channel of the target AP */
37+
SC_EVENT_GOT_SSID_PSWD, /*!< ESP32 station smartconfig got the SSID and password */
38+
SC_EVENT_SEND_ACK_DONE, /*!< ESP32 station smartconfig has sent ACK to cellphone */
39+
} smartconfig_event_t;
40+
41+
/** @brief smartconfig event base declaration */
42+
ESP_EVENT_DECLARE_BASE(SC_EVENT);
43+
44+
/** Argument structure for SC_EVENT_GOT_SSID_PSWD event */
45+
typedef struct {
46+
uint8_t ssid[32]; /**< SSID of the AP. Null terminated string. */
47+
uint8_t password[64]; /**< Password of the AP. Null terminated string. */
48+
bool bssid_set; /**< whether set MAC address of target AP or not. */
49+
uint8_t bssid[6]; /**< MAC address of target AP. */
50+
smartconfig_type_t type; /**< Type of smartconfig(ESPTouch or AirKiss). */
51+
uint8_t token; /**< Token from cellphone which is used to send ACK to cellphone. */
52+
uint8_t cellphone_ip[4]; /**< IP address of cellphone. */
53+
} smartconfig_event_got_ssid_pswd_t;
54+
55+
/** Configure structure for esp_smartconfig_start */
56+
typedef struct {
57+
bool enable_log; /**< Enable smartconfig logs. */
58+
} smartconfig_start_config_t;
59+
60+
#define SMARTCONFIG_START_CONFIG_DEFAULT() { \
61+
.enable_log = false \
62+
};
5263

5364
/**
5465
* @brief Get the version of SmartConfig.
5566
*
5667
* @return
5768
* - SmartConfig version const char.
5869
*/
59-
const char *esp_smartconfig_get_version(void);
70+
const char* esp_smartconfig_get_version(void);
6071

6172
/**
6273
* @brief Start SmartConfig, config ESP device to connect AP. You need to broadcast information by phone APP.
6374
* Device sniffer special packets from the air that containing SSID and password of target AP.
6475
*
65-
* @attention 1. This API can be called in station or softAP-station mode.
76+
* @attention 1. This API can be called in station mode.
6677
* @attention 2. Can not call esp_smartconfig_start twice before it finish, please call
6778
* esp_smartconfig_stop first.
6879
*
69-
* @param cb SmartConfig callback function.
70-
* @param ... log 1: UART output logs; 0: UART only outputs the result.
80+
* @param config pointer to smartconfig start configure structure
7181
*
7282
* @return
7383
* - ESP_OK: succeed
7484
* - others: fail
7585
*/
76-
esp_err_t esp_smartconfig_start(sc_callback_t cb, ...);
86+
esp_err_t esp_smartconfig_start(const smartconfig_start_config_t* config);
7787

7888
/**
7989
* @brief Stop SmartConfig, free the buffer taken by esp_smartconfig_start.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2018 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 SMARTCONFIG_ACK_H
16+
#define SMARTCONFIG_ACK_H
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
/**
23+
* @brief Send smartconfig ACK to cellphone.
24+
*
25+
* @attention The API can only be used when receiving SC_EVENT_GOT_SSID_PSWD event.
26+
*
27+
* @param type: smartconfig type(ESPTouch or AirKiss);
28+
* token: token from the cellphone;
29+
* cellphone_ip: IP address of the cellphone;
30+
*
31+
* @retuen ESP_OK: succeed
32+
* others: fail
33+
*/
34+
esp_err_t sc_send_ack_start(smartconfig_type_t type, uint8_t token, uint8_t* cellphone_ip);
35+
36+
/**
37+
* @brief Stop sending smartconfig ACK to cellphone.
38+
*/
39+
void sc_send_ack_stop(void);
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif
44+
#endif

components/esp8266/lib/VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ gwen:
44
pp: 4a1ab49
55
espnow: a4246a0
66

7-
smartconfig: 2.8.2
7+
smartconfig: 2.9.0
88
phy: 1163.0
-115 KB
Binary file not shown.
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2019 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+
#include <stdint.h>
16+
#include <string.h>
17+
#include "esp_log.h"
18+
#include "esp_event_base.h"
19+
#include "esp_private/wifi.h"
20+
#include "esp_smartconfig.h"
21+
#include "smartconfig_ack.h"
22+
23+
/* Smartconfig events definitions */
24+
ESP_EVENT_DEFINE_BASE(SC_EVENT);
25+
26+
static const char* TAG = "smartconfig";
27+
28+
static void handler_got_ssid_passwd(void* arg, esp_event_base_t base, int32_t event_id, void* data)
29+
{
30+
smartconfig_event_got_ssid_pswd_t* evt = (smartconfig_event_got_ssid_pswd_t*)data;
31+
uint8_t ssid[33] = { 0 };
32+
uint8_t password[65] = { 0 };
33+
uint8_t cellphone_ip[4];
34+
esp_err_t err = ESP_OK;
35+
36+
memcpy(ssid, evt->ssid, sizeof(evt->ssid));
37+
memcpy(password, evt->password, sizeof(evt->password));
38+
memcpy(cellphone_ip, evt->cellphone_ip, sizeof(evt->cellphone_ip));
39+
40+
ESP_LOGD(TAG, "SSID:%s", ssid);
41+
ESP_LOGD(TAG, "PASSWORD:%s", password);
42+
ESP_LOGD(TAG, "Phone ip: %d.%d.%d.%d\n", cellphone_ip[0], cellphone_ip[1], cellphone_ip[2], cellphone_ip[3]);
43+
44+
err = sc_send_ack_start(evt->type, evt->token, evt->cellphone_ip);
45+
46+
if (err != ESP_OK) {
47+
ESP_LOGE(TAG, "Send smartconfig ACK error: %d", err);
48+
}
49+
}
50+
51+
esp_err_t esp_smartconfig_start(const smartconfig_start_config_t* config)
52+
{
53+
esp_err_t err = ESP_OK;
54+
55+
err = esp_event_handler_register(SC_EVENT, SC_EVENT_GOT_SSID_PSWD, handler_got_ssid_passwd, NULL);
56+
57+
if (err != ESP_OK) {
58+
ESP_LOGE(TAG, "Register smartconfig default event handler fail!");
59+
return err;
60+
}
61+
62+
err = esp_smartconfig_internal_start(config);
63+
64+
if (err != ESP_OK) {
65+
esp_event_handler_unregister(SC_EVENT, SC_EVENT_GOT_SSID_PSWD, handler_got_ssid_passwd);
66+
}
67+
68+
return err;
69+
}
70+
71+
esp_err_t esp_smartconfig_stop(void)
72+
{
73+
esp_err_t err = ESP_OK;
74+
75+
err = esp_smartconfig_internal_stop();
76+
77+
if (err == ESP_OK) {
78+
sc_send_ack_stop();
79+
esp_event_handler_unregister(SC_EVENT, SC_EVENT_GOT_SSID_PSWD, handler_got_ssid_passwd);
80+
}
81+
82+
return err;
83+
}

0 commit comments

Comments
 (0)