SntpStatus_t Sntp_ReceiveTimeResponse()

in source/core_sntp_client.c [815:889]


SntpStatus_t Sntp_ReceiveTimeResponse( SntpContext_t * pContext,
                                       uint32_t blockTimeMs )
{
    SntpStatus_t status = SntpNoResponseReceived;
    bool hasResponseTimedOut = false;

    /* Validate the context parameter. */
    status = validateContext( pContext );

    if( status == SntpSuccess )
    {
        SntpTimestamp_t startTime, currentTime;
        const SntpTimestamp_t * pRequestTime = &pContext->lastRequestTime;
        bool shouldRetry = false;

        /* Record time before read attempts so that it can be used as base time for
         * for tracking the block time window across read retries. */
        pContext->getTimeFunc( &startTime );

        do
        {
            /* Reset the retry read operation flag. If the server response is not received in the current iteration's read
             * attempt and the wait has not timed out, the flag will be set to perform a retry. */
            shouldRetry = false;

            /* Make an attempt to read the server response from the network. */
            status = receiveSntpResponse( &pContext->networkIntf,
                                          pContext->currentServerAddr,
                                          pContext->pTimeServers[ pContext->currentServerIndex ].port,
                                          pContext->pNetworkBuffer,
                                          pContext->sntpPacketSize );

            /* If the server response is received, deserialize it, validate the server
             * (if authentication interface is provided), and update system time with
             * the calculated clock offset. */
            if( status == SntpSuccess )
            {
                /* Get current time to de-serialize the receive server response packet. */
                pContext->getTimeFunc( &currentTime );

                status = processServerResponse( pContext, &currentTime );
            }
            else if( status == SntpNoResponseReceived )
            {
                /* Get current time to determine whether another attempt for reading the packet can
                 * be made. */
                pContext->getTimeFunc( &currentTime );

                /* Set the flag to retry read of server response from the network. */
                shouldRetry = decideAboutReadRetry( &currentTime,
                                                    &startTime,
                                                    pRequestTime,
                                                    pContext->responseTimeoutMs,
                                                    blockTimeMs,
                                                    &hasResponseTimedOut );
            }
            else
            {
                /* Empty else marker. */
            }
        } while( shouldRetry == true );

        /* If the wait for server response to the time request has timed out, rotate the server of use in the
         * context for subsequent time request(s). Also, update the return status to indicate response timeout. */
        if( hasResponseTimedOut == true )
        {
            status = SntpErrorResponseTimeout;

            /* Rotate server to the next in the list of configured servers in the context. */
            rotateServerForNextTimeQuery( pContext );
        }
    }

    return status;
}