mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Merge branch 'master' into release
Change-Id: I5545ab2431ea72b0dc51d98d16f0ac100787a2d9
This commit is contained in:
@@ -36,7 +36,8 @@ bool Ble::IsAvailable() const {
|
||||
bool Ble::IsAvailableLocked() const { return medium_.IsValid(); }
|
||||
|
||||
bool Ble::StartAdvertising(const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes) {
|
||||
const ByteArray& advertisement_bytes,
|
||||
const std::string& fast_advertisement_service_uuid) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (advertisement_bytes.Empty()) {
|
||||
@@ -73,12 +74,17 @@ bool Ble::StartAdvertising(const std::string& service_id,
|
||||
NEARBY_LOGS(INFO) << "Turning on BLE advertising with advertisement bytes="
|
||||
<< advertisement_bytes.data() << "("
|
||||
<< advertisement_bytes.size() << ")"
|
||||
<< ", service id=" << service_id;
|
||||
if (!medium_.StartAdvertising(service_id, advertisement_bytes)) {
|
||||
<< ", service id=" << service_id
|
||||
<< ", fast advertisement service uuid="
|
||||
<< fast_advertisement_service_uuid;
|
||||
if (!medium_.StartAdvertising(service_id, advertisement_bytes,
|
||||
fast_advertisement_service_uuid)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Failed to turn on BLE advertising with advertisement bytes="
|
||||
<< advertisement_bytes.data() << "(" << advertisement_bytes.size()
|
||||
<< ")";
|
||||
<< ")"
|
||||
<< ", fast advertisement service uuid="
|
||||
<< fast_advertisement_service_uuid;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,8 @@ class Ble {
|
||||
// Sets custom advertisement data, and then enables Ble advertising.
|
||||
// Returns true, if data is successfully set, and false otherwise.
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes)
|
||||
const ByteArray& advertisement_bytes,
|
||||
const std::string& fast_advertisement_service_uuid)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables Ble advertising.
|
||||
|
||||
@@ -32,6 +32,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"};
|
||||
|
||||
class BleTest : public ::testing::Test {
|
||||
protected:
|
||||
@@ -69,6 +70,7 @@ TEST_F(BleTest, CanStartAdvertising) {
|
||||
radio_b.Enable();
|
||||
std::string service_id(kServiceID);
|
||||
ByteArray advertisement_bytes{std::string(kAdvertisementString)};
|
||||
std::string fast_advertisement_service_uuid(kFastAdvertisementServiceUuid);
|
||||
CountDownLatch found_latch(1);
|
||||
|
||||
ble_b.StartScanning(
|
||||
@@ -80,7 +82,8 @@ TEST_F(BleTest, CanStartAdvertising) {
|
||||
bool fast_advertisement) { found_latch.CountDown(); },
|
||||
});
|
||||
|
||||
EXPECT_TRUE(ble_a.StartAdvertising(service_id, advertisement_bytes));
|
||||
EXPECT_TRUE(ble_a.StartAdvertising(service_id, advertisement_bytes,
|
||||
fast_advertisement_service_uuid));
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(ble_a.StopAdvertising(service_id));
|
||||
EXPECT_TRUE(ble_b.StopScanning(service_id));
|
||||
@@ -97,10 +100,12 @@ TEST_F(BleTest, CanStartDiscovery) {
|
||||
radio_b.Enable();
|
||||
std::string service_id(kServiceID);
|
||||
ByteArray advertisement_bytes{std::string(kAdvertisementString)};
|
||||
std::string fast_advertisement_service_uuid(kFastAdvertisementServiceUuid);
|
||||
CountDownLatch accept_latch(1);
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
ble_b.StartAdvertising(service_id, advertisement_bytes);
|
||||
ble_b.StartAdvertising(service_id, advertisement_bytes,
|
||||
fast_advertisement_service_uuid);
|
||||
|
||||
EXPECT_TRUE(ble_a.StartScanning(
|
||||
service_id,
|
||||
@@ -132,10 +137,12 @@ TEST_F(BleTest, CanStartAcceptingConnectionsAndConnect) {
|
||||
radio_b.Enable();
|
||||
std::string service_id(kServiceID);
|
||||
ByteArray advertisement_bytes{std::string(kAdvertisementString)};
|
||||
std::string fast_advertisement_service_uuid(kFastAdvertisementServiceUuid);
|
||||
CountDownLatch found_latch(1);
|
||||
CountDownLatch accept_latch(1);
|
||||
|
||||
ble_a.StartAdvertising(service_id, advertisement_bytes);
|
||||
ble_a.StartAdvertising(service_id, advertisement_bytes,
|
||||
fast_advertisement_service_uuid);
|
||||
ble_a.StartAcceptingConnections(
|
||||
service_id,
|
||||
{
|
||||
|
||||
@@ -388,6 +388,11 @@ BluetoothDevice BluetoothClassic::FindRemoteDevice(
|
||||
return medium_.FindRemoteDevice(mac_address);
|
||||
}
|
||||
|
||||
std::string BluetoothClassic::GetMacAddress() const {
|
||||
MutexLock lock(&mutex_);
|
||||
return medium_.GetMacAddress();
|
||||
}
|
||||
|
||||
std::string BluetoothClassic::GenerateUuidFromString(const std::string& data) {
|
||||
return std::string(Uuid(data));
|
||||
}
|
||||
|
||||
@@ -114,6 +114,8 @@ class BluetoothClassic {
|
||||
const std::string& service_name)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
std::string GetMacAddress() const ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
BluetoothDevice FindRemoteDevice(const std::string& mac_address)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
|
||||
@@ -24,6 +24,10 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
namespace {
|
||||
constexpr absl::string_view kUpgradeServiceIdPostfix = "_UPGRADE";
|
||||
}
|
||||
|
||||
ByteArray Utils::GenerateRandomBytes(size_t length) {
|
||||
Prng rng;
|
||||
std::string data;
|
||||
@@ -54,6 +58,22 @@ ByteArray Utils::Sha256Hash(const std::string& source, size_t length) {
|
||||
return full_hash;
|
||||
}
|
||||
|
||||
std::string Utils::WrapUpgradeServiceId(const std::string& service_id) {
|
||||
if (service_id.empty()) {
|
||||
return {};
|
||||
}
|
||||
return service_id + std::string(kUpgradeServiceIdPostfix);
|
||||
}
|
||||
|
||||
std::string Utils::UnwrapUpgradeServiceId(
|
||||
const std::string& upgrade_service_id) {
|
||||
auto pos = upgrade_service_id.find(kUpgradeServiceIdPostfix);
|
||||
if (pos != std::string::npos) {
|
||||
return std::string(upgrade_service_id, 0, pos);
|
||||
}
|
||||
return upgrade_service_id;
|
||||
}
|
||||
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -28,6 +28,8 @@ class Utils {
|
||||
static ByteArray GenerateRandomBytes(size_t length);
|
||||
static ByteArray Sha256Hash(const ByteArray& source, size_t length);
|
||||
static ByteArray Sha256Hash(const std::string& source, size_t length);
|
||||
static std::string WrapUpgradeServiceId(const std::string& service_id);
|
||||
static std::string UnwrapUpgradeServiceId(const std::string& service_id);
|
||||
};
|
||||
|
||||
} // namespace connections
|
||||
|
||||
Reference in New Issue
Block a user