tutorial_pubsub_mqtt_publish.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 MQTT publish example demonstrate the simplest way to publish
  16. * informations from the information model over MQTT using the UADP (or later
  17. * JSON) encoding. To receive information the subscribe functionality of mqtt is
  18. * used. A periodical call to yield is necessary to update the mqtt stack.
  19. *
  20. * **Connection handling**
  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/server.h"
  26. #include "open62541/server_config_default.h"
  27. #include "ua_pubsub.h"
  28. #include "ua_network_pubsub_mqtt.h"
  29. #include "open62541/plugin/log_stdout.h"
  30. #include <signal.h>
  31. #define CONNECTION_NAME "MQTT Publisher Connection"
  32. #define TRANSPORT_PROFILE_URI "http://opcfoundation.org/UA-Profile/Transport/pubsub-mqtt"
  33. #define MQTT_CLIENT_ID "TESTCLIENTPUBSUBMQTT"
  34. #define CONNECTIONOPTION_NAME "mqttClientId"
  35. #define PUBLISHER_TOPIC "customTopic"
  36. #define PUBLISHER_METADATAQUEUENAME "MetaDataTopic"
  37. #define PUBLISHER_METADATAUPDATETIME 0
  38. #define BROKER_ADDRESS_URL "opc.mqtt://127.0.0.1:1883"
  39. #define PUBLISH_INTERVAL 500
  40. static UA_Boolean useJson = false;
  41. static UA_NodeId connectionIdent;
  42. static UA_NodeId publishedDataSetIdent;
  43. static UA_NodeId writerGroupIdent;
  44. static void
  45. addPubSubConnection(UA_Server *server, char *addressUrl) {
  46. /* Details about the connection configuration and handling are located
  47. * in the pubsub connection tutorial */
  48. UA_PubSubConnectionConfig connectionConfig;
  49. memset(&connectionConfig, 0, sizeof(connectionConfig));
  50. connectionConfig.name = UA_STRING(CONNECTION_NAME);
  51. connectionConfig.transportProfileUri = UA_STRING(TRANSPORT_PROFILE_URI);
  52. connectionConfig.enabled = UA_TRUE;
  53. /* configure address of the mqtt broker (local on default port) */
  54. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING(addressUrl)};
  55. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  56. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  57. /* Changed to static publisherId from random generation to identify
  58. * the publisher on Subscriber side */
  59. connectionConfig.publisherId.numeric = 2234;
  60. /* configure options, set mqtt client id */
  61. UA_KeyValuePair connectionOptions[1];
  62. connectionOptions[0].key = UA_QUALIFIEDNAME(0, CONNECTIONOPTION_NAME);
  63. UA_String mqttClientId = UA_STRING(MQTT_CLIENT_ID);
  64. UA_Variant_setScalar(&connectionOptions[0].value, &mqttClientId, &UA_TYPES[UA_TYPES_STRING]);
  65. connectionConfig.connectionProperties = connectionOptions;
  66. connectionConfig.connectionPropertiesSize = 1;
  67. UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdent);
  68. }
  69. /**
  70. * **PublishedDataSet handling**
  71. * The PublishedDataSet (PDS) and PubSubConnection are the toplevel entities and
  72. * can exist alone. The PDS contains the collection of the published fields. All
  73. * other PubSub elements are directly or indirectly linked with the PDS or
  74. * connection.
  75. */
  76. static void
  77. addPublishedDataSet(UA_Server *server) {
  78. /* The PublishedDataSetConfig contains all necessary public
  79. * informations for the creation of a new PublishedDataSet */
  80. UA_PublishedDataSetConfig publishedDataSetConfig;
  81. memset(&publishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  82. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  83. publishedDataSetConfig.name = UA_STRING("Demo PDS");
  84. /* Create new PublishedDataSet based on the PublishedDataSetConfig. */
  85. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  86. }
  87. /**
  88. * **DataSetField handling**
  89. * The DataSetField (DSF) is part of the PDS and describes exactly one published field.
  90. */
  91. static void
  92. addDataSetField(UA_Server *server) {
  93. /* Add a field to the previous created PublishedDataSet */
  94. UA_DataSetFieldConfig dataSetFieldConfig;
  95. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  96. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  97. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  98. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  99. dataSetFieldConfig.field.variable.publishParameters.publishedVariable =
  100. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  101. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  102. UA_Server_addDataSetField(server, publishedDataSetIdent, &dataSetFieldConfig, NULL);
  103. }
  104. /**
  105. * **WriterGroup handling**
  106. * The WriterGroup (WG) is part of the connection and contains the primary configuration
  107. * parameters for the message creation.
  108. */
  109. static void
  110. addWriterGroup(UA_Server *server, char *topic, int interval) {
  111. /* Now we create a new WriterGroupConfig and add the group to the existing PubSubConnection. */
  112. UA_WriterGroupConfig writerGroupConfig;
  113. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  114. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  115. writerGroupConfig.publishingInterval = interval;
  116. writerGroupConfig.enabled = UA_FALSE;
  117. writerGroupConfig.writerGroupId = 100;
  118. /* decide whether to use JSON or UADP encoding*/
  119. #ifdef UA_ENABLE_JSON_ENCODING
  120. if(useJson)
  121. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_JSON;
  122. else
  123. #endif
  124. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  125. writerGroupConfig.messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  126. writerGroupConfig.messageSettings.content.decoded.type = &UA_TYPES[UA_TYPES_UADPWRITERGROUPMESSAGEDATATYPE];
  127. /* The configuration flags for the messages are encapsulated inside the
  128. * message- and transport settings extension objects. These extension
  129. * objects are defined by the standard. e.g.
  130. * UadpWriterGroupMessageDataType */
  131. UA_UadpWriterGroupMessageDataType *writerGroupMessage = UA_UadpWriterGroupMessageDataType_new();
  132. /* Change message settings of writerGroup to send PublisherId,
  133. * WriterGroupId in GroupHeader and DataSetWriterId in PayloadHeader
  134. * of NetworkMessage */
  135. writerGroupMessage->networkMessageContentMask =
  136. (UA_UadpNetworkMessageContentMask)(UA_UADPNETWORKMESSAGECONTENTMASK_PUBLISHERID |
  137. (UA_UadpNetworkMessageContentMask)UA_UADPNETWORKMESSAGECONTENTMASK_GROUPHEADER |
  138. (UA_UadpNetworkMessageContentMask)UA_UADPNETWORKMESSAGECONTENTMASK_WRITERGROUPID |
  139. (UA_UadpNetworkMessageContentMask)UA_UADPNETWORKMESSAGECONTENTMASK_PAYLOADHEADER);
  140. writerGroupConfig.messageSettings.content.decoded.data = writerGroupMessage;
  141. /* configure the mqtt publish topic */
  142. UA_BrokerWriterGroupTransportDataType brokerTransportSettings;
  143. memset(&brokerTransportSettings, 0, sizeof(UA_BrokerWriterGroupTransportDataType));
  144. /* Assign the Topic at which MQTT publish should happen */
  145. /*ToDo: Pass the topic as argument from the writer group */
  146. brokerTransportSettings.queueName = UA_STRING(topic);
  147. brokerTransportSettings.resourceUri = UA_STRING_NULL;
  148. brokerTransportSettings.authenticationProfileUri = UA_STRING_NULL;
  149. /* Choose the QOS Level for MQTT */
  150. brokerTransportSettings.requestedDeliveryGuarantee = UA_BROKERTRANSPORTQUALITYOFSERVICE_BESTEFFORT;
  151. /* Encapsulate config in transportSettings */
  152. UA_ExtensionObject transportSettings;
  153. memset(&transportSettings, 0, sizeof(UA_ExtensionObject));
  154. transportSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  155. transportSettings.content.decoded.type = &UA_TYPES[UA_TYPES_BROKERWRITERGROUPTRANSPORTDATATYPE];
  156. transportSettings.content.decoded.data = &brokerTransportSettings;
  157. writerGroupConfig.transportSettings = transportSettings;
  158. UA_Server_addWriterGroup(server, connectionIdent, &writerGroupConfig, &writerGroupIdent);
  159. UA_Server_setWriterGroupOperational(server, writerGroupIdent);
  160. UA_UadpWriterGroupMessageDataType_delete(writerGroupMessage);
  161. }
  162. /**
  163. * **DataSetWriter handling**
  164. * A DataSetWriter (DSW) is the glue between the WG and the PDS. The DSW is
  165. * linked to exactly one PDS and contains additional informations for the
  166. * message generation.
  167. */
  168. static void
  169. addDataSetWriter(UA_Server *server, char *topic) {
  170. /* We need now a DataSetWriter within the WriterGroup. This means we must
  171. * create a new DataSetWriterConfig and add call the addWriterGroup function. */
  172. UA_NodeId dataSetWriterIdent;
  173. UA_DataSetWriterConfig dataSetWriterConfig;
  174. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  175. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  176. dataSetWriterConfig.dataSetWriterId = 62541;
  177. dataSetWriterConfig.keyFrameCount = 10;
  178. #ifdef UA_ENABLE_JSON_ENCODING
  179. UA_JsonDataSetWriterMessageDataType jsonDswMd;
  180. UA_ExtensionObject messageSettings;
  181. if(useJson) {
  182. /* JSON config for the dataSetWriter */
  183. jsonDswMd.dataSetMessageContentMask = (UA_JsonDataSetMessageContentMask)
  184. (UA_JSONDATASETMESSAGECONTENTMASK_DATASETWRITERID |
  185. UA_JSONDATASETMESSAGECONTENTMASK_SEQUENCENUMBER |
  186. UA_JSONDATASETMESSAGECONTENTMASK_STATUS |
  187. UA_JSONDATASETMESSAGECONTENTMASK_METADATAVERSION |
  188. UA_JSONDATASETMESSAGECONTENTMASK_TIMESTAMP);
  189. messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  190. messageSettings.content.decoded.type = &UA_TYPES[UA_TYPES_JSONDATASETWRITERMESSAGEDATATYPE];
  191. messageSettings.content.decoded.data = &jsonDswMd;
  192. dataSetWriterConfig.messageSettings = messageSettings;
  193. }
  194. #endif
  195. /*TODO: Modify MQTT send to add DataSetWriters broker transport settings */
  196. /*TODO: Pass the topic as argument from the writer group */
  197. /*TODO: Publish Metadata to metaDataQueueName */
  198. /* configure the mqtt publish topic */
  199. UA_BrokerDataSetWriterTransportDataType brokerTransportSettings;
  200. memset(&brokerTransportSettings, 0, sizeof(UA_BrokerDataSetWriterTransportDataType));
  201. /* Assign the Topic at which MQTT publish should happen */
  202. brokerTransportSettings.queueName = UA_STRING(topic);
  203. brokerTransportSettings.resourceUri = UA_STRING_NULL;
  204. brokerTransportSettings.authenticationProfileUri = UA_STRING_NULL;
  205. brokerTransportSettings.metaDataQueueName = UA_STRING(PUBLISHER_METADATAQUEUENAME);
  206. brokerTransportSettings.metaDataUpdateTime = PUBLISHER_METADATAUPDATETIME;
  207. /* Choose the QOS Level for MQTT */
  208. brokerTransportSettings.requestedDeliveryGuarantee = UA_BROKERTRANSPORTQUALITYOFSERVICE_BESTEFFORT;
  209. /* Encapsulate config in transportSettings */
  210. UA_ExtensionObject transportSettings;
  211. memset(&transportSettings, 0, sizeof(UA_ExtensionObject));
  212. transportSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  213. transportSettings.content.decoded.type = &UA_TYPES[UA_TYPES_BROKERDATASETWRITERTRANSPORTDATATYPE];
  214. transportSettings.content.decoded.data = &brokerTransportSettings;
  215. dataSetWriterConfig.transportSettings = transportSettings;
  216. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  217. &dataSetWriterConfig, &dataSetWriterIdent);
  218. }
  219. /**
  220. * That's it! You're now publishing the selected fields. Open a packet
  221. * inspection tool of trust e.g. wireshark and take a look on the outgoing
  222. * packages. The following graphic figures out the packages created by this
  223. * tutorial.
  224. *
  225. * .. figure:: ua-wireshark-pubsub.png
  226. * :figwidth: 100 %
  227. * :alt: OPC UA PubSub communication in wireshark
  228. *
  229. * The open62541 subscriber API will be released later. If you want to process
  230. * the the datagrams, take a look on the ua_network_pubsub_networkmessage.c
  231. * which already contains the decoding code for UADP messages.
  232. *
  233. * It follows the main server code, making use of the above definitions. */
  234. UA_Boolean running = true;
  235. static void stopHandler(int sign) {
  236. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  237. running = false;
  238. }
  239. static void usage(void) {
  240. printf("Usage: tutorial_pubsub_mqtt [--url <opc.mqtt://hostname:port>] "
  241. "[--topic <mqttTopic>] "
  242. "[--freq <frequency in ms> "
  243. "[--json]\n"
  244. " Defaults are:\n"
  245. " - Url: opc.mqtt://127.0.0.1:1883\n"
  246. " - Topic: customTopic\n"
  247. " - Frequency: 500\n"
  248. " - JSON: Off\n");
  249. }
  250. int main(int argc, char **argv) {
  251. signal(SIGINT, stopHandler);
  252. signal(SIGTERM, stopHandler);
  253. /* TODO: Change to secure mqtt port:8883 */
  254. char *addressUrl = BROKER_ADDRESS_URL;
  255. char *topic = PUBLISHER_TOPIC;
  256. int interval = PUBLISH_INTERVAL;
  257. /* Parse arguments */
  258. for(int argpos = 1; argpos < argc; argpos++) {
  259. if(strcmp(argv[argpos], "--help") == 0) {
  260. usage();
  261. return 0;
  262. }
  263. if(strcmp(argv[argpos], "--json") == 0) {
  264. useJson = true;
  265. continue;
  266. }
  267. if(strcmp(argv[argpos], "--url") == 0) {
  268. if(argpos + 1 == argc) {
  269. usage();
  270. return -1;
  271. }
  272. argpos++;
  273. addressUrl = argv[argpos];
  274. continue;
  275. }
  276. if(strcmp(argv[argpos], "--topic") == 0) {
  277. if(argpos + 1 == argc) {
  278. usage();
  279. return -1;
  280. }
  281. argpos++;
  282. topic = argv[argpos];
  283. continue;
  284. }
  285. if(strcmp(argv[argpos], "--freq") == 0) {
  286. if(argpos + 1 == argc) {
  287. usage();
  288. return -1;
  289. }
  290. if(sscanf(argv[argpos], "%d", &interval) != 1) {
  291. usage();
  292. return -1;
  293. }
  294. if(interval <= 10) {
  295. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  296. "Publication interval too small");
  297. return -1;
  298. }
  299. continue;
  300. }
  301. usage();
  302. return -1;
  303. }
  304. /* Set up the server config */
  305. UA_Server *server = UA_Server_new();
  306. UA_ServerConfig *config = UA_Server_getConfig(server);
  307. /* Details about the connection configuration and handling are located in
  308. * the pubsub connection tutorial */
  309. UA_ServerConfig_setDefault(config);
  310. config->pubsubTransportLayers = (UA_PubSubTransportLayer *)
  311. UA_malloc(1 * sizeof(UA_PubSubTransportLayer));
  312. if(!config->pubsubTransportLayers) {
  313. return -1;
  314. }
  315. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerMQTT();
  316. config->pubsubTransportLayersSize++;
  317. addPubSubConnection(server, addressUrl);
  318. addPublishedDataSet(server);
  319. addDataSetField(server);
  320. addWriterGroup(server, topic, interval);
  321. addDataSetWriter(server, topic);
  322. UA_PubSubConnection *connection = UA_PubSubConnection_findConnectionbyId(server, connectionIdent);
  323. if(!connection) {
  324. UA_LOG_WARNING(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  325. "Could not create a PubSubConnection");
  326. UA_Server_delete(server);
  327. return -1;
  328. }
  329. UA_Server_run(server, &running);
  330. UA_Server_delete(server);
  331. return 0;
  332. }