From 5a535fd0222131a0570f7f49d74c3371cbe05da9 Mon Sep 17 00:00:00 2001
From: Pawel Andruszkiewicz
Date: Fri, 7 Aug 2015 14:00:24 +0200
Subject: [PATCH] [BluetoothLE] Correctly handle unregistration of
characteristic listeners.
[Verification] Manually tested, fixes potential crashes.
Change-Id: Id88923232b8c75bd87b0481158042c7edc221a0d
Signed-off-by: Pawel Andruszkiewicz
---
src/bluetooth/bluetooth_gatt_service.cc | 9 +++++++--
src/bluetooth/bluetooth_gatt_service.h | 1 +
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/bluetooth/bluetooth_gatt_service.cc b/src/bluetooth/bluetooth_gatt_service.cc
index 24cfdaac..dd4be35f 100755
--- a/src/bluetooth/bluetooth_gatt_service.cc
+++ b/src/bluetooth/bluetooth_gatt_service.cc
@@ -65,9 +65,12 @@ BluetoothGATTService::BluetoothGATTService(BluetoothInstance& instance) :
BluetoothGATTService::~BluetoothGATTService() {
LoggerD("Entered");
- for (auto it : gatt_clients_) {
+ for (auto it : gatt_characteristic_) {
// unregister callback, ignore errors
- bt_gatt_client_unset_characteristic_value_changed_cb(it.second);
+ bt_gatt_client_unset_characteristic_value_changed_cb(it);
+ }
+
+ for (auto it : gatt_clients_) {
LoggerD("destroying client for address: %s", it.first.c_str());
bt_gatt_client_destroy(it.second);
}
@@ -477,6 +480,7 @@ void BluetoothGATTService::AddValueChangeListener(const picojson::value& args,
LoggerE("bt_gatt_client_set_characteristic_value_changed_cb() failed with: %d", ret);
ReportError(util::GetBluetoothError(ret, "Failed to register listener"), &out);
} else {
+ gatt_characteristic_.push_back(handle);
ReportSuccess(out);
}
}
@@ -500,6 +504,7 @@ void BluetoothGATTService::RemoveValueChangeListener(
LoggerE("bt_gatt_client_unset_characteristic_value_changed_cb() failed with: %d", ret);
ReportError(util::GetBluetoothError(ret, "Failed to unregister listener"), &out);
} else {
+ gatt_characteristic_.erase(std::remove(gatt_characteristic_.begin(), gatt_characteristic_.end(), handle), gatt_characteristic_.end());
ReportSuccess(out);
}
}
diff --git a/src/bluetooth/bluetooth_gatt_service.h b/src/bluetooth/bluetooth_gatt_service.h
index 7c493b0c..0ae0e365 100755
--- a/src/bluetooth/bluetooth_gatt_service.h
+++ b/src/bluetooth/bluetooth_gatt_service.h
@@ -69,6 +69,7 @@ class BluetoothGATTService {
void* user_data);
std::map gatt_clients_;
+ std::vector gatt_characteristic_;
BluetoothInstance& instance_;
};
--
2.34.1