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

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: run error_callback if offset < 0 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 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 LocalGattAttribute>
315 void ArcBluetoothBridge::OnGattAttributeReadRequest(
316 const BluetoothDevice* device,
317 const LocalGattAttribute* gatt_obj,
318 int offset,
319 const GattServerReadCallbacks& callbacks) {
320 if (!HasBluetoothInstance() ||
321 !CheckBluetoothInstanceVersion(kMinBtleNotifyVersion) || offset < 0) {
322 callbacks.error_callback.Run();
323 return;
324 }
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 */);
Luis Héctor Chávez 2016/07/19 01:49:17 What happens if |gatt_obj->GetIdentifier()| is inv
puthik_chromium 2016/07/19 22:21:47 It should valid all the time because we are delega
331 }
332
333 template <class LocalGattAttribute>
334 void ArcBluetoothBridge::OnGattAttributeWriteRequest(
335 const BluetoothDevice* device,
336 const LocalGattAttribute* gatt_obj,
337 const std::vector<uint8_t>& value,
338 int offset,
339 const GattServerWriteCallbacks& callbacks) {
340 if (!HasBluetoothInstance() ||
341 !CheckBluetoothInstanceVersion(kMinBtleNotifyVersion) || offset < 0) {
342 callbacks.error_callback.Run();
343 return;
344 }
345
346 int32_t transaction_id = CreateGattWriteTransaction(callbacks);
347
348 arc_bridge_service()->bluetooth()->instance()->RequestGattWrite(
Luis Héctor Chávez 2016/07/19 01:49:16 Instead of doing this transaction magic, is it pos
puthik_chromium 2016/07/19 22:21:47 This require 3 calls. Chrome to Android: RequestGa
Luis Héctor Chávez 2016/07/19 22:38:00 What do you need the callback of SendResponse for?
puthik_chromium 2016/07/19 23:05:43 Look like the response of SendResponse is optional
puthik_chromium 2016/07/20 01:42:09 Done. SendResponse removed
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 LocalGattAttribute> 1133 template <class LocalGattAttribute>
1060 int32_t ArcBluetoothBridge::CreateGattAttributeHandle( 1134 int32_t ArcBluetoothBridge::CreateGattAttributeHandle(
1061 LocalGattAttribute* gatt_attr) { 1135 LocalGattAttribute* gatt_attr) {
1062 if (!gatt_attr) 1136 if (!gatt_attr)
1063 return kInvalidGattAttributeHandle; 1137 return kInvalidGattAttributeHandle;
1064 gatt_server_attr_handle++; 1138 gatt_server_attr_handle++;
1065 std::string identifier = gatt_attr->GetIdentifier(); 1139 std::string identifier = gatt_attr->GetIdentifier();
1066 gatt_identifier_[gatt_server_attr_handle] = identifier; 1140 gatt_identifier_[gatt_server_attr_handle] = identifier;
1141 gatt_handle_[identifier] = gatt_server_attr_handle;
1067 return gatt_server_attr_handle; 1142 return gatt_server_attr_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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1143 service->Unregister(base::Bind(&ArcBluetoothBridge::OnGattOperationDone, 1218 service->Unregister(base::Bind(&ArcBluetoothBridge::OnGattOperationDone,
1144 weak_factory_.GetWeakPtr(), callback), 1219 weak_factory_.GetWeakPtr(), callback),
1145 base::Bind(&ArcBluetoothBridge::OnGattOperationError, 1220 base::Bind(&ArcBluetoothBridge::OnGattOperationError,
1146 weak_factory_.GetWeakPtr(), callback)); 1221 weak_factory_.GetWeakPtr(), callback));
1147 } 1222 }
1148 1223
1149 void ArcBluetoothBridge::DeleteService(int32_t service_handle, 1224 void ArcBluetoothBridge::DeleteService(int32_t service_handle,
1150 const DeleteServiceCallback& callback) { 1225 const DeleteServiceCallback& callback) {
1151 BluetoothLocalGattService* service = 1226 BluetoothLocalGattService* service =
1152 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]); 1227 bluetooth_adapter_->GetGattService(gatt_identifier_[service_handle]);
1228 gatt_identifier_.erase(service_handle);
1229 gatt_handle_.erase(service->GetIdentifier());
1153 service->Delete(); 1230 service->Delete();
1154 gatt_identifier_.erase(service_handle);
1155 OnGattOperationDone(callback); 1231 OnGattOperationDone(callback);
1156 } 1232 }
1157 1233
1158 void ArcBluetoothBridge::SendIndication( 1234 void ArcBluetoothBridge::SendIndication(
1159 int32_t attribute_handle, 1235 int32_t attribute_handle,
1160 mojom::BluetoothAddressPtr address, 1236 mojom::BluetoothAddressPtr address,
1161 bool confirm, 1237 bool confirm,
1162 mojo::Array<uint8_t> value, 1238 mojo::Array<uint8_t> value,
1163 const SendIndicationCallback& callback) {} 1239 const SendIndicationCallback& callback) {}
1164 1240
1165 void ArcBluetoothBridge::SendResponse(int32_t trans_id, 1241 void ArcBluetoothBridge::SendResponse(int32_t trans_id,
1166 int32_t status, 1242 int32_t status,
1167 mojo::Array<uint8_t> value, 1243 mojo::Array<uint8_t> value,
1168 const SendResponseCallback& callback) {} 1244 const SendResponseCallback& callback) {
1245 // We used odd transaction_id for read and even id for write.
1246 if (trans_id & 1) { // read
1247 DCHECK(gatt_server_read_callbacks_.find(trans_id) !=
1248 gatt_server_read_callbacks_.end());
1249 if (status) {
1250 gatt_server_read_callbacks_[trans_id].error_callback.Run();
1251 } else {
1252 gatt_server_read_callbacks_[trans_id].success_callback.Run(
1253 value.To<std::vector<uint8_t>>());
1254 }
1255 } else { // write
1256 DCHECK(gatt_server_write_callbacks_.find(trans_id) !=
1257 gatt_server_write_callbacks_.end());
1258 if (status)
1259 gatt_server_write_callbacks_[trans_id].error_callback.Run();
1260 else
1261 gatt_server_write_callbacks_[trans_id].success_callback.Run();
1262 }
1263 OnGattOperationDone(callback);
1264 }
1169 1265
1170 void ArcBluetoothBridge::OnDiscoveryError() { 1266 void ArcBluetoothBridge::OnDiscoveryError() {
1171 LOG(WARNING) << "failed to change discovery state"; 1267 LOG(WARNING) << "failed to change discovery state";
1172 } 1268 }
1173 1269
1174 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { 1270 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const {
1175 if (!HasBluetoothInstance()) 1271 if (!HasBluetoothInstance())
1176 return; 1272 return;
1177 1273
1178 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( 1274 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged(
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after
1498 uint32_t version_need) const { 1594 uint32_t version_need) const {
1499 uint32_t version = arc_bridge_service()->bluetooth()->version(); 1595 uint32_t version = arc_bridge_service()->bluetooth()->version();
1500 if (version >= version_need) 1596 if (version >= version_need)
1501 return true; 1597 return true;
1502 LOG(WARNING) << "Bluetooth instance is too old (version " << version 1598 LOG(WARNING) << "Bluetooth instance is too old (version " << version
1503 << ") need version " << version_need; 1599 << ") need version " << version_need;
1504 return false; 1600 return false;
1505 } 1601 }
1506 1602
1507 } // namespace arc 1603 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698