[BluetoothLE] Correctly handle unregistration of characteristic listeners.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 7 Aug 2015 12:00:24 +0000 (14:00 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 7 Aug 2015 12:00:24 +0000 (14:00 +0200)
[Verification] Manually tested, fixes potential crashes.

Change-Id: Id88923232b8c75bd87b0481158042c7edc221a0d
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/bluetooth/bluetooth_gatt_service.cc
src/bluetooth/bluetooth_gatt_service.h

index 24cfdaacb87c40db13cd901ff66b3670904a6210..dd4be35fea0dd8b19e313bf0800121f232acad9d 100755 (executable)
@@ -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);
   }
 }
index 7c493b0cf16eb0e3e801cb8f5acf07b2e8271a16..0ae0e365a63e47e1df5a3fb8b5fef74547f81a3e 100755 (executable)
@@ -69,6 +69,7 @@ class BluetoothGATTService {
                                            void* user_data);
 
   std::map<std::string, bt_gatt_client_h> gatt_clients_;
+  std::vector<bt_gatt_h> gatt_characteristic_;
 
   BluetoothInstance& instance_;
 };