Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 6 | 6 |
| 7 #import <CoreBluetooth/CoreBluetooth.h> | 7 #import <CoreBluetooth/CoreBluetooth.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | |
| 11 #include "device/bluetooth/bluetooth_adapter_mac.h" | 12 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 12 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" | 13 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
| 14 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | |
| 13 #include "device/bluetooth/bluetooth_uuid.h" | 15 #include "device/bluetooth/bluetooth_uuid.h" |
| 14 | 16 |
| 15 namespace device { | 17 namespace device { |
| 16 | 18 |
| 17 BluetoothRemoteGattServiceMac::BluetoothRemoteGattServiceMac( | 19 BluetoothRemoteGattServiceMac::BluetoothRemoteGattServiceMac( |
| 18 BluetoothLowEnergyDeviceMac* bluetooth_device_mac, | 20 BluetoothLowEnergyDeviceMac* bluetooth_device_mac, |
| 19 CBService* service, | 21 CBService* service, |
| 20 bool is_primary) | 22 bool is_primary) |
| 21 : bluetooth_device_mac_(bluetooth_device_mac), | 23 : bluetooth_device_mac_(bluetooth_device_mac), |
| 22 service_(service, base::scoped_policy::RETAIN), | 24 service_(service, base::scoped_policy::RETAIN), |
| 23 is_primary_(is_primary) { | 25 is_primary_(is_primary), |
| 26 is_discovery_complete_(false) { | |
| 24 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID([service_.get() UUID]); | 27 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID([service_.get() UUID]); |
| 25 identifier_ = | 28 identifier_ = |
| 26 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), | 29 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), |
| 27 (void*)service_] | 30 (void*)service_] |
| 28 .UTF8String; | 31 .UTF8String; |
| 29 } | 32 } |
| 30 | 33 |
| 31 BluetoothRemoteGattServiceMac::~BluetoothRemoteGattServiceMac() {} | 34 BluetoothRemoteGattServiceMac::~BluetoothRemoteGattServiceMac() {} |
| 32 | 35 |
| 33 std::string BluetoothRemoteGattServiceMac::GetIdentifier() const { | 36 std::string BluetoothRemoteGattServiceMac::GetIdentifier() const { |
| 34 return identifier_; | 37 return identifier_; |
| 35 } | 38 } |
| 36 | 39 |
| 37 BluetoothUUID BluetoothRemoteGattServiceMac::GetUUID() const { | 40 BluetoothUUID BluetoothRemoteGattServiceMac::GetUUID() const { |
| 38 return uuid_; | 41 return uuid_; |
| 39 } | 42 } |
| 40 | 43 |
| 41 bool BluetoothRemoteGattServiceMac::IsPrimary() const { | 44 bool BluetoothRemoteGattServiceMac::IsPrimary() const { |
| 42 return is_primary_; | 45 return is_primary_; |
| 43 } | 46 } |
| 44 | 47 |
| 45 BluetoothDevice* BluetoothRemoteGattServiceMac::GetDevice() const { | 48 BluetoothDevice* BluetoothRemoteGattServiceMac::GetDevice() const { |
| 46 return bluetooth_device_mac_; | 49 return bluetooth_device_mac_; |
| 47 } | 50 } |
| 48 | 51 |
| 49 std::vector<BluetoothRemoteGattCharacteristic*> | 52 std::vector<BluetoothRemoteGattCharacteristic*> |
| 50 BluetoothRemoteGattServiceMac::GetCharacteristics() const { | 53 BluetoothRemoteGattServiceMac::GetCharacteristics() const { |
| 51 NOTIMPLEMENTED(); | 54 std::vector<BluetoothRemoteGattCharacteristic*> gatt_characteristics; |
| 52 return std::vector<BluetoothRemoteGattCharacteristic*>(); | 55 for (const auto& iter : gatt_characteristic_macs_) { |
| 56 BluetoothRemoteGattCharacteristic* gatt_characteristic = | |
| 57 static_cast<BluetoothRemoteGattCharacteristic*>(iter.second.get()); | |
| 58 gatt_characteristics.push_back(gatt_characteristic); | |
| 59 } | |
| 60 return gatt_characteristics; | |
| 53 } | 61 } |
| 54 | 62 |
| 55 std::vector<BluetoothRemoteGattService*> | 63 std::vector<BluetoothRemoteGattService*> |
| 56 BluetoothRemoteGattServiceMac::GetIncludedServices() const { | 64 BluetoothRemoteGattServiceMac::GetIncludedServices() const { |
| 57 NOTIMPLEMENTED(); | 65 NOTIMPLEMENTED(); |
| 58 return std::vector<BluetoothRemoteGattService*>(); | 66 return std::vector<BluetoothRemoteGattService*>(); |
| 59 } | 67 } |
| 60 | 68 |
| 61 BluetoothRemoteGattCharacteristic* | 69 BluetoothRemoteGattCharacteristic* |
| 62 BluetoothRemoteGattServiceMac::GetCharacteristic( | 70 BluetoothRemoteGattServiceMac::GetCharacteristic( |
| 63 const std::string& identifier) const { | 71 const std::string& identifier) const { |
| 64 NOTIMPLEMENTED(); | 72 auto searched_pair = gatt_characteristic_macs_.find(identifier); |
| 65 return nullptr; | 73 if (searched_pair == gatt_characteristic_macs_.end()) { |
| 74 return nullptr; | |
| 75 } | |
| 76 return static_cast<BluetoothRemoteGattCharacteristic*>( | |
| 77 searched_pair->second.get()); | |
| 78 } | |
| 79 | |
| 80 void BluetoothRemoteGattServiceMac::DiscoverCharacteristics() { | |
| 81 is_discovery_complete_ = false; | |
| 82 [GetCBPeripheral() discoverCharacteristics:nil forService:GetService()]; | |
| 83 } | |
| 84 | |
| 85 void BluetoothRemoteGattServiceMac::DidDiscoverCharacteristics() { | |
|
ortuno
2016/06/15 19:28:51
When would you call this method for a service for
jlebel
2016/06/15 20:28:46
Every time at least one characteristic is added, r
| |
| 86 DCHECK(!is_discovery_complete_); | |
| 87 std::unordered_set<std::string> characteristic_identifier_to_remove; | |
| 88 for (const auto& iter : gatt_characteristic_macs_) { | |
| 89 characteristic_identifier_to_remove.insert(iter.first); | |
| 90 } | |
| 91 for (CBCharacteristic* cb_characteristic in GetService().characteristics) { | |
|
ortuno
2016/06/15 19:28:51
nit: new line above.
jlebel
2016/06/15 20:28:47
Done.
| |
| 92 BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac = | |
| 93 GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); | |
| 94 if (gatt_characteristic_mac) { | |
| 95 std::string identifier = gatt_characteristic_mac->GetIdentifier(); | |
|
ortuno
2016/06/15 19:28:51
nit: const std::string&
ortuno
2016/06/15 19:28:51
nit: const std::string&
jlebel
2016/06/15 20:28:47
Done.
jlebel
2016/06/15 20:28:47
Done.
| |
| 96 characteristic_identifier_to_remove.erase(identifier); | |
| 97 continue; | |
| 98 } | |
| 99 gatt_characteristic_mac = | |
| 100 new BluetoothRemoteGattCharacteristicMac(this, cb_characteristic); | |
| 101 std::string identifier = gatt_characteristic_mac->GetIdentifier(); | |
| 102 auto result_iter = gatt_characteristic_macs_.insert( | |
| 103 {identifier, base::WrapUnique(gatt_characteristic_mac)}); | |
| 104 DCHECK(result_iter.second); | |
| 105 GetMacAdapter()->NotifyGattCharacteristicAdded(gatt_characteristic_mac); | |
| 106 } | |
| 107 for (const std::string& identifier : characteristic_identifier_to_remove) { | |
|
ortuno
2016/06/15 19:28:51
nit: new line above.
jlebel
2016/06/15 20:28:47
Done.
| |
| 108 auto pair_to_remove = gatt_characteristic_macs_.find(identifier); | |
| 109 std::unique_ptr<BluetoothRemoteGattCharacteristicMac> | |
| 110 characteristic_to_remove; | |
| 111 pair_to_remove->second.swap(characteristic_to_remove); | |
| 112 gatt_characteristic_macs_.erase(pair_to_remove); | |
| 113 GetMacAdapter()->NotifyGattCharacteristicRemoved( | |
| 114 characteristic_to_remove.get()); | |
| 115 } | |
| 116 is_discovery_complete_ = true; | |
| 117 GetMacAdapter()->NotifyGattServiceChanged(this); | |
|
ortuno
2016/06/15 19:28:51
I don't think we want to call this here. See bluez
jlebel
2016/06/15 20:28:47
If I don't put this line, BluetoothRemoteGattServi
ortuno
2016/06/15 20:57:47
*sigh* we really need fix the order of our events.
| |
| 118 } | |
| 119 | |
| 120 bool BluetoothRemoteGattServiceMac::IsDiscoveryComplete() { | |
| 121 return is_discovery_complete_; | |
| 122 } | |
| 123 | |
| 124 BluetoothAdapterMac* BluetoothRemoteGattServiceMac::GetMacAdapter() const { | |
| 125 return bluetooth_device_mac_->GetMacAdapter(); | |
| 126 } | |
| 127 | |
| 128 CBPeripheral* BluetoothRemoteGattServiceMac::GetCBPeripheral() const { | |
| 129 return bluetooth_device_mac_->GetPeripheral(); | |
| 66 } | 130 } |
| 67 | 131 |
| 68 CBService* BluetoothRemoteGattServiceMac::GetService() const { | 132 CBService* BluetoothRemoteGattServiceMac::GetService() const { |
| 69 return service_.get(); | 133 return service_.get(); |
| 70 } | 134 } |
| 71 | 135 |
| 136 BluetoothRemoteGattCharacteristicMac* | |
| 137 BluetoothRemoteGattServiceMac::GetBluetoothRemoteGattCharacteristicMac( | |
| 138 CBCharacteristic* characteristic) const { | |
| 139 auto found = std::find_if( | |
| 140 gatt_characteristic_macs_.begin(), gatt_characteristic_macs_.end(), | |
| 141 [characteristic]( | |
| 142 const std::pair< | |
| 143 const std::string, | |
| 144 std::unique_ptr<BluetoothRemoteGattCharacteristicMac>>& pair) { | |
| 145 return pair.second->GetCBCharacteristic() == characteristic; | |
| 146 }); | |
| 147 if (found == gatt_characteristic_macs_.end()) { | |
| 148 return nullptr; | |
| 149 } else { | |
| 150 return found->second.get(); | |
| 151 } | |
| 152 } | |
| 153 | |
| 72 } // namespace device | 154 } // namespace device |
| OLD | NEW |