mirror of
https://dev.iopsys.eu/bbf/bbfdm.git
synced 2025-12-10 07:44:39 +01:00
Draft: Add support for init and clean module
This commit is contained in:
parent
9dc981b081
commit
2906af8a2a
7 changed files with 58 additions and 35 deletions
|
|
@ -156,6 +156,8 @@ typedef struct dm_map_obj {
|
|||
char *path;
|
||||
struct dm_obj_s *root_obj;
|
||||
struct dm_leaf_s *root_leaf;
|
||||
int (*init_module)(void *data);
|
||||
int (*clean_module)(void *data);
|
||||
} DM_MAP_OBJ;
|
||||
|
||||
struct dm_reference {
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@ LIST_HEAD(head_registered_service);
|
|||
|
||||
static void run_schema_updater(struct bbfdm_context *u);
|
||||
static void periodic_instance_updater(struct uloop_timeout *t);
|
||||
static void bbfdm_register_instance_refresh_timer(struct ubus_context *ctx, int start_in);
|
||||
static void bbfdm_cancel_instance_refresh_timer(struct ubus_context *ctx);
|
||||
|
||||
// Global variables
|
||||
static void *deamon_lib_handle = NULL;
|
||||
|
|
@ -55,7 +57,7 @@ static void bbfdm_ctx_cleanup(struct bbfdm_context *u)
|
|||
}
|
||||
|
||||
/* DotSo Plugin */
|
||||
bbfdm_free_dotso_plugin(&deamon_lib_handle);
|
||||
bbfdm_free_dotso_plugin(u, &deamon_lib_handle);
|
||||
|
||||
/* JSON Plugin */
|
||||
bbfdm_free_json_plugin();
|
||||
|
|
@ -106,7 +108,7 @@ static void async_complete_cb(struct uloop_process *p, __attribute__((unused)) i
|
|||
BBF_INFO("pid(%d) blob data sent raw(%zu)", r->process.pid, blob_raw_len(bb->head));
|
||||
ubus_complete_deferred_request(r->ctx, &r->req, 0);
|
||||
if (r->is_operate) {
|
||||
register_instance_refresh_timer(r->ctx, 0);
|
||||
bbfdm_register_instance_refresh_timer(r->ctx, 0);
|
||||
}
|
||||
munmap(r->result, DEF_IPC_DATA_LEN);
|
||||
async_req_free(r);
|
||||
|
|
@ -555,7 +557,7 @@ static int bbfdm_operate_handler(struct ubus_context *ctx, struct ubus_object *o
|
|||
if (is_sync_operate_cmd(&data)) {
|
||||
bbfdm_operate_cmd(&data, NULL);
|
||||
} else {
|
||||
cancel_instance_refresh_timer(ctx);
|
||||
bbfdm_cancel_instance_refresh_timer(ctx);
|
||||
bbfdm_start_deferred(&data, bbfdm_operate_cmd, true);
|
||||
}
|
||||
|
||||
|
|
@ -1266,7 +1268,7 @@ static void lookup_event_cb(struct ubus_context *ctx,
|
|||
}
|
||||
}
|
||||
|
||||
void register_instance_refresh_timer(struct ubus_context *ctx, int start_in)
|
||||
static void bbfdm_register_instance_refresh_timer(struct ubus_context *ctx, int start_in_sec)
|
||||
{
|
||||
struct bbfdm_context *u;
|
||||
|
||||
|
|
@ -1276,14 +1278,14 @@ void register_instance_refresh_timer(struct ubus_context *ctx, int start_in)
|
|||
return;
|
||||
}
|
||||
|
||||
if (start_in >= 0) {
|
||||
BBF_INFO("Register instance refresh timer in %d ms...", start_in);
|
||||
if (start_in_sec >= 0) {
|
||||
BBF_INFO("Register instance refresh timer in %d sec...", start_in_sec);
|
||||
u->instance_timer.cb = periodic_instance_updater;
|
||||
uloop_timeout_set(&u->instance_timer, start_in);
|
||||
uloop_timeout_set(&u->instance_timer, start_in_sec * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
void cancel_instance_refresh_timer(struct ubus_context *ctx)
|
||||
static void bbfdm_cancel_instance_refresh_timer(struct ubus_context *ctx)
|
||||
{
|
||||
struct bbfdm_context *u;
|
||||
|
||||
|
|
@ -1304,13 +1306,9 @@ static void bbf_config_change_cb(struct ubus_context *ctx, struct ubus_event_han
|
|||
(void)ctx;
|
||||
(void)msg;
|
||||
|
||||
if (type && strcmp(type, "bbf.config.change") != 0)
|
||||
return;
|
||||
|
||||
BBF_INFO("Config updated, Scheduling instance refresh timers");
|
||||
|
||||
cancel_instance_refresh_timer(ctx);
|
||||
register_instance_refresh_timer(ctx, 0);
|
||||
bbfdm_schedule_instance_refresh_timer(ctx, 0);
|
||||
}
|
||||
|
||||
static void bbfdm_ctx_init(struct bbfdm_context *bbfdm_ctx)
|
||||
|
|
@ -1327,10 +1325,10 @@ static int daemon_load_data_model(struct bbfdm_context *daemon_ctx)
|
|||
|
||||
if (INTERNAL_ROOT_TREE) {
|
||||
BBF_INFO("Loading Data Model Internal plugin (%s)", daemon_ctx->config.service_name);
|
||||
err = bbfdm_load_internal_plugin(INTERNAL_ROOT_TREE, &daemon_ctx->config, &DEAMON_DM_ROOT_OBJ);
|
||||
err = bbfdm_load_internal_plugin(daemon_ctx, INTERNAL_ROOT_TREE, &daemon_ctx->config, &DEAMON_DM_ROOT_OBJ);
|
||||
} else {
|
||||
BBF_INFO("Loading Data Model External plugin (%s)", daemon_ctx->config.service_name);
|
||||
err = bbfdm_load_external_plugin(daemon_ctx->config.in_name, &deamon_lib_handle, &daemon_ctx->config, &DEAMON_DM_ROOT_OBJ);
|
||||
err = bbfdm_load_external_plugin(daemon_ctx, &deamon_lib_handle, &daemon_ctx->config, &DEAMON_DM_ROOT_OBJ);
|
||||
}
|
||||
|
||||
if (err)
|
||||
|
|
@ -1399,7 +1397,7 @@ int bbfdm_ubus_regiter_init(struct bbfdm_context *bbfdm_ctx)
|
|||
return -1;
|
||||
|
||||
run_schema_updater(bbfdm_ctx);
|
||||
register_instance_refresh_timer(&bbfdm_ctx->ubus_ctx, 1000);
|
||||
bbfdm_register_instance_refresh_timer(&bbfdm_ctx->ubus_ctx, 1);
|
||||
|
||||
err = register_events_to_ubus(&bbfdm_ctx->ubus_ctx, &bbfdm_ctx->event_handlers);
|
||||
if (err != 0)
|
||||
|
|
@ -1448,3 +1446,9 @@ void bbfdm_ubus_load_data_model(DM_MAP_OBJ *DynamicObj)
|
|||
{
|
||||
INTERNAL_ROOT_TREE = DynamicObj;
|
||||
}
|
||||
|
||||
void bbfdm_schedule_instance_refresh_timer(struct ubus_context *ctx, int start_in_sec)
|
||||
{
|
||||
bbfdm_cancel_instance_refresh_timer(ctx);
|
||||
bbfdm_register_instance_refresh_timer(ctx, start_in_sec);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,9 +55,6 @@ typedef struct bbfdm_data {
|
|||
bool is_raw;
|
||||
} bbfdm_data_t;
|
||||
|
||||
void register_instance_refresh_timer(struct ubus_context *ctx, int start_sec);
|
||||
void cancel_instance_refresh_timer(struct ubus_context *ctx);
|
||||
|
||||
int bbfdm_ubus_regiter_init(struct bbfdm_context *bbfdm_ctx);
|
||||
int bbfdm_ubus_regiter_free(struct bbfdm_context *bbfdm_ctx);
|
||||
|
||||
|
|
@ -65,4 +62,6 @@ void bbfdm_ubus_set_service_name(struct bbfdm_context *bbfdm_ctx, const char *sr
|
|||
void bbfdm_ubus_set_log_level(int log_level);
|
||||
void bbfdm_ubus_load_data_model(DM_MAP_OBJ *DynamicObj);
|
||||
|
||||
void bbfdm_schedule_instance_refresh_timer(struct ubus_context *ctx, int start_in_sec);
|
||||
|
||||
#endif /* BBFDM_UBUS_H */
|
||||
|
|
|
|||
|
|
@ -100,8 +100,7 @@ static void bbfdm_event_handler(struct ubus_context *ctx, struct ubus_event_hand
|
|||
if (ret)
|
||||
goto end;
|
||||
|
||||
cancel_instance_refresh_timer(ctx);
|
||||
register_instance_refresh_timer(ctx, 0);
|
||||
bbfdm_schedule_instance_refresh_timer(ctx, 2);
|
||||
|
||||
size_t blob_data_len = blob_len(bbf_ctx.bb.head);
|
||||
|
||||
|
|
@ -116,7 +115,7 @@ static void bbfdm_event_handler(struct ubus_context *ctx, struct ubus_event_hand
|
|||
|
||||
memcpy(e_args->blob_data, bbf_ctx.bb.head, blob_data_len);
|
||||
|
||||
bbfdm_task_add(event_callback, e_args, NULL, 5);
|
||||
bbfdm_task_add(event_callback, e_args, NULL, 6);
|
||||
}
|
||||
|
||||
end:
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ static void fill_dotso_micro_service_out_args(bbfdm_config_t *config, DMOBJ *ent
|
|||
strncpyt(config->out_name, ms_name, sizeof(config->out_name));
|
||||
}
|
||||
|
||||
int bbfdm_load_internal_plugin(DM_MAP_OBJ *dynamic_obj, bbfdm_config_t *config, DMOBJ **main_entry)
|
||||
int bbfdm_load_internal_plugin(struct bbfdm_context *bbfdm_ctx, DM_MAP_OBJ *dynamic_obj, bbfdm_config_t *config, DMOBJ **main_entry)
|
||||
{
|
||||
if (!dynamic_obj || !config || !main_entry) {
|
||||
BBF_ERR("Input validation failed");
|
||||
|
|
@ -103,13 +103,16 @@ int bbfdm_load_internal_plugin(DM_MAP_OBJ *dynamic_obj, bbfdm_config_t *config,
|
|||
dm_entryobj[i].nextobj = dynamic_obj[i].root_obj;
|
||||
dm_entryobj[i].leaf = dynamic_obj[i].root_leaf;
|
||||
dm_entryobj[i].bbfdm_type = BBFDM_BOTH;
|
||||
|
||||
if (dynamic_obj[i].init_module)
|
||||
dynamic_obj[i].init_module(bbfdm_ctx);
|
||||
}
|
||||
|
||||
*main_entry = dm_entryobj;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bbfdm_load_dotso_plugin(void **lib_handle, const char *file_path, bbfdm_config_t *config, DMOBJ **main_entry)
|
||||
int bbfdm_load_dotso_plugin(struct bbfdm_context *bbfdm_ctx, void **lib_handle, const char *file_path, bbfdm_config_t *config, DMOBJ **main_entry)
|
||||
{
|
||||
if (!lib_handle || !file_path || !strlen(file_path) || !config || !main_entry) {
|
||||
BBF_ERR("Input validation failed");
|
||||
|
|
@ -129,12 +132,25 @@ int bbfdm_load_dotso_plugin(void **lib_handle, const char *file_path, bbfdm_conf
|
|||
//Dynamic Object
|
||||
*(void **) (&dynamic_obj) = dlsym(handle, "tDynamicObj");
|
||||
|
||||
return bbfdm_load_internal_plugin(dynamic_obj, config, main_entry);
|
||||
return bbfdm_load_internal_plugin(bbfdm_ctx, dynamic_obj, config, main_entry);
|
||||
}
|
||||
|
||||
int bbfdm_free_dotso_plugin(void **lib_handle)
|
||||
int bbfdm_free_dotso_plugin(struct bbfdm_context *bbfdm_ctx, void **lib_handle)
|
||||
{
|
||||
if (*lib_handle) {
|
||||
DM_MAP_OBJ *dynamic_obj = NULL;
|
||||
|
||||
//Dynamic Object
|
||||
*(void **) (&dynamic_obj) = dlsym(*lib_handle, "tDynamicObj");
|
||||
|
||||
if (dynamic_obj) {
|
||||
// Clean module
|
||||
for (int i = 0; dynamic_obj[i].path; i++) {
|
||||
if (dynamic_obj[i].clean_module)
|
||||
dynamic_obj[i].clean_module(bbfdm_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
dlclose(*lib_handle);
|
||||
*lib_handle = NULL;
|
||||
}
|
||||
|
|
@ -260,10 +276,13 @@ int bbfdm_free_json_plugin(void)
|
|||
return free_json_plugins();
|
||||
}
|
||||
|
||||
int bbfdm_load_external_plugin(const char *file_path, void **lib_handle, bbfdm_config_t *config, DMOBJ **main_entry)
|
||||
int bbfdm_load_external_plugin(struct bbfdm_context *bbfdm_ctx, void **lib_handle, bbfdm_config_t *config, DMOBJ **main_entry)
|
||||
{
|
||||
char file_path[128] = {0};
|
||||
int err = -1;
|
||||
|
||||
snprintf(file_path, sizeof(file_path), "%s", bbfdm_ctx->config.in_name);
|
||||
|
||||
if (DM_STRLEN(file_path) == 0) {
|
||||
BBF_ERR("Input type/name not supported or defined");
|
||||
return -1;
|
||||
|
|
@ -277,7 +296,7 @@ int bbfdm_load_external_plugin(const char *file_path, void **lib_handle, bbfdm_c
|
|||
err = bbfdm_load_json_plugin(&loaded_json_files, &json_list, &json_memhead, file_path, config, main_entry);
|
||||
} else if (strcasecmp(ext, ".so") == 0) {
|
||||
BBF_INFO("Loading DotSo plugin %s", file_path);
|
||||
err = bbfdm_load_dotso_plugin(lib_handle, file_path, config, main_entry);
|
||||
err = bbfdm_load_dotso_plugin(bbfdm_ctx, lib_handle, file_path, config, main_entry);
|
||||
} else {
|
||||
BBF_ERR("Input type %s not supported", ext);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@
|
|||
|
||||
#ifndef PLUGIN_H
|
||||
|
||||
int bbfdm_load_internal_plugin(DM_MAP_OBJ *Dynamic_Obj, bbfdm_config_t *config, DMOBJ **main_entry);
|
||||
int bbfdm_load_external_plugin(const char *file_path, void **lib_handle, bbfdm_config_t *config, DMOBJ **main_entry);
|
||||
int bbfdm_load_internal_plugin(struct bbfdm_context *bbfdm_ctx, DM_MAP_OBJ *Dynamic_Obj, bbfdm_config_t *config, DMOBJ **main_entry);
|
||||
int bbfdm_load_external_plugin(struct bbfdm_context *bbfdm_ctx, void **lib_handle, bbfdm_config_t *config, DMOBJ **main_entry);
|
||||
|
||||
int bbfdm_load_dotso_plugin(void **lib_handle, const char *file_path, bbfdm_config_t *config, DMOBJ **main_entry);
|
||||
int bbfdm_free_dotso_plugin(void *lib_handle);
|
||||
int bbfdm_load_dotso_plugin(struct bbfdm_context *bbfdm_ctx, void **lib_handle, const char *file_path, bbfdm_config_t *config, DMOBJ **main_entry);
|
||||
int bbfdm_free_dotso_plugin(struct bbfdm_context *bbfdm_ctx, void *lib_handle);
|
||||
|
||||
int bbfdm_load_json_plugin(struct list_head *json_plugin, struct list_head *json_list, struct list_head *json_memhead,
|
||||
const char *file_path, bbfdm_config_t *config, DMOBJ **main_entry);
|
||||
|
|
|
|||
|
|
@ -83,9 +83,9 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
if (plugin_path == NULL) {
|
||||
err = bbfdm_load_internal_plugin(tDynamicObj, &bbfdm_config, &CLI_DM_ROOT_OBJ);
|
||||
err = bbfdm_load_internal_plugin(NULL, tDynamicObj, &bbfdm_config, &CLI_DM_ROOT_OBJ);
|
||||
} else {
|
||||
err = bbfdm_load_dotso_plugin(&cli_lib_handle, plugin_path, &bbfdm_config, &CLI_DM_ROOT_OBJ);
|
||||
err = bbfdm_load_dotso_plugin(NULL, &cli_lib_handle, plugin_path, &bbfdm_config, &CLI_DM_ROOT_OBJ);
|
||||
}
|
||||
|
||||
if (err || !CLI_DM_ROOT_OBJ) {
|
||||
|
|
@ -111,7 +111,7 @@ int main(int argc, char **argv)
|
|||
bbf_global_clean(CLI_DM_ROOT_OBJ);
|
||||
|
||||
// Free plugin handle
|
||||
bbfdm_free_dotso_plugin(&cli_lib_handle);
|
||||
bbfdm_free_dotso_plugin(NULL, &cli_lib_handle);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue