ua_client.h 13 KB

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