From 7d5bf8e0751d3e2ec848b4da6e4a6c8366caf05c Mon Sep 17 00:00:00 2001 From: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> Date: Tue, 28 Jan 2025 08:49:49 +0000 Subject: [PATCH] Align with libbbfdm-api-v2 library --- src/Makefile | 2 +- src/fw_images.c | 4 +- src/fwbank.c | 108 +++++++++++++++++------------------ src/memory.c | 61 ++++++++++---------- src/processes.c | 89 +++++++++++++++-------------- src/reboots.c | 86 ++++++++++++---------------- src/sysmngr.c | 2 +- src/utils.c | 147 +----------------------------------------------- src/utils.h | 11 ---- 9 files changed, 169 insertions(+), 341 deletions(-) diff --git a/src/Makefile b/src/Makefile index 7393461..adb7c0b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,7 +4,7 @@ OBJS = sysmngr.o deviceinfo.o utils.o PROG_CFLAGS = $(CFLAGS) -Wall -Werror -fstrict-aliasing -g -PROG_LDFLAGS = $(LDFLAGS) -luci -lubus -lubox -lblobmsg_json -ljson-c -lssl -lcrypto -lbbfdm-ubus -lbbfdm-api +PROG_LDFLAGS = $(LDFLAGS) -luci -lubus -lubox -lblobmsg_json -ljson-c -lssl -lcrypto -lbbfdm-ubus -lbbfdm-api -lbbfdm-api-v2 FPIC := -fPIC ifeq ($(SYSMNGR_VENDOR_CONFIG_FILE),y) diff --git a/src/fw_images.c b/src/fw_images.c index 48e7c82..51dcf71 100644 --- a/src/fw_images.c +++ b/src/fw_images.c @@ -132,10 +132,10 @@ static void _exec_reboot(const void *arg1, void *arg2) sleep(3); dmubus_call_set("rpc-sys", "reboot", UBUS_ARGS{0}, 0); sleep(5); // Wait for reboot to happen - BBF_ERR("Reboot call failed with rpc-sys, trying again with system"); + BBFDM_ERR("Reboot call failed with rpc-sys, trying again with system"); dmubus_call_set("system", "reboot", UBUS_ARGS{0}, 0); sleep(5); // Wait for reboot - BBF_ERR("Reboot call failed!!!"); + BBFDM_ERR("Reboot call failed!!!"); // Set last_reboot_cause to empty because there is a problem in the system reboot dmuci_set_value(config_name, "reboots", "last_reboot_cause", ""); diff --git a/src/fwbank.c b/src/fwbank.c index fce306e..251f631 100644 --- a/src/fwbank.c +++ b/src/fwbank.c @@ -80,7 +80,7 @@ static void _sysmngr_task_finish_callback(struct uloop_process *p, int ret) task = container_of(p, struct sysmngr_task_data, process); if (task == NULL) { - BBF_ERR("Failed to decode forked task"); + BBFDM_ERR("Failed to decode forked task"); return; } @@ -137,7 +137,7 @@ static void timeout_callback(struct uloop_timeout *t) sysmngr_task_data_t *task = container_of(t, sysmngr_task_data_t, timeoutcb); if (task && task->process.pid > 0) { - BBF_ERR("Task timed out. Killing process with PID %d\n", task->process.pid); + BBFDM_ERR("Task timed out. Killing process with PID %d\n", task->process.pid); kill(task->process.pid, SIGKILL); } } @@ -153,7 +153,7 @@ static int sysmngr_task_fork(sysmngr_task_callback_t finishcb, const char *comma } if (pipe(task->pipe_fds) == -1) { - BBF_ERR("pipe failed"); + BBFDM_ERR("pipe failed"); FREE(task); return -1; } @@ -165,7 +165,7 @@ static int sysmngr_task_fork(sysmngr_task_callback_t finishcb, const char *comma child = fork(); if (child == -1) { - BBF_ERR("Failed to fork a child for task"); + BBFDM_ERR("Failed to fork a child for task"); FREE(task); return -1; } else if (child == 0) { @@ -203,7 +203,7 @@ static void fwbank_dump_timer(struct uloop_timeout *timeout) { struct fwbank_dump_data *data = NULL; - BBF_DEBUG("fwbank_dump_timer triggered"); + BBFDM_DEBUG("fwbank_dump_timer triggered"); data = container_of(timeout, struct fwbank_dump_data, tm); if (data == NULL) @@ -215,10 +215,10 @@ static void fwbank_dump_timer(struct uloop_timeout *timeout) static int free_global_fwbank_dump(struct blob_buf *fwbank_dump_bb) { if (fwbank_dump_bb->head && blob_len(fwbank_dump_bb->head)) { - BBF_DEBUG("Freeing fwbank dump blob buffer"); + BBFDM_DEBUG("Freeing fwbank dump blob buffer"); blob_buf_free(fwbank_dump_bb); } else { - BBF_DEBUG("fwbank dump blob buffer is already empty"); + BBFDM_DEBUG("fwbank dump blob buffer is already empty"); } return 0; @@ -227,20 +227,20 @@ static int free_global_fwbank_dump(struct blob_buf *fwbank_dump_bb) static int validate_global_fwbank_dump(struct blob_buf *fwbank_dump_bb) { if (!fwbank_dump_bb->head || !blob_len(fwbank_dump_bb->head)) { - BBF_ERR("fwbank dump output is empty"); + BBFDM_ERR("fwbank dump output is empty"); return -1; } - BBF_DEBUG("Validating global fwbank dump"); + BBFDM_DEBUG("Validating global fwbank dump"); struct blob_attr *tb[1] = {0}; if (blobmsg_parse(sysmngr_dump_policy, 1, tb, blobmsg_data(fwbank_dump_bb->head), blobmsg_len(fwbank_dump_bb->head))) { - BBF_ERR("Failed to parse fwbank dump blob"); + BBFDM_ERR("Failed to parse fwbank dump blob"); return -1; } if (!tb[0]) { // bank array is not found - BBF_ERR("Bank array not found in fwbank dump"); + BBFDM_ERR("Bank array not found in fwbank dump"); return -1; } @@ -251,7 +251,7 @@ static int validate_global_fwbank_dump(struct blob_buf *fwbank_dump_bb) struct blob_attr *t[8] = {0}; if (blobmsg_parse(sysmngr_bank_policy, ARRAY_SIZE(sysmngr_bank_policy), t, blobmsg_data(entry), blobmsg_len(entry))) { - BBF_ERR("Failed to parse bank entry"); + BBFDM_ERR("Failed to parse bank entry"); continue; } @@ -262,31 +262,31 @@ static int validate_global_fwbank_dump(struct blob_buf *fwbank_dump_bb) } } - BBF_DEBUG("Global fwbank dump validation passed"); + BBFDM_DEBUG("Global fwbank dump validation passed"); return valid; } static void fwbank_dump_finish_callback(struct ubus_context *ctx, struct ubus_request_data *req, int *pipe_fds, uint32_t bank_id) { - BBF_DEBUG("Task finished Line=%d && func=%s", __LINE__, __func__); + BBFDM_DEBUG("Task finished Line=%d && func=%s", __LINE__, __func__); close(pipe_fds[1]); // Close unused write end char buffer[1024] = {0}; ssize_t bytes_read; - BBF_DEBUG("Reading script output..."); + BBFDM_DEBUG("Reading script output..."); // Read the output from the script while ((bytes_read = read(pipe_fds[0], buffer, sizeof(buffer) - 1)) > 0) { buffer[bytes_read] = '\0'; // Null-terminate the buffer - BBF_DEBUG("Script output: %s", buffer); + BBFDM_DEBUG("Script output: %s", buffer); } close(pipe_fds[0]); // Close read end if (bytes_read < 0 || strlen(buffer) == 0) { - BBF_ERR("Failed to read from pipe"); + BBFDM_ERR("Failed to read from pipe"); goto retry; } @@ -297,14 +297,14 @@ static void fwbank_dump_finish_callback(struct ubus_context *ctx, struct ubus_re blob_buf_init(&temp_buf, 0); if (!blobmsg_add_json_from_string(&temp_buf, buffer)) { - BBF_ERR("Invalid JSON format in buffer"); + BBFDM_ERR("Invalid JSON format in buffer"); blob_buf_free(&temp_buf); goto retry; } int res = validate_global_fwbank_dump(&temp_buf); if (res) { - BBF_ERR("Failed to validate 'fwbank' output"); + BBFDM_ERR("Failed to validate 'fwbank' output"); blob_buf_free(&temp_buf); goto retry; } @@ -328,25 +328,25 @@ retry: g_retry_count++; uloop_timeout_set(&g_fwbank_dump.tm, FWBANK_DUMP_RETRY_DELAY * 1000); - BBF_ERR("Attempt %d/%d: fwbank dump blob buf is empty. Retrying in %d second(s)...", + BBFDM_ERR("Attempt %d/%d: fwbank dump blob buf is empty. Retrying in %d second(s)...", g_retry_count, FWBANK_DUMP_MAX_RETRIES, FWBANK_DUMP_RETRY_DELAY); } else { - BBF_ERR("Max retries (%d) reached: The fwbank dump buffer is empty. Unable to register 'fwbank' ubus object", + BBFDM_ERR("Max retries (%d) reached: The fwbank dump buffer is empty. Unable to register 'fwbank' ubus object", FWBANK_DUMP_MAX_RETRIES); } } static int init_global_fwbank_dump(void) { - BBF_DEBUG("Initializing global fwbank dump"); + BBFDM_DEBUG("Initializing global fwbank dump"); int res = sysmngr_task_fork(fwbank_dump_finish_callback, FWBANK_DUMP_CMD, 120, NULL, 0); if (res) { - BBF_ERR("Failed to start task for fwbank dump command"); + BBFDM_ERR("Failed to start task for fwbank dump command"); return -1; } - BBF_DEBUG("fwbank dump blob initialized successfully"); + BBFDM_DEBUG("fwbank dump blob initialized successfully"); return 0; } @@ -354,7 +354,7 @@ static void fwbank_listen_timeout(struct uloop_timeout *timeout) { struct fwbank_event_data *data = NULL; - BBF_DEBUG("fwbank listen timeout triggered"); + BBFDM_DEBUG("fwbank listen timeout triggered"); data = container_of(timeout, struct fwbank_event_data, tm); if (data == NULL) @@ -372,7 +372,7 @@ static void fwbank_receive_sysupgrade(struct ubus_context *ctx, struct ubus_even int rem; if (!msg || !ev) { - BBF_ERR("Invalid event data in sysupgrade handler"); + BBFDM_ERR("Invalid event data in sysupgrade handler"); return; } @@ -381,7 +381,7 @@ static void fwbank_receive_sysupgrade(struct ubus_context *ctx, struct ubus_even return; if (data->band_id < 0) { // bank_id should be a valid id - BBF_ERR("Invalid bank_id: %d", data->band_id); + BBFDM_ERR("Invalid bank_id: %d", data->band_id); return; } @@ -391,7 +391,7 @@ static void fwbank_receive_sysupgrade(struct ubus_context *ctx, struct ubus_even if (DM_STRCMP("bank_id", blobmsg_name(cur)) == 0) { char *attr_val = (char *)blobmsg_data(cur); if (DM_STRCMP(attr_val, bank_id_str) != 0) { - BBF_ERR("Mismatched bank_id (%s != %s)", attr_val, bank_id_str); + BBFDM_ERR("Mismatched bank_id (%s != %s)", attr_val, bank_id_str); return; } } @@ -400,12 +400,12 @@ static void fwbank_receive_sysupgrade(struct ubus_context *ctx, struct ubus_even char *attr_val = (char *)blobmsg_data(cur); if (DM_STRCMP(attr_val, "Downloading") == 0) { - BBF_DEBUG("Sysupgrade status: Downloading"); + BBFDM_DEBUG("Sysupgrade status: Downloading"); return; } if (DM_STRCMP(attr_val, "Available") == 0) { - BBF_DEBUG("Sysupgrade status: Available. Refreshing fwbank dump."); + BBFDM_DEBUG("Sysupgrade status: Available. Refreshing fwbank dump."); init_global_fwbank_dump(); break; } @@ -429,7 +429,7 @@ static struct fwbank_event_data g_fwbank_event_data = { static void fwbank_wait_for_sysupgrade_event(struct ubus_context *ctx, uint32_t band_id) { - BBF_DEBUG("Waiting for sysupgrade event for bank_id: %u", band_id); + BBFDM_DEBUG("Waiting for sysupgrade event for bank_id: %u", band_id); g_fwbank_event_data.ctx = ctx; g_fwbank_event_data.band_id = band_id; @@ -448,7 +448,7 @@ static bool is_set_bootbank_success(struct blob_buf *output_bb) // Parse the blob buffer for the "success" field if (blobmsg_parse(&policy, 1, &tb, blobmsg_data(output_bb->head), blobmsg_len(output_bb->head)) != 0) { - BBF_ERR("Failed to parse blobmsg data"); + BBFDM_ERR("Failed to parse blobmsg data"); return false; } @@ -469,7 +469,7 @@ static bool is_upgrade_success(struct blob_buf *output_bb) // Parse the blob buffer for the "result" field if (blobmsg_parse(&policy, 1, &tb, blobmsg_data(output_bb->head), blobmsg_len(output_bb->head)) != 0) { - BBF_ERR("Failed to parse blobmsg data"); + BBFDM_ERR("Failed to parse blobmsg data"); return false; } @@ -487,25 +487,25 @@ struct blob_buf *sysmngr_fwbank_dump(void) static void fwbank_set_bootbank_finish_callback(struct ubus_context *ctx, struct ubus_request_data *req, int *pipe_fds, uint32_t bank_id) { - BBF_DEBUG("Task finished Line=%d && func=%s", __LINE__, __func__); + BBFDM_DEBUG("Task finished Line=%d && func=%s", __LINE__, __func__); close(pipe_fds[1]); // Close unused write end char buffer[1024] = {0}; ssize_t bytes_read; - BBF_DEBUG("Reading script output..."); + BBFDM_DEBUG("Reading script output..."); // Read the output from the script while ((bytes_read = read(pipe_fds[0], buffer, sizeof(buffer) - 1)) > 0) { buffer[bytes_read] = '\0'; // Null-terminate the buffer - BBF_DEBUG("Script output: %s", buffer); + BBFDM_DEBUG("Script output: %s", buffer); } close(pipe_fds[0]); // Close read end if (bytes_read < 0) { - BBF_ERR("Failed to read from pipe"); + BBFDM_ERR("Failed to read from pipe"); return; } @@ -515,7 +515,7 @@ static void fwbank_set_bootbank_finish_callback(struct ubus_context *ctx, struct blob_buf_init(&setbootbank_bb, 0); if (!blobmsg_add_json_from_string(&setbootbank_bb, buffer)) { - BBF_ERR("Failed to create blob buf"); + BBFDM_ERR("Failed to create blob buf"); blob_buf_free(&setbootbank_bb); return; } @@ -523,7 +523,7 @@ static void fwbank_set_bootbank_finish_callback(struct ubus_context *ctx, struct bool is_success = is_set_bootbank_success(&setbootbank_bb); if (ctx && req) { - BBF_DEBUG("Send ubus output"); + BBFDM_DEBUG("Send ubus output"); ubus_send_reply(ctx, req, setbootbank_bb.head); } @@ -545,7 +545,7 @@ int sysmngr_fwbank_set_bootbank(uint32_t bank_id, struct ubus_request_data *req) int res = sysmngr_task_fork(fwbank_set_bootbank_finish_callback, cmd, 10, req, 0); if (res) { - BBF_ERR("Failed to start task for fwbank set bootbank command"); + BBFDM_ERR("Failed to start task for fwbank set bootbank command"); return -1; } @@ -554,25 +554,25 @@ int sysmngr_fwbank_set_bootbank(uint32_t bank_id, struct ubus_request_data *req) static void fwbank_upgrade_finish_callback(struct ubus_context *ctx, struct ubus_request_data *req, int *pipe_fds, uint32_t bank_id) { - BBF_DEBUG("Task finished Line=%d && func=%s", __LINE__, __func__); + BBFDM_DEBUG("Task finished Line=%d && func=%s", __LINE__, __func__); close(pipe_fds[1]); // Close unused write end char buffer[1024] = {0}; ssize_t bytes_read; - BBF_DEBUG("Reading script output..."); + BBFDM_DEBUG("Reading script output..."); // Read the output from the script while ((bytes_read = read(pipe_fds[0], buffer, sizeof(buffer) - 1)) > 0) { buffer[bytes_read] = '\0'; // Null-terminate the buffer - BBF_DEBUG("Script output: %s", buffer); + BBFDM_DEBUG("Script output: %s", buffer); } close(pipe_fds[0]); // Close read end if (bytes_read < 0) { - BBF_ERR("Failed to read from pipe"); + BBFDM_ERR("Failed to read from pipe"); return; } @@ -582,7 +582,7 @@ static void fwbank_upgrade_finish_callback(struct ubus_context *ctx, struct ubus blob_buf_init(&upgrade_bb, 0); if (!blobmsg_add_json_from_string(&upgrade_bb, buffer)) { - BBF_ERR("Failed to create blob buf"); + BBFDM_ERR("Failed to create blob buf"); blob_buf_free(&upgrade_bb); return; } @@ -590,7 +590,7 @@ static void fwbank_upgrade_finish_callback(struct ubus_context *ctx, struct ubus bool is_success = is_upgrade_success(&upgrade_bb); if (ctx && req) { - BBF_DEBUG("Send ubus output"); + BBFDM_DEBUG("Send ubus output"); ubus_send_reply(ctx, req, upgrade_bb.head); } @@ -614,7 +614,7 @@ int sysmngr_fwbank_upgrade(const char *path, bool auto_activate, uint32_t bank_i int res = sysmngr_task_fork(fwbank_upgrade_finish_callback, cmd, 10, req, bank_id); if (res) { - BBF_ERR("Failed to start task for fwbank upgrade command"); + BBFDM_ERR("Failed to start task for fwbank upgrade command"); return -1; } @@ -647,7 +647,7 @@ static int set_bootbank_handler(struct ubus_context *ctx, struct ubus_object *ob int res = 0; if (blobmsg_parse(set_bootbank_policy, __SET_BOOT_MAX, tb, blob_data(msg), blob_len(msg))) { - BBF_ERR("Failed to parse the 'set_bootbank' message"); + BBFDM_ERR("Failed to parse the 'set_bootbank' message"); return UBUS_STATUS_UNKNOWN_ERROR; } @@ -696,7 +696,7 @@ static int upgrade_handler(struct ubus_context *ctx, struct ubus_object *obj, int res = 0; if (blobmsg_parse(upgrade_policy, __UPGRADE_MAX, tb, blob_data(msg), blob_len(msg))) { - BBF_ERR("Failed to parse the 'upgrade' message"); + BBFDM_ERR("Failed to parse the 'upgrade' message"); return UBUS_STATUS_UNKNOWN_ERROR; } @@ -746,11 +746,11 @@ int sysmngr_register_fwbank(struct ubus_context *ubus_ctx) { int res = ubus_add_object(ubus_ctx, &fwbank_object); if (res) { - BBF_ERR("Failed to register 'fwbank' ubus object!!!!!!"); + BBFDM_ERR("Failed to register 'fwbank' ubus object!!!!!!"); return -1; } - BBF_INFO("'fwbank' ubus object was registered"); + BBFDM_INFO("'fwbank' ubus object was registered"); return res; } @@ -758,7 +758,7 @@ int sysmngr_unregister_fwbank(struct ubus_context *ubus_ctx) { ubus_remove_object(ubus_ctx, &fwbank_object); - BBF_INFO("'fwbank' ubus object was unregistered, and resources were freed"); + BBFDM_INFO("'fwbank' ubus object was unregistered, and resources were freed"); return 0; } @@ -770,13 +770,13 @@ int sysmngr_init_fwbank_dump(struct ubus_context *ubus_ctx) g_fwbank_dump.tm.cb = fwbank_dump_timer; if (!file_exists(FWBANK_FILE_PATH)) { - BBF_ERR("The fwbank file (%s) is missing", FWBANK_FILE_PATH); + BBFDM_ERR("The fwbank file (%s) is missing", FWBANK_FILE_PATH); return -1; } res = init_global_fwbank_dump(); if (res) { - BBF_ERR("Failed to fetch 'fwbank' output or no data available"); + BBFDM_ERR("Failed to fetch 'fwbank' output or no data available"); return -1; } diff --git a/src/memory.c b/src/memory.c index 54e5ecd..9cbc4be 100644 --- a/src/memory.c +++ b/src/memory.c @@ -11,6 +11,8 @@ #include "utils.h" +#include <libbbfdm-api/bbfdm_api.h> + #define DEFAULT_POLLING_INTERVAL "60" #define DEFAULT_CRITICAL_RISE_THRESHOLD "80" #define DEFAULT_CRITICAL_FALL_THRESHOLD "60" @@ -49,7 +51,7 @@ int sysmngr_meminfo(mem_info *info) // cppcheck-suppress cert-MSC24-C if ((f = fopen("/proc/meminfo", "r")) == NULL) { - BBF_ERR("Failed to open '/proc/meminfo' for reading memory info."); + BBFDM_ERR("Failed to open '/proc/meminfo' for reading memory info."); return -1; } @@ -81,7 +83,7 @@ static unsigned int calculate_memory_utilization(void) mem_info info = {0}; if (sysmngr_meminfo(&info) != 0) { - BBF_ERR("Failed to retrieve memory information for utilization calculation"); + BBFDM_ERR("Failed to retrieve memory information for utilization calculation"); return 0; } @@ -112,11 +114,8 @@ static void send_memory_critical_state_event(unsigned int mem_utilization) blobmsg_close_array(&bb, arr); - if (sysmngr_ubus_invoke_sync("bbfdm", "notify_event", bb.head, NULL, NULL)) { - BBF_ERR("Failed to send 'MemoryCriticalState!' event"); - } else { - BBF_DEBUG("'MemoryCriticalState!' event sent successfully with utilization at %u%%.", mem_utilization); - } + BBFDM_UBUS_INVOKE_SYNC("bbfdm", "notify_event", bb.head, 5000, NULL, NULL); + BBFDM_DEBUG("'MemoryCriticalState!' event sent successfully with utilization at %u%%.", mem_utilization); blob_buf_free(&bb); } @@ -129,12 +128,12 @@ static void run_memory_monitor(void) if ((mem_utilization > g_memory_ctx.critical_rise_threshold) && (g_memory_ctx.critical_fall_time >= g_memory_ctx.critical_rise_time)) { - BBF_ERR("Memory utilization reached critical threshold: %u%% !!!!!!!!", mem_utilization); + BBFDM_ERR("Memory utilization reached critical threshold: %u%% !!!!!!!!", mem_utilization); // Update CriticalRiseTimeStamp to the current time g_memory_ctx.critical_rise_time = time(NULL); snprintf(buf, sizeof(buf), "%ld", (long int)g_memory_ctx.critical_rise_time); - sysmngr_uci_set("sysmngr", "memory", "critical_rise_time", buf); + BBFDM_UCI_SET("sysmngr", "memory", "critical_rise_time", buf); if (g_memory_ctx.enable_critical_log) { // Generate log into the vendor log file referenced by 'VendorLogFileRef' parameter indicating critical condition is reached @@ -148,12 +147,12 @@ static void run_memory_monitor(void) if ((mem_utilization < g_memory_ctx.critical_fall_threshold) && (g_memory_ctx.critical_rise_time > g_memory_ctx.critical_fall_time)) { - BBF_ERR("Memory utilization has fallen below critical threshold: %u%% !!!!!!!!", mem_utilization); + BBFDM_ERR("Memory utilization has fallen below critical threshold: %u%% !!!!!!!!", mem_utilization); // Update CriticalFallTimeStamp to the current time g_memory_ctx.critical_fall_time = time(NULL); snprintf(buf, sizeof(buf), "%ld", (long int)g_memory_ctx.critical_fall_time); - sysmngr_uci_set("sysmngr", "memory", "critical_fall_time", buf); + BBFDM_UCI_SET("sysmngr", "memory", "critical_fall_time", buf); if (g_memory_ctx.enable_critical_log) { // Generate log into the vendor log file referenced by 'VendorLogFileRef' parameter indicating that the critical condition is no longer present @@ -161,7 +160,7 @@ static void run_memory_monitor(void) } } - BBF_INFO("Next memory monitor check scheduled in %d sec...", g_memory_ctx.polling_interval); + BBFDM_INFO("Next memory monitor check scheduled in %d sec...", g_memory_ctx.polling_interval); uloop_timeout_set(&g_memory_ctx.memory_timer, g_memory_ctx.polling_interval * 1000); } @@ -178,36 +177,36 @@ static void fill_global_memory_ctx(void) g_memory_ctx.memory_timer.cb = memory_timer_callback; - sysmngr_uci_get("sysmngr", "memory", "enable", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "memory", "enable", "0", buf, sizeof(buf)); g_memory_ctx.enable = ((int)strtol(buf, NULL, 10) != 0); - BBF_DEBUG("Memory Monitor Config: |Enable| |%d|", g_memory_ctx.enable); + BBFDM_DEBUG("Memory Monitor Config: |Enable| |%d|", g_memory_ctx.enable); - sysmngr_uci_get("sysmngr", "memory", "enable_critical_log", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "memory", "enable_critical_log", "0", buf, sizeof(buf)); g_memory_ctx.enable_critical_log = ((int)strtol(buf, NULL, 10) != 0); - BBF_DEBUG("Memory Monitor Config: |EnableCriticalLog| |%d|", g_memory_ctx.enable_critical_log); + BBFDM_DEBUG("Memory Monitor Config: |EnableCriticalLog| |%d|", g_memory_ctx.enable_critical_log); - sysmngr_uci_get("sysmngr", "memory", "polling_interval", DEFAULT_POLLING_INTERVAL, buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "memory", "polling_interval", DEFAULT_POLLING_INTERVAL, buf, sizeof(buf)); g_memory_ctx.polling_interval = strtoul(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |PollingInterval| |%lu|", g_memory_ctx.polling_interval); + BBFDM_DEBUG("Memory Monitor Config: |PollingInterval| |%lu|", g_memory_ctx.polling_interval); - sysmngr_uci_get("sysmngr", "memory", "critical_rise_threshold", DEFAULT_CRITICAL_RISE_THRESHOLD, buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "memory", "critical_rise_threshold", DEFAULT_CRITICAL_RISE_THRESHOLD, buf, sizeof(buf)); g_memory_ctx.critical_rise_threshold = strtoul(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |CriticalRiseThreshold| |%lu|", g_memory_ctx.critical_rise_threshold); + BBFDM_DEBUG("Memory Monitor Config: |CriticalRiseThreshold| |%lu|", g_memory_ctx.critical_rise_threshold); - sysmngr_uci_get("sysmngr", "memory", "critical_fall_threshold", DEFAULT_CRITICAL_FALL_THRESHOLD, buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "memory", "critical_fall_threshold", DEFAULT_CRITICAL_FALL_THRESHOLD, buf, sizeof(buf)); g_memory_ctx.critical_fall_threshold = strtoul(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |CriticalFallThreshold| |%lu|", g_memory_ctx.critical_fall_threshold); + BBFDM_DEBUG("Memory Monitor Config: |CriticalFallThreshold| |%lu|", g_memory_ctx.critical_fall_threshold); - sysmngr_uci_get("sysmngr", "memory", "critical_rise_time", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "memory", "critical_rise_time", "0", buf, sizeof(buf)); g_memory_ctx.critical_rise_time = strtol(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |CriticalRiseTimeStamp| |%lu|", g_memory_ctx.critical_rise_time); + BBFDM_DEBUG("Memory Monitor Config: |CriticalRiseTimeStamp| |%lu|", g_memory_ctx.critical_rise_time); - sysmngr_uci_get("sysmngr", "memory", "critical_fall_time", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "memory", "critical_fall_time", "0", buf, sizeof(buf)); g_memory_ctx.critical_fall_time = strtol(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |CriticalFallTimeStamp| |%lu|", g_memory_ctx.critical_fall_time); + BBFDM_DEBUG("Memory Monitor Config: |CriticalFallTimeStamp| |%lu|", g_memory_ctx.critical_fall_time); - sysmngr_uci_get("sysmngr", "memory", "file_path", DEFAULT_CRITICAL_MEMORY_LOG_PATH, g_memory_ctx.log_file, sizeof(g_memory_ctx.log_file)); - BBF_DEBUG("Memory Monitor Config: |FilePath| |%s|", g_memory_ctx.log_file); + BBFDM_UCI_GET("sysmngr", "memory", "file_path", DEFAULT_CRITICAL_MEMORY_LOG_PATH, g_memory_ctx.log_file, sizeof(g_memory_ctx.log_file)); + BBFDM_DEBUG("Memory Monitor Config: |FilePath| |%s|", g_memory_ctx.log_file); if (!file_exists(g_memory_ctx.log_file)) { // Create empty file if it doesn't exist create_empty_file(g_memory_ctx.log_file); @@ -222,18 +221,18 @@ void sysmngr_memory_init(void) fill_global_memory_ctx(); if (!g_memory_ctx.enable) { - BBF_INFO("Memory monitoring is disabled."); + BBFDM_INFO("Memory monitoring is disabled."); return; } - BBF_INFO("Memory monitoring is enabled"); + BBFDM_INFO("Memory monitoring is enabled"); run_memory_monitor(); } void sysmngr_memory_clean(void) { uloop_timeout_cancel(&g_memory_ctx.memory_timer); - BBF_INFO("Memory monitoring process stopped"); + BBFDM_INFO("Memory monitoring process stopped"); } /************************************************************* diff --git a/src/processes.c b/src/processes.c index 09a7893..f1a1dc0 100644 --- a/src/processes.c +++ b/src/processes.c @@ -12,6 +12,8 @@ #include "utils.h" #include "processes.h" +#include <libbbfdm-api/bbfdm_api.h> + #define DEFAULT_CPU_NAME "cpu" #define DEFAULT_CPU_POLL_INTERVAL "5" #define DEFAULT_CPU_NUM_SAMPLES "30" @@ -222,7 +224,7 @@ static void broadcast_add_del_event(int diff) snprintf(obj_path, sizeof(obj_path), "Device.DeviceInfo.ProcessStatus.Process.%d", (diff > 0) ? g_process_ctx.process_num + i + 1 : g_process_ctx.process_num - i); blobmsg_add_string(&bb, NULL, obj_path); - BBF_DEBUG("#%s:: %s #", (diff > 0) ? "Add" : "Del", obj_path); + BBFDM_DEBUG("#%s:: %s #", (diff > 0) ? "Add" : "Del", obj_path); } blobmsg_close_array(&bb, a); @@ -243,7 +245,7 @@ static void init_process_list(void) if (dir == NULL) return; - BBF_INFO("Init process list"); + BBFDM_INFO("Init process list"); while ((entry = readdir(dir)) != NULL) { struct stat stats = {0}; @@ -303,7 +305,7 @@ static void init_process_list(void) process_entry *pentry = (process_entry *)calloc(1, sizeof(process_entry)); if (!pentry) { - BBF_ERR("failed to allocate memory for process entry"); + BBFDM_ERR("failed to allocate memory for process entry"); return; } @@ -331,7 +333,7 @@ static void free_process_list(void) { process_entry *entry = NULL, *tmp = NULL; - BBF_INFO("Free process list"); + BBFDM_INFO("Free process list"); list_for_each_entry_safe(entry, tmp, &g_process_ctx.list, list) { list_del(&entry->list); @@ -343,7 +345,7 @@ static int get_instance_refresh_interval(void) { char buf[8] = {0}; - sysmngr_uci_get("sysmngr", "process", "instance_refresh_interval", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "process", "instance_refresh_interval", "0", buf, sizeof(buf)); return (int)strtol(buf, NULL, 10); } @@ -354,14 +356,14 @@ static void run_refresh_process_list(void) init_process_list(); if (g_process_ctx.refresh_interval > 0) { - BBF_INFO("Scheduling process list update after %d sec...", g_process_ctx.refresh_interval); + BBFDM_INFO("Scheduling process list update after %d sec...", g_process_ctx.refresh_interval); uloop_timeout_set(&g_process_ctx.instance_timer, g_process_ctx.refresh_interval * 1000); } } static void ubus_call_complete_cb(struct ubus_request *req, int ret) { - BBF_DEBUG("'tr069' ubus callback completed"); + BBFDM_DEBUG("'tr069' ubus callback completed"); run_refresh_process_list(); FREE(req); } @@ -373,14 +375,14 @@ static void process_refresh_instance_timer(struct uloop_timeout *timeout) memset(&bb, 0, sizeof(struct blob_buf)); blob_buf_init(&bb, 0); - int res = sysmngr_ubus_invoke_async(g_process_ctx.ubus_ctx, "tr069", "status", bb.head, NULL, ubus_call_complete_cb); + int res = bbfdm_ubus_invoke_async(g_process_ctx.ubus_ctx, "tr069", "status", bb.head, NULL, ubus_call_complete_cb); blob_buf_free(&bb); if (res) { - BBF_DEBUG("Update process list: 'tr069' ubus object not found"); + BBFDM_DEBUG("Update process list: 'tr069' ubus object not found"); run_refresh_process_list(); } else { - BBF_DEBUG("Process list will be updated after 'tr069' ubus session completes"); + BBFDM_DEBUG("Process list will be updated after 'tr069' ubus session completes"); } } @@ -412,11 +414,8 @@ static void send_cpu_critical_state_event(unsigned int cpu_utilization) blobmsg_close_array(&bb, arr); - if (sysmngr_ubus_invoke_sync("bbfdm", "notify_event", bb.head, NULL, NULL)) { - BBF_ERR("Failed to send 'CPUCriticalState!' event"); - } else { - BBF_DEBUG("'CPUCriticalState!' event sent successfully with utilization at %u%%.", cpu_utilization); - } + BBFDM_UBUS_INVOKE_SYNC("bbfdm", "notify_event", bb.head, 5000, NULL, NULL); + BBFDM_DEBUG("'CPUCriticalState!' event sent successfully with utilization at %u%%.", cpu_utilization); blob_buf_free(&bb); } @@ -468,12 +467,12 @@ static void run_cpu_monitor(void) if ((avg_utilization > g_cpu_info.critical_rise_threshold) && (g_cpu_info.critical_fall_time >= g_cpu_info.critical_rise_time)) { - BBF_ERR("CPU utilization reached critical threshold: %u%% !!!!!!!!", avg_utilization); + BBFDM_ERR("CPU utilization reached critical threshold: %u%% !!!!!!!!", avg_utilization); // Update CriticalRiseTimeStamp to the current time g_cpu_info.critical_rise_time = time(NULL); snprintf(buf, sizeof(buf), "%ld", (long int)g_cpu_info.critical_rise_time); - sysmngr_uci_set("sysmngr", "cpu", "critical_rise_time", buf); + BBFDM_UCI_SET("sysmngr", "cpu", "critical_rise_time", buf); if (g_cpu_info.enable_critical_log) { // Generate log into the vendor log file referenced by 'VendorLogFileRef' parameter indicating critical condition is reached @@ -487,12 +486,12 @@ static void run_cpu_monitor(void) if ((avg_utilization < g_cpu_info.critical_fall_threshold) && (g_cpu_info.critical_rise_time > g_cpu_info.critical_fall_time)) { - BBF_ERR("CPU utilization has fallen below critical threshold: %u%% !!!!!!!!", avg_utilization); + BBFDM_ERR("CPU utilization has fallen below critical threshold: %u%% !!!!!!!!", avg_utilization); // Update CriticalFallTimeStamp to the current time g_cpu_info.critical_fall_time = time(NULL); snprintf(buf, sizeof(buf), "%ld", (long int)g_cpu_info.critical_fall_time); - sysmngr_uci_set("sysmngr", "cpu", "critical_fall_time", buf); + BBFDM_UCI_SET("sysmngr", "cpu", "critical_fall_time", buf); if (g_cpu_info.enable_critical_log) { // Generate log into the vendor log file referenced by 'VendorLogFileRef' parameter indicating that the critical condition is no longer present @@ -500,7 +499,7 @@ static void run_cpu_monitor(void) } } - BBF_INFO("Next memory monitor check scheduled in %d sec...", g_cpu_info.poll_interval); + BBFDM_INFO("Next memory monitor check scheduled in %d sec...", g_cpu_info.poll_interval); uloop_timeout_set(&g_cpu_info.cpu_timer, g_cpu_info.poll_interval * 1000); } @@ -517,40 +516,40 @@ static int fill_global_cpu_info(void) g_cpu_info.cpu_timer.cb = cpu_timer_callback; - sysmngr_uci_get("sysmngr", "cpu", "enable", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "cpu", "enable", "0", buf, sizeof(buf)); g_cpu_info.enable = ((int)strtol(buf, NULL, 10) != 0); - BBF_DEBUG("Memory Monitor Config: |Enable| |%d|", g_cpu_info.enable); + BBFDM_DEBUG("Memory Monitor Config: |Enable| |%d|", g_cpu_info.enable); - sysmngr_uci_get("sysmngr", "cpu", "enable_critical_log", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "cpu", "enable_critical_log", "0", buf, sizeof(buf)); g_cpu_info.enable_critical_log = ((int)strtol(buf, NULL, 10) != 0); - BBF_DEBUG("Memory Monitor Config: |EnableCriticalLog| |%d|", g_cpu_info.enable_critical_log); + BBFDM_DEBUG("Memory Monitor Config: |EnableCriticalLog| |%d|", g_cpu_info.enable_critical_log); - sysmngr_uci_get("sysmngr", "cpu", "poll_interval", DEFAULT_CPU_POLL_INTERVAL, buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "cpu", "poll_interval", DEFAULT_CPU_POLL_INTERVAL, buf, sizeof(buf)); g_cpu_info.poll_interval = strtoul(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |PollInterval| |%lu|", g_cpu_info.poll_interval); + BBFDM_DEBUG("Memory Monitor Config: |PollInterval| |%lu|", g_cpu_info.poll_interval); - sysmngr_uci_get("sysmngr", "cpu", "num_samples", DEFAULT_CPU_NUM_SAMPLES, buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "cpu", "num_samples", DEFAULT_CPU_NUM_SAMPLES, buf, sizeof(buf)); g_cpu_info.num_samples = strtoul(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |NumSamples| |%lu|", g_cpu_info.num_samples); + BBFDM_DEBUG("Memory Monitor Config: |NumSamples| |%lu|", g_cpu_info.num_samples); - sysmngr_uci_get("sysmngr", "cpu", "critical_rise_threshold", DEFAULT_CPU_CRITICAL_RISE_THRESHOLD, buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "cpu", "critical_rise_threshold", DEFAULT_CPU_CRITICAL_RISE_THRESHOLD, buf, sizeof(buf)); g_cpu_info.critical_rise_threshold = strtoul(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |CriticalRiseThreshold| |%lu|", g_cpu_info.critical_rise_threshold); + BBFDM_DEBUG("Memory Monitor Config: |CriticalRiseThreshold| |%lu|", g_cpu_info.critical_rise_threshold); - sysmngr_uci_get("sysmngr", "cpu", "critical_fall_threshold", DEFAULT_CPU_CRITICAL_FALL_THRESHOLD, buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "cpu", "critical_fall_threshold", DEFAULT_CPU_CRITICAL_FALL_THRESHOLD, buf, sizeof(buf)); g_cpu_info.critical_fall_threshold = strtoul(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |CriticalFallThreshold| |%lu|", g_cpu_info.critical_fall_threshold); + BBFDM_DEBUG("Memory Monitor Config: |CriticalFallThreshold| |%lu|", g_cpu_info.critical_fall_threshold); - sysmngr_uci_get("sysmngr", "cpu", "critical_rise_time", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "cpu", "critical_rise_time", "0", buf, sizeof(buf)); g_cpu_info.critical_rise_time = strtol(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |CriticalRiseTimeStamp| |%lu|", g_cpu_info.critical_rise_time); + BBFDM_DEBUG("Memory Monitor Config: |CriticalRiseTimeStamp| |%lu|", g_cpu_info.critical_rise_time); - sysmngr_uci_get("sysmngr", "cpu", "critical_fall_time", "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "cpu", "critical_fall_time", "0", buf, sizeof(buf)); g_cpu_info.critical_fall_time = strtol(buf, NULL, 10); - BBF_DEBUG("Memory Monitor Config: |CriticalFallTimeStamp| |%lu|", g_cpu_info.critical_fall_time); + BBFDM_DEBUG("Memory Monitor Config: |CriticalFallTimeStamp| |%lu|", g_cpu_info.critical_fall_time); - sysmngr_uci_get("sysmngr", "cpu", "file_path", DEFAULT_CPU_CRITICAL_LOG_PATH, g_cpu_info.log_file, sizeof(g_cpu_info.log_file)); - BBF_DEBUG("Memory Monitor Config: |FilePath| |%s|", g_cpu_info.log_file); + BBFDM_UCI_GET("sysmngr", "cpu", "file_path", DEFAULT_CPU_CRITICAL_LOG_PATH, g_cpu_info.log_file, sizeof(g_cpu_info.log_file)); + BBFDM_DEBUG("Memory Monitor Config: |FilePath| |%s|", g_cpu_info.log_file); if (!file_exists(g_cpu_info.log_file)) { // Create empty file if it doesn't exist create_empty_file(g_cpu_info.log_file); @@ -562,7 +561,7 @@ static int fill_global_cpu_info(void) g_cpu_info.idle_utilization_samples = calloc(g_cpu_info.num_samples, sizeof(unsigned int)); if (!g_cpu_info.utilization_samples || !g_cpu_info.user_utilization_samples || !g_cpu_info.system_utilization_samples || !g_cpu_info.idle_utilization_samples) { - BBF_ERR("Failed to allocate memory for mode utilization samples"); + BBFDM_ERR("Failed to allocate memory for mode utilization samples"); return -1; } @@ -602,18 +601,18 @@ void sysmngr_cpu_init(void) { int res = fill_global_cpu_info(); if (res) { - BBF_ERR("Can't start CPU monitoring!!"); + BBFDM_ERR("Can't start CPU monitoring!!"); return; } if (!g_cpu_info.enable) { - BBF_INFO("CPU monitoring is disabled."); + BBFDM_INFO("CPU monitoring is disabled."); return; } else { - BBF_INFO("CPU monitoring is enabled"); + BBFDM_INFO("CPU monitoring is enabled"); } - BBF_INFO("Next CPU monitor check scheduled in %d sec...", g_cpu_info.poll_interval); + BBFDM_INFO("Next CPU monitor check scheduled in %d sec...", g_cpu_info.poll_interval); uloop_timeout_set(&g_cpu_info.cpu_timer, g_cpu_info.poll_interval * 1000); } @@ -621,7 +620,7 @@ void sysmngr_cpu_clean(void) { free_global_cpu_info(); uloop_timeout_cancel(&g_cpu_info.cpu_timer); - BBF_INFO("CPU monitoring process stopped"); + BBFDM_INFO("CPU monitoring process stopped"); } /************************************************************* @@ -635,7 +634,7 @@ static int browseProcessEntriesInst(struct dmctx *dmctx, DMNODE *parent_node, vo int id = 0; if (g_process_ctx.refresh_interval <= 0) { - BBF_INFO("Scheduling process list update after 2 sec..."); + BBFDM_INFO("Scheduling process list update after 2 sec..."); uloop_timeout_set(&g_process_ctx.instance_timer, 2 * 1000); } diff --git a/src/reboots.c b/src/reboots.c index 414281e..684160f 100644 --- a/src/reboots.c +++ b/src/reboots.c @@ -19,6 +19,8 @@ #include <time.h> #include <unistd.h> +#include <libbbfdm-api/bbfdm_api.h> + #define REBOOT_LOCK_FILE "/tmp/bbf_reboot_handler.lock" // Lock file indicating that the boot action has already been executed #define RESET_REASON_PATH "/var/reset_reason" // Path to the file containing the reason for the most recent boot/reset #define REBOOT_MAX_RETRIES 15 // Maximum number of retries for checking if RESET_REASON_PATH has been generated @@ -29,19 +31,19 @@ static int g_retry_count = 0; static void reset_option_counter(const char *option_name, const char *option_value) { - sysmngr_uci_set("sysmngr", "reboots", option_name, option_value); + BBFDM_UCI_SET("sysmngr", "reboots", option_name, option_value); } static void increment_option_counter(const char *option_name) { char buf[16] = {0}; - sysmngr_uci_get("sysmngr", "reboots", option_name, "0", buf, sizeof(buf)); + BBFDM_UCI_GET("sysmngr", "reboots", option_name, "0", buf, sizeof(buf)); int counter = (int)strtol(buf, NULL, 10) + 1; snprintf(buf, sizeof(buf), "%d", counter); - sysmngr_uci_set("sysmngr", "reboots", option_name, buf); + BBFDM_UCI_SET("sysmngr", "reboots", option_name, buf); } static void get_boot_option_value(const char *option_name, char *buffer, size_t buffer_size) @@ -109,50 +111,32 @@ static void calculate_boot_time(char *buffer, size_t buffer_size) static void delete_excess_reboot_sections(int max_reboot_entries) { - struct uci_ptr ptr = {0}; - char uci_str[16] = {0}; - - struct uci_context *ctx = uci_alloc_context(); - if (!ctx) { - BBF_ERR("Failed to allocate UCI context."); - return; - } - - snprintf(uci_str, sizeof(uci_str), "%s", "sysmngr"); - - if (uci_lookup_ptr(ctx, &ptr, uci_str, true) != UCI_OK) { - BBF_ERR("Failed to lookup for sysmngr config"); - uci_free_context(ctx); - return; - } - + struct bbfdm_ctx ctx = {0}; + struct uci_section *s = NULL, *tmp_s = NULL; int total_reboot_sections = 0; - struct uci_element *e, *tmp; + int removed_count = 0; + + bbfdm_init_ctx(&ctx); // First pass to count total reboot sections - uci_foreach_element(&ptr.p->sections, e) { - struct uci_section *section = uci_to_section(e); - if (strcmp(section->type, "reboot") == 0) { - total_reboot_sections++; - } + BBFDM_UCI_FOREACH_SECTION(&ctx, "sysmngr", "reboot", s) { + total_reboot_sections++; } // Calculate number of sections to remove int sections_to_remove = total_reboot_sections - ((max_reboot_entries > 0) ? max_reboot_entries : REBOOT_MAX_ENTRIES); if (sections_to_remove < 0) { // No need to delete sections - uci_free_context(ctx); + bbfdm_free_ctx(&ctx); return; } - int removed_count = 0; // Second pass to remove excess sections - uci_foreach_element_safe(&ptr.p->sections, tmp, e) { - struct uci_section *section = uci_to_section(e); + BBFDM_UCI_FOREACH_SECTION_SAFE(&ctx, "sysmngr", "reboot", tmp_s, s) { - if (strcmp(section->type, "reboot") == 0 && removed_count <= sections_to_remove) { + if (removed_count <= sections_to_remove) { - if (sysmngr_uci_delete(ctx, "sysmngr", section->e.name)) { - uci_free_context(ctx); + if (bbfdm_uci_delete(&ctx, "sysmngr", section_name(s), NULL)) { + bbfdm_free_ctx(&ctx); return; } @@ -161,11 +145,11 @@ static void delete_excess_reboot_sections(int max_reboot_entries) } // Commit changes to save deletions - if (uci_commit(ctx, &ptr.p, false) != UCI_OK) { - BBF_ERR("Failed to commit changes"); + if (bbfdm_uci_commit_package(&ctx, "sysmngr") != 0) { + BBFDM_ERR("Failed to commit changes"); } - uci_free_context(ctx); + bbfdm_free_ctx(&ctx); } static void create_reboot_section(const char *trigger, const char *reason) @@ -179,20 +163,20 @@ static void create_reboot_section(const char *trigger, const char *reason) snprintf(sec_name, sizeof(sec_name), "reboot_%ld", (long int)time(NULL)); calculate_boot_time(boot_time, sizeof(boot_time)); - sysmngr_uci_set("sysmngr", sec_name, NULL, "reboot"); - sysmngr_uci_set("sysmngr", sec_name, "time_stamp", boot_time); - sysmngr_uci_set("sysmngr", sec_name, "firmware_updated", strcmp(trigger, "upgrade") == 0 ? "1" : "0"); + BBFDM_UCI_SET("sysmngr", sec_name, NULL, "reboot"); + BBFDM_UCI_SET("sysmngr", sec_name, "time_stamp", boot_time); + BBFDM_UCI_SET("sysmngr", sec_name, "firmware_updated", strcmp(trigger, "upgrade") == 0 ? "1" : "0"); if (strcmp(trigger, "defaultreset") == 0) { - sysmngr_uci_set("sysmngr", sec_name, "cause", "FactoryReset"); + BBFDM_UCI_SET("sysmngr", sec_name, "cause", "FactoryReset"); } else { char last_reboot_cause[32] = {0}; - sysmngr_uci_get("sysmngr", "reboots", "last_reboot_cause", "LocalReboot", last_reboot_cause, sizeof(last_reboot_cause)); - sysmngr_uci_set("sysmngr", sec_name, "cause", last_reboot_cause); - sysmngr_uci_set("sysmngr", "reboots", "last_reboot_cause", ""); + BBFDM_UCI_GET("sysmngr", "reboots", "last_reboot_cause", "LocalReboot", last_reboot_cause, sizeof(last_reboot_cause)); + BBFDM_UCI_SET("sysmngr", sec_name, "cause", last_reboot_cause); + BBFDM_UCI_SET("sysmngr", "reboots", "last_reboot_cause", ""); } - sysmngr_uci_set("sysmngr", sec_name, "reason", boot_reason_message(trigger, reason)); + BBFDM_UCI_SET("sysmngr", sec_name, "reason", boot_reason_message(trigger, reason)); } static void sysmngr_register_boot_action(void) @@ -201,14 +185,14 @@ static void sysmngr_register_boot_action(void) // Check if boot action was already executed if (file_exists(REBOOT_LOCK_FILE)) { - BBF_INFO("Boot action already completed previously. Skipping registration."); + BBFDM_INFO("Boot action already completed previously. Skipping registration."); return; } get_boot_option_value("triggered", trigger, sizeof(trigger)); get_boot_option_value("reason", reason, sizeof(reason)); - BBF_DEBUG("RESET triggered[%s], reason[%s] ...", trigger, reason); + BBFDM_DEBUG("RESET triggered[%s], reason[%s] ...", trigger, reason); if (strcmp(trigger, "defaultreset") == 0) { reset_option_counter("boot_count", "1"); reset_option_counter("curr_version_boot_count", "0"); @@ -227,7 +211,7 @@ static void sysmngr_register_boot_action(void) increment_option_counter("warm_boot_count"); } - sysmngr_uci_get("sysmngr", "reboots", "max_reboot_entries", "3", max_entries, sizeof(max_entries)); + BBFDM_UCI_GET("sysmngr", "reboots", "max_reboot_entries", "3", max_entries, sizeof(max_entries)); int max_reboot_entries = (int)strtol(max_entries, NULL, 10); if (max_reboot_entries != 0) { @@ -271,12 +255,12 @@ static struct uloop_timeout reboot_timer = { .cb = reboot_check_timer }; void sysmngr_reboots_init(void) { if (file_exists(REBOOT_LOCK_FILE)) { - BBF_INFO("Boot action already completed previously. Skipping registration."); + BBFDM_INFO("Boot action already completed previously. Skipping registration."); return; } if (check_valid_reset_reason_file() == true) { - BBF_INFO("Valid reset reason file '%s' found. Proceeding to register boot action", RESET_REASON_PATH); + BBFDM_INFO("Valid reset reason file '%s' found. Proceeding to register boot action", RESET_REASON_PATH); sysmngr_register_boot_action(); return; } @@ -285,10 +269,10 @@ void sysmngr_reboots_init(void) g_retry_count++; uloop_timeout_set(&reboot_timer, REBOOT_RETRY_DELAY * 1000); - BBF_WARNING("## Attempt %d/%d: Reset reason file '%s' not found. Retrying in %d second(s)...", + BBFDM_WARNING("## Attempt %d/%d: Reset reason file '%s' not found. Retrying in %d second(s)...", g_retry_count, REBOOT_MAX_RETRIES, RESET_REASON_PATH, REBOOT_RETRY_DELAY); } else { - BBF_WARNING("Max retries reached (%d). A valid reset reason file '%s' not found. Proceeding with boot action registration", + BBFDM_WARNING("Max retries reached (%d). A valid reset reason file '%s' not found. Proceeding with boot action registration", REBOOT_MAX_RETRIES, RESET_REASON_PATH); sysmngr_register_boot_action(); } diff --git a/src/sysmngr.c b/src/sysmngr.c index 5541cb0..aba72a1 100644 --- a/src/sysmngr.c +++ b/src/sysmngr.c @@ -49,7 +49,7 @@ static void usage(char *prog) static void config_reload_cb(struct ubus_context *ctx, struct ubus_event_handler *ev, const char *type, struct blob_attr *msg) { - BBF_INFO("Reloading sysmngr upon 'sysmngr.reload' event"); + BBFDM_INFO("Reloading sysmngr upon 'sysmngr.reload' event"); #ifdef SYSMNGR_PROCESS_STATUS sysmngr_cpu_clean(); diff --git a/src/utils.c b/src/utils.c index 3a62440..fb17a4b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -150,149 +150,6 @@ void send_transfer_complete_event(const char *command, const char *obj_path, con blob_buf_free(&bb); } -int sysmngr_uci_get(const char *package, const char *section, const char *option, const char *default_value, char *buffer, size_t buffer_size) -{ - struct uci_ptr ptr = {0}; - char uci_str[128] = {0}; - - if (!package || !section || !option || !default_value || !buffer || !buffer_size) - return -1; - - struct uci_context *ctx = uci_alloc_context(); - if (!ctx) { - BBF_ERR("UCI context allocation failed"); - return -1; - } - - snprintf(uci_str, sizeof(uci_str), "%s.%s.%s", package, section, option); - - if (uci_lookup_ptr(ctx, &ptr, uci_str, true) != UCI_OK || ptr.o == NULL) { - snprintf(buffer, buffer_size, "%s", default_value); - uci_free_context(ctx); - return -1; - } - - snprintf(buffer, buffer_size, "%s", ptr.o->v.string); - uci_free_context(ctx); - return 0; -} - -int sysmngr_uci_set(const char *package, const char *section, const char *option, const char *value) -{ - struct uci_ptr ptr = {0}; - char uci_str[128] = {0}; - - if (!package || !section || !value) - return -1; - - struct uci_context *ctx = uci_alloc_context(); - if (!ctx) { - BBF_ERR("UCI context allocation failed"); - return -1; - } - - snprintf(uci_str, sizeof(uci_str), "%s.%s%s%s=%s", - package, - section, - option ? "." : "", - option ? option : "", - value); - - if (uci_lookup_ptr(ctx, &ptr, uci_str, true) != UCI_OK || - uci_set(ctx, &ptr) != UCI_OK || - uci_save(ctx, ptr.p) != UCI_OK || - uci_commit(ctx, &ptr.p, false) != UCI_OK) { - - uci_free_context(ctx); - return -1; - } - - uci_free_context(ctx); - return 0; -} - -int sysmngr_uci_delete(struct uci_context *uci_ctx, const char *package, const char *section) -{ - struct uci_ptr ptr = {0}; - char uci_str[64] = {0}; - - if (!package || !section) - return -1; - - snprintf(uci_str, sizeof(uci_str), "%s.%s", package, section); - - if (uci_lookup_ptr(uci_ctx, &ptr, uci_str, true) != UCI_OK || - uci_delete(uci_ctx, &ptr) != UCI_OK || - uci_save(uci_ctx, ptr.p) != UCI_OK) { - - return -1; - } - - return 0; -} - -int sysmngr_ubus_invoke_sync(const char *obj, const char *method, struct blob_attr *msg, sysmngr_ubus_cb data_callback, void *callback_args) -{ - uint32_t id; - int rc = 0; - - struct ubus_context *ubus_ctx = ubus_connect(NULL); - if (!ubus_ctx) { - BBF_ERR("Failed to connect with ubus, error: '%d'", errno); - return -1; - } - - if (!ubus_lookup_id(ubus_ctx, obj, &id)) { - rc = ubus_invoke(ubus_ctx, id, method, msg, data_callback, callback_args, 5000); - } else { - BBF_ERR("Failed to lookup ubus object: '%s'", obj); - rc = -1; - } - - ubus_free(ubus_ctx); - - return rc; -} - - -int sysmngr_ubus_invoke_async(struct ubus_context *ubus_ctx, const char *obj, const char *method, struct blob_attr *msg, - sysmngr_ubus_cb data_callback, sysmngr_ubus_async_cb complete_callback) -{ - struct ubus_request *req = NULL; - uint32_t id; - - if (ubus_ctx == NULL) { - BBF_ERR("Failed to connect with ubus, error: '%d'", errno); - return -1; - } - - if (ubus_lookup_id(ubus_ctx, obj, &id)) { - BBF_DEBUG("Failed to lookup ubus object: '%s'", obj); - return -1; - } - - req = (struct ubus_request *)calloc(1, sizeof(struct ubus_request)); - if (req == NULL) { - BBF_ERR("failed to allocate memory for ubus request"); - return -1; - } - - if (ubus_invoke_async(ubus_ctx, id, method, msg, req)) { - BBF_ERR("ubus async call failed for object: '%s', method: '%s'", obj, method); - FREE(req); - return -1; - } - - if (data_callback) - req->data_cb = data_callback; - - if (complete_callback) - req->complete_cb = complete_callback; - - ubus_complete_request_async(ubus_ctx, req); - return 0; -} - int sysmngr_get_uptime(void) { // cppcheck-suppress cert-MSC24-C @@ -323,8 +180,8 @@ void sysmngr_generate_critical_log_file(const char *log_path, const char *type, int res = run_cmd(cmd, output, sizeof(output)); if (!res || strncmp(output, "Success", 7) == 0) - BBF_DEBUG("Critical log generation succeeded: result=%d, output='%s'", res, output); + BBFDM_DEBUG("Critical log generation succeeded: result=%d, output='%s'", res, output); else - BBF_DEBUG("Critical log generation failed: result=%d, output='%s'", res, output); + BBFDM_DEBUG("Critical log generation failed: result=%d, output='%s'", res, output); } diff --git a/src/utils.h b/src/utils.h index 6ab5c0f..d4ff5e3 100644 --- a/src/utils.h +++ b/src/utils.h @@ -21,17 +21,6 @@ bool validate_server_response_code(const char *url, int response_code); void send_transfer_complete_event(const char *command, const char *obj_path, const char *transfer_url, char *fault_string, time_t start_t, time_t complete_t, const char *commandKey, const char *transfer_type); -int sysmngr_uci_get(const char *package, const char *section, const char *option, const char *default_value, char *buffer, size_t buffer_size); -int sysmngr_uci_set(const char *package, const char *section, const char *option, const char *value); -int sysmngr_uci_delete(struct uci_context *uci_ctx, const char *package, const char *section); - -typedef void (*sysmngr_ubus_cb)(struct ubus_request *req, int type, struct blob_attr *msg); -typedef void (*sysmngr_ubus_async_cb)(struct ubus_request *req, int ret); - -int sysmngr_ubus_invoke_sync(const char *obj, const char *method, struct blob_attr *msg, sysmngr_ubus_cb data_callback, void *callback_args); -int sysmngr_ubus_invoke_async(struct ubus_context *ubus_ctx, const char *obj, const char *method, struct blob_attr *msg, - sysmngr_ubus_cb data_callback, sysmngr_ubus_async_cb complete_callback); - int sysmngr_get_uptime(void); void sysmngr_generate_critical_log_file(const char *log_path, const char *log_name, bool critical_state); -- GitLab