ua_server.h 41 KB

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