Fleet Provisioning Demo for ESP32C3- "Received rejected response from Fleet Provisioning RegisterThing API"

I am trying to Implement Fleet Provisioning using the SDK : esp-aws-iot/examples/fleet_provisioning/fleet_provisioning_with_csr at master · espressif/esp-aws-iot · GitHub
But Unfortunately the AWS Configuration seems to be a problem and I can’t figure it out. I am getting this two errors and resulting in failure of the fleet provisioning. I would appreciate your help.
thanks.

I (8697) coreMQTT: MQTT connection established with the broker.
I (8707) FleetProvisioning: Established connection with claim credentials.
I (9357) corePKCS11: NOTE: define GENERATED_PRIVATE_KEY_WRITE_PATH in order to have the private key written to disk.
I (9457) TX_TASK: Wrote 30 bytes
I (9727) coreMQTT: Ack packet deserialized with result: MQTTSuccess.
I (9727) coreMQTT: State record updated. New state=MQTTPublishDone.
I (9937) coreMQTT: De-serialized incoming PUBLISH packet: DeserializerResult=MQTTSuccess.
I (9937) coreMQTT: State record updated. New state=MQTTPubAckSend.
I (9947) FleetProvisioning: Received accepted response from Fleet Provisioning CreateCertificateFromCsr API.
I (11457) TX_TASK: Wrote 30 bytes
I (13457) TX_TASK: Wrote 30 bytes
I (14987) FleetProvisioning: Received certificate with Id: 76ffa6d9fa97b04cb9c6ae8c1f6965580fdd0769c846cd94279c88383a291b78
I (14997) corePKCS11: Writing certificate into label "Device Cert".
I (14997) corePKCS11: Creating a 0x1 type object.
I (15457) TX_TASK: Wrote 30 bytes
I (17457) TX_TASK: Wrote 30 bytes
I (18237) coreMQTT: Ack packet deserialized with result: MQTTSuccess.
I (18237) coreMQTT: State record updated. New state=MQTTPublishDone.
I (18347) coreMQTT: De-serialized incoming PUBLISH packet: DeserializerResult=MQTTSuccess.
I (18347) coreMQTT: State record updated. New state=MQTTPubAckSend.
E (18347) FleetProvisioning: Received rejected response from Fleet Provisioning RegisterThing API.
E (18367) FleetProvisioning: Payload: {_ "statusCode": 400, "errorCode": "InvalidResult", "errorMessage": "Provisioning hook result is in an invalid format."}

@MahadiHasanTauhid

From the logs, it looks like the lamda function used as the pre-provisioning hook is not returning results in the expected format.

Have you added a custom hook function as lambda? Can you check if the response from the lambda is in the expected format?

I have pre-provisioning lambda function that I just copied from the link you share.
`import json

def pre_provisioning_hook(event, context):
print(event)

return {
    'allowProvisioning': True,
    'parameterOverrides': {
        'DeviceLocation': 'Bangkok'
    }
}`
Status: Succeeded
Test Event Name: (unsaved) test event

Response:
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}

Function Logs:
START RequestId: 8e69eeb8-ec77-40a4-bd05-3213d979c594 Version: $LATEST
END RequestId: 8e69eeb8-ec77-40a4-bd05-3213d979c594
REPORT RequestId: 8e69eeb8-ec77-40a4-bd05-3213d979c594	Duration: 1.76 ms	Billed Duration: 2 ms	Memory Size: 128 MB	Max Memory Used: 30 MB	Init Duration: 104.07 ms

Request ID: 8e69eeb8-ec77-40a4-bd05-3213d979c594

I did give the certificateid, certificate pem, csr, serial number above used “test” on the console and the above log was the output.
But I’m not familiar with the testing the expected format? what is the correct way to do that on console?

Where is this response coming from?

I think I’m not sure, probably some preset I had in my account for other purposes. I would like to know how to check the response from the console.

static void provisioningPublishCallback( MQTTPublishInfo_t * pPublishInfo,
                                         uint16_t packetIdentifier )
{
    FleetProvisioningStatus_t status;
    FleetProvisioningTopic_t api;
    const char * cborDump;

    /* Silence compiler warnings about unused variables. */
    ( void ) packetIdentifier;

    status = FleetProvisioning_MatchTopic( pPublishInfo->pTopicName,
                                           pPublishInfo->topicNameLength, &api );

    if( status != FleetProvisioningSuccess )
    {
        LogWarn( ( "Unexpected publish message received. Topic: %.*s.",
                   ( int ) pPublishInfo->topicNameLength,
                   ( const char * ) pPublishInfo->pTopicName ) );
    }
    else
    {
        if( api == FleetProvCborCreateCertFromCsrAccepted )
        {
            LogInfo( ( "Received accepted response from Fleet Provisioning CreateCertificateFromCsr API." ) );

            cborDump = getStringFromCbor( ( const uint8_t * ) pPublishInfo->pPayload, pPublishInfo->payloadLength );
            LogDebug( ( "Payload: %s", cborDump ) );
            free( ( void * ) cborDump );

            responseStatus = ResponseAccepted;

            /* Copy the payload from the MQTT library's buffer to #payloadBuffer. */
            ( void ) memcpy( ( void * ) payloadBuffer,
                             ( const void * ) pPublishInfo->pPayload,
                             ( size_t ) pPublishInfo->payloadLength );

            payloadLength = pPublishInfo->payloadLength;
        }
        else if( api == FleetProvCborCreateCertFromCsrRejected )
        {
            LogError( ( "Received rejected response from Fleet Provisioning CreateCertificateFromCsr API." ) );

            cborDump = getStringFromCbor( ( const uint8_t * ) pPublishInfo->pPayload, pPublishInfo->payloadLength );
            LogError( ( "Payload: %s", cborDump ) );
            free( ( void * ) cborDump );

            responseStatus = ResponseRejected;
        }
        else if( api == FleetProvCborRegisterThingAccepted )
        {
            LogInfo( ( "Received accepted response from Fleet Provisioning RegisterThing API." ) );

            cborDump = getStringFromCbor( ( const uint8_t * ) pPublishInfo->pPayload, pPublishInfo->payloadLength );
            LogDebug( ( "Payload: %s", cborDump ) );
            free( ( void * ) cborDump );

            responseStatus = ResponseAccepted;

            /* Copy the payload from the MQTT library's buffer to #payloadBuffer. */
            ( void ) memcpy( ( void * ) payloadBuffer,
                             ( const void * ) pPublishInfo->pPayload,
                             ( size_t ) pPublishInfo->payloadLength );

            payloadLength = pPublishInfo->payloadLength;
        }
        else if( api == FleetProvCborRegisterThingRejected )
        {
            LogError( ( "Received rejected response from Fleet Provisioning RegisterThing API." ) );

            cborDump = getStringFromCbor( ( const uint8_t * ) pPublishInfo->pPayload, pPublishInfo->payloadLength );
            LogError( ( "Payload: %s", cborDump ) );
            free( ( void * ) cborDump );

            responseStatus = ResponseRejected;
        }
        else
        {
            LogError( ( "Received message on unexpected Fleet Provisioning topic. Topic: %.*s.",
                        ( int ) pPublishInfo->topicNameLength,
                        ( const char * ) pPublishInfo->pTopicName ) );
        }
    }
}

This the code block from the sdk is reponsible for what is happening with the code and aws cloud platform interaction. what is interesting to me is when the “if” condition is satisfied why it is also trying to enter into “else if
“Received accepted response from Fleet Provisioning CreateCertificateFromCsr API.” this message is printed on the log in my first message, then why it again going inside “else if” Logerror “Received rejected response from Fleet Provisioning RegisterThing API.” ?

You must have created a provisioning template. Can you double check that the lambda ARN is correctly set in the provisioning template?

This is because you are getting multiple callbacks for multiple operations.

The Lambda function was the problem, I managed to solve it.

Now I’m wondering how to set my own Publish and Subcribe topic in this sdk and send Data That I intend to receive through Uart.

This example shows how to publish and subscribe - esp-aws-iot/examples/mqtt/tls_mutual_auth/main/mqtt_demo_mutual_auth.c at master · espressif/esp-aws-iot · GitHub.

I have already did this project "TLS_MUTUAL_AUTHENTICATION separately, but I’m wondering whether I can build on top of Fleet_Provisioning SDK. As the SDK has mqtt_operations.c file.

Yes, you can implement the same functionality in the fleet provisioning demo too. I linked the other one so that you can look at that for a reference.