Skip to content

Commit

Permalink
remove raw pointers for RData and action info in opcua
Browse files Browse the repository at this point in the history
  • Loading branch information
cochicde authored and azoitl committed Jun 4, 2024
1 parent 5812217 commit 848b10f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 37 deletions.
9 changes: 4 additions & 5 deletions src/com/opc_ua/opcua_action_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ bool CActionInfo::isRemote() const {
return (!mEndpoint.empty());
}

CActionInfo* CActionInfo::getActionInfoFromParams(const char *paParams, COPC_UA_Layer &paLayer) {
CActionInfo *retVal = nullptr;
std::unique_ptr<CActionInfo> CActionInfo::getActionInfoFromParams(const char *paParams, COPC_UA_Layer &paLayer) {
std::unique_ptr<CActionInfo> retVal = nullptr;
CParameterParser mainParser(paParams, ';');
size_t amountOfParameters = mainParser.parseParameters();

Expand All @@ -44,9 +44,9 @@ CActionInfo* CActionInfo::getActionInfoFromParams(const char *paParams, COPC_UA_
}

if(CActionInfo::eCreateMethod == action) {
retVal = new CLocalMethodInfo(paLayer, endpoint);
retVal.reset(new CLocalMethodInfo(paLayer, endpoint));
} else {
retVal = new CActionInfo(paLayer, action, endpoint);
retVal.reset(new CActionInfo(paLayer, action, endpoint));
}

bool somethingFailed = false;
Expand All @@ -62,7 +62,6 @@ CActionInfo* CActionInfo::getActionInfoFromParams(const char *paParams, COPC_UA_
}

if(somethingFailed) {
delete retVal;
retVal = nullptr;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/opc_ua/opcua_action_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ class CActionInfo {
* @param paParams Parameters set in the ID of the FB (string contained in the square brackets of opc_ua[...])
* @param paLayer The layer that creates and executes the action
* @param paTypes A list of type converters of the connections of the FB of the action (SDs/RDs)
* @return
* @return The new created action info if the parameters were valid, nullptr otherwise
*/
static CActionInfo* getActionInfoFromParams(const char *paParams, COPC_UA_Layer &paLayer);
static std::unique_ptr<CActionInfo> getActionInfoFromParams(const char *paParams, COPC_UA_Layer &paLayer);

/**
* Retrieves the array of CIEC_ANY to be sent
Expand Down
21 changes: 7 additions & 14 deletions src/com/opc_ua/opcua_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
using namespace forte::com_infra;

COPC_UA_Layer::COPC_UA_Layer(CComLayer *paUpperLayer, CBaseCommFB *paComFB) :
CComLayer(paUpperLayer, paComFB), mInterruptResp(e_Nothing), mHandler(nullptr), mActionInfo(nullptr), mDataAlreadyPresent(false), mIsObjectNodeStruct(false), mRDBuffer(nullptr) {
CComLayer(paUpperLayer, paComFB), mInterruptResp(e_Nothing), mHandler(nullptr), mDataAlreadyPresent(false), mIsObjectNodeStruct(false) {
}

COPC_UA_Layer::~COPC_UA_Layer() = default;
Expand All @@ -49,9 +49,8 @@ EComResponse COPC_UA_Layer::openConnection(char *paLayerParameter) {
if(UA_STATUSCODE_GOOD == mHandler->initializeAction(*mActionInfo)) {
CCriticalRegion criticalRegion(mRDBufferMutex);
response = e_InitOk;
mRDBuffer = new CIEC_ANY*[getCommFB()->getNumRD()];
for(size_t i = 0; i < getCommFB()->getNumRD(); ++i) {
mRDBuffer[i] = getCommFB()->getRDs()[i]->clone(nullptr);
mRDBuffer.emplace_back(getCommFB()->getRDs()[i]->clone(nullptr));
}
}
} else {
Expand Down Expand Up @@ -83,22 +82,16 @@ void COPC_UA_Layer::closeConnection() {
if(mHandler) {
CCriticalRegion criticalRegion(mRDBufferMutex);
mHandler->uninitializeAction(*mActionInfo);
delete mActionInfo;
mActionInfo.reset();

if(mIsObjectNodeStruct) {
mStructObjectHelper->uninitializeStruct();
}
mHandler = nullptr;
if(mRDBuffer) {
if(mIsObjectNodeStruct) {
COPC_UA_ObjectStruct_Helper::deleteRDBufferEntries(*getCommFB(), mRDBuffer);
} else {
for (size_t i = 0; i < getCommFB()->getNumRD(); ++i) {
delete mRDBuffer[i];
}
}
delete[] mRDBuffer;
mRDBuffer = nullptr;
if(mIsObjectNodeStruct) {
COPC_UA_ObjectStruct_Helper::deleteRDBufferEntries(*getCommFB(), mRDBuffer);
} else {
mRDBuffer.clear();
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/com/opc_ua/opcua_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class COPC_UA_Layer : public forte::com_infra::CComLayer {
/**
* Information about the action that's returned from the CActionInfo factory, and which is later passed to the handler when initializing, executing and uninitializing the action
*/
CActionInfo *mActionInfo;
std::unique_ptr<CActionInfo> mActionInfo;

/**
* Called when INIT is triggered in the FB and QI is set to true
Expand Down Expand Up @@ -151,9 +151,9 @@ class COPC_UA_Layer : public forte::com_infra::CComLayer {
CStringDictionary::TStringId getLocalPortNameId(int paPortIndex, bool paIsSD) const;

/**
* Array of ANY pointers used as buffer to store the received data
* List of ANY pointers used as buffer to store the received data
*/
CIEC_ANY **mRDBuffer;
std::vector<std::unique_ptr<CIEC_ANY>> mRDBuffer;

/**
* Mutex to access the mRDBuffer
Expand Down
16 changes: 6 additions & 10 deletions src/com/opc_ua/opcua_objectstruct_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,33 +222,29 @@ int COPC_UA_ObjectStruct_Helper::getRDBufferIndexFromNodeId(const UA_NodeId *paN
return -1;
}

void COPC_UA_ObjectStruct_Helper::setMemberValues(CIEC_ANY** paRDs, CIEC_ANY **paRDBuffer) {
void COPC_UA_ObjectStruct_Helper::setMemberValues(CIEC_ANY** paRDs, const std::vector<std::unique_ptr<CIEC_ANY>>& paRDBuffer) {
// TODO implement layer to handle more than 1 struct
CIEC_STRUCT& structType = static_cast<CIEC_STRUCT&>(paRDs[0]->unwrap());
for(size_t i = 0; i < structType.getStructSize(); i++) {
structType.getMember(i)->setValue(*paRDBuffer[i]);
}
}

CIEC_ANY **COPC_UA_ObjectStruct_Helper::initializeRDBuffer() {
std::vector<std::unique_ptr<CIEC_ANY>> COPC_UA_ObjectStruct_Helper::initializeRDBuffer() {
// TODO implement layer to handle more than 1 struct
CIEC_ANY** rds = mLayer.getCommFB()->getRDs();
CIEC_STRUCT& structType = static_cast<CIEC_STRUCT&>(rds[0]->unwrap());
const size_t structSize = structType.getStructSize();
CIEC_ANY **RDBuffer = new CIEC_ANY*[structSize];
std::vector<std::unique_ptr<CIEC_ANY>> RDBuffer;;
for(size_t i = 0; i < structSize; i++) {
RDBuffer[i] = structType.getMember(i)->clone(nullptr);
RDBuffer.emplace_back(structType.getMember(i)->clone(nullptr));
}
return RDBuffer;
}

void COPC_UA_ObjectStruct_Helper::deleteRDBufferEntries(forte::com_infra::CBaseCommFB &paCommFB, CIEC_ANY **paRDBuffer) {
void COPC_UA_ObjectStruct_Helper::deleteRDBufferEntries(forte::com_infra::CBaseCommFB &paCommFB, std::vector<std::unique_ptr<CIEC_ANY>>& paRDBuffer) {
if(paCommFB.getComServiceType() == e_Subscriber) {
CIEC_ANY** rds = paCommFB.getRDs();
CIEC_STRUCT& structType = static_cast<CIEC_STRUCT&>(rds[0]->unwrap());
for(size_t i = 0; i < structType.getStructSize(); i++) {
delete paRDBuffer[i];
}
paRDBuffer.clear();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/com/opc_ua/opcua_objectstruct_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ class COPC_UA_ObjectStruct_Helper {
* @param paRDs the array of data pointers to be sent
* @param paRDBuffer The buffer for the data
*/
static void setMemberValues(CIEC_ANY** paRDs, CIEC_ANY **paRDBuffer);
static void setMemberValues(CIEC_ANY** paRDs, const std::vector<std::unique_ptr<CIEC_ANY>>& paRDBuffer);

/**
* Initialize RDBuffer for Object Structs
* @return The initialized buffer
*/
CIEC_ANY **initializeRDBuffer();
std::vector<std::unique_ptr<CIEC_ANY>> initializeRDBuffer();

/**
* Delete all entries of the RDBuffer
*
* @param paCommFB The comm fb for which the rdbuffer was created
* @param paRDBuffer The buffer to be uninitialized
*/
static void deleteRDBufferEntries(forte::com_infra::CBaseCommFB &paCommFB, CIEC_ANY **paRDBuffer);
static void deleteRDBufferEntries(forte::com_infra::CBaseCommFB &paCommFB, std::vector<std::unique_ptr<CIEC_ANY>>& paRDBuffer);

/**
* Check if Data Connection is a Struct Type
Expand Down

0 comments on commit 848b10f

Please sign in to comment.