server.c 18 KB

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