check_client_subscriptions.c 4.9 KB

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