Skip to content

Commit

Permalink
Added data section filter in BLE scanning
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 554939498
  • Loading branch information
ggli-google authored and copybara-github committed Aug 8, 2023
1 parent 347d10b commit c331ef3
Show file tree
Hide file tree
Showing 31 changed files with 362 additions and 920 deletions.
7 changes: 5 additions & 2 deletions connections/implementation/client_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,11 @@ void ClientProxy::OnEndpointFound(
MutexLock lock(&mutex_);

NEARBY_LOGS(INFO) << "ClientProxy [Endpoint Found]: [enter] id="
<< endpoint_id << "; service=" << service_id << "; info="
<< absl::BytesToHexString(endpoint_info.data());
<< endpoint_id << "; service=" << service_id
<< "; info=" << absl::BytesToHexString(endpoint_info.data())
<< "; medium="
<< location::nearby::proto::connections::Medium_Name(
medium);
if (!IsDiscoveringServiceId(service_id)) {
NEARBY_LOGS(INFO) << "ClientProxy [Endpoint Found]: Ignoring event for id="
<< endpoint_id
Expand Down
6 changes: 6 additions & 0 deletions connections/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ std::string Status::ToString() const {
return "kWifiLanError";
case Status::kPayloadUnknown:
return "kPayloadUnknown";
case Status::kReset:
return "kReset";
case Status::kTimeout:
return "kTimeout";
case Status::kUnknown:
// fall through
default:
return "Unknown";
}
Expand Down
11 changes: 7 additions & 4 deletions connections/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace connections {

// Protocol operation result: kSuccess, if operation was successful;
// descriptive error code otherwise.
// LINT.IfChange
// LINT.IfChange(status_enum)
struct Status {
// Status is a struct, so it is possible to pass some context about failure,
// by adding extra fields to it when necessary, and not change any of the
Expand All @@ -44,6 +44,9 @@ struct Status {
kBleError,
kWifiLanError,
kPayloadUnknown,
kReset,
kTimeout,
kUnknown,
kNextValue,
};
Value value{kError};
Expand All @@ -53,9 +56,9 @@ struct Status {
std::string ToString() const;
};
// LINT.ThenChange(
// //depot/google3/location/nearby/cpp/sharing/implementation/nearby_connections_manager.cc:24
// //depot/google3/location/nearby/cpp/sharing/implementation/nearby_connections_types.h:46
// //depot/google3/location/nearby/cpp/sharing/implementation/nearby_connections_types_test.cc
// ../sharing/nearby_connections_manager.cc:status_enum,
// ../sharing/nearby_connections_types.h:status_enum,
// ../sharing/nearby_connections_types_test.cc:status_enum
// )

inline bool operator==(const Status& a, const Status& b) {
Expand Down
6 changes: 6 additions & 0 deletions connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ GNCStatus GNCStatusFromCppStatus(Status status) {
return GNCStatusWifiLanError;
case Status::kPayloadUnknown:
return GNCStatusPayloadUnknown;
case Status::kReset:
return GNCStatusReset;
case Status::kTimeout:
return GNCStatusTimeout;
case Status::kUnknown:
return GNCStatusUnknown;
case Status::kNextValue:
return GNCStatusUnknown;
}
Expand Down
6 changes: 6 additions & 0 deletions connections/swift/NearbyCoreAdapter/Sources/GNCError.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
return [NSError errorWithDomain:GNCErrorDomain code:GNCErrorWifiLanError userInfo:nil];
case Status::kPayloadUnknown:
return [NSError errorWithDomain:GNCErrorDomain code:GNCErrorPayloadUnknown userInfo:nil];
case Status::kReset:
return [NSError errorWithDomain:GNCErrorDomain code:GNCErrorReset userInfo:nil];
case Status::kTimeout:
return [NSError errorWithDomain:GNCErrorDomain code:GNCErrorTimeout userInfo:nil];
case Status::kUnknown:
return [NSError errorWithDomain:GNCErrorDomain code:GNCErrorUnknown userInfo:nil];
case Status::kNextValue:
return [NSError errorWithDomain:GNCErrorDomain code:GNCErrorUnknown userInfo:nil];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ typedef NS_CLOSED_ENUM(NSInteger, GNCStatus) {
GNCStatusWifiLanError,
GNCStatusPayloadUnknown,
GNCStatusUnknown,
GNCStatusReset,
GNCStatusTimeout,
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ typedef NS_ERROR_ENUM(GNCErrorDomain, GNCErrorCode){
GNCErrorBleError,
GNCErrorWifiLanError,
GNCErrorPayloadUnknown,
GNCErrorReset,
GNCErrorTimeout,
} NS_SWIFT_NAME(NearbyError);
7 changes: 6 additions & 1 deletion fastpair/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ cc_library(
"//fastpair:fast_pair_controller",
"//fastpair:fast_pair_events",
"//fastpair:fast_pair_seeker",
"//fastpair/common",
"//fastpair/internal/mediums",
"//fastpair/pairing",
"//fastpair/repository",
Expand All @@ -39,14 +40,18 @@ cc_test(
"//fastpair/common",
"//fastpair/message_stream:fake_gatt_callbacks",
"//fastpair/message_stream:fake_provider",
"//fastpair/proto:fastpair_cc_proto",
"//fastpair/repository",
"//fastpair/repository:device_repository",
"//fastpair/repository:test_support",
"//internal/account:test_support",
"//internal/platform:test_util",
"//internal/platform:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//internal/test/google3_only:test",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/time",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
],
)
20 changes: 20 additions & 0 deletions fastpair/internal/fast_pair_seeker_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@

#include "absl/status/status.h"
#include "absl/strings/str_format.h"
#include "fastpair/common/account_key.h"
#include "fastpair/fast_pair_controller.h"
#include "fastpair/fast_pair_events.h"
#include "fastpair/pairing/pairer_broker_impl.h"
#include "fastpair/scanning/scanner_broker_impl.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/logging.h"
#include "internal/platform/pending_job_registry.h"
#include "internal/platform/single_thread_executor.h"

Expand Down Expand Up @@ -303,5 +305,23 @@ void FastPairSeekerImpl::OnRetroactivePairFound(FastPairDevice& device) {
callbacks_.on_pair_event(device, PairEvent{.is_paired = true});
}

void FastPairSeekerImpl::ForgetDeviceByAccountKey(
const AccountKey& account_key) {
NEARBY_LOGS(VERBOSE) << __func__;
auto opt_device = devices_->FindDevice(account_key);
if (!opt_device.has_value()) {
NEARBY_LOGS(INFO) << __func__ << "No FP device matching the account key.";
} else {
devices_->RemoveDevice(opt_device.value());
}

repository_->DeleteAssociatedDeviceByAccountKey(
account_key, [&](absl::Status success) {
if (!success.ok()) return;
NEARBY_LOGS(VERBOSE) << "Deleted associated devcie by account key";
// Temporary solution to refresh the saved_devices_sheet.
repository_->GetUserSavedDevices();
});
}
} // namespace fastpair
} // namespace nearby
2 changes: 2 additions & 0 deletions fastpair/internal/fast_pair_seeker_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FastPairSeekerExt : public FastPairSeeker {

// Handle the state changes of screen lock.
virtual void SetIsScreenLocked(bool is_locked) = 0;
virtual void ForgetDeviceByAccountKey(const AccountKey& account_key) = 0;
};

class FastPairSeekerImpl : public FastPairSeekerExt,
Expand Down Expand Up @@ -91,6 +92,7 @@ class FastPairSeekerImpl : public FastPairSeekerExt,
absl::Status StartFastPairScan() override;
absl::Status StopFastPairScan() override;
void SetIsScreenLocked(bool is_locked) override;
void ForgetDeviceByAccountKey(const AccountKey& account_key) override;

// From BluetoothClassicMedium::Observer.
void DeviceAdded(BluetoothDevice& device) override;
Expand Down
79 changes: 79 additions & 0 deletions fastpair/internal/fast_pair_seeker_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,27 @@

#include <memory>
#include <string>
#include <vector>

#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "absl/strings/escaping.h"
#include "fastpair/common/fast_pair_device.h"
#include "fastpair/common/fast_pair_prefs.h"
#include "fastpair/fast_pair_events.h"
#include "fastpair/fast_pair_seeker.h"
#include "fastpair/message_stream/fake_gatt_callbacks.h"
#include "fastpair/message_stream/fake_provider.h"
#include "fastpair/proto/data.proto.h"
#include "fastpair/proto/enum.proto.h"
#include "fastpair/repository/fake_fast_pair_repository.h"
#include "fastpair/repository/fast_pair_device_repository.h"
#include "fastpair/repository/fast_pair_repository.h"
#include "internal/account/fake_account_manager.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/logging.h"
#include "internal/platform/medium_environment.h"
#include "internal/platform/single_thread_executor.h"
#include "internal/platform/task_runner_impl.h"
Expand Down Expand Up @@ -61,6 +70,19 @@ class MediumEnvironmentStarter {
~MediumEnvironmentStarter() { MediumEnvironment::Instance().Stop(); }
};

class FastPairRepositoryObserver : public FastPairRepository::Observer {
public:
explicit FastPairRepositoryObserver(CountDownLatch* latch) { latch_ = latch; }

void OnGetUserSavedDevices(
const proto::OptInStatus& opt_in_status,
const std::vector<proto::FastPairDevice>& devices) override {
latch_->CountDown();
}

CountDownLatch* latch_ = nullptr;
};

class FastPairSeekerImplTest : public testing::Test {
protected:
FastPairSeekerImplTest() {
Expand Down Expand Up @@ -222,6 +244,63 @@ TEST_F(FastPairSeekerImplTest, InitialPairing) {
fast_pair_seeker_.reset();
}

TEST_F(FastPairSeekerImplTest, ForgetDeviceByAccountKey) {
NEARBY_LOG_SET_SEVERITY(VERBOSE);
FakeProvider provider;
CountDownLatch discover_latch(1);
CountDownLatch pair_latch(1);
fast_pair_seeker_ = std::make_unique<FastPairSeekerImpl>(
FastPairSeekerImpl::ServiceCallbacks{
.on_initial_discovery =
[&](const FastPairDevice& device, InitialDiscoveryEvent event) {
EXPECT_EQ(device.GetModelId(), kModelId);
EXPECT_OK(fast_pair_seeker_->StartInitialPairing(
device, {},
{.on_pairing_result = [&](const FastPairDevice& device,
absl::Status status) {
EXPECT_EQ(device.GetBleAddress(),
provider.GetMacAddress());
EXPECT_OK(status);
pair_latch.CountDown();
}}));
discover_latch.CountDown();
}},
&executor_, account_manager_.get(), &devices_, repository_.get());

EXPECT_OK(fast_pair_seeker_->StartFastPairScan());
provider.PrepareForInitialPairing(
{
.private_key = absl::HexStringToBytes(kBobPrivateKey),
.public_key = absl::HexStringToBytes(kBobPublicKey),
.model_id = std::string(kModelId),
.pass_key = std::string(kPasskey),
},
&fake_gatt_callbacks_);

discover_latch.Await();
pair_latch.Await();
auto fp_device = devices_.FindDevice(provider.GetMacAddress());
ASSERT_TRUE(fp_device.has_value());
EXPECT_EQ(provider.GetAccountKey(), fp_device.value()->GetAccountKey());

// Adds FastPairRepository observer.
CountDownLatch repository_latch(1);
FastPairRepositoryObserver observer(&repository_latch);
repository_->AddObserver(&observer);
// Adds FastPairDeviceRepository observer.
CountDownLatch devices_latch(1);
FastPairDeviceRepository::RemoveDeviceCallback callback =
[&](const FastPairDevice& device) { devices_latch.CountDown(); };
devices_.AddObserver(&callback);

fast_pair_seeker_->ForgetDeviceByAccountKey(
fp_device.value()->GetAccountKey());
repository_latch.Await();
devices_latch.Await();
EXPECT_FALSE(devices_.FindDevice(provider.GetMacAddress()).has_value());
fast_pair_seeker_.reset();
}

TEST_F(FastPairSeekerImplTest, RetroactivePairingWithUserConsent) {
NEARBY_LOG_SET_SEVERITY(VERBOSE);
FakeProvider provider;
Expand Down
79 changes: 0 additions & 79 deletions fastpair/keyed_service/BUILD

This file was deleted.

Loading

0 comments on commit c331ef3

Please sign in to comment.