Skip to content

Commit 6cc2bb5

Browse files
maass-hamburgfabiobaltieri
authored andcommitted
mgmt: hawkbit: add hawkbit_autohandler_wait
Add hawkbit_autohandler_wait() to be able to wait for the autohandler to finish. Signed-off-by: Fin Maaß <[email protected]>
1 parent cf5f6aa commit 6cc2bb5

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

include/zephyr/mgmt/hawkbit.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*
2727
*/
2828
enum hawkbit_response {
29+
HAWKBIT_NO_RESPONSE,
2930
HAWKBIT_NETWORKING_ERROR,
3031
HAWKBIT_UNCONFIRMED_IMAGE,
3132
HAWKBIT_PERMISSION_ERROR,
@@ -97,6 +98,30 @@ int hawkbit_init(void);
9798
*/
9899
void hawkbit_autohandler(bool auto_reschedule);
99100

101+
/**
102+
* @brief Wait for the autohandler to finish.
103+
*
104+
* @param events Set of desired events on which to wait. Set to ::UINT32_MAX to wait for the
105+
* autohandler to finish one run, or BIT() together with a value from
106+
* ::hawkbit_response to wait for a specific event.
107+
* @param timeout Waiting period for the desired set of events or one of the
108+
* special values ::K_NO_WAIT and ::K_FOREVER.
109+
*
110+
* @retval HAWKBIT_OK if success.
111+
* @retval HAWKBIT_NO_RESPONSE if matching events were not received within the specified time
112+
* @retval HAWKBIT_NETWORKING_ERROR fail to connect to the hawkBit server.
113+
* @retval HAWKBIT_UNCONFIRMED_IMAGE image is unconfirmed.
114+
* @retval HAWKBIT_PERMISSION_ERROR fail to get the permission to access the hawkBit server.
115+
* @retval HAWKBIT_METADATA_ERROR fail to parse or to encode the metadata.
116+
* @retval HAWKBIT_DOWNLOAD_ERROR fail while downloading the update package.
117+
* @retval HAWKBIT_UPDATE_INSTALLED if an update was installed. Reboot is required to apply it.
118+
* @retval HAWKBIT_NO_UPDATE if no update was available.
119+
* @retval HAWKBIT_CANCEL_UPDATE update was cancelled.
120+
* @retval HAWKBIT_NOT_INITIALIZED if hawkBit is not initialized.
121+
* @retval HAWKBIT_PROBE_IN_PROGRESS if probe is currently running.
122+
*/
123+
enum hawkbit_response hawkbit_autohandler_wait(uint32_t events, k_timeout_t timeout);
124+
100125
/**
101126
* @brief The hawkBit probe verify if there is some update to be performed.
102127
*

subsys/mgmt/hawkbit/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ menuconfig HAWKBIT
1515
depends on DNS_RESOLVER
1616
depends on JSON_LIBRARY
1717
depends on BOOTLOADER_MCUBOOT
18+
select EVENTS
1819
select MPU_ALLOW_FLASH_WRITE
1920
select IMG_ENABLE_IMAGE_CHECK
2021
select IMG_ERASE_PROGRESSIVELY

subsys/mgmt/hawkbit/hawkbit.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ static void autohandler(struct k_work *work);
150150
static K_WORK_DELAYABLE_DEFINE(hawkbit_work_handle, autohandler);
151151
static K_WORK_DELAYABLE_DEFINE(hawkbit_work_handle_once, autohandler);
152152

153+
static K_EVENT_DEFINE(hawkbit_autohandler_event);
154+
153155
K_SEM_DEFINE(probe_sem, 1, 1);
154156

155157
static const struct json_obj_descr json_href_descr[] = {
@@ -1476,7 +1478,13 @@ enum hawkbit_response hawkbit_probe(void)
14761478

14771479
static void autohandler(struct k_work *work)
14781480
{
1479-
switch (hawkbit_probe()) {
1481+
k_event_clear(&hawkbit_autohandler_event, UINT32_MAX);
1482+
1483+
enum hawkbit_response response = hawkbit_probe();
1484+
1485+
k_event_set(&hawkbit_autohandler_event, BIT(response));
1486+
1487+
switch (response) {
14801488
case HAWKBIT_UNCONFIRMED_IMAGE:
14811489
LOG_ERR("Current image is not confirmed");
14821490
LOG_ERR("Rebooting to previous confirmed image");
@@ -1525,12 +1533,27 @@ static void autohandler(struct k_work *work)
15251533
case HAWKBIT_PROBE_IN_PROGRESS:
15261534
LOG_INF("hawkBit is already running");
15271535
break;
1536+
1537+
default:
1538+
LOG_ERR("Invalid response: %d", response);
1539+
break;
15281540
}
15291541

15301542
if (k_work_delayable_from_work(work) == &hawkbit_work_handle) {
15311543
k_work_reschedule(&hawkbit_work_handle, K_SECONDS(poll_sleep));
15321544
}
15331545
}
1546+
1547+
enum hawkbit_response hawkbit_autohandler_wait(uint32_t events, k_timeout_t timeout)
1548+
{
1549+
uint32_t ret = k_event_wait(&hawkbit_autohandler_event, events, false, timeout);
1550+
1551+
for (int i = 1; i < HAWKBIT_PROBE_IN_PROGRESS; i++) {
1552+
if (ret & BIT(i)) {
1553+
return i;
1554+
}
1555+
}
1556+
return HAWKBIT_NO_RESPONSE;
15341557
}
15351558

15361559
void hawkbit_autohandler(bool auto_reschedule)

0 commit comments

Comments
 (0)