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

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

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

Powered by Google App Engine
This is Rietveld 408576698