ua_server.h 40 KB

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