ua_client.h 18 KB

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