ua_server.h 41 KB

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