OLD | NEW |
---|---|
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_.set_connection_error_handler( | |
22 base::Bind(&Device::Disconnect, base::Unretained(this))); | |
ortuno
2016/11/01 06:27:39
base::Unretained always raises some alarms. It's l
mbrunson
2016/11/02 01:25:45
Hmm. Should I prefer WeakPtrFactory over base::Unr
ortuno
2016/11/02 07:56:04
We shouldn't add complexity unless it's needed. A
| |
16 | 23 |
17 Device::~Device() {} | 24 adapter_->AddObserver(this); |
25 } | |
26 | |
27 Device::~Device() { | |
28 binding_.Close(); | |
29 | |
30 if (connection_->IsConnected()) | |
31 connection_->Disconnect(); | |
ortuno
2016/11/01 06:27:39
No need to call Disconnect. Destroying the object
mbrunson
2016/11/02 01:25:45
Removed. Done.
| |
32 | |
33 adapter_->RemoveObserver(this); | |
34 } | |
18 | 35 |
19 // static | 36 // static |
20 mojom::DeviceInfoPtr Device::ConstructDeviceInfoStruct( | 37 mojom::DeviceInfoPtr Device::ConstructDeviceInfoStruct( |
21 const device::BluetoothDevice* device) { | 38 const device::BluetoothDevice* device) { |
22 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); | 39 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); |
23 | 40 |
24 device_info->name = device->GetName(); | 41 device_info->name = device->GetName(); |
25 device_info->name_for_display = | 42 device_info->name_for_display = |
26 base::UTF16ToUTF8(device->GetNameForDisplay()); | 43 base::UTF16ToUTF8(device->GetNameForDisplay()); |
27 device_info->address = device->GetAddress(); | 44 device_info->address = device->GetAddress(); |
45 device_info->is_gatt_connected = device->IsGattConnected(); | |
28 | 46 |
29 if (device->GetInquiryRSSI()) { | 47 if (device->GetInquiryRSSI()) { |
30 device_info->rssi = mojom::RSSIWrapper::New(); | 48 device_info->rssi = mojom::RSSIWrapper::New(); |
31 device_info->rssi->value = device->GetInquiryRSSI().value(); | 49 device_info->rssi->value = device->GetInquiryRSSI().value(); |
32 } | 50 } |
33 | 51 |
34 return device_info; | 52 return device_info; |
35 } | 53 } |
36 | 54 |
55 void Device::set_connection_error_handler(const base::Closure& closure) { | |
56 binding_.set_connection_error_handler(closure); | |
57 } | |
58 | |
59 void Device::DeviceChanged(device::BluetoothAdapter* adapter, | |
60 device::BluetoothDevice* device) { | |
61 if (device->GetAddress() == GetAddress()) { | |
ortuno
2016/11/01 06:27:39
nit:
if (device->GetAddress() != GetAddress()) {
mbrunson
2016/11/02 01:25:45
Done.
| |
62 if (!device->IsGattConnected()) { | |
63 delete this; | |
64 } | |
65 } | |
66 } | |
67 | |
68 void Device::GattServicesDiscovered(device::BluetoothAdapter* adapter, | |
69 device::BluetoothDevice* device) { | |
70 if (device->GetAddress() == GetAddress()) { | |
ortuno
2016/11/01 06:27:39
nit:
if (device->GetAddress() != GetAddress()) {
mbrunson
2016/11/02 01:25:45
Done.
| |
71 for (const base::Closure& request : pending_services_requests_) { | |
72 request.Run(); | |
ortuno
2016/11/01 06:27:39
Running closures in a for loop could lead to unexp
mbrunson
2016/11/02 01:25:45
Are you saying running the requests removes them f
ortuno
2016/11/02 07:56:04
Or adds them. For example we in device/bluetooth w
| |
73 } | |
74 | |
75 pending_services_requests_.clear(); | |
76 } | |
77 } | |
78 | |
79 void Device::Disconnect() { | |
80 delete this; | |
81 } | |
82 | |
37 void Device::GetInfo(const GetInfoCallback& callback) { | 83 void Device::GetInfo(const GetInfoCallback& callback) { |
38 device::BluetoothDevice* device = adapter_->GetDevice(address_); | 84 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
39 if (device) { | 85 if (device) { |
ortuno
2016/11/01 06:27:39
Now that lifetime of this class is owned by the co
mbrunson
2016/11/02 01:25:46
Done.
| |
40 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); | 86 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); |
41 callback.Run(std::move(device_info)); | 87 callback.Run(std::move(device_info)); |
42 } else { | 88 } else { |
43 callback.Run(nullptr); | 89 callback.Run(nullptr); |
44 } | 90 } |
45 } | 91 } |
46 | 92 |
93 void Device::GetServices(const GetServicesCallback& callback) { | |
94 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | |
95 | |
96 if (device) { | |
ortuno
2016/11/01 06:27:39
Same here. I don't think you need the if anymore.
mbrunson
2016/11/02 01:25:45
Done.
| |
97 if (device->IsGattServicesDiscoveryComplete()) { | |
98 GetServicesImpl(callback); | |
99 return; | |
100 } | |
101 | |
102 pending_services_requests_.push_back( | |
103 base::Bind(&Device::GetServicesImpl, base::Unretained(this), callback)); | |
ortuno
2016/11/01 06:27:39
Here it's a bit harder to see why base::Unretained
| |
104 return; | |
105 } | |
106 | |
107 callback.Run(std::vector<mojom::ServiceInfoPtr>()); | |
108 } | |
109 | |
110 void Device::GetServicesImpl(const GetServicesCallback& callback) { | |
111 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | |
112 std::vector<mojom::ServiceInfoPtr> services; | |
113 | |
114 for (const device::BluetoothRemoteGattService* service : | |
115 device->GetGattServices()) { | |
116 mojom::ServiceInfoPtr service_info = ConstructServiceInfoStruct(service); | |
117 services.push_back(std::move(service_info)); | |
118 } | |
119 | |
120 callback.Run(std::move(services)); | |
121 } | |
122 | |
123 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct( | |
ortuno
2016/11/01 06:27:39
Since you know service will always be there just t
mbrunson
2016/11/02 01:25:46
Done.
| |
124 const device::BluetoothRemoteGattService* service) { | |
125 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New(); | |
126 | |
127 service_info->uuid = service->GetUUID(); | |
128 service_info->is_primary = service->IsPrimary(); | |
129 | |
130 return service_info; | |
131 } | |
132 | |
133 std::string Device::GetAddress() { | |
134 return connection_->GetDeviceAddress(); | |
135 } | |
136 | |
47 } // namespace bluetooth | 137 } // namespace bluetooth |
OLD | NEW |