- some code cleanup

pull/521/head
Michael Zillgith 1 year ago
parent 6c91e34b5e
commit 3b1758c713

@ -1,7 +1,7 @@
/*
* ber_encoder.c
*
* Copyright 2013-2022 Michael Zillgith
* Copyright 2013-2024 Michael Zillgith
*
* This file is part of libIEC61850.
*

@ -1,7 +1,7 @@
/*
* ber_encoder.h
*
* Copyright 2013-2018 Michael Zillgith
* Copyright 2013-2024 Michael Zillgith
*
* This file is part of libIEC61850.
*
@ -75,6 +75,9 @@ BerEncoder_encodeFloat(uint8_t* floatValue, uint8_t formatWidth, uint8_t exponen
LIB61850_INTERNAL int
BerEncoder_UInt32determineEncodedSize(uint32_t value);
LIB61850_INTERNAL int
BerEncoder_Int32determineEncodedSize(int32_t value);
LIB61850_INTERNAL int
BerEncoder_determineLengthSize(uint32_t length);

@ -1,7 +1,7 @@
/*
* mms_server.c
*
* Copyright 2013-2023 Michael Zillgith
* Copyright 2013-2024 Michael Zillgith
*
* This file is part of libIEC61850.
*
@ -34,7 +34,8 @@ createValueCaches(MmsDevice* device)
Map valueCaches = Map_create();
int i;
for (i = 0; i < device->domainCount; i++) {
for (i = 0; i < device->domainCount; i++)
{
MmsValueCache valueCache = MmsValueCache_create(device->domains[i]);
Map_addEntry(valueCaches, device->domains[i], valueCache);
}
@ -52,7 +53,8 @@ MmsServer_create(MmsDevice* device, TLSConfiguration tlsConfiguration)
{
MmsServer self = (MmsServer) GLOBAL_CALLOC(1, sizeof(struct sMmsServer));
if (self) {
if (self)
{
#if (CONFIG_MMS_THREADLESS_STACK != 1)
self->openConnectionsLock = Semaphore_create(1);
@ -75,7 +77,8 @@ MmsServer_create(MmsDevice* device, TLSConfiguration tlsConfiguration)
if (self->isoServerList == NULL)
goto exit_error;
if (tlsConfiguration) {
if (tlsConfiguration)
{
IsoServer isoServer = IsoServer_create(tlsConfiguration);
if (isoServer == NULL)
@ -115,7 +118,8 @@ MmsServer_create(MmsDevice* device, TLSConfiguration tlsConfiguration)
{
int i;
for (i = 0; i < CONFIG_MMS_SERVER_MAX_GET_FILE_TASKS; i++) {
for (i = 0; i < CONFIG_MMS_SERVER_MAX_GET_FILE_TASKS; i++)
{
self->fileUploadTasks[i].state = 0;
#if (CONFIG_MMS_THREADLESS_STACK != 1)
@ -140,8 +144,8 @@ MmsServer_addAP(MmsServer self, const char* ipAddr, int tcpPort, TLSConfiguratio
{
IsoServer isoServer = IsoServer_create(tlsConfiguration);
if (isoServer) {
if (isoServer)
{
IsoServer_setLocalIpAddress(isoServer, ipAddr);
if (tcpPort != -1)
@ -161,13 +165,15 @@ MmsServer_addAP(MmsServer self, const char* ipAddr, int tcpPort, TLSConfiguratio
void
MmsServer_setLocalIpAddress(MmsServer self, const char* localIpAddress)
{
if (LinkedList_size(self->isoServerList) == 0) {
if (LinkedList_size(self->isoServerList) == 0)
{
MmsServer_addAP(self, NULL, -1, NULL);
}
LinkedList elem = LinkedList_get(self->isoServerList, 0);
if (elem) {
if (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
IsoServer_setLocalIpAddress(isoServer, localIpAddress);
@ -179,7 +185,8 @@ MmsServer_isRunning(MmsServer self)
{
LinkedList elem = LinkedList_get(self->isoServerList, 0);
if (elem) {
if (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
if (IsoServer_getState(isoServer) == ISO_SVR_STATE_RUNNING)
@ -193,12 +200,13 @@ void
MmsServer_setFilestoreBasepath(MmsServer self, const char* basepath)
{
#if (CONFIG_SET_FILESTORE_BASEPATH_AT_RUNTIME == 1)
if (self->filestoreBasepath != NULL) {
if (self->filestoreBasepath)
{
GLOBAL_FREEMEM(self->filestoreBasepath);
self->filestoreBasepath = NULL;
}
if (basepath != NULL)
if (basepath)
self->filestoreBasepath = StringUtils_copyString(basepath);
#endif /* (CONFIG_SET_FILESTORE_BASEPATH_AT_RUNTIME == 1) */
}
@ -208,15 +216,17 @@ MmsServer_setFilestoreBasepath(MmsServer self, const char* basepath)
void
MmsServer_setMaxConnections(MmsServer self, int maxConnections)
{
if (self->isoServerList) {
if (LinkedList_size(self->isoServerList) == 0) {
if (self->isoServerList)
{
if (LinkedList_size(self->isoServerList) == 0)
{
MmsServer_addAP(self, NULL, -1, NULL);
}
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
IsoServer_setMaxConnections(isoServer, maxConnections);
@ -307,13 +317,14 @@ MmsServer_getObtainFileTask(MmsServer self)
{
int i;
for (i = 0; i < CONFIG_MMS_SERVER_MAX_GET_FILE_TASKS; i++) {
for (i = 0; i < CONFIG_MMS_SERVER_MAX_GET_FILE_TASKS; i++)
{
#if (CONFIG_MMS_THREADLESS_STACK != 1)
Semaphore_wait(self->fileUploadTasks[i].taskLock);
#endif
if (self->fileUploadTasks[i].state == 0) {
if (self->fileUploadTasks[i].state == 0)
{
self->fileUploadTasks[i].state = 1;
return &(self->fileUploadTasks[i]);
@ -322,7 +333,6 @@ MmsServer_getObtainFileTask(MmsServer self)
#if (CONFIG_MMS_THREADLESS_STACK != 1)
Semaphore_post(self->fileUploadTasks[i].taskLock);
#endif
}
return NULL;
@ -392,11 +402,12 @@ MmsServer_setClientAuthenticator(MmsServer self, AcseAuthenticator authenticator
self->authenticator = authenticator;
self->authenticatorParameter = authenticatorParameter;
if (self->isoServerList) {
if (self->isoServerList)
{
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
IsoServer_setAuthenticator(isoServer, authenticator, authenticatorParameter);
@ -448,7 +459,8 @@ deleteSingleCache(MmsValueCache cache)
void
MmsServer_destroy(MmsServer self)
{
if (self) {
if (self)
{
LinkedList_destroyDeep(self->isoServerList, (LinkedListValueDeleteFunction) IsoServer_destroy);
Map_deleteDeep(self->openConnections, false, closeConnection);
@ -476,7 +488,8 @@ MmsServer_destroy(MmsServer self)
#if (MMS_OBTAIN_FILE_SERVICE == 1)
#if (CONFIG_MMS_THREADLESS_STACK != 1)
int i;
for (i = 0; i < CONFIG_MMS_SERVER_MAX_GET_FILE_TASKS; i++) {
for (i = 0; i < CONFIG_MMS_SERVER_MAX_GET_FILE_TASKS; i++)
{
if (self->fileUploadTasks[i].taskLock)
Semaphore_destroy(self->fileUploadTasks[i].taskLock);
}
@ -492,7 +505,7 @@ MmsServer_getValueFromCache(MmsServer self, MmsDomain* domain, const char* itemI
{
MmsValueCache cache = (MmsValueCache) Map_getEntry(self->valueCaches, domain);
if (cache != NULL)
if (cache)
return MmsValueCache_lookupValue(cache, itemId, NULL);
return NULL;
@ -503,7 +516,7 @@ MmsServer_getValueFromCacheEx(MmsServer self, MmsDomain* domain, const char* ite
{
MmsValueCache cache = (MmsValueCache) Map_getEntry(self->valueCaches, domain);
if (cache != NULL)
if (cache)
return MmsValueCache_lookupValue(cache, itemId, typeSpec);
return NULL;
@ -514,7 +527,7 @@ MmsServer_getValueFromCacheEx2(MmsServer self, MmsDomain* domain, const char* it
{
MmsValueCache cache = (MmsValueCache) Map_getEntry(self->valueCaches, domain);
if (cache != NULL)
if (cache)
return MmsValueCache_lookupValueEx(cache, itemId, idx, componentId, NULL);
return NULL;
@ -525,10 +538,9 @@ MmsServer_insertIntoCache(MmsServer self, MmsDomain* domain, char* itemId, MmsVa
{
MmsValueCache cache = (MmsValueCache) Map_getEntry(self->valueCaches, domain);
if (cache != NULL) {
if (cache)
MmsValueCache_insertValue(cache, itemId, value);
}
}
MmsDataAccessError
mmsServer_setValue(MmsServer self, MmsDomain* domain, char* itemId, MmsValue* value,
@ -541,7 +553,8 @@ mmsServer_setValue(MmsServer self, MmsDomain* domain, char* itemId, MmsValue* va
indication = self->writeHandler(self->writeHandlerParameter, domain,
itemId, -1, NULL, value, connection);
}
else {
else
{
MmsValue* cachedValue;
if (domain == NULL)
@ -549,7 +562,8 @@ mmsServer_setValue(MmsServer self, MmsDomain* domain, char* itemId, MmsValue* va
cachedValue = MmsServer_getValueFromCache(self, domain, itemId);
if (cachedValue) {
if (cachedValue)
{
MmsValue_update(cachedValue, value);
indication = DATA_ACCESS_ERROR_SUCCESS;
} else
@ -570,7 +584,8 @@ mmsServer_setValueEx(MmsServer self, MmsDomain* domain, char* itemId, MmsValue*
indication = self->writeHandler(self->writeHandlerParameter, domain,
itemId, arrayIdx, componentId, value, connection);
}
else {
else
{
MmsValue* cachedValue = NULL;
if (domain == NULL)
@ -578,10 +593,12 @@ mmsServer_setValueEx(MmsServer self, MmsDomain* domain, char* itemId, MmsValue*
cachedValue = MmsServer_getValueFromCacheEx2(self, domain, itemId, arrayIdx, componentId);
if (cachedValue) {
if (cachedValue)
{
MmsValue_update(cachedValue, value);
indication = DATA_ACCESS_ERROR_SUCCESS;
} else
}
else
indication = DATA_ACCESS_ERROR_OBJECT_VALUE_INVALID;
}
@ -593,12 +610,14 @@ mmsServer_getValue(MmsServer self, MmsDomain* domain, char* itemId, MmsServerCon
{
MmsValue* value = NULL;
if (self->readAccessHandler != NULL) {
if (self->readAccessHandler != NULL)
{
MmsDataAccessError accessError =
self->readAccessHandler(self->readAccessHandlerParameter, (domain == (MmsDomain*) self->device) ? NULL : domain,
itemId, connection, isDirectAccess);
if (accessError != DATA_ACCESS_ERROR_SUCCESS) {
if (accessError != DATA_ACCESS_ERROR_SUCCESS)
{
value = MmsValue_newDataAccessError(accessError);
MmsValue_setDeletable(value);
goto exit_function;
@ -616,26 +635,13 @@ exit_function:
return value;
}
MmsDataAccessError
mmsServer_checkReadAccess(MmsServer self, MmsDomain* domain, char* itemId, MmsServerConnection connection, bool isDirectAccess)
{
MmsDataAccessError accessError = DATA_ACCESS_ERROR_SUCCESS;
if (self->readAccessHandler) {
accessError =
self->readAccessHandler(self->readAccessHandlerParameter, (domain == (MmsDomain*) self->device) ? NULL : domain,
itemId, connection, isDirectAccess);
}
return accessError;
}
bool
mmsServer_checkListAccess(MmsServer self, MmsGetNameListType listType, MmsDomain* domain, char* itemId, MmsServerConnection connection)
{
bool allowAccess = true;
if (self->listAccessHandler) {
if (self->listAccessHandler)
{
allowAccess = self->listAccessHandler(self->listAccessHandlerParameter, listType, domain, itemId, connection);
}
@ -654,7 +660,8 @@ isoConnectionIndicationHandler(IsoConnectionIndication indication,
{
MmsServer self = (MmsServer) parameter;
if (indication == ISO_CONNECTION_OPENED) {
if (indication == ISO_CONNECTION_OPENED)
{
MmsServerConnection mmsCon = MmsServerConnection_init(0, self, connection);
#if (CONFIG_MMS_THREADLESS_STACK != 1)
@ -671,8 +678,8 @@ isoConnectionIndicationHandler(IsoConnectionIndication indication,
self->connectionHandler(self->connectionHandlerParameter,
mmsCon, MMS_SERVER_NEW_CONNECTION);
}
else if (indication == ISO_CONNECTION_CLOSED) {
else if (indication == ISO_CONNECTION_CLOSED)
{
#if (CONFIG_MMS_THREADLESS_STACK != 1)
Semaphore_wait(self->openConnectionsLock);
#endif
@ -697,15 +704,17 @@ isoConnectionIndicationHandler(IsoConnectionIndication indication,
void
MmsServer_startListening(MmsServer self, int tcpPort)
{
if (self->isoServerList) {
if (LinkedList_size(self->isoServerList) == 0) {
if (self->isoServerList)
{
if (LinkedList_size(self->isoServerList) == 0)
{
MmsServer_addAP(self, NULL, -1, NULL);
}
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
IsoServer_setConnectionHandler(isoServer, isoConnectionIndicationHandler, (void*) self);
@ -723,10 +732,12 @@ MmsServer_startListening(MmsServer self, int tcpPort)
void
MmsServer_stopListening(MmsServer self)
{
if (self->isoServerList) {
if (self->isoServerList)
{
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
IsoServer_stopListening(isoServer);
@ -740,15 +751,17 @@ MmsServer_stopListening(MmsServer self)
void
MmsServer_startListeningThreadless(MmsServer self, int tcpPort)
{
if (self->isoServerList) {
if (LinkedList_size(self->isoServerList) == 0) {
if (self->isoServerList)
{
if (LinkedList_size(self->isoServerList) == 0)
{
MmsServer_addAP(self, NULL, -1, NULL);
}
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
IsoServer_setConnectionHandler(isoServer, isoConnectionIndicationHandler, (void*) self);
@ -768,21 +781,25 @@ MmsServer_waitReady(MmsServer self, unsigned int timeoutMs)
{
int result = 0;
if (self->isoServerList) {
if (self->isoServerList)
{
bool isFirst = true;
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
int serverResult;
if (isFirst) {
if (isFirst)
{
serverResult = IsoServer_waitReady(isoServer, timeoutMs);
isFirst = false;
}
else {
else
{
serverResult = IsoServer_waitReady(isoServer, 0);
}
@ -799,10 +816,12 @@ MmsServer_waitReady(MmsServer self, unsigned int timeoutMs)
void
MmsServer_handleIncomingMessages(MmsServer self)
{
if (self->isoServerList) {
if (self->isoServerList)
{
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
IsoServer_processIncomingMessages(isoServer);
@ -844,10 +863,12 @@ MmsServer_getConnectionCounter(MmsServer self)
{
int count = 0;
if (self->isoServerList) {
if (self->isoServerList)
{
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
count += IsoServer_getConnectionCounter(isoServer);
@ -862,7 +883,8 @@ MmsServer_getConnectionCounter(MmsServer self)
void
MmsServer_callConnectionHandler(MmsServer self, MmsServerConnection connection)
{
if (self->connectionHandler) {
if (self->connectionHandler)
{
self->connectionHandler(self->connectionHandlerParameter, connection, MMS_SERVER_CONNECTION_TICK);
}
}
@ -870,10 +892,12 @@ MmsServer_callConnectionHandler(MmsServer self, MmsServerConnection connection)
void
MmsServer_stopListeningThreadless(MmsServer self)
{
if (self->isoServerList) {
if (self->isoServerList)
{
LinkedList elem = LinkedList_getNext(self->isoServerList);
while (elem) {
while (elem)
{
IsoServer isoServer = (IsoServer) LinkedList_getData(elem);
IsoServer_stopListeningThreadless(isoServer);

Loading…
Cancel
Save