server.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. UA_Server *server = UA_Server_new(config);
  108. /* add a static variable node to the server */
  109. UA_VariableAttributes myVar = UA_VariableAttributes_default;
  110. myVar.description = UA_LOCALIZEDTEXT("en-US", "the answer");
  111. myVar.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
  112. myVar.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  113. UA_Int32 myInteger = 42;
  114. UA_Variant_setScalarCopy(&myVar.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  115. const UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  116. const UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  117. UA_NodeId parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  118. UA_NodeId parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  119. UA_Server_addVariableNode(server, myIntegerNodeId, parentNodeId, parentReferenceNodeId,
  120. myIntegerName, UA_NODEID_NULL, myVar, NULL, NULL);
  121. UA_Variant_deleteMembers(&myVar.value);
  122. /* add a variable with the datetime data source */
  123. UA_DataSource dateDataSource;
  124. dateDataSource.read = readTimeData;
  125. dateDataSource.write = NULL;
  126. UA_VariableAttributes v_attr = UA_VariableAttributes_default;
  127. v_attr.description = UA_LOCALIZEDTEXT("en-US","current time");
  128. v_attr.displayName = UA_LOCALIZEDTEXT("en-US","current time");
  129. v_attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  130. const UA_QualifiedName dateName = UA_QUALIFIEDNAME(1, "current time");
  131. UA_NodeId dataSourceId;
  132. UA_Server_addDataSourceVariableNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  133. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), dateName,
  134. UA_NODEID_NULL, v_attr, dateDataSource, NULL, &dataSourceId);
  135. /* Add HelloWorld method to the server */
  136. #ifdef UA_ENABLE_METHODCALLS
  137. /* Method with IO Arguments */
  138. UA_Argument inputArguments;
  139. UA_Argument_init(&inputArguments);
  140. inputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  141. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Say your name");
  142. inputArguments.name = UA_STRING("Name");
  143. inputArguments.valueRank = -1; /* scalar argument */
  144. UA_Argument outputArguments;
  145. UA_Argument_init(&outputArguments);
  146. outputArguments.arrayDimensionsSize = 0;
  147. outputArguments.arrayDimensions = NULL;
  148. outputArguments.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
  149. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Receive a greeting");
  150. outputArguments.name = UA_STRING("greeting");
  151. outputArguments.valueRank = -1;
  152. UA_MethodAttributes addmethodattributes = UA_MethodAttributes_default;
  153. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "Hello World");
  154. addmethodattributes.executable = true;
  155. addmethodattributes.userExecutable = true;
  156. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, 62541),
  157. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  158. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  159. UA_QUALIFIEDNAME(1, "hello_world"), addmethodattributes,
  160. &helloWorld, /* callback of the method node */
  161. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  162. #endif
  163. /* Add folders for demo information model */
  164. #define DEMOID 50000
  165. #define SCALARID 50001
  166. #define ARRAYID 50002
  167. #define MATRIXID 50003
  168. #define DEPTHID 50004
  169. UA_ObjectAttributes object_attr = UA_ObjectAttributes_default;
  170. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Demo");
  171. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Demo");
  172. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, DEMOID),
  173. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  174. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Demo"),
  175. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  176. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Scalar");
  177. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Scalar");
  178. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, SCALARID),
  179. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  180. UA_QUALIFIEDNAME(1, "Scalar"),
  181. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  182. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Array");
  183. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Array");
  184. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, ARRAYID),
  185. UA_NODEID_NUMERIC(1, DEMOID), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  186. UA_QUALIFIEDNAME(1, "Array"),
  187. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  188. object_attr.description = UA_LOCALIZEDTEXT("en-US", "Matrix");
  189. object_attr.displayName = UA_LOCALIZEDTEXT("en-US", "Matrix");
  190. UA_Server_addObjectNode(server, UA_NODEID_NUMERIC(1, MATRIXID), UA_NODEID_NUMERIC(1, DEMOID),
  191. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "Matrix"),
  192. UA_NODEID_NUMERIC(0, UA_NS0ID_FOLDERTYPE), object_attr, NULL, NULL);
  193. /* Fill demo nodes for each type*/
  194. UA_UInt32 id = 51000; // running id in namespace 0
  195. for(UA_UInt32 type = 0; type < UA_TYPES_DIAGNOSTICINFO; type++) {
  196. if(type == UA_TYPES_VARIANT || type == UA_TYPES_DIAGNOSTICINFO)
  197. continue;
  198. UA_VariableAttributes attr = UA_VariableAttributes_default;
  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. addmethodattributes = UA_MethodAttributes_default;
  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. 0, NULL, 0, NULL, NULL, NULL);
  298. /* Method with in arguments */
  299. addmethodattributes = UA_MethodAttributes_default;
  300. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inarg");
  301. addmethodattributes.executable = true;
  302. addmethodattributes.userExecutable = true;
  303. UA_Argument_init(&inputArguments);
  304. inputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  305. inputArguments.description = UA_LOCALIZEDTEXT("en-US", "Input");
  306. inputArguments.name = UA_STRING("Input");
  307. inputArguments.valueRank = -1; //uaexpert will crash if set to 0 ;)
  308. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INARGID),
  309. UA_NODEID_NUMERIC(1, DEMOID),
  310. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  311. UA_QUALIFIEDNAME(1, "noarg"), addmethodattributes,
  312. &noargMethod, /* callback of the method node */
  313. 1, &inputArguments, 0, NULL, NULL, NULL);
  314. /* Method with out arguments */
  315. addmethodattributes = UA_MethodAttributes_default;
  316. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "outarg");
  317. addmethodattributes.executable = true;
  318. addmethodattributes.userExecutable = true;
  319. UA_Argument_init(&outputArguments);
  320. outputArguments.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  321. outputArguments.description = UA_LOCALIZEDTEXT("en-US", "Output");
  322. outputArguments.name = UA_STRING("Output");
  323. outputArguments.valueRank = -1;
  324. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, OUTARGID),
  325. UA_NODEID_NUMERIC(1, DEMOID),
  326. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  327. UA_QUALIFIEDNAME(1, "outarg"), addmethodattributes,
  328. &outargMethod, /* callback of the method node */
  329. 0, NULL, 1, &outputArguments, NULL, NULL);
  330. /* Method with inout arguments */
  331. addmethodattributes = UA_MethodAttributes_default;
  332. addmethodattributes.displayName = UA_LOCALIZEDTEXT("en-US", "inoutarg");
  333. addmethodattributes.executable = true;
  334. addmethodattributes.userExecutable = true;
  335. UA_Server_addMethodNode(server, UA_NODEID_NUMERIC(1, INOUTARGID),
  336. UA_NODEID_NUMERIC(1, DEMOID),
  337. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  338. UA_QUALIFIEDNAME(1, "inoutarg"), addmethodattributes,
  339. &outargMethod, /* callback of the method node */
  340. 1, &inputArguments, 1, &outputArguments, NULL, NULL);
  341. #endif
  342. /* run server */
  343. UA_StatusCode retval = UA_Server_run(server, &running);
  344. UA_Server_delete(server);
  345. UA_ServerConfig_delete(config);
  346. return (int)retval;
  347. }