server_ctt.c 28 KB

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