ua_client.h 13 KB

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