server_ctt.c 24 KB

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