1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/notimplemented.h"
#include "base/strings/sys_string_conversions.h"
#import "base/task/single_thread_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "device/bluetooth/bluetooth_device_mac.h"
#include "device/bluetooth/bluetooth_gatt_notify_session.h"
#include "device/bluetooth/bluetooth_low_energy_adapter_apple.h"
#include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h"
#include "device/bluetooth/bluetooth_remote_gatt_service_mac.h"
namespace device {
namespace {
static BluetoothGattCharacteristic::Properties ConvertProperties(
CBCharacteristicProperties cb_property) {
BluetoothGattCharacteristic::Properties result =
BluetoothGattCharacteristic::PROPERTY_NONE;
if (cb_property & CBCharacteristicPropertyBroadcast) {
result |= BluetoothGattCharacteristic::PROPERTY_BROADCAST;
}
if (cb_property & CBCharacteristicPropertyRead) {
result |= BluetoothGattCharacteristic::PROPERTY_READ;
}
if (cb_property & CBCharacteristicPropertyWriteWithoutResponse) {
result |= BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE;
}
if (cb_property & CBCharacteristicPropertyWrite) {
result |= BluetoothGattCharacteristic::PROPERTY_WRITE;
}
if (cb_property & CBCharacteristicPropertyNotify) {
result |= BluetoothGattCharacteristic::PROPERTY_NOTIFY;
}
if (cb_property & CBCharacteristicPropertyIndicate) {
result |= BluetoothGattCharacteristic::PROPERTY_INDICATE;
}
if (cb_property & CBCharacteristicPropertyAuthenticatedSignedWrites) {
result |= BluetoothGattCharacteristic::PROPERTY_AUTHENTICATED_SIGNED_WRITES;
}
if (cb_property & CBCharacteristicPropertyExtendedProperties) {
result |= BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES;
}
if (cb_property & CBCharacteristicPropertyNotifyEncryptionRequired) {
// This property is used only in CBMutableCharacteristic
// (local characteristic). So this value should never appear for
// CBCharacteristic (remote characteristic). Apple is not able to send
// this value over BLE since it is not part of the spec.
DCHECK(false);
result |= BluetoothGattCharacteristic::PROPERTY_NOTIFY;
}
if (cb_property & CBCharacteristicPropertyIndicateEncryptionRequired) {
// This property is used only in CBMutableCharacteristic
// (local characteristic). So this value should never appear for
// CBCharacteristic (remote characteristic). Apple is not able to send
// this value over BLE since it is not part of the spec.
DCHECK(false);
result |= BluetoothGattCharacteristic::PROPERTY_INDICATE;
}
return result;
}
} // namespace
BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac(
BluetoothRemoteGattServiceMac* gatt_service,
CBCharacteristic* cb_characteristic)
: is_discovery_complete_(false),
discovery_pending_count_(0),
gatt_service_(gatt_service),
cb_characteristic_(cb_characteristic),
weak_ptr_factory_(this) {
uuid_ = BluetoothLowEnergyAdapterApple::BluetoothUUIDWithCBUUID(
[cb_characteristic_ UUID]);
identifier_ = base::SysNSStringToUTF8(
[NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(),
cb_characteristic_]);
}
BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() {
destructor_called_ = true;
if (HasPendingRead()) {
std::move(read_characteristic_value_callback_)
.Run(BluetoothGattService::GattErrorCode::kFailed,
/*value=*/std::vector<uint8_t>());
}
if (HasPendingWrite()) {
std::move(write_characteristic_value_callbacks_.second)
.Run(BluetoothGattService::GattErrorCode::kFailed);
}
}
std::string BluetoothRemoteGattCharacteristicMac::GetIdentifier() const {
return identifier_;
}
BluetoothUUID BluetoothRemoteGattCharacteristicMac::GetUUID() const {
return uuid_;
}
BluetoothGattCharacteristic::Properties
BluetoothRemoteGattCharacteristicMac::GetProperties() const {
return ConvertProperties([cb_characteristic_ properties]);
}
BluetoothGattCharacteristic::Permissions
BluetoothRemoteGattCharacteristicMac::GetPermissions() const {
// Not supported for remote characteristics for CoreBluetooth.
NOTIMPLEMENTED();
return PERMISSION_NONE;
}
const std::vector<uint8_t>& BluetoothRemoteGattCharacteristicMac::GetValue()
const {
return value_;
}
BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicMac::GetService()
const {
return static_cast<BluetoothRemoteGattService*>(gatt_service_);
}
bool BluetoothRemoteGattCharacteristicMac::IsNotifying() const {
return [cb_characteristic_ isNotifying] == YES;
}
void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic(
ValueCallback callback) {
if (!IsReadable()) {
DVLOG(1) << *this << ": Characteristic not readable.";
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback),
BluetoothGattService::GattErrorCode::kNotPermitted,
/*value=*/std::vector<uint8_t>()));
return;
}
if (destructor_called_ || HasPendingRead() || HasPendingWrite()) {
DVLOG(1) << *this << ": Characteristic read already in progress.";
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback),
BluetoothGattService::GattErrorCode::kInProgress,
/*value=*/std::vector<uint8_t>()));
return;
}
DVLOG(1) << *this << ": Read characteristic.";
read_characteristic_value_callback_ = std::move(callback);
[GetCBPeripheral() readValueForCharacteristic:cb_characteristic_];
}
void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic(
base::span<const uint8_t> value,
WriteType write_type,
base::OnceClosure callback,
ErrorCallback error_callback) {
if (destructor_called_ || HasPendingRead() || HasPendingWrite()) {
DVLOG(1) << *this << ": Characteristic write already in progress.";
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(error_callback),
BluetoothGattService::GattErrorCode::kInProgress));
return;
}
DVLOG(1) << *this << ": Write characteristic.";
write_characteristic_value_callbacks_ =
std::make_pair(std::move(callback), std::move(error_callback));
NSData* nsdata_value = [[NSData alloc] initWithBytes:value.data()
length:value.size()];
CBCharacteristicWriteType cb_write_type;
switch (write_type) {
case WriteType::kWithResponse:
cb_write_type = CBCharacteristicWriteWithResponse;
break;
case WriteType::kWithoutResponse:
cb_write_type = CBCharacteristicWriteWithoutResponse;
break;
}
[GetCBPeripheral() writeValue:nsdata_value
forCharacteristic:cb_characteristic_
type:cb_write_type];
if (cb_write_type == CBCharacteristicWriteWithoutResponse) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&BluetoothRemoteGattCharacteristicMac::DidWriteValue,
weak_ptr_factory_.GetWeakPtr(), nil));
}
}
void BluetoothRemoteGattCharacteristicMac::DeprecatedWriteRemoteCharacteristic(
base::span<const uint8_t> value,
base::OnceClosure callback,
ErrorCallback error_callback) {
if (!IsWritable()) {
DVLOG(1) << *this << ": Characteristic not writable.";
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(error_callback),
BluetoothGattService::GattErrorCode::kNotPermitted));
return;
}
if (destructor_called_ || HasPendingRead() || HasPendingWrite()) {
DVLOG(1) << *this << ": Characteristic write already in progress.";
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(error_callback),
BluetoothGattService::GattErrorCode::kInProgress));
return;
}
DVLOG(1) << *this << ": Write characteristic.";
write_characteristic_value_callbacks_ =
std::make_pair(std::move(callback), std::move(error_callback));
NSData* nsdata_value = [[NSData alloc] initWithBytes:value.data()
length:value.size()];
CBCharacteristicWriteType write_type = GetCBWriteType();
[GetCBPeripheral() writeValue:nsdata_value
forCharacteristic:cb_characteristic_
type:write_type];
if (write_type == CBCharacteristicWriteWithoutResponse) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&BluetoothRemoteGattCharacteristicMac::DidWriteValue,
weak_ptr_factory_.GetWeakPtr(), nil));
}
}
void BluetoothRemoteGattCharacteristicMac::SubscribeToNotifications(
BluetoothRemoteGattDescriptor* ccc_descriptor,
base::OnceClosure callback,
ErrorCallback error_callback) {
DVLOG(1) << *this << ": Subscribe to characteristic.";
DCHECK(subscribe_to_notification_callbacks_.first.is_null());
DCHECK(subscribe_to_notification_callbacks_.second.is_null());
DCHECK(unsubscribe_from_notification_callbacks_.first.is_null());
DCHECK(unsubscribe_from_notification_callbacks_.second.is_null());
subscribe_to_notification_callbacks_ =
std::make_pair(std::move(callback), std::move(error_callback));
[GetCBPeripheral() setNotifyValue:YES forCharacteristic:cb_characteristic_];
}
void BluetoothRemoteGattCharacteristicMac::UnsubscribeFromNotifications(
BluetoothRemoteGattDescriptor* ccc_descriptor,
base::OnceClosure callback,
ErrorCallback error_callback) {
DVLOG(1) << *this << ": Unsubscribe from characteristic.";
DCHECK(subscribe_to_notification_callbacks_.first.is_null());
DCHECK(subscribe_to_notification_callbacks_.second.is_null());
DCHECK(unsubscribe_from_notification_callbacks_.first.is_null());
DCHECK(unsubscribe_from_notification_callbacks_.second.is_null());
unsubscribe_from_notification_callbacks_ =
std::make_pair(std::move(callback), std::move(error_callback));
[GetCBPeripheral() setNotifyValue:NO forCharacteristic:cb_characteristic_];
}
void BluetoothRemoteGattCharacteristicMac::DiscoverDescriptors() {
DVLOG(1) << *this << ": Discover descriptors.";
is_discovery_complete_ = false;
++discovery_pending_count_;
[GetCBPeripheral() discoverDescriptorsForCharacteristic:cb_characteristic_];
}
void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) {
CHECK_EQ(GetCBPeripheral().state, CBPeripheralStateConnected);
// This method is called when the characteristic is read and when a
// notification is received.
if (HasPendingRead()) {
ValueCallback read_callback =
std::move(read_characteristic_value_callback_);
if (error) {
BluetoothGattService::GattErrorCode error_code =
BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
DVLOG(1) << *this
<< ": Bluetooth error while reading for characteristic, domain: "
<< BluetoothLowEnergyAdapterApple::String(error)
<< ", error code: " << static_cast<int>(error_code);
std::move(read_callback)
.Run(error_code,
/*value=*/std::vector<uint8_t>());
return;
}
DVLOG(1) << *this << ": Read request arrived.";
UpdateValue();
std::move(read_callback).Run(/*error_code=*/std::nullopt, value_);
} else if (IsNotifying()) {
DVLOG(1) << *this << ": Notification arrived.";
UpdateValue();
gatt_service_->GetLowEnergyAdapter()->NotifyGattCharacteristicValueChanged(
this, value_);
} else {
// In case of buggy device, nothing should be done if receiving extra
// read confirmation.
DVLOG(1)
<< *this
<< ": Characteristic value updated while having no pending read nor "
"notification.";
}
}
void BluetoothRemoteGattCharacteristicMac::UpdateValue() {
NSData* nsdata_value = [cb_characteristic_ value];
const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes);
value_.assign(buffer, buffer + nsdata_value.length);
}
void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) {
// We could have called cancelPeripheralConnection, which causes
// [CBPeripheral state] to be CBPeripheralStateDisconnected, before or during
// a write without response callback so we flush all pending writes.
// TODO(crbug.com/41321574): Remove once we can avoid calling DidWriteValue
// when we disconnect before or during a write without response call.
if (HasPendingWrite() &&
GetCBPeripheral().state != CBPeripheralStateConnected) {
std::pair<base::OnceClosure, ErrorCallback> callbacks;
callbacks.swap(write_characteristic_value_callbacks_);
std::move(callbacks.second)
.Run(BluetoothGattService::GattErrorCode::kFailed);
return;
}
CHECK_EQ(GetCBPeripheral().state, CBPeripheralStateConnected);
if (!HasPendingWrite()) {
// In case of buggy device, nothing should be done if receiving extra
// write confirmation.
DVLOG(1) << *this
<< ": Write notification while no write operation pending.";
return;
}
std::pair<base::OnceClosure, ErrorCallback> callbacks;
callbacks.swap(write_characteristic_value_callbacks_);
if (error) {
BluetoothGattService::GattErrorCode error_code =
BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
DVLOG(1) << *this
<< ": Bluetooth error while writing for characteristic, error: "
<< BluetoothLowEnergyAdapterApple::String(error)
<< ", error code: " << static_cast<int>(error_code);
std::move(callbacks.second).Run(error_code);
return;
}
DVLOG(1) << *this << ": Write value succeeded.";
std::move(callbacks.first).Run();
}
void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState(
NSError* error) {
PendingNotifyCallbacks reentrant_safe_callbacks;
if (!subscribe_to_notification_callbacks_.first.is_null()) {
DCHECK([GetCBCharacteristic() isNotifying] || error);
reentrant_safe_callbacks.swap(subscribe_to_notification_callbacks_);
} else if (!unsubscribe_from_notification_callbacks_.first.is_null()) {
DCHECK(![GetCBCharacteristic() isNotifying] || error);
reentrant_safe_callbacks.swap(unsubscribe_from_notification_callbacks_);
} else {
DVLOG(1) << *this << ": No pending notification update for characteristic.";
return;
}
if (error) {
BluetoothGattService::GattErrorCode error_code =
BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
DVLOG(1) << *this
<< ": Bluetooth error while modifying notification state for "
"characteristic, error: "
<< BluetoothLowEnergyAdapterApple::String(error)
<< ", error code: " << static_cast<int>(error_code);
std::move(reentrant_safe_callbacks.second).Run(error_code);
return;
}
std::move(reentrant_safe_callbacks.first).Run();
}
void BluetoothRemoteGattCharacteristicMac::DidDiscoverDescriptors() {
if (discovery_pending_count_ == 0) {
// This should never happen, just in case it happens with a device, this
// notification should be ignored.
DVLOG(1) << *this
<< ": Unmatch DiscoverDescriptors and DidDiscoverDescriptors.";
return;
}
DVLOG(1) << *this << ": Did discover descriptors.";
--discovery_pending_count_;
std::unordered_set<std::string> descriptor_identifier_to_remove;
for (const auto& iter : descriptors_) {
descriptor_identifier_to_remove.insert(iter.first);
}
for (CBDescriptor* cb_descriptor in [cb_characteristic_ descriptors]) {
BluetoothRemoteGattDescriptorMac* gatt_descriptor_mac =
GetBluetoothRemoteGattDescriptorMac(cb_descriptor);
if (gatt_descriptor_mac) {
DVLOG(1) << *gatt_descriptor_mac << ": Known descriptor.";
const std::string& identifier = gatt_descriptor_mac->GetIdentifier();
descriptor_identifier_to_remove.erase(identifier);
continue;
}
gatt_descriptor_mac =
new BluetoothRemoteGattDescriptorMac(this, cb_descriptor);
bool result = AddDescriptor(base::WrapUnique(gatt_descriptor_mac));
DCHECK(result);
GetLowEnergyAdapter()->NotifyGattDescriptorAdded(gatt_descriptor_mac);
DVLOG(1) << *gatt_descriptor_mac << ": New descriptor.";
}
for (const std::string& identifier : descriptor_identifier_to_remove) {
auto iter = descriptors_.find(identifier);
auto pair = std::move(*iter);
DVLOG(1) << static_cast<BluetoothRemoteGattDescriptorMac&>(*pair.second)
<< ": Removed descriptor.";
descriptors_.erase(iter);
GetLowEnergyAdapter()->NotifyGattDescriptorRemoved(pair.second.get());
}
is_discovery_complete_ = discovery_pending_count_ == 0;
}
bool BluetoothRemoteGattCharacteristicMac::IsReadable() const {
return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ;
}
bool BluetoothRemoteGattCharacteristicMac::IsWritable() const {
BluetoothGattCharacteristic::Properties properties = GetProperties();
return (properties & BluetoothGattCharacteristic::PROPERTY_WRITE) ||
(properties & PROPERTY_WRITE_WITHOUT_RESPONSE);
}
bool BluetoothRemoteGattCharacteristicMac::SupportsNotificationsOrIndications()
const {
BluetoothGattCharacteristic::Properties properties = GetProperties();
return (properties & PROPERTY_NOTIFY) || (properties & PROPERTY_INDICATE);
}
CBCharacteristicWriteType BluetoothRemoteGattCharacteristicMac::GetCBWriteType()
const {
return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE)
? CBCharacteristicWriteWithResponse
: CBCharacteristicWriteWithoutResponse;
}
CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic()
const {
return cb_characteristic_;
}
BluetoothLowEnergyAdapterApple*
BluetoothRemoteGattCharacteristicMac::GetLowEnergyAdapter() const {
return gatt_service_->GetLowEnergyAdapter();
}
CBPeripheral* BluetoothRemoteGattCharacteristicMac::GetCBPeripheral() const {
return gatt_service_->GetCBPeripheral();
}
bool BluetoothRemoteGattCharacteristicMac::IsDiscoveryComplete() const {
return is_discovery_complete_;
}
BluetoothRemoteGattDescriptorMac*
BluetoothRemoteGattCharacteristicMac::GetBluetoothRemoteGattDescriptorMac(
CBDescriptor* cb_descriptor) const {
for (const auto& pair : descriptors_) {
auto* descriptor_mac =
static_cast<BluetoothRemoteGattDescriptorMac*>(pair.second.get());
if (descriptor_mac->GetCBDescriptor() == cb_descriptor)
return descriptor_mac;
}
return nullptr;
}
DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<(
std::ostream& out,
const BluetoothRemoteGattCharacteristicMac& characteristic) {
const BluetoothRemoteGattServiceMac* service_mac =
static_cast<const BluetoothRemoteGattServiceMac*>(
characteristic.GetService());
return out << "<BluetoothRemoteGattCharacteristicMac "
<< characteristic.GetUUID().canonical_value() << "/"
<< &characteristic
<< ", service: " << service_mac->GetUUID().canonical_value() << "/"
<< service_mac << ">";
}
} // namespace device.
|