check_client_subscriptions.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include "ua_types.h"
  8. #include "ua_server.h"
  9. #include "ua_client.h"
  10. #include "ua_client_highlevel.h"
  11. #include "ua_config_standard.h"
  12. #include "ua_network_tcp.h"
  13. #include "check.h"
  14. #include "testing_clock.h"
  15. UA_Server *server;
  16. UA_ServerConfig *config;
  17. UA_Boolean *running;
  18. UA_ServerNetworkLayer nl;
  19. pthread_t server_thread;
  20. static void * serverloop(void *_) {
  21. while(*running)
  22. UA_Server_run_iterate(server, true);
  23. return NULL;
  24. }
  25. static void setup(void) {
  26. running = UA_Boolean_new();
  27. *running = true;
  28. config = UA_ServerConfig_new_default();
  29. server = UA_Server_new(config);
  30. UA_Server_run_startup(server);
  31. pthread_create(&server_thread, NULL, serverloop, NULL);
  32. }
  33. static void teardown(void) {
  34. *running = false;
  35. pthread_join(server_thread, NULL);
  36. UA_Server_run_shutdown(server);
  37. UA_Boolean_delete(running);
  38. UA_Server_delete(server);
  39. UA_ServerConfig_delete(config);
  40. }
  41. UA_Boolean notificationReceived;
  42. static void monitoredItemHandler(UA_UInt32 monId, UA_DataValue *value, void *context) {
  43. notificationReceived = true;
  44. }
  45. START_TEST(Client_subscription) {
  46. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  47. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  48. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  49. UA_UInt32 subId;
  50. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  51. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  52. /* monitor the server state */
  53. UA_UInt32 monId;
  54. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  55. UA_ATTRIBUTEID_VALUE, monitoredItemHandler,
  56. NULL, &monId);
  57. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  58. UA_sleep((UA_UInt32)UA_SubscriptionSettings_default.requestedPublishingInterval + 1);
  59. notificationReceived = false;
  60. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  61. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  62. ck_assert_uint_eq(notificationReceived, true);
  63. UA_Client_disconnect(client);
  64. UA_Client_delete(client);
  65. }
  66. END_TEST
  67. START_TEST(Client_methodcall) {
  68. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  69. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  70. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  71. UA_UInt32 subId;
  72. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId);
  73. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  74. /* monitor the server state */
  75. UA_UInt32 monId;
  76. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  77. UA_ATTRIBUTEID_VALUE, NULL, NULL, &monId);
  78. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  79. /* call a method to get monitored item id */
  80. UA_Variant input;
  81. UA_Variant_init(&input);
  82. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  83. size_t outputSize;
  84. UA_Variant *output;
  85. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  86. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  87. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  88. ck_assert_uint_eq(outputSize, 2);
  89. ck_assert_uint_eq(output[0].arrayLength, 1);
  90. ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId);
  91. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  92. UA_Variant_deleteMembers(&input);
  93. /* call with invalid subscription id */
  94. UA_Variant_init(&input);
  95. subId = 0;
  96. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  97. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  98. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  99. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  100. UA_Variant_deleteMembers(&input);
  101. UA_Client_disconnect(client);
  102. UA_Client_delete(client);
  103. }
  104. END_TEST
  105. static Suite* testSuite_Client(void) {
  106. Suite *s = suite_create("Client Subscription");
  107. TCase *tc_client = tcase_create("Client Subscription Basic");
  108. tcase_add_checked_fixture(tc_client, setup, teardown);
  109. tcase_add_test(tc_client, Client_subscription);
  110. suite_add_tcase(s,tc_client);
  111. TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");
  112. tcase_add_checked_fixture(tc_client2, setup, teardown);
  113. tcase_add_test(tc_client2, Client_methodcall);
  114. suite_add_tcase(s,tc_client2);
  115. return s;
  116. }
  117. int main(void) {
  118. Suite *s = testSuite_Client();
  119. SRunner *sr = srunner_create(s);
  120. srunner_set_fork_status(sr, CK_NOFORK);
  121. srunner_run_all(sr,CK_NORMAL);
  122. int number_failed = srunner_ntests_failed(sr);
  123. srunner_free(sr);
  124. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  125. }