Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4025)

Unified Diff: chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.cc

Issue 262053003: chrome.bluetoothLowEnergy: Implement getCharacteristic. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.cc
diff --git a/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.cc b/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.cc
index c505780136886dd7a15420fab6cdc759d374d623..5dd4ec1203bfba8fbd43f4a622f4f0d2bd22f35c 100644
--- a/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.cc
+++ b/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.cc
@@ -102,11 +102,6 @@ void PopulateCharacteristic(const BluetoothGattCharacteristic* characteristic,
namespace extensions {
-BluetoothLowEnergyEventRouter::GattObjectData::GattObjectData() {
-}
-BluetoothLowEnergyEventRouter::GattObjectData::~GattObjectData() {
-}
-
BluetoothLowEnergyEventRouter::BluetoothLowEnergyEventRouter(
content::BrowserContext* context)
: adapter_(NULL), browser_context_(context), weak_ptr_factory_(this) {
@@ -140,20 +135,9 @@ BluetoothLowEnergyEventRouter::~BluetoothLowEnergyEventRouter() {
observed_gatt_services_.begin();
iter != observed_gatt_services_.end();
++iter) {
- InstanceIdToObjectDataMap::const_iterator id_iter =
- service_ids_to_objects_.find(*iter);
- if (id_iter == service_ids_to_objects_.end())
- continue;
-
- GattObjectData data = id_iter->second;
- BluetoothDevice* device = adapter_->GetDevice(data.device_address);
- if (!device)
- continue;
-
- BluetoothGattService* service = device->GetGattService(data.service_id);
+ BluetoothGattService* service = FindServiceById(*iter);
if (!service)
continue;
-
service->RemoveObserver(this);
}
@@ -312,6 +296,26 @@ bool BluetoothLowEnergyEventRouter::GetCharacteristics(
return true;
}
+bool BluetoothLowEnergyEventRouter::GetCharacteristic(
+ const std::string& instance_id,
+ apibtle::Characteristic* out_characteristic) const {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ if (!adapter_) {
+ VLOG(1) << "BluetoothAdapter not ready.";
+ return false;
+ }
+
+ BluetoothGattCharacteristic* characteristic =
+ FindCharacteristicById(instance_id);
+ if (!characteristic) {
+ VLOG(1) << "Characteristic not found: " << instance_id;
+ return false;
+ }
+
+ PopulateCharacteristic(characteristic, out_characteristic);
+ return true;
+}
+
void BluetoothLowEnergyEventRouter::SetAdapterForTesting(
device::BluetoothAdapter* adapter) {
adapter_ = adapter;
@@ -344,16 +348,14 @@ void BluetoothLowEnergyEventRouter::GattServiceAdded(
DCHECK(observed_gatt_services_.find(service->GetIdentifier()) ==
observed_gatt_services_.end());
- DCHECK(service_ids_to_objects_.find(service->GetIdentifier()) ==
- service_ids_to_objects_.end());
+ DCHECK(service_id_to_device_address_.find(service->GetIdentifier()) ==
+ service_id_to_device_address_.end());
service->AddObserver(this);
- observed_gatt_services_.insert(service->GetIdentifier());
- GattObjectData data;
- data.device_address = device->GetAddress();
- data.service_id = service->GetIdentifier();
- service_ids_to_objects_[data.service_id] = data;
+ const std::string& service_id = service->GetIdentifier();
+ observed_gatt_services_.insert(service_id);
+ service_id_to_device_address_[service_id] = device->GetAddress();
// Signal API event.
apibtle::Service api_service;
@@ -374,15 +376,15 @@ void BluetoothLowEnergyEventRouter::GattServiceRemoved(
DCHECK(observed_gatt_services_.find(service->GetIdentifier()) !=
observed_gatt_services_.end());
- DCHECK(service_ids_to_objects_.find(service->GetIdentifier()) !=
- service_ids_to_objects_.end());
+ DCHECK(service_id_to_device_address_.find(service->GetIdentifier()) !=
+ service_id_to_device_address_.end());
service->RemoveObserver(this);
observed_gatt_services_.erase(service->GetIdentifier());
DCHECK(device->GetAddress() ==
- service_ids_to_objects_[service->GetIdentifier()].device_address);
- service_ids_to_objects_.erase(service->GetIdentifier());
+ service_id_to_device_address_[service->GetIdentifier()]);
+ service_id_to_device_address_.erase(service->GetIdentifier());
// Signal API event.
apibtle::Service api_service;
@@ -402,8 +404,8 @@ void BluetoothLowEnergyEventRouter::GattServiceChanged(
DCHECK(observed_gatt_services_.find(service->GetIdentifier()) !=
observed_gatt_services_.end());
- DCHECK(service_ids_to_objects_.find(service->GetIdentifier()) !=
- service_ids_to_objects_.end());
+ DCHECK(service_id_to_device_address_.find(service->GetIdentifier()) !=
+ service_id_to_device_address_.end());
// Signal API event.
apibtle::Service api_service;
@@ -422,14 +424,11 @@ void BluetoothLowEnergyEventRouter::GattCharacteristicAdded(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
VLOG(2) << "GATT characteristic added: " << characteristic->GetIdentifier();
- DCHECK(chrc_ids_to_objects_.find(characteristic->GetIdentifier()) ==
- chrc_ids_to_objects_.end());
+ DCHECK(chrc_id_to_service_id_.find(characteristic->GetIdentifier()) ==
+ chrc_id_to_service_id_.end());
- GattObjectData data;
- data.device_address = service->GetDevice()->GetAddress();
- data.service_id = service->GetIdentifier();
- data.characteristic_id = characteristic->GetIdentifier();
- chrc_ids_to_objects_[characteristic->GetIdentifier()] = data;
+ chrc_id_to_service_id_[characteristic->GetIdentifier()] =
+ service->GetIdentifier();
}
void BluetoothLowEnergyEventRouter::GattCharacteristicRemoved(
@@ -438,14 +437,12 @@ void BluetoothLowEnergyEventRouter::GattCharacteristicRemoved(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
VLOG(2) << "GATT characteristic removed: " << characteristic->GetIdentifier();
- DCHECK(chrc_ids_to_objects_.find(characteristic->GetIdentifier()) !=
- chrc_ids_to_objects_.end());
- DCHECK(service->GetDevice()->GetAddress() ==
- chrc_ids_to_objects_[characteristic->GetIdentifier()].device_address);
+ DCHECK(chrc_id_to_service_id_.find(characteristic->GetIdentifier()) !=
+ chrc_id_to_service_id_.end());
DCHECK(service->GetIdentifier() ==
- chrc_ids_to_objects_[characteristic->GetIdentifier()].service_id);
+ chrc_id_to_service_id_[characteristic->GetIdentifier()]);
- chrc_ids_to_objects_.erase(characteristic->GetIdentifier());
+ chrc_id_to_service_id_.erase(characteristic->GetIdentifier());
}
void BluetoothLowEnergyEventRouter::GattCharacteristicValueChanged(
@@ -458,11 +455,6 @@ void BluetoothLowEnergyEventRouter::GattCharacteristicValueChanged(
void BluetoothLowEnergyEventRouter::OnGetAdapter(
const base::Closure& callback,
scoped_refptr<device::BluetoothAdapter> adapter) {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- DCHECK(service_ids_to_objects_.empty());
- DCHECK(observed_devices_.empty());
- DCHECK(observed_gatt_services_.empty());
-
adapter_ = adapter;
// Initialize instance ID mappings for all discovered GATT objects and add
@@ -474,6 +466,12 @@ void BluetoothLowEnergyEventRouter::OnGetAdapter(
}
void BluetoothLowEnergyEventRouter::InitializeIdentifierMappings() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ DCHECK(service_id_to_device_address_.empty());
+ DCHECK(chrc_id_to_service_id_.empty());
+ DCHECK(observed_devices_.empty());
+ DCHECK(observed_gatt_services_.empty());
+
// Devices
BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
for (BluetoothAdapter::DeviceList::iterator iter = devices.begin();
@@ -490,12 +488,10 @@ void BluetoothLowEnergyEventRouter::InitializeIdentifierMappings() {
++siter) {
BluetoothGattService* service = *siter;
service->AddObserver(this);
- observed_gatt_services_.insert(service->GetIdentifier());
- GattObjectData service_data;
- service_data.device_address = device->GetAddress();
- service_data.service_id = service->GetIdentifier();
- service_ids_to_objects_[service_data.service_id] = service_data;
+ const std::string& service_id = service->GetIdentifier();
+ observed_gatt_services_.insert(service_id);
+ service_id_to_device_address_[service_id] = device->GetAddress();
// Characteristics
std::vector<BluetoothGattCharacteristic*> characteristics =
@@ -505,12 +501,9 @@ void BluetoothLowEnergyEventRouter::InitializeIdentifierMappings() {
citer != characteristics.end();
++citer) {
BluetoothGattCharacteristic* characteristic = *citer;
- GattObjectData characteristic_data;
- characteristic_data.device_address = device->GetAddress();
- characteristic_data.service_id = service->GetIdentifier();
- characteristic_data.characteristic_id = characteristic->GetIdentifier();
- chrc_ids_to_objects_[characteristic_data.characteristic_id] =
- characteristic_data;
+
+ const std::string& chrc_id = characteristic->GetIdentifier();
+ chrc_id_to_service_id_[chrc_id] = service_id;
// TODO(armansito): Initialize mapping for descriptors.
}
@@ -520,33 +513,57 @@ void BluetoothLowEnergyEventRouter::InitializeIdentifierMappings() {
BluetoothGattService* BluetoothLowEnergyEventRouter::FindServiceById(
const std::string& instance_id) const {
- InstanceIdToObjectDataMap::const_iterator iter =
- service_ids_to_objects_.find(instance_id);
- if (iter == service_ids_to_objects_.end()) {
+ InstanceIdMap::const_iterator iter =
+ service_id_to_device_address_.find(instance_id);
+ if (iter == service_id_to_device_address_.end()) {
VLOG(1) << "GATT service identifier unknown: " << instance_id;
return NULL;
}
- GattObjectData data = iter->second;
- DCHECK(!data.device_address.empty());
- DCHECK(!data.service_id.empty());
- DCHECK(data.characteristic_id.empty());
- DCHECK(data.descriptor_id.empty());
+ const std::string& address = iter->second;
- BluetoothDevice* device = adapter_->GetDevice(data.device_address);
+ BluetoothDevice* device = adapter_->GetDevice(address);
if (!device) {
- VLOG(1) << "Bluetooth device not found: " << data.device_address;
+ VLOG(1) << "Bluetooth device not found: " << address;
return NULL;
}
- BluetoothGattService* service = device->GetGattService(data.service_id);
+ BluetoothGattService* service = device->GetGattService(instance_id);
if (!service) {
- VLOG(1) << "GATT service with ID \"" << data.service_id
- << "\" not found on device \"" << data.device_address << "\"";
+ VLOG(1) << "GATT service with ID \"" << instance_id
+ << "\" not found on device \"" << address << "\"";
return NULL;
}
return service;
}
+BluetoothGattCharacteristic*
+BluetoothLowEnergyEventRouter::FindCharacteristicById(
+ const std::string& instance_id) const {
+ InstanceIdMap::const_iterator iter = chrc_id_to_service_id_.find(instance_id);
+ if (iter == chrc_id_to_service_id_.end()) {
+ VLOG(1) << "GATT characteristic identifier unknown: " << instance_id;
+ return NULL;
+ }
+
+ const std::string& service_id = iter->second;
+
+ BluetoothGattService* service = FindServiceById(service_id);
+ if (!service) {
+ VLOG(1) << "Failed to obtain service for characteristic: " << instance_id;
+ return NULL;
+ }
+
+ BluetoothGattCharacteristic* characteristic =
+ service->GetCharacteristic(instance_id);
+ if (!characteristic) {
+ VLOG(1) << "GATT characteristic with ID \"" << instance_id
+ << "\" not found on service \"" << service_id << "\"";
+ return NULL;
+ }
+
+ return characteristic;
+}
+
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698