check_local_monitored_item.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. *
  5. * Copyright 2018 (c) basysKom GmbH <opensource@basyskom.com> (Author: Peter Rustler)
  6. */
  7. #include <open62541/client_subscriptions.h>
  8. #include <open62541/server.h>
  9. #include <open62541/server_config_default.h>
  10. #include <open62541/types.h>
  11. #include "ua_network_tcp.h"
  12. #include <stddef.h>
  13. #include "check.h"
  14. #include "testing_clock.h"
  15. #include "testing_networklayers.h"
  16. #ifdef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  17. #define ASSERT_STATUSCODE(a,b) ck_assert_str_eq(UA_StatusCode_name(a),UA_StatusCode_name(b));
  18. #else
  19. #define ASSERT_STATUSCODE(a,b) ck_assert_uint_eq((a),(b));
  20. #endif
  21. UA_Server *server;
  22. UA_ServerConfig *config;
  23. size_t callbackCount = 0;
  24. UA_NodeId parentNodeId;
  25. UA_NodeId parentReferenceNodeId;
  26. UA_NodeId outNodeId;
  27. static void
  28. setup(void)
  29. {
  30. config = UA_ServerConfig_new_default();
  31. server = UA_Server_new(config);
  32. UA_StatusCode retval = UA_Server_run_startup(server);
  33. ASSERT_STATUSCODE(retval, UA_STATUSCODE_GOOD);
  34. /* Define the attribute of the uint32 variable node */
  35. UA_VariableAttributes attr = UA_VariableAttributes_default;
  36. UA_UInt32 myUint32 = 40;
  37. UA_Variant_setScalar(&attr.value, &myUint32, &UA_TYPES[UA_TYPES_UINT32]);
  38. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  39. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  40. attr.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  41. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  42. /* Add the variable node to the information model */
  43. UA_NodeId uint32NodeId = UA_NODEID_STRING(1, "the.answer");
  44. UA_QualifiedName uint32Name = UA_QUALIFIEDNAME(1, "the answer");
  45. parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  46. parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  47. UA_NodeId_init(&outNodeId);
  48. ASSERT_STATUSCODE(UA_Server_addVariableNode(server,
  49. uint32NodeId,
  50. parentNodeId,
  51. parentReferenceNodeId,
  52. uint32Name,
  53. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  54. attr,
  55. NULL,
  56. &outNodeId), UA_STATUSCODE_GOOD);
  57. }
  58. static void
  59. teardown(void)
  60. {
  61. /* cleanup */
  62. UA_NodeId_deleteMembers(&parentNodeId);
  63. UA_NodeId_deleteMembers(&parentReferenceNodeId);
  64. UA_NodeId_deleteMembers(&outNodeId);
  65. UA_Server_run_shutdown(server);
  66. UA_Server_delete(server);
  67. UA_ServerConfig_delete(config);
  68. }
  69. static void
  70. dataChangeNotificationCallback(UA_Server *thisServer,
  71. UA_UInt32 monitoredItemId,
  72. void *monitoredItemContext,
  73. const UA_NodeId *nodeId,
  74. void *nodeContext,
  75. UA_UInt32 attributeId,
  76. const UA_DataValue *value)
  77. {
  78. static UA_UInt32 lastValue = 100;
  79. UA_UInt32 currentValue = *((UA_UInt32*)value->value.data);
  80. ck_assert_uint_ne(lastValue, currentValue);
  81. lastValue = currentValue;
  82. callbackCount++;
  83. }
  84. START_TEST(Server_LocalMonitoredItem) {
  85. ck_assert_uint_eq(callbackCount, 0);
  86. UA_MonitoredItemCreateRequest monitorRequest =
  87. UA_MonitoredItemCreateRequest_default(outNodeId);
  88. monitorRequest.requestedParameters.samplingInterval = (double)100;
  89. monitorRequest.monitoringMode = UA_MONITORINGMODE_REPORTING;
  90. UA_MonitoredItemCreateResult result =
  91. UA_Server_createDataChangeMonitoredItem(server,
  92. UA_TIMESTAMPSTORETURN_BOTH,
  93. monitorRequest,
  94. NULL,
  95. &dataChangeNotificationCallback);
  96. ASSERT_STATUSCODE(result.statusCode, UA_STATUSCODE_GOOD);
  97. ck_assert_uint_eq(callbackCount, 1);
  98. UA_UInt32 count = 0;
  99. UA_Variant val;
  100. UA_Variant_setScalar(&val, &count, &UA_TYPES[UA_TYPES_UINT32]);
  101. for(size_t i = 0; i < 10; i++) {
  102. count++;
  103. UA_Server_writeValue(server, outNodeId, val);
  104. UA_fakeSleep(100);
  105. UA_Server_run_iterate(server, 1);
  106. }
  107. ck_assert_uint_eq(callbackCount, 11);
  108. }
  109. END_TEST
  110. static Suite* testSuite_Client(void)
  111. {
  112. Suite *s = suite_create("Local Monitored Item");
  113. TCase *tc_server = tcase_create("Local Monitored Item Basic");
  114. tcase_add_checked_fixture(tc_server, setup, teardown);
  115. tcase_add_test(tc_server, Server_LocalMonitoredItem);
  116. suite_add_tcase(s, tc_server);
  117. return s;
  118. }
  119. int main(void)
  120. {
  121. Suite *s = testSuite_Client();
  122. SRunner *sr = srunner_create(s);
  123. srunner_set_fork_status(sr, CK_NOFORK);
  124. srunner_run_all(sr,CK_NORMAL);
  125. int number_failed = srunner_ntests_failed(sr);
  126. srunner_free(sr);
  127. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  128. }