Skip to content

Commit 3c7a563

Browse files
committed
Merge branch 'feature/valid_vals' into 'master'
esp_rmaker_core: Add a facility to add a valid set of string values See merge request app-frameworks/esp-rainmaker!83
2 parents f74179b + 1cca9ba commit 3c7a563

File tree

4 files changed

+86
-13
lines changed

4 files changed

+86
-13
lines changed

components/esp_rainmaker/include/esp_rmaker_core.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,9 @@ esp_err_t esp_rmaker_param_add_ui_type(const esp_rmaker_param_t *param, const ch
578578
*
579579
* This can be used to add bounds (min/max values) for a given integer parameter. Eg. brightness
580580
* will have bounds as 0 and 100 if it is a percentage.
581-
* Eg. esp_rmaker_param_add_bounds("Light", "brightness", esp_rmaker_int(0), esp_rmaker_int(100), esp_rmaker_int(5));
581+
* Eg. esp_rmaker_param_add_bounds(brightness_param, esp_rmaker_int(0), esp_rmaker_int(100), esp_rmaker_int(5));
582+
*
583+
* @note The RainMaker core does not check the bounds. It is upto the application to handle it.
582584
*
583585
* @param[in] param Parameter handle.
584586
* @param[in] min Minimum allowed value.
@@ -591,6 +593,27 @@ esp_err_t esp_rmaker_param_add_ui_type(const esp_rmaker_param_t *param, const ch
591593
esp_err_t esp_rmaker_param_add_bounds(const esp_rmaker_param_t *param,
592594
esp_rmaker_param_val_t min, esp_rmaker_param_val_t max, esp_rmaker_param_val_t step);
593595

596+
/**
597+
* Add a list of valid strings for a string parameter
598+
*
599+
* This can be used to add a list of valid strings for a given string parameter.
600+
*
601+
* Eg.
602+
* static const char *valid_strs[] = {"None","Yes","No","Can't Say"};
603+
* esp_rmaker_param_add_valid_str_list(param, valid_strs, 4);
604+
*
605+
* @note The RainMaker core does not check the values. It is upto the application to handle it.
606+
*
607+
* @param[in] param Parameter handle.
608+
* @param[in] strs Pointer to an array of strings. Note that this memory should stay allocated
609+
* throughout the lifetime of this parameter.
610+
* @param[in] count Number of strings in the above array.
611+
*
612+
* @return ESP_OK on success.
613+
* return error in case of failure.
614+
*/
615+
esp_err_t esp_rmaker_param_add_valid_str_list(const esp_rmaker_param_t *param, const char *strs[], uint8_t count);
616+
594617
/** Update and report a parameter
595618
*
596619
* Calling this API will update the parameter and report it to ESP RainMaker cloud.

components/esp_rainmaker/src/core/esp_rmaker_internal.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@
1818
#include <json_generator.h>
1919
#include <esp_rmaker_core.h>
2020
#define RMAKER_PARAM_FLAG_VALUE_CHANGE 0x01
21+
typedef struct {
22+
esp_rmaker_param_val_t min;
23+
esp_rmaker_param_val_t max;
24+
esp_rmaker_param_val_t step;
25+
} esp_rmaker_param_bounds_t;
26+
27+
typedef struct {
28+
uint8_t str_list_cnt;
29+
const char **str_list;
30+
} esp_rmaker_param_valid_str_list_t;
31+
2132
struct esp_rmaker_param {
2233
char *name;
2334
char *type;
2435
uint8_t flags;
2536
uint8_t prop_flags;
2637
char *ui_type;
2738
esp_rmaker_param_val_t val;
28-
esp_rmaker_param_val_t min;
29-
esp_rmaker_param_val_t max;
30-
esp_rmaker_param_val_t step;
39+
esp_rmaker_param_bounds_t *bounds;
40+
esp_rmaker_param_valid_str_list_t *valid_str_list;
3141
struct esp_rmaker_device *parent;
3242
struct esp_rmaker_param * next;
3343
};

components/esp_rainmaker/src/core/esp_rmaker_node_config.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,21 @@ static esp_err_t esp_rmaker_report_param_config(_esp_rmaker_param_t *param, json
144144
json_gen_arr_set_string(jptr, "time_series");
145145
}
146146
json_gen_pop_array(jptr);
147-
if ((param->min.type != RMAKER_VAL_TYPE_INVALID) || (param->max.type != RMAKER_VAL_TYPE_INVALID)) {
147+
if (param->bounds) {
148148
json_gen_push_object(jptr, "bounds");
149-
esp_rmaker_report_value(&param->min, "min", jptr);
150-
esp_rmaker_report_value(&param->max, "max", jptr);
151-
if (param->step.val.i) {
152-
esp_rmaker_report_value(&param->step, "step", jptr);
149+
esp_rmaker_report_value(&param->bounds->min, "min", jptr);
150+
esp_rmaker_report_value(&param->bounds->max, "max", jptr);
151+
if (param->bounds->step.val.i) {
152+
esp_rmaker_report_value(&param->bounds->step, "step", jptr);
153153
}
154154
json_gen_pop_object(jptr);
155-
155+
}
156+
if (param->valid_str_list) {
157+
json_gen_push_array(jptr, "valid_strs");
158+
for (int i = 0; i < param->valid_str_list->str_list_cnt; i++) {
159+
json_gen_arr_set_string(jptr, param->valid_str_list->str_list[i]);
160+
}
161+
json_gen_pop_array(jptr);
156162
}
157163
if (param->ui_type) {
158164
json_gen_obj_set_string(jptr, "ui_type", param->ui_type);

components/esp_rainmaker/src/core/esp_rmaker_param.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,46 @@ esp_err_t esp_rmaker_param_add_bounds(const esp_rmaker_param_t *param,
411411
ESP_LOGE(TAG, "Cannot set bounds for %s because of value type mismatch.", _param->name);
412412
return ESP_ERR_INVALID_ARG;
413413
}
414-
_param->min = min;
415-
_param->max = max;
416-
_param->step = step;
414+
esp_rmaker_param_bounds_t *bounds = calloc(1, sizeof(esp_rmaker_param_bounds_t));
415+
if (!bounds) {
416+
ESP_LOGE(TAG, "Failed to allocate memory for parameter bounds.");
417+
return ESP_ERR_NO_MEM;
418+
}
419+
bounds->min = min;
420+
bounds->max = max;
421+
bounds->step = step;
422+
if (_param->bounds) {
423+
free(_param->bounds);
424+
}
425+
_param->bounds = bounds;
417426
return ESP_OK;
418427
}
419428

429+
esp_err_t esp_rmaker_param_add_valid_str_list(const esp_rmaker_param_t *param, const char *strs[], uint8_t count)
430+
{
431+
if (!param) {
432+
ESP_LOGE(TAG, "Param handle cannot be NULL.");
433+
return ESP_ERR_INVALID_ARG;
434+
}
435+
_esp_rmaker_param_t *_param = (_esp_rmaker_param_t *)param;
436+
if (_param->val.type != RMAKER_VAL_TYPE_STRING) {
437+
ESP_LOGE(TAG, "Only string params can have valid strings array.");
438+
return ESP_ERR_INVALID_ARG;
439+
}
440+
esp_rmaker_param_valid_str_list_t *valid_str_list = calloc(1, sizeof(esp_rmaker_param_valid_str_list_t));
441+
if (!valid_str_list) {
442+
ESP_LOGE(TAG, "Failed to allocate memory for valid strings array.");
443+
return ESP_ERR_NO_MEM;
444+
}
445+
valid_str_list->str_list = strs;
446+
valid_str_list->str_list_cnt = count;
447+
if (_param->valid_str_list) {
448+
free(_param->valid_str_list);
449+
}
450+
_param->valid_str_list = valid_str_list;
451+
return ESP_OK;
452+
}
453+
420454
esp_err_t esp_rmaker_param_add_ui_type(const esp_rmaker_param_t *param, const char *ui_type)
421455
{
422456
if (!param || !ui_type) {

0 commit comments

Comments
 (0)