From a2fbe7a897d31828ee0fd60dbb2e0b03d1005f8d Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Thu, 29 Dec 2022 16:58:58 +0000 Subject: [PATCH] - CS 104 connection: call event handler at end of thread when connection is closed or failed to open --- .../src/iec60870/cs104/cs104_connection.c | 21 ++++++++++++------- lib60870-C/src/iec60870/cs104/cs104_frame.c | 5 ++--- lib60870-C/src/inc/api/cs104_connection.h | 3 +++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib60870-C/src/iec60870/cs104/cs104_connection.c b/lib60870-C/src/iec60870/cs104/cs104_connection.c index f30bee02..32617822 100644 --- a/lib60870-C/src/iec60870/cs104/cs104_connection.c +++ b/lib60870-C/src/iec60870/cs104/cs104_connection.c @@ -924,6 +924,8 @@ handleConnection(void* parameter) { CS104_Connection self = (CS104_Connection) parameter; + CS104_ConnectionEvent event = CS104_CONNECTION_OPENED; + resetConnection(self); self->socket = TcpSocket_create(); @@ -973,7 +975,7 @@ handleConnection(void* parameter) #endif /* (CONFIG_USE_SEMAPHORES == 1) */ /* Call connection handler */ - if (self->connectionHandler != NULL) + if (self->connectionHandler) self->connectionHandler(self->connectionHandlerParameter, self, CS104_CONNECTION_OPENED); HandleSet handleSet = Handleset_new(); @@ -1037,10 +1039,8 @@ handleConnection(void* parameter) Handleset_destroy(handleSet); - /* Call connection handler */ - if (self->connectionHandler != NULL) - self->connectionHandler(self->connectionHandlerParameter, self, CS104_CONNECTION_CLOSED); - + /* register CLOSED event */ + event = CS104_CONNECTION_CLOSED; } } else { @@ -1053,9 +1053,8 @@ handleConnection(void* parameter) Semaphore_post(self->conStateLock); #endif /* (CONFIG_USE_SEMAPHORES == 1) */ - /* Call connection handler */ - if (self->connectionHandler != NULL) - self->connectionHandler(self->connectionHandlerParameter, self, CS104_CONNECTION_FAILED); + /* register CLOSED event */ + event = CS104_CONNECTION_FAILED; } #if (CONFIG_USE_SEMAPHORES == 1) @@ -1094,6 +1093,12 @@ handleConnection(void* parameter) Semaphore_post(self->conStateLock); #endif /* (CONFIG_USE_SEMAPHORES == 1) */ + /* Call connection handler */ + if ((event == CS104_CONNECTION_CLOSED) || (event == CS104_CONNECTION_FAILED)) { + if (self->connectionHandler) + self->connectionHandler(self->connectionHandlerParameter, self, event); + } + return NULL; } #endif /* (CONFIG_USE_THREADS == 1) */ diff --git a/lib60870-C/src/iec60870/cs104/cs104_frame.c b/lib60870-C/src/iec60870/cs104/cs104_frame.c index 3b998aec..b75bc731 100644 --- a/lib60870-C/src/iec60870/cs104/cs104_frame.c +++ b/lib60870-C/src/iec60870/cs104/cs104_frame.c @@ -1,5 +1,5 @@ /* - * Copyright 2016 MZ Automation GmbH + * Copyright 2016-2022 Michael Zillgith * * This file is part of lib60870-C * @@ -136,7 +136,7 @@ T104Frame_destroy(Frame super) #if (CONFIG_LIB60870_STATIC_FRAMES == 1) self->allocated = 0; #else - free(self); + GLOBAL_FREEMEM(self); #endif } @@ -208,4 +208,3 @@ T104Frame_getSpaceLeft(Frame super) return (IEC60870_5_104_MAX_ASDU_LENGTH + IEC60870_5_104_APCI_LENGTH - self->msgSize); } - diff --git a/lib60870-C/src/inc/api/cs104_connection.h b/lib60870-C/src/inc/api/cs104_connection.h index 221841fc..6487b10d 100644 --- a/lib60870-C/src/inc/api/cs104_connection.h +++ b/lib60870-C/src/inc/api/cs104_connection.h @@ -319,6 +319,9 @@ typedef enum { /** * \brief Handler that is called when the connection is established or closed + * + * \note Calling \ref CS104_Connection_destroy or \ref CS104_Connection_close inside + * of the callback causes a memory leak! * * \param parameter user provided parameter * \param connection the connection object