Amazon FreeRtos : Issue when retrieving MQTT Topic Name

YBdw wrote on February 01, 2018:


I’ve started to use Amazon FreeRTOS with the nxp OM40007 platform and have successfully followed the tutorial and got the echo demo to work.

I’m now trying to develop my own very simple examples to be more familiar with the libraries.

I’ve created a topic, and got my device to subscribe to it. I am sending a message from the AWS web console, and I want my device to receive it.

Topic name: dw/receive_answer
Message: “test”

When I look at the pxPublishParameters structure to retrieve the topic name, the value is wrong, it corresponds to the topic name appended to my message. See below for detail :

configPRINTF(("TOPIC CLIENT : ‘%s’\r
", echoTOPIC_NAME)); // Name of my topic
-> TOPIC CLIENT : ‘dw/receive_answer’

configPRINTF(("TOPIC BROKER ‘%s’\r
", pxPublishParameters->pucTopic)); // From MQTT lib
-> TOPIC BROKER ‘dw/receive_answertest

", pxPublishParameters->pvData)); // From MQTT lib

So as you can see, the message field in the PublishParameters structure is correct but the topic name isn’t.

I’ve had a look at the Amazon freertos MQTT drivers itself, and I am not quite sure why the message is appended to the topic name.

Do you have any idea of what I am doing wrongly ?


hbouvier-AWS wrote on February 01, 2018:


Thank you for using Amazon FreeRTOS!

The pxPublishParameters structure fields pucTopic and pvData are not NULL terminated so you can’t use configPRINTF to display them.
When you do, it will continue past the end of the string and continue to print characters until ‘\0’ is encountered.

That pointed us to a bug in one of our examples, below, where we are making a similar mistake. We’re working on a fix. Thank you for drawing this to our attention.

In aws_hello_world.c, line 377
/* Don’t expect the callback to be invoked for any other topics. */
configASSERT( strcmp( ( const char * ) pxPublishParameters->pucTopic, ( const char * ) echoTOPIC_NAME ) == 0 );

The length of the topic is available in the same structure in usTopicLength and ulDataLength.

A better way of handling this would be:
/* Don’t expect the callback to be invoked for any other topics. */
configASSERT( strncmp( ( const char * ) pxPublishParameters->pucTopic, ( const char * ) echoTOPIC_NAME, pxPublishParameters->usTopicLength ) == 0 );

To be able to printf, the best way would be to copy the string in a buffer using strncpy

strncpy( cPrintBuffer, ( const char * )pxPublishParameters->pucTopic, pxPublishParameters->usTopicLength );
cPrintBuffer[pxPublishParameters->usTopicLength] = '\0';
configPRINTF( ( "TOPIC BROKER '%s'\r

", cPrintBuffer ) );

Thank you,


YBdw wrote on February 02, 2018:

Hi Hugues,

Thanks a lot for your answer.

The reason I was trying to printf the topic value was because I was seeing a failure during the string compare.

I have actually used the strncmp and the topic length as a workaway, exactly as you’ve suggested and it worked well.

Thanks for the help,