client_async.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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();
  92. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  93. UA_UInt32 reqId = 0;
  94. UA_String userdata = UA_STRING("userdata");
  95. UA_BrowseRequest bReq;
  96. UA_BrowseRequest_init(&bReq);
  97. bReq.requestedMaxReferencesPerNode = 0;
  98. bReq.nodesToBrowse = UA_BrowseDescription_new();
  99. bReq.nodesToBrowseSize = 1;
  100. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  101. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
  102. UA_Client_connect_async(client, "opc.tcp://localhost:4840", onConnect, NULL);
  103. /*Windows needs time to response*/
  104. UA_sleep_ms(100);
  105. /* What happens if client tries to send request before connected? */
  106. UA_Client_sendAsyncBrowseRequest(client, &bReq, fileBrowsed, &userdata, &reqId);
  107. UA_DateTime startTime = UA_DateTime_nowMonotonic();
  108. do {
  109. /*TODO: fix memory-related bugs if condition not checked*/
  110. if(UA_Client_getState(client) == UA_CLIENTSTATE_SESSION) {
  111. /* If not connected requests are not sent */
  112. UA_Client_sendAsyncBrowseRequest(client, &bReq, fileBrowsed, &userdata, &reqId);
  113. }
  114. /* Requests are processed */
  115. UA_BrowseRequest_clear(&bReq);
  116. UA_Client_run_iterate(client, 0);
  117. UA_sleep_ms(100);
  118. /* Break loop if server cannot be connected within 2s -- prevents build timeout */
  119. if(UA_DateTime_nowMonotonic() - startTime > 2000 * UA_DATETIME_MSEC)
  120. break;
  121. } while(reqId < 10);
  122. /* Demo: high-level functions */
  123. UA_Int32 value = 0;
  124. UA_Variant myVariant;
  125. UA_Variant_init(&myVariant);
  126. UA_Variant input;
  127. UA_Variant_init(&input);
  128. for(UA_UInt16 i = 0; i < 5; i++) {
  129. if(UA_Client_getState(client) == UA_CLIENTSTATE_SESSION) {
  130. /* writing and reading value 1 to 5 */
  131. UA_Variant_setScalarCopy(&myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
  132. value++;
  133. UA_Client_writeValueAttribute_async(client,
  134. UA_NODEID_STRING(1, "the.answer"),
  135. &myVariant, attrWritten, NULL,
  136. &reqId);
  137. UA_Variant_clear(&myVariant);
  138. UA_Client_readValueAttribute_async(client,
  139. UA_NODEID_STRING(1, "the.answer"),
  140. readValueAttributeCallback, NULL,
  141. &reqId);
  142. //TODO: check the existance of the nodes inside these functions (otherwise seg faults)
  143. #ifdef NODES_EXIST
  144. #ifdef UA_ENABLE_METHODCALLS
  145. UA_String stringValue = UA_String_fromChars("World");
  146. UA_Variant_setScalar(&input, &stringValue, &UA_TYPES[UA_TYPES_STRING]);
  147. UA_Client_call_async(client,
  148. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  149. UA_NODEID_NUMERIC(1, 62541), 1, &input,
  150. methodCalled, NULL, &reqId);
  151. UA_String_clear(&stringValue);
  152. #define pathSize 3
  153. char *paths[pathSize] = { "Server", "ServerStatus", "State" };
  154. UA_UInt32 ids[pathSize] = { UA_NS0ID_ORGANIZES,
  155. UA_NS0ID_HASCOMPONENT, UA_NS0ID_HASCOMPONENT };
  156. UA_Cient_translateBrowsePathsToNodeIds_async(client, paths, ids, pathSize,
  157. translateCalled, NULL, &reqId);
  158. #endif /* UA_ENABLE_METHODCALLS */
  159. #endif
  160. /* How often UA_Client_run_iterate is called depends on the number of request sent */
  161. UA_Client_run_iterate(client, 0);
  162. UA_Client_run_iterate(client, 0);
  163. }
  164. }
  165. UA_Client_run_iterate(client, 0);
  166. /* Async disconnect kills unprocessed requests */
  167. // UA_Client_disconnect_async (client, &reqId); //can only be used when connected = true
  168. // UA_Client_run_iterate (client, &timedOut);
  169. UA_Client_disconnect(client);
  170. UA_Client_delete(client);
  171. return (int) UA_STATUSCODE_GOOD;
  172. }