check_local_monitored_item.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "ua_types.h"
  8. #include "ua_server.h"
  9. #include "ua_client.h"
  10. #include "client/ua_client_internal.h"
  11. #include "ua_client_highlevel.h"
  12. #include "ua_config_default.h"
  13. #include "ua_network_tcp.h"
  14. #include "check.h"
  15. #include "testing_clock.h"
  16. #include "testing_networklayers.h"
  17. #include "thread_wrapper.h"
  18. #include <stddef.h>
  19. #ifdef UA_ENABLE_STATUSCODE_DESCRIPTIONS
  20. #define ASSERT_STATUSCODE(a,b) ck_assert_str_eq(UA_StatusCode_name(a),UA_StatusCode_name(b));
  21. #else
  22. #define ASSERT_STATUSCODE(a,b) ck_assert_uint_eq((a),(b));
  23. #endif
  24. UA_Server *server;
  25. UA_ServerConfig *config;
  26. UA_Boolean running;
  27. THREAD_HANDLE server_thread;
  28. UA_Client *client;
  29. UA_NodeId parentNodeId;
  30. UA_NodeId parentReferenceNodeId;
  31. UA_NodeId outNodeId;
  32. THREAD_CALLBACK(serverloop)
  33. {
  34. while(running)
  35. UA_Server_run_iterate(server, true);
  36. return 0;
  37. }
  38. static void
  39. setup(void)
  40. {
  41. running = true;
  42. config = UA_ServerConfig_new_default();
  43. server = UA_Server_new(config);
  44. UA_StatusCode retval = UA_Server_run_startup(server);
  45. ASSERT_STATUSCODE(retval, UA_STATUSCODE_GOOD);
  46. THREAD_CREATE(server_thread, serverloop);
  47. /* Define the attribute of the uint32 variable node */
  48. UA_VariableAttributes attr = UA_VariableAttributes_default;
  49. UA_UInt32 myUint32 = 40;
  50. UA_Variant_setScalar(&attr.value, &myUint32, &UA_TYPES[UA_TYPES_UINT32]);
  51. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  52. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  53. attr.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  54. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  55. /* Add the variable node to the information model */
  56. UA_NodeId uint32NodeId = UA_NODEID_STRING(1, "the.answer");
  57. UA_QualifiedName uint32Name = UA_QUALIFIEDNAME(1, "the answer");
  58. parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  59. parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  60. UA_NodeId_init(&outNodeId);
  61. ASSERT_STATUSCODE(UA_Server_addVariableNode(server,
  62. uint32NodeId,
  63. parentNodeId,
  64. parentReferenceNodeId,
  65. uint32Name,
  66. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  67. attr,
  68. NULL,
  69. &outNodeId)
  70. , UA_STATUSCODE_GOOD);
  71. client = UA_Client_new(UA_ClientConfig_default);
  72. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  73. ASSERT_STATUSCODE(retval, UA_STATUSCODE_GOOD);
  74. UA_Client_recv = client->connection.recv;
  75. client->connection.recv = UA_Client_recvTesting;
  76. }
  77. static void
  78. teardown(void)
  79. {
  80. /* cleanup */
  81. UA_Client_disconnect(client);
  82. UA_Client_delete(client);
  83. UA_NodeId_deleteMembers(&parentNodeId);
  84. UA_NodeId_deleteMembers(&parentReferenceNodeId);
  85. UA_NodeId_deleteMembers(&outNodeId);
  86. running = false;
  87. THREAD_JOIN(server_thread);
  88. UA_Server_run_shutdown(server);
  89. UA_Server_delete(server);
  90. UA_ServerConfig_delete(config);
  91. }
  92. static UA_StatusCode
  93. setUInt32(UA_Client *thisClient, UA_NodeId node, UA_UInt32 value)
  94. {
  95. UA_Variant variant;
  96. UA_Variant_setScalar(&variant, &value, &UA_TYPES[UA_TYPES_UINT32]);
  97. return UA_Client_writeValueAttribute(thisClient, node, &variant);
  98. }
  99. static void
  100. dataChangeNotificationCallback(UA_Server *thisServer,
  101. UA_UInt32 monitoredItemId,
  102. void *monitoredItemContext,
  103. const UA_NodeId *nodeId,
  104. void *nodeContext,
  105. UA_UInt32 attributeId,
  106. const UA_DataValue *value)
  107. {
  108. static size_t count = 0;
  109. static UA_UInt32 lastValue = 100;
  110. UA_UInt32 currentValue = *((UA_UInt32*)value->value.data);
  111. ck_assert_uint_ne(lastValue, currentValue);
  112. lastValue = currentValue;
  113. if (count++ == 10) {
  114. UA_Server_deleteMonitoredItem(server, monitoredItemId);
  115. running = false;
  116. }
  117. }
  118. START_TEST(Server_LocalMonitoredItem)
  119. {
  120. ASSERT_STATUSCODE(setUInt32(client, outNodeId, 0), UA_STATUSCODE_GOOD);
  121. UA_MonitoredItemCreateRequest monitorRequest =
  122. UA_MonitoredItemCreateRequest_default(outNodeId);
  123. monitorRequest.requestedParameters.samplingInterval = (double)100;
  124. monitorRequest.monitoringMode = UA_MONITORINGMODE_REPORTING;
  125. UA_MonitoredItemCreateResult result =
  126. UA_Server_createDataChangeMonitoredItem(server,
  127. UA_TIMESTAMPSTORETURN_BOTH,
  128. monitorRequest,
  129. NULL,
  130. &dataChangeNotificationCallback);
  131. ASSERT_STATUSCODE(result.statusCode, UA_STATUSCODE_GOOD);
  132. UA_UInt32 count = 0;
  133. while (running) {
  134. ASSERT_STATUSCODE(setUInt32(client, outNodeId, count++), UA_STATUSCODE_GOOD);
  135. UA_fakeSleep(100);
  136. UA_realSleep(100);
  137. }
  138. }
  139. END_TEST
  140. static Suite* testSuite_Client(void)
  141. {
  142. Suite *s = suite_create("Local Monitored Item");
  143. TCase *tc_server = tcase_create("Local Monitored Item Basic");
  144. tcase_add_checked_fixture(tc_server, setup, teardown);
  145. tcase_add_test(tc_server, Server_LocalMonitoredItem);
  146. suite_add_tcase(s, tc_server);
  147. return s;
  148. }
  149. int main(void)
  150. {
  151. Suite *s = testSuite_Client();
  152. SRunner *sr = srunner_create(s);
  153. srunner_set_fork_status(sr, CK_NOFORK);
  154. srunner_run_all(sr,CK_NORMAL);
  155. int number_failed = srunner_ntests_failed(sr);
  156. srunner_free(sr);
  157. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  158. }