tutorial_pubsub_publish.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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: This Tutorial will be continuously extended during the next
  10. * PubSub batches. More details about the PubSub extension and corresponding
  11. * 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 the UADP
  17. * encoding.
  18. *
  19. * **Connection handling**
  20. *
  21. * PubSubConnections can be created and deleted on runtime. More details about
  22. * the system preconfiguration and connection can be found in
  23. * ``tutorial_pubsub_connection.c``.
  24. */
  25. #include "open62541.h"
  26. #include <signal.h>
  27. #include <stdio.h>
  28. UA_NodeId connectionIdent, publishedDataSetIdent, writerGroupIdent;
  29. static void
  30. addPubSubConnection(UA_Server *server, UA_String *transportProfile,
  31. UA_NetworkAddressUrlDataType *networkAddressUrl){
  32. /* Details about the connection configuration and handling are located
  33. * in the pubsub connection tutorial */
  34. UA_PubSubConnectionConfig connectionConfig;
  35. memset(&connectionConfig, 0, sizeof(connectionConfig));
  36. connectionConfig.name = UA_STRING("UADP Connection 1");
  37. connectionConfig.transportProfileUri = *transportProfile;
  38. connectionConfig.enabled = UA_TRUE;
  39. UA_Variant_setScalar(&connectionConfig.address, networkAddressUrl,
  40. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  41. connectionConfig.publisherId.numeric = UA_UInt32_random();
  42. UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdent);
  43. }
  44. /**
  45. * **PublishedDataSet handling**
  46. *
  47. * The PublishedDataSet (PDS) and PubSubConnection are the toplevel entities and
  48. * can exist alone. The PDS contains the collection of the published fields. All
  49. * other PubSub elements are directly or indirectly linked with the PDS or
  50. * connection. */
  51. static void
  52. addPublishedDataSet(UA_Server *server) {
  53. /* The PublishedDataSetConfig contains all necessary public
  54. * informations for the creation of a new PublishedDataSet */
  55. UA_PublishedDataSetConfig publishedDataSetConfig;
  56. memset(&publishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  57. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  58. publishedDataSetConfig.name = UA_STRING("Demo PDS");
  59. /* Create new PublishedDataSet based on the PublishedDataSetConfig. */
  60. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  61. }
  62. /**
  63. * **DataSetField handling**
  64. *
  65. * The DataSetField (DSF) is part of the PDS and describes exactly one published
  66. * field. */
  67. static void
  68. addDataSetField(UA_Server *server) {
  69. /* Add a field to the previous created PublishedDataSet */
  70. UA_NodeId dataSetFieldIdent;
  71. UA_DataSetFieldConfig dataSetFieldConfig;
  72. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  73. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  74. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  75. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  76. dataSetFieldConfig.field.variable.publishParameters.publishedVariable =
  77. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  78. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  79. UA_Server_addDataSetField(server, publishedDataSetIdent,
  80. &dataSetFieldConfig, &dataSetFieldIdent);
  81. }
  82. /**
  83. * **WriterGroup handling**
  84. *
  85. * The WriterGroup (WG) is part of the connection and contains the primary
  86. * configuration parameters for the message creation. */
  87. static void
  88. addWriterGroup(UA_Server *server) {
  89. /* Now we create a new WriterGroupConfig and add the group to the existing
  90. * PubSubConnection. */
  91. UA_WriterGroupConfig writerGroupConfig;
  92. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  93. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  94. writerGroupConfig.publishingInterval = 100;
  95. writerGroupConfig.enabled = UA_FALSE;
  96. writerGroupConfig.writerGroupId = 100;
  97. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  98. /* The configuration flags for the messages are encapsulated inside the
  99. * message- and transport settings extension objects. These extension
  100. * objects are defined by the standard. e.g.
  101. * UadpWriterGroupMessageDataType */
  102. UA_Server_addWriterGroup(server, connectionIdent, &writerGroupConfig, &writerGroupIdent);
  103. }
  104. /**
  105. * **DataSetWriter handling**
  106. *
  107. * A DataSetWriter (DSW) is the glue between the WG and the PDS. The DSW is
  108. * linked to exactly one PDS and contains additional informations for the
  109. * message generation. */
  110. static void
  111. addDataSetWriter(UA_Server *server) {
  112. /* We need now a DataSetWriter within the WriterGroup. This means we must
  113. * create a new DataSetWriterConfig and add call the addWriterGroup function. */
  114. UA_NodeId dataSetWriterIdent;
  115. UA_DataSetWriterConfig dataSetWriterConfig;
  116. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  117. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  118. dataSetWriterConfig.dataSetWriterId = 62541;
  119. dataSetWriterConfig.keyFrameCount = 10;
  120. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  121. &dataSetWriterConfig, &dataSetWriterIdent);
  122. }
  123. /**
  124. * That's it! You're now publishing the selected fields. Open a packet
  125. * inspection tool of trust e.g. wireshark and take a look on the outgoing
  126. * packages. The following graphic figures out the packages created by this
  127. * tutorial.
  128. *
  129. * .. figure:: ua-wireshark-pubsub.png
  130. * :figwidth: 100 %
  131. * :alt: OPC UA PubSub communication in wireshark
  132. *
  133. * The open62541 subscriber API will be released later. If you want to process
  134. * the the datagrams, take a look on the ua_network_pubsub_networkmessage.c
  135. * which already contains the decoding code for UADP messages.
  136. *
  137. * It follows the main server code, making use of the above definitions. */
  138. UA_Boolean running = true;
  139. static void stopHandler(int sign) {
  140. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  141. running = false;
  142. }
  143. static int run(UA_String *transportProfile,
  144. UA_NetworkAddressUrlDataType *networkAddressUrl) {
  145. signal(SIGINT, stopHandler);
  146. signal(SIGTERM, stopHandler);
  147. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  148. UA_ServerConfig *config = UA_ServerConfig_new_default();
  149. /* Details about the connection configuration and handling are located in
  150. * the pubsub connection tutorial */
  151. config->pubsubTransportLayers =
  152. (UA_PubSubTransportLayer *) UA_calloc(2, sizeof(UA_PubSubTransportLayer));
  153. if(!config->pubsubTransportLayers) {
  154. UA_ServerConfig_delete(config);
  155. return -1;
  156. }
  157. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  158. config->pubsubTransportLayersSize++;
  159. #ifdef UA_ENABLE_PUBSUB_ETH_UADP
  160. config->pubsubTransportLayers[1] = UA_PubSubTransportLayerEthernet();
  161. config->pubsubTransportLayersSize++;
  162. #endif
  163. UA_Server *server = UA_Server_new(config);
  164. addPubSubConnection(server, transportProfile, networkAddressUrl);
  165. addPublishedDataSet(server);
  166. addDataSetField(server);
  167. addWriterGroup(server);
  168. addDataSetWriter(server);
  169. retval |= UA_Server_run(server, &running);
  170. UA_Server_delete(server);
  171. UA_ServerConfig_delete(config);
  172. return (int)retval;
  173. }
  174. static void
  175. usage(char *progname) {
  176. printf("usage: %s <uri> [device]\n", progname);
  177. }
  178. int main(int argc, char **argv) {
  179. UA_String transportProfile =
  180. UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  181. UA_NetworkAddressUrlDataType networkAddressUrl =
  182. {UA_STRING_NULL , UA_STRING("opc.udp://224.0.0.22:4840/")};
  183. if (argc > 1) {
  184. if (strcmp(argv[1], "-h") == 0) {
  185. usage(argv[0]);
  186. return 0;
  187. } else if (strncmp(argv[1], "opc.udp://", 10) == 0) {
  188. networkAddressUrl.url = UA_STRING(argv[1]);
  189. } else if (strncmp(argv[1], "opc.eth://", 10) == 0) {
  190. transportProfile =
  191. UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-eth-uadp");
  192. if (argc < 3) {
  193. printf("Error: UADP/ETH needs an interface name\n");
  194. return 1;
  195. }
  196. networkAddressUrl.networkInterface = UA_STRING(argv[2]);
  197. networkAddressUrl.url = UA_STRING(argv[1]);
  198. } else {
  199. printf("Error: unknown URI\n");
  200. return 1;
  201. }
  202. }
  203. return run(&transportProfile, &networkAddressUrl);
  204. }