ua_client.h 13 KB

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