tutorial_pubsub_publish.c 9.0 KB

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