Skip to content

Commit 05abdf5

Browse files
SeppoTakalokartben
authored andcommitted
net: lwm2m: Deprecate LWM2M_ENGINE_MESSAGE_HEADER_SIZE
Kconfig value LWM2M_ENGINE_MESSAGE_HEADER_SIZE added an extra headroom for CoAP packet sizes so that if CoAP Block-Wise transfer block-size is configured to be same as LWM2M_COAP_MAX_MSG_SIZE, the full payload block would usually fit to the datagram. This causes too much confusion to be usable. CoAP headers and options vary on sizes, and there is no runtime limitations that we should check the header size against. Only real limitation is the CoAP packet size, which must fit into the UDP datagram with typical DTLS headers. Only limitation for CoAP block-size then is that it must fit into the CoAP message with all the headers and options. But as the option sizes, like path, vary, it must be checked runtime. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
1 parent 044a94f commit 05abdf5

File tree

7 files changed

+33
-30
lines changed

7 files changed

+33
-30
lines changed

doc/releases/release-notes-4.2.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Deprecated APIs and options
6767
renamed and deprecated. Use :kconfig:option:`CONFIG_SCHED_SIMPLE` and
6868
:kconfig:option:`CONFIG_WAITQ_SIMPLE` instead.
6969

70+
* The :kconfig:option:`CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE` Kconfig option has been removed.
71+
The required header size should be included in the message size, configured using
72+
:kconfig:option:`CONFIG_LWM2M_COAP_MAX_MSG_SIZE`. Special care should be taken to ensure that
73+
used CoAP block size :kconfig:option:`CONFIG_LWM2M_COAP_BLOCK_SIZE` can fit given message size
74+
with headers. Previous headroom was 48 bytes.
75+
7076
* TLS credential type ``TLS_CREDENTIAL_SERVER_CERTIFICATE`` was renamed and
7177
deprecated, use :c:enumerator:`TLS_CREDENTIAL_PUBLIC_CERTIFICATE` instead.
7278

subsys/net/lib/lwm2m/Kconfig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -273,25 +273,25 @@ config LWM2M_ENGINE_MAX_MESSAGES
273273

274274
config LWM2M_COAP_BLOCK_SIZE
275275
int "LWM2M CoAP block-wise transfer size"
276-
default 256
276+
default 512
277277
range 64 1024
278278
help
279279
CoAP block size used by LwM2M when performing block-wise
280280
transfers. Possible values: 64, 128, 256, 512 and 1024.
281-
282-
config LWM2M_ENGINE_MESSAGE_HEADER_SIZE
283-
int "Room for CoAP header data"
284-
default 48
285-
range 24 128
286-
help
287-
Extra room allocated to handle CoAP header data
281+
When adjusting the value, ensure that LWM2M_COAP_MAX_MSG_SIZE is large enough
282+
to fit all CoAP headers and full block size.
288283

289284
config LWM2M_COAP_MAX_MSG_SIZE
290285
int "LWM2M CoAP maximum message size"
291-
default LWM2M_COAP_BLOCK_SIZE
292-
help
293-
CoAP message size used by LWM2M. Minimum is the block size used
294-
in blockwise transfers.
286+
default 1232 if !LWM2M_DTLS_SUPPORT
287+
default 1195 if LWM2M_DTLS_SUPPORT && !LWM2M_DTLS_CID
288+
default 1187 if LWM2M_DTLS_SUPPORT && LWM2M_DTLS_CID
289+
help
290+
CoAP message size used by LWM2M. Default is the maximum
291+
packet size for IPv6 network (MTU(1280)-IPv6(40)-UDP(8)-DTLS(29..53)).
292+
If the network has a smaller MTU, this value should be adjusted accordingly.
293+
If CoAP block-wise transfer is enabled, this value should be larger than
294+
the block size and estimated CoAP header sizes.
295295

296296
config LWM2M_ENGINE_VALIDATION_BUFFER_SIZE
297297
int "Size of the validation buffer for the incoming data"

subsys/net/lib/lwm2m/lwm2m_message_handling.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ STATIC int build_msg_block_for_send(struct lwm2m_message *msg, uint16_t block_nu
383383
ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt),
384384
complete_payload + (block_num * block_size_bytes), payload_size);
385385
if (ret < 0) {
386+
LOG_ERR("CoAP message size overflow");
386387
return ret;
387388
}
388389

@@ -392,19 +393,16 @@ STATIC int build_msg_block_for_send(struct lwm2m_message *msg, uint16_t block_nu
392393
STATIC int prepare_msg_for_send(struct lwm2m_message *msg)
393394
{
394395
int ret;
395-
uint16_t len;
396-
const uint8_t *payload;
397-
398396
/* save the big buffer for later use (splitting blocks) */
399397
msg->body_encode_buffer = msg->cpkt;
400398

401399
/* set the default (small) buffer for sending blocks */
402400
msg->cpkt.data = msg->msg_data;
403401
msg->cpkt.offset = 0;
404-
msg->cpkt.max_len = MAX_PACKET_SIZE;
402+
msg->cpkt.max_len = sizeof(msg->msg_data);
405403

406-
payload = coap_packet_get_payload(&msg->body_encode_buffer, &len);
407-
if (len <= CONFIG_LWM2M_COAP_MAX_MSG_SIZE) {
404+
/* Can we fit a whole message into one frame */
405+
if (msg->body_encode_buffer.offset <= msg->cpkt.max_len) {
408406

409407
/* copy the packet */
410408
ret = buf_append(CPKT_BUF_WRITE(&msg->cpkt), msg->body_encode_buffer.data,
@@ -422,6 +420,9 @@ STATIC int prepare_msg_for_send(struct lwm2m_message *msg)
422420

423421
NET_ASSERT(msg->out.block_ctx == NULL, "Expecting to have no context to release");
424422
} else {
423+
uint16_t len;
424+
const uint8_t *payload = coap_packet_get_payload(&msg->body_encode_buffer, &len);
425+
425426
/* Before splitting the content, append Etag option to protect the integrity of
426427
* the payload.
427428
*/

subsys/net/lib/lwm2m/lwm2m_object.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,12 @@
131131

132132
BUILD_ASSERT(CONFIG_LWM2M_COAP_BLOCK_SIZE <= CONFIG_LWM2M_COAP_MAX_MSG_SIZE,
133133
"CoAP block size can't exceed maximum message size");
134-
135-
#define MAX_PACKET_SIZE (CONFIG_LWM2M_COAP_MAX_MSG_SIZE + \
136-
CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE)
137-
138-
#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER)
139-
BUILD_ASSERT(CONFIG_LWM2M_COAP_ENCODE_BUFFER_SIZE >
140-
(CONFIG_LWM2M_COAP_BLOCK_SIZE + CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE),
141-
"The buffer for serializing message needs to be bigger than a message with one block");
142-
#endif
134+
BUILD_ASSERT(CONFIG_LWM2M_COAP_BLOCK_SIZE == 64 ||
135+
CONFIG_LWM2M_COAP_BLOCK_SIZE == 128 ||
136+
CONFIG_LWM2M_COAP_BLOCK_SIZE == 256 ||
137+
CONFIG_LWM2M_COAP_BLOCK_SIZE == 512 ||
138+
CONFIG_LWM2M_COAP_BLOCK_SIZE == 1024,
139+
"CoAP block must be 64, 128, 256, 512 or 1024");
143140

144141
/* buffer util macros */
145142
#define CPKT_BUF_WRITE(cpkt) (cpkt)->data, &(cpkt)->offset, (cpkt)->max_len
@@ -494,7 +491,7 @@ struct lwm2m_message {
494491
struct coap_packet cpkt;
495492

496493
/** Buffer data related outgoing message */
497-
uint8_t msg_data[MAX_PACKET_SIZE];
494+
uint8_t msg_data[CONFIG_LWM2M_COAP_MAX_MSG_SIZE];
498495

499496
#if defined(CONFIG_LWM2M_COAP_BLOCK_TRANSFER)
500497
/** Buffer data containing complete message */

tests/net/lib/lwm2m/block_transfer/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ CONFIG_LWM2M=y
1414
CONFIG_LWM2M_COAP_BLOCK_TRANSFER=y
1515
CONFIG_LWM2M_COAP_BLOCK_SIZE=64
1616
CONFIG_LWM2M_COAP_ENCODE_BUFFER_SIZE=256
17+
CONFIG_LWM2M_COAP_MAX_MSG_SIZE=96

tests/net/lib/lwm2m/lwm2m_engine/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ target_include_directories(app PRIVATE ${ZEPHYR_BASE}/../modules/crypto/mbedtls/
2121
add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_PENDING=2)
2222
add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_REPLIES=2)
2323
add_compile_definitions(CONFIG_LWM2M_ENGINE_VALIDATION_BUFFER_SIZE=512)
24-
add_compile_definitions(CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE=512)
2524
add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_OBSERVER=10)
2625
add_compile_definitions(CONFIG_LWM2M_ENGINE_STACK_SIZE=2048)
2726
add_compile_definitions(CONFIG_LWM2M_NUM_BLOCK1_CONTEXT=3)

tests/net/lib/lwm2m/lwm2m_rd_client/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/net/lib/lwm2m/)
1818
add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_PENDING=2)
1919
add_compile_definitions(CONFIG_LWM2M_ENGINE_MAX_REPLIES=2)
2020
add_compile_definitions(CONFIG_LWM2M_ENGINE_VALIDATION_BUFFER_SIZE=512)
21-
add_compile_definitions(CONFIG_LWM2M_ENGINE_MESSAGE_HEADER_SIZE=512)
2221
add_compile_definitions(CONFIG_LWM2M_RD_CLIENT_ENDPOINT_NAME_MAX_LENGTH=32)
2322
add_compile_definitions(CONFIG_LWM2M_RD_CLIENT_MAX_RETRIES=2)
2423
add_compile_definitions(CONFIG_LWM2M_COAP_BLOCK_SIZE=256)

0 commit comments

Comments
 (0)