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

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

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Fix device.cc 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 {
13 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter,
14 std::unique_ptr<device::BluetoothGattConnection> connection)
15 : adapter_(std::move(adapter)), connection_(std::move(connection)) {
16 adapter_->AddObserver(this);
17 }
12 18
13 Device::Device(const std::string& address, 19 Device::~Device() {
14 scoped_refptr<device::BluetoothAdapter> adapter) 20 adapter_->RemoveObserver(this);
15 : address_(address), adapter_(std::move(adapter)) {} 21 }
16 22
17 Device::~Device() {} 23 // static
24 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter,
25 std::unique_ptr<device::BluetoothGattConnection> connection,
26 mojom::DeviceRequest request) {
27 auto* device_impl = new Device(adapter, std::move(connection));
ortuno 2016/11/17 03:54:45 nit: auto device_impl = base::WrapUnique(new Devic
mbrunson 2016/11/17 19:33:33 Doesn't doing std::move clear the device_impl uniq
28 auto binding = mojo::MakeStrongBinding(base::WrapUnique(device_impl),
29 std::move(request));
30 device_impl->binding_ = binding;
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 void Device::DeviceChanged(device::BluetoothAdapter* adapter,
38 device::BluetoothDevice* device = adapter_->GetDevice(address_); 53 device::BluetoothDevice* device) {
39 if (device) { 54 if (device->GetAddress() != GetAddress()) {
40 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); 55 return;
41 callback.Run(std::move(device_info)); 56 }
42 } else { 57
43 callback.Run(nullptr); 58 if (!device->IsGattConnected()) {
59 binding_->Close();
44 } 60 }
45 } 61 }
46 62
63 void Device::GattServicesDiscovered(device::BluetoothAdapter* adapter,
64 device::BluetoothDevice* device) {
65 if (device->GetAddress() != GetAddress()) {
66 return;
67 }
68
69 std::vector<base::Closure> requests;
70 requests.swap(pending_services_requests_);
71 for (const base::Closure& request : requests) {
72 request.Run();
73 }
74 }
75
76 void Device::Disconnect() {
77 binding_->Close();
78 }
79
80 void Device::GetInfo(const GetInfoCallback& callback) {
81 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
82 DCHECK(device);
83
84 callback.Run(ConstructDeviceInfoStruct(device));
85 }
86
87 void Device::GetServices(const GetServicesCallback& callback) {
88 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
89 DCHECK(device);
90
91 if (device->IsGattServicesDiscoveryComplete()) {
92 GetServicesImpl(callback);
93 return;
94 }
95
96 // pending_services_requests_ is owned by Device, so base::Unretained is
97 // safe.
98 pending_services_requests_.push_back(
99 base::Bind(&Device::GetServicesImpl, base::Unretained(this), callback));
100 }
101
102 void Device::GetServicesImpl(const GetServicesCallback& callback) {
103 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
104 DCHECK(device);
105
106 std::vector<mojom::ServiceInfoPtr> services;
107
108 for (const device::BluetoothRemoteGattService* service :
109 device->GetGattServices()) {
110 services.push_back(ConstructServiceInfoStruct(*service));
111 }
112
113 callback.Run(std::move(services));
114 }
115
116 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct(
117 const device::BluetoothRemoteGattService& service) {
118 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New();
119
120 service_info->uuid = service.GetUUID();
121 service_info->is_primary = service.IsPrimary();
122
123 return service_info;
124 }
125
126 const std::string& Device::GetAddress() {
127 return connection_->GetDeviceAddress();
128 }
129
47 } // namespace bluetooth 130 } // namespace bluetooth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698