ua_client.h 14 KB

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