From 4ae7a268dede98f6b77a1b5c7d0cf742bf3df226 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Tue, 26 Mar 2019 09:24:50 +0100 Subject: [PATCH] - MMS server: added semaphore for open connections map to prevent problem in multi-threaded mode --- src/iec61850/server/mms_mapping/mms_mapping.c | 2 +- src/mms/inc_private/mms_server_internal.h | 4 ++ src/mms/iso_mms/server/mms_server.c | 38 +++++++++++++++---- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/iec61850/server/mms_mapping/mms_mapping.c b/src/iec61850/server/mms_mapping/mms_mapping.c index 87919a7a..20723f22 100644 --- a/src/iec61850/server/mms_mapping/mms_mapping.c +++ b/src/iec61850/server/mms_mapping/mms_mapping.c @@ -1,7 +1,7 @@ /* * mms_mapping.c * - * Copyright 2013-2018 Michael Zillgith + * Copyright 2013-2019 Michael Zillgith * * This file is part of libIEC61850. * diff --git a/src/mms/inc_private/mms_server_internal.h b/src/mms/inc_private/mms_server_internal.h index eef28d1f..74c093cc 100644 --- a/src/mms/inc_private/mms_server_internal.h +++ b/src/mms/inc_private/mms_server_internal.h @@ -119,6 +119,10 @@ struct sMmsServer { MmsNamedVariableListChangedHandler variableListChangedHandler; //TODO this is only required if dynamic data sets are supported! void* variableListChangedHandlerParameter; +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore openConnectionsLock; +#endif + Map openConnections; Map valueCaches; bool isLocked; diff --git a/src/mms/iso_mms/server/mms_server.c b/src/mms/iso_mms/server/mms_server.c index 7d504887..dfdee698 100644 --- a/src/mms/iso_mms/server/mms_server.c +++ b/src/mms/iso_mms/server/mms_server.c @@ -61,6 +61,8 @@ MmsServer_create(MmsDevice* device, TLSConfiguration tlsConfiguration) self->transmitBuffer = ByteBuffer_create(NULL, CONFIG_MMS_MAXIMUM_PDU_SIZE); #if (CONFIG_MMS_THREADLESS_STACK != 1) + self->openConnectionsLock = Semaphore_create(1); + self->modelMutex = Semaphore_create(1); self->transmitBufferMutex = Semaphore_create(1); @@ -299,6 +301,8 @@ MmsServer_destroy(MmsServer self) Map_deleteDeep(self->valueCaches, false, (void (*) (void*)) deleteSingleCache); #if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_destroy(self->openConnectionsLock); + Semaphore_destroy(self->modelMutex); Semaphore_destroy(self->transmitBufferMutex); #endif @@ -401,23 +405,41 @@ static void /* will be called by ISO server stack */ isoConnectionIndicationHandler(IsoConnectionIndication indication, void* parameter, IsoConnection connection) { - MmsServer mmsServer = (MmsServer) parameter; + MmsServer self = (MmsServer) parameter; if (indication == ISO_CONNECTION_OPENED) { - MmsServerConnection mmsCon = MmsServerConnection_init(0, mmsServer, connection); + MmsServerConnection mmsCon = MmsServerConnection_init(0, self, connection); + + +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_wait(self->openConnectionsLock); +#endif - Map_addEntry(mmsServer->openConnections, connection, mmsCon); + Map_addEntry(self->openConnections, connection, mmsCon); + +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_post(self->openConnectionsLock); +#endif - if (mmsServer->connectionHandler != NULL) - mmsServer->connectionHandler(mmsServer->connectionHandlerParameter, + if (self->connectionHandler != NULL) + self->connectionHandler(self->connectionHandlerParameter, mmsCon, MMS_SERVER_NEW_CONNECTION); } else if (indication == ISO_CONNECTION_CLOSED) { + +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_wait(self->openConnectionsLock); +#endif + MmsServerConnection mmsCon = (MmsServerConnection) - Map_removeEntry(mmsServer->openConnections, connection, false); + Map_removeEntry(self->openConnections, connection, false); + +#if (CONFIG_MMS_THREADLESS_STACK != 1) + Semaphore_post(self->openConnectionsLock); +#endif - if (mmsServer->connectionHandler != NULL) - mmsServer->connectionHandler(mmsServer->connectionHandlerParameter, + if (self->connectionHandler != NULL) + self->connectionHandler(self->connectionHandlerParameter, mmsCon, MMS_SERVER_CONNECTION_CLOSED); if (mmsCon != NULL)