tutorial_pubsub_publish.c 8.8 KB

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