server_ctt.c 24 KB

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