Skip to content

Commit 72283a5

Browse files
committed
esp_rmaker_node_config: Added some new fields
Node info: - project_name: The name of the project as configured in CMakeLists.txt. "model" was earlier being used for this and will continue to be used for backward compatibility, but now it would be ok to override model using the esp_rmaker_node_add_model() API. - platformm: Read from CONFIG_IDF_TARGET, tells the platform being used (esp32, esp32s2, esp32c3,....) Device - model (optional)
1 parent f57a5ec commit 72283a5

File tree

5 files changed

+88
-2
lines changed

5 files changed

+88
-2
lines changed

components/esp_rainmaker/include/esp_rmaker_core.h

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ typedef struct {
6363
char *fw_version;
6464
/** Model (Optional). If not set, PROJECT_NAME is used as default (recommended)*/
6565
char *model;
66+
/** Subtype (Optional). */
67+
char *subtype;
6668
} esp_rmaker_node_info_t;
6769

6870
/** ESP RainMaker Configuration */
@@ -430,10 +432,11 @@ esp_err_t esp_rmaker_node_add_attribute(const esp_rmaker_node_t *node, const cha
430432
*/
431433
esp_err_t esp_rmaker_node_add_fw_version(const esp_rmaker_node_t *node, const char *fw_version);
432434

433-
/** Add model for a node (Not recommended)
435+
/** Add model for a node
434436
*
435437
* Model is set internally to the project name. This API can be used to
436-
* override that name.
438+
* override that name, now that a new field "project" has also been added
439+
* internally to the node info.
437440
*
438441
* @param node Node handle.
439442
* @param[in] model New model string.
@@ -443,6 +446,16 @@ esp_err_t esp_rmaker_node_add_fw_version(const esp_rmaker_node_t *node, const ch
443446
*/
444447
esp_err_t esp_rmaker_node_add_model(const esp_rmaker_node_t *node, const char *model);
445448

449+
/** Add subtype for a node
450+
*
451+
* @param node Node handle.
452+
* @param[in] subtype Subtype string.
453+
*
454+
* @return ESP_OK on success.
455+
* @return error in case of failure.
456+
*/
457+
esp_err_t esp_rmaker_node_add_subtype(const esp_rmaker_node_t *node, const char *subtype);
458+
446459
/**
447460
* Create a Device
448461
*
@@ -572,6 +585,18 @@ esp_err_t esp_rmaker_device_add_attribute(const esp_rmaker_device_t *device, con
572585
*/
573586
esp_err_t esp_rmaker_device_add_subtype(const esp_rmaker_device_t *device, const char *subtype);
574587

588+
/** Add a Device model
589+
*
590+
* This would primarily be used by the phone apps to render different icons for the same device type.
591+
*
592+
* @param[in] device Device handle.
593+
* @param[in] model String describing the model.
594+
*
595+
* @return ESP_OK if the model was added successfully.
596+
* @return error in case of failure.
597+
*/
598+
esp_err_t esp_rmaker_device_add_model(const esp_rmaker_device_t *device, const char *model);
599+
575600
/** Get device name from handle
576601
*
577602
* @param[in] device Device handle.

components/esp_rainmaker/src/core/esp_rmaker_device.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ esp_err_t esp_rmaker_device_delete(const esp_rmaker_device_t *device)
4545
if (_device->subtype) {
4646
free(_device->subtype);
4747
}
48+
if (_device->model) {
49+
free(_device->model);
50+
}
4851
if (_device->name) {
4952
free(_device->name);
5053
}
@@ -217,6 +220,26 @@ esp_err_t esp_rmaker_device_add_subtype(const esp_rmaker_device_t *device, const
217220
if ((_device->subtype = strdup(subtype)) != NULL ){
218221
return ESP_OK;
219222
} else {
223+
ESP_LOGE(TAG, "Failed to allocate memory for device subtype");
224+
return ESP_ERR_NO_MEM;
225+
}
226+
}
227+
228+
/* Add a device model */
229+
esp_err_t esp_rmaker_device_add_model(const esp_rmaker_device_t *device, const char *model)
230+
{
231+
if (!device || !model) {
232+
ESP_LOGE(TAG, "Device handle or model cannot be NULL.");
233+
return ESP_ERR_INVALID_ARG;
234+
}
235+
_esp_rmaker_device_t *_device = (_esp_rmaker_device_t *)device;
236+
if (_device->model) {
237+
free(_device->model);
238+
}
239+
if ((_device->model = strdup(model)) != NULL ){
240+
return ESP_OK;
241+
} else {
242+
ESP_LOGE(TAG, "Failed to allocate memory for device model");
220243
return ESP_ERR_NO_MEM;
221244
}
222245
}

components/esp_rainmaker/src/core/esp_rmaker_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct esp_rmaker_device {
6767
char *name;
6868
char *type;
6969
char *subtype;
70+
char *model;
7071
esp_rmaker_device_write_cb_t write_cb;
7172
esp_rmaker_device_read_cb_t read_cb;
7273
void *priv_data;

components/esp_rainmaker/src/core/esp_rmaker_node.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ static void esp_rmaker_node_info_free(esp_rmaker_node_info_t *info)
3838
if (info->fw_version) {
3939
free(info->fw_version);
4040
}
41+
if (info->subtype) {
42+
free(info->subtype);
43+
}
4144
free(info);
4245
}
4346
}
@@ -150,6 +153,7 @@ esp_err_t esp_rmaker_node_add_fw_version(const esp_rmaker_node_t *node, const ch
150153
info->fw_version = strdup(fw_version);
151154
if (!info->fw_version) {
152155
ESP_LOGE(TAG, "Failed to allocate memory for fw version.");
156+
return ESP_ERR_NO_MEM;
153157
}
154158
return ESP_OK;
155159
}
@@ -171,6 +175,29 @@ esp_err_t esp_rmaker_node_add_model(const esp_rmaker_node_t *node, const char *m
171175
info->model = strdup(model);
172176
if (!info->model) {
173177
ESP_LOGE(TAG, "Failed to allocate memory for node model.");
178+
return ESP_ERR_NO_MEM;
179+
}
180+
return ESP_OK;
181+
}
182+
183+
esp_err_t esp_rmaker_node_add_subtype(const esp_rmaker_node_t *node, const char *subtype)
184+
{
185+
if (!node || !subtype) {
186+
ESP_LOGE(TAG, "Node handle or subtype cannot be NULL.");
187+
return ESP_ERR_INVALID_ARG;
188+
}
189+
esp_rmaker_node_info_t *info = esp_rmaker_node_get_info(node);
190+
if (!info) {
191+
ESP_LOGE(TAG, "Failed to get Node Info.");
192+
return ESP_ERR_INVALID_ARG;
193+
}
194+
if (info->subtype) {
195+
free(info->subtype);
196+
}
197+
info->subtype = strdup(subtype);
198+
if (!info->subtype) {
199+
ESP_LOGE(TAG, "Failed to allocate memory for node subtype.");
200+
return ESP_ERR_NO_MEM;
174201
}
175202
return ESP_OK;
176203
}

components/esp_rainmaker/src/core/esp_rmaker_node_config.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <sdkconfig.h>
1515
#include <string.h>
1616
#include <esp_log.h>
17+
#include <esp_ota_ops.h>
1718
#include <json_generator.h>
1819
#include <esp_rmaker_core.h>
1920
#include "esp_rmaker_internal.h"
@@ -32,7 +33,13 @@ static esp_err_t esp_rmaker_report_info(json_gen_str_t *jptr)
3233
json_gen_obj_set_string(jptr, "name", info->name);
3334
json_gen_obj_set_string(jptr, "fw_version", info->fw_version);
3435
json_gen_obj_set_string(jptr, "type", info->type);
36+
if (info->subtype) {
37+
json_gen_obj_set_string(jptr, "subtype", info->subtype);
38+
}
3539
json_gen_obj_set_string(jptr, "model", info->model);
40+
const esp_app_desc_t *app_desc = esp_ota_get_app_description();
41+
json_gen_obj_set_string(jptr, "project_name", app_desc->project_name);
42+
json_gen_obj_set_string(jptr, "platform", CONFIG_IDF_TARGET);
3643
json_gen_pop_object(jptr);
3744
return ESP_OK;
3845
}
@@ -187,6 +194,9 @@ static esp_err_t esp_rmaker_report_devices_or_services(json_gen_str_t *jptr, cha
187194
if (device->subtype) {
188195
json_gen_obj_set_string(jptr, "subtype", device->subtype);
189196
}
197+
if (device->model) {
198+
json_gen_obj_set_string(jptr, "model", device->model);
199+
}
190200
if (device->attributes) {
191201
json_gen_push_array(jptr, "attributes");
192202
esp_rmaker_attr_t *attr = device->attributes;

0 commit comments

Comments
 (0)