static uint32_t prvBuildStatusMessageFinish()

in source/ota_mqtt.c [696:833]


static uint32_t prvBuildStatusMessageFinish( char * pMsgBuffer,
                                             size_t msgBufferSize,
                                             OtaJobStatus_t status,
                                             int32_t reason,
                                             int32_t subReason,
                                             uint32_t previousVersion )
{
    uint32_t msgSize = 0;
    char reasonString[ U32_MAX_LEN + 1 ];
    char subReasonString[ U32_MAX_LEN + 1 ];
    char newVersionMajorString[ U32_MAX_LEN + 1 ];
    char newVersionMinorString[ U32_MAX_LEN + 1 ];
    char newVersionBuildString[ U32_MAX_LEN + 1 ];
    char prevVersionMajorString[ U32_MAX_LEN + 1 ];
    char prevVersionMinorString[ U32_MAX_LEN + 1 ];
    char prevVersionBuildString[ U32_MAX_LEN + 1 ];

    AppVersion32_t newVersion = { 0 }, prevVersion = { 0 };

    /* NULL-terminated list of payload string parts */
    /* NOTE: this must conform to the following format, do not add spaces, etc. */
    /*       "\"reason\":\"0x%08x: 0x%08x\"}}" */

    const char * pPayloadPartsStatusFailedWithValue[] =
    {
        NULL, /* Job status string not available at compile time, initialized below. */
        "\"reason\":\"0x",
        NULL, /* Reason string not available at compile time, initialized below. */
        ": 0x",
        NULL, /* Sub-Reason string not available at compile time, initialized below. */
        "\"}}",
        NULL
    };
    /* NULL-terminated list of payload string parts */
    /* NOTE: this must agree with the following format, do not add spaces, etc. */
    /*       "\"reason\":\"%s v%u.%u.%u\"}}" */
    const char * pPayloadPartsStatusSucceeded[] =
    {
        NULL,                                         /* Job status string not available at compile time, initialized below. */
        "\"reason\":\"",
        NULL,                                         /*  Reason string not available at compile time, initialized below. */
        "  v",
        NULL,                                         /* Version major string not available at compile time, initialized below. */
        ".",
        NULL,                                         /* Version minor string not available at compile time, initialized below. */
        ".",
        NULL,                                         /* Version build string not available at compile time, initialized below. */
        "\",\"" OTA_JSON_UPDATED_BY_KEY_ONLY "\":\"", /* Expands to `","updatedBy":` */
        "  v",
        NULL,                                         /* Previous version major string not available at compile time, initialized below. */
        ".",
        NULL,                                         /* Previous version minor string not available at compile time, initialized below. */
        ".",
        NULL,                                         /* Previous version build string not available at compile time, initialized below. */
        "\"}}",
        NULL
    };

    /* NULL-terminated list of payload string parts */
    /* NOTE: this must agree with the following format, do not add spaces, etc. */
    /*       "\"reason\":\"%s: 0x%08x\"}}" */
    const char * pPayloadPartsStatusOther[] =
    {
        NULL, /* Job status string not available at compile time, initialized below. */
        "\"reason\":\"",
        NULL, /*  Reason string not available at compile time, initialized below. */
        ": 0x",
        NULL, /* Sub-Reason string not available at compile time, initialized below. */
        "\"}}",
        NULL
    };
    const char ** pPayloadParts;

    assert( pMsgBuffer != NULL );

    newVersion.u.signedVersion32 = subReason;
    prevVersion.u.signedVersion32 = ( int32_t ) previousVersion;

    ( void ) stringBuilderUInt32Hex( reasonString, sizeof( reasonString ), ( uint32_t ) reason );
    ( void ) stringBuilderUInt32Hex( subReasonString, sizeof( subReasonString ), ( uint32_t ) subReason );

    /* FailedWithVal uses a numeric OTA error code and sub-reason code to cover
     * the case where there may be too many description strings to reasonably
     * include in the code.
     */
    if( status == JobStatusFailedWithVal )
    {
        pPayloadParts = pPayloadPartsStatusFailedWithValue;
        pPayloadParts[ 0 ] = pOtaJobStatusStrings[ status ];
        pPayloadParts[ 2 ] = reasonString;
        pPayloadParts[ 4 ] = subReasonString;
    }

    /* If the status update is for "Succeeded," we are identifying the version
     * of firmware that has been accepted. This makes it easy to find the
     * version associated with each device (Thing) when examining the OTA jobs
     * on the service side via the CLI or possibly with some console tool.
     */
    else if( status == JobStatusSucceeded )
    {
        /* New version string.*/
        ( void ) stringBuilderUInt32Decimal( newVersionMajorString, sizeof( newVersionMajorString ), newVersion.u.x.major );
        ( void ) stringBuilderUInt32Decimal( newVersionMinorString, sizeof( newVersionMinorString ), newVersion.u.x.minor );
        ( void ) stringBuilderUInt32Decimal( newVersionBuildString, sizeof( newVersionBuildString ), newVersion.u.x.build );

        /* Updater version string.*/
        ( void ) stringBuilderUInt32Decimal( prevVersionMajorString, sizeof( prevVersionMajorString ), prevVersion.u.x.major );
        ( void ) stringBuilderUInt32Decimal( prevVersionMinorString, sizeof( prevVersionMinorString ), prevVersion.u.x.minor );
        ( void ) stringBuilderUInt32Decimal( prevVersionBuildString, sizeof( prevVersionMinorString ), prevVersion.u.x.build );

        pPayloadParts = pPayloadPartsStatusSucceeded;
        pPayloadParts[ 0 ] = pOtaJobStatusStrings[ status ];
        pPayloadParts[ 2 ] = pOtaJobReasonStrings[ reason ];
        pPayloadParts[ 4 ] = newVersionMajorString;
        pPayloadParts[ 6 ] = newVersionMinorString;
        pPayloadParts[ 8 ] = newVersionBuildString;
        pPayloadParts[ 11 ] = prevVersionMajorString;
        pPayloadParts[ 13 ] = prevVersionMinorString;
        pPayloadParts[ 15 ] = prevVersionBuildString;
    }
    else
    {
        pPayloadParts = pPayloadPartsStatusOther;
        pPayloadParts[ 0 ] = pOtaJobStatusStrings[ status ];
        pPayloadParts[ 2 ] = pOtaJobReasonStrings[ reason ];
        pPayloadParts[ 4 ] = subReasonString;
    }

    msgSize = ( uint32_t ) stringBuilder(
        pMsgBuffer,
        msgBufferSize,
        pPayloadParts );

    /* The buffer is static and the size is calculated to fit. */
    assert( ( msgSize > 0U ) && ( msgSize < msgBufferSize ) );

    return msgSize;
}