static uint8_t nceReinitConnParams()

in source/1nce_zero_touch_provisioning.c [407:579]


static uint8_t nceReinitConnParams( char * completeResponse,
                                    char ** pThingName,
                                    char ** pEndpoint,
                                    char ** pExampleTopic,
                                    char ** pRootCA,
                                    char ** pClientCert,
                                    char ** pPrvKey )
{
    uint8_t status = EXIT_FAILURE;

    int i = 0;
    int32_t strSize = 0;
    int32_t offset = 0;
    char find[] = "\\n";
    char replaceWith[] = "\n";
    char endKey[] = "-----END RSA PRIVATE KEY-----";
    int32_t endKeyLen = sizeof( endKey );
    char endCert[] = "-----END CERTIFICATE-----";
    int32_t endCertLen = sizeof( endCert );
    char * location = NULL;

    memset( nceThingName, '\0', sizeof( nceThingName ) );
    memset( nceEndpoint, '\0', sizeof( nceEndpoint ) );
    memset( nceExampleTopic, '\0', sizeof( nceExampleTopic ) );
    memset( rootCA, '\0', sizeof( rootCA ) );
    memset( clientCert, '\0', sizeof( clientCert ) );
    memset( prvKey, '\0', sizeof( prvKey ) );

    if( NULL == completeResponse )
    {
        LogError( ( "input argument is null." ) );
        return status;
    }

    /* Get the first token. */
    char * token = strtok( completeResponse, "," );

    strSize = strlen( token );
    token[ strSize - 1 ] = '\0';

    if( sizeof( nceThingName ) < strSize )
    {
        LogError( ( "nceThingName array size is not enough to hold incoming thing name." ) );
        return status;
    }

    /* In the token we have now "ICCID" so we add 1 to start from the first number of ICCID */
    memcpy( nceThingName, token + 1, strSize );
    LogDebug( ( "Thing name is: %s.", nceThingName ) );

    /* Walk through other tokens. */
    while( token != NULL )
    {
        token = strtok( NULL, "," );

        if( i == 0 )
        {
            strSize = strlen( token ) - 2;

            if( sizeof( nceEndpoint ) < ( strSize + 1 ) )
            {
                LogError( ( "nceEndpoint array size is not enough to hold incoming endpoint." ) );
                return status;
            }

            memcpy( nceEndpoint, token + 1, strSize );
            LogDebug( ( "IoTEndpoint is: %s.", nceEndpoint ) );
        }

        /*
         * if(i == 1) {
         *  memcpy(amazonRootCaUrl, token, strlen(token));
         * }
         */
        if( i == 2 )
        {
            /* Process root.pem. */
            memcpy( PART, token + 1, strlen( token ) - 1 );
            char * result = str_replace( PART, find, replaceWith );
            memcpy( PART, result, strlen( PART ) );
            vPortFree( result );
            offset = ( int32_t ) ( &PART[ 0 ] );
            location = strstr( PART, endCert );
            strSize = ( int32_t ) location + endCertLen - offset;
            PART[ strSize ] = '\0';
            nceNetworkCredentials.rootCaSize = strSize + 1;

            if( sizeof( rootCA ) < ( strSize + 1 ) )
            {
                LogError( ( "rootCA array size is not enough to hold incoming root CA." ) );
                return status;
            }

            memcpy( rootCA, PART, ( strSize + 1 ) );
            ( void ) memset( &PART, ( int8_t ) '\0', sizeof( PART ) );

            LogDebug( ( "\r\n%s", rootCA ) );
        }

        if( i == 3 )
        {
            /* Process client_cert.pem. */
            memcpy( PART, token + 1, strlen( token ) - 1 );
            char * result = str_replace( PART, find, replaceWith );
            memcpy( PART, result, strlen( PART ) );
            vPortFree( result );
            offset = ( int32_t ) ( &PART[ 0 ] );
            location = strstr( PART, endCert );
            strSize = ( int32_t ) location + endCertLen - offset;
            PART[ strSize ] = '\0';

            if( sizeof( clientCert ) < ( strSize + 1 ) )
            {
                LogError( ( "clientCert array size is not enough to hold incoming client certificate." ) );
                return status;
            }

            memcpy( clientCert, PART, ( strSize + 1 ) );
            ( void ) memset( &PART, ( int8_t ) '\0', sizeof( PART ) );

            LogDebug( ( "\r\n%s", clientCert ) );
        }

        if( i == 4 )
        {
            /* Process client_key.pem. */
            memcpy( PART, token + 1, strlen( token ) - 1 );
            char * result = str_replace( PART, find, replaceWith );
            memcpy( PART, result, strlen( PART ) );
            vPortFree( result );
            offset = ( int32_t ) ( &PART[ 0 ] );
            location = strstr( PART, endKey );
            strSize = ( int32_t ) location + endKeyLen - offset;
            PART[ strSize ] = '\0';

            if( sizeof( prvKey ) < ( strSize + 1 ) )
            {
                LogError( ( "prvKey size is not enough to hold incoming client private key." ) );
                return status;
            }

            memcpy( prvKey, PART, ( strSize + 1 ) );
            ( void ) memset( &PART, ( int8_t ) '\0', sizeof( PART ) );

            LogDebug( ( "\r\n%s", prvKey ) );
        }

        i++;
    }

    /* Initialize MQTT topic. */
    strSize = strlen( nceThingName ) + strlen( "/example/topic" ) + 1;

    if( sizeof( nceExampleTopic ) < strSize )
    {
        LogError( ( "nceExampleTopic size is not enough to hold new example topic." ) );
        return status;
    }

    sprintf( nceExampleTopic, "%s/example/topic", nceThingName );

    *pThingName = nceThingName;
    *pEndpoint = nceEndpoint;
    *pExampleTopic = nceExampleTopic;
    *pRootCA = rootCA;
    *pClientCert = clientCert;
    *pPrvKey = prvKey;

    LogInfo( ( "Connection Info is re-initialized." ) );

    status = EXIT_SUCCESS;
    return status;
}