client_subscription_loop.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  4. * Client disconnect handling
  5. * --------------------------
  6. * This example shows you how to handle a client disconnect, e.g., if the server
  7. * is shut down while the client is connected. You just need to call connect
  8. * again and the client will automatically reconnect.
  9. *
  10. * This example is very similar to the tutorial_client_firststeps.c. */
  11. #include "open62541.h"
  12. #include <signal.h>
  13. UA_Boolean running = true;
  14. static void stopHandler(int sign) {
  15. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Received Ctrl-C");
  16. running = 0;
  17. }
  18. static void
  19. handler_currentTimeChanged(UA_Client *client, UA_UInt32 subId, void *subContext,
  20. UA_UInt32 monId, void *monContext, UA_DataValue *value) {
  21. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "currentTime has changed!");
  22. if(UA_Variant_hasScalarType(&value->value, &UA_TYPES[UA_TYPES_DATETIME])) {
  23. UA_DateTime raw_date = *(UA_DateTime *) value->value.data;
  24. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  25. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  26. "date is: %02u-%02u-%04u %02u:%02u:%02u.%03u",
  27. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  28. }
  29. }
  30. static void
  31. deleteSubscriptionCallback(UA_Client *client, UA_UInt32 subscriptionId, void *subscriptionContext) {
  32. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  33. "Subscription Id %u was deleted", subscriptionId);
  34. }
  35. static void
  36. subscriptionInactivityCallback (UA_Client *client, UA_UInt32 subId, void *subContext) {
  37. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Inactivity for subscription %u", subId);
  38. }
  39. static void
  40. stateCallback (UA_Client *client, UA_ClientState clientState) {
  41. switch(clientState) {
  42. case UA_CLIENTSTATE_DISCONNECTED:
  43. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "The client is disconnected");
  44. break;
  45. case UA_CLIENTSTATE_WAITING_FOR_ACK:
  46. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Waiting for ack");
  47. break;
  48. case UA_CLIENTSTATE_CONNECTED:
  49. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  50. "A TCP connection to the server is open");
  51. break;
  52. case UA_CLIENTSTATE_SECURECHANNEL:
  53. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  54. "A SecureChannel to the server is open");
  55. break;
  56. case UA_CLIENTSTATE_SESSION:{
  57. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "A session with the server is open");
  58. /* A new session was created. We need to create the subscription. */
  59. /* Create a subscription */
  60. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  61. UA_CreateSubscriptionResponse response = UA_Client_Subscriptions_create(client, request,
  62. NULL, NULL, deleteSubscriptionCallback);
  63. if(response.responseHeader.serviceResult == UA_STATUSCODE_GOOD)
  64. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  65. "Create subscription succeeded, id %u", response.subscriptionId);
  66. else
  67. return;
  68. /* Add a MonitoredItem */
  69. UA_MonitoredItemCreateRequest monRequest =
  70. UA_MonitoredItemCreateRequest_default(UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME));
  71. UA_MonitoredItemCreateResult monResponse =
  72. UA_Client_MonitoredItems_createDataChange(client, response.subscriptionId,
  73. UA_TIMESTAMPSTORETURN_BOTH,
  74. monRequest, NULL, handler_currentTimeChanged, NULL);
  75. if(monResponse.statusCode == UA_STATUSCODE_GOOD)
  76. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  77. "Monitoring UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME', id %u",
  78. monResponse.monitoredItemId);
  79. }
  80. break;
  81. case UA_CLIENTSTATE_SESSION_RENEWED:
  82. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  83. "A session with the server is open (renewed)");
  84. /* The session was renewed. We don't need to recreate the subscription. */
  85. break;
  86. case UA_CLIENTSTATE_SESSION_DISCONNECTED:
  87. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Session disconnected");
  88. break;
  89. }
  90. return;
  91. }
  92. int
  93. main(void) {
  94. signal(SIGINT, stopHandler); /* catches ctrl-c */
  95. UA_ClientConfig config = UA_ClientConfig_default;
  96. /* Set stateCallback */
  97. config.stateCallback = stateCallback;
  98. config.subscriptionInactivityCallback = subscriptionInactivityCallback;
  99. UA_Client *client = UA_Client_new(config);
  100. /* Endless loop runAsync */
  101. while(running) {
  102. /* if already connected, this will return GOOD and do nothing */
  103. /* if the connection is closed/errored, the connection will be reset and then reconnected */
  104. /* Alternatively you can also use UA_Client_getState to get the current state */
  105. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  106. if(retval != UA_STATUSCODE_GOOD) {
  107. UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  108. "Not connected. Retrying to connect in 1 second");
  109. /* The connect may timeout after 1 second (see above) or it may fail immediately on network errors */
  110. /* E.g. name resolution errors or unreachable network. Thus there should be a small sleep here */
  111. UA_sleep_ms(1000);
  112. continue;
  113. }
  114. UA_Client_run_iterate(client, 1000);
  115. };
  116. /* Clean up */
  117. UA_Client_delete(client); /* Disconnects the client internally */
  118. return UA_STATUSCODE_GOOD;
  119. }