- MMS client: added mutex for state in IsoClientConnection

v1.1
Michael Zillgith 8 years ago
parent 579248accb
commit d68b2109ae

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

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

Loading…
Cancel
Save