Skip to content

Commit

Permalink
file_transfer: move heap to the common pool
Browse files Browse the repository at this point in the history
use sidewalk heap to store data received by FUOTA

Signed-off-by: Robert Gałat <robert.galat@nordicsemi.no>
  • Loading branch information
RobertGalatNordic committed Mar 7, 2024
1 parent cbb4b63 commit add8b64
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ config SIDEWALK_HEAP_SIZE
help
Set the heap size for dynamic memory alocation in Sidewalk.

config SIDEWALK_TRACE_HEAP
bool "Trace allocation and free of Sidewalk heap"
help
Add debug log to every alloc and free operation on Sidewalk heap

config SID_HAL_PROTOCOL_MEMORY_SZ
int
default 1024
Expand Down
2 changes: 1 addition & 1 deletion samples/sid_end_device/Kconfig.defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ config NVS_LOOKUP_CACHE
default y

config NVS_LOOKUP_CACHE_SIZE
default 1024
default 256

# Stack and Heap
config SYSTEM_WORKQUEUE_STACK_SIZE
Expand Down
7 changes: 2 additions & 5 deletions samples/sid_end_device/src/file_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ LOG_MODULE_REGISTER(file_transfer, CONFIG_SIDEWALK_LOG_LEVEL);

#define PARALEL_TRANSFER_MAX 3

K_HEAP_DEFINE(File_transfer_heap, KB(10));

struct buffer_repo_element {
uint32_t file_id;
void *memory_slab_for_transfer;
Expand Down Expand Up @@ -52,8 +50,7 @@ static void on_transfer_request(const struct sid_bulk_data_transfer_request *con
transfer_response->reject_reason = SID_BULK_DATA_TRANSFER_REJECT_REASON_GENERIC;
return;
}
void *ptr = k_heap_alloc(&File_transfer_heap, transfer_request->minimum_scratch_buffer_size,
K_NO_WAIT);
void *ptr = sid_hal_malloc(transfer_request->minimum_scratch_buffer_size);
if (ptr == NULL) {
LOG_ERR("Failed to alloc memory");
transfer_response->status = SID_BULK_DATA_TRANSFER_ACTION_REJECT;
Expand Down Expand Up @@ -129,7 +126,7 @@ static void on_release_scratch_buffer(uint32_t file_id, void *context)

for (size_t i = 0; i < PARALEL_TRANSFER_MAX; i++) {
if (buffer_repo[i].file_id == file_id) {
k_heap_free(&File_transfer_heap, buffer_repo[i].memory_slab_for_transfer);
sid_hal_free(buffer_repo[i].memory_slab_for_transfer);

buffer_repo[i].memory_slab_for_transfer = NULL;
buffer_repo[i].file_id = UINT32_MAX;
Expand Down
18 changes: 16 additions & 2 deletions subsys/hal/src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ LOG_MODULE_REGISTER(hal_memory, CONFIG_SIDEWALK_LOG_LEVEL);
#define CONFIG_SID_END_DEVICE_EVENT_HEAP_SIZE 0
#endif

#define HEAP_SIZE CONFIG_SID_HAL_PROTOCOL_MEMORY_SZ + CONFIG_SIDEWALK_HEAP_SIZE + CONFIG_SID_END_DEVICE_EVENT_HEAP_SIZE
#ifdef CONFIG_SIDEWALK_FILE_TRANSFER
#define SIDEWALK_FILE_TRANSFER_HEAP KB(10)
#else
#define SIDEWALK_FILE_TRANSFER_HEAP 0
#endif

#define HEAP_SIZE CONFIG_SID_HAL_PROTOCOL_MEMORY_SZ + CONFIG_SIDEWALK_HEAP_SIZE + CONFIG_SID_END_DEVICE_EVENT_HEAP_SIZE + SIDEWALK_FILE_TRANSFER_HEAP

K_HEAP_DEFINE(sid_heap, HEAP_SIZE);

Expand Down Expand Up @@ -62,13 +68,21 @@ void *sid_hal_malloc(size_t size)
heap_alloc_stats(&sid_heap.heap, size);
#endif

return k_heap_alloc(&sid_heap, size, K_NO_WAIT);
void * ptr = k_heap_alloc(&sid_heap, size, K_NO_WAIT);
#if CONFIG_SIDEWALK_TRACE_HEAP
LOG_DBG("Alloc %d bytes at addr %p", size, ptr);
#endif /* CONFIG_SIDEWALK_TRACE_HEAP */
return ptr;
}

void sid_hal_free(void *ptr)
{
if (!ptr) {
LOG_ERR("Can not free NULL ptr");
return;
}
#if CONFIG_SIDEWALK_TRACE_HEAP
LOG_DBG("Free ptr at addr %p", ptr);
#endif /* CONFIG_SIDEWALK_TRACE_HEAP */
k_heap_free(&sid_heap, ptr);
}

0 comments on commit add8b64

Please sign in to comment.