client_async.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_deleteMembers(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_deleteMembers (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. }
  82. else {
  83. printf ("---Method call was unsuccessful, returned %x values.\n",
  84. retval);
  85. }
  86. UA_CallResponse_deleteMembers (response);
  87. }
  88. static void
  89. translateCalled (UA_Client *client, void *userdata, UA_UInt32 requestId,
  90. UA_TranslateBrowsePathsToNodeIdsResponse *response) {
  91. printf ("%-50s%i\n", "Translated path for request ", requestId);
  92. if (response->results[0].targetsSize == 1) {
  93. return;
  94. }
  95. UA_TranslateBrowsePathsToNodeIdsResponse_deleteMembers (response);
  96. }
  97. #endif /* UA_ENABLE_METHODCALLS */
  98. #endif
  99. int
  100. main (int argc, char *argv[]) {
  101. UA_Client *client = UA_Client_new (UA_ClientConfig_default);
  102. UA_UInt32 reqId = 0;
  103. UA_String userdata = UA_STRING ("userdata");
  104. UA_BrowseRequest bReq;
  105. UA_BrowseRequest_init (&bReq);
  106. bReq.requestedMaxReferencesPerNode = 0;
  107. bReq.nodesToBrowse = UA_BrowseDescription_new ();
  108. bReq.nodesToBrowseSize = 1;
  109. bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC (0,
  110. UA_NS0ID_OBJECTSFOLDER); /* browse objects folder */
  111. bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; /* return everything */
  112. UA_Client_connect_async (client, "opc.tcp://localhost:4840", onConnect,
  113. NULL);
  114. /*Windows needs time to response*/
  115. UA_sleep_ms(100);
  116. /* What happens if client tries to send request before connected? */
  117. UA_Client_sendAsyncBrowseRequest (client, &bReq, fileBrowsed, &userdata,
  118. &reqId);
  119. UA_DateTime startTime = UA_DateTime_nowMonotonic();
  120. do {
  121. /*TODO: fix memory-related bugs if condition not checked*/
  122. if (UA_Client_getState (client) == UA_CLIENTSTATE_SESSION) {
  123. /* If not connected requests are not sent */
  124. UA_Client_sendAsyncBrowseRequest (client, &bReq, fileBrowsed,
  125. &userdata, &reqId);
  126. }
  127. /* Requests are processed */
  128. UA_BrowseRequest_deleteMembers(&bReq);
  129. UA_Client_run_iterate (client, 0);
  130. UA_sleep_ms(100);
  131. /* Break loop if server cannot be connected within 2s -- prevents build timeout */
  132. if (UA_DateTime_nowMonotonic() - startTime > 2000 * UA_DATETIME_MSEC)
  133. break;
  134. }
  135. while (reqId < 10);
  136. /* Demo: high-level functions */
  137. UA_Int32 value = 0;
  138. UA_Variant myVariant;
  139. UA_Variant_init(&myVariant);
  140. UA_Variant input;
  141. UA_Variant_init (&input);
  142. for (UA_UInt16 i = 0; i < 5; i++) {
  143. if (UA_Client_getState (client) == UA_CLIENTSTATE_SESSION) {
  144. /* writing and reading value 1 to 5 */
  145. UA_Variant_setScalarCopy (&myVariant, &value, &UA_TYPES[UA_TYPES_INT32]);
  146. value++;
  147. UA_Client_writeValueAttribute_async(client,
  148. UA_NODEID_STRING (1, "the.answer"),
  149. &myVariant, attrWritten, NULL,
  150. &reqId);
  151. UA_Variant_deleteMembers (&myVariant);
  152. UA_Client_readValueAttribute_async(client,
  153. UA_NODEID_STRING (1, "the.answer"),
  154. readValueAttributeCallback, NULL,
  155. &reqId);
  156. //TODO: check the existance of the nodes inside these functions (otherwise seg faults)
  157. #ifdef NODES_EXIST
  158. #ifdef UA_ENABLE_METHODCALLS
  159. UA_String stringValue = UA_String_fromChars ("World");
  160. UA_Variant_setScalar (&input, &stringValue, &UA_TYPES[UA_TYPES_STRING]);
  161. UA_Client_call_async(client,
  162. UA_NODEID_NUMERIC (0, UA_NS0ID_OBJECTSFOLDER),
  163. UA_NODEID_NUMERIC (1, 62541), 1, &input,
  164. methodCalled, NULL, &reqId);
  165. UA_String_deleteMembers(&stringValue);
  166. #define pathSize 3
  167. char *paths[pathSize] = { "Server", "ServerStatus", "State" };
  168. UA_UInt32 ids[pathSize] = { UA_NS0ID_ORGANIZES,
  169. UA_NS0ID_HASCOMPONENT, UA_NS0ID_HASCOMPONENT };
  170. UA_Cient_translateBrowsePathsToNodeIds_async (client, paths, ids,
  171. pathSize,
  172. translateCalled, NULL,
  173. &reqId);
  174. #endif /* UA_ENABLE_METHODCALLS */
  175. #endif
  176. /* How often UA_Client_run_iterate is called depends on the number of request sent */
  177. UA_Client_run_iterate(client, 0);
  178. UA_Client_run_iterate(client, 0);
  179. }
  180. }
  181. UA_Client_run_iterate (client, 0);
  182. /* Async disconnect kills unprocessed requests */
  183. // UA_Client_disconnect_async (client, &reqId); //can only be used when connected = true
  184. // UA_Client_run_iterate (client, &timedOut);
  185. UA_Client_disconnect(client);
  186. UA_Client_delete (client);
  187. return (int) UA_STATUSCODE_GOOD;
  188. }