diff --git a/samples/subsys/mgmt/hawkbit/prj.conf b/samples/subsys/mgmt/hawkbit/prj.conf index 9c2362964cc9d7..ebbd3b09f78a8b 100644 --- a/samples/subsys/mgmt/hawkbit/prj.conf +++ b/samples/subsys/mgmt/hawkbit/prj.conf @@ -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 diff --git a/samples/subsys/mgmt/hawkbit/src/main.c b/samples/subsys/mgmt/hawkbit/src/main.c index 3e917f614a42b8..6cf6ea0bf72f6e 100644 --- a/samples/subsys/mgmt/hawkbit/src/main.c +++ b/samples/subsys/mgmt/hawkbit/src/main.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "dhcp.h" @@ -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; @@ -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();