client_async.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #ifndef _XOPEN_SOURCE
  4. # define _XOPEN_SOURCE 600
  5. #endif
  6. #ifndef _DEFAULT_SOURCE
  7. # define _DEFAULT_SOURCE
  8. #endif
  9. /* On older systems we need to define _BSD_SOURCE.
  10. * _DEFAULT_SOURCE is an alias for that. */
  11. #ifndef _BSD_SOURCE
  12. # define _BSD_SOURCE
  13. #endif
  14. #include <stdio.h>
  15. #include "open62541.h"
  16. #define NODES_EXIST
  17. /* async connection callback, it only gets called after the completion of the whole
  18. * connection process*/
  19. static void
  20. onConnect (UA_Client *client, void *userdata, UA_UInt32 requestId,
  21. void *status) {
  22. printf ("Async connect returned with status code %s\n",
  23. UA_StatusCode_name (*(UA_StatusCode *) status));
  24. }
  25. static
  26. void
  27. fileBrowsed (UA_Client *client, void *userdata, UA_UInt32 requestId,
  28. UA_BrowseResponse *response) {
  29. printf ("%-50s%i\n", "Received BrowseResponse for request ", requestId);
  30. UA_String us = *(UA_String *) userdata;
  31. printf ("---%.*s passed safely \n", (int) us.length, us.data);
  32. }
  33. /*high-level function callbacks*/
  34. static
  35. void
  36. readValueAttributeCallback (UA_Client *client, void *userdata,
  37. UA_UInt32 requestId, UA_Variant *var) {
  38. printf ("%-50s%i\n", "Read value attribute for request", requestId);
  39. if (UA_Variant_hasScalarType (var, &UA_TYPES[UA_TYPES_INT32])) {
  40. UA_Int32 int_val = *(UA_Int32*) var->data;
  41. printf ("---%-40s%-8i\n",
  42. "Reading the value of node (1, \"the.answer\"):", int_val);
  43. }
  44. /*more type distinctions possible*/
  45. return;
  46. }
  47. static
  48. void
  49. attrWritten (UA_Client *client, void *userdata, UA_UInt32 requestId,
  50. UA_WriteResponse *response) {
  51. /*assuming no data to be retrieved by writing attributes*/
  52. printf ("%-50s%i\n", "Wrote value attribute for request ", requestId);
  53. UA_WriteResponse_deleteMembers(response);
  54. }
  55. #ifdef NODES_EXIST
  56. #ifdef UA_ENABLE_METHODCALLS
  57. static void
  58. methodCalled(UA_Client *client, void *userdata, UA_UInt32 requestId,
  59. UA_CallResponse *response) {
  60. printf ("%-50s%i\n", "Called method for request ", requestId);
  61. size_t outputSize;
  62. UA_Variant *output;
  63. UA_StatusCode retval = response->responseHeader.serviceResult;
  64. if (retval == UA_STATUSCODE_GOOD) {
  65. if (response->resultsSize == 1)
  66. retval = response->results[0].statusCode;
  67. else
  68. retval = UA_STATUSCODE_BADUNEXPECTEDERROR;
  69. }
  70. if (retval != UA_STATUSCODE_GOOD) {
  71. UA_CallResponse_deleteMembers (response);
  72. }
  73. /* Move the output arguments */
  74. output = response->results[0].outputArguments;
  75. outputSize = response->results[0].outputArgumentsSize;
  76. response->results[0].outputArguments = NULL;
  77. response->results[0].outputArgumentsSize = 0;
  78. if (retval == UA_STATUSCODE_GOOD) {
  79. printf ("---Method call was successful, returned %lu values.\n",
  80. (unsigned long) outputSize);
  81. UA_Array_delete (output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  82. }
  83. else {
  84. printf ("---Method call was unsuccessful, returned %x values.\n",
  85. retval);
  86. }
  87. UA_CallResponse_deleteMembers (response);
  88. }
  89. static void
  90. translateCalled (UA_Client *client, void *userdata, UA_UInt32 requestId,
  91. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  92. printf ("%-50s%i\n", "Translated path for request ", requestId);
  93. if (response->results[0].targetsSize == 1) {
  94. return;
  95. }
  96. UA_TranslateBrowsePathsToNodeIdsResponse_deleteMembers (response);
  97. }
  98. #endif /* UA_ENABLE_METHODCALLS */
  99. #endif
  100. int
  101. main (int argc, char *argv[]) {
  102. UA_Client *client = UA_Client_new (UA_ClientConfig_default);
  103. UA_UInt32 reqId = 0;
  104. UA_String userdata = UA_STRING ("userdata");
  105. UA_BrowseRequest bReq;
  106. UA_BrowseRequest_init (&bReq);
  107. bReq.requestedMaxReferencesPerNode = 0;
  108. bReq.nodesToBrowse = UA_BrowseDescription_new ();
  109. bReq.nodesToBrowseSize = 1;
  110. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC (0,
  111. UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */
  112. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
  113. UA_Client_connect_async (client, "opc.tcp://localhost:4840", onConnect,
  114. NULL);
  115. /*Windows needs time to response*/
  116. UA_sleep_ms(100);
  117. /* What happens if client tries to send request before connected? */
  118. UA_Client_sendAsyncBrowseRequest (client, &bReq, fileBrowsed, &userdata,
  119. &reqId);
  120. UA_DateTime startTime = UA_DateTime_nowMonotonic();
  121. do {
  122. /*TODO: fix memory-related bugs if condition not checked*/
  123. if (UA_Client_getState (client) == UA_CLIENTSTATE_SESSION) {
  124. /* If not connected requests are not sent */
  125. UA_Client_sendAsyncBrowseRequest (client, &bReq, fileBrowsed,
  126. &userdata, &reqId);
  127. }
  128. /* Requests are processed */
  129. UA_BrowseRequest_deleteMembers(&bReq);
  130. UA_Client_run_iterate (client, 0);
  131. UA_sleep_ms(100);
  132. /* Break loop if server cannot be connected within 2s -- prevents build timeout */
  133. if (UA_DateTime_nowMonotonic() - startTime > 2000 * UA_DATETIME_MSEC)
  134. break;
  135. }
  136. while (reqId < 10);
  137. /* Demo: high-level functions */
  138. UA_Int32 value = 0;
  139. UA_Variant myVariant;
  140. UA_Variant_init(&myVariant);
  141. UA_Variant input;
  142. UA_Variant_init (&input);
  143. for (UA_UInt16 i = 0; i < 5; i++) {
  144. if (UA_Client_getState (client) == UA_CLIENTSTATE_SESSION) {
  145. /* writing and reading value 1 to 5 */
  146. UA_Variant_setScalarCopy (&myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
  147. value++;
  148. UA_Client_writeValueAttribute_async(client,
  149. UA_NODEID_STRING (1, "the.answer"),
  150. &myVariant, attrWritten, NULL,
  151. &reqId);
  152. UA_Variant_deleteMembers (&myVariant);
  153. UA_Client_readValueAttribute_async(client,
  154. UA_NODEID_STRING (1, "the.answer"),
  155. readValueAttributeCallback, NULL,
  156. &reqId);
  157. //TODO: check the existance of the nodes inside these functions (otherwise seg faults)
  158. #ifdef NODES_EXIST
  159. #ifdef UA_ENABLE_METHODCALLS
  160. UA_String stringValue = UA_String_fromChars ("World");
  161. UA_Variant_setScalar (&input, &stringValue, &UA_TYPES[UA_TYPES_STRING]);
  162. UA_Client_call_async(client,
  163. UA_NODEID_NUMERIC (0, UA_NS0ID_OBJECTSFOLDER),
  164. UA_NODEID_NUMERIC (1, 62541), 1, &input,
  165. methodCalled, NULL, &reqId);
  166. UA_String_deleteMembers(&stringValue);
  167. #define pathSize 3
  168. char *paths[pathSize] = { "Server", "ServerStatus", "State" };
  169. UA_UInt32 ids[pathSize] = { UA_NS0ID_ORGANIZES,
  170. UA_NS0ID_HASCOMPONENT, UA_NS0ID_HASCOMPONENT };
  171. UA_Cient_translateBrowsePathsToNodeIds_async (client, paths, ids,
  172. pathSize,
  173. translateCalled, NULL,
  174. &reqId);
  175. #endif /* UA_ENABLE_METHODCALLS */
  176. #endif
  177. /* How often UA_Client_run_iterate is called depends on the number of request sent */
  178. UA_Client_run_iterate(client, 0);
  179. UA_Client_run_iterate(client, 0);
  180. }
  181. }
  182. UA_Client_run_iterate (client, 0);
  183. /* Async disconnect kills unprocessed requests */
  184. // UA_Client_disconnect_async (client, &reqId); //can only be used when connected = true
  185. // UA_Client_run_iterate (client, &timedOut);
  186. UA_Client_disconnect(client);
  187. UA_Client_delete (client);
  188. return (int) UA_STATUSCODE_GOOD;
  189. }