tutorial_pubsub_mqtt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. /* server */
  26. #include "open62541/server.h"
  27. #include "open62541/server_config_default.h"
  28. /* PubSubConnection */
  29. #include "ua_pubsub.h"
  30. /* UA_PubSubTransportLayerMQTT */
  31. #include "ua_network_pubsub_mqtt.h"
  32. /* Logging */
  33. #include "open62541/plugin/log_stdout.h"
  34. /* uncomment to use json encoding */
  35. //#define USE_JSON_ENCODING
  36. /* global ids */
  37. static UA_NodeId connectionIdent, publishedDataSetIdent, writerGroupIdent;
  38. static void
  39. addPubSubConnection(UA_Server *server){
  40. /* Details about the connection configuration and handling are located
  41. * in the pubsub connection tutorial */
  42. UA_PubSubConnectionConfig connectionConfig;
  43. memset(&connectionConfig, 0, sizeof(connectionConfig));
  44. connectionConfig.name = UA_STRING("MQTT Connection 1");
  45. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-mqtt");
  46. connectionConfig.enabled = UA_TRUE;
  47. /* configure address of the mqtt broker (local on default port) */
  48. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL , UA_STRING("opc.mqtt://127.0.0.1:1883/")};
  49. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  50. connectionConfig.publisherId.numeric = UA_UInt32_random();
  51. /* configure options, set mqtt client id */
  52. UA_KeyValuePair connectionOptions[1];
  53. connectionOptions[0].key = UA_QUALIFIEDNAME(0, "mqttClientId");
  54. UA_String mqttClientId = UA_STRING("TESTCLIENTPUBSUBMQTT");
  55. UA_Variant_setScalar(&connectionOptions[0].value, &mqttClientId, &UA_TYPES[UA_TYPES_STRING]);
  56. connectionConfig.connectionProperties = connectionOptions;
  57. connectionConfig.connectionPropertiesSize = 1;
  58. UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdent);
  59. }
  60. /**
  61. * **PublishedDataSet handling**
  62. * The PublishedDataSet (PDS) and PubSubConnection are the toplevel entities and can exist alone. The PDS contains
  63. * the collection of the published fields.
  64. * All other PubSub elements are directly or indirectly linked with the PDS or connection.
  65. */
  66. static void
  67. addPublishedDataSet(UA_Server *server) {
  68. /* The PublishedDataSetConfig contains all necessary public
  69. * informations for the creation of a new PublishedDataSet */
  70. UA_PublishedDataSetConfig publishedDataSetConfig;
  71. memset(&publishedDataSetConfig, 0, sizeof(UA_PublishedDataSetConfig));
  72. publishedDataSetConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  73. publishedDataSetConfig.name = UA_STRING("Demo PDS");
  74. /* Create new PublishedDataSet based on the PublishedDataSetConfig. */
  75. UA_Server_addPublishedDataSet(server, &publishedDataSetConfig, &publishedDataSetIdent);
  76. }
  77. static void
  78. addVariable(UA_Server *server) {
  79. // Define the attribute of the myInteger variable node
  80. UA_VariableAttributes attr = UA_VariableAttributes_default;
  81. UA_Int32 myInteger = 42;
  82. UA_Variant_setScalar(&attr.value, &myInteger, &UA_TYPES[UA_TYPES_INT32]);
  83. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  84. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  85. attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  86. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE;
  87. // Add the variable node to the information model
  88. UA_Server_addVariableNode(server, UA_NODEID_NUMERIC(1, 42), UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  89. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "the answer"),
  90. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), attr, NULL, NULL);
  91. }
  92. /**
  93. * **DataSetField handling**
  94. * The DataSetField (DSF) is part of the PDS and describes exactly one published field.
  95. */
  96. static void
  97. addDataSetField(UA_Server *server) {
  98. /* Add a field to the previous created PublishedDataSet */
  99. UA_DataSetFieldConfig dataSetFieldConfig;
  100. memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
  101. dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  102. dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
  103. dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
  104. dataSetFieldConfig.field.variable.publishParameters.publishedVariable =
  105. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  106. dataSetFieldConfig.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  107. UA_Server_addDataSetField(server, publishedDataSetIdent, &dataSetFieldConfig, NULL);
  108. UA_DataSetFieldConfig dataSetFieldConfig2;
  109. memset(&dataSetFieldConfig2, 0, sizeof(UA_DataSetFieldConfig));
  110. dataSetFieldConfig2.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
  111. dataSetFieldConfig2.field.variable.fieldNameAlias = UA_STRING("Test");
  112. dataSetFieldConfig2.field.variable.promotedField = UA_FALSE;
  113. dataSetFieldConfig2.field.variable.publishParameters.publishedVariable = UA_NODEID_NUMERIC(1, 42);
  114. dataSetFieldConfig2.field.variable.publishParameters.attributeId = UA_ATTRIBUTEID_VALUE;
  115. UA_Server_addDataSetField(server, publishedDataSetIdent, &dataSetFieldConfig2, NULL);
  116. }
  117. /**
  118. * **WriterGroup handling**
  119. * The WriterGroup (WG) is part of the connection and contains the primary configuration
  120. * parameters for the message creation.
  121. */
  122. static void
  123. addWriterGroup(UA_Server *server) {
  124. /* Now we create a new WriterGroupConfig and add the group to the existing PubSubConnection. */
  125. UA_WriterGroupConfig writerGroupConfig;
  126. memset(&writerGroupConfig, 0, sizeof(UA_WriterGroupConfig));
  127. writerGroupConfig.name = UA_STRING("Demo WriterGroup");
  128. writerGroupConfig.publishingInterval = 500;
  129. writerGroupConfig.enabled = UA_FALSE;
  130. writerGroupConfig.writerGroupId = 100;
  131. /* decide whether to use JSON or UADP encoding*/
  132. #if defined(USE_JSON_ENCODING) && defined(UA_ENABLE_JSON_ENCODING)
  133. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_JSON;
  134. #else
  135. writerGroupConfig.encodingMimeType = UA_PUBSUB_ENCODING_UADP;
  136. #endif
  137. /* configure the mqtt publish topic */
  138. UA_BrokerWriterGroupTransportDataType brokerTransportSettings;
  139. memset(&brokerTransportSettings, 0, sizeof(UA_BrokerWriterGroupTransportDataType));
  140. brokerTransportSettings.queueName = UA_STRING("customTopic");
  141. brokerTransportSettings.resourceUri = UA_STRING_NULL;
  142. brokerTransportSettings.authenticationProfileUri = UA_STRING_NULL;
  143. /* Choose the QOS Level for MQTT */
  144. brokerTransportSettings.requestedDeliveryGuarantee = UA_BROKERTRANSPORTQUALITYOFSERVICE_BESTEFFORT;
  145. /* Encapsulate config in transportSettings */
  146. UA_ExtensionObject transportSettings;
  147. memset(&transportSettings, 0, sizeof(UA_ExtensionObject));
  148. transportSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  149. transportSettings.content.decoded.type = &UA_TYPES[UA_TYPES_BROKERWRITERGROUPTRANSPORTDATATYPE];
  150. transportSettings.content.decoded.data = &brokerTransportSettings;
  151. writerGroupConfig.transportSettings = transportSettings;
  152. UA_Server_addWriterGroup(server, connectionIdent, &writerGroupConfig, &writerGroupIdent);
  153. }
  154. /**
  155. * **DataSetWriter handling**
  156. * A DataSetWriter (DSW) is the glue between the WG and the PDS. The DSW is linked to exactly one
  157. * PDS and contains additional informations for the message generation.
  158. */
  159. static void
  160. addDataSetWriter(UA_Server *server) {
  161. /* We need now a DataSetWriter within the WriterGroup. This means we must
  162. * create a new DataSetWriterConfig and add call the addWriterGroup function. */
  163. UA_NodeId dataSetWriterIdent;
  164. UA_DataSetWriterConfig dataSetWriterConfig;
  165. memset(&dataSetWriterConfig, 0, sizeof(UA_DataSetWriterConfig));
  166. dataSetWriterConfig.name = UA_STRING("Demo DataSetWriter");
  167. dataSetWriterConfig.dataSetWriterId = 62541;
  168. dataSetWriterConfig.keyFrameCount = 10;
  169. #if defined(USE_JSON_ENCODING) && defined(UA_ENABLE_JSON_ENCODING)
  170. /* JSON config for the dataSetWriter */
  171. UA_JsonDataSetWriterMessageDataType jsonDswMd;
  172. jsonDswMd.dataSetMessageContentMask = (UA_JsonDataSetMessageContentMask)
  173. (UA_JSONDATASETMESSAGECONTENTMASK_DATASETWRITERID
  174. | UA_JSONDATASETMESSAGECONTENTMASK_SEQUENCENUMBER
  175. | UA_JSONDATASETMESSAGECONTENTMASK_STATUS
  176. | UA_JSONDATASETMESSAGECONTENTMASK_METADATAVERSION
  177. | UA_JSONDATASETMESSAGECONTENTMASK_TIMESTAMP);
  178. UA_ExtensionObject messageSettings;
  179. messageSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  180. messageSettings.content.decoded.type = &UA_TYPES[UA_TYPES_JSONDATASETWRITERMESSAGEDATATYPE];
  181. messageSettings.content.decoded.data = &jsonDswMd;
  182. dataSetWriterConfig.messageSettings = messageSettings;
  183. #endif
  184. UA_Server_addDataSetWriter(server, writerGroupIdent, publishedDataSetIdent,
  185. &dataSetWriterConfig, &dataSetWriterIdent);
  186. }
  187. /**
  188. * That's it! You're now publishing the selected fields.
  189. * Open a packet inspection tool of trust e.g. wireshark and take a look on the outgoing packages.
  190. * The following graphic figures out the packages created by this tutorial.
  191. *
  192. * .. figure:: ua-wireshark-pubsub.png
  193. * :figwidth: 100 %
  194. * :alt: OPC UA PubSub communication in wireshark
  195. *
  196. * The open62541 subscriber API will be released later. If you want to process the the datagrams,
  197. * take a look on the ua_network_pubsub_networkmessage.c which already contains the decoding code for UADP messages.
  198. *
  199. * It follows the main server code, making use of the above definitions. */
  200. UA_Boolean running = true;
  201. static void stopHandler(int sign) {
  202. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  203. running = false;
  204. }
  205. static void callback(UA_ByteString *encodedBuffer, UA_ByteString *topic){
  206. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "PubSub MQTT: Message Received");
  207. /* For example try to decode as a Json Networkmessage...
  208. UA_NetworkMessage dst;
  209. UA_StatusCode ret = UA_NetworkMessage_decodeJson(&dst, encodedBuffer);
  210. if( ret == UA_STATUSCODE_GOOD){
  211. }
  212. */
  213. UA_ByteString_delete(encodedBuffer);
  214. UA_ByteString_delete(topic);
  215. //UA_NetworkMessage_deleteMembers(&dst);
  216. }
  217. /* Adds a subscription */
  218. static void
  219. addSubscription(UA_Server *server, UA_PubSubConnection *connection){
  220. /* transportSettings for subscription! */
  221. UA_BrokerWriterGroupTransportDataType brokerTransportSettings;
  222. memset(&brokerTransportSettings, 0, sizeof(UA_BrokerWriterGroupTransportDataType));
  223. brokerTransportSettings.queueName = UA_STRING("customTopic");
  224. brokerTransportSettings.resourceUri = UA_STRING_NULL;
  225. brokerTransportSettings.authenticationProfileUri = UA_STRING_NULL;
  226. /* QOS */
  227. brokerTransportSettings.requestedDeliveryGuarantee = UA_BROKERTRANSPORTQUALITYOFSERVICE_BESTEFFORT;
  228. UA_ExtensionObject transportSettings;
  229. memset(&transportSettings, 0, sizeof(UA_ExtensionObject));
  230. transportSettings.encoding = UA_EXTENSIONOBJECT_DECODED;
  231. transportSettings.content.decoded.type = &UA_TYPES[UA_TYPES_BROKERWRITERGROUPTRANSPORTDATATYPE];
  232. transportSettings.content.decoded.data = &brokerTransportSettings;
  233. connection->channel->regist(connection->channel, &transportSettings, &callback);
  234. return;
  235. }
  236. int main(void) {
  237. signal(SIGINT, stopHandler);
  238. signal(SIGTERM, stopHandler);
  239. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  240. UA_Server *server = UA_Server_new();
  241. UA_ServerConfig *config = UA_Server_getConfig(server);
  242. UA_ServerConfig_setDefault(config);
  243. /* Details about the connection configuration and handling are located in the pubsub connection tutorial */
  244. config->pubsubTransportLayers = (UA_PubSubTransportLayer *) UA_malloc(1 * sizeof(UA_PubSubTransportLayer));
  245. if(!config->pubsubTransportLayers) {
  246. return -1;
  247. }
  248. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerMQTT();
  249. config->pubsubTransportLayersSize++;
  250. addVariable(server);
  251. addPubSubConnection(server);
  252. addPublishedDataSet(server);
  253. addDataSetField(server);
  254. addWriterGroup(server);
  255. addDataSetWriter(server);
  256. UA_PubSubConnection *connection =
  257. UA_PubSubConnection_findConnectionbyId(server, connectionIdent);
  258. if(connection != NULL) {
  259. addSubscription(server, connection);
  260. }
  261. retval |= UA_Server_run(server, &running);
  262. UA_Server_delete(server);
  263. return (int)retval;
  264. }