Skip to content

Commit

Permalink
mgmt: hawkbit: also use workqueue for shell run
Browse files Browse the repository at this point in the history
Also use a workqueue, when execution of
hawkBit is requested via shell.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
  • Loading branch information
maass-hamburg committed Apr 10, 2024
1 parent d1f0970 commit 4755f4d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
26 changes: 25 additions & 1 deletion include/zephyr/mgmt/hawkbit.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*
*/
enum hawkbit_response {
HAWKBIT_NO_RESPONSE,
HAWKBIT_NETWORKING_ERROR,
HAWKBIT_UNCONFIRMED_IMAGE,
HAWKBIT_PERMISSION_ERROR,
Expand All @@ -48,8 +49,30 @@ int hawkbit_init(void);
*
* @details The hawkbit_autohandler handles the whole process
* in pre-determined time intervals.
*
* @param auto_reschedule If true, the handler will reschedule itself
*/
void hawkbit_autohandler(void);
void hawkbit_autohandler(bool auto_reschedule);

/**
* @brief Wait for the autohandler to finish.
*
* @param events Set of desired events on which to wait
* @param timeout Waiting period for the desired set of events or one of the
* special values K_NO_WAIT and K_FOREVER.
*
* @return HAWKBIT_NO_RESPONSE if matching events were not received within the specified time
* @return HAWKBIT_NETWORKING_ERROR fail to connect to the hawkBit server.
* @return HAWKBIT_UNCONFIRMED_IMAGE image is unconfirmed.
* @return HAWKBIT_PERMISSION_ERROR fail to get the permission to access the hawkBit server.
* @return HAWKBIT_METADATA_ERROR fail to parse or to encode the metadata.
* @return HAWKBIT_DOWNLOAD_ERROR fail while downloading the update package.
* @return HAWKBIT_OK if success.
* @return HAWKBIT_UPDATE_INSTALLED has an update available.
* @return HAWKBIT_NO_UPDATE no update available.
* @return HAWKBIT_CANCEL_UPDATE update was cancelled.
*/
enum hawkbit_response hawkbit_autohandler_wait(uint32_t events, k_timeout_t timeout);

/**
* @brief The hawkBit probe verify if there is some update to be performed.
Expand All @@ -63,6 +86,7 @@ void hawkbit_autohandler(void);
*/
enum hawkbit_response hawkbit_probe(void);


/**
* @brief Request system to reboot.
*/
Expand Down
2 changes: 1 addition & 1 deletion samples/subsys/mgmt/hawkbit/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main(void)

#if defined(CONFIG_HAWKBIT_POLLING)
LOG_INF("Starting hawkBit polling mode");
hawkbit_autohandler();
hawkbit_autohandler(true);
#endif

#if defined(CONFIG_HAWKBIT_MANUAL)
Expand Down
1 change: 1 addition & 0 deletions subsys/mgmt/hawkbit/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ menuconfig HAWKBIT
depends on DNS_RESOLVER
depends on JSON_LIBRARY
depends on BOOTLOADER_MCUBOOT
select EVENTS
select MPU_ALLOW_FLASH_WRITE
select IMG_ENABLE_IMAGE_CHECK
select IMG_ERASE_PROGRESSIVELY
Expand Down
35 changes: 31 additions & 4 deletions subsys/mgmt/hawkbit/hawkbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ static union {
static void autohandler(struct k_work *work);

static K_WORK_DELAYABLE_DEFINE(hawkbit_work_handle, autohandler);
static K_WORK_DELAYABLE_DEFINE(hawkbit_work_handle_once, autohandler);

static K_EVENT_DEFINE(hawkbit_event);

static struct k_sem probe_sem;

Expand Down Expand Up @@ -1188,7 +1191,13 @@ enum hawkbit_response hawkbit_probe(void)

static void autohandler(struct k_work *work)
{
switch (hawkbit_probe()) {
k_event_clear(&hawkbit_event, __UINT32_MAX__);

enum hawkbit_response response = hawkbit_probe();

k_event_set(&hawkbit_event, BIT(response));

switch (response) {
case HAWKBIT_UNCONFIRMED_IMAGE:
LOG_ERR("Current image is not confirmed");
LOG_ERR("Rebooting to previous confirmed image");
Expand Down Expand Up @@ -1235,10 +1244,28 @@ static void autohandler(struct k_work *work)
break;
}

k_work_reschedule(&hawkbit_work_handle, K_SECONDS(poll_sleep));
if (k_work_delayable_from_work(work) == &hawkbit_work_handle) {
k_work_reschedule(&hawkbit_work_handle, K_SECONDS(poll_sleep));
}
}

void hawkbit_autohandler(void)
enum hawkbit_response hawkbit_autohandler_wait(uint32_t events, k_timeout_t timeout)
{
k_work_reschedule(&hawkbit_work_handle, K_NO_WAIT);
uint32_t ret = k_event_wait(&hawkbit_event, events, false, timeout);

for (int i = HAWKBIT_NETWORKING_ERROR; i < HAWKBIT_PROBE_IN_PROGRESS; i++) {
if (ret & BIT(i)) {
return i;
}
}
return HAWKBIT_NO_RESPONSE;
}

void hawkbit_autohandler(bool auto_reschedule)
{
if (auto_reschedule) {
k_work_reschedule(&hawkbit_work_handle, K_NO_WAIT);
} else {
k_work_reschedule(&hawkbit_work_handle_once, K_NO_WAIT);
}
}
6 changes: 3 additions & 3 deletions subsys/mgmt/hawkbit/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ static void cmd_run(const struct shell *sh, size_t argc, char **argv)
ARG_UNUSED(argv);

shell_fprintf(sh, SHELL_INFO, "Starting hawkBit run...\n");
switch (hawkbit_probe()) {
hawkbit_autohandler(false);

switch (hawkbit_autohandler_wait(__UINT32_MAX__, K_FOREVER)) {
case HAWKBIT_UNCONFIRMED_IMAGE:
shell_fprintf(
sh, SHELL_ERROR,
"Image is unconfirmed."
"Rebooting to revert back to previous confirmed image\n");
hawkbit_reboot();
break;

case HAWKBIT_CANCEL_UPDATE:
Expand All @@ -42,7 +43,6 @@ static void cmd_run(const struct shell *sh, size_t argc, char **argv)

case HAWKBIT_UPDATE_INSTALLED:
shell_fprintf(sh, SHELL_INFO, "Update Installed\n");
hawkbit_reboot();
break;

case HAWKBIT_DOWNLOAD_ERROR:
Expand Down

0 comments on commit 4755f4d

Please sign in to comment.