Skip to content

Commit

Permalink
Add opaque struct and functions to serialize it.
Browse files Browse the repository at this point in the history
  • Loading branch information
AniruddhaKanhere committed Oct 22, 2024
1 parent 451e63c commit 4905c90
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
34 changes: 34 additions & 0 deletions source/core_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
*/
#define CORE_MQTT_UNSUBSCRIBE_PER_TOPIC_VECTOR_LENGTH ( 2U )

struct MQTTVec
{
TransportOutVector_t pVector;
};

/*-----------------------------------------------------------*/

/**
Expand Down Expand Up @@ -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;
}
}

/*-----------------------------------------------------------*/
29 changes: 29 additions & 0 deletions source/include/core_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 4905c90

Please sign in to comment.