ua_server.h 33 KB

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