diff --git a/src/iec61850/server/model/dynamic_model.c b/src/iec61850/server/model/dynamic_model.c index 05771a4d..61155732 100644 --- a/src/iec61850/server/model/dynamic_model.c +++ b/src/iec61850/server/model/dynamic_model.c @@ -1,7 +1,7 @@ /* * dynamic_model.c * - * Copyright 2014-2022 Michael Zillgith + * Copyright 2014-2023 Michael Zillgith * * This file is part of libIEC61850. * @@ -202,12 +202,15 @@ LogicalDevice_create(const char* name, IedModel* parent) { LogicalDevice* self = (LogicalDevice*) GLOBAL_CALLOC(1, sizeof(LogicalDevice)); - self->name = StringUtils_copyString(name); - self->modelType = LogicalDeviceModelType; - self->parent = (ModelNode*) parent; - self->sibling = NULL; + if (self) + { + self->name = StringUtils_copyString(name); + self->modelType = LogicalDeviceModelType; + self->parent = (ModelNode*) parent; + self->sibling = NULL; - IedModel_addLogicalDevice(parent, self); + IedModel_addLogicalDevice(parent, self); + } return self; } @@ -268,7 +271,6 @@ LogicalNode_getLastDataObject(LogicalNode* self) } return lastNode; - } static void @@ -296,11 +298,14 @@ Log_create(const char* name, LogicalNode* parent) { Log* self = (Log*) GLOBAL_MALLOC(sizeof(Log)); - self->name = StringUtils_copyString(name); - self->parent = parent; - self->sibling = NULL; + if (self) + { + self->name = StringUtils_copyString(name); + self->parent = parent; + self->sibling = NULL; - LogicalNode_addLog(parent, self); + LogicalNode_addLog(parent, self); + } return self; } @@ -319,26 +324,29 @@ LogControlBlock_create(const char* name, LogicalNode* parent, const char* dataSe { LogControlBlock* self = (LogControlBlock*) GLOBAL_MALLOC(sizeof(LogControlBlock)); - self->name = StringUtils_copyString(name); - self->parent = parent; - self->sibling = NULL; + if (self) + { + self->name = StringUtils_copyString(name); + self->parent = parent; + self->sibling = NULL; - if (dataSetName) - self->dataSetName = StringUtils_copyString(dataSetName); - else - self->dataSetName = NULL; + if (dataSetName) + self->dataSetName = StringUtils_copyString(dataSetName); + else + self->dataSetName = NULL; - if (logRef) - self->logRef = StringUtils_copyString(logRef); - else - self->logRef = NULL; + if (logRef) + self->logRef = StringUtils_copyString(logRef); + else + self->logRef = NULL; - self->trgOps = trgOps; - self->intPeriod = intPeriod; - self->logEna = logEna; - self->reasonCode = reasonCode; + self->trgOps = trgOps; + self->intPeriod = intPeriod; + self->logEna = logEna; + self->reasonCode = reasonCode; - LogicalNode_addLogControlBlock(parent, self); + LogicalNode_addLogControlBlock(parent, self); + } return self; } @@ -357,30 +365,33 @@ ReportControlBlock_create(const char* name, LogicalNode* parent, const char* rpt { ReportControlBlock* self = (ReportControlBlock*) GLOBAL_MALLOC(sizeof(ReportControlBlock)); - self->name = StringUtils_copyString(name); - self->parent = parent; + if (self) + { + self->name = StringUtils_copyString(name); + self->parent = parent; - if (rptId) - self->rptId = StringUtils_copyString(rptId); - else - self->rptId = NULL; + if (rptId) + self->rptId = StringUtils_copyString(rptId); + else + self->rptId = NULL; - self->buffered = isBuffered; + self->buffered = isBuffered; - if (dataSetName) - self->dataSetName = StringUtils_copyString(dataSetName); - else - self->dataSetName = NULL; + if (dataSetName) + self->dataSetName = StringUtils_copyString(dataSetName); + else + self->dataSetName = NULL; - self->confRef = confRef; - self->trgOps = trgOps; - self->options = options; - self->bufferTime = bufTm; - self->intPeriod = intgPd; - self->sibling = NULL; - self->clientReservation[0] = 0; /* no pre-configured client */ + self->confRef = confRef; + self->trgOps = trgOps; + self->options = options; + self->bufferTime = bufTm; + self->intPeriod = intgPd; + self->sibling = NULL; + self->clientReservation[0] = 0; /* no pre-configured client */ - LogicalNode_addReportControlBlock(parent, self); + LogicalNode_addReportControlBlock(parent, self); + } return self; } @@ -436,13 +447,16 @@ SettingGroupControlBlock_create(LogicalNode* parent, uint8_t actSG, uint8_t numO SettingGroupControlBlock* self = (SettingGroupControlBlock*) GLOBAL_MALLOC(sizeof(SettingGroupControlBlock)); - self->parent = parent; - self->actSG = actSG; - self->numOfSGs = numOfSGs; - self->sibling = NULL; - self->editSG = 0; + if (self) + { + self->parent = parent; + self->actSG = actSG; + self->numOfSGs = numOfSGs; + self->sibling = NULL; + self->editSG = 0; - LogicalNode_addSettingGroupControlBlock(parent, self); + LogicalNode_addSettingGroupControlBlock(parent, self); + } return self; } @@ -462,30 +476,33 @@ GSEControlBlock_create(const char* name, LogicalNode* parent, const char* appId, { GSEControlBlock* self = (GSEControlBlock*) GLOBAL_MALLOC(sizeof(GSEControlBlock)); - self->name = StringUtils_copyString(name); - self->parent = parent; + if (self) + { + self->name = StringUtils_copyString(name); + self->parent = parent; - if (appId) - self->appId = StringUtils_copyString(appId); - else - self->appId = NULL; + if (appId) + self->appId = StringUtils_copyString(appId); + else + self->appId = NULL; - if (dataSet) - self->dataSetName = StringUtils_copyString(dataSet); - else - self->dataSetName = NULL; + if (dataSet) + self->dataSetName = StringUtils_copyString(dataSet); + else + self->dataSetName = NULL; - self->confRev = confRef; - self->fixedOffs = fixedOffs; - self->minTime = minTime; - self->maxTime = maxTime; + self->confRev = confRef; + self->fixedOffs = fixedOffs; + self->minTime = minTime; + self->maxTime = maxTime; - self->address = NULL; + self->address = NULL; - self->sibling = NULL; + self->sibling = NULL; - if (parent != NULL) - LogicalNode_addGSEControlBlock(parent, self); + if (parent != NULL) + LogicalNode_addGSEControlBlock(parent, self); + } return self; } @@ -504,29 +521,32 @@ SVControlBlock_create(const char* name, LogicalNode* parent, const char* svID, c { SVControlBlock* self = (SVControlBlock*) GLOBAL_MALLOC(sizeof(SVControlBlock)); - self->name = StringUtils_copyString(name); - self->parent = parent; + if (self) + { + 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) - self->dataSetName = StringUtils_copyString(dataSet); - else - self->dataSetName = NULL; + if (dataSet) + self->dataSetName = StringUtils_copyString(dataSet); + else + self->dataSetName = NULL; - self->confRev = confRev; + self->confRev = confRev; - self->smpMod = smpMod; - self->smpRate = smpRate; + self->smpMod = smpMod; + self->smpRate = smpRate; - self->optFlds = optFlds; - self->isUnicast = isUnicast; + self->optFlds = optFlds; + self->isUnicast = isUnicast; - self->dstAddress = NULL; - self->sibling = NULL; + self->dstAddress = NULL; + self->sibling = NULL; - if (parent) - LogicalNode_addSMVControlBlock(parent, self); + if (parent) + LogicalNode_addSMVControlBlock(parent, self); + } return self; } @@ -548,11 +568,14 @@ PhyComAddress_create(uint8_t vlanPriority, uint16_t vlanId, uint16_t appId, uint { PhyComAddress* self = (PhyComAddress*) GLOBAL_MALLOC(sizeof(PhyComAddress)); - self->vlanPriority = vlanPriority; - self->vlanId = vlanId; - self->appId = appId; + if (self) + { + self->vlanPriority = vlanPriority; + self->vlanId = vlanId; + self->appId = appId; - memcpy(self->dstAddress, dstAddress, 6); + memcpy(self->dstAddress, dstAddress, 6); + } return self; } @@ -589,17 +612,20 @@ DataObject_create(const char* name, ModelNode* parent, int arrayElements) { DataObject* self = (DataObject*) GLOBAL_MALLOC(sizeof(DataObject)); - self->name = StringUtils_copyString(name); - self->modelType = DataObjectModelType; - self->elementCount = arrayElements; - self->firstChild = NULL; - self->parent = parent; - self->sibling = NULL; - - if (parent->modelType == LogicalNodeModelType) - LogicalNode_addDataObject((LogicalNode*) parent, self); - else if (parent->modelType == DataObjectModelType) - DataObject_addChild((DataObject*) parent, (ModelNode*) self); + if (self) + { + self->name = StringUtils_copyString(name); + self->modelType = DataObjectModelType; + self->elementCount = arrayElements; + self->firstChild = NULL; + self->parent = parent; + self->sibling = NULL; + + if (parent->modelType == LogicalNodeModelType) + LogicalNode_addDataObject((LogicalNode*) parent, self); + else if (parent->modelType == DataObjectModelType) + DataObject_addChild((DataObject*) parent, (ModelNode*) self); + } return self; } @@ -637,22 +663,25 @@ DataAttribute_create(const char* name, ModelNode* parent, DataAttributeType type { DataAttribute* self = (DataAttribute*) GLOBAL_MALLOC(sizeof(DataAttribute)); - self->name = StringUtils_copyString(name); - self->elementCount = arrayElements; - self->modelType = DataAttributeModelType; - self->type = type; - self->fc = fc; - self->firstChild = NULL; - self->mmsValue = NULL; - self->parent = parent; - self->sibling = NULL; - self->triggerOptions = triggerOptions; - self->sAddr = sAddr; - - if (parent->modelType == DataObjectModelType) - DataObject_addChild((DataObject*) parent, (ModelNode*) self); - else if (parent->modelType == DataAttributeModelType) - DataAttribute_addChild((DataAttribute*) parent, (ModelNode*) self); + if (self) + { + self->name = StringUtils_copyString(name); + self->elementCount = arrayElements; + self->modelType = DataAttributeModelType; + self->type = type; + self->fc = fc; + self->firstChild = NULL; + self->mmsValue = NULL; + self->parent = parent; + self->sibling = NULL; + self->triggerOptions = triggerOptions; + self->sAddr = sAddr; + + if (parent->modelType == DataObjectModelType) + DataObject_addChild((DataObject*) parent, (ModelNode*) self); + else if (parent->modelType == DataAttributeModelType) + DataAttribute_addChild((DataAttribute*) parent, (ModelNode*) self); + } return self; } @@ -691,15 +720,18 @@ DataSet_create(const char* name, LogicalNode* parent) { 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->elementCount = 0; - self->sibling = NULL; - self->logicalDeviceName = ld->name; - self->fcdas = NULL; + self->name = StringUtils_createString(3, parent->name, "$", name); + self->elementCount = 0; + self->sibling = NULL; + self->logicalDeviceName = ld->name; + self->fcdas = NULL; - IedModel_addDataSet((IedModel*) ld->parent, self); + IedModel_addDataSet((IedModel*) ld->parent, self); + } return self; } @@ -755,37 +787,40 @@ DataSetEntry_create(DataSet* dataSet, const char* variable, int index, const cha { 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) { - *separator = 0; + if (separator != NULL) { + *separator = 0; - self->variableName = StringUtils_copyString(separator + 1); - self->logicalDeviceName = StringUtils_copyString(variableName); - self->isLDNameDynamicallyAllocated = true; - } - else { - self->variableName = StringUtils_copyString(variable); - self->logicalDeviceName = dataSet->logicalDeviceName; - self->isLDNameDynamicallyAllocated = false; - } + self->variableName = StringUtils_copyString(separator + 1); + self->logicalDeviceName = StringUtils_copyString(variableName); + self->isLDNameDynamicallyAllocated = true; + } + else { + self->variableName = StringUtils_copyString(variable); + self->logicalDeviceName = dataSet->logicalDeviceName; + self->isLDNameDynamicallyAllocated = false; + } - if (component != NULL) - self->componentName = StringUtils_copyString(component); - else - self->componentName = NULL; + if (component != NULL) + self->componentName = StringUtils_copyString(component); + else + 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; }