ua_client.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include "ua_client_config.h"
  26. /**
  27. * .. _client:
  28. *
  29. * Client
  30. * ======
  31. *
  32. * The client implementation allows remote access to all OPC UA services. For
  33. * convenience, some functionality has been wrapped in :ref:`high-level
  34. * abstractions <client-highlevel>`.
  35. *
  36. * **However**: At this time, the client does not yet contain its own thread or
  37. * event-driven main-loop. So the client will not perform any actions
  38. * automatically in the background. This is especially relevant for
  39. * subscriptions. The user will have to periodically call
  40. * `UA_Client_Subscriptions_manuallySendPublishRequest`. See also :ref:`here
  41. * <client-subscriptions>`.
  42. *
  43. * Client Lifecycle
  44. * ---------------- */
  45. /* Create a new client */
  46. UA_Client UA_EXPORT *
  47. UA_Client_new(UA_ClientConfig config);
  48. /* Get the client connection status */
  49. UA_ClientState UA_EXPORT
  50. UA_Client_getState(UA_Client *client);
  51. /* Get the client context */
  52. void UA_EXPORT *
  53. UA_Client_getContext(UA_Client *client);
  54. /* Reset a client */
  55. void UA_EXPORT
  56. UA_Client_reset(UA_Client *client);
  57. /* Delete a client */
  58. void UA_EXPORT
  59. UA_Client_delete(UA_Client *client);
  60. /**
  61. * Connect to a Server
  62. * ------------------- */
  63. /* Connect to the server
  64. *
  65. * @param client to use
  66. * @param endpointURL to connect (for example "opc.tcp://localhost:4840")
  67. * @return Indicates whether the operation succeeded or returns an error code */
  68. UA_StatusCode UA_EXPORT
  69. UA_Client_connect(UA_Client *client, const char *endpointUrl);
  70. /* Connect to the selected server with the given username and password
  71. *
  72. * @param client to use
  73. * @param endpointURL to connect (for example "opc.tcp://localhost:4840")
  74. * @param username
  75. * @param password
  76. * @return Indicates whether the operation succeeded or returns an error code */
  77. UA_StatusCode UA_EXPORT
  78. UA_Client_connect_username(UA_Client *client, const char *endpointUrl,
  79. const char *username, const char *password);
  80. /* Disconnect and close a connection to the selected server */
  81. UA_StatusCode UA_EXPORT
  82. UA_Client_disconnect(UA_Client *client);
  83. /* Close a connection to the selected server */
  84. UA_StatusCode UA_EXPORT
  85. UA_Client_close(UA_Client *client);
  86. /* Renew the underlying secure channel */
  87. UA_StatusCode UA_EXPORT
  88. UA_Client_manuallyRenewSecureChannel(UA_Client *client);
  89. /**
  90. * Discovery
  91. * --------- */
  92. /* Gets a list of endpoints of a server
  93. *
  94. * @param client to use. Must be connected to the same endpoint given in
  95. * serverUrl or otherwise in disconnected state.
  96. * @param serverUrl url to connect (for example "opc.tcp://localhost:4840")
  97. * @param endpointDescriptionsSize size of the array of endpoint descriptions
  98. * @param endpointDescriptions array of endpoint descriptions that is allocated
  99. * by the function (you need to free manually)
  100. * @return Indicates whether the operation succeeded or returns an error code */
  101. UA_StatusCode UA_EXPORT
  102. UA_Client_getEndpoints(UA_Client *client, const char *serverUrl,
  103. size_t* endpointDescriptionsSize,
  104. UA_EndpointDescription** endpointDescriptions);
  105. /* Gets a list of all registered servers at the given server.
  106. *
  107. * You can pass an optional filter for serverUris. If the given server is not registered,
  108. * an empty array will be returned. If the server is registered, only that application
  109. * description will be returned.
  110. *
  111. * Additionally you can optionally indicate which locale you want for the server name
  112. * in the returned application description. The array indicates the order of preference.
  113. * A server may have localized names.
  114. *
  115. * @param client to use. Must be connected to the same endpoint given in
  116. * serverUrl or otherwise in disconnected state.
  117. * @param serverUrl url to connect (for example "opc.tcp://localhost:4840")
  118. * @param serverUrisSize Optional filter for specific server uris
  119. * @param serverUris Optional filter for specific server uris
  120. * @param localeIdsSize Optional indication which locale you prefer
  121. * @param localeIds Optional indication which locale you prefer
  122. * @param registeredServersSize size of returned array, i.e., number of found/registered servers
  123. * @param registeredServers array containing found/registered servers
  124. * @return Indicates whether the operation succeeded or returns an error code */
  125. UA_StatusCode UA_EXPORT
  126. UA_Client_findServers(UA_Client *client, const char *serverUrl,
  127. size_t serverUrisSize, UA_String *serverUris,
  128. size_t localeIdsSize, UA_String *localeIds,
  129. size_t *registeredServersSize,
  130. UA_ApplicationDescription **registeredServers);
  131. /* Get a list of all known server in the network. Only supported by LDS servers.
  132. *
  133. * @param client to use. Must be connected to the same endpoint given in
  134. * serverUrl or otherwise in disconnected state.
  135. * @param serverUrl url to connect (for example "opc.tcp://localhost:4840")
  136. * @param startingRecordId optional. Only return the records with an ID higher
  137. * or equal the given. Can be used for pagination to only get a subset of
  138. * the full list
  139. * @param maxRecordsToReturn optional. Only return this number of records
  140. * @param serverCapabilityFilterSize optional. Filter the returned list to only
  141. * get servers with given capabilities, e.g. "LDS"
  142. * @param serverCapabilityFilter optional. Filter the returned list to only get
  143. * servers with given capabilities, e.g. "LDS"
  144. * @param serverOnNetworkSize size of returned array, i.e., number of
  145. * known/registered servers
  146. * @param serverOnNetwork array containing known/registered servers
  147. * @return Indicates whether the operation succeeded or returns an error code */
  148. UA_StatusCode UA_EXPORT
  149. UA_Client_findServersOnNetwork(UA_Client *client, const char *serverUrl,
  150. UA_UInt32 startingRecordId, UA_UInt32 maxRecordsToReturn,
  151. size_t serverCapabilityFilterSize, UA_String *serverCapabilityFilter,
  152. size_t *serverOnNetworkSize, UA_ServerOnNetwork **serverOnNetwork);
  153. /**
  154. * .. _client-services:
  155. *
  156. * Services
  157. * --------
  158. * The raw OPC UA services are exposed to the client. But most of them time, it
  159. * is better to use the convenience functions from ``ua_client_highlevel.h``
  160. * that wrap the raw services. */
  161. /* Don't use this function. Use the type versions below instead. */
  162. void UA_EXPORT
  163. __UA_Client_Service(UA_Client *client, const void *request,
  164. const UA_DataType *requestType, void *response,
  165. const UA_DataType *responseType);
  166. /**
  167. * Attribute Service Set
  168. * ^^^^^^^^^^^^^^^^^^^^^ */
  169. static UA_INLINE UA_ReadResponse
  170. UA_Client_Service_read(UA_Client *client, const UA_ReadRequest request) {
  171. UA_ReadResponse response;
  172. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_READREQUEST],
  173. &response, &UA_TYPES[UA_TYPES_READRESPONSE]);
  174. return response;
  175. }
  176. static UA_INLINE UA_WriteResponse
  177. UA_Client_Service_write(UA_Client *client, const UA_WriteRequest request) {
  178. UA_WriteResponse response;
  179. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_WRITEREQUEST],
  180. &response, &UA_TYPES[UA_TYPES_WRITERESPONSE]);
  181. return response;
  182. }
  183. /**
  184. * Method Service Set
  185. * ^^^^^^^^^^^^^^^^^^ */
  186. #ifdef UA_ENABLE_METHODCALLS
  187. static UA_INLINE UA_CallResponse
  188. UA_Client_Service_call(UA_Client *client, const UA_CallRequest request) {
  189. UA_CallResponse response;
  190. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_CALLREQUEST],
  191. &response, &UA_TYPES[UA_TYPES_CALLRESPONSE]);
  192. return response;
  193. }
  194. #endif
  195. /**
  196. * NodeManagement Service Set
  197. * ^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  198. static UA_INLINE UA_AddNodesResponse
  199. UA_Client_Service_addNodes(UA_Client *client, const UA_AddNodesRequest request) {
  200. UA_AddNodesResponse response;
  201. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  202. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  203. return response;
  204. }
  205. static UA_INLINE UA_AddReferencesResponse
  206. UA_Client_Service_addReferences(UA_Client *client,
  207. const UA_AddReferencesRequest request) {
  208. UA_AddReferencesResponse response;
  209. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDREFERENCESREQUEST],
  210. &response, &UA_TYPES[UA_TYPES_ADDREFERENCESRESPONSE]);
  211. return response;
  212. }
  213. static UA_INLINE UA_DeleteNodesResponse
  214. UA_Client_Service_deleteNodes(UA_Client *client,
  215. const UA_DeleteNodesRequest request) {
  216. UA_DeleteNodesResponse response;
  217. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  218. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  219. return response;
  220. }
  221. static UA_INLINE UA_DeleteReferencesResponse
  222. UA_Client_Service_deleteReferences(UA_Client *client,
  223. const UA_DeleteReferencesRequest request) {
  224. UA_DeleteReferencesResponse response;
  225. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETEREFERENCESREQUEST],
  226. &response, &UA_TYPES[UA_TYPES_DELETEREFERENCESRESPONSE]);
  227. return response;
  228. }
  229. /**
  230. * View Service Set
  231. * ^^^^^^^^^^^^^^^^ */
  232. static UA_INLINE UA_BrowseResponse
  233. UA_Client_Service_browse(UA_Client *client, const UA_BrowseRequest request) {
  234. UA_BrowseResponse response;
  235. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSEREQUEST],
  236. &response, &UA_TYPES[UA_TYPES_BROWSERESPONSE]);
  237. return response;
  238. }
  239. static UA_INLINE UA_BrowseNextResponse
  240. UA_Client_Service_browseNext(UA_Client *client,
  241. const UA_BrowseNextRequest request) {
  242. UA_BrowseNextResponse response;
  243. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSENEXTREQUEST],
  244. &response, &UA_TYPES[UA_TYPES_BROWSENEXTRESPONSE]);
  245. return response;
  246. }
  247. static UA_INLINE UA_TranslateBrowsePathsToNodeIdsResponse
  248. UA_Client_Service_translateBrowsePathsToNodeIds(UA_Client *client,
  249. const UA_TranslateBrowsePathsToNodeIdsRequest request) {
  250. UA_TranslateBrowsePathsToNodeIdsResponse response;
  251. __UA_Client_Service(client, &request,
  252. &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSREQUEST],
  253. &response,
  254. &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE]);
  255. return response;
  256. }
  257. static UA_INLINE UA_RegisterNodesResponse
  258. UA_Client_Service_registerNodes(UA_Client *client,
  259. const UA_RegisterNodesRequest request) {
  260. UA_RegisterNodesResponse response;
  261. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERNODESREQUEST],
  262. &response, &UA_TYPES[UA_TYPES_REGISTERNODESRESPONSE]);
  263. return response;
  264. }
  265. static UA_INLINE UA_UnregisterNodesResponse
  266. UA_Client_Service_unregisterNodes(UA_Client *client,
  267. const UA_UnregisterNodesRequest request) {
  268. UA_UnregisterNodesResponse response;
  269. __UA_Client_Service(client, &request,
  270. &UA_TYPES[UA_TYPES_UNREGISTERNODESREQUEST],
  271. &response, &UA_TYPES[UA_TYPES_UNREGISTERNODESRESPONSE]);
  272. return response;
  273. }
  274. /**
  275. * Query Service Set
  276. * ^^^^^^^^^^^^^^^^^ */
  277. static UA_INLINE UA_QueryFirstResponse
  278. UA_Client_Service_queryFirst(UA_Client *client,
  279. const UA_QueryFirstRequest request) {
  280. UA_QueryFirstResponse response;
  281. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  282. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  283. return response;
  284. }
  285. static UA_INLINE UA_QueryNextResponse
  286. UA_Client_Service_queryNext(UA_Client *client,
  287. const UA_QueryNextRequest request) {
  288. UA_QueryNextResponse response;
  289. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  290. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  291. return response;
  292. }
  293. /**
  294. * .. _client-async-services:
  295. *
  296. * Asynchronous Services
  297. * ---------------------
  298. * All OPC UA services are asynchronous in nature. So several service calls can
  299. * be made without waiting for a response first. Responess may come in a
  300. * different ordering. */
  301. /* Listen on the network and process arriving asynchronous responses in the
  302. * background. Internal housekeeping and subscription management is done as
  303. * well. */
  304. UA_StatusCode UA_EXPORT
  305. UA_Client_runAsync(UA_Client *client, UA_UInt16 timeout);
  306. typedef void
  307. (*UA_ClientAsyncServiceCallback)(UA_Client *client, void *userdata,
  308. UA_UInt32 requestId, void *response,
  309. const UA_DataType *responseType);
  310. /* Use the type versions of this method. See below. However, the general
  311. * mechanism of async service calls is explained here.
  312. *
  313. * We say that an async service call has been dispatched once this method
  314. * returns UA_STATUSCODE_GOOD. If there is an error after an async service has
  315. * been dispatched, the callback is called with an "empty" response where the
  316. * statusCode has been set accordingly. This is also done if the client is
  317. * shutting down and the list of dispatched async services is emptied.
  318. *
  319. * The statusCode received when the client is shutting down is
  320. * UA_STATUSCODE_BADSHUTDOWN.
  321. *
  322. * The userdata and requestId arguments can be NULL. */
  323. UA_StatusCode UA_EXPORT
  324. __UA_Client_AsyncService(UA_Client *client, const void *request,
  325. const UA_DataType *requestType,
  326. UA_ClientAsyncServiceCallback callback,
  327. const UA_DataType *responseType,
  328. void *userdata, UA_UInt32 *requestId);
  329. static UA_INLINE UA_StatusCode
  330. UA_Client_AsyncService_read(UA_Client *client, const UA_ReadRequest *request,
  331. UA_ClientAsyncServiceCallback callback,
  332. void *userdata, UA_UInt32 *requestId) {
  333. return __UA_Client_AsyncService(client, (const void*)request,
  334. &UA_TYPES[UA_TYPES_READREQUEST], callback,
  335. &UA_TYPES[UA_TYPES_READRESPONSE],
  336. userdata, requestId);
  337. }
  338. static UA_INLINE UA_StatusCode
  339. UA_Client_AsyncService_write(UA_Client *client, const UA_WriteRequest *request,
  340. UA_ClientAsyncServiceCallback callback,
  341. void *userdata, UA_UInt32 *requestId) {
  342. return __UA_Client_AsyncService(client, (const void*)request,
  343. &UA_TYPES[UA_TYPES_WRITEREQUEST], callback,
  344. &UA_TYPES[UA_TYPES_WRITERESPONSE],
  345. userdata, requestId);
  346. }
  347. static UA_INLINE UA_StatusCode
  348. UA_Client_AsyncService_call(UA_Client *client, const UA_CallRequest *request,
  349. UA_ClientAsyncServiceCallback callback,
  350. void *userdata, UA_UInt32 *requestId) {
  351. return __UA_Client_AsyncService(client, (const void*)request,
  352. &UA_TYPES[UA_TYPES_CALLREQUEST], callback,
  353. &UA_TYPES[UA_TYPES_CALLRESPONSE],
  354. userdata, requestId);
  355. }
  356. static UA_INLINE UA_StatusCode
  357. UA_Client_AsyncService_browse(UA_Client *client, const UA_BrowseRequest *request,
  358. UA_ClientAsyncServiceCallback callback,
  359. void *userdata, UA_UInt32 *requestId) {
  360. return __UA_Client_AsyncService(client, (const void*)request,
  361. &UA_TYPES[UA_TYPES_BROWSEREQUEST], callback,
  362. &UA_TYPES[UA_TYPES_BROWSERESPONSE],
  363. userdata, requestId);
  364. }
  365. /**
  366. * .. toctree::
  367. *
  368. * client_highlevel
  369. * client_subscriptions */
  370. #ifdef __cplusplus
  371. } // extern "C"
  372. #endif
  373. #endif /* UA_CLIENT_H_ */