bool __attribute__()

in AZ3166/src/libraries/AzureIoT/src/DevkitDPSClient.cpp [198:328]


bool __attribute__((section(".riot_fw"))) DevkitDPSClientStart(const char* global_prov_uri,
    const char* id_scope, const char* registration_id, char* udsString,
    const char* proxy_address, int proxy_port)
{
    bool result = true;
    
    if (global_prov_uri == NULL || id_scope == NULL)
    {
        LogError("invalid parameter global_prov_uri: %p id_scope: %p", global_prov_uri, id_scope);
        result = false;
    }
    else if (is_iothub_from_dps)
    {
        return result; // already been here
    }
    
    LogInfo("   DPS Version: %s\r\n", Prov_Device_GetVersionString());

    if (g_auth_type == DPS_AUTH_X509_INDIVIDUAL)
    {
        // Initialize DICE
        udsString = DiceInit(udsString);
        if (udsString == NULL)
        {
            LogError("DiceInit failed! Check UDS string provided or set on configuration mode");
            return false;
        }
        
        // Launch protected DICE code. This will measure RIoT Core, derive the
        // CDI value. It must execute with interrupts disabled. Therefore, it
        // must return so we can restore interrupt state.
        if (DiceCore() != 0)
        {
            return false;
        }

        // If DiceCore detects an error condition, it will not enable access to
        // the volatile storage segment. This attempt to transfer control to RIoT
        // will trigger a system reset. We will not be able to proceed.
        // TODO: DETECT WHEN A RESET HAS OCCURRED AND TAKE SOME ACTION.
        if (RiotStart(DiceCDI.bytes, (uint16_t)DICE_DIGEST_LENGTH, registration_id) != 0)
        {
            return false;
        }
    }
    
    if (platform_init() != 0)
    {
        LogError("Failed to initialize the platform.");
        result = false;
    }
    else if ((g_auth_type == DPS_AUTH_X509_INDIVIDUAL || g_auth_type == DPS_AUTH_X509_GROUP) && prov_dev_security_init(SECURE_DEVICE_TYPE_X509) != 0)
    {
        LogError("Failed to initialize the platform.");
        result = false;
    }
    else if (g_auth_type == DPS_AUTH_SYMMETRIC_KEY && prov_dev_security_init(SECURE_DEVICE_TYPE_SYMMETRIC_KEY) != 0)
    {
        LogError("Failed to initialize the platform.");
        result = false;
    }
    else if (g_auth_type == DPS_AUTH_SYMMETRIC_KEY && prov_dev_set_symmetric_key_info(registration_id, udsString) != 0)
    {
        LogError("Failed to initialize the platform.");
        result = false;
    }
    else
    {
        memset(&user_ctx, 0, sizeof(CLIENT_SAMPLE_INFO));

        // Set ini
        user_ctx.registration_complete = 0;
        user_ctx.sleep_time = 10;
        
        PROV_DEVICE_LL_HANDLE handle = NULL;

        if ((handle = Prov_Device_LL_Create(global_prov_uri, id_scope, Prov_Device_HTTP_Protocol)) == NULL)
        {
            LogError("failed calling Prov_Device_LL_Create");
            result = false;
        }
        else
        {
            if (proxy_address != NULL)
            {
                HTTP_PROXY_OPTIONS http_proxy;
                http_proxy.host_address = proxy_address;
                http_proxy.port = proxy_port;
                if (Prov_Device_LL_SetOption(handle, OPTION_HTTP_PROXY, &http_proxy) != PROV_DEVICE_RESULT_OK)
                {
                    LogError("Failed to set option \"HTTP Proxy\"");
                    result = false;
                }
            }

            (void)Prov_Device_LL_SetOption(handle, "logtrace", &g_trace_on);
            if (Prov_Device_LL_SetOption(handle, "TrustedCerts", certificates) != PROV_DEVICE_RESULT_OK)
            {
                LogError("Failed to set option \"TrustedCerts\"");
                result = false;
            }
            else if (Prov_Device_LL_Register_Device(handle, register_device_callback, &user_ctx, registation_status_callback, &user_ctx) != PROV_DEVICE_RESULT_OK)
            {
                LogError("failed calling Prov_Device_LL_Register_Device");
                result = false;
            }
            else
            {
                // Waiting the register to be completed
                do
                {
                    Prov_Device_LL_DoWork(handle);
                    ThreadAPI_Sleep(user_ctx.sleep_time);
                } while (user_ctx.registration_complete == 0);
            }
            // Free DPS client
            Prov_Device_LL_Destroy(handle);
        }

        if (user_ctx.registration_complete != 1)
        {
            LogError("registration failed!\r\n");
            result = false;
        }
        else
        {
            is_iothub_from_dps = true;
        }
    }
    return result;
}