Skip to content

Commit edf5af8

Browse files
authored
Use MQTT_PublishToResend() in the basic_tls and mutual_auth demos (#1101)
MQTT_PublishToResend() is now used in the mqtt_basic_tls_demo and the mqtt_mutual_auth_demo because it preserves the ordering of MQTT publish messages. These demos use a Qos > 0. Updated the README.md in tools/spell for clarity on how to use the scripts.
1 parent 4a292f5 commit edf5af8

File tree

4 files changed

+125
-52
lines changed

4 files changed

+125
-52
lines changed

demos/lexicon.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ developerguide
3131
doesn
3232
dup
3333
endif
34+
hashmap
3435
hasn
3536
html
3637
http
@@ -53,6 +54,7 @@ org
5354
outgoingpublishpackets
5455
packetid
5556
packetidentifier
57+
packetidtoresend
5658
param
5759
pathlen
5860
pbuffer

demos/mqtt/mqtt_demo_basic_tls/mqtt_demo_basic_tls.c

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@
4444
/* Include Demo Config as the first non-system header. */
4545
#include "demo_config.h"
4646

47-
/* MQTT API header. */
47+
/* MQTT API headers. */
4848
#include "mqtt.h"
49+
#include "mqtt_state.h"
4950

5051
/* OpenSSL sockets transport implementation. */
5152
#include "openssl_posix.h"
@@ -497,39 +498,68 @@ static int handlePublishResend( MQTTContext_t * pMqttContext )
497498
int returnStatus = EXIT_SUCCESS;
498499
MQTTStatus_t mqttStatus = MQTTSuccess;
499500
uint8_t index = 0U;
501+
MQTTStateCursor_t cursor = MQTT_STATE_CURSOR_INITIALIZER;
502+
uint16_t packetIdToResend = MQTT_PACKET_ID_INVALID;
503+
bool foundPacketId = false;
500504

505+
assert( pMqttContext != NULL );
501506
assert( outgoingPublishPackets != NULL );
502507

503-
/* Resend all the QoS2 publishes still in the array. These are the
504-
* publishes that hasn't received a PUBREC. When a PUBREC is
505-
* received, the publish is removed from the array. */
506-
for( index = 0U; index < MAX_OUTGOING_PUBLISHES; index++ )
508+
/* MQTT_PublishToResend() provides a packet ID of the next PUBLISH packet
509+
* that should be resent. In accordance with the MQTT v3.1.1 spec,
510+
* MQTT_PublishToResend() preserves the ordering of when the original
511+
* PUBLISH packets were sent. The outgoingPublishPackets array is searched
512+
* through for the associated packet ID. If the application requires
513+
* increased efficiency in the look up of the packet ID, then a hashmap of
514+
* packetId key and PublishPacket_t values may be used instead. */
515+
packetIdToResend = MQTT_PublishToResend( pMqttContext, &cursor );
516+
517+
while( packetIdToResend != MQTT_PACKET_ID_INVALID )
507518
{
508-
if( outgoingPublishPackets[ index ].packetId != MQTT_PACKET_ID_INVALID )
519+
foundPacketId = false;
520+
for( index = 0U; index < MAX_OUTGOING_PUBLISHES; index++ )
509521
{
510-
outgoingPublishPackets[ index ].pubInfo.dup = true;
511-
512-
LogInfo( ( "Sending duplicate PUBLISH with packet id %u.",
513-
outgoingPublishPackets[ index ].packetId ) );
514-
mqttStatus = MQTT_Publish( pMqttContext,
515-
&outgoingPublishPackets[ index ].pubInfo,
516-
outgoingPublishPackets[ index ].packetId );
517-
518-
if( mqttStatus != MQTTSuccess )
522+
if( outgoingPublishPackets[ index ].packetId == packetIdToResend )
519523
{
520-
LogError( ( "Sending duplicate PUBLISH for packet id %u "
521-
" failed with status %u.",
522-
outgoingPublishPackets[ index ].packetId,
523-
mqttStatus ) );
524-
returnStatus = EXIT_FAILURE;
525-
break;
526-
}
527-
else
528-
{
529-
LogInfo( ( "Sent duplicate PUBLISH successfully for packet id %u.\n\n",
524+
foundPacketId = true;
525+
outgoingPublishPackets[ index ].pubInfo.dup = true;
526+
527+
LogInfo( ( "Sending duplicate PUBLISH with packet id %u.",
530528
outgoingPublishPackets[ index ].packetId ) );
529+
mqttStatus = MQTT_Publish( pMqttContext,
530+
&outgoingPublishPackets[ index ].pubInfo,
531+
outgoingPublishPackets[ index ].packetId );
532+
533+
if( mqttStatus != MQTTSuccess )
534+
{
535+
LogError( ( "Sending duplicate PUBLISH for packet id %u "
536+
" failed with status %u.",
537+
outgoingPublishPackets[ index ].packetId,
538+
mqttStatus ) );
539+
returnStatus = EXIT_FAILURE;
540+
break;
541+
}
542+
else
543+
{
544+
LogInfo( ( "Sent duplicate PUBLISH successfully for packet id %u.\n\n",
545+
outgoingPublishPackets[ index ].packetId ) );
546+
}
531547
}
532548
}
549+
550+
if( foundPacketId == false )
551+
{
552+
LogError( ( "Packet id %u requires resend, but was not found in "
553+
"outgoingPublishPackets.",
554+
packetIdToResend ) );
555+
returnStatus = EXIT_FAILURE;
556+
break;
557+
}
558+
else
559+
{
560+
/* Get the next packetID to be resent. */
561+
packetIdToResend = MQTT_PublishToResend( pMqttContext, &cursor );
562+
}
533563
}
534564

535565
return returnStatus;

demos/mqtt/mqtt_demo_mutual_auth/mqtt_demo_mutual_auth.c

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@
4949
/* Include Demo Config as the first non-system header. */
5050
#include "demo_config.h"
5151

52-
/* MQTT API header. */
52+
/* MQTT API headers. */
5353
#include "mqtt.h"
54+
#include "mqtt_state.h"
5455

5556
/* OpenSSL sockets transport implementation. */
5657
#include "openssl_posix.h"
@@ -544,39 +545,68 @@ static int handlePublishResend( MQTTContext_t * pMqttContext )
544545
int returnStatus = EXIT_SUCCESS;
545546
MQTTStatus_t mqttStatus = MQTTSuccess;
546547
uint8_t index = 0U;
548+
MQTTStateCursor_t cursor = MQTT_STATE_CURSOR_INITIALIZER;
549+
uint16_t packetIdToResend = MQTT_PACKET_ID_INVALID;
550+
bool foundPacketId = false;
547551

552+
assert( pMqttContext != NULL );
548553
assert( outgoingPublishPackets != NULL );
549554

550-
/* Resend all the QoS1 publishes still in the array. These are the
551-
* publishes that hasn't received a PUBACK. When a PUBACK is
552-
* received, the publish is removed from the array. */
553-
for( index = 0U; index < MAX_OUTGOING_PUBLISHES; index++ )
555+
/* MQTT_PublishToResend() provides a packet ID of the next PUBLISH packet
556+
* that should be resent. In accordance with the MQTT v3.1.1 spec,
557+
* MQTT_PublishToResend() preserves the ordering of when the original
558+
* PUBLISH packets were sent. The outgoingPublishPackets array is searched
559+
* through for the associated packet ID. If the application requires
560+
* increased efficiency in the look up of the packet ID, then a hashmap of
561+
* packetId key and PublishPacket_t values may be used instead. */
562+
packetIdToResend = MQTT_PublishToResend( pMqttContext, &cursor );
563+
564+
while( packetIdToResend != MQTT_PACKET_ID_INVALID )
554565
{
555-
if( outgoingPublishPackets[ index ].packetId != MQTT_PACKET_ID_INVALID )
566+
foundPacketId = false;
567+
for( index = 0U; index < MAX_OUTGOING_PUBLISHES; index++ )
556568
{
557-
outgoingPublishPackets[ index ].pubInfo.dup = true;
558-
559-
LogInfo( ( "Sending duplicate PUBLISH with packet id %u.",
560-
outgoingPublishPackets[ index ].packetId ) );
561-
mqttStatus = MQTT_Publish( pMqttContext,
562-
&outgoingPublishPackets[ index ].pubInfo,
563-
outgoingPublishPackets[ index ].packetId );
564-
565-
if( mqttStatus != MQTTSuccess )
569+
if( outgoingPublishPackets[ index ].packetId == packetIdToResend )
566570
{
567-
LogError( ( "Sending duplicate PUBLISH for packet id %u "
568-
" failed with status %u.",
569-
outgoingPublishPackets[ index ].packetId,
570-
mqttStatus ) );
571-
returnStatus = EXIT_FAILURE;
572-
break;
573-
}
574-
else
575-
{
576-
LogInfo( ( "Sent duplicate PUBLISH successfully for packet id %u.\n\n",
571+
foundPacketId = true;
572+
outgoingPublishPackets[ index ].pubInfo.dup = true;
573+
574+
LogInfo( ( "Sending duplicate PUBLISH with packet id %u.",
577575
outgoingPublishPackets[ index ].packetId ) );
576+
mqttStatus = MQTT_Publish( pMqttContext,
577+
&outgoingPublishPackets[ index ].pubInfo,
578+
outgoingPublishPackets[ index ].packetId );
579+
580+
if( mqttStatus != MQTTSuccess )
581+
{
582+
LogError( ( "Sending duplicate PUBLISH for packet id %u "
583+
" failed with status %u.",
584+
outgoingPublishPackets[ index ].packetId,
585+
mqttStatus ) );
586+
returnStatus = EXIT_FAILURE;
587+
break;
588+
}
589+
else
590+
{
591+
LogInfo( ( "Sent duplicate PUBLISH successfully for packet id %u.\n\n",
592+
outgoingPublishPackets[ index ].packetId ) );
593+
}
578594
}
579595
}
596+
597+
if( foundPacketId == false )
598+
{
599+
LogError( ( "Packet id %u requires resend, but was not found in "
600+
"outgoingPublishPackets.",
601+
packetIdToResend ) );
602+
returnStatus = EXIT_FAILURE;
603+
break;
604+
}
605+
else
606+
{
607+
/* Get the next packetID to be resent. */
608+
packetIdToResend = MQTT_PublishToResend( pMqttContext, &cursor );
609+
}
580610
}
581611

582612
return returnStatus;

tools/spell/README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# How to create a lexicon.txt for a new library.
1+
# Pre-requisites to running the spell check scripts
22

33
1. In your GNU environment, install the *spell* and *getopt* programs. Use the following commands in Debian distributions, to install the packages (*getopt* is part of the `util-linux` package):
44
```shell
@@ -11,9 +11,20 @@
1111
export PATH=<CSDK_ROOT>/tools/spell:$PATH
1212
```
1313

14+
# How to create a lexicon.txt for a new library.
15+
1416
1. Ensure there does not exist a file called "lexicon.txt" in your library's directory. Run the following command to create a lexicon.txt for your library:
1517
```shell
1618
find-unknown-comment-words -d <CSDK_ROOT>/libraries/<LIBRARY_TYPE>/<MY_LIBRARY_NAME> > <CSDK_ROOT>/libraries/<LIBRARY_TYPE>/<MY_LIBRARY_NAME>/lexicon.txt
1719
```
1820

19-
1. Check the contents of *<CSDK_ROOT>/libraries/<LIBRARY_TYPE>/<MY_LIBRARY_NAME>/lexicon.txt* for any misspelled words. Fix them in your library's source code and delete them from the lexicon.txt.
21+
1. Check the contents of *<CSDK_ROOT>/libraries/<LIBRARY_TYPE>/<MY_LIBRARY_NAME>/lexicon.txt* for any misspelled words. Fix them in your library's source code and delete them from the lexicon.txt.
22+
23+
# How to run for changes to an existing library.
24+
25+
1. If there exists a lexicon.txt in the library's directory, run the following command:
26+
```shell
27+
find-unknown-comment-words -d <CSDK_ROOT>/libraries/<LIBRARY_TYPE>/<MY_LIBRARY_NAME>
28+
```
29+
30+
1. Add any non-dictionary correctly spelled words to *<CSDK_ROOT>/libraries/<LIBRARY_TYPE>/<MY_LIBRARY_NAME>/lexicon.txt*. Fix any misspelled words in your code comment change.

0 commit comments

Comments
 (0)