server_pubsub_publisher_rt_level.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /* possible options: PUBSUB_CONFIG_FASTPATH_NONE, PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS, PUBSUB_CONFIG_FASTPATH_STATIC_VALUES */
  11. #define PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS
  12. #define PUBSUB_CONFIG_PUBLISH_CYCLE_MS 100
  13. #define PUBSUB_CONFIG_PUBLISH_CYCLES 100
  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
  17. * used together with an RT capable OS for deterministic message generation. The main target is to reduce the time
  18. * spread and effort during the publish cycle. Most of the RT levels are based on pre-generated and buffered
  19. * DataSetMesseges and NetworkMessages. Since changes in the PubSub-configuration will invalidate the buffered
  20. * frames, the PubSub configuration must be frozen after the configuration phase.
  21. *
  22. * This example can be configured to compare and measure the different PubSub options by the following define options:
  23. * PUBSUB_CONFIG_FASTPATH_NONE -> The published fields are added to the information model and published in the default non-RT mode
  24. * PUBSUB_CONFIG_FASTPATH_STATIC_VALUES -> The published fields are not visible in the information model. The publish cycle is improved
  25. * by prevent the value lookup within the information model.
  26. * PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS -> The published fields are not visible in the information model. After the PubSub-configuration
  27. * freeze, the NetworkMessages and DataSetMessages will be calculated and buffered. During the publish cycle these buffers will only be updated.
  28. */
  29. UA_NodeId publishedDataSetIdent, dataSetFieldIdent, writerGroupIdent, connectionIdentifier;
  30. UA_UInt32 *valueStore[PUBSUB_CONFIG_FIELD_COUNT];
  31. UA_Boolean running = true;
  32. static void stopHandler(int sign) {
  33. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  34. running = false;
  35. }
  36. /* The following PubSub configuration does not differ from the 'normal' configuration */
  37. static void
  38. addMinimalPubSubConfiguration(UA_Server * server){
  39. /* Add one PubSubConnection */
  40. UA_PubSubConnectionConfig connectionConfig;
  41. memset(&connectionConfig, 0, sizeof(connectionConfig));
  42. connectionConfig.name = UA_STRING("UDP-UADP Connection 1");
  43. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  44. connectionConfig.enabled = UA_TRUE;
  45. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  46. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  47. connectionConfig.publisherId.numeric = UA_UInt32_random();
  48. UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdentifier);
  49. /* Add one PublishedDataSet */
  50. UA_PublishedDataSetConfig publishedDataSetConfig;
  51. memset(&publishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  52. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  53. publishedDataSetConfig.name = UA_STRING("Demo PDS");
  54. /* Add one DataSetField to the PDS */
  55. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  56. }
  57. static void
  58. valueUpdateCallback(UA_Server *server, void *data) {
  59. #if defined PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS || defined PUBSUB_CONFIG_FASTPATH_STATIC_VALUES
  60. for (int i = 0; i < PUBSUB_CONFIG_FIELD_COUNT; ++i)
  61. *valueStore[i] = *valueStore[i] + 1;
  62. if(*valueStore[0] > PUBSUB_CONFIG_PUBLISH_CYCLES)
  63. running = false;
  64. #endif
  65. #if defined PUBSUB_CONFIG_FASTPATH_NONE
  66. for(size_t i = 0; i < PUBSUB_CONFIG_FIELD_COUNT; i++){
  67. UA_Variant value;
  68. UA_Variant_init(&value);
  69. if(UA_Server_readValue(server, UA_NODEID_NUMERIC(1, 1000 + (UA_UInt32) i), &value) != UA_STATUSCODE_GOOD) {
  70. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to read publish value. Node number: %zu", i);
  71. continue;
  72. }
  73. UA_UInt32 *intValue = (UA_UInt32 *) value.data;
  74. *intValue = *intValue + 1;
  75. if(UA_Server_writeValue(server, UA_NODEID_NUMERIC(1, 1000 + (UA_UInt32) i), value) != UA_STATUSCODE_GOOD){
  76. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to update publish value. Node number: %zu", i);
  77. continue;
  78. }
  79. if(i == 0 && *intValue > PUBSUB_CONFIG_PUBLISH_CYCLES)
  80. running = false;
  81. UA_Variant_deleteMembers(&value);
  82. }
  83. #endif
  84. }
  85. int main(void) {
  86. signal(SIGINT, stopHandler);
  87. signal(SIGTERM, stopHandler);
  88. UA_Server *server = UA_Server_new();
  89. UA_ServerConfig *config = UA_Server_getConfig(server);
  90. UA_ServerConfig_setDefault(config);
  91. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  92. if(!config->pubsubTransportLayers) {
  93. UA_Server_delete(server);
  94. return -1;
  95. }
  96. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  97. config->pubsubTransportLayersSize++;
  98. /*Add standard PubSub configuration (no difference to the std. configuration)*/
  99. addMinimalPubSubConfiguration(server);
  100. UA_WriterGroupConfig writerGroupConfig;
  101. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  102. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  103. writerGroupConfig.publishingInterval = PUBSUB_CONFIG_PUBLISH_CYCLE_MS;
  104. writerGroupConfig.enabled = UA_FALSE;
  105. writerGroupConfig.writerGroupId = 100;
  106. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  107. writerGroupConfig.messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  108. writerGroupConfig.messageSettings.content.decoded.type = &UA_TYPES[UA_TYPES_UADPWRITERGROUPMESSAGEDATATYPE];
  109. UA_UadpWriterGroupMessageDataType writerGroupMessage;
  110. UA_UadpWriterGroupMessageDataType_init(&writerGroupMessage);
  111. /* Change message settings of writerGroup to send PublisherId,
  112. * WriterGroupId in GroupHeader and DataSetWriterId in PayloadHeader
  113. * of NetworkMessage */
  114. writerGroupMessage.networkMessageContentMask = (UA_UadpNetworkMessageContentMask) ((UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_PUBLISHERID |
  115. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_GROUPHEADER |
  116. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_WRITERGROUPID |
  117. (UA_UadpNetworkMessageContentMask) UA_UADPNETWORKMESSAGECONTENTMASK_PAYLOADHEADER);
  118. writerGroupConfig.messageSettings.content.decoded.data = &writerGroupMessage;
  119. #ifdef PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS
  120. writerGroupConfig.rtLevel = UA_PUBSUB_RT_FIXED_SIZE;
  121. #elif defined PUBSUB_CONFIG_FASTPATH_STATIC_VALUES
  122. writerGroupConfig.rtLevel = UA_PUBSUB_RT_DIRECT_VALUE_ACCESS;
  123. #endif
  124. UA_Server_addWriterGroup(server, connectionIdentifier, &writerGroupConfig, &writerGroupIdent);
  125. /* Add one DataSetWriter */
  126. UA_NodeId dataSetWriterIdent;
  127. UA_DataSetWriterConfig dataSetWriterConfig;
  128. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  129. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  130. dataSetWriterConfig.dataSetWriterId = 62541;
  131. dataSetWriterConfig.keyFrameCount = 10;
  132. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent, &dataSetWriterConfig, &dataSetWriterIdent);
  133. #if defined PUBSUB_CONFIG_FASTPATH_FIXED_OFFSETS || defined PUBSUB_CONFIG_FASTPATH_STATIC_VALUES
  134. /* Add one DataSetField with static value source to PDS */
  135. UA_DataSetFieldConfig dsfConfig;
  136. for(size_t i = 0; i < PUBSUB_CONFIG_FIELD_COUNT; i++){
  137. memset(&dsfConfig, 0, sizeof(UA_DataSetFieldConfig));
  138. /* Create Variant and configure as DataSetField source */
  139. UA_UInt32 *intValue = UA_UInt32_new();
  140. *intValue = (UA_UInt32) i * 1000;
  141. valueStore[i] = intValue;
  142. UA_Variant variant;
  143. memset(&variant, 0, sizeof(UA_Variant));
  144. UA_Variant_setScalar(&variant, intValue, &UA_TYPES[UA_TYPES_UINT32]);
  145. UA_DataValue staticValueSource;
  146. memset(&staticValueSource, 0, sizeof(staticValueSource));
  147. staticValueSource.value = variant;
  148. dsfConfig.field.variable.staticValueSourceEnabled = UA_TRUE;
  149. dsfConfig.field.variable.staticValueSource.value = variant;
  150. UA_Server_addDataSetField(server, publishedDataSetIdent, &dsfConfig, &dataSetFieldIdent);
  151. }
  152. #endif
  153. #if defined PUBSUB_CONFIG_FASTPATH_NONE
  154. UA_DataSetFieldConfig dsfConfig;
  155. memset(&dsfConfig, 0, sizeof(UA_DataSetFieldConfig));
  156. for(size_t i = 0; i < PUBSUB_CONFIG_FIELD_COUNT; i++){
  157. UA_VariableAttributes attributes = UA_VariableAttributes_default;
  158. UA_Variant value;
  159. UA_Variant_init(&value);
  160. UA_UInt32 intValue = (UA_UInt32) i * 1000;
  161. UA_Variant_setScalar(&value, &intValue, &UA_TYPES[UA_TYPES_UINT32]);
  162. attributes.value = value;
  163. if(UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 1000 + (UA_UInt32) i),
  164. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  165. UA_QUALIFIEDNAME(1, "variable"), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  166. attributes, NULL, NULL) != UA_STATUSCODE_GOOD){
  167. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Failed to add Publish-Node to the server. Node number: %zu", i);
  168. continue;
  169. }
  170. dsfConfig.field.variable.publishParameters.publishedVariable = UA_NODEID_NUMERIC(1, 1000 + (UA_UInt32) i);
  171. dsfConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  172. UA_Server_addDataSetField(server, publishedDataSetIdent, &dsfConfig, &dataSetFieldIdent);
  173. }
  174. #endif
  175. /* The PubSub configuration is currently editable and the publish callback is not running */
  176. writerGroupConfig.publishingInterval = PUBSUB_CONFIG_PUBLISH_CYCLE_MS;
  177. UA_Server_updateWriterGroupConfig(server, writerGroupIdent, &writerGroupConfig);
  178. /* Freeze the PubSub configuration (and start implicitly the publish callback) */
  179. UA_Server_freezeWriterGroupConfiguration(server, writerGroupIdent);
  180. UA_Server_setWriterGroupOperational(server, writerGroupIdent);
  181. UA_UInt64 callbackId;
  182. UA_Server_addRepeatedCallback(server, valueUpdateCallback, NULL, PUBSUB_CONFIG_PUBLISH_CYCLE_MS, &callbackId);
  183. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  184. retval |= UA_Server_run(server, &running);
  185. UA_Server_delete(server);
  186. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  187. }