ua_client.h 17 KB

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