From c62dd778cd574ea74c5f280d76130266bd958b78 Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Tue, 26 Mar 2024 14:15:22 +0200 Subject: [PATCH] net: lwm2m: Fix socket hints for block transfer Outgoing block-transfers now set the socket hint to ONGOING as long as the BLOCK1/BLOCK2 header has MORE flag set to true. This means as only the last packet in the block-transfer set the socket hint to LAST or ONE_RESPONSE. Signed-off-by: Seppo Takalo --- include/zephyr/net/coap.h | 9 +++++++++ subsys/net/lib/coap/coap.c | 16 ++++++++++++++++ subsys/net/lib/lwm2m/lwm2m_engine.c | 4 +++- tests/net/lib/lwm2m/lwm2m_engine/src/stubs.c | 1 + tests/net/lib/lwm2m/lwm2m_engine/src/stubs.h | 2 ++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/zephyr/net/coap.h b/include/zephyr/net/coap.h index 43073a5b20b..7f69cc4e9a7 100644 --- a/include/zephyr/net/coap.h +++ b/include/zephyr/net/coap.h @@ -757,6 +757,15 @@ bool coap_has_descriptive_block_option(struct coap_packet *cpkt); */ int coap_remove_descriptive_block_option(struct coap_packet *cpkt); +/** + * @brief Check if BLOCK1 or BLOCK2 option has more flag set + * + * @param cpkt Packet to be checked. + * @return true If more flag is set in BLOCK1 or BLOCK2 + * @return false If MORE flag is not set or BLOCK header not found. + */ +bool coap_block_has_more(struct coap_packet *cpkt); + /** * @brief Append BLOCK1 option to the packet. * diff --git a/subsys/net/lib/coap/coap.c b/subsys/net/lib/coap/coap.c index ac434680740..543e6c14704 100644 --- a/subsys/net/lib/coap/coap.c +++ b/subsys/net/lib/coap/coap.c @@ -1257,6 +1257,22 @@ int coap_remove_descriptive_block_option(struct coap_packet *cpkt) } } +bool coap_block_has_more(struct coap_packet *cpkt) +{ + bool more = false; + int opt; + + if (coap_packet_is_request(cpkt)) { + opt = coap_get_option_int(cpkt, COAP_OPTION_BLOCK1); + } else { + opt = coap_get_option_int(cpkt, COAP_OPTION_BLOCK2); + } + if (opt >= 0) { + more = GET_MORE(opt); + } + return more; +} + int coap_append_block1_option(struct coap_packet *cpkt, struct coap_block_context *ctx) { diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index df9689dc094..db7906da4cf 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -659,7 +659,9 @@ static void hint_socket_state(struct lwm2m_ctx *ctx, struct lwm2m_message *ongoi empty = false; } - if (!empty) { + bool ongoing_block_tx = coap_block_has_more(&ongoing_tx->cpkt); + + if (!empty || ongoing_block_tx) { ctx->set_socket_state(ctx->sock_fd, LWM2M_SOCKET_STATE_ONGOING); } else if (ongoing_tx->type == COAP_TYPE_CON) { ctx->set_socket_state(ctx->sock_fd, LWM2M_SOCKET_STATE_ONE_RESPONSE); diff --git a/tests/net/lib/lwm2m/lwm2m_engine/src/stubs.c b/tests/net/lib/lwm2m/lwm2m_engine/src/stubs.c index 55573eda168..13b0e38e8e6 100644 --- a/tests/net/lib/lwm2m/lwm2m_engine/src/stubs.c +++ b/tests/net/lib/lwm2m/lwm2m_engine/src/stubs.c @@ -43,6 +43,7 @@ DEFINE_FAKE_VOID_FUNC(lwm2m_clear_block_contexts); DEFINE_FAKE_VALUE_FUNC(int, lwm2m_security_mode, struct lwm2m_ctx *); DEFINE_FAKE_VALUE_FUNC(int, z_impl_zsock_setsockopt, int, int, int, const void *, socklen_t); DEFINE_FAKE_VOID_FUNC(engine_update_tx_time); +DEFINE_FAKE_VALUE_FUNC(bool, coap_block_has_more, struct coap_packet *); static sys_slist_t obs_obj_path_list = SYS_SLIST_STATIC_INIT(&obs_obj_path_list); sys_slist_t *lwm2m_obs_obj_path_list(void) diff --git a/tests/net/lib/lwm2m/lwm2m_engine/src/stubs.h b/tests/net/lib/lwm2m/lwm2m_engine/src/stubs.h index 67c4cde883d..74685bbcb31 100644 --- a/tests/net/lib/lwm2m/lwm2m_engine/src/stubs.h +++ b/tests/net/lib/lwm2m/lwm2m_engine/src/stubs.h @@ -58,6 +58,7 @@ DECLARE_FAKE_VALUE_FUNC(int, z_impl_zsock_connect, int, const struct sockaddr *, DECLARE_FAKE_VALUE_FUNC(int, lwm2m_security_mode, struct lwm2m_ctx *); DECLARE_FAKE_VALUE_FUNC(int, z_impl_zsock_setsockopt, int, int, int, const void *, socklen_t); DECLARE_FAKE_VOID_FUNC(engine_update_tx_time); +DECLARE_FAKE_VALUE_FUNC(bool, coap_block_has_more, struct coap_packet *); #define DO_FOREACH_FAKE(FUNC) \ do { \ @@ -89,6 +90,7 @@ DECLARE_FAKE_VOID_FUNC(engine_update_tx_time); FUNC(lwm2m_security_mode) \ FUNC(z_impl_zsock_setsockopt) \ FUNC(engine_update_tx_time) \ + FUNC(coap_block_has_more) \ } while (0) #endif /* STUBS_H */