From 3d14348555b50d796176032875d67f676ad96259 Mon Sep 17 00:00:00 2001 From: Jeremie Chabod Date: Thu, 23 Nov 2023 10:54:51 +0100 Subject: [PATCH] Correction for SONARQUBE --- include/opcua_server_addrspace.h | 26 ++++++++++------- src/opcua_server.cpp | 4 +-- src/opcua_server_addrspace.cpp | 49 ++++++++++++++++---------------- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/include/opcua_server_addrspace.h b/include/opcua_server_addrspace.h index cbc5fc0..63074b9 100644 --- a/include/opcua_server_addrspace.h +++ b/include/opcua_server_addrspace.h @@ -48,15 +48,16 @@ static const SOPC_StatusCode GoodStatus = 0x00000000; static const SOPC_Byte ReadOnlyAccess = 0x01; static const SOPC_Byte ReadWriteAccess = 0x03; -typedef enum { +enum SOPC_AddressSpace_WriteEvent { we_Read_Only, we_Trigger, we_Value -} SOPC_AddressSpace_WriteEvent; +}; +using SOPC_AddressSpace_WriteEvent = enum SOPC_AddressSpace_WriteEvent; string getNodeIdName(const string &address); -struct NodeInfoCtx_t{ +struct NodeInfoCtx_t { SOPC_AddressSpace_WriteEvent mEvent; string mOpcParentAddress; string mPivotId; @@ -130,7 +131,7 @@ class CNode { protected: explicit CNode(const string& nodeName, OpcUa_NodeClass nodeClass, SOPC_StatusCode defaultStatusCode = GoodStatus); - virtual ~CNode(void); + virtual ~CNode(void); // //NOSONAR S2OPC API. private: void createReverseRef(NodeVect_t* nodes, const OpcUa_ReferenceNode& ref)const; @@ -145,7 +146,7 @@ class CNode { class CFolderNode : public CNode { public: explicit CFolderNode(const string& nodeName, const SOPC_NodeId& parent); - virtual ~CFolderNode(void) = default; + ~CFolderNode(void) override = default; }; /** @@ -154,7 +155,7 @@ class CFolderNode : public CNode { class CCommonVarNode : public CNode { public: explicit CCommonVarNode(const CVarInfo& varInfo); - virtual ~CCommonVarNode(void) = default; + ~CCommonVarNode(void) override = default; }; /** @@ -163,7 +164,7 @@ class CCommonVarNode : public CNode { class CVarNode : public CCommonVarNode { public: explicit CVarNode(const CVarInfo& varInfo, SOPC_BuiltinId sopcTypeId); - virtual ~CVarNode(void) = default; + ~CVarNode(void) override = default; private: void initializeCommonFields(const CVarInfo& varInfo); @@ -213,11 +214,16 @@ class Server_AddrSpace{ * @return The CNode of the created object */ CNode* createFolderNode(const string& nodeId, const SOPC_NodeId& parent); - void createPivotNodes(const string& label, const string& pivotId, + void createPivotNodes(const string& pivotId, const string& address, const string& pivotType); - void insertUnrefVarNode(const string& address, const string& pivotId, const std::string &name, + struct NodeInsertRef { + const string address; + const string pivotId; + const SOPC_NodeId parent; + }; + void insertUnrefVarNode(const NodeInsertRef& ref, + const std::string &name, const std::string &descr, SOPC_BuiltinId type, - const SOPC_NodeId& parent, bool isReadOnly = true, const SOPC_AddressSpace_WriteEvent& event = we_Read_Only, const string& pivotType = ""); diff --git a/src/opcua_server.cpp b/src/opcua_server.cpp index 617a402..8997295 100644 --- a/src/opcua_server.cpp +++ b/src/opcua_server.cpp @@ -789,7 +789,7 @@ writeNotificationCallback(const SOPC_CallContext* callContextPtr, context.mEvent); if (nullptr == ctrlInfo) { // //LCOV_EXCL_START => Robustness (Not reacheable) - WARNING("Missing ControlInfo for PIVOT ID[%s] ", LOGGABLE(context.mPivotId)); // //LCOV_EXCL_LINE + WARNING("Missing ControlInfo for PIVOT ID[%s] ", LOGGABLE(context.mPivotId)); return; // //LCOV_EXCL_STOP } @@ -821,7 +821,7 @@ writeNotificationCallback(const SOPC_CallContext* callContextPtr, if (!(writeValue->Value.Value.BuiltInTypeId == SOPC_Byte_Id) && writeValue->Value.Value.ArrayType == SOPC_VariantArrayType_SingleValue) { WARNING("TRIGGER for PIVOT ID[%s] does not have the expected OPC type (found type %d)", - LOGGABLE(context.mPivotId), writeValue->Value.Value.BuiltInTypeId); // //LCOV_EXCL_LINE + LOGGABLE(context.mPivotId), writeValue->Value.Value.BuiltInTypeId); return; } // //LCOV_EXCL_STOP diff --git a/src/opcua_server_addrspace.cpp b/src/opcua_server_addrspace.cpp index b1b3d51..282f32e 100644 --- a/src/opcua_server_addrspace.cpp +++ b/src/opcua_server_addrspace.cpp @@ -105,7 +105,7 @@ template void GarbageCollectorC:: reallocate(pointer* ptr, size_t oldSize, size_t newSize) { - // ASSERT(nullptr != ptr); // useless: only static calls using addresses + // Note : ptr is always not-NULL: only static calls using addresses const pointer oldPtr(*ptr); auto it = mAllocated.find(oldPtr); @@ -184,7 +184,7 @@ string getNodeIdName(const string &address) {return ::ns1 + address;} CNode:: CNode(const string& nodeName, OpcUa_NodeClass nodeClass, SOPC_StatusCode defaultStatusCode) { const string nodeId(getNodeIdName(nodeName)); - mNodeId.reset(SOPC_NodeId_FromCString(nodeId.c_str(), nodeId.length())); + mNodeId.reset(SOPC_NodeId_FromCString(nodeId.c_str(), static_cast(nodeId.length()))); memset(get(), 0, sizeof(SOPC_AddressSpace_Node)); get()->node_class = nodeClass; // Filled by child classes get()->value_status = defaultStatusCode; @@ -341,7 +341,7 @@ CNode(varInfo.mAddress, OpcUa_NodeClass_Variable) { variableNode.Value.ArrayType = SOPC_VariantArrayType_SingleValue; // Node Id - status = SOPC_NodeId_Copy(&variableNode.NodeId, &nodeId()); + SOPC_NodeId_Copy(&variableNode.NodeId, &nodeId()); // Browse name variableNode.BrowseName.NamespaceIndex = variableNode.NodeId.Namespace; @@ -386,12 +386,12 @@ createFolderNode(const string& nodeId, const SOPC_NodeId& parent) { /**************************************************************************/ void Server_AddrSpace:: -insertUnrefVarNode(const string& address, const string& pivotId, const std::string &name, - const std::string &descr, SOPC_BuiltinId type, const SOPC_NodeId& parent, +insertUnrefVarNode(const NodeInsertRef& ref, + const std::string &name, const std::string &descr, SOPC_BuiltinId type, bool isReadOnly, const SOPC_AddressSpace_WriteEvent& event, const string& pivotType) { - const string opcAddr(address + "/" + name); - const NodeInfoCtx_t context(event, getNodeIdName(address), pivotId, pivotType); - CVarInfo cVarInfo(opcAddr, name, name, descr, parent, isReadOnly); + const string opcAddr(ref.address + "/" + name); + const NodeInfoCtx_t context(event, getNodeIdName(ref.address), ref.pivotId, pivotType); + CVarInfo cVarInfo(opcAddr, name, name, descr, ref.parent, isReadOnly); CVarNode* pNode(new CVarNode(cVarInfo, type)); // //NOSONAR (deletion managed by S2OPC) DEBUG("Adding node data '%s' of type '%d' (%s)", SOPC_tools::toString(pNode->nodeId()).c_str(), type, (isReadOnly ? "RO" : "RW")); @@ -401,8 +401,7 @@ insertUnrefVarNode(const string& address, const string& pivotId, const std::stri /**************************************************************************/ void Server_AddrSpace:: -createPivotNodes(const string& label, const string& pivotId, - const string& address, const string& pivotType) { +createPivotNodes(const string& pivotId, const string& address, const string& pivotType) { const SOPC_BuiltinId sopcTypeId(SOPC_tools::toBuiltinId(pivotType)); // Parent object folder node @@ -413,27 +412,28 @@ createPivotNodes(const string& label, const string& pivotId, // Create <..>/Value, (dynamic type, based on 'pivotType') const bool readOnly(SOPC_tools::pivotTypeToReadOnly(pivotType)); const string nodeIdName(address + "/Value"); + const NodeInsertRef nodeRef{address, pivotId, parent}; if (readOnly) { /* This is a monitoring variable that can be read by an OPC Client. */ - insertUnrefVarNode(address, pivotId, "Cause", "Cause of transmission", SOPC_UInt32_Id, parent); - insertUnrefVarNode(address, pivotId, "Confirmation", "Confirmation", SOPC_Boolean_Id, parent); - insertUnrefVarNode(address, pivotId, "Source", "Source", SOPC_String_Id, parent); - insertUnrefVarNode(address, pivotId, "ComingFrom", "Origin protocol", SOPC_String_Id, parent); - insertUnrefVarNode(address, pivotId, "TmOrg", "Origin Timestamp", SOPC_String_Id, parent); - insertUnrefVarNode(address, pivotId, "TmValidity", "Timestamp validity", SOPC_String_Id, parent); - insertUnrefVarNode(address, pivotId, "DetailQuality", "Quality default details", SOPC_UInt32_Id, parent); - insertUnrefVarNode(address, pivotId, "TimeQuality", "Time default details", SOPC_UInt32_Id, parent); - insertUnrefVarNode(address, pivotId, "SecondSinceEpoch", "Timestamp", SOPC_UInt64_Id, parent); - insertUnrefVarNode(address, pivotId, "Value", string("Value of type ") + pivotType, sopcTypeId, parent); + insertUnrefVarNode(nodeRef, "Cause", "Cause of transmission", SOPC_UInt32_Id); + insertUnrefVarNode(nodeRef, "Confirmation", "Confirmation", SOPC_Boolean_Id); + insertUnrefVarNode(nodeRef, "Source", "Source", SOPC_String_Id); + insertUnrefVarNode(nodeRef, "ComingFrom", "Origin protocol", SOPC_String_Id); + insertUnrefVarNode(nodeRef, "TmOrg", "Origin Timestamp", SOPC_String_Id); + insertUnrefVarNode(nodeRef, "TmValidity", "Timestamp validity", SOPC_String_Id); + insertUnrefVarNode(nodeRef, "DetailQuality", "Quality default details", SOPC_UInt32_Id); + insertUnrefVarNode(nodeRef, "TimeQuality", "Time default details", SOPC_UInt32_Id); + insertUnrefVarNode(nodeRef, "SecondSinceEpoch", "Timestamp", SOPC_UInt64_Id); + insertUnrefVarNode(nodeRef, "Value", string("Value of type ") + pivotType, sopcTypeId); } else { /* This is a control variable that can be written by an OPC Client. It only contains Value(RW), Reply(RO) and Trigger(RW) */ - insertUnrefVarNode(address, pivotId, "Reply", "Control reply", SOPC_Boolean_Id, parent); - insertUnrefVarNode(address, pivotId, "Trigger", "Control trigger", SOPC_Byte_Id, parent, + insertUnrefVarNode(nodeRef, "Reply", "Control reply", SOPC_Boolean_Id); + insertUnrefVarNode(nodeRef, "Trigger", "Control trigger", SOPC_Byte_Id, false, we_Trigger, pivotType); - insertUnrefVarNode(address, pivotId, "Value", string("Value of type ") + pivotType, sopcTypeId, parent, + insertUnrefVarNode(nodeRef, "Value", string("Value of type ") + pivotType, sopcTypeId, false, we_Value, pivotType); mControls.emplace(pivotId, ControlInfo{getNodeInfo(address, "Trigger"), getNodeInfo(address, "Value"), @@ -466,7 +466,6 @@ Server_AddrSpace(const std::string& json) { const string label(::getString(datapoint, JSON_LABEL, JSON_DATAPOINTS)); const string pivot_id(::getString(datapoint, JSON_PIVOT_ID, JSON_DATAPOINTS)); DEBUG("Parsing DATAPOINT(%s/%s)", label.c_str(), pivot_id.c_str()); - // const string pivot_type(::getString(datapoint, JSON_PIVOT_TYPE, JSON_DATAPOINTS)); const Value::ConstArray& protocols(getArray(datapoint, JSON_PROTOCOLS, JSON_DATAPOINTS)); for (const Value& protocol : protocols) { @@ -474,7 +473,7 @@ Server_AddrSpace(const std::string& json) { const ExchangedDataC data(protocol); // throws NotAnS2opcInstance if not OPCUA protocol // Create a parent node of type Folder - createPivotNodes(label, pivot_id, data.address, data.typeId); + createPivotNodes(pivot_id, data.address, data.typeId); } catch (const ExchangedDataC::NotAnS2opcInstance&) { // Just ignore other protocols