From 4905c9062ef6cc411343342678df4fa354d3c0df Mon Sep 17 00:00:00 2001 From: AniruddhaKanhere <60444055+AniruddhaKanhere@users.noreply.github.com> Date: Tue, 22 Oct 2024 02:28:55 +0000 Subject: [PATCH 1/3] Add opaque struct and functions to serialize it. --- source/core_mqtt.c | 34 ++++++++++++++++++++++++++++++++++ source/include/core_mqtt.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/source/core_mqtt.c b/source/core_mqtt.c index 0166462c..b258daa3 100644 --- a/source/core_mqtt.c +++ b/source/core_mqtt.c @@ -90,6 +90,11 @@ */ #define CORE_MQTT_UNSUBSCRIBE_PER_TOPIC_VECTOR_LENGTH ( 2U ) +struct MQTTVec +{ + TransportOutVector_t pVector; +}; + /*-----------------------------------------------------------*/ /** @@ -3726,3 +3731,32 @@ const char * MQTT_Status_strerror( MQTTStatus_t status ) } /*-----------------------------------------------------------*/ + +size_t MQTT_GetBytesInMQTTVec( MQTTVec_t *pVec, size_t len ) +{ + size_t memoryRequired = 0; + size_t i; + TransportOutVector_t * pTransportVec = (TransportOutVector_t*)pVec; + + for (i = 0; i < len; i++) { + memoryRequired += pTransportVec[i].iov_len; + } + + return memoryRequired; +} + +/*-----------------------------------------------------------*/ + +void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem, MQTTVec_t *pVec, size_t len ) +{ + TransportOutVector_t * pTransportVec = (TransportOutVector_t*)pVec; + size_t index = 0; + size_t i = 0; + + for (i = 0; i < len; i++) { + memcpy(&pAllocatedMem[index], pTransportVec[i].iov_base, pTransportVec[i].iov_len); + index += pTransportVec[i].iov_len; + } +} + +/*-----------------------------------------------------------*/ diff --git a/source/include/core_mqtt.h b/source/include/core_mqtt.h index 38069274..828bb23c 100644 --- a/source/include/core_mqtt.h +++ b/source/include/core_mqtt.h @@ -343,6 +343,12 @@ typedef struct MQTTDeserializedInfo MQTTStatus_t deserializationResult; /**< @brief Return code of deserialization. */ } MQTTDeserializedInfo_t; +/** + * @ingroup mqtt_struct_types + * @brief An opaque structure provided by the library to the #MQTTStorePacketForRetransmit function when using #MQTTStorePacketForRetransmit. + */ +typedef struct MQTTVec MQTTVec_t; + /** * @brief Initialize an MQTT context. * @@ -1240,6 +1246,29 @@ MQTTStatus_t MQTT_GetSubAckStatusCodes( const MQTTPacketInfo_t * pSubackPacket, const char * MQTT_Status_strerror( MQTTStatus_t status ); /* @[declare_mqtt_status_strerror] */ +/** + * @brief Get the bytes in an array of #MQTTVec_t which can store the whole array as a an MQTT packet when calling MQTT_SerializeMQTTVec( void * pAllocatedMem, MQTTVec_t *pVec, size_t len ) function. + * + * @param[in] pVec The #MQTTVec_t array. + * @param[in] len The length of the #MQTTVec_t array. + * + * @return The bytes in the provided MQTTVec_t array which can then be used to set aside memory to be used with MQTT_SerializeMQTTVec( void * pAllocatedMem, MQTTVec_t *pVec, size_t len ) function. + */ +/* @[declare_mqtt_getbytesinmqttvec] */ +size_t MQTT_GetBytesInMQTTVec( MQTTVec_t *pVec, size_t len ); +/* @[declare_mqtt_getbytesinmqttvec] */ + +/** + * @brief Serialize the bytes in an array of #MQTTVec_t in the provided \p pAllocatedMem + * + * @param[in] pAllocatedMem Memory in which to serialize the data in the #MQTTVec_t array. It must be of size provided by MQTT_GetBytesInMQTTVec( MQTTVec_t *pVec, size_t len ). + * @param[in] pVec The #MQTTVec_t array. + * @param[in] len The length of the #MQTTVec_t array. + */ +/* @[declare_mqtt_serializemqttvec] */ +void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem, MQTTVec_t *pVec, size_t len ); +/* @[declare_mqtt_serializemqttvec] */ + /* *INDENT-OFF* */ #ifdef __cplusplus } From 7808d02ab3e30d74b7f63ea1e28fe0235dca784e Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 22 Oct 2024 02:33:49 +0000 Subject: [PATCH 2/3] Uncrustify: triggered by comment. --- source/core_mqtt.c | 25 +++++++++++++++---------- source/include/core_mqtt.h | 19 +++++++++++-------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/source/core_mqtt.c b/source/core_mqtt.c index b258daa3..5ab027b6 100644 --- a/source/core_mqtt.c +++ b/source/core_mqtt.c @@ -3732,14 +3732,16 @@ const char * MQTT_Status_strerror( MQTTStatus_t status ) /*-----------------------------------------------------------*/ -size_t MQTT_GetBytesInMQTTVec( MQTTVec_t *pVec, size_t len ) +size_t MQTT_GetBytesInMQTTVec( MQTTVec_t * pVec, + size_t len ) { size_t memoryRequired = 0; size_t i; - TransportOutVector_t * pTransportVec = (TransportOutVector_t*)pVec; - - for (i = 0; i < len; i++) { - memoryRequired += pTransportVec[i].iov_len; + TransportOutVector_t * pTransportVec = ( TransportOutVector_t * ) pVec; + + for( i = 0; i < len; i++ ) + { + memoryRequired += pTransportVec[ i ].iov_len; } return memoryRequired; @@ -3747,15 +3749,18 @@ size_t MQTT_GetBytesInMQTTVec( MQTTVec_t *pVec, size_t len ) /*-----------------------------------------------------------*/ -void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem, MQTTVec_t *pVec, size_t len ) +void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem, + MQTTVec_t * pVec, + size_t len ) { - TransportOutVector_t * pTransportVec = (TransportOutVector_t*)pVec; + TransportOutVector_t * pTransportVec = ( TransportOutVector_t * ) pVec; size_t index = 0; size_t i = 0; - for (i = 0; i < len; i++) { - memcpy(&pAllocatedMem[index], pTransportVec[i].iov_base, pTransportVec[i].iov_len); - index += pTransportVec[i].iov_len; + for( i = 0; i < len; i++ ) + { + memcpy( &pAllocatedMem[ index ], pTransportVec[ i ].iov_base, pTransportVec[ i ].iov_len ); + index += pTransportVec[ i ].iov_len; } } diff --git a/source/include/core_mqtt.h b/source/include/core_mqtt.h index 828bb23c..eef73276 100644 --- a/source/include/core_mqtt.h +++ b/source/include/core_mqtt.h @@ -655,7 +655,7 @@ MQTTStatus_t MQTT_CheckConnectStatus( MQTTContext_t * pContext ); * #MQTTNoDataAvailable if no data available to receive in transport until * the @p timeoutMs for CONNACK; * #MQTTStatusConnected if the connection is already established - * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect + * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect * before calling any other API * MQTTPublishClearAllFailed if on a clean session connection, clearing all the * previously copied publishes fails @@ -750,7 +750,7 @@ MQTTStatus_t MQTT_Connect( MQTTContext_t * pContext, * #MQTTBadParameter if invalid parameters are passed; * #MQTTSendFailed if transport write failed; * #MQTTStatusNotConnected if the connection is not established yet - * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect + * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect * before calling any other API * #MQTTSuccess otherwise. * @@ -806,7 +806,7 @@ MQTTStatus_t MQTT_Subscribe( MQTTContext_t * pContext, * #MQTTBadParameter if invalid parameters are passed; * #MQTTSendFailed if transport write failed; * #MQTTStatusNotConnected if the connection is not established yet - * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect + * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect * before calling any other API * #MQTTPublishStoreFailed if the user provided callback to copy and store the * outgoing publish packet fails @@ -878,7 +878,7 @@ MQTTStatus_t MQTT_CancelCallback( const MQTTContext_t * pContext, * #MQTTBadParameter if invalid parameters are passed; * #MQTTSendFailed if transport write failed; * #MQTTStatusNotConnected if the connection is not established yet - * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect + * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect * before calling any other API * #MQTTSuccess otherwise. */ @@ -900,7 +900,7 @@ MQTTStatus_t MQTT_Ping( MQTTContext_t * pContext ); * #MQTTBadParameter if invalid parameters are passed; * #MQTTSendFailed if transport write failed; * #MQTTStatusNotConnected if the connection is not established yet - * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect + * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect * before calling any other API * #MQTTSuccess otherwise. * @@ -990,7 +990,7 @@ MQTTStatus_t MQTT_Disconnect( MQTTContext_t * pContext ); * incomplete data; it should be called again (probably after a delay); * #MQTTStatusNotConnected if the connection is not established yet and a PING * or an ACK is being sent. - * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect + * #MQTTStatusDisconnectPending if the user is expected to call MQTT_Disconnect * before calling any other API * #MQTTSuccess on success. * @@ -1255,7 +1255,8 @@ const char * MQTT_Status_strerror( MQTTStatus_t status ); * @return The bytes in the provided MQTTVec_t array which can then be used to set aside memory to be used with MQTT_SerializeMQTTVec( void * pAllocatedMem, MQTTVec_t *pVec, size_t len ) function. */ /* @[declare_mqtt_getbytesinmqttvec] */ -size_t MQTT_GetBytesInMQTTVec( MQTTVec_t *pVec, size_t len ); +size_t MQTT_GetBytesInMQTTVec( MQTTVec_t * pVec, + size_t len ); /* @[declare_mqtt_getbytesinmqttvec] */ /** @@ -1266,7 +1267,9 @@ size_t MQTT_GetBytesInMQTTVec( MQTTVec_t *pVec, size_t len ); * @param[in] len The length of the #MQTTVec_t array. */ /* @[declare_mqtt_serializemqttvec] */ -void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem, MQTTVec_t *pVec, size_t len ); +void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem, + MQTTVec_t * pVec, + size_t len ); /* @[declare_mqtt_serializemqttvec] */ /* *INDENT-OFF* */ From d3b3c8ede5c45f126ff8448c2f726b85401d80fb Mon Sep 17 00:00:00 2001 From: AniruddhaKanhere <60444055+AniruddhaKanhere@users.noreply.github.com> Date: Tue, 22 Oct 2024 02:35:49 +0000 Subject: [PATCH 3/3] Add Doxygen comment --- source/core_mqtt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/core_mqtt.c b/source/core_mqtt.c index 5ab027b6..55afe35d 100644 --- a/source/core_mqtt.c +++ b/source/core_mqtt.c @@ -92,7 +92,7 @@ struct MQTTVec { - TransportOutVector_t pVector; + TransportOutVector_t pVector; /**< Pointer to transport vector. USER SHOULD NOT ACCESS THIS DIRECTLY - IT IS AN INTERNAL DETAIL AND CAN CHANGE. */ }; /*-----------------------------------------------------------*/