Update reference db refresh

This commit is contained in:
Amin Ben Romdhane 2025-04-15 15:20:55 +00:00 committed by IOPSYS Dev
parent cecb673d82
commit 56937cd498
No known key found for this signature in database
7 changed files with 114 additions and 68 deletions

View file

@ -185,7 +185,7 @@ static void resolve_reference_path(struct async_request_context *ctx, struct blo
calculate_hash(token, hash_str, sizeof(hash_str));
_uci_get_option_str(ctx->uci_ctx, "reference_translation", "reference_path", hash_str, reference_path, sizeof(reference_path));
_uci_get_option_str(ctx->uci_ctx, "bbfdm_reference_db", "reference_path", hash_str, reference_path, sizeof(reference_path));
// Add path to list in order to be used by other parameters
add_linker_entry(ctx, token, reference_path);
@ -200,8 +200,6 @@ static void resolve_reference_path(struct async_request_context *ctx, struct blo
if (pos > 0) {
output[pos - 1] = 0; // Remove trailing comma
} else {
BBFDM_INFO("Can't resolve reference path '%s' -> Set its value to empty", ref_path);
}
}
@ -278,7 +276,7 @@ void send_response(struct async_request_context *ctx)
// Init uci context for only Get method
ctx->uci_ctx = uci_alloc_context();
if (ctx->uci_ctx) uci_set_confdir(ctx->uci_ctx, "/etc/bbfdm/dmmap/");
if (ctx->uci_ctx) uci_set_confdir(ctx->uci_ctx, "/var/state/");
}
prepare_and_send_response(ctx);

View file

@ -302,7 +302,7 @@ int bbfdm_get_reference_linker(struct dmctx *ctx, char *reference_path, struct d
calculate_hash(reference_path, hash_str, sizeof(hash_str));
int res = dmuci_get_option_value_string_bbfdm("reference_translation", "reference_value", hash_str, &uci_val);
int res = dmuci_get_option_value_string_varstate("bbfdm_reference_db", "reference_value", hash_str, &uci_val);
if (uci_val && uci_val[0] == '#' && uci_val[1] == '\0') {
reference_args->value = uci_val;

View file

@ -915,7 +915,7 @@ static bool is_same_reference_path(const char *curr_value, const char *in_value)
calculate_hash(pch, hash_str, sizeof(hash_str));
dmuci_get_option_value_string_bbfdm("reference_translation", "reference_path", hash_str, &uci_val);
dmuci_get_option_value_string_varstate("bbfdm_reference_db", "reference_path", hash_str, &uci_val);
if (DM_STRLEN(uci_val)) {
pos += snprintf(&resolved_path[pos], sizeof(resolved_path) - pos, "%s,", uci_val);
@ -938,42 +938,6 @@ static bool is_same_reference_path(const char *curr_value, const char *in_value)
return false;
}
static int convert_path_with_star(const char *full_obj, char *out_str, size_t out_len)
{
char str[1024] = {0};
char *pch, *pchr;
size_t pos = 0;
DM_STRNCPY(str, full_obj, sizeof(str));
for (pch = strtok_r(str, ".", &pchr); pch != NULL; pch = strtok_r(NULL, ".", &pchr)) {
const char *part = isdigit_str(pch) ? "*" : pch;
int written = snprintf(out_str + pos, out_len - pos, "%s.", part);
if (written < 0 || written >= (int)(out_len - pos)) {
return -1; // overflow
}
pos += written;
}
return 0;
}
static void set_references(const char *parent_path, const char *current_path, const char *key_name, const char *key_value, char *out_str, size_t out_len)
{
char linker[MAX_DM_PATH * 2] = {0};
char hash_str[9] = {0};
convert_path_with_star(parent_path, out_str, out_len);
snprintf(linker, sizeof(linker), "%s[%s==%s].", out_str, key_name, DM_STRLEN(key_value) ? key_value : "");
calculate_hash(linker, hash_str, sizeof(hash_str));
DM_STRNCPY(out_str, current_path, strlen(current_path));
dmuci_set_value_bbfdm("reference_translation", "reference_path", hash_str, out_str);
calculate_hash(out_str, hash_str, sizeof(hash_str));
dmuci_set_value_bbfdm("reference_translation", "reference_value", hash_str, DM_STRLEN(key_value) ? key_value : "#");
}
/* **********
* get value
* **********/
@ -1000,12 +964,6 @@ static int get_value_param(DMPARAM_ARGS)
}
fill_blob_param(&dmctx->bb, full_param, value, DMT_TYPE[leaf->type], leaf->dm_flags);
if (leaf->dm_flags & DM_FLAG_LINKER) {
// Update reference path & value
set_references(node->parent->current_object, node->current_object, leaf->parameter, value, full_param, sizeof(full_param));
}
return 0;
}
@ -1040,11 +998,6 @@ static int mparam_get_value_in_param(DMPARAM_ARGS)
fill_blob_param(&dmctx->bb, full_param, value, DMT_TYPE[leaf->type], leaf->dm_flags);
if (leaf->dm_flags & DM_FLAG_LINKER) {
// Update reference path & value
set_references(node->parent->current_object, node->current_object, leaf->parameter, value, full_param, sizeof(full_param));
}
dmctx->findparam = (dmctx->iswildcard) ? 1 : 0;
dmctx->stop = (dmctx->iswildcard) ? false : true;
return 0;
@ -1946,22 +1899,100 @@ int dm_entry_event(struct dmctx *dmctx)
/* **********
* get instances data base
* **********/
static int mobj_get_references_db(DMOBJECT_ARGS)
static void create_required_sections(struct dmctx *ctx)
{
struct uci_section *ref_s = NULL;
ref_s = dmuci_get_section_bbfdm("reference_translation", "reference_path");
ref_s = dmuci_get_section_varstate("bbfdm_reference_db", "reference_path");
if (ref_s == NULL) {
dmuci_add_section_bbfdm("reference_translation", "reference_path", &ref_s);
dmuci_add_section_varstate("bbfdm_reference_db", "reference_path", &ref_s);
dmuci_rename_section_by_section(ref_s, "reference_path");
}
ref_s = dmuci_get_section_bbfdm("reference_translation", "reference_value");
ref_s = dmuci_get_section_varstate("bbfdm_reference_db", "reference_value");
if (ref_s == NULL) {
dmuci_add_section_bbfdm("reference_translation", "reference_value", &ref_s);
dmuci_add_section_varstate("bbfdm_reference_db", "reference_value", &ref_s);
dmuci_rename_section_by_section(ref_s, "reference_value");
}
ref_s = dmuci_get_section_varstate("bbfdm_reference_db", ctx->in_value);
if (ref_s == NULL) {
dmuci_add_section_varstate("bbfdm_reference_db", "service", &ref_s);
dmuci_rename_section_by_section(ref_s, ctx->in_value);
} else {
struct uci_list *uci_list = NULL;
struct uci_element *e = NULL, *tmp = NULL;
dmuci_get_value_by_section_list(ref_s, "reference_path", &uci_list);
if (uci_list != NULL) {
uci_foreach_element_safe(uci_list, tmp, e) {
dmuci_set_value_varstate("bbfdm_reference_db", "reference_path", e->name, "");
dmuci_del_list_value_by_section(ref_s, "reference_path", e->name);
}
}
dmuci_get_value_by_section_list(ref_s, "reference_value", &uci_list);
if (uci_list != NULL) {
uci_foreach_element_safe(uci_list, tmp, e) {
dmuci_set_value_varstate("bbfdm_reference_db", "reference_value", e->name, "");
dmuci_del_list_value_by_section(ref_s, "reference_value", e->name);
}
}
}
// This argument is used as internal variable to pass service uci section
ctx->addobj_instance = (void *)ref_s;
}
static int convert_path_with_star(const char *full_obj, char *out_str, size_t out_len)
{
char str[1024] = {0};
char *pch, *pchr;
size_t pos = 0;
DM_STRNCPY(str, full_obj, sizeof(str));
for (pch = strtok_r(str, ".", &pchr); pch != NULL; pch = strtok_r(NULL, ".", &pchr)) {
const char *part = isdigit_str(pch) ? "*" : pch;
int written = snprintf(out_str + pos, out_len - pos, "%s.", part);
if (written < 0 || written >= (int)(out_len - pos)) {
return -1; // overflow
}
pos += written;
}
return 0;
}
static void set_references(struct uci_section *service_sec, const char *parent_path, const char *current_path, const char *key_name, const char *key_value, char *out_str, size_t out_len)
{
struct uci_list *uci_list = NULL;
char linker[MAX_DM_PATH * 2] = {0};
char hash_str[9] = {0};
convert_path_with_star(parent_path, out_str, out_len);
snprintf(linker, sizeof(linker), "%s[%s==%s].", out_str, key_name, DM_STRLEN(key_value) ? key_value : "");
calculate_hash(linker, hash_str, sizeof(hash_str));
DM_STRNCPY(out_str, current_path, strlen(current_path));
dmuci_set_value_varstate("bbfdm_reference_db", "reference_path", hash_str, out_str);
dmuci_get_value_by_section_list(service_sec, "reference_path", &uci_list);
if (!value_exists_in_uci_list(uci_list, hash_str))
dmuci_add_list_value_varstate("bbfdm_reference_db", section_name(service_sec), "reference_path", hash_str);
calculate_hash(out_str, hash_str, sizeof(hash_str));
dmuci_set_value_varstate("bbfdm_reference_db", "reference_value", hash_str, DM_STRLEN(key_value) ? key_value : "#");
dmuci_get_value_by_section_list(service_sec, "reference_value", &uci_list);
if (!value_exists_in_uci_list(uci_list, hash_str))
dmuci_add_list_value_varstate("bbfdm_reference_db", section_name(service_sec), "reference_value", hash_str);
}
static int mobj_get_references_db(DMOBJECT_ARGS)
{
return 0;
}
@ -1978,7 +2009,7 @@ static int mparam_get_references_db(DMPARAM_ARGS)
(leaf->getvalue)(full_param, dmctx, data, instance, &value);
set_references(node->parent->current_object, node->current_object, leaf->parameter, value, full_param, sizeof(full_param));
set_references((void *)dmctx->addobj_instance, node->parent->current_object, node->current_object, leaf->parameter, value, full_param, sizeof(full_param));
}
return 0;
@ -1990,6 +2021,8 @@ int dm_entry_references_db(struct dmctx *ctx)
DMNODE node = {.current_object = ""};
int err = 0;
create_required_sections(ctx);
ctx->inparam_isparam = 0;
ctx->findparam = 1;
ctx->stop = 0;

View file

@ -322,6 +322,7 @@ struct uci_section *dmuci_walk_all_sections(const char *package, struct uci_sect
int dmuci_get_option_value_string_bbfdm(const char *package, const char *section, const char *option, char **value);
int dmuci_set_value_bbfdm(const char *package, const char *section, const char *option, const char *value);
int dmuci_set_value_by_section_bbfdm(struct uci_section *s, const char *option, const char *value);
int dmuci_add_list_value_bbfdm(const char *package, const char *section, const char *option, const char *value);
int dmuci_add_section_bbfdm(const char *package, const char *stype, struct uci_section **s);
int dmuci_delete_bbfdm(const char *package, const char *section, const char *option, const char *value);
int dmuci_delete_by_section_unnamed_bbfdm(struct uci_section *s, const char *option, const char *value);
@ -331,13 +332,15 @@ int dmuci_commit_bbfdm(void);
struct uci_section *dmuci_get_section_bbfdm(const char *package, const char *section);
struct uci_section *dmuci_walk_section_bbfdm(const char *package, const char *stype, const void *arg1, const void *arg2, int cmp , int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk);
struct uci_section *dmuci_walk_section_varstate(const char *package, const char *stype, const void *arg1, const void *arg2, int cmp , int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk);
int dmuci_add_list_value_varstate(const char *package, const char *section, const char *option, const char *value);
int dmuci_add_section_varstate(const char *package, const char *stype, struct uci_section **s);
int dmuci_delete_by_section_varstate(struct uci_section *s, const char *option, const char *value);
int dmuci_get_option_value_string_varstate(const char *package, const char *section, const char *option, char **value);
int dmuci_set_value_varstate(const char *package, const char *section, const char *option, const char *value);
int dmuci_set_value_by_section_varstate(struct uci_section *s, const char *option, const char *value);
int dmuci_commit_package_varstate(char *package);
struct uci_section *dmuci_get_section_varstate(const char *package, const char *section);
struct uci_section *dmuci_walk_section_varstate(const char *package, const char *stype, const void *arg1, const void *arg2, int cmp , int (*filter)(struct uci_section *s, const void *value), struct uci_section *prev_section, int walk);
int db_get_value_string(const char *package, const char *section, const char *option, char **value);

View file

@ -306,6 +306,8 @@ static int bbfdm_instances_handler(struct ubus_context *ctx, struct ubus_object
fill_optional_data(&data, tb[DM_INSTANCES_OPTIONAL]);
bbfdm_refresh_references(data.bbf_ctx.dm_type, obj->name);
bbfdm_get(&data, BBF_INSTANCES);
free_path_list(&paths_list);
@ -377,7 +379,7 @@ int bbfdm_set_handler(struct ubus_context *ctx, struct ubus_object *obj,
bbf_cleanup(&data.bbf_ctx);
if (!fault) {
bbfdm_refresh_references(data.bbf_ctx.dm_type);
bbfdm_refresh_references(data.bbf_ctx.dm_type, obj->name);
}
end:
@ -512,7 +514,7 @@ end:
bbf_cleanup(&data.bbf_ctx);
if (!fault) {
bbfdm_refresh_references(data.bbf_ctx.dm_type);
bbfdm_refresh_references(data.bbf_ctx.dm_type, obj->name);
}
ubus_send_reply(ctx, req, data.bb.head);
@ -601,7 +603,7 @@ int bbfdm_refresh_references_db(struct ubus_context *ctx, struct ubus_object *ob
memset(&bb, 0, sizeof(struct blob_buf));
blob_buf_init(&bb, 0);
int res = bbfdm_refresh_references(BBFDM_BOTH);
int res = bbfdm_refresh_references(BBFDM_BOTH, obj->name);
blobmsg_add_u8(&bb, "status", !res ? true : false);
@ -742,7 +744,7 @@ int bbfdm_ubus_regiter_init(struct bbfdm_context *bbfdm_ctx)
if (err != UBUS_STATUS_OK)
return -1;
err = bbfdm_refresh_references(BBFDM_BOTH);
err = bbfdm_refresh_references(BBFDM_BOTH, bbfdm_ctx->config.out_name);
if (err) {
BBF_ERR("Failed to refresh instance data base");
return -1;
@ -777,10 +779,15 @@ void bbfdm_ubus_load_data_model(DM_MAP_OBJ *DynamicObj)
INTERNAL_ROOT_TREE = DynamicObj;
}
int bbfdm_refresh_references(unsigned int dm_type)
int bbfdm_refresh_references(unsigned int dm_type, const char *srv_obj_name)
{
char hash_str[9] = {0};
calculate_hash(srv_obj_name, hash_str, sizeof(hash_str));
struct dmctx bbf_ctx = {
.in_param = ROOT_NODE,
.in_value = hash_str,
.dm_type = dm_type
};
@ -788,8 +795,12 @@ int bbfdm_refresh_references(unsigned int dm_type)
int res = bbfdm_cmd_exec(&bbf_ctx, BBF_REFERENCES_DB);
if (!res) {
// Apply all bbfdm changes
dmuci_commit_bbfdm();
char config_name[32] = {0};
snprintf(config_name, sizeof(config_name), "%s", "bbfdm_reference_db");
// Apply all changes
dmuci_commit_package_varstate(config_name);
}
bbf_cleanup(&bbf_ctx);

View file

@ -50,6 +50,6 @@ int bbfdm_ubus_regiter_free(struct bbfdm_context *bbfdm_ctx);
void bbfdm_ubus_set_service_name(struct bbfdm_context *bbfdm_ctx, const char *srv_name);
void bbfdm_ubus_set_log_level(int log_level);
void bbfdm_ubus_load_data_model(DM_MAP_OBJ *DynamicObj);
int bbfdm_refresh_references(unsigned int dm_type);
int bbfdm_refresh_references(unsigned int dm_type, const char *srv_obj_name);
#endif /* BBFDM_UBUS_H */

View file

@ -363,6 +363,7 @@
"src/dataelements.c"
],
"extra_dependencies": [
"-DSERVICE_NAME=\\\"wifidmd\\\"",
"-DWIFIDMD_ENABLE_WIFI_DATAELEMENTS",
"-lm"
]