ChimeraTK-ControlSystemAdapter-OPCUAAdapter  04.00.01
csa_namespace.c
Go to the documentation of this file.
1 /*
2  * This file is part of ChimeraTKs ControlSystem-OPC-UA-Adapter.
3  *
4  * ChimeraTKs ControlSystem-OPC-UA-Adapter is free software: you can
5  * redistribute it and/or modify it under the terms of the Lesser GNU
6  * General Public License as published by the Free Software Foundation,
7  * either version 3 of the License, or (at your option) any later version.
8  *
9  * ChimeraTKs ControlSystem-OPC-UA-Adapter is distributed in the hope
10  * that it will be useful, but WITHOUT ANY WARRANTY; without even the
11  * implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the Lesser GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Foobar. If not, see https://www.gnu.org/licenses/lgpl.html
16  *
17  * Copyright (c) 2023 Andreas Ebner <Andreas.Ebner@iosb.fraunhofer.de>
18  */
19 #include "csa_namespace.h"
20 
21 #include "csa_config.h"
22 #include "loggingLevel_type.h"
23 
24 UA_INLINE UA_StatusCode csa_namespace_add_additional_variable(UA_Server* server) {
25  UA_VariableTypeAttributes attr;
26  UA_VariableTypeAttributes_init(&attr);
27  attr.displayName = UA_LOCALIZEDTEXT("en-US", "ctkAdditionalVariable");
28  attr.description = UA_LOCALIZEDTEXT("en-US", "ctkAdditionalVariable");
29  attr.valueRank = UA_VALUERANK_ANY;
30  attr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATATYPE);
31  UA_NodeId nodeId = UA_NODEID_NUMERIC(CSA_NSID, UA_NS2ID_CTKADDITIONALVARIABLE);
32  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE);
33  // UA_NodeId typeDefinition = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEVARIABLETYPE);
34  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE);
35  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "ctkAdditionalVariable");
36  return UA_Server_addVariableTypeNode(
37  server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, UA_NODEID_NULL, attr, NULL, NULL);
38 }
39 
40 // ToDo move node add logic to seperated functions, use macros for node-id's
41 
42 UA_INLINE UA_StatusCode csa_namespace_add_LoggingLevelValues(UA_Server* server) {
43  UA_StatusCode retVal = UA_STATUSCODE_GOOD;
44  UA_VariableAttributes attr = UA_VariableAttributes_default;
45  attr.minimumSamplingInterval = 0.000000;
46  attr.userAccessLevel = 1;
47  attr.accessLevel = 1;
48  attr.valueRank = 1;
49  attr.arrayDimensionsSize = 1;
50  attr.arrayDimensions = (UA_UInt32*)UA_Array_new(1, &UA_TYPES[UA_TYPES_UINT32]);
51  if(!attr.arrayDimensions) {
52  return UA_STATUSCODE_BADOUTOFMEMORY;
53  }
54  attr.arrayDimensions[0] = 6;
55  attr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_LOCALIZEDTEXT);
56 
57  UA_LocalizedText enumValues[6] = {UA_LOCALIZEDTEXT("", "Trace"), UA_LOCALIZEDTEXT("", "Debug"),
58  UA_LOCALIZEDTEXT("", "Info"), UA_LOCALIZEDTEXT("", "Warning"), UA_LOCALIZEDTEXT("", "Error"),
59  UA_LOCALIZEDTEXT("", "Fatal")};
60  UA_Variant_setArray(&attr.value, enumValues, 6, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
61 
62  attr.displayName = UA_LOCALIZEDTEXT("", "EnumStrings");
63  attr.description = UA_LOCALIZEDTEXT("", "");
64  attr.writeMask = 0;
65  attr.userWriteMask = 0;
66 
67  UA_Server_addNode_begin(server, UA_NODECLASS_VARIABLE, UA_NODEID_STRING(1, "EnumStrings"),
68  UA_NODEID_STRING(1, "LoggingLevels"), UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY),
69  UA_QUALIFIEDNAME(0, "EnumStrings"), UA_NODEID_NUMERIC(0, UA_NS0ID_PROPERTYTYPE), (const UA_NodeAttributes*)&attr,
70  &UA_TYPES[UA_TYPES_VARIABLEATTRIBUTES], NULL, NULL);
71  // cleanup
72  UA_Array_delete(attr.arrayDimensions, 1, &UA_TYPES[UA_TYPES_UINT32]);
73  if(retVal != UA_STATUSCODE_GOOD) {
74  UA_ServerConfig* config = UA_Server_getConfig(server);
75  UA_LOG_ERROR(
76  config->logging, UA_LOGCATEGORY_USERLAND, "Failed adding variable reference. %s", UA_StatusCode_name(retVal));
77  }
78  return retVal;
79 }
80 
81 UA_INLINE void csa_namespace_add_LoggingLevelEnumType(UA_Server* server, char* enumName) {
82  UA_DataTypeAttributes attr = UA_DataTypeAttributes_default;
83  attr.description = UA_LOCALIZEDTEXT("", enumName);
84  attr.displayName = UA_LOCALIZEDTEXT("", enumName);
85  UA_StatusCode st =
86  UA_Server_addDataTypeNode(server, UA_NODEID_STRING(1, enumName), UA_NODEID_NUMERIC(0, UA_NS0ID_ENUMERATION),
87  UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE), UA_QUALIFIEDNAME(1, enumName), attr, NULL, NULL);
88  if(st != UA_STATUSCODE_GOOD) {
89  UA_ServerConfig* config = UA_Server_getConfig(server);
90  UA_LOG_ERROR(config->logging, UA_LOGCATEGORY_USERLAND, "Failed adding type node. %s", UA_StatusCode_name(st));
91  }
92  // add enum values
94  if(st != UA_STATUSCODE_GOOD) {
95  UA_ServerConfig* config = UA_Server_getConfig(server);
96  UA_LOG_ERROR(config->logging, UA_LOGCATEGORY_USERLAND, "Failed adding LoggingLevels enum values. %s",
97  UA_StatusCode_name(st));
98  }
99 }
100 
101 UA_INLINE UA_StatusCode csa_namespace_init(UA_Server* server) {
102  UA_StatusCode retval = UA_STATUSCODE_GOOD;
103  if(UA_Server_addNamespace(server, "http://adapterIM/") != 2) return UA_STATUSCODE_BADUNEXPECTEDERROR;
104 
105  retval = csa_namespace_add_additional_variable(server);
106 
107  do {
108  // Referencing node found and declared as parent: i=58/BaseObjectType using i=45/HasSubtype
109  // Node: opcua_node_objectType_t(ns=2;i=1002), 1:ctkModule
110  UA_ObjectTypeAttributes attr;
111  UA_ObjectTypeAttributes_init(&attr);
112  attr.displayName = UA_LOCALIZEDTEXT("", "ctkModule");
113  attr.description = UA_LOCALIZEDTEXT("", "");
114  UA_NodeId nodeId = UA_NODEID_NUMERIC(2, 1002);
115  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, 58);
116  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, 45);
117  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "ctkModule");
118  UA_Server_addObjectTypeNode(server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, attr, NULL, NULL);
119  } while(0);
120 
121  do {
122  // Referencing node found and declared as parent: i=62/BaseVariableType using i=45/HasSubtype
123  // Node: opcua_node_variableType_t(ns=2;i=1001), 1:ctkProcessVariable
124  UA_VariableTypeAttributes attr = UA_VariableTypeAttributes_default;
125  attr.displayName = UA_LOCALIZEDTEXT("", "ctkProcessVariable");
126  attr.description = UA_LOCALIZEDTEXT("", "");
127  attr.valueRank = UA_VALUERANK_ANY;
128  UA_NodeId nodeId = UA_NODEID_NUMERIC(2, 1001);
129  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE);
130  // UA_NodeId typeDefinition = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEVARIABLETYPE);
131  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE);
132  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "ctkProcessVariable");
133  UA_Server_addVariableTypeNode(
134  server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, UA_NODEID_NULL, attr, NULL, NULL);
135  // UA_Server_addObjectTypeNode(server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, attr, NULL, NULL);
136  // UA_Server_deleteReference(server, nodeId, UA_NODEID_NUMERIC(0, 40), true, UA_EXPANDEDNODEID_NUMERIC(0, 62), true);
137  // //remove HasTypeDefinition refs generated by addVariableNode UA_Server_addReference(server, UA_NODEID_NUMERIC(2,
138  // 1001), UA_NODEID_NUMERIC(0, UA_NS0ID_HASTYPEDEFINITION), UA_EXPANDEDNODEID_NUMERIC(0, 63), true);
139  } while(0);
140 
141  do {
142  // Referencing node found and declared as parent: ns=2;i=1002/1:ctkModule using i=47/HasComponent
143  // Node: opcua_node_object_t(ns=2;i=5001), 1:Variables
144  UA_ObjectAttributes attr;
145  UA_ObjectAttributes_init(&attr);
146  attr.displayName = UA_LOCALIZEDTEXT("", "Variables");
147  attr.description = UA_LOCALIZEDTEXT("", "");
148  UA_NodeId nodeId = UA_NODEID_NUMERIC(2, 5001);
149  UA_NodeId typeDefinition = UA_NODEID_NUMERIC(0, 61);
150  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(2, 1002);
151  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, 47);
152  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "Variables");
153  UA_Server_addObjectNode(
154  server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, typeDefinition, attr, NULL, NULL);
155  UA_Server_deleteReference(server, nodeId, UA_NODEID_NUMERIC(0, 40), true, UA_EXPANDEDNODEID_NUMERIC(0, 58),
156  true); // remove HasTypeDefinition refs generated by addObjectNode
157  // This node has the following references that can be created:
158  UA_Server_addReference(
159  server, UA_NODEID_NUMERIC(2, 5001), UA_NODEID_NUMERIC(0, 40), UA_EXPANDEDNODEID_NUMERIC(0, 61), true);
160  } while(0);
161 
162  do {
163  // Referencing node found and declared as parent: ns=2;i=1001/1:ctkProcessVariable using i=47/HasComponent
164  // Node: opcua_node_variable_t(ns=2;i=6001), 1:Description
165  UA_VariableAttributes attr = UA_VariableAttributes_default;
166  attr.displayName = UA_LOCALIZEDTEXT("", "Description");
167  attr.description = UA_LOCALIZEDTEXT("", "");
168  attr.accessLevel = 3;
169  attr.userAccessLevel = 3;
170  attr.valueRank = UA_VALUERANK_SCALAR;
171  attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
172  // UA_String *opcua_node_variable_t_ns_2_i_6001_variant_DataContents = UA_String_new();
173  //*opcua_node_variable_t_ns_2_i_6001_variant_DataContents = UA_STRING_ALLOC("");
174  UA_String opcua_node_variable_t_ns_2_i_6001_variant_DataContents = UA_STRING_ALLOC("");
175  UA_Variant_setScalar(
176  &attr.value, &opcua_node_variable_t_ns_2_i_6001_variant_DataContents, &UA_TYPES[UA_TYPES_STRING]);
177  UA_NodeId nodeId = UA_NODEID_NUMERIC(2, 6001);
178  UA_NodeId typeDefinition = UA_NODEID_NUMERIC(0, 63);
179  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(2, 1001);
180  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, 47);
181  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "Description");
182  UA_Server_addVariableNode(
183  server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, typeDefinition, attr, NULL, NULL);
184  UA_Server_deleteReference(server, nodeId, UA_NODEID_NUMERIC(0, 40), true, UA_EXPANDEDNODEID_NUMERIC(0, 62),
185  true); // remove HasTypeDefinition refs generated by addVariableNode
186  // This node has the following references that can be created:
187  UA_Server_addReference(
188  server, UA_NODEID_NUMERIC(2, 6001), UA_NODEID_NUMERIC(0, 40), UA_EXPANDEDNODEID_NUMERIC(0, 63), true);
189  } while(0);
190 
191  do {
192  // Referencing node found and declared as parent: ns=2;i=1001/1:ctkProcessVariable using i=47/HasComponent
193  // Node: opcua_node_variable_t(ns=2;i=6006), 1:EngineeringUnit
194  UA_VariableAttributes attr = UA_VariableAttributes_default;
195  attr.displayName = UA_LOCALIZEDTEXT("", "EngineeringUnit");
196  attr.description = UA_LOCALIZEDTEXT("", "");
197  attr.accessLevel = 3;
198  attr.userAccessLevel = 3;
199  attr.valueRank = UA_VALUERANK_SCALAR;
200  attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
201  // UA_String *opcua_node_variable_t_ns_2_i_6001_variant_DataContents = UA_String_new();
202  //*opcua_node_variable_t_ns_2_i_6001_variant_DataContents = UA_STRING_ALLOC("");
203  UA_String opcua_node_variable_t_ns_2_i_6001_variant_DataContents = UA_STRING_ALLOC("");
204  UA_Variant_setScalar(
205  &attr.value, &opcua_node_variable_t_ns_2_i_6001_variant_DataContents, &UA_TYPES[UA_TYPES_STRING]);
206  UA_NodeId nodeId = UA_NODEID_NUMERIC(2, 6006);
207  UA_NodeId typeDefinition = UA_NODEID_NUMERIC(0, 63);
208  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(2, 1001);
209  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, 47);
210  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "EngineeringUnit");
211  UA_Server_addVariableNode(
212  server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, typeDefinition, attr, NULL, NULL);
213  UA_Server_deleteReference(server, nodeId, UA_NODEID_NUMERIC(0, 40), true, UA_EXPANDEDNODEID_NUMERIC(0, 62),
214  true); // remove HasTypeDefinition refs generated by addVariableNode
215  // This node has the following references that can be created:
216  UA_Server_addReference(
217  server, UA_NODEID_NUMERIC(2, 6006), UA_NODEID_NUMERIC(0, 40), UA_EXPANDEDNODEID_NUMERIC(0, 63), true);
218  } while(0);
219 
220  do {
221  // Referencing node found and declared as parent: ns=2;i=1001/1:ctkProcessVariable using i=47/HasComponent
222  // Node: opcua_node_variable_t(ns=2;i=6004), 1:Name
223  UA_VariableAttributes attr = UA_VariableAttributes_default;
224  attr.displayName = UA_LOCALIZEDTEXT("", "Name");
225  attr.description = UA_LOCALIZEDTEXT("", "");
226  attr.accessLevel = 3;
227  attr.userAccessLevel = 3;
228  attr.valueRank = UA_VALUERANK_SCALAR;
229  attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
230  // UA_String *opcua_node_variable_t_ns_2_i_6004_variant_DataContents = UA_String_new();
231  //*opcua_node_variable_t_ns_2_i_6004_variant_DataContents = UA_STRING_ALLOC("");
232  UA_String opcua_node_variable_t_ns_2_i_6004_variant_DataContents = UA_STRING_ALLOC("");
233  UA_Variant_setScalar(
234  &attr.value, &opcua_node_variable_t_ns_2_i_6004_variant_DataContents, &UA_TYPES[UA_TYPES_STRING]);
235  UA_NodeId nodeId = UA_NODEID_NUMERIC(2, 6004);
236  UA_NodeId typeDefinition = UA_NODEID_NUMERIC(0, 63);
237  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(2, 1001);
238  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, 47);
239  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "Name");
240  UA_Server_addVariableNode(
241  server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, typeDefinition, attr, NULL, NULL);
242  UA_Server_deleteReference(server, nodeId, UA_NODEID_NUMERIC(0, 40), true, UA_EXPANDEDNODEID_NUMERIC(0, 62),
243  true); // remove HasTypeDefinition refs generated by addVariableNode
244  // This node has the following references that can be created:
245  UA_Server_addReference(
246  server, UA_NODEID_NUMERIC(2, 6004), UA_NODEID_NUMERIC(0, 40), UA_EXPANDEDNODEID_NUMERIC(0, 63), true);
247  } while(0);
248 
249  do {
250  // Referencing node found and declared as parent: ns=2;i=1001/1:ctkProcessVariable using i=47/HasComponent
251  // Node: opcua_node_variable_t(ns=2;i=6012), 1:Type
252  UA_VariableAttributes attr = UA_VariableAttributes_default;
253  attr.displayName = UA_LOCALIZEDTEXT("", "Type");
254  attr.description = UA_LOCALIZEDTEXT("", "");
255  attr.accessLevel = 3;
256  attr.userAccessLevel = 3;
257  attr.valueRank = UA_VALUERANK_SCALAR;
258  attr.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
259  // UA_String *opcua_node_variable_t_ns_2_i_6012_variant_DataContents = UA_String_new();
260  //*opcua_node_variable_t_ns_2_i_6012_variant_DataContents = UA_STRING_ALLOC("");
261  UA_String opcua_node_variable_t_ns_2_i_6012_variant_DataContents = UA_STRING_ALLOC("");
262  UA_Variant_setScalar(
263  &attr.value, &opcua_node_variable_t_ns_2_i_6012_variant_DataContents, &UA_TYPES[UA_TYPES_STRING]);
264  UA_NodeId nodeId = UA_NODEID_NUMERIC(2, 6012);
265  UA_NodeId typeDefinition = UA_NODEID_NUMERIC(0, 63);
266  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(2, 1001);
267  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, 47);
268  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "Type");
269  UA_Server_addVariableNode(
270  server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, typeDefinition, attr, NULL, NULL);
271  UA_Server_deleteReference(server, nodeId, UA_NODEID_NUMERIC(0, 40), true, UA_EXPANDEDNODEID_NUMERIC(0, 62),
272  true); // remove HasTypeDefinition refs generated by addVariableNode
273  // This node has the following references that can be created:
274  UA_Server_addReference(
275  server, UA_NODEID_NUMERIC(2, 6012), UA_NODEID_NUMERIC(0, 40), UA_EXPANDEDNODEID_NUMERIC(0, 63), true);
276  } while(0);
277 
278  do {
279  // Referencing node found and declared as parent: ns=2;i=1001/1:ctkProcessVariable using i=47/HasComponent
280  // Node: opcua_node_variable_t(ns=2;i=6012), 1:Validity
281  UA_VariableAttributes attr = UA_VariableAttributes_default;
282  attr.displayName = UA_LOCALIZEDTEXT("", "Validity");
283  attr.description = UA_LOCALIZEDTEXT("", "");
284  attr.accessLevel = 3;
285  attr.userAccessLevel = 3;
286  attr.valueRank = UA_VALUERANK_SCALAR;
287  attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
288  // UA_String *opcua_node_variable_t_ns_2_i_6012_variant_DataContents = UA_String_new();
289  //*opcua_node_variable_t_ns_2_i_6012_variant_DataContents = UA_STRING_ALLOC("");
290  UA_String opcua_node_variable_t_ns_2_i_6012_variant_DataContents = UA_STRING_ALLOC("");
291  UA_Variant_setScalar(
292  &attr.value, &opcua_node_variable_t_ns_2_i_6012_variant_DataContents, &UA_TYPES[UA_TYPES_INT32]);
293  UA_NodeId nodeId = UA_NODEID_NUMERIC(2, 6018);
294  UA_NodeId typeDefinition = UA_NODEID_NUMERIC(0, 63);
295  UA_NodeId parentNodeId = UA_NODEID_NUMERIC(2, 1001);
296  UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, 47);
297  UA_QualifiedName nodeName = UA_QUALIFIEDNAME(1, "Validity");
298  UA_Server_addVariableNode(
299  server, nodeId, parentNodeId, parentReferenceNodeId, nodeName, typeDefinition, attr, NULL, NULL);
300  UA_Server_deleteReference(server, nodeId, UA_NODEID_NUMERIC(0, 40), true, UA_EXPANDEDNODEID_NUMERIC(0, 62),
301  true); // remove HasTypeDefinition refs generated by addVariableNode
302  // This node has the following references that can be created:
303  UA_Server_addReference(
304  server, UA_NODEID_NUMERIC(2, 6018), UA_NODEID_NUMERIC(0, 40), UA_EXPANDEDNODEID_NUMERIC(0, 63), true);
305  } while(0);
306 
307  csa_namespace_add_LoggingLevelEnumType(server, (char*)"LoggingLevels");
308 
309  return retval;
310 }
csa_namespace_init
UA_INLINE UA_StatusCode csa_namespace_init(UA_Server *server)
Definition: csa_namespace.c:101
csa_namespace.h
csa_namespace_add_additional_variable
UA_INLINE UA_StatusCode csa_namespace_add_additional_variable(UA_Server *server)
Definition: csa_namespace.c:24
csa_namespace_add_LoggingLevelEnumType
UA_INLINE void csa_namespace_add_LoggingLevelEnumType(UA_Server *server, char *enumName)
Definition: csa_namespace.c:81
loggingLevel_type.h
csa_namespace_add_LoggingLevelValues
UA_INLINE UA_StatusCode csa_namespace_add_LoggingLevelValues(UA_Server *server)
Definition: csa_namespace.c:42