From c4701f09682668a638f5e070d7298e6292221653 Mon Sep 17 00:00:00 2001 From: Amin Ben Romdhane Date: Tue, 6 May 2025 15:24:40 +0200 Subject: [PATCH] Add support for data model schema --- dm-service/dm_service.c | 15 ++++++++-- libbbfdm-ubus/bbfdm-ubus.c | 59 ++++++++++++++++++++++++++++++++++++++ libbbfdm-ubus/bbfdm-ubus.h | 2 ++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/dm-service/dm_service.c b/dm-service/dm_service.c index d4fbd97f..7b47e120 100644 --- a/dm-service/dm_service.c +++ b/dm-service/dm_service.c @@ -20,7 +20,8 @@ static void usage(char *prog) fprintf(stderr, "options:\n"); fprintf(stderr, " -m micro-service name\n"); fprintf(stderr, " -l log verbosity value as per standard syslog\n"); - fprintf(stderr, " -h Displays this help\n"); + fprintf(stderr, " -d Display the schema data model supported by micro-service\n"); + fprintf(stderr, " -h Display this help\n"); fprintf(stderr, "\n"); } @@ -29,11 +30,11 @@ int main(int argc, char **argv) struct bbfdm_context bbfdm_ctx = {0}; char proc_name[64] = {0}; int log_level = LOG_ERR; - int err = 0, ch; + int err = 0, ch, dm_type = 0; memset(&bbfdm_ctx, 0, sizeof(struct bbfdm_context)); - while ((ch = getopt(argc, argv, "hl:m:")) != -1) { + while ((ch = getopt(argc, argv, "hdl:m:")) != -1) { switch (ch) { case 'm': bbfdm_ubus_set_service_name(&bbfdm_ctx, optarg); @@ -45,6 +46,9 @@ int main(int argc, char **argv) log_level = 7; } break; + case 'd': + dm_type++; + break; case 'h': usage(argv[0]); exit(0); @@ -58,6 +62,11 @@ int main(int argc, char **argv) exit(-1); } + if (dm_type > 0) { + int res = bbfdm_print_data_model_schema(&bbfdm_ctx, dm_type); + exit(res); + } + bbfdm_ubus_set_log_level(log_level); openlog(bbfdm_ctx.config.service_name, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); diff --git a/libbbfdm-ubus/bbfdm-ubus.c b/libbbfdm-ubus/bbfdm-ubus.c index b81160b5..fe7e7261 100644 --- a/libbbfdm-ubus/bbfdm-ubus.c +++ b/libbbfdm-ubus/bbfdm-ubus.c @@ -716,6 +716,65 @@ static int load_micro_service_data_model(struct bbfdm_context *daemon_ctx) return 0; } +int bbfdm_print_data_model_schema(struct bbfdm_context *bbfdm_ctx, const enum bbfdm_type_enum type) +{ + struct dmctx bbf_ctx = { + .in_param = ROOT_NODE, + .nextlevel = false, + .iscommand = true, + .isevent = true, + .isinfo = true, + .dm_type = type + }; + int err = 0; + + err = load_micro_service_config(&bbfdm_ctx->config); + if (err) { + fprintf(stderr, "Failed to load micro-service config\n"); + return err; + } + + err = load_micro_service_data_model(bbfdm_ctx); + if (err) { + fprintf(stderr, "Failed to load micro-service data model\n"); + bbfdm_ctx_cleanup(bbfdm_ctx); + return err; + } + + bbf_init(&bbf_ctx); + + err = bbf_entry_method(&bbf_ctx, BBF_SCHEMA); + if (!err) { + struct blob_attr *cur = NULL; + size_t rem = 0; + + blobmsg_for_each_attr(cur, bbf_ctx.bb.head, rem) { + struct blob_attr *tb[3] = {0}; + const struct blobmsg_policy p[3] = { + { "path", BLOBMSG_TYPE_STRING }, + { "data", BLOBMSG_TYPE_STRING }, + { "type", BLOBMSG_TYPE_STRING } + }; + + blobmsg_parse(p, 3, tb, blobmsg_data(cur), blobmsg_len(cur)); + + char *name = (tb[0]) ? blobmsg_get_string(tb[0]) : ""; + char *data = (tb[1]) ? blobmsg_get_string(tb[1]) : ""; + char *type = (tb[2]) ? blobmsg_get_string(tb[2]) : ""; + + printf("%s %s %s\n", name, type, strlen(data) ? data : "0"); + } + } else { + printf("ERROR: %d retrieving %s\n", err, ROOT_NODE); + err = -1; + } + + bbf_cleanup(&bbf_ctx); + + bbfdm_ctx_cleanup(bbfdm_ctx); + return 0; +} + int bbfdm_ubus_regiter_init(struct bbfdm_context *bbfdm_ctx) { int err = 0; diff --git a/libbbfdm-ubus/bbfdm-ubus.h b/libbbfdm-ubus/bbfdm-ubus.h index 2c169e3c..f3742267 100644 --- a/libbbfdm-ubus/bbfdm-ubus.h +++ b/libbbfdm-ubus/bbfdm-ubus.h @@ -47,6 +47,8 @@ typedef struct bbfdm_data { int bbfdm_ubus_regiter_init(struct bbfdm_context *bbfdm_ctx); int bbfdm_ubus_regiter_free(struct bbfdm_context *bbfdm_ctx); +int bbfdm_print_data_model_schema(struct bbfdm_context *bbfdm_ctx, const enum bbfdm_type_enum type); + 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);