static IOTHUB_DEVICE_HANDLE IoTHubTransportHttp_Register()

in iothub_client/src/iothubtransporthttp.c [691:806]


static IOTHUB_DEVICE_HANDLE IoTHubTransportHttp_Register(TRANSPORT_LL_HANDLE handle, const IOTHUB_DEVICE_CONFIG* device, PDLIST_ENTRY waitingToSend)
{
    HTTPTRANSPORT_PERDEVICE_DATA* result;
    if (handle == NULL || device == NULL)
    {
        LogError("Transport handle is NULL");
        result = NULL;
    }
    else if (device->deviceId == NULL || ((device->deviceKey != NULL) && (device->deviceSasToken != NULL)) || waitingToSend == NULL)
    {
        LogError("invalid parameters detected TRANSPORT_LL_HANDLE handle=%p, const IOTHUB_DEVICE_CONFIG* device=%p, PDLIST_ENTRY waitingToSend=%p",
            handle, device, waitingToSend);
        result = NULL;
    }
    else
    {
        HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
        void* listItem = VECTOR_find_if(handleData->perDeviceList, findDeviceById, device->deviceId);
        if (listItem != NULL)
        {
            LogError("Transport already has device registered by id: [%s]", device->deviceId);
            result = NULL;
        }
        else
        {
            bool was_create_deviceKey_ok = false;
            bool was_create_deviceSasToken_ok = false;
            bool was_sasObject_ok = false;
            bool was_x509_ok = false; /*there's nothing "created" in the case of x509, it is a flag indicating that x509 is used*/

            bool was_resultCreated_ok = ((result = (HTTPTRANSPORT_PERDEVICE_DATA *)malloc(sizeof(HTTPTRANSPORT_PERDEVICE_DATA))) != NULL);
            bool was_create_deviceId_ok = was_resultCreated_ok && create_deviceId(result, device->deviceId);

            if (was_create_deviceId_ok)
            {
                if (device->deviceSasToken != NULL)
                {
                    /*device->deviceKey is certainly NULL (because of the validation condition (else if (device->deviceId == NULL || ((device->deviceKey != NULL) && (device->deviceSasToken != NULL)) || waitingToSend == NULL))that does not accept that both satoken AND devicekey are non-NULL*/
                    was_create_deviceSasToken_ok = create_deviceSasToken(result, device->deviceSasToken);
                    result->deviceKey = NULL;
                    result->sasObject = NULL;
                }
                else /*when deviceSasToken == NULL*/
                {
                    if (device->deviceKey != NULL)
                    {
                        was_create_deviceKey_ok = create_deviceKey(result, device->deviceKey);
                        result->deviceSasToken = NULL;
                    }
                    else
                    {
                        /*when both of them are NULL*/
                        was_x509_ok = true;
                        result->deviceKey = NULL;
                        result->deviceSasToken = NULL;
                    }
                }
            }

            bool was_eventHTTPrelativePath_ok = (was_create_deviceKey_ok || was_create_deviceSasToken_ok || was_x509_ok) && create_eventHTTPrelativePath(result, device->deviceId);
            bool was_messageHTTPrelativePath_ok = was_eventHTTPrelativePath_ok && create_messageHTTPrelativePath(result, device->deviceId);
            bool was_eventHTTPrequestHeaders_ok;
            if (was_messageHTTPrelativePath_ok)
            {
                result->device_transport_ctx = handleData->transport_ctx;
                was_eventHTTPrequestHeaders_ok = create_eventHTTPrequestHeaders(result, handleData, device->deviceId, was_x509_ok);
            }
            else
            {
                was_eventHTTPrequestHeaders_ok = false;
            }
            bool was_messageHTTPrequestHeaders_ok = was_eventHTTPrequestHeaders_ok && create_messageHTTPrequestHeaders(result, handleData, was_x509_ok);
            bool was_abandonHTTPrelativePathBegin_ok = was_messageHTTPrequestHeaders_ok && create_abandonHTTPrelativePathBegin(result, device->deviceId);

            if (was_x509_ok)
            {
                result->sasObject = NULL; /**/
                was_sasObject_ok = true;
            }
            else
            {
                if (!was_create_deviceSasToken_ok)
                {
                    was_sasObject_ok = was_abandonHTTPrelativePathBegin_ok && create_deviceSASObject(result, handleData->hostName, device->deviceId, device->deviceKey);
                }
            }

            bool was_list_add_ok = (was_sasObject_ok || was_create_deviceSasToken_ok || was_x509_ok) && (VECTOR_push_back(handleData->perDeviceList, &result, 1) == 0);

            if (was_list_add_ok)
            {
                result->DoWork_PullMessage = false;
                result->isFirstPoll = true;
                result->waitingToSend = waitingToSend;
                DList_InitializeListHead(&(result->eventConfirmations));
                result->transportHandle = (HTTPTRANSPORT_HANDLE_DATA *)handle;
            }
            else
            {
                if (was_sasObject_ok) destroy_SASObject(result);
                if (was_abandonHTTPrelativePathBegin_ok) destroy_abandonHTTPrelativePathBegin(result);
                if (was_messageHTTPrelativePath_ok) destroy_messageHTTPrelativePath(result);
                if (was_eventHTTPrequestHeaders_ok) destroy_eventHTTPrequestHeaders(result);
                if (was_messageHTTPrequestHeaders_ok) destroy_messageHTTPrequestHeaders(result);
                if (was_eventHTTPrelativePath_ok) destroy_eventHTTPrelativePath(result);
                if (was_create_deviceId_ok) destroy_deviceId(result);
                if (was_create_deviceKey_ok) destroy_deviceKey(result);
                if (was_create_deviceSasToken_ok) destroy_deviceSasToken(result);
                if (was_resultCreated_ok) free(result);
                result = NULL;
            }
        }

    }
    return (IOTHUB_DEVICE_HANDLE)result;
}