- code format update

- fixed problem in handling of memory allocation error (#553)
v1.6
Michael Zillgith 4 weeks ago
parent f5fceab050
commit a41d2dce37

@ -41,15 +41,15 @@ readLine(FileHandle fileHandle, uint8_t* buffer, int maxSize)
int fileReadResult = 1; int fileReadResult = 1;
/* eat up leading cr or lf */ /* eat up leading cr or lf */
while (fileReadResult > 0) { while (fileReadResult > 0)
{
if (bytesRead == maxSize) if (bytesRead == maxSize)
break; break;
fileReadResult = FileSystem_readFile(fileHandle, buffer + bufPos, 1); fileReadResult = FileSystem_readFile(fileHandle, buffer + bufPos, 1);
if (fileReadResult == 1) { if (fileReadResult == 1)
{
if (!((buffer[bufPos] == '\n') || (buffer[bufPos] == '\r'))) { if (!((buffer[bufPos] == '\n') || (buffer[bufPos] == '\r'))) {
bufPos++; bufPos++;
bytesRead++; bytesRead++;
@ -58,16 +58,17 @@ readLine(FileHandle fileHandle, uint8_t* buffer, int maxSize)
} }
} }
if (fileReadResult > 0) { if (fileReadResult > 0)
while (fileReadResult > 0) { {
while (fileReadResult > 0)
{
if (bytesRead == maxSize) if (bytesRead == maxSize)
break; break;
fileReadResult = FileSystem_readFile(fileHandle, buffer + bufPos, 1); fileReadResult = FileSystem_readFile(fileHandle, buffer + bufPos, 1);
if (fileReadResult == 1) { if (fileReadResult == 1)
{
if ((buffer[bufPos] == '\n') || (buffer[bufPos] == '\r')) if ((buffer[bufPos] == '\n') || (buffer[bufPos] == '\r'))
break; break;
else { else {
@ -86,8 +87,10 @@ terminateString(char* string, char ch)
{ {
int index = 0; int index = 0;
while (string[index] != 0) { while (string[index] != 0)
if (string[index] == ch) { {
if (string[index] == ch)
{
string[index] = 0; string[index] = 0;
break; break;
} }
@ -101,7 +104,8 @@ ConfigFileParser_createModelFromConfigFileEx(const char* filename)
{ {
FileHandle configFile = FileSystem_openFile((char*)filename, false); FileHandle configFile = FileSystem_openFile((char*)filename, false);
if (configFile == NULL) { if (configFile == NULL)
{
if (DEBUG_IED_SERVER) if (DEBUG_IED_SERVER)
printf("IED_SERVER: Error opening config file!\n"); printf("IED_SERVER: Error opening config file!\n");
return NULL; return NULL;
@ -119,7 +123,8 @@ setValue(char* lineBuffer, DataAttribute* dataAttribute)
{ {
char* valueIndicator = strchr((char*) lineBuffer, '='); char* valueIndicator = strchr((char*) lineBuffer, '=');
if (valueIndicator != NULL) { if (valueIndicator)
{
switch (dataAttribute->type) { switch (dataAttribute->type) {
case IEC61850_UNICODE_STRING_255: case IEC61850_UNICODE_STRING_255:
{ {
@ -226,7 +231,7 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
uint8_t* lineBuffer = (uint8_t*)GLOBAL_MALLOC(READ_BUFFER_MAX_SIZE); uint8_t* lineBuffer = (uint8_t*)GLOBAL_MALLOC(READ_BUFFER_MAX_SIZE);
if (lineBuffer == NULL) if (lineBuffer == NULL)
goto exit_error; return NULL;
int bytesRead = 1; int bytesRead = 1;
@ -261,43 +266,54 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
lineBuffer[bytesRead] = 0; lineBuffer[bytesRead] = 0;
/* trim trailing spaces */ /* trim trailing spaces */
while (bytesRead > 1) { while (bytesRead > 1)
{
bytesRead--; bytesRead--;
if (isspace(lineBuffer[bytesRead])) { if (isspace(lineBuffer[bytesRead]))
{
lineBuffer[bytesRead] = 0; lineBuffer[bytesRead] = 0;
} }
else { else
{
break; break;
} }
} }
if (stateInModel) if (stateInModel)
{ {
if (StringUtils_startsWith((char*) lineBuffer, "}")) { if (StringUtils_startsWith((char*)lineBuffer, "}"))
if (indendation == 1) { {
if (indendation == 1)
{
stateInModel = false; stateInModel = false;
indendation = 0; indendation = 0;
} }
else if (indendation == 2) { else if (indendation == 2)
{
indendation = 1; indendation = 1;
} }
else if (indendation == 3) { else if (indendation == 3)
{
indendation = 2; indendation = 2;
} }
else if (indendation == 4) { else if (indendation == 4)
{
indendation = 3; indendation = 3;
} }
else if (indendation > 4) { else if (indendation > 4)
{
if (inArrayElement && currentModelNode->parent == currentArrayNode) { if (inArrayElement && currentModelNode->parent == currentArrayNode)
{
inArrayElement = false; inArrayElement = false;
} }
else { else
{
indendation--; indendation--;
} }
if (inArray && currentModelNode == currentArrayNode) { if (inArray && currentModelNode == currentArrayNode)
{
inArray = false; inArray = false;
} }
@ -306,14 +322,14 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
} }
else if (indendation == 1) else if (indendation == 1)
{ {
if (StringUtils_startsWith((char*) lineBuffer, "LD")) if (StringUtils_startsWith((char*)lineBuffer, "LD"))
{ {
indendation = 2; indendation = 2;
char ldName[65]; char ldName[65];
ldName[0] = 0; ldName[0] = 0;
if (sscanf((char*) lineBuffer, "LD(%129s %64s)", nameString, ldName) < 1) if (sscanf((char*)lineBuffer, "LD(%129s %64s)", nameString, ldName) < 1)
goto exit_error; goto exit_error;
terminateString(nameString, ')'); terminateString(nameString, ')');
@ -324,7 +340,8 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
currentLD = LogicalDevice_createEx(nameString, model, ldName); currentLD = LogicalDevice_createEx(nameString, model, ldName);
} }
else { else
{
currentLD = LogicalDevice_create(nameString, model); currentLD = LogicalDevice_create(nameString, model);
} }
} }
@ -349,24 +366,26 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
} }
else if (indendation == 3) else if (indendation == 3)
{ {
if (StringUtils_startsWith((char*) lineBuffer, "DO")) if (StringUtils_startsWith((char*)lineBuffer, "DO"))
{ {
indendation = 4; indendation = 4;
int arrayElements = 0; int arrayElements = 0;
if (sscanf((char*)lineBuffer, "DO(%129s %i)", nameString, &arrayElements) != 2) { if (sscanf((char*)lineBuffer, "DO(%129s %i)", nameString, &arrayElements) != 2)
{
goto exit_error; goto exit_error;
} }
currentModelNode = (ModelNode*) currentModelNode =
DataObject_create(nameString, (ModelNode*) currentLN, arrayElements); (ModelNode*)DataObject_create(nameString, (ModelNode*)currentLN, arrayElements);
} }
else if (StringUtils_startsWith((char*) lineBuffer, "DS")) else if (StringUtils_startsWith((char*)lineBuffer, "DS"))
{ {
indendation = 4; indendation = 4;
if (sscanf((char*)lineBuffer, "DS(%129s)", nameString) != 1) { if (sscanf((char*)lineBuffer, "DS(%129s)", nameString) != 1)
{
goto exit_error; goto exit_error;
} }
@ -478,7 +497,8 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
#if (CONFIG_IEC61850_SETTING_GROUPS == 1) #if (CONFIG_IEC61850_SETTING_GROUPS == 1)
else if (StringUtils_startsWith((char*) lineBuffer, "SG")) else if (StringUtils_startsWith((char*) lineBuffer, "SG"))
{ {
if (strcmp(currentLN->name, "LLN0") != 0) { if (strcmp(currentLN->name, "LLN0") != 0)
{
if (DEBUG_IED_SERVER) if (DEBUG_IED_SERVER)
printf("IED_SERVER: Setting group control is not defined in LLN0\n"); printf("IED_SERVER: Setting group control is not defined in LLN0\n");
@ -496,7 +516,6 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
SettingGroupControlBlock_create(currentLN, actSG, numOfSGs); SettingGroupControlBlock_create(currentLN, actSG, numOfSGs);
} }
#endif /* (CONFIG_IEC61850_SETTING_GROUPS == 1) */ #endif /* (CONFIG_IEC61850_SETTING_GROUPS == 1) */
else else
{ {
if (DEBUG_IED_SERVER) if (DEBUG_IED_SERVER)
@ -527,17 +546,20 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
} }
else if (StringUtils_startsWith((char*) lineBuffer, "[")) else if (StringUtils_startsWith((char*) lineBuffer, "["))
{ {
if (inArray == false) { if (inArray == false)
{
goto exit_error; goto exit_error;
} }
int arrayIndex; int arrayIndex;
if (sscanf((char*)lineBuffer, "[%i]", &arrayIndex) != 1) { if (sscanf((char*)lineBuffer, "[%i]", &arrayIndex) != 1)
{
goto exit_error; goto exit_error;
} }
if (arrayIndex < 0) { if (arrayIndex < 0)
{
goto exit_error; goto exit_error;
} }
@ -548,10 +570,12 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
/* array of basic data attribute */ /* array of basic data attribute */
ModelNode* arrayElementNode = ModelNode_getChildWithIdx(currentArrayNode, arrayIndex); ModelNode* arrayElementNode = ModelNode_getChildWithIdx(currentArrayNode, arrayIndex);
if (arrayElementNode) { if (arrayElementNode)
{
setValue((char*)lineBuffer, (DataAttribute*)arrayElementNode); setValue((char*)lineBuffer, (DataAttribute*)arrayElementNode);
} }
else { else
{
goto exit_error; goto exit_error;
} }
} }
@ -560,10 +584,12 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
/* array of constructed data attribtute */ /* array of constructed data attribtute */
currentModelNode = ModelNode_getChildWithIdx(currentArrayNode, arrayIndex); currentModelNode = ModelNode_getChildWithIdx(currentArrayNode, arrayIndex);
if (currentModelNode) { if (currentModelNode)
{
inArrayElement = true; inArrayElement = true;
} }
else { else
{
goto exit_error; goto exit_error;
} }
} }
@ -575,10 +601,12 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
/* array of constructed data attribtute */ /* array of constructed data attribtute */
currentModelNode = ModelNode_getChildWithIdx(currentArrayNode, arrayIndex); currentModelNode = ModelNode_getChildWithIdx(currentArrayNode, arrayIndex);
if (currentModelNode) { if (currentModelNode)
{
inArrayElement = true; inArrayElement = true;
} }
else { else
{
goto exit_error; goto exit_error;
} }
} }
@ -665,26 +693,30 @@ ConfigFileParser_createModelFromConfigFile(FileHandle fileHandle)
uint32_t vlanId; uint32_t vlanId;
uint32_t appId; uint32_t appId;
int matchedItems = sscanf((char*) lineBuffer, "PA(%u %u %u %129s)", &vlanPrio, &vlanId, &appId, nameString); int matchedItems =
sscanf((char*)lineBuffer, "PA(%u %u %u %129s)", &vlanPrio, &vlanId, &appId, nameString);
if ((matchedItems != 4) || ((currentGoCB == NULL) && (currentSMVCB == NULL))) goto exit_error; if ((matchedItems != 4) || ((currentGoCB == NULL) && (currentSMVCB == NULL)))
goto exit_error;
terminateString(nameString, ')'); terminateString(nameString, ')');
if (strlen(nameString) != 12) goto exit_error; if (strlen(nameString) != 12)
goto exit_error;
if (StringUtils_createBufferFromHexString(nameString, (uint8_t*) nameString2) != 6) if (StringUtils_createBufferFromHexString(nameString, (uint8_t*)nameString2) != 6)
goto exit_error; goto exit_error;
PhyComAddress* dstAddress = PhyComAddress* dstAddress = PhyComAddress_create((uint8_t)vlanPrio, (uint16_t)vlanId,
PhyComAddress_create((uint8_t) vlanPrio, (uint16_t) vlanId, (uint16_t) appId, (uint16_t)appId, (uint8_t*)nameString2);
(uint8_t*) nameString2);
if (currentGoCB) { if (currentGoCB)
{
GSEControlBlock_addPhyComAddress(currentGoCB, dstAddress); GSEControlBlock_addPhyComAddress(currentGoCB, dstAddress);
} }
if (currentSMVCB) { if (currentSMVCB)
{
SVControlBlock_addPhyComAddress(currentSMVCB, dstAddress); SVControlBlock_addPhyComAddress(currentSMVCB, dstAddress);
} }
} }

@ -38,7 +38,7 @@ setAttributeValuesToNull(ModelNode* node)
ModelNode* child = node->firstChild; ModelNode* child = node->firstChild;
while (child != NULL) while (child)
{ {
setAttributeValuesToNull(child); setAttributeValuesToNull(child);
child = child->sibling; child = child->sibling;
@ -56,15 +56,15 @@ IedModel_setAttributeValuesToNull(IedModel* iedModel)
{ {
LogicalDevice* ld = iedModel->firstChild; LogicalDevice* ld = iedModel->firstChild;
while (ld != NULL) while (ld)
{ {
LogicalNode* ln = (LogicalNode*)ld->firstChild; LogicalNode* ln = (LogicalNode*)ld->firstChild;
while (ln != NULL) while (ln)
{ {
ModelNode* node = ln->firstChild; ModelNode* node = ln->firstChild;
while (node != NULL) while (node)
{ {
setAttributeValuesToNull(node); setAttributeValuesToNull(node);
node = node->sibling; node = node->sibling;
@ -87,7 +87,7 @@ IedModel_getLogicalDeviceCount(IedModel* self)
int ldCount = 1; int ldCount = 1;
while (logicalDevice->sibling != NULL) while (logicalDevice->sibling)
{ {
logicalDevice = (LogicalDevice*)logicalDevice->sibling; logicalDevice = (LogicalDevice*)logicalDevice->sibling;
ldCount++; ldCount++;
@ -117,7 +117,7 @@ IedModel_lookupDataSet(IedModel* self, const char* dataSetReference /* e.g. ied1
memcpy(domainName, self->name, modelNameLen); memcpy(domainName, self->name, modelNameLen);
while (dataSet != NULL) while (dataSet)
{ {
LogicalDevice* ld = IedModel_getDeviceByInst(self, dataSet->logicalDeviceName); LogicalDevice* ld = IedModel_getDeviceByInst(self, dataSet->logicalDeviceName);
@ -227,7 +227,7 @@ ModelNode_getDataAttributeByMmsValue(ModelNode* self, MmsValue* value)
{ {
ModelNode* node = self->firstChild; ModelNode* node = self->firstChild;
while (node != NULL) while (node)
{ {
if (node->modelType == DataAttributeModelType) if (node->modelType == DataAttributeModelType)
{ {
@ -239,7 +239,7 @@ ModelNode_getDataAttributeByMmsValue(ModelNode* self, MmsValue* value)
DataAttribute* da = ModelNode_getDataAttributeByMmsValue(node, value); DataAttribute* da = ModelNode_getDataAttributeByMmsValue(node, value);
if (da != NULL) if (da)
return da; return da;
node = node->sibling; node = node->sibling;
@ -253,11 +253,11 @@ IedModel_lookupDataAttributeByMmsValue(IedModel* model, MmsValue* value)
{ {
LogicalDevice* ld = model->firstChild; LogicalDevice* ld = model->firstChild;
while (ld != NULL) while (ld)
{ {
DataAttribute* da = ModelNode_getDataAttributeByMmsValue((ModelNode*)ld, value); DataAttribute* da = ModelNode_getDataAttributeByMmsValue((ModelNode*)ld, value);
if (da != NULL) if (da)
return da; return da;
ld = (LogicalDevice*)ld->sibling; ld = (LogicalDevice*)ld->sibling;
@ -273,7 +273,7 @@ getChildWithShortAddress(ModelNode* node, uint32_t sAddr)
child = node->firstChild; child = node->firstChild;
while (child != NULL) while (child)
{ {
if (child->modelType == DataAttributeModelType) if (child->modelType == DataAttributeModelType)
{ {
@ -285,7 +285,7 @@ getChildWithShortAddress(ModelNode* node, uint32_t sAddr)
ModelNode* childChild = getChildWithShortAddress(child, sAddr); ModelNode* childChild = getChildWithShortAddress(child, sAddr);
if (childChild != NULL) if (childChild)
return childChild; return childChild;
child = child->sibling; child = child->sibling;
@ -301,19 +301,19 @@ IedModel_getModelNodeByShortAddress(IedModel* model, uint32_t sAddr)
LogicalDevice* ld = (LogicalDevice*)model->firstChild; LogicalDevice* ld = (LogicalDevice*)model->firstChild;
while (ld != NULL) while (ld)
{ {
LogicalNode* ln = (LogicalNode*)ld->firstChild; LogicalNode* ln = (LogicalNode*)ld->firstChild;
while (ln != NULL) while (ln)
{ {
ModelNode* doNode = ln->firstChild; ModelNode* doNode = ln->firstChild;
while (doNode != NULL) while (doNode)
{ {
ModelNode* matchingNode = getChildWithShortAddress(doNode, sAddr); ModelNode* matchingNode = getChildWithShortAddress(doNode, sAddr);
if (matchingNode != NULL) if (matchingNode)
return matchingNode; return matchingNode;
doNode = doNode->sibling; doNode = doNode->sibling;
@ -339,7 +339,7 @@ IedModel_getModelNodeByObjectReference(IedModel* model, const char* objectRefere
char* separator = strchr(objRef, '/'); char* separator = strchr(objRef, '/');
if (separator != NULL) if (separator)
*separator = 0; *separator = 0;
LogicalDevice* ld = IedModel_getDevice(model, objRef); LogicalDevice* ld = IedModel_getDevice(model, objRef);
@ -362,7 +362,7 @@ IedModel_getSVControlBlock(IedModel* self, LogicalNode* parentLN, const char* sv
SVControlBlock* svCb = self->svCBs; SVControlBlock* svCb = self->svCBs;
while (svCb != NULL) while (svCb)
{ {
if ((svCb->parent == parentLN) && (strcmp(svCb->name, svcbName) == 0)) if ((svCb->parent == parentLN) && (strcmp(svCb->name, svcbName) == 0))
{ {
@ -389,7 +389,7 @@ IedModel_getModelNodeByShortObjectReference(IedModel* model, const char* objectR
char* separator = strchr(objRef, '/'); char* separator = strchr(objRef, '/');
if (separator != NULL) if (separator)
*separator = 0; *separator = 0;
char ldInst[65]; char ldInst[65];
@ -418,7 +418,7 @@ DataObject_hasFCData(DataObject* dataObject, FunctionalConstraint fc)
{ {
ModelNode* modelNode = dataObject->firstChild; ModelNode* modelNode = dataObject->firstChild;
while (modelNode != NULL) while (modelNode)
{ {
if (modelNode->modelType == DataAttributeModelType) if (modelNode->modelType == DataAttributeModelType)
{ {
@ -444,7 +444,7 @@ LogicalNode_hasFCData(LogicalNode* node, FunctionalConstraint fc)
{ {
DataObject* dataObject = (DataObject*)node->firstChild; DataObject* dataObject = (DataObject*)node->firstChild;
while (dataObject != NULL) while (dataObject)
{ {
if (DataObject_hasFCData(dataObject, fc)) if (DataObject_hasFCData(dataObject, fc))
return true; return true;
@ -481,7 +481,7 @@ LogicalNode_getDataSet(LogicalNode* self, const char* dataSetName)
DataSet* ds = iedModel->dataSets; DataSet* ds = iedModel->dataSets;
while (ds != NULL) while (ds)
{ {
if (strcmp(ds->logicalDeviceName, ld->name) == 0) if (strcmp(ds->logicalDeviceName, ld->name) == 0)
{ {
@ -505,7 +505,7 @@ LogicalDevice_getLogicalNodeCount(LogicalDevice* logicalDevice)
LogicalNode* logicalNode = (LogicalNode*)logicalDevice->firstChild; LogicalNode* logicalNode = (LogicalNode*)logicalDevice->firstChild;
while (logicalNode != NULL) while (logicalNode)
{ {
logicalNode = (LogicalNode*)logicalNode->sibling; logicalNode = (LogicalNode*)logicalNode->sibling;
lnCount++; lnCount++;
@ -721,7 +721,7 @@ ModelNode_getChildCount(ModelNode* modelNode)
ModelNode* child = modelNode->firstChild; ModelNode* child = modelNode->firstChild;
while (child != NULL) while (child)
{ {
childCount++; childCount++;
child = child->sibling; child = child->sibling;
@ -810,7 +810,7 @@ ModelNode_getChild(ModelNode* self, const char* name)
int nameElementLength = 0; int nameElementLength = 0;
if (separator != NULL) if (separator)
nameElementLength = (separator - name); nameElementLength = (separator - name);
else else
nameElementLength = strlen(name); nameElementLength = strlen(name);
@ -884,7 +884,7 @@ ModelNode_getChildWithFc(ModelNode* self, const char* name, FunctionalConstraint
int nameElementLength = 0; int nameElementLength = 0;
if (separator != NULL) if (separator)
nameElementLength = (separator - name); nameElementLength = (separator - name);
else else
nameElementLength = strlen(name); nameElementLength = strlen(name);
@ -893,7 +893,7 @@ ModelNode_getChildWithFc(ModelNode* self, const char* name, FunctionalConstraint
ModelNode* matchingNode = NULL; ModelNode* matchingNode = NULL;
while (nextNode != NULL) while (nextNode)
{ {
int nodeNameLen = strlen(nextNode->name); int nodeNameLen = strlen(nextNode->name);
@ -1010,7 +1010,7 @@ LogicalDevice_getSettingGroupControlBlock(LogicalDevice* self)
SettingGroupControlBlock* sgcb = model->sgcbs; SettingGroupControlBlock* sgcb = model->sgcbs;
while (sgcb != NULL) while (sgcb)
{ {
if (sgcb->parent == ln) if (sgcb->parent == ln)
return sgcb; return sgcb;

Loading…
Cancel
Save