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() { |
| 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 |
| 92 for (CBCharacteristic* cb_characteristic in GetService().characteristics) { |
| 93 BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac = |
| 94 GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
| 95 if (gatt_characteristic_mac) { |
| 96 const std::string& identifier = gatt_characteristic_mac->GetIdentifier(); |
| 97 characteristic_identifier_to_remove.erase(identifier); |
| 98 continue; |
| 99 } |
| 100 gatt_characteristic_mac = |
| 101 new BluetoothRemoteGattCharacteristicMac(this, cb_characteristic); |
| 102 const std::string& identifier = gatt_characteristic_mac->GetIdentifier(); |
| 103 auto result_iter = gatt_characteristic_macs_.insert( |
| 104 {identifier, base::WrapUnique(gatt_characteristic_mac)}); |
| 105 DCHECK(result_iter.second); |
| 106 GetMacAdapter()->NotifyGattCharacteristicAdded(gatt_characteristic_mac); |
| 107 } |
| 108 |
| 109 for (const std::string& identifier : characteristic_identifier_to_remove) { |
| 110 auto pair_to_remove = gatt_characteristic_macs_.find(identifier); |
| 111 std::unique_ptr<BluetoothRemoteGattCharacteristicMac> |
| 112 characteristic_to_remove; |
| 113 pair_to_remove->second.swap(characteristic_to_remove); |
| 114 gatt_characteristic_macs_.erase(pair_to_remove); |
| 115 GetMacAdapter()->NotifyGattCharacteristicRemoved( |
| 116 characteristic_to_remove.get()); |
| 117 } |
| 118 is_discovery_complete_ = true; |
| 119 GetMacAdapter()->NotifyGattServiceChanged(this); |
| 120 } |
| 121 |
| 122 bool BluetoothRemoteGattServiceMac::IsDiscoveryComplete() { |
| 123 return is_discovery_complete_; |
| 124 } |
| 125 |
| 126 BluetoothAdapterMac* BluetoothRemoteGattServiceMac::GetMacAdapter() const { |
| 127 return bluetooth_device_mac_->GetMacAdapter(); |
| 128 } |
| 129 |
| 130 CBPeripheral* BluetoothRemoteGattServiceMac::GetCBPeripheral() const { |
| 131 return bluetooth_device_mac_->GetPeripheral(); |
66 } | 132 } |
67 | 133 |
68 CBService* BluetoothRemoteGattServiceMac::GetService() const { | 134 CBService* BluetoothRemoteGattServiceMac::GetService() const { |
69 return service_.get(); | 135 return service_.get(); |
70 } | 136 } |
71 | 137 |
| 138 BluetoothRemoteGattCharacteristicMac* |
| 139 BluetoothRemoteGattServiceMac::GetBluetoothRemoteGattCharacteristicMac( |
| 140 CBCharacteristic* characteristic) const { |
| 141 auto found = std::find_if( |
| 142 gatt_characteristic_macs_.begin(), gatt_characteristic_macs_.end(), |
| 143 [characteristic]( |
| 144 const std::pair< |
| 145 const std::string, |
| 146 std::unique_ptr<BluetoothRemoteGattCharacteristicMac>>& pair) { |
| 147 return pair.second->GetCBCharacteristic() == characteristic; |
| 148 }); |
| 149 if (found == gatt_characteristic_macs_.end()) { |
| 150 return nullptr; |
| 151 } else { |
| 152 return found->second.get(); |
| 153 } |
| 154 } |
| 155 |
72 } // namespace device | 156 } // namespace device |
OLD | NEW |