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

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

Issue 2448713002: bluetooth: Add Device connection logic and accompanying user interface. (Closed)
Patch Set: Add TypeConverter for ConnectResult 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
« no previous file with comments | « device/bluetooth/device.h ('k') | device/bluetooth/device_unittest.cc » ('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 <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() {
14 adapter_->RemoveObserver(this);
15 }
12 16
13 Device::Device(const std::string& address, 17 // static
14 scoped_refptr<device::BluetoothAdapter> adapter) 18 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter,
15 : address_(address), adapter_(std::move(adapter)) {} 19 std::unique_ptr<device::BluetoothGattConnection> connection,
16 20 mojom::DeviceRequest request) {
17 Device::~Device() {} 21 auto device_impl =
22 base::WrapUnique(new Device(adapter, std::move(connection)));
23 auto* device_ptr = device_impl.get();
dcheng 2016/11/18 19:27:03 Nit: save some lines by just doing: auto* device
mbrunson 2016/11/18 20:55:14 ortuno wanted me to change this earlier so I'm jus
dcheng 2016/11/18 20:58:41 Condensing it is probably slightly better. I'd get
mbrunson 2016/11/18 21:39:17 I'll remove |binding| then. Done.
24 auto binding =
25 mojo::MakeStrongBinding(std::move(device_impl), std::move(request));
26 device_ptr->binding_ = binding;
27 }
18 28
19 // static 29 // static
20 mojom::DeviceInfoPtr Device::ConstructDeviceInfoStruct( 30 mojom::DeviceInfoPtr Device::ConstructDeviceInfoStruct(
21 const device::BluetoothDevice* device) { 31 const device::BluetoothDevice* device) {
22 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); 32 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New();
23 33
24 device_info->name = device->GetName(); 34 device_info->name = device->GetName();
25 device_info->name_for_display = 35 device_info->name_for_display =
26 base::UTF16ToUTF8(device->GetNameForDisplay()); 36 base::UTF16ToUTF8(device->GetNameForDisplay());
27 device_info->address = device->GetAddress(); 37 device_info->address = device->GetAddress();
38 device_info->is_gatt_connected = device->IsGattConnected();
28 39
29 if (device->GetInquiryRSSI()) { 40 if (device->GetInquiryRSSI()) {
30 device_info->rssi = mojom::RSSIWrapper::New(); 41 device_info->rssi = mojom::RSSIWrapper::New();
31 device_info->rssi->value = device->GetInquiryRSSI().value(); 42 device_info->rssi->value = device->GetInquiryRSSI().value();
32 } 43 }
33 44
34 return device_info; 45 return device_info;
35 } 46 }
36 47
37 void Device::GetInfo(const GetInfoCallback& callback) { 48 void Device::DeviceChanged(device::BluetoothAdapter* adapter,
38 device::BluetoothDevice* device = adapter_->GetDevice(address_); 49 device::BluetoothDevice* device) {
39 if (device) { 50 if (device->GetAddress() != GetAddress()) {
40 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); 51 return;
41 callback.Run(std::move(device_info)); 52 }
42 } else { 53
43 callback.Run(nullptr); 54 if (!device->IsGattConnected()) {
55 binding_->Close();
44 } 56 }
45 } 57 }
46 58
59 void Device::GattServicesDiscovered(device::BluetoothAdapter* adapter,
60 device::BluetoothDevice* device) {
61 if (device->GetAddress() != GetAddress()) {
62 return;
63 }
64
65 std::vector<base::Closure> requests;
66 requests.swap(pending_services_requests_);
67 for (const base::Closure& request : requests) {
68 request.Run();
69 }
70 }
71
72 void Device::Disconnect() {
73 binding_->Close();
74 }
75
76 void Device::GetInfo(const GetInfoCallback& callback) {
77 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
78 DCHECK(device);
79
80 callback.Run(ConstructDeviceInfoStruct(device));
81 }
82
83 void Device::GetServices(const GetServicesCallback& callback) {
84 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
85 DCHECK(device);
86
87 if (device->IsGattServicesDiscoveryComplete()) {
88 GetServicesImpl(callback);
89 return;
90 }
91
92 // pending_services_requests_ is owned by Device, so base::Unretained is
93 // safe.
94 pending_services_requests_.push_back(
95 base::Bind(&Device::GetServicesImpl, base::Unretained(this), callback));
96 }
97
98 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter,
99 std::unique_ptr<device::BluetoothGattConnection> connection)
100 : adapter_(std::move(adapter)), connection_(std::move(connection)) {
101 adapter_->AddObserver(this);
102 }
103
104 void Device::GetServicesImpl(const GetServicesCallback& callback) {
105 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress());
106 DCHECK(device);
107
108 std::vector<mojom::ServiceInfoPtr> services;
109
110 for (const device::BluetoothRemoteGattService* service :
111 device->GetGattServices()) {
112 services.push_back(ConstructServiceInfoStruct(*service));
113 }
114
115 callback.Run(std::move(services));
116 }
117
118 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct(
119 const device::BluetoothRemoteGattService& service) {
120 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New();
121
122 service_info->uuid = service.GetUUID();
123 service_info->is_primary = service.IsPrimary();
124
125 return service_info;
126 }
127
128 const std::string& Device::GetAddress() {
129 return connection_->GetDeviceAddress();
130 }
131
47 } // namespace bluetooth 132 } // namespace bluetooth
OLDNEW
« no previous file with comments | « device/bluetooth/device.h ('k') | device/bluetooth/device_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698