ua_client.h 13 KB

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