IOTHUB_ACCOUNT_INFO_HANDLE IoTHubAccount_Init(bool createDevice, const char* callerName) { IOTHUB_ACCOUNT_INFO* result = malloc(sizeof(IOTHUB_ACCOUNT_INFO)); if (result == NULL) { LogError("[IoTHubAccount] Failed allocating IOTHUB_ACCOUNT_INFO."); } else { memset(result, 0, sizeof(IOTHUB_ACCOUNT_INFO)); #ifdef MBED_BUILD_TIMESTAMP result->connString = getMbedParameter("IOTHUB_CONNECTION_STRING"); result->eventhubConnString = getMbedParameter("IOTHUB_EVENTHUB_CONNECTION_STRING"); #else result->connString = getenv("IOTHUB_CONNECTION_STRING"); result->eventhubConnString = getenv("IOTHUB_EVENTHUB_CONNECTION_STRING"); #endif if (result->connString == NULL || result->eventhubConnString == NULL) { LogError("Failure retrieving Connection Strings values.\r\n"); free(result); result = NULL; } else { if (retrieveConnStringInfo(result) != 0) { free(result); result = NULL; } else if (createDevice) { int create_device_result; if ((create_device_result = create_Device(result, callerName)) != 0) { LogError("Failed creating IoT device (%d)", create_device_result); IoTHubAccount_deinit((IOTHUB_ACCOUNT_INFO_HANDLE)result); result = NULL; } } } } return (IOTHUB_ACCOUNT_INFO_HANDLE)result; }
IOTHUB_ACCOUNT_INFO_HANDLE IoTHubAccount_Init(bool createDevice) { IOTHUB_ACCOUNT_INFO* iothub_account_info = malloc(sizeof(IOTHUB_ACCOUNT_INFO)); if (iothub_account_info == NULL) { LogError("[IoTHubAccount] Failed allocating IOTHUB_ACCOUNT_INFO."); } else { memset(iothub_account_info, 0, sizeof(IOTHUB_ACCOUNT_INFO)); #ifdef MBED_BUILD_TIMESTAMP iothub_account_info->connString = getMbedParameter("IOTHUB_CONNECTION_STRING"); iothub_account_info->eventhubConnString = getMbedParameter("IOTHUB_EVENTHUB_CONNECTION_STRING"); #else iothub_account_info->connString = getenv("IOTHUB_CONNECTION_STRING"); iothub_account_info->eventhubConnString = getenv("IOTHUB_EVENTHUB_CONNECTION_STRING"); #endif if (iothub_account_info->connString == NULL || iothub_account_info->eventhubConnString == NULL) { LogError("Failure retrieving Connection Strings values.\r\n"); free(iothub_account_info); iothub_account_info = NULL; } else { if (retrieveConnStringInfo(iothub_account_info) != 0) { LogError("retrieveConnStringInfo failed.\r\n"); free(iothub_account_info); iothub_account_info = NULL; } else if (createDevice) { iothub_account_info->iothub_service_client_auth_handle = IoTHubServiceClientAuth_CreateFromConnectionString(iothub_account_info->connString); if (iothub_account_info->iothub_service_client_auth_handle == NULL) { LogError("IoTHubServiceClientAuth_CreateFromConnectionString failed\r\n"); free(iothub_account_info); iothub_account_info = NULL; } else { iothub_account_info->iothub_messaging_handle = IoTHubMessaging_LL_Create(iothub_account_info->iothub_service_client_auth_handle); if (iothub_account_info->iothub_messaging_handle == NULL) { LogError("IoTHubMessaging_LL_Create failed\r\n"); IoTHubServiceClientAuth_Destroy(iothub_account_info->iothub_service_client_auth_handle); free(iothub_account_info); iothub_account_info = NULL; } else { iothub_account_info->iothub_registrymanager_handle = IoTHubRegistryManager_Create(iothub_account_info->iothub_service_client_auth_handle); if (iothub_account_info->iothub_registrymanager_handle == NULL) { LogError("IoTHubRegistryManager_Create failed\r\n"); IoTHubMessaging_LL_Destroy(iothub_account_info->iothub_messaging_handle); IoTHubServiceClientAuth_Destroy(iothub_account_info->iothub_service_client_auth_handle); free(iothub_account_info); iothub_account_info = NULL; } else { if (generateDeviceName(iothub_account_info) != 0) { LogError("generateDeviceName failed\r\n"); IoTHubMessaging_LL_Destroy(iothub_account_info->iothub_messaging_handle); IoTHubRegistryManager_Destroy(iothub_account_info->iothub_registrymanager_handle); IoTHubServiceClientAuth_Destroy(iothub_account_info->iothub_service_client_auth_handle); free(iothub_account_info); iothub_account_info = NULL; } IOTHUB_REGISTRYMANAGER_RESULT iothub_registrymanager_result; IOTHUB_REGISTRY_DEVICE_CREATE deviceCreateInfo; IOTHUB_DEVICE deviceInfo; deviceInfo.deviceId = NULL; deviceInfo.primaryKey = NULL; deviceInfo.secondaryKey = NULL; deviceInfo.generationId = NULL; deviceInfo.eTag = NULL; deviceInfo.connectionStateUpdatedTime = NULL; deviceInfo.statusReason = NULL; deviceInfo.statusUpdatedTime = NULL; deviceInfo.lastActivityTime = NULL; deviceInfo.configuration = NULL; deviceInfo.deviceProperties = NULL; deviceInfo.serviceProperties = NULL; deviceCreateInfo.deviceId = iothub_account_info->deviceId; deviceCreateInfo.primaryKey = ""; deviceCreateInfo.secondaryKey = ""; iothub_registrymanager_result = IoTHubRegistryManager_CreateDevice(iothub_account_info->iothub_registrymanager_handle, &deviceCreateInfo, &deviceInfo); if (iothub_registrymanager_result != IOTHUB_REGISTRYMANAGER_OK) { LogError("IoTHubRegistryManager_CreateDevice failed\r\n"); IoTHubRegistryManager_Destroy(iothub_account_info->iothub_registrymanager_handle); IoTHubServiceClientAuth_Destroy(iothub_account_info->iothub_service_client_auth_handle); free(iothub_account_info->deviceId); free(iothub_account_info); iothub_account_info = NULL; } else { if (mallocAndStrcpy_s((char**)&iothub_account_info->deviceKey, (char*)deviceInfo.primaryKey) != 0) { LogError("mallocAndStrcpy_s failed for primaryKey\r\n"); } } if (deviceInfo.deviceId != NULL) free((char*)deviceInfo.deviceId); if (deviceInfo.primaryKey != NULL) free((char*)deviceInfo.primaryKey); if(deviceInfo.secondaryKey != NULL) free((char*)deviceInfo.secondaryKey); if(deviceInfo.generationId != NULL) free((char*)deviceInfo.generationId); if(deviceInfo.eTag != NULL) free((char*)deviceInfo.eTag); if(deviceInfo.connectionStateUpdatedTime != NULL) free((char*)deviceInfo.connectionStateUpdatedTime); if(deviceInfo.statusReason != NULL) free((char*)deviceInfo.statusReason); if(deviceInfo.statusUpdatedTime != NULL) free((char*)deviceInfo.statusUpdatedTime); if(deviceInfo.lastActivityTime != NULL) free((char*)deviceInfo.lastActivityTime); if(deviceInfo.configuration != NULL) free((char*)deviceInfo.configuration); if(deviceInfo.deviceProperties != NULL) free((char*)deviceInfo.deviceProperties); if(deviceInfo.serviceProperties != NULL) free((char*)deviceInfo.serviceProperties); } } } } } } return (IOTHUB_ACCOUNT_INFO_HANDLE)iothub_account_info; }