static int create_Device(IOTHUB_ACCOUNT_INFO* accountInfo, const char* callerName) { int result = 0; if (generateDeviceName(accountInfo, callerName) != 0) { result = __LINE__; } else { BUFFER_HANDLE deviceJson = constructDeviceJson(accountInfo); if (deviceJson == NULL) { result = __LINE__; free(accountInfo->deviceId); accountInfo->deviceId = NULL; } else { BUFFER_HANDLE deviceResp = sendDeviceRegistryInfo(accountInfo, deviceJson, HTTPAPI_REQUEST_PUT); if (deviceResp != NULL) { if (parseDeviceJson(accountInfo, deviceResp) != 0) { result = __LINE__; free(accountInfo->deviceId); accountInfo->deviceId = NULL; } else { result = 0; } BUFFER_delete(deviceResp); } else { result = __LINE__; free(accountInfo->deviceId); accountInfo->deviceId = NULL; } BUFFER_delete(deviceJson); } } return 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; }