From d68b2109aef659f6f2fd5d80413760fe88b6e3d0 Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Sun, 14 Jan 2018 13:17:20 +0100 Subject: [PATCH] - MMS client: added mutex for state in IsoClientConnection --- .../client_example1.c | 2 - src/mms/iso_client/iso_client_connection.c | 43 ++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/examples/iec61850_client_example1/client_example1.c b/examples/iec61850_client_example1/client_example1.c index 1142c768..e20a2327 100644 --- a/examples/iec61850_client_example1/client_example1.c +++ b/examples/iec61850_client_example1/client_example1.c @@ -50,8 +50,6 @@ int main(int argc, char** argv) { if (error == IED_ERROR_OK) { - IedConnection_getServerDirectory(con, &error, false); - /* read an analog measurement value from server */ MmsValue* value = IedConnection_readObject(con, &error, "simpleIOGenericIO/GGIO1.AnIn1.mag.f", IEC61850_FC_MX); diff --git a/src/mms/iso_client/iso_client_connection.c b/src/mms/iso_client/iso_client_connection.c index 6f6d48f6..01b3bb67 100644 --- a/src/mms/iso_client/iso_client_connection.c +++ b/src/mms/iso_client/iso_client_connection.c @@ -55,7 +55,10 @@ struct sIsoClientConnection { IsoIndicationCallback callback; void* callbackParameter; + volatile int state; + Semaphore stateMutex; + Socket socket; CotpConnection* cotpConnection; IsoPresentation* presentation; @@ -86,13 +89,32 @@ struct sIsoClientConnection Thread thread; }; +static void +setState(IsoClientConnection self, int newState) +{ + Semaphore_wait(self->stateMutex); + self->state = newState; + Semaphore_post(self->stateMutex); +} + +static int +getState(IsoClientConnection self) +{ + int stateVal; + + Semaphore_wait(self->stateMutex); + stateVal = self->state; + Semaphore_post(self->stateMutex); + + return stateVal; +} + static void connectionHandlingThread(IsoClientConnection self) { IsoSessionIndication sessionIndication; self->handlingThreadRunning = true; - self->stopHandlingThread = false; if (DEBUG_ISO_CLIENT) printf("ISO_CLIENT_CONNECTION: new connection %p\n", self); @@ -157,7 +179,7 @@ connectionHandlingThread(IsoClientConnection self) self->callback(ISO_IND_CLOSED, self->callbackParameter, NULL);; - self->state = STATE_IDLE; + setState(self, STATE_IDLE); Socket_destroy(self->socket); @@ -202,7 +224,9 @@ IsoClientConnection_create(IsoIndicationCallback callback, void* callbackParamet self->callback = callback; self->callbackParameter = callbackParameter; + self->state = STATE_IDLE; + self->stateMutex = Semaphore_create(1); self->sendBuffer = (uint8_t*) GLOBAL_MALLOC(ISO_CLIENT_BUFFER_SIZE); @@ -235,6 +259,12 @@ IsoClientConnection_create(IsoIndicationCallback callback, void* callbackParamet self->cotpConnection = (CotpConnection*) GLOBAL_CALLOC(1, sizeof(CotpConnection)); + self->handlingThreadRunning = false; + + self->stopHandlingThread = false; + self->destroyHandlingThread = false; + self->startHandlingThread = false; + return self; } @@ -361,7 +391,7 @@ IsoClientConnection_associate(IsoClientConnection self, IsoConnectionParameters /* wait for upper layer to release buffer */ Semaphore_wait(self->receiveBufferMutex); - self->state = STATE_ASSOCIATED; + setState(self, STATE_ASSOCIATED); if (self->thread == NULL) { self->thread = Thread_create(connectionThreadFunction, self, false); @@ -378,7 +408,7 @@ IsoClientConnection_associate(IsoClientConnection self, IsoConnectionParameters returnError: self->callback(ISO_IND_ASSOCIATION_FAILED, self->callbackParameter, NULL); - self->state = STATE_ERROR; + setState(self, STATE_ERROR); Socket_destroy(self->socket); self->socket = NULL; @@ -432,7 +462,7 @@ IsoClientConnection_close(IsoClientConnection self) Thread_sleep(1); } - self->state = STATE_IDLE; + setState(self, STATE_IDLE); } @@ -442,7 +472,7 @@ IsoClientConnection_destroy(IsoClientConnection self) if (DEBUG_ISO_CLIENT) printf("ISO_CLIENT: IsoClientConnection_destroy\n"); - if (self->state == STATE_ASSOCIATED) { + if (getState(self) == STATE_ASSOCIATED) { if (DEBUG_ISO_CLIENT) printf("ISO_CLIENT: call IsoClientConnection_close\n"); @@ -489,6 +519,7 @@ IsoClientConnection_destroy(IsoClientConnection self) Semaphore_destroy(self->receiveBufferMutex); Semaphore_destroy(self->transmitBufferMutex); + Semaphore_destroy(self->stateMutex); GLOBAL_FREEMEM(self->sendBuffer); GLOBAL_FREEMEM(self);