Refactor bluetooth code to use shared pointers instead of references.

This commit is contained in:
Vibhav Pant
2023-09-05 19:45:10 +05:30
parent e8236aad62
commit ad7401616b
11 changed files with 85 additions and 94 deletions
@@ -37,7 +37,7 @@ void BluetoothPairing::pairing_reply_handler(const sdbus::Error *error) {
<< "Got error '" << error->getName()
<< "' with message '" << error->getMessage()
<< "' while pairing with device "
<< device_.getObjectPath();
<< device_->getObjectPath();
if (name == "org.bluez.Error.AuthenticationCanceled") {
err = api::BluetoothPairingCallback::PairingError::kAuthCanceled;
@@ -60,9 +60,9 @@ void BluetoothPairing::pairing_reply_handler(const sdbus::Error *error) {
}
}
BluetoothPairing::BluetoothPairing(BluetoothAdapter &adapter,
BluetoothDevice &remote_device)
: device_(remote_device), adapter_(adapter) {}
BluetoothPairing::BluetoothPairing(
BluetoothAdapter &adapter, std::shared_ptr<BluetoothDevice> remote_device)
: device_(std::move(remote_device)), adapter_(adapter) {}
bool BluetoothPairing::InitiatePairing(
api::BluetoothPairingCallback pairing_cb) {
@@ -76,17 +76,17 @@ bool BluetoothPairing::InitiatePairing(
bool BluetoothPairing::FinishPairing(
std::optional<absl::string_view> pin_code) {
device_.set_pair_reply_callback([this](const sdbus::Error *error) {
device_->set_pair_reply_callback([this](const sdbus::Error *error) {
this->pairing_reply_handler(error);
});
try {
pair_async_call_ = device_.Pair();
pair_async_call_ = device_->Pair();
} catch (const sdbus::Error &e) {
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
<< "' with message '" << e.getMessage()
<< "' while trying to initiate pairing for device "
<< device_.getObjectPath();
<< device_->getObjectPath();
return false;
}
@@ -99,12 +99,12 @@ bool BluetoothPairing::CancelPairing() {
pair_async_call_.cancel();
}
device_.CancelPairing();
device_->CancelPairing();
} catch (const sdbus::Error &e) {
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
<< "' with message '" << e.getMessage()
<< "' while trying to cancel pairing for device "
<< device_.getObjectPath();
<< device_->getObjectPath();
return false;
}
@@ -113,13 +113,13 @@ bool BluetoothPairing::CancelPairing() {
bool BluetoothPairing::Unpair() {
try {
adapter_.RemoveDeviceByObjectPath(device_.getObjectPath());
adapter_.RemoveDeviceByObjectPath(device_->getObjectPath());
return true;
} catch (const sdbus::Error &e) {
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
<< "' with message '" << e.getMessage()
<< "' while trying to unpair device "
<< device_.getObjectPath() << " on adapter "
<< device_->getObjectPath() << " on adapter "
<< adapter_.GetObjectPath();
return false;
}
@@ -127,13 +127,13 @@ bool BluetoothPairing::Unpair() {
bool BluetoothPairing::IsPaired() {
try {
bool bonded = device_.Bonded();
bool bonded = device_->Bonded();
return bonded;
} catch (const sdbus::Error &e) {
NEARBY_LOGS(ERROR) << __func__ << ": Got error '" << e.getName()
<< "' with message '" << e.getMessage()
<< "' while trying to get Bonded state for device "
<< device_.getObjectPath();
<< device_->getObjectPath();
return false;
}
}