client_async.c 7.5 KB

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