ua_client.h 14 KB

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