check_local_monitored_item.c 5.1 KB

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