mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Roll forward to cl/333580336
Signed-off-by: Josh Nohle <nohle@google.com>
This commit is contained in:
@@ -75,6 +75,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;
|
||||
|
||||
@@ -136,7 +136,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
|
||||
|
||||
@@ -42,7 +42,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
|
||||
@@ -58,8 +58,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);
|
||||
|
||||
@@ -340,10 +340,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,
|
||||
@@ -353,10 +355,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;
|
||||
|
||||
@@ -151,10 +151,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.
|
||||
|
||||
@@ -252,11 +252,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;
|
||||
@@ -277,7 +281,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -146,6 +146,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_);
|
||||
|
||||
|
||||
@@ -240,7 +240,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);
|
||||
|
||||
@@ -207,7 +207,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:
|
||||
|
||||
@@ -17,8 +17,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);
|
||||
@@ -26,6 +28,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,
|
||||
|
||||
@@ -106,6 +106,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;
|
||||
|
||||
@@ -15,7 +15,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:
|
||||
@@ -59,6 +59,7 @@ TEST_F(BleMediumTest, CanStartAdvertising) {
|
||||
|
||||
EXPECT_TRUE(ble_b.StartScanning(
|
||||
service_id,
|
||||
fast_advertisement_service_uuid,
|
||||
DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](
|
||||
@@ -85,6 +86,7 @@ TEST_F(BleMediumTest, CanStartScanning) {
|
||||
|
||||
ble_a.StartScanning(
|
||||
service_id,
|
||||
fast_advertisement_service_uuid,
|
||||
DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](
|
||||
@@ -119,6 +121,7 @@ TEST_F(BleMediumTest, CanStopDiscovery) {
|
||||
|
||||
ble_a.StartScanning(
|
||||
service_id,
|
||||
fast_advertisement_service_uuid,
|
||||
DiscoveredPeripheralCallback{
|
||||
.peripheral_discovered_cb =
|
||||
[&found_latch](
|
||||
@@ -154,6 +157,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](
|
||||
|
||||
@@ -188,8 +188,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:
|
||||
|
||||
Reference in New Issue
Block a user