ua_server.h 40 KB

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