@@ -630,6 +630,50 @@ static int64_t check_notifications(struct lwm2m_ctx *ctx, const int64_t timestam
630630 return next ;
631631}
632632
633+ /**
634+ * @brief Check TX queue states as well as number or pending CoAP transmissions.
635+ *
636+ * If all queues are empty and there is no packet we are currently transmitting and no
637+ * CoAP responses (pendings) we are waiting, inform the application by a callback
638+ * that socket is in state LWM2M_SOCKET_STATE_NO_DATA.
639+ * Otherwise, before sending a packet, depending on the state of the queues, inform with
640+ * one of the ONGOING, ONE_RESPONSE or LAST indicators.
641+ *
642+ * @param ctx Client context.
643+ * @param ongoing_tx Current packet to be transmitted or NULL.
644+ */
645+ static void hint_socket_state (struct lwm2m_ctx * ctx , struct lwm2m_message * ongoing_tx )
646+ {
647+ if (!ctx -> set_socket_state ) {
648+ return ;
649+ }
650+
651+ #if defined(CONFIG_LWM2M_QUEUE_MODE_ENABLED )
652+ bool empty = sys_slist_is_empty (& ctx -> pending_sends ) &&
653+ sys_slist_is_empty (& ctx -> queued_messages );
654+ #else
655+ bool empty = sys_slist_is_empty (& ctx -> pending_sends );
656+ #endif
657+ size_t pendings = coap_pendings_count (ctx -> pendings , ARRAY_SIZE (ctx -> pendings ));
658+
659+ if (ongoing_tx ) {
660+ /* Check if more than current TX is in pendings list*/
661+ if (pendings > 1 ) {
662+ empty = false;
663+ }
664+
665+ if (!empty ) {
666+ ctx -> set_socket_state (ctx -> sock_fd , LWM2M_SOCKET_STATE_ONGOING );
667+ } else if (ongoing_tx -> type == COAP_TYPE_CON ) {
668+ ctx -> set_socket_state (ctx -> sock_fd , LWM2M_SOCKET_STATE_ONE_RESPONSE );
669+ } else {
670+ ctx -> set_socket_state (ctx -> sock_fd , LWM2M_SOCKET_STATE_LAST );
671+ }
672+ } else if (empty && pendings == 0 ) {
673+ ctx -> set_socket_state (ctx -> sock_fd , LWM2M_SOCKET_STATE_NO_DATA );
674+ }
675+ }
676+
633677static int socket_recv_message (struct lwm2m_ctx * client_ctx )
634678{
635679 static uint8_t in_buf [NET_IPV6_MTU ];
@@ -684,31 +728,7 @@ static int socket_send_message(struct lwm2m_ctx *ctx)
684728 coap_pending_cycle (msg -> pending );
685729 }
686730
687- if (ctx -> set_socket_state ) {
688- #if defined(CONFIG_LWM2M_QUEUE_MODE_ENABLED )
689- bool empty = sys_slist_is_empty (& ctx -> pending_sends ) &&
690- sys_slist_is_empty (& ctx -> queued_messages );
691- #else
692- bool empty = sys_slist_is_empty (& ctx -> pending_sends );
693- #endif
694- if (coap_pendings_count (ctx -> pendings , ARRAY_SIZE (ctx -> pendings )) > 1 ) {
695- empty = false;
696- }
697-
698- if (!empty ) {
699- ctx -> set_socket_state (ctx -> sock_fd , LWM2M_SOCKET_STATE_ONGOING );
700- } else {
701- switch (msg -> type ) {
702- case COAP_TYPE_CON :
703- ctx -> set_socket_state (ctx -> sock_fd ,
704- LWM2M_SOCKET_STATE_ONE_RESPONSE );
705- break ;
706- default :
707- ctx -> set_socket_state (ctx -> sock_fd , LWM2M_SOCKET_STATE_LAST );
708- break ;
709- }
710- }
711- }
731+ hint_socket_state (ctx , msg );
712732
713733 rc = zsock_send (msg -> ctx -> sock_fd , msg -> cpkt .data , msg -> cpkt .offset , 0 );
714734
@@ -847,6 +867,8 @@ static void socket_loop(void *p1, void *p2, void *p3)
847867 break ;
848868 }
849869 }
870+
871+ hint_socket_state (sock_ctx [i ], NULL );
850872 }
851873
852874 if (sock_fds [i ].revents & ZSOCK_POLLOUT ) {
0 commit comments