Merge branch 'google3' to roll forward cl/336363104.

This commit is contained in:
hai007
2020-10-09 14:53:41 -07:00
31 changed files with 728 additions and 254 deletions
+3
View File
@@ -25,7 +25,9 @@ cc_library(
"//core_v2/internal:__subpackages__",
],
deps = [
":utils",
"//core_v2:core_types",
"//core_v2/internal/mediums/ble_v2",
"//core_v2/internal/mediums/webrtc",
"//platform_v2/base",
"//platform_v2/public:comm",
@@ -49,6 +51,7 @@ cc_library(
hdrs = ["utils.h"],
visibility = [
"//core_v2/internal:__pkg__",
"//core_v2/internal/mediums:__pkg__",
"//core_v2/internal/mediums/ble_v2:__pkg__",
"//core_v2/internal/mediums/webrtc:__pkg__",
],
+64 -3
View File
@@ -4,6 +4,9 @@
#include <string>
#include <utility>
#include "core_v2/internal/mediums/ble_v2/ble_advertisement.h"
#include "core_v2/internal/mediums/utils.h"
#include "platform_v2/base/prng.h"
#include "platform_v2/public/logging.h"
#include "platform_v2/public/mutex_lock.h"
@@ -11,6 +14,15 @@ namespace location {
namespace nearby {
namespace connections {
ByteArray Ble::GenerateHash(const std::string& source, size_t size) {
return Utils::Sha256Hash(source, size);
}
ByteArray Ble::GenerateDeviceToken() {
return Utils::Sha256Hash(std::to_string(Prng().NextUint32()),
mediums::BleAdvertisement::kDeviceTokenLength);
}
Ble::Ble(BluetoothRadio& radio) : radio_(radio) {}
bool Ble::IsAvailable() const {
@@ -63,7 +75,23 @@ bool Ble::StartAdvertising(const std::string& service_id,
<< ", service id=" << service_id
<< ", fast advertisement service uuid="
<< fast_advertisement_service_uuid;
if (!medium_.StartAdvertising(service_id, advertisement_bytes,
// Wrap the connections advertisement to the medium advertisement.
const bool fast_advertisement = !fast_advertisement_service_uuid.empty();
ByteArray service_id_hash{GenerateHash(
service_id, mediums::BleAdvertisement::kServiceIdHashLength)};
ByteArray medium_advertisement_bytes{mediums::BleAdvertisement{
mediums::BleAdvertisement::Version::kV2,
mediums::BleAdvertisement::SocketVersion::kV2,
fast_advertisement ? ByteArray{} : service_id_hash, advertisement_bytes,
GenerateDeviceToken()}};
if (medium_advertisement_bytes.Empty()) {
NEARBY_LOGS(INFO) << "Failed to BLE advertise because we could not "
"create a medium advertisement.";
return false;
}
if (!medium_.StartAdvertising(service_id, medium_advertisement_bytes,
fast_advertisement_service_uuid)) {
NEARBY_LOGS(INFO)
<< "Failed to turn on BLE advertising with advertisement bytes="
@@ -110,6 +138,8 @@ bool Ble::StartScanning(const std::string& service_id,
DiscoveredPeripheralCallback callback) {
MutexLock lock(&mutex_);
discovered_peripheral_callback_ = std::move(callback);
if (service_id.empty()) {
NEARBY_LOGS(INFO)
<< "Refusing to start BLE scanning with empty service id.";
@@ -134,8 +164,29 @@ bool Ble::StartScanning(const std::string& service_id,
return false;
}
if (!medium_.StartScanning(service_id, fast_advertisement_service_uuid,
callback)) {
if (!medium_.StartScanning(
service_id, fast_advertisement_service_uuid,
{
.peripheral_discovered_cb =
[this](BlePeripheral& peripheral,
const std::string& service_id,
const ByteArray& medium_advertisement_bytes,
bool fast_advertisement) {
// Unwrap connection BleAdvertisement from medium
// BleAdvertisement.
auto connection_advertisement_bytes =
UnwrapAdvertisementBytes(medium_advertisement_bytes);
discovered_peripheral_callback_.peripheral_discovered_cb(
peripheral, service_id, connection_advertisement_bytes,
fast_advertisement);
},
.peripheral_lost_cb =
[this](BlePeripheral& peripheral,
const std::string& service_id) {
discovered_peripheral_callback_.peripheral_lost_cb(
peripheral, service_id);
},
})) {
NEARBY_LOGS(INFO) << "Failed to start scan of BLE services.";
return false;
}
@@ -272,6 +323,16 @@ BleSocket Ble::Connect(BlePeripheral& peripheral,
return socket;
}
ByteArray Ble::UnwrapAdvertisementBytes(
const ByteArray& medium_advertisement_data) {
mediums::BleAdvertisement medium_ble_advertisement{medium_advertisement_data};
if (!medium_ble_advertisement.IsValid()) {
return ByteArray{};
}
return medium_ble_advertisement.GetData();
}
} // namespace connections
} // namespace nearby
} // namespace location
+10 -2
View File
@@ -88,8 +88,6 @@ class Ble {
ABSL_LOCKS_EXCLUDED(mutex_);
private:
static constexpr int kMaxAdvertisementLength = 512;
struct AdvertisingInfo {
bool Empty() const { return service_ids.empty(); }
void Clear() { service_ids.clear(); }
@@ -132,6 +130,11 @@ class Ble {
absl::flat_hash_set<std::string> service_ids;
};
static constexpr int kMaxAdvertisementLength = 512;
static ByteArray GenerateHash(const std::string& source, size_t size);
static ByteArray GenerateDeviceToken();
// Same as IsAvailable(), but must be called with mutex_ held.
bool IsAvailableLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -147,6 +150,10 @@ class Ble {
bool IsAcceptingConnectionsLocked(const std::string& service_id)
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Extract connection advertisement from medium advertisement.
ByteArray UnwrapAdvertisementBytes(
const ByteArray& medium_advertisement_data);
mutable Mutex mutex_;
BluetoothRadio& radio_ ABSL_GUARDED_BY(mutex_);
BluetoothAdapter& adapter_ ABSL_GUARDED_BY(mutex_){
@@ -154,6 +161,7 @@ class Ble {
BleMedium medium_ ABSL_GUARDED_BY(mutex_){adapter_};
AdvertisingInfo advertising_info_ ABSL_GUARDED_BY(mutex_);
ScanningInfo scanning_info_ ABSL_GUARDED_BY(mutex_);
DiscoveredPeripheralCallback discovered_peripheral_callback_;
AcceptingConnectionsInfo accepting_connections_info_ ABSL_GUARDED_BY(mutex_);
};
+6 -6
View File
@@ -60,12 +60,12 @@ TEST_F(BleTest, CanStartAdvertising) {
CountDownLatch found_latch(1);
ble_b.StartScanning(
service_id,
fast_advertisement_service_uuid,
service_id, fast_advertisement_service_uuid,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](
BlePeripheral& peripheral, const std::string& service_id,
const ByteArray& advertisement_bytes,
bool fast_advertisement) { found_latch.CountDown(); },
});
@@ -95,12 +95,12 @@ TEST_F(BleTest, CanStartDiscovery) {
fast_advertisement_service_uuid);
EXPECT_TRUE(ble_a.StartScanning(
service_id,
fast_advertisement_service_uuid,
service_id, fast_advertisement_service_uuid,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&accept_latch](
BlePeripheral& peripheral, const std::string& service_id,
const ByteArray& advertisement_bytes,
bool fast_advertisement) { accept_latch.CountDown(); },
.peripheral_lost_cb =
[&lost_latch](BlePeripheral& peripheral,
@@ -140,12 +140,12 @@ TEST_F(BleTest, CanStartAcceptingConnectionsAndConnect) {
});
BlePeripheral discovered_peripheral;
ble_b.StartScanning(
service_id,
fast_advertisement_service_uuid,
service_id, fast_advertisement_service_uuid,
{
.peripheral_discovered_cb =
[&found_latch, &discovered_peripheral](
BlePeripheral& peripheral, const std::string& service_id,
const ByteArray& advertisement_bytes,
bool fast_advertisement) {
discovered_peripheral = peripheral;
NEARBY_LOG(
@@ -16,16 +16,8 @@ BleAdvertisement::BleAdvertisement(Version version,
const ByteArray &service_id_hash,
const ByteArray &data,
const ByteArray &device_token) {
DoInitialize(/*fast_advertisement=*/false, version, socket_version,
service_id_hash, data, device_token);
}
BleAdvertisement::BleAdvertisement(Version version,
SocketVersion socket_version,
const ByteArray &data,
const ByteArray &device_token) {
DoInitialize(/*fast_advertisement=*/true, version, socket_version,
{}, data, device_token);
DoInitialize(/*fast_advertisement=*/service_id_hash.Empty(), version,
socket_version, service_id_hash, data, device_token);
}
void BleAdvertisement::DoInitialize(bool fast_advertisement, Version version,
@@ -47,8 +47,6 @@ class BleAdvertisement {
BleAdvertisement(Version version, SocketVersion socket_version,
const ByteArray &service_id_hash, const ByteArray &data,
const ByteArray &device_token);
BleAdvertisement(Version version, SocketVersion socket_version,
const ByteArray &data, const ByteArray &device_token);
explicit BleAdvertisement(const ByteArray &ble_advertisement_bytes);
BleAdvertisement(const BleAdvertisement &) = default;
BleAdvertisement &operator=(const BleAdvertisement &) = default;
@@ -53,6 +53,7 @@ TEST(BleAdvertisementTest, ConstructionWorksV1ForFastAdvertisement) {
BleAdvertisement ble_advertisement{BleAdvertisement::Version::kV1,
BleAdvertisement::SocketVersion::kV1,
ByteArray{},
fast_data,
device_token};
@@ -83,6 +84,7 @@ TEST(BleAdvertisementTest, ConstructionFailsWithBadVersion) {
BleAdvertisement fast_ble_advertisement{bad_version,
kSocketVersion,
ByteArray{},
data,
device_token};
EXPECT_FALSE(fast_ble_advertisement.IsValid());
@@ -105,6 +107,7 @@ TEST(BleAdvertisementTest, ConstructionFailsWithBadSocketVersion) {
BleAdvertisement fast_ble_advertisement{kVersion,
bad_socket_version,
ByteArray{},
data,
device_token};
EXPECT_FALSE(fast_ble_advertisement.IsValid());
@@ -160,6 +163,7 @@ TEST(BleAdvertisementTest, ConstructionFailsWithLongData) {
BleAdvertisement fast_ble_advertisement{kVersion,
kSocketVersion,
ByteArray{},
bad_data,
device_token};
EXPECT_FALSE(fast_ble_advertisement.IsValid());
@@ -191,6 +195,7 @@ TEST(BleAdvertisementTest,
BleAdvertisement ble_advertisement{kVersion,
kSocketVersion,
ByteArray{},
fast_data,
ByteArray{}};
@@ -228,12 +233,14 @@ TEST(BleAdvertisementTest, ConstructionFailsWithWrongSizeofDeviceToken) {
BleAdvertisement fast_ble_advertisement_1{kVersion,
kSocketVersion,
ByteArray{},
data,
bad_device_token_1};
EXPECT_FALSE(fast_ble_advertisement_1.IsValid());
BleAdvertisement fast_ble_advertisement_2{kVersion,
kSocketVersion,
ByteArray{},
data,
bad_device_token_2};
EXPECT_FALSE(fast_ble_advertisement_2.IsValid());
@@ -270,6 +277,7 @@ TEST(BleAdvertisementTest,
BleAdvertisement org_ble_advertisement{kVersion,
kSocketVersion,
ByteArray{},
fast_data,
device_token};
@@ -312,6 +320,7 @@ TEST(BleAdvertisementTest,
BleAdvertisement org_ble_advertisement{kVersion,
kSocketVersion,
ByteArray{},
ByteArray(),
device_token};
ByteArray ble_advertisement_bytes{org_ble_advertisement};
@@ -366,6 +375,7 @@ TEST(BleAdvertisementTest,
BleAdvertisement org_ble_advertisement{kVersion,
kSocketVersion,
ByteArray{},
fast_data,
device_token};
ByteArray org_ble_advertisement_bytes{org_ble_advertisement};
@@ -424,6 +434,7 @@ TEST(BleAdvertisementTest,
BleAdvertisement org_ble_advertisement{kVersion,
kSocketVersion,
ByteArray{},
fast_data,
device_token};
ByteArray org_ble_advertisement_bytes{org_ble_advertisement};
@@ -475,6 +486,7 @@ TEST(BleAdvertisementTest,
BleAdvertisement org_ble_advertisement{kVersion,
kSocketVersion,
ByteArray{},
fast_data,
device_token};
ByteArray org_ble_advertisement_bytes{org_ble_advertisement};
@@ -231,6 +231,10 @@ bool ConnectionFlow::Close() {
bool ConnectionFlow::InitPeerConnection(WebRtcMedium& webrtc_medium) {
Future<bool> success_future;
// CreatePeerConnection callback may be invoked after ConnectionFlow lifetime
// has ended, in case of a timeout. Future is captured by value, and is safe
// to access, but it is not safe to access ConnectionFlow member variables
// unless the Future::Set() returns true.
webrtc_medium.CreatePeerConnection(
&peer_connection_observer_,
[this, success_future](rtc::scoped_refptr<webrtc::PeerConnectionInterface>
@@ -240,6 +244,11 @@ bool ConnectionFlow::InitPeerConnection(WebRtcMedium& webrtc_medium) {
return;
}
// If this fails, means we have already assigned something to
// success_future; it is either:
// 1) this is the 2nd call of this callback (and this is a bug), or
// 2) Get(timeout) has set the future value as exception already.
if (success_future.IsSet()) return;
peer_connection_ = peer_connection;
success_future.Set(true);
});