Skip to content

Commit 8f9148e

Browse files
maass-hamburgcarlescufi
authored andcommitted
mgmt: hawkbit: move to zephyr kernel heap
switch to kernel heap from the libc, as there is now a `k_realloc()` available. Signed-off-by: Fin Maaß <[email protected]>
1 parent 8e1ac56 commit 8f9148e

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

subsys/mgmt/hawkbit/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ config HAWKBIT_REBOOT_COLD
161161

162162
endchoice
163163

164+
config HEAP_MEM_POOL_ADD_SIZE_HAWKBIT
165+
int "Heap memory pool size for hawkBit"
166+
default 4096
167+
help
168+
Set the size of the heap memory pool for hawkBit.
169+
164170
module = HAWKBIT
165171
module-str = Log Level for hawkbit
166172
module-help = Enables logging for hawkBit code.

subsys/mgmt/hawkbit/hawkbit.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static struct hawkbit_context {
121121
int sock;
122122
int32_t action_id;
123123
uint8_t *response_data;
124+
size_t response_data_size;
124125
int32_t json_action_id;
125126
struct hawkbit_download dl;
126127
struct http_request http_req;
@@ -859,7 +860,6 @@ static void response_cb(struct http_response *rsp, enum http_final_call final_da
859860
static size_t body_len;
860861
int ret, type, downloaded;
861862
uint8_t *body_data = NULL, *rsp_tmp = NULL;
862-
static size_t response_buffer_size = RESPONSE_BUFFER_SIZE;
863863

864864
type = enum_for_http_req_string(userdata);
865865

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

886-
if ((hb_context.dl.downloaded_size + body_len) > response_buffer_size) {
887-
response_buffer_size <<= 1;
888-
rsp_tmp = realloc(hb_context.response_data, response_buffer_size);
886+
if ((hb_context.dl.downloaded_size + body_len) >
887+
hb_context.response_data_size) {
888+
hb_context.response_data_size =
889+
hb_context.dl.downloaded_size + body_len;
890+
rsp_tmp = k_realloc(hb_context.response_data,
891+
hb_context.response_data_size);
889892
if (rsp_tmp == NULL) {
890893
LOG_ERR("Failed to realloc memory");
891894
hb_context.code_status = HAWKBIT_METADATA_ERROR;
@@ -934,9 +937,12 @@ static void response_cb(struct http_response *rsp, enum http_final_call final_da
934937
body_data = rsp->body_frag_start;
935938
body_len = rsp->body_frag_len;
936939

937-
if ((hb_context.dl.downloaded_size + body_len) > response_buffer_size) {
938-
response_buffer_size <<= 1;
939-
rsp_tmp = realloc(hb_context.response_data, response_buffer_size);
940+
if ((hb_context.dl.downloaded_size + body_len) >
941+
hb_context.response_data_size) {
942+
hb_context.response_data_size =
943+
hb_context.dl.downloaded_size + body_len;
944+
rsp_tmp = k_realloc(hb_context.response_data,
945+
hb_context.response_data_size);
940946
if (rsp_tmp == NULL) {
941947
LOG_ERR("Failed to realloc memory");
942948
hb_context.code_status = HAWKBIT_METADATA_ERROR;
@@ -1251,7 +1257,14 @@ enum hawkbit_response hawkbit_probe(void)
12511257
}
12521258

12531259
memset(&hb_context, 0, sizeof(hb_context));
1254-
hb_context.response_data = malloc(RESPONSE_BUFFER_SIZE);
1260+
1261+
hb_context.response_data_size = RESPONSE_BUFFER_SIZE;
1262+
hb_context.response_data = k_calloc(hb_context.response_data_size, sizeof(uint8_t));
1263+
if (hb_context.response_data == NULL) {
1264+
LOG_ERR("Failed to allocate memory");
1265+
hb_context.code_status = HAWKBIT_METADATA_ERROR;
1266+
goto error;
1267+
}
12551268

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

13641377
if (!send_request(HTTP_GET, HAWKBIT_PROBE_DEPLOYMENT_BASE, HAWKBIT_STATUS_FINISHED_NONE,
13651378
HAWKBIT_STATUS_EXEC_NONE)) {
@@ -1453,7 +1466,7 @@ enum hawkbit_response hawkbit_probe(void)
14531466
cleanup_connection();
14541467

14551468
error:
1456-
free(hb_context.response_data);
1469+
k_free(hb_context.response_data);
14571470
k_sem_give(&probe_sem);
14581471
return hb_context.code_status;
14591472
}

0 commit comments

Comments
 (0)