OLD | NEW |
---|---|
1 // Copyright 2015 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 "components/proximity_auth/ble/bluetooth_low_energy_connection.h" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_weave_client_connec tion.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | |
16 #include "components/proximity_auth/ble/bluetooth_low_energy_characteristics_fin der.h" | |
17 #include "components/proximity_auth/ble/fake_wire_message.h" | |
18 #include "components/proximity_auth/bluetooth_throttler.h" | 13 #include "components/proximity_auth/bluetooth_throttler.h" |
19 #include "components/proximity_auth/connection_finder.h" | 14 #include "components/proximity_auth/connection_finder.h" |
20 #include "components/proximity_auth/logging/logging.h" | 15 #include "components/proximity_auth/logging/logging.h" |
21 #include "components/proximity_auth/wire_message.h" | 16 #include "components/proximity_auth/wire_message.h" |
22 #include "device/bluetooth/bluetooth_adapter.h" | |
23 #include "device/bluetooth/bluetooth_device.h" | |
24 #include "device/bluetooth/bluetooth_gatt_connection.h" | 17 #include "device/bluetooth/bluetooth_gatt_connection.h" |
25 #include "device/bluetooth/bluetooth_gatt_notify_session.h" | |
26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | |
27 #include "device/bluetooth/bluetooth_uuid.h" | |
28 | 18 |
29 using device::BluetoothAdapter; | 19 using device::BluetoothAdapter; |
30 using device::BluetoothDevice; | 20 using device::BluetoothDevice; |
31 using device::BluetoothGattConnection; | 21 using device::BluetoothGattConnection; |
32 using device::BluetoothRemoteGattService; | 22 using device::BluetoothRemoteGattService; |
33 using device::BluetoothRemoteGattCharacteristic; | 23 using device::BluetoothRemoteGattCharacteristic; |
34 using device::BluetoothGattNotifySession; | 24 using device::BluetoothGattNotifySession; |
35 using device::BluetoothUUID; | 25 using device::BluetoothUUID; |
36 | 26 |
37 namespace proximity_auth { | |
38 namespace { | 27 namespace { |
39 | 28 |
40 // The UUID of the characteristic used to send data to the peripheral. | 29 typedef proximity_auth::BluetoothLowEnergyWeavePacketReceiver::State |
41 const char kToPeripheralCharUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; | 30 ReceiverState; |
42 | 31 |
43 // The UUID of the characteristic used to receive data from the peripheral. | 32 // TODO(jingxy): figure out the right UUID to use |
44 const char kFromPeripheralCharUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; | 33 // The UUID of the TX characteristic used to send data to the server. |
Kyle Horimoto
2016/06/27 18:06:02
s/send/transmit/
jingxuy
2016/06/30 00:27:20
Done.
| |
34 const char kTXCharacteristicUUID[] = "977c6674-1239-4e72-993b-502369b8bb5a"; | |
Kyle Horimoto
2016/06/27 18:06:02
00000100-0004-1000-8000-001A11000101
sacomoto
2016/06/29 14:26:58
+1
jingxuy
2016/06/30 00:27:20
Done.
| |
45 | 35 |
46 // Deprecated signal send as the first byte in send byte operations. | 36 // TODO(jingxy): figure out the right UUID to use |
47 const int kFirstByteZero = 0; | 37 // The UUID of the RX characteristic used to receiver data from the server. |
Kyle Horimoto
2016/06/27 18:06:02
s/receiver/receive/
jingxuy
2016/06/30 00:27:20
Done.
| |
38 const char kRXCharacteristicUUID[] = "f4b904a2-a030-43b3-98a8-221c536c03cb"; | |
Kyle Horimoto
2016/06/27 18:06:02
00000100-0004-1000-8000-001A11000102
sacomoto
2016/06/29 14:26:57
+1
jingxuy
2016/06/30 00:27:20
Done.
| |
48 | 39 |
49 // The maximum number of bytes written in a remote characteristic with a single | |
50 // write request. This is not the connection MTU, we are assuming that the | |
51 // remote device allows for writes larger than MTU. | |
52 const int kMaxChunkSize = 500; | |
53 } // namespace | 40 } // namespace |
54 | 41 |
55 BluetoothLowEnergyConnection::BluetoothLowEnergyConnection( | 42 namespace proximity_auth { |
56 const RemoteDevice& device, | 43 |
57 scoped_refptr<device::BluetoothAdapter> adapter, | 44 BluetoothLowEnergyWeaveClientConnection:: |
58 const BluetoothUUID remote_service_uuid, | 45 BluetoothLowEnergyWeaveClientConnection( |
Kyle Horimoto
2016/06/27 18:06:02
Your constructor should take a generator and recei
jingxuy
2016/06/30 00:27:20
Done.
| |
59 BluetoothThrottler* bluetooth_throttler, | 46 const RemoteDevice& device, |
60 int max_number_of_write_attempts) | 47 scoped_refptr<device::BluetoothAdapter> adapter, |
48 const BluetoothUUID remote_service_uuid, | |
49 BluetoothThrottler* bluetooth_throttler, | |
50 int max_number_of_write_attempts) | |
61 : Connection(device), | 51 : Connection(device), |
62 adapter_(adapter), | 52 adapter_(adapter), |
63 remote_service_({remote_service_uuid, ""}), | 53 remote_service_({remote_service_uuid, ""}), |
64 to_peripheral_char_({BluetoothUUID(kToPeripheralCharUUID), ""}), | 54 packet_generator_( |
65 from_peripheral_char_({BluetoothUUID(kFromPeripheralCharUUID), ""}), | 55 BluetoothLowEnergyWeavePacketGenerator::Factory::NewInstance()), |
56 packet_receiver_( | |
57 BluetoothLowEnergyWeavePacketReceiver::Factory::NewInstance( | |
58 BluetoothLowEnergyWeavePacketReceiver::ReceiverType::CLIENT)), | |
59 tx_characteristic_({BluetoothUUID(kTXCharacteristicUUID), ""}), | |
60 rx_characteristic_({BluetoothUUID(kRXCharacteristicUUID), ""}), | |
66 bluetooth_throttler_(bluetooth_throttler), | 61 bluetooth_throttler_(bluetooth_throttler), |
67 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 62 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
68 sub_status_(SubStatus::DISCONNECTED), | 63 sub_status_(SubStatus::DISCONNECTED), |
69 receiving_bytes_(false), | |
70 write_remote_characteristic_pending_(false), | 64 write_remote_characteristic_pending_(false), |
71 max_number_of_write_attempts_(max_number_of_write_attempts), | 65 max_number_of_write_attempts_(max_number_of_write_attempts), |
72 max_chunk_size_(kMaxChunkSize), | |
73 weak_ptr_factory_(this) { | 66 weak_ptr_factory_(this) { |
74 DCHECK(adapter_); | 67 DCHECK(adapter_); |
75 DCHECK(adapter_->IsInitialized()); | 68 DCHECK(adapter_->IsInitialized()); |
76 | 69 |
77 adapter_->AddObserver(this); | 70 adapter_->AddObserver(this); |
78 } | 71 } |
79 | 72 |
80 BluetoothLowEnergyConnection::~BluetoothLowEnergyConnection() { | 73 BluetoothLowEnergyWeaveClientConnection:: |
74 ~BluetoothLowEnergyWeaveClientConnection() { | |
81 Disconnect(); | 75 Disconnect(); |
82 if (adapter_) { | 76 if (adapter_) { |
83 adapter_->RemoveObserver(this); | 77 adapter_->RemoveObserver(this); |
84 adapter_ = NULL; | 78 adapter_ = NULL; |
85 } | 79 } |
86 } | 80 } |
87 | 81 |
88 void BluetoothLowEnergyConnection::Connect() { | 82 void BluetoothLowEnergyWeaveClientConnection::Connect() { |
89 DCHECK(sub_status() == SubStatus::DISCONNECTED); | 83 DCHECK(sub_status() == SubStatus::DISCONNECTED); |
90 | 84 |
91 SetSubStatus(SubStatus::WAITING_GATT_CONNECTION); | 85 SetSubStatus(SubStatus::WAITING_GATT_CONNECTION); |
92 base::TimeDelta throttler_delay = bluetooth_throttler_->GetDelay(); | 86 base::TimeDelta throttler_delay = bluetooth_throttler_->GetDelay(); |
93 PA_LOG(INFO) << "Connecting in " << throttler_delay; | 87 PA_LOG(INFO) << "Connecting in " << throttler_delay; |
94 | 88 |
95 start_time_ = base::TimeTicks::Now(); | 89 start_time_ = base::TimeTicks::Now(); |
96 | 90 |
97 // If necessary, wait to create a new GATT connection. | 91 // If necessary, wait to create a new GATT connection. |
98 // | 92 // |
99 // Avoid creating a new GATT connection immediately after a given device was | 93 // Avoid creating a new GATT connection immediately after a given device was |
100 // disconnected. This is a workaround for crbug.com/508919. | 94 // disconnected. This is a workaround for crbug.com/508919. |
101 if (!throttler_delay.is_zero()) { | 95 if (!throttler_delay.is_zero()) { |
102 task_runner_->PostDelayedTask( | 96 task_runner_->PostDelayedTask( |
103 FROM_HERE, | 97 FROM_HERE, |
104 base::Bind(&BluetoothLowEnergyConnection::CreateGattConnection, | 98 base::Bind( |
105 weak_ptr_factory_.GetWeakPtr()), | 99 &BluetoothLowEnergyWeaveClientConnection::CreateGattConnection, |
100 weak_ptr_factory_.GetWeakPtr()), | |
106 throttler_delay); | 101 throttler_delay); |
107 return; | 102 return; |
108 } | 103 } |
109 | 104 |
110 CreateGattConnection(); | 105 CreateGattConnection(); |
111 } | 106 } |
112 | 107 |
113 void BluetoothLowEnergyConnection::CreateGattConnection() { | 108 void BluetoothLowEnergyWeaveClientConnection::CreateGattConnection() { |
114 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); | 109 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); |
115 | 110 |
116 BluetoothDevice* remote_device = GetRemoteDevice(); | 111 BluetoothDevice* remote_device = GetRemoteDevice(); |
117 if (remote_device) { | 112 if (remote_device) { |
118 PA_LOG(INFO) << "Creating GATT connection with " | 113 PA_LOG(INFO) << "Creating GATT connection with " |
119 << remote_device->GetAddress(); | 114 << remote_device->GetAddress(); |
120 | 115 |
121 remote_device->CreateGattConnection( | 116 remote_device->CreateGattConnection( |
122 base::Bind(&BluetoothLowEnergyConnection::OnGattConnectionCreated, | 117 base::Bind( |
123 weak_ptr_factory_.GetWeakPtr()), | 118 &BluetoothLowEnergyWeaveClientConnection::OnGattConnectionCreated, |
124 base::Bind(&BluetoothLowEnergyConnection::OnCreateGattConnectionError, | 119 weak_ptr_factory_.GetWeakPtr()), |
120 base::Bind(&BluetoothLowEnergyWeaveClientConnection:: | |
121 OnCreateGattConnectionError, | |
125 weak_ptr_factory_.GetWeakPtr())); | 122 weak_ptr_factory_.GetWeakPtr())); |
126 } | 123 } |
127 } | 124 } |
128 | 125 |
129 void BluetoothLowEnergyConnection::Disconnect() { | 126 void BluetoothLowEnergyWeaveClientConnection::Disconnect() { |
130 if (sub_status() != SubStatus::DISCONNECTED) { | 127 if (sub_status() != SubStatus::DISCONNECTED) { |
131 weak_ptr_factory_.InvalidateWeakPtrs(); | 128 weak_ptr_factory_.InvalidateWeakPtrs(); |
132 StopNotifySession(); | 129 StopNotifySession(); |
133 characteristic_finder_.reset(); | 130 characteristic_finder_.reset(); |
134 if (gatt_connection_) { | 131 if (gatt_connection_) { |
135 PA_LOG(INFO) << "Disconnect from device " | 132 PA_LOG(INFO) << "Disconnect from device " |
136 << gatt_connection_->GetDeviceAddress(); | 133 << gatt_connection_->GetDeviceAddress(); |
137 | 134 |
138 // Destroying BluetoothGattConnection also disconnects it. | 135 // Destroying BluetoothGattConnection also disconnects it. |
139 gatt_connection_.reset(); | 136 gatt_connection_.reset(); |
140 } | 137 } |
141 | 138 |
142 // Only transition to the DISCONNECTED state after perfoming all necessary | 139 // Only transition to the DISCONNECTED state after perfoming all necessary |
143 // operations. Otherwise, it'll trigger observers that can pontentially | 140 // operations. Otherwise, it'll trigger observers that can pontentially |
144 // destroy the current instance (causing a crash). | 141 // destroy the current instance (causing a crash). |
145 SetSubStatus(SubStatus::DISCONNECTED); | 142 SetSubStatus(SubStatus::DISCONNECTED); |
146 } | 143 } |
147 } | 144 } |
148 | 145 |
149 void BluetoothLowEnergyConnection::SetSubStatus(SubStatus new_sub_status) { | 146 void BluetoothLowEnergyWeaveClientConnection::SetSubStatus( |
147 SubStatus new_sub_status) { | |
150 sub_status_ = new_sub_status; | 148 sub_status_ = new_sub_status; |
151 | 149 |
152 // Sets the status of parent class proximity_auth::Connection accordingly. | 150 // Sets the status of parent class proximity_auth::Connection accordingly. |
153 if (new_sub_status == SubStatus::CONNECTED) { | 151 if (new_sub_status == SubStatus::CONNECTED) { |
154 SetStatus(CONNECTED); | 152 SetStatus(Status::CONNECTED); |
155 } else if (new_sub_status == SubStatus::DISCONNECTED) { | 153 } else if (new_sub_status == SubStatus::DISCONNECTED) { |
156 SetStatus(DISCONNECTED); | 154 SetStatus(Status::DISCONNECTED); |
157 } else { | 155 } else { |
158 SetStatus(IN_PROGRESS); | 156 SetStatus(Status::IN_PROGRESS); |
159 } | 157 } |
160 } | 158 } |
161 | 159 |
162 void BluetoothLowEnergyConnection::SetTaskRunnerForTesting( | 160 void BluetoothLowEnergyWeaveClientConnection::SetTaskRunnerForTesting( |
163 scoped_refptr<base::TaskRunner> task_runner) { | 161 scoped_refptr<base::TaskRunner> task_runner) { |
164 task_runner_ = task_runner; | 162 task_runner_ = task_runner; |
165 } | 163 } |
166 | 164 |
167 void BluetoothLowEnergyConnection::SendMessageImpl( | 165 void BluetoothLowEnergyWeaveClientConnection::SendMessageImpl( |
168 std::unique_ptr<WireMessage> message) { | 166 std::unique_ptr<WireMessage> message) { |
169 PA_LOG(INFO) << "Sending message " << message->Serialize(); | 167 PA_LOG(INFO) << "Sending message " << message->Serialize(); |
170 std::string serialized_msg = message->Serialize(); | 168 std::string serialized_msg = message->Serialize(); |
171 | 169 |
172 // [First write]: Build a header with the [send signal] + [size of the | 170 std::vector<std::vector<uint8_t>> packets = |
173 // message]. | 171 packet_generator_->EncodeDataMessage(serialized_msg); |
174 WriteRequest write_request = BuildWriteRequest( | |
175 ToByteVector(static_cast<uint32_t>(ControlSignal::kSendSignal)), | |
176 ToByteVector(static_cast<uint32_t>(serialized_msg.size())), false); | |
177 | 172 |
178 // [First write]: Fill the it with a prefix of |serialized_msg| up to | 173 for (uint32_t i = 0; i < packets.size(); ++i) { |
179 // |max_chunk_size_|. | 174 WriteRequest request = WriteRequest(packets[i], i == packets.size() - 1); |
180 size_t first_chunk_size = std::min( | 175 WriteRemoteCharacteristic(request); |
181 max_chunk_size_ - write_request.value.size(), serialized_msg.size()); | |
182 std::vector<uint8_t> bytes(serialized_msg.begin(), | |
183 serialized_msg.begin() + first_chunk_size); | |
184 write_request.value.insert(write_request.value.end(), bytes.begin(), | |
185 bytes.end()); | |
186 | |
187 bool is_last_write_request = first_chunk_size == serialized_msg.size(); | |
188 write_request.is_last_write_for_wire_message = is_last_write_request; | |
189 WriteRemoteCharacteristic(write_request); | |
190 if (is_last_write_request) | |
191 return; | |
192 | |
193 // [Other write requests]: Each chunk has to include a deprecated signal: | |
194 // |kFirstByteZero| as the first byte. | |
195 int chunk_size = max_chunk_size_ - 1; | |
196 std::vector<uint8_t> kFirstByteZeroVector; | |
197 kFirstByteZeroVector.push_back(static_cast<uint8_t>(kFirstByteZero)); | |
198 | |
199 int message_size = static_cast<int>(serialized_msg.size()); | |
200 int start_index = first_chunk_size; | |
201 while (start_index < message_size) { | |
202 int end_index = (start_index + chunk_size) <= message_size | |
203 ? (start_index + chunk_size) | |
204 : message_size; | |
205 bool is_last_write_request = (end_index == message_size); | |
206 write_request = BuildWriteRequest( | |
207 kFirstByteZeroVector, | |
208 std::vector<uint8_t>(serialized_msg.begin() + start_index, | |
209 serialized_msg.begin() + end_index), | |
210 is_last_write_request); | |
211 WriteRemoteCharacteristic(write_request); | |
212 start_index = end_index; | |
213 } | 176 } |
214 } | 177 } |
215 | 178 |
216 // Changes in the GATT connection with the remote device should be observed | 179 // Changes in the GATT connection with the remote device should be observed |
217 // here. If the GATT connection is dropped, we should call Disconnect() anyway, | 180 // here. If the GATT connection is dropped, we should call Disconnect() anyway, |
218 // so the object can notify its observers. | 181 // so the object can notify its observers. |
219 void BluetoothLowEnergyConnection::DeviceChanged(BluetoothAdapter* adapter, | 182 void BluetoothLowEnergyWeaveClientConnection::DeviceChanged( |
220 BluetoothDevice* device) { | 183 BluetoothAdapter* adapter, |
184 BluetoothDevice* device) { | |
221 DCHECK(device); | 185 DCHECK(device); |
222 if (sub_status() == SubStatus::DISCONNECTED || | 186 if (sub_status() == SubStatus::DISCONNECTED || |
223 device->GetAddress() != GetDeviceAddress()) | 187 device->GetAddress() != GetDeviceAddress()) |
224 return; | 188 return; |
225 | 189 |
226 if (sub_status() != SubStatus::WAITING_GATT_CONNECTION && | 190 if (sub_status() != SubStatus::WAITING_GATT_CONNECTION && |
227 !device->IsConnected()) { | 191 !device->IsConnected()) { |
228 PA_LOG(INFO) << "GATT connection dropped " << GetDeviceAddress() | 192 PA_LOG(INFO) << "GATT connection dropped " << GetDeviceAddress() |
229 << "\ndevice connected: " << device->IsConnected() | 193 << "\ndevice connected: " << device->IsConnected() |
230 << "\ngatt connection: " | 194 << "\ngatt connection: " |
231 << (gatt_connection_ ? gatt_connection_->IsConnected() | 195 << (gatt_connection_ ? gatt_connection_->IsConnected() |
232 : false); | 196 : false); |
233 Disconnect(); | 197 Disconnect(); |
234 } | 198 } |
235 } | 199 } |
236 | 200 |
237 void BluetoothLowEnergyConnection::DeviceRemoved(BluetoothAdapter* adapter, | 201 void BluetoothLowEnergyWeaveClientConnection::DeviceRemoved( |
238 BluetoothDevice* device) { | 202 BluetoothAdapter* adapter, |
203 BluetoothDevice* device) { | |
239 DCHECK(device); | 204 DCHECK(device); |
240 if (sub_status_ == SubStatus::DISCONNECTED || | 205 if (sub_status_ == SubStatus::DISCONNECTED || |
241 device->GetAddress() != GetDeviceAddress()) | 206 device->GetAddress() != GetDeviceAddress()) |
242 return; | 207 return; |
243 | 208 |
244 PA_LOG(INFO) << "Device removed " << GetDeviceAddress(); | 209 PA_LOG(INFO) << "Device removed " << GetDeviceAddress(); |
245 Disconnect(); | 210 Disconnect(); |
246 } | 211 } |
247 | 212 |
248 void BluetoothLowEnergyConnection::GattCharacteristicValueChanged( | 213 void BluetoothLowEnergyWeaveClientConnection::GattCharacteristicValueChanged( |
249 BluetoothAdapter* adapter, | 214 BluetoothAdapter* adapter, |
250 BluetoothRemoteGattCharacteristic* characteristic, | 215 BluetoothRemoteGattCharacteristic* characteristic, |
251 const std::vector<uint8_t>& value) { | 216 const std::vector<uint8_t>& value) { |
252 DCHECK_EQ(adapter, adapter_.get()); | 217 DCHECK_EQ(adapter, adapter_.get()); |
253 if (sub_status() != SubStatus::WAITING_RESPONSE_SIGNAL && | 218 if (sub_status() != SubStatus::WAITING_RESPONSE_SIGNAL && |
254 sub_status() != SubStatus::CONNECTED) | 219 sub_status() != SubStatus::CONNECTED) |
255 return; | 220 return; |
256 | 221 |
257 PA_LOG(INFO) << "Characteristic value changed: " | 222 PA_LOG(INFO) << "Characteristic value changed: " |
258 << characteristic->GetUUID().canonical_value(); | 223 << characteristic->GetUUID().canonical_value(); |
259 | 224 |
260 if (characteristic->GetIdentifier() == from_peripheral_char_.id) { | 225 if (characteristic->GetIdentifier() == rx_characteristic_.id) { |
261 if (receiving_bytes_) { | 226 ReceiverState state = packet_receiver_->ReceivePacket(value); |
262 // Ignoring the first byte, as it contains a deprecated signal. | 227 PA_LOG(INFO) << "\nReceiver State: " << state; |
263 const std::string bytes(value.begin() + 1, value.end()); | 228 switch (state) { |
264 incoming_bytes_buffer_.append(bytes); | 229 case ReceiverState::DATA_READY: |
265 if (incoming_bytes_buffer_.size() >= expected_number_of_incoming_bytes_) { | 230 OnBytesReceived(packet_receiver_->GetDataMessage()); |
266 OnBytesReceived(incoming_bytes_buffer_); | 231 break; |
267 receiving_bytes_ = false; | 232 case ReceiverState::CONNECTION_CLOSED: |
268 } | 233 // TODO(jingxuy): what to do with the reason for close? |
sacomoto
2016/06/29 14:26:57
Just log the error for now.
This class doesn't p
jingxuy
2016/06/30 00:27:20
Done.
Kyle Horimoto
2016/06/30 00:42:33
Add a function which converts a ReasonForClose to
jingxuy
2016/06/30 01:14:55
Done.
| |
269 return; | 234 Disconnect(); |
270 } | 235 break; |
271 | 236 case ReceiverState::ERROR: |
272 if (value.size() < 4) { | 237 // TODO(jingxuy): send a control close over the channel |
Kyle Horimoto
2016/06/27 18:06:02
Can you do this in this CL please?
sacomoto
2016/06/29 14:26:58
+1
You'll need to change the logic here. If try t
jingxuy
2016/06/30 00:27:20
Done.
| |
273 PA_LOG(WARNING) << "Incoming data corrupted, no signal found."; | 238 Disconnect(); |
274 return; | 239 break; |
275 } | 240 case ReceiverState::WAITING: |
276 | 241 packet_generator_->SetMaxPacketSize( |
Kyle Horimoto
2016/06/27 18:06:02
Please add a comment about what the WAITING state
jingxuy
2016/06/30 00:27:20
Done.
| |
277 const ControlSignal signal = static_cast<ControlSignal>(ToUint32(value)); | 242 packet_receiver_->GetMaxPacketSize()); |
278 switch (signal) { | |
279 case ControlSignal::kInvitationResponseSignal: | |
280 if (sub_status() == SubStatus::WAITING_RESPONSE_SIGNAL) | 243 if (sub_status() == SubStatus::WAITING_RESPONSE_SIGNAL) |
281 CompleteConnection(); | 244 CompleteConnection(); |
282 break; | 245 break; |
283 case ControlSignal::kInviteToConnectSignal: | 246 case ReceiverState::CONNECTING: |
Kyle Horimoto
2016/06/27 18:06:02
Is CONNECTING a valid state? If we have already re
jingxuy
2016/06/30 00:27:20
Done.
| |
247 case ReceiverState::RECEIVING_DATA: | |
248 // Normal in between states, so do nothing. | |
284 break; | 249 break; |
285 case ControlSignal::kSendSignal: { | 250 default: |
286 if (value.size() < 8) { | 251 NOTREACHED(); |
287 PA_LOG(WARNING) | |
288 << "Incoming data corrupted, expected message size not found."; | |
289 return; | |
290 } | |
291 std::vector<uint8_t> size(value.begin() + 4, value.begin() + 8); | |
292 expected_number_of_incoming_bytes_ = | |
293 static_cast<size_t>(ToUint32(size)); | |
294 receiving_bytes_ = true; | |
295 incoming_bytes_buffer_.clear(); | |
296 | |
297 const std::string bytes(value.begin() + 8, value.end()); | |
298 incoming_bytes_buffer_.append(bytes); | |
299 if (incoming_bytes_buffer_.size() >= | |
300 expected_number_of_incoming_bytes_) { | |
301 OnBytesReceived(incoming_bytes_buffer_); | |
302 receiving_bytes_ = false; | |
303 } | |
304 break; | |
305 } | |
306 case ControlSignal::kDisconnectSignal: | |
307 PA_LOG(INFO) << "Disconnect signal received."; | |
308 Disconnect(); | |
309 break; | |
310 } | 252 } |
311 } | 253 } |
312 } | 254 } |
313 | 255 |
314 BluetoothLowEnergyConnection::WriteRequest::WriteRequest( | 256 BluetoothLowEnergyWeaveClientConnection::WriteRequest::WriteRequest( |
315 const std::vector<uint8_t>& val, | 257 const std::vector<uint8_t>& val, |
316 bool flag) | 258 bool flag) |
317 : value(val), | 259 : value(val), |
318 is_last_write_for_wire_message(flag), | 260 is_last_write_for_wire_message(flag), |
319 number_of_failed_attempts(0) {} | 261 number_of_failed_attempts(0) {} |
320 | 262 |
321 BluetoothLowEnergyConnection::WriteRequest::WriteRequest( | 263 BluetoothLowEnergyWeaveClientConnection::WriteRequest::WriteRequest( |
322 const WriteRequest& other) = default; | 264 const WriteRequest& other) = default; |
323 | 265 |
324 BluetoothLowEnergyConnection::WriteRequest::~WriteRequest() {} | 266 BluetoothLowEnergyWeaveClientConnection::WriteRequest::~WriteRequest() {} |
325 | 267 |
326 void BluetoothLowEnergyConnection::CompleteConnection() { | 268 void BluetoothLowEnergyWeaveClientConnection::CompleteConnection() { |
327 PA_LOG(INFO) << "Connection completed. Time elapsed: " | 269 PA_LOG(INFO) << "Connection completed. Time elapsed: " |
328 << base::TimeTicks::Now() - start_time_; | 270 << base::TimeTicks::Now() - start_time_; |
329 SetSubStatus(SubStatus::CONNECTED); | 271 SetSubStatus(SubStatus::CONNECTED); |
330 } | 272 } |
331 | 273 |
332 void BluetoothLowEnergyConnection::OnCreateGattConnectionError( | 274 void BluetoothLowEnergyWeaveClientConnection::OnCreateGattConnectionError( |
333 device::BluetoothDevice::ConnectErrorCode error_code) { | 275 device::BluetoothDevice::ConnectErrorCode error_code) { |
334 DCHECK(sub_status_ == SubStatus::WAITING_GATT_CONNECTION); | 276 DCHECK(sub_status_ == SubStatus::WAITING_GATT_CONNECTION); |
335 PA_LOG(WARNING) << "Error creating GATT connection to " | 277 PA_LOG(WARNING) << "Error creating GATT connection to " |
336 << remote_device().bluetooth_address | 278 << remote_device().bluetooth_address |
337 << "error code: " << error_code; | 279 << "error code: " << error_code; |
338 Disconnect(); | 280 Disconnect(); |
339 } | 281 } |
340 | 282 |
341 void BluetoothLowEnergyConnection::OnGattConnectionCreated( | 283 void BluetoothLowEnergyWeaveClientConnection::OnGattConnectionCreated( |
342 std::unique_ptr<device::BluetoothGattConnection> gatt_connection) { | 284 std::unique_ptr<device::BluetoothGattConnection> gatt_connection) { |
343 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); | 285 DCHECK(sub_status() == SubStatus::WAITING_GATT_CONNECTION); |
344 PA_LOG(INFO) << "GATT connection with " << gatt_connection->GetDeviceAddress() | 286 PA_LOG(INFO) << "GATT connection with " << gatt_connection->GetDeviceAddress() |
345 << " created."; | 287 << " created."; |
346 PrintTimeElapsed(); | 288 PrintTimeElapsed(); |
347 | 289 |
348 // Informing |bluetooth_trottler_| a new connection was established. | 290 // Informing |bluetooth_trottler_| a new connection was established. |
349 bluetooth_throttler_->OnConnection(this); | 291 bluetooth_throttler_->OnConnection(this); |
350 | 292 |
351 gatt_connection_ = std::move(gatt_connection); | 293 gatt_connection_ = std::move(gatt_connection); |
352 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); | 294 SetSubStatus(SubStatus::WAITING_CHARACTERISTICS); |
353 characteristic_finder_.reset(CreateCharacteristicsFinder( | 295 characteristic_finder_.reset(CreateCharacteristicsFinder( |
354 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFound, | 296 base::Bind( |
355 weak_ptr_factory_.GetWeakPtr()), | 297 &BluetoothLowEnergyWeaveClientConnection::OnCharacteristicsFound, |
356 base::Bind(&BluetoothLowEnergyConnection::OnCharacteristicsFinderError, | 298 weak_ptr_factory_.GetWeakPtr()), |
299 base::Bind(&BluetoothLowEnergyWeaveClientConnection:: | |
300 OnCharacteristicsFinderError, | |
357 weak_ptr_factory_.GetWeakPtr()))); | 301 weak_ptr_factory_.GetWeakPtr()))); |
358 } | 302 } |
359 | 303 |
360 BluetoothLowEnergyCharacteristicsFinder* | 304 BluetoothLowEnergyCharacteristicsFinder* |
361 BluetoothLowEnergyConnection::CreateCharacteristicsFinder( | 305 BluetoothLowEnergyWeaveClientConnection::CreateCharacteristicsFinder( |
362 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& | 306 const BluetoothLowEnergyCharacteristicsFinder::SuccessCallback& |
363 success_callback, | 307 success_callback, |
364 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& | 308 const BluetoothLowEnergyCharacteristicsFinder::ErrorCallback& |
365 error_callback) { | 309 error_callback) { |
366 return new BluetoothLowEnergyCharacteristicsFinder( | 310 return new BluetoothLowEnergyCharacteristicsFinder( |
367 adapter_, GetRemoteDevice(), remote_service_, to_peripheral_char_, | 311 adapter_, GetRemoteDevice(), remote_service_, tx_characteristic_, |
368 from_peripheral_char_, success_callback, error_callback); | 312 rx_characteristic_, success_callback, error_callback); |
369 } | 313 } |
370 | 314 |
371 void BluetoothLowEnergyConnection::OnCharacteristicsFound( | 315 void BluetoothLowEnergyWeaveClientConnection::OnCharacteristicsFound( |
372 const RemoteAttribute& service, | 316 const RemoteAttribute& service, |
373 const RemoteAttribute& to_peripheral_char, | 317 const RemoteAttribute& tx_characteristic, |
374 const RemoteAttribute& from_peripheral_char) { | 318 const RemoteAttribute& rx_characteristic) { |
375 PA_LOG(INFO) << "Remote chacteristics found."; | 319 PA_LOG(INFO) << "Remote chacteristics found."; |
376 PrintTimeElapsed(); | 320 PrintTimeElapsed(); |
377 | 321 |
378 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); | 322 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); |
379 remote_service_ = service; | 323 remote_service_ = service; |
380 to_peripheral_char_ = to_peripheral_char; | 324 tx_characteristic_ = tx_characteristic; |
381 from_peripheral_char_ = from_peripheral_char; | 325 rx_characteristic_ = rx_characteristic; |
382 | 326 |
383 SetSubStatus(SubStatus::CHARACTERISTICS_FOUND); | 327 SetSubStatus(SubStatus::CHARACTERISTICS_FOUND); |
384 StartNotifySession(); | 328 StartNotifySession(); |
385 } | 329 } |
386 | 330 |
387 void BluetoothLowEnergyConnection::OnCharacteristicsFinderError( | 331 void BluetoothLowEnergyWeaveClientConnection::OnCharacteristicsFinderError( |
388 const RemoteAttribute& to_peripheral_char, | 332 const RemoteAttribute& tx_characteristic, |
389 const RemoteAttribute& from_peripheral_char) { | 333 const RemoteAttribute& rx_characteristic) { |
390 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); | 334 DCHECK(sub_status() == SubStatus::WAITING_CHARACTERISTICS); |
391 PA_LOG(WARNING) << "Connection error, missing characteristics for SmartLock " | 335 PA_LOG(WARNING) << "Connection error, missing characteristics for SmartLock " |
392 "service.\n" | 336 "service.\n" |
393 << (to_peripheral_char.id.empty() | 337 << (tx_characteristic.id.empty() |
394 ? to_peripheral_char.uuid.canonical_value() | 338 ? tx_characteristic.uuid.canonical_value() |
395 : "") | 339 : "") |
396 << (from_peripheral_char.id.empty() | 340 << (rx_characteristic.id.empty() |
397 ? ", " + from_peripheral_char.uuid.canonical_value() | 341 ? ", " + rx_characteristic.uuid.canonical_value() |
398 : "") << " not found."; | 342 : "") |
343 << " not found."; | |
399 | 344 |
400 Disconnect(); | 345 Disconnect(); |
401 } | 346 } |
402 | 347 |
403 void BluetoothLowEnergyConnection::StartNotifySession() { | 348 void BluetoothLowEnergyWeaveClientConnection::StartNotifySession() { |
404 if (sub_status() == SubStatus::CHARACTERISTICS_FOUND) { | 349 if (sub_status() == SubStatus::CHARACTERISTICS_FOUND) { |
405 BluetoothRemoteGattCharacteristic* characteristic = | 350 BluetoothRemoteGattCharacteristic* characteristic = |
406 GetGattCharacteristic(from_peripheral_char_.id); | 351 GetGattCharacteristic(rx_characteristic_.id); |
407 DCHECK(characteristic); | 352 DCHECK(characteristic); |
408 | 353 |
409 // This is a workaround for crbug.com/507325. If |characteristic| is already | 354 // This is a workaround for crbug.com/507325. If |characteristic| is already |
410 // notifying |characteristic->StartNotifySession()| will fail with | 355 // notifying |characteristic->StartNotifySession()| will fail with |
411 // GATT_ERROR_FAILED. | 356 // GATT_ERROR_FAILED. |
412 if (characteristic->IsNotifying()) { | 357 if (characteristic->IsNotifying()) { |
413 PA_LOG(INFO) << characteristic->GetUUID().canonical_value() | 358 PA_LOG(INFO) << characteristic->GetUUID().canonical_value() |
414 << " already notifying."; | 359 << " already notifying."; |
415 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); | 360 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); |
416 SendInviteToConnectSignal(); | 361 SendInviteToConnectSignal(); |
417 return; | 362 return; |
418 } | 363 } |
419 | 364 |
420 SetSubStatus(SubStatus::WAITING_NOTIFY_SESSION); | 365 SetSubStatus(SubStatus::WAITING_NOTIFY_SESSION); |
421 characteristic->StartNotifySession( | 366 characteristic->StartNotifySession( |
422 base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionStarted, | 367 base::Bind( |
423 weak_ptr_factory_.GetWeakPtr()), | 368 &BluetoothLowEnergyWeaveClientConnection::OnNotifySessionStarted, |
424 base::Bind(&BluetoothLowEnergyConnection::OnNotifySessionError, | 369 weak_ptr_factory_.GetWeakPtr()), |
425 weak_ptr_factory_.GetWeakPtr())); | 370 base::Bind( |
371 &BluetoothLowEnergyWeaveClientConnection::OnNotifySessionError, | |
372 weak_ptr_factory_.GetWeakPtr())); | |
426 } | 373 } |
427 } | 374 } |
428 | 375 |
429 void BluetoothLowEnergyConnection::OnNotifySessionError( | 376 void BluetoothLowEnergyWeaveClientConnection::OnNotifySessionStarted( |
430 BluetoothRemoteGattService::GattErrorCode error) { | |
431 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); | |
432 PA_LOG(WARNING) << "Error starting notification session: " << error; | |
433 Disconnect(); | |
434 } | |
435 | |
436 void BluetoothLowEnergyConnection::OnNotifySessionStarted( | |
437 std::unique_ptr<BluetoothGattNotifySession> notify_session) { | 377 std::unique_ptr<BluetoothGattNotifySession> notify_session) { |
438 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); | 378 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); |
439 PA_LOG(INFO) << "Notification session started " | 379 PA_LOG(INFO) << "Notification session started " |
440 << notify_session->GetCharacteristicIdentifier(); | 380 << notify_session->GetCharacteristicIdentifier(); |
441 PrintTimeElapsed(); | 381 PrintTimeElapsed(); |
442 | 382 |
443 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); | 383 SetSubStatus(SubStatus::NOTIFY_SESSION_READY); |
444 notify_session_ = std::move(notify_session); | 384 notify_session_ = std::move(notify_session); |
445 | 385 |
446 SendInviteToConnectSignal(); | 386 SendInviteToConnectSignal(); |
447 } | 387 } |
448 | 388 |
449 void BluetoothLowEnergyConnection::StopNotifySession() { | 389 void BluetoothLowEnergyWeaveClientConnection::OnNotifySessionError( |
390 BluetoothRemoteGattService::GattErrorCode error) { | |
391 DCHECK(sub_status() == SubStatus::WAITING_NOTIFY_SESSION); | |
392 PA_LOG(WARNING) << "Error starting notification session: " << error; | |
393 Disconnect(); | |
394 } | |
395 | |
396 void BluetoothLowEnergyWeaveClientConnection::StopNotifySession() { | |
450 if (notify_session_) { | 397 if (notify_session_) { |
451 notify_session_->Stop(base::Bind(&base::DoNothing)); | 398 notify_session_->Stop(base::Bind(&base::DoNothing)); |
452 notify_session_.reset(); | 399 notify_session_.reset(); |
453 } | 400 } |
454 } | 401 } |
455 | 402 |
456 void BluetoothLowEnergyConnection::SendInviteToConnectSignal() { | 403 void BluetoothLowEnergyWeaveClientConnection::SendInviteToConnectSignal() { |
457 if (sub_status() == SubStatus::NOTIFY_SESSION_READY) { | 404 if (sub_status() == SubStatus::NOTIFY_SESSION_READY) { |
458 PA_LOG(INFO) << "Sending invite to connect signal"; | 405 PA_LOG(INFO) << "Sending invite to connect signal"; |
459 SetSubStatus(SubStatus::WAITING_RESPONSE_SIGNAL); | 406 SetSubStatus(SubStatus::WAITING_RESPONSE_SIGNAL); |
460 | 407 |
461 WriteRequest write_request = BuildWriteRequest( | 408 WriteRequest write_request = |
462 ToByteVector( | 409 WriteRequest(packet_generator_->CreateConnectionRequest(), false); |
463 static_cast<uint32_t>(ControlSignal::kInviteToConnectSignal)), | |
464 std::vector<uint8_t>(), false); | |
465 | 410 |
466 WriteRemoteCharacteristic(write_request); | 411 WriteRemoteCharacteristic(write_request); |
467 } | 412 } |
468 } | 413 } |
469 | 414 |
470 void BluetoothLowEnergyConnection::WriteRemoteCharacteristic( | 415 void BluetoothLowEnergyWeaveClientConnection::WriteRemoteCharacteristic( |
471 WriteRequest request) { | 416 const WriteRequest& request) { |
472 write_requests_queue_.push(request); | 417 write_requests_queue_.push(request); |
473 ProcessNextWriteRequest(); | 418 ProcessNextWriteRequest(); |
474 } | 419 } |
475 | 420 |
476 void BluetoothLowEnergyConnection::ProcessNextWriteRequest() { | 421 void BluetoothLowEnergyWeaveClientConnection::ProcessNextWriteRequest() { |
477 BluetoothRemoteGattCharacteristic* characteristic = | 422 BluetoothRemoteGattCharacteristic* characteristic = |
478 GetGattCharacteristic(to_peripheral_char_.id); | 423 GetGattCharacteristic(tx_characteristic_.id); |
479 if (!write_requests_queue_.empty() && !write_remote_characteristic_pending_ && | 424 if (!write_requests_queue_.empty() && !write_remote_characteristic_pending_ && |
480 characteristic) { | 425 characteristic) { |
481 write_remote_characteristic_pending_ = true; | 426 write_remote_characteristic_pending_ = true; |
482 WriteRequest next_request = write_requests_queue_.front(); | 427 WriteRequest next_request = write_requests_queue_.front(); |
483 PA_LOG(INFO) << "Writing characteristic..."; | 428 PA_LOG(INFO) << "Writing characteristic..."; |
484 characteristic->WriteRemoteCharacteristic( | 429 characteristic->WriteRemoteCharacteristic( |
485 next_request.value, | 430 next_request.value, |
486 base::Bind(&BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten, | 431 base::Bind(&BluetoothLowEnergyWeaveClientConnection:: |
432 OnRemoteCharacteristicWritten, | |
487 weak_ptr_factory_.GetWeakPtr(), | 433 weak_ptr_factory_.GetWeakPtr(), |
488 next_request.is_last_write_for_wire_message), | 434 next_request.is_last_write_for_wire_message), |
489 base::Bind( | 435 base::Bind(&BluetoothLowEnergyWeaveClientConnection:: |
490 &BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError, | 436 OnWriteRemoteCharacteristicError, |
491 weak_ptr_factory_.GetWeakPtr(), | 437 weak_ptr_factory_.GetWeakPtr(), |
492 next_request.is_last_write_for_wire_message)); | 438 next_request.is_last_write_for_wire_message)); |
493 } | 439 } |
494 } | 440 } |
495 | 441 |
496 void BluetoothLowEnergyConnection::OnRemoteCharacteristicWritten( | 442 void BluetoothLowEnergyWeaveClientConnection::OnRemoteCharacteristicWritten( |
497 bool run_did_send_message_callback) { | 443 bool run_did_send_message_callback) { |
498 PA_LOG(INFO) << "Characteristic written."; | 444 PA_LOG(INFO) << "Characteristic written."; |
499 write_remote_characteristic_pending_ = false; | 445 write_remote_characteristic_pending_ = false; |
500 // TODO(sacomoto): Actually pass the current message to the observer. | 446 // TODO(sacomoto): Actually pass the current message to the observer. |
501 if (run_did_send_message_callback) | 447 if (run_did_send_message_callback) |
502 OnDidSendMessage(WireMessage(std::string(), std::string()), true); | 448 OnDidSendMessage(WireMessage(std::string(), std::string()), true); |
503 | 449 |
504 // Removes the top of queue (already processed) and process the next request. | 450 // Removes the top of queue (already processed) and process the next request. |
505 DCHECK(!write_requests_queue_.empty()); | 451 DCHECK(!write_requests_queue_.empty()); |
506 write_requests_queue_.pop(); | 452 write_requests_queue_.pop(); |
507 ProcessNextWriteRequest(); | 453 ProcessNextWriteRequest(); |
508 } | 454 } |
509 | 455 |
510 void BluetoothLowEnergyConnection::OnWriteRemoteCharacteristicError( | 456 void BluetoothLowEnergyWeaveClientConnection::OnWriteRemoteCharacteristicError( |
511 bool run_did_send_message_callback, | 457 bool run_did_send_message_callback, |
512 BluetoothRemoteGattService::GattErrorCode error) { | 458 BluetoothRemoteGattService::GattErrorCode error) { |
513 PA_LOG(WARNING) << "Error " << error << " writing characteristic: " | 459 PA_LOG(WARNING) << "Error " << error << " writing characteristic: " |
514 << to_peripheral_char_.uuid.canonical_value(); | 460 << tx_characteristic_.uuid.canonical_value(); |
515 write_remote_characteristic_pending_ = false; | 461 write_remote_characteristic_pending_ = false; |
516 // TODO(sacomoto): Actually pass the current message to the observer. | 462 // TODO(sacomoto): Actually pass the current message to the observer. |
517 if (run_did_send_message_callback) | 463 if (run_did_send_message_callback) |
518 OnDidSendMessage(WireMessage(std::string(), std::string()), false); | 464 OnDidSendMessage(WireMessage(std::string(), std::string()), false); |
519 | 465 |
520 // Increases the number of failed attempts and retry. | 466 // Increases the number of failed attempts and retry. |
521 DCHECK(!write_requests_queue_.empty()); | 467 DCHECK(!write_requests_queue_.empty()); |
522 if (++write_requests_queue_.front().number_of_failed_attempts >= | 468 if (++write_requests_queue_.front().number_of_failed_attempts >= |
523 max_number_of_write_attempts_) { | 469 max_number_of_write_attempts_) { |
524 Disconnect(); | 470 Disconnect(); |
525 return; | 471 return; |
526 } | 472 } |
527 ProcessNextWriteRequest(); | 473 ProcessNextWriteRequest(); |
528 } | 474 } |
529 | 475 |
530 BluetoothLowEnergyConnection::WriteRequest | 476 void BluetoothLowEnergyWeaveClientConnection::PrintTimeElapsed() { |
531 BluetoothLowEnergyConnection::BuildWriteRequest( | |
532 const std::vector<uint8_t>& signal, | |
533 const std::vector<uint8_t>& bytes, | |
534 bool is_last_write_for_wire_message) { | |
535 std::vector<uint8_t> value(signal.begin(), signal.end()); | |
536 value.insert(value.end(), bytes.begin(), bytes.end()); | |
537 return WriteRequest(value, is_last_write_for_wire_message); | |
538 } | |
539 | |
540 void BluetoothLowEnergyConnection::PrintTimeElapsed() { | |
541 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; | 477 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; |
542 } | 478 } |
543 | 479 |
544 std::string BluetoothLowEnergyConnection::GetDeviceAddress() { | 480 std::string BluetoothLowEnergyWeaveClientConnection::GetDeviceAddress() { |
545 // When the remote device is connected we should rely on the address given by | 481 // When the remote device is connected we should rely on the address given by |
546 // |gatt_connection_|. As the device address may change if the device is | 482 // |gatt_connection_|. As the device address may change if the device is |
547 // paired. The address in |gatt_connection_| is automatically updated in this | 483 // paired. The address in |gatt_connection_| is automatically updated in this |
548 // case. | 484 // case. |
549 return gatt_connection_ ? gatt_connection_->GetDeviceAddress() | 485 return gatt_connection_ ? gatt_connection_->GetDeviceAddress() |
550 : remote_device().bluetooth_address; | 486 : remote_device().bluetooth_address; |
551 } | 487 } |
552 | 488 |
553 BluetoothDevice* BluetoothLowEnergyConnection::GetRemoteDevice() { | 489 BluetoothDevice* BluetoothLowEnergyWeaveClientConnection::GetRemoteDevice() { |
554 // It's not possible to simply use | 490 // It's not possible to simply use |
555 // |adapter_->GetDevice(GetDeviceAddress())| to find the device with MAC | 491 // |adapter_->GetDevice(GetDeviceAddress())| to find the device with MAC |
556 // address |GetDeviceAddress()|. For paired devices, | 492 // address |GetDeviceAddress()|. For paired devices, |
557 // BluetoothAdapter::GetDevice(XXX) searches for the temporary MAC address | 493 // BluetoothAdapter::GetDevice(XXX) searches for the temporary MAC address |
558 // XXX, whereas |GetDeviceAddress()| is the real MAC address. This is a | 494 // XXX, whereas |GetDeviceAddress()| is the real MAC address. This is a |
559 // bug in the way device::BluetoothAdapter is storing the devices (see | 495 // bug in the way device::BluetoothAdapter is storing the devices (see |
560 // crbug.com/497841). | 496 // crbug.com/497841). |
561 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); | 497 std::vector<BluetoothDevice*> devices = adapter_->GetDevices(); |
562 for (const auto& device : devices) { | 498 for (const auto& device : devices) { |
563 if (device->GetAddress() == GetDeviceAddress()) | 499 if (device->GetAddress() == GetDeviceAddress()) |
564 return device; | 500 return device; |
565 } | 501 } |
566 | 502 |
567 return nullptr; | 503 return nullptr; |
568 } | 504 } |
569 | 505 |
570 BluetoothRemoteGattService* BluetoothLowEnergyConnection::GetRemoteService() { | 506 BluetoothRemoteGattService* |
507 BluetoothLowEnergyWeaveClientConnection::GetRemoteService() { | |
571 BluetoothDevice* remote_device = GetRemoteDevice(); | 508 BluetoothDevice* remote_device = GetRemoteDevice(); |
572 if (!remote_device) { | 509 if (!remote_device) { |
573 PA_LOG(WARNING) << "Remote device not found."; | 510 PA_LOG(WARNING) << "Remote device not found."; |
574 return NULL; | 511 return NULL; |
575 } | 512 } |
576 if (remote_service_.id.empty()) { | 513 if (remote_service_.id.empty()) { |
577 std::vector<BluetoothRemoteGattService*> services = | 514 std::vector<BluetoothRemoteGattService*> services = |
578 remote_device->GetGattServices(); | 515 remote_device->GetGattServices(); |
579 for (const auto& service : services) | 516 for (const auto& service : services) |
580 if (service->GetUUID() == remote_service_.uuid) { | 517 if (service->GetUUID() == remote_service_.uuid) { |
581 remote_service_.id = service->GetIdentifier(); | 518 remote_service_.id = service->GetIdentifier(); |
582 break; | 519 break; |
583 } | 520 } |
584 } | 521 } |
585 return remote_device->GetGattService(remote_service_.id); | 522 return remote_device->GetGattService(remote_service_.id); |
586 } | 523 } |
587 | 524 |
588 BluetoothRemoteGattCharacteristic* | 525 BluetoothRemoteGattCharacteristic* |
589 BluetoothLowEnergyConnection::GetGattCharacteristic( | 526 BluetoothLowEnergyWeaveClientConnection::GetGattCharacteristic( |
590 const std::string& gatt_characteristic) { | 527 const std::string& gatt_characteristic) { |
591 BluetoothRemoteGattService* remote_service = GetRemoteService(); | 528 BluetoothRemoteGattService* remote_service = GetRemoteService(); |
592 if (!remote_service) { | 529 if (!remote_service) { |
593 PA_LOG(WARNING) << "Remote service not found."; | 530 PA_LOG(WARNING) << "Remote service not found."; |
594 return NULL; | 531 return NULL; |
595 } | 532 } |
596 return remote_service->GetCharacteristic(gatt_characteristic); | 533 return remote_service->GetCharacteristic(gatt_characteristic); |
597 } | 534 } |
598 | 535 |
599 // TODO(sacomoto): make this robust to byte ordering in both sides of the | |
600 // SmartLock BLE socket. | |
601 uint32_t BluetoothLowEnergyConnection::ToUint32( | |
602 const std::vector<uint8_t>& bytes) { | |
603 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); | |
604 } | |
605 | |
606 // TODO(sacomoto): make this robust to byte ordering in both sides of the | |
607 // SmartLock BLE socket. | |
608 const std::vector<uint8_t> BluetoothLowEnergyConnection::ToByteVector( | |
609 const uint32_t value) { | |
610 std::vector<uint8_t> bytes(4, 0); | |
611 bytes[0] = static_cast<uint8_t>(value); | |
612 bytes[1] = static_cast<uint8_t>(value >> 8); | |
613 bytes[2] = static_cast<uint8_t>(value >> 16); | |
614 bytes[3] = static_cast<uint8_t>(value >> 24); | |
615 return bytes; | |
616 } | |
617 | |
618 } // namespace proximity_auth | 536 } // namespace proximity_auth |
OLD | NEW |