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

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

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

Powered by Google App Engine
This is Rietveld 408576698