ua_client.h 17 KB

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