Skip to content

Commit 3614972

Browse files
committed
Merge branch 'task/claim_separation' into 'master'
esp_mqtt_data: Internal API changes See merge request app-frameworks/esp-rainmaker!188
2 parents d54adcd + 27ea88d commit 3614972

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

Diff for: components/esp_rainmaker/src/core/esp_rmaker_core.c

+39-28
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ typedef struct {
8080

8181
static esp_rmaker_priv_data_t *esp_rmaker_priv_data;
8282

83-
static char *esp_rmaker_populate_node_id()
83+
static char *esp_rmaker_populate_node_id(bool use_claiming)
8484
{
8585
char *node_id = esp_rmaker_storage_get("node_id");
8686
#ifdef ESP_RMAKER_CLAIM_ENABLED
87-
if (!node_id) {
87+
if (!node_id && use_claiming) {
8888
uint8_t eth_mac[6];
8989
esp_err_t err = esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
9090
if (err != ESP_OK) {
@@ -188,8 +188,34 @@ char *esp_rmaker_get_node_id(void)
188188
}
189189
return NULL;
190190
}
191+
192+
static esp_err_t esp_rmaker_mqtt_data_init(esp_rmaker_priv_data_t *rmaker_priv_data, bool use_claiming)
193+
{
194+
rmaker_priv_data->mqtt_config = esp_rmaker_get_mqtt_config();
195+
if (rmaker_priv_data->mqtt_config) {
196+
return ESP_OK;
197+
}
198+
#ifdef ESP_RMAKER_CLAIM_ENABLED
199+
if (use_claiming) {
200+
#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM
201+
rmaker_priv_data->claim_data = esp_rmaker_self_claim_init();
202+
#endif
203+
#ifdef CONFIG_ESP_RMAKER_ASSISTED_CLAIM
204+
rmaker_priv_data->claim_data = esp_rmaker_assisted_claim_init();
205+
#endif
206+
if (!rmaker_priv_data->claim_data) {
207+
ESP_LOGE(TAG, "Failed to initialise Claiming.");
208+
return ESP_FAIL;
209+
} else {
210+
rmaker_priv_data->need_claim = true;
211+
return ESP_OK;
212+
}
213+
}
214+
#endif /* ESP_RMAKER_CLAIM_ENABLED */
215+
return ESP_FAIL;
216+
}
191217
/* Initialize ESP RainMaker */
192-
static esp_err_t esp_rmaker_init(const esp_rmaker_config_t *config)
218+
static esp_err_t esp_rmaker_init(const esp_rmaker_config_t *config, bool use_claiming)
193219
{
194220
if (esp_rmaker_priv_data) {
195221
ESP_LOGE(TAG, "ESP RainMaker already initialised");
@@ -209,7 +235,7 @@ static esp_err_t esp_rmaker_init(const esp_rmaker_config_t *config)
209235
return ESP_ERR_NO_MEM;
210236
}
211237

212-
esp_rmaker_priv_data->node_id = esp_rmaker_populate_node_id();
238+
esp_rmaker_priv_data->node_id = esp_rmaker_populate_node_id(use_claiming);
213239
if (!esp_rmaker_priv_data->node_id) {
214240
esp_rmaker_deinit_priv_data(esp_rmaker_priv_data);
215241
esp_rmaker_priv_data = NULL;
@@ -232,34 +258,19 @@ static esp_err_t esp_rmaker_init(const esp_rmaker_config_t *config)
232258
return ESP_FAIL;
233259
}
234260
#endif /* !CONFIG_ESP_RMAKER_DISABLE_USER_MAPPING_PROV */
235-
esp_rmaker_priv_data->mqtt_config = esp_rmaker_get_mqtt_config();
236-
if (!esp_rmaker_priv_data->mqtt_config) {
237-
#ifdef ESP_RMAKER_CLAIM_ENABLED
238-
#ifdef CONFIG_ESP_RMAKER_SELF_CLAIM
239-
esp_rmaker_priv_data->claim_data = esp_rmaker_self_claim_init();
240-
#endif
241-
#ifdef CONFIG_ESP_RMAKER_ASSISTED_CLAIM
242-
esp_rmaker_priv_data->claim_data = esp_rmaker_assisted_claim_init();
243-
#endif
244-
if (!esp_rmaker_priv_data->claim_data) {
245-
esp_rmaker_deinit_priv_data(esp_rmaker_priv_data);
246-
esp_rmaker_priv_data = NULL;
247-
ESP_LOGE(TAG, "Failed to initialise Claiming.");
248-
return ESP_FAIL;
249-
}
250-
esp_rmaker_priv_data->need_claim = true;
251-
#else
261+
if (esp_rmaker_mqtt_data_init(esp_rmaker_priv_data, use_claiming) != ESP_OK) {
252262
esp_rmaker_deinit_priv_data(esp_rmaker_priv_data);
253263
esp_rmaker_priv_data = NULL;
254264
ESP_LOGE(TAG, "Failed to initialise MQTT Config. Please perform \"claiming\" using RainMaker CLI.");
255265
return ESP_FAIL;
256-
#endif /* !ESP_RMAKER_CLAIM_ENABLED */
257266
} else {
258-
if (esp_rmaker_mqtt_init(esp_rmaker_priv_data->mqtt_config) != ESP_OK) {
259-
esp_rmaker_deinit_priv_data(esp_rmaker_priv_data);
260-
esp_rmaker_priv_data = NULL;
261-
ESP_LOGE(TAG, "Failed to initialise MQTT");
262-
return ESP_FAIL;
267+
if (!esp_rmaker_priv_data->need_claim) {
268+
if (esp_rmaker_mqtt_init(esp_rmaker_priv_data->mqtt_config) != ESP_OK) {
269+
esp_rmaker_deinit_priv_data(esp_rmaker_priv_data);
270+
esp_rmaker_priv_data = NULL;
271+
ESP_LOGE(TAG, "Failed to initialise MQTT");
272+
return ESP_FAIL;
273+
}
263274
}
264275
}
265276
esp_rmaker_priv_data->enable_time_sync = config->enable_time_sync;
@@ -285,7 +296,7 @@ static esp_err_t esp_rmaker_register_node(const esp_rmaker_node_t *node)
285296

286297
esp_rmaker_node_t *esp_rmaker_node_init(const esp_rmaker_config_t *config, const char *name, const char *type)
287298
{
288-
esp_err_t err = esp_rmaker_init(config);
299+
esp_err_t err = esp_rmaker_init(config, true);
289300
if (err != ESP_OK) {
290301
return NULL;
291302
}

0 commit comments

Comments
 (0)