tutorial_pubsub_mqtt.c 15 KB

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