ua_client.h 17 KB

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