From 5fafdbdb0b690f3a8da5ee64a88595ee04ec4d3c Mon Sep 17 00:00:00 2001 From: Joel Guittet Date: Thu, 20 Jun 2024 09:32:04 +0200 Subject: [PATCH] client: rework apis so that the mender-client registers add-ons and manage them --- add-ons/src/mender-configure.c | 28 +++-- add-ons/src/mender-inventory.c | 27 +++-- add-ons/src/mender-troubleshoot.c | 16 ++- core/src/mender-client.c | 168 ++++++++++++++++++++++++++++-- include/mender-addon.h | 51 +++++++++ include/mender-client.h | 23 ++++ include/mender-configure.h | 17 ++- include/mender-inventory.h | 16 ++- include/mender-troubleshoot.h | 12 ++- tests/src/main.c | 57 ++++------ 10 files changed, 345 insertions(+), 70 deletions(-) create mode 100644 include/mender-addon.h diff --git a/add-ons/src/mender-configure.c b/add-ons/src/mender-configure.c index 6a8e930..a1471f8 100644 --- a/add-ons/src/mender-configure.c +++ b/add-ons/src/mender-configure.c @@ -41,6 +41,12 @@ #define CONFIG_MENDER_CLIENT_CONFIGURE_REFRESH_INTERVAL (28800) #endif /* CONFIG_MENDER_CLIENT_CONFIGURE_REFRESH_INTERVAL */ +/** + * @brief Mender configure instance + */ +const mender_addon_instance_t mender_configure_addon_instance + = { .init = mender_configure_init, .activate = mender_configure_activate, .deactivate = mender_configure_deactivate, .exit = mender_configure_exit }; + /** * @brief Mender configure configuration */ @@ -85,7 +91,7 @@ static mender_err_t mender_configure_download_artifact_callback( char *id, char *artifact_name, char *type, cJSON *meta_data, char *filename, size_t size, void *data, size_t index, size_t length); mender_err_t -mender_configure_init(mender_configure_config_t *config, mender_configure_callbacks_t *callbacks) { +mender_configure_init(void *config, void *callbacks) { assert(NULL != config); char * device_config = NULL; @@ -93,14 +99,16 @@ mender_configure_init(mender_configure_config_t *config, mender_configure_callba mender_err_t ret; /* Save configuration */ - if (0 != config->refresh_interval) { - mender_configure_config.refresh_interval = config->refresh_interval; + if (0 != ((mender_configure_config_t *)config)->refresh_interval) { + mender_configure_config.refresh_interval = ((mender_configure_config_t *)config)->refresh_interval; } else { mender_configure_config.refresh_interval = CONFIG_MENDER_CLIENT_CONFIGURE_REFRESH_INTERVAL; } /* Save callbacks */ - memcpy(&mender_configure_callbacks, callbacks, sizeof(mender_configure_callbacks_t)); + if (NULL != callbacks) { + memcpy(&mender_configure_callbacks, callbacks, sizeof(mender_configure_callbacks_t)); + } /* Create configure mutex */ if (MENDER_OK != (ret = mender_scheduler_mutex_create(&mender_configure_mutex))) { @@ -180,6 +188,15 @@ mender_configure_activate(void) { return ret; } +mender_err_t +mender_configure_deactivate(void) { + + /* Deactivate mender configure work */ + mender_scheduler_work_deactivate(mender_configure_work_handle); + + return MENDER_OK; +} + mender_err_t mender_configure_get(mender_keystore_t **configuration) { @@ -297,9 +314,6 @@ mender_configure_exit(void) { mender_err_t ret; - /* Deactivate mender configure work */ - mender_scheduler_work_deactivate(mender_configure_work_handle); - /* Delete mender configure work */ mender_scheduler_work_delete(mender_configure_work_handle); mender_configure_work_handle = NULL; diff --git a/add-ons/src/mender-inventory.c b/add-ons/src/mender-inventory.c index 3365dbd..b218edc 100755 --- a/add-ons/src/mender-inventory.c +++ b/add-ons/src/mender-inventory.c @@ -39,13 +39,19 @@ #define CONFIG_MENDER_CLIENT_INVENTORY_REFRESH_INTERVAL (28800) #endif /* CONFIG_MENDER_CLIENT_INVENTORY_REFRESH_INTERVAL */ +/** + * @brief Mender inventory instance + */ +const mender_addon_instance_t mender_inventory_addon_instance + = { .init = mender_inventory_init, .activate = mender_inventory_activate, .deactivate = mender_inventory_deactivate, .exit = mender_inventory_exit }; + /** * @brief Mender inventory configuration */ static mender_inventory_config_t mender_inventory_config; /** - * @brief Mender inventory + * @brief Mender inventory keystore */ static mender_keystore_t *mender_inventory_keystore = NULL; static void * mender_inventory_mutex = NULL; @@ -62,14 +68,15 @@ static void *mender_inventory_work_handle = NULL; static mender_err_t mender_inventory_work_function(void); mender_err_t -mender_inventory_init(mender_inventory_config_t *config) { +mender_inventory_init(void *config, void *callbacks) { assert(NULL != config); + (void)callbacks; mender_err_t ret; /* Save configuration */ - if (0 != config->refresh_interval) { - mender_inventory_config.refresh_interval = config->refresh_interval; + if (0 != ((mender_inventory_config_t *)config)->refresh_interval) { + mender_inventory_config.refresh_interval = ((mender_inventory_config_t *)config)->refresh_interval; } else { mender_inventory_config.refresh_interval = CONFIG_MENDER_CLIENT_INVENTORY_REFRESH_INTERVAL; } @@ -107,6 +114,15 @@ mender_inventory_activate(void) { return ret; } +mender_err_t +mender_inventory_deactivate(void) { + + /* Deactivate mender inventory work */ + mender_scheduler_work_deactivate(mender_inventory_work_handle); + + return MENDER_OK; +} + mender_err_t mender_inventory_set(mender_keystore_t *inventory) { @@ -157,9 +173,6 @@ mender_inventory_exit(void) { mender_err_t ret; - /* Deactivate mender inventory work */ - mender_scheduler_work_deactivate(mender_inventory_work_handle); - /* Delete mender inventory work */ mender_scheduler_work_delete(mender_inventory_work_handle); mender_inventory_work_handle = NULL; diff --git a/add-ons/src/mender-troubleshoot.c b/add-ons/src/mender-troubleshoot.c index cad1bb7..3646703 100644 --- a/add-ons/src/mender-troubleshoot.c +++ b/add-ons/src/mender-troubleshoot.c @@ -53,6 +53,12 @@ */ #define MENDER_TROUBLESHOOT_SBUFFER_INIT_SIZE (256) +/** + * @brief Mender troubleshoot instance + */ +const mender_addon_instance_t mender_troubleshoot_addon_instance + = { .init = mender_troubleshoot_init, .activate = NULL, .deactivate = mender_troubleshoot_deactivate, .exit = mender_troubleshoot_exit }; + /** * Proto type */ @@ -297,20 +303,22 @@ static void mender_troubleshoot_release_protohdr(mender_troubleshoot_protohdr_t static void mender_troubleshoot_release_protohdr_properties(mender_troubleshoot_protohdr_properties_t *properties); mender_err_t -mender_troubleshoot_init(mender_troubleshoot_config_t *config, mender_troubleshoot_callbacks_t *callbacks) { +mender_troubleshoot_init(void *config, void *callbacks) { assert(NULL != config); mender_err_t ret; /* Save configuration */ - if (0 != config->healthcheck_interval) { - mender_troubleshoot_config.healthcheck_interval = config->healthcheck_interval; + if (0 != ((mender_troubleshoot_config_t *)config)->healthcheck_interval) { + mender_troubleshoot_config.healthcheck_interval = ((mender_troubleshoot_config_t *)config)->healthcheck_interval; } else { mender_troubleshoot_config.healthcheck_interval = CONFIG_MENDER_CLIENT_TROUBLESHOOT_HEALTHCHECK_INTERVAL; } /* Save callbacks */ - memcpy(&mender_troubleshoot_callbacks, callbacks, sizeof(mender_troubleshoot_callbacks_t)); + if (NULL != callbacks) { + memcpy(&mender_troubleshoot_callbacks, callbacks, sizeof(mender_troubleshoot_callbacks_t)); + } /* Create troubleshoot healthcheck work */ mender_scheduler_work_params_t healthcheck_work_params; diff --git a/core/src/mender-client.c b/core/src/mender-client.c index 9aaf5b0..856a4d9 100644 --- a/core/src/mender-client.c +++ b/core/src/mender-client.c @@ -108,6 +108,13 @@ static mender_client_artifact_type_t **mender_client_artifact_types_list = NULL static size_t mender_client_artifact_types_count = 0; static void * mender_client_artifact_types_mutex = NULL; +/** + * @brief Mender client add-ons list and mutex + */ +static mender_addon_instance_t **mender_client_addons_list = NULL; +static size_t mender_client_addons_count = 0; +static void * mender_client_addons_mutex = NULL; + /** * @brief Mender client work handle */ @@ -290,6 +297,12 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca return ret; } + /* Create add-ons management mutex */ + if (MENDER_OK != (ret = mender_scheduler_mutex_create(&mender_client_addons_mutex))) { + mender_log_error("Unable to create add-ons management mutex"); + return ret; + } + /* Register rootfs-image artifact type */ if (MENDER_OK != (ret = mender_client_register_artifact_type("rootfs-image", &mender_client_download_artifact_flash_callback, true, config->artifact_name))) { @@ -307,12 +320,6 @@ mender_client_init(mender_client_config_t *config, mender_client_callbacks_t *ca goto END; } - /* Activate update work */ - if (MENDER_OK != (ret = mender_scheduler_work_activate(mender_client_work_handle))) { - mender_log_error("Unable to activate update work"); - goto END; - } - END: return ret; @@ -366,6 +373,106 @@ mender_client_register_artifact_type(char *type, return ret; } +mender_err_t +mender_client_register_addon(mender_addon_instance_t *addon, void *config, void *callbacks) { + + assert(NULL != addon); + mender_addon_instance_t **tmp; + mender_err_t ret; + + /* Take mutex used to protect access to the add-ons management list */ + if (MENDER_OK != (ret = mender_scheduler_mutex_take(mender_client_addons_mutex, -1))) { + mender_log_error("Unable to take mutex"); + return ret; + } + + /* Initialization of the add-on */ + if (NULL != addon->init) { + if (MENDER_OK != (ret = addon->init(config, callbacks))) { + mender_log_error("Unable to initialize add-on"); + goto END; + } + } + + /* Activate add-on if authentication is already done */ + if (MENDER_CLIENT_STATE_AUTHENTICATED == mender_client_state) { + if (NULL != addon->activate) { + if (MENDER_OK != (ret = addon->activate())) { + mender_log_error("Unable to activate add-on"); + if (NULL != addon->exit) { + addon->exit(); + } + goto END; + } + } + } + + /* Add add-on to the list */ + if (NULL == (tmp = (mender_addon_instance_t **)realloc(mender_client_addons_list, (mender_client_addons_count + 1) * sizeof(mender_addon_instance_t *)))) { + mender_log_error("Unable to allocate memory"); + if (NULL != addon->exit) { + addon->exit(); + } + ret = MENDER_FAIL; + goto END; + } + mender_client_addons_list = tmp; + mender_client_addons_list[mender_client_addons_count] = addon; + mender_client_addons_count++; + +END: + + /* Release mutex used to protect access to the add-ons management list */ + mender_scheduler_mutex_give(mender_client_addons_mutex); + + return ret; +} + +mender_err_t +mender_client_activate(void) { + + mender_err_t ret; + + /* Activate update work */ + if (MENDER_OK != (ret = mender_scheduler_work_activate(mender_client_work_handle))) { + mender_log_error("Unable to activate update work"); + goto END; + } + +END: + + return ret; +} + +mender_err_t +mender_client_deactivate(void) { + + mender_err_t ret; + + /* Take mutex used to protect access to the add-ons management list */ + if (MENDER_OK != (ret = mender_scheduler_mutex_take(mender_client_addons_mutex, -1))) { + mender_log_error("Unable to take mutex"); + return ret; + } + + /* Deactivate add-ons */ + if (NULL != mender_client_addons_list) { + for (size_t index = 0; index < mender_client_addons_count; index++) { + if (NULL != mender_client_addons_list[index]->deactivate) { + mender_client_addons_list[index]->deactivate(); + } + } + } + + /* Release mutex used to protect access to the add-ons management list */ + mender_scheduler_mutex_give(mender_client_addons_mutex); + + /* Deactivate mender client work */ + mender_scheduler_work_deactivate(mender_client_work_handle); + + return ret; +} + mender_err_t mender_client_execute(void) { @@ -385,8 +492,25 @@ mender_client_execute(void) { mender_err_t mender_client_exit(void) { - /* Deactivate mender client work */ - mender_scheduler_work_deactivate(mender_client_work_handle); + mender_err_t ret; + + /* Take mutex used to protect access to the add-ons management list */ + if (MENDER_OK != (ret = mender_scheduler_mutex_take(mender_client_addons_mutex, -1))) { + mender_log_error("Unable to take mutex"); + return ret; + } + + /* Release add-ons */ + if (NULL != mender_client_addons_list) { + for (size_t index = 0; index < mender_client_addons_count; index++) { + if (NULL != mender_client_addons_list[index]->exit) { + mender_client_addons_list[index]->exit(); + } + } + } + + /* Release mutex used to protect access to the add-ons management list */ + mender_scheduler_mutex_give(mender_client_addons_mutex); /* Delete mender client work */ mender_scheduler_work_delete(mender_client_work_handle); @@ -423,8 +547,16 @@ mender_client_exit(void) { mender_scheduler_mutex_give(mender_client_artifact_types_mutex); mender_scheduler_mutex_delete(mender_client_artifact_types_mutex); mender_client_artifact_types_mutex = NULL; + if (NULL != mender_client_addons_list) { + free(mender_client_addons_list); + mender_client_addons_list = NULL; + } + mender_client_addons_count = 0; + mender_scheduler_mutex_give(mender_client_addons_mutex); + mender_scheduler_mutex_delete(mender_client_addons_mutex); + mender_client_addons_mutex = NULL; - return MENDER_OK; + return ret; } static mender_err_t @@ -615,6 +747,24 @@ mender_client_authentication_work_function(void) { mender_client_deployment_data = NULL; } + /* Take mutex used to protect access to the add-ons management list */ + if (MENDER_OK != (ret = mender_scheduler_mutex_take(mender_client_addons_mutex, -1))) { + mender_log_error("Unable to take mutex"); + return ret; + } + + /* Activate add-ons */ + if (NULL != mender_client_addons_list) { + for (size_t index = 0; index < mender_client_addons_count; index++) { + if (NULL != mender_client_addons_list[index]->activate) { + mender_client_addons_list[index]->activate(); + } + } + } + + /* Release mutex used to protect access to the add-ons management list */ + mender_scheduler_mutex_give(mender_client_addons_mutex); + return MENDER_DONE; REBOOT: diff --git a/include/mender-addon.h b/include/mender-addon.h new file mode 100644 index 0000000..20c10a9 --- /dev/null +++ b/include/mender-addon.h @@ -0,0 +1,51 @@ +/** + * @file mender-addon.h + * @brief Mender MCU addon implementation + * + * MIT License + * + * Copyright (c) 2022-2023 joelguittet and mender-mcu-client contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef __MENDER_ADDON_H__ +#define __MENDER_ADDON_H__ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#include "mender-utils.h" + +/** + * @brief Mender add-on instance + */ +typedef struct { + mender_err_t (*init)(void *, void *); /**< Invoked to initialize the add-on */ + mender_err_t (*activate)(void); /**< Invoked to activate the add-on */ + mender_err_t (*deactivate)(void); /**< Invoked to deactivate the add-on */ + mender_err_t (*exit)(void); /**< Invoked to cleanup the add-on */ +} mender_addon_instance_t; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __MENDER_ADDON_H__ */ diff --git a/include/mender-client.h b/include/mender-client.h index 9ecd711..bda5024 100644 --- a/include/mender-client.h +++ b/include/mender-client.h @@ -32,6 +32,7 @@ extern "C" { #endif /* __cplusplus */ +#include "mender-addon.h" #include "mender-utils.h" /** @@ -85,6 +86,28 @@ mender_err_t mender_client_register_artifact_type(char *type, bool needs_restart, char *artifact_name); +/** + * @brief Register add-on + * @param addon Add-on + * @param config Add-on configuration + * @param callbacks Add-on callbacks + * @return MENDER_OK if the function succeeds, error code otherwise + */ +mender_err_t mender_client_register_addon(mender_addon_instance_t *addon, void *config, void *callbacks); + +/** + * @brief Activate mender client + * @return MENDER_OK if the function succeeds, error code otherwise + */ +mender_err_t mender_client_activate(void); + +/** + * @brief Deactivate mender client + * @note This function stops synchronization with the server + * @return MENDER_OK if the function succeeds, error code otherwise + */ +mender_err_t mender_client_deactivate(void); + /** * @brief Function used to trigger execution of the authentication and update work * @note Calling this function is optional when the periodic execution of the work is configured diff --git a/include/mender-configure.h b/include/mender-configure.h index b0d2460..ec65c3b 100755 --- a/include/mender-configure.h +++ b/include/mender-configure.h @@ -32,10 +32,16 @@ extern "C" { #endif /* __cplusplus */ +#include "mender-addon.h" #include "mender-utils.h" #ifdef CONFIG_MENDER_CLIENT_ADD_ON_CONFIGURE +/** + * @brief Mender configure instance + */ +extern const mender_addon_instance_t mender_configure_addon_instance; + /** * @brief Mender configure configuration */ @@ -55,10 +61,10 @@ typedef struct { /** * @brief Initialize mender configure add-on * @param config Mender configure configuration - * @param callbacks Mender configure callbacks + * @param callbacks Mender configure callbacks (optional) * @return MENDER_OK if the function succeeds, error code otherwise */ -mender_err_t mender_configure_init(mender_configure_config_t *config, mender_configure_callbacks_t *callbacks); +mender_err_t mender_configure_init(void *config, void *callbacks); /** * @brief Activate mender configure add-on @@ -66,6 +72,13 @@ mender_err_t mender_configure_init(mender_configure_config_t *config, mender_con */ mender_err_t mender_configure_activate(void); +/** + * @brief Deactivate mender configure add-on + * @note This function stops synchronization with the server + * @return MENDER_OK if the function succeeds, error code otherwise + */ +mender_err_t mender_configure_deactivate(void); + /** * @brief Get mender configuration * @param configuration Mender configuration key/value pairs table, ends with a NULL/NULL element, NULL if not defined diff --git a/include/mender-inventory.h b/include/mender-inventory.h index e0b753f..63e92dc 100755 --- a/include/mender-inventory.h +++ b/include/mender-inventory.h @@ -32,10 +32,16 @@ extern "C" { #endif /* __cplusplus */ +#include "mender-addon.h" #include "mender-utils.h" #ifdef CONFIG_MENDER_CLIENT_ADD_ON_INVENTORY +/** + * @brief Mender inventory instance + */ +extern const mender_addon_instance_t mender_inventory_addon_instance; + /** * @brief Mender inventory configuration */ @@ -46,9 +52,10 @@ typedef struct { /** * @brief Initialize mender inventory add-on * @param config Mender inventory configuration + * @param callbacks Mender inventory callbacks (not used) * @return MENDER_OK if the function succeeds, error code otherwise */ -mender_err_t mender_inventory_init(mender_inventory_config_t *config); +mender_err_t mender_inventory_init(void *config, void *callbacks); /** * @brief Activate mender inventory add-on @@ -56,6 +63,13 @@ mender_err_t mender_inventory_init(mender_inventory_config_t *config); */ mender_err_t mender_inventory_activate(void); +/** + * @brief Deactivate mender inventory add-on + * @note This function stops synchronization with the server + * @return MENDER_OK if the function succeeds, error code otherwise + */ +mender_err_t mender_inventory_deactivate(void); + /** * @brief Set mender inventory * @param inventory Mender inventory key/value pairs table, must end with a NULL/NULL element, NULL if not defined diff --git a/include/mender-troubleshoot.h b/include/mender-troubleshoot.h index 4e21fb7..dedb01a 100644 --- a/include/mender-troubleshoot.h +++ b/include/mender-troubleshoot.h @@ -32,9 +32,15 @@ extern "C" { #endif /* __cplusplus */ +#include "mender-addon.h" +#include "mender-utils.h" + #ifdef CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT -#include "mender-utils.h" +/** + * @brief Mender troubleshoot instance + */ +extern const mender_addon_instance_t mender_troubleshoot_addon_instance; /** * @brief Mender troubleshoot configuration @@ -56,10 +62,10 @@ typedef struct { /** * @brief Initialize mender troubleshoot add-on * @param config Mender troubleshoot configuration - * @param callbacks Mender troubleshoot callbacks + * @param callbacks Mender troubleshoot callbacks (optional) * @return MENDER_OK if the function succeeds, error code otherwise */ -mender_err_t mender_troubleshoot_init(mender_troubleshoot_config_t *config, mender_troubleshoot_callbacks_t *callbacks); +mender_err_t mender_troubleshoot_init(void *config, void *callbacks); /** * @brief Activate mender troubleshoot add-on diff --git a/tests/src/main.c b/tests/src/main.c index 05ebeb4..af07a30 100644 --- a/tests/src/main.c +++ b/tests/src/main.c @@ -60,21 +60,8 @@ authentication_success_cb(void) { mender_log_info("Mender client authenticated"); - /* Activate mender add-ons */ - /* The application can activate each add-on depending of the current status of the device */ -#ifdef CONFIG_MENDER_CLIENT_ADD_ON_CONFIGURE - if (MENDER_OK != (ret = mender_configure_activate())) { - mender_log_error("Unable to activate configure add-on"); - return ret; - } -#endif /* CONFIG_MENDER_CLIENT_ADD_ON_CONFIGURE */ -#ifdef CONFIG_MENDER_CLIENT_ADD_ON_INVENTORY - if (MENDER_OK != (ret = mender_inventory_activate())) { - mender_log_error("Unable to activate inventory add-on"); - return ret; - } -#endif /* CONFIG_MENDER_CLIENT_ADD_ON_INVENTORY */ #ifdef CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT + /* Activate troubleshoot add-on (deactivated by default) */ if (MENDER_OK != (ret = mender_troubleshoot_activate())) { mender_log_error("Unable to activate troubleshoot add-on"); return ret; @@ -485,16 +472,18 @@ main(int argc, char **argv) { .config_updated = config_updated_cb, #endif /* CONFIG_MENDER_CLIENT_CONFIGURE_STORAGE */ }; - if (MENDER_OK != mender_configure_init(&mender_configure_config, &mender_configure_callbacks)) { - mender_log_error("Unable to initialize mender-configure add-on"); + if (MENDER_OK + != mender_client_register_addon( + (mender_addon_instance_t *)&mender_configure_addon_instance, (void *)&mender_configure_config, (void *)&mender_configure_callbacks)) { + mender_log_error("Unable to register mender-configure add-on"); ret = EXIT_FAILURE; goto RELEASE; } #endif /* CONFIG_MENDER_CLIENT_ADD_ON_CONFIGURE */ #ifdef CONFIG_MENDER_CLIENT_ADD_ON_INVENTORY mender_inventory_config_t mender_inventory_config = { .refresh_interval = 0 }; - if (MENDER_OK != mender_inventory_init(&mender_inventory_config)) { - mender_log_error("Unable to initialize mender-inventory add-on"); + if (MENDER_OK != mender_client_register_addon((mender_addon_instance_t *)&mender_inventory_addon_instance, (void *)&mender_inventory_config, NULL)) { + mender_log_error("Unable to register mender-inventory add-on"); ret = EXIT_FAILURE; goto RELEASE; } @@ -503,37 +492,31 @@ main(int argc, char **argv) { mender_troubleshoot_config_t mender_troubleshoot_config = { .healthcheck_interval = 0 }; mender_troubleshoot_callbacks_t mender_troubleshoot_callbacks = { .shell_begin = shell_begin_cb, .shell_resize = shell_resize_cb, .shell_write = shell_write_cb, .shell_end = shell_end_cb }; - if (MENDER_OK != mender_troubleshoot_init(&mender_troubleshoot_config, &mender_troubleshoot_callbacks)) { - mender_log_error("Unable to initialize mender-troubleshoot add-on"); + if (MENDER_OK + != mender_client_register_addon( + (mender_addon_instance_t *)&mender_troubleshoot_addon_instance, (void *)&mender_troubleshoot_config, (void *)&mender_troubleshoot_callbacks)) { + mender_log_error("Unable to register mender-troubleshoot add-on"); ret = EXIT_FAILURE; goto RELEASE; } #endif /* CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT */ + /* Finally activate mender client */ + if (MENDER_OK != mender_client_activate()) { + mender_log_error("Unable to activate mender-client"); + ret = EXIT_FAILURE; + goto RELEASE; + } + /* Wait for mender-mcu-client events */ pthread_mutex_lock(&mender_client_events_mutex); pthread_cond_wait(&mender_client_events_cond, &mender_client_events_mutex); pthread_mutex_unlock(&mender_client_events_mutex); - /* Deactivate mender add-ons */ -#ifdef CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT - mender_troubleshoot_deactivate(); -#endif /* CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT */ - RELEASE: - /* Release mender add-ons */ -#ifdef CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT - mender_troubleshoot_exit(); -#endif /* CONFIG_MENDER_CLIENT_ADD_ON_TROUBLESHOOT */ -#ifdef CONFIG_MENDER_CLIENT_ADD_ON_INVENTORY - mender_inventory_exit(); -#endif /* CONFIG_MENDER_CLIENT_ADD_ON_INVENTORY */ -#ifdef CONFIG_MENDER_CLIENT_ADD_ON_CONFIGURE - mender_configure_exit(); -#endif /* CONFIG_MENDER_CLIENT_ADD_ON_CONFIGURE */ - - /* Release mender-client */ + /* Deactivate and release mender-client */ + mender_client_deactivate(); mender_client_exit(); END: