From 29435eeb6f923ede5d6553c19adc1fee50d30a47 Mon Sep 17 00:00:00 2001 From: Lukas Woodtli Date: Thu, 23 Jan 2025 11:45:41 +0100 Subject: [PATCH] examples: Update connection handling Add function for removing connection. --- transport/tinydtls/connection.c | 24 +++++++++++++++++++++ transport/udp/connection.c | 37 +++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/transport/tinydtls/connection.c b/transport/tinydtls/connection.c index cc8d82c4b..9b3e19475 100644 --- a/transport/tinydtls/connection.c +++ b/transport/tinydtls/connection.c @@ -564,3 +564,27 @@ uint8_t lwm2m_buffer_send(void *sessionH, uint8_t *buffer, size_t length, void * } bool lwm2m_session_is_equal(void *session1, void *session2, void *userData) { return (session1 == session2); } + +void lwm2m_session_remove(void *sessionH) { + lwm2m_dtls_connection_t *connP = (lwm2m_dtls_connection_t *)sessionH; + dtls_app_context_t *appContext = (dtls_app_context_t *)connP->dtlsContext; + lwm2m_dtls_connection_t *connIter; + + connIter = appContext->connList; + + if (connIter == NULL) { + return; + } + if (connIter == sessionH) { + /* First element */ + free(sessionH); + appContext->connList = NULL; + return; + } + + while (connIter->next != connP) + ; + connIter->next = connP->next; + + free(connP); +} diff --git a/transport/udp/connection.c b/transport/udp/connection.c index a523f82c3..574521a30 100644 --- a/transport/udp/connection.c +++ b/transport/udp/connection.c @@ -26,6 +26,8 @@ #include #include +static lwm2m_connection_t *connectionList = NULL; + static int find_and_bind_to_address(struct addrinfo *res) { int s = -1; for (struct addrinfo *p = res; p != NULL && s == -1; p = p->ai_next) { @@ -80,7 +82,7 @@ int lwm2m_create_socket(const char *portStr, int addressFamily) { lwm2m_connection_t *lwm2m_connection_find(lwm2m_connection_t *connList, struct sockaddr_storage *addr, size_t addrLen) { lwm2m_connection_t *connP; - connP = connList; + connP = connectionList; while (connP != NULL) { if ((connP->addrLen == addrLen) && (memcmp(&(connP->addr), addr, addrLen) == 0)) { // NOSONAR return connP; @@ -100,7 +102,9 @@ lwm2m_connection_t *lwm2m_connection_new_incoming(lwm2m_connection_t *connList, connP->sock = sock; memcpy(&(connP->addr), addr, addrLen); connP->addrLen = addrLen; - connP->next = connList; + connP->next = connectionList; + + connectionList = connP; } return connP; @@ -139,10 +143,10 @@ void lwm2m_connection_free(lwm2m_connection_t *connList) { while (connList != NULL) { lwm2m_connection_t *nextP; - nextP = connList->next; - lwm2m_free(connList); + nextP = connectionList->next; + lwm2m_free(connectionList); - connList = nextP; + connectionList = nextP; } } @@ -214,4 +218,25 @@ bool lwm2m_session_is_equal(void *session1, void *session2, void *userData) { return (session1 == session2); } -void lwm2m_session_remove(void *session_h) { (void)session_h; /* unused */ } +void lwm2m_session_remove(void *sessionH) { + lwm2m_connection_t *connP = (lwm2m_connection_t *)sessionH; + lwm2m_connection_t *connIter; + + connIter = connectionList; + + if (connIter == NULL) { + return; + } + if (connIter == sessionH) { + /* First element */ + free(sessionH); + connectionList = NULL; + return; + } + + while (connIter->next != connP) + ; + connIter->next = connP->next; + + free(connP); +}