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" | |
12 #include "device/bluetooth/bluetooth_adapter_mac.h" | 11 #include "device/bluetooth/bluetooth_adapter_mac.h" |
13 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" | 12 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
14 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | |
15 #include "device/bluetooth/bluetooth_uuid.h" | 13 #include "device/bluetooth/bluetooth_uuid.h" |
16 | 14 |
17 namespace device { | 15 namespace device { |
18 | 16 |
19 BluetoothRemoteGattServiceMac::BluetoothRemoteGattServiceMac( | 17 BluetoothRemoteGattServiceMac::BluetoothRemoteGattServiceMac( |
20 BluetoothLowEnergyDeviceMac* bluetooth_device_mac, | 18 BluetoothLowEnergyDeviceMac* bluetooth_device_mac, |
21 CBService* service, | 19 CBService* service, |
22 bool is_primary) | 20 bool is_primary) |
23 : bluetooth_device_mac_(bluetooth_device_mac), | 21 : bluetooth_device_mac_(bluetooth_device_mac), |
24 service_(service, base::scoped_policy::RETAIN), | 22 service_(service, base::scoped_policy::RETAIN), |
25 is_primary_(is_primary), | 23 is_primary_(is_primary) { |
26 is_discovery_complete_(false) { | |
27 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID([service_.get() UUID]); | 24 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID([service_.get() UUID]); |
28 identifier_ = | 25 identifier_ = |
29 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), | 26 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), |
30 (void*)service_] | 27 (void*)service_] |
31 .UTF8String; | 28 .UTF8String; |
32 } | 29 } |
33 | 30 |
34 BluetoothRemoteGattServiceMac::~BluetoothRemoteGattServiceMac() {} | 31 BluetoothRemoteGattServiceMac::~BluetoothRemoteGattServiceMac() {} |
35 | 32 |
36 std::string BluetoothRemoteGattServiceMac::GetIdentifier() const { | 33 std::string BluetoothRemoteGattServiceMac::GetIdentifier() const { |
37 return identifier_; | 34 return identifier_; |
38 } | 35 } |
39 | 36 |
40 BluetoothUUID BluetoothRemoteGattServiceMac::GetUUID() const { | 37 BluetoothUUID BluetoothRemoteGattServiceMac::GetUUID() const { |
41 return uuid_; | 38 return uuid_; |
42 } | 39 } |
43 | 40 |
44 bool BluetoothRemoteGattServiceMac::IsPrimary() const { | 41 bool BluetoothRemoteGattServiceMac::IsPrimary() const { |
45 return is_primary_; | 42 return is_primary_; |
46 } | 43 } |
47 | 44 |
48 BluetoothDevice* BluetoothRemoteGattServiceMac::GetDevice() const { | 45 BluetoothDevice* BluetoothRemoteGattServiceMac::GetDevice() const { |
49 return bluetooth_device_mac_; | 46 return bluetooth_device_mac_; |
50 } | 47 } |
51 | 48 |
52 std::vector<BluetoothRemoteGattCharacteristic*> | 49 std::vector<BluetoothRemoteGattCharacteristic*> |
53 BluetoothRemoteGattServiceMac::GetCharacteristics() const { | 50 BluetoothRemoteGattServiceMac::GetCharacteristics() const { |
54 std::vector<BluetoothRemoteGattCharacteristic*> gatt_characteristics; | 51 NOTIMPLEMENTED(); |
55 for (const auto& iter : gatt_characteristic_macs_) { | 52 return std::vector<BluetoothRemoteGattCharacteristic*>(); |
56 BluetoothRemoteGattCharacteristic* gatt_characteristic = | |
57 static_cast<BluetoothRemoteGattCharacteristic*>(iter.second.get()); | |
58 gatt_characteristics.push_back(gatt_characteristic); | |
59 } | |
60 return gatt_characteristics; | |
61 } | 53 } |
62 | 54 |
63 std::vector<BluetoothRemoteGattService*> | 55 std::vector<BluetoothRemoteGattService*> |
64 BluetoothRemoteGattServiceMac::GetIncludedServices() const { | 56 BluetoothRemoteGattServiceMac::GetIncludedServices() const { |
65 NOTIMPLEMENTED(); | 57 NOTIMPLEMENTED(); |
66 return std::vector<BluetoothRemoteGattService*>(); | 58 return std::vector<BluetoothRemoteGattService*>(); |
67 } | 59 } |
68 | 60 |
69 BluetoothRemoteGattCharacteristic* | 61 BluetoothRemoteGattCharacteristic* |
70 BluetoothRemoteGattServiceMac::GetCharacteristic( | 62 BluetoothRemoteGattServiceMac::GetCharacteristic( |
71 const std::string& identifier) const { | 63 const std::string& identifier) const { |
72 auto searched_pair = gatt_characteristic_macs_.find(identifier); | 64 NOTIMPLEMENTED(); |
73 if (searched_pair == gatt_characteristic_macs_.end()) { | 65 return nullptr; |
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(); | |
132 } | 66 } |
133 | 67 |
134 CBService* BluetoothRemoteGattServiceMac::GetService() const { | 68 CBService* BluetoothRemoteGattServiceMac::GetService() const { |
135 return service_.get(); | 69 return service_.get(); |
136 } | 70 } |
137 | 71 |
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 | |
156 } // namespace device | 72 } // namespace device |
OLD | NEW |