server_pubsub_publisher_rt_level.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. static void
  45. valueUpdateCallback(UA_Server *server, void *data) {
  46. UA_DataValue dataValue = *((UA_DataValue *) data);
  47. UA_UInt32 *integerValue = (UA_UInt32 *) dataValue.value.data;
  48. *integerValue += 1;
  49. UA_Variant_setScalar(&dataValue.value, integerValue, &UA_TYPES[UA_TYPES_UINT32]);
  50. }
  51. int main(void) {
  52. signal(SIGINT, stopHandler);
  53. signal(SIGTERM, stopHandler);
  54. UA_Server *server = UA_Server_new();
  55. UA_ServerConfig *config = UA_Server_getConfig(server);
  56. UA_ServerConfig_setDefault(config);
  57. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  58. if(!config->pubsubTransportLayers) {
  59. UA_Server_delete(server);
  60. return -1;
  61. }
  62. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  63. config->pubsubTransportLayersSize++;
  64. /*Add standard PubSub configuration (no difference to the std. configuration)*/
  65. addMinimalPubSubConfiguration(server);
  66. /* Add one WriterGroup with PubSub RT Level 0. If any rtLevel != UA_PUBSUB_RT_NONE is set, the
  67. * writerGroup does not start the publishing interval automatically.*/
  68. UA_WriterGroupConfig writerGroupConfig;
  69. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  70. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  71. writerGroupConfig.publishingInterval = 100;
  72. writerGroupConfig.enabled = UA_FALSE;
  73. writerGroupConfig.writerGroupId = 100;
  74. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  75. writerGroupConfig.messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  76. writerGroupConfig.messageSettings.content.decoded.type = &UA_TYPES[UA_TYPES_UADPWRITERGROUPMESSAGEDATATYPE];
  77. /* RT Level 0 setup */
  78. UA_UadpWriterGroupMessageDataType *writerGroupMessage = UA_UadpWriterGroupMessageDataType_new();
  79. /* Change message settings of writerGroup to send PublisherId,
  80. * WriterGroupId in GroupHeader and DataSetWriterId in PayloadHeader
  81. * of NetworkMessage */
  82. writerGroupMessage->networkMessageContentMask = (UA_UadpNetworkMessageContentMask) ((UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_PUBLISHERID |
  83. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_GROUPHEADER |
  84. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_WRITERGROUPID |
  85. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_PAYLOADHEADER);
  86. writerGroupConfig.messageSettings.content.decoded.data = writerGroupMessage;
  87. writerGroupConfig.rtLevel = UA_PUBSUB_RT_FIXED_SIZE;
  88. UA_Server_addWriterGroup(server, connectionIdentifier, &writerGroupConfig, &writerGroupIdent);
  89. /* Add one DataSetWriter */
  90. UA_NodeId dataSetWriterIdent;
  91. UA_DataSetWriterConfig dataSetWriterConfig;
  92. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  93. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  94. dataSetWriterConfig.dataSetWriterId = 62541;
  95. dataSetWriterConfig.keyFrameCount = 10;
  96. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent, &dataSetWriterConfig, &dataSetWriterIdent);
  97. /* Add one DataSetField with static value source to PDS */
  98. UA_DataSetFieldConfig dsfConfig;
  99. UA_Server_getDataSetFieldConfig(server, dataSetFieldIdent, &dsfConfig);
  100. /* Create Variant and configure as DataSetField source */
  101. UA_UInt32 *intValue = UA_UInt32_new();
  102. UA_Variant variant;
  103. memset(&variant, 0, sizeof(UA_Variant));
  104. UA_Variant_setScalar(&variant, intValue, &UA_TYPES[UA_TYPES_UINT32]);
  105. UA_DataValue staticValueSource;
  106. memset(&staticValueSource, 0, sizeof(staticValueSource));
  107. staticValueSource.value = variant;
  108. dsfConfig.field.variable.staticValueSourceEnabled = UA_TRUE;
  109. dsfConfig.field.variable.staticValueSource.value = variant;
  110. UA_Server_addDataSetField(server, publishedDataSetIdent, &dsfConfig, &dataSetFieldIdent);
  111. /* The PubSub configuration is currently editable and the publish callback is not running */
  112. writerGroupConfig.publishingInterval = 1000;
  113. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  114. /* Freeze the PubSub configuration (and start implicitly the publish callback) */
  115. UA_Server_freezeWriterGroupConfiguration(server, writerGroupIdent);
  116. UA_Server_setWriterGroupOperational(server, writerGroupIdent);
  117. /* Changes of the PubSub configuration is restricted after freeze */
  118. UA_StatusCode retVal = UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  119. if(retVal != UA_STATUSCODE_BADCONFIGURATIONERROR)
  120. return EXIT_FAILURE;
  121. /* Unfreeze the PubSub configuration (and stop implicitly the publish callback) */
  122. //UA_Server_setWriterGroupDisabled(server, writerGroupIdent);
  123. //UA_Server_unfreezeWriterGroupConfiguration(server, writerGroupIdent);
  124. UA_UInt64 callbackId;
  125. UA_Server_addRepeatedCallback(server, valueUpdateCallback, &staticValueSource, 1000, &callbackId);
  126. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  127. retval |= UA_Server_run(server, &running);
  128. UA_Server_delete(server);
  129. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  130. }