check_pubsub_publish_uadp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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) 2019 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 "ua_server_internal.h"
  11. #include <check.h>
  12. UA_Server *server = NULL;
  13. UA_NodeId connection1, publishedDataSetIdent, dataSetFieldIdent, writerGroupIdent, dataSetWriterIdent;
  14. static void setup(void) {
  15. server = UA_Server_new();
  16. UA_ServerConfig *config = UA_Server_getConfig(server);
  17. UA_ServerConfig_setDefault(config);
  18. config->pubsubTransportLayers = (UA_PubSubTransportLayer*)
  19. UA_malloc(sizeof(UA_PubSubTransportLayer));
  20. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  21. config->pubsubTransportLayersSize++;
  22. UA_Server_run_startup(server);
  23. //add connection
  24. UA_PubSubConnectionConfig connectionConfig;
  25. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  26. connectionConfig.name = UA_STRING("UADP Connection");
  27. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4840/")};
  28. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  29. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  30. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  31. connectionConfig.publisherId.numeric = 62541;
  32. UA_Server_addPubSubConnection(server, &connectionConfig, &connection1);
  33. UA_PublishedDataSetConfig publishedDataSetConfig;
  34. memset(&publishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  35. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  36. publishedDataSetConfig.name = UA_STRING("Test PDS");
  37. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  38. UA_DataSetFieldConfig dataSetFieldConfig;
  39. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  40. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  41. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  42. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  43. dataSetFieldConfig.field.variable.publishParameters.publishedVariable =
  44. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  45. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  46. UA_Server_addDataSetField(server, publishedDataSetIdent,
  47. &dataSetFieldConfig, &dataSetFieldIdent);
  48. }
  49. static void teardown(void) {
  50. UA_Server_run_shutdown(server);
  51. UA_Server_delete(server);
  52. }
  53. static void receiveSingleMessage(UA_ByteString buffer, UA_PubSubConnection *connection, UA_NetworkMessage *networkMessage) {
  54. if (UA_ByteString_allocBuffer(&buffer, 512) != UA_STATUSCODE_GOOD) {
  55. ck_abort_msg("Message buffer allocation failed!");
  56. }
  57. UA_StatusCode retval =
  58. connection->channel->receive(connection->channel, &buffer, NULL, 10000);
  59. if(retval != UA_STATUSCODE_GOOD || buffer.length == 0) {
  60. buffer.length = 512;
  61. UA_ByteString_clear(&buffer);
  62. ck_abort_msg("Expected message not received!");
  63. }
  64. memset(networkMessage, 0, sizeof(UA_NetworkMessage));
  65. size_t currentPosition = 0;
  66. UA_NetworkMessage_decodeBinary(&buffer, &currentPosition, networkMessage);
  67. UA_ByteString_clear(&buffer);
  68. }
  69. START_TEST(CheckNMandDSMcalculation){
  70. UA_WriterGroupConfig writerGroupConfig;
  71. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  72. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  73. writerGroupConfig.publishingInterval = 10;
  74. writerGroupConfig.enabled = UA_FALSE;
  75. writerGroupConfig.writerGroupId = 100;
  76. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  77. UA_UadpWriterGroupMessageDataType *wgm = UA_UadpWriterGroupMessageDataType_new();
  78. wgm->networkMessageContentMask = UA_UADPNETWORKMESSAGECONTENTMASK_PAYLOADHEADER;
  79. writerGroupConfig.messageSettings.content.decoded.data = wgm;
  80. writerGroupConfig.messageSettings.content.decoded.type =
  81. &UA_TYPES[UA_TYPES_UADPWRITERGROUPMESSAGEDATATYPE];
  82. writerGroupConfig.messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  83. //maximum DSM in one NM = 10
  84. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 10;
  85. UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &writerGroupIdent);
  86. UA_Server_setWriterGroupOperational(server, writerGroupIdent);
  87. UA_UadpWriterGroupMessageDataType_delete(wgm);
  88. UA_DataSetWriterConfig dataSetWriterConfig;
  89. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  90. dataSetWriterConfig.name = UA_STRING("Test DataSetWriter");
  91. dataSetWriterConfig.dataSetWriterId = 10;
  92. dataSetWriterConfig.keyFrameCount = 1;
  93. //add 10 dataSetWriter
  94. for(UA_UInt16 i = 0; i < 10; i++){
  95. dataSetWriterConfig.dataSetWriterId = (UA_UInt16) (dataSetWriterConfig.dataSetWriterId + 1);
  96. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  97. &dataSetWriterConfig, &dataSetWriterIdent);
  98. }
  99. UA_PubSubConnection *connection = UA_PubSubConnection_findConnectionbyId(server, connection1);
  100. if(connection != NULL) {
  101. UA_StatusCode rv = connection->channel->regist(connection->channel, NULL, NULL);
  102. ck_assert(rv == UA_STATUSCODE_GOOD);
  103. }
  104. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  105. writerGroupConfig.publishingInterval = 100000;
  106. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  107. UA_ByteString buffer = UA_BYTESTRING_ALLOC("");
  108. UA_NetworkMessage networkMessage;
  109. receiveSingleMessage(buffer, connection, &networkMessage);
  110. //ck_assert_int_eq(networkMessage.publisherId.publisherIdUInt32 , 62541);
  111. ck_assert_int_eq(networkMessage.payloadHeader.dataSetPayloadHeader.count, 10);
  112. for(size_t i = 10; i > 0; i--){
  113. ck_assert_int_eq(*(networkMessage.payloadHeader.dataSetPayloadHeader.dataSetWriterIds+(i-1)), 21-i);
  114. }
  115. UA_NetworkMessage_clear(&networkMessage);
  116. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  117. writerGroupConfig.publishingInterval = 200000;
  118. //maximum DSM in one NM = 5
  119. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 5;
  120. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  121. UA_NetworkMessage networkMessage1, networkMessage2;
  122. receiveSingleMessage(buffer, connection, &networkMessage1);
  123. receiveSingleMessage(buffer, connection, &networkMessage2);
  124. ck_assert_int_eq(networkMessage1.payloadHeader.dataSetPayloadHeader.count, 5);
  125. ck_assert_int_eq(networkMessage1.payloadHeader.dataSetPayloadHeader.count, 5);
  126. UA_NetworkMessage_clear(&networkMessage1);
  127. UA_NetworkMessage_clear(&networkMessage2);
  128. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  129. writerGroupConfig.publishingInterval = 300000;
  130. //maximum DSM in one NM = 20
  131. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 20;
  132. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  133. UA_NetworkMessage networkMessage3;
  134. receiveSingleMessage(buffer, connection, &networkMessage3);
  135. ck_assert_int_eq(networkMessage3.payloadHeader.dataSetPayloadHeader.count, 10);
  136. UA_NetworkMessage_clear(&networkMessage3);
  137. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  138. writerGroupConfig.publishingInterval = 400000;
  139. //maximum DSM in one NM = 1
  140. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 1;
  141. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  142. UA_NetworkMessage messageArray[10];
  143. for (int j = 0; j < 10; ++j) {
  144. receiveSingleMessage(buffer, connection, &(messageArray[j]));
  145. ck_assert_int_eq(messageArray[j].payloadHeader.dataSetPayloadHeader.count, 1);
  146. UA_NetworkMessage_clear(&messageArray[j]);
  147. }
  148. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  149. writerGroupConfig.publishingInterval = 500000;
  150. //maximum DSM in one NM = 0 -> should be equal to 1
  151. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 0;
  152. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  153. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  154. for (int j = 0; j < 10; ++j) {
  155. receiveSingleMessage(buffer, connection, &(messageArray[j]));
  156. ck_assert_int_eq(messageArray[j].payloadHeader.dataSetPayloadHeader.count, 1);
  157. UA_NetworkMessage_clear(&messageArray[j]);
  158. }
  159. } END_TEST
  160. START_TEST(CheckNMandDSMBufferCalculation){
  161. UA_WriterGroupConfig writerGroupConfig;
  162. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  163. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  164. writerGroupConfig.publishingInterval = 10;
  165. writerGroupConfig.enabled = UA_FALSE;
  166. writerGroupConfig.writerGroupId = 100;
  167. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  168. writerGroupConfig.rtLevel = UA_PUBSUB_RT_FIXED_SIZE;
  169. UA_UadpWriterGroupMessageDataType *wgm = UA_UadpWriterGroupMessageDataType_new();
  170. wgm->networkMessageContentMask = UA_UADPNETWORKMESSAGECONTENTMASK_PAYLOADHEADER;
  171. writerGroupConfig.messageSettings.content.decoded.data = wgm;
  172. writerGroupConfig.messageSettings.content.decoded.type =
  173. &UA_TYPES[UA_TYPES_UADPWRITERGROUPMESSAGEDATATYPE];
  174. writerGroupConfig.messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  175. //maximum DSM in one NM = 10
  176. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 10;
  177. UA_Server_addWriterGroup(server, connection1, &writerGroupConfig, &writerGroupIdent);
  178. UA_Server_setWriterGroupOperational(server, writerGroupIdent);
  179. UA_UadpWriterGroupMessageDataType_delete(wgm);
  180. UA_DataSetWriterConfig dataSetWriterConfig;
  181. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  182. dataSetWriterConfig.name = UA_STRING("Test DataSetWriter");
  183. dataSetWriterConfig.dataSetWriterId = 10;
  184. dataSetWriterConfig.keyFrameCount = 1;
  185. //add 10 dataSetWriter
  186. for(UA_UInt16 i = 0; i < 10; i++){
  187. dataSetWriterConfig.dataSetWriterId = (UA_UInt16) (dataSetWriterConfig.dataSetWriterId + 1);
  188. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  189. &dataSetWriterConfig, &dataSetWriterIdent);
  190. }
  191. //TODO extend this test with the buffer and offset calculation of the uadp network layer
  192. UA_Server_freezeWriterGroupConfiguration(server, writerGroupIdent);
  193. UA_Server_unfreezeWriterGroupConfiguration(server, writerGroupIdent);
  194. } END_TEST
  195. int main(void) {
  196. TCase *tc_add_pubsub_DSMandNMcalculation = tcase_create("PubSub NM and DSM");
  197. tcase_add_checked_fixture(tc_add_pubsub_DSMandNMcalculation, setup, teardown);
  198. tcase_add_test(tc_add_pubsub_DSMandNMcalculation, CheckNMandDSMcalculation);
  199. tcase_add_test(tc_add_pubsub_DSMandNMcalculation, CheckNMandDSMBufferCalculation);
  200. Suite *s = suite_create("PubSub NM and DSM calculation");
  201. suite_add_tcase(s, tc_add_pubsub_DSMandNMcalculation);
  202. SRunner *sr = srunner_create(s);
  203. srunner_set_fork_status(sr, CK_NOFORK);
  204. srunner_run_all(sr,CK_NORMAL);
  205. int number_failed = srunner_ntests_failed(sr);
  206. srunner_free(sr);
  207. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  208. }