Skip to content

Commit dce3d2d

Browse files
maass-hamburgfabiobaltieri
authored andcommitted
mgmt: hawkbit: seperate autohandler
seperate the autohandler from the main hawkbit source. This way the autohandler can be disabled if it is not needed. Signed-off-by: Fin Maaß <[email protected]>
1 parent 621cf5b commit dce3d2d

File tree

5 files changed

+198
-171
lines changed

5 files changed

+198
-171
lines changed

include/zephyr/mgmt/hawkbit.h

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -88,62 +88,6 @@ int hawkbit_set_custom_data_cb(hawkbit_config_device_data_cb_handler_t cb);
8888
*/
8989
int hawkbit_init(void);
9090

91-
/**
92-
* @brief Runs hawkBit probe and hawkBit update automatically
93-
*
94-
* @details The hawkbit_autohandler handles the whole process
95-
* in pre-determined time intervals.
96-
*
97-
* @param auto_reschedule If true, the handler will reschedule itself
98-
*/
99-
void hawkbit_autohandler(bool auto_reschedule);
100-
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-
125-
/**
126-
* @brief Cancel the run of the hawkBit autohandler.
127-
*
128-
* @return a value from k_work_cancel_delayable().
129-
*/
130-
int hawkbit_autohandler_cancel(void);
131-
132-
/**
133-
* @brief Set the delay for the next run of the autohandler.
134-
*
135-
* @details This function will only delay the next run of the autohandler. The delay will not
136-
* persist after the autohandler runs.
137-
*
138-
* @param timeout The delay to set.
139-
* @param if_bigger If true, the delay will be set only if the new delay is bigger than the current
140-
* one.
141-
*
142-
* @retval 0 if @a if_bigger was true and the current delay was bigger than the new one.
143-
* @retval otherwise, a value from k_work_reschedule().
144-
*/
145-
int hawkbit_autohandler_set_delay(k_timeout_t timeout, bool if_bigger);
146-
14791
/**
14892
* @brief The hawkBit probe verify if there is some update to be performed.
14993
*
@@ -330,6 +274,70 @@ uint32_t hawkbit_get_poll_interval(void);
330274
int hawkbit_reset_action_id(void);
331275

332276
/**
277+
* @brief hawkBit autohandler.
278+
* @defgroup hawkbit_autohandler hawkBit autohandler
279+
* @ingroup hawkbit
280+
* @{
281+
*/
282+
283+
/**
284+
* @brief Runs hawkBit probe and hawkBit update automatically
285+
*
286+
* @details The hawkbit_autohandler handles the whole process
287+
* in pre-determined time intervals.
288+
*
289+
* @param auto_reschedule If true, the handler will reschedule itself
290+
*/
291+
void hawkbit_autohandler(bool auto_reschedule);
292+
293+
/**
294+
* @brief Wait for the autohandler to finish.
295+
*
296+
* @param events Set of desired events on which to wait. Set to ::UINT32_MAX to wait for the
297+
* autohandler to finish one run, or BIT() together with a value from
298+
* ::hawkbit_response to wait for a specific event.
299+
* @param timeout Waiting period for the desired set of events or one of the
300+
* special values ::K_NO_WAIT and ::K_FOREVER.
301+
*
302+
* @retval HAWKBIT_OK if success.
303+
* @retval HAWKBIT_NO_RESPONSE if matching events were not received within the specified time
304+
* @retval HAWKBIT_NETWORKING_ERROR fail to connect to the hawkBit server.
305+
* @retval HAWKBIT_UNCONFIRMED_IMAGE image is unconfirmed.
306+
* @retval HAWKBIT_PERMISSION_ERROR fail to get the permission to access the hawkBit server.
307+
* @retval HAWKBIT_METADATA_ERROR fail to parse or to encode the metadata.
308+
* @retval HAWKBIT_DOWNLOAD_ERROR fail while downloading the update package.
309+
* @retval HAWKBIT_UPDATE_INSTALLED if an update was installed. Reboot is required to apply it.
310+
* @retval HAWKBIT_NO_UPDATE if no update was available.
311+
* @retval HAWKBIT_CANCEL_UPDATE update was cancelled.
312+
* @retval HAWKBIT_NOT_INITIALIZED if hawkBit is not initialized.
313+
* @retval HAWKBIT_PROBE_IN_PROGRESS if probe is currently running.
314+
*/
315+
enum hawkbit_response hawkbit_autohandler_wait(uint32_t events, k_timeout_t timeout);
316+
317+
/**
318+
* @brief Cancel the run of the hawkBit autohandler.
319+
*
320+
* @return a value from k_work_cancel_delayable().
321+
*/
322+
int hawkbit_autohandler_cancel(void);
323+
324+
/**
325+
* @brief Set the delay for the next run of the autohandler.
326+
*
327+
* @details This function will only delay the next run of the autohandler. The delay will not
328+
* persist after the autohandler runs.
329+
*
330+
* @param timeout The delay to set.
331+
* @param if_bigger If true, the delay will be set only if the new delay is bigger than the current
332+
* one.
333+
*
334+
* @retval 0 if @a if_bigger was true and the current delay was bigger than the new one.
335+
* @retval otherwise, a value from k_work_reschedule().
336+
*/
337+
int hawkbit_autohandler_set_delay(k_timeout_t timeout, bool if_bigger);
338+
339+
/**
340+
* @}
333341
* @}
334342
*/
335343

subsys/mgmt/hawkbit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
zephyr_library()
88

99
zephyr_library_sources_ifdef(CONFIG_HAWKBIT hawkbit.c)
10+
zephyr_library_sources_ifdef(CONFIG_HAWKBIT_AUTOHANDLER hawkbit_autohandler.c)
1011
zephyr_library_sources_ifdef(CONFIG_HAWKBIT hawkbit_device.c)
1112
zephyr_library_sources_ifdef(CONFIG_HAWKBIT hawkbit_firmware.c)
1213
zephyr_library_sources_ifdef(CONFIG_HAWKBIT_SHELL shell.c)

subsys/mgmt/hawkbit/Kconfig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ menuconfig HAWKBIT
1515
depends on DNS_RESOLVER
1616
depends on JSON_LIBRARY
1717
depends on BOOTLOADER_MCUBOOT
18-
select EVENTS
1918
select MPU_ALLOW_FLASH_WRITE
2019
select IMG_ENABLE_IMAGE_CHECK
2120
select IMG_ERASE_PROGRESSIVELY
@@ -34,9 +33,17 @@ config HAWKBIT_POLL_INTERVAL
3433
This time interval is zero and 43200 minutes(30 days). This will be overridden
3534
by the value configured in the settings of the hawkBit server.
3635

36+
config HAWKBIT_AUTOHANDLER
37+
bool "hawkBit autohandler"
38+
select EVENTS
39+
default y
40+
help
41+
Activate autohandler to handle the update process automatically.
42+
3743
config HAWKBIT_SHELL
3844
bool "hawkBit shell utilities"
3945
depends on SHELL
46+
depends on HAWKBIT_AUTOHANDLER
4047
help
4148
Activate shell module that provides hawkBit commands.
4249

subsys/mgmt/hawkbit/hawkbit.c

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,6 @@ int hawkbit_default_config_data_cb(const char *device_id, uint8_t *buffer,
145145
static hawkbit_config_device_data_cb_handler_t hawkbit_config_device_data_cb_handler =
146146
hawkbit_default_config_data_cb;
147147

148-
static void autohandler(struct k_work *work);
149-
150-
static K_WORK_DELAYABLE_DEFINE(hawkbit_work_handle, autohandler);
151-
static K_WORK_DELAYABLE_DEFINE(hawkbit_work_handle_once, autohandler);
152-
153-
static K_EVENT_DEFINE(hawkbit_autohandler_event);
154-
155148
K_SEM_DEFINE(probe_sem, 1, 1);
156149

157150
static const struct json_obj_descr json_href_descr[] = {
@@ -1480,110 +1473,3 @@ enum hawkbit_response hawkbit_probe(void)
14801473
k_sem_give(&probe_sem);
14811474
return hb_context.code_status;
14821475
}
1483-
1484-
static void autohandler(struct k_work *work)
1485-
{
1486-
k_event_clear(&hawkbit_autohandler_event, UINT32_MAX);
1487-
1488-
enum hawkbit_response response = hawkbit_probe();
1489-
1490-
k_event_set(&hawkbit_autohandler_event, BIT(response));
1491-
1492-
switch (response) {
1493-
case HAWKBIT_UNCONFIRMED_IMAGE:
1494-
LOG_ERR("Current image is not confirmed");
1495-
LOG_ERR("Rebooting to previous confirmed image");
1496-
LOG_ERR("If this image is flashed using a hardware tool");
1497-
LOG_ERR("Make sure that it is a confirmed image");
1498-
hawkbit_reboot();
1499-
break;
1500-
1501-
case HAWKBIT_NO_UPDATE:
1502-
LOG_INF("No update found");
1503-
break;
1504-
1505-
case HAWKBIT_CANCEL_UPDATE:
1506-
LOG_INF("hawkBit update cancelled from server");
1507-
break;
1508-
1509-
case HAWKBIT_OK:
1510-
LOG_INF("Image is already updated");
1511-
break;
1512-
1513-
case HAWKBIT_UPDATE_INSTALLED:
1514-
LOG_INF("Update installed");
1515-
hawkbit_reboot();
1516-
break;
1517-
1518-
case HAWKBIT_DOWNLOAD_ERROR:
1519-
LOG_INF("Update failed");
1520-
break;
1521-
1522-
case HAWKBIT_NETWORKING_ERROR:
1523-
LOG_INF("Network error");
1524-
break;
1525-
1526-
case HAWKBIT_PERMISSION_ERROR:
1527-
LOG_INF("Permission error");
1528-
break;
1529-
1530-
case HAWKBIT_METADATA_ERROR:
1531-
LOG_INF("Metadata error");
1532-
break;
1533-
1534-
case HAWKBIT_NOT_INITIALIZED:
1535-
LOG_INF("hawkBit not initialized");
1536-
break;
1537-
1538-
case HAWKBIT_PROBE_IN_PROGRESS:
1539-
LOG_INF("hawkBit is already running");
1540-
break;
1541-
1542-
default:
1543-
LOG_ERR("Invalid response: %d", response);
1544-
break;
1545-
}
1546-
1547-
if (k_work_delayable_from_work(work) == &hawkbit_work_handle) {
1548-
k_work_reschedule(&hawkbit_work_handle, K_SECONDS(poll_sleep));
1549-
}
1550-
}
1551-
1552-
enum hawkbit_response hawkbit_autohandler_wait(uint32_t events, k_timeout_t timeout)
1553-
{
1554-
uint32_t ret = k_event_wait(&hawkbit_autohandler_event, events, false, timeout);
1555-
1556-
for (int i = 1; i < HAWKBIT_PROBE_IN_PROGRESS; i++) {
1557-
if (ret & BIT(i)) {
1558-
return i;
1559-
}
1560-
}
1561-
return HAWKBIT_NO_RESPONSE;
1562-
}
1563-
1564-
int hawkbit_autohandler_cancel(void)
1565-
{
1566-
return k_work_cancel_delayable(&hawkbit_work_handle);
1567-
}
1568-
1569-
int hawkbit_autohandler_set_delay(k_timeout_t timeout, bool if_bigger)
1570-
{
1571-
if (!if_bigger || timeout.ticks > k_work_delayable_remaining_get(&hawkbit_work_handle)) {
1572-
hawkbit_autohandler_cancel();
1573-
LOG_INF("Setting new delay for next run: %02u:%02u:%02u",
1574-
(uint32_t)(timeout.ticks / CONFIG_SYS_CLOCK_TICKS_PER_SEC) / 3600,
1575-
(uint32_t)((timeout.ticks / CONFIG_SYS_CLOCK_TICKS_PER_SEC) % 3600) / 60,
1576-
(uint32_t)(timeout.ticks / CONFIG_SYS_CLOCK_TICKS_PER_SEC) % 60);
1577-
return k_work_reschedule(&hawkbit_work_handle, timeout);
1578-
}
1579-
return 0;
1580-
}
1581-
1582-
void hawkbit_autohandler(bool auto_reschedule)
1583-
{
1584-
if (auto_reschedule) {
1585-
k_work_reschedule(&hawkbit_work_handle, K_NO_WAIT);
1586-
} else {
1587-
k_work_reschedule(&hawkbit_work_handle_once, K_NO_WAIT);
1588-
}
1589-
}

0 commit comments

Comments
 (0)