ua_client.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* Copyright (C) 2014-2016 the contributors as stated in the AUTHORS file
  2. *
  3. * This file is part of open62541. open62541 is free software: you can
  4. * redistribute it and/or modify it under the terms of the GNU Lesser General
  5. * Public License, version 3 (as published by the Free Software Foundation) with
  6. * a static linking exception as stated in the LICENSE file provided with
  7. * open62541.
  8. *
  9. * open62541 is distributed in the hope that it will be useful, but WITHOUT ANY
  10. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details. */
  13. #ifndef UA_CLIENT_H_
  14. #define UA_CLIENT_H_
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "ua_config.h"
  19. #include "ua_types.h"
  20. #include "ua_connection.h"
  21. #include "ua_log.h"
  22. #include "ua_types_generated.h"
  23. #include "ua_types_generated_handling.h"
  24. /**
  25. * .. _client:
  26. *
  27. * Client
  28. * ======
  29. *
  30. * The client implementation allows remote access to all OPC UA services. For
  31. * convenience, some functionality has been wrapped in :ref:`high-level
  32. * abstractions <client-highlevel>`.
  33. *
  34. * **However**: At this point, the client does not yet contain its own thread or
  35. * event-driven main-loop. So the client will not perform any actions
  36. * automatically in the background. This is especially relevant for
  37. * subscriptions. The user will have to periodically call
  38. * `UA_Client_Subscriptions_manuallySendPublishRequest`. See also :ref:`here
  39. * <client-subscriptions>`.
  40. *
  41. * Client Configuration
  42. * -------------------- */
  43. typedef UA_Connection
  44. (*UA_ConnectClientConnection)(UA_ConnectionConfig localConf,
  45. const char *endpointUrl, UA_Logger logger);
  46. typedef struct UA_ClientConfig {
  47. UA_UInt32 timeout; /* Sync response timeout */
  48. UA_UInt32 secureChannelLifeTime; /* Lifetime in ms (then the channel needs
  49. to be renewed) */
  50. UA_Logger logger;
  51. UA_ConnectionConfig localConnectionConfig;
  52. UA_ConnectClientConnection connectionFunc;
  53. } UA_ClientConfig;
  54. /**
  55. * Client Lifecycle
  56. * ---------------- */
  57. typedef enum {
  58. UA_CLIENTSTATE_READY, /* The client is not connected but initialized
  59. and ready to use. */
  60. UA_CLIENTSTATE_CONNECTED, /* The client is connected to a server. */
  61. UA_CLIENTSTATE_FAULTED, /* An error has occured that might have
  62. influenced the connection state. A successfull
  63. service call or renewal of the secure channel
  64. will reset the state to CONNECTED. */
  65. UA_CLIENTSTATE_ERRORED /* A non-recoverable error has occured and the
  66. connection is no longer reliable. The client
  67. needs to be disconnected and reinitialized to
  68. recover into a CONNECTED state. */
  69. } UA_ClientState;
  70. struct UA_Client;
  71. typedef struct UA_Client UA_Client;
  72. /* Create a new client
  73. *
  74. * @param config for the new client. You can use UA_ClientConfig_standard
  75. * which has sane defaults
  76. * @param logger function pointer to a logger function. See
  77. * examples/logger_stdout.c for a simple implementation
  78. * @return return the new Client object */
  79. UA_Client UA_EXPORT * UA_Client_new(UA_ClientConfig config);
  80. /* Get the client connection status */
  81. UA_ClientState UA_EXPORT UA_Client_getState(UA_Client *client);
  82. /* Reset a client */
  83. void UA_EXPORT UA_Client_reset(UA_Client *client);
  84. /* Delete a client */
  85. void UA_EXPORT UA_Client_delete(UA_Client *client);
  86. /**
  87. * Manage the Connection
  88. * --------------------- */
  89. /* Gets a list of endpoints of a server
  90. *
  91. * @param client to use
  92. * @param server url to connect (for example "opc.tcp://localhost:16664")
  93. * @param endpointDescriptionsSize size of the array of endpoint descriptions
  94. * @param endpointDescriptions array of endpoint descriptions that is allocated
  95. * by the function (you need to free manually)
  96. * @return Indicates whether the operation succeeded or returns an error code */
  97. UA_StatusCode UA_EXPORT
  98. UA_Client_getEndpoints(UA_Client *client, const char *serverUrl,
  99. size_t* endpointDescriptionsSize,
  100. UA_EndpointDescription** endpointDescriptions);
  101. /* Connect to the selected server
  102. *
  103. * @param client to use
  104. * @param endpointURL to connect (for example "opc.tcp://localhost:16664")
  105. * @return Indicates whether the operation succeeded or returns an error code */
  106. UA_StatusCode UA_EXPORT
  107. UA_Client_connect(UA_Client *client, const char *endpointUrl);
  108. /* Connect to the selected server with the given username and password
  109. *
  110. * @param client to use
  111. * @param endpointURL to connect (for example "opc.tcp://localhost:16664")
  112. * @param username
  113. * @param password
  114. * @return Indicates whether the operation succeeded or returns an error code */
  115. UA_StatusCode UA_EXPORT
  116. UA_Client_connect_username(UA_Client *client, const char *endpointUrl,
  117. const char *username, const char *password);
  118. /* Close a connection to the selected server */
  119. UA_StatusCode UA_EXPORT UA_Client_disconnect(UA_Client *client);
  120. /* Renew the underlying secure channel */
  121. UA_StatusCode UA_EXPORT UA_Client_manuallyRenewSecureChannel(UA_Client *client);
  122. /**
  123. * .. _client-services:
  124. *
  125. * Raw Services
  126. * ------------
  127. * The raw OPC UA services are exposed to the client. But most of them time, it
  128. * is better to use the convenience functions from ``ua_client_highlevel.h``
  129. * that wrap the raw services. */
  130. /* Don't use this function. Use the type versions below instead. */
  131. void UA_EXPORT
  132. __UA_Client_Service(UA_Client *client, const void *request,
  133. const UA_DataType *requestType, void *response,
  134. const UA_DataType *responseType);
  135. /**
  136. * Attribute Service Set
  137. * ^^^^^^^^^^^^^^^^^^^^^ */
  138. static UA_INLINE UA_ReadResponse
  139. UA_Client_Service_read(UA_Client *client, const UA_ReadRequest request) {
  140. UA_ReadResponse response;
  141. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_READREQUEST],
  142. &response, &UA_TYPES[UA_TYPES_READRESPONSE]);
  143. return response;
  144. }
  145. static UA_INLINE UA_WriteResponse
  146. UA_Client_Service_write(UA_Client *client, const UA_WriteRequest request) {
  147. UA_WriteResponse response;
  148. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_WRITEREQUEST],
  149. &response, &UA_TYPES[UA_TYPES_WRITERESPONSE]);
  150. return response;
  151. }
  152. /**
  153. * Method Service Set
  154. * ^^^^^^^^^^^^^^^^^^ */
  155. static UA_INLINE UA_CallResponse
  156. UA_Client_Service_call(UA_Client *client, const UA_CallRequest request) {
  157. UA_CallResponse response;
  158. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_CALLREQUEST],
  159. &response, &UA_TYPES[UA_TYPES_CALLRESPONSE]);
  160. return response;
  161. }
  162. /**
  163. * NodeManagement Service Set
  164. * ^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  165. static UA_INLINE UA_AddNodesResponse
  166. UA_Client_Service_addNodes(UA_Client *client, const UA_AddNodesRequest request) {
  167. UA_AddNodesResponse response;
  168. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  169. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  170. return response;
  171. }
  172. static UA_INLINE UA_AddReferencesResponse
  173. UA_Client_Service_addReferences(UA_Client *client,
  174. const UA_AddReferencesRequest request) {
  175. UA_AddReferencesResponse response;
  176. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  177. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  178. return response;
  179. }
  180. static UA_INLINE UA_DeleteNodesResponse
  181. UA_Client_Service_deleteNodes(UA_Client *client,
  182. const UA_DeleteNodesRequest request) {
  183. UA_DeleteNodesResponse response;
  184. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  185. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  186. return response;
  187. }
  188. static UA_INLINE UA_DeleteReferencesResponse
  189. UA_Client_Service_deleteReferences(UA_Client *client,
  190. const UA_DeleteReferencesRequest request) {
  191. UA_DeleteReferencesResponse response;
  192. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  193. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  194. return response;
  195. }
  196. /**
  197. * View Service Set
  198. * ^^^^^^^^^^^^^^^^ */
  199. static UA_INLINE UA_BrowseResponse
  200. UA_Client_Service_browse(UA_Client *client, const UA_BrowseRequest request) {
  201. UA_BrowseResponse response;
  202. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSEREQUEST],
  203. &response, &UA_TYPES[UA_TYPES_BROWSERESPONSE]);
  204. return response;
  205. }
  206. static UA_INLINE UA_BrowseNextResponse
  207. UA_Client_Service_browseNext(UA_Client *client,
  208. const UA_BrowseNextRequest request) {
  209. UA_BrowseNextResponse response;
  210. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSENEXTREQUEST],
  211. &response, &UA_TYPES[UA_TYPES_BROWSENEXTRESPONSE]);
  212. return response;
  213. }
  214. static UA_INLINE UA_TranslateBrowsePathsToNodeIdsResponse
  215. UA_Client_Service_translateBrowsePathsToNodeIds(UA_Client *client,
  216. const UA_TranslateBrowsePathsToNodeIdsRequest request) {
  217. UA_TranslateBrowsePathsToNodeIdsResponse response;
  218. __UA_Client_Service(client, &request,
  219. &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSREQUEST],
  220. &response,
  221. &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE]);
  222. return response;
  223. }
  224. static UA_INLINE UA_RegisterNodesResponse
  225. UA_Client_Service_registerNodes(UA_Client *client,
  226. const UA_RegisterNodesRequest request) {
  227. UA_RegisterNodesResponse response;
  228. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERNODESREQUEST],
  229. &response, &UA_TYPES[UA_TYPES_REGISTERNODESRESPONSE]);
  230. return response;
  231. }
  232. static UA_INLINE UA_UnregisterNodesResponse
  233. UA_Client_Service_unregisterNodes(UA_Client *client,
  234. const UA_UnregisterNodesRequest request) {
  235. UA_UnregisterNodesResponse response;
  236. __UA_Client_Service(client, &request,
  237. &UA_TYPES[UA_TYPES_UNREGISTERNODESREQUEST],
  238. &response, &UA_TYPES[UA_TYPES_UNREGISTERNODESRESPONSE]);
  239. return response;
  240. }
  241. /**
  242. * Query Service Set
  243. * ^^^^^^^^^^^^^^^^^ */
  244. static UA_INLINE UA_QueryFirstResponse
  245. UA_Client_Service_queryFirst(UA_Client *client,
  246. const UA_QueryFirstRequest request) {
  247. UA_QueryFirstResponse response;
  248. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  249. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  250. return response;
  251. }
  252. static UA_INLINE UA_QueryNextResponse
  253. UA_Client_Service_queryNext(UA_Client *client,
  254. const UA_QueryNextRequest request) {
  255. UA_QueryNextResponse response;
  256. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  257. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  258. return response;
  259. }
  260. #ifdef UA_ENABLE_SUBSCRIPTIONS
  261. /**
  262. * MonitoredItem Service Set
  263. * ^^^^^^^^^^^^^^^^^^^^^^^^^ */
  264. static UA_INLINE UA_CreateMonitoredItemsResponse
  265. UA_Client_Service_createMonitoredItems(UA_Client *client,
  266. const UA_CreateMonitoredItemsRequest request) {
  267. UA_CreateMonitoredItemsResponse response;
  268. __UA_Client_Service(client, &request,
  269. &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSREQUEST], &response,
  270. &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSRESPONSE]);
  271. return response;
  272. }
  273. static UA_INLINE UA_DeleteMonitoredItemsResponse
  274. UA_Client_Service_deleteMonitoredItems(UA_Client *client,
  275. const UA_DeleteMonitoredItemsRequest request) {
  276. UA_DeleteMonitoredItemsResponse response;
  277. __UA_Client_Service(client, &request,
  278. &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSREQUEST], &response,
  279. &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSRESPONSE]);
  280. return response;
  281. }
  282. /**
  283. * Subscription Service Set
  284. * ^^^^^^^^^^^^^^^^^^^^^^^^ */
  285. static UA_INLINE UA_CreateSubscriptionResponse
  286. UA_Client_Service_createSubscription(UA_Client *client,
  287. const UA_CreateSubscriptionRequest request) {
  288. UA_CreateSubscriptionResponse response;
  289. __UA_Client_Service(client, &request,
  290. &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONREQUEST], &response,
  291. &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONRESPONSE]);
  292. return response;
  293. }
  294. static UA_INLINE UA_ModifySubscriptionResponse
  295. UA_Client_Service_modifySubscription(UA_Client *client,
  296. const UA_ModifySubscriptionRequest request) {
  297. UA_ModifySubscriptionResponse response;
  298. __UA_Client_Service(client, &request,
  299. &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONREQUEST], &response,
  300. &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONRESPONSE]);
  301. return response;
  302. }
  303. static UA_INLINE UA_DeleteSubscriptionsResponse
  304. UA_Client_Service_deleteSubscriptions(UA_Client *client,
  305. const UA_DeleteSubscriptionsRequest request) {
  306. UA_DeleteSubscriptionsResponse response;
  307. __UA_Client_Service(client, &request,
  308. &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSREQUEST], &response,
  309. &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSRESPONSE]);
  310. return response;
  311. }
  312. static UA_INLINE UA_PublishResponse
  313. UA_Client_Service_publish(UA_Client *client, const UA_PublishRequest request) {
  314. UA_PublishResponse response;
  315. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  316. &response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  317. return response;
  318. }
  319. #endif
  320. /** .. include:: client_highlevel.rst */
  321. #ifdef __cplusplus
  322. } // extern "C"
  323. #endif
  324. #endif /* UA_CLIENT_H_ */