Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: rework apis so that the mender-client registers add-ons and manage them #58

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions add-ons/src/mender-configure.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -85,22 +91,24 @@ 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;
cJSON * json_device_config = NULL;
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))) {
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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;
Expand Down
27 changes: 20 additions & 7 deletions add-ons/src/mender-inventory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -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;
Expand Down
16 changes: 12 additions & 4 deletions add-ons/src/mender-troubleshoot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
Expand Down
168 changes: 159 additions & 9 deletions core/src/mender-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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))) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading
Loading