int32_t Sockets_Send()

in source/coreMQTT/sockets_wrapper.c [849:928]


int32_t Sockets_Send( Socket_t xSocket,
                      const void * pvBuffer,
                      size_t xDataLength )
{
    uint8_t * buf = ( uint8_t * ) pvBuffer;
    CellularSocketHandle_t cellularSocketHandle = NULL;
    BaseType_t retSendLength = 0;
    uint32_t sentLength = 0;
    CellularError_t socketStatus = CELLULAR_SUCCESS;
    cellularSocketWrapper_t * pCellularSocketContext = ( cellularSocketWrapper_t * ) xSocket;
    uint32_t bytesToSend = xDataLength;
    uint64_t entryTimeMs = getTimeMs();
    uint64_t elapsedTimeMs = 0;
    uint32_t sendTimeoutMs = 0;

    if( pCellularSocketContext == NULL )
    {
        IotLogError( "Cellular Sockets_Send Invalid xSocket %p", pCellularSocketContext );
        retSendLength = ( BaseType_t ) SOCKETS_SOCKET_ERROR;
    }
    else if( ( ( pCellularSocketContext->ulFlags & CELLULAR_SOCKET_OPEN_FLAG ) == 0U ) ||
             ( ( pCellularSocketContext->ulFlags & CELLULAR_SOCKET_CONNECT_FLAG ) == 0U ) )
    {
        IotLogError( "Cellular Sockets_Send Invalid xSocket flag %p 0x%08x",
                     pCellularSocketContext, pCellularSocketContext->ulFlags );
        retSendLength = ( BaseType_t ) SOCKETS_SOCKET_ERROR;
    }
    else
    {
        cellularSocketHandle = pCellularSocketContext->cellularSocketHandle;

        /* Convert ticks to ms delay. */
        if( ( pCellularSocketContext->sendTimeout >= UINT32_MAX_MS_TICKS ) || ( pCellularSocketContext->sendTimeout >= portMAX_DELAY ) )
        {
            /* Check if the ticks cause overflow. */
            sendTimeoutMs = UINT32_MAX_DELAY_MS;
        }
        else
        {
            sendTimeoutMs = TICKS_TO_MS( pCellularSocketContext->sendTimeout );
        }

        /* Loop sending data until data is sent completly or timeout. */
        while( bytesToSend > 0U )
        {
            socketStatus = Cellular_SocketSend( CellularHandle,
                                                cellularSocketHandle,
                                                &buf[ retSendLength ],
                                                bytesToSend,
                                                &sentLength );

            if( socketStatus == CELLULAR_SUCCESS )
            {
                retSendLength = retSendLength + ( BaseType_t ) sentLength;
                bytesToSend = bytesToSend - sentLength;
            }

            /* Check socket status or timeout break. */
            if( ( socketStatus != CELLULAR_SUCCESS ) ||
                ( _calculateElapsedTime( entryTimeMs, sendTimeoutMs, &elapsedTimeMs ) ) )
            {
                if( socketStatus == CELLULAR_SOCKET_CLOSED )
                {
                    /* Socket already closed. No data is sent. */
                    retSendLength = 0;
                }
                else if( socketStatus != CELLULAR_SUCCESS )
                {
                    retSendLength = ( BaseType_t ) SOCKETS_SOCKET_ERROR;
                }

                break;
            }
        }

        IotLogDebug( "Sockets_Send expect %d write %d", len, sentLength );
    }

    return retSendLength;
}