- fixed code to support arrays of complex data attributes

v1.6_develop_rgoose_sntp
Michael Zillgith 3 years ago
parent 9e1a9a63ba
commit 6b23f87b23

@ -26,30 +26,33 @@
char* char*
StringUtils_copySubString(char* startPos, char* endPos) StringUtils_copySubString(char* startPos, char* endPos)
{ {
int newStringLength = endPos - startPos; int newStringLength = endPos - startPos;
char* newString = (char*) GLOBAL_MALLOC(newStringLength + 1); char* newString = (char*) GLOBAL_MALLOC(newStringLength + 1);
if (newString) { if (newString) {
memcpy(newString, startPos, newStringLength); memcpy(newString, startPos, newStringLength);
newString[newStringLength] = 0; newString[newStringLength] = 0;
} }
return newString; return newString;
} }
char* char*
StringUtils_copyString(const char* string) StringUtils_copyString(const char* str)
{ {
int newStringLength = strlen(string) + 1; if (str == NULL)
return NULL;
char* newString = (char*) GLOBAL_MALLOC(newStringLength); int newStringLength = strlen(str) + 1;
if (newString) char* newString = (char*) GLOBAL_MALLOC(newStringLength);
memcpy(newString, string, newStringLength);
return newString; if (newString)
memcpy(newString, str, newStringLength);
return newString;
} }
char* char*

@ -247,6 +247,10 @@ installDefaultValuesForDataAttribute(IedServer self, LogicalDevice* ld, DataAttr
sprintf(componentId + compIdPos, "$%s", dataAttribute->name); sprintf(componentId + compIdPos, "$%s", dataAttribute->name);
} }
} }
else {
if (compIdPos == 0)
componentId[0] = 0;
}
char mmsVariableName[65]; /* maximum size is 64 according to 61850-8-1 */ char mmsVariableName[65]; /* maximum size is 64 according to 61850-8-1 */
@ -306,8 +310,10 @@ installDefaultValuesForDataAttribute(IedServer self, LogicalDevice* ld, DataAttr
DataAttribute* subDataAttribute = (DataAttribute*) dataAttribute->firstChild; DataAttribute* subDataAttribute = (DataAttribute*) dataAttribute->firstChild;
int childIdPos = childCompIdPos;
while (subDataAttribute != NULL) { while (subDataAttribute != NULL) {
installDefaultValuesForDataAttribute(self, ld, subDataAttribute, objectReference, childPosition, subIdx, componentId, childCompIdPos); installDefaultValuesForDataAttribute(self, ld, subDataAttribute, objectReference, childPosition, subIdx, componentId, childIdPos);
subIdx++; subIdx++;
@ -317,8 +323,10 @@ installDefaultValuesForDataAttribute(IedServer self, LogicalDevice* ld, DataAttr
else { else {
DataAttribute* subDataAttribute = (DataAttribute*) dataAttribute->firstChild; DataAttribute* subDataAttribute = (DataAttribute*) dataAttribute->firstChild;
int childIdPos = childCompIdPos;
while (subDataAttribute != NULL) { while (subDataAttribute != NULL) {
installDefaultValuesForDataAttribute(self, ld, subDataAttribute, objectReference, childPosition, idx, componentId, childCompIdPos); installDefaultValuesForDataAttribute(self, ld, subDataAttribute, objectReference, childPosition, idx, componentId, childIdPos);
subDataAttribute = (DataAttribute*) subDataAttribute->sibling; subDataAttribute = (DataAttribute*) subDataAttribute->sibling;
} }

@ -126,9 +126,12 @@ createNamedVariableFromDataAttribute(DataAttribute* attribute)
MmsVariableSpecification* namedVariable = origNamedVariable; MmsVariableSpecification* namedVariable = origNamedVariable;
bool isBasicArray = false; bool isBasicArray = false;
bool isArray = false;
if (attribute->elementCount > 0) if (attribute->elementCount > 0)
{ {
isArray = true;
namedVariable->type = MMS_ARRAY; namedVariable->type = MMS_ARRAY;
namedVariable->typeSpec.array.elementCount = attribute->elementCount; namedVariable->typeSpec.array.elementCount = attribute->elementCount;
namedVariable->typeSpec.array.elementTypeSpec = (MmsVariableSpecification*) GLOBAL_CALLOC(1, namedVariable->typeSpec.array.elementTypeSpec = (MmsVariableSpecification*) GLOBAL_CALLOC(1,
@ -141,6 +144,11 @@ createNamedVariableFromDataAttribute(DataAttribute* attribute)
} }
if ((attribute->firstChild != NULL) && (isBasicArray == false)) { if ((attribute->firstChild != NULL) && (isBasicArray == false)) {
if (isArray) {
attribute = (DataAttribute*)attribute->firstChild;
}
namedVariable->type = MMS_STRUCTURE; namedVariable->type = MMS_STRUCTURE;
int componentCount = ModelNode_getChildCount((ModelNode*) attribute); int componentCount = ModelNode_getChildCount((ModelNode*) attribute);

@ -242,13 +242,15 @@ LogicalNode_create(const char* name, LogicalDevice* parent)
{ {
LogicalNode* self = (LogicalNode*) GLOBAL_MALLOC(sizeof(LogicalNode)); LogicalNode* self = (LogicalNode*) GLOBAL_MALLOC(sizeof(LogicalNode));
self->name = StringUtils_copyString(name); if (self) {
self->parent = (ModelNode*) parent; self->name = StringUtils_copyString(name);
self->modelType = LogicalNodeModelType; self->parent = (ModelNode*) parent;
self->firstChild = NULL; self->modelType = LogicalNodeModelType;
self->sibling = NULL; self->firstChild = NULL;
self->sibling = NULL;
LogicalDevice_addLogicalNode(parent, self); LogicalDevice_addLogicalNode(parent, self);
}
return self; return self;
} }
@ -294,11 +296,13 @@ Log_create(const char* name, LogicalNode* parent)
{ {
Log* self = (Log*) GLOBAL_MALLOC(sizeof(Log)); Log* self = (Log*) GLOBAL_MALLOC(sizeof(Log));
self->name = StringUtils_copyString(name); if (self) {
self->parent = parent; self->name = StringUtils_copyString(name);
self->sibling = NULL; self->parent = parent;
self->sibling = NULL;
LogicalNode_addLog(parent, self); LogicalNode_addLog(parent, self);
}
return self; return self;
} }
@ -317,26 +321,28 @@ LogControlBlock_create(const char* name, LogicalNode* parent, const char* dataSe
{ {
LogControlBlock* self = (LogControlBlock*) GLOBAL_MALLOC(sizeof(LogControlBlock)); LogControlBlock* self = (LogControlBlock*) GLOBAL_MALLOC(sizeof(LogControlBlock));
self->name = StringUtils_copyString(name); if (self) {
self->parent = parent; self->name = StringUtils_copyString(name);
self->sibling = NULL; self->parent = parent;
self->sibling = NULL;
if (dataSetName) if (dataSetName)
self->dataSetName = StringUtils_copyString(dataSetName); self->dataSetName = StringUtils_copyString(dataSetName);
else else
self->dataSetName = NULL; self->dataSetName = NULL;
if (logRef) if (logRef)
self->logRef = StringUtils_copyString(logRef); self->logRef = StringUtils_copyString(logRef);
else else
self->logRef = NULL; self->logRef = NULL;
self->trgOps = trgOps; self->trgOps = trgOps;
self->intPeriod = intPeriod; self->intPeriod = intPeriod;
self->logEna = logEna; self->logEna = logEna;
self->reasonCode = reasonCode; self->reasonCode = reasonCode;
LogicalNode_addLogControlBlock(parent, self); LogicalNode_addLogControlBlock(parent, self);
}
return self; return self;
} }
@ -355,30 +361,32 @@ ReportControlBlock_create(const char* name, LogicalNode* parent, const char* rpt
{ {
ReportControlBlock* self = (ReportControlBlock*) GLOBAL_MALLOC(sizeof(ReportControlBlock)); ReportControlBlock* self = (ReportControlBlock*) GLOBAL_MALLOC(sizeof(ReportControlBlock));
self->name = StringUtils_copyString(name); if (self) {
self->parent = parent; self->name = StringUtils_copyString(name);
self->parent = parent;
if (rptId) if (rptId)
self->rptId = StringUtils_copyString(rptId); self->rptId = StringUtils_copyString(rptId);
else else
self->rptId = NULL; self->rptId = NULL;
self->buffered = isBuffered; self->buffered = isBuffered;
if (dataSetName) if (dataSetName)
self->dataSetName = StringUtils_copyString(dataSetName); self->dataSetName = StringUtils_copyString(dataSetName);
else else
self->dataSetName = NULL; self->dataSetName = NULL;
self->confRef = confRef; self->confRef = confRef;
self->trgOps = trgOps; self->trgOps = trgOps;
self->options = options; self->options = options;
self->bufferTime = bufTm; self->bufferTime = bufTm;
self->intPeriod = intgPd; self->intPeriod = intgPd;
self->sibling = NULL; self->sibling = NULL;
self->clientReservation[0] = 0; /* no pre-configured client */ self->clientReservation[0] = 0; /* no pre-configured client */
LogicalNode_addReportControlBlock(parent, self); LogicalNode_addReportControlBlock(parent, self);
}
return self; return self;
} }
@ -434,13 +442,15 @@ SettingGroupControlBlock_create(LogicalNode* parent, uint8_t actSG, uint8_t numO
SettingGroupControlBlock* self = (SettingGroupControlBlock*) GLOBAL_MALLOC(sizeof(SettingGroupControlBlock)); SettingGroupControlBlock* self = (SettingGroupControlBlock*) GLOBAL_MALLOC(sizeof(SettingGroupControlBlock));
self->parent = parent; if (self) {
self->actSG = actSG; self->parent = parent;
self->numOfSGs = numOfSGs; self->actSG = actSG;
self->sibling = NULL; self->numOfSGs = numOfSGs;
self->editSG = 0; self->sibling = NULL;
self->editSG = 0;
LogicalNode_addSettingGroupControlBlock(parent, self); LogicalNode_addSettingGroupControlBlock(parent, self);
}
return self; return self;
} }
@ -460,30 +470,32 @@ GSEControlBlock_create(const char* name, LogicalNode* parent, const char* appId,
{ {
GSEControlBlock* self = (GSEControlBlock*) GLOBAL_MALLOC(sizeof(GSEControlBlock)); GSEControlBlock* self = (GSEControlBlock*) GLOBAL_MALLOC(sizeof(GSEControlBlock));
self->name = StringUtils_copyString(name); if (self) {
self->parent = parent; self->name = StringUtils_copyString(name);
self->parent = parent;
if (appId) if (appId)
self->appId = StringUtils_copyString(appId); self->appId = StringUtils_copyString(appId);
else else
self->appId = NULL; self->appId = NULL;
if (dataSet) if (dataSet)
self->dataSetName = StringUtils_copyString(dataSet); self->dataSetName = StringUtils_copyString(dataSet);
else else
self->dataSetName = NULL; self->dataSetName = NULL;
self->confRev = confRef; self->confRev = confRef;
self->fixedOffs = fixedOffs; self->fixedOffs = fixedOffs;
self->minTime = minTime; self->minTime = minTime;
self->maxTime = maxTime; self->maxTime = maxTime;
self->address = NULL; self->address = NULL;
self->sibling = NULL; self->sibling = NULL;
if (parent != NULL) if (parent != NULL)
LogicalNode_addGSEControlBlock(parent, self); LogicalNode_addGSEControlBlock(parent, self);
}
return self; return self;
} }
@ -494,23 +506,25 @@ SVControlBlock_create(const char* name, LogicalNode* parent, const char* svID, c
{ {
SVControlBlock* self = (SVControlBlock*) GLOBAL_MALLOC(sizeof(SVControlBlock)); SVControlBlock* self = (SVControlBlock*) GLOBAL_MALLOC(sizeof(SVControlBlock));
self->name = StringUtils_copyString(name); if (self) {
self->parent = parent; self->name = StringUtils_copyString(name);
self->parent = parent;
self->svId = StringUtils_copyString(svID); /* Is there a default value? */ self->svId = StringUtils_copyString(svID); /* Is there a default value? */
if (dataSet) if (dataSet)
self->dataSetName = StringUtils_copyString(dataSet); self->dataSetName = StringUtils_copyString(dataSet);
else else
self->dataSetName = NULL; self->dataSetName = NULL;
self->confRev = confRev; self->confRev = confRev;
self->smpMod = smpMod; self->smpMod = smpMod;
self->smpRate = smpRate; self->smpRate = smpRate;
self->optFlds = optFlds; self->optFlds = optFlds;
self->isUnicast = isUnicast; self->isUnicast = isUnicast;
}
return self; return self;
} }
@ -532,11 +546,13 @@ PhyComAddress_create(uint8_t vlanPriority, uint16_t vlanId, uint16_t appId, uint
{ {
PhyComAddress* self = (PhyComAddress*) GLOBAL_MALLOC(sizeof(PhyComAddress)); PhyComAddress* self = (PhyComAddress*) GLOBAL_MALLOC(sizeof(PhyComAddress));
self->vlanPriority = vlanPriority; if (self) {
self->vlanId = vlanId; self->vlanPriority = vlanPriority;
self->appId = appId; self->vlanId = vlanId;
self->appId = appId;
memcpy(self->dstAddress, dstAddress, 6); memcpy(self->dstAddress, dstAddress, 6);
}
return self; return self;
} }
@ -573,38 +589,42 @@ DataObject_create(const char* name, ModelNode* parent, int arrayElements)
{ {
DataObject* self = (DataObject*) GLOBAL_MALLOC(sizeof(DataObject)); DataObject* self = (DataObject*) GLOBAL_MALLOC(sizeof(DataObject));
self->name = StringUtils_copyString(name); if (self) {
self->modelType = DataObjectModelType; self->name = StringUtils_copyString(name);
self->firstChild = NULL; self->modelType = DataObjectModelType;
self->parent = parent; self->firstChild = NULL;
self->sibling = NULL; self->parent = parent;
self->sibling = NULL;
self->elementCount = arrayElements; self->elementCount = arrayElements;
self->arrayIndex = -1; self->arrayIndex = -1;
if (arrayElements > 0) { if (arrayElements > 0) {
int i; int i;
for (i = 0; i < arrayElements; i++) { for (i = 0; i < arrayElements; i++) {
DataObject* arrayElement = (DataObject*) GLOBAL_MALLOC(sizeof(DataObject)); DataObject* arrayElement = (DataObject*) GLOBAL_MALLOC(sizeof(DataObject));
self->name = NULL; if (self) {
self->modelType = DataObjectModelType; self->name = NULL;
self->firstChild = NULL; self->modelType = DataObjectModelType;
self->parent = parent; self->firstChild = NULL;
self->sibling = NULL; self->parent = parent;
self->sibling = NULL;
self->elementCount = 0; self->elementCount = 0;
self->arrayIndex = i; self->arrayIndex = i;
DataObject_addChild(self, (ModelNode*) arrayElement); DataObject_addChild(self, (ModelNode*) arrayElement);
}
}
} }
}
if (parent->modelType == LogicalNodeModelType) if (parent->modelType == LogicalNodeModelType)
LogicalNode_addDataObject((LogicalNode*) parent, self); LogicalNode_addDataObject((LogicalNode*) parent, self);
else if (parent->modelType == DataObjectModelType) else if (parent->modelType == DataObjectModelType)
DataObject_addChild((DataObject*) parent, (ModelNode*) self); DataObject_addChild((DataObject*) parent, (ModelNode*) self);
}
return self; return self;
} }
@ -642,22 +662,50 @@ DataAttribute_create(const char* name, ModelNode* parent, DataAttributeType type
{ {
DataAttribute* self = (DataAttribute*) GLOBAL_MALLOC(sizeof(DataAttribute)); DataAttribute* self = (DataAttribute*) GLOBAL_MALLOC(sizeof(DataAttribute));
self->name = StringUtils_copyString(name); if (self) {
self->elementCount = arrayElements; self->name = StringUtils_copyString(name);
self->modelType = DataAttributeModelType; self->elementCount = arrayElements;
self->type = type; self->arrayIndex = -1;
self->fc = fc; self->modelType = DataAttributeModelType;
self->firstChild = NULL; self->type = type;
self->mmsValue = NULL; self->fc = fc;
self->parent = parent; self->firstChild = NULL;
self->sibling = NULL; self->mmsValue = NULL;
self->triggerOptions = triggerOptions; self->parent = parent;
self->sAddr = sAddr; self->sibling = NULL;
self->triggerOptions = triggerOptions;
if (parent->modelType == DataObjectModelType) self->sAddr = sAddr;
DataObject_addChild((DataObject*) parent, (ModelNode*) self);
else if (parent->modelType == DataAttributeModelType) if ((arrayElements > 0) && (type != IEC61850_CONSTRUCTED)) {
DataAttribute_addChild((DataAttribute*) parent, (ModelNode*) self); int i;
for (i = 0; i < arrayElements; i++) {
DataAttribute* arrayElement = (DataAttribute*) GLOBAL_MALLOC(sizeof(DataAttribute));
if (arrayElement) {
arrayElement->name = NULL;
arrayElement->elementCount = 0;
arrayElement->arrayIndex = i;
arrayElement->modelType = DataAttributeModelType;
arrayElement->type = type;
arrayElement->fc = fc;
arrayElement->firstChild = NULL;
arrayElement->mmsValue = NULL;
arrayElement->parent = parent;
arrayElement->sibling = NULL;
arrayElement->triggerOptions = triggerOptions;
arrayElement->sAddr = sAddr;
DataAttribute_addChild(self, arrayElement);
}
}
}
if (parent->modelType == DataObjectModelType)
DataObject_addChild((DataObject*) parent, (ModelNode*) self);
else if (parent->modelType == DataAttributeModelType)
DataAttribute_addChild((DataAttribute*) parent, (ModelNode*) self);
}
return self; return self;
} }
@ -696,15 +744,17 @@ DataSet_create(const char* name, LogicalNode* parent)
{ {
DataSet* self = (DataSet*) GLOBAL_MALLOC(sizeof(DataSet)); DataSet* self = (DataSet*) GLOBAL_MALLOC(sizeof(DataSet));
LogicalDevice* ld = (LogicalDevice*) parent->parent; if (self) {
LogicalDevice* ld = (LogicalDevice*) parent->parent;
self->name = StringUtils_createString(3, parent->name, "$", name); self->name = StringUtils_createString(3, parent->name, "$", name);
self->elementCount = 0; self->elementCount = 0;
self->sibling = NULL; self->sibling = NULL;
self->logicalDeviceName = ld->name; self->logicalDeviceName = ld->name;
self->fcdas = NULL; self->fcdas = NULL;
IedModel_addDataSet((IedModel*) ld->parent, self); IedModel_addDataSet((IedModel*) ld->parent, self);
}
return self; return self;
} }
@ -760,37 +810,39 @@ DataSetEntry_create(DataSet* dataSet, const char* variable, int index, const cha
{ {
DataSetEntry* self = (DataSetEntry*) GLOBAL_MALLOC(sizeof(DataSetEntry)); DataSetEntry* self = (DataSetEntry*) GLOBAL_MALLOC(sizeof(DataSetEntry));
char variableName[130]; if (self) {
char variableName[130];
StringUtils_copyStringMax(variableName, 130, variable); StringUtils_copyStringMax(variableName, 130, variable);
char* separator = strchr(variableName, '/'); char* separator = strchr(variableName, '/');
if (separator != NULL) { if (separator != NULL) {
*separator = 0; *separator = 0;
self->variableName = StringUtils_copyString(separator + 1); self->variableName = StringUtils_copyString(separator + 1);
self->logicalDeviceName = StringUtils_copyString(variableName); self->logicalDeviceName = StringUtils_copyString(variableName);
self->isLDNameDynamicallyAllocated = true; self->isLDNameDynamicallyAllocated = true;
} }
else { else {
self->variableName = StringUtils_copyString(variable); self->variableName = StringUtils_copyString(variable);
self->logicalDeviceName = dataSet->logicalDeviceName; self->logicalDeviceName = dataSet->logicalDeviceName;
self->isLDNameDynamicallyAllocated = false; self->isLDNameDynamicallyAllocated = false;
} }
if (component != NULL) if (component != NULL)
self->componentName = StringUtils_copyString(component); self->componentName = StringUtils_copyString(component);
else else
self->componentName = NULL; self->componentName = NULL;
self->index = index; self->index = index;
self->sibling = NULL; self->sibling = NULL;
self->value = NULL; self->value = NULL;
DataSet_addEntry(dataSet, self); DataSet_addEntry(dataSet, self);
}
return self; return self;
} }
@ -838,8 +890,11 @@ IedModel_destroy(IedModel* model)
while (ld != NULL) { while (ld != NULL) {
if (ld->name)
GLOBAL_FREEMEM(ld->name);
if (ld->ldName) if (ld->ldName)
GLOBAL_FREEMEM (ld->name); GLOBAL_FREEMEM (ld->ldName);
LogicalNode* ln = (LogicalNode*) ld->firstChild; LogicalNode* ln = (LogicalNode*) ld->firstChild;

@ -185,6 +185,7 @@ searchCacheForValueEx(MmsValueCache self, const char* itemId, char* parentId, in
if (elementValue) { if (elementValue) {
if ((componentId != NULL) && (componentId[0] != 0)) { if ((componentId != NULL) && (componentId[0] != 0)) {
MmsVariableSpecification* childSpec = MmsVariableSpecification_getNamedVariableRecursive(typeSpec, childId); MmsVariableSpecification* childSpec = MmsVariableSpecification_getNamedVariableRecursive(typeSpec, childId);
if (childSpec) { if (childSpec) {
@ -234,13 +235,12 @@ MmsValueCache_lookupValueEx(MmsValueCache self, const char* itemId, int idx, con
char itemIdCopy[65]; char itemIdCopy[65];
char componentIdCopyBuf[65]; char componentIdCopyBuf[65];
StringUtils_copyStringToBuffer(itemId, itemIdCopy); StringUtils_copyStringMax(itemIdCopy, 65, itemId);
char* componentIdCopy = NULL; char* componentIdCopy = NULL;
if (componentId) { if (componentId) {
componentIdCopy = StringUtils_copyStringToBuffer(componentId, componentIdCopyBuf); componentIdCopy = StringUtils_copyStringMax(componentIdCopyBuf, 65, componentId);
} }
char* parentItemId = getParentSubString(itemIdCopy); char* parentItemId = getParentSubString(itemIdCopy);

Loading…
Cancel
Save