client_async.c 7.4 KB

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