static uint16_t getRegisterThingTopicLength()

in source/fleet_provisioning.c [72:258]


static uint16_t getRegisterThingTopicLength( uint16_t templateNameLength,
                                             FleetProvisioningFormat_t format,
                                             FleetProvisioningApiTopics_t topic );

/**
 * @brief Write the given piece of the topic to the remaining buffer and advance
 * the remaining buffer pointer.
 *
 * The caller is responsible for assuring that there is enough space remaining
 * in the buffer to write the given string.
 *
 * @param[in,out] pBufferCursor Pointer to the remaining buffer.
 * @param[in] fragment The piece of the topic string to write.
 * @param[in] length The length of @p fragment.
 */
static void writeTopicFragmentAndAdvance( char ** pBufferCursor,
                                          const char * fragment,
                                          uint16_t length );

/**
 * @brief Check the parameters for FleetProvisioning_GetRegisterThingTopic().
 *
 * @param[in] pTopicBuffer The buffer to write the topic string into.
 * @param[in] format The desired RegisterThing format.
 * @param[in] topic The desired RegisterThing topic.
 * @param[in] pTemplateName The name of the provisioning template configured
 *     with AWS IoT.
 * @param[in] templateNameLength The length of @p pTemplateName.
 * @param[in] pOutLength The length of the topic string written to
 * the buffer.
 *
 * @return FleetProvisioningSuccess if no errors are found with the parameters;
 * FleetProvisioningBadParameter otherwise.
 */
static FleetProvisioningStatus_t GetRegisterThingTopicCheckParams( const char * pTopicBuffer,
                                                                   FleetProvisioningFormat_t format,
                                                                   FleetProvisioningApiTopics_t topic,
                                                                   const char * pTemplateName,
                                                                   uint16_t templateNameLength,
                                                                   const uint16_t * pOutLength );

/**
 * @brief Match the suffix from the remaining topic string and return the
 * corresponding suffix.
 *
 * Suffix: empty, /accepted, or /rejected.
 *
 * @param[in] pRemainingTopic The remaining portion of the topic.
 * @param[in] remainingLength The remaining length of the topic.
 *
 * @return The matching #TopicSuffix_t.
 */
static TopicSuffix_t parseTopicSuffix( const char * pRemainingTopic,
                                       uint16_t remainingLength );

/**
 * @brief Match the format and suffix from the remaining topic string and
 * return the corresponding format and suffix.
 *
 * Format: json or cbor.
 * Suffix: empty, /accepted, or /rejected.
 *
 * @param[in] pRemainingTopic The remaining portion of the topic.
 * @param[in] remainingLength The remaining length of the topic.
 *
 * @return The matching #TopicFormatSuffix_t.
 */
static TopicFormatSuffix_t parseTopicFormatSuffix( const char * pRemainingTopic,
                                                   uint16_t remainingLength );

/**
 * @brief Match a topic string with the CreateCertificateFromCsr topics.
 *
 * @param[in] pTopic The topic string to match.
 * @param[in] topicLength The length of the topic string.
 *
 * @return The matching #FleetProvisioningTopic_t if the topic string is a
 *     Fleet Provisioning CreateCertificateFromCsr topic, else
 *     FleetProvisioningInvalidTopic.
 */
static FleetProvisioningTopic_t parseCreateCertificateFromCsrTopic( const char * pTopic,
                                                                    uint16_t topicLength );

/**
 * @brief Match a topic string with the CreateKeysAndCertificate topics.
 *
 * @param[in] pTopic The topic string to match.
 * @param[in] topicLength The length of the topic string.
 *
 * @return The matching FleetProvisioningTopic_t if the topic string is a
 *     Fleet Provisioning CreateKeysAndCertificate topic, else
 *     FleetProvisioningInvalidTopic.
 */
static FleetProvisioningTopic_t parseCreateKeysAndCertificateTopic( const char * pTopic,
                                                                    uint16_t topicLength );

/**
 * @brief Match a topic string with the RegisterThing topics.
 *
 * @param[in] pTopic The topic string to match.
 * @param[in] topicLength The length of the topic string.
 *
 * @return The matching #FleetProvisioningTopic_t if the topic string is a
 *     Fleet Provisioning RegisterThing topic, else
 *     FleetProvisioningInvalidTopic.
 */
static FleetProvisioningTopic_t parseRegisterThingTopic( const char * pTopic,
                                                         uint16_t topicLength );

/**
 * @brief Check if the remaining buffer starts with a specified string. If so,
 * moves the remaining buffer pointer past the matched section and updates the
 * remaining length.
 *
 * @param[in,out] pBufferCursor Pointer to the remaining portion of the buffer.
 * @param[in,out] pRemainingLength The remaining length of the buffer.
 * @param[in] matchString The string to match against.
 * @param[in] matchLength The length of @p matchString.
 *
 * @return FleetProvisioningSuccess if the string matches and is skipped over;
 * FleetProvisioningNoMatch otherwise.
 */
static FleetProvisioningStatus_t consumeIfMatch( const char ** pBufferCursor,
                                                 uint16_t * pRemainingLength,
                                                 const char * matchString,
                                                 uint16_t matchLength );

/**
 * @brief Move the remaining topic pointer past the template name in the
 * unparsed topic so far, and update the remaining topic length.
 *
 * The end of thing name is marked by a forward slash. A zero length thing name
 * is not valid.
 *
 * This function extracts the same template name from the following topic strings:
 *   - $aws/provisioning-templates/TEMPLATE_NAME/provision/json/accepted
 *   - $aws/provisioning-templates/TEMPLATE_NAME
 * The second topic is not a valid Fleet Provisioning topic and the matching
 * will fail when we try to match the bridge part.
 *
 * @param[in,out] pTopicCursor Pointer to the remaining topic string.
 * @param[in,out] pRemainingLength Pointer to the length of the remaining topic string.
 *
 * @return FleetProvisioningSuccess if a valid template name is skipped over;
 * FleetProvisioningNoMatch otherwise.
 */
static FleetProvisioningStatus_t consumeTemplateName( const char ** pTopicCursor,
                                                      uint16_t * pRemainingLength );
/*-----------------------------------------------------------*/

static uint16_t getRegisterThingTopicLength( uint16_t templateNameLength,
                                             FleetProvisioningFormat_t format,
                                             FleetProvisioningApiTopics_t topic )
{
    uint16_t topicLength = 0U;

    assert( ( templateNameLength != 0U ) &&
            ( templateNameLength <= FP_TEMPLATENAME_MAX_LENGTH ) );
    assert( ( format == FleetProvisioningJson ) || ( format == FleetProvisioningCbor ) );
    assert( ( topic >= FleetProvisioningPublish ) && ( topic <= FleetProvisioningRejected ) );

    topicLength = FP_REGISTER_API_LENGTH_PREFIX +
                  templateNameLength +
                  FP_REGISTER_API_LENGTH_BRIDGE;

    if( format == FleetProvisioningJson )
    {
        topicLength += FP_API_LENGTH_JSON_FORMAT;
    }

    if( format == FleetProvisioningCbor )
    {
        topicLength += FP_API_LENGTH_CBOR_FORMAT;
    }

    if( topic == FleetProvisioningAccepted )
    {
        topicLength += FP_API_LENGTH_ACCEPTED_SUFFIX;
    }

    if( topic == FleetProvisioningRejected )
    {
        topicLength += FP_API_LENGTH_REJECTED_SUFFIX;
    }

    return topicLength;
}