Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unit tests #212

Merged
merged 23 commits into from
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/doxygen/include/size_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</tr>
<tr>
<td>core_mqtt_state.c</td>
<td><center>1.6K</center></td>
<td><center>1.2K</center></td>
<td><center>1.7K</center></td>
<td><center>1.3K</center></td>
</tr>
<tr>
<td>core_mqtt_serializer.c</td>
Expand All @@ -24,7 +24,7 @@
</tr>
<tr>
<td><b>Total estimates</b></td>
<td><b><center>8.3K</center></b></td>
<td><b><center>6.7K</center></b></td>
<td><b><center>8.4K</center></b></td>
<td><b><center>6.8K</center></b></td>
</tr>
</table>
90 changes: 48 additions & 42 deletions source/core_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ static TransportOutVector_t * addEncodedStringToVector( uint8_t serailizedLength
* @note All the arrays must stay in scope until the message contained in the vector has
* been sent.
*/
static void addWillAndConnectInfo( const MQTTConnectInfo_t * pConnectInfo,
const MQTTPublishInfo_t * pWillInfo,
size_t * pTotalMessageLength,
TransportOutVector_t * iterator,
uint8_t serializedTopicLength[ 2 ],
uint8_t serializedPayloadLength[ 2 ],
uint8_t serializedUsernameLength[ 2 ],
uint8_t serializedPasswordLength[ 2 ] );
static TransportOutVector_t * addWillAndConnectInfo( const MQTTConnectInfo_t * pConnectInfo,
const MQTTPublishInfo_t * pWillInfo,
size_t * pTotalMessageLength,
TransportOutVector_t * iterator,
uint8_t serializedTopicLength[ 2 ],
uint8_t serializedPayloadLength[ 2 ],
uint8_t serializedUsernameLength[ 2 ],
uint8_t serializedPasswordLength[ 2 ] );

/**
* @brief Send MQTT SUBSCRIBE message without copying the user data into a buffer and
Expand Down Expand Up @@ -835,7 +835,7 @@ static int32_t sendMessageVector( MQTTContext_t * pContext,
if( ( bytesSentThisVector > 0U ) &&
( pIoVectIterator < &( pIoVec[ ioVecCount ] ) ) )
{
( *( ( uint8_t * ) pIoVectIterator->iov_base ) ) += bytesSentThisVector;
pIoVectIterator->iov_base = ( void * ) &( ( ( uint8_t * ) pIoVectIterator->iov_base )[ bytesSentThisVector ] );
pIoVectIterator->iov_len -= bytesSentThisVector;
}
}
Expand Down Expand Up @@ -1042,7 +1042,9 @@ static MQTTStatus_t discardPacket( const MQTTContext_t * pContext,
MQTTStatus_t status = MQTTRecvFailed;
int32_t bytesReceived = 0;
size_t bytesToReceive = 0U;
uint32_t totalBytesReceived = 0U, entryTimeMs = 0U, elapsedTimeMs = 0U;
uint32_t totalBytesReceived = 0U;
uint32_t entryTimeMs = 0U;
uint32_t elapsedTimeMs = 0U;
MQTTGetCurrentTimeFunc_t getTimeStampMs = NULL;
bool receiveError = false;

Expand Down Expand Up @@ -1334,7 +1336,8 @@ static MQTTStatus_t sendPublishAcks( MQTTContext_t * pContext,
static MQTTStatus_t handleKeepAlive( MQTTContext_t * pContext )
{
MQTTStatus_t status = MQTTSuccess;
uint32_t now = 0U, packetTxTimeoutMs = 0U;
uint32_t now = 0U;
uint32_t packetTxTimeoutMs = 0U;

assert( pContext != NULL );
assert( pContext->getTime != NULL );
Expand Down Expand Up @@ -1651,7 +1654,6 @@ static MQTTStatus_t handleIncomingAck( MQTTContext_t * pContext,

return status;
}

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

static MQTTStatus_t receiveSingleIteration( MQTTContext_t * pContext,
Expand Down Expand Up @@ -1727,7 +1729,7 @@ static MQTTStatus_t receiveSingleIteration( MQTTContext_t * pContext,
}
else
{
/* MISRA else */
/* MISRA else. */
}

/* Handle received packet. If incomplete data was read then this will not execute. */
Expand Down Expand Up @@ -1900,6 +1902,10 @@ static MQTTStatus_t sendSubscribeWithoutCopy( MQTTContext_t * pContext,
/* Lastly, the QoS gets sent. */
pIterator->iov_base = &( pSubscriptionList[ subscriptionsSent ].qos );
pIterator->iov_len = 1U;
totalPacketLength += pIterator->iov_len;

/* Increment the pointer. */
pIterator++;

/* Two slots get used by the topic string length and topic string. And
* one slot gets used by the quality of service. */
Expand All @@ -1908,7 +1914,9 @@ static MQTTStatus_t sendSubscribeWithoutCopy( MQTTContext_t * pContext,
subscriptionsSent++;
}

if( sendMessageVector( pContext, pIoVector, ioVectorLength ) != ( int32_t ) totalPacketLength )
if( sendMessageVector( pContext,
pIoVector,
ioVectorLength ) != ( int32_t ) totalPacketLength )
{
status = MQTTSendFailed;
}
Expand Down Expand Up @@ -2060,14 +2068,14 @@ static MQTTStatus_t sendPublishWithoutCopy( MQTTContext_t * pContext,

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

static void addWillAndConnectInfo( const MQTTConnectInfo_t * pConnectInfo,
const MQTTPublishInfo_t * pWillInfo,
size_t * pTotalMessageLength,
TransportOutVector_t * iterator,
uint8_t serializedTopicLength[ 2 ],
uint8_t serializedPayloadLength[ 2 ],
uint8_t serializedUsernameLength[ 2 ],
uint8_t serializedPasswordLength[ 2 ] )
static TransportOutVector_t * addWillAndConnectInfo( const MQTTConnectInfo_t * pConnectInfo,
const MQTTPublishInfo_t * pWillInfo,
size_t * pTotalMessageLength,
TransportOutVector_t * iterator,
uint8_t serializedTopicLength[ 2 ],
uint8_t serializedPayloadLength[ 2 ],
uint8_t serializedUsernameLength[ 2 ],
uint8_t serializedPasswordLength[ 2 ] )
{
if( pWillInfo != NULL )
{
Expand Down Expand Up @@ -2107,6 +2115,8 @@ static void addWillAndConnectInfo( const MQTTConnectInfo_t * pConnectInfo,
iterator,
pTotalMessageLength );
}

return iterator;
}

/*-----------------------------------------------------------*/
Expand Down Expand Up @@ -2135,13 +2145,7 @@ static MQTTStatus_t sendConnectWithoutCopy( MQTTContext_t * pContext,
iterator = pIoVector;

/* Validate arguments. */
if( pConnectInfo == NULL )
{
LogError( ( "Argument cannot be NULL: pConnectInfo=%p.",
( void * ) pConnectInfo ) );
status = MQTTBadParameter;
}
else if( ( pWillInfo != NULL ) && ( pWillInfo->pTopicName == NULL ) )
if( ( pWillInfo != NULL ) && ( pWillInfo->pTopicName == NULL ) )
{
LogError( ( "pWillInfo->pTopicName cannot be NULL if Will is present." ) );
status = MQTTBadParameter;
Expand All @@ -2168,14 +2172,14 @@ static MQTTStatus_t sendConnectWithoutCopy( MQTTContext_t * pContext,
iterator,
&totalMessageLength );

addWillAndConnectInfo( pConnectInfo,
pWillInfo,
&totalMessageLength,
iterator,
serializedTopicLength,
serializedPayloadLength,
serializedUsernameLength,
serializedPasswordLength );
iterator = addWillAndConnectInfo( pConnectInfo,
pWillInfo,
&totalMessageLength,
iterator,
serializedTopicLength,
serializedPayloadLength,
serializedUsernameLength,
serializedPasswordLength );

ioVectorLength = ( size_t ) ( iterator - pIoVector );

Expand Down Expand Up @@ -2484,17 +2488,17 @@ MQTTStatus_t MQTT_InitStatefulQoS( MQTTContext_t * pContext,
( pOutgoingPublishRecords == NULL ) )
{
LogError( ( "Arguments do not match: pOutgoingPublishRecords=%p, "
"outgoingPublishCount=%u",
pOutgoingPublishRecords,
"outgoingPublishCount=%lu",
( void * ) pOutgoingPublishRecords,
outgoingPublishCount ) );
status = MQTTBadParameter;
}
else if( ( incomingPublishCount == 0 ) ^
( pIncomingPublishRecords == NULL ) )
{
LogError( ( "Arguments do not match: pIncomingPublishRecords=%p, "
"incomingPublishCount=%u",
pIncomingPublishRecords,
"incomingPublishCount=%lu",
( void * ) pIncomingPublishRecords,
incomingPublishCount ) );
status = MQTTBadParameter;
}
Expand Down Expand Up @@ -2679,7 +2683,9 @@ MQTTStatus_t MQTT_Publish( MQTTContext_t * pContext,
const MQTTPublishInfo_t * pPublishInfo,
uint16_t packetId )
{
size_t headerSize = 0UL, remainingLength = 0UL, packetSize = 0UL;
size_t headerSize = 0UL;
size_t remainingLength = 0UL;
size_t packetSize = 0UL;
MQTTPublishState_t publishStatus = MQTTStateNull;
bool stateUpdateHookExecuted = false;

Expand Down
9 changes: 2 additions & 7 deletions source/core_mqtt_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,6 @@ MQTTStatus_t MQTT_SerializePublishHeaderWithoutTopic( const MQTTPublishInfo_t *

*headerSize = headerLength;

if( headerLength > 7 )
{
status = MQTTBadParameter;
}

return status;
}

Expand Down Expand Up @@ -1568,11 +1563,11 @@ uint8_t * MQTT_SerializeConnectFixedHeader( uint8_t * pIndex,
/* The remaining length of the CONNECT packet is encoded starting from the
* second byte. The remaining length does not include the length of the fixed
* header or the encoding of the remaining length. */
pIndexLocal = encodeRemainingLength( pIndex, remainingLength );
pIndexLocal = encodeRemainingLength( pIndexLocal, remainingLength );

/* The string "MQTT" is placed at the beginning of the CONNECT packet's variable
* header. This string is 4 bytes long. */
pIndexLocal = encodeString( pIndex, "MQTT", 4 );
pIndexLocal = encodeString( pIndexLocal, "MQTT", 4 );

/* The MQTT protocol version is the second field of the variable header. */
*pIndexLocal = MQTT_VERSION_3_1_1;
Expand Down
15 changes: 10 additions & 5 deletions source/core_mqtt_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ static size_t findInRecord( const MQTTPubAckInfo_t * records,
}
}

if( index == recordCount )
{
index = MQTT_INVALID_STATE_COUNT;
}

return index;
}

Expand Down Expand Up @@ -791,7 +796,10 @@ static MQTTStatus_t updateStatePublish( MQTTContext_t * pMqttContext,
* update is required. */
if( currentState != newState )
{
updateRecord( pMqttContext->outgoingPublishRecords, recordIndex, newState, false );
updateRecord( pMqttContext->outgoingPublishRecords,
recordIndex,
newState,
false );
}
}
}
Expand Down Expand Up @@ -1004,8 +1012,6 @@ MQTTStatus_t MQTT_UpdateStateAck( MQTTContext_t * pMqttContext,
MQTTPubAckInfo_t * records = NULL;
MQTTStatus_t status = MQTTBadResponse;



if( ( pMqttContext == NULL ) || ( pNewState == NULL ) )
{
LogError( ( "Argument cannot be NULL: pMqttContext=%p, pNewState=%p.",
Expand Down Expand Up @@ -1043,8 +1049,7 @@ MQTTStatus_t MQTT_UpdateStateAck( MQTTContext_t * pMqttContext,
&currentState );
}

if( ( recordIndex < MQTT_INVALID_STATE_COUNT ) &&
( recordIndex < maxRecordCount ) )
if( recordIndex != MQTT_INVALID_STATE_COUNT )
{
newState = MQTT_CalculateStateAck( packetType, opType, qos );

Expand Down
8 changes: 1 addition & 7 deletions source/include/core_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,13 @@
#define MQTT_LIBRARY_VERSION "v1.2.0"
/** @endcond */

/**
* @ingroup mqtt_constants
* @brief Maximum number of vectors in subscribe and unsubscribe packet.
*/
#define MQTT_SUB_UNSUB_MAX_VECTORS ( 4U )

/**
* @ingroup mqtt_constants
* @brief Invalid packet identifier.
*
* Zero is an invalid packet identifier as per MQTT v3.1.1 spec.
*/
#define MQTT_PACKET_ID_INVALID ( ( uint16_t ) 0U )
#define MQTT_PACKET_ID_INVALID ( ( uint16_t ) 0U )

/* Structures defined in this file. */
struct MQTTPubAckInfo;
Expand Down
8 changes: 8 additions & 0 deletions source/include/core_mqtt_config_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
#define MQTT_DO_NOT_USE_CUSTOM_CONFIG
#endif

/**
* @ingroup mqtt_constants
* @brief Maximum number of vectors in subscribe and unsubscribe packet.
*/
#ifndef MQTT_SUB_UNSUB_MAX_VECTORS
#define MQTT_SUB_UNSUB_MAX_VECTORS ( 4U )
#endif

/**
* @brief The number of retries for receiving CONNACK.
*
Expand Down
2 changes: 2 additions & 0 deletions test/unit-test/core_mqtt_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ struct NetworkContext
uint8_t ** buffer;
};

#define MQTT_SUB_UNSUB_MAX_VECTORS ( 8U )

#endif /* ifndef CORE_MQTT_CONFIG_H_ */
Loading