check_client_historical_data.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "server/ua_server_internal.h"
  10. #include "ua_client.h"
  11. #include "client/ua_client_internal.h"
  12. #include "ua_client_highlevel.h"
  13. #include "ua_config_default.h"
  14. #include "ua_network_tcp.h"
  15. #include "check.h"
  16. #include "testing_clock.h"
  17. #include "testing_networklayers.h"
  18. #include "thread_wrapper.h"
  19. #include "ua_plugin_historydatabase.h"
  20. #include "ua_historydatabase_default.h"
  21. #include "ua_plugin_history_data_gathering.h"
  22. #include "ua_historydatabackend_memory.h"
  23. #include "ua_historydatagathering_default.h"
  24. #ifdef UA_ENABLE_HISTORIZING
  25. #include "historical_read_test_data.h"
  26. #endif
  27. #include <stddef.h>
  28. static UA_Server *server;
  29. static UA_ServerConfig *config;
  30. #ifdef UA_ENABLE_HISTORIZING
  31. static UA_HistoryDataGathering *gathering;
  32. #endif
  33. static UA_Boolean running;
  34. static THREAD_HANDLE server_thread;
  35. static UA_Client *client;
  36. static UA_NodeId parentNodeId;
  37. static UA_NodeId parentReferenceNodeId;
  38. static UA_NodeId outNodeId;
  39. #ifdef UA_ENABLE_HISTORIZING
  40. static UA_HistoryDataBackend serverBackend;
  41. // same size as the test data
  42. static const size_t testDataSize = sizeof(testData) / sizeof(testData[0]);
  43. static UA_DateTime receivedTestData[sizeof(testData) / sizeof(testData[0])];
  44. static size_t receivedTestDataPos;
  45. #endif
  46. THREAD_CALLBACK(serverloop)
  47. {
  48. while(running)
  49. UA_Server_run_iterate(server, true);
  50. return 0;
  51. }
  52. #ifdef UA_ENABLE_HISTORIZING
  53. static UA_Boolean
  54. fillHistoricalDataBackend(UA_HistoryDataBackend backend);
  55. #endif
  56. static void
  57. setup(void)
  58. {
  59. running = true;
  60. config = UA_ServerConfig_new_default();
  61. #ifdef UA_ENABLE_HISTORIZING
  62. receivedTestDataPos = 0;
  63. memset(receivedTestData, 0, sizeof(receivedTestData));
  64. gathering = (UA_HistoryDataGathering*)UA_calloc(1, sizeof(UA_HistoryDataGathering));
  65. *gathering = UA_HistoryDataGathering_Default(1);
  66. config->historyDatabase = UA_HistoryDatabase_default(*gathering);
  67. #endif
  68. server = UA_Server_new(config);
  69. UA_StatusCode retval = UA_Server_run_startup(server);
  70. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  71. THREAD_CREATE(server_thread, serverloop);
  72. /* Define the attribute of the uint32 variable node */
  73. UA_VariableAttributes attr = UA_VariableAttributes_default;
  74. UA_UInt32 myUint32 = 40;
  75. UA_Variant_setScalar(&attr.value, &myUint32, &UA_TYPES[UA_TYPES_UINT32]);
  76. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  77. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  78. attr.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  79. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE | UA_ACCESSLEVELMASK_HISTORYREAD;
  80. attr.historizing = true;
  81. /* Add the variable node to the information model */
  82. UA_NodeId uint32NodeId = UA_NODEID_STRING(1, "the.answer");
  83. UA_QualifiedName uint32Name = UA_QUALIFIEDNAME(1, "the answer");
  84. parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  85. parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  86. UA_NodeId_init(&outNodeId);
  87. ck_assert_uint_eq(UA_Server_addVariableNode(server,
  88. uint32NodeId,
  89. parentNodeId,
  90. parentReferenceNodeId,
  91. uint32Name,
  92. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  93. attr,
  94. NULL,
  95. &outNodeId)
  96. , UA_STATUSCODE_GOOD);
  97. #ifdef UA_ENABLE_HISTORIZING
  98. UA_HistorizingNodeIdSettings setting;
  99. serverBackend = UA_HistoryDataBackend_Memory(1, 100);
  100. setting.historizingBackend = serverBackend;
  101. setting.maxHistoryDataResponseSize = 100;
  102. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  103. retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  104. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  105. ck_assert(fillHistoricalDataBackend(setting.historizingBackend));
  106. #endif
  107. client = UA_Client_new(UA_ClientConfig_default);
  108. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  109. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  110. UA_Client_recv = client->connection.recv;
  111. client->connection.recv = UA_Client_recvTesting;
  112. }
  113. static void
  114. teardown(void)
  115. {
  116. /* cleanup */
  117. #ifdef UA_ENABLE_HISTORIZING
  118. UA_HistoryDataBackend_Memory_deleteMembers(&serverBackend);
  119. #endif
  120. UA_Client_disconnect(client);
  121. UA_Client_delete(client);
  122. UA_NodeId_deleteMembers(&parentNodeId);
  123. UA_NodeId_deleteMembers(&parentReferenceNodeId);
  124. UA_NodeId_deleteMembers(&outNodeId);
  125. running = false;
  126. THREAD_JOIN(server_thread);
  127. UA_Server_run_shutdown(server);
  128. UA_Server_delete(server);
  129. UA_ServerConfig_delete(config);
  130. #ifdef UA_ENABLE_HISTORIZING
  131. UA_free(gathering);
  132. #endif
  133. }
  134. #ifdef UA_ENABLE_HISTORIZING
  135. #include <stdio.h>
  136. #include "ua_session.h"
  137. static UA_Boolean
  138. fillHistoricalDataBackend(UA_HistoryDataBackend backend)
  139. {
  140. fprintf(stderr, "Adding to historical data backend: ");
  141. for (size_t i = 0; i < testDataSize; ++i) {
  142. fprintf(stderr, "%lld, ", testData[i] / UA_DATETIME_SEC);
  143. UA_DataValue value;
  144. UA_DataValue_init(&value);
  145. value.hasValue = true;
  146. UA_Int64 d = testData[i];
  147. UA_Variant_setScalarCopy(&value.value, &d, &UA_TYPES[UA_TYPES_INT64]);
  148. value.hasSourceTimestamp = true;
  149. value.sourceTimestamp = testData[i];
  150. value.hasServerTimestamp = true;
  151. value.serverTimestamp = testData[i];
  152. value.hasStatus = true;
  153. value.status = UA_STATUSCODE_GOOD;
  154. if (backend.serverSetHistoryData(server, backend.context, NULL, NULL, &outNodeId, UA_FALSE, &value) != UA_STATUSCODE_GOOD) {
  155. fprintf(stderr, "\n");
  156. return false;
  157. }
  158. UA_DataValue_deleteMembers(&value);
  159. }
  160. fprintf(stderr, "\n");
  161. return true;
  162. }
  163. static UA_Boolean checkTestData(UA_Boolean inverse) {
  164. for (size_t i = 0; i < testDataSize; ++i) {
  165. if (testDataSize != receivedTestDataPos)
  166. return false;
  167. if (!inverse && testData[i] != receivedTestData[i])
  168. return false;
  169. if (inverse && testData[i] != receivedTestData[testDataSize-i-1])
  170. return false;
  171. }
  172. return true;
  173. }
  174. static UA_Boolean
  175. receiveCallback(UA_Client *clt,
  176. const UA_NodeId *nodeId,
  177. UA_Boolean moreDataAvailable,
  178. const UA_ExtensionObject *_data,
  179. void *callbackContext) {
  180. UA_HistoryData *data = (UA_HistoryData*)_data->content.decoded.data;
  181. fprintf(stderr, "Received %lu at pos %lu. moreDataAvailable %d\n", data->dataValuesSize, receivedTestDataPos, moreDataAvailable);
  182. if (receivedTestDataPos + data->dataValuesSize > testDataSize)
  183. return false;
  184. for (size_t i = 0; i < data->dataValuesSize; ++i) {
  185. receivedTestData[i+receivedTestDataPos] = *((UA_Int64*)data->dataValues[i].value.data);
  186. }
  187. receivedTestDataPos += data->dataValuesSize;
  188. return true;
  189. }
  190. START_TEST(Client_HistorizingReadRawAll)
  191. {
  192. UA_Client_HistoryRead_raw(client,
  193. &outNodeId,
  194. receiveCallback,
  195. TESTDATA_START_TIME,
  196. TESTDATA_STOP_TIME,
  197. UA_STRING_NULL,
  198. false,
  199. 100,
  200. UA_TIMESTAMPSTORETURN_BOTH,
  201. (void*)false);
  202. ck_assert(checkTestData(false));
  203. }
  204. END_TEST
  205. START_TEST(Client_HistorizingReadRawOne)
  206. {
  207. UA_Client_HistoryRead_raw(client,
  208. &outNodeId,
  209. receiveCallback,
  210. TESTDATA_START_TIME,
  211. TESTDATA_STOP_TIME,
  212. UA_STRING_NULL,
  213. false,
  214. 1,
  215. UA_TIMESTAMPSTORETURN_BOTH,
  216. (void*)false);
  217. ck_assert(checkTestData(false));
  218. }
  219. END_TEST
  220. START_TEST(Client_HistorizingReadRawTwo)
  221. {
  222. UA_Client_HistoryRead_raw(client,
  223. &outNodeId,
  224. receiveCallback,
  225. TESTDATA_START_TIME,
  226. TESTDATA_STOP_TIME,
  227. UA_STRING_NULL,
  228. false,
  229. 2,
  230. UA_TIMESTAMPSTORETURN_BOTH,
  231. (void*)false);
  232. ck_assert(checkTestData(false));
  233. }
  234. END_TEST
  235. START_TEST(Client_HistorizingReadRawAllInv)
  236. {
  237. UA_Client_HistoryRead_raw(client,
  238. &outNodeId,
  239. receiveCallback,
  240. TESTDATA_STOP_TIME,
  241. TESTDATA_START_TIME,
  242. UA_STRING_NULL,
  243. false,
  244. 100,
  245. UA_TIMESTAMPSTORETURN_BOTH,
  246. (void*)true);
  247. ck_assert(checkTestData(true));
  248. }
  249. END_TEST
  250. START_TEST(Client_HistorizingReadRawOneInv)
  251. {
  252. UA_Client_HistoryRead_raw(client,
  253. &outNodeId,
  254. receiveCallback,
  255. TESTDATA_STOP_TIME,
  256. TESTDATA_START_TIME,
  257. UA_STRING_NULL,
  258. false,
  259. 1,
  260. UA_TIMESTAMPSTORETURN_BOTH,
  261. (void*)true);
  262. ck_assert(checkTestData(true));
  263. }
  264. END_TEST
  265. START_TEST(Client_HistorizingReadRawTwoInv)
  266. {
  267. UA_Client_HistoryRead_raw(client,
  268. &outNodeId,
  269. receiveCallback,
  270. TESTDATA_STOP_TIME,
  271. TESTDATA_START_TIME,
  272. UA_STRING_NULL,
  273. false,
  274. 2,
  275. UA_TIMESTAMPSTORETURN_BOTH,
  276. (void*)true);
  277. ck_assert(checkTestData(true));
  278. }
  279. END_TEST
  280. #endif /*UA_ENABLE_HISTORIZING*/
  281. static Suite* testSuite_Client(void)
  282. {
  283. Suite *s = suite_create("Client Historical Data");
  284. TCase *tc_client = tcase_create("Client Historical Data read_raw");
  285. tcase_add_checked_fixture(tc_client, setup, teardown);
  286. #ifdef UA_ENABLE_HISTORIZING
  287. tcase_add_test(tc_client, Client_HistorizingReadRawAll);
  288. tcase_add_test(tc_client, Client_HistorizingReadRawOne);
  289. tcase_add_test(tc_client, Client_HistorizingReadRawTwo);
  290. tcase_add_test(tc_client, Client_HistorizingReadRawAllInv);
  291. tcase_add_test(tc_client, Client_HistorizingReadRawOneInv);
  292. tcase_add_test(tc_client, Client_HistorizingReadRawTwoInv);
  293. #endif /* UA_ENABLE_HISTORIZING */
  294. suite_add_tcase(s, tc_client);
  295. return s;
  296. }
  297. int main(void)
  298. {
  299. Suite *s = testSuite_Client();
  300. SRunner *sr = srunner_create(s);
  301. srunner_set_fork_status(sr, CK_NOFORK);
  302. srunner_run_all(sr,CK_NORMAL);
  303. int number_failed = srunner_ntests_failed(sr);
  304. srunner_free(sr);
  305. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  306. }