tutorial_pubsub_publish.c 10 KB

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