esp_rmaker_claim: Fetch MQTT Host from assisted claiming service

For assisted claiming, the node itself does not connect to the
claiming service directly. Instead, it just creates and parses payloads which
are sent/received by phone apps on its behalf. Since the phone app
can get certificates from any claiming service, it will be better
to have the MQTT Host URL also part of the response, so that there
is no mismatch between certificate and MQTT Host URL.
This commit is contained in:
Piyush Shah
2024-09-03 00:59:23 +05:30
parent e98d36b5ab
commit d0f22ea9d3

View File

@@ -252,17 +252,26 @@ static esp_err_t handle_claim_verify_response(esp_rmaker_claim_data_t *claim_dat
int required_len = 0;
if (json_obj_get_strlen(&jctx, "certificate", &required_len) == 0) {
required_len++; /* For NULL termination */
char *certificate = MEM_CALLOC_EXTRAM(1, required_len);
if (!certificate) {
char *value_buf = MEM_CALLOC_EXTRAM(1, required_len);
if (!value_buf) {
json_parse_end(&jctx);
ESP_LOGE(TAG, "Failed to allocate %d bytes for certificate.", required_len);
return ESP_ERR_NO_MEM;
}
json_obj_get_string(&jctx, "certificate", certificate, required_len);
/* Just using the certificate buffer itself (which is expected to be large enough) to
* check if the claiming service has also sent an MQTT Host, before going on to read
* the certificate itself.
*/
if (json_obj_get_string(&jctx, "mqtt_host", value_buf, required_len) == 0) {
ESP_LOGI(TAG, "Storing received MQTT Host: %s", value_buf);
esp_rmaker_factory_set(ESP_RMAKER_MQTT_HOST_NVS_KEY, value_buf, strlen(value_buf));
memset(value_buf, 0, required_len);
}
json_obj_get_string(&jctx, "certificate", value_buf, required_len);
json_parse_end(&jctx);
unescape_new_line(certificate);
esp_err_t err = esp_rmaker_factory_set(ESP_RMAKER_CLIENT_CERT_NVS_KEY, certificate, strlen(certificate));
free(certificate);
unescape_new_line(value_buf);
esp_err_t err = esp_rmaker_factory_set(ESP_RMAKER_CLIENT_CERT_NVS_KEY, value_buf, strlen(value_buf));
free(value_buf);
return err;
} else {
ESP_LOGE(TAG, "Claim Verify Response invalid.");
@@ -579,6 +588,7 @@ static esp_err_t handle_assisted_claim_init_response(esp_rmaker_claim_data_t *cl
json_gen_str_start(&jstr, claim_data->payload, sizeof(claim_data->payload), NULL, NULL);
json_gen_start_object(&jstr);
json_gen_obj_set_string(&jstr, "csr", (char *)claim_data->csr);
json_gen_obj_set_bool(&jstr, "send_mqtt_host", true);
json_gen_end_object(&jstr);
json_gen_str_end(&jstr);
claim_data->payload_len = strlen(claim_data->payload);