Skip to content

Commit

Permalink
mgmt: hawkbit: move to zephyr kernel heap
Browse files Browse the repository at this point in the history
switch to kernel heap from the libc, as there
is now a `k_realloc()` available.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
  • Loading branch information
maass-hamburg authored and carlescufi committed Aug 5, 2024
1 parent 8e1ac56 commit 8f9148e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
6 changes: 6 additions & 0 deletions subsys/mgmt/hawkbit/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ config HAWKBIT_REBOOT_COLD

endchoice

config HEAP_MEM_POOL_ADD_SIZE_HAWKBIT
int "Heap memory pool size for hawkBit"
default 4096
help
Set the size of the heap memory pool for hawkBit.

module = HAWKBIT
module-str = Log Level for hawkbit
module-help = Enables logging for hawkBit code.
Expand Down
33 changes: 23 additions & 10 deletions subsys/mgmt/hawkbit/hawkbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ static struct hawkbit_context {
int sock;
int32_t action_id;
uint8_t *response_data;
size_t response_data_size;
int32_t json_action_id;
struct hawkbit_download dl;
struct http_request http_req;
Expand Down Expand Up @@ -859,7 +860,6 @@ static void response_cb(struct http_response *rsp, enum http_final_call final_da
static size_t body_len;
int ret, type, downloaded;
uint8_t *body_data = NULL, *rsp_tmp = NULL;
static size_t response_buffer_size = RESPONSE_BUFFER_SIZE;

type = enum_for_http_req_string(userdata);

Expand All @@ -883,9 +883,12 @@ static void response_cb(struct http_response *rsp, enum http_final_call final_da
body_data = rsp->body_frag_start;
body_len = rsp->body_frag_len;

if ((hb_context.dl.downloaded_size + body_len) > response_buffer_size) {
response_buffer_size <<= 1;
rsp_tmp = realloc(hb_context.response_data, response_buffer_size);
if ((hb_context.dl.downloaded_size + body_len) >
hb_context.response_data_size) {
hb_context.response_data_size =
hb_context.dl.downloaded_size + body_len;
rsp_tmp = k_realloc(hb_context.response_data,
hb_context.response_data_size);
if (rsp_tmp == NULL) {
LOG_ERR("Failed to realloc memory");
hb_context.code_status = HAWKBIT_METADATA_ERROR;
Expand Down Expand Up @@ -934,9 +937,12 @@ static void response_cb(struct http_response *rsp, enum http_final_call final_da
body_data = rsp->body_frag_start;
body_len = rsp->body_frag_len;

if ((hb_context.dl.downloaded_size + body_len) > response_buffer_size) {
response_buffer_size <<= 1;
rsp_tmp = realloc(hb_context.response_data, response_buffer_size);
if ((hb_context.dl.downloaded_size + body_len) >
hb_context.response_data_size) {
hb_context.response_data_size =
hb_context.dl.downloaded_size + body_len;
rsp_tmp = k_realloc(hb_context.response_data,
hb_context.response_data_size);
if (rsp_tmp == NULL) {
LOG_ERR("Failed to realloc memory");
hb_context.code_status = HAWKBIT_METADATA_ERROR;
Expand Down Expand Up @@ -1251,7 +1257,14 @@ enum hawkbit_response hawkbit_probe(void)
}

memset(&hb_context, 0, sizeof(hb_context));
hb_context.response_data = malloc(RESPONSE_BUFFER_SIZE);

hb_context.response_data_size = RESPONSE_BUFFER_SIZE;
hb_context.response_data = k_calloc(hb_context.response_data_size, sizeof(uint8_t));
if (hb_context.response_data == NULL) {
LOG_ERR("Failed to allocate memory");
hb_context.code_status = HAWKBIT_METADATA_ERROR;
goto error;
}

if (!boot_is_img_confirmed()) {
LOG_ERR("Current image is not confirmed");
Expand Down Expand Up @@ -1359,7 +1372,7 @@ enum hawkbit_response hawkbit_probe(void)
snprintk(hb_context.url_buffer, sizeof(hb_context.url_buffer), "%s/%s-%s/%s",
HAWKBIT_JSON_URL, CONFIG_BOARD, device_id, deployment_base);
memset(&hawkbit_results.dep, 0, sizeof(hawkbit_results.dep));
memset(hb_context.response_data, 0, RESPONSE_BUFFER_SIZE);
memset(hb_context.response_data, 0, hb_context.response_data_size);

if (!send_request(HTTP_GET, HAWKBIT_PROBE_DEPLOYMENT_BASE, HAWKBIT_STATUS_FINISHED_NONE,
HAWKBIT_STATUS_EXEC_NONE)) {
Expand Down Expand Up @@ -1453,7 +1466,7 @@ enum hawkbit_response hawkbit_probe(void)
cleanup_connection();

error:
free(hb_context.response_data);
k_free(hb_context.response_data);
k_sem_give(&probe_sem);
return hb_context.code_status;
}
Expand Down

0 comments on commit 8f9148e

Please sign in to comment.