]> git.sur5r.net Git - freertos/blobdiff - 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
index 3ad5e662d1f061805690d08945a957f55b6cd2c6..80a04169e78208d8e5223203d12369920199a592 100644 (file)
 /**\r
  * @brief The timeout for MQTT operations in this example.\r
  */\r
-#define mqttexampleMQTT_TIMEOUT_MS                     ( 5000 )\r
+#define mqttexampleMQTT_TIMEOUT_MS                     ( 50000 )\r
 \r
 /**\r
  * @brief The MQTT client identifier used in this example.\r
  */\r
 #define mqttexampleCLIENT_IDENTIFIER           "mqttexampleclient"\r
 \r
+const char *pcClientIdentifiers[] = { "AAA" };//, "BBB", "CCC", "DDD", "EEE", "FFF", "GGG", "HHH", "III", "JJJ" };\r
+\r
 /**\r
  * @brief Details of the MQTT broker to connect to.\r
  *\r
 #define mqttexampleMESSAGE_RECEIVED_BIT                ( 1UL << 1UL )\r
 /*-----------------------------------------------------------*/\r
 \r
-/**\r
- * @brief The MQTT connection handle used in this example.\r
- */\r
-static IotMqttConnection_t xMQTTConnection = IOT_MQTT_CONNECTION_INITIALIZER;\r
-\r
 /**\r
  * @brief Parameters used to create the system task pool.\r
  */\r
@@ -167,28 +164,28 @@ static void prvExample_OnMessageReceived( void * pvCallbackContext,
  *\r
  * @note This example does not use TLS and therefore will not work with MQTT.\r
  */\r
-static void prvMQTTConnect( void );\r
+static void prvMQTTConnect( IotMqttConnection_t *xMQTTConnection, const char *pcClientID );\r
 \r
 /**\r
- * @brief Subscribes to the topic as specified in mqttexampleTOPIC.\r
+ * @brief Subscribes to pcTopicString.\r
  */\r
-static void prvMQTTSubscribe( void );\r
+static void prvMQTTSubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
 \r
 /**\r
  * @brief Publishes a messages mqttexampleMESSAGE on mqttexampleTOPIC topic.\r
  */\r
-static void prvMQTTPublish( void );\r
+static void prvMQTTPublish( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
 \r
 /**\r
  * @brief Unsubscribes from the mqttexampleTOPIC topic.\r
  */\r
-static void prvMQTTUnsubscribe( void );\r
+static void prvMQTTUnsubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString );\r
 \r
 /**\r
  * @brief Disconnects from the MQTT broker gracefully by sending an MQTT\r
  * DISCONNECT message.\r
  */\r
-static void prvMQTTDisconnect( void );\r
+static void prvMQTTDisconnect( IotMqttConnection_t xMQTTConnection );\r
 /*-----------------------------------------------------------*/\r
 \r
 static void prvExample_OnDisconnect( void * pvCallbackContext,\r
@@ -213,10 +210,10 @@ static void prvExample_OnMessageReceived( void * pvCallbackContext,
 TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;\r
 \r
        /* Ensure that the message is received on the expected topic. */\r
-       configASSERT( pxCallbackParams->u.message.info.topicNameLength == strlen( mqttexampleTOPIC ) );\r
-       configASSERT( strncmp( pxCallbackParams->u.message.info.pTopicName,\r
-                                                  mqttexampleTOPIC,\r
-                                                  strlen( mqttexampleTOPIC ) ) == 0 );\r
+//     configASSERT( pxCallbackParams->u.message.info.topicNameLength == strlen( mqttexampleTOPIC ) );\r
+//     configASSERT( strncmp( pxCallbackParams->u.message.info.pTopicName,\r
+//                                                mqttexampleTOPIC,\r
+//                                                strlen( mqttexampleTOPIC ) ) == 0 );\r
 \r
        /* Ensure that the expected message is received. */\r
        configASSERT( pxCallbackParams->u.message.info.payloadLength == strlen( mqttexampleMESSAGE ) );\r
@@ -243,24 +240,34 @@ TaskHandle_t xDemoTaskHandle = ( TaskHandle_t ) pvCallbackContext;
 \r
 void vStartSimpleMQTTDemo( void )\r
 {\r
+uint32_t x;\r
+const uint32_t ulMax_x = sizeof( pcClientIdentifiers ) / sizeof( char * );\r
+\r
        /* This example uses a single application task, which in turn is used to\r
         * connect, subscribe, publish, unsubscribe and disconnect from the MQTT\r
         * broker. */\r
+for( x = 0; x < ulMax_x; x++ )\r
+{\r
        xTaskCreate( prvMQTTDemoTask,                   /* Function that implements the task. */\r
                                 "MQTTDemo",                            /* Text name for the task - only used for debugging. */\r
                                 configMINIMAL_STACK_SIZE,      /* Size of stack (in words, not bytes) to allocate for the task. */\r
-                                NULL,                                          /* Task parameter - not used in this case. */\r
+                                ( void * ) x,                                          /* Task parameter - not used in this case. */\r
                                 tskIDLE_PRIORITY,                      /* Task priority, must be between 0 and configMAX_PRIORITIES - 1. */\r
                                 NULL );                                        /* Used to pass out a handle to the created task - not used in this case. */\r
 }\r
+}\r
 /*-----------------------------------------------------------*/\r
 \r
 static void prvMQTTDemoTask( void *pvParameters )\r
 {\r
 IotMqttError_t xResult;\r
 uint32_t ulNotificationValue = 0, ulPublishCount;\r
-const uint32_t ulMaxPublishCount = 5UL;\r
-const TickType_t xNoDelay = ( TickType_t ) 0;\r
+uint32_t ulMaxPublishCount = 0UL;\r
+const TickType_t xNoDelay = ( TickType_t ) 1;\r
+IotMqttConnection_t xMQTTConnection = IOT_MQTT_CONNECTION_INITIALIZER;\r
+uint32_t ulTaskNumber = ( uint32_t ) pvParameters, x;\r
+char cTopicString[ sizeof( mqttexampleTOPIC ) + 5 ];//_RB_ Access by other tasks so must be persistant and will cause memory faults on memory protected systems.\r
+#pragma message ("Access by other tasks so must be persistant and will cause memory faults on memory protected systems.")\r
 \r
        /* Remove compiler warnings about unused parameters. */\r
        ( void ) pvParameters;\r
@@ -274,10 +281,15 @@ const TickType_t xNoDelay = ( TickType_t ) 0;
        xResult = IotMqtt_Init();\r
        configASSERT( xResult == IOT_MQTT_SUCCESS );\r
 \r
+       /* Create a topic string that is unique to the MQTT connection created by\r
+       this task. */\r
+       snprintf( cTopicString, sizeof( cTopicString ), "%s/%s", mqttexampleTOPIC, pcClientIdentifiers[ ulTaskNumber ] );\r
+\r
        for( ; ; )\r
        {\r
                /* Don't expect any notifications to be pending yet. */\r
-               configASSERT( ulTaskNotifyTake( pdTRUE, xNoDelay ) == 0 );\r
+               ulNotificationValue = ulTaskNotifyTake( pdTRUE, xNoDelay );\r
+               configASSERT( ulNotificationValue == 0 );\r
 \r
 \r
                /****************************** Connect. ******************************/\r
@@ -287,7 +299,7 @@ const TickType_t xNoDelay = ( TickType_t ) 0;
                 * mqttexampleMQTT_BROKER_PORT at the top of this file. Please change\r
                 * it to the MQTT broker you want to connect to. Note that this example\r
                 * does not use TLS and therefore will not work with AWS IoT. */\r
-               prvMQTTConnect();\r
+               prvMQTTConnect( &xMQTTConnection, pcClientIdentifiers[ ulTaskNumber ] );\r
                configPRINTF( ( "Connected to %s\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );\r
 \r
 \r
@@ -298,19 +310,28 @@ const TickType_t xNoDelay = ( TickType_t ) 0;
                 * client will then publish to the same topic it subscribed to, so will\r
                 * expect all the messages it sends to the broker to be sent back to it\r
                 * from the broker. */\r
-               prvMQTTSubscribe();\r
-               configPRINTF( ( "Subscribed to the topic %s\r\n", mqttexampleTOPIC ) );\r
+               prvMQTTSubscribe( xMQTTConnection, cTopicString );\r
+               configPRINTF( ( "Subscribed to the topic %s\r\n", cTopicString ) );\r
 \r
 \r
                /*********************** Publish 5 messages. **************************/\r
 \r
                /* Publish a few messages while connected. */\r
+               for( x = 0; x < ( ulTaskNumber + 1UL ); x++ )\r
+               {\r
+                       ulMaxPublishCount = uxRand();\r
+               }\r
+\r
+               /* Cap ulMaxPublishCount but ensure it is not zero. */\r
+               ulMaxPublishCount %= 10UL;\r
+               ulMaxPublishCount++;\r
+\r
                for( ulPublishCount = 0; ulPublishCount < ulMaxPublishCount; ulPublishCount++ )\r
                {\r
                        /* Publish a message on the mqttexampleTOPIC topic as specified at\r
                         * the top of this file. */\r
-                       prvMQTTPublish();\r
-                       configPRINTF( ( "Published %s on the topic %s\r\n", mqttexampleMESSAGE, mqttexampleTOPIC ) );\r
+                       prvMQTTPublish( xMQTTConnection, cTopicString );\r
+                       configPRINTF( ( "Published %s on the topic %s\r\n", mqttexampleMESSAGE, cTopicString ) );\r
 \r
                        /* Since we are subscribed to the same topic as we published on, we\r
                         * will get the same message back from the MQTT broker. Wait for the\r
@@ -331,8 +352,8 @@ const TickType_t xNoDelay = ( TickType_t ) 0;
 \r
                /* Unsubscribe from the topic mqttexampleTOPIC and disconnect\r
                 * gracefully. */\r
-               prvMQTTUnsubscribe();\r
-               prvMQTTDisconnect();\r
+               prvMQTTUnsubscribe( xMQTTConnection, cTopicString );\r
+               prvMQTTDisconnect( xMQTTConnection );\r
                configPRINTF( ( "Disconnected from %s\r\n\r\n", mqttexampleMQTT_BROKER_ENDPOINT ) );\r
 \r
                /* Wait for the disconnect operation to complete which is informed to us\r
@@ -349,18 +370,19 @@ const TickType_t xNoDelay = ( TickType_t ) 0;
                /* Wait for some time between two iterations to ensure that we do not\r
                 * bombard the public test mosquitto broker. */\r
                configPRINTF( ( "prvMQTTDemoTask() completed an iteration without hitting an assert. Total free heap is %u\r\n\r\n", xPortGetFreeHeapSize() ) );\r
-               vTaskDelay( pdMS_TO_TICKS( 5000 ) );\r
+//             vTaskDelay( pdMS_TO_TICKS( 5000 ) );\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTConnect( void )\r
+static void prvMQTTConnect( IotMqttConnection_t *xMQTTConnection, const char *pcClientID )\r
 {\r
 IotMqttError_t xResult;\r
 IotNetworkServerInfo_t xMQTTBrokerInfo;\r
 IotMqttNetworkInfo_t xNetworkInfo = IOT_MQTT_NETWORK_INFO_INITIALIZER;\r
 IotMqttConnectInfo_t xConnectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;\r
-\r
+static char c[ 10 ];\r
+static int id = 0;\r
 \r
        /******************* Broker information setup. **********************/\r
 \r
@@ -417,8 +439,8 @@ IotMqttConnectInfo_t xConnectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;
        /* The client identifier is used to uniquely identify this MQTT client to\r
         * the MQTT broker.  In a production device the identifier can be something\r
         * unique, such as a device serial number. */\r
-       xConnectInfo.pClientIdentifier = mqttexampleCLIENT_IDENTIFIER;\r
-       xConnectInfo.clientIdentifierLength = ( uint16_t ) strlen( mqttexampleCLIENT_IDENTIFIER );\r
+       xConnectInfo.pClientIdentifier = pcClientID;\r
+       xConnectInfo.clientIdentifierLength = ( uint16_t ) strlen( pcClientID );\r
 \r
        /* This example does not use any authentication and therefore username and\r
         * password fields are not used. */\r
@@ -432,12 +454,12 @@ IotMqttConnectInfo_t xConnectInfo = IOT_MQTT_CONNECT_INFO_INITIALIZER;
        xResult = IotMqtt_Connect( &( xNetworkInfo ),\r
                                                           &( xConnectInfo ),\r
                                                           mqttexampleMQTT_TIMEOUT_MS,\r
-                                                          &( xMQTTConnection ) );\r
+                                                          xMQTTConnection );\r
        configASSERT( xResult == IOT_MQTT_SUCCESS );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTSubscribe( void )\r
+static void prvMQTTSubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
 {\r
 IotMqttError_t xResult;\r
 IotMqttSubscription_t xMQTTSubscription;\r
@@ -446,8 +468,8 @@ IotMqttSubscription_t xMQTTSubscription;
         * as the callback context which is used by the callback to send a task\r
         * notification to this task.*/\r
        xMQTTSubscription.qos = IOT_MQTT_QOS_1;\r
-       xMQTTSubscription.pTopicFilter = mqttexampleTOPIC;\r
-       xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( mqttexampleTOPIC );\r
+       xMQTTSubscription.pTopicFilter = pcTopicString;\r
+       xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( pcTopicString );\r
        xMQTTSubscription.callback.pCallbackContext = ( void * ) xTaskGetCurrentTaskHandle();\r
        xMQTTSubscription.callback.function = prvExample_OnMessageReceived;\r
 \r
@@ -462,7 +484,7 @@ IotMqttSubscription_t xMQTTSubscription;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTPublish( void )\r
+static void prvMQTTPublish( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
 {\r
 IotMqttError_t xResult;\r
 IotMqttPublishInfo_t xMQTTPublishInfo;\r
@@ -472,8 +494,8 @@ IotMqttPublishInfo_t xMQTTPublishInfo;
         * back to us. It is verified in the publish callback. */\r
        xMQTTPublishInfo.qos = IOT_MQTT_QOS_1;\r
        xMQTTPublishInfo.retain = false;\r
-       xMQTTPublishInfo.pTopicName = mqttexampleTOPIC;\r
-       xMQTTPublishInfo.topicNameLength = ( uint16_t ) strlen( mqttexampleTOPIC );\r
+       xMQTTPublishInfo.pTopicName = pcTopicString;\r
+       xMQTTPublishInfo.topicNameLength = ( uint16_t ) strlen( pcTopicString );\r
        xMQTTPublishInfo.pPayload = mqttexampleMESSAGE;\r
        xMQTTPublishInfo.payloadLength = strlen( mqttexampleMESSAGE );\r
        xMQTTPublishInfo.retryMs = mqttexamplePUBLISH_RETRY_MS;\r
@@ -489,14 +511,14 @@ IotMqttPublishInfo_t xMQTTPublishInfo;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTUnsubscribe( void )\r
+static void prvMQTTUnsubscribe( IotMqttConnection_t xMQTTConnection, const char * const pcTopicString )\r
 {\r
 IotMqttError_t xResult;\r
 IotMqttSubscription_t xMQTTSubscription;\r
 \r
        /* Unsubscribe from the mqttexampleTOPIC topic filter. */\r
-       xMQTTSubscription.pTopicFilter = mqttexampleTOPIC;\r
-       xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( mqttexampleTOPIC );\r
+       xMQTTSubscription.pTopicFilter = pcTopicString;\r
+       xMQTTSubscription.topicFilterLength = ( uint16_t ) strlen( pcTopicString );\r
        /* The following members of the IotMqttSubscription_t are ignored by the\r
         * unsubscribe operation. Just initialize them to avoid "use of uninitialized\r
         * variable" warnings. */\r
@@ -515,7 +537,7 @@ IotMqttSubscription_t xMQTTSubscription;
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvMQTTDisconnect( void )\r
+static void prvMQTTDisconnect( IotMqttConnection_t xMQTTConnection )\r
 {\r
        /* Send a MQTT DISCONNECT packet to the MQTT broker to do a graceful\r
         * disconnect. */\r