2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard inclues. */
\r
32 /* Kernel includes. */
\r
33 #include "FreeRTOS.h"
\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
42 * @brief The keep-alive interval used for this example.
\r
44 * An MQTT ping request will be sent periodically at this interval.
\r
46 #define mqttexampleKEEP_ALIVE_SECONDS ( 60 )
\r
49 * @brief The timeout for MQTT operations in this example.
\r
51 #define mqttexampleMQTT_TIMEOUT_MS ( 50000 )
\r
54 * @brief The MQTT client identifier used in this example.
\r
56 #define mqttexampleCLIENT_IDENTIFIER "mqttexampleclient"
\r
58 const char *pcClientIdentifiers[] = { "AAA" };//, "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH", "III", "JJJ" };
\r
61 * @brief Details of the MQTT broker to connect to.
\r
63 * @note This example does not use TLS and therefore won't work with AWS IoT.
\r
66 #define mqttexampleMQTT_BROKER_ENDPOINT "test.mosquitto.org"
\r
67 #define mqttexampleMQTT_BROKER_PORT 1883
\r
70 * @brief The topic to subscribe and publish to in the example.
\r
72 #define mqttexampleTOPIC "example/topic"
\r
75 * @brief The MQTT message published in this example.
\r
77 #define mqttexampleMESSAGE "Hello World!"
\r
80 * @brief Paramters to control the retry behaviour in case a QoS1 publish
\r
81 * message gets lost.
\r
83 * Retry every minutes up to a maximum of 5 retries.
\r
85 #define mqttexamplePUBLISH_RETRY_MS ( 1000 )
\r
86 #define mqttexamplePUBLISH_RETRY_LIMIT ( 5 )
\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
92 #define mqttexampleDISCONNECTED_BIT ( 1UL << 0UL )
\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
99 #define mqttexampleMESSAGE_RECEIVED_BIT ( 1UL << 1UL )
\r
100 /*-----------------------------------------------------------*/
\r
103 * @brief Parameters used to create the system task pool.
\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
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
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
127 /*-----------------------------------------------------------*/
\r
130 * @brief The task used to demonstrate the MQTT API.
\r
132 * @param[in] pvParameters Parmaters as passed at the time of task creation. Not
\r
133 * used in this example.
\r
135 static void prvMQTTDemoTask( void *pvParameters );
\r
138 * @brief The callback invoked by the MQTT library when the MQTT connection gets
\r
141 * @param[in] pvCallbackContext Callback context as provided at the time of
\r
143 * @param[in] pxCallbackParams Contains the reason why the MQTT connection was
\r
146 static void prvExample_OnDisconnect( void * pvCallbackContext,
\r
147 IotMqttCallbackParam_t * pxCallbackParams );
\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
153 * @param[in] pvCallbackContext Callback context as provided at the time of
\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
158 static void prvExample_OnMessageReceived( void * pvCallbackContext,
\r
159 IotMqttCallbackParam_t * pxCallbackParams );
\r
162 * @brief Connects to the MQTT broker as specified in mqttexampleMQTT_BROKER_ENDPOINT
\r
163 * and mqttexampleMQTT_BROKER_PORT.
\r
165 * @note This example does not use TLS and therefore will not work with MQTT.
\r
167 static void prvMQTTConnect( IotMqttConnection_t *xMQTTConnection, const char *pcClientID );
\r
170 * @brief Subscribes to pcTopicString.
\r
172 static void prvMQTTSubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );
\r
175 * @brief Publishes a messages mqttexampleMESSAGE on mqttexampleTOPIC topic.
\r
177 static void prvMQTTPublish( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );
\r
180 * @brief Unsubscribes from the mqttexampleTOPIC topic.
\r
182 static void prvMQTTUnsubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );
\r
185 * @brief Disconnects from the MQTT broker gracefully by sending an MQTT
\r
186 * DISCONNECT message.
\r
188 static void prvMQTTDisconnect( IotMqttConnection_t xMQTTConnection );
\r
189 /*-----------------------------------------------------------*/
\r
191 static void prvExample_OnDisconnect( void * pvCallbackContext,
\r
192 IotMqttCallbackParam_t * pxCallbackParams )
\r
194 TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;
\r
196 /* Ensure that we initiated the disconnect. */
\r
197 configASSERT( pxCallbackParams->u.disconnectReason == IOT_MQTT_DISCONNECT_CALLED );
\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
205 /*-----------------------------------------------------------*/
\r
207 static void prvExample_OnMessageReceived( void * pvCallbackContext,
\r
208 IotMqttCallbackParam_t * pxCallbackParams )
\r
210 TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;
\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
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
224 /* Ensure that the message QoS is as expected. */
\r
225 configASSERT( pxCallbackParams->u.message.info.qos == IOT_MQTT_QOS_1 );
\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
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
239 /*-----------------------------------------------------------*/
\r
241 void vStartSimpleMQTTDemo( void )
\r
244 const uint32_t ulMax_x = sizeof( pcClientIdentifiers ) / sizeof( char * );
\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
249 for( x = 0; x < ulMax_x; x++ )
\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
259 /*-----------------------------------------------------------*/
\r
261 static void prvMQTTDemoTask( void *pvParameters )
\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
272 /* Remove compiler warnings about unused parameters. */
\r
273 ( void ) pvParameters;
\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
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
284 /* Create a topic string that is unique to the MQTT connection created by
\r
286 snprintf( cTopicString, sizeof( cTopicString ), "%s/%s", mqttexampleTOPIC, pcClientIdentifiers[ ulTaskNumber ] );
\r
290 /* Don't expect any notifications to be pending yet. */
\r
291 ulNotificationValue = ulTaskNotifyTake( pdTRUE, xNoDelay );
\r
292 configASSERT( ulNotificationValue == 0 );
\r
295 /****************************** Connect. ******************************/
\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
306 /**************************** Subscribe. ******************************/
\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
317 /*********************** Publish 5 messages. **************************/
\r
319 /* Publish a few messages while connected. */
\r
320 for( x = 0; x < ( ulTaskNumber + 1UL ); x++ )
\r
322 ulMaxPublishCount = uxRand();
\r
325 /* Cap ulMaxPublishCount but ensure it is not zero. */
\r
326 ulMaxPublishCount %= 10UL;
\r
327 ulMaxPublishCount++;
\r
329 for( ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++ )
\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
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
351 /******************* Unsubscribe and Disconnect. **********************/
\r
353 /* Unsubscribe from the topic mqttexampleTOPIC and disconnect
\r
355 prvMQTTUnsubscribe( xMQTTConnection, cTopicString );
\r
356 prvMQTTDisconnect( xMQTTConnection );
\r
357 configPRINTF( ( "Disconnected from %s\r\n\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );
\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
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
376 /*-----------------------------------------------------------*/
\r
378 static void prvMQTTConnect( IotMqttConnection_t *xMQTTConnection, const char *pcClientID )
\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
387 /******************* Broker information setup. **********************/
\r
389 xMQTTBrokerInfo.pHostName = mqttexampleMQTT_BROKER_ENDPOINT;
\r
390 xMQTTBrokerInfo.port = mqttexampleMQTT_BROKER_PORT;
\r
393 /******************* Network information setup. **********************/
\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
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
404 /* Use FreeRTOS+TCP network. */
\r
405 xNetworkInfo.pNetworkInterface = IOT_NETWORK_INTERFACE_FREERTOS;
\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
414 /****************** MQTT Connection information setup. ********************/
\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
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
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
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
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
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
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
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
456 mqttexampleMQTT_TIMEOUT_MS,
\r
458 configASSERT( xResult == IOT_MQTT_SUCCESS );
\r
460 /*-----------------------------------------------------------*/
\r
462 static void prvMQTTSubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )
\r
464 IotMqttError_t xResult;
\r
465 IotMqttSubscription_t xMQTTSubscription;
\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
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
485 /*-----------------------------------------------------------*/
\r
487 static void prvMQTTPublish( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )
\r
489 IotMqttError_t xResult;
\r
490 IotMqttPublishInfo_t xMQTTPublishInfo;
\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
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
512 /*-----------------------------------------------------------*/
\r
514 static void prvMQTTUnsubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )
\r
516 IotMqttError_t xResult;
\r
517 IotMqttSubscription_t xMQTTSubscription;
\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
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
538 /*-----------------------------------------------------------*/
\r
540 static void prvMQTTDisconnect( IotMqttConnection_t xMQTTConnection )
\r
542 /* Send a MQTT DISCONNECT packet to the MQTT broker to do a graceful
\r
544 IotMqtt_Disconnect( xMQTTConnection,
\r
545 0 /* flags - 0 means a graceful disconnect by sending MQTT DISCONNECT. */
\r
548 /*-----------------------------------------------------------*/
\r