ua_server.h 40 KB

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