check_pubsub_publish_uadp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_UadpWriterGroupMessageDataType_delete(wgm);
  87. UA_DataSetWriterConfig dataSetWriterConfig;
  88. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  89. dataSetWriterConfig.name = UA_STRING("Test DataSetWriter");
  90. dataSetWriterConfig.dataSetWriterId = 10;
  91. dataSetWriterConfig.keyFrameCount = 1;
  92. //add 10 dataSetWriter
  93. for(UA_UInt16 i = 0; i < 10; i++){
  94. dataSetWriterConfig.dataSetWriterId = (UA_UInt16) (dataSetWriterConfig.dataSetWriterId + 1);
  95. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  96. &dataSetWriterConfig, &dataSetWriterIdent);
  97. }
  98. UA_PubSubConnection *connection = UA_PubSubConnection_findConnectionbyId(server, connection1);
  99. if(connection != NULL) {
  100. UA_StatusCode rv = connection->channel->regist(connection->channel, NULL, NULL);
  101. ck_assert(rv == UA_STATUSCODE_GOOD);
  102. }
  103. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  104. writerGroupConfig.publishingInterval = 100000;
  105. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  106. UA_ByteString buffer = UA_BYTESTRING_ALLOC("");
  107. UA_NetworkMessage networkMessage;
  108. receiveSingleMessage(buffer, connection, &networkMessage);
  109. //ck_assert_int_eq(networkMessage.publisherId.publisherIdUInt32 , 62541);
  110. ck_assert_int_eq(networkMessage.payloadHeader.dataSetPayloadHeader.count, 10);
  111. for(size_t i = 10; i > 0; i--){
  112. ck_assert_int_eq(*(networkMessage.payloadHeader.dataSetPayloadHeader.dataSetWriterIds+(i-1)), 21-i);
  113. }
  114. UA_NetworkMessage_deleteMembers(&networkMessage);
  115. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  116. writerGroupConfig.publishingInterval = 200000;
  117. //maximum DSM in one NM = 5
  118. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 5;
  119. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  120. UA_NetworkMessage networkMessage1, networkMessage2;
  121. receiveSingleMessage(buffer, connection, &networkMessage1);
  122. receiveSingleMessage(buffer, connection, &networkMessage2);
  123. ck_assert_int_eq(networkMessage1.payloadHeader.dataSetPayloadHeader.count, 5);
  124. ck_assert_int_eq(networkMessage1.payloadHeader.dataSetPayloadHeader.count, 5);
  125. UA_NetworkMessage_deleteMembers(&networkMessage1);
  126. UA_NetworkMessage_deleteMembers(&networkMessage2);
  127. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  128. writerGroupConfig.publishingInterval = 300000;
  129. //maximum DSM in one NM = 20
  130. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 20;
  131. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  132. UA_NetworkMessage networkMessage3;
  133. receiveSingleMessage(buffer, connection, &networkMessage3);
  134. ck_assert_int_eq(networkMessage3.payloadHeader.dataSetPayloadHeader.count, 10);
  135. UA_NetworkMessage_deleteMembers(&networkMessage3);
  136. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  137. writerGroupConfig.publishingInterval = 400000;
  138. //maximum DSM in one NM = 1
  139. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 1;
  140. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  141. UA_NetworkMessage messageArray[10];
  142. for (int j = 0; j < 10; ++j) {
  143. receiveSingleMessage(buffer, connection, &(messageArray[j]));
  144. ck_assert_int_eq(messageArray[j].payloadHeader.dataSetPayloadHeader.count, 1);
  145. UA_NetworkMessage_deleteMembers(&messageArray[j]);
  146. }
  147. //change publish interval triggers implicit one publish callback run | alternatively run UA_Server_iterate
  148. writerGroupConfig.publishingInterval = 500000;
  149. //maximum DSM in one NM = 0 -> should be equal to 1
  150. writerGroupConfig.maxEncapsulatedDataSetMessageCount = 0;
  151. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  152. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  153. for (int j = 0; j < 10; ++j) {
  154. receiveSingleMessage(buffer, connection, &(messageArray[j]));
  155. ck_assert_int_eq(messageArray[j].payloadHeader.dataSetPayloadHeader.count, 1);
  156. UA_NetworkMessage_deleteMembers(&messageArray[j]);
  157. }
  158. } END_TEST
  159. int main(void) {
  160. TCase *tc_add_pubsub_DSMandNMcalculation = tcase_create("PubSub NM and DSM");
  161. tcase_add_checked_fixture(tc_add_pubsub_DSMandNMcalculation, setup, teardown);
  162. tcase_add_test(tc_add_pubsub_DSMandNMcalculation, CheckNMandDSMcalculation);
  163. Suite *s = suite_create("PubSub NM and DSM calculation");
  164. suite_add_tcase(s, tc_add_pubsub_DSMandNMcalculation);
  165. SRunner *sr = srunner_create(s);
  166. srunner_set_fork_status(sr, CK_NOFORK);
  167. srunner_run_all(sr,CK_NORMAL);
  168. int number_failed = srunner_ntests_failed(sr);
  169. srunner_free(sr);
  170. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  171. }