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

Side by Side Diff: components/arc/bluetooth/arc_bluetooth_bridge.cc

Issue 2101283003: arc: bluetooth: Implement Gatt server request to read/write (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gs2
Patch Set: Created 4 years, 5 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 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 "components/arc/bluetooth/arc_bluetooth_bridge.h" 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h"
6 6
7 #include <fcntl.h> 7 #include <fcntl.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <iomanip> 10 #include <iomanip>
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 true /* is_notify */, mojo::Array<uint8_t>::From(value)); 291 true /* is_notify */, mojo::Array<uint8_t>::From(value));
292 } 292 }
293 293
294 void ArcBluetoothBridge::GattDescriptorValueChanged( 294 void ArcBluetoothBridge::GattDescriptorValueChanged(
295 BluetoothAdapter* adapter, 295 BluetoothAdapter* adapter,
296 BluetoothRemoteGattDescriptor* descriptor, 296 BluetoothRemoteGattDescriptor* descriptor,
297 const std::vector<uint8_t>& value) { 297 const std::vector<uint8_t>& value) {
298 // Placeholder for GATT client functionality 298 // Placeholder for GATT client functionality
299 } 299 }
300 300
301 // We will use odd transaction_id for read and even id for write.
rkc 2016/07/01 22:51:32 Why?
puthik_chromium 2016/07/01 22:56:09 1. To make sure that it does not repeat same numbe
rkc 2016/07/01 23:04:02 You got me wrong :) I was saying that the comment
puthik_chromium 2016/07/14 22:45:11 Done.
302 int32_t ArcBluetoothBridge::CreateGattReadTransaction(
303 const ValueCallback& successCallback,
304 const ErrorCallback& errorCallback) {
305 static int32_t transaction_id = 1;
306 transaction_id += 2;
307 gatt_read_callbacks_.emplace(
rkc 2016/07/01 22:51:32 Can't use map::emplace. https://chromium-cpp.appsp
puthik_chromium 2016/07/14 22:45:12 Done.
308 transaction_id,
309 std::pair<ValueCallback, ErrorCallback>(successCallback, errorCallback));
Eric Caruso 2016/06/30 23:46:20 Instead of using std::pair here and pair::first an
puthik_chromium 2016/07/14 22:45:12 Done.
310 return transaction_id;
311 }
312
313 int32_t ArcBluetoothBridge::CreateGattWriteTransaction(
314 const base::Closure& successCallback,
315 const ErrorCallback& errorCallback) {
316 static int32_t transaction_id = 2;
317 transaction_id += 2;
318 gatt_write_callbacks_.emplace(
319 transaction_id,
320 std::pair<base::Closure, ErrorCallback>(successCallback, errorCallback));
321 return transaction_id;
322 }
323
324 template <class T>
325 void ArcBluetoothBridge::OnGattAttributeReadRequest(
326 const BluetoothDevice* device,
327 const T* gatt_obj,
328 int offset,
329 const ValueCallback& callback,
330 const ErrorCallback& error_callback) {
331 if (!HasBluetoothInstance())
332 return;
333
334 if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
335 return;
336
337 int32_t transaction_id = CreateGattReadTransaction(callback, error_callback);
338
339 arc_bridge_service()->bluetooth_instance()->RequestGattRead(
340 mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
341 ConvertGattIdentifierToId(gatt_obj->GetIdentifier()), offset,
342 false /*is_long */);
343 }
344
345 template <class T>
346 void ArcBluetoothBridge::OnGattAttributeWriteRequest(
347 const BluetoothDevice* device,
348 const T* gatt_obj,
349 const std::vector<uint8_t>& value,
350 int offset,
351 const base::Closure& callback,
352 const ErrorCallback& error_callback) {
353 if (!HasBluetoothInstance())
354 return;
355
356 if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
357 return;
358
359 int32_t transaction_id = CreateGattWriteTransaction(callback, error_callback);
360
361 arc_bridge_service()->bluetooth_instance()->RequestGattWrite(
362 mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
363 ConvertGattIdentifierToId(gatt_obj->GetIdentifier()), offset,
364 mojo::Array<uint8_t>::From(value));
365 }
366
301 void ArcBluetoothBridge::OnCharacteristicReadRequest( 367 void ArcBluetoothBridge::OnCharacteristicReadRequest(
302 const BluetoothDevice* device, 368 const BluetoothDevice* device,
303 const BluetoothLocalGattCharacteristic* characteristic, 369 const BluetoothLocalGattCharacteristic* characteristic,
304 int offset, 370 int offset,
305 const ValueCallback& callback, 371 const ValueCallback& callback,
306 const ErrorCallback& error_callback) {} 372 const ErrorCallback& error_callback) {
373 OnGattAttributeReadRequest<BluetoothLocalGattCharacteristic>(
374 device, characteristic, offset, callback, error_callback);
375 }
307 376
308 void ArcBluetoothBridge::OnCharacteristicWriteRequest( 377 void ArcBluetoothBridge::OnCharacteristicWriteRequest(
309 const BluetoothDevice* device, 378 const BluetoothDevice* device,
310 const BluetoothLocalGattCharacteristic* characteristic, 379 const BluetoothLocalGattCharacteristic* characteristic,
311 const std::vector<uint8_t>& value, 380 const std::vector<uint8_t>& value,
312 int offset, 381 int offset,
313 const base::Closure& callback, 382 const base::Closure& callback,
314 const ErrorCallback& error_callback) {} 383 const ErrorCallback& error_callback) {
384 OnGattAttributeWriteRequest<BluetoothLocalGattCharacteristic>(
385 device, characteristic, value, offset, callback, error_callback);
386 }
315 387
316 void ArcBluetoothBridge::OnDescriptorReadRequest( 388 void ArcBluetoothBridge::OnDescriptorReadRequest(
317 const BluetoothDevice* device, 389 const BluetoothDevice* device,
318 const BluetoothLocalGattDescriptor* descriptor, 390 const BluetoothLocalGattDescriptor* descriptor,
319 int offset, 391 int offset,
320 const ValueCallback& callback, 392 const ValueCallback& callback,
321 const ErrorCallback& error_callback) {} 393 const ErrorCallback& error_callback) {
394 OnGattAttributeReadRequest<BluetoothLocalGattDescriptor>(
395 device, descriptor, offset, callback, error_callback);
396 }
322 397
323 void ArcBluetoothBridge::OnDescriptorWriteRequest( 398 void ArcBluetoothBridge::OnDescriptorWriteRequest(
324 const BluetoothDevice* device, 399 const BluetoothDevice* device,
325 const BluetoothLocalGattDescriptor* descriptor, 400 const BluetoothLocalGattDescriptor* descriptor,
326 const std::vector<uint8_t>& value, 401 const std::vector<uint8_t>& value,
327 int offset, 402 int offset,
328 const base::Closure& callback, 403 const base::Closure& callback,
329 const ErrorCallback& error_callback) {} 404 const ErrorCallback& error_callback) {
405 OnGattAttributeWriteRequest<BluetoothLocalGattDescriptor>(
406 device, descriptor, value, offset, callback, error_callback);
407 }
330 408
331 void ArcBluetoothBridge::OnNotificationsStart( 409 void ArcBluetoothBridge::OnNotificationsStart(
332 const BluetoothDevice* device, 410 const BluetoothDevice* device,
333 const BluetoothLocalGattCharacteristic* characteristic) {} 411 const BluetoothLocalGattCharacteristic* characteristic) {}
334 412
335 void ArcBluetoothBridge::OnNotificationsStop( 413 void ArcBluetoothBridge::OnNotificationsStop(
336 const BluetoothDevice* device, 414 const BluetoothDevice* device,
337 const BluetoothLocalGattCharacteristic* characteristic) {} 415 const BluetoothLocalGattCharacteristic* characteristic) {}
338 416
339 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) { 417 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) {
(...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after
1138 void ArcBluetoothBridge::SendIndication( 1216 void ArcBluetoothBridge::SendIndication(
1139 int32_t attribute_handle, 1217 int32_t attribute_handle,
1140 mojom::BluetoothAddressPtr address, 1218 mojom::BluetoothAddressPtr address,
1141 int32_t confirm, 1219 int32_t confirm,
1142 mojo::Array<uint8_t> value, 1220 mojo::Array<uint8_t> value,
1143 const SendIndicationCallback& callback) {} 1221 const SendIndicationCallback& callback) {}
1144 1222
1145 void ArcBluetoothBridge::SendResponse(int32_t trans_id, 1223 void ArcBluetoothBridge::SendResponse(int32_t trans_id,
1146 int32_t status, 1224 int32_t status,
1147 mojo::Array<uint8_t> value, 1225 mojo::Array<uint8_t> value,
1148 const SendResponseCallback& callback) {} 1226 const SendResponseCallback& callback) {
1227 // We will use odd transaction_id for read and even id for write.
1228 if (trans_id & 1) { // read
1229 DCHECK(gatt_read_callbacks_.find(trans_id) != gatt_read_callbacks_.end());
1230 if (status)
1231 gatt_read_callbacks_[trans_id].second.Run();
1232 else
1233 gatt_read_callbacks_[trans_id].first.Run(
1234 value.To<std::vector<uint8_t>>());
1235 } else { // write
1236 DCHECK(gatt_write_callbacks_.find(trans_id) != gatt_write_callbacks_.end());
1237 if (status)
1238 gatt_write_callbacks_[trans_id].second.Run();
1239 else
1240 gatt_write_callbacks_[trans_id].first.Run();
1241 }
1242 OnGattOperationDone(callback);
1243 }
1149 1244
1150 void ArcBluetoothBridge::OnDiscoveryError() { 1245 void ArcBluetoothBridge::OnDiscoveryError() {
1151 LOG(WARNING) << "failed to change discovery state"; 1246 LOG(WARNING) << "failed to change discovery state";
1152 } 1247 }
1153 1248
1154 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { 1249 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const {
1155 if (!HasBluetoothInstance()) 1250 if (!HasBluetoothInstance())
1156 return; 1251 return;
1157 1252
1158 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( 1253 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged(
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
1478 int32_t version_need) const { 1573 int32_t version_need) const {
1479 int32_t version = arc_bridge_service()->bluetooth_version(); 1574 int32_t version = arc_bridge_service()->bluetooth_version();
1480 if (version >= version_need) 1575 if (version >= version_need)
1481 return true; 1576 return true;
1482 LOG(WARNING) << "Bluetooth instance is too old (version " << version 1577 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1483 << ") need version " << version_need; 1578 << ") need version " << version_need;
1484 return false; 1579 return false;
1485 } 1580 }
1486 1581
1487 } // namespace arc 1582 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698