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

Side by Side Diff: components/proximity_auth/ble/bluetooth_low_energy_connection.cc

Issue 1551433002: Switch to standard integer types in components/, part 3 of 4. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more Created 4 years, 12 months 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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_connection.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 } 163 }
164 164
165 void BluetoothLowEnergyConnection::SendMessageImpl( 165 void BluetoothLowEnergyConnection::SendMessageImpl(
166 scoped_ptr<WireMessage> message) { 166 scoped_ptr<WireMessage> message) {
167 PA_LOG(INFO) << "Sending message " << message->Serialize(); 167 PA_LOG(INFO) << "Sending message " << message->Serialize();
168 std::string serialized_msg = message->Serialize(); 168 std::string serialized_msg = message->Serialize();
169 169
170 // [First write]: Build a header with the [send signal] + [size of the 170 // [First write]: Build a header with the [send signal] + [size of the
171 // message]. 171 // message].
172 WriteRequest write_request = BuildWriteRequest( 172 WriteRequest write_request = BuildWriteRequest(
173 ToByteVector(static_cast<uint32>(ControlSignal::kSendSignal)), 173 ToByteVector(static_cast<uint32_t>(ControlSignal::kSendSignal)),
174 ToByteVector(static_cast<uint32>(serialized_msg.size())), false); 174 ToByteVector(static_cast<uint32_t>(serialized_msg.size())), false);
175 175
176 // [First write]: Fill the it with a prefix of |serialized_msg| up to 176 // [First write]: Fill the it with a prefix of |serialized_msg| up to
177 // |max_chunk_size_|. 177 // |max_chunk_size_|.
178 size_t first_chunk_size = std::min( 178 size_t first_chunk_size = std::min(
179 max_chunk_size_ - write_request.value.size(), serialized_msg.size()); 179 max_chunk_size_ - write_request.value.size(), serialized_msg.size());
180 std::vector<uint8> bytes(serialized_msg.begin(), 180 std::vector<uint8_t> bytes(serialized_msg.begin(),
181 serialized_msg.begin() + first_chunk_size); 181 serialized_msg.begin() + first_chunk_size);
182 write_request.value.insert(write_request.value.end(), bytes.begin(), 182 write_request.value.insert(write_request.value.end(), bytes.begin(),
183 bytes.end()); 183 bytes.end());
184 184
185 bool is_last_write_request = first_chunk_size == serialized_msg.size(); 185 bool is_last_write_request = first_chunk_size == serialized_msg.size();
186 write_request.is_last_write_for_wire_message = is_last_write_request; 186 write_request.is_last_write_for_wire_message = is_last_write_request;
187 WriteRemoteCharacteristic(write_request); 187 WriteRemoteCharacteristic(write_request);
188 if (is_last_write_request) 188 if (is_last_write_request)
189 return; 189 return;
190 190
191 // [Other write requests]: Each chunk has to include a deprecated signal: 191 // [Other write requests]: Each chunk has to include a deprecated signal:
192 // |kFirstByteZero| as the first byte. 192 // |kFirstByteZero| as the first byte.
193 int chunk_size = max_chunk_size_ - 1; 193 int chunk_size = max_chunk_size_ - 1;
194 std::vector<uint8> kFirstByteZeroVector; 194 std::vector<uint8_t> kFirstByteZeroVector;
195 kFirstByteZeroVector.push_back(static_cast<uint8>(kFirstByteZero)); 195 kFirstByteZeroVector.push_back(static_cast<uint8_t>(kFirstByteZero));
196 196
197 int message_size = static_cast<int>(serialized_msg.size()); 197 int message_size = static_cast<int>(serialized_msg.size());
198 int start_index = first_chunk_size; 198 int start_index = first_chunk_size;
199 while (start_index < message_size) { 199 while (start_index < message_size) {
200 int end_index = (start_index + chunk_size) <= message_size 200 int end_index = (start_index + chunk_size) <= message_size
201 ? (start_index + chunk_size) 201 ? (start_index + chunk_size)
202 : message_size; 202 : message_size;
203 bool is_last_write_request = (end_index == message_size); 203 bool is_last_write_request = (end_index == message_size);
204 write_request = BuildWriteRequest( 204 write_request = BuildWriteRequest(
205 kFirstByteZeroVector, 205 kFirstByteZeroVector,
206 std::vector<uint8>(serialized_msg.begin() + start_index, 206 std::vector<uint8_t>(serialized_msg.begin() + start_index,
207 serialized_msg.begin() + end_index), 207 serialized_msg.begin() + end_index),
208 is_last_write_request); 208 is_last_write_request);
209 WriteRemoteCharacteristic(write_request); 209 WriteRemoteCharacteristic(write_request);
210 start_index = end_index; 210 start_index = end_index;
211 } 211 }
212 } 212 }
213 213
214 // Changes in the GATT connection with the remote device should be observed 214 // Changes in the GATT connection with the remote device should be observed
215 // here. If the GATT connection is dropped, we should call Disconnect() anyway, 215 // here. If the GATT connection is dropped, we should call Disconnect() anyway,
216 // so the object can notify its observers. 216 // so the object can notify its observers.
217 void BluetoothLowEnergyConnection::DeviceChanged(BluetoothAdapter* adapter, 217 void BluetoothLowEnergyConnection::DeviceChanged(BluetoothAdapter* adapter,
(...skipping 21 matching lines...) Expand all
239 device->GetAddress() != GetDeviceAddress()) 239 device->GetAddress() != GetDeviceAddress())
240 return; 240 return;
241 241
242 PA_LOG(INFO) << "Device removed " << GetDeviceAddress(); 242 PA_LOG(INFO) << "Device removed " << GetDeviceAddress();
243 Disconnect(); 243 Disconnect();
244 } 244 }
245 245
246 void BluetoothLowEnergyConnection::GattCharacteristicValueChanged( 246 void BluetoothLowEnergyConnection::GattCharacteristicValueChanged(
247 BluetoothAdapter* adapter, 247 BluetoothAdapter* adapter,
248 BluetoothGattCharacteristic* characteristic, 248 BluetoothGattCharacteristic* characteristic,
249 const std::vector<uint8>& value) { 249 const std::vector<uint8_t>& value) {
250 DCHECK_EQ(adapter, adapter_.get()); 250 DCHECK_EQ(adapter, adapter_.get());
251 if (sub_status() != SubStatus::WAITING_RESPONSE_SIGNAL && 251 if (sub_status() != SubStatus::WAITING_RESPONSE_SIGNAL &&
252 sub_status() != SubStatus::CONNECTED) 252 sub_status() != SubStatus::CONNECTED)
253 return; 253 return;
254 254
255 PA_LOG(INFO) << "Characteristic value changed: " 255 PA_LOG(INFO) << "Characteristic value changed: "
256 << characteristic->GetUUID().canonical_value(); 256 << characteristic->GetUUID().canonical_value();
257 257
258 if (characteristic->GetIdentifier() == from_peripheral_char_.id) { 258 if (characteristic->GetIdentifier() == from_peripheral_char_.id) {
259 if (receiving_bytes_) { 259 if (receiving_bytes_) {
(...skipping 19 matching lines...) Expand all
279 CompleteConnection(); 279 CompleteConnection();
280 break; 280 break;
281 case ControlSignal::kInviteToConnectSignal: 281 case ControlSignal::kInviteToConnectSignal:
282 break; 282 break;
283 case ControlSignal::kSendSignal: { 283 case ControlSignal::kSendSignal: {
284 if (value.size() < 8) { 284 if (value.size() < 8) {
285 PA_LOG(WARNING) 285 PA_LOG(WARNING)
286 << "Incoming data corrupted, expected message size not found."; 286 << "Incoming data corrupted, expected message size not found.";
287 return; 287 return;
288 } 288 }
289 std::vector<uint8> size(value.begin() + 4, value.begin() + 8); 289 std::vector<uint8_t> size(value.begin() + 4, value.begin() + 8);
290 expected_number_of_incoming_bytes_ = 290 expected_number_of_incoming_bytes_ =
291 static_cast<size_t>(ToUint32(size)); 291 static_cast<size_t>(ToUint32(size));
292 receiving_bytes_ = true; 292 receiving_bytes_ = true;
293 incoming_bytes_buffer_.clear(); 293 incoming_bytes_buffer_.clear();
294 294
295 const std::string bytes(value.begin() + 8, value.end()); 295 const std::string bytes(value.begin() + 8, value.end());
296 incoming_bytes_buffer_.append(bytes); 296 incoming_bytes_buffer_.append(bytes);
297 if (incoming_bytes_buffer_.size() >= 297 if (incoming_bytes_buffer_.size() >=
298 expected_number_of_incoming_bytes_) { 298 expected_number_of_incoming_bytes_) {
299 OnBytesReceived(incoming_bytes_buffer_); 299 OnBytesReceived(incoming_bytes_buffer_);
300 receiving_bytes_ = false; 300 receiving_bytes_ = false;
301 } 301 }
302 break; 302 break;
303 } 303 }
304 case ControlSignal::kDisconnectSignal: 304 case ControlSignal::kDisconnectSignal:
305 PA_LOG(INFO) << "Disconnect signal received."; 305 PA_LOG(INFO) << "Disconnect signal received.";
306 Disconnect(); 306 Disconnect();
307 break; 307 break;
308 } 308 }
309 } 309 }
310 } 310 }
311 311
312 BluetoothLowEnergyConnection::WriteRequest::WriteRequest( 312 BluetoothLowEnergyConnection::WriteRequest::WriteRequest(
313 const std::vector<uint8>& val, 313 const std::vector<uint8_t>& val,
314 bool flag) 314 bool flag)
315 : value(val), 315 : value(val),
316 is_last_write_for_wire_message(flag), 316 is_last_write_for_wire_message(flag),
317 number_of_failed_attempts(0) { 317 number_of_failed_attempts(0) {}
318 }
319 318
320 BluetoothLowEnergyConnection::WriteRequest::~WriteRequest() {} 319 BluetoothLowEnergyConnection::WriteRequest::~WriteRequest() {}
321 320
322 void BluetoothLowEnergyConnection::CompleteConnection() { 321 void BluetoothLowEnergyConnection::CompleteConnection() {
323 PA_LOG(INFO) << "Connection completed. Time elapsed: " 322 PA_LOG(INFO) << "Connection completed. Time elapsed: "
324 << base::TimeTicks::Now() - start_time_; 323 << base::TimeTicks::Now() - start_time_;
325 SetSubStatus(SubStatus::CONNECTED); 324 SetSubStatus(SubStatus::CONNECTED);
326 } 325 }
327 326
328 void BluetoothLowEnergyConnection::OnCreateGattConnectionError( 327 void BluetoothLowEnergyConnection::OnCreateGattConnectionError(
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 } 448 }
450 } 449 }
451 450
452 void BluetoothLowEnergyConnection::SendInviteToConnectSignal() { 451 void BluetoothLowEnergyConnection::SendInviteToConnectSignal() {
453 if (sub_status() == SubStatus::NOTIFY_SESSION_READY) { 452 if (sub_status() == SubStatus::NOTIFY_SESSION_READY) {
454 PA_LOG(INFO) << "Sending invite to connect signal"; 453 PA_LOG(INFO) << "Sending invite to connect signal";
455 SetSubStatus(SubStatus::WAITING_RESPONSE_SIGNAL); 454 SetSubStatus(SubStatus::WAITING_RESPONSE_SIGNAL);
456 455
457 WriteRequest write_request = BuildWriteRequest( 456 WriteRequest write_request = BuildWriteRequest(
458 ToByteVector( 457 ToByteVector(
459 static_cast<uint32>(ControlSignal::kInviteToConnectSignal)), 458 static_cast<uint32_t>(ControlSignal::kInviteToConnectSignal)),
460 std::vector<uint8>(), false); 459 std::vector<uint8_t>(), false);
461 460
462 WriteRemoteCharacteristic(write_request); 461 WriteRemoteCharacteristic(write_request);
463 } 462 }
464 } 463 }
465 464
466 void BluetoothLowEnergyConnection::WriteRemoteCharacteristic( 465 void BluetoothLowEnergyConnection::WriteRemoteCharacteristic(
467 WriteRequest request) { 466 WriteRequest request) {
468 write_requests_queue_.push(request); 467 write_requests_queue_.push(request);
469 ProcessNextWriteRequest(); 468 ProcessNextWriteRequest();
470 } 469 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 if (++write_requests_queue_.front().number_of_failed_attempts >= 517 if (++write_requests_queue_.front().number_of_failed_attempts >=
519 max_number_of_write_attempts_) { 518 max_number_of_write_attempts_) {
520 Disconnect(); 519 Disconnect();
521 return; 520 return;
522 } 521 }
523 ProcessNextWriteRequest(); 522 ProcessNextWriteRequest();
524 } 523 }
525 524
526 BluetoothLowEnergyConnection::WriteRequest 525 BluetoothLowEnergyConnection::WriteRequest
527 BluetoothLowEnergyConnection::BuildWriteRequest( 526 BluetoothLowEnergyConnection::BuildWriteRequest(
528 const std::vector<uint8>& signal, 527 const std::vector<uint8_t>& signal,
529 const std::vector<uint8>& bytes, 528 const std::vector<uint8_t>& bytes,
530 bool is_last_write_for_wire_message) { 529 bool is_last_write_for_wire_message) {
531 std::vector<uint8> value(signal.begin(), signal.end()); 530 std::vector<uint8_t> value(signal.begin(), signal.end());
532 value.insert(value.end(), bytes.begin(), bytes.end()); 531 value.insert(value.end(), bytes.begin(), bytes.end());
533 return WriteRequest(value, is_last_write_for_wire_message); 532 return WriteRequest(value, is_last_write_for_wire_message);
534 } 533 }
535 534
536 void BluetoothLowEnergyConnection::PrintTimeElapsed() { 535 void BluetoothLowEnergyConnection::PrintTimeElapsed() {
537 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_; 536 PA_LOG(INFO) << "Time elapsed: " << base::TimeTicks::Now() - start_time_;
538 } 537 }
539 538
540 std::string BluetoothLowEnergyConnection::GetDeviceAddress() { 539 std::string BluetoothLowEnergyConnection::GetDeviceAddress() {
541 // When the remote device is connected we should rely on the address given by 540 // When the remote device is connected we should rely on the address given by
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 BluetoothGattService* remote_service = GetRemoteService(); 586 BluetoothGattService* remote_service = GetRemoteService();
588 if (!remote_service) { 587 if (!remote_service) {
589 PA_LOG(WARNING) << "Remote service not found."; 588 PA_LOG(WARNING) << "Remote service not found.";
590 return NULL; 589 return NULL;
591 } 590 }
592 return remote_service->GetCharacteristic(gatt_characteristic); 591 return remote_service->GetCharacteristic(gatt_characteristic);
593 } 592 }
594 593
595 // TODO(sacomoto): make this robust to byte ordering in both sides of the 594 // TODO(sacomoto): make this robust to byte ordering in both sides of the
596 // SmartLock BLE socket. 595 // SmartLock BLE socket.
597 uint32 BluetoothLowEnergyConnection::ToUint32(const std::vector<uint8>& bytes) { 596 uint32_t BluetoothLowEnergyConnection::ToUint32(
597 const std::vector<uint8_t>& bytes) {
598 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); 598 return bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
599 } 599 }
600 600
601 // TODO(sacomoto): make this robust to byte ordering in both sides of the 601 // TODO(sacomoto): make this robust to byte ordering in both sides of the
602 // SmartLock BLE socket. 602 // SmartLock BLE socket.
603 const std::vector<uint8> BluetoothLowEnergyConnection::ToByteVector( 603 const std::vector<uint8_t> BluetoothLowEnergyConnection::ToByteVector(
604 const uint32 value) { 604 const uint32_t value) {
605 std::vector<uint8> bytes(4, 0); 605 std::vector<uint8_t> bytes(4, 0);
606 bytes[0] = static_cast<uint8>(value); 606 bytes[0] = static_cast<uint8_t>(value);
607 bytes[1] = static_cast<uint8>(value >> 8); 607 bytes[1] = static_cast<uint8_t>(value >> 8);
608 bytes[2] = static_cast<uint8>(value >> 16); 608 bytes[2] = static_cast<uint8_t>(value >> 16);
609 bytes[3] = static_cast<uint8>(value >> 24); 609 bytes[3] = static_cast<uint8_t>(value >> 24);
610 return bytes; 610 return bytes;
611 } 611 }
612 612
613 } // namespace proximity_auth 613 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698