check_client_subscriptions.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_Boolean *running;
  17. UA_ServerNetworkLayer nl;
  18. pthread_t server_thread;
  19. static void * serverloop(void *_) {
  20. while(*running)
  21. UA_Server_run_iterate(server, true);
  22. return NULL;
  23. }
  24. static void setup(void) {
  25. running = UA_Boolean_new();
  26. *running = true;
  27. UA_ServerConfig config = UA_ServerConfig_standard;
  28. nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  29. config.networkLayers = &nl;
  30. config.networkLayersSize = 1;
  31. server = UA_Server_new(config);
  32. UA_Server_run_startup(server);
  33. pthread_create(&server_thread, NULL, serverloop, NULL);
  34. }
  35. static void teardown(void) {
  36. *running = false;
  37. pthread_join(server_thread, NULL);
  38. UA_Server_run_shutdown(server);
  39. UA_Boolean_delete(running);
  40. UA_Server_delete(server);
  41. nl.deleteMembers(&nl);
  42. }
  43. UA_Boolean notificationReceived;
  44. static void monitoredItemHandler(UA_UInt32 monId, UA_DataValue *value, void *context) {
  45. notificationReceived = true;
  46. }
  47. START_TEST(Client_subscription) {
  48. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  49. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  50. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  51. UA_UInt32 subId;
  52. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_standard, &subId);
  53. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  54. /* monitor the server state */
  55. UA_UInt32 monId;
  56. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  57. UA_ATTRIBUTEID_VALUE, monitoredItemHandler, NULL, &monId);
  58. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  59. UA_sleep((UA_UInt32)UA_SubscriptionSettings_standard.requestedPublishingInterval + 1);
  60. notificationReceived = false;
  61. retval = UA_Client_Subscriptions_manuallySendPublishRequest(client);
  62. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  63. ck_assert_uint_eq(notificationReceived, true);
  64. UA_Client_disconnect(client);
  65. UA_Client_delete(client);
  66. }
  67. END_TEST
  68. START_TEST(Client_methodcall) {
  69. UA_Client *client = UA_Client_new(UA_ClientConfig_standard);
  70. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:16664");
  71. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  72. UA_UInt32 subId;
  73. retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_standard, &subId);
  74. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  75. /* monitor the server state */
  76. UA_UInt32 monId;
  77. retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259),
  78. UA_ATTRIBUTEID_VALUE, NULL, NULL, &monId);
  79. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  80. /* call a method to get monitored item id */
  81. UA_Variant input;
  82. UA_Variant_init(&input);
  83. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  84. size_t outputSize;
  85. UA_Variant *output;
  86. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  87. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  88. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  89. ck_assert_uint_eq(outputSize, 2);
  90. ck_assert_uint_eq(output[0].arrayLength, 1);
  91. ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId);
  92. UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]);
  93. UA_Variant_deleteMembers(&input);
  94. /* call with invalid subscription id */
  95. UA_Variant_init(&input);
  96. subId = 0;
  97. UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]);
  98. retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  99. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output);
  100. ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  101. UA_Variant_deleteMembers(&input);
  102. UA_Client_disconnect(client);
  103. UA_Client_delete(client);
  104. }
  105. END_TEST
  106. static Suite* testSuite_Client(void) {
  107. Suite *s = suite_create("Client Subscription");
  108. TCase *tc_client = tcase_create("Client Subscription Basic");
  109. tcase_add_checked_fixture(tc_client, setup, teardown);
  110. tcase_add_test(tc_client, Client_subscription);
  111. suite_add_tcase(s,tc_client);
  112. TCase *tc_client2 = tcase_create("Client Subscription + Method Call of GetMonitoredItmes");
  113. tcase_add_checked_fixture(tc_client2, setup, teardown);
  114. tcase_add_test(tc_client2, Client_methodcall);
  115. suite_add_tcase(s,tc_client2);
  116. return s;
  117. }
  118. int main(void) {
  119. Suite *s = testSuite_Client();
  120. SRunner *sr = srunner_create(s);
  121. srunner_set_fork_status(sr, CK_NOFORK);
  122. srunner_run_all(sr,CK_NORMAL);
  123. int number_failed = srunner_ntests_failed(sr);
  124. srunner_free(sr);
  125. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  126. }