ua_server.h 33 KB

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