server.c 17 KB

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