Skip to content

Commit 52f9d01

Browse files
committed
esp_rmaker_user_mapping: Create and register user_mapping handlers internally
Earlier, it was the application code's responsibility to call the esp_rmaker_user_mapping_endpoint_create() and esp_rmaker_user_mapping_endpoint_register() APIs at appropriate places in Wi-Fi provisioning code to enable user node mapping functionality. To simplify this, the logic has been moved to RainMaker Core, so that this happens automatically. For some reason, if you want to retain the older logic in your application code and do not want RainMaker_core to enable this, please set the CONFIG_ESP_RMAKER_DISABLE_USER_MAPPING_PROV config option.
1 parent b61dc04 commit 52f9d01

File tree

7 files changed

+70
-32
lines changed

7 files changed

+70
-32
lines changed

components/esp_rainmaker/Kconfig.projbuild

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ menu "ESP RainMaker Config"
109109
help
110110
Default SNTP Server which is used for time synchronization.
111111

112+
config ESP_RMAKER_DISABLE_USER_MAPPING_PROV
113+
bool "Disable User Mapping during Provisioning"
114+
default n
115+
help
116+
The handlers for User Node Mapping are now registered internally by ESP RainMaker core,
117+
by registering to appropriate Wi-Fi Provisioning events. If your application code also
118+
has the calls to create and register the user mapping handlers, enable this config
119+
option to prevent duplication.
120+
112121
choice ESP_RMAKER_CONSOLE_UART_NUM
113122
prompt "UART for console input"
114123
default ESP_RMAKER_CONSOLE_UART_NUM_0

components/esp_rainmaker/include/esp_rmaker_user_mapping.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extern "C"
2929
* @return ESP_OK on success
3030
* @return error on failure
3131
*/
32-
esp_err_t esp_rmaker_user_mapping_endpoint_create();
32+
esp_err_t esp_rmaker_user_mapping_endpoint_create(void);
3333

3434
/**
3535
* Register User Mapping Endpoint
@@ -41,7 +41,7 @@ esp_err_t esp_rmaker_user_mapping_endpoint_create();
4141
* @return ESP_OK on success
4242
* @return error on failure
4343
*/
44-
esp_err_t esp_rmaker_user_mapping_endpoint_register();
44+
esp_err_t esp_rmaker_user_mapping_endpoint_register(void);
4545

4646
/** Add User-Node mapping
4747
*

components/esp_rainmaker/src/core/esp_rmaker_core.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ static esp_err_t esp_rmaker_deinit_priv_data(esp_rmaker_priv_data_t *rmaker_priv
144144
if (rmaker_priv_data->work_queue) {
145145
vQueueDelete(rmaker_priv_data->work_queue);
146146
}
147+
#ifndef CONFIG_ESP_RMAKER_DISABLE_USER_MAPPING_PROV
148+
esp_rmaker_user_mapping_prov_deinit();
149+
#endif
147150
#ifdef ESP_RMAKER_CLAIM_ENABLED
148151
if (rmaker_priv_data->claim_data) {
149152
esp_rmaker_claim_data_free(rmaker_priv_data->claim_data);
@@ -221,6 +224,14 @@ static esp_err_t esp_rmaker_init(const esp_rmaker_config_t *config)
221224
ESP_LOGE(TAG, "ESP RainMaker Queue Creation Failed");
222225
return ESP_ERR_NO_MEM;
223226
}
227+
#ifndef CONFIG_ESP_RMAKER_DISABLE_USER_MAPPING_PROV
228+
if (esp_rmaker_user_mapping_prov_init()) {
229+
esp_rmaker_deinit_priv_data(esp_rmaker_priv_data);
230+
esp_rmaker_priv_data = NULL;
231+
ESP_LOGE(TAG, "Could not initialise User-Node mapping.");
232+
return ESP_FAIL;
233+
}
234+
#endif /* !CONFIG_ESP_RMAKER_DISABLE_USER_MAPPING_PROV */
224235
esp_rmaker_priv_data->mqtt_config = esp_rmaker_get_mqtt_config();
225236
if (!esp_rmaker_priv_data->mqtt_config) {
226237
#ifdef ESP_RMAKER_CLAIM_ENABLED

components/esp_rainmaker/src/core/esp_rmaker_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ esp_err_t esp_rmaker_attribute_delete(esp_rmaker_attr_t *attr);
9797
char *esp_rmaker_get_node_config(void);
9898
char *esp_rmaker_get_node_params(void);
9999
esp_err_t esp_rmaker_handle_set_params(char *data, size_t data_len, esp_rmaker_req_src_t src);
100-
100+
esp_err_t esp_rmaker_user_mapping_prov_init(void);
101+
esp_err_t esp_rmaker_user_mapping_prov_deinit(void);
101102
static inline esp_err_t esp_rmaker_post_event(esp_rmaker_event_t event_id, void* data, size_t data_size)
102103
{
103104
return esp_event_post(RMAKER_EVENT, event_id, data, data_size, portMAX_DELAY);

components/esp_rainmaker/src/core/esp_rmaker_user_mapping.c

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414

1515
#include <string.h>
1616
#include <esp_log.h>
17+
#include <esp_event.h>
1718
#include <wifi_provisioning/manager.h>
1819
#include <json_generator.h>
1920
#include <esp_rmaker_core.h>
21+
#include <esp_rmaker_user_mapping.h>
2022
#include <esp_rmaker_mqtt.h>
21-
2223
#include "esp_rmaker_user_mapping.pb-c.h"
24+
#include "esp_rmaker_internal.h"
2325

2426
static const char *TAG = "esp_rmaker_user_mapping";
2527

@@ -44,6 +46,28 @@ static void esp_rmaker_user_mapping_cleanup_data(esp_rmaker_user_mapping_data_t
4446
}
4547
}
4648

49+
static void esp_rmaker_user_mapping_event_handler(void* arg, esp_event_base_t event_base,
50+
int event_id, void* event_data)
51+
{
52+
if (event_base == WIFI_PROV_EVENT) {
53+
switch (event_id) {
54+
case WIFI_PROV_INIT: {
55+
if (esp_rmaker_user_mapping_endpoint_create() != ESP_OK) {
56+
ESP_LOGE(TAG, "Failed to create user mapping end point.");
57+
}
58+
break;
59+
}
60+
case WIFI_PROV_START:
61+
if (esp_rmaker_user_mapping_endpoint_register() != ESP_OK) {
62+
ESP_LOGE(TAG, "Failed to register user mapping end point.");
63+
}
64+
break;
65+
default:
66+
break;
67+
}
68+
}
69+
}
70+
4771
static void esp_rmaker_user_mapping_cb(void *priv_data)
4872
{
4973
esp_rmaker_user_mapping_data_t *data = (esp_rmaker_user_mapping_data_t *)priv_data;
@@ -86,6 +110,7 @@ esp_err_t esp_rmaker_start_user_node_mapping(char *user_id, char *secret_key)
86110
if (esp_rmaker_queue_work(esp_rmaker_user_mapping_cb, data) != ESP_OK) {
87111
goto user_mapping_error;
88112
}
113+
esp_rmaker_user_mapping_prov_deinit();
89114
return ESP_OK;
90115

91116
user_mapping_error:
@@ -141,13 +166,31 @@ int esp_rmaker_user_mapping_handler(uint32_t session_id, const uint8_t *inbuf, s
141166
rainmaker__rmaker_config_payload__free_unpacked(data, NULL);
142167
return ESP_OK;
143168
}
144-
esp_err_t esp_rmaker_user_mapping_endpoint_create()
169+
esp_err_t esp_rmaker_user_mapping_endpoint_create(void)
145170
{
146171
esp_err_t err = wifi_prov_mgr_endpoint_create(USER_MAPPING_ENDPOINT);
147172
return err;
148173
}
149174

150-
esp_err_t esp_rmaker_user_mapping_endpoint_register()
175+
esp_err_t esp_rmaker_user_mapping_endpoint_register(void)
151176
{
152177
return wifi_prov_mgr_endpoint_register(USER_MAPPING_ENDPOINT, esp_rmaker_user_mapping_handler, NULL);
153178
}
179+
180+
esp_err_t esp_rmaker_user_mapping_prov_init(void)
181+
{
182+
int ret = ESP_OK;
183+
ret = esp_event_handler_register(WIFI_PROV_EVENT, WIFI_PROV_INIT, &esp_rmaker_user_mapping_event_handler, NULL);
184+
if (ret != ESP_OK) {
185+
return ret;
186+
}
187+
ret = esp_event_handler_register(WIFI_PROV_EVENT, WIFI_PROV_START, &esp_rmaker_user_mapping_event_handler, NULL);
188+
return ret;
189+
}
190+
191+
esp_err_t esp_rmaker_user_mapping_prov_deinit(void)
192+
{
193+
esp_event_handler_unregister(WIFI_PROV_EVENT, WIFI_PROV_INIT, &esp_rmaker_user_mapping_event_handler);
194+
esp_event_handler_unregister(WIFI_PROV_EVENT, WIFI_PROV_START, &esp_rmaker_user_mapping_event_handler);
195+
return ESP_OK;
196+
}

examples/common/app_wifi/app_wifi.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <wifi_provisioning/scheme_softap.h>
3232
#endif /* CONFIG_APP_WIFI_PROV_TRANSPORT_BLE */
3333

34-
#include <esp_rmaker_user_mapping.h>
3534
#include <qrcode.h>
3635
#include <nvs.h>
3736
#include <nvs_flash.h>
@@ -314,20 +313,8 @@ esp_err_t app_wifi_start(app_wifi_pop_type_t pop_type)
314313
}
315314
#endif /* CONFIG_APP_WIFI_PROV_TRANSPORT_BLE */
316315

317-
/* Create endpoint for ESP Cloud User-Device Association */
318-
err = esp_rmaker_user_mapping_endpoint_create();
319-
if (err != ESP_OK) {
320-
ESP_LOGE(TAG, "esp_rmaker_user_mapping_endpoint_create failed %d", err);
321-
return err;
322-
}
323316
/* Start provisioning service */
324317
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));
325-
/* Register endpoint for ESP Cloud User-Device Association */
326-
err = esp_rmaker_user_mapping_endpoint_register();
327-
if (err != ESP_OK) {
328-
ESP_LOGE(TAG, "esp_rmaker_user_mapping_endpoint_register failed %d", err);
329-
return err;
330-
}
331318
/* Print QR code for provisioning */
332319
#ifdef CONFIG_APP_WIFI_PROV_TRANSPORT_BLE
333320
app_wifi_print_qr(service_name, pop, PROV_TRANSPORT_BLE);

examples/homekit_switch/main/app_wifi_with_homekit.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <hap_platform_httpd.h>
3333
#endif /* CONFIG_APP_WIFI_PROV_TRANSPORT_BLE */
3434

35-
#include <esp_rmaker_user_mapping.h>
3635
#include <qrcode.h>
3736
#include <nvs.h>
3837
#include <nvs_flash.h>
@@ -318,20 +317,8 @@ esp_err_t app_wifi_with_homekit_start(app_wifi_pop_type_t pop_type)
318317
wifi_prov_scheme_softap_set_httpd_handle(hap_platform_httpd_get_handle());
319318
#endif /* CONFIG_APP_WIFI_PROV_TRANSPORT_SOFTAP */
320319

321-
/* Create endpoint for ESP Cloud User-Device Association */
322-
err = esp_rmaker_user_mapping_endpoint_create();
323-
if (err != ESP_OK) {
324-
ESP_LOGE(TAG, "esp_rmaker_user_mapping_endpoint_create failed %d", err);
325-
return err;
326-
}
327320
/* Start provisioning service */
328321
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));
329-
/* Register endpoint for ESP Cloud User-Device Association */
330-
err = esp_rmaker_user_mapping_endpoint_register();
331-
if (err != ESP_OK) {
332-
ESP_LOGE(TAG, "esp_rmaker_user_mapping_endpoint_register failed %d", err);
333-
return err;
334-
}
335322
/* Print QR code for provisioning */
336323
#ifdef CONFIG_APP_WIFI_PROV_TRANSPORT_BLE
337324
app_wifi_print_qr(service_name, pop, PROV_TRANSPORT_BLE);

0 commit comments

Comments
 (0)