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

Side by Side Diff: components/arc/bluetooth/arc_bluetooth_bridge.cc

Issue 2085083002: arc: bluetooth: Implement Gatt notify (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix version check Created 4 years, 5 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 unified diff | Download patch
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | components/arc/common/bluetooth.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "components/arc/bluetooth/arc_bluetooth_bridge.h" 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <iomanip> 10 #include <iomanip>
(...skipping 27 matching lines...) Expand all
38 using device::BluetoothGattNotifySession; 38 using device::BluetoothGattNotifySession;
39 using device::BluetoothGattCharacteristic; 39 using device::BluetoothGattCharacteristic;
40 using device::BluetoothGattDescriptor; 40 using device::BluetoothGattDescriptor;
41 using device::BluetoothGattService; 41 using device::BluetoothGattService;
42 using device::BluetoothRemoteGattCharacteristic; 42 using device::BluetoothRemoteGattCharacteristic;
43 using device::BluetoothRemoteGattDescriptor; 43 using device::BluetoothRemoteGattDescriptor;
44 using device::BluetoothRemoteGattService; 44 using device::BluetoothRemoteGattService;
45 using device::BluetoothUUID; 45 using device::BluetoothUUID;
46 46
47 namespace { 47 namespace {
48 const int kMinBtleVersion = 1; 48 constexpr int32_t kMinBtleVersion = 1;
49 const uint32_t kGattReadPermission = 49 constexpr int32_t kMinBtleNotifyVersion = 2;
50 constexpr uint32_t kGattReadPermission =
50 BluetoothGattCharacteristic::Permission::PERMISSION_READ | 51 BluetoothGattCharacteristic::Permission::PERMISSION_READ |
51 BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED | 52 BluetoothGattCharacteristic::Permission::PERMISSION_READ_ENCRYPTED |
52 BluetoothGattCharacteristic::Permission:: 53 BluetoothGattCharacteristic::Permission::
53 PERMISSION_READ_ENCRYPTED_AUTHENTICATED; 54 PERMISSION_READ_ENCRYPTED_AUTHENTICATED;
54 const uint32_t kGattWritePermission = 55 constexpr uint32_t kGattWritePermission =
55 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE | 56 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE |
56 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED | 57 BluetoothGattCharacteristic::Permission::PERMISSION_WRITE_ENCRYPTED |
57 BluetoothGattCharacteristic::Permission:: 58 BluetoothGattCharacteristic::Permission::
58 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; 59 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED;
59 } // namespace 60 } // namespace
60 61
61 namespace arc { 62 namespace arc {
62 63
63 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) 64 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service)
64 : ArcService(bridge_service), binding_(this), weak_factory_(this) { 65 : ArcService(bridge_service), binding_(this), weak_factory_(this) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 125
125 if (device->IsConnected()) 126 if (device->IsConnected())
126 return; 127 return;
127 128
128 mojo::Array<mojom::BluetoothPropertyPtr> properties = 129 mojo::Array<mojom::BluetoothPropertyPtr> properties =
129 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); 130 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device);
130 131
131 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( 132 arc_bridge_service()->bluetooth_instance()->OnDeviceFound(
132 std::move(properties)); 133 std::move(properties));
133 134
134 if (arc_bridge_service()->bluetooth_version() < kMinBtleVersion) { 135 if (!CheckBluetoothInstanceVersion(kMinBtleVersion))
135 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE";
136 return; 136 return;
137 }
138 137
139 mojom::BluetoothAddressPtr addr = 138 mojom::BluetoothAddressPtr addr =
140 mojom::BluetoothAddress::From(device->GetAddress()); 139 mojom::BluetoothAddress::From(device->GetAddress());
141 int rssi = device->GetInquiryRSSI(); 140 int rssi = device->GetInquiryRSSI();
142 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = 141 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data =
143 GetAdvertisingData(device); 142 GetAdvertisingData(device);
144 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( 143 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound(
145 std::move(addr), rssi, std::move(adv_data)); 144 std::move(addr), rssi, std::move(adv_data));
146 } 145 }
147 146
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 BluetoothDevice* device, 197 BluetoothDevice* device,
199 BluetoothRemoteGattService* service) { 198 BluetoothRemoteGattService* service) {
200 // Placeholder for GATT client functionality 199 // Placeholder for GATT client functionality
201 } 200 }
202 201
203 void ArcBluetoothBridge::GattServicesDiscovered(BluetoothAdapter* adapter, 202 void ArcBluetoothBridge::GattServicesDiscovered(BluetoothAdapter* adapter,
204 BluetoothDevice* device) { 203 BluetoothDevice* device) {
205 if (!HasBluetoothInstance()) 204 if (!HasBluetoothInstance())
206 return; 205 return;
207 206
208 if (arc_bridge_service()->bluetooth_version() < kMinBtleVersion) { 207 if (!CheckBluetoothInstanceVersion(kMinBtleVersion))
209 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE";
210 return; 208 return;
211 }
212 209
213 mojom::BluetoothAddressPtr addr = 210 mojom::BluetoothAddressPtr addr =
214 mojom::BluetoothAddress::From(device->GetAddress()); 211 mojom::BluetoothAddress::From(device->GetAddress());
215 212
216 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( 213 arc_bridge_service()->bluetooth_instance()->OnSearchComplete(
217 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); 214 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS);
218 } 215 }
219 216
220 void ArcBluetoothBridge::GattDiscoveryCompleteForService( 217 void ArcBluetoothBridge::GattDiscoveryCompleteForService(
221 BluetoothAdapter* adapter, 218 BluetoothAdapter* adapter,
(...skipping 28 matching lines...) Expand all
250 void ArcBluetoothBridge::GattDescriptorRemoved( 247 void ArcBluetoothBridge::GattDescriptorRemoved(
251 BluetoothAdapter* adapter, 248 BluetoothAdapter* adapter,
252 BluetoothRemoteGattDescriptor* descriptor) { 249 BluetoothRemoteGattDescriptor* descriptor) {
253 // Placeholder for GATT client functionality 250 // Placeholder for GATT client functionality
254 } 251 }
255 252
256 void ArcBluetoothBridge::GattCharacteristicValueChanged( 253 void ArcBluetoothBridge::GattCharacteristicValueChanged(
257 BluetoothAdapter* adapter, 254 BluetoothAdapter* adapter,
258 BluetoothRemoteGattCharacteristic* characteristic, 255 BluetoothRemoteGattCharacteristic* characteristic,
259 const std::vector<uint8_t>& value) { 256 const std::vector<uint8_t>& value) {
260 // Placeholder for GATT client functionality 257 if (!HasBluetoothInstance())
258 return;
259
260 if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
261 return;
262
263 BluetoothRemoteGattService* service = characteristic->GetService();
264 BluetoothDevice* device = service->GetDevice();
265 mojom::BluetoothAddressPtr address =
266 mojom::BluetoothAddress::From(device->GetAddress());
267 mojom::BluetoothGattServiceIDPtr service_id =
268 mojom::BluetoothGattServiceID::New();
269 service_id->is_primary = service->IsPrimary();
270 service_id->id = mojom::BluetoothGattID::New();
271 service_id->id->inst_id = ConvertGattIdentifierToId(service->GetIdentifier());
272 service_id->id->uuid = mojom::BluetoothUUID::From(service->GetUUID());
273
274 mojom::BluetoothGattIDPtr char_id = mojom::BluetoothGattID::New();
275 char_id->inst_id = ConvertGattIdentifierToId(characteristic->GetIdentifier());
276 char_id->uuid = mojom::BluetoothUUID::From(characteristic->GetUUID());
277
278 arc_bridge_service()->bluetooth_instance()->OnGattNotify(
279 std::move(address), std::move(service_id), std::move(char_id),
280 true /* is_notify */, mojo::Array<uint8_t>::From(value));
261 } 281 }
262 282
263 void ArcBluetoothBridge::GattDescriptorValueChanged( 283 void ArcBluetoothBridge::GattDescriptorValueChanged(
264 BluetoothAdapter* adapter, 284 BluetoothAdapter* adapter,
265 BluetoothRemoteGattDescriptor* descriptor, 285 BluetoothRemoteGattDescriptor* descriptor,
266 const std::vector<uint8_t>& value) { 286 const std::vector<uint8_t>& value) {
267 // Placeholder for GATT client functionality 287 // Placeholder for GATT client functionality
268 } 288 }
269 289
270 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) { 290 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) {
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 void ArcBluetoothBridge::StopLEScan() { 550 void ArcBluetoothBridge::StopLEScan() {
531 CancelDiscovery(); 551 CancelDiscovery();
532 } 552 }
533 553
534 void ArcBluetoothBridge::OnGattConnectStateChanged( 554 void ArcBluetoothBridge::OnGattConnectStateChanged(
535 mojom::BluetoothAddressPtr addr, 555 mojom::BluetoothAddressPtr addr,
536 bool connected) const { 556 bool connected) const {
537 if (!HasBluetoothInstance()) 557 if (!HasBluetoothInstance())
538 return; 558 return;
539 559
540 if (arc_bridge_service()->bluetooth_version() < kMinBtleVersion) { 560 if (!CheckBluetoothInstanceVersion(kMinBtleVersion))
541 LOG(WARNING) << "Bluetooth instance is too old and does not support BTLE";
542 return; 561 return;
543 }
544 562
545 DCHECK(addr); 563 DCHECK(addr);
546 564
547 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( 565 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange(
548 std::move(addr), connected); 566 std::move(addr), connected);
549 } 567 }
550 568
551 void ArcBluetoothBridge::OnGattConnected( 569 void ArcBluetoothBridge::OnGattConnected(
552 mojom::BluetoothAddressPtr addr, 570 mojom::BluetoothAddressPtr addr,
553 std::unique_ptr<BluetoothGattConnection> connection) const { 571 std::unique_ptr<BluetoothGattConnection> connection) const {
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 mojom::BluetoothGattIDPtr char_id, 913 mojom::BluetoothGattIDPtr char_id,
896 mojom::BluetoothGattIDPtr desc_id, 914 mojom::BluetoothGattIDPtr desc_id,
897 mojom::BluetoothGattValuePtr value, 915 mojom::BluetoothGattValuePtr value,
898 const WriteGattDescriptorCallback& callback) { 916 const WriteGattDescriptorCallback& callback) {
899 BluetoothRemoteGattDescriptor* descriptor = 917 BluetoothRemoteGattDescriptor* descriptor =
900 FindGattDescriptor(std::move(remote_addr), std::move(service_id), 918 FindGattDescriptor(std::move(remote_addr), std::move(service_id),
901 std::move(char_id), std::move(desc_id)); 919 std::move(char_id), std::move(desc_id));
902 DCHECK(descriptor); 920 DCHECK(descriptor);
903 DCHECK(descriptor->GetPermissions() & kGattWritePermission); 921 DCHECK(descriptor->GetPermissions() & kGattWritePermission);
904 922
923 // To register / deregister GATT notification, we need to
924 // 1) Write to CCC Descriptor to enable/disable the notification
925 // 2) Ask BT hw to register / deregister the notification
926 // The Chrome API groups both steps into one API, and does not support writing
927 // directly to the CCC Descriptor. Therefore, until we fix
928 // https://crbug.com/622832, we return successfully when we encounter this.
929 // TODO(http://crbug.com/622832)
930 if (descriptor->GetUUID() ==
931 BluetoothGattDescriptor::ClientCharacteristicConfigurationUuid()) {
932 OnGattWriteDone(callback);
933 return;
934 }
935
905 descriptor->WriteRemoteDescriptor( 936 descriptor->WriteRemoteDescriptor(
906 value->value.To<std::vector<uint8_t>>(), 937 value->value.To<std::vector<uint8_t>>(),
907 base::Bind(&ArcBluetoothBridge::OnGattWriteDone, 938 base::Bind(&ArcBluetoothBridge::OnGattWriteDone,
908 weak_factory_.GetWeakPtr(), callback), 939 weak_factory_.GetWeakPtr(), callback),
909 base::Bind(&ArcBluetoothBridge::OnGattWriteError, 940 base::Bind(&ArcBluetoothBridge::OnGattWriteError,
910 weak_factory_.GetWeakPtr(), callback)); 941 weak_factory_.GetWeakPtr(), callback));
911 } 942 }
912 943
913 void ArcBluetoothBridge::OnGattNotifyStartDone( 944 void ArcBluetoothBridge::OnGattNotifyStartDone(
914 const RegisterForGattNotificationCallback& callback, 945 const RegisterForGattNotificationCallback& callback,
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING 1341 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING
1311 // to 1342 // to
1312 // make sure the bond state machine on Android is ready to take the 1343 // make sure the bond state machine on Android is ready to take the
1313 // pair-done event. Otherwise the pair-done event will be dropped as an 1344 // pair-done event. Otherwise the pair-done event will be dropped as an
1314 // invalid change of paired status. 1345 // invalid change of paired status.
1315 OnPairing(addr->Clone()); 1346 OnPairing(addr->Clone());
1316 OnPairedDone(std::move(addr)); 1347 OnPairedDone(std::move(addr));
1317 } 1348 }
1318 } 1349 }
1319 1350
1351 bool ArcBluetoothBridge::CheckBluetoothInstanceVersion(
1352 int32_t version_need) const {
1353 int32_t version = arc_bridge_service()->bluetooth_version();
1354 if (version >= version_need)
1355 return true;
1356 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1357 << ") need version " << version_need;
1358 return false;
1359 }
1360
1320 } // namespace arc 1361 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | components/arc/common/bluetooth.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698