Merge branch 'google3'

Change-Id: Iac43cc18efc80e7cca66ffa7cd2e472603ac4afc
This commit is contained in:
Alexey Polyudov
2020-09-14 02:01:04 -07:00
41 changed files with 2153 additions and 81 deletions
+10 -4
View File
@@ -22,7 +22,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()) {
@@ -59,12 +60,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;
}
+2 -1
View File
@@ -31,7 +31,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.
+10 -3
View File
@@ -18,6 +18,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:
@@ -55,6 +56,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(
@@ -66,7 +68,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));
@@ -83,10 +86,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,
@@ -118,10 +123,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,
{
@@ -374,6 +374,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));
}
@@ -100,6 +100,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_);
+20
View File
@@ -10,6 +10,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;
@@ -40,6 +44,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
+2
View File
@@ -14,6 +14,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