ua_client.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 2015-2017 (c) Julius Pfrommer, Fraunhofer IOSB
  6. * Copyright 2015-2016 (c) Sten Grüner
  7. * Copyright 2015-2016 (c) Chris Iatrou
  8. * Copyright 2015-2017 (c) Florian Palm
  9. * Copyright 2015 (c) Holger Jeromin
  10. * Copyright 2015 (c) Oleksiy Vasylyev
  11. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  12. * Copyright 2017 (c) Mark Giraud, Fraunhofer IOSB
  13. * Copyright 2018 (c) Thomas Stalder
  14. */
  15. #ifndef UA_CLIENT_H_
  16. #define UA_CLIENT_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include "ua_types.h"
  21. #include "ua_types_generated.h"
  22. #include "ua_types_generated_handling.h"
  23. #include "ua_plugin_network.h"
  24. #include "ua_plugin_log.h"
  25. /**
  26. * .. _client:
  27. *
  28. * Client
  29. * ======
  30. *
  31. * The client implementation allows remote access to all OPC UA services. For
  32. * convenience, some functionality has been wrapped in :ref:`high-level
  33. * abstractions <client-highlevel>`.
  34. *
  35. * **However**: At this time, the client does not yet contain its own thread or
  36. * event-driven main-loop. So the client will not perform any actions
  37. * automatically in the background. This is especially relevant for
  38. * subscriptions. The user will have to periodically call
  39. * `UA_Client_Subscriptions_manuallySendPublishRequest`. See also :ref:`here
  40. * <client-subscriptions>`.
  41. *
  42. * Client Lifecycle
  43. * ---------------- */
  44. typedef enum {
  45. UA_CLIENTSTATE_DISCONNECTED, /* The client is disconnected */
  46. UA_CLIENTSTATE_CONNECTED, /* A TCP connection to the server is open */
  47. UA_CLIENTSTATE_SECURECHANNEL, /* A SecureChannel to the server is open */
  48. UA_CLIENTSTATE_SESSION, /* A session with the server is open */
  49. UA_CLIENTSTATE_SESSION_RENEWED /* A session with the server is open (renewed) */
  50. } UA_ClientState;
  51. struct UA_Client;
  52. typedef struct UA_Client UA_Client;
  53. /**
  54. * Client Lifecycle callback
  55. * ------------------------- */
  56. typedef void (*UA_ClientStateCallback)(UA_Client *client, UA_ClientState clientState);
  57. /**
  58. * Client Configuration
  59. * -------------------- */
  60. typedef struct UA_ClientConfig {
  61. UA_UInt32 timeout; /* Sync response timeout in ms */
  62. UA_UInt32 secureChannelLifeTime; /* Lifetime in ms (then the channel needs
  63. to be renewed) */
  64. UA_Logger logger;
  65. UA_ConnectionConfig localConnectionConfig;
  66. UA_ConnectClientConnection connectionFunc;
  67. /* Custom DataTypes */
  68. size_t customDataTypesSize;
  69. const UA_DataType *customDataTypes;
  70. /* Callback function */
  71. UA_ClientStateCallback stateCallback;
  72. void *clientContext;
  73. } UA_ClientConfig;
  74. /* Create a new client */
  75. UA_Client UA_EXPORT *
  76. UA_Client_new(UA_ClientConfig config);
  77. /* Get the client connection status */
  78. UA_ClientState UA_EXPORT
  79. UA_Client_getState(UA_Client *client);
  80. /* Get the client context */
  81. void UA_EXPORT *
  82. UA_Client_getContext(UA_Client *client);
  83. /* Reset a client */
  84. void UA_EXPORT
  85. UA_Client_reset(UA_Client *client);
  86. /* Delete a client */
  87. void UA_EXPORT
  88. UA_Client_delete(UA_Client *client);
  89. /**
  90. * Connect to a Server
  91. * ------------------- */
  92. /* Connect to the server
  93. *
  94. * @param client to use
  95. * @param endpointURL to connect (for example "opc.tcp://localhost:16664")
  96. * @return Indicates whether the operation succeeded or returns an error code */
  97. UA_StatusCode UA_EXPORT
  98. UA_Client_connect(UA_Client *client, const char *endpointUrl);
  99. /* Connect to the selected server with the given username and password
  100. *
  101. * @param client to use
  102. * @param endpointURL to connect (for example "opc.tcp://localhost:16664")
  103. * @param username
  104. * @param password
  105. * @return Indicates whether the operation succeeded or returns an error code */
  106. UA_StatusCode UA_EXPORT
  107. UA_Client_connect_username(UA_Client *client, const char *endpointUrl,
  108. const char *username, const char *password);
  109. /* Disconnect and close a connection to the selected server */
  110. UA_StatusCode UA_EXPORT
  111. UA_Client_disconnect(UA_Client *client);
  112. /* Close a connection to the selected server */
  113. UA_StatusCode UA_EXPORT
  114. UA_Client_close(UA_Client *client);
  115. /* Renew the underlying secure channel */
  116. UA_StatusCode UA_EXPORT
  117. UA_Client_manuallyRenewSecureChannel(UA_Client *client);
  118. /**
  119. * Discovery
  120. * --------- */
  121. /* Gets a list of endpoints of a server
  122. *
  123. * @param client to use. Must be connected to the same endpoint given in
  124. * serverUrl or otherwise in disconnected state.
  125. * @param serverUrl url to connect (for example "opc.tcp://localhost:16664")
  126. * @param endpointDescriptionsSize size of the array of endpoint descriptions
  127. * @param endpointDescriptions array of endpoint descriptions that is allocated
  128. * by the function (you need to free manually)
  129. * @return Indicates whether the operation succeeded or returns an error code */
  130. UA_StatusCode UA_EXPORT
  131. UA_Client_getEndpoints(UA_Client *client, const char *serverUrl,
  132. size_t* endpointDescriptionsSize,
  133. UA_EndpointDescription** endpointDescriptions);
  134. /* Gets a list of all registered servers at the given server.
  135. *
  136. * You can pass an optional filter for serverUris. If the given server is not registered,
  137. * an empty array will be returned. If the server is registered, only that application
  138. * description will be returned.
  139. *
  140. * Additionally you can optionally indicate which locale you want for the server name
  141. * in the returned application description. The array indicates the order of preference.
  142. * A server may have localized names.
  143. *
  144. * @param client to use. Must be connected to the same endpoint given in
  145. * serverUrl or otherwise in disconnected state.
  146. * @param serverUrl url to connect (for example "opc.tcp://localhost:16664")
  147. * @param serverUrisSize Optional filter for specific server uris
  148. * @param serverUris Optional filter for specific server uris
  149. * @param localeIdsSize Optional indication which locale you prefer
  150. * @param localeIds Optional indication which locale you prefer
  151. * @param registeredServersSize size of returned array, i.e., number of found/registered servers
  152. * @param registeredServers array containing found/registered servers
  153. * @return Indicates whether the operation succeeded or returns an error code */
  154. UA_StatusCode UA_EXPORT
  155. UA_Client_findServers(UA_Client *client, const char *serverUrl,
  156. size_t serverUrisSize, UA_String *serverUris,
  157. size_t localeIdsSize, UA_String *localeIds,
  158. size_t *registeredServersSize,
  159. UA_ApplicationDescription **registeredServers);
  160. /* Get a list of all known server in the network. Only supported by LDS servers.
  161. *
  162. * @param client to use. Must be connected to the same endpoint given in
  163. * serverUrl or otherwise in disconnected state.
  164. * @param serverUrl url to connect (for example "opc.tcp://localhost:16664")
  165. * @param startingRecordId optional. Only return the records with an ID higher
  166. * or equal the given. Can be used for pagination to only get a subset of
  167. * the full list
  168. * @param maxRecordsToReturn optional. Only return this number of records
  169. * @param serverCapabilityFilterSize optional. Filter the returned list to only
  170. * get servers with given capabilities, e.g. "LDS"
  171. * @param serverCapabilityFilter optional. Filter the returned list to only get
  172. * servers with given capabilities, e.g. "LDS"
  173. * @param serverOnNetworkSize size of returned array, i.e., number of
  174. * known/registered servers
  175. * @param serverOnNetwork array containing known/registered servers
  176. * @return Indicates whether the operation succeeded or returns an error code */
  177. UA_StatusCode UA_EXPORT
  178. UA_Client_findServersOnNetwork(UA_Client *client, const char *serverUrl,
  179. UA_UInt32 startingRecordId, UA_UInt32 maxRecordsToReturn,
  180. size_t serverCapabilityFilterSize, UA_String *serverCapabilityFilter,
  181. size_t *serverOnNetworkSize, UA_ServerOnNetwork **serverOnNetwork);
  182. /**
  183. * .. _client-services:
  184. *
  185. * Services
  186. * --------
  187. * The raw OPC UA services are exposed to the client. But most of them time, it
  188. * is better to use the convenience functions from ``ua_client_highlevel.h``
  189. * that wrap the raw services. */
  190. /* Don't use this function. Use the type versions below instead. */
  191. void UA_EXPORT
  192. __UA_Client_Service(UA_Client *client, const void *request,
  193. const UA_DataType *requestType, void *response,
  194. const UA_DataType *responseType);
  195. /**
  196. * Attribute Service Set
  197. * ^^^^^^^^^^^^^^^^^^^^^ */
  198. static UA_INLINE UA_ReadResponse
  199. UA_Client_Service_read(UA_Client *client, const UA_ReadRequest request) {
  200. UA_ReadResponse response;
  201. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_READREQUEST],
  202. &response, &UA_TYPES[UA_TYPES_READRESPONSE]);
  203. return response;
  204. }
  205. static UA_INLINE UA_WriteResponse
  206. UA_Client_Service_write(UA_Client *client, const UA_WriteRequest request) {
  207. UA_WriteResponse response;
  208. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_WRITEREQUEST],
  209. &response, &UA_TYPES[UA_TYPES_WRITERESPONSE]);
  210. return response;
  211. }
  212. /**
  213. * Method Service Set
  214. * ^^^^^^^^^^^^^^^^^^ */
  215. #ifdef UA_ENABLE_METHODCALLS
  216. static UA_INLINE UA_CallResponse
  217. UA_Client_Service_call(UA_Client *client, const UA_CallRequest request) {
  218. UA_CallResponse response;
  219. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_CALLREQUEST],
  220. &response, &UA_TYPES[UA_TYPES_CALLRESPONSE]);
  221. return response;
  222. }
  223. #endif
  224. /**
  225. * NodeManagement Service Set
  226. * ^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  227. static UA_INLINE UA_AddNodesResponse
  228. UA_Client_Service_addNodes(UA_Client *client, const UA_AddNodesRequest request) {
  229. UA_AddNodesResponse response;
  230. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  231. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  232. return response;
  233. }
  234. static UA_INLINE UA_AddReferencesResponse
  235. UA_Client_Service_addReferences(UA_Client *client,
  236. const UA_AddReferencesRequest request) {
  237. UA_AddReferencesResponse response;
  238. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDREFERENCESREQUEST],
  239. &response, &UA_TYPES[UA_TYPES_ADDREFERENCESRESPONSE]);
  240. return response;
  241. }
  242. static UA_INLINE UA_DeleteNodesResponse
  243. UA_Client_Service_deleteNodes(UA_Client *client,
  244. const UA_DeleteNodesRequest request) {
  245. UA_DeleteNodesResponse response;
  246. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  247. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  248. return response;
  249. }
  250. static UA_INLINE UA_DeleteReferencesResponse
  251. UA_Client_Service_deleteReferences(UA_Client *client,
  252. const UA_DeleteReferencesRequest request) {
  253. UA_DeleteReferencesResponse response;
  254. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETEREFERENCESREQUEST],
  255. &response, &UA_TYPES[UA_TYPES_DELETEREFERENCESRESPONSE]);
  256. return response;
  257. }
  258. /**
  259. * View Service Set
  260. * ^^^^^^^^^^^^^^^^ */
  261. static UA_INLINE UA_BrowseResponse
  262. UA_Client_Service_browse(UA_Client *client, const UA_BrowseRequest request) {
  263. UA_BrowseResponse response;
  264. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSEREQUEST],
  265. &response, &UA_TYPES[UA_TYPES_BROWSERESPONSE]);
  266. return response;
  267. }
  268. static UA_INLINE UA_BrowseNextResponse
  269. UA_Client_Service_browseNext(UA_Client *client,
  270. const UA_BrowseNextRequest request) {
  271. UA_BrowseNextResponse response;
  272. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSENEXTREQUEST],
  273. &response, &UA_TYPES[UA_TYPES_BROWSENEXTRESPONSE]);
  274. return response;
  275. }
  276. static UA_INLINE UA_TranslateBrowsePathsToNodeIdsResponse
  277. UA_Client_Service_translateBrowsePathsToNodeIds(UA_Client *client,
  278. const UA_TranslateBrowsePathsToNodeIdsRequest request) {
  279. UA_TranslateBrowsePathsToNodeIdsResponse response;
  280. __UA_Client_Service(client, &request,
  281. &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSREQUEST],
  282. &response,
  283. &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE]);
  284. return response;
  285. }
  286. static UA_INLINE UA_RegisterNodesResponse
  287. UA_Client_Service_registerNodes(UA_Client *client,
  288. const UA_RegisterNodesRequest request) {
  289. UA_RegisterNodesResponse response;
  290. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERNODESREQUEST],
  291. &response, &UA_TYPES[UA_TYPES_REGISTERNODESRESPONSE]);
  292. return response;
  293. }
  294. static UA_INLINE UA_UnregisterNodesResponse
  295. UA_Client_Service_unregisterNodes(UA_Client *client,
  296. const UA_UnregisterNodesRequest request) {
  297. UA_UnregisterNodesResponse response;
  298. __UA_Client_Service(client, &request,
  299. &UA_TYPES[UA_TYPES_UNREGISTERNODESREQUEST],
  300. &response, &UA_TYPES[UA_TYPES_UNREGISTERNODESRESPONSE]);
  301. return response;
  302. }
  303. /**
  304. * Query Service Set
  305. * ^^^^^^^^^^^^^^^^^ */
  306. static UA_INLINE UA_QueryFirstResponse
  307. UA_Client_Service_queryFirst(UA_Client *client,
  308. const UA_QueryFirstRequest request) {
  309. UA_QueryFirstResponse response;
  310. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  311. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  312. return response;
  313. }
  314. static UA_INLINE UA_QueryNextResponse
  315. UA_Client_Service_queryNext(UA_Client *client,
  316. const UA_QueryNextRequest request) {
  317. UA_QueryNextResponse response;
  318. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  319. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  320. return response;
  321. }
  322. #ifdef UA_ENABLE_SUBSCRIPTIONS
  323. /**
  324. * MonitoredItem Service Set
  325. * ^^^^^^^^^^^^^^^^^^^^^^^^^ */
  326. static UA_INLINE UA_CreateMonitoredItemsResponse
  327. UA_Client_Service_createMonitoredItems(UA_Client *client,
  328. const UA_CreateMonitoredItemsRequest request) {
  329. UA_CreateMonitoredItemsResponse response;
  330. __UA_Client_Service(client, &request,
  331. &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSREQUEST], &response,
  332. &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSRESPONSE]);
  333. return response;
  334. }
  335. static UA_INLINE UA_DeleteMonitoredItemsResponse
  336. UA_Client_Service_deleteMonitoredItems(UA_Client *client,
  337. const UA_DeleteMonitoredItemsRequest request) {
  338. UA_DeleteMonitoredItemsResponse response;
  339. __UA_Client_Service(client, &request,
  340. &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSREQUEST], &response,
  341. &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSRESPONSE]);
  342. return response;
  343. }
  344. /**
  345. * Subscription Service Set
  346. * ^^^^^^^^^^^^^^^^^^^^^^^^ */
  347. static UA_INLINE UA_CreateSubscriptionResponse
  348. UA_Client_Service_createSubscription(UA_Client *client,
  349. const UA_CreateSubscriptionRequest request) {
  350. UA_CreateSubscriptionResponse response;
  351. __UA_Client_Service(client, &request,
  352. &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONREQUEST], &response,
  353. &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONRESPONSE]);
  354. return response;
  355. }
  356. static UA_INLINE UA_ModifySubscriptionResponse
  357. UA_Client_Service_modifySubscription(UA_Client *client,
  358. const UA_ModifySubscriptionRequest request) {
  359. UA_ModifySubscriptionResponse response;
  360. __UA_Client_Service(client, &request,
  361. &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONREQUEST], &response,
  362. &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONRESPONSE]);
  363. return response;
  364. }
  365. static UA_INLINE UA_DeleteSubscriptionsResponse
  366. UA_Client_Service_deleteSubscriptions(UA_Client *client,
  367. const UA_DeleteSubscriptionsRequest request) {
  368. UA_DeleteSubscriptionsResponse response;
  369. __UA_Client_Service(client, &request,
  370. &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSREQUEST], &response,
  371. &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSRESPONSE]);
  372. return response;
  373. }
  374. static UA_INLINE UA_PublishResponse
  375. UA_Client_Service_publish(UA_Client *client, const UA_PublishRequest request) {
  376. UA_PublishResponse response;
  377. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  378. &response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  379. return response;
  380. }
  381. #endif
  382. /**
  383. * .. _client-async-services:
  384. *
  385. * Asynchronous Services
  386. * ---------------------
  387. * All OPC UA services are asynchronous in nature. So several service calls can
  388. * be made without waiting for a response first. Responess may come in a
  389. * different ordering. */
  390. typedef void
  391. (*UA_ClientAsyncServiceCallback)(UA_Client *client, void *userdata,
  392. UA_UInt32 requestId, const void *response);
  393. /* Don't use this function. Use the type versions below instead. */
  394. UA_StatusCode UA_EXPORT
  395. __UA_Client_AsyncService(UA_Client *client, const void *request,
  396. const UA_DataType *requestType,
  397. UA_ClientAsyncServiceCallback callback,
  398. const UA_DataType *responseType,
  399. void *userdata, UA_UInt32 *requestId);
  400. UA_StatusCode UA_EXPORT
  401. UA_Client_runAsync(UA_Client *client, UA_UInt16 timeout);
  402. /**
  403. * .. toctree::
  404. *
  405. * client_highlevel */
  406. #ifdef __cplusplus
  407. } // extern "C"
  408. #endif
  409. #endif /* UA_CLIENT_H_ */