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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/bluetooth_low_energy_device_mac.h"
#import <CoreFoundation/CoreFoundation.h>
#include <stddef.h>
#include <algorithm>
#include "base/apple/scoped_cftyperef.h"
#include "base/logging.h"
#include "base/mac/mac_util.h"
#include "base/memory/ptr_util.h"
#include "base/notimplemented.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/sys_string_conversions.h"
#include "crypto/hash.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_low_energy_adapter_apple.h"
#include "device/bluetooth/bluetooth_low_energy_peripheral_delegate.h"
#include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h"
#include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h"
#include "device/bluetooth/bluetooth_remote_gatt_service_mac.h"
#include "device/bluetooth/public/cpp/bluetooth_address.h"
namespace device {
BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac(
BluetoothAdapter* adapter,
CBPeripheral* peripheral)
: BluetoothDeviceMac(adapter),
peripheral_(peripheral),
connected_(false),
discovery_pending_count_(0) {
DCHECK(peripheral_);
peripheral_delegate_ = [[BluetoothLowEnergyPeripheralDelegate alloc]
initWithBluetoothLowEnergyDeviceMac:this];
[peripheral_ setDelegate:peripheral_delegate_];
identifier_ = GetPeripheralIdentifier(peripheral);
hash_address_ = GetPeripheralHashAddress(peripheral);
UpdateTimestamp();
}
BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() {
if (IsGattConnected()) {
GetLowEnergyAdapter()->DisconnectGatt(this);
}
[peripheral_ setDelegate:nil];
}
std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const {
return identifier_;
}
uint32_t BluetoothLowEnergyDeviceMac::GetBluetoothClass() const {
return 0x1F00; // Unspecified Device Class
}
std::string BluetoothLowEnergyDeviceMac::GetAddress() const {
return hash_address_;
}
BluetoothDevice::AddressType BluetoothLowEnergyDeviceMac::GetAddressType()
const {
NOTIMPLEMENTED();
return ADDR_TYPE_UNKNOWN;
}
BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource()
const {
return VENDOR_ID_UNKNOWN;
}
uint16_t BluetoothLowEnergyDeviceMac::GetVendorID() const {
return 0;
}
uint16_t BluetoothLowEnergyDeviceMac::GetProductID() const {
return 0;
}
uint16_t BluetoothLowEnergyDeviceMac::GetDeviceID() const {
return 0;
}
uint16_t BluetoothLowEnergyDeviceMac::GetAppearance() const {
// TODO(crbug.com/41240161): Implementing GetAppearance()
// on mac, win, and android platforms for chrome
NOTIMPLEMENTED();
return 0;
}
std::optional<std::string> BluetoothLowEnergyDeviceMac::GetName() const {
if ([peripheral_ name])
return base::SysNSStringToUTF8([peripheral_ name]);
return std::nullopt;
}
bool BluetoothLowEnergyDeviceMac::IsPaired() const {
return GetLowEnergyAdapter()->IsBluetoothLowEnergyDeviceSystemPaired(
identifier_);
}
bool BluetoothLowEnergyDeviceMac::IsConnected() const {
return IsGattConnected();
}
bool BluetoothLowEnergyDeviceMac::IsGattConnected() const {
// |connected_| can be false while |[peripheral_ state]| is
// |CBPeripheralStateConnected|. This happens
// BluetoothLowEnergyAdapterApple::DidConnectPeripheral() is called and
// BluetoothLowEnergyDeviceMac::DidConnectGatt() has not been called yet.
return connected_;
}
bool BluetoothLowEnergyDeviceMac::IsConnectable() const {
// Only available for Chrome OS.
NOTIMPLEMENTED();
return false;
}
bool BluetoothLowEnergyDeviceMac::IsConnecting() const {
return ([peripheral_ state] == CBPeripheralStateConnecting);
}
bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const {
return false;
}
bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const {
return false;
}
bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const {
return false;
}
void BluetoothLowEnergyDeviceMac::GetConnectionInfo(
ConnectionInfoCallback callback) {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::SetConnectionLatency(
ConnectionLatency connection_latency,
base::OnceClosure callback,
ErrorCallback error_callback) {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::Connect(PairingDelegate* pairing_delegate,
ConnectCallback callback) {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::SetPasskey(uint32_t passkey) {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::ConfirmPairing() {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::RejectPairing() {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::CancelPairing() {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::Disconnect(base::OnceClosure callback,
ErrorCallback error_callback) {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::Forget(base::OnceClosure callback,
ErrorCallback error_callback) {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::ConnectToService(
const BluetoothUUID& uuid,
ConnectToServiceCallback callback,
ConnectToServiceErrorCallback error_callback) {
NOTIMPLEMENTED();
}
void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely(
const BluetoothUUID& uuid,
ConnectToServiceCallback callback,
ConnectToServiceErrorCallback error_callback) {
NOTIMPLEMENTED();
}
bool BluetoothLowEnergyDeviceMac::IsLowEnergyDevice() {
return true;
}
void BluetoothLowEnergyDeviceMac::CreateGattConnectionImpl(
std::optional<BluetoothUUID> serivce_uuid) {
if (!IsGattConnected()) {
GetLowEnergyAdapter()->CreateGattConnection(this);
}
}
void BluetoothLowEnergyDeviceMac::DisconnectGatt() {
GetLowEnergyAdapter()->DisconnectGatt(this);
}
void BluetoothLowEnergyDeviceMac::DidDiscoverPrimaryServices(NSError* error) {
--discovery_pending_count_;
if (discovery_pending_count_ < 0) {
// This should never happen, just in case it happens with a device,
// discovery_pending_count_ is set back to 0.
DVLOG(1) << *this
<< ": BluetoothLowEnergyDeviceMac::discovery_pending_count_ "
<< discovery_pending_count_;
discovery_pending_count_ = 0;
return;
}
if (error) {
// TODO(http://crbug.com/609320): Need to pass the error.
// TODO(http://crbug.com/609844): Decide what to do if discover failed
// a device services.
DVLOG(1) << *this << ": Can't discover primary services: "
<< BluetoothLowEnergyAdapterApple::String(error);
return;
}
if (!IsGattConnected()) {
// Don't create services if the device disconnected.
DVLOG(1) << *this << ": DidDiscoverPrimaryServices, gatt not connected.";
return;
}
DVLOG(1) << *this << ": DidDiscoverPrimaryServices, pending count: "
<< discovery_pending_count_;
for (CBService* cb_service in GetPeripheral().services) {
BluetoothRemoteGattServiceMac* gatt_service =
GetBluetoothRemoteGattServiceMac(cb_service);
if (!gatt_service) {
gatt_service = new BluetoothRemoteGattServiceMac(this, cb_service,
true /* is_primary */);
auto result_iter = gatt_services_.insert(std::make_pair(
gatt_service->GetIdentifier(), base::WrapUnique(gatt_service)));
DCHECK(result_iter.second);
DVLOG(1) << *gatt_service << ": New service.";
adapter_->NotifyGattServiceAdded(gatt_service);
} else {
DVLOG(1) << *gatt_service << ": Known service.";
}
}
if (discovery_pending_count_ == 0) {
for (auto& it : gatt_services_) {
BluetoothRemoteGattService* gatt_service = it.second.get();
BluetoothRemoteGattServiceMac* gatt_service_mac =
static_cast<BluetoothRemoteGattServiceMac*>(gatt_service);
gatt_service_mac->DiscoverCharacteristics();
}
SendNotificationIfDiscoveryComplete();
}
}
void BluetoothLowEnergyDeviceMac::DidDiscoverCharacteristics(
CBService* cb_service,
NSError* error) {
if (error) {
// TODO(http://crbug.com/609320): Need to pass the error.
// TODO(http://crbug.com/609844): Decide what to do if discover failed
DVLOG(1) << *this << ": Can't discover characteristics: "
<< BluetoothLowEnergyAdapterApple::String(error);
return;
}
if (!IsGattConnected()) {
DVLOG(1) << *this << ": DidDiscoverCharacteristics, gatt disconnected.";
// Don't create characteristics if the device disconnected.
return;
}
if (IsGattServicesDiscoveryComplete()) {
// This should never happen, just in case it happens with a device, this
// notification should be ignored.
DVLOG(1) << *this
<< ": Discovery complete, ignoring DidDiscoverCharacteristics.";
return;
}
BluetoothRemoteGattServiceMac* gatt_service =
GetBluetoothRemoteGattServiceMac(cb_service);
DCHECK(gatt_service);
gatt_service->DidDiscoverCharacteristics();
SendNotificationIfDiscoveryComplete();
}
void BluetoothLowEnergyDeviceMac::DidModifyServices(
NSArray* invalidatedServices) {
DVLOG(1) << *this << ": DidModifyServices: "
<< " invalidated services "
<< base::SysNSStringToUTF8([invalidatedServices description]);
for (CBService* cb_service in invalidatedServices) {
BluetoothRemoteGattServiceMac* gatt_service =
GetBluetoothRemoteGattServiceMac(cb_service);
DCHECK(gatt_service);
DVLOG(1) << gatt_service->GetUUID().canonical_value();
std::unique_ptr<BluetoothRemoteGattService> scoped_service =
std::move(gatt_services_[gatt_service->GetIdentifier()]);
gatt_services_.erase(gatt_service->GetIdentifier());
adapter_->NotifyGattServiceRemoved(scoped_service.get());
}
device_uuids_.ClearServiceUUIDs();
SetGattServicesDiscoveryComplete(false);
adapter_->NotifyDeviceChanged(this);
DiscoverPrimaryServices();
}
void BluetoothLowEnergyDeviceMac::DidUpdateValue(
CBCharacteristic* characteristic,
NSError* error) {
BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac =
GetBluetoothRemoteGattCharacteristicMac(characteristic);
DCHECK(gatt_characteristic_mac);
gatt_characteristic_mac->DidUpdateValue(error);
}
void BluetoothLowEnergyDeviceMac::DidWriteValue(
CBCharacteristic* characteristic,
NSError* error) {
BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac =
GetBluetoothRemoteGattCharacteristicMac(characteristic);
DCHECK(gatt_characteristic_mac);
gatt_characteristic_mac->DidWriteValue(error);
}
void BluetoothLowEnergyDeviceMac::DidUpdateNotificationState(
CBCharacteristic* characteristic,
NSError* error) {
BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac =
GetBluetoothRemoteGattCharacteristicMac(characteristic);
DCHECK(gatt_characteristic_mac);
gatt_characteristic_mac->DidUpdateNotificationState(error);
}
void BluetoothLowEnergyDeviceMac::DidDiscoverDescriptors(
CBCharacteristic* cb_characteristic,
NSError* error) {
if (error) {
// TODO(http://crbug.com/609320): Need to pass the error.
// TODO(http://crbug.com/609844): Decide what to do if discover failed
DVLOG(1) << *this << ": Can't discover descriptors: "
<< BluetoothLowEnergyAdapterApple::String(error);
return;
}
if (!IsGattConnected()) {
DVLOG(1) << *this << ": DidDiscoverDescriptors, disconnected.";
// Don't discover descriptors if the device disconnected.
return;
}
if (IsGattServicesDiscoveryComplete()) {
// This should never happen, just in case it happens with a device, this
// notification should be ignored.
DVLOG(1) << *this
<< ": Discovery complete, ignoring DidDiscoverDescriptors.";
return;
}
BluetoothRemoteGattServiceMac* gatt_service =
GetBluetoothRemoteGattServiceMac(cb_characteristic.service);
DCHECK(gatt_service);
gatt_service->DidDiscoverDescriptors(cb_characteristic);
SendNotificationIfDiscoveryComplete();
}
void BluetoothLowEnergyDeviceMac::DidUpdateValueForDescriptor(
CBDescriptor* cb_descriptor,
NSError* error) {
BluetoothRemoteGattDescriptorMac* gatt_descriptor =
GetBluetoothRemoteGattDescriptorMac(cb_descriptor);
DCHECK(gatt_descriptor);
gatt_descriptor->DidUpdateValueForDescriptor(error);
}
void BluetoothLowEnergyDeviceMac::DidWriteValueForDescriptor(
CBDescriptor* cb_descriptor,
NSError* error) {
BluetoothRemoteGattDescriptorMac* gatt_descriptor =
GetBluetoothRemoteGattDescriptorMac(cb_descriptor);
DCHECK(gatt_descriptor);
gatt_descriptor->DidWriteValueForDescriptor(error);
}
// static
std::string BluetoothLowEnergyDeviceMac::GetPeripheralIdentifier(
CBPeripheral* peripheral) {
NSUUID* uuid = [peripheral identifier];
NSString* uuidString = [uuid UUIDString];
return base::SysNSStringToUTF8(uuidString);
}
// static
std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(
CBPeripheral* peripheral) {
return GetPeripheralHashAddress(GetPeripheralIdentifier(peripheral));
}
// static
std::string BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(
std::string_view device_identifier) {
const size_t kCanonicalAddressNumberOfBytes = 6;
const auto hash = crypto::hash::Sha256(device_identifier);
return CanonicalizeBluetoothAddress(
base::span(hash).first<kCanonicalAddressNumberOfBytes>());
}
void BluetoothLowEnergyDeviceMac::DidConnectPeripheral() {
DVLOG(1) << *this << ": GATT connected.";
if (!connected_) {
connected_ = true;
DidConnectGatt(/*error_code=*/std::nullopt);
DiscoverPrimaryServices();
} else {
// -[<CBCentralManagerDelegate> centralManager:didConnectPeripheral:] can be
// called twice because of a macOS bug. This second call should be ignored.
// See crbug.com/681414.
DVLOG(1) << *this << ": Already connected, ignoring event.";
}
}
void BluetoothLowEnergyDeviceMac::DiscoverPrimaryServices() {
DVLOG(1) << *this << ": DiscoverPrimaryServices, pending count "
<< discovery_pending_count_;
++discovery_pending_count_;
[GetPeripheral() discoverServices:nil];
}
void BluetoothLowEnergyDeviceMac::SendNotificationIfDiscoveryComplete() {
DCHECK(!IsGattServicesDiscoveryComplete());
// Notify when all services have been discovered.
bool discovery_complete =
discovery_pending_count_ == 0 &&
std::ranges::all_of(
gatt_services_, [](GattServiceMap::value_type& pair) {
BluetoothRemoteGattService* gatt_service = pair.second.get();
return static_cast<BluetoothRemoteGattServiceMac*>(gatt_service)
->IsDiscoveryComplete();
});
if (discovery_complete) {
DVLOG(1) << *this << ": Discovery complete.";
device_uuids_.ReplaceServiceUUIDs(gatt_services_);
SetGattServicesDiscoveryComplete(true);
adapter_->NotifyGattServicesDiscovered(this);
adapter_->NotifyDeviceChanged(this);
}
}
BluetoothLowEnergyAdapterApple*
BluetoothLowEnergyDeviceMac::GetLowEnergyAdapter() {
return static_cast<BluetoothLowEnergyAdapterApple*>(this->adapter_);
}
BluetoothLowEnergyAdapterApple*
BluetoothLowEnergyDeviceMac::GetLowEnergyAdapter() const {
return static_cast<BluetoothLowEnergyAdapterApple*>(this->adapter_);
}
CBPeripheral* BluetoothLowEnergyDeviceMac::GetPeripheral() {
return peripheral_;
}
BluetoothRemoteGattServiceMac*
BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattServiceMac(
CBService* cb_service) const {
for (const auto& it : gatt_services_) {
BluetoothRemoteGattService* gatt_service = it.second.get();
BluetoothRemoteGattServiceMac* gatt_service_mac =
static_cast<BluetoothRemoteGattServiceMac*>(gatt_service);
if (gatt_service_mac->GetService() == cb_service)
return gatt_service_mac;
}
return nullptr;
}
BluetoothRemoteGattCharacteristicMac*
BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattCharacteristicMac(
CBCharacteristic* cb_characteristic) const {
CBService* cb_service = [cb_characteristic service];
BluetoothRemoteGattServiceMac* gatt_service_mac =
GetBluetoothRemoteGattServiceMac(cb_service);
if (!gatt_service_mac) {
return nullptr;
}
return gatt_service_mac->GetBluetoothRemoteGattCharacteristicMac(
cb_characteristic);
}
BluetoothRemoteGattDescriptorMac*
BluetoothLowEnergyDeviceMac::GetBluetoothRemoteGattDescriptorMac(
CBDescriptor* cb_descriptor) const {
CBService* cb_service = [[cb_descriptor characteristic] service];
BluetoothRemoteGattServiceMac* gatt_service_mac =
GetBluetoothRemoteGattServiceMac(cb_service);
if (!gatt_service_mac) {
return nullptr;
}
return gatt_service_mac->GetBluetoothRemoteGattDescriptorMac(cb_descriptor);
}
void BluetoothLowEnergyDeviceMac::DidDisconnectPeripheral(NSError* error) {
connected_ = false;
DVLOG(1) << *this << ": Disconnected from peripheral.";
if (error) {
DVLOG(1) << *this << ": Bluetooth error: "
<< BluetoothLowEnergyAdapterApple::String(error);
}
SetGattServicesDiscoveryComplete(false);
// Removing all services at once to ensure that calling GetGattService on
// removed service in GattServiceRemoved returns null.
GattServiceMap gatt_services_swapped;
gatt_services_swapped.swap(gatt_services_);
gatt_services_swapped.clear();
device_uuids_.ClearServiceUUIDs();
// There are two cases in which this function will be called:
// 1. When the connection to the device breaks (either because
// we closed it or the device closed it).
// 2. When we cancel a pending connection request.
if (create_gatt_connection_callbacks_.empty()) {
// If there are no pending callbacks then the connection broke (#1).
DidDisconnectGatt();
return;
}
// Else we canceled the connection request (#2).
// TODO(http://crbug.com/585897): Need to pass the error.
DidConnectGatt(BluetoothDevice::ConnectErrorCode::ERROR_FAILED);
}
std::ostream& operator<<(std::ostream& out,
const BluetoothLowEnergyDeviceMac& device) {
// TODO(crbug.com/40511884): Should use
// BluetoothLowEnergyDeviceMac::GetNameForDisplay() instead.
std::optional<std::string> name = device.GetName();
const char* is_gatt_connected =
device.IsGattConnected() ? "GATT connected" : "GATT disconnected";
return out << "<BluetoothLowEnergyDeviceMac " << device.GetAddress() << "/"
<< &device << ", " << is_gatt_connected << ", \""
<< name.value_or("Unnamed device") << "\">";
}
} // namespace device
|