ua_client.h 18 KB

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