ua_server.h 40 KB

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