tutorial_pubsub_publish.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  4. * .. _pubsub-tutorial:
  5. *
  6. * Working with Publish/Subscribe
  7. * ------------------------------
  8. *
  9. * Work in progress:
  10. * This Tutorial will be continuously extended during the next PubSub batches. More details about
  11. * the PubSub extension and corresponding open62541 API are located here: :ref:`pubsub`.
  12. *
  13. * Publishing Fields
  14. * ^^^^^^^^^^^^^^^^^
  15. * The PubSub publish example demonstrate the simplest way to publish
  16. * informations from the information model over UDP multicast using
  17. * the UADP encoding.
  18. *
  19. * **Connection handling**
  20. * PubSubConnections can be created and deleted on runtime. More details about the system preconfiguration and
  21. * connection can be found in ``tutorial_pubsub_connection.c``.
  22. */
  23. #include <signal.h>
  24. #include "open62541.h"
  25. UA_NodeId connectionIdent, publishedDataSetIdent, writerGroupIdent;
  26. static void
  27. addPubSubConnection(UA_Server *server){
  28. /* Details about the connection configuration and handling are located
  29. * in the pubsub connection tutorial */
  30. UA_PubSubConnectionConfig connectionConfig;
  31. memset(&connectionConfig, 0, sizeof(connectionConfig));
  32. connectionConfig.name = UA_STRING("UDP-UADP Connection 1");
  33. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  34. connectionConfig.enabled = UA_TRUE;
  35. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  36. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  37. connectionConfig.publisherId.numeric = UA_UInt32_random();
  38. UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdent);
  39. }
  40. /**
  41. * **PublishedDataSet handling**
  42. * The PublishedDataSet (PDS) and PubSubConnection are the toplevel entities and can exist alone. The PDS contains
  43. * the collection of the published fields.
  44. * All other PubSub elements are directly or indirectly linked with the PDS or connection.
  45. */
  46. static void
  47. addPublishedDataSet(UA_Server *server) {
  48. /* The PublishedDataSetConfig contains all necessary public
  49. * informations for the creation of a new 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. /* Create new PublishedDataSet based on the PublishedDataSetConfig. */
  55. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  56. }
  57. /**
  58. * **DataSetField handling**
  59. * The DataSetField (DSF) is part of the PDS and describes exactly one published field.
  60. */
  61. static void
  62. addDataSetField(UA_Server *server) {
  63. /* Add a field to the previous created PublishedDataSet */
  64. UA_NodeId dataSetFieldIdent;
  65. UA_DataSetFieldConfig dataSetFieldConfig;
  66. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  67. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  68. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  69. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  70. dataSetFieldConfig.field.variable.publishParameters.publishedVariable =
  71. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_LOCALTIME);
  72. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  73. UA_Server_addDataSetField(server, publishedDataSetIdent, &dataSetFieldConfig, &dataSetFieldIdent);
  74. }
  75. /**
  76. * **WriterGroup handling**
  77. * The WriterGroup (WG) is part of the connection and contains the primary configuration
  78. * parameters for the message creation.
  79. */
  80. static void
  81. addWriterGroup(UA_Server *server) {
  82. /* Now we create a new WriterGroupConfig and add the group to the existing PubSubConnection. */
  83. UA_WriterGroupConfig writerGroupConfig;
  84. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  85. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  86. writerGroupConfig.publishingInterval = 100;
  87. writerGroupConfig.enabled = UA_FALSE;
  88. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  89. /* The configuration flags for the messages are encapsulated inside the
  90. * message- and transport settings extension objects. These extension objects
  91. * are defined by the standard. e.g. UadpWriterGroupMessageDataType */
  92. UA_Server_addWriterGroup(server, connectionIdent, &writerGroupConfig, &writerGroupIdent);
  93. }
  94. /**
  95. * **DataSetWriter handling**
  96. * A DataSetWriter (DSW) is the glue between the WG and the PDS. The DSW is linked to exactly one
  97. * PDS and contains additional informations for the message generation.
  98. */
  99. static void
  100. addDataSetWriter(UA_Server *server) {
  101. /* We need now a DataSetWriter within the WriterGroup. This means we must
  102. * create a new DataSetWriterConfig and add call the addWriterGroup function. */
  103. UA_NodeId dataSetWriterIdent;
  104. UA_DataSetWriterConfig dataSetWriterConfig;
  105. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  106. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  107. dataSetWriterConfig.dataSetWriterId = 62541;
  108. dataSetWriterConfig.keyFrameCount = 10;
  109. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  110. &dataSetWriterConfig, &dataSetWriterIdent);
  111. }
  112. /**
  113. * That's it! You're now publishing the selected fields.
  114. * Open a packet inspection tool of trust e.g. wireshark and take a look on the outgoing packages.
  115. * The following graphic figures out the packages created by this tutorial.
  116. *
  117. * .. figure:: ua-wireshark-pubsub.png
  118. * :figwidth: 100 %
  119. * :alt: OPC UA PubSub communication in wireshark
  120. *
  121. * The open62541 subscriber API will be released later. If you want to process the the datagrams,
  122. * take a look on the ua_network_pubsub_networkmessage.c which already contains the decoding code for UADP messages.
  123. *
  124. * It follows the main server code, making use of the above definitions. */
  125. UA_Boolean running = true;
  126. static void stopHandler(int sign) {
  127. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  128. running = false;
  129. }
  130. int main(void) {
  131. signal(SIGINT, stopHandler);
  132. signal(SIGTERM, stopHandler);
  133. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  134. UA_ServerConfig *config = UA_ServerConfig_new_default();
  135. /* Details about the connection configuration and handling are located in the pubsub connection tutorial */
  136. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  137. if(!config->pubsubTransportLayers) {
  138. UA_ServerConfig_delete(config);
  139. return -1;
  140. }
  141. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  142. config->pubsubTransportLayersSize++;
  143. UA_Server *server = UA_Server_new(config);
  144. addPubSubConnection(server);
  145. addPublishedDataSet(server);
  146. addDataSetField(server);
  147. addWriterGroup(server);
  148. addDataSetWriter(server);
  149. retval |= UA_Server_run(server, &running);
  150. UA_Server_delete(server);
  151. UA_ServerConfig_delete(config);
  152. return (int)retval;
  153. }