ua_client.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #ifndef UA_CLIENT_H_
  2. #define UA_CLIENT_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include "ua_config.h"
  7. #include "ua_types.h"
  8. #include "ua_connection.h"
  9. #include "ua_log.h"
  10. #include "ua_types_generated.h"
  11. struct UA_Client;
  12. typedef struct UA_Client UA_Client;
  13. typedef enum {
  14. UA_CLIENTSTATE_READY,
  15. UA_CLIENTSTATE_CONNECTED,
  16. UA_CLIENTSTATE_FAULTED,
  17. UA_CLIENTSTATE_ERRORED
  18. } UA_Client_State;
  19. typedef struct UA_ClientConfig {
  20. UA_UInt32 timeout; //sync response timeout
  21. UA_UInt32 secureChannelLifeTime; // lifetime in ms (then the channel needs to be renewed)
  22. UA_ConnectionConfig localConnectionConfig;
  23. } UA_ClientConfig;
  24. extern UA_EXPORT const UA_ClientConfig UA_ClientConfig_standard;
  25. /**
  26. * Creates and initialize a Client object
  27. *
  28. * @param config for the new client. You can use UA_ClientConfig_standard which has sane defaults
  29. * @param logger function pointer to a logger function. See examples/logger_stdout.c for a simple implementation
  30. * @return return the new Client object
  31. */
  32. UA_Client UA_EXPORT * UA_Client_new(UA_ClientConfig config, UA_Logger logger);
  33. /**
  34. * resets a Client object
  35. *
  36. * @param client to reset
  37. * @return Void
  38. */
  39. void UA_EXPORT UA_Client_reset(UA_Client* client);
  40. /**
  41. * delete a Client object
  42. *
  43. * @param client to delete
  44. * @return Void
  45. */
  46. void UA_EXPORT UA_Client_delete(UA_Client* client);
  47. /*************************/
  48. /* Manage the Connection */
  49. /*************************/
  50. typedef UA_Connection (*UA_ConnectClientConnection)(UA_ConnectionConfig localConf, const char *endpointUrl,
  51. UA_Logger logger);
  52. /**
  53. * Gets a list of endpoints of a server
  54. * @param client to use
  55. * @param connection function. You can use ClientNetworkLayerTCP_connect from examples/networklayer_tcp.h
  56. * @param server url to connect (for example "opc.tcp://localhost:16664")
  57. * @param endpointDescriptionsSize size of the array of endpoint descriptions
  58. * @param endpointDescriptions array of endpoint descriptions that is allocated by the function (you need to free manually)
  59. * @return Indicates whether the operation succeeded or returns an error code
  60. */
  61. UA_StatusCode UA_EXPORT
  62. UA_Client_getEndpoints(UA_Client *client, UA_ConnectClientConnection connectFunc,
  63. const char *serverUrl, size_t* endpointDescriptionsSize,
  64. UA_EndpointDescription** endpointDescriptions);
  65. /**
  66. * start a connection to the selected server
  67. *
  68. * @param client to use
  69. * @param connection function. You can use ClientNetworkLayerTCP_connect from examples/networklayer_tcp.h
  70. * @param endpointURL to connect (for example "opc.tcp://localhost:16664")
  71. * @return Indicates whether the operation succeeded or returns an error code
  72. */
  73. UA_StatusCode UA_EXPORT
  74. UA_Client_connect(UA_Client *client, UA_ConnectClientConnection connFunc, const char *endpointUrl);
  75. /**
  76. * start a connection to the selected server
  77. *
  78. * @param client to use
  79. * @param connection function. You can use ClientNetworkLayerTCP_connect from examples/networklayer_tcp.h
  80. * @param endpointURL to connect (for example "opc.tcp://localhost:16664")
  81. * @param username
  82. * @param password
  83. * @return Indicates whether the operation succeeded or returns an error code
  84. */
  85. UA_StatusCode UA_EXPORT
  86. UA_Client_connect_username(UA_Client *client, UA_ConnectClientConnection connFunc, const char *endpointUrl, const char *username, const char *password);
  87. /**
  88. * close a connection to the selected server
  89. *
  90. * @param client to use
  91. * @return Indicates whether the operation succeeded or returns an error code
  92. */
  93. UA_StatusCode UA_EXPORT UA_Client_disconnect(UA_Client *client);
  94. /**
  95. * renew a secure channel if needed
  96. *
  97. * @param client to use
  98. * @return Indicates whether the operation succeeded or returns an error code
  99. */
  100. UA_StatusCode UA_EXPORT UA_Client_manuallyRenewSecureChannel(UA_Client *client);
  101. /**
  102. * @brief Get the client connection status
  103. *
  104. * Returns the client connection status, being one of the following:
  105. * - UA_CLIENTSTATE_READY: The client is not connected but initialized and ready to use.
  106. * - UA_CLIENTSTATE_CONNECTED: The client is connected to a server.
  107. * - UA_CLIENTSTATE_FAULTED: An error has occured that might have influenced the connection state.
  108. * A successfull service call or renewal of the secure channel will
  109. * reset the state to CONNECTED.
  110. * - UA_CLIENTSTATE_ERRORED: A non-recoverable error has occured and the connection is no longer
  111. * reliable. The client needs to be disconnected and reinitialized to
  112. * recover into a CONNECTED state.
  113. * @param client to use.
  114. * @return Current state of the client.
  115. */
  116. UA_Client_State UA_EXPORT UA_Client_getState(UA_Client *client);
  117. /****************/
  118. /* Raw Services */
  119. /****************/
  120. /* Don't use this function. There are typed versions. */
  121. void UA_EXPORT
  122. __UA_Client_Service(UA_Client *client, const void *request, const UA_DataType *requestType,
  123. void *response, const UA_DataType *responseType);
  124. /* NodeManagement Service Set */
  125. /**
  126. * performs a service
  127. *
  128. * @param client to use
  129. * @param request to use
  130. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  131. */
  132. static UA_INLINE UA_AddNodesResponse
  133. UA_Client_Service_addNodes(UA_Client *client, const UA_AddNodesRequest request) {
  134. UA_AddNodesResponse response;
  135. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  136. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  137. return response; }
  138. /**
  139. * performs a service
  140. *
  141. * @param client to use
  142. * @param request to use
  143. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  144. */
  145. static UA_INLINE UA_AddReferencesResponse
  146. UA_Client_Service_addReferences(UA_Client *client, const UA_AddReferencesRequest request) {
  147. UA_AddReferencesResponse response;
  148. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_ADDNODESREQUEST],
  149. &response, &UA_TYPES[UA_TYPES_ADDNODESRESPONSE]);
  150. return response; }
  151. /**
  152. * performs a service
  153. *
  154. * @param client to use
  155. * @param request to use
  156. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  157. */
  158. static UA_INLINE UA_DeleteNodesResponse
  159. UA_Client_Service_deleteNodes(UA_Client *client, const UA_DeleteNodesRequest request) {
  160. UA_DeleteNodesResponse response;
  161. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  162. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  163. return response; }
  164. /**
  165. * performs a service
  166. *
  167. * @param client to use
  168. * @param request to use
  169. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  170. */
  171. static UA_INLINE UA_DeleteReferencesResponse
  172. UA_Client_Service_deleteReferences(UA_Client *client, const UA_DeleteReferencesRequest request) {
  173. UA_DeleteReferencesResponse response;
  174. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETENODESREQUEST],
  175. &response, &UA_TYPES[UA_TYPES_DELETENODESRESPONSE]);
  176. return response; }
  177. /* View Service Set */
  178. /**
  179. * performs a service
  180. *
  181. * @param client to use
  182. * @param request to use
  183. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  184. */
  185. static UA_INLINE UA_BrowseResponse
  186. UA_Client_Service_browse(UA_Client *client, const UA_BrowseRequest request) {
  187. UA_BrowseResponse response;
  188. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSEREQUEST],
  189. &response, &UA_TYPES[UA_TYPES_BROWSERESPONSE]);
  190. return response; }
  191. /**
  192. * performs a service
  193. *
  194. * @param client to use
  195. * @param request to use
  196. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  197. */
  198. static UA_INLINE UA_BrowseNextResponse
  199. UA_Client_Service_browseNext(UA_Client *client, const UA_BrowseNextRequest request) {
  200. UA_BrowseNextResponse response;
  201. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_BROWSENEXTREQUEST],
  202. &response, &UA_TYPES[UA_TYPES_BROWSENEXTRESPONSE]);
  203. return response; }
  204. /**
  205. * performs a service
  206. *
  207. * @param client to use
  208. * @param request to use
  209. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  210. */
  211. static UA_INLINE UA_TranslateBrowsePathsToNodeIdsResponse
  212. UA_Client_Service_translateBrowsePathsToNodeIds(UA_Client *client,
  213. const UA_TranslateBrowsePathsToNodeIdsRequest request) {
  214. UA_TranslateBrowsePathsToNodeIdsResponse response;
  215. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSREQUEST],
  216. &response, &UA_TYPES[UA_TYPES_TRANSLATEBROWSEPATHSTONODEIDSRESPONSE]);
  217. return response; }
  218. /**
  219. * performs a service
  220. *
  221. * @param client to use
  222. * @param request to use
  223. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  224. */
  225. static UA_INLINE UA_RegisterNodesResponse
  226. UA_Client_Service_registerNodes(UA_Client *client, const UA_RegisterNodesRequest request) {
  227. UA_RegisterNodesResponse response;
  228. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERNODESREQUEST],
  229. &response, &UA_TYPES[UA_TYPES_REGISTERNODESRESPONSE]);
  230. return response; }
  231. /**
  232. * performs a service
  233. *
  234. * @param client to use
  235. * @param request to use
  236. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  237. */
  238. static UA_INLINE UA_UnregisterNodesResponse
  239. UA_Client_Service_unregisterNodes(UA_Client *client, const UA_UnregisterNodesRequest request) {
  240. UA_UnregisterNodesResponse response;
  241. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_UNREGISTERNODESREQUEST],
  242. &response, &UA_TYPES[UA_TYPES_UNREGISTERNODESRESPONSE]);
  243. return response; }
  244. /* Query Service Set */
  245. /**
  246. * performs a service
  247. *
  248. * @param client to use
  249. * @param request to use
  250. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  251. */
  252. static UA_INLINE UA_QueryFirstResponse
  253. UA_Client_Service_queryFirst(UA_Client *client, const UA_QueryFirstRequest request) {
  254. UA_QueryFirstResponse response;
  255. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  256. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  257. return response; }
  258. /**
  259. * performs a service
  260. *
  261. * @param client to use
  262. * @param request to use
  263. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  264. */
  265. static UA_INLINE UA_QueryNextResponse
  266. UA_Client_Service_queryNext(UA_Client *client, const UA_QueryNextRequest request) {
  267. UA_QueryNextResponse response;
  268. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_QUERYFIRSTREQUEST],
  269. &response, &UA_TYPES[UA_TYPES_QUERYFIRSTRESPONSE]);
  270. return response; }
  271. /* Attribute Service Set */
  272. /**
  273. * performs a service
  274. *
  275. * @param client to use
  276. * @param request to use
  277. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  278. */
  279. static UA_INLINE UA_ReadResponse
  280. UA_Client_Service_read(UA_Client *client, const UA_ReadRequest request) {
  281. UA_ReadResponse response;
  282. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_READREQUEST],
  283. &response, &UA_TYPES[UA_TYPES_READRESPONSE]);
  284. return response; }
  285. /**
  286. * performs a service
  287. *
  288. * @param client to use
  289. * @param request to use
  290. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  291. */
  292. static UA_INLINE UA_WriteResponse
  293. UA_Client_Service_write(UA_Client *client, const UA_WriteRequest request) {
  294. UA_WriteResponse response;
  295. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_WRITEREQUEST],
  296. &response, &UA_TYPES[UA_TYPES_WRITERESPONSE]);
  297. return response; }
  298. /* Method Service Set */
  299. /**
  300. * performs a service
  301. *
  302. * @param client to use
  303. * @param request to use
  304. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  305. */
  306. static UA_INLINE UA_CallResponse
  307. UA_Client_Service_call(UA_Client *client, const UA_CallRequest request) {
  308. UA_CallResponse response;
  309. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_CALLREQUEST],
  310. &response, &UA_TYPES[UA_TYPES_CALLRESPONSE]);
  311. return response; }
  312. #ifdef UA_ENABLE_SUBSCRIPTIONS
  313. /* MonitoredItem Service Set */
  314. /**
  315. * performs a service
  316. *
  317. * @param client to use
  318. * @param request to use
  319. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  320. */
  321. static UA_INLINE UA_CreateMonitoredItemsResponse
  322. UA_Client_Service_createMonitoredItems(UA_Client *client, const UA_CreateMonitoredItemsRequest request) {
  323. UA_CreateMonitoredItemsResponse response;
  324. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSREQUEST],
  325. &response, &UA_TYPES[UA_TYPES_CREATEMONITOREDITEMSRESPONSE]);
  326. return response; }
  327. /**
  328. * performs a service
  329. *
  330. * @param client to use
  331. * @param request to use
  332. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  333. */
  334. static UA_INLINE UA_DeleteMonitoredItemsResponse
  335. UA_Client_Service_deleteMonitoredItems(UA_Client *client, const UA_DeleteMonitoredItemsRequest request) {
  336. UA_DeleteMonitoredItemsResponse response;
  337. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSREQUEST],
  338. &response, &UA_TYPES[UA_TYPES_DELETEMONITOREDITEMSRESPONSE]);
  339. return response; }
  340. /* Subscription Service Set */
  341. /**
  342. * performs a service
  343. *
  344. * @param client to use
  345. * @param request to use
  346. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  347. */
  348. static UA_INLINE UA_CreateSubscriptionResponse
  349. UA_Client_Service_createSubscription(UA_Client *client, const UA_CreateSubscriptionRequest request) {
  350. UA_CreateSubscriptionResponse response;
  351. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONREQUEST],
  352. &response, &UA_TYPES[UA_TYPES_CREATESUBSCRIPTIONRESPONSE]);
  353. return response; }
  354. /**
  355. * performs a service
  356. *
  357. * @param client to use
  358. * @param request to use
  359. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  360. */
  361. static UA_INLINE UA_ModifySubscriptionResponse
  362. UA_Client_Service_modifySubscription(UA_Client *client, const UA_ModifySubscriptionRequest request) {
  363. UA_ModifySubscriptionResponse response;
  364. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONREQUEST],
  365. &response, &UA_TYPES[UA_TYPES_MODIFYSUBSCRIPTIONRESPONSE]);
  366. return response; }
  367. /**
  368. * performs a service
  369. *
  370. * @param client to use
  371. * @param request to use
  372. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  373. */
  374. static UA_INLINE UA_DeleteSubscriptionsResponse
  375. UA_Client_Service_deleteSubscriptions(UA_Client *client, const UA_DeleteSubscriptionsRequest request) {
  376. UA_DeleteSubscriptionsResponse response;
  377. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSREQUEST],
  378. &response, &UA_TYPES[UA_TYPES_DELETESUBSCRIPTIONSRESPONSE]);
  379. return response; }
  380. /**
  381. * performs a service
  382. *
  383. * @param client to use
  384. * @param request to use
  385. * @return returns the response which has the UA_StatusCode in a UA_ResponseHeader
  386. */
  387. static UA_INLINE UA_PublishResponse
  388. UA_Client_Service_publish(UA_Client *client, const UA_PublishRequest request) {
  389. UA_PublishResponse response;
  390. __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_PUBLISHREQUEST],
  391. &response, &UA_TYPES[UA_TYPES_PUBLISHRESPONSE]);
  392. return response; }
  393. #endif
  394. #ifdef __cplusplus
  395. } // extern "C"
  396. #endif
  397. #endif /* UA_CLIENT_H_ */