diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..f94ce2fb1800e2a93c19fd5277dcd57e67881d09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +src/sysmngr +src/*.o +src/*.so diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000000000000000000000000000000000000..9180c1e19b375a4a24578b43f91f47e156bbb639 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,12 @@ +variables: + SOURCE_FOLDER: "src" + CFLAGS: "-DBBF_VENDOR_PREFIX=\\\"X_IOPSYS_EU_\\\"" + +include: + - project: 'iopsys/gitlab-ci-pipeline' + file: '/static-code-analysis.yml' + ref: '1.2' + +stages: + - static_code_analysis + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..9c18b5fa0523fd0f8804004b213adce675f7ce69 --- /dev/null +++ b/LICENSE @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2024, IOPSYS +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/docs/.pages b/docs/.pages new file mode 100644 index 0000000000000000000000000000000000000000..09e223612f4985d08dbe805cc410cb7405918452 --- /dev/null +++ b/docs/.pages @@ -0,0 +1,8 @@ +title: "System Manager" + +nav: + - "arch" + - "api" + - "spec" + - "guide" + - "..." diff --git a/docs/api/.pages b/docs/api/.pages new file mode 100644 index 0000000000000000000000000000000000000000..6796f2230333928aa8162d9af27449be093597cf --- /dev/null +++ b/docs/api/.pages @@ -0,0 +1,4 @@ +title: "System Manager" + +nav: + - "..." diff --git a/docs/arch/.pages b/docs/arch/.pages new file mode 100644 index 0000000000000000000000000000000000000000..5c3d44cf2da6177defe65b8ad7bea75d95ed1750 --- /dev/null +++ b/docs/arch/.pages @@ -0,0 +1,2 @@ +title: "System Manager" +hide: true diff --git a/docs/guide/.pages b/docs/guide/.pages new file mode 100644 index 0000000000000000000000000000000000000000..6796f2230333928aa8162d9af27449be093597cf --- /dev/null +++ b/docs/guide/.pages @@ -0,0 +1,4 @@ +title: "System Manager" + +nav: + - "..." diff --git a/docs/guide/libbbfdm_DeviceInfo_FirmwareImage.md b/docs/guide/libbbfdm_DeviceInfo_FirmwareImage.md new file mode 100644 index 0000000000000000000000000000000000000000..3d101bf01a5a7cad20adee799abdf83d48183045 --- /dev/null +++ b/docs/guide/libbbfdm_DeviceInfo_FirmwareImage.md @@ -0,0 +1,141 @@ +# Design For Firmware Activation + +According to the TR181 data model, the Activate() command is an operation to activate the firmware image immediately or schedule it in another time. + +In fact, the Linux system already provides us a cron to schedule some jobs. And since Openwrt is one of the Linux systems, so we can use a cron job as solution to handle the firmware activation. + +The Activate() command has as arguments the TimeWindow object which is used to activate the required firmware in a specified time. For that, foreach TimeWindow instance a cron job will be created. + +> Note: As per TR181 data model, max 5 instances of TimeWindow is supported. + +Below is an example of an 'Activate()' command call with three TimeWindow instances. As a result, three jobs are created according to the defined TimeWindow.{i}.Start: + +```bash +root@iopsys-44d43771aff0:~# ubus call bbfdm operate '{"path":"Device.DeviceInfo.FirmwareImage.2.", "action":"Activate()", "input":{"TimeWindow.1 +.Start":"1800", "TimeWindow.1.End":"3600", "TimeWindow.1.Mode":"WhenIdle", "TimeWindow.2.Start":"5400", "TimeWindow.2.End":"9000", "TimeWindow +.2.Mode":"WhenIdle", "TimeWindow.3.Start":"86400", "TimeWindow.3.End":"172800", "TimeWindow.3.Mode":"Immediately"}}' +{ + "Results": [ + { + "path": "Device.DeviceInfo.FirmwareImage.2.Activate()", + "result": [ + { + + } + ] + } + ] +} +root@iopsys-44d43771aff0:~# crontab -l +52 22 21 2 * sh /usr/share/bbfdm/scripts/bbf_activate_handler.sh 'WhenIdle' '2' '1800' '0' '' '' +52 23 21 2 * sh /usr/share/bbfdm/scripts/bbf_activate_handler.sh 'WhenIdle' '2' '3600' '0' '' '' +22 22 22 2 * sh /usr/share/bbfdm/scripts/bbf_activate_handler.sh 'Immediately' '2' '86400' '1' '' '' +root@iopsys-44d43771aff0:~# + +``` + +For those cron jobs it is required to give the handler script to be executed which is in our case [bbf_activate_handler.sh](../../libbbfdm/scripts/bbf_activate_handler.sh). And, it is located under '/usr/share/bbfdm/' in the device. + + +## Cron job specification + +For each cron job related to the activated firmware, it is needed to define it as below: + +```bash +* * * * * command to execute * * * * * * +- - - - - - - - - - - - +| | | | | | | | | | | | +| | | | | | | | | | | --- Message that informs the user of a new activation request +| | | | | | | | | | ----- Maximum number of retries +| | | | | | | | | ------- Force firmware activation when it's not idle (0 - 1) +| | | | | | | | --------- End of the time window +| | | | | | | ----------- Firmware Bank id to be activated +| | | | | | ------------- Mode (AnyTime, Immediately, WhenIdle, ConfirmationNeeded) +| | | | | -------------------------------- Activate firmware script 'bbf_activate_handler.sh' +| | | | ---------------------------------- Day of week (0 - 6) (Sunday =0) +| | | ------------------------------------ Month (1 - 12) +| | -------------------------------------- Day (1 - 31) +| ---------------------------------------- Hour (0 - 23) +------------------------------------------ Minute (0 - 59) +``` + + +## Activate Handler script + +As described, we create a cron job for each TimeWindow in order to activate the required firmware within a specified time by running the [bbf_activate_handler.sh](../../libbbfdm/scripts/bbf_activate_handler.sh) handler script. + +In fact, the aim of this script is to manage firmware images based on the **mode** and the other passed arguments. + + +### 1. Mode 'AnyTime' and 'Immediately': + +For these modes and based on the firmware bank id, the required firmware image will be immediately activated at start time. The TimeWindow.{i}.End is ignored. + +### 2. How to handle 'WhenIdle' mode: + +Definition of WhenIdle may vary for each deployment and customer, to make it customizable [bbf_check_idle.sh](../../libbbfdm/scripts/bbf_check_idle.sh) script is used. It is assumed that customer shall overwrite this file using customer-config to match with there requirement. + +In this mode, [bbf_activate_handler.sh](../../libbbfdm/scripts/bbf_activate_handler.sh) script calls this script [bbf_check_idle.sh](../../libbbfdm/scripts/bbf_check_idle.sh) to determine the idle state of the device. [bbf_activate_handler.sh](../../libbbfdm/scripts/bbf_activate_handler.sh) assumes the device as idle if the exit status of the above script is 0, or if the [bbf_check_idle.sh](../../libbbfdm/scripts/bbf_check_idle.sh) is not present in the predefined path "ACTIVATE_HANDLER_FILE@dmcommon.h". + + +If the exit code from the idle script is zero then firmware image can be activated. Otherwise, it has to wait for next time slot which is defined by 'RETRY_TIME' variable. + +> Note1: The time slot is set through 'RETRY_TIME' variable which is defined under '/usr/share/bbfdm/scripts/bbf_activate_handler.sh' script. + +> Note2: The exit status of the script [bbf_check_idle.sh](../../libbbfdm/scripts/bbf_check_idle.sh) is important because based on it, the '[bbf_activate_handler.sh](../../libbbfdm/scripts/bbf_activate_handler.sh) script will decide whether the image can be activated or not. + +> Note3: Algorithm/Logic to determine the Idle state of device is out of scope of this document and it is expected that users overwrite this script with the logic to determine the same in actual deployment. + +> Note4: If 1 or more TimeWindow.{i}.Mode is set to 'WhenIdle' and all of them fails to get the idle state. The latest TimeWindow instance will force the device to activate the firmware image. + +> Note5: If the idle script [bbf_check_idle.sh](../../libbbfdm/scripts/bbf_check_idle.sh) not present in the pre-defined path "ACTIVATE_HANDLER_FILE@dmcommon.h", then the device is assumed to be in ideal state and the firmware shall be activated instantly. + +> Note6: It is very likely that TimeWindow with 'WhenIdle' mode might not find any suitable Idle state, in that case firmware shall not be activated. If users/operators want to make sure that firmware gets activated at the end, then they can add a TimeWindow with 'AnyTime/Immediate' mode at the end, to activate the firmware. + + +## Good to know + +* TimeWindow instance arguments are optional. + +* TimeWindow instances attributes must not overlap. + +* If TimeWindow.{i}.Start is set, TimeWindow.{i}.End and TimeWindow.{i}.Mode become mondatory. + +* The firmware activation is done by [bbf_activate_handler.sh](../../libbbfdm/scripts/bbf_activate_handler.sh) script as per the defined Mode in TimeWindow, but if the TimeWindow is not defined, it will activate the requested FirmwareImage instance immediately. + +* If the customer wants to be sure that the required firmware is getting activated at the end then they can define the TimeWindow.{i}.Mode as 'AnyTime' or 'Immediately' in the last TimeWindow instance. + +* This document is only target for Firmware management using USP. + +* TimeWindow.{i}.Mode = 'ConfirmationNeeded' is not supported. + + +## Vendor extension option to keep config while firmware download + +It deployments for some customers, its required to do a factory reset after doing a firmware upgrade to start the CPE from clean state and then provision it from ACS/Controller. + +As per standard datamodel, it's at-least 2 step time consuming process: +- Download the Firmware using 'Device.DeviceInfo.FirmwareImage.{i}.Download()' operate command with AutoActivate=1 +- Wait for the 'Device.Boot!' event +- Factory reset the CPE using 'Device.FactoryReset()' +- Wait for the Boot event and then start provisioning. + +We added an addition vendor specific input option which can be used by USP controller to factoryReset the CPE along with Firmware Upgrade, with this customer can save the cost of one additional reboot, which result into faster provisioning of the CPE. + +Below are the current input options defined for Download operate command +```bash +Device.DeviceInfo.FirmwareImage.{i}.Download() +Device.DeviceInfo.FirmwareImage.{i}.Download() input:AutoActivate +Device.DeviceInfo.FirmwareImage.{i}.Download() input:CheckSum +Device.DeviceInfo.FirmwareImage.{i}.Download() input:CheckSumAlgorithm +Device.DeviceInfo.FirmwareImage.{i}.Download() input:CommandKey +Device.DeviceInfo.FirmwareImage.{i}.Download() input:FileSize +Device.DeviceInfo.FirmwareImage.{i}.Download() input:Password +Device.DeviceInfo.FirmwareImage.{i}.Download() input:URL +Device.DeviceInfo.FirmwareImage.{i}.Download() input:Username +Device.DeviceInfo.FirmwareImage.{i}.Download() input:X_IOPSYS_EU_KeepConfig +``` + +Customer can use X_IOPSYS_EU_KeepConfig=0, to do factory reset(not copy the current config to next firmware) while doing the download. + +> Note: Default value of X_IOPSYS_EU_KeepConfig is 1, so in case this option not used, it keeps the config(as the default behavior of the CPE). diff --git a/docs/guide/libbbfdm_DeviceInfo_Reboots.md b/docs/guide/libbbfdm_DeviceInfo_Reboots.md new file mode 100644 index 0000000000000000000000000000000000000000..d86662f333ab6dd9eae3b95cc1dbffb1510b59a2 --- /dev/null +++ b/docs/guide/libbbfdm_DeviceInfo_Reboots.md @@ -0,0 +1,74 @@ +# Design for Reboot Data Model + +In TR-181 version 2.18, a new object, Device.DeviceInfo.Reboots, was introduced to track and monitor reboot operations. This object provides details such as reboot reasons, counts, timestamps, and more, offering a comprehensive view of the device's state. It simplifies diagnostics and troubleshooting for applications and processes running on the device. + +Currently, there is no standard configuration mapping to this object. However, we propose introducing a custom config called `deviceinfo` to manage this information effectively. + +The idea is to maintain a 1-to-1 mapping between the parameters and UCI config. To achieve this, we need to create an `init.d` service script that generates a UCI section each time the boot() function is called. Essentially, when the boot() function is executed, it will check the `/tmp/reset_reason` file for specific markers, such as (reset reason) and (reset triggered), to identify the cause of the last boot. And based on these markers, it will calculate the required counter for data model parameters and commit the changes in `deviceinfo.globals` section. Furthermore, if necessary, it will create a UCI reboot section by checking `deviceinfo.globals.max_reboot_entries` and adjusting the config accordingly. + +This approach ensures that the data model maps directly to UCI config as closely as possible, eliminating the need for any adjustments at the data model layer. + +## Parameter Mapping Details + +- Device.DeviceInfo.Reboots.BootCount: Maps to deviceinfo.globals.boot_count. This value is determined based on the marker (reset triggered: defaultreset) defined in `/tmp/reset_reason` file. +- Device.DeviceInfo.Reboots.CurrentVersionBootCount: Maps to deviceinfo.globals.curr_version_boot_count. This value is determined based on the marker (reset triggered: upgrade) defined in `/tmp/reset_reason` file. +- Device.DeviceInfo.Reboots.WatchdogBootCount: Maps to deviceinfo.globals.watchdog_boot_count. This value is determined based on the marker (reset reason: WATCHDOG) defined in `/tmp/reset_reason` file. +- Device.DeviceInfo.Reboots.ColdBootCount: +- Device.DeviceInfo.Reboots.WarmBootCount: +- Device.DeviceInfo.Reboots.MaxRebootEntries: Maps to deviceinfo.globals.max_reboot_entries. Possible values include {-1, 0, etc..}. Each case will be handled internally by bbfdm and default value is 3 and maximum reboot entry supported is 255. +- Device.DeviceInfo.Reboots.RebootNumberOfEntries: This is an internal bbfdm mechanism used to count the number of reboot entries. +- Device.DeviceInfo.Reboots.RemoveAllReboots(): An internal bbfdm API to remove all reboot sections. +- Device.DeviceInfo.Reboots.Reboot.{i}.: Each reboot entry is stored in a 'reboot' section. +- Device.DeviceInfo.Reboots.Reboot.{i}.Alias: Maps to deviceinfo.reboot[i].alias. This is managed internally by bbfdm. +- Device.DeviceInfo.Reboots.Reboot.{i}.TimeStamp: Maps to deviceinfo.reboot[i].time_stamp. This value is based on system uptime. +- Device.DeviceInfo.Reboots.Reboot.{i}.FirmwareUpdated: Maps to deviceinfo.reboot[i].firmware_updated. +- Device.DeviceInfo.Reboots.Reboot.{i}.Cause: Maps to deviceinfo.reboot[i].cause. Possible values include {LocalReboot, RemoteReboot, FactoryReset, LocalFactoryReset, RemoteFactoryReset}. +- Device.DeviceInfo.Reboots.Reboot.{i}.Reason: Maps to deviceinfo.reboot[i].reason. This value is determined based on the marker (reset reason) defined in `/tmp/reset_reason` file. +- Device.DeviceInfo.Reboots.Reboot.{i}.Remove(): An internal bbfdm API to remove the current 'reboot' section. + + +## Example Configuration + +Below is an example of the configuration file: + +```bash +cat /etc/config/deviceinfo + +config globals 'globals' + option boot_count '2' + option curr_version_boot_count '4' + option watchdog_boot_count '3' + option cold_boot_count '2' + option warm_boot_count '2' + option max_reboot_entries '3' + +config reboot 'reboot_1' + option alias 'cpe-1' + option time_stamp '2024-09-22T20:34:45Z' + option firmware_updated '0' + option cause 'RemoteReboot' + option reason 'REBOOT' + +config reboot 'reboot_2' + option alias 'cpe-2' + option time_stamp '2024-09-22T21:55:09Z' + option firmware_updated '0' + option cause 'LocalReboot' + option reason 'POR_RESET' + +config reboot 'reboot_3' + option alias 'cpe-3' + option time_stamp '2024-09-23T04:11:24Z' + option firmware_updated '1' + option cause 'LocalReboot' + option reason 'upgrade' + +config reboot 'reboot_4' + option alias 'cpe-4' + option time_stamp '2024-09-23T04:15:53Z' + option firmware_updated '0' + option cause 'RemoteFactoryReset' + option reason 'REBOOT' + +``` + diff --git a/docs/spec/.pages b/docs/spec/.pages new file mode 100644 index 0000000000000000000000000000000000000000..5c3d44cf2da6177defe65b8ad7bea75d95ed1750 --- /dev/null +++ b/docs/spec/.pages @@ -0,0 +1,2 @@ +title: "System Manager" +hide: true diff --git a/gitlab-ci/install-dependencies.sh b/gitlab-ci/install-dependencies.sh new file mode 100755 index 0000000000000000000000000000000000000000..285cc185721796dab4b7e8dba2c5292c8a88c21f --- /dev/null +++ b/gitlab-ci/install-dependencies.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +echo "install dependencies for tests" +source ./gitlab-ci/shared.sh + +# install bbfdm +install_bbfdm diff --git a/gitlab-ci/shared.sh b/gitlab-ci/shared.sh new file mode 100644 index 0000000000000000000000000000000000000000..3fe0bcefabbe58ee33b8cd469463cf2c4cf17a2e --- /dev/null +++ b/gitlab-ci/shared.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +function exec_cmd() +{ + echo "executing $@" + $@ >/dev/null 2>&1 + + if [ $? -ne 0 ]; then + echo "Failed to execute $@" + exit 1 + fi +} + +function install_bbfdm() +{ + [ -d "/opt/dev/bbfdm" ] && return 0 + + if [ -n "${BBFDM_BRANCH}" ]; then + exec_cmd git clone -b ${BBFDM_BRANCH} https://dev.iopsys.eu/bbf/bbfdm.git /opt/dev/bbfdm + else + exec_cmd git clone -b sysmngr https://dev.iopsys.eu/bbf/bbfdm.git /opt/dev/bbfdm + fi + + cd /opt/dev/bbfdm + ./gitlab-ci/install-dependencies.sh install + ./gitlab-ci/setup.sh install +} diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..253e5e1c4fa025e5e2ecba3c2f78469b3b90bf32 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,55 @@ +PROG = sysmngr + +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 +FPIC := -fPIC + +ifeq ($(SYSMNGR_VENDOR_CONFIG_FILE),y) +OBJS += configs.o +PROG_CFLAGS += -DSYSMNGR_VENDOR_CONFIG_FILE +endif + +ifeq ($(SYSMNGR_MEMORY_STATUS),y) +OBJS += memory.o +PROG_CFLAGS += -DSYSMNGR_MEMORY_STATUS +endif + +ifeq ($(SYSMNGR_PROCESS_STATUS),y) +OBJS += processes.o +PROG_CFLAGS += -DSYSMNGR_PROCESS_STATUS +endif + +ifeq ($(SYSMNGR_SUPPORTED_DATA_MODEL),y) +OBJS += supported_dm.o +PROG_CFLAGS += -DSYSMNGR_SUPPORTED_DATA_MODEL +endif + +ifeq ($(SYSMNGR_FIRMWARE_IMAGE),y) +OBJS += fw_images.o +PROG_CFLAGS += -DSYSMNGR_FIRMWARE_IMAGE +endif + +ifeq ($(SYSMNGR_REBOOTS),y) +OBJS += reboots.o +PROG_CFLAGS += -DSYSMNGR_REBOOTS +endif + +ifeq ($(SYSMNGR_VENDOR_EXTENSIONS),y) +PROG_CFLAGS += -DSYSMNGR_VENDOR_EXTENSIONS +endif + +.PHONY: all clean + +%.o: %.c + $(CC) $(PROG_CFLAGS) $(FPIC) -c -o $@ $< + +all: $(PROG) + +$(PROG): $(OBJS) + $(CC) $(PROG_CFLAGS) -o $@ $^ $(PROG_LDFLAGS) + +clean: + rm -f *.o $(PROG) diff --git a/src/configs.c b/src/configs.c new file mode 100644 index 0000000000000000000000000000000000000000..4bab4696b7d560beaa452a9bc9eeb4af4ba4361a --- /dev/null +++ b/src/configs.c @@ -0,0 +1,343 @@ +/* + * Copyright (C) 2019-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include "utils.h" + +#define CONFIG_BACKUP "/tmp/bbf_config_backup" + +/************************************************************* +* COMMON FUNCTIONS +**************************************************************/ +static int dmmap_synchronizeVcfInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) +{ + struct uci_section *s = NULL, *stmp = NULL; + DIR *dir; + struct dirent *d_file; + + sysfs_foreach_file(DEFAULT_CONFIG_DIR, dir, d_file) { + + if(d_file->d_name[0] == '.') + continue; + + if (!is_dmmap_section_exist_eq("dmmap", "vcf", "name", d_file->d_name)) { + dmuci_add_section_bbfdm("dmmap", "vcf", &s); + dmuci_set_value_by_section(s, "name", d_file->d_name); + dmuci_set_value_by_section(s, "backup_restore", "1"); + } + } + + if (dir) + closedir (dir); + + uci_path_foreach_sections_safe(bbfdm, "dmmap", "vcf", stmp, s) { + char cfg_path[128] = {0}; + char *cfg_name = NULL; + + dmuci_get_value_by_section_string(s, "name", &cfg_name); + + snprintf(cfg_path, sizeof(cfg_path), "%s%s", DEFAULT_CONFIG_DIR, cfg_name); + + if (!file_exists(cfg_path)) + dmuci_delete_by_section_bbfdm(s, NULL, NULL); + } + + return 0; +} + +static int bbf_config_backup(const char *url, const char *username, const char *password, + char *config_name, const char *command, const char *obj_path) +{ + int res = 0; + char fault_msg[128] = {0}; + time_t complete_time = 0; + time_t start_time = time(NULL); + + // Export config file to backup file + if (dmuci_export_package(config_name, CONFIG_BACKUP)) { + snprintf(fault_msg, sizeof(fault_msg), "Failed to export the configurations"); + res = -1; + goto end; + } + + // Upload the config file + long res_code = upload_file(CONFIG_BACKUP, url, username, password); + complete_time = time(NULL); + + // Check if the upload operation was successful + if (!validate_server_response_code(url, res_code)) { + res = -1; + snprintf(fault_msg, sizeof(fault_msg), "Upload operation is failed, fault code (%ld)", res_code); + } + +end: + // Send the transfer complete event + send_transfer_complete_event(command, obj_path, url, fault_msg, start_time, complete_time, NULL, "Upload"); + + // Remove temporary file + if (file_exists(CONFIG_BACKUP) && remove(CONFIG_BACKUP)) + res = -1; + + return res; +} + + +static int bbf_config_restore(const char *url, const char *username, const char *password, + const char *file_size, const char *checksum_algorithm, const char *checksum, + const char *command, const char *obj_path) +{ + char config_restore[256] = {0}; + int res = 0; + char fault_msg[128] = {0}; + time_t complete_time = 0; + time_t start_time = time(NULL); + + DM_STRNCPY(config_restore, "/tmp/bbf_config_restore", sizeof(config_restore)); + + // Check the file system size if there is sufficient space for downloading the config file + if (!validate_file_system_size(file_size)) { + snprintf(fault_msg, sizeof(fault_msg), "Available memory space is less than required for the operation"); + res = -1; + goto end; + } + + // Download the firmware image + long res_code = download_file(config_restore, url, username, password); + complete_time = time(NULL); + + // Check if the download operation was successful + if (!validate_server_response_code(url, res_code)) { + snprintf(fault_msg, sizeof(fault_msg), "Upload operation is failed, fault code (%ld)", res_code); + res = -1; + goto end; + } + + // Validate the CheckSum value according to its algorithm + if (!validate_checksum_value(config_restore, checksum_algorithm, checksum)) { + snprintf(fault_msg, sizeof(fault_msg), "Checksum of the downloaded file is mismatched"); + res = -1; + goto end; + } + + // Apply config file + if (dmuci_import(NULL, config_restore)) { + snprintf(fault_msg, sizeof(fault_msg), "Failed to import the configurations"); + res = -1; + } + +end: + // Send the transfer complete event + send_transfer_complete_event(command, obj_path, url, fault_msg, start_time, complete_time, NULL, "Download"); + + // Remove temporary file + if (file_exists(config_restore) && strncmp(url, FILE_URI, strlen(FILE_URI)) && remove(config_restore)) + res = -1; + + return res; +} + +/************************************************************* +* ENTRY METHOD +**************************************************************/ +int browseVcfInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) +{ + struct dm_data curr_data = {0}; + struct uci_section *s = NULL; + char *inst = NULL; + + dmmap_synchronizeVcfInst(dmctx, parent_node, prev_data, prev_instance); + uci_path_foreach_sections(bbfdm, "dmmap", "vcf", s) { + + curr_data.config_section = s; + + inst = handle_instance(dmctx, parent_node, s, "vcf_instance", "vcf_alias"); + + if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)&curr_data, inst) == DM_STOP) + break; + } + return 0; +} + +/************************************************************* +* GET & SET PARAM +**************************************************************/ +int get_DeviceInfo_VendorConfigFileNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + int cnt = get_number_of_entries(ctx, data, instance, browseVcfInst); + dmasprintf(value, "%d", cnt); + return 0; +} + +static int get_vcf_name(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "name", value); + return 0; +} + +static int get_vcf_version(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "version", value); + return 0; +} + +static int get_vcf_date(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + DIR *dir = NULL; + struct dirent *d_file = NULL; + char *config_name = NULL; + + *value = dmstrdup("0001-01-01T00:00:00Z"); + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "name", &config_name); + if ((dir = opendir (DEFAULT_CONFIG_DIR)) != NULL) { + while ((d_file = readdir (dir)) != NULL) { + if (config_name && DM_STRCMP(config_name, d_file->d_name) == 0) { + char date[sizeof("AAAA-MM-JJTHH:MM:SSZ")], path[280] = {0}; + struct stat attr; + + snprintf(path, sizeof(path), "%s%s", DEFAULT_CONFIG_DIR, d_file->d_name); + stat(path, &attr); + strftime(date, sizeof(date), "%Y-%m-%dT%H:%M:%SZ", gmtime(&attr.st_mtime)); + *value = dmstrdup(date); + } + } + closedir (dir); + } + return 0; +} + +static int get_vcf_backup_restore(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "backup_restore", value); + return 0; +} + +static int get_vcf_desc(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "description", value); + return 0; +} + +static int get_vcf_alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + return bbf_get_alias(ctx, ((struct dm_data *)data)->config_section, "vcf_alias", instance, value); +} + +static int set_vcf_alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + return bbf_set_alias(ctx, ((struct dm_data *)data)->config_section, "vcf_alias", instance, value); +} + +/************************************************************* + * OPERATE COMMANDS + *************************************************************/ +static operation_args vendor_config_file_backup_args = { + .in = (const char *[]) { + "URL", + "Username", + "Password", + NULL + } +}; + +static int get_operate_args_DeviceInfoVendorConfigFile_Backup(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = (char *)&vendor_config_file_backup_args; + return 0; +} + +static int operate_DeviceInfoVendorConfigFile_Backup(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + const char *backup_command = "Backup()"; + char backup_path[256] = {'\0'}; + char *vcf_name = NULL; + + char *ret = DM_STRRCHR(refparam, '.'); + if (!ret) + return USP_FAULT_INVALID_ARGUMENT; + + if ((ret - refparam + 2) < sizeof(backup_path)) + snprintf(backup_path, ret - refparam + 2, "%s", refparam); + + char *url = dmjson_get_value((json_object *)value, 1, "URL"); + if (url[0] == '\0') + return USP_FAULT_INVALID_ARGUMENT; + + char *user = dmjson_get_value((json_object *)value, 1, "Username"); + char *pass = dmjson_get_value((json_object *)value, 1, "Password"); + + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "name", &vcf_name); + + int res = bbf_config_backup(url, user, pass, vcf_name, backup_command, backup_path); + + return res ? USP_FAULT_COMMAND_FAILURE : 0; +} + +static operation_args vendor_config_file_restore_args = { + .in = (const char *[]) { + "URL", + "Username", + "Password", + "FileSize", + "TargetFileName", + "CheckSumAlgorithm", + "CheckSum", + NULL + } +}; + +static int get_operate_args_DeviceInfoVendorConfigFile_Restore(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = (char *)&vendor_config_file_restore_args; + return 0; +} + +static int operate_DeviceInfoVendorConfigFile_Restore(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + const char *restore_command = "Restore()"; + char restore_path[256] = {'\0'}; + + char *ret = DM_STRRCHR(refparam, '.'); + if (!ret) + return USP_FAULT_INVALID_ARGUMENT; + + if ((ret - refparam + 2) < sizeof(restore_path)) + snprintf(restore_path, ret - refparam + 2, "%s", refparam); + + char *url = dmjson_get_value((json_object *)value, 1, "URL"); + if (url[0] == '\0') + return USP_FAULT_INVALID_ARGUMENT; + + char *user = dmjson_get_value((json_object *)value, 1, "Username"); + char *pass = dmjson_get_value((json_object *)value, 1, "Password"); + char *file_size = dmjson_get_value((json_object *)value, 1, "FileSize"); + char *checksum_algorithm = dmjson_get_value((json_object *)value, 1, "CheckSumAlgorithm"); + char *checksum = dmjson_get_value((json_object *)value, 1, "CheckSum"); + + int res = bbf_config_restore(url, user, pass, file_size, checksum_algorithm, checksum, restore_command, restore_path); + + return res ? USP_FAULT_COMMAND_FAILURE : 0; +} + +/********************************************************************************************************************************** +* OBJ & LEAF DEFINITION +***********************************************************************************************************************************/ +/* *** Device.DeviceInfo.VendorConfigFile.{i}. *** */ +DMLEAF tDeviceInfoVendorConfigFileParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version*/ +{"Alias", &DMWRITE, DMT_STRING, get_vcf_alias, set_vcf_alias, BBFDM_BOTH, DM_FLAG_UNIQUE}, +{"Name", &DMREAD, DMT_STRING, get_vcf_name, NULL, BBFDM_BOTH, DM_FLAG_UNIQUE}, +{"Version", &DMREAD, DMT_STRING, get_vcf_version, NULL, BBFDM_BOTH}, +{"Date", &DMREAD, DMT_TIME, get_vcf_date, NULL, BBFDM_BOTH}, +{"Description", &DMREAD, DMT_STRING, get_vcf_desc, NULL, BBFDM_BOTH}, +{"UseForBackupRestore", &DMREAD, DMT_BOOL, get_vcf_backup_restore, NULL, BBFDM_BOTH}, +{"Backup()", &DMASYNC, DMT_COMMAND, get_operate_args_DeviceInfoVendorConfigFile_Backup, operate_DeviceInfoVendorConfigFile_Backup, BBFDM_USP}, +{"Restore()", &DMASYNC, DMT_COMMAND, get_operate_args_DeviceInfoVendorConfigFile_Restore, operate_DeviceInfoVendorConfigFile_Restore, BBFDM_USP}, +{0} +}; diff --git a/src/configs.h b/src/configs.h new file mode 100644 index 0000000000000000000000000000000000000000..5eaab74970139687dd4f962ee12de2cfab07c859 --- /dev/null +++ b/src/configs.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2019-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#ifndef __CONFIGS_H +#define __CONFIGS_H + +extern DMLEAF tDeviceInfoVendorConfigFileParams[]; + +int browseVcfInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance); + +int get_DeviceInfo_VendorConfigFileNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value); + +#endif //__CONFIGS_H diff --git a/src/datamodel_description.json b/src/datamodel_description.json new file mode 100644 index 0000000000000000000000000000000000000000..87dbd75253cb1c68558a8afb419beb8103766117 --- /dev/null +++ b/src/datamodel_description.json @@ -0,0 +1,43 @@ +{ + "Device.": { + "type": "object", + "protocols": [ + "cwmp", + "usp" + ], + "description": "The top-level object for a Device.", + "access": false, + "array": false, + "Device.DeviceInfo.": { + "type": "object", + "protocols": [ + "cwmp", + "usp" + ], + "description": "This object contains general device information.", + "access": false, + "array": false, + "{BBF_VENDOR_PREFIX}BaseMACAddress": { + "type": "string", + "read": true, + "write": false, + "protocols": [ + "cwmp", + "usp" + ], + "description": "The MAC Address of the device.", + "datatype": "MACAddress", + "range": [ + { + "max": 17 + } + ], + "pattern": [ + "", + "([0-9A-Fa-f][0-9A-Fa-f]:){5}([0-9A-Fa-f][0-9A-Fa-f])" + ] + } + } + } +} + diff --git a/src/deviceinfo.c b/src/deviceinfo.c new file mode 100644 index 0000000000000000000000000000000000000000..db8ea6c4002fcc69204c3877625154a3ff6b94a2 --- /dev/null +++ b/src/deviceinfo.c @@ -0,0 +1,311 @@ +/* + * Copyright (C) 2019-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include <libbbfdm-api/dmcommon.h> + +#ifdef SYSMNGR_VENDOR_CONFIG_FILE +#include "configs.h" +#endif + +#ifdef SYSMNGR_FIRMWARE_IMAGE +#include "fw_images.h" +#endif + +#ifdef SYSMNGR_MEMORY_STATUS +#include "memory.h" +#endif + +#ifdef SYSMNGR_PROCESS_STATUS +#include "processes.h" +#endif + +#ifdef SYSMNGR_REBOOTS +#include "reboots.h" +#endif + +#ifdef SYSMNGR_SUPPORTED_DATA_MODEL +#include "supported_dm.h" +#endif + +/************************************************************* +* GET & SET PARAM +**************************************************************/ +static int get_device_devicecategory(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "DeviceCategory", value); + return 0; +} + +static int get_device_manufacturer(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("cwmp","cpe","manufacturer", value); + if (*value[0] == '\0') + db_get_value_string("device", "deviceinfo", "Manufacturer", value); + + return 0; +} + +static int get_device_manufactureroui(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("cwmp", "cpe", "manufacturer_oui", value); + if (*value[0] == '\0') + db_get_value_string("device", "deviceinfo", "ManufacturerOUI", value); + + return 0; +} + +static int get_device_modelname(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("cwmp", "cpe", "model_name", value); + if (*value[0] == '\0') + db_get_value_string("device", "deviceinfo", "ModelName", value); + return 0; +} + +static int get_device_description(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("cwmp", "cpe", "description", value); + if (*value[0] == '\0') + db_get_value_string("device", "deviceinfo", "Description", value); + return 0; +} + +static int get_device_productclass(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("cwmp", "cpe", "product_class", value); + if (*value[0] == '\0') + db_get_value_string("device", "deviceinfo", "ProductClass", value); + + return 0; +} + +static int get_device_serialnumber(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("cwmp", "cpe", "serial_number", value); + if (*value[0] == '\0') + db_get_value_string("device", "deviceinfo", "SerialNumber", value); + + return 0; +} + +static int get_device_hardwareversion(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "HardwareVersion", value); + return 0; +} + +static int get_device_softwareversion(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("cwmp", "cpe", "software_version", value); + if (*value[0] == '\0') + db_get_value_string("device", "deviceinfo", "SoftwareVersion", value); + + return 0; +} + +static int get_device_additionalhardwareversion(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "AdditionalHardwareVersion", value); + return 0; +} + +static int get_device_additionalsoftwareversion(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "AdditionalSoftwareVersion", value); + return 0; +} + +static int get_device_provisioningcode(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + char *dhcp = NULL, *provisioning_code = NULL, *dhcp_provisioning_code = NULL; + bool discovery = false; + + dmuci_get_option_value_string("cwmp", "acs", "dhcp_discovery", &dhcp); + dmuci_get_option_value_string("cwmp", "cpe", "provisioning_code", &provisioning_code); + dmuci_get_option_value_string("cwmp", "cpe", "dhcp_provisioning_code", &dhcp_provisioning_code); + + discovery = dmuci_string_to_boolean(dhcp); + + if ((discovery == true) && (DM_STRLEN(dhcp_provisioning_code) != 0)) + *value = dhcp_provisioning_code; + else + *value = provisioning_code; + + return 0; +} + +static int set_device_provisioningcode(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + switch (action) { + case VALUECHECK: + if (bbfdm_validate_string(ctx, value, -1, 64, NULL, NULL)) + return FAULT_9007; + return 0; + case VALUESET: + dmuci_set_value("cwmp", "cpe", "provisioning_code", value); + return 0; + } + return 0; +} + +static int get_device_info_uptime(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = get_uptime(); + return 0; +} + +static int get_device_info_firstusedate(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("time", "global", "first_use_date", value); + return 0; +} + +static int get_deviceinfo_cid (char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "CID", value); + return 0; +} + +static int get_deviceinfo_friendlyname(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "FriendlyName", value); + return 0; +} + +static int get_deviceinfo_pen (char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "PEN", value); + return 0; +} + +static int get_deviceinfo_modelnumber (char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "ModelNumber", value); + return 0; +} + +static int get_DeviceInfo_HostName(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("system", "@system[0]", "hostname", value); + return 0; +} + +static int set_DeviceInfo_HostName(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + switch (action) { + case VALUECHECK: + if (bbfdm_validate_string(ctx, value, -1, 255, NULL, NULL)) + return FAULT_9007; + break; + case VALUESET: + dmuci_set_value("system", "@system[0]", "hostname", value); + break; + } + return 0; +} + +#ifdef SYSMNGR_VENDOR_EXTENSIONS +static int get_deviceinfo_base_mac_addr(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + db_get_value_string("device", "deviceinfo", "BaseMACAddress", value); + return 0; +} +#endif + +/********************************************************************************************************************************** +* OBJ & LEAF DEFINITION +***********************************************************************************************************************************/ +/* *** Device.DeviceInfo. *** */ +DMOBJ tDeviceInfoObj[] = { +/* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type*/ +#ifdef SYSMNGR_VENDOR_CONFIG_FILE +{"VendorConfigFile", &DMREAD, NULL, NULL, NULL, browseVcfInst, NULL, NULL, NULL, tDeviceInfoVendorConfigFileParams, NULL, BBFDM_BOTH}, +#endif + +#ifdef SYSMNGR_MEMORY_STATUS +{"MemoryStatus", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoMemoryStatusParams, NULL, BBFDM_BOTH}, +#endif + +#ifdef SYSMNGR_PROCESS_STATUS +{"ProcessStatus", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoProcessStatusObj, tDeviceInfoProcessStatusParams, NULL, BBFDM_BOTH}, +#endif + +#ifdef SYSMNGR_SUPPORTED_DATA_MODEL +{"SupportedDataModel", &DMREAD, NULL, NULL, NULL, browseDeviceInfoSupportedDataModelInst, NULL, NULL, NULL, tDeviceInfoSupportedDataModelParams, NULL, BBFDM_CWMP}, +#endif + +#ifdef SYSMNGR_FIRMWARE_IMAGE +{"FirmwareImage", &DMREAD, NULL, NULL, "file:/usr/libexec/rpcd/fwbank", browseDeviceInfoFirmwareImageInst, NULL, NULL, NULL, tDeviceInfoFirmwareImageParams, NULL, BBFDM_BOTH}, +#endif + +#ifdef SYSMNGR_REBOOTS +{"Reboots", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoRebootsObj, tDeviceInfoRebootsParams, NULL, BBFDM_USP}, +#endif + +{0} +}; + +DMLEAF tDeviceInfoParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type*/ +{"DeviceCategory", &DMREAD, DMT_STRING, get_device_devicecategory, NULL, BBFDM_BOTH}, +{"Manufacturer", &DMREAD, DMT_STRING, get_device_manufacturer, NULL, BBFDM_BOTH}, +{"ManufacturerOUI", &DMREAD, DMT_STRING, get_device_manufactureroui, NULL, BBFDM_BOTH}, +{"ModelName", &DMREAD, DMT_STRING, get_device_modelname, NULL, BBFDM_BOTH}, +{"Description", &DMREAD, DMT_STRING, get_device_description, NULL, BBFDM_BOTH}, +{"ProductClass", &DMREAD, DMT_STRING, get_device_productclass, NULL, BBFDM_BOTH}, +{"SerialNumber", &DMREAD, DMT_STRING, get_device_serialnumber, NULL, BBFDM_BOTH}, +{"HardwareVersion", &DMREAD, DMT_STRING, get_device_hardwareversion, NULL, BBFDM_BOTH}, +{"SoftwareVersion", &DMREAD, DMT_STRING, get_device_softwareversion, NULL, BBFDM_BOTH}, +{"AdditionalHardwareVersion", &DMREAD, DMT_STRING, get_device_additionalhardwareversion, NULL, BBFDM_BOTH}, +{"AdditionalSoftwareVersion", &DMREAD, DMT_STRING, get_device_additionalsoftwareversion, NULL, BBFDM_BOTH}, +{"ProvisioningCode", &DMWRITE, DMT_STRING, get_device_provisioningcode, set_device_provisioningcode, BBFDM_BOTH}, +{"UpTime", &DMREAD, DMT_UNINT, get_device_info_uptime, NULL, BBFDM_BOTH}, +{"FirstUseDate", &DMREAD, DMT_TIME, get_device_info_firstusedate, NULL, BBFDM_BOTH}, +{"CID", &DMREAD, DMT_STRING, get_deviceinfo_cid, NULL, BBFDM_USP}, +{"FriendlyName", &DMREAD, DMT_STRING, get_deviceinfo_friendlyname, NULL, BBFDM_USP}, +{"PEN", &DMREAD, DMT_STRING, get_deviceinfo_pen, NULL, BBFDM_USP}, +{"ModelNumber", &DMREAD, DMT_STRING, get_deviceinfo_modelnumber, NULL, BBFDM_BOTH}, +{"HostName", &DMWRITE, DMT_STRING, get_DeviceInfo_HostName, set_DeviceInfo_HostName, BBFDM_BOTH}, + +#ifdef SYSMNGR_VENDOR_CONFIG_FILE +{"VendorConfigFileNumberOfEntries", &DMREAD, DMT_UNINT, get_DeviceInfo_VendorConfigFileNumberOfEntries, NULL, BBFDM_BOTH}, +#endif + +#ifdef SYSMNGR_SUPPORTED_DATA_MODEL +{"SupportedDataModelNumberOfEntries", &DMREAD, DMT_UNINT, get_DeviceInfo_SupportedDataModelNumberOfEntries, NULL, BBFDM_CWMP}, +#endif + +#ifdef SYSMNGR_FIRMWARE_IMAGE +{"ActiveFirmwareImage", &DMREAD, DMT_STRING, get_device_active_fwimage, NULL, BBFDM_BOTH, DM_FLAG_REFERENCE}, +{"BootFirmwareImage", &DMWRITE, DMT_STRING, get_device_boot_fwimage, set_device_boot_fwimage, BBFDM_BOTH, DM_FLAG_REFERENCE}, +{"MaxNumberOfActivateTimeWindows", &DMREAD, DMT_UNINT, get_DeviceInfo_MaxNumberOfActivateTimeWindows, NULL, BBFDM_USP}, +{"FirmwareImageNumberOfEntries", &DMREAD, DMT_UNINT, get_DeviceInfo_FirmwareImageNumberOfEntries, NULL, BBFDM_BOTH}, +#endif + +#ifdef SYSMNGR_VENDOR_EXTENSIONS +{BBF_VENDOR_PREFIX"BaseMACAddress", &DMREAD, DMT_STRING, get_deviceinfo_base_mac_addr, NULL, BBFDM_BOTH}, +#endif + +{0} +}; + +/* *** Device. *** */ +DMOBJ tDMDeviceInfoObj[] = { +/* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type*/ +{"DeviceInfo", &DMREAD, NULL, NULL, NULL, NULL, NULL, NULL, tDeviceInfoObj, tDeviceInfoParams, NULL, BBFDM_BOTH}, +{0} +}; + +DM_MAP_OBJ tDynamicObj[] = { +/* parentobj, nextobject, parameter */ +{"Device.", tDMDeviceInfoObj, NULL}, +{0} +}; diff --git a/src/files/usr/share/bbfdm/scripts/bbf_activate_handler.sh b/src/files/usr/share/bbfdm/scripts/bbf_activate_handler.sh new file mode 100755 index 0000000000000000000000000000000000000000..8619310144b7bfcbfa885b5df3c2da0a2ac3590e --- /dev/null +++ b/src/files/usr/share/bbfdm/scripts/bbf_activate_handler.sh @@ -0,0 +1,94 @@ +#!/bin/sh + +# Script to activate image in specified time. +# +# Copyright © 2022 IOPSYS Software Solutions AB +# Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> +# + +ROOT="$(dirname "${0}")" + +CHECK_IDLE_FILE="${ROOT}/bbf_check_idle.sh" +RETRY_TIME=300 +START_TIME=$(date +%s) +MODE="${1}" + +log() { + echo "${@}"|logger -t bbf.activate_firmware -p info +} + +activate_and_reboot_device() { + local bank_id="${1}" + local success + + success=$(ubus call fwbank set_bootbank "{'bank':${bank_id}}" | jsonfilter -e @.success) + if [ "${success}" != "true" ]; then + log "Can't activate the bank id ${bank_id}" + exit 1 + fi + + log "The device will restart after a few seconds" + ubus call rpc-sys reboot + exit 0 +} + +handle_whenidle_mode() { + local bank_id="${1}" + local end_time="${2}" + local force_activation="${3}" + local diff=0 + + [ ! -x "${CHECK_IDLE_FILE}" ] && { + activate_and_reboot_device "${bank_id}" + } + + sh "${CHECK_IDLE_FILE}" + if [ "$?" = "0" ]; then + activate_and_reboot_device "${bank_id}" + else + [ "${end_time}" -gt "$((diff + RETRY_TIME))" ] && { + sleep "${RETRY_TIME}" + } + + diff=$(($(date +%s) - START_TIME)) + fi + + while [ "${end_time}" -gt "${diff}" ]; do + sh "${CHECK_IDLE_FILE}" + if [ "$?" = "0" ]; then + activate_and_reboot_device "${bank_id}" + else + + if [ "${end_time}" -gt "$((diff + RETRY_TIME))" ]; then + sleep "${RETRY_TIME}" + else + break + fi + + diff=$(($(date +%s) - START_TIME)) + fi + + done + + [ "${force_activation}" = "1" ] && { + activate_and_reboot_device "${bank_id}" + } +} + +handle_confirmation_needed_mode() { + + log "[ConfirmationNeeded] mode is not implemented" + exit 0 +} + +######################## main ######################## +if [ "${MODE}" = "Immediately" ] || [ "${MODE}" = "AnyTime" ]; then + activate_and_reboot_device "${2}" +elif [ "${MODE}" = "WhenIdle" ]; then + handle_whenidle_mode "${2}" "${3}" "${4}" +elif [ "${MODE}" = "ConfirmationNeeded" ]; then + handle_confirmation_needed_mode "${2}" "${3}" "${4}" "${5}" "${6}" +else + log "[${MODE}] mode is not supported" + exit 1 +fi diff --git a/src/files/usr/share/bbfdm/scripts/bbf_check_idle.sh b/src/files/usr/share/bbfdm/scripts/bbf_check_idle.sh new file mode 100755 index 0000000000000000000000000000000000000000..076e22ece777b2912191606c128c2736e40c2f62 --- /dev/null +++ b/src/files/usr/share/bbfdm/scripts/bbf_check_idle.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# Script to check idle state +# +# This script is used to determine if the device is in idle state or not +# And any idle state checking should be implemented here +# The value returned by this script is more important because based on it, will decide whether the image can be activated or not +# If (returned idle state == '0'), device is in idle state and can be activated. Otherwise, '1' + + +exit 0 \ No newline at end of file diff --git a/src/files/usr/share/bbfdm/scripts/bbf_reboot_handler.sh b/src/files/usr/share/bbfdm/scripts/bbf_reboot_handler.sh new file mode 100755 index 0000000000000000000000000000000000000000..c2eb7bc4c613985e0b6722c90d930c526894188b --- /dev/null +++ b/src/files/usr/share/bbfdm/scripts/bbf_reboot_handler.sh @@ -0,0 +1,206 @@ +#!/bin/sh + +# Script to handle Reboots Object +# +# Copyright © 2024 IOPSYS Software Solutions AB +# Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> +# + +. /lib/functions.sh + +RESET_REASON_PATH="/tmp/reset_reason" +MAX_RETRIES=3 +RETRY_DELAY=1 + +log() { + echo "$@" | logger -t bbf_reboot -p info +} + +reset_option_counter() { + local option_name=$1 + local option_value=$2 + uci_set "deviceinfo" "globals" "${option_name}" "${option_value}" +} + +increment_option_counter() { + local option_name=$1 + local option_value=$(uci_get "deviceinfo" "globals" "${option_name}" "0") + local counter=$((option_value + 1)) + uci_set "deviceinfo" "globals" "${option_name}" "$counter" +} + +get_boot_trigger() { + local trigger + trigger=$(grep "triggered" ${RESET_REASON_PATH} | cut -d ':' -f2 | xargs) + echo "${trigger}" +} + +get_boot_reason() { + local reason + reason=$(grep "reason" ${RESET_REASON_PATH} | cut -d ':' -f2 | xargs) + echo "${reason}" +} + +calculate_boot_time() { + # Get current time and uptime in seconds + local current_time uptime_seconds boot_time boot_time_formatted + + current_time=$(date +%s) + uptime_seconds=$(awk '{print $1}' /proc/uptime | cut -d. -f1) + + # Calculate the boot time by subtracting the uptime from the current time + boot_time=$((current_time - uptime_seconds)) + + # Convert the boot time to a human-readable format + boot_time_formatted=$(date -d "@$boot_time" +"%Y-%m-%dT%H:%M:%SZ") + + echo "${boot_time_formatted}" +} + +boot_reason_message() { + # Generate a human-readable message based on the boot reason and trigger + local trigger reason + + trigger=$(get_boot_trigger) + + if [ -n "${trigger}" ]; then + case "${trigger}" in + "defaultreset") + echo "FACTORY RESET" + ;; + "upgrade") + echo "FIRMWARE UPGRADE" + ;; + *) + echo "${trigger}" + ;; + esac + else + reason=$(get_boot_reason) + case "${reason}" in + "POR_RESET") + echo "POWER ON RESET" + ;; + *) + echo "${reason}" + ;; + esac + fi +} + +create_reboot_section() { + local trigger=$1 + local reboot_sec + + reboot_sec="reboot_$(date +%Y%m%d%H%M%S)" + uci_add "deviceinfo" "reboot" "${reboot_sec}" + uci_set "deviceinfo" "${reboot_sec}" "time_stamp" "$(calculate_boot_time)" + + if [ "${trigger}" = "upgrade" ]; then + uci_set "deviceinfo" "${reboot_sec}" "firmware_updated" "1" + else + uci_set "deviceinfo" "${reboot_sec}" "firmware_updated" "0" + fi + + if [ "${trigger}" = "defaultreset" ]; then + uci_set "deviceinfo" "${reboot_sec}" "cause" "FactoryReset" + else + local last_reboot_cause + last_reboot_cause=$(uci_get "deviceinfo" "globals" "last_reboot_cause" "LocalReboot") + uci_set "deviceinfo" "${reboot_sec}" "cause" "${last_reboot_cause}" + uci_set "deviceinfo" "globals" "last_reboot_cause" "" + fi + + uci_set "deviceinfo" "${reboot_sec}" "reason" "$(boot_reason_message)" +} + +handle_reboot_action() { + local trigger reason max_reboot_entries retry_count reboot_sec_num + + retry_count=0 + + # Retry fetching the reset reason file + while [ ! -f "${RESET_REASON_PATH}" ] && [ $retry_count -lt $MAX_RETRIES ]; do + log "Warning: '${RESET_REASON_PATH}' not found. Attempt $((retry_count + 1)) of ${MAX_RETRIES}" + sleep $RETRY_DELAY + retry_count=$((retry_count + 1)) + done + + if [ ! -f "${RESET_REASON_PATH}" ]; then + log "Error: '${RESET_REASON_PATH}' is not generated after ${MAX_RETRIES} attempts!!!" + return 1 + fi + + uci_load "deviceinfo" + + trigger=$(get_boot_trigger) + reason=$(get_boot_reason) + + # Reset or increment boot counter based on trigger + if [ "${trigger}" = "defaultreset" ]; then + ## Reset all counters ## + reset_option_counter "boot_count" "1" + reset_option_counter "curr_version_boot_count" "0" + reset_option_counter "watchdog_boot_count" "0" + reset_option_counter "cold_boot_count" "0" + reset_option_counter "warm_boot_count" "0" + else + # Incrementing boot counter + increment_option_counter "boot_count" + fi + + # Reset or increment current version boot counter based on trigger + if [ "${trigger}" = "upgrade" ]; then + # Resetting current version boot counter + reset_option_counter "curr_version_boot_count" "1" + else + # Incrementing current version boot counter + increment_option_counter "curr_version_boot_count" + fi + + # Increment watchdog boot counter if the reason indicates a watchdog reset + if echo "${reason}" | grep -qi "watchdog"; then + # Incrementing watchdog boot counter + increment_option_counter "watchdog_boot_count" + fi + + # Increment cold or warm boot counter based on the reason + if [ "${reason}" = "POR_RESET" ]; then + increment_option_counter "cold_boot_count" + else + increment_option_counter "warm_boot_count" + fi + + # Get the max reboot entries + max_reboot_entries=$(uci_get "deviceinfo" "globals" "max_reboot_entries" "3") + + if [ "${max_reboot_entries}" -eq 0 ]; then + # Commit the UCI changes to persist the configuration + uci_commit "deviceinfo" + return 0 + fi + + if [ $max_reboot_entries -gt 0 ]; then + # Calculate the number of reboot sections in the config + reboot_sec_num=$(uci -q show deviceinfo | grep "=reboot" | wc -l) + + # Delete excess reboot sections if they exceed the max reboot entries + if [ "${reboot_sec_num}" -ge "${max_reboot_entries}" ]; then + local diff=$((reboot_sec_num - max_reboot_entries + 1)) + + for i in $(seq 1 $diff); do + uci_remove "deviceinfo" "@reboot[0]" + done + fi + fi + + # Create a new reboot section with the current boot information + create_reboot_section "${trigger}" + + # Commit the UCI changes to persist the configuration + uci_commit "deviceinfo" +} + +# Run the main function +handle_reboot_action +exit 0 diff --git a/src/fw_images.c b/src/fw_images.c new file mode 100644 index 0000000000000000000000000000000000000000..1f91399053281ef1b46f08f0507f84e4eea0dcc3 --- /dev/null +++ b/src/fw_images.c @@ -0,0 +1,636 @@ +/* + * Copyright (C) 2022-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include "utils.h" + +#define MAX_TIME_WINDOW 5 + +struct sysupgrade_ev_data { + const char *bank_id; + bool status; +}; + +/************************************************************* +* COMMON FUNCTIONS +**************************************************************/ +static void _exec_reboot(const void *arg1, void *arg2) +{ + char config_name[16] = {0}; + + snprintf(config_name, sizeof(config_name), "%s", "deviceinfo"); + + // Set last_reboot_cause to 'RemoteReboot' because the upcoming reboot will be initiated by USP Operate + dmuci_set_value(config_name, "globals", "last_reboot_cause", "RemoteReboot"); + dmuci_commit_package(config_name); + + 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"); + dmubus_call_set("system", "reboot", UBUS_ARGS{0}, 0); + sleep(5); // Wait for reboot + BBF_ERR("Reboot call failed!!!"); + + // Set last_reboot_cause to empty because there is a problem in the system reboot + dmuci_set_value(config_name, "globals", "last_reboot_cause", ""); + dmuci_commit_package(config_name); +} + +static void dmubus_receive_sysupgrade(struct ubus_context *ctx, struct ubus_event_handler *ev, + const char *type, struct blob_attr *msg) +{ + struct dmubus_event_data *data; + struct blob_attr *msg_attr; + + if (!msg || !ev) + return; + + data = container_of(ev, struct dmubus_event_data, ev); + if (data == NULL) + return; + + struct sysupgrade_ev_data *ev_data = (struct sysupgrade_ev_data *)data->ev_data; + if (ev_data == NULL) + return; + + size_t msg_len = (size_t)blobmsg_data_len(msg); + __blob_for_each_attr(msg_attr, blobmsg_data(msg), msg_len) { + if (DM_STRCMP("bank_id", blobmsg_name(msg_attr)) == 0) { + char *attr_val = (char *)blobmsg_data(msg_attr); + if (DM_STRCMP(attr_val, ev_data->bank_id) != 0) + return; + } + + if (DM_STRCMP("status", blobmsg_name(msg_attr)) == 0) { + char *attr_val = (char *)blobmsg_data(msg_attr); + if (DM_STRCMP(attr_val, "Downloading") == 0) + return; + else if (DM_STRCMP(attr_val, "Available") == 0) + ev_data->status = true; + else + ev_data->status = false; + + } + } + + uloop_end(); + return; +} + +static int bbf_fw_image_download(const char *url, const char *auto_activate, const char *username, const char *password, + const char *file_size, const char *checksum_algorithm, const char *checksum, + const char *bank_id, const char *command, const char *obj_path, const char *commandKey, char *keep) +{ + char fw_image_path[256] = {0}; + json_object *json_obj = NULL; + bool activate = false, valid = false; + int res = 0; + char fault_msg[128] = {0}; + time_t complete_time = 0; + time_t start_time = time(NULL); + + DM_STRNCPY(fw_image_path, "/tmp/firmware-XXXXXX", sizeof(fw_image_path)); + + // Check the file system size if there is sufficient space for downloading the firmware image + if (!validate_file_system_size(file_size)) { + res = -1; + snprintf(fault_msg, sizeof(fault_msg), "Available memory space is lower than required for downloading"); + goto end; + } + + res = mkstemp(fw_image_path); + if (res == -1) { + snprintf(fault_msg, sizeof(fault_msg), "Operation failed due to some internal failure"); + goto end; + } else { + close(res); // close the fd, as only filename required + res = 0; + } + + // Download the firmware image + long res_code = download_file(fw_image_path, url, username, password); + complete_time = time(NULL); + + // Check if the download operation was successful + if (!validate_server_response_code(url, res_code)) { + snprintf(fault_msg, sizeof(fault_msg), "Download operation is failed, fault code (%ld)", res_code); + res = -1; + goto end; + } + + // Validate the CheckSum value according to its algorithm + if (!validate_checksum_value(fw_image_path, checksum_algorithm, checksum)) { + res = -1; + snprintf(fault_msg, sizeof(fault_msg), "Checksum of the file is not matched with the specified value"); + goto end; + } + + string_to_bool(auto_activate, &activate); + char *act = (activate) ? "1" : "0"; + + dmubus_call_blocking("system", "validate_firmware_image", UBUS_ARGS{{"path", fw_image_path, String}}, 1, &json_obj); + if (json_obj == NULL) { + res = -1; + snprintf(fault_msg, sizeof(fault_msg), "Failed in validation of the file"); + goto end; + } + + char *val = dmjson_get_value(json_obj, 1, "valid"); + string_to_bool(val, &valid); + json_object_put(json_obj); + json_obj = NULL; + if (valid == false) { + snprintf(fault_msg, sizeof(fault_msg), "File is not a valid firmware image"); + res = -1; + goto end; + } + + // default state is to preserve the config over firmware upgrades + char *keep_config = DM_STRLEN((char *)keep) ? keep : "1"; + + // Apply Firmware Image + dmubus_call_blocking("fwbank", "upgrade", UBUS_ARGS{{"path", fw_image_path, String}, {"auto_activate", act, Boolean}, {"bank", bank_id, Integer}, {"keep_settings", keep_config, Boolean}}, 4, &json_obj); + if (json_obj == NULL) { + res = 1; + snprintf(fault_msg, sizeof(fault_msg), "Internal error occurred when applying the firmware"); + goto end; + } + + struct sysupgrade_ev_data ev_data = { + .bank_id = bank_id, + .status = false, + }; + + dmubus_wait_for_event("sysupgrade", 120, &ev_data, dmubus_receive_sysupgrade, NULL); + + if (ev_data.status == false) { + res = 1; + snprintf(fault_msg, sizeof(fault_msg), "Failed to apply the downloaded image file"); + goto end; + } + + // Schedule a device Reboot, if auto activation is true + if (activate) { + bbfdm_task_fork(_exec_reboot, NULL, NULL, NULL); + } + +end: + // Send the transfer complete event + send_transfer_complete_event(command, obj_path, url, fault_msg, start_time, complete_time, commandKey, "Download"); + + // Remove temporary file if ubus upgrade failed and file exists + if (!json_obj && file_exists(fw_image_path) && strncmp(url, FILE_URI, strlen(FILE_URI))) { + remove(fw_image_path); + res = -1; + } + + if (json_obj != NULL) + json_object_put(json_obj); + + return res; +} + +/************************************************************* +* ENTRY METHOD +**************************************************************/ +int browseDeviceInfoFirmwareImageInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) +{ + json_object *res = NULL, *bank_obj = NULL, *arrobj = NULL; + struct dm_data curr_data = {0}; + char *inst = NULL; + int id = 0, i = 0; + + dmubus_call("fwbank", "dump", UBUS_ARGS{0}, 0, &res); + + dmjson_foreach_obj_in_array(res, arrobj, bank_obj, i, 1, "bank") { + + curr_data.json_object = bank_obj; + + inst = handle_instance_without_section(dmctx, parent_node, ++id); + + if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)&curr_data, inst) == DM_STOP) + break; + } + return 0; +} + +/************************************************************* +* GET & SET PARAM +**************************************************************/ +int get_device_active_fwimage(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + json_object *res = NULL, *bank_obj = NULL, *arrobj = NULL; + char linker[16] = {0}, *id = NULL; + int i = 0; + + dmubus_call("fwbank", "dump", UBUS_ARGS{0}, 0, &res); + dmjson_foreach_obj_in_array(res, arrobj, bank_obj, i, 1, "bank") { + char *active = dmjson_get_value(bank_obj, 1, "active"); + if (active && DM_LSTRCMP(active, "true") == 0) { + id = dmjson_get_value(bank_obj, 1, "id"); + break; + } + } + + if (DM_STRLEN(id) == 0) { + *value = dmstrdup(""); + return 0; + } + + snprintf(linker, sizeof(linker), "cpe-%s", id); + _bbfdm_get_references(ctx, "Device.DeviceInfo.FirmwareImage.", "Alias", linker, value); + return 0; +} + +int get_device_boot_fwimage(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + json_object *res = NULL, *bank_obj = NULL, *arrobj = NULL; + char linker[16] = {0}, *id = NULL; + int i = 0; + + dmubus_call("fwbank", "dump", UBUS_ARGS{0}, 0, &res); + dmjson_foreach_obj_in_array(res, arrobj, bank_obj, i, 1, "bank") { + char *boot = dmjson_get_value(bank_obj, 1, "boot"); + if (boot && DM_LSTRCMP(boot, "true") == 0) { + id = dmjson_get_value(bank_obj, 1, "id"); + break; + } + } + + if (DM_STRLEN(id) == 0) { + *value = dmstrdup(""); + return 0; + } + + snprintf(linker, sizeof(linker), "cpe-%s", id); + _bbfdm_get_references(ctx, "Device.DeviceInfo.FirmwareImage.", "Alias", linker, value); + return 0; +} + +int set_device_boot_fwimage(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + char *allowed_objects[] = {"Device.DeviceInfo.FirmwareImage.", NULL}; + struct dm_reference reference = {0}; + + bbfdm_get_reference_linker(ctx, value, &reference); + + switch (action) { + case VALUECHECK: + if (bbfdm_validate_string(ctx, reference.path, -1, -1, NULL, NULL)) + return FAULT_9007; + + if (dm_validate_allowed_objects(ctx, &reference, allowed_objects)) + return FAULT_9007; + + break; + case VALUESET: + if (DM_STRLEN(reference.value)) { + struct uci_section *dmmap_s = NULL; + json_object *res = NULL; + char *available = NULL; + + char *bank_id = DM_STRCHR(reference.value, '-'); // Get bank id 'X' which is linker from Alias prefix 'cpe-X' + if (!bank_id) + return FAULT_9001; + + get_dmmap_section_of_config_section_cont("dmmap_fw_image", "fw_image", "id", bank_id + 1, &dmmap_s); + dmuci_get_value_by_section_string(dmmap_s, "available", &available); + if (DM_LSTRCMP(available, "false") == 0) + return FAULT_9001; + + dmubus_call("fwbank", "set_bootbank", UBUS_ARGS{{"bank", bank_id + 1, Integer}}, 1, &res); + char *success = dmjson_get_value(res, 1, "success"); + if (DM_LSTRCMP(success, "true") != 0) + return FAULT_9001; + + } + break; + } + return 0; +} + +int get_DeviceInfo_MaxNumberOfActivateTimeWindows(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmasprintf(value, "%d", MAX_TIME_WINDOW); + return 0; +} + +int get_DeviceInfo_FirmwareImageNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + int cnt = get_number_of_entries(ctx, data, instance, browseDeviceInfoFirmwareImageInst); + dmasprintf(value, "%d", cnt); + return 0; +} + +static int get_DeviceInfoFirmwareImage_Alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + char *id = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "id"); + dmasprintf(value, "cpe-%s", id ? id : instance); + return 0; +} + +static int set_DeviceInfoFirmwareImage_Alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + switch (action) { + case VALUECHECK: + if (bbfdm_validate_string(ctx, value, -1, 64, NULL, NULL)) + return FAULT_9007; + break; + case VALUESET: + bbfdm_set_fault_message(ctx, "Internal designated unique identifier, not allowed to update"); + return FAULT_9007; + } + return 0; +} + +static int get_DeviceInfoFirmwareImage_Name(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + char *name; + + name = dmstrdup(dmjson_get_value(((struct dm_data *)data)->json_object, 1, "fwver")); + if (DM_STRLEN(name) > 64 ) { + name[64] = '\0'; + } + + *value = name; + return 0; +} + +static int get_DeviceInfoFirmwareImage_Version(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "swver"); + return 0; +} + +static int get_DeviceInfoFirmwareImage_Available(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + struct uci_section *s = NULL; + + char *id = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "id"); + + uci_path_foreach_option_eq(bbfdm, "dmmap_fw_image", "fw_image", "id", id, s) { + dmuci_get_value_by_section_string(s, "available", value); + break; + } + + if ((*value)[0] == '\0') + *value = dmstrdup("true"); + return 0; +} + +static int set_DeviceInfoFirmwareImage_Available(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + struct uci_section *s = NULL, *dmmap = NULL; + char *id = NULL; + bool b; + + switch (action) { + case VALUECHECK: + if (bbfdm_validate_boolean(ctx, value)) + return FAULT_9007; + break; + case VALUESET: + string_to_bool(value, &b); + + if (!b) { + char *boot = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "boot"); + char *active = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "active"); + if (DM_LSTRCMP(boot, "true") == 0 || DM_LSTRCMP(active, "true") == 0) + return FAULT_9001; + } + + id = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "id"); + + uci_path_foreach_option_eq(bbfdm, "dmmap_fw_image", "fw_image", "id", id, s) { + dmuci_set_value_by_section_bbfdm(s, "available", b ? "true" : "false"); + return 0; + } + + dmuci_add_section_bbfdm("dmmap_fw_image", "fw_image", &dmmap); + dmuci_set_value_by_section(dmmap, "id", id); + dmuci_set_value_by_section(dmmap, "available", b ? "true" : "false"); + break; + } + return 0; +} + +static int get_DeviceInfoFirmwareImage_Status(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "status"); + return 0; +} + +/************************************************************* + * OPERATE COMMANDS + *************************************************************/ +static operation_args firmware_image_download_args = { + .in = (const char *[]) { + "URL", + "AutoActivate", + "Username", + "Password", + "FileSize", + "CheckSumAlgorithm", + "CheckSum", + "CommandKey", + BBF_VENDOR_PREFIX"KeepConfig", + NULL + } +}; + +static int get_operate_args_DeviceInfoFirmwareImage_Download(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = (char *)&firmware_image_download_args; + return 0; +} + +static int operate_DeviceInfoFirmwareImage_Download(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + const char *command = "Download()"; + char obj_path[256] = {0}; + + char *ret = DM_STRRCHR(refparam, '.'); + if (!ret) + return USP_FAULT_INVALID_ARGUMENT; + + if ((ret - refparam + 2) < sizeof(obj_path)) + snprintf(obj_path, ret - refparam + 2, "%s", refparam); + + char *url = dmjson_get_value((json_object *)value, 1, "URL"); + char *auto_activate = dmjson_get_value((json_object *)value, 1, "AutoActivate"); + if (url[0] == '\0') + return USP_FAULT_INVALID_ARGUMENT; + + // Assuming auto activate as false, if not provided by controller, in case of strict validation, + // this should result into a fault + if (DM_STRLEN(auto_activate) == 0) + auto_activate = dmstrdup("0"); + + char *username = dmjson_get_value((json_object *)value, 1, "Username"); + char *password = dmjson_get_value((json_object *)value, 1, "Password"); + char *file_size = dmjson_get_value((json_object *)value, 1, "FileSize"); + char *checksum_algorithm = dmjson_get_value((json_object *)value, 1, "CheckSumAlgorithm"); + char *checksum = dmjson_get_value((json_object *)value, 1, "CheckSum"); + char *commandKey = dmjson_get_value((json_object *)value, 1, "CommandKey"); + char *keep_config = dmjson_get_value((json_object *)value, 1, BBF_VENDOR_PREFIX"KeepConfig"); + + char *bank_id = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "id"); + + int res = bbf_fw_image_download(url, auto_activate, username, password, file_size, checksum_algorithm, checksum, bank_id, command, obj_path, commandKey, keep_config); + + if (res == 1) { + bbfdm_set_fault_message(ctx, "Firmware validation failed"); + } + + return res ? USP_FAULT_COMMAND_FAILURE : 0; +} + +static operation_args firmware_image_activate_args = { + .in = (const char *[]) { + + "TimeWindow.{i}.Start", + "TimeWindow.{i}.End", + "TimeWindow.{i}.Mode", + "TimeWindow.{i}.UserMessage", + "TimeWindow.{i}.MaxRetries", + NULL + } +}; + +static int get_operate_args_DeviceInfoFirmwareImage_Activate(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = (char *)&firmware_image_activate_args; + return 0; +} + +static int operate_DeviceInfoFirmwareImage_Activate(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ +#define CRONTABS_ROOT "/etc/crontabs/root" +#define ACTIVATE_HANDLER_FILE "/usr/share/bbfdm/scripts/bbf_activate_handler.sh" + + char *FW_Mode[] = {"AnyTime", "Immediately", "WhenIdle", "ConfirmationNeeded", NULL}; + char *start_time[MAX_TIME_WINDOW] = {0}; + char *end_time[MAX_TIME_WINDOW] = {0}; + char *mode[MAX_TIME_WINDOW] = {0}; + char *user_message[MAX_TIME_WINDOW] = {0}; + char *max_retries[MAX_TIME_WINDOW] = {0}; + int res = 0, last_idx = -1; + + for (int i = 0; i < MAX_TIME_WINDOW; i++) { + char buf[32] = {0}; + + snprintf(buf, sizeof(buf), "TimeWindow.%d.Start", i + 1); + start_time[i] = dmjson_get_value((json_object *)value, 1, buf); + + snprintf(buf, sizeof(buf), "TimeWindow.%d.End", i + 1); + end_time[i] = dmjson_get_value((json_object *)value, 1, buf); + + snprintf(buf, sizeof(buf), "TimeWindow.%d.Mode", i + 1); + mode[i] = dmjson_get_value((json_object *)value, 1, buf); + + snprintf(buf, sizeof(buf), "TimeWindow.%d.UserMessage", i + 1); + user_message[i] = dmjson_get_value((json_object *)value, 1, buf); + + snprintf(buf, sizeof(buf), "TimeWindow.%d.MaxRetries", i + 1); + max_retries[i] = dmjson_get_value((json_object *)value, 1, buf); + + if (!DM_STRLEN(start_time[i])) + break; + + if (!DM_STRLEN(end_time[i]) || !DM_STRLEN(mode[i])) + return USP_FAULT_INVALID_ARGUMENT; + + if (bbfdm_validate_unsignedInt(ctx, start_time[i], RANGE_ARGS{{NULL,NULL}}, 1)) + return USP_FAULT_INVALID_ARGUMENT; + + if (bbfdm_validate_unsignedInt(ctx, end_time[i], RANGE_ARGS{{NULL,NULL}}, 1)) + return USP_FAULT_INVALID_ARGUMENT; + + if (DM_STRLEN(max_retries[i]) && bbfdm_validate_int(ctx, max_retries[i], RANGE_ARGS{{"-1","10"}}, 1)) + return USP_FAULT_INVALID_ARGUMENT; + + if (bbfdm_validate_string(ctx, mode[i], -1, -1, FW_Mode, NULL)) + return USP_FAULT_INVALID_ARGUMENT; + + if (DM_STRTOL(start_time[i]) > DM_STRTOL(end_time[i])) + return USP_FAULT_INVALID_ARGUMENT; + + if (i != 0 && DM_STRTOL(end_time[i - 1]) > DM_STRTOL(start_time[i])) + return USP_FAULT_INVALID_ARGUMENT; + + last_idx++; + } + + char *bank_id = dmjson_get_value(((struct dm_data *)data)->json_object, 1, "id"); + if (!DM_STRLEN(bank_id)) + return USP_FAULT_COMMAND_FAILURE; + + if (DM_STRLEN(start_time[0])) { + FILE *file = fopen(CRONTABS_ROOT, "a"); + if (!file) + return USP_FAULT_COMMAND_FAILURE; + + for (int i = 0; i < MAX_TIME_WINDOW && DM_STRLEN(start_time[i]); i++) { + char buffer[512] = {0}; + time_t t_time = time(NULL); + long int start_t = (DM_STRTOL(start_time[i]) > 60) ? DM_STRTOL(start_time[i]) : 60; + t_time += start_t; + struct tm *tm_local = localtime(&t_time); + + snprintf(buffer, sizeof(buffer), "%d %d %d %d * sh %s '%s' '%s' '%ld' '%d' '%s' '%s'\n", + tm_local->tm_min, + tm_local->tm_hour, + tm_local->tm_mday, + tm_local->tm_mon + 1, + ACTIVATE_HANDLER_FILE, + mode[i], + bank_id, + (DM_STRTOL(end_time[i]) - DM_STRTOL(start_time[i])), + (i == last_idx), + user_message[i], + max_retries[i]); + + fprintf(file, "%s", buffer); + } + + fclose(file); + + res = dmcmd_no_wait("/etc/init.d/cron", 1, "restart"); + } else { + json_object *json_obj = NULL; + + dmubus_call("fwbank", "set_bootbank", UBUS_ARGS{{"bank", bank_id, Integer}}, 1, &json_obj); + char *status = dmjson_get_value(json_obj, 1, "success"); + if (strcasecmp(status, "true") != 0) + return USP_FAULT_COMMAND_FAILURE; + + bbfdm_task_fork(_exec_reboot, NULL, NULL, NULL); + } + + return res ? USP_FAULT_COMMAND_FAILURE : 0; +} + +/********************************************************************************************************************************** +* OBJ & LEAF DEFINITION +***********************************************************************************************************************************/ +/* *** Device.DeviceInfo.FirmwareImage.{i}. *** */ +DMLEAF tDeviceInfoFirmwareImageParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version*/ +{"Alias", &DMWRITE, DMT_STRING, get_DeviceInfoFirmwareImage_Alias, set_DeviceInfoFirmwareImage_Alias, BBFDM_BOTH, DM_FLAG_UNIQUE|DM_FLAG_LINKER}, +{"Name", &DMREAD, DMT_STRING, get_DeviceInfoFirmwareImage_Name, NULL, BBFDM_BOTH}, +{"Version", &DMREAD, DMT_STRING, get_DeviceInfoFirmwareImage_Version, NULL, BBFDM_BOTH}, +{"Available", &DMWRITE, DMT_BOOL, get_DeviceInfoFirmwareImage_Available, set_DeviceInfoFirmwareImage_Available, BBFDM_BOTH}, +{"Status", &DMREAD, DMT_STRING, get_DeviceInfoFirmwareImage_Status, NULL, BBFDM_BOTH}, +{"BootFailureLog", &DMREAD, DMT_STRING, get_empty, NULL, BBFDM_BOTH}, +{"Download()", &DMASYNC, DMT_COMMAND, get_operate_args_DeviceInfoFirmwareImage_Download, operate_DeviceInfoFirmwareImage_Download, BBFDM_USP}, +{"Activate()", &DMASYNC, DMT_COMMAND, get_operate_args_DeviceInfoFirmwareImage_Activate, operate_DeviceInfoFirmwareImage_Activate, BBFDM_USP}, +{0} +}; diff --git a/src/fw_images.h b/src/fw_images.h new file mode 100644 index 0000000000000000000000000000000000000000..23047d9f375bf591c7e3a4cf1e3d5475853ba8af --- /dev/null +++ b/src/fw_images.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2022-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#ifndef __FW_IMAGES_H +#define __FW_IMAGES_H + +extern DMLEAF tDeviceInfoFirmwareImageParams[]; + +int browseDeviceInfoFirmwareImageInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance); + +int get_device_active_fwimage(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value); +int get_device_boot_fwimage(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value); +int set_device_boot_fwimage(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action); +int get_DeviceInfo_MaxNumberOfActivateTimeWindows(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value); +int get_DeviceInfo_FirmwareImageNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value); + +#endif //__FW_IMAGES_H diff --git a/src/memory.c b/src/memory.c new file mode 100644 index 0000000000000000000000000000000000000000..6f8cf1f2ff8f2f69d4f823f9e30d675eaca1732a --- /dev/null +++ b/src/memory.c @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2019-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include "utils.h" + +/************************************************************* +* GET & SET PARAM +**************************************************************/ +static int get_memory_status_total(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + json_object *res = NULL; + dmubus_call("system", "info", UBUS_ARGS{{}}, 0, &res); + DM_ASSERT(res, *value = dmstrdup("0")); + char *total = dmjson_get_value(res, 2, "memory", "total"); + dmasprintf(value, "%lu", DM_STRTOUL(total) / 1024); + return 0; +} + +static int get_memory_status_free(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + json_object *res = NULL; + dmubus_call("system", "info", UBUS_ARGS{{}}, 0, &res); + DM_ASSERT(res, *value = dmstrdup("0")); + char *free = dmjson_get_value(res, 2, "memory", "free"); + dmasprintf(value, "%lu", DM_STRTOUL(free) / 1024); + return 0; +} + +static int get_memory_status_total_persistent(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + struct statvfs dinfo; + + if (statvfs("/overlay/", &dinfo) == 0) { + unsigned int total = (dinfo.f_bsize * dinfo.f_blocks) / 1024; + dmasprintf(value, "%u", total); + } else { + *value = dmstrdup("0"); + } + + return 0; +} + +static int get_memory_status_free_persistent(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + struct statvfs dinfo; + + if (statvfs("/overlay/", &dinfo) == 0) { + unsigned int free = (dinfo.f_bsize * dinfo.f_bavail) / 1024; + dmasprintf(value, "%u", free); + } else { + *value = dmstrdup("0"); + } + + return 0; +} + +/********************************************************************************************************************************** +* OBJ & LEAF DEFINITION +***********************************************************************************************************************************/ +/* *** Device.DeviceInfo.MemoryStatus. *** */ +DMLEAF tDeviceInfoMemoryStatusParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version*/ +{"Total", &DMREAD, DMT_UNINT, get_memory_status_total, NULL, BBFDM_BOTH}, +{"Free", &DMREAD, DMT_UNINT, get_memory_status_free, NULL, BBFDM_BOTH}, +{"TotalPersistent", &DMREAD, DMT_UNINT, get_memory_status_total_persistent, NULL, BBFDM_BOTH}, +{"FreePersistent", &DMREAD, DMT_UNINT, get_memory_status_free_persistent, NULL, BBFDM_BOTH}, +{0} +}; diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000000000000000000000000000000000000..5607f7f3ff8035d156f96f1e6fc264a22252eed1 --- /dev/null +++ b/src/memory.h @@ -0,0 +1,17 @@ +/* + * Copyright (C) 2019-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#ifndef __MEMORY_H +#define __MEMORY_H + +extern DMLEAF tDeviceInfoMemoryStatusParams[]; + +#endif //__MEMORY_H diff --git a/src/processes.c b/src/processes.c new file mode 100644 index 0000000000000000000000000000000000000000..32ca26ba6d5e22a57541962c03dbf75745457626 --- /dev/null +++ b/src/processes.c @@ -0,0 +1,448 @@ +/* + * Copyright (C) 2019-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include "utils.h" + +extern struct list_head global_memhead; + +LIST_HEAD(process_list); +static int process_count = 0; + +#define PROCPS_BUFSIZE 1024 + +struct process_entry { + struct list_head list; + + char command[256]; + char state[16]; + char pid[8]; + char size[8]; + char priority[8]; + char cputime[8]; +}; + +typedef struct jiffy_counts_t { + unsigned long long usr, nic, sys, idle; + unsigned long long iowait, irq, softirq, steal; + unsigned long long total; + unsigned long long busy; +} jiffy_counts_t; + +/************************************************************* +* COMMON FUNCTIONS +**************************************************************/ +static void get_jif_val(jiffy_counts_t *p_jif) +{ + FILE *file = NULL; + char line[128]; + int ret; + + if ((file = fopen("/proc/stat", "r"))) { + while(fgets(line, sizeof(line), file) != NULL) + { + remove_new_line(line); + ret = sscanf(line, "cpu %llu %llu %llu %llu %llu %llu %llu %llu", &p_jif->usr, &p_jif->nic, &p_jif->sys, &p_jif->idle, + &p_jif->iowait, &p_jif->irq, &p_jif->softirq, &p_jif->steal); + + if (ret >= 4) { + p_jif->total = p_jif->usr + p_jif->nic + p_jif->sys + p_jif->idle + + p_jif->iowait + p_jif->irq + p_jif->softirq + p_jif->steal; + + p_jif->busy = p_jif->total - p_jif->idle - p_jif->iowait; + break; + } + } + fclose(file); + } +} + +static unsigned int get_cpu_load(jiffy_counts_t *prev_jif, jiffy_counts_t *cur_jif) +{ + unsigned total_diff, cpu; + + total_diff = (unsigned)(cur_jif->total - prev_jif->total); + + if (total_diff == 0) + total_diff = 1; + + cpu = 100 * (unsigned)(cur_jif->busy - prev_jif->busy) / total_diff; + + return cpu; +} + +static unsigned int get_cpu_usage(void) +{ + jiffy_counts_t prev_jif = {0}; + jiffy_counts_t cur_jif = {0}; + + get_jif_val(&prev_jif); + usleep(100000); + get_jif_val(&cur_jif); + + return get_cpu_load(&prev_jif, &cur_jif); +} + +static bool is_update_process_allowed(void) +{ + char *tr069_status = NULL; + + if (dmubus_object_method_exists("tr069")) { + struct uci_section *s = NULL, *stmp = NULL; + uci_path_foreach_sections_safe(varstate, "icwmp", "sess_status", stmp, s) { + dmuci_get_value_by_section_string(s, "current_status", &tr069_status); + } + } + + if (tr069_status == NULL) + goto end; + + if (strcmp(tr069_status, "running") == 0) { + return false; + } + +end: + return true; +} + +static char *get_proc_state(char state) +{ + switch(state) { + case 'R': + return "Running"; + case 'S': + return "Sleeping"; + case 'T': + return "Stopped"; + case 'D': + return "Uninterruptible"; + case 'Z': + return "Zombie"; + case 'I': + return "Idle"; + }; + + return "Idle"; +} + +static struct process_entry *check_entry_exists(const char *pid) +{ + struct process_entry *entry = NULL; + + list_for_each_entry(entry, &process_list, list) { + if (DM_STRCMP(entry->pid, pid) == 0) + return entry; + } + + return NULL; +} + +static void check_killed_process(void) +{ + struct process_entry *entry = NULL; + struct process_entry *entry_tmp = NULL; + char fstat[32]; + + list_for_each_entry_safe(entry, entry_tmp, &process_list, list) { + + snprintf(fstat, sizeof(fstat), "/proc/%s/stat", entry->pid); + if (file_exists(fstat)) + continue; + + list_del(&entry->list); + dmfree(entry); + } +} + +static void procps_get_cmdline(char *buf, int bufsz, const char *pid, const char *comm) +{ + int sz; + char filename[270]; + + snprintf(filename, sizeof(filename), "/proc/%s/cmdline", pid); + sz = dm_file_to_buf(filename, buf, bufsz); + if (sz > 0) { + const char *base; + int comm_len; + + while (--sz >= 0 && buf[sz] == '\0') + continue; + /* Prevent basename("process foo/bar") = "bar" */ + strchrnul(buf, ' ')[0] = '\0'; + base = basename(buf); /* before we replace argv0's NUL with space */ + while (sz >= 0) { + if ((unsigned char)(buf[sz]) < ' ') + buf[sz] = ' '; + sz--; + } + if (base[0] == '-') /* "-sh" (login shell)? */ + base++; + + /* If comm differs from argv0, prepend "{comm} ". + * It allows to see thread names set by prctl(PR_SET_NAME). + */ + if (!comm) + return; + comm_len = strlen(comm); + /* Why compare up to comm_len? + * Well, some processes rewrite argv, and use _spaces_ there + * while rewriting. (KDE is observed to do it). + * I prefer to still treat argv0 "process foo bar" + * as 'equal' to comm "process". + */ + if (strncmp(base, comm, comm_len) != 0) { + comm_len += 3; + if (bufsz > comm_len) + memmove(buf + comm_len, buf, bufsz - comm_len); + snprintf(buf, bufsz, "{%s}", comm); + if (bufsz <= comm_len) + return; + buf[comm_len - 1] = ' '; + buf[bufsz - 1] = '\0'; + } + } else { + snprintf(buf, bufsz, "[%s]", comm ? comm : "?"); + } +} + +static void init_processes(void) +{ + DIR *dir = NULL; + struct dirent *entry = NULL; + struct stat stats = {0}; + char buf[PROCPS_BUFSIZE]; + char fstat[288]; + char command[256]; + char comm[32]; + char bsize[32]; + char cputime[32]; + char priori[32]; + char *comm1 = NULL; + char *comm2 = NULL; + char state; + unsigned long stime; + unsigned long utime; + unsigned long vsize; + int priority, n; + int curr_process_idx = 0; + + if (!is_update_process_allowed()) + return; + + check_killed_process(); + + dir = opendir("/proc"); + if (dir == NULL) + return; + + while ((entry = readdir(dir)) != NULL) { + struct process_entry *pentry = NULL; + struct process_entry *pentry_exits = NULL; + + int digit = entry->d_name[0] - '0'; + if (digit < 0 || digit > 9) + continue; + + snprintf(fstat, sizeof(fstat), "/proc/%s/stat", entry->d_name); + if (stat(fstat, &stats)) + continue; + + n = dm_file_to_buf(fstat, buf, PROCPS_BUFSIZE); + if (n < 0) + continue; + + comm2 = strrchr(buf, ')'); /* split into "PID (cmd" and "<rest>" */ + if (!comm2) /* sanity check */ + continue; + + comm2[0] = '\0'; + comm1 = strchr(buf, '('); + if (!comm1) /* sanity check */ + continue; + + DM_STRNCPY(comm, comm1 + 1, sizeof(comm)); + + n = sscanf(comm2 + 2, /* Flawfinder: ignore */ \ + "%c %*u " /* state, ppid */ + "%*u %*u %*d %*s " /* pgid, sid, tty, tpgid */ + "%*s %*s %*s %*s %*s " /* flags, min_flt, cmin_flt, maj_flt, cmaj_flt */ + "%lu %lu " /* utime, stime */ + "%*u %*u %d " /* cutime, cstime, priority */ + "%*d " /* niceness */ + "%*s %*s " /* timeout, it_real_value */ + "%*s " /* start_time */ + "%lu " /* vsize */ + , + &state, + &utime, &stime, + &priority, + &vsize + ); + + if (n != 5) + continue; + + procps_get_cmdline(command, sizeof(command), entry->d_name, comm); + curr_process_idx++; + + snprintf(cputime, sizeof(cputime), "%lu", ((stime / sysconf(_SC_CLK_TCK)) + (utime / sysconf(_SC_CLK_TCK))) * 1000); + snprintf(bsize, sizeof(bsize), "%lu", vsize >> 10); + snprintf(priori, sizeof(priori), "%u", (unsigned)round((priority + 100) * 99 / 139)); + + if (process_count == 0 || !(pentry_exits = check_entry_exists(entry->d_name))) { + + pentry = dm_dynamic_calloc(&global_memhead, 1, sizeof(struct process_entry)); + if (!pentry) + return; + + list_add_tail(&pentry->list, &process_list); + } + + if (pentry_exits) + pentry = pentry_exits; + + DM_STRNCPY(pentry->pid, entry->d_name, sizeof(pentry->pid)); + DM_STRNCPY(pentry->command, command, sizeof(pentry->command)); + DM_STRNCPY(pentry->size, bsize, sizeof(pentry->size)); + DM_STRNCPY(pentry->priority, priori, sizeof(pentry->priority)); + DM_STRNCPY(pentry->cputime, cputime, sizeof(pentry->cputime)); + DM_STRNCPY(pentry->state, get_proc_state(state), sizeof(pentry->state)); + } + + closedir(dir); + process_count = curr_process_idx; +} + + + + +/************************************************************* +* ENTRY METHOD +**************************************************************/ +static int browseProcessEntriesInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) +{ + struct process_entry *entry = NULL; + struct dm_data curr_data = {0}; + char *inst = NULL; + int id = 0; + + init_processes(); + list_for_each_entry(entry, &process_list, list) { + + curr_data.additional_data = entry; + + inst = handle_instance_without_section(dmctx, parent_node, ++id); + + if (DM_LINK_INST_OBJ(dmctx, parent_node, &curr_data, inst) == DM_STOP) + break; + } + return 0; +} + + + + + +/************************************************************* +* GET & SET PARAM +**************************************************************/ +static int get_process_cpu_usage(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmasprintf(value, "%u", get_cpu_usage()); + return 0; +} + +static int get_process_number_of_entries(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + int cnt = get_number_of_entries(ctx, data, instance, browseProcessEntriesInst); + dmasprintf(value, "%d", cnt); + return 0; +} + +static int get_process_pid(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = data ? ((struct process_entry *)((struct dm_data *)data)->additional_data)->pid : ""; + return 0; +} + +static int get_process_command(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = data ? ((struct process_entry *)((struct dm_data *)data)->additional_data)->command : ""; + return 0; +} + +static int get_process_size(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = data ? ((struct process_entry *)((struct dm_data *)data)->additional_data)->size : ""; + return 0; +} + +static int get_process_priority(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = data ? ((struct process_entry *)((struct dm_data *)data)->additional_data)->priority : ""; + return 0; +} + +static int get_process_cpu_time(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = data ? ((struct process_entry *)((struct dm_data *)data)->additional_data)->cputime : ""; + return 0; +} + +static int get_process_state(char* refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = data ? ((struct process_entry *)((struct dm_data *)data)->additional_data)->state : ""; + return 0; +} + + + + + + + + + + + + + + +/********************************************************************************************************************************** +* OBJ & LEAF DEFINITION +***********************************************************************************************************************************/ +/* *** Device.DeviceInfo.ProcessStatus.Process.{i}. *** */ +DMLEAF tDeviceInfoProcessStatusProcessParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version*/ +{"PID", &DMREAD, DMT_UNINT, get_process_pid, NULL, BBFDM_BOTH, DM_FLAG_UNIQUE}, +{"Command", &DMREAD, DMT_STRING, get_process_command, NULL, BBFDM_BOTH}, +{"Size", &DMREAD, DMT_UNINT, get_process_size, NULL, BBFDM_BOTH}, +{"Priority", &DMREAD, DMT_UNINT, get_process_priority, NULL, BBFDM_BOTH}, +{"CPUTime", &DMREAD, DMT_UNINT, get_process_cpu_time, NULL, BBFDM_BOTH}, +{"State", &DMREAD, DMT_STRING, get_process_state, NULL, BBFDM_BOTH}, +{0} +}; + +/* *** Device.DeviceInfo.ProcessStatus. *** */ +DMOBJ tDeviceInfoProcessStatusObj[] = { +/* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type, uniqueKeys, version*/ +{"Process", &DMREAD, NULL, NULL, NULL, browseProcessEntriesInst, NULL, NULL, NULL, tDeviceInfoProcessStatusProcessParams, NULL, BBFDM_BOTH, NULL}, +{0} +}; + +DMLEAF tDeviceInfoProcessStatusParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version*/ +{"CPUUsage", &DMREAD, DMT_UNINT, get_process_cpu_usage, NULL, BBFDM_BOTH}, +{"ProcessNumberOfEntries", &DMREAD, DMT_UNINT, get_process_number_of_entries, NULL, BBFDM_BOTH}, +{0} +}; + + + diff --git a/src/processes.h b/src/processes.h new file mode 100644 index 0000000000000000000000000000000000000000..f75f8540be1e004ddd2ade7404edaf4c6a20ceb5 --- /dev/null +++ b/src/processes.h @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2019-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#ifndef __PROCESSES_H +#define __PROCESSES_H + +extern DMOBJ tDeviceInfoProcessStatusObj[]; +extern DMLEAF tDeviceInfoProcessStatusParams[]; + +#endif //__PROCESSES_H diff --git a/src/reboots.c b/src/reboots.c new file mode 100644 index 0000000000000000000000000000000000000000..dc71b8cb7a3a988da953bf02a656aab764d5aa7f --- /dev/null +++ b/src/reboots.c @@ -0,0 +1,211 @@ +/* + * Copyright (C) 2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include "utils.h" + +/************************************************************* +* ENTRY METHOD +**************************************************************/ +static int browseDeviceInfoRebootsRebootInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) +{ + struct dm_data *p = NULL; + char *inst = NULL; + LIST_HEAD(dup_list); + + synchronize_specific_config_sections_with_dmmap("deviceinfo", "reboot", "dmmap_deviceinfo", &dup_list); + list_for_each_entry(p, &dup_list, list) { + + inst = handle_instance(dmctx, parent_node, p->dmmap_section, "reboot_instance", "reboot_alias"); + + if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)p, inst) == DM_STOP) + break; + } + free_dmmap_config_dup_list(&dup_list); + return 0; +} + +/************************************************************* +* GET & SET PARAM +**************************************************************/ +static int get_DeviceInfoReboots_BootCount(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("deviceinfo", "globals", "boot_count", value); + return 0; +} + +static int get_DeviceInfoReboots_CurrentVersionBootCount(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("deviceinfo", "globals", "curr_version_boot_count", value); + return 0; +} + +static int get_DeviceInfoReboots_WatchdogBootCount(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("deviceinfo", "globals", "watchdog_boot_count", value); + return 0; +} + +static int get_DeviceInfoReboots_ColdBootCount(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("deviceinfo", "globals", "cold_boot_count", value); + return 0; +} + +static int get_DeviceInfoReboots_WarmBootCount(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_option_value_string("deviceinfo", "globals", "warm_boot_count", value); + return 0; +} + +static int get_DeviceInfoReboots_MaxRebootEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = dmuci_get_option_value_fallback_def("deviceinfo", "globals", "max_reboot_entries", "3"); + return 0; +} + +static int set_DeviceInfoReboots_MaxRebootEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + struct uci_section *s = NULL, *tmp_s = NULL; + int max_entries = DM_STRTOL(value); + + switch (action) { + case VALUECHECK: + if (bbfdm_validate_int(ctx, value, RANGE_ARGS{{"-1",NULL}}, 1)) + return FAULT_9007; + break; + case VALUESET: + if (max_entries == 0) { + // Delete all sections if value is "0" + uci_foreach_sections_safe("deviceinfo", "reboot", tmp_s, s) { + dmuci_delete_by_section(s, NULL, NULL); + } + } else if (max_entries > 0) { + // Step 1: Count total sections + int total_sections = 0; + + uci_foreach_sections_safe("deviceinfo", "reboot", tmp_s, s) { + total_sections++; + } + + // Step 2: Calculate how many sections to delete (earliest sections) + int to_delete = total_sections - max_entries; + + // Step 3: Delete the earliest sections that exceed max_entries + if (to_delete > 0) { + int idx = 0; + uci_foreach_sections_safe("deviceinfo", "reboot", tmp_s, s) { + if (idx++ < to_delete) { + dmuci_delete_by_section(s, NULL, NULL); + } + } + } + } + + dmuci_set_value("deviceinfo", "globals", "max_reboot_entries", value); + break; + } + return 0; +} + +static int get_DeviceInfoReboots_RebootNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + int cnt = get_number_of_entries(ctx, data, instance, browseDeviceInfoRebootsRebootInst); + dmasprintf(value, "%d", cnt); + return 0; +} + +static int get_DeviceInfoRebootsReboot_Alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + return bbf_get_alias(ctx, ((struct dm_data *)data)->dmmap_section, "reboot_alias", instance, value); +} + +static int set_DeviceInfoRebootsReboot_Alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + return bbf_set_alias(ctx, ((struct dm_data *)data)->dmmap_section, "reboot_alias", instance, value); +} + +static int get_DeviceInfoRebootsReboot_TimeStamp(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "time_stamp", value); + return 0; +} + +static int get_DeviceInfoRebootsReboot_FirmwareUpdated(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "firmware_updated", value); + return 0; +} + +static int get_DeviceInfoRebootsReboot_Cause(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "cause", value); + return 0; +} + +static int get_DeviceInfoRebootsReboot_Reason(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "reason", value); + return 0; +} + +/************************************************************* + * OPERATE COMMANDS + *************************************************************/ +static int operate_DeviceInfoReboots_RemoveAllReboots(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + struct uci_section *s = NULL, *tmp_s = NULL; + + uci_foreach_sections_safe("deviceinfo", "reboot", tmp_s, s) { + dmuci_delete_by_section(s, NULL, NULL); + } + return 0; +} + +static int operate_DeviceInfoRebootsReboot_Remove(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + dmuci_delete_by_section(((struct dm_data *)data)->config_section, NULL, NULL); + return 0; +} + +/********************************************************************************************************************************** +* OBJ & LEAF DEFINITION +***********************************************************************************************************************************/ +/* *** Device.DeviceInfo.Reboots.Reboot.{i}. *** */ +DMLEAF tDeviceInfoRebootsRebootParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type */ +{"Alias", &DMWRITE, DMT_STRING, get_DeviceInfoRebootsReboot_Alias, set_DeviceInfoRebootsReboot_Alias, BBFDM_USP}, +{"TimeStamp", &DMREAD, DMT_TIME, get_DeviceInfoRebootsReboot_TimeStamp, NULL, BBFDM_USP}, +{"FirmwareUpdated", &DMREAD, DMT_BOOL, get_DeviceInfoRebootsReboot_FirmwareUpdated, NULL, BBFDM_USP}, +{"Cause", &DMREAD, DMT_STRING, get_DeviceInfoRebootsReboot_Cause, NULL, BBFDM_USP}, +{"Reason", &DMREAD, DMT_STRING, get_DeviceInfoRebootsReboot_Reason, NULL, BBFDM_USP}, +{"Remove()", &DMASYNC, DMT_COMMAND, NULL, operate_DeviceInfoRebootsReboot_Remove, BBFDM_USP}, +{0} +}; + +/* *** Device.DeviceInfo.Reboots. *** */ +DMOBJ tDeviceInfoRebootsObj[] = { +/* OBJ, permission, addobj, delobj, checkdep, browseinstobj, nextdynamicobj, dynamicleaf, nextobj, leaf, linker, bbfdm_type, uniqueKeys */ +{"Reboot", &DMREAD, NULL, NULL, NULL, browseDeviceInfoRebootsRebootInst, NULL, NULL, NULL, tDeviceInfoRebootsRebootParams, NULL, BBFDM_USP, NULL}, +{0} +}; + +DMLEAF tDeviceInfoRebootsParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type */ +{"BootCount", &DMREAD, DMT_UNINT, get_DeviceInfoReboots_BootCount, NULL, BBFDM_USP}, +{"CurrentVersionBootCount", &DMREAD, DMT_UNINT, get_DeviceInfoReboots_CurrentVersionBootCount, NULL, BBFDM_USP}, +{"WatchdogBootCount", &DMREAD, DMT_UNINT, get_DeviceInfoReboots_WatchdogBootCount, NULL, BBFDM_USP}, +{"ColdBootCount", &DMREAD, DMT_UNINT, get_DeviceInfoReboots_ColdBootCount, NULL, BBFDM_USP}, +{"WarmBootCount", &DMREAD, DMT_UNINT, get_DeviceInfoReboots_WarmBootCount, NULL, BBFDM_USP}, +{"MaxRebootEntries", &DMWRITE, DMT_INT, get_DeviceInfoReboots_MaxRebootEntries, set_DeviceInfoReboots_MaxRebootEntries, BBFDM_USP}, +{"RebootNumberOfEntries", &DMREAD, DMT_UNINT, get_DeviceInfoReboots_RebootNumberOfEntries, NULL, BBFDM_USP}, +{"RemoveAllReboots()", &DMASYNC, DMT_COMMAND, NULL, operate_DeviceInfoReboots_RemoveAllReboots, BBFDM_USP}, +{0} +}; diff --git a/src/reboots.h b/src/reboots.h new file mode 100644 index 0000000000000000000000000000000000000000..5e76a4c692edc362f6e26a67f51af344fcb47467 --- /dev/null +++ b/src/reboots.h @@ -0,0 +1,18 @@ +/* + * Copyright (C) 2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#ifndef __REBOOTS_H +#define __REBOOTS_H + +extern DMOBJ tDeviceInfoRebootsObj[]; +extern DMLEAF tDeviceInfoRebootsParams[]; + +#endif //__REBOOTS_H diff --git a/src/supported_dm.c b/src/supported_dm.c new file mode 100644 index 0000000000000000000000000000000000000000..a19a0365be5e6659d05d41009304c8fc7e8300cf --- /dev/null +++ b/src/supported_dm.c @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2021-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include "utils.h" + +struct Supported_Data_Models +{ + char url[128]; + char urn[128]; + char features[128]; +}; + +struct Supported_Data_Models Data_Models[] = { +{"http://www.broadband-forum.org/cwmp/tr-181-2-18-1.xml","urn:broadband-forum-org:tr-181-2-18-1","IP,Wireless,Firewall,NAT,DHCP,QoS,DNS,GRE,UPnP"}, +{"http://www.broadband-forum.org/cwmp/tr-104-2-0-2.xml","urn:broadband-forum-org:tr-104-2-0-2", "VoiceService"}, +{"http://www.broadband-forum.org/cwmp/tr-143-1-1-0.xml","urn:broadband-forum-org:tr-143-1-1-0", "Ping,TraceRoute,Download,Upload,UDPecho,ServerSelectionDiag"}, +{"http://www.broadband-forum.org/cwmp/tr-157-1-3-0.xml","urn:broadband-forum-org:tr-157-1-3-0", "Bulkdata,SoftwareModules"}, +}; + +/************************************************************* +* ENTRY METHOD +**************************************************************/ +int browseDeviceInfoSupportedDataModelInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) +{ + char *inst = NULL; + int i; + + for (i = 0; i < ARRAY_SIZE(Data_Models); i++) { + inst = handle_instance_without_section(dmctx, parent_node, i+1); + if (DM_LINK_INST_OBJ(dmctx, parent_node, (void *)&Data_Models[i], inst) == DM_STOP) + break; + } + return 0; +} + +/************************************************************* +* GET & SET PARAM +**************************************************************/ +int get_DeviceInfo_SupportedDataModelNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + dmasprintf(value, "%d", ARRAY_SIZE(Data_Models)); + return 0; +} + +static int get_DeviceInfoSupportedDataModel_Alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + struct uci_section *s = NULL; + + uci_path_foreach_option_eq(bbfdm, "dmmap", "data_model", "data_model_inst", instance, s) { + dmuci_get_value_by_section_string(s, "alias", value); + break; + } + if ((*value)[0] == '\0') + dmasprintf(value, "cpe-%s", instance); + return 0; +} + +static int set_DeviceInfoSupportedDataModel_Alias(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) +{ + struct uci_section *s = NULL, *dmmap = NULL; + + switch (action) { + case VALUECHECK: + if (bbfdm_validate_string(ctx, value, -1, 64, NULL, NULL)) + return FAULT_9007; + break; + case VALUESET: + uci_path_foreach_option_eq(bbfdm, "dmmap", "data_model", "data_model_inst", instance, s) { + dmuci_set_value_by_section_bbfdm(s, "alias", value); + return 0; + } + dmuci_add_section_bbfdm("dmmap", "data_model", &dmmap); + dmuci_set_value_by_section(dmmap, "data_model_inst", instance); + dmuci_set_value_by_section(dmmap, "alias", value); + break; + } + return 0; +} + +static int get_DeviceInfoSupportedDataModel_URL(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = dmstrdup(((struct Supported_Data_Models *)data)->url); + return 0; +} + +static int get_DeviceInfoSupportedDataModel_URN(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = dmstrdup(((struct Supported_Data_Models *)data)->urn); + return 0; +} + +static int get_DeviceInfoSupportedDataModel_Features(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) +{ + *value = dmstrdup(((struct Supported_Data_Models *)data)->features); + return 0; +} + +/********************************************************************************************************************************** +* OBJ & LEAF DEFINITION +***********************************************************************************************************************************/ +/* *** Device.DeviceInfo.SupportedDataModel.{i}. *** */ +DMLEAF tDeviceInfoSupportedDataModelParams[] = { +/* PARAM, permission, type, getvalue, setvalue, bbfdm_type, version*/ +{"Alias", &DMWRITE, DMT_STRING, get_DeviceInfoSupportedDataModel_Alias, set_DeviceInfoSupportedDataModel_Alias, BBFDM_CWMP, DM_FLAG_UNIQUE}, +{"URL", &DMREAD, DMT_STRING, get_DeviceInfoSupportedDataModel_URL, NULL, BBFDM_CWMP, DM_FLAG_UNIQUE}, +{"URN", &DMREAD, DMT_STRING, get_DeviceInfoSupportedDataModel_URN, NULL, BBFDM_CWMP}, +{"Features", &DMREAD, DMT_STRING, get_DeviceInfoSupportedDataModel_Features, NULL, BBFDM_CWMP}, +{0} +}; diff --git a/src/supported_dm.h b/src/supported_dm.h new file mode 100644 index 0000000000000000000000000000000000000000..34fc059c7e4b68b18c908a00e859f61b682f08fe --- /dev/null +++ b/src/supported_dm.h @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2021-2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#ifndef __SUPPORTED_DM_H +#define __SUPPORTED_DM_H + +extern DMLEAF tDeviceInfoSupportedDataModelParams[]; + +int browseDeviceInfoSupportedDataModelInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance); + +int get_DeviceInfo_SupportedDataModelNumberOfEntries(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value); + +#endif //__SUPPORTED_DM_H diff --git a/src/sysmngr.c b/src/sysmngr.c new file mode 100644 index 0000000000000000000000000000000000000000..360f0cab3e3843af6c5fc0e518a2b26a86aff2bd --- /dev/null +++ b/src/sysmngr.c @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <libubox/uloop.h> +#include <libbbfdm-ubus/bbfdm-ubus.h> + +extern DM_MAP_OBJ tDynamicObj[]; + +static void usage(char *prog) +{ + fprintf(stderr, "Usage: %s [options]\n", prog); + fprintf(stderr, "\n"); + fprintf(stderr, "options:\n"); + fprintf(stderr, " -d Use multiple time to get more verbose debug logs (Debug: -dddd)\n"); + fprintf(stderr, " -h Displays this help\n"); + fprintf(stderr, "\n"); +} + +int main(int argc, char **argv) +{ + struct bbfdm_context bbfdm_ctx = {0}; + int log_level = LOG_ERR; + int c = 0; + + while ((c = getopt(argc, argv, "dh")) != -1) { + switch (c) { + case 'd': + log_level += 1; + break; + case 'h': + usage(argv[0]); + return EXIT_SUCCESS; + default: + usage(argv[0]); + return EXIT_FAILURE; + } + } + + memset(&bbfdm_ctx, 0, sizeof(struct bbfdm_context)); + + bbfdm_ubus_set_service_name(&bbfdm_ctx, "sysmngr"); + bbfdm_ubus_set_log_level(log_level); + bbfdm_ubus_load_data_model(tDynamicObj); + + openlog("sysmngr", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); + + if (bbfdm_ubus_regiter_init(&bbfdm_ctx)) + goto out; + + uloop_run(); + +out: + bbfdm_ubus_regiter_free(&bbfdm_ctx); + closelog(); + + return 0; +} diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000000000000000000000000000000000000..33408d422154a94beee3a595716ab0e306a2a8b3 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#include "utils.h" + +#include <openssl/sha.h> +#include <openssl/evp.h> + +static bool validate_hash_value(const char *algo, const char *file_path, const char *checksum) +{ + unsigned char buffer[1024 * 16] = {0}; + char hash[BUFSIZ] = {0}; + bool res = false; + unsigned int bytes = 0; + FILE *file; + + EVP_MD_CTX *mdctx; + const EVP_MD *md; + unsigned char md_value[EVP_MAX_MD_SIZE]; + + // cppcheck-suppress cert-MSC24-C + file = fopen(file_path, "rb"); + if (!file) + return false; + + md = EVP_get_digestbyname(algo); + mdctx = EVP_MD_CTX_create(); + EVP_DigestInit_ex(mdctx, md, NULL); + + if (md == NULL) + goto end; + + while ((bytes = fread (buffer, 1, sizeof(buffer), file))) { + EVP_DigestUpdate(mdctx, buffer, bytes); + } + + bytes = 0; + EVP_DigestFinal_ex(mdctx, md_value, &bytes); + + for (int i = 0; i < bytes; i++) + snprintf(&hash[i * 2], sizeof(hash) - (i * 2), "%02x", md_value[i]); + + if (DM_STRCMP(hash, checksum) == 0) + res = true; + +end: + EVP_MD_CTX_destroy(mdctx); + EVP_cleanup(); + + fclose(file); + return res; +} + +bool validate_checksum_value(const char *file_path, const char *checksum_algorithm, const char *checksum) +{ + if (checksum && *checksum) { + + if (strcmp(checksum_algorithm, "SHA-1") == 0) + return validate_hash_value("SHA1", file_path, checksum); + else if (strcmp(checksum_algorithm, "SHA-224") == 0) + return validate_hash_value("SHA224", file_path, checksum); + else if (strcmp(checksum_algorithm, "SHA-256") == 0) + return validate_hash_value("SHA256", file_path, checksum); + else if (strcmp(checksum_algorithm, "SHA-384") == 0) + return validate_hash_value("SHA384", file_path, checksum); + else if (strcmp(checksum_algorithm, "SHA-512") == 0) + return validate_hash_value("SHA512", file_path, checksum); + else + return false; + } + + return true; +} + +bool validate_server_response_code(const char *url, int response_code) +{ + if ((strncmp(url, HTTP_URI, strlen(HTTP_URI)) == 0 && response_code != 200) || + (strncmp(url, FTP_URI, strlen(FTP_URI)) == 0 && response_code != 226) || + (strncmp(url, FILE_URI, strlen(FILE_URI)) == 0 && response_code != 0) || + (strncmp(url, HTTP_URI, strlen(HTTP_URI)) && strncmp(url, FTP_URI, strlen(FTP_URI)) && strncmp(url, FILE_URI, strlen(FILE_URI)))) { + return false; + } + + return true; +} + +bool validate_file_system_size(const char *file_size) +{ + if (file_size && *file_size) { + unsigned long f_size = strtoul(file_size, NULL, 10); + unsigned long fs_available_size = file_system_size("/tmp", FS_SIZE_AVAILABLE); + + if (fs_available_size < f_size) + return false; + } + + return true; +} + +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) +{ + char start_time[32] = {0}; + char complete_time[32] = {0}; + struct blob_buf bb; + + strftime(start_time, sizeof(start_time), "%Y-%m-%dT%H:%M:%SZ", gmtime(&start_t)); + strftime(complete_time, sizeof(complete_time), "%Y-%m-%dT%H:%M:%SZ", gmtime(&complete_t)); + + memset(&bb, 0, sizeof(struct blob_buf)); + blob_buf_init(&bb, 0); + + blobmsg_add_string(&bb, "name", "Device.LocalAgent.TransferComplete!"); + void *arr = blobmsg_open_array(&bb, "input"); + + fill_blob_param(&bb, "Command", command, DMT_TYPE[DMT_STRING], 0); + if(commandKey) + fill_blob_param(&bb, "CommandKey", commandKey, DMT_TYPE[DMT_STRING], 0); + else + fill_blob_param(&bb, "CommandKey", "", DMT_TYPE[DMT_STRING], 0); + + fill_blob_param(&bb, "Requestor", "", DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "TransferType", transfer_type, DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "Affected", obj_path, DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "TransferURL", transfer_url, DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "StartTime", start_time, DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "CompleteTime", complete_time, DMT_TYPE[DMT_STRING], 0); + + if (DM_STRLEN(fault_string) == 0) { + fill_blob_param(&bb, "FaultCode", "0", DMT_TYPE[DMT_STRING], 0); + } else { + fill_blob_param(&bb, "FaultCode", "7000", DMT_TYPE[DMT_STRING], 0); + } + + fill_blob_param(&bb, "FaultString", fault_string, DMT_TYPE[DMT_STRING], 0); + blobmsg_close_array(&bb, arr); + + dmubus_call_blob_msg_set("bbfdm", "notify_event", &bb); + + blob_buf_free(&bb); +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000000000000000000000000000000000000..734ea2e30cebe29c7b691294e9aadfb9799b29fa --- /dev/null +++ b/src/utils.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2024 iopsys Software Solutions AB + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 + * as published by the Free Software Foundation + * + * Author: Amin Ben Romdhane <amin.benromdhane@iopsys.eu> + * + */ + +#ifndef __UTILS_H +#define __UTILS_H + +#include <libbbfdm-api/dmcommon.h> + +bool validate_checksum_value(const char *file_path, const char *checksum_algorithm, const char *checksum); +bool validate_file_system_size(const char *file_size); +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); + +#endif //__UTILS_H