Skip to content

Commit

Permalink
mgmt: hawkbit: add support to define attributes
Browse files Browse the repository at this point in the history
Allows the attributes to be defined in the user application
by using a callback function.

Signed-off-by: Fin Maaß <[email protected]>
  • Loading branch information
maass-hamburg authored and carlescufi committed Apr 17, 2024
1 parent 67a293e commit 11e58b3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
27 changes: 27 additions & 0 deletions include/zephyr/mgmt/hawkbit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
13 changes: 13 additions & 0 deletions subsys/mgmt/hawkbit/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
45 changes: 35 additions & 10 deletions subsys/mgmt/hawkbit/hawkbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 11e58b3

Please sign in to comment.