static uint16_t getTopicLength()

in source/defender.c [44:180]


static uint16_t getTopicLength( uint16_t thingNameLength,
                                DefenderTopic_t api );

/**
 * @brief Write the format and suffix part for the given defender API to the buffer.
 *
 * Format: json or cbor.
 * Suffix: /accepted or /rejected or empty.
 *
 * @param[in] pBuffer The buffer to write the format and suffix part into.
 * @param[in] api The defender API value.
 *
 * @note This function assumes that the buffer is large enough to hold the
 * value.
 */
static void writeFormatAndSuffix( char * pBuffer,
                                  DefenderTopic_t api );

/**
 * @brief Check if the unparsed topic so far starts with the defender prefix.
 *
 * The defender prefix is "$aws/things/".
 *
 * @param[in] pRemainingTopic Starting location of the unparsed topic.
 * @param[in] remainingTopicLength The length of the unparsed topic.
 *
 * @return #DefenderSuccess if the unparsed topic starts with the defender
 * prefix; #DefenderNoMatch otherwise.
 */
static DefenderStatus_t matchPrefix( const char * pRemainingTopic,
                                     uint16_t remainingTopicLength );

/**
 * @brief Extract the length of thing name in the unparsed topic so far.
 *
 * The end of thing name is marked by a forward slash. A zero length thing name
 * is not valid.
 *
 * This function extracts the same thing name from the following topic strings:
 *   - $aws/things/THING_NAME/defender/metrics/json
 *   - $aws/things/THING_NAME
 * The second topic is not a valid defender topic and the matching will fail
 * when we try to match the bridge part.
 *
 * @param[in] pRemainingTopic Starting location of the unparsed topic.
 * @param[in] remainingTopicLength The length of the unparsed topic.
 * @param[out] pOutThingNameLength The length of the thing name in the topic string.
 *
 * @return #DefenderSuccess if a valid thing name is found; #DefenderNoMatch
 * otherwise.
 */
static DefenderStatus_t extractThingNameLength( const char * pRemainingTopic,
                                                uint16_t remainingTopicLength,
                                                uint16_t * pOutThingNameLength );

/**
 * @brief Check if the unparsed topic so far starts with the defender bridge.
 *
 * The defender bridge is "/defender/metrics/".
 *
 * @param[in] pRemainingTopic Starting location of the unparsed topic.
 * @param[in] remainingTopicLength The length of the unparsed topic.
 *
 * @return #DefenderSuccess if the unparsed topic starts with the defender
 * bridge; #DefenderNoMatch otherwise.
 */
static DefenderStatus_t matchBridge( const char * pRemainingTopic,
                                     uint16_t remainingTopicLength );

/**
 * @brief Check if the unparsed topic so far exactly matches one of the defender
 * api topics.
 *
 * The defender api topics are the following:
 *   - json
 *   - json/accepted
 *   - json/rejected
 *   - cbor
 *   - cbor/accepted
 *   - cbor/rejected
 *
 * The function also outputs the defender API value if a match is found.
 *
 * @param[in] pRemainingTopic Starting location of the unparsed topic.
 * @param[in] remainingTopicLength The length of the unparsed topic.
 * @param[out] pOutApi The defender topic API value.
 *
 * @return #DefenderSuccess if the unparsed topic exactly matches one of the
 * defender api topics; #DefenderNoMatch otherwise.
 */
static DefenderStatus_t matchApi( const char * pRemainingTopic,
                                  uint16_t remainingTopicLength,
                                  DefenderTopic_t * pOutApi );
/*-----------------------------------------------------------*/

static uint16_t getTopicLength( uint16_t thingNameLength,
                                DefenderTopic_t api )
{
    uint16_t topicLength = 0U;

    assert( ( thingNameLength != 0U ) && ( thingNameLength <= DEFENDER_THINGNAME_MAX_LENGTH ) );
    assert( ( api > DefenderInvalidTopic ) && ( api < DefenderMaxTopic ) );

    switch( api )
    {
        case DefenderJsonReportPublish:
            topicLength = DEFENDER_API_LENGTH_JSON_PUBLISH( thingNameLength );
            break;

        case DefenderJsonReportAccepted:
            topicLength = DEFENDER_API_LENGTH_JSON_ACCEPTED( thingNameLength );
            break;

        case DefenderJsonReportRejected:
            topicLength = DEFENDER_API_LENGTH_JSON_REJECTED( thingNameLength );
            break;

        case DefenderCborReportPublish:
            topicLength = DEFENDER_API_LENGTH_CBOR_PUBLISH( thingNameLength );
            break;

        case DefenderCborReportAccepted:
            topicLength = DEFENDER_API_LENGTH_CBOR_ACCEPTED( thingNameLength );
            break;

        /* The default is here just to silence compiler warnings in a way which
         * does not bring coverage down. The assert at the beginning of this
         * function ensures that the only api value hitting this case can be
         * DefenderCborReportRejected. */
        case DefenderCborReportRejected:
        default:
            topicLength = DEFENDER_API_LENGTH_CBOR_REJECTED( thingNameLength );
            break;
    }

    return topicLength;
}