From 11e58b3aaebc44098677455fb5e47b28c24233f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fin=20Maa=C3=9F?= Date: Thu, 14 Mar 2024 12:17:51 +0100 Subject: [PATCH] mgmt: hawkbit: add support to define attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows the attributes to be defined in the user application by using a callback function. Signed-off-by: Fin Maaß --- include/zephyr/mgmt/hawkbit.h | 27 +++++++++++++++++++++ subsys/mgmt/hawkbit/Kconfig | 13 ++++++++++ subsys/mgmt/hawkbit/hawkbit.c | 45 +++++++++++++++++++++++++++-------- 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/include/zephyr/mgmt/hawkbit.h b/include/zephyr/mgmt/hawkbit.h index 46a0ff34328b..c00ed1bd3bda 100644 --- a/include/zephyr/mgmt/hawkbit.h +++ b/include/zephyr/mgmt/hawkbit.h @@ -36,6 +36,33 @@ enum hawkbit_response { HAWKBIT_PROBE_IN_PROGRESS, }; +/** + * @brief Callback to provide the custom data to the hawkBit server. + * + * @details This callback is used to provide the custom data to the hawkBit server. + * The custom data is used to provide the hawkBit server with the device specific + * data. + * + * @param device_id The device ID. + * @param buffer The buffer to store the json. + * @param buffer_size The size of the buffer. + */ +typedef int (*hawkbit_config_device_data_cb_handler_t)(const char *device_id, uint8_t *buffer, + const size_t buffer_size); + +/** + * @brief Set the custom data callback. + * + * @details This function is used to set the custom data callback. + * The callback is used to provide the custom data to the hawkBit server. + * + * @param cb The callback function. + * + * @return 0 on success. + * @return -EINVAL if the callback is NULL. + */ +int hawkbit_set_custom_data_cb(hawkbit_config_device_data_cb_handler_t cb); + /** * @brief Init the flash partition * diff --git a/subsys/mgmt/hawkbit/Kconfig b/subsys/mgmt/hawkbit/Kconfig index e2365cf3c1d7..40c49cc657d0 100644 --- a/subsys/mgmt/hawkbit/Kconfig +++ b/subsys/mgmt/hawkbit/Kconfig @@ -79,6 +79,19 @@ config HAWKBIT_DDI_SECURITY_TOKEN Authentication security token for the configured hawkBit DDI authentication mode. +config HAWKBIT_CUSTOM_ATTRIBUTES + bool "Custom device attributes" + help + Use custom definition of device attributes. + +config HAWKBIT_STATUS_BUFFER_SIZE + int "hawkBit status buffer size" + default 200 + help + Set the size of the buffer, which is used to store the + json strings, that are sent to the hawkBit server. It might + be increased if the custom attributes are used extensively. + module = HAWKBIT module-str = Log Level for hawkbit module-help = Enables logging for hawkBit code. diff --git a/subsys/mgmt/hawkbit/hawkbit.c b/subsys/mgmt/hawkbit/hawkbit.c index 1bfcdc630646..5a6dc7aaecad 100644 --- a/subsys/mgmt/hawkbit/hawkbit.c +++ b/subsys/mgmt/hawkbit/hawkbit.c @@ -44,7 +44,6 @@ LOG_MODULE_REGISTER(hawkbit, CONFIG_HAWKBIT_LOG_LEVEL); #define RECV_BUFFER_SIZE 640 #define URL_BUFFER_SIZE 300 #define SHA256_HASH_SIZE 32 -#define STATUS_BUFFER_SIZE 200 #define DOWNLOAD_HTTP_SIZE 200 #define DEPLOYMENT_BASE_SIZE 50 #define RESPONSE_BUFFER_SIZE 1100 @@ -80,7 +79,7 @@ static struct hawkbit_context { struct http_request http_req; struct flash_img_context flash_ctx; uint8_t url_buffer[URL_BUFFER_SIZE]; - uint8_t status_buffer[STATUS_BUFFER_SIZE]; + uint8_t status_buffer[CONFIG_HAWKBIT_STATUS_BUFFER_SIZE]; uint8_t recv_buf_tcp[RECV_BUFFER_SIZE]; enum hawkbit_response code_status; bool final_data_received; @@ -92,6 +91,12 @@ static union { struct hawkbit_cancel cancel; } hawkbit_results; +int hawkbit_default_config_data_cb(const char *device_id, uint8_t *buffer, + const size_t buffer_size); + +static hawkbit_config_device_data_cb_handler_t hawkbit_config_device_data_cb_handler = + hawkbit_default_config_data_cb; + static struct k_work_delayable hawkbit_work_handle; static struct k_sem probe_sem; @@ -546,6 +551,32 @@ static void hawkbit_dump_deployment(struct hawkbit_dep_res *d) LOG_DBG("%s=%s", "md5sum-http", l->md5sum_http.href); } +int hawkbit_set_custom_data_cb(hawkbit_config_device_data_cb_handler_t cb) +{ + if (IS_ENABLED(CONFIG_HAWKBIT_CUSTOM_ATTRIBUTES)) { + if (cb == NULL) { + LOG_ERR("Invalid callback"); + return -EINVAL; + } + + hawkbit_config_device_data_cb_handler = cb; + + return 0; + } + return -ENOTSUP; +} + +int hawkbit_default_config_data_cb(const char *device_id, uint8_t *buffer, const size_t buffer_size) +{ + struct hawkbit_cfg cfg = { + .mode = "merge", + .data.VIN = device_id, + }; + + return json_obj_encode_buf(json_cfg_descr, ARRAY_SIZE(json_cfg_descr), &cfg, buffer, + buffer_size); +} + int hawkbit_init(void) { bool image_ok; @@ -824,14 +855,8 @@ static bool send_request(enum http_method method, enum hawkbit_http_request type hb_context.code_status = HAWKBIT_METADATA_ERROR; } - struct hawkbit_cfg cfg = { - .mode = "merge", - .data.VIN = device_id, - }; - - ret = json_obj_encode_buf(json_cfg_descr, ARRAY_SIZE(json_cfg_descr), &cfg, - hb_context.status_buffer, - sizeof(hb_context.status_buffer)); + ret = hawkbit_config_device_data_cb_handler(device_id, hb_context.status_buffer, + sizeof(hb_context.status_buffer)); if (ret) { LOG_ERR("Can't encode the JSON script (%s): %d", "HAWKBIT_CONFIG_DEVICE", ret);