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

Side by Side Diff: device/bluetooth/device.cc

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Merge upstream, fix issues Created 4 years, 1 month 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
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 <utility> 5 #include <utility>
6 #include <vector>
6 7
7 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
8 #include "device/bluetooth/device.h" 9 #include "device/bluetooth/device.h"
9 #include "mojo/public/cpp/bindings/strong_binding.h" 10 #include "mojo/public/cpp/bindings/strong_binding.h"
10 11
11 namespace bluetooth { 12 namespace bluetooth {
12 13
13 Device::Device(const std::string& address, 14 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter,
14 scoped_refptr<device::BluetoothAdapter> adapter) 15 std::unique_ptr<device::BluetoothGattConnection> connection,
15 : address_(address), adapter_(std::move(adapter)) {} 16 mojom::DeviceRequest request)
17 : adapter_(std::move(adapter)),
18 connection_(std::move(connection)),
19 binding_(this, std::move(request)) {
20 // Binding is owned by this Device, so base::Unretained is safe.
21 binding_.set_connection_error_handler(
22 base::Bind(&Device::Disconnect, base::Unretained(this)));
16 23
17 Device::~Device() {} 24 adapter_->AddObserver(this);
25 }
26
27 Device::~Device() {
28 binding_.Close();
29 adapter_->RemoveObserver(this);
30 }
18 31
19 // static 32 // static
20 mojom::DeviceInfoPtr Device::ConstructDeviceInfoStruct( 33 mojom::DeviceInfoPtr Device::ConstructDeviceInfoStruct(
21 const device::BluetoothDevice* device) { 34 const device::BluetoothDevice* device) {
22 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); 35 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New();
23 36
24 device_info->name = device->GetName(); 37 device_info->name = device->GetName();
25 device_info->name_for_display = 38 device_info->name_for_display =
26 base::UTF16ToUTF8(device->GetNameForDisplay()); 39 base::UTF16ToUTF8(device->GetNameForDisplay());
27 device_info->address = device->GetAddress(); 40 device_info->address = device->GetAddress();
41 device_info->is_gatt_connected = device->IsGattConnected();
28 42
29 if (device->GetInquiryRSSI()) { 43 if (device->GetInquiryRSSI()) {
30 device_info->rssi = mojom::RSSIWrapper::New(); 44 device_info->rssi = mojom::RSSIWrapper::New();
31 device_info->rssi->value = device->GetInquiryRSSI().value(); 45 device_info->rssi->value = device->GetInquiryRSSI().value();
32 } 46 }
33 47
34 return device_info; 48 return device_info;
35 } 49 }
36 50
37 void Device::GetInfo(const GetInfoCallback& callback) { 51 mojo::Binding<mojom::Device>* Device::GetBindingForTesting() {
38 device::BluetoothDevice* device = adapter_->GetDevice(address_); 52 return &binding_;
39 if (device) { 53 }
40 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); 54
41 callback.Run(std::move(device_info)); 55 void Device::DeviceChanged(device::BluetoothAdapter* adapter,
42 } else { 56 device::BluetoothDevice* device) {
43 callback.Run(nullptr); 57 if (device->GetAddress() != GetAddress()) {
58 return;
59 }
60
61 if (!device->IsGattConnected()) {
62 delete this;
44 } 63 }
45 } 64 }
46 65
66 void Device::GattServicesDiscovered(device::BluetoothAdapter* adapter,
67 device::BluetoothDevice* device) {
68 if (device->GetAddress() != GetAddress()) {
69 return;
70 }
71
72 std::vector<base::Closure> requests;
73 requests.swap(pending_services_requests_);
74 for (const base::Closure& request : requests) {
75 request.Run();
76 }
77 }
78
79 void Device::Disconnect() {
80 delete this;
81 }
82
83 void Device::GetInfo(const GetInfoCallback& callback) {
84 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
85 DCHECK(device);
86
87 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device);
88 callback.Run(std::move(device_info));
89 }
90
91 void Device::GetServices(const GetServicesCallback& callback) {
92 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
93 DCHECK(device);
94
95 if (device->IsGattServicesDiscoveryComplete()) {
96 GetServicesImpl(callback);
97 return;
98 }
99
100 // pending_services_requests_ is owned by Device, so base::Unretained is
101 // safe.
102 pending_services_requests_.push_back(
103 base::Bind(&Device::GetServicesImpl, base::Unretained(this), callback));
104 }
105
106 void Device::GetServicesImpl(const GetServicesCallback& callback) {
107 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
108 DCHECK(device);
109
110 std::vector<mojom::ServiceInfoPtr> services;
111
112 for (const device::BluetoothRemoteGattService* service :
113 device->GetGattServices()) {
114 mojom::ServiceInfoPtr service_info = ConstructServiceInfoStruct(*service);
115 services.push_back(std::move(service_info));
116 }
117
118 callback.Run(std::move(services));
119 }
120
121 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct(
122 const device::BluetoothRemoteGattService& service) {
123 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New();
124
125 service_info->uuid = service.GetUUID();
126 service_info->is_primary = service.IsPrimary();
127
128 return service_info;
129 }
130
131 const std::string& Device::GetAddress() {
132 return connection_->GetDeviceAddress();
133 }
134
47 } // namespace bluetooth 135 } // namespace bluetooth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698