client_subscription_loop.c 5.2 KB

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