Commit 2ccef118 authored by jdoerrie's avatar jdoerrie Committed by Commit Bot

[bluetooth] Move DescriptorMap in Base Class

Similarly to r574552 this change moves the map of descriptors from the
platform implementations to the BluetoothRemoteGattCharacteristic base
class. In addition, default implementations for GetDescriptor() and
GetDescriptors() are provided. Lastly, this change cleans up a few lint
errors, mostly missing includes.

Bug: 821766
Change-Id: Iada71c198d241acf3bddcc1414ff89aa150c1c84
Reviewed-on: https://chromium-review.googlesource.com/1136296Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575603}
parent 9a7ea2e9
...@@ -4,7 +4,9 @@ ...@@ -4,7 +4,9 @@
#include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.h" #include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.h"
#include <set>
#include <utility> #include <utility>
#include <vector>
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
...@@ -595,7 +597,7 @@ LayoutTestBluetoothAdapterProvider::GetDelayedServicesDiscoveryAdapter() { ...@@ -595,7 +597,7 @@ LayoutTestBluetoothAdapterProvider::GetDelayedServicesDiscoveryAdapter() {
base::BindOnce(&NotifyServicesDiscovered, base::BindOnce(&NotifyServicesDiscovered,
base::RetainedRef(adapter_ptr), device_ptr)); base::RetainedRef(adapter_ptr), device_ptr));
DCHECK(services.size() == 0); DCHECK(services.empty());
return false; return false;
} }
...@@ -903,7 +905,7 @@ scoped_refptr<NiceMockBluetoothAdapter> LayoutTestBluetoothAdapterProvider:: ...@@ -903,7 +905,7 @@ scoped_refptr<NiceMockBluetoothAdapter> LayoutTestBluetoothAdapterProvider::
base::BindOnce(&NotifyDeviceChanged, base::BindOnce(&NotifyDeviceChanged,
base::RetainedRef(adapter_ptr), device_ptr)); base::RetainedRef(adapter_ptr), device_ptr));
} }
DCHECK(services.size() == 0); DCHECK(services.empty());
return false; return false;
} }
...@@ -1126,7 +1128,6 @@ scoped_refptr<NiceMockBluetoothAdapter> LayoutTestBluetoothAdapterProvider:: ...@@ -1126,7 +1128,6 @@ scoped_refptr<NiceMockBluetoothAdapter> LayoutTestBluetoothAdapterProvider::
ON_CALL(*notify_session, Stop(_)) ON_CALL(*notify_session, Stop(_))
.WillByDefault(Invoke([adapter_ptr, device_ptr, disconnect]( .WillByDefault(Invoke([adapter_ptr, device_ptr, disconnect](
const base::Closure& callback) { const base::Closure& callback) {
device_ptr->PushPendingCallback(callback); device_ptr->PushPendingCallback(callback);
if (disconnect) { if (disconnect) {
...@@ -1595,16 +1596,6 @@ LayoutTestBluetoothAdapterProvider::GetBaseGATTCharacteristic( ...@@ -1595,16 +1596,6 @@ LayoutTestBluetoothAdapterProvider::GetBaseGATTCharacteristic(
.WillByDefault( .WillByDefault(
RunCallback<1>(BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); RunCallback<1>(BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED));
ON_CALL(*characteristic, GetDescriptors())
.WillByDefault(
Invoke(characteristic.get(),
&MockBluetoothGattCharacteristic::GetMockDescriptors));
ON_CALL(*characteristic, GetDescriptor(_))
.WillByDefault(
Invoke(characteristic.get(),
&MockBluetoothGattCharacteristic::GetMockDescriptor));
return characteristic; return characteristic;
} }
......
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
#include <stdint.h> #include <stdint.h>
#include <memory>
#include <string>
#include "base/callback.h" #include "base/callback.h"
#include "device/bluetooth/test/mock_bluetooth_adapter.h" #include "device/bluetooth/test/mock_bluetooth_adapter.h"
#include "device/bluetooth/test/mock_bluetooth_device.h" #include "device/bluetooth/test/mock_bluetooth_device.h"
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
#include <utility>
#include "base/bind.h" #include "base/bind.h"
#include "base/location.h" #include "base/location.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
...@@ -22,15 +24,30 @@ BluetoothRemoteGattCharacteristic::~BluetoothRemoteGattCharacteristic() { ...@@ -22,15 +24,30 @@ BluetoothRemoteGattCharacteristic::~BluetoothRemoteGattCharacteristic() {
} }
} }
std::vector<BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristic::GetDescriptors() const {
std::vector<BluetoothRemoteGattDescriptor*> descriptors;
descriptors.reserve(descriptors_.size());
for (const auto& pair : descriptors_)
descriptors.push_back(pair.second.get());
return descriptors;
}
BluetoothRemoteGattDescriptor* BluetoothRemoteGattCharacteristic::GetDescriptor(
const std::string& identifier) const {
auto iter = descriptors_.find(identifier);
return iter != descriptors_.end() ? iter->second.get() : nullptr;
}
std::vector<BluetoothRemoteGattDescriptor*> std::vector<BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristic::GetDescriptorsByUUID( BluetoothRemoteGattCharacteristic::GetDescriptorsByUUID(
const BluetoothUUID& uuid) const { const BluetoothUUID& uuid) const {
std::vector<BluetoothRemoteGattDescriptor*> descriptors; std::vector<BluetoothRemoteGattDescriptor*> descriptors;
for (BluetoothRemoteGattDescriptor* descriptor : GetDescriptors()) { for (const auto& pair : descriptors_) {
if (descriptor->GetUUID() == uuid) { if (pair.second->GetUUID() == uuid)
descriptors.push_back(descriptor); descriptors.push_back(pair.second.get());
}
} }
return descriptors; return descriptors;
} }
...@@ -89,6 +106,17 @@ bool BluetoothRemoteGattCharacteristic::WriteWithoutResponse( ...@@ -89,6 +106,17 @@ bool BluetoothRemoteGattCharacteristic::WriteWithoutResponse(
return false; return false;
} }
bool BluetoothRemoteGattCharacteristic::AddDescriptor(
std::unique_ptr<BluetoothRemoteGattDescriptor> descriptor) {
if (!descriptor)
return false;
auto* descriptor_raw = descriptor.get();
return descriptors_
.try_emplace(descriptor_raw->GetIdentifier(), std::move(descriptor))
.second;
}
void BluetoothRemoteGattCharacteristic::StartNotifySessionInternal( void BluetoothRemoteGattCharacteristic::StartNotifySessionInternal(
const base::Optional<NotificationType>& notification_type, const base::Optional<NotificationType>& notification_type,
const NotifySessionCallback& callback, const NotifySessionCallback& callback,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/containers/flat_map.h"
#include "base/containers/queue.h" #include "base/containers/queue.h"
#include "base/containers/span.h" #include "base/containers/span.h"
#include "base/macros.h" #include "base/macros.h"
...@@ -62,18 +63,17 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic ...@@ -62,18 +63,17 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic
// Returns the list of GATT characteristic descriptors that provide more // Returns the list of GATT characteristic descriptors that provide more
// information about this characteristic. // information about this characteristic.
virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const;
const = 0;
// Returns the GATT characteristic descriptor with identifier |identifier| if // Returns the GATT characteristic descriptor with identifier |identifier| if
// it belongs to this GATT characteristic. // it belongs to this GATT characteristic.
virtual BluetoothRemoteGattDescriptor* GetDescriptor( virtual BluetoothRemoteGattDescriptor* GetDescriptor(
const std::string& identifier) const = 0; const std::string& identifier) const;
// Returns the GATT characteristic descriptors that match |uuid|. There may be // Returns the GATT characteristic descriptors that match |uuid|. There may be
// multiple, as illustrated by Core Bluetooth Specification [V4.2 Vol 3 Part G // multiple, as illustrated by Core Bluetooth Specification [V4.2 Vol 3 Part G
// 3.3.3.5 Characteristic Presentation Format]. // 3.3.3.5 Characteristic Presentation Format].
std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID( virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID(
const BluetoothUUID& uuid) const; const BluetoothUUID& uuid) const;
// Get a weak pointer to the characteristic. // Get a weak pointer to the characteristic.
...@@ -201,6 +201,14 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic ...@@ -201,6 +201,14 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic
const base::Closure& callback, const base::Closure& callback,
const ErrorCallback& error_callback) = 0; const ErrorCallback& error_callback) = 0;
// Utility function to add a |descriptor| to the map of |descriptors_|.
bool AddDescriptor(std::unique_ptr<BluetoothRemoteGattDescriptor> descriptor);
// Descriptors owned by the chracteristic. The descriptors' identifiers serve
// as keys.
base::flat_map<std::string, std::unique_ptr<BluetoothRemoteGattDescriptor>>
descriptors_;
private: private:
friend class BluetoothGattNotifySession; friend class BluetoothGattNotifySession;
......
...@@ -106,20 +106,21 @@ BluetoothRemoteGattCharacteristicAndroid::GetPermissions() const { ...@@ -106,20 +106,21 @@ BluetoothRemoteGattCharacteristicAndroid::GetPermissions() const {
std::vector<BluetoothRemoteGattDescriptor*> std::vector<BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristicAndroid::GetDescriptors() const { BluetoothRemoteGattCharacteristicAndroid::GetDescriptors() const {
EnsureDescriptorsCreated(); EnsureDescriptorsCreated();
std::vector<BluetoothRemoteGattDescriptor*> descriptors; return BluetoothRemoteGattCharacteristic::GetDescriptors();
for (const auto& map_iter : descriptors_)
descriptors.push_back(map_iter.second.get());
return descriptors;
} }
BluetoothRemoteGattDescriptor* BluetoothRemoteGattDescriptor*
BluetoothRemoteGattCharacteristicAndroid::GetDescriptor( BluetoothRemoteGattCharacteristicAndroid::GetDescriptor(
const std::string& identifier) const { const std::string& identifier) const {
EnsureDescriptorsCreated(); EnsureDescriptorsCreated();
const auto& iter = descriptors_.find(identifier); return BluetoothRemoteGattCharacteristic::GetDescriptor(identifier);
if (iter == descriptors_.end()) }
return nullptr;
return iter->second.get(); std::vector<BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristicAndroid::GetDescriptorsByUUID(
const BluetoothUUID& uuid) const {
EnsureDescriptorsCreated();
return BluetoothRemoteGattCharacteristic::GetDescriptorsByUUID(uuid);
} }
void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic( void BluetoothRemoteGattCharacteristicAndroid::ReadRemoteCharacteristic(
...@@ -236,10 +237,9 @@ void BluetoothRemoteGattCharacteristicAndroid::CreateGattRemoteDescriptor( ...@@ -236,10 +237,9 @@ void BluetoothRemoteGattCharacteristicAndroid::CreateGattRemoteDescriptor(
base::android::ConvertJavaStringToUTF8(env, instanceId); base::android::ConvertJavaStringToUTF8(env, instanceId);
DCHECK(!base::ContainsKey(descriptors_, instanceIdString)); DCHECK(!base::ContainsKey(descriptors_, instanceIdString));
AddDescriptor(BluetoothRemoteGattDescriptorAndroid::Create(
descriptors_[instanceIdString] = BluetoothRemoteGattDescriptorAndroid::Create(
instanceIdString, bluetooth_gatt_descriptor_wrapper, instanceIdString, bluetooth_gatt_descriptor_wrapper,
chrome_bluetooth_device); chrome_bluetooth_device));
} }
void BluetoothRemoteGattCharacteristicAndroid::SubscribeToNotifications( void BluetoothRemoteGattCharacteristicAndroid::SubscribeToNotifications(
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
#include <stdint.h> #include <stdint.h>
#include <memory> #include <memory>
#include <unordered_map> #include <string>
#include <vector>
#include "base/android/jni_android.h" #include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h" #include "base/android/scoped_java_ref.h"
...@@ -19,7 +20,6 @@ ...@@ -19,7 +20,6 @@
namespace device { namespace device {
class BluetoothAdapterAndroid; class BluetoothAdapterAndroid;
class BluetoothRemoteGattDescriptorAndroid;
class BluetoothRemoteGattServiceAndroid; class BluetoothRemoteGattServiceAndroid;
// BluetoothRemoteGattCharacteristicAndroid along with its owned Java class // BluetoothRemoteGattCharacteristicAndroid along with its owned Java class
...@@ -61,6 +61,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid ...@@ -61,6 +61,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid
std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const override; std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const override;
BluetoothRemoteGattDescriptor* GetDescriptor( BluetoothRemoteGattDescriptor* GetDescriptor(
const std::string& identifier) const override; const std::string& identifier) const override;
std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID(
const BluetoothUUID& uuid) const override;
void ReadRemoteCharacteristic(const ValueCallback& callback, void ReadRemoteCharacteristic(const ValueCallback& callback,
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
void WriteRemoteCharacteristic(const std::vector<uint8_t>& value, void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
...@@ -138,11 +140,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid ...@@ -138,11 +140,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicAndroid
std::vector<uint8_t> value_; std::vector<uint8_t> value_;
// Map of descriptors, keyed by descriptor identifier.
std::unordered_map<std::string,
std::unique_ptr<BluetoothRemoteGattDescriptorAndroid>>
descriptors_;
DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicAndroid); DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicAndroid);
}; };
......
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
#include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
#import <CoreBluetooth/CoreBluetooth.h> #import <CoreBluetooth/CoreBluetooth.h>
#include <unordered_map> #include <string>
#include <utility>
#include <vector>
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
...@@ -39,9 +41,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac ...@@ -39,9 +41,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac
const std::vector<uint8_t>& GetValue() const override; const std::vector<uint8_t>& GetValue() const override;
BluetoothRemoteGattService* GetService() const override; BluetoothRemoteGattService* GetService() const override;
bool IsNotifying() const override; bool IsNotifying() const override;
std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const override;
BluetoothRemoteGattDescriptor* GetDescriptor(
const std::string& identifier) const override;
void ReadRemoteCharacteristic(const ValueCallback& callback, void ReadRemoteCharacteristic(const ValueCallback& callback,
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
void WriteRemoteCharacteristic(const std::vector<uint8_t>& value, void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
...@@ -49,8 +48,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac ...@@ -49,8 +48,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
bool WriteWithoutResponse(base::span<const uint8_t> value) override; bool WriteWithoutResponse(base::span<const uint8_t> value) override;
DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicMac);
protected: protected:
void SubscribeToNotifications(BluetoothRemoteGattDescriptor* ccc_descriptor, void SubscribeToNotifications(BluetoothRemoteGattDescriptor* ccc_descriptor,
const base::Closure& callback, const base::Closure& callback,
...@@ -104,10 +101,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac ...@@ -104,10 +101,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac
CBDescriptor* cb_descriptor) const; CBDescriptor* cb_descriptor) const;
bool HasPendingRead() const { bool HasPendingRead() const {
return !read_characteristic_value_callbacks_.first.is_null(); return !read_characteristic_value_callbacks_.first.is_null();
}; }
bool HasPendingWrite() const { bool HasPendingWrite() const {
return !write_characteristic_value_callbacks_.first.is_null(); return !write_characteristic_value_callbacks_.first.is_null();
}; }
// Is true if the characteristic has been discovered with all its descriptors // Is true if the characteristic has been discovered with all its descriptors
// and discovery_pending_count_ is 0. // and discovery_pending_count_ is 0.
bool is_discovery_complete_; bool is_discovery_complete_;
...@@ -135,12 +132,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac ...@@ -135,12 +132,10 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicMac
PendingNotifyCallbacks subscribe_to_notification_callbacks_; PendingNotifyCallbacks subscribe_to_notification_callbacks_;
// Stores UnsubscribeFromNotifications request callbacks. // Stores UnsubscribeFromNotifications request callbacks.
PendingNotifyCallbacks unsubscribe_from_notification_callbacks_; PendingNotifyCallbacks unsubscribe_from_notification_callbacks_;
// Map of descriptors, keyed by descriptor identifier.
std::unordered_map<std::string,
std::unique_ptr<BluetoothRemoteGattDescriptorMac>>
gatt_descriptor_macs_;
base::WeakPtrFactory<BluetoothRemoteGattCharacteristicMac> weak_ptr_factory_; base::WeakPtrFactory<BluetoothRemoteGattCharacteristicMac> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicMac);
}; };
// Stream operator for logging. // Stream operator for logging.
......
...@@ -129,28 +129,6 @@ bool BluetoothRemoteGattCharacteristicMac::IsNotifying() const { ...@@ -129,28 +129,6 @@ bool BluetoothRemoteGattCharacteristicMac::IsNotifying() const {
return [cb_characteristic_ isNotifying] == YES; return [cb_characteristic_ isNotifying] == YES;
} }
std::vector<BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristicMac::GetDescriptors() const {
std::vector<BluetoothRemoteGattDescriptor*> gatt_descriptors;
for (const auto& iter : gatt_descriptor_macs_) {
BluetoothRemoteGattDescriptor* gatt_descriptor =
static_cast<BluetoothRemoteGattDescriptor*>(iter.second.get());
gatt_descriptors.push_back(gatt_descriptor);
}
return gatt_descriptors;
}
BluetoothRemoteGattDescriptor*
BluetoothRemoteGattCharacteristicMac::GetDescriptor(
const std::string& identifier) const {
auto searched_pair = gatt_descriptor_macs_.find(identifier);
if (searched_pair == gatt_descriptor_macs_.end()) {
return nullptr;
}
return static_cast<BluetoothRemoteGattDescriptor*>(
searched_pair->second.get());
}
void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic(
const ValueCallback& callback, const ValueCallback& callback,
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
...@@ -389,7 +367,7 @@ void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() { ...@@ -389,7 +367,7 @@ void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() {
VLOG(1) << *this << ": Did discover descriptors."; VLOG(1) << *this << ": Did discover descriptors.";
--discovery_pending_count_; --discovery_pending_count_;
std::unordered_set<std::string> descriptor_identifier_to_remove; std::unordered_set<std::string> descriptor_identifier_to_remove;
for (const auto& iter : gatt_descriptor_macs_) { for (const auto& iter : descriptors_) {
descriptor_identifier_to_remove.insert(iter.first); descriptor_identifier_to_remove.insert(iter.first);
} }
...@@ -404,21 +382,19 @@ void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() { ...@@ -404,21 +382,19 @@ void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() {
} }
gatt_descriptor_mac = gatt_descriptor_mac =
new BluetoothRemoteGattDescriptorMac(this, cb_descriptor); new BluetoothRemoteGattDescriptorMac(this, cb_descriptor);
const std::string& identifier = gatt_descriptor_mac->GetIdentifier(); bool result = AddDescriptor(base::WrapUnique(gatt_descriptor_mac));
auto result_iter = gatt_descriptor_macs_.insert( DCHECK(result);
{identifier, base::WrapUnique(gatt_descriptor_mac)});
DCHECK(result_iter.second);
GetMacAdapter()->NotifyGattDescriptorAdded(gatt_descriptor_mac); GetMacAdapter()->NotifyGattDescriptorAdded(gatt_descriptor_mac);
VLOG(1) << *gatt_descriptor_mac << ": New descriptor."; VLOG(1) << *gatt_descriptor_mac << ": New descriptor.";
} }
for (const std::string& identifier : descriptor_identifier_to_remove) { for (const std::string& identifier : descriptor_identifier_to_remove) {
auto pair_to_remove = gatt_descriptor_macs_.find(identifier); auto iter = descriptors_.find(identifier);
std::unique_ptr<BluetoothRemoteGattDescriptorMac> descriptor_to_remove; auto pair = std::move(*iter);
VLOG(1) << *descriptor_to_remove << ": Removed descriptor."; VLOG(1) << static_cast<BluetoothRemoteGattDescriptorMac&>(*pair.second)
pair_to_remove->second.swap(descriptor_to_remove); << ": Removed descriptor.";
gatt_descriptor_macs_.erase(pair_to_remove); descriptors_.erase(iter);
GetMacAdapter()->NotifyGattDescriptorRemoved(descriptor_to_remove.get()); GetMacAdapter()->NotifyGattDescriptorRemoved(pair.second.get());
} }
is_discovery_complete_ = discovery_pending_count_ == 0; is_discovery_complete_ = discovery_pending_count_ == 0;
} }
...@@ -471,19 +447,14 @@ bool BluetoothRemoteGattCharacteristicMac::IsDiscoveryComplete() const { ...@@ -471,19 +447,14 @@ bool BluetoothRemoteGattCharacteristicMac::IsDiscoveryComplete() const {
BluetoothRemoteGattDescriptorMac* BluetoothRemoteGattDescriptorMac*
BluetoothRemoteGattCharacteristicMac::GetBluetoothRemoteGattDescriptorMac( BluetoothRemoteGattCharacteristicMac::GetBluetoothRemoteGattDescriptorMac(
CBDescriptor* cb_descriptor) const { CBDescriptor* cb_descriptor) const {
auto found = std::find_if( for (const auto& pair : descriptors_) {
gatt_descriptor_macs_.begin(), gatt_descriptor_macs_.end(), auto* descriptor_mac =
[cb_descriptor]( static_cast<BluetoothRemoteGattDescriptorMac*>(pair.second.get());
const std::pair<const std::string, if (descriptor_mac->GetCBDescriptor() == cb_descriptor)
std::unique_ptr<BluetoothRemoteGattDescriptorMac>>& return descriptor_mac;
pair) {
return pair.second->GetCBDescriptor() == cb_descriptor;
});
if (found == gatt_descriptor_macs_.end()) {
return nullptr;
} else {
return found->second.get();
} }
return nullptr;
} }
DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<( DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<(
......
...@@ -20,10 +20,10 @@ namespace device { ...@@ -20,10 +20,10 @@ namespace device {
BluetoothRemoteGattCharacteristicWin::BluetoothRemoteGattCharacteristicWin( BluetoothRemoteGattCharacteristicWin::BluetoothRemoteGattCharacteristicWin(
BluetoothRemoteGattServiceWin* parent_service, BluetoothRemoteGattServiceWin* parent_service,
BTH_LE_GATT_CHARACTERISTIC* characteristic_info, BTH_LE_GATT_CHARACTERISTIC* characteristic_info,
scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) scoped_refptr<base::SequencedTaskRunner> ui_task_runner)
: parent_service_(parent_service), : parent_service_(parent_service),
characteristic_info_(characteristic_info), characteristic_info_(characteristic_info),
ui_task_runner_(ui_task_runner), ui_task_runner_(std::move(ui_task_runner)),
characteristic_added_notified_(false), characteristic_added_notified_(false),
characteristic_value_read_or_write_in_progress_(false), characteristic_value_read_or_write_in_progress_(false),
gatt_event_handle_(nullptr), gatt_event_handle_(nullptr),
...@@ -135,23 +135,6 @@ bool BluetoothRemoteGattCharacteristicWin::IsNotifying() const { ...@@ -135,23 +135,6 @@ bool BluetoothRemoteGattCharacteristicWin::IsNotifying() const {
return gatt_event_handle_ != nullptr; return gatt_event_handle_ != nullptr;
} }
std::vector<BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristicWin::GetDescriptors() const {
std::vector<BluetoothRemoteGattDescriptor*> descriptors;
for (const auto& descriptor : included_descriptors_)
descriptors.push_back(descriptor.second.get());
return descriptors;
}
BluetoothRemoteGattDescriptor*
BluetoothRemoteGattCharacteristicWin::GetDescriptor(
const std::string& identifier) const {
GattDescriptorMap::const_iterator it = included_descriptors_.find(identifier);
if (it != included_descriptors_.end())
return it->second.get();
return nullptr;
}
void BluetoothRemoteGattCharacteristicWin::ReadRemoteCharacteristic( void BluetoothRemoteGattCharacteristicWin::ReadRemoteCharacteristic(
const ValueCallback& callback, const ValueCallback& callback,
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
...@@ -264,23 +247,26 @@ void BluetoothRemoteGattCharacteristicWin::UpdateIncludedDescriptors( ...@@ -264,23 +247,26 @@ void BluetoothRemoteGattCharacteristicWin::UpdateIncludedDescriptors(
PBTH_LE_GATT_DESCRIPTOR descriptors, PBTH_LE_GATT_DESCRIPTOR descriptors,
uint16_t num) { uint16_t num) {
if (num == 0) { if (num == 0) {
included_descriptors_.clear(); descriptors_.clear();
return; return;
} }
// First, remove descriptors that no longer exist. // First, remove descriptors that no longer exist.
std::vector<std::string> to_be_removed; std::vector<std::string> to_be_removed;
for (const auto& d : included_descriptors_) { for (const auto& d : descriptors_) {
if (!DoesDescriptorExist(descriptors, num, d.second.get())) if (!DoesDescriptorExist(
descriptors, num,
static_cast<BluetoothRemoteGattDescriptorWin*>(d.second.get())))
to_be_removed.push_back(d.second->GetIdentifier()); to_be_removed.push_back(d.second->GetIdentifier());
} }
for (auto id : to_be_removed) { for (const auto& id : to_be_removed) {
included_descriptors_[id].reset(); auto iter = descriptors_.find(id);
included_descriptors_.erase(id); auto pair = std::move(*iter);
descriptors_.erase(iter);
} }
// Return if no new descriptors have been added. // Return if no new descriptors have been added.
if (included_descriptors_.size() == num) if (descriptors_.size() == num)
return; return;
// Add new descriptors. // Add new descriptors.
...@@ -293,20 +279,21 @@ void BluetoothRemoteGattCharacteristicWin::UpdateIncludedDescriptors( ...@@ -293,20 +279,21 @@ void BluetoothRemoteGattCharacteristicWin::UpdateIncludedDescriptors(
BluetoothRemoteGattDescriptorWin* descriptor = BluetoothRemoteGattDescriptorWin* descriptor =
new BluetoothRemoteGattDescriptorWin(this, win_descriptor_info, new BluetoothRemoteGattDescriptorWin(this, win_descriptor_info,
ui_task_runner_); ui_task_runner_);
included_descriptors_[descriptor->GetIdentifier()] = AddDescriptor(base::WrapUnique(descriptor));
base::WrapUnique(descriptor);
} }
} }
} }
bool BluetoothRemoteGattCharacteristicWin::IsDescriptorDiscovered( bool BluetoothRemoteGattCharacteristicWin::IsDescriptorDiscovered(
BTH_LE_UUID& uuid, const BTH_LE_UUID& uuid,
uint16_t attribute_handle) { uint16_t attribute_handle) {
BluetoothUUID bt_uuid = BluetoothUUID bt_uuid =
BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid(uuid); BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid(uuid);
for (const auto& d : included_descriptors_) { for (const auto& d : descriptors_) {
if (bt_uuid == d.second->GetUUID() && if (bt_uuid == d.second->GetUUID() &&
attribute_handle == d.second->GetAttributeHandle()) { attribute_handle ==
static_cast<BluetoothRemoteGattDescriptorWin*>(d.second.get())
->GetAttributeHandle()) {
return true; return true;
} }
} }
...@@ -409,9 +396,7 @@ void BluetoothRemoteGattCharacteristicWin::GattEventRegistrationCallback( ...@@ -409,9 +396,7 @@ void BluetoothRemoteGattCharacteristicWin::GattEventRegistrationCallback(
void BluetoothRemoteGattCharacteristicWin::ClearIncludedDescriptors() { void BluetoothRemoteGattCharacteristicWin::ClearIncludedDescriptors() {
// Explicitly reset to null to ensure that calling GetDescriptor() on the // Explicitly reset to null to ensure that calling GetDescriptor() on the
// removed descriptor in GattDescriptorRemoved() returns null. // removed descriptor in GattDescriptorRemoved() returns null.
for (auto& entry : included_descriptors_) std::exchange(descriptors_, {});
entry.second.reset();
included_descriptors_.clear();
} }
} // namespace device. } // namespace device.
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_WIN_H_ #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_WIN_H_
#include <memory> #include <memory>
#include <unordered_map> #include <string>
#include <utility>
#include <vector>
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h" #include "base/sequenced_task_runner.h"
...@@ -29,7 +31,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin ...@@ -29,7 +31,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin
BluetoothRemoteGattCharacteristicWin( BluetoothRemoteGattCharacteristicWin(
BluetoothRemoteGattServiceWin* parent_service, BluetoothRemoteGattServiceWin* parent_service,
BTH_LE_GATT_CHARACTERISTIC* characteristic_info, BTH_LE_GATT_CHARACTERISTIC* characteristic_info,
scoped_refptr<base::SequencedTaskRunner>& ui_task_runner); scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
~BluetoothRemoteGattCharacteristicWin() override; ~BluetoothRemoteGattCharacteristicWin() override;
// Override BluetoothRemoteGattCharacteristic interfaces. // Override BluetoothRemoteGattCharacteristic interfaces.
...@@ -40,9 +42,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin ...@@ -40,9 +42,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin
Properties GetProperties() const override; Properties GetProperties() const override;
Permissions GetPermissions() const override; Permissions GetPermissions() const override;
bool IsNotifying() const override; bool IsNotifying() const override;
std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const override;
BluetoothRemoteGattDescriptor* GetDescriptor(
const std::string& identifier) const override;
void ReadRemoteCharacteristic(const ValueCallback& callback, void ReadRemoteCharacteristic(const ValueCallback& callback,
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
void WriteRemoteCharacteristic(const std::vector<uint8_t>& value, void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
...@@ -73,7 +72,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin ...@@ -73,7 +72,8 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin
// Checks if the descriptor with |uuid| and |attribute_handle| has already // Checks if the descriptor with |uuid| and |attribute_handle| has already
// been discovered as included descriptor. // been discovered as included descriptor.
bool IsDescriptorDiscovered(BTH_LE_UUID& uuid, uint16_t attribute_handle); bool IsDescriptorDiscovered(const BTH_LE_UUID& uuid,
uint16_t attribute_handle);
// Checks if |descriptor| still exists in this characteristic according to // Checks if |descriptor| still exists in this characteristic according to
// newly discovered |num| of |descriptors|. // newly discovered |num| of |descriptors|.
...@@ -104,13 +104,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin ...@@ -104,13 +104,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWin
std::vector<uint8_t> characteristic_value_; std::vector<uint8_t> characteristic_value_;
std::string characteristic_identifier_; std::string characteristic_identifier_;
// The key of GattDescriptorMap is the identitfier of
// BluetoothRemoteGattDescriptorWin instance.
typedef std::unordered_map<std::string,
std::unique_ptr<BluetoothRemoteGattDescriptorWin>>
GattDescriptorMap;
GattDescriptorMap included_descriptors_;
// Flag indicates if characteristic added notification of this characteristic // Flag indicates if characteristic added notification of this characteristic
// has been sent out to avoid duplicate notification. // has been sent out to avoid duplicate notification.
bool characteristic_added_notified_; bool characteristic_added_notified_;
......
...@@ -48,19 +48,6 @@ BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicWinrt::GetService() ...@@ -48,19 +48,6 @@ BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicWinrt::GetService()
return nullptr; return nullptr;
} }
std::vector<BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristicWinrt::GetDescriptors() const {
NOTIMPLEMENTED();
return {};
}
BluetoothRemoteGattDescriptor*
BluetoothRemoteGattCharacteristicWinrt::GetDescriptor(
const std::string& identifier) const {
NOTIMPLEMENTED();
return nullptr;
}
void BluetoothRemoteGattCharacteristicWinrt::ReadRemoteCharacteristic( void BluetoothRemoteGattCharacteristicWinrt::ReadRemoteCharacteristic(
const ValueCallback& callback, const ValueCallback& callback,
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
......
...@@ -36,9 +36,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWinrt ...@@ -36,9 +36,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristicWinrt
// BluetoothRemoteGattCharacteristic: // BluetoothRemoteGattCharacteristic:
const std::vector<uint8_t>& GetValue() const override; const std::vector<uint8_t>& GetValue() const override;
BluetoothRemoteGattService* GetService() const override; BluetoothRemoteGattService* GetService() const override;
std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const override;
BluetoothRemoteGattDescriptor* GetDescriptor(
const std::string& identifier) const override;
void ReadRemoteCharacteristic(const ValueCallback& callback, void ReadRemoteCharacteristic(const ValueCallback& callback,
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
void WriteRemoteCharacteristic(const std::vector<uint8_t>& value, void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
......
...@@ -29,6 +29,8 @@ class BluetoothRemoteGattCharacteristic; ...@@ -29,6 +29,8 @@ class BluetoothRemoteGattCharacteristic;
class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattDescriptor class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattDescriptor
: public virtual BluetoothGattDescriptor { : public virtual BluetoothGattDescriptor {
public: public:
~BluetoothRemoteGattDescriptor() override;
// The ValueCallback is used to return the value of a remote characteristic // The ValueCallback is used to return the value of a remote characteristic
// descriptor upon a read request. // descriptor upon a read request.
typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback; typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback;
...@@ -60,7 +62,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattDescriptor ...@@ -60,7 +62,6 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattDescriptor
protected: protected:
BluetoothRemoteGattDescriptor(); BluetoothRemoteGattDescriptor();
~BluetoothRemoteGattDescriptor() override;
private: private:
DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattDescriptor); DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattDescriptor);
......
...@@ -155,21 +155,6 @@ bool BluetoothRemoteGattCharacteristicBlueZ::IsNotifying() const { ...@@ -155,21 +155,6 @@ bool BluetoothRemoteGattCharacteristicBlueZ::IsNotifying() const {
return has_notify_session_ && properties->notifying.value(); return has_notify_session_ && properties->notifying.value();
} }
std::vector<device::BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristicBlueZ::GetDescriptors() const {
std::vector<device::BluetoothRemoteGattDescriptor*> descriptors;
for (const auto& descriptor : descriptors_)
descriptors.push_back(descriptor.second.get());
return descriptors;
}
device::BluetoothRemoteGattDescriptor*
BluetoothRemoteGattCharacteristicBlueZ::GetDescriptor(
const std::string& identifier) const {
auto iter = descriptors_.find(dbus::ObjectPath(identifier));
return iter != descriptors_.end() ? iter->second.get() : nullptr;
}
void BluetoothRemoteGattCharacteristicBlueZ::ReadRemoteCharacteristic( void BluetoothRemoteGattCharacteristicBlueZ::ReadRemoteCharacteristic(
const ValueCallback& callback, const ValueCallback& callback,
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
...@@ -262,7 +247,7 @@ void BluetoothRemoteGattCharacteristicBlueZ::UnsubscribeFromNotifications( ...@@ -262,7 +247,7 @@ void BluetoothRemoteGattCharacteristicBlueZ::UnsubscribeFromNotifications(
void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded( void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded(
const dbus::ObjectPath& object_path) { const dbus::ObjectPath& object_path) {
if (descriptors_.find(object_path) != descriptors_.end()) { if (descriptors_.find(object_path.value()) != descriptors_.end()) {
VLOG(1) << "Remote GATT characteristic descriptor already exists: " VLOG(1) << "Remote GATT characteristic descriptor already exists: "
<< object_path.value(); << object_path.value();
return; return;
...@@ -284,7 +269,7 @@ void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded( ...@@ -284,7 +269,7 @@ void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded(
// NOTE: Can't use std::make_unique due to private constructor. // NOTE: Can't use std::make_unique due to private constructor.
BluetoothRemoteGattDescriptorBlueZ* descriptor = BluetoothRemoteGattDescriptorBlueZ* descriptor =
new BluetoothRemoteGattDescriptorBlueZ(this, object_path); new BluetoothRemoteGattDescriptorBlueZ(this, object_path);
descriptors_.emplace(object_path, base::WrapUnique(descriptor)); AddDescriptor(base::WrapUnique(descriptor));
DCHECK(descriptor->GetIdentifier() == object_path.value()); DCHECK(descriptor->GetIdentifier() == object_path.value());
DCHECK(descriptor->GetUUID().IsValid()); DCHECK(descriptor->GetUUID().IsValid());
DCHECK(service_); DCHECK(service_);
...@@ -295,7 +280,7 @@ void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded( ...@@ -295,7 +280,7 @@ void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorAdded(
void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorRemoved( void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorRemoved(
const dbus::ObjectPath& object_path) { const dbus::ObjectPath& object_path) {
DescriptorMap::iterator iter = descriptors_.find(object_path); auto iter = descriptors_.find(object_path.value());
if (iter == descriptors_.end()) { if (iter == descriptors_.end()) {
VLOG(2) << "Unknown descriptor removed: " << object_path.value(); VLOG(2) << "Unknown descriptor removed: " << object_path.value();
return; return;
...@@ -305,19 +290,21 @@ void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorRemoved( ...@@ -305,19 +290,21 @@ void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorRemoved(
<< GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); << GetIdentifier() << ", UUID: " << GetUUID().canonical_value();
auto descriptor = std::move(iter->second); auto descriptor = std::move(iter->second);
DCHECK(descriptor->object_path() == object_path); auto* descriptor_bluez =
static_cast<BluetoothRemoteGattDescriptorBlueZ*>(descriptor.get());
DCHECK(descriptor_bluez->object_path() == object_path);
descriptors_.erase(iter); descriptors_.erase(iter);
DCHECK(service_); DCHECK(service_);
static_cast<BluetoothRemoteGattServiceBlueZ*>(service_) static_cast<BluetoothRemoteGattServiceBlueZ*>(service_)
->NotifyDescriptorAddedOrRemoved(this, descriptor.get(), ->NotifyDescriptorAddedOrRemoved(this, descriptor_bluez,
false /* added */); false /* added */);
} }
void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorPropertyChanged( void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorPropertyChanged(
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const std::string& property_name) { const std::string& property_name) {
DescriptorMap::iterator iter = descriptors_.find(object_path); auto iter = descriptors_.find(object_path.value());
if (iter == descriptors_.end()) { if (iter == descriptors_.end()) {
VLOG(2) << "Unknown descriptor removed: " << object_path.value(); VLOG(2) << "Unknown descriptor removed: " << object_path.value();
return; return;
...@@ -335,8 +322,10 @@ void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorPropertyChanged( ...@@ -335,8 +322,10 @@ void BluetoothRemoteGattCharacteristicBlueZ::GattDescriptorPropertyChanged(
DCHECK(service_); DCHECK(service_);
static_cast<BluetoothRemoteGattServiceBlueZ*>(service_) static_cast<BluetoothRemoteGattServiceBlueZ*>(service_)
->NotifyDescriptorValueChanged(this, iter->second.get(), ->NotifyDescriptorValueChanged(
properties->value.value()); this,
static_cast<BluetoothRemoteGattDescriptorBlueZ*>(iter->second.get()),
properties->value.value());
} }
void BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifySuccess( void BluetoothRemoteGattCharacteristicBlueZ::OnStartNotifySuccess(
......
...@@ -33,7 +33,6 @@ class BluetoothRemoteGattService; ...@@ -33,7 +33,6 @@ class BluetoothRemoteGattService;
namespace bluez { namespace bluez {
class BluetoothRemoteGattDescriptorBlueZ;
class BluetoothRemoteGattServiceBlueZ; class BluetoothRemoteGattServiceBlueZ;
// The BluetoothRemoteGattCharacteristicBlueZ class implements // The BluetoothRemoteGattCharacteristicBlueZ class implements
...@@ -55,10 +54,6 @@ class BluetoothRemoteGattCharacteristicBlueZ ...@@ -55,10 +54,6 @@ class BluetoothRemoteGattCharacteristicBlueZ
const std::vector<uint8_t>& GetValue() const override; const std::vector<uint8_t>& GetValue() const override;
device::BluetoothRemoteGattService* GetService() const override; device::BluetoothRemoteGattService* GetService() const override;
bool IsNotifying() const override; bool IsNotifying() const override;
std::vector<device::BluetoothRemoteGattDescriptor*> GetDescriptors()
const override;
device::BluetoothRemoteGattDescriptor* GetDescriptor(
const std::string& identifier) const override;
void ReadRemoteCharacteristic(const ValueCallback& callback, void ReadRemoteCharacteristic(const ValueCallback& callback,
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
void WriteRemoteCharacteristic(const std::vector<uint8_t>& value, void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
...@@ -137,16 +132,6 @@ class BluetoothRemoteGattCharacteristicBlueZ ...@@ -137,16 +132,6 @@ class BluetoothRemoteGattCharacteristicBlueZ
// True, if there exists a Bluez notify session. // True, if there exists a Bluez notify session.
bool has_notify_session_; bool has_notify_session_;
using DescriptorMap =
std::map<dbus::ObjectPath,
std::unique_ptr<BluetoothRemoteGattDescriptorBlueZ>>;
// Mapping from GATT descriptor object paths to descriptor objects owned by
// this characteristic. Since the BlueZ implementation uses object paths
// as unique identifiers, we also use this mapping to return descriptors by
// identifier.
DescriptorMap descriptors_;
// The GATT service this GATT characteristic belongs to. // The GATT service this GATT characteristic belongs to.
BluetoothRemoteGattServiceBlueZ* service_; BluetoothRemoteGattServiceBlueZ* service_;
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "device/bluetooth/cast/bluetooth_remote_gatt_characteristic_cast.h" #include "device/bluetooth/cast/bluetooth_remote_gatt_characteristic_cast.h"
#include <memory>
#include "base/bind.h" #include "base/bind.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/callback_forward.h" #include "base/callback_forward.h"
...@@ -94,7 +96,7 @@ BluetoothRemoteGattCharacteristicCast::BluetoothRemoteGattCharacteristicCast( ...@@ -94,7 +96,7 @@ BluetoothRemoteGattCharacteristicCast::BluetoothRemoteGattCharacteristicCast(
auto descriptors = remote_characteristic_->GetDescriptors(); auto descriptors = remote_characteristic_->GetDescriptors();
descriptors_.reserve(descriptors.size()); descriptors_.reserve(descriptors.size());
for (const auto& descriptor : descriptors) { for (const auto& descriptor : descriptors) {
descriptors_.push_back( AddDescriptor(
std::make_unique<BluetoothRemoteGattDescriptorCast>(this, descriptor)); std::make_unique<BluetoothRemoteGattDescriptorCast>(this, descriptor));
} }
} }
...@@ -130,27 +132,6 @@ BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicCast::GetService() ...@@ -130,27 +132,6 @@ BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicCast::GetService()
return service_; return service_;
} }
std::vector<BluetoothRemoteGattDescriptor*>
BluetoothRemoteGattCharacteristicCast::GetDescriptors() const {
std::vector<BluetoothRemoteGattDescriptor*> descriptors;
descriptors.reserve(descriptors_.size());
for (auto& descriptor : descriptors_) {
descriptors.push_back(descriptor.get());
}
return descriptors;
}
BluetoothRemoteGattDescriptor*
BluetoothRemoteGattCharacteristicCast::GetDescriptor(
const std::string& identifier) const {
for (auto& descriptor : descriptors_) {
if (descriptor->GetIdentifier() == identifier) {
return descriptor.get();
}
}
return nullptr;
}
void BluetoothRemoteGattCharacteristicCast::ReadRemoteCharacteristic( void BluetoothRemoteGattCharacteristicCast::ReadRemoteCharacteristic(
const ValueCallback& callback, const ValueCallback& callback,
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include <set> #include <set>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
...@@ -24,7 +25,6 @@ class RemoteCharacteristic; ...@@ -24,7 +25,6 @@ class RemoteCharacteristic;
namespace device { namespace device {
class BluetoothRemoteGattDescriptorCast;
class BluetoothRemoteGattServiceCast; class BluetoothRemoteGattServiceCast;
class BluetoothRemoteGattCharacteristicCast class BluetoothRemoteGattCharacteristicCast
...@@ -45,9 +45,6 @@ class BluetoothRemoteGattCharacteristicCast ...@@ -45,9 +45,6 @@ class BluetoothRemoteGattCharacteristicCast
// BluetoothRemoteGattCharacteristic implementation: // BluetoothRemoteGattCharacteristic implementation:
const std::vector<uint8_t>& GetValue() const override; const std::vector<uint8_t>& GetValue() const override;
BluetoothRemoteGattService* GetService() const override; BluetoothRemoteGattService* GetService() const override;
std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const override;
BluetoothRemoteGattDescriptor* GetDescriptor(
const std::string& identifier) const override;
void ReadRemoteCharacteristic(const ValueCallback& callback, void ReadRemoteCharacteristic(const ValueCallback& callback,
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
void WriteRemoteCharacteristic(const std::vector<uint8_t>& value, void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
...@@ -92,7 +89,6 @@ class BluetoothRemoteGattCharacteristicCast ...@@ -92,7 +89,6 @@ class BluetoothRemoteGattCharacteristicCast
remote_characteristic_; remote_characteristic_;
std::vector<uint8_t> value_; std::vector<uint8_t> value_;
std::vector<std::unique_ptr<BluetoothRemoteGattDescriptorCast>> descriptors_;
base::WeakPtrFactory<BluetoothRemoteGattCharacteristicCast> weak_factory_; base::WeakPtrFactory<BluetoothRemoteGattCharacteristicCast> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicCast); DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattCharacteristicCast);
...@@ -100,4 +96,4 @@ class BluetoothRemoteGattCharacteristicCast ...@@ -100,4 +96,4 @@ class BluetoothRemoteGattCharacteristicCast
} // namespace device } // namespace device
#endif // DEVICE_BLUETOOTH_CAST_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_CAST_H_ #endif // DEVICE_BLUETOOTH_CAST_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_CAST_H_
\ No newline at end of file
...@@ -47,30 +47,20 @@ FakeRemoteGattCharacteristic::~FakeRemoteGattCharacteristic() = default; ...@@ -47,30 +47,20 @@ FakeRemoteGattCharacteristic::~FakeRemoteGattCharacteristic() = default;
std::string FakeRemoteGattCharacteristic::AddFakeDescriptor( std::string FakeRemoteGattCharacteristic::AddFakeDescriptor(
const device::BluetoothUUID& descriptor_uuid) { const device::BluetoothUUID& descriptor_uuid) {
FakeDescriptorMap::iterator it;
bool inserted;
// Attribute instance Ids need to be unique. // Attribute instance Ids need to be unique.
std::string new_descriptor_id = base::StringPrintf( std::string new_descriptor_id = base::StringPrintf(
"%s_%zu", GetIdentifier().c_str(), ++last_descriptor_id_); "%s_%zu", GetIdentifier().c_str(), ++last_descriptor_id_);
std::tie(it, inserted) = fake_descriptors_.emplace( bool result = AddDescriptor(std::make_unique<FakeRemoteGattDescriptor>(
new_descriptor_id, std::make_unique<FakeRemoteGattDescriptor>( new_descriptor_id, descriptor_uuid, this));
new_descriptor_id, descriptor_uuid, this));
DCHECK(inserted); DCHECK(result);
return it->second->GetIdentifier(); return new_descriptor_id;
} }
bool FakeRemoteGattCharacteristic::RemoveFakeDescriptor( bool FakeRemoteGattCharacteristic::RemoveFakeDescriptor(
const std::string& identifier) { const std::string& identifier) {
auto it = fake_descriptors_.find(identifier); return descriptors_.erase(identifier) != 0u;
if (it == fake_descriptors_.end()) {
return false;
}
fake_descriptors_.erase(it);
return true;
} }
void FakeRemoteGattCharacteristic::SetNextReadResponse( void FakeRemoteGattCharacteristic::SetNextReadResponse(
...@@ -103,8 +93,10 @@ bool FakeRemoteGattCharacteristic::AllResponsesConsumed() { ...@@ -103,8 +93,10 @@ bool FakeRemoteGattCharacteristic::AllResponsesConsumed() {
return !next_read_response_ && !next_write_response_ && return !next_read_response_ && !next_write_response_ &&
!next_subscribe_to_notifications_response_ && !next_subscribe_to_notifications_response_ &&
std::all_of( std::all_of(
fake_descriptors_.begin(), fake_descriptors_.end(), descriptors_.begin(), descriptors_.end(), [](const auto& e) {
[](const auto& e) { return e.second->AllResponsesConsumed(); }); return static_cast<FakeRemoteGattDescriptor*>(e.second.get())
->AllResponsesConsumed();
});
} }
std::string FakeRemoteGattCharacteristic::GetIdentifier() const { std::string FakeRemoteGattCharacteristic::GetIdentifier() const {
...@@ -136,24 +128,6 @@ device::BluetoothRemoteGattService* FakeRemoteGattCharacteristic::GetService() ...@@ -136,24 +128,6 @@ device::BluetoothRemoteGattService* FakeRemoteGattCharacteristic::GetService()
return service_; return service_;
} }
std::vector<device::BluetoothRemoteGattDescriptor*>
FakeRemoteGattCharacteristic::GetDescriptors() const {
std::vector<device::BluetoothRemoteGattDescriptor*> descriptors;
for (const auto& it : fake_descriptors_)
descriptors.push_back(it.second.get());
return descriptors;
}
device::BluetoothRemoteGattDescriptor*
FakeRemoteGattCharacteristic::GetDescriptor(
const std::string& identifier) const {
const auto& it = fake_descriptors_.find(identifier);
if (it == fake_descriptors_.end())
return nullptr;
return it->second.get();
}
void FakeRemoteGattCharacteristic::ReadRemoteCharacteristic( void FakeRemoteGattCharacteristic::ReadRemoteCharacteristic(
const ValueCallback& callback, const ValueCallback& callback,
const ErrorCallback& error_callback) { const ErrorCallback& error_callback) {
......
...@@ -83,10 +83,6 @@ class FakeRemoteGattCharacteristic ...@@ -83,10 +83,6 @@ class FakeRemoteGattCharacteristic
// device::BluetoothRemoteGattCharacteristic overrides: // device::BluetoothRemoteGattCharacteristic overrides:
const std::vector<uint8_t>& GetValue() const override; const std::vector<uint8_t>& GetValue() const override;
device::BluetoothRemoteGattService* GetService() const override; device::BluetoothRemoteGattService* GetService() const override;
std::vector<device::BluetoothRemoteGattDescriptor*> GetDescriptors()
const override;
device::BluetoothRemoteGattDescriptor* GetDescriptor(
const std::string& identifier) const override;
void ReadRemoteCharacteristic(const ValueCallback& callback, void ReadRemoteCharacteristic(const ValueCallback& callback,
const ErrorCallback& error_callback) override; const ErrorCallback& error_callback) override;
void WriteRemoteCharacteristic(const std::vector<uint8_t>& value, void WriteRemoteCharacteristic(const std::vector<uint8_t>& value,
...@@ -160,10 +156,6 @@ class FakeRemoteGattCharacteristic ...@@ -160,10 +156,6 @@ class FakeRemoteGattCharacteristic
size_t last_descriptor_id_; size_t last_descriptor_id_;
using FakeDescriptorMap =
std::map<std::string, std::unique_ptr<FakeRemoteGattDescriptor>>;
FakeDescriptorMap fake_descriptors_;
base::WeakPtrFactory<FakeRemoteGattCharacteristic> weak_ptr_factory_; base::WeakPtrFactory<FakeRemoteGattCharacteristic> weak_ptr_factory_;
}; };
......
...@@ -3,9 +3,13 @@ ...@@ -3,9 +3,13 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
#include <utility>
#include "device/bluetooth/test/mock_bluetooth_gatt_descriptor.h" #include "device/bluetooth/test/mock_bluetooth_gatt_descriptor.h"
#include "device/bluetooth/test/mock_bluetooth_gatt_service.h" #include "device/bluetooth/test/mock_bluetooth_gatt_service.h"
using testing::Invoke;
using testing::Return; using testing::Return;
using testing::ReturnRefOfCopy; using testing::ReturnRefOfCopy;
using testing::_; using testing::_;
...@@ -28,35 +32,20 @@ MockBluetoothGattCharacteristic::MockBluetoothGattCharacteristic( ...@@ -28,35 +32,20 @@ MockBluetoothGattCharacteristic::MockBluetoothGattCharacteristic(
ON_CALL(*this, GetProperties()).WillByDefault(Return(properties)); ON_CALL(*this, GetProperties()).WillByDefault(Return(properties));
ON_CALL(*this, GetPermissions()).WillByDefault(Return(permissions)); ON_CALL(*this, GetPermissions()).WillByDefault(Return(permissions));
ON_CALL(*this, IsNotifying()).WillByDefault(Return(false)); ON_CALL(*this, IsNotifying()).WillByDefault(Return(false));
ON_CALL(*this, GetDescriptors()) ON_CALL(*this, GetDescriptors()).WillByDefault(Invoke([this] {
.WillByDefault(Return(std::vector<BluetoothRemoteGattDescriptor*>())); return BluetoothRemoteGattCharacteristic::GetDescriptors();
}));
ON_CALL(*this, GetDescriptor(_))
.WillByDefault(Invoke([this](const std::string& id) {
return BluetoothRemoteGattCharacteristic::GetDescriptor(id);
}));
} }
MockBluetoothGattCharacteristic::~MockBluetoothGattCharacteristic() = default; MockBluetoothGattCharacteristic::~MockBluetoothGattCharacteristic() = default;
void MockBluetoothGattCharacteristic::AddMockDescriptor( void MockBluetoothGattCharacteristic::AddMockDescriptor(
std::unique_ptr<MockBluetoothGattDescriptor> mock_descriptor) { std::unique_ptr<MockBluetoothGattDescriptor> mock_descriptor) {
mock_descriptors_.push_back(std::move(mock_descriptor)); AddDescriptor(std::move(mock_descriptor));
}
std::vector<BluetoothRemoteGattDescriptor*>
MockBluetoothGattCharacteristic::GetMockDescriptors() const {
std::vector<BluetoothRemoteGattDescriptor*> descriptors;
for (auto& descriptor : mock_descriptors_) {
descriptors.push_back(descriptor.get());
}
return descriptors;
}
BluetoothRemoteGattDescriptor*
MockBluetoothGattCharacteristic::GetMockDescriptor(
const std::string& identifier) const {
for (auto& descriptor : mock_descriptors_) {
if (descriptor->GetIdentifier() == identifier) {
return descriptor.get();
}
}
return nullptr;
} }
} // namespace device } // namespace device
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -47,7 +48,6 @@ class MockBluetoothGattCharacteristic ...@@ -47,7 +48,6 @@ class MockBluetoothGattCharacteristic
std::vector<BluetoothRemoteGattDescriptor*>()); std::vector<BluetoothRemoteGattDescriptor*>());
MOCK_CONST_METHOD1(GetDescriptor, MOCK_CONST_METHOD1(GetDescriptor,
BluetoothRemoteGattDescriptor*(const std::string&)); BluetoothRemoteGattDescriptor*(const std::string&));
MOCK_METHOD1(AddDescriptor, bool(BluetoothRemoteGattDescriptor*));
MOCK_METHOD1(UpdateValue, bool(const std::vector<uint8_t>&)); MOCK_METHOD1(UpdateValue, bool(const std::vector<uint8_t>&));
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
MOCK_METHOD3(StartNotifySession, MOCK_METHOD3(StartNotifySession,
...@@ -74,9 +74,6 @@ class MockBluetoothGattCharacteristic ...@@ -74,9 +74,6 @@ class MockBluetoothGattCharacteristic
void AddMockDescriptor( void AddMockDescriptor(
std::unique_ptr<MockBluetoothGattDescriptor> mock_descriptor); std::unique_ptr<MockBluetoothGattDescriptor> mock_descriptor);
std::vector<BluetoothRemoteGattDescriptor*> GetMockDescriptors() const;
BluetoothRemoteGattDescriptor* GetMockDescriptor(
const std::string& identifier) const;
protected: protected:
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -97,8 +94,6 @@ class MockBluetoothGattCharacteristic ...@@ -97,8 +94,6 @@ class MockBluetoothGattCharacteristic
const ErrorCallback&)); const ErrorCallback&));
private: private:
std::vector<std::unique_ptr<MockBluetoothGattDescriptor>> mock_descriptors_;
DISALLOW_COPY_AND_ASSIGN(MockBluetoothGattCharacteristic); DISALLOW_COPY_AND_ASSIGN(MockBluetoothGattCharacteristic);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment