DefenderStatus_t Defender_MatchTopic()

in source/defender.c [462:569]


DefenderStatus_t Defender_MatchTopic( const char * pTopic,
                                      uint16_t topicLength,
                                      DefenderTopic_t * pOutApi,
                                      const char ** ppOutThingName,
                                      uint16_t * pOutThingNameLength )
{
    DefenderStatus_t ret = DefenderSuccess;
    uint16_t remainingTopicLength = 0U, consumedTopicLength = 0U, thingNameLength = 0U;

    if( ( pTopic == NULL ) || ( pOutApi == NULL ) )
    {
        ret = DefenderBadParameter;
        LogError( ( "Invalid input parameter. pTopic: %p, pOutApi: %p.",
                    ( void * ) pTopic,
                    ( void * ) pOutApi ) );
    }

    /* Nothing is consumed yet. */
    remainingTopicLength = topicLength;
    consumedTopicLength = 0;

    if( ret == DefenderSuccess )
    {
        ret = matchPrefix( &( pTopic[ consumedTopicLength ] ),
                           remainingTopicLength );

        if( ret == DefenderSuccess )
        {
            remainingTopicLength -= DEFENDER_API_LENGTH_PREFIX;
            consumedTopicLength += DEFENDER_API_LENGTH_PREFIX;
        }
        else
        {
            LogDebug( ( "The topic does not contain defender prefix $aws/things/." ) );
        }
    }

    if( ret == DefenderSuccess )
    {
        ret = extractThingNameLength( &( pTopic[ consumedTopicLength ] ),
                                      remainingTopicLength,
                                      &( thingNameLength ) );

        if( ret == DefenderSuccess )
        {
            remainingTopicLength -= thingNameLength;
            consumedTopicLength += thingNameLength;
        }
        else
        {
            LogDebug( ( "The topic does not contain a valid thing name." ) );
        }
    }

    if( ret == DefenderSuccess )
    {
        ret = matchBridge( &( pTopic[ consumedTopicLength ] ),
                           remainingTopicLength );

        if( ret == DefenderSuccess )
        {
            remainingTopicLength -= DEFENDER_API_LENGTH_BRIDGE;
            consumedTopicLength += DEFENDER_API_LENGTH_BRIDGE;
        }
        else
        {
            LogDebug( ( "The topic does not contain the defender bridge /defender/metrics/." ) );
        }
    }

    if( ret == DefenderSuccess )
    {
        ret = matchApi( &( pTopic[ consumedTopicLength ] ),
                        remainingTopicLength,
                        pOutApi );

        if( ret != DefenderSuccess )
        {
            LogDebug( ( "The topic does not contain valid report format or suffix "
                        " needed to be a valid defender topic." ) );
        }
    }

    /* Update the out parameters for thing name and thing length location, if we
     * successfully matched the topic. */
    if( ret == DefenderSuccess )
    {
        if( ppOutThingName != NULL )
        {
            /* Thing name comes after the defender prefix. */
            *ppOutThingName = &( pTopic[ DEFENDER_API_LENGTH_PREFIX ] );
        }

        if( pOutThingNameLength != NULL )
        {
            *pOutThingNameLength = thingNameLength;
        }
    }

    /* Update the output parameter for API if the topic did not match. In case
     * of a match, it is updated in the matchApi function. */
    if( ret == DefenderNoMatch )
    {
        *pOutApi = DefenderInvalidTopic;
    }

    return ret;
}