client_async.c 7.6 KB

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