Skip to content

Commit

Permalink
Correction for SONARQUBE
Browse files Browse the repository at this point in the history
  • Loading branch information
jchabod committed Nov 23, 2023
1 parent ddc4e5e commit 3d14348
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
26 changes: 16 additions & 10 deletions include/opcua_server_addrspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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 = "");
Expand Down
4 changes: 2 additions & 2 deletions src/opcua_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
49 changes: 24 additions & 25 deletions src/opcua_server_addrspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ template<typename T>
void
GarbageCollectorC<T>::
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);

Expand Down Expand Up @@ -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<int32_t>(nodeId.length())));
memset(get(), 0, sizeof(SOPC_AddressSpace_Node));
get()->node_class = nodeClass; // Filled by child classes
get()->value_status = defaultStatusCode;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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"));
Expand All @@ -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
Expand All @@ -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"),
Expand Down Expand Up @@ -466,15 +466,14 @@ 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) {
try {
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
Expand Down

0 comments on commit 3d14348

Please sign in to comment.