server_ctt.c 23 KB

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