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

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: Fix bug / More descriptive type name for template 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
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | no next file » | 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 "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 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 true /* is_notify */, mojo::Array<uint8_t>::From(value)); 286 true /* is_notify */, mojo::Array<uint8_t>::From(value));
287 } 287 }
288 288
289 void ArcBluetoothBridge::GattDescriptorValueChanged( 289 void ArcBluetoothBridge::GattDescriptorValueChanged(
290 BluetoothAdapter* adapter, 290 BluetoothAdapter* adapter,
291 BluetoothRemoteGattDescriptor* descriptor, 291 BluetoothRemoteGattDescriptor* descriptor,
292 const std::vector<uint8_t>& value) { 292 const std::vector<uint8_t>& value) {
293 // Placeholder for GATT client functionality 293 // Placeholder for GATT client functionality
294 } 294 }
295 295
296 // We will use odd transaction_id for read and even id for write to make sure
297 // that it does not overlap and make it easy to determine the transaction type.
298 int32_t ArcBluetoothBridge::CreateGattReadTransaction(
299 const GattServerReadCallbacks& callbacks) {
300 static int32_t transaction_id = 1;
301 transaction_id += 2;
302 gatt_server_read_callbacks_[transaction_id] = callbacks;
303 return transaction_id;
304 }
305
306 int32_t ArcBluetoothBridge::CreateGattWriteTransaction(
307 const GattServerWriteCallbacks& callbacks) {
308 static int32_t transaction_id = 2;
309 transaction_id += 2;
310 gatt_server_write_callbacks_[transaction_id] = callbacks;
311 return transaction_id;
312 }
313
314 template <class LocalGattObjectT>
315 void ArcBluetoothBridge::OnGattAttributeReadRequest(
316 const BluetoothDevice* device,
317 const LocalGattObjectT* gatt_obj,
318 int offset,
319 const GattServerReadCallbacks& callbacks) {
320 if (!HasBluetoothInstance())
321 return;
322
323 if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
324 return;
325
326 int32_t transaction_id = CreateGattReadTransaction(callbacks);
327
328 arc_bridge_service()->bluetooth()->instance()->RequestGattRead(
329 mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
330 gatt_handle_[gatt_obj->GetIdentifier()], offset, false /*is_long */);
palmer 2016/07/16 00:48:26 Is there code somewhere (in the recipient?) that v
puthik_chromium 2016/07/18 21:30:32 Look like Android API expected app to override thi
puthik_chromium 2016/07/19 01:03:15 I also add sanity check to just call error_callbac
331 }
332
333 template <class LocalGattObjectT>
334 void ArcBluetoothBridge::OnGattAttributeWriteRequest(
335 const BluetoothDevice* device,
336 const LocalGattObjectT* gatt_obj,
337 const std::vector<uint8_t>& value,
338 int offset,
339 const GattServerWriteCallbacks& callbacks) {
340 if (!HasBluetoothInstance())
341 return;
342
343 if (!CheckBluetoothInstanceVersion(kMinBtleNotifyVersion))
344 return;
345
346 int32_t transaction_id = CreateGattWriteTransaction(callbacks);
347
348 arc_bridge_service()->bluetooth()->instance()->RequestGattWrite(
349 mojom::BluetoothAddress::From(device->GetAddress()), transaction_id,
350 gatt_handle_[gatt_obj->GetIdentifier()], offset,
351 mojo::Array<uint8_t>::From(value));
352 }
353
296 void ArcBluetoothBridge::OnCharacteristicReadRequest( 354 void ArcBluetoothBridge::OnCharacteristicReadRequest(
297 const BluetoothDevice* device, 355 const BluetoothDevice* device,
298 const BluetoothLocalGattCharacteristic* characteristic, 356 const BluetoothLocalGattCharacteristic* characteristic,
299 int offset, 357 int offset,
300 const ValueCallback& callback, 358 const ValueCallback& callback,
301 const ErrorCallback& error_callback) {} 359 const ErrorCallback& error_callback) {
360 OnGattAttributeReadRequest<BluetoothLocalGattCharacteristic>(
361 device, characteristic, offset,
362 GattServerReadCallbacks(callback, error_callback));
363 }
302 364
303 void ArcBluetoothBridge::OnCharacteristicWriteRequest( 365 void ArcBluetoothBridge::OnCharacteristicWriteRequest(
304 const BluetoothDevice* device, 366 const BluetoothDevice* device,
305 const BluetoothLocalGattCharacteristic* characteristic, 367 const BluetoothLocalGattCharacteristic* characteristic,
306 const std::vector<uint8_t>& value, 368 const std::vector<uint8_t>& value,
307 int offset, 369 int offset,
308 const base::Closure& callback, 370 const base::Closure& callback,
309 const ErrorCallback& error_callback) {} 371 const ErrorCallback& error_callback) {
372 OnGattAttributeWriteRequest<BluetoothLocalGattCharacteristic>(
373 device, characteristic, value, offset,
374 GattServerWriteCallbacks(callback, error_callback));
375 }
310 376
311 void ArcBluetoothBridge::OnDescriptorReadRequest( 377 void ArcBluetoothBridge::OnDescriptorReadRequest(
312 const BluetoothDevice* device, 378 const BluetoothDevice* device,
313 const BluetoothLocalGattDescriptor* descriptor, 379 const BluetoothLocalGattDescriptor* descriptor,
314 int offset, 380 int offset,
315 const ValueCallback& callback, 381 const ValueCallback& callback,
316 const ErrorCallback& error_callback) {} 382 const ErrorCallback& error_callback) {
383 OnGattAttributeReadRequest<BluetoothLocalGattDescriptor>(
384 device, descriptor, offset,
385 GattServerReadCallbacks(callback, error_callback));
386 }
317 387
318 void ArcBluetoothBridge::OnDescriptorWriteRequest( 388 void ArcBluetoothBridge::OnDescriptorWriteRequest(
319 const BluetoothDevice* device, 389 const BluetoothDevice* device,
320 const BluetoothLocalGattDescriptor* descriptor, 390 const BluetoothLocalGattDescriptor* descriptor,
321 const std::vector<uint8_t>& value, 391 const std::vector<uint8_t>& value,
322 int offset, 392 int offset,
323 const base::Closure& callback, 393 const base::Closure& callback,
324 const ErrorCallback& error_callback) {} 394 const ErrorCallback& error_callback) {
395 OnGattAttributeWriteRequest<BluetoothLocalGattDescriptor>(
396 device, descriptor, value, offset,
397 GattServerWriteCallbacks(callback, error_callback));
398 }
325 399
326 void ArcBluetoothBridge::OnNotificationsStart( 400 void ArcBluetoothBridge::OnNotificationsStart(
327 const BluetoothDevice* device, 401 const BluetoothDevice* device,
328 const BluetoothLocalGattCharacteristic* characteristic) {} 402 const BluetoothLocalGattCharacteristic* characteristic) {}
329 403
330 void ArcBluetoothBridge::OnNotificationsStop( 404 void ArcBluetoothBridge::OnNotificationsStop(
331 const BluetoothDevice* device, 405 const BluetoothDevice* device,
332 const BluetoothLocalGattCharacteristic* characteristic) {} 406 const BluetoothLocalGattCharacteristic* characteristic) {}
333 407
334 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) { 408 void ArcBluetoothBridge::EnableAdapter(const EnableAdapterCallback& callback) {
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after
1057 } 1131 }
1058 1132
1059 template <class LocalGattObjectT> 1133 template <class LocalGattObjectT>
1060 int32_t ArcBluetoothBridge::CreateGattAttributeHandle( 1134 int32_t ArcBluetoothBridge::CreateGattAttributeHandle(
1061 LocalGattObjectT* gatt_obj) { 1135 LocalGattObjectT* gatt_obj) {
1062 if (!gatt_obj) 1136 if (!gatt_obj)
1063 return kInvalidGattAttributeHandle; 1137 return kInvalidGattAttributeHandle;
1064 gatt_server_obj_handle++; 1138 gatt_server_obj_handle++;
1065 std::string identifier = gatt_obj->GetIdentifier(); 1139 std::string identifier = gatt_obj->GetIdentifier();
1066 gatt_identifier_[gatt_server_obj_handle] = identifier; 1140 gatt_identifier_[gatt_server_obj_handle] = identifier;
1141 gatt_handle_[identifier] = gatt_server_obj_handle;
1067 return gatt_server_obj_handle; 1142 return gatt_server_obj_handle;
1068 } 1143 }
1069 1144
1070 void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id, 1145 void ArcBluetoothBridge::AddService(mojom::BluetoothGattServiceIDPtr service_id,
1071 int32_t num_handles, 1146 int32_t num_handles,
1072 const AddServiceCallback& callback) { 1147 const AddServiceCallback& callback) {
1073 base::WeakPtr<BluetoothLocalGattService> service = 1148 base::WeakPtr<BluetoothLocalGattService> service =
1074 BluetoothLocalGattService::Create( 1149 BluetoothLocalGattService::Create(
1075 bluetooth_adapter_.get(), service_id->id->uuid.To<BluetoothUUID>(), 1150 bluetooth_adapter_.get(), service_id->id->uuid.To<BluetoothUUID>(),
1076 service_id->is_primary, nullptr /*included_service */, 1151 service_id->is_primary, nullptr /*included_service */,
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
1141 service->Unregister(base::Bind(&ArcBluetoothBridge::OnGattOperationDone, 1216 service->Unregister(base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
1142 weak_factory_.GetWeakPtr(), callback), 1217 weak_factory_.GetWeakPtr(), callback),
1143 base::Bind(&ArcBluetoothBridge::OnGattOperationError, 1218 base::Bind(&ArcBluetoothBridge::OnGattOperationError,
1144 weak_factory_.GetWeakPtr(), callback)); 1219 weak_factory_.GetWeakPtr(), callback));
1145 } 1220 }
1146 1221
1147 void ArcBluetoothBridge::DeleteService(int32_t service_handle, 1222 void ArcBluetoothBridge::DeleteService(int32_t service_handle,
1148 const DeleteServiceCallback& callback) { 1223 const DeleteServiceCallback& callback) {
1149 BluetoothLocalGattService* service = 1224 BluetoothLocalGattService* service =
1150 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]); 1225 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
1226 gatt_identifier_.erase(service_handle);
1227 gatt_handle_.erase(service->GetIdentifier());
1151 service->Delete(); 1228 service->Delete();
1152 gatt_identifier_.erase(service_handle);
1153 OnGattOperationDone(callback); 1229 OnGattOperationDone(callback);
1154 } 1230 }
1155 1231
1156 void ArcBluetoothBridge::SendIndication( 1232 void ArcBluetoothBridge::SendIndication(
1157 int32_t attribute_handle, 1233 int32_t attribute_handle,
1158 mojom::BluetoothAddressPtr address, 1234 mojom::BluetoothAddressPtr address,
1159 bool confirm, 1235 bool confirm,
1160 mojo::Array<uint8_t> value, 1236 mojo::Array<uint8_t> value,
1161 const SendIndicationCallback& callback) {} 1237 const SendIndicationCallback& callback) {}
1162 1238
1163 void ArcBluetoothBridge::SendResponse(int32_t trans_id, 1239 void ArcBluetoothBridge::SendResponse(int32_t trans_id,
1164 int32_t status, 1240 int32_t status,
1165 mojo::Array<uint8_t> value, 1241 mojo::Array<uint8_t> value,
1166 const SendResponseCallback& callback) {} 1242 const SendResponseCallback& callback) {
1243 // We used odd transaction_id for read and even id for write.
1244 if (trans_id & 1) { // read
1245 DCHECK(gatt_server_read_callbacks_.find(trans_id) !=
1246 gatt_server_read_callbacks_.end());
1247 if (status)
1248 gatt_server_read_callbacks_[trans_id].error_callback.Run();
1249 else
1250 gatt_server_read_callbacks_[trans_id].success_callback.Run(
palmer 2016/07/16 00:48:26 When the body if an if/else block is more than 1 l
puthik_chromium 2016/07/18 21:30:32 Done.
1251 value.To<std::vector<uint8_t>>());
1252 } else { // write
1253 DCHECK(gatt_server_write_callbacks_.find(trans_id) !=
1254 gatt_server_write_callbacks_.end());
1255 if (status)
1256 gatt_server_write_callbacks_[trans_id].error_callback.Run();
1257 else
1258 gatt_server_write_callbacks_[trans_id].success_callback.Run();
1259 }
1260 OnGattOperationDone(callback);
1261 }
1167 1262
1168 void ArcBluetoothBridge::OnDiscoveryError() { 1263 void ArcBluetoothBridge::OnDiscoveryError() {
1169 LOG(WARNING) << "failed to change discovery state"; 1264 LOG(WARNING) << "failed to change discovery state";
1170 } 1265 }
1171 1266
1172 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { 1267 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const {
1173 if (!HasBluetoothInstance()) 1268 if (!HasBluetoothInstance())
1174 return; 1269 return;
1175 1270
1176 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( 1271 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged(
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
1496 uint32_t version_need) const { 1591 uint32_t version_need) const {
1497 uint32_t version = arc_bridge_service()->bluetooth()->version(); 1592 uint32_t version = arc_bridge_service()->bluetooth()->version();
1498 if (version >= version_need) 1593 if (version >= version_need)
1499 return true; 1594 return true;
1500 LOG(WARNING) << "Bluetooth instance is too old (version " << version 1595 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1501 << ") need version " << version_need; 1596 << ") need version " << version_need;
1502 return false; 1597 return false;
1503 } 1598 }
1504 1599
1505 } // namespace arc 1600 } // namespace arc
OLDNEW
« no previous file with comments | « components/arc/bluetooth/arc_bluetooth_bridge.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698