ua_server.h 33 KB

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