server_ctt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. #include <stdlib.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. int main(int argc, char **argv) {
  397. signal(SIGINT, stopHandler); /* catches ctrl-c */
  398. signal(SIGTERM, stopHandler);
  399. UA_ServerConfig *config;
  400. #ifdef UA_ENABLE_ENCRYPTION
  401. if(argc < 3) {
  402. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  403. "Missing arguments for encryption support. "
  404. "Arguments are <server-certificate.der> "
  405. "<private-key.der> [<trustlist1.crl>, ...]");
  406. config = UA_ServerConfig_new_minimal(4840, NULL);
  407. } else {
  408. /* Load certificate and private key */
  409. UA_ByteString certificate = loadFile(argv[1]);
  410. if(certificate.length == 0) {
  411. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  412. "Unable to load file %s.", argv[1]);
  413. return EXIT_FAILURE;
  414. }
  415. UA_ByteString privateKey = loadFile(argv[2]);
  416. if(privateKey.length == 0) {
  417. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  418. "Unable to load file %s.", argv[2]);
  419. return EXIT_FAILURE;
  420. }
  421. /* Load the trustlist */
  422. size_t trustListSize = 0;
  423. if(argc > 3)
  424. trustListSize = (size_t)argc-3;
  425. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  426. for(size_t i = 0; i < trustListSize; i++)
  427. trustList[i] = loadFile(argv[i+3]);
  428. /* Loading of a revocation list currently unsupported */
  429. UA_ByteString *revocationList = NULL;
  430. size_t revocationListSize = 0;
  431. config = UA_ServerConfig_new_allSecurityPolicies(4840, &certificate, &privateKey,
  432. trustList, trustListSize,
  433. revocationList, revocationListSize);
  434. UA_ByteString_clear(&certificate);
  435. UA_ByteString_clear(&privateKey);
  436. for(size_t i = 0; i < trustListSize; i++)
  437. UA_ByteString_clear(&trustList[i]);
  438. }
  439. #else
  440. UA_ByteString certificate = UA_BYTESTRING_NULL;
  441. if(argc < 2) {
  442. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  443. "Missing argument for the server certificate");
  444. } else {
  445. certificate = loadFile(argv[1]);
  446. }
  447. config = UA_ServerConfig_new_minimal(4840, &certificate);
  448. UA_ByteString_clear(&certificate);
  449. #endif
  450. if(!config) {
  451. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  452. "Could not create the server config");
  453. return EXIT_FAILURE;
  454. }
  455. /* Override with a custom access control policy */
  456. config->accessControl.getUserAccessLevel = getUserAccessLevel_disallowSpecific;
  457. /* uncomment next line to add a custom hostname */
  458. // UA_ServerConfig_set_customHostname(config, UA_STRING("custom"));
  459. UA_Server *server = UA_Server_new(config);
  460. if(server == NULL)
  461. return EXIT_FAILURE;
  462. setInformationModel(server);
  463. /* run server */
  464. UA_StatusCode retval = UA_Server_run(server, &running);
  465. UA_Server_delete(server);
  466. UA_ServerConfig_delete(config);
  467. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  468. }