From f51f84d6478088873e42e88133e66c34c945c809 Mon Sep 17 00:00:00 2001 From: Amin Ben Ramdhane Date: Mon, 1 Nov 2021 13:44:30 +0100 Subject: [PATCH] Ticket refs #6099: JSON method doesn't support passing the index in path argument --- dmdynamicjson.c | 26 ++++++++++++++++++++++++-- libbbf_api/dmbbf.c | 3 +++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/dmdynamicjson.c b/dmdynamicjson.c index 1099f538..bf720d15 100644 --- a/dmdynamicjson.c +++ b/dmdynamicjson.c @@ -420,6 +420,22 @@ static char *get_param_ubus_value(json_object *json_obj, char *arguments) return value; } +static void replace_indexes(struct dmctx *ctx, char *refparam, char *obj, size_t obj_size) +{ + unsigned pos = 0; + unsigned char idx = 0; + + for (int i = 0; refparam[i] != '\0'; i++) { + if (strstr(&refparam[i], "{i}") == &refparam[i]) { + pos += snprintf(&obj[pos], obj_size - pos, "%s", ctx->inst_buf[idx] ? ctx->inst_buf[idx] : ""); + idx++; + i += 3; // increase i with length of "{i}" + } + + pos += snprintf(&obj[pos], obj_size - pos, "%c", refparam[i]); + } +} + static char *uci_get_value(json_object *mapping_obj, char *refparam, struct dmctx *ctx, void *data, char *instance) { struct json_object *obj = NULL; @@ -500,10 +516,16 @@ static char *ubus_get_value(json_object *mapping_obj, char *refparam, struct dmc } if (args1 && args2) { - if (data && (strcmp(json_object_get_string(args2), "@Name") == 0)) + if (data && (strcmp(json_object_get_string(args2), "@Name") == 0)) { dmubus_call(arg2_1, json_object_get_string(method), UBUS_ARGS{{args1, section_name((struct uci_section *)data), String}}, 1, &res); - else + } else if (strstr(json_object_get_string(args2), "{i}")) { + char arg2_buf[512] = {0}; + + replace_indexes(ctx, json_object_get_string(args2), arg2_buf, sizeof(arg2_buf)); + dmubus_call(arg2_1, json_object_get_string(method), UBUS_ARGS{{args1, arg2_buf, String}}, 1, &res); + } else { dmubus_call(arg2_1, json_object_get_string(method), UBUS_ARGS{{args1, json_object_get_string(args2), String}}, 1, &res); + } } else { dmubus_call(arg2_1, json_object_get_string(method), UBUS_ARGS{0}, 0, &res); } diff --git a/libbbf_api/dmbbf.c b/libbbf_api/dmbbf.c index 91bea29c..e0d3e05b 100644 --- a/libbbf_api/dmbbf.c +++ b/libbbf_api/dmbbf.c @@ -639,6 +639,7 @@ char *handle_instance(struct dmctx *dmctx, DMNODE *parent_node, struct uci_secti break; } + dmctx->inst_buf[parent_node->instance_level] = instance; return instance; } @@ -657,6 +658,8 @@ char *handle_instance_without_section(struct dmctx *dmctx, DMNODE *parent_node, case BROWSE_NUM_OF_ENTRIES: break; } + + dmctx->inst_buf[parent_node->instance_level] = instance; return instance; }