diff --git a/src/iec61850/inc_private/ied_server_private.h b/src/iec61850/inc_private/ied_server_private.h index 003c1613..f2e7fad1 100644 --- a/src/iec61850/inc_private/ied_server_private.h +++ b/src/iec61850/inc_private/ied_server_private.h @@ -49,6 +49,7 @@ struct sIedServer #if (CONFIG_MMS_THREADLESS_STACK != 1) Semaphore dataModelLock; + Semaphore clientConnectionsLock; #endif #if (CONFIG_MMS_SERVER_CONFIG_SERVICES_AT_RUNTIME == 1) diff --git a/src/iec61850/server/impl/ied_server.c b/src/iec61850/server/impl/ied_server.c index 035e3325..4a32ebec 100644 --- a/src/iec61850/server/impl/ied_server.c +++ b/src/iec61850/server/impl/ied_server.c @@ -418,6 +418,7 @@ IedServer_createWithConfig(IedModel* dataModel, TLSConfiguration tlsConfiguratio #if (CONFIG_MMS_THREADLESS_STACK != 1) self->dataModelLock = Semaphore_create(1); + self->clientConnectionsLock = Semaphore_create(1); #endif /* (CONFIG_MMS_SERVER_CONFIG_SERVICES_AT_RUNTIME == 1) */ #if (CONFIG_IEC61850_REPORT_SERVICE == 1) @@ -527,6 +528,7 @@ IedServer_destroy(IedServer self) #if (CONFIG_MMS_THREADLESS_STACK != 1) Semaphore_destroy(self->dataModelLock); + Semaphore_destroy(self->clientConnectionsLock); #endif GLOBAL_FREEMEM(self); @@ -1471,6 +1473,10 @@ IedServer_setLogStorage(IedServer self, const char* logRef, LogStorage logStorag ClientConnection private_IedServer_getClientConnectionByHandle(IedServer self, void* serverConnectionHandle) { +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_wait(self->clientConnectionsLock); +#endif + LinkedList element = LinkedList_getNext(self->clientConnections); ClientConnection matchingConnection = NULL; @@ -1485,19 +1491,39 @@ private_IedServer_getClientConnectionByHandle(IedServer self, void* serverConnec element = LinkedList_getNext(element); } +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_post(self->clientConnectionsLock); +#endif + return matchingConnection; } void private_IedServer_addNewClientConnection(IedServer self, ClientConnection newClientConnection) { +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_wait(self->clientConnectionsLock); +#endif + LinkedList_add(self->clientConnections, (void*) newClientConnection); + +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_post(self->clientConnectionsLock); +#endif } void private_IedServer_removeClientConnection(IedServer self, ClientConnection clientConnection) { +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_wait(self->clientConnectionsLock); +#endif + LinkedList_remove(self->clientConnections, clientConnection); + +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_post(self->clientConnectionsLock); +#endif } diff --git a/src/mms/inc_private/iso_server_private.h b/src/mms/inc_private/iso_server_private.h index e700c9ec..b9398e18 100644 --- a/src/mms/inc_private/iso_server_private.h +++ b/src/mms/inc_private/iso_server_private.h @@ -30,6 +30,9 @@ IsoConnection IsoConnection_create(Socket socket, IsoServer isoServer); +void +IsoConnection_start(IsoConnection self); + void IsoConnection_destroy(IsoConnection self); diff --git a/src/mms/iso_mms/server/mms_server.c b/src/mms/iso_mms/server/mms_server.c index dfdee698..b21499af 100644 --- a/src/mms/iso_mms/server/mms_server.c +++ b/src/mms/iso_mms/server/mms_server.c @@ -410,7 +410,6 @@ isoConnectionIndicationHandler(IsoConnectionIndication indication, if (indication == ISO_CONNECTION_OPENED) { MmsServerConnection mmsCon = MmsServerConnection_init(0, self, connection); - #if (CONFIG_MMS_THREADLESS_STACK != 1) Semaphore_wait(self->openConnectionsLock); #endif diff --git a/src/mms/iso_server/iso_connection.c b/src/mms/iso_server/iso_connection.c index 6454a069..d31d9f82 100644 --- a/src/mms/iso_server/iso_connection.c +++ b/src/mms/iso_server/iso_connection.c @@ -551,11 +551,6 @@ IsoConnection_create(Socket socket, IsoServer isoServer) #if (CONFIG_MMS_SINGLE_THREADED == 0) #if (CONFIG_MMS_THREADLESS_STACK == 0) self->thread = Thread_create((ThreadExecutionFunction) handleTcpConnection, self, true); - - if (self->thread) - Thread_start(self->thread); - else - goto exit_error; #endif #endif @@ -571,6 +566,16 @@ exit_error: return NULL; } +void +IsoConnection_start(IsoConnection self) +{ +#if (CONFIG_MMS_SINGLE_THREADED == 0) +#if (CONFIG_MMS_THREADLESS_STACK == 0) + Thread_start(self->thread); +#endif +#endif +} + void IsoConnection_destroy(IsoConnection self) { diff --git a/src/mms/iso_server/iso_server.c b/src/mms/iso_server/iso_server.c index ddd43d81..bc0e9de9 100644 --- a/src/mms/iso_server/iso_server.c +++ b/src/mms/iso_server/iso_server.c @@ -410,6 +410,7 @@ handleIsoConnections(IsoServer self) self->connectionHandler(ISO_CONNECTION_OPENED, self->connectionHandlerParameter, isoConnection); + IsoConnection_start(isoConnection); } }