ua_client.h 18 KB

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