server_pubsub_publisher_rt_level.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include <open62541/plugin/log_stdout.h>
  4. #include <open62541/plugin/pubsub_udp.h>
  5. #include <open62541/server.h>
  6. #include <open62541/server_config_default.h>
  7. #include <signal.h>
  8. #include <stdlib.h>
  9. #include <open62541/server_pubsub.h>
  10. #define PUBSUB_CONFIG_PUBLISH_CYCLE_MS 100
  11. #define PUBSUB_CONFIG_PUBLISH_CYCLES 100
  12. /* possible values: PUBSUB_CONFIG_FASTPATH_NONE (WIP not implemented), PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS, PUBSUB_CONFIG_FASTPATH_STATIC_VALUES (WIP not implemented)*/
  13. #define PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS
  14. #define PUBSUB_CONFIG_FIELD_COUNT 10
  15. /**
  16. * The PubSub RT level example points out the configuration of different PubSub RT levels. These levels will be later
  17. * used for deterministic message generation. The main target is to reduce the time spread and effort during the publish cycle.
  18. * Most of the RT levels are based on a pre-generated and buffered DataSetMesseges and
  19. * NetworkMessages. Since changes in the PubSub-configuration will invalidate the buffered frames, the PubSub
  20. * configuration can be frozen after the configuration phase.
  21. *
  22. * This example can be configured to compare and measure the different PubSub options.
  23. */
  24. UA_NodeId publishedDataSetIdent, dataSetFieldIdent, writerGroupIdent, connectionIdentifier;
  25. UA_UInt32 *valueStore[PUBSUB_CONFIG_FIELD_COUNT];
  26. UA_Boolean running = true;
  27. static void stopHandler(int sign) {
  28. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  29. running = false;
  30. }
  31. /* The following PubSub configuration does not differ from the 'normal' configuration */
  32. static void
  33. addMinimalPubSubConfiguration(UA_Server * server){
  34. /* Add one PubSubConnection */
  35. UA_PubSubConnectionConfig connectionConfig;
  36. memset(&connectionConfig, 0, sizeof(connectionConfig));
  37. connectionConfig.name = UA_STRING("UDP-UADP Connection 1");
  38. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  39. connectionConfig.enabled = UA_TRUE;
  40. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  41. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  42. connectionConfig.publisherId.numeric = UA_UInt32_random();
  43. UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdentifier);
  44. /* Add one PublishedDataSet */
  45. UA_PublishedDataSetConfig publishedDataSetConfig;
  46. memset(&publishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  47. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  48. publishedDataSetConfig.name = UA_STRING("Demo PDS");
  49. /* Add one DataSetField to the PDS */
  50. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  51. }
  52. static void
  53. valueUpdateCallback(UA_Server *server, void *data) {
  54. for (int i = 0; i < PUBSUB_CONFIG_FIELD_COUNT; ++i)
  55. *valueStore[i] = *valueStore[i]+1;
  56. if(*valueStore[0] > PUBSUB_CONFIG_PUBLISH_CYCLES)
  57. running = false;
  58. }
  59. int main(void) {
  60. signal(SIGINT, stopHandler);
  61. signal(SIGTERM, stopHandler);
  62. UA_Server *server = UA_Server_new();
  63. UA_ServerConfig *config = UA_Server_getConfig(server);
  64. UA_ServerConfig_setDefault(config);
  65. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  66. if(!config->pubsubTransportLayers) {
  67. UA_Server_delete(server);
  68. return -1;
  69. }
  70. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  71. config->pubsubTransportLayersSize++;
  72. /*Add standard PubSub configuration (no difference to the std. configuration)*/
  73. addMinimalPubSubConfiguration(server);
  74. /* Add one WriterGroup with PubSub RT Level 0. If any rtLevel != UA_PUBSUB_RT_NONE is set, the
  75. * writerGroup does not start the publishing interval automatically.*/
  76. UA_WriterGroupConfig writerGroupConfig;
  77. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  78. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  79. writerGroupConfig.publishingInterval = PUBSUB_CONFIG_PUBLISH_CYCLE_MS;
  80. writerGroupConfig.enabled = UA_FALSE;
  81. writerGroupConfig.writerGroupId = 100;
  82. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  83. writerGroupConfig.messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  84. writerGroupConfig.messageSettings.content.decoded.type = &UA_TYPES[UA_TYPES_UADPWRITERGROUPMESSAGEDATATYPE];
  85. /* RT Level 0 setup */
  86. UA_UadpWriterGroupMessageDataType writerGroupMessage;
  87. UA_UadpWriterGroupMessageDataType_init(&writerGroupMessage);
  88. /* Change message settings of writerGroup to send PublisherId,
  89. * WriterGroupId in GroupHeader and DataSetWriterId in PayloadHeader
  90. * of NetworkMessage */
  91. writerGroupMessage.networkMessageContentMask = (UA_UadpNetworkMessageContentMask) ((UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_PUBLISHERID |
  92. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_GROUPHEADER |
  93. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_WRITERGROUPID |
  94. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_PAYLOADHEADER);
  95. writerGroupConfig.messageSettings.content.decoded.data = &writerGroupMessage;
  96. #if defined PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS
  97. writerGroupConfig.rtLevel = UA_PUBSUB_RT_FIXED_SIZE;
  98. #endif
  99. UA_Server_addWriterGroup(server, connectionIdentifier, &writerGroupConfig, &writerGroupIdent);
  100. /* Add one DataSetWriter */
  101. UA_NodeId dataSetWriterIdent;
  102. UA_DataSetWriterConfig dataSetWriterConfig;
  103. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  104. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  105. dataSetWriterConfig.dataSetWriterId = 62541;
  106. dataSetWriterConfig.keyFrameCount = 10;
  107. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent, &dataSetWriterConfig, &dataSetWriterIdent);
  108. #if defined PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS || defined PUBSUB_CONFIG_FASTPATH_STATIC_VALUES
  109. /* Add one DataSetField with static value source to PDS */
  110. UA_DataSetFieldConfig dsfConfig;
  111. for(size_t i = 0; i < PUBSUB_CONFIG_FIELD_COUNT; i++){
  112. memset(&dsfConfig, 0, sizeof(UA_DataSetFieldConfig));
  113. /* Create Variant and configure as DataSetField source */
  114. UA_UInt32 *intValue = UA_UInt32_new();
  115. *intValue = (UA_UInt32) i * 1000;
  116. valueStore[i] = intValue;
  117. UA_Variant variant;
  118. memset(&variant, 0, sizeof(UA_Variant));
  119. UA_Variant_setScalar(&variant, intValue, &UA_TYPES[UA_TYPES_UINT32]);
  120. UA_DataValue staticValueSource;
  121. memset(&staticValueSource, 0, sizeof(staticValueSource));
  122. staticValueSource.value = variant;
  123. dsfConfig.field.variable.staticValueSourceEnabled = UA_TRUE;
  124. dsfConfig.field.variable.staticValueSource.value = variant;
  125. UA_Server_addDataSetField(server, publishedDataSetIdent, &dsfConfig, &dataSetFieldIdent);
  126. }
  127. #endif
  128. /* The PubSub configuration is currently editable and the publish callback is not running */
  129. writerGroupConfig.publishingInterval = PUBSUB_CONFIG_PUBLISH_CYCLE_MS;
  130. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  131. /* Freeze the PubSub configuration (and start implicitly the publish callback) */
  132. UA_Server_freezeWriterGroupConfiguration(server, writerGroupIdent);
  133. UA_Server_setWriterGroupOperational(server, writerGroupIdent);
  134. /* Changes of the PubSub configuration is restricted after freeze */
  135. UA_StatusCode retVal = UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  136. if(retVal != UA_STATUSCODE_BADCONFIGURATIONERROR)
  137. return EXIT_FAILURE;
  138. /* Unfreeze the PubSub configuration (and stop implicitly the publish callback) */
  139. //UA_Server_setWriterGroupDisabled(server, writerGroupIdent);
  140. //UA_Server_unfreezeWriterGroupConfiguration(server, writerGroupIdent);
  141. UA_UInt64 callbackId;
  142. UA_Server_addRepeatedCallback(server, valueUpdateCallback, NULL, PUBSUB_CONFIG_PUBLISH_CYCLE_MS, &callbackId);
  143. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  144. retval |= UA_Server_run(server, &running);
  145. UA_Server_delete(server);
  146. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  147. }