tutorial_pubsub_publish.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_SERVERSTATUS_CURRENTTIME);
  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.writerGroupId = 100;
  89. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  90. /* The configuration flags for the messages are encapsulated inside the
  91. * message- and transport settings extension objects. These extension objects
  92. * are defined by the standard. e.g. UadpWriterGroupMessageDataType */
  93. UA_Server_addWriterGroup(server, connectionIdent, &writerGroupConfig, &writerGroupIdent);
  94. }
  95. /**
  96. * **DataSetWriter handling**
  97. * A DataSetWriter (DSW) is the glue between the WG and the PDS. The DSW is linked to exactly one
  98. * PDS and contains additional informations for the message generation.
  99. */
  100. static void
  101. addDataSetWriter(UA_Server *server) {
  102. /* We need now a DataSetWriter within the WriterGroup. This means we must
  103. * create a new DataSetWriterConfig and add call the addWriterGroup function. */
  104. UA_NodeId dataSetWriterIdent;
  105. UA_DataSetWriterConfig dataSetWriterConfig;
  106. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  107. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  108. dataSetWriterConfig.dataSetWriterId = 62541;
  109. dataSetWriterConfig.keyFrameCount = 10;
  110. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  111. &dataSetWriterConfig, &dataSetWriterIdent);
  112. }
  113. /**
  114. * That's it! You're now publishing the selected fields.
  115. * Open a packet inspection tool of trust e.g. wireshark and take a look on the outgoing packages.
  116. * The following graphic figures out the packages created by this tutorial.
  117. *
  118. * .. figure:: ua-wireshark-pubsub.png
  119. * :figwidth: 100 %
  120. * :alt: OPC UA PubSub communication in wireshark
  121. *
  122. * The open62541 subscriber API will be released later. If you want to process the the datagrams,
  123. * take a look on the ua_network_pubsub_networkmessage.c which already contains the decoding code for UADP messages.
  124. *
  125. * It follows the main server code, making use of the above definitions. */
  126. UA_Boolean running = true;
  127. static void stopHandler(int sign) {
  128. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  129. running = false;
  130. }
  131. int main(void) {
  132. signal(SIGINT, stopHandler);
  133. signal(SIGTERM, stopHandler);
  134. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  135. UA_ServerConfig *config = UA_ServerConfig_new_default();
  136. /* Details about the connection configuration and handling are located in the pubsub connection tutorial */
  137. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(sizeof(UA_PubSubTransportLayer));
  138. if(!config->pubsubTransportLayers) {
  139. UA_ServerConfig_delete(config);
  140. return -1;
  141. }
  142. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  143. config->pubsubTransportLayersSize++;
  144. UA_Server *server = UA_Server_new(config);
  145. addPubSubConnection(server);
  146. addPublishedDataSet(server);
  147. addDataSetField(server);
  148. addWriterGroup(server);
  149. addDataSetWriter(server);
  150. retval |= UA_Server_run(server, &running);
  151. UA_Server_delete(server);
  152. UA_ServerConfig_delete(config);
  153. return (int)retval;
  154. }