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

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

Loading…
Cancel
Save