ua_client.h 17 KB

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