ua_client.h 14 KB

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