- MMS client: added mutex for state in IsoClientConnection

pull/143/head
Michael Zillgith 8 years ago
parent f9030a8b4f
commit 5a0315b52b

@ -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);

@ -57,7 +57,9 @@ struct sIsoClientConnection
{
IsoIndicationCallback callback;
void* callbackParameter;
volatile int state;
Semaphore stateMutex;
Socket socket;
@ -94,13 +96,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);
@ -165,7 +186,7 @@ connectionHandlingThread(IsoClientConnection self)
self->callback(ISO_IND_CLOSED, self->callbackParameter, NULL);;
self->state = STATE_IDLE;
setState(self, STATE_IDLE);
#if (CONFIG_MMS_SUPPORT_TLS == 1)
if (self->cotpConnection->tlsSocket)
@ -215,7 +236,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);
@ -248,6 +271,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;
}
@ -395,7 +424,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);
@ -412,7 +441,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;
@ -466,7 +495,7 @@ IsoClientConnection_close(IsoClientConnection self)
Thread_sleep(1);
}
self->state = STATE_IDLE;
setState(self, STATE_IDLE);
}
@ -476,7 +505,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");
@ -523,6 +552,7 @@ IsoClientConnection_destroy(IsoClientConnection self)
Semaphore_destroy(self->receiveBufferMutex);
Semaphore_destroy(self->transmitBufferMutex);
Semaphore_destroy(self->stateMutex);
GLOBAL_FREEMEM(self->sendBuffer);
GLOBAL_FREEMEM(self);

Loading…
Cancel
Save