ua_server.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef UA_SERVER_H_
  5. #define UA_SERVER_H_
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include "ua_types.h"
  10. #include "ua_types_generated.h"
  11. #include "ua_types_generated_handling.h"
  12. #include "ua_nodeids.h"
  13. #include "ua_plugin_log.h"
  14. #include "ua_plugin_network.h"
  15. #include "ua_plugin_access_control.h"
  16. /**
  17. * .. _server:
  18. *
  19. * Server
  20. * ======
  21. *
  22. * Server Configuration
  23. * --------------------
  24. * The configuration structure is passed to the server during initialization. */
  25. typedef struct {
  26. UA_UInt32 min;
  27. UA_UInt32 max;
  28. } UA_UInt32Range;
  29. typedef struct {
  30. UA_Double min;
  31. UA_Double max;
  32. } UA_DoubleRange;
  33. typedef struct {
  34. UA_UInt16 nThreads; /* only if multithreading is enabled */
  35. UA_Logger logger;
  36. /* Server Description */
  37. UA_BuildInfo buildInfo;
  38. UA_ApplicationDescription applicationDescription;
  39. UA_ByteString serverCertificate;
  40. #ifdef UA_ENABLE_DISCOVERY
  41. UA_String mdnsServerName;
  42. size_t serverCapabilitiesSize;
  43. UA_String *serverCapabilities;
  44. #endif
  45. /* Custom DataTypes */
  46. size_t customDataTypesSize;
  47. const UA_DataType *customDataTypes;
  48. /* Networking */
  49. size_t networkLayersSize;
  50. UA_ServerNetworkLayer *networkLayers;
  51. /* Access Control */
  52. UA_AccessControl accessControl;
  53. /* Limits for SecureChannels */
  54. UA_UInt16 maxSecureChannels;
  55. UA_UInt32 maxSecurityTokenLifetime; /* in ms */
  56. /* Limits for Sessions */
  57. UA_UInt16 maxSessions;
  58. UA_Double maxSessionTimeout; /* in ms */
  59. /* Limits for Subscriptions */
  60. UA_DoubleRange publishingIntervalLimits;
  61. UA_UInt32Range lifeTimeCountLimits;
  62. UA_UInt32Range keepAliveCountLimits;
  63. UA_UInt32 maxNotificationsPerPublish;
  64. UA_UInt32 maxRetransmissionQueueSize; /* 0 -> unlimited size */
  65. /* Limits for MonitoredItems */
  66. UA_DoubleRange samplingIntervalLimits;
  67. UA_UInt32Range queueSizeLimits; /* Negotiated with the client */
  68. /* Discovery */
  69. #ifdef UA_ENABLE_DISCOVERY
  70. /* Timeout in seconds when to automatically remove a registered server from
  71. * the list, if it doesn't re-register within the given time frame. A value
  72. * of 0 disables automatic removal. Default is 60 Minutes (60*60). Must be
  73. * bigger than 10 seconds, because cleanup is only triggered approximately
  74. * ervery 10 seconds. The server will still be removed depending on the
  75. * state of the semaphore file. */
  76. UA_UInt32 discoveryCleanupTimeout;
  77. #endif
  78. } UA_ServerConfig;
  79. /**
  80. * .. _server-lifecycle:
  81. *
  82. * Server Lifecycle
  83. * ---------------- */
  84. UA_Server UA_EXPORT * UA_Server_new(const UA_ServerConfig config);
  85. void UA_EXPORT UA_Server_delete(UA_Server *server);
  86. /* Runs the main loop of the server. In each iteration, this calls into the
  87. * networklayers to see if messages have arrived.
  88. *
  89. * @param server The server object.
  90. * @param running The loop is run as long as *running is true.
  91. * Otherwise, the server shuts down.
  92. * @return Returns the statuscode of the UA_Server_run_shutdown method */
  93. UA_StatusCode UA_EXPORT
  94. UA_Server_run(UA_Server *server, volatile UA_Boolean *running);
  95. /* The prologue part of UA_Server_run (no need to use if you call
  96. * UA_Server_run) */
  97. UA_StatusCode UA_EXPORT UA_Server_run_startup(UA_Server *server);
  98. /* Executes a single iteration of the server's main loop.
  99. *
  100. * @param server The server object.
  101. * @param waitInternal Should we wait for messages in the networklayer?
  102. * Otherwise, the timouts for the networklayers are set to zero.
  103. * The default max wait time is 50millisec.
  104. * @return Returns how long we can wait until the next scheduled
  105. * callback (in ms) */
  106. UA_UInt16 UA_EXPORT
  107. UA_Server_run_iterate(UA_Server *server, UA_Boolean waitInternal);
  108. /* The epilogue part of UA_Server_run (no need to use if you call
  109. * UA_Server_run) */
  110. UA_StatusCode UA_EXPORT UA_Server_run_shutdown(UA_Server *server);
  111. /**
  112. * Repeated Callbacks
  113. * ------------------ */
  114. typedef void (*UA_ServerCallback)(UA_Server *server, void *data);
  115. /* Add a callback for cyclic repetition to the server.
  116. *
  117. * @param server The server object.
  118. * @param callback The callback that shall be added.
  119. * @param interval The callback shall be repeatedly executed with the given interval
  120. * (in ms). The interval must be larger than 5ms. The first execution
  121. * occurs at now() + interval at the latest.
  122. * @param callbackId Set to the identifier of the repeated callback . This can be used to cancel
  123. * the callback later on. If the pointer is null, the identifier is not set.
  124. * @return Upon success, UA_STATUSCODE_GOOD is returned.
  125. * An error code otherwise. */
  126. UA_StatusCode UA_EXPORT
  127. UA_Server_addRepeatedCallback(UA_Server *server, UA_ServerCallback callback,
  128. void *data, UA_UInt32 interval, UA_UInt64 *callbackId);
  129. UA_StatusCode UA_EXPORT
  130. UA_Server_changeRepeatedCallbackInterval(UA_Server *server, UA_UInt64 callbackId,
  131. UA_UInt32 interval);
  132. /* Remove a repeated callback.
  133. *
  134. * @param server The server object.
  135. * @param callbackId The id of the callback that shall be removed.
  136. * @return Upon sucess, UA_STATUSCODE_GOOD is returned.
  137. * An error code otherwise. */
  138. UA_StatusCode UA_EXPORT
  139. UA_Server_removeRepeatedCallback(UA_Server *server, UA_UInt64 callbackId);
  140. /**
  141. * Reading and Writing Node Attributes
  142. * -----------------------------------
  143. * The functions for reading and writing node attributes call the regular read
  144. * and write service in the background that are also used over the network.
  145. *
  146. * The following attributes cannot be read, since the local "admin" user always
  147. * has full rights.
  148. *
  149. * - UserWriteMask
  150. * - UserAccessLevel
  151. * - UserExecutable */
  152. /* Read an attribute of a node. The specialized functions below provide a more
  153. * concise syntax.
  154. *
  155. * @param server The server object.
  156. * @param item ReadValueIds contain the NodeId of the target node, the id of the
  157. * attribute to read and (optionally) an index range to read parts
  158. * of an array only. See the section on NumericRange for the format
  159. * used for array ranges.
  160. * @param timestamps Which timestamps to return for the attribute.
  161. * @return Returns a DataValue that contains either an error code, or a variant
  162. * with the attribute value and the timestamps. */
  163. UA_DataValue UA_EXPORT
  164. UA_Server_read(UA_Server *server, const UA_ReadValueId *item,
  165. UA_TimestampsToReturn timestamps);
  166. /* Don't use this function. There are typed versions for every supported
  167. * attribute. */
  168. UA_StatusCode UA_EXPORT
  169. __UA_Server_read(UA_Server *server, const UA_NodeId *nodeId,
  170. UA_AttributeId attributeId, void *v);
  171. static UA_INLINE UA_StatusCode
  172. UA_Server_readNodeId(UA_Server *server, const UA_NodeId nodeId,
  173. UA_NodeId *outNodeId) {
  174. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_NODEID, outNodeId);
  175. }
  176. static UA_INLINE UA_StatusCode
  177. UA_Server_readNodeClass(UA_Server *server, const UA_NodeId nodeId,
  178. UA_NodeClass *outNodeClass) {
  179. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_NODECLASS,
  180. outNodeClass);
  181. }
  182. static UA_INLINE UA_StatusCode
  183. UA_Server_readBrowseName(UA_Server *server, const UA_NodeId nodeId,
  184. UA_QualifiedName *outBrowseName) {
  185. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_BROWSENAME,
  186. outBrowseName);
  187. }
  188. static UA_INLINE UA_StatusCode
  189. UA_Server_readDisplayName(UA_Server *server, const UA_NodeId nodeId,
  190. UA_LocalizedText *outDisplayName) {
  191. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_DISPLAYNAME,
  192. outDisplayName);
  193. }
  194. static UA_INLINE UA_StatusCode
  195. UA_Server_readDescription(UA_Server *server, const UA_NodeId nodeId,
  196. UA_LocalizedText *outDescription) {
  197. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_DESCRIPTION,
  198. outDescription);
  199. }
  200. static UA_INLINE UA_StatusCode
  201. UA_Server_readWriteMask(UA_Server *server, const UA_NodeId nodeId,
  202. UA_UInt32 *outWriteMask) {
  203. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_WRITEMASK,
  204. outWriteMask);
  205. }
  206. static UA_INLINE UA_StatusCode
  207. UA_Server_readIsAbstract(UA_Server *server, const UA_NodeId nodeId,
  208. UA_Boolean *outIsAbstract) {
  209. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_ISABSTRACT,
  210. outIsAbstract);
  211. }
  212. static UA_INLINE UA_StatusCode
  213. UA_Server_readSymmetric(UA_Server *server, const UA_NodeId nodeId,
  214. UA_Boolean *outSymmetric) {
  215. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_SYMMETRIC,
  216. outSymmetric);
  217. }
  218. static UA_INLINE UA_StatusCode
  219. UA_Server_readInverseName(UA_Server *server, const UA_NodeId nodeId,
  220. UA_LocalizedText *outInverseName) {
  221. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_INVERSENAME,
  222. outInverseName);
  223. }
  224. static UA_INLINE UA_StatusCode
  225. UA_Server_readContainsNoLoop(UA_Server *server, const UA_NodeId nodeId,
  226. UA_Boolean *outContainsNoLoops) {
  227. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_CONTAINSNOLOOPS,
  228. outContainsNoLoops);
  229. }
  230. static UA_INLINE UA_StatusCode
  231. UA_Server_readEventNotifier(UA_Server *server, const UA_NodeId nodeId,
  232. UA_Byte *outEventNotifier) {
  233. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_EVENTNOTIFIER,
  234. outEventNotifier);
  235. }
  236. static UA_INLINE UA_StatusCode
  237. UA_Server_readValue(UA_Server *server, const UA_NodeId nodeId,
  238. UA_Variant *outValue) {
  239. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_VALUE, outValue);
  240. }
  241. static UA_INLINE UA_StatusCode
  242. UA_Server_readDataType(UA_Server *server, const UA_NodeId nodeId,
  243. UA_NodeId *outDataType) {
  244. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_DATATYPE,
  245. outDataType);
  246. }
  247. static UA_INLINE UA_StatusCode
  248. UA_Server_readValueRank(UA_Server *server, const UA_NodeId nodeId,
  249. UA_Int32 *outValueRank) {
  250. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_VALUERANK,
  251. outValueRank);
  252. }
  253. /* Returns a variant with an int32 array */
  254. static UA_INLINE UA_StatusCode
  255. UA_Server_readArrayDimensions(UA_Server *server, const UA_NodeId nodeId,
  256. UA_Variant *outArrayDimensions) {
  257. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_ARRAYDIMENSIONS,
  258. outArrayDimensions);
  259. }
  260. static UA_INLINE UA_StatusCode
  261. UA_Server_readAccessLevel(UA_Server *server, const UA_NodeId nodeId,
  262. UA_Byte *outAccessLevel) {
  263. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_ACCESSLEVEL,
  264. outAccessLevel);
  265. }
  266. static UA_INLINE UA_StatusCode
  267. UA_Server_readMinimumSamplingInterval(UA_Server *server, const UA_NodeId nodeId,
  268. UA_Double *outMinimumSamplingInterval) {
  269. return __UA_Server_read(server, &nodeId,
  270. UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL,
  271. outMinimumSamplingInterval);
  272. }
  273. static UA_INLINE UA_StatusCode
  274. UA_Server_readHistorizing(UA_Server *server, const UA_NodeId nodeId,
  275. UA_Boolean *outHistorizing) {
  276. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_HISTORIZING,
  277. outHistorizing);
  278. }
  279. static UA_INLINE UA_StatusCode
  280. UA_Server_readExecutable(UA_Server *server, const UA_NodeId nodeId,
  281. UA_Boolean *outExecutable) {
  282. return __UA_Server_read(server, &nodeId, UA_ATTRIBUTEID_EXECUTABLE,
  283. outExecutable);
  284. }
  285. /**
  286. * The following node attributes cannot be changed once a node has been created:
  287. *
  288. * - NodeClass
  289. * - NodeId
  290. * - Symmetric
  291. * - ContainsNoLoop
  292. *
  293. * The following attributes cannot be written from the server, as they are
  294. * specific to the different users and set by the access control callback:
  295. *
  296. * - UserWriteMask
  297. * - UserAccessLevel
  298. * - UserExecutable
  299. *
  300. * Historizing is currently unsupported */
  301. /* Overwrite an attribute of a node. The specialized functions below provide a
  302. * more concise syntax.
  303. *
  304. * @param server The server object.
  305. * @param value WriteValues contain the NodeId of the target node, the id of the
  306. * attribute to overwritten, the actual value and (optionally) an
  307. * index range to replace parts of an array only. of an array only.
  308. * See the section on NumericRange for the format used for array
  309. * ranges.
  310. * @return Returns a status code. */
  311. UA_StatusCode UA_EXPORT
  312. UA_Server_write(UA_Server *server, const UA_WriteValue *value);
  313. /* Don't use this function. There are typed versions with no additional
  314. * overhead. */
  315. UA_StatusCode UA_EXPORT
  316. __UA_Server_write(UA_Server *server, const UA_NodeId *nodeId,
  317. const UA_AttributeId attributeId,
  318. const UA_DataType *attr_type, const void *attr);
  319. static UA_INLINE UA_StatusCode
  320. UA_Server_writeBrowseName(UA_Server *server, const UA_NodeId nodeId,
  321. const UA_QualifiedName browseName) {
  322. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_BROWSENAME,
  323. &UA_TYPES[UA_TYPES_QUALIFIEDNAME], &browseName);
  324. }
  325. static UA_INLINE UA_StatusCode
  326. UA_Server_writeDisplayName(UA_Server *server, const UA_NodeId nodeId,
  327. const UA_LocalizedText displayName) {
  328. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_DISPLAYNAME,
  329. &UA_TYPES[UA_TYPES_LOCALIZEDTEXT], &displayName);
  330. }
  331. static UA_INLINE UA_StatusCode
  332. UA_Server_writeDescription(UA_Server *server, const UA_NodeId nodeId,
  333. const UA_LocalizedText description) {
  334. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_DESCRIPTION,
  335. &UA_TYPES[UA_TYPES_LOCALIZEDTEXT], &description);
  336. }
  337. static UA_INLINE UA_StatusCode
  338. UA_Server_writeWriteMask(UA_Server *server, const UA_NodeId nodeId,
  339. const UA_UInt32 writeMask) {
  340. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_WRITEMASK,
  341. &UA_TYPES[UA_TYPES_UINT32], &writeMask);
  342. }
  343. static UA_INLINE UA_StatusCode
  344. UA_Server_writeIsAbstract(UA_Server *server, const UA_NodeId nodeId,
  345. const UA_Boolean isAbstract) {
  346. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_ISABSTRACT,
  347. &UA_TYPES[UA_TYPES_BOOLEAN], &isAbstract);
  348. }
  349. static UA_INLINE UA_StatusCode
  350. UA_Server_writeInverseName(UA_Server *server, const UA_NodeId nodeId,
  351. const UA_LocalizedText inverseName) {
  352. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_INVERSENAME,
  353. &UA_TYPES[UA_TYPES_LOCALIZEDTEXT], &inverseName);
  354. }
  355. static UA_INLINE UA_StatusCode
  356. UA_Server_writeEventNotifier(UA_Server *server, const UA_NodeId nodeId,
  357. const UA_Byte eventNotifier) {
  358. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_EVENTNOTIFIER,
  359. &UA_TYPES[UA_TYPES_BYTE], &eventNotifier);
  360. }
  361. static UA_INLINE UA_StatusCode
  362. UA_Server_writeValue(UA_Server *server, const UA_NodeId nodeId,
  363. const UA_Variant value) {
  364. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_VALUE,
  365. &UA_TYPES[UA_TYPES_VARIANT], &value);
  366. }
  367. static UA_INLINE UA_StatusCode
  368. UA_Server_writeDataType(UA_Server *server, const UA_NodeId nodeId,
  369. const UA_NodeId dataType) {
  370. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_DATATYPE,
  371. &UA_TYPES[UA_TYPES_NODEID], &dataType);
  372. }
  373. static UA_INLINE UA_StatusCode
  374. UA_Server_writeValueRank(UA_Server *server, const UA_NodeId nodeId,
  375. const UA_Int32 valueRank) {
  376. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_VALUERANK,
  377. &UA_TYPES[UA_TYPES_INT32], &valueRank);
  378. }
  379. static UA_INLINE UA_StatusCode
  380. UA_Server_writeArrayDimensions(UA_Server *server, const UA_NodeId nodeId,
  381. const UA_Variant arrayDimensions) {
  382. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_VALUE,
  383. &UA_TYPES[UA_TYPES_VARIANT], &arrayDimensions);
  384. }
  385. static UA_INLINE UA_StatusCode
  386. UA_Server_writeAccessLevel(UA_Server *server, const UA_NodeId nodeId,
  387. const UA_Byte accessLevel) {
  388. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_ACCESSLEVEL,
  389. &UA_TYPES[UA_TYPES_BYTE], &accessLevel);
  390. }
  391. static UA_INLINE UA_StatusCode
  392. UA_Server_writeMinimumSamplingInterval(UA_Server *server, const UA_NodeId nodeId,
  393. const UA_Double miniumSamplingInterval) {
  394. return __UA_Server_write(server, &nodeId,
  395. UA_ATTRIBUTEID_MINIMUMSAMPLINGINTERVAL,
  396. &UA_TYPES[UA_TYPES_DOUBLE],
  397. &miniumSamplingInterval);
  398. }
  399. static UA_INLINE UA_StatusCode
  400. UA_Server_writeExecutable(UA_Server *server, const UA_NodeId nodeId,
  401. const UA_Boolean executable) {
  402. return __UA_Server_write(server, &nodeId, UA_ATTRIBUTEID_EXECUTABLE,
  403. &UA_TYPES[UA_TYPES_BOOLEAN], &executable); }
  404. /**
  405. * Browsing
  406. * -------- */
  407. UA_BrowseResult UA_EXPORT
  408. UA_Server_browse(UA_Server *server, UA_UInt32 maxrefs,
  409. const UA_BrowseDescription *descr);
  410. UA_BrowseResult UA_EXPORT
  411. UA_Server_browseNext(UA_Server *server, UA_Boolean releaseContinuationPoint,
  412. const UA_ByteString *continuationPoint);
  413. UA_BrowsePathResult UA_EXPORT
  414. UA_Server_translateBrowsePathToNodeIds(UA_Server *server,
  415. const UA_BrowsePath *browsePath);
  416. #ifndef HAVE_NODEITER_CALLBACK
  417. #define HAVE_NODEITER_CALLBACK
  418. /* Iterate over all nodes referenced by parentNodeId by calling the callback
  419. * function for each child node (in ifdef because GCC/CLANG handle include order
  420. * differently) */
  421. typedef UA_StatusCode
  422. (*UA_NodeIteratorCallback)(UA_NodeId childId, UA_Boolean isInverse,
  423. UA_NodeId referenceTypeId, void *handle);
  424. #endif
  425. UA_StatusCode UA_EXPORT
  426. UA_Server_forEachChildNodeCall(UA_Server *server, UA_NodeId parentNodeId,
  427. UA_NodeIteratorCallback callback, void *handle);
  428. #ifdef UA_ENABLE_DISCOVERY
  429. /**
  430. * Discovery
  431. * --------- */
  432. /* Register the given server instance at the discovery server.
  433. * This should be called periodically.
  434. * The semaphoreFilePath is optional. If the given file is deleted,
  435. * the server will automatically be unregistered. This could be
  436. * for example a pid file which is deleted if the server crashes.
  437. *
  438. * When the server shuts down you need to call unregister.
  439. *
  440. * @param server
  441. * @param discoveryServerUrl if set to NULL, the default value
  442. * 'opc.tcp://localhost:4840' will be used
  443. * @param semaphoreFilePath optional parameter pointing to semaphore file. */
  444. UA_StatusCode UA_EXPORT
  445. UA_Server_register_discovery(UA_Server *server, const char* discoveryServerUrl,
  446. const char* semaphoreFilePath);
  447. /* Unregister the given server instance from the discovery server.
  448. * This should only be called when the server is shutting down.
  449. * @param server
  450. * @param discoveryServerUrl if set to NULL, the default value
  451. * 'opc.tcp://localhost:4840' will be used */
  452. UA_StatusCode UA_EXPORT
  453. UA_Server_unregister_discovery(UA_Server *server, const char* discoveryServerUrl);
  454. /* Adds a periodic callback to register the server with the LDS (local discovery server)
  455. * periodically. The interval between each register call is given as second parameter.
  456. * It should be 10 minutes by default (= 10*60*1000).
  457. *
  458. * The delayFirstRegisterMs parameter indicates the delay for the first register call.
  459. * If it is 0, the first register call will be after intervalMs milliseconds,
  460. * otherwise the server's first register will be after delayFirstRegisterMs.
  461. *
  462. * When you manually unregister the server, you also need to cancel the
  463. * periodic callback, otherwise it will be automatically be registered again.
  464. *
  465. * @param server
  466. * @param discoveryServerUrl if set to NULL, the default value
  467. * 'opc.tcp://localhost:4840' will be used
  468. * @param intervalMs
  469. * @param delayFirstRegisterMs
  470. * @param periodicCallbackId */
  471. UA_StatusCode UA_EXPORT
  472. UA_Server_addPeriodicServerRegisterCallback(UA_Server *server, const char* discoveryServerUrl,
  473. UA_UInt32 intervalMs,
  474. UA_UInt32 delayFirstRegisterMs,
  475. UA_UInt64 *periodicCallbackId);
  476. /* Callback for RegisterServer. Data is passed from the register call */
  477. typedef void (*UA_Server_registerServerCallback)(const UA_RegisteredServer *registeredServer,
  478. void* data);
  479. /* Set the callback which is called if another server registeres or unregisters
  480. * with this instance. If called multiple times, previous data will be
  481. * overwritten.
  482. *
  483. * @param server
  484. * @param cb the callback
  485. * @param data data passed to the callback
  486. * @return UA_STATUSCODE_SUCCESS on success */
  487. void UA_EXPORT
  488. UA_Server_setRegisterServerCallback(UA_Server *server, UA_Server_registerServerCallback cb,
  489. void* data);
  490. #ifdef UA_ENABLE_DISCOVERY_MULTICAST
  491. /* Callback for server detected through mDNS. Data is passed from the register
  492. * call
  493. *
  494. * @param isServerAnnounce indicates if the server has just been detected. If
  495. * set to false, this means the server is shutting down.
  496. * @param isTxtReceived indicates if we already received the corresponding TXT
  497. * record with the path and caps data */
  498. typedef void (*UA_Server_serverOnNetworkCallback)(const UA_ServerOnNetwork *serverOnNetwork,
  499. UA_Boolean isServerAnnounce,
  500. UA_Boolean isTxtReceived, void* data);
  501. /* Set the callback which is called if another server is found through mDNS or
  502. * deleted. It will be called for any mDNS message from the remote server, thus
  503. * it may be called multiple times for the same instance. Also the SRV and TXT
  504. * records may arrive later, therefore for the first call the server
  505. * capabilities may not be set yet. If called multiple times, previous data will
  506. * be overwritten.
  507. *
  508. * @param server
  509. * @param cb the callback
  510. * @param data data passed to the callback
  511. * @return UA_STATUSCODE_SUCCESS on success */
  512. void UA_EXPORT
  513. UA_Server_setServerOnNetworkCallback(UA_Server *server,
  514. UA_Server_serverOnNetworkCallback cb,
  515. void* data);
  516. #endif /* UA_ENABLE_DISCOVERY_MULTICAST */
  517. #endif /* UA_ENABLE_DISCOVERY */
  518. /**
  519. * Method Call
  520. * ----------- */
  521. #ifdef UA_ENABLE_METHODCALLS
  522. UA_CallMethodResult UA_EXPORT
  523. UA_Server_call(UA_Server *server, const UA_CallMethodRequest *request);
  524. #endif
  525. /**
  526. * Node Management
  527. * ---------------
  528. *
  529. * Callback Mechanisms
  530. * ^^^^^^^^^^^^^^^^^^^
  531. * There are four mechanisms for callbacks from the node-based information model
  532. * into userspace:
  533. *
  534. * - Datasources for variable nodes, where the variable content is managed
  535. * externally
  536. * - Value-callbacks for variable nodes, where userspace is notified when a
  537. * read/write occurs
  538. * - Object lifecycle management, where a user-defined constructor and
  539. * destructor is added to an object type
  540. * - Method callbacks, where a user-defined method is exposed in the information
  541. * model
  542. *
  543. * .. _datasource:
  544. *
  545. * Data Source Callback
  546. * ~~~~~~~~~~~~~~~~~~~~
  547. *
  548. * The server has a unique way of dealing with the content of variables. Instead
  549. * of storing a variant attached to the variable node, the node can point to a
  550. * function with a local data provider. Whenever the value attribute is read,
  551. * the function will be called and asked to provide a UA_DataValue return value
  552. * that contains the value content and additional timestamps.
  553. *
  554. * It is expected that the read callback is implemented. The write callback can
  555. * be set to a null-pointer. */
  556. typedef struct {
  557. void *handle; /* A custom pointer to reuse the same datasource functions for
  558. multiple sources */
  559. /* Copies the data from the source into the provided value.
  560. *
  561. * @param handle An optional pointer to user-defined data for the
  562. * specific data source
  563. * @param nodeid Id of the read node
  564. * @param includeSourceTimeStamp If true, then the datasource is expected to
  565. * set the source timestamp in the returned value
  566. * @param range If not null, then the datasource shall return only a
  567. * selection of the (nonscalar) data. Set
  568. * UA_STATUSCODE_BADINDEXRANGEINVALID in the value if this does not
  569. * apply.
  570. * @param value The (non-null) DataValue that is returned to the client. The
  571. * data source sets the read data, the result status and optionally a
  572. * sourcetimestamp.
  573. * @return Returns a status code for logging. Error codes intended for the
  574. * original caller are set in the value. If an error is returned,
  575. * then no releasing of the value is done. */
  576. UA_StatusCode (*read)(void *handle, const UA_NodeId nodeid,
  577. UA_Boolean includeSourceTimeStamp,
  578. const UA_NumericRange *range, UA_DataValue *value);
  579. /* Write into a data source. The write member of UA_DataSource can be empty
  580. * if the operation is unsupported.
  581. *
  582. * @param handle An optional pointer to user-defined data for the
  583. * specific data source
  584. * @param nodeid Id of the node being written to
  585. * @param data The data to be written into the data source
  586. * @param range An optional data range. If the data source is scalar or does
  587. * not support writing of ranges, then an error code is returned.
  588. * @return Returns a status code that is returned to the user
  589. */
  590. UA_StatusCode (*write)(void *handle, const UA_NodeId nodeid,
  591. const UA_Variant *data, const UA_NumericRange *range);
  592. } UA_DataSource;
  593. UA_StatusCode UA_EXPORT
  594. UA_Server_setVariableNode_dataSource(UA_Server *server, const UA_NodeId nodeId,
  595. const UA_DataSource dataSource);
  596. /**
  597. * .. _value-callback:
  598. *
  599. * Value Callback
  600. * ~~~~~~~~~~~~~~
  601. * Value Callbacks can be attached to variable and variable type nodes. If
  602. * not-null, they are called before reading and after writing respectively. */
  603. typedef struct {
  604. /* Pointer to user-provided data for the callback */
  605. void *handle;
  606. /* Called before the value attribute is read. It is possible to write into the
  607. * value attribute during onRead (using the write service). The node is
  608. * re-opened afterwards so that changes are considered in the following read
  609. * operation.
  610. *
  611. * @param handle Points to user-provided data for the callback.
  612. * @param nodeid The identifier of the node.
  613. * @param data Points to the current node value.
  614. * @param range Points to the numeric range the client wants to read from
  615. * (or NULL). */
  616. void (*onRead)(void *handle, const UA_NodeId nodeid,
  617. const UA_Variant *data, const UA_NumericRange *range);
  618. /* Called after writing the value attribute. The node is re-opened after
  619. * writing so that the new value is visible in the callback.
  620. *
  621. * @param handle Points to user-provided data for the callback.
  622. * @param nodeid The identifier of the node.
  623. * @param data Points to the current node value (after writing).
  624. * @param range Points to the numeric range the client wants to write to (or
  625. * NULL). */
  626. void (*onWrite)(void *handle, const UA_NodeId nodeid,
  627. const UA_Variant *data, const UA_NumericRange *range);
  628. } UA_ValueCallback;
  629. UA_StatusCode UA_EXPORT
  630. UA_Server_setVariableNode_valueCallback(UA_Server *server, const UA_NodeId nodeId,
  631. const UA_ValueCallback callback);
  632. /**
  633. * .. _object-lifecycle:
  634. *
  635. * Object Lifecycle Management Callbacks
  636. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  637. * Lifecycle management adds constructor and destructor callbacks to
  638. * object types. */
  639. typedef struct {
  640. /* Returns the instance handle that is then attached to the node */
  641. void * (*constructor)(const UA_NodeId instance);
  642. void (*destructor)(const UA_NodeId instance, void *instanceHandle);
  643. } UA_ObjectLifecycleManagement;
  644. UA_StatusCode UA_EXPORT
  645. UA_Server_setObjectTypeNode_lifecycleManagement(UA_Server *server,
  646. UA_NodeId nodeId,
  647. UA_ObjectLifecycleManagement olm);
  648. /**
  649. * Method Callbacks
  650. * ~~~~~~~~~~~~~~~~ */
  651. typedef UA_StatusCode
  652. (*UA_MethodCallback)(void *methodHandle, const UA_NodeId *objectId,
  653. const UA_NodeId *sessionId, void *sessionHandle,
  654. size_t inputSize, const UA_Variant *input,
  655. size_t outputSize, UA_Variant *output);
  656. #ifdef UA_ENABLE_METHODCALLS
  657. UA_StatusCode UA_EXPORT
  658. UA_Server_setMethodNode_callback(UA_Server *server, const UA_NodeId methodNodeId,
  659. UA_MethodCallback method, void *handle);
  660. #endif
  661. /**
  662. * .. _addnodes:
  663. *
  664. * Node Addition and Deletion
  665. * ^^^^^^^^^^^^^^^^^^^^^^^^^^
  666. *
  667. * When creating dynamic node instances at runtime, chances are that you will
  668. * not care about the specific NodeId of the new node, as long as you can
  669. * reference it later. When passing numeric NodeIds with a numeric identifier 0,
  670. * the stack evaluates this as "select a random unassigned numeric NodeId in
  671. * that namespace". To find out which NodeId was actually assigned to the new
  672. * node, you may pass a pointer `outNewNodeId`, which will (after a successfull
  673. * node insertion) contain the nodeId of the new node. You may also pass NULL
  674. * pointer if this result is not relevant. The namespace index for nodes you
  675. * create should never be 0, as that index is reserved for OPC UA's
  676. * self-description (namespace * 0).
  677. *
  678. * The methods for node addition and deletion take mostly const arguments that
  679. * are not modified. When creating a node, a deep copy of the node identifier,
  680. * node attributes, etc. is created. Therefore, it is possible to call for
  681. * example `UA_Server_addVariablenode` with a value attribute (a :ref:`variant`)
  682. * pointing to a memory location on the stack. If you need changes to a variable
  683. * value to manifest at a specific memory location, please use a
  684. * :ref:`datasource` or a :ref:`value-callback`. */
  685. /* The instantiation callback is used to track the addition of new nodes. It is
  686. * also called for all sub-nodes contained in an object or variable type node
  687. * that is instantiated. */
  688. typedef struct {
  689. UA_StatusCode (*method)(const UA_NodeId objectId,
  690. const UA_NodeId typeDefinitionId, void *handle);
  691. void *handle;
  692. } UA_InstantiationCallback;
  693. /* Don't use this function. There are typed versions as inline functions. */
  694. UA_StatusCode UA_EXPORT
  695. __UA_Server_addNode(UA_Server *server, const UA_NodeClass nodeClass,
  696. const UA_NodeId *requestedNewNodeId,
  697. const UA_NodeId *parentNodeId,
  698. const UA_NodeId *referenceTypeId,
  699. const UA_QualifiedName browseName,
  700. const UA_NodeId *typeDefinition,
  701. const UA_NodeAttributes *attr,
  702. const UA_DataType *attributeType,
  703. UA_InstantiationCallback *instantiationCallback,
  704. UA_NodeId *outNewNodeId);
  705. static UA_INLINE UA_StatusCode
  706. UA_Server_addVariableNode(UA_Server *server, const UA_NodeId requestedNewNodeId,
  707. const UA_NodeId parentNodeId,
  708. const UA_NodeId referenceTypeId,
  709. const UA_QualifiedName browseName,
  710. const UA_NodeId typeDefinition,
  711. const UA_VariableAttributes attr,
  712. UA_InstantiationCallback *instantiationCallback,
  713. UA_NodeId *outNewNodeId) {
  714. return __UA_Server_addNode(server, UA_NODECLASS_VARIABLE, &requestedNewNodeId,
  715. &parentNodeId, &referenceTypeId, browseName,
  716. &typeDefinition, (const UA_NodeAttributes*)&attr,
  717. &UA_TYPES[UA_TYPES_VARIABLEATTRIBUTES],
  718. instantiationCallback, outNewNodeId);
  719. }
  720. static UA_INLINE UA_StatusCode
  721. UA_Server_addVariableTypeNode(UA_Server *server,
  722. const UA_NodeId requestedNewNodeId,
  723. const UA_NodeId parentNodeId,
  724. const UA_NodeId referenceTypeId,
  725. const UA_QualifiedName browseName,
  726. const UA_NodeId typeDefinition,
  727. const UA_VariableTypeAttributes attr,
  728. UA_InstantiationCallback *instantiationCallback,
  729. UA_NodeId *outNewNodeId) {
  730. return __UA_Server_addNode(server, UA_NODECLASS_VARIABLETYPE,
  731. &requestedNewNodeId, &parentNodeId, &referenceTypeId,
  732. browseName, &typeDefinition,
  733. (const UA_NodeAttributes*)&attr,
  734. &UA_TYPES[UA_TYPES_VARIABLETYPEATTRIBUTES],
  735. instantiationCallback, outNewNodeId);
  736. }
  737. static UA_INLINE UA_StatusCode
  738. UA_Server_addObjectNode(UA_Server *server, const UA_NodeId requestedNewNodeId,
  739. const UA_NodeId parentNodeId,
  740. const UA_NodeId referenceTypeId,
  741. const UA_QualifiedName browseName,
  742. const UA_NodeId typeDefinition,
  743. const UA_ObjectAttributes attr,
  744. UA_InstantiationCallback *instantiationCallback,
  745. UA_NodeId *outNewNodeId) {
  746. return __UA_Server_addNode(server, UA_NODECLASS_OBJECT, &requestedNewNodeId,
  747. &parentNodeId, &referenceTypeId, browseName,
  748. &typeDefinition, (const UA_NodeAttributes*)&attr,
  749. &UA_TYPES[UA_TYPES_OBJECTATTRIBUTES],
  750. instantiationCallback, outNewNodeId);
  751. }
  752. static UA_INLINE UA_StatusCode
  753. UA_Server_addObjectTypeNode(UA_Server *server, const UA_NodeId requestedNewNodeId,
  754. const UA_NodeId parentNodeId,
  755. const UA_NodeId referenceTypeId,
  756. const UA_QualifiedName browseName,
  757. const UA_ObjectTypeAttributes attr,
  758. UA_InstantiationCallback *instantiationCallback,
  759. UA_NodeId *outNewNodeId) {
  760. return __UA_Server_addNode(server, UA_NODECLASS_OBJECTTYPE, &requestedNewNodeId,
  761. &parentNodeId, &referenceTypeId, browseName,
  762. &UA_NODEID_NULL, (const UA_NodeAttributes*)&attr,
  763. &UA_TYPES[UA_TYPES_OBJECTTYPEATTRIBUTES],
  764. instantiationCallback, outNewNodeId);
  765. }
  766. static UA_INLINE UA_StatusCode
  767. UA_Server_addViewNode(UA_Server *server, const UA_NodeId requestedNewNodeId,
  768. const UA_NodeId parentNodeId,
  769. const UA_NodeId referenceTypeId,
  770. const UA_QualifiedName browseName,
  771. const UA_ViewAttributes attr,
  772. UA_InstantiationCallback *instantiationCallback,
  773. UA_NodeId *outNewNodeId) {
  774. return __UA_Server_addNode(server, UA_NODECLASS_VIEW, &requestedNewNodeId,
  775. &parentNodeId, &referenceTypeId, browseName,
  776. &UA_NODEID_NULL, (const UA_NodeAttributes*)&attr,
  777. &UA_TYPES[UA_TYPES_VIEWATTRIBUTES],
  778. instantiationCallback, outNewNodeId);
  779. }
  780. static UA_INLINE UA_StatusCode
  781. UA_Server_addReferenceTypeNode(UA_Server *server,
  782. const UA_NodeId requestedNewNodeId,
  783. const UA_NodeId parentNodeId,
  784. const UA_NodeId referenceTypeId,
  785. const UA_QualifiedName browseName,
  786. const UA_ReferenceTypeAttributes attr,
  787. UA_InstantiationCallback *instantiationCallback,
  788. UA_NodeId *outNewNodeId) {
  789. return __UA_Server_addNode(server, UA_NODECLASS_REFERENCETYPE,
  790. &requestedNewNodeId, &parentNodeId, &referenceTypeId,
  791. browseName, &UA_NODEID_NULL,
  792. (const UA_NodeAttributes*)&attr,
  793. &UA_TYPES[UA_TYPES_REFERENCETYPEATTRIBUTES],
  794. instantiationCallback, outNewNodeId);
  795. }
  796. static UA_INLINE UA_StatusCode
  797. UA_Server_addDataTypeNode(UA_Server *server,
  798. const UA_NodeId requestedNewNodeId,
  799. const UA_NodeId parentNodeId,
  800. const UA_NodeId referenceTypeId,
  801. const UA_QualifiedName browseName,
  802. const UA_DataTypeAttributes attr,
  803. UA_InstantiationCallback *instantiationCallback,
  804. UA_NodeId *outNewNodeId) {
  805. return __UA_Server_addNode(server, UA_NODECLASS_DATATYPE, &requestedNewNodeId,
  806. &parentNodeId, &referenceTypeId, browseName,
  807. &UA_NODEID_NULL, (const UA_NodeAttributes*)&attr,
  808. &UA_TYPES[UA_TYPES_DATATYPEATTRIBUTES],
  809. instantiationCallback, outNewNodeId);
  810. }
  811. UA_StatusCode UA_EXPORT
  812. UA_Server_addDataSourceVariableNode(UA_Server *server,
  813. const UA_NodeId requestedNewNodeId,
  814. const UA_NodeId parentNodeId,
  815. const UA_NodeId referenceTypeId,
  816. const UA_QualifiedName browseName,
  817. const UA_NodeId typeDefinition,
  818. const UA_VariableAttributes attr,
  819. const UA_DataSource dataSource,
  820. UA_NodeId *outNewNodeId);
  821. #ifdef UA_ENABLE_METHODCALLS
  822. UA_StatusCode UA_EXPORT
  823. UA_Server_addMethodNode(UA_Server *server, const UA_NodeId requestedNewNodeId,
  824. const UA_NodeId parentNodeId,
  825. const UA_NodeId referenceTypeId,
  826. const UA_QualifiedName browseName,
  827. const UA_MethodAttributes attr,
  828. UA_MethodCallback method, void *handle,
  829. size_t inputArgumentsSize,
  830. const UA_Argument* inputArguments,
  831. size_t outputArgumentsSize,
  832. const UA_Argument* outputArguments,
  833. UA_NodeId *outNewNodeId);
  834. #endif
  835. UA_StatusCode UA_EXPORT
  836. UA_Server_deleteNode(UA_Server *server, const UA_NodeId nodeId,
  837. UA_Boolean deleteReferences);
  838. /**
  839. * Reference Management
  840. * -------------------- */
  841. UA_StatusCode UA_EXPORT
  842. UA_Server_addReference(UA_Server *server, const UA_NodeId sourceId,
  843. const UA_NodeId refTypeId,
  844. const UA_ExpandedNodeId targetId, UA_Boolean isForward);
  845. UA_StatusCode UA_EXPORT
  846. UA_Server_deleteReference(UA_Server *server, const UA_NodeId sourceNodeId,
  847. const UA_NodeId referenceTypeId, UA_Boolean isForward,
  848. const UA_ExpandedNodeId targetNodeId,
  849. UA_Boolean deleteBidirectional);
  850. /**
  851. * Utility Functions
  852. * ----------------- */
  853. /* Add a new namespace to the server. Returns the index of the new namespace */
  854. UA_UInt16 UA_EXPORT UA_Server_addNamespace(UA_Server *server, const char* name);
  855. #ifdef __cplusplus
  856. }
  857. #endif
  858. /* Old interfaces kept for API compatibility */
  859. #include "ua_server_deprecated.h"
  860. #endif /* UA_SERVER_H_ */