server.c 20 KB

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