check_pubsub_publish.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 (c) 2017 - 2018 Fraunhofer IOSB (Author: Andreas Ebner)
  6. */
  7. #include <open62541/plugin/pubsub_udp.h>
  8. #include <open62541/server_config_default.h>
  9. #include <open62541/server_pubsub.h>
  10. #include "open62541/types_generated_encoding_binary.h"
  11. #include "ua_pubsub.h"
  12. #include "ua_server_internal.h"
  13. #include <check.h>
  14. UA_Server *server = NULL;
  15. UA_NodeId connection1, connection2, writerGroup1, writerGroup2, writerGroup3,
  16. publishedDataSet1, publishedDataSet2, dataSetWriter1, dataSetWriter2, dataSetWriter3;
  17. static void setup(void) {
  18. server = UA_Server_new();
  19. UA_ServerConfig *config = UA_Server_getConfig(server);
  20. UA_ServerConfig_setDefault(config);
  21. config->pubsubTransportLayers = (UA_PubSubTransportLayer*)
  22. UA_malloc(sizeof(UA_PubSubTransportLayer));
  23. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  24. config->pubsubTransportLayersSize++;
  25. UA_Server_run_startup(server);
  26. //add 2 connections
  27. UA_PubSubConnectionConfig connectionConfig;
  28. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  29. connectionConfig.name = UA_STRING("UADP Connection");
  30. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4840/")};
  31. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  32. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  33. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  34. UA_Server_addPubSubConnection(server, &connectionConfig, &connection1);
  35. UA_Server_addPubSubConnection(server, &connectionConfig, &connection2);
  36. }
  37. static void teardown(void) {
  38. UA_Server_run_shutdown(server);
  39. UA_Server_delete(server);
  40. }
  41. START_TEST(AddWriterGroupWithValidConfiguration){
  42. UA_StatusCode retVal;
  43. UA_WriterGroupConfig writerGroupConfig;
  44. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  45. writerGroupConfig.name = UA_STRING("WriterGroup 1");
  46. writerGroupConfig.publishingInterval = 10;
  47. UA_NodeId localWriterGroup;
  48. retVal = UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &localWriterGroup);
  49. UA_Server_setWriterGroupOperational(server, localWriterGroup);
  50. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  51. size_t writerGroupCount = 0;
  52. UA_WriterGroup *writerGroup;
  53. LIST_FOREACH(writerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection1)->writerGroups, listEntry){
  54. writerGroupCount++;
  55. }
  56. ck_assert_int_eq(writerGroupCount, 1);
  57. UA_Server_setWriterGroupDisabled(server, localWriterGroup);
  58. } END_TEST
  59. START_TEST(AddRemoveAddWriterGroupWithMinimalValidConfiguration){
  60. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  61. UA_WriterGroupConfig writerGroupConfig;
  62. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  63. writerGroupConfig.name = UA_STRING("WriterGroup 1");
  64. writerGroupConfig.publishingInterval = 10;
  65. UA_NodeId localWriterGroup;
  66. retVal |= UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &localWriterGroup);
  67. UA_Server_setWriterGroupOperational(server, localWriterGroup);
  68. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  69. retVal |= UA_Server_removeWriterGroup(server, localWriterGroup);
  70. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  71. size_t writerGroupCount = 0;
  72. UA_WriterGroup *writerGroup;
  73. LIST_FOREACH(writerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection1)->writerGroups, listEntry){
  74. writerGroupCount++;
  75. }
  76. ck_assert_int_eq(writerGroupCount, 0);
  77. retVal |= UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &localWriterGroup);
  78. UA_Server_setWriterGroupOperational(server, localWriterGroup);
  79. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  80. writerGroupCount = 0;
  81. LIST_FOREACH(writerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection1)->writerGroups, listEntry){
  82. writerGroupCount++;
  83. }
  84. ck_assert_int_eq(writerGroupCount, 1);
  85. retVal |= UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &localWriterGroup);
  86. UA_Server_setWriterGroupOperational(server, localWriterGroup);
  87. writerGroupCount = 0;
  88. LIST_FOREACH(writerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection1)->writerGroups, listEntry){
  89. writerGroupCount++;
  90. }
  91. ck_assert_int_eq(writerGroupCount, 2);
  92. } END_TEST
  93. START_TEST(AddWriterGroupWithNullConfig){
  94. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  95. retVal |= UA_Server_addWriterGroup(server, connection1, NULL, NULL);
  96. size_t writerGroupCount = 0;
  97. UA_WriterGroup *writerGroup;
  98. LIST_FOREACH(writerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection1)->writerGroups, listEntry){
  99. writerGroupCount++;
  100. }
  101. ck_assert_int_eq(writerGroupCount, 0);
  102. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  103. } END_TEST
  104. START_TEST(AddWriterGroupWithInvalidConnectionId){
  105. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  106. UA_WriterGroupConfig writerGroupConfig;
  107. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  108. writerGroupConfig.name = UA_STRING("WriterGroup 1");
  109. writerGroupConfig.publishingInterval = 10;
  110. retVal |= UA_Server_addWriterGroup(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), &writerGroupConfig, NULL);
  111. size_t writerGroupCount = 0;
  112. UA_WriterGroup *writerGroup;
  113. LIST_FOREACH(writerGroup, &UA_PubSubConnection_findConnectionbyId(server, connection1)->writerGroups, listEntry){
  114. writerGroupCount++;
  115. }
  116. ck_assert_int_eq(writerGroupCount, 0);
  117. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  118. } END_TEST
  119. START_TEST(GetWriterGroupConfigurationAndCompareValues){
  120. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  121. UA_WriterGroupConfig writerGroupConfig;
  122. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  123. writerGroupConfig.name = UA_STRING("WriterGroup 1");
  124. writerGroupConfig.publishingInterval = 10;
  125. UA_NodeId localWriterGroup;
  126. retVal |= UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &localWriterGroup);
  127. UA_Server_setWriterGroupOperational(server, localWriterGroup);
  128. UA_WriterGroupConfig writerGroupConfigCopy;
  129. retVal |= UA_Server_getWriterGroupConfig(server, localWriterGroup, &writerGroupConfigCopy);
  130. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  131. ck_assert_int_eq(UA_String_equal(&writerGroupConfig.name, &writerGroupConfigCopy.name), UA_TRUE);
  132. //todo remove == for floating point compare
  133. ck_assert(writerGroupConfig.publishingInterval == writerGroupConfig.publishingInterval);
  134. UA_WriterGroupConfig_clear(&writerGroupConfigCopy);
  135. } END_TEST
  136. static void setupDataSetWriterTestEnvironment(void){
  137. UA_WriterGroupConfig writerGroupConfig;
  138. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  139. writerGroupConfig.name = UA_STRING("WriterGroup 1");
  140. writerGroupConfig.publishingInterval = 10;
  141. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  142. UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &writerGroup1);
  143. UA_Server_setWriterGroupOperational(server, writerGroup1);
  144. writerGroupConfig.name = UA_STRING("WriterGroup 2");
  145. writerGroupConfig.publishingInterval = 50;
  146. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  147. UA_Server_addWriterGroup(server, connection2, &writerGroupConfig, &writerGroup2);
  148. UA_Server_setWriterGroupOperational(server, writerGroup2);
  149. writerGroupConfig.name = UA_STRING("WriterGroup 3");
  150. writerGroupConfig.publishingInterval = 100;
  151. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  152. UA_Server_addWriterGroup(server, connection2, &writerGroupConfig, &writerGroup3);
  153. UA_Server_setWriterGroupOperational(server, writerGroup3);
  154. UA_PublishedDataSetConfig pdsConfig;
  155. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  156. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  157. pdsConfig.name = UA_STRING("PublishedDataSet 1");
  158. UA_Server_addPublishedDataSet(server, &pdsConfig, &publishedDataSet1);
  159. UA_Server_addPublishedDataSet(server, &pdsConfig, &publishedDataSet2);
  160. }
  161. START_TEST(AddDataSetWriterWithValidConfiguration){
  162. setupDataSetWriterTestEnvironment();
  163. UA_StatusCode retVal;
  164. UA_DataSetWriterConfig dataSetWriterConfig;
  165. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  166. dataSetWriterConfig.name = UA_STRING("DataSetWriter 1 ");
  167. UA_NodeId localDataSetWriter;
  168. retVal = UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &localDataSetWriter);
  169. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  170. UA_DataSetWriter *dsw1 = UA_DataSetWriter_findDSWbyId(server, localDataSetWriter);
  171. ck_assert_ptr_ne(dsw1, NULL);
  172. UA_WriterGroup *wg1 = UA_WriterGroup_findWGbyId(server, writerGroup1);
  173. ck_assert_ptr_ne(wg1, NULL);
  174. ck_assert_int_eq(wg1->writersCount, 1);
  175. } END_TEST
  176. START_TEST(AddRemoveAddDataSetWriterWithValidConfiguration){
  177. setupDataSetWriterTestEnvironment();
  178. UA_StatusCode retVal;
  179. UA_WriterGroup *wg1 = UA_WriterGroup_findWGbyId(server, writerGroup1);
  180. ck_assert_ptr_ne(wg1, NULL);
  181. UA_DataSetWriterConfig dataSetWriterConfig;
  182. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  183. dataSetWriterConfig.name = UA_STRING("DataSetWriter 1 ");
  184. UA_NodeId dataSetWriter;
  185. ck_assert_int_eq(wg1->writersCount, 0);
  186. retVal = UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter);
  187. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  188. ck_assert_int_eq(wg1->writersCount, 1);
  189. retVal = UA_Server_removeDataSetWriter(server, dataSetWriter);
  190. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  191. ck_assert_int_eq(wg1->writersCount, 0);
  192. retVal = UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter);
  193. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  194. ck_assert_int_eq(wg1->writersCount, 1);
  195. retVal = UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter);
  196. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  197. ck_assert_int_eq(wg1->writersCount, 2);
  198. UA_WriterGroup *wg2 = UA_WriterGroup_findWGbyId(server, writerGroup2);
  199. ck_assert_ptr_ne(wg2, NULL);
  200. retVal = UA_Server_addDataSetWriter(server, writerGroup2, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter);
  201. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  202. ck_assert_int_eq(wg2->writersCount, 1);
  203. } END_TEST
  204. START_TEST(AddDataSetWriterWithNullConfig){
  205. setupDataSetWriterTestEnvironment();
  206. UA_StatusCode retVal;
  207. retVal = UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, NULL, NULL);
  208. UA_WriterGroup *wg1 = UA_WriterGroup_findWGbyId(server, writerGroup1);
  209. ck_assert_ptr_ne(wg1, NULL);
  210. ck_assert_int_eq(wg1->writersCount, 0);
  211. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  212. } END_TEST
  213. START_TEST(AddDataSetWriterWithInvalidPDSId){
  214. setupDataSetWriterTestEnvironment();
  215. UA_StatusCode retVal;
  216. UA_DataSetWriterConfig dataSetWriterConfig;
  217. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  218. dataSetWriterConfig.name = UA_STRING("DataSetWriter 1 ");
  219. retVal = UA_Server_addDataSetWriter(server, writerGroup1, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), &dataSetWriterConfig, NULL);
  220. UA_WriterGroup *wg1 = UA_WriterGroup_findWGbyId(server, writerGroup1);
  221. ck_assert_ptr_ne(wg1, NULL);
  222. ck_assert_int_eq(wg1->writersCount, 0);
  223. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  224. } END_TEST
  225. START_TEST(GetDataSetWriterConfigurationAndCompareValues){
  226. setupDataSetWriterTestEnvironment();
  227. UA_StatusCode retVal;
  228. UA_DataSetWriterConfig dataSetWriterConfig;
  229. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  230. dataSetWriterConfig.name = UA_STRING("DataSetWriter 1 ");
  231. UA_NodeId localDataSetWriter;
  232. retVal = UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &localDataSetWriter);
  233. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  234. UA_DataSetWriterConfig dataSetWiterConfigCopy;
  235. retVal |= UA_Server_getDataSetWriterConfig(server, localDataSetWriter, &dataSetWiterConfigCopy);
  236. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  237. ck_assert_int_eq(UA_String_equal(&dataSetWiterConfigCopy.name, &dataSetWiterConfigCopy.name), UA_TRUE);
  238. UA_DataSetWriterConfig_clear(&dataSetWiterConfigCopy);
  239. } END_TEST
  240. static void setupDataSetFieldTestEnvironment(void){
  241. setupDataSetWriterTestEnvironment();
  242. UA_DataSetWriterConfig dataSetWriterConfig;
  243. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  244. dataSetWriterConfig.name = UA_STRING("DataSetWriter 1");
  245. UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter1);
  246. dataSetWriterConfig.name = UA_STRING("DataSetWriter 2");
  247. UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter2);
  248. dataSetWriterConfig.name = UA_STRING("DataSetWriter 3");
  249. UA_Server_addDataSetWriter(server, writerGroup2, publishedDataSet2, &dataSetWriterConfig, &dataSetWriter3);
  250. }
  251. START_TEST(AddDataSetFieldWithValidConfiguration){
  252. setupDataSetFieldTestEnvironment();
  253. UA_StatusCode retVal;
  254. UA_DataSetFieldConfig fieldConfig;
  255. memset(&fieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  256. fieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  257. fieldConfig.field.variable.fieldNameAlias = UA_STRING("field 1");
  258. UA_NodeId localDataSetField;
  259. UA_PublishedDataSet *pds = UA_PublishedDataSet_findPDSbyId(server, publishedDataSet1);
  260. ck_assert_ptr_ne(pds, NULL);
  261. ck_assert_int_eq(pds->fieldSize, 0);
  262. retVal = UA_Server_addDataSetField(server, publishedDataSet1, &fieldConfig, &localDataSetField).result;
  263. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  264. ck_assert_int_eq(pds->fieldSize, 1);
  265. } END_TEST
  266. START_TEST(AddRemoveAddDataSetFieldWithValidConfiguration){
  267. setupDataSetFieldTestEnvironment();
  268. UA_StatusCode retVal;
  269. UA_DataSetFieldConfig fieldConfig;
  270. memset(&fieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  271. fieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  272. fieldConfig.field.variable.fieldNameAlias = UA_STRING("field 1");
  273. UA_NodeId localDataSetField;
  274. UA_PublishedDataSet *pds1 = UA_PublishedDataSet_findPDSbyId(server, publishedDataSet1);
  275. ck_assert_ptr_ne(pds1, NULL);
  276. ck_assert_int_eq(pds1->fieldSize, 0);
  277. retVal = UA_Server_addDataSetField(server, publishedDataSet1, &fieldConfig, &localDataSetField).result;
  278. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  279. ck_assert_int_eq(pds1->fieldSize, 1);
  280. retVal = UA_Server_removeDataSetField(server, localDataSetField).result;
  281. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  282. ck_assert_int_eq(pds1->fieldSize, 0);
  283. retVal = UA_Server_addDataSetField(server, publishedDataSet1, &fieldConfig, &localDataSetField).result;
  284. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  285. ck_assert_int_eq(pds1->fieldSize, 1);
  286. retVal = UA_Server_addDataSetField(server, publishedDataSet1, &fieldConfig, &localDataSetField).result;
  287. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  288. ck_assert_int_eq(pds1->fieldSize, 2);
  289. UA_PublishedDataSet *pds2 = UA_PublishedDataSet_findPDSbyId(server, publishedDataSet2);
  290. ck_assert_ptr_ne(pds2, NULL);
  291. retVal = UA_Server_addDataSetField(server, publishedDataSet2, &fieldConfig, &localDataSetField).result;
  292. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  293. ck_assert_int_eq(pds2->fieldSize, 1);
  294. } END_TEST
  295. START_TEST(AddDataSetFieldWithNullConfig){
  296. setupDataSetFieldTestEnvironment();
  297. UA_StatusCode retVal;
  298. retVal = UA_Server_addDataSetField(server, publishedDataSet1, NULL, NULL).result;
  299. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  300. UA_PublishedDataSet *pds1 = UA_PublishedDataSet_findPDSbyId(server, publishedDataSet1);
  301. ck_assert_ptr_ne(pds1, NULL);
  302. ck_assert_int_eq(pds1->fieldSize, 0);
  303. } END_TEST
  304. START_TEST(AddDataSetFieldWithInvalidPDSId){
  305. setupDataSetFieldTestEnvironment();
  306. UA_StatusCode retVal;
  307. UA_DataSetFieldConfig fieldConfig;
  308. memset(&fieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  309. fieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  310. fieldConfig.field.variable.fieldNameAlias = UA_STRING("field 1");
  311. retVal = UA_Server_addDataSetField(server, UA_NODEID_NUMERIC(0, UA_UINT32_MAX), &fieldConfig, NULL).result;
  312. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  313. UA_PublishedDataSet *pds1 = UA_PublishedDataSet_findPDSbyId(server, publishedDataSet1);
  314. ck_assert_ptr_ne(pds1, NULL);
  315. ck_assert_int_eq(pds1->fieldSize, 0);
  316. } END_TEST
  317. START_TEST(GetDataSetFieldConfigurationAndCompareValues){
  318. setupDataSetFieldTestEnvironment();
  319. UA_StatusCode retVal;
  320. UA_DataSetFieldConfig fieldConfig;
  321. memset(&fieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  322. fieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  323. fieldConfig.field.variable.fieldNameAlias = UA_STRING("field 1");
  324. UA_NodeId dataSetFieldId;
  325. retVal = UA_Server_addDataSetField(server, publishedDataSet1, &fieldConfig, &dataSetFieldId).result;
  326. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  327. UA_DataSetFieldConfig fieldConfigCopy;
  328. retVal |= UA_Server_getDataSetFieldConfig(server, dataSetFieldId, &fieldConfigCopy);
  329. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  330. ck_assert_int_eq(fieldConfig.dataSetFieldType, fieldConfigCopy.dataSetFieldType);
  331. ck_assert_int_eq(UA_String_equal(&fieldConfig.field.variable.fieldNameAlias, &fieldConfigCopy.field.variable.fieldNameAlias), UA_TRUE);
  332. UA_DataSetFieldConfig_clear(&fieldConfigCopy);
  333. } END_TEST
  334. START_TEST(SinglePublishDataSetField){
  335. UA_WriterGroupConfig writerGroupConfig;
  336. memset(&writerGroupConfig, 0, sizeof(writerGroupConfig));
  337. writerGroupConfig.name = UA_STRING("WriterGroup 1");
  338. writerGroupConfig.publishingInterval = 10;
  339. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  340. UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &writerGroup1);
  341. UA_Server_setWriterGroupOperational(server, writerGroup1);
  342. writerGroupConfig.name = UA_STRING("WriterGroup 2");
  343. writerGroupConfig.publishingInterval = 50;
  344. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  345. UA_Server_addWriterGroup(server, connection2, &writerGroupConfig, &writerGroup2);
  346. UA_Server_setWriterGroupOperational(server, writerGroup2);
  347. writerGroupConfig.name = UA_STRING("WriterGroup 3");
  348. writerGroupConfig.publishingInterval = 100;
  349. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  350. UA_Server_addWriterGroup(server, connection2, &writerGroupConfig, &writerGroup3);
  351. UA_Server_setWriterGroupOperational(server, writerGroup3);
  352. UA_PublishedDataSetConfig pdsConfig;
  353. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  354. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  355. pdsConfig.name = UA_STRING("PublishedDataSet 1");
  356. UA_Server_addPublishedDataSet(server, &pdsConfig, &publishedDataSet1);
  357. UA_Server_addPublishedDataSet(server, &pdsConfig, &publishedDataSet2);
  358. UA_DataSetWriterConfig dataSetWriterConfig;
  359. memset(&dataSetWriterConfig, 0, sizeof(dataSetWriterConfig));
  360. dataSetWriterConfig.name = UA_STRING("DataSetWriter 1");
  361. UA_Server_addDataSetWriter(server, writerGroup1, publishedDataSet1, &dataSetWriterConfig, &dataSetWriter1);
  362. UA_DataSetFieldConfig dataSetFieldConfig;
  363. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  364. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  365. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  366. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  367. dataSetFieldConfig.field.variable.publishParameters.publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_LOCALTIME);
  368. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  369. UA_Server_addDataSetField(server, publishedDataSet1, &dataSetFieldConfig, NULL);
  370. UA_WriterGroup *wg = UA_WriterGroup_findWGbyId(server, writerGroup1);
  371. UA_WriterGroup_publishCallback(server, wg);
  372. } END_TEST
  373. START_TEST(PublishDataSetFieldAsDeltaFrame){
  374. setupDataSetFieldTestEnvironment();
  375. UA_DataSetFieldConfig dataSetFieldConfig;
  376. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  377. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  378. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  379. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  380. dataSetFieldConfig.field.variable.publishParameters.publishedVariable = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_LOCALTIME);
  381. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  382. UA_Server_addDataSetField(server, publishedDataSet1, &dataSetFieldConfig, NULL);
  383. UA_Server_addDataSetField(server, publishedDataSet1, &dataSetFieldConfig, NULL);
  384. UA_WriterGroup *wg = UA_WriterGroup_findWGbyId(server, writerGroup1);
  385. wg->config.maxEncapsulatedDataSetMessageCount = 3;
  386. UA_DataSetWriter *dsw = UA_DataSetWriter_findDSWbyId(server, dataSetWriter1);
  387. dsw->config.keyFrameCount = 3;
  388. UA_WriterGroup_publishCallback(server, wg);
  389. UA_WriterGroup_publishCallback(server, wg);
  390. UA_WriterGroup_publishCallback(server, wg);
  391. UA_WriterGroup_publishCallback(server, wg);
  392. UA_WriterGroup_publishCallback(server, wg);
  393. } END_TEST
  394. int main(void) {
  395. TCase *tc_add_pubsub_writergroup = tcase_create("PubSub WriterGroup items handling");
  396. tcase_add_checked_fixture(tc_add_pubsub_writergroup, setup, teardown);
  397. tcase_add_test(tc_add_pubsub_writergroup, AddWriterGroupWithValidConfiguration);
  398. tcase_add_test(tc_add_pubsub_writergroup, AddRemoveAddWriterGroupWithMinimalValidConfiguration);
  399. tcase_add_test(tc_add_pubsub_writergroup, AddWriterGroupWithNullConfig);
  400. tcase_add_test(tc_add_pubsub_writergroup, AddWriterGroupWithInvalidConnectionId);
  401. tcase_add_test(tc_add_pubsub_writergroup, GetWriterGroupConfigurationAndCompareValues);
  402. TCase *tc_add_pubsub_datasetwriter = tcase_create("PubSub DataSetWriter items handling");
  403. tcase_add_checked_fixture(tc_add_pubsub_datasetwriter, setup, teardown);
  404. tcase_add_test(tc_add_pubsub_datasetwriter, AddDataSetWriterWithValidConfiguration);
  405. tcase_add_test(tc_add_pubsub_datasetwriter, AddRemoveAddDataSetWriterWithValidConfiguration);
  406. tcase_add_test(tc_add_pubsub_datasetwriter, AddDataSetWriterWithNullConfig);
  407. tcase_add_test(tc_add_pubsub_datasetwriter, AddDataSetWriterWithInvalidPDSId);
  408. tcase_add_test(tc_add_pubsub_datasetwriter, GetDataSetWriterConfigurationAndCompareValues);
  409. TCase *tc_add_pubsub_datasetfields = tcase_create("PubSub DataSetField items handling");
  410. tcase_add_checked_fixture(tc_add_pubsub_datasetfields, setup, teardown);
  411. tcase_add_test(tc_add_pubsub_datasetfields, AddDataSetFieldWithValidConfiguration);
  412. tcase_add_test(tc_add_pubsub_datasetfields, AddRemoveAddDataSetFieldWithValidConfiguration);
  413. tcase_add_test(tc_add_pubsub_datasetfields, AddDataSetFieldWithNullConfig);
  414. tcase_add_test(tc_add_pubsub_datasetfields, AddDataSetFieldWithInvalidPDSId);
  415. tcase_add_test(tc_add_pubsub_datasetfields, GetDataSetFieldConfigurationAndCompareValues);
  416. TCase *tc_pubsub_publish = tcase_create("PubSub publish DataSetFields");
  417. tcase_add_checked_fixture(tc_pubsub_publish, setup, teardown);
  418. tcase_add_test(tc_pubsub_publish, SinglePublishDataSetField);
  419. tcase_add_test(tc_pubsub_publish, PublishDataSetFieldAsDeltaFrame);
  420. Suite *s = suite_create("PubSub WriterGroups/Writer/Fields handling and publishing");
  421. suite_add_tcase(s, tc_add_pubsub_writergroup);
  422. suite_add_tcase(s, tc_add_pubsub_datasetwriter);
  423. suite_add_tcase(s, tc_add_pubsub_datasetfields);
  424. suite_add_tcase(s, tc_pubsub_publish);
  425. SRunner *sr = srunner_create(s);
  426. srunner_set_fork_status(sr, CK_NOFORK);
  427. srunner_run_all(sr,CK_NORMAL);
  428. int number_failed = srunner_ntests_failed(sr);
  429. srunner_free(sr);
  430. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  431. }