Roll forward to cl/333580336

Signed-off-by: Josh Nohle <nohle@google.com>
This commit is contained in:
Josh Nohle
2020-09-24 14:12:46 -07:00
parent 4e3f343b7a
commit 74fbb495ed
41 changed files with 426 additions and 188 deletions
+1
View File
@@ -89,6 +89,7 @@ class BleMedium {
// Returns true once the BLE scan has been initiated.
virtual bool StartScanning(const std::string& service_id,
const std::string& fast_advertisement_service_uuid,
DiscoveredPeripheralCallback callback) = 0;
// Returns true once BLE scanning for service_id is well and truly stopped;
+1 -1
View File
@@ -150,7 +150,7 @@ class BluetoothClassicMedium {
virtual std::unique_ptr<BluetoothServerSocket> ListenForService(
const std::string& service_name, const std::string& service_uuid) = 0;
virtual BluetoothDevice* FindRemoteDevice(const std::string& mac_address) = 0;
virtual BluetoothDevice* GetRemoteDevice(const std::string& mac_address) = 0;
};
} // namespace api
+2 -3
View File
@@ -56,7 +56,7 @@ class ImplementationPlatform {
// - synchronization primitives:
// - mutex (regular, and recursive)
// - condition variable (must work with regular mutex only)
// - Future<T> : to synchronize on Callable<T> schduled to execute.
// - Future<T> : to synchronize on Callable<T> scheduled to execute.
// - CountDownLatch : to ensure at least N threads are waiting.
// - file I/O
// - Logging
@@ -72,8 +72,7 @@ class ImplementationPlatform {
// Supports enums and integers up to 32-bit.
// Does not use locking, if platform supports 32-bit atimics natively.
// Does not use dynamic memory allocations in operations.
static std::unique_ptr<AtomicUint32>
CreateAtomicUint32(std::uint32_t value);
static std::unique_ptr<AtomicUint32> CreateAtomicUint32(std::uint32_t value);
static std::unique_ptr<CountDownLatch> CreateCountDownLatch(
std::int32_t count);
+9 -5
View File
@@ -354,10 +354,12 @@ void MediumEnvironment::UpdateBleMediumForAdvertising(
void MediumEnvironment::UpdateBleMediumForScanning(
api::BleMedium& medium, const std::string& service_id,
const std::string& fast_advertisement_service_uuid,
BleDiscoveredPeripheralCallback callback, bool enabled) {
if (!enabled_) return;
RunOnMediumEnvironmentThread(
[this, &medium, service_id, callback = std::move(callback), enabled]() {
[this, &medium, service_id, fast_advertisement_service_uuid,
callback = std::move(callback), enabled]() {
auto item = ble_mediums_.find(&medium);
if (item == ble_mediums_.end()) {
NEARBY_LOG(INFO,
@@ -367,10 +369,12 @@ void MediumEnvironment::UpdateBleMediumForScanning(
}
auto& context = item->second;
context.discovery_callback = std::move(callback);
NEARBY_LOG(INFO,
"Update Ble medium for scanning: this=%p; medium=%p; "
"service_id=%s; enabled=%d ;",
this, &medium, service_id.c_str(), enabled);
NEARBY_LOG(
INFO,
"Update Ble medium for scanning: this=%p; medium=%p; "
"service_id=%s; fast_advertisement_service_uuid=%s; enabled=%d ;",
this, &medium, service_id.c_str(),
fast_advertisement_service_uuid.c_str(), enabled);
for (auto& medium_info : ble_mediums_) {
auto& local_medium = medium_info.first;
auto& info = medium_info.second;
+4 -4
View File
@@ -165,10 +165,10 @@ class MediumEnvironment {
// This should be called when discoverable state changes.
// with user-specified callback when discovery is enabled, and with default
// (empty) callback otherwise.
void UpdateBleMediumForScanning(api::BleMedium& medium,
const std::string& service_id,
BleDiscoveredPeripheralCallback callback,
bool enabled);
void UpdateBleMediumForScanning(
api::BleMedium& medium, const std::string& service_id,
const std::string& fast_advertisement_service_uuid,
BleDiscoveredPeripheralCallback callback, bool enabled);
// Updates Accepted connection callback info to allow for dispatch of
// advertising events.
+8 -4
View File
@@ -266,11 +266,15 @@ bool BleMedium::StopAdvertising(const std::string& service_id) {
return true;
}
bool BleMedium::StartScanning(const std::string& service_id,
DiscoveredPeripheralCallback callback) {
bool BleMedium::StartScanning(
const std::string& service_id,
const std::string& fast_advertisement_service_uuid,
DiscoveredPeripheralCallback callback) {
NEARBY_LOGS(INFO) << "G3 Ble StartScanning: service_id=" << service_id;
auto& env = MediumEnvironment::Instance();
env.UpdateBleMediumForScanning(*this, service_id, std::move(callback), true);
env.UpdateBleMediumForScanning(*this, service_id,
fast_advertisement_service_uuid,
std::move(callback), true);
{
absl::MutexLock lock(&mutex_);
scanning_info_.service_id = service_id;
@@ -291,7 +295,7 @@ bool BleMedium::StopScanning(const std::string& service_id) {
}
auto& env = MediumEnvironment::Instance();
env.UpdateBleMediumForScanning(*this, service_id, {}, false);
env.UpdateBleMediumForScanning(*this, service_id, {}, {}, false);
return true;
}
+1
View File
@@ -160,6 +160,7 @@ class BleMedium : public api::BleMedium {
// Returns true once the Ble scanning has been initiated.
bool StartScanning(const std::string& service_id,
const std::string& fast_advertisement_service_uuid,
DiscoveredPeripheralCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
+1 -1
View File
@@ -254,7 +254,7 @@ BluetoothClassicMedium::ListenForService(const std::string& service_name,
return socket;
}
api::BluetoothDevice* BluetoothClassicMedium::FindRemoteDevice(
api::BluetoothDevice* BluetoothClassicMedium::GetRemoteDevice(
const std::string& mac_address) {
auto& env = MediumEnvironment::Instance();
return env.FindBluetoothDevice(mac_address);
+1 -1
View File
@@ -221,7 +221,7 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium {
const std::string& service_name, const std::string& service_uuid) override
ABSL_LOCKS_EXCLUDED(mutex_);
api::BluetoothDevice* FindRemoteDevice(
api::BluetoothDevice* GetRemoteDevice(
const std::string& mac_address) override;
private:
+5 -2
View File
@@ -31,8 +31,10 @@ bool BleMedium::StopAdvertising(const std::string& service_id) {
return impl_->StopAdvertising(service_id);
}
bool BleMedium::StartScanning(const std::string& service_id,
DiscoveredPeripheralCallback callback) {
bool BleMedium::StartScanning(
const std::string& service_id,
const std::string& fast_advertisement_service_uuid,
DiscoveredPeripheralCallback callback) {
{
MutexLock lock(&mutex_);
discovered_peripheral_callback_ = std::move(callback);
@@ -40,6 +42,7 @@ bool BleMedium::StartScanning(const std::string& service_id,
}
return impl_->StartScanning(
service_id,
fast_advertisement_service_uuid,
{
.peripheral_discovered_cb =
[this](api::BlePeripheral& peripheral,
+1
View File
@@ -120,6 +120,7 @@ class BleMedium final {
// Returns true once the BLE scan has been initiated.
bool StartScanning(const std::string& service_id,
const std::string& fast_advertisement_service_uuid,
DiscoveredPeripheralCallback callback);
// Returns true once BLE scanning for service_id is well and truly stopped;
+5 -1
View File
@@ -29,7 +29,7 @@ namespace {
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
constexpr absl::string_view kServiceID{"com.google.location.nearby.apps.test"};
constexpr absl::string_view kAdvertisementString{"\x0a\x0b\x0c\x0d"};
constexpr absl::string_view kFastAdvertisementServiceUuid{"\xff\xfe"};
constexpr absl::string_view kFastAdvertisementServiceUuid{"\xf3\xfe"};
class BleMediumTest : public ::testing::Test {
protected:
@@ -73,6 +73,7 @@ TEST_F(BleMediumTest, CanStartAdvertising) {
EXPECT_TRUE(ble_b.StartScanning(
service_id,
fast_advertisement_service_uuid,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](
@@ -99,6 +100,7 @@ TEST_F(BleMediumTest, CanStartScanning) {
ble_a.StartScanning(
service_id,
fast_advertisement_service_uuid,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](
@@ -133,6 +135,7 @@ TEST_F(BleMediumTest, CanStopDiscovery) {
ble_a.StartScanning(
service_id,
fast_advertisement_service_uuid,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](
@@ -168,6 +171,7 @@ TEST_F(BleMediumTest, CanStartAcceptingConnectionsAndConnect) {
BlePeripheral* discovered_peripheral = nullptr;
ble_a.StartScanning(
service_id,
fast_advertisement_service_uuid,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch, &discovered_peripheral](
+2 -2
View File
@@ -202,8 +202,8 @@ class BluetoothClassicMedium final {
api::BluetoothClassicMedium& GetImpl() { return *impl_; }
BluetoothAdapter& GetAdapter() { return adapter_; }
std::string GetMacAddress() const { return adapter_.GetMacAddress(); }
BluetoothDevice FindRemoteDevice(const std::string& mac_address) {
return BluetoothDevice(impl_->FindRemoteDevice(mac_address));
BluetoothDevice GetRemoteDevice(const std::string& mac_address) {
return BluetoothDevice(impl_->GetRemoteDevice(mac_address));
}
private: