ua_client.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 time, 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. /* Custom DataTypes */
  54. size_t customDataTypesSize;
  55. const UA_DataType *customDataTypes;
  56. } UA_ClientConfig;
  57. /**
  58. * Client Lifecycle
  59. * ---------------- */
  60. typedef enum {
  61. UA_CLIENTSTATE_READY, /* The client is not connected but initialized
  62. and ready to use. */
  63. UA_CLIENTSTATE_CONNECTED, /* The client is connected to a server. */
  64. UA_CLIENTSTATE_FAULTED, /* An error has occured that might have
  65. influenced the connection state. A successfull
  66. service call or renewal of the secure channel
  67. will reset the state to CONNECTED. */
  68. UA_CLIENTSTATE_ERRORED /* A non-recoverable error has occured and the
  69. connection is no longer reliable. The client
  70. needs to be disconnected and reinitialized to
  71. recover into a CONNECTED state. */
  72. } UA_ClientState;
  73. struct UA_Client;
  74. typedef struct UA_Client UA_Client;
  75. /* Create a new client
  76. *
  77. * @param config for the new client. You can use UA_ClientConfig_standard
  78. * which has sane defaults
  79. * @param logger function pointer to a logger function. See
  80. * examples/logger_stdout.c for a simple implementation
  81. * @return return the new Client object */
  82. UA_Client UA_EXPORT * UA_Client_new(UA_ClientConfig config);
  83. /* Get the client connection status */
  84. UA_ClientState UA_EXPORT UA_Client_getState(UA_Client *client);
  85. /* Reset a client */
  86. void UA_EXPORT UA_Client_reset(UA_Client *client);
  87. /* Delete a client */
  88. void UA_EXPORT UA_Client_delete(UA_Client *client);
  89. /**
  90. * Manage the Connection
  91. * --------------------- */
  92. /* Gets a list of endpoints of a server
  93. *
  94. * @param client to use
  95. * @param server url to connect (for example "opc.tcp://localhost:16664")
  96. * @param endpointDescriptionsSize size of the array of endpoint descriptions
  97. * @param endpointDescriptions array of endpoint descriptions that is allocated
  98. * by the function (you need to free manually)
  99. * @return Indicates whether the operation succeeded or returns an error code */
  100. UA_StatusCode UA_EXPORT
  101. UA_Client_getEndpoints(UA_Client *client, const char *serverUrl,
  102. size_t* endpointDescriptionsSize,
  103. UA_EndpointDescription** endpointDescriptions);
  104. /* Connect to the selected server
  105. *
  106. * @param client to use
  107. * @param endpointURL to connect (for example "opc.tcp://localhost:16664")
  108. * @return Indicates whether the operation succeeded or returns an error code */
  109. UA_StatusCode UA_EXPORT
  110. UA_Client_connect(UA_Client *client, const char *endpointUrl);
  111. /* Connect to the selected server with the given username and password
  112. *
  113. * @param client to use
  114. * @param endpointURL to connect (for example "opc.tcp://localhost:16664")
  115. * @param username
  116. * @param password
  117. * @return Indicates whether the operation succeeded or returns an error code */
  118. UA_StatusCode UA_EXPORT
  119. UA_Client_connect_username(UA_Client *client, const char *endpointUrl,
  120. const char *username, const char *password);
  121. /* Close a connection to the selected server */
  122. UA_StatusCode UA_EXPORT UA_Client_disconnect(UA_Client *client);
  123. /* Renew the underlying secure channel */
  124. UA_StatusCode UA_EXPORT UA_Client_manuallyRenewSecureChannel(UA_Client *client);
  125. /**
  126. * .. _client-services:
  127. *
  128. * Raw Services
  129. * ------------
  130. * The raw OPC UA services are exposed to the client. But most of them time, it
  131. * is better to use the convenience functions from ``ua_client_highlevel.h``
  132. * that wrap the raw services. */
  133. /* Don't use this function. Use the type versions below instead. */
  134. void UA_EXPORT
  135. __UA_Client_Service(UA_Client *client, const void *request,
  136. const UA_DataType *requestType, void *response,
  137. const UA_DataType *responseType);
  138. /**
  139. * Attribute Service Set
  140. * ^^^^^^^^^^^^^^^^^^^^^ */
  141. static UA_INLINE UA_ReadResponse
  142. UA_Client_Service_read(UA_Client *client, const UA_ReadRequest request) {
  143. UA_ReadResponse response;
  144. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_READREQUEST],
  145. &response, &UA_TYPES[UA_TYPES_READRESPONSE]);
  146. return response;
  147. }
  148. static UA_INLINE UA_WriteResponse
  149. UA_Client_Service_write(UA_Client *client, const UA_WriteRequest request) {
  150. UA_WriteResponse response;
  151. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_WRITEREQUEST],
  152. &response, &UA_TYPES[UA_TYPES_WRITERESPONSE]);
  153. return response;
  154. }
  155. /**
  156. * Method Service Set
  157. * ^^^^^^^^^^^^^^^^^^ */
  158. #ifdef UA_ENABLE_METHODCALLS
  159. static UA_INLINE UA_CallResponse
  160. UA_Client_Service_call(UA_Client *client, const UA_CallRequest request) {
  161. UA_CallResponse response;
  162. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_CALLREQUEST],
  163. &response, &UA_TYPES[UA_TYPES_CALLRESPONSE]);
  164. return response;
  165. }
  166. #endif
  167. /**
  168. * NodeManagement Service Set
  169. * ^^^^^^^^^^^^^^^^^^^^^^^^^^ */
  170. static UA_INLINE UA_AddNodesResponse
  171. UA_Client_Service_addNodes(UA_Client *client, const UA_AddNodesRequest request) {
  172. UA_AddNodesResponse response;
  173. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  174. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  175. return response;
  176. }
  177. static UA_INLINE UA_AddReferencesResponse
  178. UA_Client_Service_addReferences(UA_Client *client,
  179. const UA_AddReferencesRequest request) {
  180. UA_AddReferencesResponse response;
  181. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  182. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  183. return response;
  184. }
  185. static UA_INLINE UA_DeleteNodesResponse
  186. UA_Client_Service_deleteNodes(UA_Client *client,
  187. const UA_DeleteNodesRequest request) {
  188. UA_DeleteNodesResponse response;
  189. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  190. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  191. return response;
  192. }
  193. static UA_INLINE UA_DeleteReferencesResponse
  194. UA_Client_Service_deleteReferences(UA_Client *client,
  195. const UA_DeleteReferencesRequest request) {
  196. UA_DeleteReferencesResponse response;
  197. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  198. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  199. return response;
  200. }
  201. /**
  202. * View Service Set
  203. * ^^^^^^^^^^^^^^^^ */
  204. static UA_INLINE UA_BrowseResponse
  205. UA_Client_Service_browse(UA_Client *client, const UA_BrowseRequest request) {
  206. UA_BrowseResponse response;
  207. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSEREQUEST],
  208. &response, &UA_TYPES[UA_TYPES_BROWSERESPONSE]);
  209. return response;
  210. }
  211. static UA_INLINE UA_BrowseNextResponse
  212. UA_Client_Service_browseNext(UA_Client *client,
  213. const UA_BrowseNextRequest request) {
  214. UA_BrowseNextResponse response;
  215. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSENEXTREQUEST],
  216. &response, &UA_TYPES[UA_TYPES_BROWSENEXTRESPONSE]);
  217. return response;
  218. }
  219. static UA_INLINE UA_TranslateBrowsePathsToNodeIdsResponse
  220. UA_Client_Service_translateBrowsePathsToNodeIds(UA_Client *client,
  221. const UA_TranslateBrowsePathsToNodeIdsRequest request) {
  222. UA_TranslateBrowsePathsToNodeIdsResponse response;
  223. __UA_Client_Service(client, &request,
  224. &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSREQUEST],
  225. &response,
  226. &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE]);
  227. return response;
  228. }
  229. static UA_INLINE UA_RegisterNodesResponse
  230. UA_Client_Service_registerNodes(UA_Client *client,
  231. const UA_RegisterNodesRequest request) {
  232. UA_RegisterNodesResponse response;
  233. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERNODESREQUEST],
  234. &response, &UA_TYPES[UA_TYPES_REGISTERNODESRESPONSE]);
  235. return response;
  236. }
  237. static UA_INLINE UA_UnregisterNodesResponse
  238. UA_Client_Service_unregisterNodes(UA_Client *client,
  239. const UA_UnregisterNodesRequest request) {
  240. UA_UnregisterNodesResponse response;
  241. __UA_Client_Service(client, &request,
  242. &UA_TYPES[UA_TYPES_UNREGISTERNODESREQUEST],
  243. &response, &UA_TYPES[UA_TYPES_UNREGISTERNODESRESPONSE]);
  244. return response;
  245. }
  246. /**
  247. * Query Service Set
  248. * ^^^^^^^^^^^^^^^^^ */
  249. static UA_INLINE UA_QueryFirstResponse
  250. UA_Client_Service_queryFirst(UA_Client *client,
  251. const UA_QueryFirstRequest request) {
  252. UA_QueryFirstResponse response;
  253. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  254. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  255. return response;
  256. }
  257. static UA_INLINE UA_QueryNextResponse
  258. UA_Client_Service_queryNext(UA_Client *client,
  259. const UA_QueryNextRequest request) {
  260. UA_QueryNextResponse response;
  261. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  262. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  263. return response;
  264. }
  265. #ifdef UA_ENABLE_SUBSCRIPTIONS
  266. /**
  267. * MonitoredItem Service Set
  268. * ^^^^^^^^^^^^^^^^^^^^^^^^^ */
  269. static UA_INLINE UA_CreateMonitoredItemsResponse
  270. UA_Client_Service_createMonitoredItems(UA_Client *client,
  271. const UA_CreateMonitoredItemsRequest request) {
  272. UA_CreateMonitoredItemsResponse response;
  273. __UA_Client_Service(client, &request,
  274. &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSREQUEST], &response,
  275. &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSRESPONSE]);
  276. return response;
  277. }
  278. static UA_INLINE UA_DeleteMonitoredItemsResponse
  279. UA_Client_Service_deleteMonitoredItems(UA_Client *client,
  280. const UA_DeleteMonitoredItemsRequest request) {
  281. UA_DeleteMonitoredItemsResponse response;
  282. __UA_Client_Service(client, &request,
  283. &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSREQUEST], &response,
  284. &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSRESPONSE]);
  285. return response;
  286. }
  287. /**
  288. * Subscription Service Set
  289. * ^^^^^^^^^^^^^^^^^^^^^^^^ */
  290. static UA_INLINE UA_CreateSubscriptionResponse
  291. UA_Client_Service_createSubscription(UA_Client *client,
  292. const UA_CreateSubscriptionRequest request) {
  293. UA_CreateSubscriptionResponse response;
  294. __UA_Client_Service(client, &request,
  295. &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONREQUEST], &response,
  296. &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONRESPONSE]);
  297. return response;
  298. }
  299. static UA_INLINE UA_ModifySubscriptionResponse
  300. UA_Client_Service_modifySubscription(UA_Client *client,
  301. const UA_ModifySubscriptionRequest request) {
  302. UA_ModifySubscriptionResponse response;
  303. __UA_Client_Service(client, &request,
  304. &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONREQUEST], &response,
  305. &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONRESPONSE]);
  306. return response;
  307. }
  308. static UA_INLINE UA_DeleteSubscriptionsResponse
  309. UA_Client_Service_deleteSubscriptions(UA_Client *client,
  310. const UA_DeleteSubscriptionsRequest request) {
  311. UA_DeleteSubscriptionsResponse response;
  312. __UA_Client_Service(client, &request,
  313. &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSREQUEST], &response,
  314. &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSRESPONSE]);
  315. return response;
  316. }
  317. static UA_INLINE UA_PublishResponse
  318. UA_Client_Service_publish(UA_Client *client, const UA_PublishRequest request) {
  319. UA_PublishResponse response;
  320. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  321. &response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  322. return response;
  323. }
  324. #endif
  325. /**
  326. * .. toctree::
  327. *
  328. * client_highlevel */
  329. #ifdef __cplusplus
  330. } // extern "C"
  331. #endif
  332. #endif /* UA_CLIENT_H_ */