Skip to content

Commit

Permalink
- CS 104 connection: call event handler at end of thread when connect…
Browse files Browse the repository at this point in the history
…ion is closed or failed to open
  • Loading branch information
mzillgith committed Dec 29, 2022
1 parent ee832dd commit a2fbe7a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
21 changes: 13 additions & 8 deletions lib60870-C/src/iec60870/cs104/cs104_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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) */
Expand Down
5 changes: 2 additions & 3 deletions lib60870-C/src/iec60870/cs104/cs104_frame.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 MZ Automation GmbH
* Copyright 2016-2022 Michael Zillgith
*
* This file is part of lib60870-C
*
Expand Down Expand Up @@ -136,7 +136,7 @@ T104Frame_destroy(Frame super)
#if (CONFIG_LIB60870_STATIC_FRAMES == 1)
self->allocated = 0;
#else
free(self);
GLOBAL_FREEMEM(self);
#endif
}

Expand Down Expand Up @@ -208,4 +208,3 @@ T104Frame_getSpaceLeft(Frame super)

return (IEC60870_5_104_MAX_ASDU_LENGTH + IEC60870_5_104_APCI_LENGTH - self->msgSize);
}

3 changes: 3 additions & 0 deletions lib60870-C/src/inc/api/cs104_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a2fbe7a

Please sign in to comment.