ua_server.h 38 KB

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