server_ctt.c 25 KB

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