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 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
|
// Copyright 2021 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/floss/bluetooth_device_floss.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/notimplemented.h"
#include "base/notreached.h"
#include "base/task/sequenced_task_runner.h"
#include "components/device_event_log/device_event_log.h"
#include "dbus/bus.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_gatt_connection.h"
#include "device/bluetooth/floss/bluetooth_adapter_floss.h"
#include "device/bluetooth/floss/bluetooth_gatt_connection_floss.h"
#include "device/bluetooth/floss/bluetooth_remote_gatt_service_floss.h"
#include "device/bluetooth/floss/bluetooth_socket_floss.h"
#include "device/bluetooth/floss/floss_dbus_client.h"
#include "device/bluetooth/floss/floss_dbus_manager.h"
#include "device/bluetooth/floss/floss_gatt_manager_client.h"
#include "device/bluetooth/floss/floss_socket_manager.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "device/bluetooth/chromeos/bluetooth_utils.h"
#endif
namespace floss {
namespace {
// Connection intervals for LE connections.
// The unit for connection interval values are in multiples of 1.25ms.
const int32_t kMinConnectionIntervalLow = 6;
const int32_t kMaxConnectionIntervalLow = 6;
const int32_t kMinConnectionIntervalMedium = 40;
const int32_t kMaxConnectionIntervalMedium = 56;
const int32_t kMinConnectionIntervalHigh = 80;
const int32_t kMaxConnectionIntervalHigh = 100;
// Default connection latency for LE connections.
const int32_t kDefaultConnectionLatency = 0;
// Link supervision timeout for LE connections.
const int32_t kDefaultConnectionTimeout = 2000;
// Maximum MTU size that can be requested by Android.
const int32_t kMaxMtuSize = 517;
// Timeout for connection response after Connect() method is called.
constexpr base::TimeDelta kDefaultConnectTimeout = base::Seconds(10);
void OnRemoveBond(base::OnceClosure callback, DBusResult<bool> ret) {
if (!ret.has_value()) {
BLUETOOTH_LOG(ERROR) << "Failed to remove bond: " << ret.error();
} else if (!*ret) {
BLUETOOTH_LOG(ERROR) << "RemoveBond returned failure";
}
#if BUILDFLAG(IS_CHROMEOS)
bool success = ret.has_value() && *ret;
device::RecordForgetResult(success ? device::ForgetResult::kSuccess
: device::ForgetResult::kFailure);
#endif
std::move(callback).Run();
}
} // namespace
using AddressType = device::BluetoothDevice::AddressType;
using VendorIDSource = device::BluetoothDevice::VendorIDSource;
uint32_t BluetoothDeviceFloss::GetBluetoothClass() const {
return cod_;
}
device::BluetoothTransport BluetoothDeviceFloss::GetType() const {
return transport_;
}
std::string BluetoothDeviceFloss::GetAddress() const {
return address_;
}
AddressType BluetoothDeviceFloss::GetAddressType() const {
return address_type_;
}
VendorIDSource BluetoothDeviceFloss::GetVendorIDSource() const {
return static_cast<VendorIDSource>(vpi_.vendorIdSrc);
}
uint16_t BluetoothDeviceFloss::GetVendorID() const {
return vpi_.vendorId;
}
uint16_t BluetoothDeviceFloss::GetProductID() const {
return vpi_.productId;
}
uint16_t BluetoothDeviceFloss::GetDeviceID() const {
return vpi_.version;
}
uint16_t BluetoothDeviceFloss::GetAppearance() const {
return appearance_;
}
std::optional<std::string> BluetoothDeviceFloss::GetName() const {
if (name_.length() == 0)
return std::nullopt;
return name_;
}
bool BluetoothDeviceFloss::IsPaired() const {
return IsBondedImpl() ||
FlossAdapterClient::IsConnectionPaired(connection_state_);
}
#if BUILDFLAG(IS_CHROMEOS)
bool BluetoothDeviceFloss::IsBonded() const {
return IsBondedImpl();
}
#endif // BUILDFLAG(IS_CHROMEOS)
bool BluetoothDeviceFloss::IsConnected() const {
return is_acl_connected_;
}
bool BluetoothDeviceFloss::IsGattConnected() const {
return gatt_connecting_state_ == GattConnectingState::kGattConnected;
}
bool BluetoothDeviceFloss::IsConnectable() const {
// Mimic current BlueZ behavior that Non-HID is connectable
switch (GetDeviceType()) {
case device::BluetoothDeviceType::PERIPHERAL:
case device::BluetoothDeviceType::JOYSTICK:
case device::BluetoothDeviceType::KEYBOARD:
case device::BluetoothDeviceType::MOUSE:
case device::BluetoothDeviceType::KEYBOARD_MOUSE_COMBO:
return false;
default:
return true;
}
}
bool BluetoothDeviceFloss::IsConnecting() const {
return (connecting_state_ == ConnectingState::kACLConnecting) ||
(connecting_state_ == ConnectingState::kProfilesConnecting) ||
(gatt_connecting_state_ == GattConnectingState::kGattConnecting);
}
device::BluetoothDevice::UUIDSet BluetoothDeviceFloss::GetUUIDs() const {
return device_uuids_.GetUUIDs();
}
std::optional<int8_t> BluetoothDeviceFloss::GetInquiryTxPower() const {
NOTIMPLEMENTED();
return std::nullopt;
}
bool BluetoothDeviceFloss::ExpectingPinCode() const {
if (!pairing_)
return false;
return pairing_->pairing_expectation() ==
BluetoothPairingFloss::PairingExpectation::kPinCode;
}
bool BluetoothDeviceFloss::ExpectingPasskey() const {
if (!pairing_)
return false;
return pairing_->pairing_expectation() ==
BluetoothPairingFloss::PairingExpectation::kPasskey;
}
bool BluetoothDeviceFloss::ExpectingConfirmation() const {
if (!pairing_)
return false;
return pairing_->pairing_expectation() ==
BluetoothPairingFloss::PairingExpectation::kConfirmation;
}
void BluetoothDeviceFloss::GetConnectionInfo(ConnectionInfoCallback callback) {
// TODO(b/255650738): Floss doesn't currently provide max_transmit_power.
std::move(callback).Run(ConnectionInfo(inquiry_rssi_.value_or(0),
inquiry_tx_power_.value_or(0),
/*max_transmit_power=*/0));
}
void BluetoothDeviceFloss::SetConnectionLatency(
ConnectionLatency connection_latency,
base::OnceClosure callback,
ErrorCallback error_callback) {
int32_t min_connection_interval = kMinConnectionIntervalMedium;
int32_t max_connection_interval = kMaxConnectionIntervalMedium;
switch (connection_latency) {
case ConnectionLatency::CONNECTION_LATENCY_LOW:
min_connection_interval = kMinConnectionIntervalLow;
max_connection_interval = kMaxConnectionIntervalLow;
break;
case ConnectionLatency::CONNECTION_LATENCY_MEDIUM:
min_connection_interval = kMinConnectionIntervalMedium;
max_connection_interval = kMaxConnectionIntervalMedium;
break;
case ConnectionLatency::CONNECTION_LATENCY_HIGH:
min_connection_interval = kMinConnectionIntervalHigh;
max_connection_interval = kMaxConnectionIntervalHigh;
break;
default:
NOTREACHED();
}
BLUETOOTH_LOG(EVENT) << "Setting LE connection parameters: min="
<< min_connection_interval
<< ", max=" << max_connection_interval;
FlossDBusManager::Get()->GetGattManagerClient()->UpdateConnectionParameters(
base::BindOnce(&BluetoothDeviceFloss::OnSetConnectionLatency,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)),
GetAddress(), min_connection_interval, max_connection_interval,
kDefaultConnectionLatency, kDefaultConnectionTimeout,
/*min_ce_len=*/min_connection_interval * 2,
/*max_ce_len=*/max_connection_interval * 2);
}
void BluetoothDeviceFloss::OnSetConnectionLatency(base::OnceClosure callback,
ErrorCallback error_callback,
DBusResult<Void> ret) {
if (!ret.has_value()) {
std::move(error_callback).Run();
return;
}
// If we already had a pending call, fail it.
if (pending_set_connection_latency_.has_value()) {
auto& [pending_cb, pending_error_cb] =
pending_set_connection_latency_.value();
std::move(pending_error_cb).Run();
pending_set_connection_latency_ = std::nullopt;
}
pending_set_connection_latency_ =
std::make_pair(std::move(callback), std::move(error_callback));
}
void BluetoothDeviceFloss::Connect(
device::BluetoothDevice::PairingDelegate* pairing_delegate,
ConnectCallback callback) {
ConnectWithTransport(pairing_delegate, std::move(callback),
FlossAdapterClient::BluetoothTransport::kAuto);
}
void BluetoothDeviceFloss::ConnectWithTransport(
device::BluetoothDevice::PairingDelegate* pairing_delegate,
ConnectCallback callback,
FlossAdapterClient::BluetoothTransport transport) {
BLUETOOTH_LOG(EVENT) << "Connecting to " << address_;
if ((connecting_state_ == ConnectingState::kACLConnecting) ||
(connecting_state_ == ConnectingState::kProfilesConnecting) ||
(pending_callback_on_connect_profiles_ != std::nullopt)) {
std::move(callback).Run(
BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS);
return;
} else if (connecting_state_ == ConnectingState::kProfilesConnected) {
std::move(callback).Run(
BluetoothDevice::ConnectErrorCode::ERROR_ALREADY_CONNECTED);
return;
}
// To simulate BlueZ API behavior, we don't reply the callback as soon as
// Floss CreateBond API returns success, but rather we trigger the callback
// later after pairing is done and profiles are connected. In the event of
// immediate failure OnCreateBond will handle invoking the callback.
pending_callback_on_connect_profiles_ = std::move(callback);
if (IsPaired() || !pairing_delegate) {
// No need to pair, or unable to, skip straight to connection.
ConnectAllEnabledProfiles();
} else {
pairing_ = std::make_unique<BluetoothPairingFloss>(pairing_delegate);
if (FlossDBusManager::Get()->GetFlossApiVersion() >=
base::Version("0.4.0")) {
FlossDBusManager::Get()->GetAdapterClient()->CreateBond(
base::BindOnce(static_cast<void (BluetoothDeviceFloss::*)(
DBusResult<FlossDBusClient::BtifStatus>)>(
&BluetoothDeviceFloss::OnCreateBond),
weak_ptr_factory_.GetWeakPtr()),
AsFlossDeviceId(), transport);
} else {
FlossDBusManager::Get()->GetAdapterClient()->CreateBond(
base::BindOnce(
static_cast<void (BluetoothDeviceFloss::*)(DBusResult<bool>)>(
&BluetoothDeviceFloss::OnCreateBond),
weak_ptr_factory_.GetWeakPtr()),
AsFlossDeviceId(), transport);
}
}
}
void BluetoothDeviceFloss::OnCreateBond(DBusResult<bool> ret) {
if (ret.has_value() && !*ret) {
BLUETOOTH_LOG(ERROR) << "CreateBond returned failure";
TriggerConnectCallback(BluetoothDevice::ConnectErrorCode::ERROR_FAILED);
} else if (!ret.has_value()) {
BLUETOOTH_LOG(ERROR) << "Failed to create bond: " << ret.error();
TriggerConnectCallback(BluetoothDevice::ConnectErrorCode::ERROR_FAILED);
}
}
void BluetoothDeviceFloss::OnCreateBond(
DBusResult<FlossDBusClient::BtifStatus> ret) {
if (!ret.has_value()) {
BLUETOOTH_LOG(ERROR) << "Failed to create bond, D-Bus error: "
<< ret.error();
TriggerConnectCallback(BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN);
return;
}
if (*ret != FlossDBusClient::BtifStatus::kSuccess) {
BLUETOOTH_LOG(ERROR) << "Failed to create bond, status: "
<< static_cast<uint32_t>(*ret);
TriggerConnectCallback(FlossDBusClient::BtifStatusToConnectErrorCode(*ret));
return;
}
}
void BluetoothDeviceFloss::ConnectionIncomplete() {
UpdateConnectingState(
ConnectingState::kIdle,
BluetoothDevice::ConnectErrorCode::ERROR_NON_AUTH_TIMEOUT);
}
#if BUILDFLAG(IS_CHROMEOS)
void BluetoothDeviceFloss::ConnectClassic(
device::BluetoothDevice::PairingDelegate* pairing_delegate,
ConnectCallback callback) {
ConnectWithTransport(pairing_delegate, std::move(callback),
FlossAdapterClient::BluetoothTransport::kBrEdr);
}
#endif // BUILDFLAG(IS_CHROMEOS)
void BluetoothDeviceFloss::SetPinCode(const std::string& pincode) {
std::vector<uint8_t> pin(pincode.begin(), pincode.end());
FlossDBusManager::Get()->GetAdapterClient()->SetPin(
base::DoNothing(), AsFlossDeviceId(), /*accept=*/true, pin);
}
void BluetoothDeviceFloss::SetPasskey(uint32_t passkey) {
// No use case in Chrome OS.
NOTIMPLEMENTED();
}
void BluetoothDeviceFloss::ConfirmPairing() {
FlossDBusManager::Get()->GetAdapterClient()->SetPairingConfirmation(
base::DoNothing(), AsFlossDeviceId(), /*accept=*/true);
}
void BluetoothDeviceFloss::RejectPairing() {
FlossDBusManager::Get()->GetAdapterClient()->SetPairingConfirmation(
base::DoNothing(), AsFlossDeviceId(), /*accept=*/false);
}
void BluetoothDeviceFloss::CancelPairing() {
FlossDBusManager::Get()->GetAdapterClient()->CancelBondProcess(
base::DoNothing(), AsFlossDeviceId());
}
void BluetoothDeviceFloss::Disconnect(base::OnceClosure callback,
ErrorCallback error_callback) {
FlossDBusManager::Get()->GetAdapterClient()->DisconnectAllEnabledProfiles(
base::BindOnce(&BluetoothDeviceFloss::OnDisconnectAllEnabledProfiles,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::Forget(base::OnceClosure callback,
ErrorCallback error_callback) {
FlossDBusManager::Get()->GetAdapterClient()->RemoveBond(
base::BindOnce(&OnRemoveBond, std::move(callback)), AsFlossDeviceId());
}
void BluetoothDeviceFloss::ConnectToService(
const device::BluetoothUUID& uuid,
ConnectToServiceCallback callback,
ConnectToServiceErrorCallback error_callback) {
BLUETOOTH_LOG(EVENT) << address_
<< ": Connecting to service: " << uuid.canonical_value();
scoped_refptr<BluetoothSocketFloss> socket =
BluetoothSocketFloss::CreateBluetoothSocket(ui_task_runner_,
socket_thread_);
socket->Connect(this, FlossSocketManager::Security::kSecure, uuid,
base::BindOnce(std::move(callback), socket),
base::BindOnce(&BluetoothDeviceFloss::OnConnectToServiceError,
weak_ptr_factory_.GetWeakPtr(), socket,
std::move(error_callback)));
}
void BluetoothDeviceFloss::ConnectToServiceInsecurely(
const device::BluetoothUUID& uuid,
ConnectToServiceCallback callback,
ConnectToServiceErrorCallback error_callback) {
BLUETOOTH_LOG(EVENT) << address_
<< ": Connecting to service: " << uuid.canonical_value();
scoped_refptr<BluetoothSocketFloss> socket =
BluetoothSocketFloss::CreateBluetoothSocket(ui_task_runner_,
socket_thread_);
socket->Connect(this, FlossSocketManager::Security::kInsecure, uuid,
base::BindOnce(std::move(callback), socket),
base::BindOnce(&BluetoothDeviceFloss::OnConnectToServiceError,
weak_ptr_factory_.GetWeakPtr(), socket,
std::move(error_callback)));
}
std::unique_ptr<device::BluetoothGattConnection>
BluetoothDeviceFloss::CreateBluetoothGattConnectionObject() {
return std::make_unique<BluetoothGattConnectionFloss>(adapter_,
AsFlossDeviceId());
}
void BluetoothDeviceFloss::SetGattServicesDiscoveryComplete(bool complete) {
// This API actually refers to all services including SDP, not just GATT.
// This function is called by BluetoothSocketFloss because a connected socket
// implies SDP is completed, but Floss doesn't emit the UUIDs to the clients.
if (complete && !svc_resolved_) {
svc_resolved_ = true;
adapter()->NotifyGattServicesDiscovered(this);
} else if (!complete) {
svc_resolved_ = false;
}
}
bool BluetoothDeviceFloss::IsGattServicesDiscoveryComplete() const {
// This API actually refers to all services including SDP, not just GATT.
// In GATT case, services are only considered resolved if connection was
// established without a specific search uuid or was subsequently upgraded to
// full discovery.
return svc_resolved_ && !search_uuid.has_value();
}
void BluetoothDeviceFloss::Pair(
device::BluetoothDevice::PairingDelegate* pairing_delegate,
ConnectCallback callback) {
// Pair is the same as Connect due to influence from BlueZ.
// TODO(b/269516642): We should make distinction between them in the future.
Connect(pairing_delegate, std::move(callback));
}
BluetoothPairingFloss* BluetoothDeviceFloss::BeginPairing(
BluetoothDevice::PairingDelegate* pairing_delegate) {
pairing_ = std::make_unique<BluetoothPairingFloss>(pairing_delegate);
return pairing_.get();
}
#if BUILDFLAG(IS_CHROMEOS)
void BluetoothDeviceFloss::OnExecuteWrite(
base::OnceClosure callback,
ExecuteWriteErrorCallback error_callback,
DBusResult<Void> ret) {
if (!ret.has_value()) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kFailed);
return;
}
pending_execute_write_ =
std::make_pair(std::move(callback), std::move(error_callback));
}
void BluetoothDeviceFloss::BeginReliableWrite() {
DCHECK(!using_reliable_write_);
if (!using_reliable_write_) {
using_reliable_write_ = true;
FlossDBusManager::Get()->GetGattManagerClient()->BeginReliableWrite(
base::DoNothing(), address_);
}
}
void BluetoothDeviceFloss::ExecuteWrite(
base::OnceClosure callback,
ExecuteWriteErrorCallback error_callback) {
// Only one pending execute allowed at a time.
if (pending_execute_write_) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kInProgress);
return;
}
if (!using_reliable_write_) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kFailed);
return;
}
FlossDBusManager::Get()->GetGattManagerClient()->EndReliableWrite(
base::BindOnce(&BluetoothDeviceFloss::OnExecuteWrite,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)),
address_, /*execute=*/true);
}
void BluetoothDeviceFloss::AbortWrite(base::OnceClosure callback,
AbortWriteErrorCallback error_callback) {
// Only one pending execute allowed at a time.
if (pending_execute_write_) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kInProgress);
return;
}
if (!using_reliable_write_) {
std::move(error_callback)
.Run(device::BluetoothGattService::GattErrorCode::kFailed);
return;
}
FlossDBusManager::Get()->GetGattManagerClient()->EndReliableWrite(
base::BindOnce(&BluetoothDeviceFloss::OnExecuteWrite,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)),
address_, /*execute=*/false);
}
void BluetoothDeviceFloss::GattExecuteWrite(std::string address,
GattStatus status) {
if (address != address_) {
return;
}
if (!pending_execute_write_) {
return;
}
if (status != GattStatus::kSuccess) {
std::move(pending_execute_write_->second)
.Run(
floss::BluetoothGattServiceFloss::GattStatusToServiceError(status));
} else {
std::move(pending_execute_write_->first).Run();
}
pending_execute_write_ = std::nullopt;
}
#endif // BUILDFLAG(IS_CHROMEOS)
FlossDeviceId BluetoothDeviceFloss::AsFlossDeviceId() const {
return FlossDeviceId{.address = address_, .name = name_};
}
void BluetoothDeviceFloss::SetName(const std::string& name) {
name_ = name;
}
void BluetoothDeviceFloss::SetBondState(
FlossAdapterClient::BondState bond_state,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) {
bond_state_ = bond_state;
switch (bond_state_) {
case FlossAdapterClient::BondState::kNotBonded:
UpdateConnectingState(ConnectingState::kIdle, error_code);
break;
case FlossAdapterClient::BondState::kBondingInProgress:
UpdateConnectingState(ConnectingState::kACLConnecting, std::nullopt);
break;
case FlossAdapterClient::BondState::kBonded:
if (connecting_state_ == ConnectingState::kACLConnecting) {
ConnectAllEnabledProfiles();
}
break;
default:
NOTREACHED();
}
}
void BluetoothDeviceFloss::SetIsConnected(bool is_connected) {
is_acl_connected_ = is_connected;
// Update connection state to "ConnectedOnly" if it was previously
// disconnected and we are now connected. Also, update any connection state
// back to disconnected if acl state disconnects.
if (is_acl_connected_ &&
connection_state_ ==
static_cast<uint32_t>(
FlossAdapterClient::ConnectionState::kDisconnected)) {
connection_state_ = static_cast<uint32_t>(
FlossAdapterClient::ConnectionState::kConnectedOnly);
} else if (!is_acl_connected_) {
connection_state_ = static_cast<uint32_t>(
FlossAdapterClient::ConnectionState::kDisconnected);
}
if (!is_connected) {
UpdateConnectingState(
ConnectingState::kIdle,
BluetoothDevice::ConnectErrorCode::ERROR_DEVICE_UNCONNECTED);
} else if (is_connected &&
connecting_state_ == ConnectingState::kProfilesConnecting) {
UpdateConnectingState(ConnectingState::kProfilesConnected, std::nullopt);
}
}
void BluetoothDeviceFloss::SetConnectionState(uint32_t connection_state) {
connection_state_ = connection_state;
}
void BluetoothDeviceFloss::ConnectAllEnabledProfiles() {
UpdateConnectingState(ConnectingState::kProfilesConnecting, std::nullopt);
connection_incomplete_timer_.Start(
FROM_HERE, kDefaultConnectTimeout,
base::BindOnce(&BluetoothDeviceFloss::ConnectionIncomplete,
weak_ptr_factory_.GetWeakPtr()));
if (FlossDBusManager::Get()->GetFlossApiVersion() >= base::Version("0.4.0")) {
FlossDBusManager::Get()->GetAdapterClient()->ConnectAllEnabledProfiles(
base::BindOnce(static_cast<void (BluetoothDeviceFloss::*)(
DBusResult<FlossDBusClient::BtifStatus>)>(
&BluetoothDeviceFloss::OnConnectAllEnabledProfiles),
weak_ptr_factory_.GetWeakPtr()),
AsFlossDeviceId());
} else {
FlossDBusManager::Get()->GetAdapterClient()->ConnectAllEnabledProfiles(
base::BindOnce(
static_cast<void (BluetoothDeviceFloss::*)(DBusResult<Void>)>(
&BluetoothDeviceFloss::OnConnectAllEnabledProfiles),
weak_ptr_factory_.GetWeakPtr()),
AsFlossDeviceId());
}
}
void BluetoothDeviceFloss::ResetPairing() {
pairing_.reset();
}
void BluetoothDeviceFloss::CreateGattConnectionImpl(
std::optional<device::BluetoothUUID> service_uuid) {
// Generally, the first ever connection to a device should be direct and
// subsequent connections to known devices should be invoked with is_direct =
// false. Refer to |autoConnect| on BluetoothGatt.java.
bool is_direct =
gatt_connecting_state_ == GattConnectingState::kGattConnectionInit ||
!IsBondedImpl();
UpdateGattConnectingState(GattConnectingState::kGattConnecting);
// Save the service uuid to trigger service discovery later.
search_uuid = service_uuid;
// Gatt connections establish over LE.
FlossDBusManager::Get()->GetGattManagerClient()->Connect(
base::BindOnce(&BluetoothDeviceFloss::OnConnectGatt,
weak_ptr_factory_.GetWeakPtr()),
address_, FlossDBusClient::BluetoothTransport::kLe, is_direct);
}
void BluetoothDeviceFloss::OnConnectGatt(DBusResult<Void> ret) {
if (!ret.has_value()) {
UpdateGattConnectingState(GattConnectingState::kGattDisconnected);
}
}
void BluetoothDeviceFloss::UpgradeToFullDiscovery() {
if (!search_uuid.has_value()) {
LOG(ERROR) << "Attempting to upgrade to full discovery without having "
"searched any uuid.";
return;
}
// Clear previous search uuid.
search_uuid.reset();
svc_resolved_ = false;
FlossDBusManager::Get()->GetGattManagerClient()->DiscoverAllServices(
base::DoNothing(), address_);
}
void BluetoothDeviceFloss::DisconnectGatt() {
svc_resolved_ = false;
FlossDBusManager::Get()->GetGattManagerClient()->Disconnect(base::DoNothing(),
address_);
}
BluetoothDeviceFloss::BluetoothDeviceFloss(
BluetoothAdapterFloss* adapter,
const FlossDeviceId& device,
scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
scoped_refptr<device::BluetoothSocketThread> socket_thread)
: BluetoothDevice(adapter),
address_(device.address),
name_(device.name),
ui_task_runner_(ui_task_runner),
socket_thread_(socket_thread) {
FlossDBusManager::Get()->GetGattManagerClient()->AddObserver(this);
// Enable service specific discovery. This allows gatt connections to
// immediately trigger service discovery for specific uuids without
// requiring full discovery.
supports_service_specific_discovery_ = true;
}
BluetoothDeviceFloss::~BluetoothDeviceFloss() {
if (floss::FlossDBusManager::IsInitialized()) {
FlossDBusManager::Get()->GetGattManagerClient()->RemoveObserver(this);
}
}
bool BluetoothDeviceFloss::IsBondedImpl() const {
return bond_state_ == FlossAdapterClient::BondState::kBonded;
}
void BluetoothDeviceFloss::OnGetRemoteType(
base::OnceClosure callback,
DBusResult<FlossAdapterClient::BluetoothDeviceType> ret) {
if (ret.has_value()) {
switch (*ret) {
case FlossAdapterClient::BluetoothDeviceType::kBle:
transport_ = device::BluetoothTransport::BLUETOOTH_TRANSPORT_LE;
break;
case FlossAdapterClient::BluetoothDeviceType::kDual:
transport_ = device::BluetoothTransport::BLUETOOTH_TRANSPORT_DUAL;
break;
// Default to BrEdr. ARC++ for example doesn't know how to translate the
// Invalid state.
case FlossAdapterClient::BluetoothDeviceType::kBredr:
[[fallthrough]];
default:
transport_ = device::BluetoothTransport::BLUETOOTH_TRANSPORT_CLASSIC;
break;
}
} else {
BLUETOOTH_LOG(ERROR) << "GetRemoteType() failed: " << ret.error();
}
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnGetRemoteClass(base::OnceClosure callback,
DBusResult<uint32_t> ret) {
if (ret.has_value()) {
cod_ = *ret;
} else {
BLUETOOTH_LOG(ERROR) << "GetRemoteClass() failed: " << ret.error();
}
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnGetRemoteAppearance(base::OnceClosure callback,
DBusResult<uint16_t> ret) {
if (ret.has_value()) {
appearance_ = *ret;
} else {
BLUETOOTH_LOG(ERROR) << "GetRemoteAppearance() failed: " << ret.error();
}
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnGetRemoteUuids(base::OnceClosure callback,
DBusResult<UUIDList> ret) {
if (ret.has_value()) {
device_uuids_.ReplaceServiceUUIDs(*ret);
} else {
BLUETOOTH_LOG(ERROR) << "GetRemoteUuids() failed: " << ret.error();
}
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnGetRemoteVendorProductInfo(
base::OnceClosure callback,
DBusResult<FlossAdapterClient::VendorProductInfo> ret) {
if (ret.has_value()) {
vpi_ = *ret;
if (vpi_.vendorIdSrc >
static_cast<uint8_t>(VendorIDSource::VENDOR_ID_MAX_VALUE)) {
vpi_.vendorIdSrc =
static_cast<uint8_t>(VendorIDSource::VENDOR_ID_UNKNOWN);
}
} else {
BLUETOOTH_LOG(ERROR) << "GetRemoteVendorProductInfo() failed: "
<< ret.error();
}
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnGetRemoteAddressType(
base::OnceClosure callback,
DBusResult<FlossAdapterClient::BtAddressType> ret) {
if (ret.has_value()) {
switch (*ret) {
case FlossAdapterClient::BtAddressType::kPublic:
case FlossAdapterClient::BtAddressType::kPublicId:
address_type_ = AddressType::ADDR_TYPE_PUBLIC;
break;
case FlossAdapterClient::BtAddressType::kRandom:
case FlossAdapterClient::BtAddressType::kRandomId:
address_type_ = AddressType::ADDR_TYPE_RANDOM;
break;
default:
address_type_ = AddressType::ADDR_TYPE_UNKNOWN;
break;
}
} else {
BLUETOOTH_LOG(ERROR) << "GetRemoteAddressType() failed: " << ret.error();
}
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnGetRemoteBondState(base::OnceClosure callback,
DBusResult<uint32_t> ret) {
if (!ret.has_value()) {
BLUETOOTH_LOG(ERROR) << "GetBondState() failed: " << ret.error();
return;
}
SetBondState(static_cast<FlossAdapterClient::BondState>(*ret), std::nullopt);
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnGetRemoteConnectionState(
base::OnceClosure callback,
DBusResult<uint32_t> ret) {
if (!ret.has_value()) {
BLUETOOTH_LOG(ERROR) << "GetConnectionState() failed: " << ret.error();
return;
}
SetConnectionState(*ret);
if ((*ret >= 1) != IsConnected()) {
SetIsConnected(*ret >= 1);
}
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnConnectAllEnabledProfiles(DBusResult<Void> ret) {
if (!ret.has_value()) {
BLUETOOTH_LOG(ERROR) << "Failed to connect all enabled profiles: "
<< ret.error();
// TODO(b/202874707): Design a proper new errors for Floss.
UpdateConnectingState(ConnectingState::kIdle,
BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN);
return;
}
// Floss does not send any notifications that profiles have successfully
// connected if we are already ACL connected.
if (is_acl_connected_) {
UpdateConnectingState(ConnectingState::kProfilesConnected, std::nullopt);
}
}
void BluetoothDeviceFloss::OnConnectAllEnabledProfiles(
DBusResult<FlossDBusClient::BtifStatus> ret) {
if (!ret.has_value()) {
BLUETOOTH_LOG(ERROR)
<< "Failed to connect all enabled profiles, D-Bus error: "
<< ret.error();
UpdateConnectingState(ConnectingState::kIdle,
BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN);
return;
}
if (*ret != FlossDBusClient::BtifStatus::kSuccess) {
BLUETOOTH_LOG(ERROR) << "Failed to connect all enabled profiles, status: "
<< static_cast<uint32_t>(*ret);
UpdateConnectingState(ConnectingState::kIdle,
FlossDBusClient::BtifStatusToConnectErrorCode(*ret));
return;
}
// Floss does not send any notifications that profiles have successfully
// connected if we are already ACL connected.
if (is_acl_connected_) {
UpdateConnectingState(ConnectingState::kProfilesConnected, std::nullopt);
}
}
void BluetoothDeviceFloss::OnDeviceConnectionFailed(
FlossDBusClient::BtifStatus status) {
if (status == FlossDBusClient::BtifStatus::kTimeout) {
// If the connection failed due to timeout, avoid updating the connecting
// state, as the upper layers might retry the connection. Instead, wait for
// the connection to succeed, in which case we will record the success, or
// fail again, in which case the `connection_incomplete_timer_` will fire
// and record the same timeout failure anyway.
return;
}
UpdateConnectingState(ConnectingState::kIdle,
FlossDBusClient::BtifStatusToConnectErrorCode(status));
}
void BluetoothDeviceFloss::UpdateConnectingState(
ConnectingState state,
std::optional<BluetoothDevice::ConnectErrorCode> error) {
if ((state == ConnectingState::kIdle) &&
((connecting_state_ == ConnectingState::kACLConnecting) ||
(connecting_state_ == ConnectingState::kProfilesConnecting))) {
// Something went wrong during connecting
TriggerConnectCallback(error);
} else if ((state == ConnectingState::kProfilesConnected) &&
(connecting_state_ == ConnectingState::kProfilesConnecting)) {
// Successful profile connection
TriggerConnectCallback(std::nullopt);
}
if (connecting_state_ != state) {
connecting_state_ = state;
adapter_->NotifyDeviceChanged(this);
}
}
void BluetoothDeviceFloss::UpdateGattConnectingState(
GattConnectingState state) {
if (gatt_connecting_state_ != state) {
gatt_connecting_state_ = state;
adapter_->NotifyDeviceChanged(this);
}
}
void BluetoothDeviceFloss::TriggerConnectCallback(
std::optional<BluetoothDevice::ConnectErrorCode> error_code) {
connection_incomplete_timer_.Stop();
if (pending_callback_on_connect_profiles_) {
// We need to move it first and set pending_callback_on_connect_profiles_
// to nullopt before Run-ing the callback, because this may trigger arriving
// at this same location.
auto callback = std::move(*pending_callback_on_connect_profiles_);
pending_callback_on_connect_profiles_ = std::nullopt;
std::move(callback).Run(error_code);
}
pairing_ = nullptr;
}
void BluetoothDeviceFloss::OnDisconnectAllEnabledProfiles(
base::OnceClosure callback,
ErrorCallback error_callback,
DBusResult<Void> ret) {
if (!ret.has_value()) {
#if BUILDFLAG(IS_CHROMEOS)
device::RecordUserInitiatedDisconnectResult(
device::DisconnectResult::kFailure,
/*transport=*/GetType());
#endif
BLUETOOTH_LOG(ERROR) << "Failed to discconnect all enabled profiles: "
<< ret.error();
std::move(error_callback).Run();
return;
}
#if BUILDFLAG(IS_CHROMEOS)
device::RecordUserInitiatedDisconnectResult(
device::DisconnectResult::kSuccess,
/*transport=*/GetType());
#endif
std::move(callback).Run();
}
void BluetoothDeviceFloss::OnConnectToServiceError(
scoped_refptr<BluetoothSocketFloss> socket,
ConnectToServiceErrorCallback error_callback,
const std::string& error_message) {
BLUETOOTH_LOG(ERROR) << address_
<< ": Failed to connect to service: " << error_message;
// TODO - Log service connection failures for metrics.
std::move(error_callback).Run(error_message);
}
void BluetoothDeviceFloss::FetchRemoteType(base::OnceClosure callback) {
FlossDBusManager::Get()->GetAdapterClient()->GetRemoteType(
base::BindOnce(&BluetoothDeviceFloss::OnGetRemoteType,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::FetchRemoteClass(base::OnceClosure callback) {
FlossDBusManager::Get()->GetAdapterClient()->GetRemoteClass(
base::BindOnce(&BluetoothDeviceFloss::OnGetRemoteClass,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::FetchRemoteAppearance(base::OnceClosure callback) {
FlossDBusManager::Get()->GetAdapterClient()->GetRemoteAppearance(
base::BindOnce(&BluetoothDeviceFloss::OnGetRemoteAppearance,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::FetchRemoteUuids(base::OnceClosure callback) {
FlossDBusManager::Get()->GetAdapterClient()->GetRemoteUuids(
base::BindOnce(&BluetoothDeviceFloss::OnGetRemoteUuids,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::FetchRemoteVendorProductInfo(
base::OnceClosure callback) {
FlossDBusManager::Get()->GetAdapterClient()->GetRemoteVendorProductInfo(
base::BindOnce(&BluetoothDeviceFloss::OnGetRemoteVendorProductInfo,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::FetchRemoteAddressType(base::OnceClosure callback) {
FlossDBusManager::Get()->GetAdapterClient()->GetRemoteAddressType(
base::BindOnce(&BluetoothDeviceFloss::OnGetRemoteAddressType,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::FetchRemoteBondState(base::OnceClosure callback) {
FlossDBusManager::Get()->GetAdapterClient()->GetBondState(
base::BindOnce(&BluetoothDeviceFloss::OnGetRemoteBondState,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::FetchRemoteConnectionState(
base::OnceClosure callback) {
FlossDBusManager::Get()->GetAdapterClient()->GetConnectionState(
base::BindOnce(&BluetoothDeviceFloss::OnGetRemoteConnectionState,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)),
AsFlossDeviceId());
}
void BluetoothDeviceFloss::InitializeDeviceProperties(
PropertiesState state,
base::OnceClosure callback) {
// If a property read is already active, don't re-run it.
if (IsReadingProperties()) {
return;
}
property_reads_triggered_ = state;
pending_callback_on_init_props_ = std::move(callback);
// This must be incremented when adding more properties below
// and followed up with a TriggerInitDevicePropertiesCallback()
// in the callback.
num_pending_properties_ += 8;
FetchRemoteType(
base::BindOnce(&BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()));
FetchRemoteClass(
base::BindOnce(&BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()));
FetchRemoteAppearance(
base::BindOnce(&BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()));
FetchRemoteUuids(
base::BindOnce(&BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()));
FetchRemoteVendorProductInfo(
base::BindOnce(&BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()));
FetchRemoteAddressType(
base::BindOnce(&BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()));
FetchRemoteBondState(
base::BindOnce(&BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()));
FetchRemoteConnectionState(
base::BindOnce(&BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback,
weak_ptr_factory_.GetWeakPtr()));
}
void BluetoothDeviceFloss::TriggerInitDevicePropertiesCallback() {
if (--num_pending_properties_ == 0 && pending_callback_on_init_props_) {
property_reads_completed_ = static_cast<PropertiesState>(
property_reads_completed_ | property_reads_triggered_);
property_reads_triggered_ = PropertiesState::kNotRead;
std::move(*pending_callback_on_init_props_).Run();
pending_callback_on_init_props_ = std::nullopt;
}
DCHECK(num_pending_properties_ >= 0);
}
void BluetoothDeviceFloss::GattClientConnectionState(GattStatus status,
int32_t client_id,
bool connected,
std::string address) {
// We only care about connections for this device.
if (address != address_)
return;
std::optional<ConnectErrorCode> err = std::nullopt;
if (status != GattStatus::kSuccess) {
// TODO(b/193686094) - Convert GattStatus to other connect error codes.
err = ERROR_UNKNOWN;
}
if (connected) {
UpdateGattConnectingState(GattConnectingState::kGattConnected);
// Request for maximum MTU only when connected.
FlossDBusManager::Get()->GetGattManagerClient()->ConfigureMTU(
base::DoNothing(), address_, kMaxMtuSize);
return;
}
UpdateGattConnectingState(GattConnectingState::kGattDisconnected);
// Complete GATT connection callback.
DidConnectGatt(err);
}
void BluetoothDeviceFloss::GattSearchComplete(
std::string address,
const std::vector<GattService>& services,
GattStatus status) {
if (address != address_)
return;
if (status != GattStatus::kSuccess) {
LOG(ERROR) << "Failed Gatt service discovery with result: "
<< static_cast<uint32_t>(status);
return;
}
svc_resolved_ = true;
// Copy the GATT services list here and clear the original so that when we
// send GattServiceRemoved(), GetGattServices() returns no services.
GattServiceMap gatt_services_swapped;
gatt_services_swapped.swap(gatt_services_);
for (const auto& service : services) {
BLUETOOTH_LOG(EVENT) << "Adding new remote GATT service for device: "
<< address_;
std::unique_ptr<BluetoothRemoteGattServiceFloss> remote_service =
BluetoothRemoteGattServiceFloss::Create(adapter(), this, service);
BluetoothRemoteGattServiceFloss* remote_service_ptr = remote_service.get();
gatt_services_[remote_service_ptr->GetIdentifier()] =
std::move(remote_service);
DCHECK(remote_service_ptr->GetUUID().IsValid());
// GattDiscoveryCompleteForService is deprecated. Currently only Fast Pair
// needs to listen to this.
//
// TODO(b/269478974): We should remove this once all callers migrate to
// GattServicesDiscovered.
adapter()->NotifyGattDiscoveryComplete(remote_service_ptr);
}
adapter()->NotifyGattServicesDiscovered(this);
}
void BluetoothDeviceFloss::GattConnectionUpdated(std::string address,
int32_t interval,
int32_t latency,
int32_t timeout,
GattStatus status) {
if (address != GetAddress())
return;
VLOG(1) << "Gatt connection updated on " << GetAddress()
<< " with status=" << static_cast<uint32_t>(status);
if (pending_set_connection_latency_.has_value()) {
auto& [pending_cb, pending_error_cb] =
pending_set_connection_latency_.value();
if (status == GattStatus::kSuccess) {
std::move(pending_cb).Run();
} else {
std::move(pending_error_cb).Run();
}
pending_set_connection_latency_ = std::nullopt;
}
}
void BluetoothDeviceFloss::GattConfigureMtu(std::string address,
int32_t mtu,
GattStatus status) {
if (address != GetAddress())
return;
VLOG(1) << "GattConfigureMtu on " << GetAddress() << "; mtu=" << mtu
<< "; status=" << static_cast<uint32_t>(status);
// Discover services after configuring MTU
// This can be done even if configuring MTU failed.
if (search_uuid.has_value()) {
FlossDBusManager::Get()->GetGattManagerClient()->DiscoverServiceByUuid(
base::DoNothing(), address_, search_uuid.value());
} else if (!IsGattServicesDiscoveryComplete()) {
FlossDBusManager::Get()->GetGattManagerClient()->DiscoverAllServices(
base::DoNothing(), address_);
}
DidConnectGatt(std::nullopt);
}
#if BUILDFLAG(IS_CHROMEOS)
void BluetoothDeviceFloss::GattServiceChanged(std::string address) {
if (address != GetAddress()) {
return;
}
adapter()->NotifyGattNeedsDiscovery(this);
}
#endif
} // namespace floss
|