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
|
// 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.
#include "device/bluetooth/test/test_bluetooth_local_gatt_service_delegate.h"
#include "base/functional/callback.h"
#include "device/bluetooth/test/bluetooth_gatt_server_test.h"
namespace device {
TestBluetoothLocalGattServiceDelegate::TestBluetoothLocalGattServiceDelegate()
: should_fail_(false),
last_written_value_(0),
value_to_write_(0),
expected_service_(nullptr),
expected_characteristic_(nullptr),
expected_descriptor_(nullptr) {}
TestBluetoothLocalGattServiceDelegate::
~TestBluetoothLocalGattServiceDelegate() = default;
void TestBluetoothLocalGattServiceDelegate::OnCharacteristicReadRequest(
const BluetoothDevice* device,
const BluetoothLocalGattCharacteristic* characteristic,
int offset,
ValueCallback callback) {
EXPECT_EQ(expected_characteristic_->GetIdentifier(),
characteristic->GetIdentifier());
if (should_fail_) {
std::move(callback).Run(BluetoothGattService::GattErrorCode::kFailed,
/*value=*/std::vector<uint8_t>());
return;
}
last_seen_device_ = device->GetIdentifier();
std::move(callback).Run(/*error_code=*/std::nullopt,
BluetoothGattServerTest::GetValue(value_to_write_));
}
void TestBluetoothLocalGattServiceDelegate::OnCharacteristicWriteRequest(
const BluetoothDevice* device,
const BluetoothLocalGattCharacteristic* characteristic,
const std::vector<uint8_t>& value,
int offset,
base::OnceClosure callback,
ErrorCallback error_callback) {
EXPECT_EQ(expected_characteristic_->GetIdentifier(),
characteristic->GetIdentifier());
if (should_fail_) {
std::move(error_callback).Run();
return;
}
last_seen_device_ = device->GetIdentifier();
last_written_value_ = BluetoothGattServerTest::GetInteger(value);
std::move(callback).Run();
}
void TestBluetoothLocalGattServiceDelegate::OnCharacteristicPrepareWriteRequest(
const BluetoothDevice* device,
const BluetoothLocalGattCharacteristic* characteristic,
const std::vector<uint8_t>& value,
int offset,
bool has_subsequent_request,
base::OnceClosure callback,
ErrorCallback error_callback) {
EXPECT_EQ(expected_characteristic_->GetIdentifier(),
characteristic->GetIdentifier());
if (should_fail_) {
std::move(error_callback).Run();
return;
}
// For testing purpose, we don't maintain a queue for all the pending prepare
// write requests. Instead, we just write the last value, that is, we assume
// |offset| is always 0.
if (!has_subsequent_request)
last_written_value_ = BluetoothGattServerTest::GetInteger(value);
last_seen_device_ = device->GetIdentifier();
std::move(callback).Run();
}
void TestBluetoothLocalGattServiceDelegate::OnDescriptorReadRequest(
const BluetoothDevice* device,
const BluetoothLocalGattDescriptor* descriptor,
int offset,
ValueCallback callback) {
EXPECT_EQ(expected_descriptor_->GetIdentifier(), descriptor->GetIdentifier());
if (should_fail_) {
std::move(callback).Run(BluetoothGattService::GattErrorCode::kFailed,
/*value=*/std::vector<uint8_t>());
return;
}
last_seen_device_ = device->GetIdentifier();
std::move(callback).Run(/*error_code=*/std::nullopt,
BluetoothGattServerTest::GetValue(value_to_write_));
}
void TestBluetoothLocalGattServiceDelegate::OnDescriptorWriteRequest(
const BluetoothDevice* device,
const BluetoothLocalGattDescriptor* descriptor,
const std::vector<uint8_t>& value,
int offset,
base::OnceClosure callback,
ErrorCallback error_callback) {
EXPECT_EQ(expected_descriptor_->GetIdentifier(), descriptor->GetIdentifier());
if (should_fail_) {
std::move(error_callback).Run();
return;
}
last_seen_device_ = device->GetIdentifier();
last_written_value_ = BluetoothGattServerTest::GetInteger(value);
std::move(callback).Run();
}
void TestBluetoothLocalGattServiceDelegate::OnNotificationsStart(
const BluetoothDevice* device,
device::BluetoothGattCharacteristic::NotificationType notification_type,
const BluetoothLocalGattCharacteristic* characteristic) {
DCHECK(device);
EXPECT_EQ(expected_characteristic_->GetIdentifier(),
characteristic->GetIdentifier());
notifications_started_for_characteristic_[characteristic->GetIdentifier()] =
true;
}
void TestBluetoothLocalGattServiceDelegate::OnNotificationsStop(
const BluetoothDevice* device,
const BluetoothLocalGattCharacteristic* characteristic) {
DCHECK(device);
EXPECT_EQ(expected_characteristic_->GetIdentifier(),
characteristic->GetIdentifier());
notifications_started_for_characteristic_[characteristic->GetIdentifier()] =
false;
}
bool TestBluetoothLocalGattServiceDelegate::NotificationStatusForCharacteristic(
BluetoothLocalGattCharacteristic* characteristic) {
auto found = notifications_started_for_characteristic_.find(
characteristic->GetIdentifier());
if (found == notifications_started_for_characteristic_.end())
return false;
return found->second;
}
} // namespace device
|