ua_server.h 39 KB

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