ua_server.h 42 KB

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