]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_IoT_Libraries/mqtt/DemoTasks/SimpleMQTTExamples.c
Update libraries and sundry check-ins ready for the V10.3.0 kernel release.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_IoT_Libraries / mqtt / DemoTasks / SimpleMQTTExamples.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /* Standard inclues. */\r
29 #include <string.h>\r
30 #include <stdio.h>\r
31 \r
32 /* Kernel includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 \r
36 /* IoT SDK includes. */\r
37 #include "iot_mqtt.h"\r
38 #include "iot_taskpool.h"\r
39 #include "platform/iot_network_freertos.h"\r
40 \r
41 /**\r
42  * @brief The keep-alive interval used for this example.\r
43  *\r
44  * An MQTT ping request will be sent periodically at this interval.\r
45  */\r
46 #define mqttexampleKEEP_ALIVE_SECONDS           ( 60 )\r
47 \r
48 /**\r
49  * @brief The timeout for MQTT operations in this example.\r
50  */\r
51 #define mqttexampleMQTT_TIMEOUT_MS                      ( 50000 )\r
52 \r
53 /**\r
54  * @brief The MQTT client identifier used in this example.\r
55  */\r
56 #define mqttexampleCLIENT_IDENTIFIER            "mqttexampleclient"\r
57 \r
58 const char *pcClientIdentifiers[] = { "AAA" };//, "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH", "III", "JJJ" };\r
59 \r
60 /**\r
61  * @brief Details of the MQTT broker to connect to.\r
62  *\r
63  * @note This example does not use TLS and therefore won't work with AWS IoT.\r
64  *\r
65  */\r
66 #define mqttexampleMQTT_BROKER_ENDPOINT         "test.mosquitto.org"\r
67 #define mqttexampleMQTT_BROKER_PORT                     1883\r
68 \r
69 /**\r
70  * @brief The topic to subscribe and publish to in the example.\r
71  */\r
72 #define mqttexampleTOPIC                                        "example/topic"\r
73 \r
74 /**\r
75  * @brief The MQTT message published in this example.\r
76  */\r
77 #define mqttexampleMESSAGE                                      "Hello World!"\r
78 \r
79 /**\r
80  * @brief Paramters to control the retry behaviour in case a QoS1 publish\r
81  * message gets lost.\r
82  *\r
83  * Retry every minutes up to a maximum of 5 retries.\r
84  */\r
85 #define mqttexamplePUBLISH_RETRY_MS                     ( 1000 )\r
86 #define mqttexamplePUBLISH_RETRY_LIMIT          ( 5 )\r
87 \r
88 /**\r
89  * @brief The bit which is set in the demo task's notification value from the\r
90  * disconnect callback to inform the demo task about the MQTT disconnect.\r
91  */\r
92 #define mqttexampleDISCONNECTED_BIT                     ( 1UL << 0UL )\r
93 \r
94 /**\r
95  * @brief The bit which is set in the demo task's notification value from the\r
96  * publish callback to inform the demo task about the message received from the\r
97  * MQTT broker.\r
98  */\r
99 #define mqttexampleMESSAGE_RECEIVED_BIT         ( 1UL << 1UL )\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /**\r
103  * @brief Parameters used to create the system task pool.\r
104  */\r
105 static const IotTaskPoolInfo_t xTaskPoolParameters = {\r
106                                                                                                                 /* Minimum number of threads in a task pool.\r
107                                                                                                                  * Note the slimmed down version of the task\r
108                                                                                                                  * pool used by this library does not autoscale\r
109                                                                                                                  * the number of tasks in the pool so in this\r
110                                                                                                                  * case this sets the number of tasks in the\r
111                                                                                                                  * pool. */\r
112                                                                                                                 2,\r
113                                                                                                                 /* Maximum number of threads in a task pool.\r
114                                                                                                                  * Note the slimmed down version of the task\r
115                                                                                                                  * pool used by this library does not autoscale\r
116                                                                                                                  * the number of tasks in the pool so in this\r
117                                                                                                                  * case this parameter is just ignored. */\r
118                                                                                                                 2,\r
119                                                                                                                 /* Stack size for every task pool thread - in\r
120                                                                                                                  * bytes, hence multiplying by the number of bytes\r
121                                                                                                                  * in a word as configMINIMAL_STACK_SIZE is\r
122                                                                                                                  * specified in words. */\r
123                                                                                                                 configMINIMAL_STACK_SIZE * sizeof( portSTACK_TYPE ),\r
124                                                                                                                 /* Priority for every task pool thread. */\r
125                                                                                                                 tskIDLE_PRIORITY,\r
126                                                                                                          };\r
127 /*-----------------------------------------------------------*/\r
128 \r
129 /**\r
130  * @brief The task used to demonstrate the MQTT API.\r
131  *\r
132  * @param[in] pvParameters Parmaters as passed at the time of task creation. Not\r
133  * used in this example.\r
134  */\r
135 static void prvMQTTDemoTask( void *pvParameters );\r
136 \r
137 /**\r
138  * @brief The callback invoked by the MQTT library when the MQTT connection gets\r
139  * disconnected.\r
140  *\r
141  * @param[in] pvCallbackContext Callback context as provided at the time of\r
142  * connect.\r
143  * @param[in] pxCallbackParams Contains the reason why the MQTT connection was\r
144  * disconnected.\r
145  */\r
146 static void prvExample_OnDisconnect( void * pvCallbackContext,\r
147                                                                          IotMqttCallbackParam_t * pxCallbackParams );\r
148 \r
149 /**\r
150  * @brief The callback invoked by the MQTT library when a message is received on\r
151  * a subscribed topic from the MQTT broker.\r
152  *\r
153  * @param[in] pvCallbackContext Callback context as provided at the time of\r
154  * subscribe.\r
155  * @param[in] pxCallbackParams Contain the details about the received message -\r
156  * topic on which the message was received, the received message.\r
157  */\r
158 static void prvExample_OnMessageReceived( void * pvCallbackContext,\r
159                                                                                   IotMqttCallbackParam_t * pxCallbackParams );\r
160 \r
161 /**\r
162  * @brief Connects to the MQTT broker as specified in mqttexampleMQTT_BROKER_ENDPOINT\r
163  * and mqttexampleMQTT_BROKER_PORT.\r
164  *\r
165  * @note This example does not use TLS and therefore will not work with MQTT.\r
166  */\r
167 static void prvMQTTConnect( IotMqttConnection_t *xMQTTConnection, const char *pcClientID );\r
168 \r
169 /**\r
170  * @brief Subscribes to pcTopicString.\r
171  */\r
172 static void prvMQTTSubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
173 \r
174 /**\r
175  * @brief Publishes a messages mqttexampleMESSAGE on mqttexampleTOPIC topic.\r
176  */\r
177 static void prvMQTTPublish( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
178 \r
179 /**\r
180  * @brief Unsubscribes from the mqttexampleTOPIC topic.\r
181  */\r
182 static void prvMQTTUnsubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
183 \r
184 /**\r
185  * @brief Disconnects from the MQTT broker gracefully by sending an MQTT\r
186  * DISCONNECT message.\r
187  */\r
188 static void prvMQTTDisconnect( IotMqttConnection_t xMQTTConnection );\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvExample_OnDisconnect( void * pvCallbackContext,\r
192                                                                                    IotMqttCallbackParam_t * pxCallbackParams )\r
193 {\r
194 TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;\r
195 \r
196         /* Ensure that we initiated the disconnect. */\r
197         configASSERT( pxCallbackParams->u.disconnectReason == IOT_MQTT_DISCONNECT_CALLED );\r
198 \r
199         /* Inform the demo task about the disconnect. */\r
200         xTaskNotify( xDemoTaskHandle,\r
201                                 mqttexampleDISCONNECTED_BIT,\r
202                                 eSetBits /* Set the mqttexampleDISCONNECTED_BIT in the demo task's notification value. */\r
203                                 );\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void prvExample_OnMessageReceived( void * pvCallbackContext,\r
208                                                                                 IotMqttCallbackParam_t * pxCallbackParams )\r
209 {\r
210 TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;\r
211 \r
212         /* Ensure that the message is received on the expected topic. */\r
213 //      configASSERT( pxCallbackParams->u.message.info.topicNameLength == strlen( mqttexampleTOPIC ) );\r
214 //      configASSERT( strncmp( pxCallbackParams->u.message.info.pTopicName,\r
215 //                                                 mqttexampleTOPIC,\r
216 //                                                 strlen( mqttexampleTOPIC ) ) == 0 );\r
217 \r
218         /* Ensure that the expected message is received. */\r
219         configASSERT( pxCallbackParams->u.message.info.payloadLength == strlen( mqttexampleMESSAGE ) );\r
220         configASSERT( strncmp( pxCallbackParams->u.message.info.pPayload,\r
221                                                    mqttexampleMESSAGE,\r
222                                                    strlen( mqttexampleMESSAGE ) ) == 0 );\r
223 \r
224         /* Ensure that the message QoS is as expected. */\r
225         configASSERT( pxCallbackParams->u.message.info.qos == IOT_MQTT_QOS_1 );\r
226 \r
227         /* Although this print uses the constants rather than the data from the\r
228          * message payload the asserts above have already checked the message\r
229          * payload equals the constants, and it is more efficient not to have to\r
230          * worry about lengths in the print. */\r
231         configPRINTF( ( "Received %s on the topic %s\r\n", mqttexampleMESSAGE, mqttexampleTOPIC ) );\r
232 \r
233         /* Inform the demo task about the message received from the MQTT broker. */\r
234         xTaskNotify( xDemoTaskHandle,\r
235                                  mqttexampleMESSAGE_RECEIVED_BIT,\r
236                                  eSetBits /* Set the mqttexampleMESSAGE_RECEIVED_BIT in the demo task's notification value. */\r
237                                 );\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 void vStartSimpleMQTTDemo( void )\r
242 {\r
243 uint32_t x;\r
244 const uint32_t ulMax_x = sizeof( pcClientIdentifiers ) / sizeof( char * );\r
245 \r
246         /* This example uses a single application task, which in turn is used to\r
247          * connect, subscribe, publish, unsubscribe and disconnect from the MQTT\r
248          * broker. */\r
249 for( x = 0; x < ulMax_x; x++ )\r
250 {\r
251         xTaskCreate( prvMQTTDemoTask,                   /* Function that implements the task. */\r
252                                  "MQTTDemo",                            /* Text name for the task - only used for debugging. */\r
253                                  configMINIMAL_STACK_SIZE,      /* Size of stack (in words, not bytes) to allocate for the task. */\r
254                                  ( void * ) x,                                          /* Task parameter - not used in this case. */\r
255                                  tskIDLE_PRIORITY,                      /* Task priority, must be between 0 and configMAX_PRIORITIES - 1. */\r
256                                  NULL );                                        /* Used to pass out a handle to the created task - not used in this case. */\r
257 }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void prvMQTTDemoTask( void *pvParameters )\r
262 {\r
263 IotMqttError_t xResult;\r
264 uint32_t ulNotificationValue = 0, ulPublishCount;\r
265 uint32_t ulMaxPublishCount = 0UL;\r
266 const TickType_t xNoDelay = ( TickType_t ) 1;\r
267 IotMqttConnection_t xMQTTConnection = IOT_MQTT_CONNECTION_INITIALIZER;\r
268 uint32_t ulTaskNumber = ( uint32_t ) pvParameters, x;\r
269 char cTopicString[ sizeof( mqttexampleTOPIC ) + 5 ];//_RB_ Access by other tasks so must be persistant and will cause memory faults on memory protected systems.\r
270 #pragma message ("Access by other tasks so must be persistant and will cause memory faults on memory protected systems.")\r
271 \r
272         /* Remove compiler warnings about unused parameters. */\r
273         ( void ) pvParameters;\r
274 \r
275         /* The MQTT library needs a task pool, so create the system task pool. */\r
276         xResult = IotTaskPool_CreateSystemTaskPool( &( xTaskPoolParameters ) );\r
277         configASSERT( xResult == IOT_TASKPOOL_SUCCESS );\r
278 \r
279         /* MQTT library must be initialized before it can be used. This is just one\r
280          * time initialization. */\r
281         xResult = IotMqtt_Init();\r
282         configASSERT( xResult == IOT_MQTT_SUCCESS );\r
283 \r
284         /* Create a topic string that is unique to the MQTT connection created by\r
285         this task. */\r
286         snprintf( cTopicString, sizeof( cTopicString ), "%s/%s", mqttexampleTOPIC, pcClientIdentifiers[ ulTaskNumber ] );\r
287 \r
288         for( ; ; )\r
289         {\r
290                 /* Don't expect any notifications to be pending yet. */\r
291                 ulNotificationValue = ulTaskNotifyTake( pdTRUE, xNoDelay );\r
292                 configASSERT( ulNotificationValue == 0 );\r
293 \r
294 \r
295                 /****************************** Connect. ******************************/\r
296 \r
297                 /* Establish a connection to the MQTT broker. This example connects to\r
298                  * the MQTT broker as specified in mqttexampleMQTT_BROKER_ENDPOINT and\r
299                  * mqttexampleMQTT_BROKER_PORT at the top of this file. Please change\r
300                  * it to the MQTT broker you want to connect to. Note that this example\r
301                  * does not use TLS and therefore will not work with AWS IoT. */\r
302                 prvMQTTConnect( &xMQTTConnection, pcClientIdentifiers[ ulTaskNumber ] );\r
303                 configPRINTF( ( "Connected to %s\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );\r
304 \r
305 \r
306                 /**************************** Subscribe. ******************************/\r
307 \r
308                 /* The client is now connected to the broker. Subscribe to the topic\r
309                  * as specified in mqttexampleTOPIC at the top of this file.  This\r
310                  * client will then publish to the same topic it subscribed to, so will\r
311                  * expect all the messages it sends to the broker to be sent back to it\r
312                  * from the broker. */\r
313                 prvMQTTSubscribe( xMQTTConnection, cTopicString );\r
314                 configPRINTF( ( "Subscribed to the topic %s\r\n", cTopicString ) );\r
315 \r
316 \r
317                 /*********************** Publish 5 messages. **************************/\r
318 \r
319                 /* Publish a few messages while connected. */\r
320                 for( x = 0; x < ( ulTaskNumber + 1UL ); x++ )\r
321                 {\r
322                         ulMaxPublishCount = uxRand();\r
323                 }\r
324 \r
325                 /* Cap ulMaxPublishCount but ensure it is not zero. */\r
326                 ulMaxPublishCount %= 10UL;\r
327                 ulMaxPublishCount++;\r
328 \r
329                 for( ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++ )\r
330                 {\r
331                         /* Publish a message on the mqttexampleTOPIC topic as specified at\r
332                          * the top of this file. */\r
333                         prvMQTTPublish( xMQTTConnection, cTopicString );\r
334                         configPRINTF( ( "Published %s on the topic %s\r\n", mqttexampleMESSAGE, cTopicString ) );\r
335 \r
336                         /* Since we are subscribed to the same topic as we published on, we\r
337                          * will get the same message back from the MQTT broker. Wait for the\r
338                          * message to be received which is informed to us by the publish\r
339                          * callback (prvExample_OnMessageReceived) by setting the\r
340                          * mqttexampleMESSAGE_RECEIVED_BIT in this task's notification\r
341                          * value. Note that the bit is cleared in the task's notification\r
342                          * value to ensure that it is ready for next message. */\r
343                         xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
344                                                          mqttexampleMESSAGE_RECEIVED_BIT, /* Clear bit on exit. */\r
345                                                          &( ulNotificationValue ), /* Obtain the notification value. */\r
346                                                          pdMS_TO_TICKS( mqttexampleMQTT_TIMEOUT_MS ) );\r
347                         configASSERT( ( ulNotificationValue & mqttexampleMESSAGE_RECEIVED_BIT ) == mqttexampleMESSAGE_RECEIVED_BIT );\r
348                 }\r
349 \r
350 \r
351                 /******************* Unsubscribe and Disconnect. **********************/\r
352 \r
353                 /* Unsubscribe from the topic mqttexampleTOPIC and disconnect\r
354                  * gracefully. */\r
355                 prvMQTTUnsubscribe( xMQTTConnection, cTopicString );\r
356                 prvMQTTDisconnect( xMQTTConnection );\r
357                 configPRINTF( ( "Disconnected from %s\r\n\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );\r
358 \r
359                 /* Wait for the disconnect operation to complete which is informed to us\r
360                  * by the disconnect callback (prvExample_OnDisconnect)by setting\r
361                  * the mqttexampleDISCONNECTED_BIT in this task's notification value.\r
362                  * Note that the bit is cleared in the task's notification value to\r
363                  * ensure that it is ready for the next run. */\r
364                 xTaskNotifyWait( 0UL, /* Don't clear any bits on entry. */\r
365                                                  mqttexampleDISCONNECTED_BIT, /* Clear bit on exit. */\r
366                                                  &( ulNotificationValue ), /* Obtain the notification value. */\r
367                                                  pdMS_TO_TICKS( mqttexampleMQTT_TIMEOUT_MS ) );\r
368                 configASSERT( ( ulNotificationValue & mqttexampleDISCONNECTED_BIT ) == mqttexampleDISCONNECTED_BIT );\r
369 \r
370                 /* Wait for some time between two iterations to ensure that we do not\r
371                  * bombard the public test mosquitto broker. */\r
372                 configPRINTF( ( "prvMQTTDemoTask() completed an iteration without hitting an assert. Total free heap is %u\r\n\r\n", xPortGetFreeHeapSize() ) );\r
373 //              vTaskDelay( pdMS_TO_TICKS( 5000 ) );\r
374         }\r
375 }\r
376 /*-----------------------------------------------------------*/\r
377 \r
378 static void prvMQTTConnect( IotMqttConnection_t *xMQTTConnection, const char *pcClientID )\r
379 {\r
380 IotMqttError_t xResult;\r
381 IotNetworkServerInfo_t xMQTTBrokerInfo;\r
382 IotMqttNetworkInfo_t xNetworkInfo = IOT_MQTT_NETWORK_INFO_INITIALIZER;\r
383 IotMqttConnectInfo_t xConnectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;\r
384 static char c[ 10 ];\r
385 static int id = 0;\r
386 \r
387         /******************* Broker information setup. **********************/\r
388 \r
389         xMQTTBrokerInfo.pHostName = mqttexampleMQTT_BROKER_ENDPOINT;\r
390         xMQTTBrokerInfo.port = mqttexampleMQTT_BROKER_PORT;\r
391 \r
392 \r
393         /******************* Network information setup. **********************/\r
394 \r
395         /* No connection to the MQTT broker has been established yet and we want to\r
396          * establish a new connection. */\r
397         xNetworkInfo.createNetworkConnection = true;\r
398         xNetworkInfo.u.setup.pNetworkServerInfo = &( xMQTTBrokerInfo );\r
399 \r
400         /* This example does not use TLS and therefore pNetworkCredentialInfo must\r
401          * be set to NULL. */\r
402         xNetworkInfo.u.setup.pNetworkCredentialInfo = NULL;\r
403 \r
404         /* Use FreeRTOS+TCP network. */\r
405         xNetworkInfo.pNetworkInterface = IOT_NETWORK_INTERFACE_FREERTOS;\r
406 \r
407         /* Setup the callback which is called when the MQTT connection is\r
408          * disconnected. The task handle is passed as the callback context which\r
409          * is used by the callback to send a task notification to this task.*/\r
410         xNetworkInfo.disconnectCallback.pCallbackContext = ( void * ) xTaskGetCurrentTaskHandle();\r
411         xNetworkInfo.disconnectCallback.function = prvExample_OnDisconnect;\r
412 \r
413 \r
414         /****************** MQTT Connection information setup. ********************/\r
415 \r
416         /* Set this flag to true if connecting to the AWS IoT MQTT broker. This\r
417          * example does not use TLS and therefore won't work with AWS IoT. */\r
418         xConnectInfo.awsIotMqttMode = false;\r
419 \r
420         /* Start with a clean session i.e. direct the MQTT broker to discard any\r
421          * previous session data. Also, establishing a connection with clean session\r
422          * will ensure that the broker does not store any data when this client\r
423          * gets disconnected. */\r
424         xConnectInfo.cleanSession = true;\r
425 \r
426         /* Since we are starting with a clean session, there are no previous\r
427          * subscriptions to be restored. */\r
428         xConnectInfo.pPreviousSubscriptions = NULL;\r
429         xConnectInfo.previousSubscriptionCount = 0;\r
430 \r
431         /* We do not want to publish Last Will and Testament (LWT) message if the\r
432          * client gets disconnected. */\r
433         xConnectInfo.pWillInfo = NULL;\r
434 \r
435         /* Send an MQTT PING request every minute to keep the connection open if\r
436         there is no other MQTT traffic. */\r
437         xConnectInfo.keepAliveSeconds = mqttexampleKEEP_ALIVE_SECONDS;\r
438 \r
439         /* The client identifier is used to uniquely identify this MQTT client to\r
440          * the MQTT broker.  In a production device the identifier can be something\r
441          * unique, such as a device serial number. */\r
442         xConnectInfo.pClientIdentifier = pcClientID;\r
443         xConnectInfo.clientIdentifierLength = ( uint16_t ) strlen( pcClientID );\r
444 \r
445         /* This example does not use any authentication and therefore username and\r
446          * password fields are not used. */\r
447         xConnectInfo.pUserName = NULL;\r
448         xConnectInfo.userNameLength = 0;\r
449         xConnectInfo.pPassword = NULL;\r
450         xConnectInfo.passwordLength = 0;\r
451 \r
452         /* Establish the connection to the MQTT broker - It is a blocking call and\r
453         will return only when connection is complete or a timeout occurs. */\r
454         xResult = IotMqtt_Connect( &( xNetworkInfo ),\r
455                                                            &( xConnectInfo ),\r
456                                                            mqttexampleMQTT_TIMEOUT_MS,\r
457                                                            xMQTTConnection );\r
458         configASSERT( xResult == IOT_MQTT_SUCCESS );\r
459 }\r
460 /*-----------------------------------------------------------*/\r
461 \r
462 static void prvMQTTSubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
463 {\r
464 IotMqttError_t xResult;\r
465 IotMqttSubscription_t xMQTTSubscription;\r
466 \r
467         /* Subscribe to the mqttexampleTOPIC topic filter. The task handle is passed\r
468          * as the callback context which is used by the callback to send a task\r
469          * notification to this task.*/\r
470         xMQTTSubscription.qos = IOT_MQTT_QOS_1;\r
471         xMQTTSubscription.pTopicFilter = pcTopicString;\r
472         xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( pcTopicString );\r
473         xMQTTSubscription.callback.pCallbackContext = ( void * ) xTaskGetCurrentTaskHandle();\r
474         xMQTTSubscription.callback.function = prvExample_OnMessageReceived;\r
475 \r
476         /* Use the synchronous API to subscribe - It is a blocking call and only\r
477          * returns when the subscribe operation is complete or a timeout occurs. */\r
478         xResult = IotMqtt_TimedSubscribe( xMQTTConnection,\r
479                                                                           &( xMQTTSubscription ),\r
480                                                                           1, /* We are subscribing to one topic filter. */\r
481                                                                           0, /* flags - currently ignored. */\r
482                                                                           mqttexampleMQTT_TIMEOUT_MS );\r
483         configASSERT( xResult == IOT_MQTT_SUCCESS );\r
484 }\r
485 /*-----------------------------------------------------------*/\r
486 \r
487 static void prvMQTTPublish( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
488 {\r
489 IotMqttError_t xResult;\r
490 IotMqttPublishInfo_t xMQTTPublishInfo;\r
491 \r
492         /* Publish a message with QoS1 on the mqttexampleTOPIC topic. Since we are\r
493          * subscribed to the same topic, the MQTT broker will send the same message\r
494          * back to us. It is verified in the publish callback. */\r
495         xMQTTPublishInfo.qos = IOT_MQTT_QOS_1;\r
496         xMQTTPublishInfo.retain = false;\r
497         xMQTTPublishInfo.pTopicName = pcTopicString;\r
498         xMQTTPublishInfo.topicNameLength = ( uint16_t ) strlen( pcTopicString );\r
499         xMQTTPublishInfo.pPayload = mqttexampleMESSAGE;\r
500         xMQTTPublishInfo.payloadLength = strlen( mqttexampleMESSAGE );\r
501         xMQTTPublishInfo.retryMs = mqttexamplePUBLISH_RETRY_MS;\r
502         xMQTTPublishInfo.retryLimit = mqttexamplePUBLISH_RETRY_LIMIT;\r
503 \r
504         /* Use the synchronous API to publish - It is a blocking call and only\r
505          * returns when the publish operation is complete or a timeout occurs. */\r
506         xResult = IotMqtt_TimedPublish( xMQTTConnection,\r
507                                                                         &( xMQTTPublishInfo ),\r
508                                                                         0, /* flags - currently ignored. */\r
509                                                                         mqttexampleMQTT_TIMEOUT_MS );\r
510         configASSERT( xResult == IOT_MQTT_SUCCESS );\r
511 }\r
512 /*-----------------------------------------------------------*/\r
513 \r
514 static void prvMQTTUnsubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
515 {\r
516 IotMqttError_t xResult;\r
517 IotMqttSubscription_t xMQTTSubscription;\r
518 \r
519         /* Unsubscribe from the mqttexampleTOPIC topic filter. */\r
520         xMQTTSubscription.pTopicFilter = pcTopicString;\r
521         xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( pcTopicString );\r
522         /* The following members of the IotMqttSubscription_t are ignored by the\r
523          * unsubscribe operation. Just initialize them to avoid "use of uninitialized\r
524          * variable" warnings. */\r
525         xMQTTSubscription.qos = IOT_MQTT_QOS_1;\r
526         xMQTTSubscription.callback.pCallbackContext = NULL;\r
527         xMQTTSubscription.callback.function = NULL;\r
528 \r
529         /* Use the synchronous API to unsubscribe - It is a blocking call and only\r
530          * returns when the unsubscribe operation is complete or a timeout occurs. */\r
531         xResult = IotMqtt_TimedUnsubscribe( xMQTTConnection,\r
532                                                                                 &( xMQTTSubscription ),\r
533                                                                                 1, /* We are unsubscribing from one topic filter. */\r
534                                                                                 0, /* flags - currently ignored. */\r
535                                                                                 mqttexampleMQTT_TIMEOUT_MS );\r
536         configASSERT( xResult == IOT_MQTT_SUCCESS );\r
537 }\r
538 /*-----------------------------------------------------------*/\r
539 \r
540 static void prvMQTTDisconnect( IotMqttConnection_t xMQTTConnection )\r
541 {\r
542         /* Send a MQTT DISCONNECT packet to the MQTT broker to do a graceful\r
543          * disconnect. */\r
544         IotMqtt_Disconnect( xMQTTConnection,\r
545                                                 0 /* flags - 0 means a graceful disconnect by sending MQTT DISCONNECT. */\r
546                                                 );\r
547 }\r
548 /*-----------------------------------------------------------*/\r