From f8b833ac2f5caa1d2de2c249d9855f36141b4543 Mon Sep 17 00:00:00 2001 From: Piyush Shah Date: Wed, 26 May 2021 22:32:16 +0530 Subject: [PATCH 1/2] esp_rmaker_core: Add a new field "subtype" for devices --- .../esp_rainmaker/include/esp_rmaker_core.h | 13 ++++++++++++ .../src/core/esp_rmaker_device.c | 21 +++++++++++++++++++ .../src/core/esp_rmaker_internal.h | 1 + .../src/core/esp_rmaker_node_config.c | 3 +++ 4 files changed, 38 insertions(+) diff --git a/components/esp_rainmaker/include/esp_rmaker_core.h b/components/esp_rainmaker/include/esp_rmaker_core.h index fa6a922..a6fd3c9 100644 --- a/components/esp_rainmaker/include/esp_rmaker_core.h +++ b/components/esp_rainmaker/include/esp_rmaker_core.h @@ -533,6 +533,19 @@ esp_err_t esp_rmaker_node_remove_device(const esp_rmaker_node_t *node, const esp */ esp_err_t esp_rmaker_device_add_attribute(const esp_rmaker_device_t *device, const char *attr_name, const char *val); +/** Add a Device subtype + * + * This can be something like esp.subtype.rgb-light for a device of type esp.device.lightbulb. + * This would primarily be used by the phone apps to render different icons for the same device type. + * + * @param[in] device Device handle. + * @param[in] subtype String describing the sub type. + * + * @return ESP_OK if the subtype was added successfully. + * @return error in case of failure. + */ +esp_err_t esp_rmaker_device_add_subtype(const esp_rmaker_device_t *device, const char *subtype); + /** Get device name from handle * * @param[in] device Device handle. diff --git a/components/esp_rainmaker/src/core/esp_rmaker_device.c b/components/esp_rainmaker/src/core/esp_rmaker_device.c index ce667c7..96c4de1 100644 --- a/components/esp_rainmaker/src/core/esp_rmaker_device.c +++ b/components/esp_rainmaker/src/core/esp_rmaker_device.c @@ -42,6 +42,9 @@ esp_err_t esp_rmaker_device_delete(const esp_rmaker_device_t *device) esp_rmaker_param_delete((esp_rmaker_param_t *)param); param = next_param; } + if (_device->subtype) { + free(_device->subtype); + } if (_device->name) { free(_device->name); } @@ -204,6 +207,24 @@ esp_err_t esp_rmaker_device_add_attribute(const esp_rmaker_device_t *device, con return ESP_OK; } +/* Add a device subtype */ +esp_err_t esp_rmaker_device_add_subtype(const esp_rmaker_device_t *device, const char *subtype) +{ + if (!device || !subtype) { + ESP_LOGE(TAG, "Device handle or subtype cannot be NULL."); + return ESP_ERR_INVALID_ARG; + } + _esp_rmaker_device_t *_device = (_esp_rmaker_device_t *)device; + if (_device->subtype) { + free(_device->subtype); + } + if ((_device->subtype = strdup(subtype)) != NULL ){ + return ESP_OK; + } else { + return ESP_ERR_NO_MEM; + } +} + esp_err_t esp_rmaker_device_assign_primary_param(const esp_rmaker_device_t *device, const esp_rmaker_param_t *param) { if (!device || !param) { diff --git a/components/esp_rainmaker/src/core/esp_rmaker_internal.h b/components/esp_rainmaker/src/core/esp_rmaker_internal.h index b5c00ed..972795d 100644 --- a/components/esp_rainmaker/src/core/esp_rmaker_internal.h +++ b/components/esp_rainmaker/src/core/esp_rmaker_internal.h @@ -63,6 +63,7 @@ typedef struct esp_rmaker_attr esp_rmaker_attr_t; struct esp_rmaker_device { char *name; char *type; + char *subtype; esp_rmaker_device_write_cb_t write_cb; esp_rmaker_device_read_cb_t read_cb; void *priv_data; diff --git a/components/esp_rainmaker/src/core/esp_rmaker_node_config.c b/components/esp_rainmaker/src/core/esp_rmaker_node_config.c index b71461f..3cd82c0 100644 --- a/components/esp_rainmaker/src/core/esp_rmaker_node_config.c +++ b/components/esp_rainmaker/src/core/esp_rmaker_node_config.c @@ -184,6 +184,9 @@ static esp_err_t esp_rmaker_report_devices_or_services(json_gen_str_t *jptr, cha if (device->type) { json_gen_obj_set_string(jptr, "type", device->type); } + if (device->subtype) { + json_gen_obj_set_string(jptr, "subtype", device->subtype); + } if (device->attributes) { json_gen_push_array(jptr, "attributes"); esp_rmaker_attr_t *attr = device->attributes; From 8f73dda4fb52ac4818773a99b5ff30770a59c74c Mon Sep 17 00:00:00 2001 From: Piyush Shah Date: Mon, 31 May 2021 18:24:09 +0530 Subject: [PATCH 2/2] esp_rmaker_core: Add an API to get parameter value from Rmaker core --- components/esp_rainmaker/include/esp_rmaker_core.h | 13 +++++++++++++ .../esp_rainmaker/src/core/esp_rmaker_param.c | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/components/esp_rainmaker/include/esp_rmaker_core.h b/components/esp_rainmaker/include/esp_rmaker_core.h index a6fd3c9..5483734 100644 --- a/components/esp_rainmaker/include/esp_rmaker_core.h +++ b/components/esp_rainmaker/include/esp_rmaker_core.h @@ -742,6 +742,19 @@ char *esp_rmaker_param_get_name(const esp_rmaker_param_t *param); */ char *esp_rmaker_param_get_type(const esp_rmaker_param_t *param); +/** Get parameter value + * + * This gives the parameter value that is stored in the RainMaker core. + * + * @note This does not call any explicit functions to read value from hardware/driver. + * + * @param[in] param Parameter handle + * + * @return Pointer to parameter value on success. + * @return NULL in case of failure. + */ +esp_rmaker_param_val_t *esp_rmaker_param_get_val(esp_rmaker_param_t *param); + /** Report the node details to the cloud * * This API reports node details i.e. the node configuration and values of all the parameters to the ESP RainMaker cloud. diff --git a/components/esp_rainmaker/src/core/esp_rmaker_param.c b/components/esp_rainmaker/src/core/esp_rmaker_param.c index 0115362..4d114b5 100644 --- a/components/esp_rainmaker/src/core/esp_rmaker_param.c +++ b/components/esp_rainmaker/src/core/esp_rmaker_param.c @@ -438,6 +438,15 @@ esp_err_t esp_rmaker_param_store_value(_esp_rmaker_param_t *param) return err; } +esp_rmaker_param_val_t *esp_rmaker_param_get_val(esp_rmaker_param_t *param) +{ + if (!param) { + ESP_LOGE(TAG, "Param handle cannot be NULL."); + return NULL; + } + return &((_esp_rmaker_param_t *)param)->val; +} + esp_err_t esp_rmaker_param_delete(const esp_rmaker_param_t *param) { _esp_rmaker_param_t *_param = (_esp_rmaker_param_t *)param;