Fix issue with reestablishing device client on Linux

* Properly init/deinit hub connection
This commit is contained in:
Alex Scarpantoni 2019-10-02 14:30:25 -07:00 коммит произвёл GitHub
Родитель 33368d6c3e
Коммит cef6def7f1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 22 добавлений и 22 удалений

Просмотреть файл

@ -120,9 +120,6 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotHubViaProvisioning(bool TraceOn, PCON
if (-1 == sprintf_s(customProvisioningData, customProvDataLength, pnpSample_CustomProvisioningData, dcmModelId)) {
LogError("Failed to create the customProvisiongData. DcmModelId is too long.");
}
else if (IoTHub_Init() != 0) {
LogError("IoTHub_Init failed");
}
else if ((AUTH_TYPE_SYMMETRIC_KEY == ConnectionParams->AuthParameters.AuthType)
&& (prov_dev_set_symmetric_key_info(deviceId, deviceKey) != 0)) {
LogError("prov_dev_set_symmetric_key_info failed.");
@ -312,26 +309,21 @@ IOTHUB_DEVICE_HANDLE IotComms_InitializeIotHubDeviceHandle(bool TraceOn, const c
IOTHUB_DEVICE_HANDLE deviceHandle = NULL;
IOTHUB_CLIENT_RESULT iothubClientResult;
// TODO: PnP SDK should auto-set OPTION_AUTO_URL_ENCODE_DECODE for MQTT as its strictly required. Need way for IoTHub handle to communicate this back.
if (IoTHub_Init() != 0) {
LogError("IoTHub_Init failed\n");
} else {
// Get connection string from config
if ((deviceHandle = IoTHubDeviceClient_CreateFromConnectionString(ConnectionString, MQTT_Protocol)) == NULL)
{
LogError("Failed to create device handle\n");
}
else if ((iothubClientResult = IoTHubDeviceClient_SetOption(deviceHandle, OPTION_LOG_TRACE, &TraceOn)) != IOTHUB_CLIENT_OK)
{
LogError("Failed to set option %s, error=%d\n", OPTION_LOG_TRACE, iothubClientResult);
IoTHubDeviceClient_Destroy(deviceHandle);
deviceHandle = NULL;
}
// Get connection string from config
if ((deviceHandle = IoTHubDeviceClient_CreateFromConnectionString(ConnectionString, MQTT_Protocol)) == NULL)
{
LogError("Failed to create device handle\n");
}
else if ((iothubClientResult = IoTHubDeviceClient_SetOption(deviceHandle, OPTION_LOG_TRACE, &TraceOn)) != IOTHUB_CLIENT_OK)
{
LogError("Failed to set option %s, error=%d\n", OPTION_LOG_TRACE, iothubClientResult);
IoTHubDeviceClient_Destroy(deviceHandle);
deviceHandle = NULL;
}
if (deviceHandle == NULL)
{
IoTHub_Deinit();
}
if (deviceHandle == NULL)
{
IoTHub_Deinit();
}
return deviceHandle;

Просмотреть файл

@ -396,6 +396,12 @@ PnpBridge_Main()
TRY {
LogInfo("Starting Azure PnpBridge");
if (IoTHub_Init() != 0) {
LogError("IoTHub_Init failed\n");
result = PNPBRIDGE_FAILED;
LEAVE;
}
result = PnpBridge_Initialize(&pnpBridge);
if (PNPBRIDGE_OK != result) {
LogError("PnpBridge_Initialize failed: %d", result);
@ -454,6 +460,8 @@ PnpBridge_Stop()
Condition_Post(g_PnpBridge->ExitCondition);
Unlock(g_PnpBridge->ExitLock);
}
IoTHub_Deinit();
}
// Note: PnpBridge_UploadToBlobAsync method is not synchronized