server_ctt.c 25 KB

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