Removed platform types dependency from windows platform codes

PiperOrigin-RevId: 676447748
This commit is contained in:
Guogang Li
2024-09-19 09:37:29 -07:00
committed by Copybara-Service
parent a9e753e1e1
commit 47f2dfec28
48 changed files with 1972 additions and 2133 deletions
@@ -53,7 +53,7 @@ BluetoothPairing::BluetoothPairing(
BluetoothDevice bluetooth_device,
DeviceInformationCustomPairing custom_pairing)
: bluetooth_device_(bluetooth_device), custom_pairing_(custom_pairing) {
NEARBY_VLOG(1) << __func__ << ": BluetoothPairing is created for device.";
VLOG(1) << __func__ << ": BluetoothPairing is created for device.";
}
BluetoothPairing::~BluetoothPairing() {
@@ -62,17 +62,17 @@ BluetoothPairing::~BluetoothPairing() {
std::exchange(pairing_requested_token_, {}));
}
CancelPairing();
NEARBY_VLOG(1) << __func__ << ": BluetoothPairing is destroyed for device.";
VLOG(1) << __func__ << ": BluetoothPairing is destroyed for device.";
}
bool BluetoothPairing::InitiatePairing(
api::BluetoothPairingCallback pairing_cb) {
NEARBY_VLOG(1) << __func__ << ": Start to initiate pairing process.";
VLOG(1) << __func__ << ": Start to initiate pairing process.";
try {
pairing_requested_token_ = custom_pairing_.PairingRequested(
{this, &BluetoothPairing::OnPairingRequested});
if (!pairing_requested_token_) {
NEARBY_VLOG(1) << __func__ << " Failed to registered pairing callback.";
VLOG(1) << __func__ << " Failed to registered pairing callback.";
return false;
}
pairing_callback_ = std::move(pairing_cb);
@@ -87,35 +87,32 @@ bool BluetoothPairing::InitiatePairing(
OnPair(pairing_result);
return true;
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to initiate pairing. exception: "
<< exception.what();
LOG(ERROR) << __func__ << ": Failed to initiate pairing. exception: "
<< exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to initiate pairing. WinRT exception: "
<< error.code() << ": "
<< winrt::to_string(error.message());
LOG(ERROR) << __func__ << ": Failed to initiate pairing. WinRT exception: "
<< error.code() << ": " << winrt::to_string(error.message());
} catch (...) {
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception.";
LOG(ERROR) << __func__ << ": Unknown exception.";
}
return false;
}
bool BluetoothPairing::FinishPairing(
std::optional<absl::string_view> pin_code) {
NEARBY_VLOG(1) << __func__ << "Start to finish pairing.";
VLOG(1) << __func__ << "Start to finish pairing.";
try {
if (!pairing_requested_) {
NEARBY_VLOG(1) << __func__ << "No pairing requested.";
VLOG(1) << __func__ << "No pairing requested.";
return false;
}
if (!pairing_deferral_) {
NEARBY_VLOG(1) << __func__ << "No ongoing pairing process.";
VLOG(1) << __func__ << "No ongoing pairing process.";
return false;
}
if (expecting_pin_code_) {
if (!pin_code.has_value()) {
NEARBY_LOGS(INFO) << __func__ << " Failed to get pin code";
LOG(INFO) << __func__ << " Failed to get pin code";
return false;
}
expecting_pin_code_ = false;
@@ -125,27 +122,25 @@ bool BluetoothPairing::FinishPairing(
pairing_requested_.Accept();
}
pairing_deferral_.Complete();
NEARBY_VLOG(1) << "Successfully finished pairing.";
VLOG(1) << "Successfully finished pairing.";
return true;
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Failed to finish pairing. exception: "
<< exception.what();
LOG(ERROR) << __func__
<< ": Failed to finish pairing. exception: " << exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to finish pairing. WinRT exception: "
<< error.code() << ": "
<< winrt::to_string(error.message());
LOG(ERROR) << __func__ << ": Failed to finish pairing. WinRT exception: "
<< error.code() << ": " << winrt::to_string(error.message());
} catch (...) {
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception.";
LOG(ERROR) << __func__ << ": Unknown exception.";
}
return false;
}
bool BluetoothPairing::CancelPairing() {
NEARBY_VLOG(1) << __func__ << " Start to cancel ongoing pairing process.";
VLOG(1) << __func__ << " Start to cancel ongoing pairing process.";
try {
if (!pairing_deferral_) {
NEARBY_VLOG(1) << __func__ << "No ongoing pairing process.";
VLOG(1) << __func__ << "No ongoing pairing process.";
return true;
}
// There is no way to explicitly cancel an in-progress pairing on Windows as
@@ -156,47 +151,44 @@ bool BluetoothPairing::CancelPairing() {
// deferral is completed, will know that cancellation was the actual result.
was_cancelled_ = true;
pairing_deferral_.Close();
NEARBY_VLOG(1) << __func__ << "Canceled ongoing pairing process.";
VLOG(1) << __func__ << "Canceled ongoing pairing process.";
return true;
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Failed to cancel ongoing pairing "
<< "process. exception: " << exception.what();
LOG(ERROR) << __func__ << ": Failed to cancel ongoing pairing "
<< "process. exception: " << exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to cancel ongoing pairing process. "
<< "WinRT exception: " << error.code() << ": "
<< winrt::to_string(error.message());
LOG(ERROR) << __func__ << ": Failed to cancel ongoing pairing process. "
<< "WinRT exception: " << error.code() << ": "
<< winrt::to_string(error.message());
} catch (...) {
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception.";
LOG(ERROR) << __func__ << ": Unknown exception.";
}
return false;
}
bool BluetoothPairing::Unpair() {
NEARBY_VLOG(1) << __func__ << ": Start to unpair with remote device.";
VLOG(1) << __func__ << ": Start to unpair with remote device.";
try {
if (!IsPaired()) {
NEARBY_VLOG(1) << __func__ << " : Remote device Was not paired.";
VLOG(1) << __func__ << " : Remote device Was not paired.";
return true;
}
DeviceUnpairingResult unpairing_result =
bluetooth_device_.DeviceInformation().Pairing().UnpairAsync().get();
if (unpairing_result.Status() == DeviceUnpairingResultStatus::Unpaired) {
NEARBY_VLOG(1) << __func__ << ": Unpaired with remote device.";
VLOG(1) << __func__ << ": Unpaired with remote device.";
return true;
}
NEARBY_VLOG(1) << __func__ << ": Failed to unpaired with remote device.";
VLOG(1) << __func__ << ": Failed to unpaired with remote device.";
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to unpaired with device. exception: "
<< exception.what();
LOG(ERROR) << __func__ << ": Failed to unpaired with device. exception: "
<< exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to unpaired with device. WinRT exception: "
<< error.code() << ": "
<< winrt::to_string(error.message());
LOG(ERROR) << __func__
<< ": Failed to unpaired with device. WinRT exception: "
<< error.code() << ": " << winrt::to_string(error.message());
} catch (...) {
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception.";
LOG(ERROR) << __func__ << ": Unknown exception.";
}
return false;
}
@@ -204,18 +196,17 @@ bool BluetoothPairing::Unpair() {
bool BluetoothPairing::IsPaired() {
try {
bool is_paired = bluetooth_device_.DeviceInformation().Pairing().IsPaired();
NEARBY_LOGS(INFO) << __func__ << (is_paired ? " True" : " False");
LOG(INFO) << __func__ << (is_paired ? " True" : " False");
return is_paired;
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Failed to get IsPaired. exception: "
<< exception.what();
LOG(ERROR) << __func__
<< ": Failed to get IsPaired. exception: " << exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to get IsPaired. WinRT exception: "
<< error.code() << ": "
<< winrt::to_string(error.message());
LOG(ERROR) << __func__
<< ": Failed to get IsPaired. WinRT exception: " << error.code()
<< ": " << winrt::to_string(error.message());
} catch (...) {
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception.";
LOG(ERROR) << __func__ << ": Unknown exception.";
}
return false;
}
@@ -223,7 +214,7 @@ bool BluetoothPairing::IsPaired() {
void BluetoothPairing::OnPairingRequested(
DeviceInformationCustomPairing custom_pairing,
DevicePairingRequestedEventArgs pairing_requested) {
NEARBY_VLOG(1) << __func__ << "Requested to pair.";
VLOG(1) << __func__ << "Requested to pair.";
try {
DevicePairingKinds pairing_kind = pairing_requested.PairingKind();
pairing_requested_ = pairing_requested;
@@ -231,40 +222,38 @@ void BluetoothPairing::OnPairingRequested(
api::PairingParams params;
switch (pairing_kind) {
case DevicePairingKinds::ProvidePin:
NEARBY_LOGS(INFO) << __func__ << "DevicePairingKind: RequestPinCode.";
LOG(INFO) << __func__ << "DevicePairingKind: RequestPinCode.";
expecting_pin_code_ = true;
params.pairing_type = PairingType::kRequestPin;
pairing_callback_.on_pairing_initiated_cb(params);
return;
case DevicePairingKinds::ConfirmOnly:
NEARBY_LOGS(INFO) << __func__ << "DevicePairingKind: ConfirmOnly.";
LOG(INFO) << __func__ << "DevicePairingKind: ConfirmOnly.";
params.pairing_type = PairingType::kConsent;
pairing_callback_.on_pairing_initiated_cb(params);
return;
case DevicePairingKinds::ConfirmPinMatch:
NEARBY_LOGS(INFO) << __func__
<< "DevicePairingKind: Confirm Pin Match.";
LOG(INFO) << __func__ << "DevicePairingKind: Confirm Pin Match.";
params.pairing_type = PairingType::kConfirmPasskey;
params.passkey = winrt::to_string(pairing_requested.Pin());
pairing_callback_.on_pairing_initiated_cb(params);
return;
default:
params.pairing_type = PairingType::kUnknown;
NEARBY_LOGS(INFO) << __func__ << "Unsupported DevicePairingKind:"
<< static_cast<int>(pairing_kind);
LOG(INFO) << __func__ << "Unsupported DevicePairingKind:"
<< static_cast<int>(pairing_kind);
break;
}
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to request to pair with device. exception: "
<< exception.what();
LOG(ERROR) << __func__
<< ": Failed to request to pair with device. exception: "
<< exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Failed to request to pair with device. WinRT exception: "
<< error.code() << ": " << winrt::to_string(error.message());
LOG(ERROR) << __func__
<< ": Failed to request to pair with device. WinRT exception: "
<< error.code() << ": " << winrt::to_string(error.message());
} catch (...) {
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception.";
LOG(ERROR) << __func__ << ": Unknown exception.";
}
pairing_callback_.on_pairing_error_cb(PairingError::kFailed);
}
@@ -272,8 +261,8 @@ void BluetoothPairing::OnPairingRequested(
void BluetoothPairing::OnPair(DevicePairingResult& pairing_result) {
try {
DevicePairingResultStatus status = pairing_result.Status();
NEARBY_LOGS(INFO) << __func__
<< "Pairing Result Status: " << static_cast<int>(status);
LOG(INFO) << __func__
<< "Pairing Result Status: " << static_cast<int>(status);
if (was_cancelled_ &&
status == DevicePairingResultStatus::RejectedByHandler) {
// See comment in CancelPairing() for explanation of why was_cancelled_
@@ -283,53 +272,52 @@ void BluetoothPairing::OnPair(DevicePairingResult& pairing_result) {
switch (status) {
case DevicePairingResultStatus::AlreadyPaired:
case DevicePairingResultStatus::Paired:
NEARBY_LOGS(ERROR) << __func__ << "Pairing Result Status: Paired.";
LOG(ERROR) << __func__ << "Pairing Result Status: Paired.";
pairing_callback_.on_paired_cb();
return;
case DevicePairingResultStatus::PairingCanceled:
NEARBY_LOGS(ERROR) << __func__
<< "Pairing Result Status: Pairing Canceled.";
LOG(ERROR) << __func__ << "Pairing Result Status: Pairing Canceled.";
pairing_callback_.on_pairing_error_cb(PairingError::kAuthCanceled);
return;
case DevicePairingResultStatus::AuthenticationFailure:
NEARBY_LOGS(ERROR) << __func__
<< "Pairing Result Status: Authentication Failure.";
LOG(ERROR) << __func__
<< "Pairing Result Status: Authentication Failure.";
pairing_callback_.on_pairing_error_cb(PairingError::kAuthFailed);
return;
case DevicePairingResultStatus::ConnectionRejected:
case DevicePairingResultStatus::RejectedByHandler:
NEARBY_LOGS(ERROR) << __func__
<< "Pairing Result Status: Authentication Rejected.";
LOG(ERROR) << __func__
<< "Pairing Result Status: Authentication Rejected.";
pairing_callback_.on_pairing_error_cb(PairingError::kAuthRejected);
return;
case DevicePairingResultStatus::AuthenticationTimeout:
NEARBY_LOGS(ERROR) << __func__
<< "Pairing Result Status: Authentication Timeout.";
LOG(ERROR) << __func__
<< "Pairing Result Status: Authentication Timeout.";
pairing_callback_.on_pairing_error_cb(PairingError::kAuthTimeout);
return;
case DevicePairingResultStatus::Failed:
NEARBY_LOGS(ERROR) << __func__ << "Pairing Result Status: Failed.";
LOG(ERROR) << __func__ << "Pairing Result Status: Failed.";
pairing_callback_.on_pairing_error_cb(PairingError::kFailed);
return;
case DevicePairingResultStatus::OperationAlreadyInProgress:
NEARBY_LOGS(ERROR) << __func__
<< "Pairing Result Status: Operation In Progress.";
LOG(ERROR) << __func__
<< "Pairing Result Status: Operation In Progress.";
pairing_callback_.on_pairing_error_cb(PairingError::kRepeatedAttempts);
return;
default:
break;
}
NEARBY_LOGS(ERROR) << __func__ << "Pairing Result Status: Failed.";
LOG(ERROR) << __func__ << "Pairing Result Status: Failed.";
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to get Pairing Result Status. exception: "
<< exception.what();
LOG(ERROR) << __func__
<< ": Failed to get Pairing Result Status. exception: "
<< exception.what();
} catch (const winrt::hresult_error& error) {
NEARBY_LOGS(ERROR) << __func__ << ": Failed to get Pairing Result Status."
<< " WinRT exception: " << error.code() << ": "
<< winrt::to_string(error.message());
LOG(ERROR) << __func__ << ": Failed to get Pairing Result Status."
<< " WinRT exception: " << error.code() << ": "
<< winrt::to_string(error.message());
} catch (...) {
NEARBY_LOGS(ERROR) << __func__ << ": Unknown exception.";
LOG(ERROR) << __func__ << ": Unknown exception.";
}
pairing_callback_.on_pairing_error_cb(PairingError::kFailed);
}