Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mgmt: hawkbit: define attributes in the user application #69293

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions samples/subsys/mgmt/hawkbit/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ CONFIG_LOG_BUFFER_SIZE=4096

#Generate HEX output
CONFIG_BUILD_OUTPUT_HEX=y

#Use custom attributes for hawkBit
CONFIG_HAWKBIT_CUSTOM_ATTRIBUTES=y
41 changes: 41 additions & 0 deletions samples/subsys/mgmt/hawkbit/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <zephyr/sys/printk.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/logging/log.h>
#include <zephyr/data/json.h>

#include "dhcp.h"

Expand All @@ -20,6 +21,40 @@

LOG_MODULE_REGISTER(main);

#ifdef CONFIG_HAWKBIT_CUSTOM_ATTRIBUTES
struct hawkbit_cfg_data {
const char *VIN;
const char *customAttr;
};

struct hawkbit_cfg {
const char *mode;
struct hawkbit_cfg_data data;
};

static const struct json_obj_descr json_cfg_data_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_cfg_data, VIN, JSON_TOK_STRING),
JSON_OBJ_DESCR_PRIM(struct hawkbit_cfg_data, customAttr, JSON_TOK_STRING),
};

static const struct json_obj_descr json_cfg_descr[] = {
JSON_OBJ_DESCR_PRIM(struct hawkbit_cfg, mode, JSON_TOK_STRING),
JSON_OBJ_DESCR_OBJECT(struct hawkbit_cfg, data, json_cfg_data_descr),
};

int hawkbit_new_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,
.data.customAttr = "Hello World!",
maass-hamburg marked this conversation as resolved.
Show resolved Hide resolved
};

return json_obj_encode_buf(json_cfg_descr, ARRAY_SIZE(json_cfg_descr), &cfg, buffer,
buffer_size);
}
#endif /* CONFIG_HAWKBIT_CUSTOM_ATTRIBUTES */

int main(void)
{
int ret = -1;
Expand All @@ -33,6 +68,12 @@ int main(void)
tls_credential_add(CA_CERTIFICATE_TAG, TLS_CREDENTIAL_CA_CERTIFICATE,
ca_certificate, sizeof(ca_certificate));
#endif
#ifdef CONFIG_HAWKBIT_CUSTOM_ATTRIBUTES
ret = hawkbit_set_custom_data_cb(hawkbit_new_config_data_cb);
if (ret < 0) {
LOG_ERR("Failed to set custom data callback");
}
#endif /* CONFIG_HAWKBIT_CUSTOM_ATTRIBUTES */

ret = hawkbit_init();

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
Loading