client_async.c 7.6 KB

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