ua_server.h 43 KB

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