server_ctt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #ifdef _MSC_VER
  4. #define _CRT_SECURE_NO_WARNINGS /* disable fopen deprication warning in msvs */
  5. #endif
  6. #ifdef UA_ENABLE_AMALGAMATION
  7. # include <open62541.h>
  8. #else
  9. # include <ua_server.h>
  10. # include <ua_config_default.h>
  11. # include <ua_log_stdout.h>
  12. #endif
  13. # include "common.h"
  14. #include <signal.h>
  15. /* This server is configured to the Compliance Testing Tools (CTT) against. The
  16. * corresponding CTT configuration is available at
  17. * https://github.com/open62541/open62541-ctt */
  18. static const UA_NodeId baseDataVariableType = {0, UA_NODEIDTYPE_NUMERIC, {UA_NS0ID_BASEDATAVARIABLETYPE}};
  19. static const UA_NodeId accessDenied = {1, UA_NODEIDTYPE_NUMERIC, {1337}};
  20. /* Custom AccessControl policy that disallows access to one specific node */
  21. static UA_Byte
  22. getUserAccessLevel_disallowSpecific(UA_Server *server, UA_AccessControl *ac,
  23. const UA_NodeId *sessionId, void *sessionContext,
  24. const UA_NodeId *nodeId, void *nodeContext) {
  25. if(UA_NodeId_equal(nodeId, &accessDenied))
  26. return 0x00;
  27. return 0xFF;
  28. }
  29. /* Datasource Example */
  30. static UA_StatusCode
  31. readTimeData(UA_Server *server,
  32. const UA_NodeId *sessionId, void *sessionContext,
  33. const UA_NodeId *nodeId, void *nodeContext,
  34. UA_Boolean sourceTimeStamp,
  35. const UA_NumericRange *range, UA_DataValue *value) {
  36. if(range) {
  37. value->hasStatus = true;
  38. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  39. return UA_STATUSCODE_GOOD;
  40. }
  41. UA_DateTime currentTime = UA_DateTime_now();
  42. UA_Variant_setScalarCopy(&value->value, &currentTime, &UA_TYPES[UA_TYPES_DATETIME]);
  43. value->hasValue = true;
  44. if(sourceTimeStamp) {
  45. value->hasSourceTimestamp = true;
  46. value->sourceTimestamp = currentTime;
  47. }
  48. return UA_STATUSCODE_GOOD;
  49. }
  50. /* Method Node Example */
  51. #ifdef UA_ENABLE_METHODCALLS
  52. static UA_StatusCode
  53. helloWorld(UA_Server *server,
  54. const UA_NodeId *sessionId, void *sessionContext,
  55. const UA_NodeId *methodId, void *methodContext,
  56. const UA_NodeId *objectId, void *objectContext,
  57. size_t inputSize, const UA_Variant *input,
  58. size_t outputSize, UA_Variant *output) {
  59. /* input is a scalar string (checked by the server) */
  60. UA_String *name = (UA_String *)input[0].data;
  61. UA_String hello = UA_STRING("Hello ");
  62. UA_String greet;
  63. greet.length = hello.length + name->length;
  64. greet.data = (UA_Byte *)UA_malloc(greet.length);
  65. memcpy(greet.data, hello.data, hello.length);
  66. memcpy(greet.data + hello.length, name->data, name->length);
  67. UA_Variant_setScalarCopy(output, &greet, &UA_TYPES[UA_TYPES_STRING]);
  68. UA_String_clear(&greet);
  69. return UA_STATUSCODE_GOOD;
  70. }
  71. static UA_StatusCode
  72. noargMethod(UA_Server *server,
  73. const UA_NodeId *sessionId, void *sessionContext,
  74. const UA_NodeId *methodId, void *methodContext,
  75. const UA_NodeId *objectId, void *objectContext,
  76. size_t inputSize, const UA_Variant *input,
  77. size_t outputSize, UA_Variant *output) {
  78. return UA_STATUSCODE_GOOD;
  79. }
  80. static UA_StatusCode
  81. outargMethod(UA_Server *server,
  82. const UA_NodeId *sessionId, void *sessionContext,
  83. const UA_NodeId *methodId, void *methodContext,
  84. const UA_NodeId *objectId, void *objectContext,
  85. size_t inputSize, const UA_Variant *input,
  86. size_t outputSize, UA_Variant *output) {
  87. UA_Int32 out = 42;
  88. UA_Variant_setScalarCopy(output, &out, &UA_TYPES[UA_TYPES_INT32]);
  89. return UA_STATUSCODE_GOOD;
  90. }
  91. #endif
  92. static void
  93. setInformationModel(UA_Server *server) {
  94. /* add a static variable node to the server */
  95. UA_VariableAttributes myVar = UA_VariableAttributes_default;
  96. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer");
  97. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
  98. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  99. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  100. myVar.valueRank = UA_VALUERANK_SCALAR;
  101. UA_Int32 myInteger = 42;
  102. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  103. const UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  104. const UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  105. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  106. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  107. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  108. myIntegerName, baseDataVariableType, myVar, NULL, NULL);
  109. /* add a static variable that is readable but not writable*/
  110. myVar = UA_VariableAttributes_default;
  111. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer - not readable");
  112. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer - not readable");
  113. myVar.accessLevel = UA_ACCESSLEVELMASK_WRITE;
  114. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  115. myVar.valueRank = UA_VALUERANK_SCALAR;
  116. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  117. const UA_QualifiedName myInteger2Name = UA_QUALIFIEDNAME(1, "the answer - not readable");
  118. const UA_NodeId myInteger2NodeId = UA_NODEID_STRING(1, "the.answer.no.read");
  119. UA_Server_addVariableNode(server, myInteger2NodeId, parentNodeId, parentReferenceNodeId,
  120. myInteger2Name, baseDataVariableType, myVar, NULL, NULL);
  121. /* add a variable that is not readable or writable for the current user */
  122. myVar = UA_VariableAttributes_default;
  123. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer - not current user");
  124. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer - not current user");
  125. myVar.accessLevel = UA_ACCESSLEVELMASK_WRITE;
  126. myVar.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  127. myVar.valueRank = UA_VALUERANK_SCALAR;
  128. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  129. UA_Variant_setScalar(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  130. const UA_QualifiedName accessDeniedName = UA_QUALIFIEDNAME(1, "the answer - not current user");
  131. UA_Server_addVariableNode(server, accessDenied, parentNodeId, parentReferenceNodeId,
  132. accessDeniedName, baseDataVariableType, myVar, NULL, NULL);
  133. /* add a variable with the datetime data source */
  134. UA_DataSource dateDataSource;
  135. dateDataSource.read = readTimeData;
  136. dateDataSource.write = NULL;
  137. UA_VariableAttributes v_attr = UA_VariableAttributes_default;
  138. v_attr.description = UA_LOCALIZEDTEXT("en-US", "current time");
  139. v_attr.displayName = UA_LOCALIZEDTEXT("en-US", "current time");
  140. v_attr.accessLevel = UA_ACCESSLEVELMASK_READ;
  141. v_attr.dataType = UA_TYPES[UA_TYPES_DATETIME].typeId;
  142. v_attr.valueRank = UA_VALUERANK_SCALAR;
  143. const UA_QualifiedName dateName = UA_QUALIFIEDNAME(1, "current time");
  144. UA_Server_addDataSourceVariableNode(server, UA_NODEID_NUMERIC(1, 2345),
  145. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  146. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), dateName,
  147. baseDataVariableType, v_attr, dateDataSource, NULL, NULL);
  148. /* add a bytestring variable with some content */
  149. myVar = UA_VariableAttributes_default;
  150. myVar.description = UA_LOCALIZEDTEXT("", "");
  151. myVar.displayName = UA_LOCALIZEDTEXT("", "example bytestring");
  152. myVar.dataType = UA_TYPES[UA_TYPES_BYTESTRING].typeId;
  153. myVar.valueRank = UA_VALUERANK_SCALAR;
  154. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  155. UA_ByteString myByteString = UA_BYTESTRING("test123\0test123");
  156. UA_Variant_setScalar(&myVar.value, &myByteString, &UA_TYPES[UA_TYPES_BYTESTRING]);
  157. const UA_QualifiedName byteStringName = UA_QUALIFIEDNAME(1, "example bytestring");
  158. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "myByteString"),
  159. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  160. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), byteStringName,
  161. baseDataVariableType, myVar, NULL, NULL);
  162. /* Add HelloWorld method to the server */
  163. #ifdef UA_ENABLE_METHODCALLS
  164. /* Method with IO Arguments */
  165. UA_Argument inputArguments;
  166. UA_Argument_init(&inputArguments);
  167. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  168. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Say your name");
  169. inputArguments.name = UA_STRING("Name");
  170. inputArguments.valueRank = UA_VALUERANK_SCALAR; /* scalar argument */
  171. UA_Argument outputArguments;
  172. UA_Argument_init(&outputArguments);
  173. outputArguments.arrayDimensionsSize = 0;
  174. outputArguments.arrayDimensions = NULL;
  175. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  176. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Receive a greeting");
  177. outputArguments.name = UA_STRING("greeting");
  178. outputArguments.valueRank = UA_VALUERANK_SCALAR;
  179. UA_MethodAttributes addmethodattributes = UA_MethodAttributes_default;
  180. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "Hello World");
  181. addmethodattributes.executable = true;
  182. addmethodattributes.userExecutable = true;
  183. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
  184. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  185. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  186. UA_QUALIFIEDNAME(1, "hello_world"), addmethodattributes,
  187. &helloWorld, /* callback of the method node */
  188. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  189. #endif
  190. /* Add folders for demo information model */
  191. #define DEMOID 50000
  192. #define SCALARID 50001
  193. #define ARRAYID 50002
  194. #define MATRIXID 50003
  195. #define DEPTHID 50004
  196. UA_ObjectAttributes object_attr = UA_ObjectAttributes_default;
  197. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Demo");
  198. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Demo");
  199. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEMOID),
  200. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  201. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Demo"),
  202. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  203. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Scalar");
  204. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Scalar");
  205. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALARID),
  206. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  207. UA_QUALIFIEDNAME(1, "Scalar"),
  208. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  209. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Array");
  210. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Array");
  211. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, ARRAYID),
  212. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  213. UA_QUALIFIEDNAME(1, "Array"),
  214. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  215. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Matrix");
  216. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Matrix");
  217. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, MATRIXID), UA_NODEID_NUMERIC(1, DEMOID),
  218. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Matrix"),
  219. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  220. /* Fill demo nodes for each type*/
  221. UA_UInt32 matrixDims[2] = {3, 3};
  222. UA_UInt32 id = 51000; // running id in namespace 0
  223. for(UA_UInt32 type = 0; type < UA_TYPES_DIAGNOSTICINFO; type++) {
  224. if(type == UA_TYPES_VARIANT || type == UA_TYPES_DIAGNOSTICINFO)
  225. continue;
  226. UA_VariableAttributes attr = UA_VariableAttributes_default;
  227. attr.dataType = UA_TYPES[type].typeId;
  228. #ifndef UA_ENABLE_TYPENAMES
  229. char name[15];
  230. UA_snprintf(name, 15, "%02d", type);
  231. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  232. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  233. #else
  234. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", UA_TYPES[type].typeName);
  235. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME_ALLOC(1, UA_TYPES[type].typeName);
  236. #endif
  237. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  238. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  239. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  240. /* add a scalar node for every built-in type */
  241. attr.valueRank = UA_VALUERANK_SCALAR;
  242. void *value = UA_new(&UA_TYPES[type]);
  243. UA_Variant_setScalar(&attr.value, value, &UA_TYPES[type]);
  244. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id),
  245. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  246. qualifiedName, baseDataVariableType, attr, NULL, NULL);
  247. UA_Variant_clear(&attr.value);
  248. /* add an array node for every built-in type */
  249. UA_UInt32 arrayDims = 0;
  250. attr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  251. attr.arrayDimensions = &arrayDims;
  252. attr.arrayDimensionsSize = 1;
  253. UA_Variant_setArray(&attr.value, UA_Array_new(10, &UA_TYPES[type]), 10, &UA_TYPES[type]);
  254. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, ARRAYID),
  255. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  256. baseDataVariableType, attr, NULL, NULL);
  257. UA_Variant_clear(&attr.value);
  258. /* add an matrix node for every built-in type */
  259. attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
  260. attr.arrayDimensions = matrixDims;
  261. attr.arrayDimensionsSize = 2;
  262. void *myMultiArray = UA_Array_new(9, &UA_TYPES[type]);
  263. attr.value.arrayDimensions = (UA_UInt32 *)UA_Array_new(2, &UA_TYPES[UA_TYPES_INT32]);
  264. attr.value.arrayDimensions[0] = 3;
  265. attr.value.arrayDimensions[1] = 3;
  266. attr.value.arrayDimensionsSize = 2;
  267. attr.value.arrayLength = 9;
  268. attr.value.data = myMultiArray;
  269. attr.value.type = &UA_TYPES[type];
  270. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, MATRIXID),
  271. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  272. baseDataVariableType, attr, NULL, NULL);
  273. UA_Variant_clear(&attr.value);
  274. #ifdef UA_ENABLE_TYPENAMES
  275. UA_LocalizedText_clear(&attr.displayName);
  276. UA_QualifiedName_clear(&qualifiedName);
  277. #endif
  278. }
  279. /* Add Integer and UInteger variables */
  280. UA_VariableAttributes iattr = UA_VariableAttributes_default;
  281. iattr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_INTEGER);
  282. iattr.displayName = UA_LOCALIZEDTEXT("en-US", "Integer");
  283. iattr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  284. iattr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  285. iattr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  286. iattr.valueRank = UA_VALUERANK_SCALAR;
  287. UA_QualifiedName iQualifiedName = UA_QUALIFIEDNAME(1, "integer");
  288. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "integer"),
  289. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  290. iQualifiedName, baseDataVariableType, iattr, NULL, NULL);
  291. iattr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_UINTEGER);
  292. iattr.displayName = UA_LOCALIZEDTEXT("en-US", "UInteger");
  293. UA_QualifiedName uQualifiedName = UA_QUALIFIEDNAME(1, "uinteger");
  294. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "uinteger"),
  295. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  296. uQualifiedName, baseDataVariableType, iattr, NULL, NULL);
  297. UA_Variant_clear(&iattr.value);
  298. /* Hierarchy of depth 10 for CTT testing with forward and inverse references */
  299. /* Enter node "depth 9" in CTT configuration - Project->Settings->Server
  300. Test->NodeIds->Paths->Starting Node 1 */
  301. object_attr.description = UA_LOCALIZEDTEXT("en-US", "DepthDemo");
  302. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "DepthDemo");
  303. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEPTHID), UA_NODEID_NUMERIC(1, DEMOID),
  304. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "DepthDemo"),
  305. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  306. id = DEPTHID; // running id in namespace 0 - Start with Matrix NODE
  307. for(UA_UInt32 i = 1; i <= 20; i++) {
  308. char name[15];
  309. UA_snprintf(name, 15, "depth%i", i);
  310. object_attr.description = UA_LOCALIZEDTEXT("en-US", name);
  311. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  312. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, id + i),
  313. UA_NODEID_NUMERIC(1, i == 1 ? DEPTHID : id + i - 1),
  314. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  315. UA_QUALIFIEDNAME(1, name),
  316. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  317. }
  318. /* Add the variable to some more places to get a node with three inverse references for the CTT */
  319. UA_ExpandedNodeId answer_nodeid = UA_EXPANDEDNODEID_STRING(1, "the.answer");
  320. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, DEMOID),
  321. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  322. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, SCALARID),
  323. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  324. /* Example for manually setting an attribute within the server */
  325. UA_LocalizedText objectsName = UA_LOCALIZEDTEXT("en-US", "Objects");
  326. UA_Server_writeDisplayName(server, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), objectsName);
  327. #define NOARGID 60000
  328. #define INARGID 60001
  329. #define OUTARGID 60002
  330. #define INOUTARGID 60003
  331. #ifdef UA_ENABLE_METHODCALLS
  332. /* adding some more method nodes to pass CTT */
  333. /* Method without arguments */
  334. addmethodattributes = UA_MethodAttributes_default;
  335. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "noarg");
  336. addmethodattributes.executable = true;
  337. addmethodattributes.userExecutable = true;
  338. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, NOARGID),
  339. UA_NODEID_NUMERIC(1, DEMOID),
  340. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  341. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  342. &noargMethod, /* callback of the method node */
  343. 0, NULL, 0, NULL, NULL, NULL);
  344. /* Method with in arguments */
  345. addmethodattributes = UA_MethodAttributes_default;
  346. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inarg");
  347. addmethodattributes.executable = true;
  348. addmethodattributes.userExecutable = true;
  349. UA_Argument_init(&inputArguments);
  350. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  351. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Input");
  352. inputArguments.name = UA_STRING("Input");
  353. inputArguments.valueRank = UA_VALUERANK_SCALAR; //uaexpert will crash if set to 0 ;)
  354. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INARGID),
  355. UA_NODEID_NUMERIC(1, DEMOID),
  356. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  357. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  358. &noargMethod, /* callback of the method node */
  359. 1, &inputArguments, 0, NULL, NULL, NULL);
  360. /* Method with out arguments */
  361. addmethodattributes = UA_MethodAttributes_default;
  362. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "outarg");
  363. addmethodattributes.executable = true;
  364. addmethodattributes.userExecutable = true;
  365. UA_Argument_init(&outputArguments);
  366. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  367. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Output");
  368. outputArguments.name = UA_STRING("Output");
  369. outputArguments.valueRank = UA_VALUERANK_SCALAR;
  370. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, OUTARGID),
  371. UA_NODEID_NUMERIC(1, DEMOID),
  372. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  373. UA_QUALIFIEDNAME(1, "outarg"), addmethodattributes,
  374. &outargMethod, /* callback of the method node */
  375. 0, NULL, 1, &outputArguments, NULL, NULL);
  376. /* Method with inout arguments */
  377. addmethodattributes = UA_MethodAttributes_default;
  378. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inoutarg");
  379. addmethodattributes.executable = true;
  380. addmethodattributes.userExecutable = true;
  381. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INOUTARGID),
  382. UA_NODEID_NUMERIC(1, DEMOID),
  383. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  384. UA_QUALIFIEDNAME(1, "inoutarg"), addmethodattributes,
  385. &outargMethod, /* callback of the method node */
  386. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  387. #endif
  388. }
  389. UA_Boolean running = true;
  390. static void
  391. stopHandler(int sign) {
  392. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Received Ctrl-C");
  393. running = 0;
  394. }
  395. int main(int argc, char **argv) {
  396. signal(SIGINT, stopHandler); /* catches ctrl-c */
  397. signal(SIGTERM, stopHandler);
  398. UA_ServerConfig *config;
  399. #ifdef UA_ENABLE_ENCRYPTION
  400. if(argc < 3) {
  401. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  402. "Missing arguments for encryption support. "
  403. "Arguments are <server-certificate.der> "
  404. "<private-key.der> [<trustlist1.crl>, ...]");
  405. config = UA_ServerConfig_new_minimal(4840, NULL);
  406. } else {
  407. /* Load certificate and private key */
  408. UA_ByteString certificate = loadFile(argv[1]);
  409. if(certificate.length == 0) {
  410. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  411. "Unable to load file %s.", argv[1]);
  412. return 1;
  413. }
  414. UA_ByteString privateKey = loadFile(argv[2]);
  415. if(privateKey.length == 0) {
  416. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  417. "Unable to load file %s.", argv[2]);
  418. return 1;
  419. }
  420. /* Load the trustlist */
  421. size_t trustListSize = 0;
  422. if(argc > 3)
  423. trustListSize = (size_t)argc-3;
  424. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  425. for(size_t i = 0; i < trustListSize; i++)
  426. trustList[i] = loadFile(argv[i+3]);
  427. /* Loading of a revocation list currently unsupported */
  428. UA_ByteString *revocationList = NULL;
  429. size_t revocationListSize = 0;
  430. config = UA_ServerConfig_new_allSecurityPolicies(4840, &certificate, &privateKey,
  431. trustList, trustListSize,
  432. revocationList, revocationListSize);
  433. UA_ByteString_clear(&certificate);
  434. UA_ByteString_clear(&privateKey);
  435. for(size_t i = 0; i < trustListSize; i++)
  436. UA_ByteString_clear(&trustList[i]);
  437. }
  438. #else
  439. UA_ByteString certificate = UA_BYTESTRING_NULL;
  440. if(argc < 2) {
  441. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  442. "Missing argument for the server certificate");
  443. } else {
  444. certificate = loadFile(argv[1]);
  445. }
  446. config = UA_ServerConfig_new_minimal(4840, &certificate);
  447. UA_ByteString_clear(&certificate);
  448. #endif
  449. if(!config) {
  450. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  451. "Could not create the server config");
  452. return 1;
  453. }
  454. /* Override with a custom access control policy */
  455. config->accessControl.getUserAccessLevel = getUserAccessLevel_disallowSpecific;
  456. /* uncomment next line to add a custom hostname */
  457. // UA_ServerConfig_set_customHostname(config, UA_STRING("custom"));
  458. UA_Server *server = UA_Server_new(config);
  459. if(server == NULL)
  460. return 1;
  461. setInformationModel(server);
  462. /* run server */
  463. UA_StatusCode retval = UA_Server_run(server, &running);
  464. UA_Server_delete(server);
  465. UA_ServerConfig_delete(config);
  466. return (int)retval;
  467. }