Add GattServer#NotifyCharacteristicChanged to ble_v2

PiperOrigin-RevId: 512124578
This commit is contained in:
Anay Wadhera
2023-02-24 11:46:33 -08:00
committed by Copybara-Service
parent 4ac88c17c5
commit abe68db97d
7 changed files with 67 additions and 0 deletions
+9
View File
@@ -148,6 +148,7 @@ class BleV2ServerSocket final {
//
// Note that some of the methods return absl::optional instead
// of std::optional, because iOS platform is still in C++14.
// LINT.IfChange
class GattServer final {
public:
explicit GattServer(std::unique_ptr<api::ble_v2::GattServer> gatt_server)
@@ -171,6 +172,13 @@ class GattServer final {
return impl_->UpdateCharacteristic(characteristic, value);
}
absl::Status NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) {
return impl_->NotifyCharacteristicChanged(characteristic, confirm,
new_value);
}
void Stop() {
if (impl_) return impl_->Stop();
}
@@ -187,6 +195,7 @@ class GattServer final {
private:
std::unique_ptr<api::ble_v2::GattServer> impl_;
};
// LINT.ThenChange(//depot/google3/third_party/nearby/internal/platform/implementation/ble_v2.h)
// Opaque wrapper for a GattClient.
//
+27
View File
@@ -532,5 +532,32 @@ TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) {
env_.Stop();
}
TEST_F(BleV2MediumTest, GattServerCanNotifyChange) {
env_.Start();
BluetoothAdapter adapter_a;
BleV2Medium ble_a(adapter_a);
Uuid service_uuid(1234, 5678);
Uuid characteristic_uuid(5678, 1234);
std::vector<GattCharacteristic::Permission> permissions = {
GattCharacteristic::Permission::kRead};
std::vector<GattCharacteristic::Property> properties = {
GattCharacteristic::Property::kRead};
// Start GattServer
std::unique_ptr<GattServer> gatt_server =
ble_a.StartGattServer(/*ServerGattConnectionCallback=*/{});
ASSERT_NE(gatt_server, nullptr);
// Add characteristic and its value.
// NOLINTNEXTLINE(google3-legacy-absl-backports)
absl::optional<GattCharacteristic> server_characteristic =
gatt_server->CreateCharacteristic(service_uuid, characteristic_uuid,
permissions, properties);
EXPECT_THAT(gatt_server->NotifyCharacteristicChanged(
server_characteristic.value(), false, ByteArray("hello")),
testing::status::StatusIs(absl::StatusCode::kUnimplemented));
}
} // namespace
} // namespace nearby
@@ -157,6 +157,8 @@ class BleMedium : public api::ble_v2::BleMedium {
bool UpdateCharacteristic(const api::ble_v2::GattCharacteristic &characteristic,
const nearby::ByteArray &value) override;
absl::Status NotifyCharacteristicChanged(const api::ble_v2::GattCharacteristic &characteristic,
bool confirm, const ByteArray &new_value) override;
void Stop() override;
private:
@@ -511,6 +511,13 @@ bool BleMedium::GattServer::UpdateCharacteristic(
return true;
}
absl::Status BleMedium::GattServer::NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) {
// no-op because client cannot request notifications.
return absl::UnimplementedError("Unimplemented!");
}
void BleMedium::GattServer::Stop() { [peripheral_ stopGATTService]; }
bool BleMedium::GattClient::DiscoverServiceAndCharacteristics(
+10
View File
@@ -194,6 +194,7 @@ class GattClient {
// https://developer.android.com/reference/android/bluetooth/BluetoothGattServer
//
// Representation of a BLE GATT server.
// LINT.IfChange
class GattServer {
public:
virtual ~GattServer() = default;
@@ -223,9 +224,18 @@ class GattServer {
virtual bool UpdateCharacteristic(const GattCharacteristic& characteristic,
const nearby::ByteArray& value) = 0;
// https://developer.android.com/reference/android/bluetooth/BluetoothGattServer#notifyCharacteristicChanged(android.bluetooth.BluetoothDevice,%20android.bluetooth.BluetoothGattCharacteristic,%20boolean,%20byte[])
//
// Send a notification or indication that a local characteristic has been
// updated and returns an absl::Status indicating success or what went wrong.
virtual absl::Status NotifyCharacteristicChanged(
const GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) = 0;
// Stops a GATT server.
virtual void Stop() = 0;
};
// LINT.ThenChange(//depot/google3/third_party/nearby/internal/platform/ble_v2.h)
// Callback for asynchronous events on the client side of a GATT connection.
struct ClientGattConnectionCallback {
@@ -375,6 +375,14 @@ bool BleV2Medium::GattServer::UpdateCharacteristic(
return true;
}
absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) {
// check if client has requested notifications.
// no-op for now because client cannot request notifications.
return absl::UnimplementedError("Unimplemented!");
}
void BleV2Medium::GattServer::Stop() {
NEARBY_LOGS(INFO) << "G3 Ble GattServer Stop";
MediumEnvironment::Instance().ClearBleV2MediumGattCharacteristics();
@@ -220,6 +220,10 @@ class BleV2Medium : public api::ble_v2::BleMedium {
const api::ble_v2::GattCharacteristic& characteristic,
const nearby::ByteArray& value) override;
absl::Status NotifyCharacteristicChanged(
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
const ByteArray& new_value) override;
void Stop() override;
};