server.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /* Compile with single file release:
  4. * - single-threaded: gcc -std=c99 server.c open62541.c -o server
  5. * - multi-threaded: gcc -std=c99 server.c open62541.c -o server -lurcu-cds -lurcu -lurcu-common -lpthread */
  6. #ifdef UA_NO_AMALGAMATION
  7. # include <time.h>
  8. # include "ua_types.h"
  9. # include "ua_server.h"
  10. # include "ua_config_standard.h"
  11. # include "ua_network_tcp.h"
  12. # include "ua_log_stdout.h"
  13. #else
  14. # include "open62541.h"
  15. #endif
  16. #include <signal.h>
  17. #include <errno.h> // errno, EINTR
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #ifdef _MSC_VER
  22. # include <io.h> //access
  23. #else
  24. # include <unistd.h> //access
  25. #endif
  26. #ifdef UA_ENABLE_MULTITHREADING
  27. # ifdef UA_NO_AMALGAMATION
  28. # ifndef __USE_XOPEN2K
  29. # define __USE_XOPEN2K
  30. # endif
  31. # endif
  32. #include <pthread.h>
  33. #endif
  34. UA_Boolean running = 1;
  35. UA_Logger logger = UA_Log_Stdout;
  36. static void stopHandler(int sign) {
  37. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "Received Ctrl-C");
  38. running = 0;
  39. }
  40. /* Datasource Example */
  41. static UA_StatusCode
  42. readTimeData(void *handle, const UA_NodeId nodeId, UA_Boolean sourceTimeStamp,
  43. const UA_NumericRange *range, UA_DataValue *value) {
  44. if(range) {
  45. value->hasStatus = true;
  46. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  47. return UA_STATUSCODE_GOOD;
  48. }
  49. UA_DateTime currentTime = UA_DateTime_now();
  50. UA_Variant_setScalarCopy(&value->value, &currentTime, &UA_TYPES[UA_TYPES_DATETIME]);
  51. value->hasValue = true;
  52. if(sourceTimeStamp) {
  53. value->hasSourceTimestamp = true;
  54. value->sourceTimestamp = currentTime;
  55. }
  56. return UA_STATUSCODE_GOOD;
  57. }
  58. /* Method Node Example */
  59. #ifdef UA_ENABLE_METHODCALLS
  60. static UA_StatusCode
  61. helloWorld(void *methodHandle, const UA_NodeId objectId,
  62. size_t inputSize, const UA_Variant *input,
  63. size_t outputSize, UA_Variant *output) {
  64. /* input is a scalar string (checked by the server) */
  65. UA_String *name = (UA_String*)input[0].data;
  66. UA_String hello = UA_STRING("Hello ");
  67. UA_String greet;
  68. greet.length = hello.length + name->length;
  69. greet.data = malloc(greet.length);
  70. memcpy(greet.data, hello.data, hello.length);
  71. memcpy(greet.data + hello.length, name->data, name->length);
  72. UA_Variant_setScalarCopy(output, &greet, &UA_TYPES[UA_TYPES_STRING]);
  73. UA_String_deleteMembers(&greet);
  74. return UA_STATUSCODE_GOOD;
  75. }
  76. static UA_StatusCode
  77. noargMethod (void *methodHandle, const UA_NodeId objectId,
  78. size_t inputSize, const UA_Variant *input,
  79. size_t outputSize, UA_Variant *output) {
  80. return UA_STATUSCODE_GOOD;
  81. }
  82. static UA_StatusCode
  83. outargMethod (void *methodHandle, const UA_NodeId objectId,
  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. int main(int argc, char** argv) {
  92. signal(SIGINT, stopHandler); /* catches ctrl-c */
  93. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  94. UA_ServerConfig config = UA_ServerConfig_standard;
  95. config.networkLayers = &nl;
  96. config.networkLayersSize = 1;
  97. UA_Server *server = UA_Server_new(config);
  98. /* add a static variable node to the server */
  99. UA_VariableAttributes myVar;
  100. UA_VariableAttributes_init(&myVar);
  101. myVar.description = UA_LOCALIZEDTEXT("en_US", "the answer");
  102. myVar.displayName = UA_LOCALIZEDTEXT("en_US", "the answer");
  103. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  104. UA_Int32 myInteger = 42;
  105. UA_Variant_setScalarCopy(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  106. const UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  107. const UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  108. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  109. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  110. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  111. myIntegerName, UA_NODEID_NULL, myVar, NULL, NULL);
  112. UA_Variant_deleteMembers(&myVar.value);
  113. /* add a variable with the datetime data source */
  114. UA_DataSource dateDataSource = (UA_DataSource) {.handle = NULL, .read = readTimeData, .write = NULL};
  115. UA_VariableAttributes v_attr;
  116. UA_VariableAttributes_init(&v_attr);
  117. v_attr.description = UA_LOCALIZEDTEXT("en_US","current time");
  118. v_attr.displayName = UA_LOCALIZEDTEXT("en_US","current time");
  119. v_attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  120. const UA_QualifiedName dateName = UA_QUALIFIEDNAME(1, "current time");
  121. UA_NodeId dataSourceId;
  122. UA_Server_addDataSourceVariableNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  123. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), dateName,
  124. UA_NODEID_NULL, v_attr, dateDataSource, &dataSourceId);
  125. /* Add HelloWorld method to the server */
  126. #ifdef UA_ENABLE_METHODCALLS
  127. /* Method with IO Arguments */
  128. UA_Argument inputArguments;
  129. UA_Argument_init(&inputArguments);
  130. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  131. inputArguments.description = UA_LOCALIZEDTEXT("en_US", "Say your name");
  132. inputArguments.name = UA_STRING("Name");
  133. inputArguments.valueRank = -1; /* scalar argument */
  134. UA_Argument outputArguments;
  135. UA_Argument_init(&outputArguments);
  136. outputArguments.arrayDimensionsSize = 0;
  137. outputArguments.arrayDimensions = NULL;
  138. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  139. outputArguments.description = UA_LOCALIZEDTEXT("en_US", "Receive a greeting");
  140. outputArguments.name = UA_STRING("greeting");
  141. outputArguments.valueRank = -1;
  142. UA_MethodAttributes addmethodattributes;
  143. UA_MethodAttributes_init(&addmethodattributes);
  144. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en_US", "Hello World");
  145. addmethodattributes.executable = true;
  146. addmethodattributes.userExecutable = true;
  147. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
  148. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  149. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  150. UA_QUALIFIEDNAME(1, "hello_world"), addmethodattributes,
  151. &helloWorld, /* callback of the method node */
  152. NULL, /* handle passed with the callback */
  153. 1, &inputArguments, 1, &outputArguments, NULL);
  154. #endif
  155. /* Add folders for demo information model */
  156. #define DEMOID 50000
  157. #define SCALARID 50001
  158. #define ARRAYID 50002
  159. #define MATRIXID 50003
  160. #define DEPTHID 50004
  161. UA_ObjectAttributes object_attr;
  162. UA_ObjectAttributes_init(&object_attr);
  163. object_attr.description = UA_LOCALIZEDTEXT("en_US", "Demo");
  164. object_attr.displayName = UA_LOCALIZEDTEXT("en_US", "Demo");
  165. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEMOID),
  166. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  167. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Demo"),
  168. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  169. object_attr.description = UA_LOCALIZEDTEXT("en_US", "Scalar");
  170. object_attr.displayName = UA_LOCALIZEDTEXT("en_US", "Scalar");
  171. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALARID),
  172. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  173. UA_QUALIFIEDNAME(1, "Scalar"),
  174. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  175. object_attr.description = UA_LOCALIZEDTEXT("en_US", "Array");
  176. object_attr.displayName = UA_LOCALIZEDTEXT("en_US", "Array");
  177. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, ARRAYID),
  178. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  179. UA_QUALIFIEDNAME(1, "Array"),
  180. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  181. object_attr.description = UA_LOCALIZEDTEXT("en_US", "Matrix");
  182. object_attr.displayName = UA_LOCALIZEDTEXT("en_US", "Matrix");
  183. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, MATRIXID), UA_NODEID_NUMERIC(1, DEMOID),
  184. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Matrix"),
  185. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  186. /* Fill demo nodes for each type*/
  187. UA_UInt32 id = 51000; // running id in namespace 0
  188. for(UA_UInt32 type = 0; type < UA_TYPES_DIAGNOSTICINFO; type++) {
  189. if(type == UA_TYPES_VARIANT || type == UA_TYPES_DIAGNOSTICINFO)
  190. continue;
  191. UA_VariableAttributes attr;
  192. UA_VariableAttributes_init(&attr);
  193. char name[15];
  194. #if defined(_WIN32) && !defined(__MINGW32__)
  195. sprintf_s(name, 15, "%02d", type);
  196. #else
  197. sprintf(name, "%02d", type);
  198. #endif
  199. attr.displayName = UA_LOCALIZEDTEXT("en_US",name);
  200. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  201. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  202. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  203. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  204. /* add a scalar node for every built-in type */
  205. void *value = UA_new(&UA_TYPES[type]);
  206. UA_Variant_setScalar(&attr.value, value, &UA_TYPES[type]);
  207. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id),
  208. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  209. qualifiedName, UA_NODEID_NULL, attr, NULL, NULL);
  210. UA_Variant_deleteMembers(&attr.value);
  211. /* add an array node for every built-in type */
  212. UA_Variant_setArray(&attr.value, UA_Array_new(10, &UA_TYPES[type]), 10, &UA_TYPES[type]);
  213. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, ARRAYID),
  214. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  215. UA_NODEID_NULL, attr, NULL, NULL);
  216. UA_Variant_deleteMembers(&attr.value);
  217. /* add an matrix node for every built-in type */
  218. void* myMultiArray = UA_Array_new(9, &UA_TYPES[type]);
  219. attr.value.arrayDimensions = UA_Array_new(2, &UA_TYPES[UA_TYPES_INT32]);
  220. attr.value.arrayDimensions[0] = 3;
  221. attr.value.arrayDimensions[1] = 3;
  222. attr.value.arrayDimensionsSize = 2;
  223. attr.value.arrayLength = 9;
  224. attr.value.data = myMultiArray;
  225. attr.value.type = &UA_TYPES[type];
  226. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, MATRIXID),
  227. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  228. UA_NODEID_NULL, attr, NULL, NULL);
  229. UA_Variant_deleteMembers(&attr.value);
  230. }
  231. /* Hierarchy of depth 10 for CTT testing with forward and inverse references */
  232. /* Enter node "depth 9" in CTT configuration - Project->Settings->Server Test->NodeIds->Paths->Starting Node 1 */
  233. object_attr.description = UA_LOCALIZEDTEXT("en_US","DepthDemo");
  234. object_attr.displayName = UA_LOCALIZEDTEXT("en_US","DepthDemo");
  235. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEPTHID),
  236. UA_NODEID_NUMERIC(1, DEMOID),
  237. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "DepthDemo"),
  238. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  239. id = DEPTHID; // running id in namespace 0 - Start with Matrix NODE
  240. for(UA_UInt32 i = 1; i <= 20; i++) {
  241. char name[15];
  242. #if defined(_WIN32) && !defined(__MINGW32__)
  243. sprintf_s(name, 15, "depth%i", i);
  244. #else
  245. sprintf(name, "depth%i", i);
  246. #endif
  247. object_attr.description = UA_LOCALIZEDTEXT("en_US",name);
  248. object_attr.displayName = UA_LOCALIZEDTEXT("en_US",name);
  249. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, id+i),
  250. UA_NODEID_NUMERIC(1, i==1 ? DEPTHID : id+i-1), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  251. UA_QUALIFIEDNAME(1, name),
  252. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  253. }
  254. /* Add the variable to some more places to get a node with three inverse references for the CTT */
  255. UA_ExpandedNodeId answer_nodeid = UA_EXPANDEDNODEID_STRING(1, "the.answer");
  256. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, DEMOID),
  257. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  258. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, SCALARID),
  259. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  260. /* Example for manually setting an attribute within the server */
  261. UA_LocalizedText objectsName = UA_LOCALIZEDTEXT("en_US", "Objects");
  262. UA_Server_writeDisplayName(server, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), objectsName);
  263. #define NOARGID 60000
  264. #define INARGID 60001
  265. #define OUTARGID 60002
  266. #define INOUTARGID 60003
  267. #ifdef UA_ENABLE_METHODCALLS
  268. /* adding some more method nodes to pass CTT */
  269. /* Method without arguments */
  270. UA_MethodAttributes_init(&addmethodattributes);
  271. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en_US", "noarg");
  272. addmethodattributes.executable = true;
  273. addmethodattributes.userExecutable = true;
  274. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, NOARGID),
  275. UA_NODEID_NUMERIC(1, DEMOID),
  276. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  277. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  278. &noargMethod, /* callback of the method node */
  279. NULL, /* handle passed with the callback */
  280. 0, NULL, 0, NULL, NULL);
  281. /* Method with in arguments */
  282. UA_MethodAttributes_init(&addmethodattributes);
  283. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en_US", "inarg");
  284. addmethodattributes.executable = true;
  285. addmethodattributes.userExecutable = true;
  286. UA_Argument_init(&inputArguments);
  287. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  288. inputArguments.description = UA_LOCALIZEDTEXT("en_US", "Input");
  289. inputArguments.name = UA_STRING("Input");
  290. inputArguments.valueRank = -1; //uaexpert will crash if set to 0 ;)
  291. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INARGID),
  292. UA_NODEID_NUMERIC(1, DEMOID),
  293. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  294. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  295. &noargMethod, /* callback of the method node */
  296. NULL, /* handle passed with the callback */
  297. 1, &inputArguments, 0, NULL, NULL);
  298. /* Method with out arguments */
  299. UA_MethodAttributes_init(&addmethodattributes);
  300. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en_US", "outarg");
  301. addmethodattributes.executable = true;
  302. addmethodattributes.userExecutable = true;
  303. UA_Argument_init(&outputArguments);
  304. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  305. outputArguments.description = UA_LOCALIZEDTEXT("en_US", "Output");
  306. outputArguments.name = UA_STRING("Output");
  307. outputArguments.valueRank = -1;
  308. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, OUTARGID),
  309. UA_NODEID_NUMERIC(1, DEMOID),
  310. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  311. UA_QUALIFIEDNAME(1, "outarg"), addmethodattributes,
  312. &outargMethod, /* callback of the method node */
  313. NULL, /* handle passed with the callback */
  314. 0, NULL, 1, &outputArguments, NULL);
  315. /* Method with inout arguments */
  316. UA_MethodAttributes_init(&addmethodattributes);
  317. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en_US", "inoutarg");
  318. addmethodattributes.executable = true;
  319. addmethodattributes.userExecutable = true;
  320. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INOUTARGID),
  321. UA_NODEID_NUMERIC(1, DEMOID),
  322. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  323. UA_QUALIFIEDNAME(1, "inoutarg"), addmethodattributes,
  324. &outargMethod, /* callback of the method node */
  325. NULL, /* handle passed with the callback */
  326. 1, &inputArguments, 1, &outputArguments, NULL);
  327. #endif
  328. /* run server */
  329. UA_StatusCode retval = UA_Server_run(server, &running); /* run until ctrl-c is received */
  330. UA_Server_delete(server);
  331. nl.deleteMembers(&nl);
  332. return (int)retval;
  333. }