Skip to content

Commit e942ad7

Browse files
Add MQTT_Publish API with ACK handling
1 parent ae8daca commit e942ad7

9 files changed

+1773
-265
lines changed

source/core_mqtt.c

Lines changed: 745 additions & 153 deletions
Large diffs are not rendered by default.

source/core_mqtt_prop_deserializer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ MQTTStatus_t MQTT_GetNextPropertyType( MQTTPropBuilder_t * pPropertyBuilder,
278278

279279
if( status != MQTTSuccess )
280280
{
281-
LogError( ( "Property type is invalid." ) );
281+
/* Do nothing. checkPropBuilderParams will log the warning/error. */
282282
}
283283
else if( property == NULL )
284284
{

source/core_mqtt_prop_serializer.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <inttypes.h>
3333

3434
#include "core_mqtt_serializer.h"
35-
#include "transport_interface.h"
3635

3736
#include "private/core_mqtt_serializer_private.h"
3837

source/core_mqtt_serializer.c

Lines changed: 750 additions & 85 deletions
Large diffs are not rendered by default.

source/core_mqtt_serializer_private.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,28 @@ MQTTStatus_t decodeVariableLength( const uint8_t * pBuffer,
364364
}
365365

366366
/*-----------------------------------------------------------*/
367+
368+
uint8_t * MQTT_SerializeAckFixed( uint8_t * pIndex,
369+
uint8_t packetType,
370+
uint16_t packetId,
371+
size_t remainingLength,
372+
MQTTSuccessFailReasonCode_t reasonCode )
373+
{
374+
uint8_t * pIndexLocal = pIndex;
375+
376+
/* The first byte in the publish ack packet is the control packet type. */
377+
*pIndexLocal = packetType;
378+
pIndexLocal++;
379+
/*After the packet type fixed header has remaining length.*/
380+
pIndexLocal = encodeVariableLength( pIndexLocal, remainingLength );
381+
/*Encode the packet id.*/
382+
pIndexLocal[ 0 ] = UINT16_HIGH_BYTE( packetId );
383+
pIndexLocal[ 1 ] = UINT16_LOW_BYTE( packetId );
384+
pIndexLocal = &pIndexLocal[ 2 ];
385+
/*We are now sending the ack.*/
386+
*pIndexLocal = ( uint8_t ) reasonCode;
387+
pIndexLocal++;
388+
return pIndexLocal;
389+
}
390+
391+
/*-----------------------------------------------------------*/

source/include/core_mqtt.h

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -925,16 +925,28 @@ MQTTStatus_t MQTT_Subscribe( MQTTContext_t * pContext,
925925
* @param[in] pContext Initialized MQTT context.
926926
* @param[in] pPublishInfo MQTT PUBLISH packet parameters.
927927
* @param[in] packetId packet ID generated by #MQTT_GetPacketId.
928+
* @param[in] pPropertyBuilder Properties to be sent in the outgoing packet.
928929
*
929-
* @return #MQTTNoMemory if pBuffer is too small to hold the MQTT packet;
930-
* #MQTTBadParameter if invalid parameters are passed;
931-
* #MQTTSendFailed if transport write failed;
932-
* #MQTTStatusNotConnected if the connection is not established yet
930+
* @return
931+
* #MQTTBadParameter if invalid parameters are passed;<br>
932+
* #MQTTBadResponse if there is an error in property parsing;<br>
933+
* #MQTTSendFailed if transport write failed;<br>
934+
* #MQTTStatusNotConnected if the connection is not established yet<br>
933935
* #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect
934-
* before calling any other API
936+
* before calling any other API<br>
935937
* #MQTTPublishStoreFailed if the user provided callback to copy and store the
936-
* outgoing publish packet fails
937-
* #MQTTSuccess otherwise.
938+
* outgoing publish packet fails<br>
939+
* #MQTTSuccess otherwise.<br>
940+
*
941+
* Functions to add optional properties to the PUBLISH packet are:
942+
*
943+
* - #MQTTPropAdd_PubPayloadFormat
944+
* - #MQTTPropAdd_PubMessageExpiry
945+
* - #MQTTPropAdd_PubTopicAlias
946+
* - #MQTTPropAdd_PubResponseTopic
947+
* - #MQTTPropAdd_PubCorrelationData
948+
* - #MQTTPropAdd_PubContentType
949+
* - #MQTTPropAdd_UserProp
938950
*
939951
* <b>Example</b>
940952
* @code{c}
@@ -952,11 +964,19 @@ MQTTStatus_t MQTT_Subscribe( MQTTContext_t * pContext,
952964
* publishInfo.topicNameLength = strlen( publishInfo.pTopicName );
953965
* publishInfo.pPayload = "Hello World!";
954966
* publishInfo.payloadLength = strlen( "Hello World!" );
967+
* // Optional properties to be sent in the PUBLISH packet.
968+
* MQTTPropBuilder_t propertyBuilder;
969+
* uint8_t propertyBuffer[ 100 ];
970+
* size_t propertyBufferLength = sizeof( propertyBuffer );
971+
* status = MQTTPropertyBuilder_Init( &propertyBuilder, propertyBuffer, propertyBufferLength );
972+
*
973+
* // Set a property in the propertyBuilder
974+
* status = MQTTPropAdd_PubPayloadFormat( &propertyBuilder, 1);
955975
*
956976
* // Packet ID is needed for QoS > 0.
957977
* packetId = MQTT_GetPacketId( pContext );
958978
*
959-
* status = MQTT_Publish( pContext, &publishInfo, packetId );
979+
* status = MQTT_Publish( pContext, &publishInfo, packetId, &propertyBuilder );
960980
*
961981
* if( status == MQTTSuccess )
962982
* {
@@ -968,7 +988,8 @@ MQTTStatus_t MQTT_Subscribe( MQTTContext_t * pContext,
968988
/* @[declare_mqtt_publish] */
969989
MQTTStatus_t MQTT_Publish( MQTTContext_t * pContext,
970990
const MQTTPublishInfo_t * pPublishInfo,
971-
uint16_t packetId );
991+
uint16_t packetId,
992+
const MQTTPropBuilder_t * pPropertyBuilder );
972993
/* @[declare_mqtt_publish] */
973994

974995
/**
@@ -1392,6 +1413,30 @@ void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem,
13921413
const MQTTVec_t * pVec );
13931414
/* @[declare_mqtt_serializemqttvec] */
13941415

1416+
/**
1417+
* @brief Get a human-readable string representation of an MQTT packet type.
1418+
*
1419+
* This function converts an MQTT packet type byte into a corresponding
1420+
* string representation for debugging and logging purposes.
1421+
*
1422+
* @param[in] packetType The MQTT packet type byte to convert.
1423+
*
1424+
* @return A pointer to a constant string containing the packet type name.
1425+
* Returns "UNKNOWN" if the packet type is not recognized.
1426+
*
1427+
* @note The returned string is statically allocated and should not be freed.
1428+
* @note For PUBLISH packets, the function masks the lower 4 bits (flags) and
1429+
* returns "PUBLISH" regardless of the QoS, DUP, or RETAIN flag values.
1430+
*
1431+
* <b>Example</b>
1432+
* @code{c}
1433+
* uint8_t packetType = MQTT_PACKET_TYPE_PUBLISH;
1434+
* const char * packetName = MQTT_GetPacketTypeString( packetType );
1435+
* printf( "Received packet: %s\n", packetName ); // Prints "Received packet: PUBLISH"
1436+
* @endcode
1437+
*/
1438+
const char * MQTT_GetPacketTypeString( uint8_t packetType );
1439+
13951440
/* *INDENT-OFF* */
13961441
#ifdef __cplusplus
13971442
}

source/include/core_mqtt_config_defaults.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,26 @@
275275
#define LogDebug( message )
276276
#endif
277277

278+
/**
279+
* @brief Macro that is called in the MQTT library for logging "Trace" level
280+
* messages.
281+
*
282+
* To enable trace level logging from MQTT library, this macro should be mapped to the
283+
* application-specific logging implementation that supports trace logging.
284+
*
285+
* @note This logging macro is called in the MQTT library with parameters wrapped in
286+
* double parentheses to be ISO C89/C90 standard compliant. For a reference
287+
* POSIX implementation of the logging macros, refer to core_mqtt_config.h files, and the
288+
* logging-stack in demos folder of the
289+
* [AWS IoT Embedded C SDK repository](https://github.com/aws/aws-iot-device-sdk-embedded-C/).
290+
*
291+
* <b>Default value</b>: Trace logging is turned off, and no code is generated for calls
292+
* to the macro in the MQTT library on compilation.
293+
*/
294+
#ifndef LogTrace
295+
#define LogTrace( message )
296+
#endif
297+
278298
/* *INDENT-OFF* */
279299
#ifdef __cplusplus
280300
}

0 commit comments

Comments
 (0)