Skip to content

Commit

Permalink
samples: mgmt: hawkbit: add custom attributes
Browse files Browse the repository at this point in the history
Extend sample for the custom attributes

Signed-off-by: Fin Maaß <[email protected]>
  • Loading branch information
maass-hamburg authored and carlescufi committed Apr 17, 2024
1 parent 11e58b3 commit d0be201
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
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!",
};

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

0 comments on commit d0be201

Please sign in to comment.