Skip to content

Commit 4f3ef0a

Browse files
adigierlubos
authored andcommitted
[nrf fromtree] net: openthread: Add platform message management
* Add CONFIG_OPENTHREAD_PLATFORM_MESSAGE_MANAGEMENT to allow enabling message management by the platform. * Add implementation of `otPlatMessagePoolInit`, `otPlatMessagePoolNew` and `otPlatMessagePoolFree`. Signed-off-by: Adrian Gielniewski <[email protected]> (cherry picked from commit 9fd9e23)
1 parent 90eeeeb commit 4f3ef0a

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

modules/openthread/platform/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_COPROCESSOR uart.c)
1616
zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_CRYPTO_PSA crypto_psa.c)
1717
zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_SHELL shell.c)
1818
zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_EXTERNAL_HEAP memory.c)
19+
zephyr_library_sources_ifdef(CONFIG_OPENTHREAD_PLATFORM_MESSAGE_MANAGEMENT messagepool.c)
1920
zephyr_library_sources_ifdef(CONFIG_SETTINGS settings.c)
2021
zephyr_library_sources_ifndef(CONFIG_LOG_BACKEND_SPINEL logging.c)
2122

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/logging/log.h>
9+
10+
#include <openthread/platform/messagepool.h>
11+
12+
#define LOG_MODULE_NAME net_otPlat_messagepool
13+
#define LOG_LEVEL CONFIG_OPENTHREAD_LOG_LEVEL
14+
15+
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
16+
17+
#define BUF_TIMEOUT K_MSEC(50)
18+
19+
#define MESSAGE_POOL_SIZE \
20+
(CONFIG_OPENTHREAD_NUM_MESSAGE_BUFFERS * CONFIG_OPENTHREAD_MESSAGE_BUFFER_SIZE)
21+
#define MAX_ALIGNMENT __alignof__(z_max_align_t)
22+
23+
BUILD_ASSERT(CONFIG_OPENTHREAD_MESSAGE_BUFFER_SIZE % MAX_ALIGNMENT == 0,
24+
"Invalid message buffer size");
25+
26+
static struct k_mem_slab message_pool;
27+
__aligned(MAX_ALIGNMENT) static uint8_t message_pool_buffer[MESSAGE_POOL_SIZE];
28+
29+
void otPlatMessagePoolInit(otInstance *aInstance, uint16_t aMinNumFreeBuffers, size_t aBufferSize)
30+
{
31+
ARG_UNUSED(aInstance);
32+
33+
__ASSERT(aBufferSize == CONFIG_OPENTHREAD_MESSAGE_BUFFER_SIZE,
34+
"Message buffer size does not match configuration");
35+
36+
if (aMinNumFreeBuffers > CONFIG_OPENTHREAD_NUM_MESSAGE_BUFFERS) {
37+
LOG_WRN("Minimum number of free buffers (%d) is greater than number of allocated "
38+
"buffers (%d)",
39+
aMinNumFreeBuffers, CONFIG_OPENTHREAD_NUM_MESSAGE_BUFFERS);
40+
}
41+
42+
if (k_mem_slab_init(&message_pool, message_pool_buffer, aBufferSize,
43+
CONFIG_OPENTHREAD_NUM_MESSAGE_BUFFERS) != 0) {
44+
__ASSERT(false, "Failed to initialize message pool");
45+
}
46+
}
47+
48+
otMessageBuffer *otPlatMessagePoolNew(otInstance *aInstance)
49+
{
50+
ARG_UNUSED(aInstance);
51+
52+
otMessageBuffer *buffer;
53+
54+
if (k_mem_slab_alloc(&message_pool, (void **)&buffer, BUF_TIMEOUT) != 0) {
55+
LOG_ERR("Failed to allocate message buffer");
56+
return NULL;
57+
}
58+
59+
buffer->mNext = NULL;
60+
return buffer;
61+
}
62+
63+
void otPlatMessagePoolFree(otInstance *aInstance, otMessageBuffer *aBuffer)
64+
{
65+
ARG_UNUSED(aInstance);
66+
67+
k_mem_slab_free(&message_pool, (void *)aBuffer);
68+
}

modules/openthread/platform/openthread-core-zephyr-config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,16 @@
416416
#define OPENTHREAD_CONFIG_MESSAGE_BUFFER_SIZE CONFIG_OPENTHREAD_MESSAGE_BUFFER_SIZE
417417
#endif
418418

419+
/**
420+
* @def OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT
421+
*
422+
* The message pool is managed by platform defined logic.
423+
*
424+
*/
425+
#ifdef CONFIG_OPENTHREAD_PLATFORM_MESSAGE_MANAGEMENT
426+
#define OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT CONFIG_OPENTHREAD_PLATFORM_MESSAGE_MANAGEMENT
427+
#endif
428+
419429
/**
420430
* @def OPENTHREAD_CONFIG_MAC_STAY_AWAKE_BETWEEN_FRAGMENTS
421431
*

subsys/net/l2/openthread/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,11 @@ config OPENTHREAD_MESSAGE_BUFFER_SIZE
273273
help
274274
"The size of a message buffer in bytes"
275275

276+
config OPENTHREAD_PLATFORM_MESSAGE_MANAGEMENT
277+
bool "Use platform message management"
278+
help
279+
The message pool is managed by platform defined logic.
280+
276281
config OPENTHREAD_MAX_STATECHANGE_HANDLERS
277282
int "The maximum number of state-changed callback handlers"
278283
default 2

0 commit comments

Comments
 (0)