in source/core_mqtt_agent.c [308:372]
static MQTTStatus_t addAwaitingOperation( MQTTAgentContext_t * pAgentContext,
uint16_t packetId,
MQTTAgentCommand_t * pCommand )
{
size_t i = 0, unusedPos = MQTT_AGENT_MAX_OUTSTANDING_ACKS;
MQTTStatus_t status = MQTTNoMemory;
MQTTAgentAckInfo_t * pendingAcks = NULL;
assert( pAgentContext != NULL );
assert( pCommand != NULL );
assert( packetId != MQTT_PACKET_ID_INVALID );
pendingAcks = pAgentContext->pPendingAcks;
/* Before adding the record for the pending acknowledgement of the packet ID,
* make sure that there doesn't already exist an entry for the same packet ID.
* Also, as part of iterating through the list of pending acknowledgements,
* find an unused space for the packet ID to be added, if it can be. */
for( i = 0; i < MQTT_AGENT_MAX_OUTSTANDING_ACKS; i++ )
{
/* If the packetId is MQTT_PACKET_ID_INVALID then the array space is not in
* use. */
if( ( unusedPos == MQTT_AGENT_MAX_OUTSTANDING_ACKS ) &&
( pendingAcks[ i ].packetId == MQTT_PACKET_ID_INVALID ) )
{
unusedPos = i;
status = MQTTSuccess;
}
if( pendingAcks[ i ].packetId == packetId )
{
/* Check whether there exists a duplicate entry for pending
* acknowledgment for the same packet ID that we want to add to
* the list.
* Note: This is an unlikely edge case which represents that a packet ID
* didn't receive acknowledgment, but subsequent SUBSCRIBE/PUBLISH operations
* representing 65535 packet IDs were successful that caused the bit packet
* ID value to wrap around and reached the same packet ID as that was still
* pending acknowledgment.
*/
status = MQTTStateCollision;
LogError( ( "Failed to add operation to list of pending acknowledgments: "
"Existing entry found for same packet: PacketId=%u\n", packetId ) );
break;
}
}
/* Add the packet ID to the list if there is space available, and there is no
* duplicate entry for the same packet ID found. */
if( status == MQTTSuccess )
{
pendingAcks[ unusedPos ].packetId = packetId;
pendingAcks[ unusedPos ].pOriginalCommand = pCommand;
}
else if( status == MQTTNoMemory )
{
LogError( ( "Failed to add operation to list of pending acknowledgments: "
"No memory available: PacketId=%u\n", packetId ) );
}
else
{
/* Empty else MISRA 15.7 */
}
return status;
}