server_ctt.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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. * Copyright 2019 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  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. #define SCALETESTID 40005
  198. UA_ObjectAttributes object_attr = UA_ObjectAttributes_default;
  199. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Demo");
  200. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Demo");
  201. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEMOID),
  202. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  203. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Demo"),
  204. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  205. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Scalar");
  206. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Scalar");
  207. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALARID),
  208. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  209. UA_QUALIFIEDNAME(1, "Scalar"),
  210. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  211. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Array");
  212. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Array");
  213. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, ARRAYID),
  214. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  215. UA_QUALIFIEDNAME(1, "Array"),
  216. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  217. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Matrix");
  218. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Matrix");
  219. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, MATRIXID), UA_NODEID_NUMERIC(1, DEMOID),
  220. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Matrix"),
  221. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  222. object_attr.description = UA_LOCALIZEDTEXT("en-US", "ScaleTest");
  223. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "ScaleTest");
  224. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALETESTID), UA_NODEID_NUMERIC(1, DEMOID),
  225. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "ScaleTest"),
  226. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  227. /* Fill demo nodes for each type*/
  228. UA_UInt32 matrixDims[2] = {3, 3};
  229. UA_UInt32 id = 51000; // running id in namespace 0
  230. for(UA_UInt32 type = 0; type < UA_TYPES_DIAGNOSTICINFO; type++) {
  231. if(type == UA_TYPES_VARIANT || type == UA_TYPES_DIAGNOSTICINFO)
  232. continue;
  233. UA_VariableAttributes attr = UA_VariableAttributes_default;
  234. attr.dataType = UA_TYPES[type].typeId;
  235. #ifndef UA_ENABLE_TYPENAMES
  236. char name[15];
  237. UA_snprintf(name, 15, "%02d", type);
  238. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  239. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  240. #else
  241. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", UA_TYPES[type].typeName);
  242. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME_ALLOC(1, UA_TYPES[type].typeName);
  243. #endif
  244. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  245. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  246. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  247. /* add a scalar node for every built-in type */
  248. attr.valueRank = UA_VALUERANK_SCALAR;
  249. void *value = UA_new(&UA_TYPES[type]);
  250. UA_Variant_setScalar(&attr.value, value, &UA_TYPES[type]);
  251. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id),
  252. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  253. qualifiedName, baseDataVariableType, attr, NULL, NULL);
  254. UA_Variant_clear(&attr.value);
  255. /* add an array node for every built-in type */
  256. UA_UInt32 arrayDims = 0;
  257. attr.valueRank = UA_VALUERANK_ONE_DIMENSION;
  258. attr.arrayDimensions = &arrayDims;
  259. attr.arrayDimensionsSize = 1;
  260. UA_Variant_setArray(&attr.value, UA_Array_new(10, &UA_TYPES[type]), 10, &UA_TYPES[type]);
  261. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, ARRAYID),
  262. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  263. baseDataVariableType, attr, NULL, NULL);
  264. UA_Variant_clear(&attr.value);
  265. /* add an matrix node for every built-in type */
  266. attr.valueRank = UA_VALUERANK_TWO_DIMENSIONS;
  267. attr.arrayDimensions = matrixDims;
  268. attr.arrayDimensionsSize = 2;
  269. void *myMultiArray = UA_Array_new(9, &UA_TYPES[type]);
  270. attr.value.arrayDimensions = (UA_UInt32 *)UA_Array_new(2, &UA_TYPES[UA_TYPES_INT32]);
  271. attr.value.arrayDimensions[0] = 3;
  272. attr.value.arrayDimensions[1] = 3;
  273. attr.value.arrayDimensionsSize = 2;
  274. attr.value.arrayLength = 9;
  275. attr.value.data = myMultiArray;
  276. attr.value.type = &UA_TYPES[type];
  277. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, MATRIXID),
  278. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  279. baseDataVariableType, attr, NULL, NULL);
  280. UA_Variant_clear(&attr.value);
  281. #ifdef UA_ENABLE_TYPENAMES
  282. UA_LocalizedText_clear(&attr.displayName);
  283. UA_QualifiedName_clear(&qualifiedName);
  284. #endif
  285. }
  286. /* Add Integer and UInteger variables */
  287. UA_VariableAttributes iattr = UA_VariableAttributes_default;
  288. iattr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_INTEGER);
  289. iattr.displayName = UA_LOCALIZEDTEXT("en-US", "Integer");
  290. iattr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  291. iattr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  292. iattr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  293. iattr.valueRank = UA_VALUERANK_SCALAR;
  294. UA_QualifiedName iQualifiedName = UA_QUALIFIEDNAME(1, "integer");
  295. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "integer"),
  296. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  297. iQualifiedName, baseDataVariableType, iattr, NULL, NULL);
  298. iattr.dataType = UA_NODEID_NUMERIC(0, UA_NS0ID_UINTEGER);
  299. iattr.displayName = UA_LOCALIZEDTEXT("en-US", "UInteger");
  300. UA_QualifiedName uQualifiedName = UA_QUALIFIEDNAME(1, "uinteger");
  301. UA_Server_addVariableNode(server, UA_NODEID_STRING(1, "uinteger"),
  302. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  303. uQualifiedName, baseDataVariableType, iattr, NULL, NULL);
  304. UA_Variant_clear(&iattr.value);
  305. /* Hierarchy of depth 10 for CTT testing with forward and inverse references */
  306. /* Enter node "depth 9" in CTT configuration - Project->Settings->Server
  307. Test->NodeIds->Paths->Starting Node 1 */
  308. object_attr.description = UA_LOCALIZEDTEXT("en-US", "DepthDemo");
  309. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "DepthDemo");
  310. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEPTHID), UA_NODEID_NUMERIC(1, DEMOID),
  311. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "DepthDemo"),
  312. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  313. id = DEPTHID; // running id in namespace 0 - Start with Matrix NODE
  314. for(UA_UInt32 i = 1; i <= 20; i++) {
  315. char name[15];
  316. UA_snprintf(name, 15, "depth%i", i);
  317. object_attr.description = UA_LOCALIZEDTEXT("en-US", name);
  318. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  319. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, id + i),
  320. UA_NODEID_NUMERIC(1, i == 1 ? DEPTHID : id + i - 1),
  321. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  322. UA_QUALIFIEDNAME(1, name),
  323. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  324. }
  325. /* Scale Test: 100 nodes of each type */
  326. int scale_i = 0;
  327. UA_UInt32 scale_nodeid = 43000;
  328. for(UA_UInt32 type = 0; type < UA_TYPES_QUALIFIEDNAME; type++) {
  329. if(type == UA_TYPES_VARIANT || type == UA_TYPES_QUALIFIEDNAME)
  330. continue;
  331. UA_VariableAttributes attr = UA_VariableAttributes_default;
  332. attr.dataType = UA_TYPES[type].typeId;
  333. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  334. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  335. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  336. attr.valueRank = UA_VALUERANK_SCALAR;
  337. void *value = UA_new(&UA_TYPES[type]);
  338. UA_Variant_setScalar(&attr.value, value, &UA_TYPES[type]);
  339. for(size_t j = 0; j < 100; j++) {
  340. char name[32];
  341. #ifndef UA_ENABLE_TYPENAMES
  342. UA_snprintf(name, 20, "%02d - %i", type, scale_i);
  343. #else
  344. UA_snprintf(name, 20, "%s - %i", UA_TYPES[type].typeName, scale_i);
  345. #endif
  346. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  347. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  348. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++scale_nodeid),
  349. UA_NODEID_NUMERIC(1, SCALETESTID),
  350. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  351. qualifiedName, baseDataVariableType, attr, NULL, NULL);
  352. scale_i++;
  353. }
  354. UA_Variant_clear(&attr.value);
  355. }
  356. /* Add the variable to some more places to get a node with three inverse references for the CTT */
  357. UA_ExpandedNodeId answer_nodeid = UA_EXPANDEDNODEID_STRING(1, "the.answer");
  358. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, DEMOID),
  359. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  360. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, SCALARID),
  361. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  362. /* Example for manually setting an attribute within the server */
  363. UA_LocalizedText objectsName = UA_LOCALIZEDTEXT("en-US", "Objects");
  364. UA_Server_writeDisplayName(server, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), objectsName);
  365. #define NOARGID 60000
  366. #define INARGID 60001
  367. #define OUTARGID 60002
  368. #define INOUTARGID 60003
  369. #ifdef UA_ENABLE_METHODCALLS
  370. /* adding some more method nodes to pass CTT */
  371. /* Method without arguments */
  372. addmethodattributes = UA_MethodAttributes_default;
  373. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "noarg");
  374. addmethodattributes.executable = true;
  375. addmethodattributes.userExecutable = true;
  376. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, NOARGID),
  377. UA_NODEID_NUMERIC(1, DEMOID),
  378. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  379. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  380. &noargMethod, /* callback of the method node */
  381. 0, NULL, 0, NULL, NULL, NULL);
  382. /* Method with in arguments */
  383. addmethodattributes = UA_MethodAttributes_default;
  384. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inarg");
  385. addmethodattributes.executable = true;
  386. addmethodattributes.userExecutable = true;
  387. UA_Argument_init(&inputArguments);
  388. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  389. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Input");
  390. inputArguments.name = UA_STRING("Input");
  391. inputArguments.valueRank = UA_VALUERANK_SCALAR; //uaexpert will crash if set to 0 ;)
  392. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INARGID),
  393. UA_NODEID_NUMERIC(1, DEMOID),
  394. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  395. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  396. &noargMethod, /* callback of the method node */
  397. 1, &inputArguments, 0, NULL, NULL, NULL);
  398. /* Method with out arguments */
  399. addmethodattributes = UA_MethodAttributes_default;
  400. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "outarg");
  401. addmethodattributes.executable = true;
  402. addmethodattributes.userExecutable = true;
  403. UA_Argument_init(&outputArguments);
  404. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  405. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Output");
  406. outputArguments.name = UA_STRING("Output");
  407. outputArguments.valueRank = UA_VALUERANK_SCALAR;
  408. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, OUTARGID),
  409. UA_NODEID_NUMERIC(1, DEMOID),
  410. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  411. UA_QUALIFIEDNAME(1, "outarg"), addmethodattributes,
  412. &outargMethod, /* callback of the method node */
  413. 0, NULL, 1, &outputArguments, NULL, NULL);
  414. /* Method with inout arguments */
  415. addmethodattributes = UA_MethodAttributes_default;
  416. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inoutarg");
  417. addmethodattributes.executable = true;
  418. addmethodattributes.userExecutable = true;
  419. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INOUTARGID),
  420. UA_NODEID_NUMERIC(1, DEMOID),
  421. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  422. UA_QUALIFIEDNAME(1, "inoutarg"), addmethodattributes,
  423. &outargMethod, /* callback of the method node */
  424. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  425. #endif
  426. }
  427. static void
  428. disableAnonymous(UA_ServerConfig *config) {
  429. for(size_t i = 0; i < config->endpointsSize; i++) {
  430. UA_EndpointDescription *ep = &config->endpoints[i];
  431. for(size_t j = 0; j < ep->userIdentityTokensSize; j++) {
  432. UA_UserTokenPolicy *utp = &ep->userIdentityTokens[j];
  433. if(utp->tokenType != UA_USERTOKENTYPE_ANONYMOUS)
  434. continue;
  435. UA_UserTokenPolicy_clear(utp);
  436. /* Move the last to this position */
  437. if(j + 1 < ep->userIdentityTokensSize) {
  438. ep->userIdentityTokens[j] = ep->userIdentityTokens[ep->userIdentityTokensSize-1];
  439. j--;
  440. }
  441. ep->userIdentityTokensSize--;
  442. }
  443. /* Delete the entire array if the last UserTokenPolicy was removed */
  444. if(ep->userIdentityTokensSize == 0) {
  445. UA_free(ep->userIdentityTokens);
  446. ep->userIdentityTokens = NULL;
  447. }
  448. }
  449. }
  450. #ifdef UA_ENABLE_ENCRYPTION
  451. static void
  452. disableUnencrypted(UA_ServerConfig *config) {
  453. for(size_t i = 0; i < config->endpointsSize; i++) {
  454. UA_EndpointDescription *ep = &config->endpoints[i];
  455. if(ep->securityMode != UA_MESSAGESECURITYMODE_NONE)
  456. continue;
  457. UA_EndpointDescription_clear(ep);
  458. /* Move the last to this position */
  459. if(i + 1 < config->endpointsSize) {
  460. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  461. i--;
  462. }
  463. config->endpointsSize--;
  464. }
  465. /* Delete the entire array if the last Endpoint was removed */
  466. if(config->endpointsSize== 0) {
  467. UA_free(config->endpoints);
  468. config->endpoints = NULL;
  469. }
  470. }
  471. static void
  472. disableOutdatedSecurityPolicy(UA_ServerConfig *config) {
  473. for(size_t i = 0; i < config->endpointsSize; i++) {
  474. UA_EndpointDescription *ep = &config->endpoints[i];
  475. UA_ByteString basic128uri = UA_BYTESTRING("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  476. if(!UA_String_equal(&ep->securityPolicyUri, &basic128uri))
  477. continue;
  478. UA_EndpointDescription_clear(ep);
  479. /* Move the last to this position */
  480. if(i + 1 < config->endpointsSize) {
  481. config->endpoints[i] = config->endpoints[config->endpointsSize-1];
  482. i--;
  483. }
  484. config->endpointsSize--;
  485. }
  486. /* Delete the entire array if the last Endpoint was removed */
  487. if(config->endpointsSize== 0) {
  488. UA_free(config->endpoints);
  489. config->endpoints = NULL;
  490. }
  491. }
  492. #endif
  493. UA_Boolean running = true;
  494. static void
  495. stopHandler(int sign) {
  496. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Received Ctrl-C");
  497. running = 0;
  498. }
  499. static void
  500. usage(void) {
  501. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  502. "Usage:\n"
  503. #ifndef UA_ENABLE_ENCRYPTION
  504. "server_ctt [<server-certificate.der>]\n"
  505. #else
  506. "server_ctt <server-certificate.der> <private-key.der>\n"
  507. "\t[--trustlist <tl1.ctl> <tl2.ctl> ... ]\n"
  508. "\t[--issuerlist <il1.der> <il2.der> ... ]\n"
  509. "\t[--revocationlist <rv1.crl> <rv2.crl> ...]\n"
  510. "\t[--enableUnencrypted]\n"
  511. "\t[--enableOutdatedSecurityPolicy]\n"
  512. #endif
  513. "\t[--enableAnonymous]\n");
  514. }
  515. int main(int argc, char **argv) {
  516. signal(SIGINT, stopHandler); /* catches ctrl-c */
  517. signal(SIGTERM, stopHandler);
  518. for(int i = 1; i < argc; i++) {
  519. if(strcmp(argv[i], "--help") == 0 ||
  520. strcmp(argv[i], "-h") == 0) {
  521. usage();
  522. return EXIT_SUCCESS;
  523. }
  524. }
  525. UA_Server *server = UA_Server_new();
  526. if(server == NULL)
  527. return EXIT_FAILURE;
  528. UA_ServerConfig *config = UA_Server_getConfig(server);
  529. /* Load certificate */
  530. size_t pos = 1;
  531. UA_ByteString certificate = UA_BYTESTRING_NULL;
  532. if((size_t)argc >= pos + 1) {
  533. certificate = loadFile(argv[1]);
  534. if(certificate.length == 0) {
  535. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  536. "Unable to load file %s.", argv[pos]);
  537. return EXIT_FAILURE;
  538. }
  539. pos++;
  540. }
  541. #ifdef UA_ENABLE_ENCRYPTION
  542. /* Load the private key */
  543. UA_ByteString privateKey = UA_BYTESTRING_NULL;
  544. if((size_t)argc >= pos + 1) {
  545. privateKey = loadFile(argv[2]);
  546. if(privateKey.length == 0) {
  547. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  548. "Unable to load file %s.", argv[pos]);
  549. return EXIT_FAILURE;
  550. }
  551. pos++;
  552. }
  553. UA_ByteString trustList[100];
  554. size_t trustListSize = 0;
  555. UA_ByteString issuerList[100];
  556. size_t issuerListSize = 0;
  557. UA_ByteString revocationList[100];
  558. size_t revocationListSize = 0;
  559. char filetype = ' '; /* t==trustlist, l == issuerList, r==revocationlist */
  560. UA_Boolean enableUnencr = false;
  561. UA_Boolean enableSec = false;
  562. #endif
  563. UA_Boolean enableAnon = false;
  564. /* Loop over the remaining arguments */
  565. for(; pos < (size_t)argc; pos++) {
  566. if(strcmp(argv[pos], "--enableAnonymous") == 0) {
  567. enableAnon = true;
  568. continue;
  569. }
  570. #ifdef UA_ENABLE_ENCRYPTION
  571. if(strcmp(argv[pos], "--enableUnencrypted") == 0) {
  572. enableUnencr = true;
  573. continue;
  574. }
  575. if(strcmp(argv[pos], "--enableOutdatedSecurityPolicy") == 0) {
  576. enableSec = true;
  577. continue;
  578. }
  579. if(strcmp(argv[pos], "--trustlist") == 0) {
  580. filetype = 't';
  581. continue;
  582. }
  583. if(strcmp(argv[pos], "--issuerlist") == 0) {
  584. filetype = 'l';
  585. continue;
  586. }
  587. if(strcmp(argv[pos], "--revocationlist") == 0) {
  588. filetype = 'r';
  589. continue;
  590. }
  591. if(filetype == 't') {
  592. if(trustListSize >= 100) {
  593. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  594. "Too many trust lists");
  595. return EXIT_FAILURE;
  596. }
  597. trustList[trustListSize] = loadFile(argv[pos]);
  598. if(trustList[trustListSize].data == NULL) {
  599. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  600. "Unable to load trust list %s", argv[pos]);
  601. return EXIT_FAILURE;
  602. }
  603. trustListSize++;
  604. continue;
  605. }
  606. if(filetype == 'l') {
  607. if(issuerListSize >= 100) {
  608. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  609. "Too many trust lists");
  610. return EXIT_FAILURE;
  611. }
  612. issuerList[issuerListSize] = loadFile(argv[pos]);
  613. if(issuerList[issuerListSize].data == NULL) {
  614. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  615. "Unable to load trust list %s", argv[pos]);
  616. return EXIT_FAILURE;
  617. }
  618. issuerListSize++;
  619. continue;
  620. }
  621. if(filetype == 'r') {
  622. if(revocationListSize >= 100) {
  623. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  624. "Too many revocation lists");
  625. return EXIT_FAILURE;
  626. }
  627. revocationList[revocationListSize] = loadFile(argv[pos]);
  628. if(revocationList[revocationListSize].data == NULL) {
  629. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  630. "Unable to load revocationlist %s", argv[pos]);
  631. return EXIT_FAILURE;
  632. }
  633. revocationListSize++;
  634. continue;
  635. }
  636. #endif
  637. usage();
  638. return EXIT_FAILURE;
  639. }
  640. #ifdef UA_ENABLE_ENCRYPTION
  641. UA_ServerConfig_setDefaultWithSecurityPolicies(config, 4840,
  642. &certificate, &privateKey,
  643. trustList, trustListSize,
  644. issuerList, issuerListSize,
  645. revocationList, revocationListSize);
  646. if(!enableUnencr)
  647. disableUnencrypted(config);
  648. if(!enableSec)
  649. disableOutdatedSecurityPolicy(&config);
  650. #else
  651. UA_ServerConfig_setMinimal(config, 4840, &certificate);
  652. #endif
  653. if(!enableAnon)
  654. disableAnonymous(config);
  655. /* Clean up temp values */
  656. UA_ByteString_clear(&certificate);
  657. #ifdef UA_ENABLE_ENCRYPTION
  658. UA_ByteString_clear(&privateKey);
  659. for(size_t i = 0; i < trustListSize; i++)
  660. UA_ByteString_clear(&trustList[i]);
  661. for(size_t i = 0; i < issuerListSize; i++)
  662. UA_ByteString_clear(&issuerList[i]);
  663. for(size_t i = 0; i < revocationListSize; i++)
  664. UA_ByteString_clear(&revocationList[i]);
  665. #endif
  666. /* Override with a custom access control policy */
  667. config->accessControl.getUserAccessLevel = getUserAccessLevel_disallowSpecific;
  668. UA_String_clear(&config->applicationDescription.applicationUri);
  669. config->applicationDescription.applicationUri = UA_String_fromChars("urn:open62541.server.application");
  670. setInformationModel(server);
  671. /* run server */
  672. UA_StatusCode retval = UA_Server_run(server, &running);
  673. UA_Server_delete(server);
  674. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  675. }