server.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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*)UA_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(UA_Server *server,
  41. const UA_NodeId *sessionId, void *sessionContext,
  42. const UA_NodeId *nodeId, void *nodeContext,
  43. UA_Boolean sourceTimeStamp,
  44. const UA_NumericRange *range, UA_DataValue *value) {
  45. if(range) {
  46. value->hasStatus = true;
  47. value->status = UA_STATUSCODE_BADINDEXRANGEINVALID;
  48. return UA_STATUSCODE_GOOD;
  49. }
  50. UA_DateTime currentTime = UA_DateTime_now();
  51. UA_Variant_setScalarCopy(&value->value, &currentTime, &UA_TYPES[UA_TYPES_DATETIME]);
  52. value->hasValue = true;
  53. if(sourceTimeStamp) {
  54. value->hasSourceTimestamp = true;
  55. value->sourceTimestamp = currentTime;
  56. }
  57. return UA_STATUSCODE_GOOD;
  58. }
  59. /* Method Node Example */
  60. #ifdef UA_ENABLE_METHODCALLS
  61. static UA_StatusCode
  62. helloWorld(UA_Server *server,
  63. const UA_NodeId *sessionId, void *sessionContext,
  64. const UA_NodeId *methodId, void *methodContext,
  65. const UA_NodeId *objectId, void *objectContext,
  66. size_t inputSize, const UA_Variant *input,
  67. size_t outputSize, UA_Variant *output) {
  68. /* input is a scalar string (checked by the server) */
  69. UA_String *name = (UA_String*)input[0].data;
  70. UA_String hello = UA_STRING("Hello ");
  71. UA_String greet;
  72. greet.length = hello.length + name->length;
  73. greet.data = (UA_Byte*)UA_malloc(greet.length);
  74. memcpy(greet.data, hello.data, hello.length);
  75. memcpy(greet.data + hello.length, name->data, name->length);
  76. UA_Variant_setScalarCopy(output, &greet, &UA_TYPES[UA_TYPES_STRING]);
  77. UA_String_deleteMembers(&greet);
  78. return UA_STATUSCODE_GOOD;
  79. }
  80. static UA_StatusCode
  81. noargMethod(UA_Server *server,
  82. const UA_NodeId *sessionId, void *sessionContext,
  83. const UA_NodeId *methodId, void *methodContext,
  84. const UA_NodeId *objectId, void *objectContext,
  85. size_t inputSize, const UA_Variant *input,
  86. size_t outputSize, UA_Variant *output) {
  87. return UA_STATUSCODE_GOOD;
  88. }
  89. static UA_StatusCode
  90. outargMethod(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. UA_Int32 out = 42;
  97. UA_Variant_setScalarCopy(output, &out, &UA_TYPES[UA_TYPES_INT32]);
  98. return UA_STATUSCODE_GOOD;
  99. }
  100. #endif
  101. int main(int argc, char** argv) {
  102. signal(SIGINT, stopHandler); /* catches ctrl-c */
  103. UA_ByteString certificate = loadCertificate();
  104. UA_ServerConfig *config =
  105. UA_ServerConfig_new_minimal(4840, &certificate);
  106. UA_ByteString_deleteMembers(&certificate);
  107. /* uncomment next line to add a custom hostname */
  108. // UA_ServerConfig_set_customHostname(config, UA_STRING("custom"));
  109. UA_Server *server = UA_Server_new(config);
  110. /* add a static variable node to the server */
  111. UA_VariableAttributes myVar = UA_VariableAttributes_default;
  112. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer");
  113. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
  114. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  115. UA_Int32 myInteger = 42;
  116. UA_Variant_setScalarCopy(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  117. const UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  118. const UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  119. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  120. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  121. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  122. myIntegerName, UA_NODEID_NULL, myVar, NULL, NULL);
  123. UA_Variant_deleteMembers(&myVar.value);
  124. /* add a variable with the datetime data source */
  125. UA_DataSource dateDataSource;
  126. dateDataSource.read = readTimeData;
  127. dateDataSource.write = NULL;
  128. UA_VariableAttributes v_attr = UA_VariableAttributes_default;
  129. v_attr.description = UA_LOCALIZEDTEXT("en-US","current time");
  130. v_attr.displayName = UA_LOCALIZEDTEXT("en-US","current time");
  131. v_attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  132. const UA_QualifiedName dateName = UA_QUALIFIEDNAME(1, "current time");
  133. UA_Server_addDataSourceVariableNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  134. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), dateName,
  135. UA_NODEID_NULL, v_attr, dateDataSource, NULL, NULL);
  136. /* Add HelloWorld method to the server */
  137. #ifdef UA_ENABLE_METHODCALLS
  138. /* Method with IO Arguments */
  139. UA_Argument inputArguments;
  140. UA_Argument_init(&inputArguments);
  141. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  142. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Say your name");
  143. inputArguments.name = UA_STRING("Name");
  144. inputArguments.valueRank = -1; /* scalar argument */
  145. UA_Argument outputArguments;
  146. UA_Argument_init(&outputArguments);
  147. outputArguments.arrayDimensionsSize = 0;
  148. outputArguments.arrayDimensions = NULL;
  149. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  150. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Receive a greeting");
  151. outputArguments.name = UA_STRING("greeting");
  152. outputArguments.valueRank = -1;
  153. UA_MethodAttributes addmethodattributes = UA_MethodAttributes_default;
  154. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "Hello World");
  155. addmethodattributes.executable = true;
  156. addmethodattributes.userExecutable = true;
  157. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
  158. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  159. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  160. UA_QUALIFIEDNAME(1, "hello_world"), addmethodattributes,
  161. &helloWorld, /* callback of the method node */
  162. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  163. #endif
  164. /* Add folders for demo information model */
  165. #define DEMOID 50000
  166. #define SCALARID 50001
  167. #define ARRAYID 50002
  168. #define MATRIXID 50003
  169. #define DEPTHID 50004
  170. UA_ObjectAttributes object_attr = UA_ObjectAttributes_default;
  171. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Demo");
  172. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Demo");
  173. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEMOID),
  174. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  175. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Demo"),
  176. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  177. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Scalar");
  178. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Scalar");
  179. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALARID),
  180. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  181. UA_QUALIFIEDNAME(1, "Scalar"),
  182. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  183. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Array");
  184. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Array");
  185. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, ARRAYID),
  186. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  187. UA_QUALIFIEDNAME(1, "Array"),
  188. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  189. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Matrix");
  190. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Matrix");
  191. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, MATRIXID), UA_NODEID_NUMERIC(1, DEMOID),
  192. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Matrix"),
  193. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  194. /* Fill demo nodes for each type*/
  195. UA_UInt32 id = 51000; // running id in namespace 0
  196. for(UA_UInt32 type = 0; type < UA_TYPES_DIAGNOSTICINFO; type++) {
  197. if(type == UA_TYPES_VARIANT || type == UA_TYPES_DIAGNOSTICINFO)
  198. continue;
  199. UA_VariableAttributes attr = UA_VariableAttributes_default;
  200. attr.valueRank = -2;
  201. attr.dataType = UA_TYPES[type].typeId;
  202. #ifndef UA_ENABLE_TYPENAMES
  203. char name[15];
  204. #if defined(_WIN32) && !defined(__MINGW32__)
  205. sprintf_s(name, 15, "%02d", type);
  206. #else
  207. sprintf(name, "%02d", type);
  208. #endif
  209. attr.displayName = UA_LOCALIZEDTEXT("en-US", name);
  210. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME(1, name);
  211. #else
  212. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", UA_TYPES[type].typeName);
  213. UA_QualifiedName qualifiedName = UA_QUALIFIEDNAME_ALLOC(1, UA_TYPES[type].typeName);
  214. #endif
  215. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  216. attr.writeMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  217. attr.userWriteMask = UA_WRITEMASK_DISPLAYNAME | UA_WRITEMASK_DESCRIPTION;
  218. /* add a scalar node for every built-in type */
  219. void *value = UA_new(&UA_TYPES[type]);
  220. UA_Variant_setScalar(&attr.value, value, &UA_TYPES[type]);
  221. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id),
  222. UA_NODEID_NUMERIC(1, SCALARID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  223. qualifiedName, UA_NODEID_NULL, attr, NULL, NULL);
  224. UA_Variant_deleteMembers(&attr.value);
  225. /* add an array node for every built-in type */
  226. UA_Variant_setArray(&attr.value, UA_Array_new(10, &UA_TYPES[type]), 10, &UA_TYPES[type]);
  227. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, ARRAYID),
  228. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  229. UA_NODEID_NULL, attr, NULL, NULL);
  230. UA_Variant_deleteMembers(&attr.value);
  231. /* add an matrix node for every built-in type */
  232. void* myMultiArray = UA_Array_new(9, &UA_TYPES[type]);
  233. attr.value.arrayDimensions = (UA_UInt32*)UA_Array_new(2, &UA_TYPES[UA_TYPES_INT32]);
  234. attr.value.arrayDimensions[0] = 3;
  235. attr.value.arrayDimensions[1] = 3;
  236. attr.value.arrayDimensionsSize = 2;
  237. attr.value.arrayLength = 9;
  238. attr.value.data = myMultiArray;
  239. attr.value.type = &UA_TYPES[type];
  240. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, ++id), UA_NODEID_NUMERIC(1, MATRIXID),
  241. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), qualifiedName,
  242. UA_NODEID_NULL, attr, NULL, NULL);
  243. UA_Variant_deleteMembers(&attr.value);
  244. #ifdef UA_ENABLE_TYPENAMES
  245. UA_LocalizedText_deleteMembers(&attr.displayName);
  246. UA_QualifiedName_deleteMembers(&qualifiedName);
  247. #endif
  248. }
  249. /* Hierarchy of depth 10 for CTT testing with forward and inverse references */
  250. /* Enter node "depth 9" in CTT configuration - Project->Settings->Server
  251. Test->NodeIds->Paths->Starting Node 1 */
  252. object_attr.description = UA_LOCALIZEDTEXT("en-US","DepthDemo");
  253. object_attr.displayName = UA_LOCALIZEDTEXT("en-US","DepthDemo");
  254. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEPTHID),
  255. UA_NODEID_NUMERIC(1, DEMOID),
  256. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "DepthDemo"),
  257. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  258. id = DEPTHID; // running id in namespace 0 - Start with Matrix NODE
  259. for(UA_UInt32 i = 1; i <= 20; i++) {
  260. char name[15];
  261. #if defined(_WIN32) && !defined(__MINGW32__)
  262. sprintf_s(name, 15, "depth%i", i);
  263. #else
  264. sprintf(name, "depth%i", i);
  265. #endif
  266. object_attr.description = UA_LOCALIZEDTEXT("en-US",name);
  267. object_attr.displayName = UA_LOCALIZEDTEXT("en-US",name);
  268. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, id+i),
  269. UA_NODEID_NUMERIC(1, i==1 ? DEPTHID : id+i-1), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  270. UA_QUALIFIEDNAME(1, name),
  271. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  272. }
  273. /* Add the variable to some more places to get a node with three inverse references for the CTT */
  274. UA_ExpandedNodeId answer_nodeid = UA_EXPANDEDNODEID_STRING(1, "the.answer");
  275. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, DEMOID),
  276. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  277. UA_Server_addReference(server, UA_NODEID_NUMERIC(1, SCALARID),
  278. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), answer_nodeid, true);
  279. /* Example for manually setting an attribute within the server */
  280. UA_LocalizedText objectsName = UA_LOCALIZEDTEXT("en-US", "Objects");
  281. UA_Server_writeDisplayName(server, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), objectsName);
  282. #define NOARGID 60000
  283. #define INARGID 60001
  284. #define OUTARGID 60002
  285. #define INOUTARGID 60003
  286. #ifdef UA_ENABLE_METHODCALLS
  287. /* adding some more method nodes to pass CTT */
  288. /* Method without arguments */
  289. addmethodattributes = UA_MethodAttributes_default;
  290. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "noarg");
  291. addmethodattributes.executable = true;
  292. addmethodattributes.userExecutable = true;
  293. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, NOARGID),
  294. UA_NODEID_NUMERIC(1, DEMOID),
  295. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  296. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  297. &noargMethod, /* callback of the method node */
  298. 0, NULL, 0, NULL, NULL, NULL);
  299. /* Method with in arguments */
  300. addmethodattributes = UA_MethodAttributes_default;
  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. 1, &inputArguments, 0, NULL, NULL, NULL);
  315. /* Method with out arguments */
  316. addmethodattributes = UA_MethodAttributes_default;
  317. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "outarg");
  318. addmethodattributes.executable = true;
  319. addmethodattributes.userExecutable = true;
  320. UA_Argument_init(&outputArguments);
  321. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  322. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Output");
  323. outputArguments.name = UA_STRING("Output");
  324. outputArguments.valueRank = -1;
  325. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, OUTARGID),
  326. UA_NODEID_NUMERIC(1, DEMOID),
  327. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  328. UA_QUALIFIEDNAME(1, "outarg"), addmethodattributes,
  329. &outargMethod, /* callback of the method node */
  330. 0, NULL, 1, &outputArguments, NULL, NULL);
  331. /* Method with inout arguments */
  332. addmethodattributes = UA_MethodAttributes_default;
  333. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inoutarg");
  334. addmethodattributes.executable = true;
  335. addmethodattributes.userExecutable = true;
  336. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INOUTARGID),
  337. UA_NODEID_NUMERIC(1, DEMOID),
  338. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  339. UA_QUALIFIEDNAME(1, "inoutarg"), addmethodattributes,
  340. &outargMethod, /* callback of the method node */
  341. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  342. #endif
  343. /* run server */
  344. UA_StatusCode retval = UA_Server_run(server, &running);
  345. UA_Server_delete(server);
  346. UA_ServerConfig_delete(config);
  347. return (int)retval;
  348. }