server_pubsub_publisher_rt_level.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. UA_NodeId publishedDataSetIdent, dataSetFieldIdent, writerGroupIdent, connectionIdentifier;
  11. UA_Boolean running = true;
  12. static void stopHandler(int sign) {
  13. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  14. running = false;
  15. }
  16. /**
  17. * The PubSub RT level example points out the configuration of different PubSub RT levels. These levels will be later
  18. * used for deterministic message generation. The underlying base concept is the target to reduce the time spread and
  19. * affort during the publish cycle. Most of the RT levels are based on a pregenerated and buffered DataSetMesseges and
  20. * NetworkMessages. Since changes in the PubSub configuration will invalidate the buffered frames, the pubsub
  21. * configuration can be frozen after the configuration phase
  22. */
  23. /* The following PubSub configuration does not differ from the 'normal' configuration */
  24. static void
  25. addMinimalPubSubConfiguration(UA_Server * server){
  26. /* Add one PubSubConnection */
  27. UA_PubSubConnectionConfig connectionConfig;
  28. memset(&connectionConfig, 0, sizeof(connectionConfig));
  29. connectionConfig.name = UA_STRING("UDP-UADP Connection 1");
  30. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  31. connectionConfig.enabled = UA_TRUE;
  32. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  33. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  34. connectionConfig.publisherId.numeric = UA_UInt32_random();
  35. UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdentifier);
  36. /* Add one PublishedDataSet */
  37. UA_PublishedDataSetConfig publishedDataSetConfig;
  38. memset(&publishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  39. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  40. publishedDataSetConfig.name = UA_STRING("Demo PDS");
  41. /* Add one DataSetField to the PDS */
  42. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  43. }
  44. int main(void) {
  45. signal(SIGINT, stopHandler);
  46. signal(SIGTERM, stopHandler);
  47. UA_Server *server = UA_Server_new();
  48. UA_ServerConfig *config = UA_Server_getConfig(server);
  49. UA_ServerConfig_setDefault(config);
  50. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  51. if(!config->pubsubTransportLayers) {
  52. UA_Server_delete(server);
  53. return -1;
  54. }
  55. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  56. config->pubsubTransportLayersSize++;
  57. /*Add standard PubSub configuration (no difference to the std. configuration)*/
  58. addMinimalPubSubConfiguration(server);
  59. /* Add one WriterGroup with PubSub RT Level 0. If any rtLevel != UA_PUBSUB_RT_NONE is set, the
  60. * writerGroup does not start the publishing interval automatically.*/
  61. UA_WriterGroupConfig writerGroupConfig;
  62. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  63. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  64. writerGroupConfig.publishingInterval = 100;
  65. writerGroupConfig.enabled = UA_FALSE;
  66. writerGroupConfig.writerGroupId = 100;
  67. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  68. /* RT Level 0 setup */
  69. writerGroupConfig.rtLevel = UA_PUBSUB_RT_FIXED_SIZE;
  70. UA_Server_addWriterGroup(server, connectionIdentifier, &writerGroupConfig, &writerGroupIdent);
  71. /* Add one DataSetWriter */
  72. UA_NodeId dataSetWriterIdent;
  73. UA_DataSetWriterConfig dataSetWriterConfig;
  74. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  75. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  76. dataSetWriterConfig.dataSetWriterId = 62541;
  77. dataSetWriterConfig.keyFrameCount = 10;
  78. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent, &dataSetWriterConfig, &dataSetWriterIdent);
  79. /* Add one DataSetField with static value source to PDS */
  80. UA_DataSetFieldConfig dsfConfig;
  81. UA_Server_getDataSetFieldConfig(server, dataSetFieldIdent, &dsfConfig);
  82. /* Create Variant and configure as DataSetField source */
  83. UA_UInt32 *intValue = UA_UInt32_new();
  84. UA_Variant variant;
  85. memset(&variant, 0, sizeof(UA_Variant));
  86. UA_Variant_setScalar(&variant, intValue, &UA_TYPES[UA_TYPES_UINT32]);
  87. UA_DataValue staticValueSource;
  88. memset(&staticValueSource, 0, sizeof(staticValueSource));
  89. staticValueSource.value = variant;
  90. dsfConfig.field.variable.staticValueSourceEnabled = UA_TRUE;
  91. dsfConfig.field.variable.staticValueSource.value = variant;
  92. UA_Server_addDataSetField(server, publishedDataSetIdent, &dsfConfig, &dataSetFieldIdent);
  93. /* The PubSub configuration is currently editable and the publish callback is not running */
  94. writerGroupConfig.publishingInterval = 1000;
  95. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  96. /* Freeze the PubSub configuration (and start implicitly the publish callback) */
  97. UA_Server_freezeWriterGroupConfiguration(server, writerGroupIdent);
  98. UA_Server_setWriterGroupOperational(server, writerGroupIdent);
  99. /* Changes of the PubSub configuration is restricted after freeze */
  100. UA_StatusCode retVal = UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  101. if(retVal != UA_STATUSCODE_BADCONFIGURATIONERROR)
  102. return EXIT_FAILURE;
  103. /* Unfreeze the PubSub configuration (and stop implicitly the publish callback) */
  104. UA_Server_setWriterGroupDisabled(server, writerGroupIdent);
  105. UA_Server_unfreezeWriterGroupConfiguration(server, writerGroupIdent);
  106. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  107. retval |= UA_Server_run(server, &running);
  108. UA_Server_delete(server);
  109. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  110. }