Merge branch 'master' into release

Change-Id: I89401ad2264e728c6a9e5feadf00f92ce60c7248
This commit is contained in:
Alexey Polyudov
2020-09-03 02:23:32 -07:00
39 changed files with 1266 additions and 650 deletions
+77 -68
View File
@@ -174,7 +174,7 @@ api::BluetoothDevice* MediumEnvironment::FindBluetoothDevice(
const std::string& mac_address) {
api::BluetoothDevice* device = nullptr;
CountDownLatch latch(1);
RunOnMediumEnvironmentThread([this, &device, &latch, &mac_address](){
RunOnMediumEnvironmentThread([this, &device, &latch, &mac_address]() {
for (auto& item : bluetooth_mediums_) {
auto* adapter = item.second.adapter;
if (!adapter) continue;
@@ -320,85 +320,85 @@ void MediumEnvironment::UpdateBleMediumForAdvertising(
api::BleMedium& medium, api::BlePeripheral& peripheral,
const std::string& service_id, bool enabled) {
if (!enabled_) return;
RunOnMediumEnvironmentThread([this, &medium, &peripheral, service_id,
enabled]() {
auto item = ble_mediums_.find(&medium);
if (item == ble_mediums_.end()) {
NEARBY_LOG(INFO,
"UpdateBleMediumForAdvertising failed. There is no medium "
"registered.");
return;
}
auto& context = item->second;
context.ble_peripheral = &peripheral;
context.advertising = enabled;
NEARBY_LOG(INFO,
"Update Ble medium for advertising: this=%p; medium=%p; "
"service_id=%s; name=%s; enabled=%d; ",
this, &medium, service_id.c_str(), peripheral.GetName().c_str(),
enabled);
for (auto& medium_info : ble_mediums_) {
auto& local_medium = medium_info.first;
auto& info = medium_info.second;
// Do not send notification to the same medium.
if (local_medium == &medium) continue;
OnBlePeripheralStateChanged(info, peripheral, service_id, enabled);
}
});
RunOnMediumEnvironmentThread(
[this, &medium, &peripheral, service_id, enabled]() {
auto item = ble_mediums_.find(&medium);
if (item == ble_mediums_.end()) {
NEARBY_LOG(INFO,
"UpdateBleMediumForAdvertising failed. There is no medium "
"registered.");
return;
}
auto& context = item->second;
context.ble_peripheral = &peripheral;
context.advertising = enabled;
NEARBY_LOG(INFO,
"Update Ble medium for advertising: this=%p; medium=%p; "
"service_id=%s; name=%s; enabled=%d; ",
this, &medium, service_id.c_str(),
peripheral.GetName().c_str(), enabled);
for (auto& medium_info : ble_mediums_) {
auto& local_medium = medium_info.first;
auto& info = medium_info.second;
// Do not send notification to the same medium.
if (local_medium == &medium) continue;
OnBlePeripheralStateChanged(info, peripheral, service_id, enabled);
}
});
}
void MediumEnvironment::UpdateBleMediumForScanning(
api::BleMedium& medium, const std::string& service_id,
BleDiscoveredPeripheralCallback callback, bool enabled) {
if (!enabled_) return;
RunOnMediumEnvironmentThread([this, &medium, service_id,
callback = std::move(callback), enabled]() {
auto item = ble_mediums_.find(&medium);
if (item == ble_mediums_.end()) {
NEARBY_LOG(INFO,
"UpdateBleMediumFoScanning failed. There is no medium "
"registered.");
return;
}
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);
for (auto& medium_info : ble_mediums_) {
auto& local_medium = medium_info.first;
auto& info = medium_info.second;
// Do not send notification to the same medium.
if (local_medium == &medium) continue;
// Search advertising mediums and send notification.
if (info.advertising && enabled) {
OnBlePeripheralStateChanged(context, *(info.ble_peripheral), service_id,
enabled);
}
}
});
RunOnMediumEnvironmentThread(
[this, &medium, service_id, callback = std::move(callback), enabled]() {
auto item = ble_mediums_.find(&medium);
if (item == ble_mediums_.end()) {
NEARBY_LOG(INFO,
"UpdateBleMediumFoScanning failed. There is no medium "
"registered.");
return;
}
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);
for (auto& medium_info : ble_mediums_) {
auto& local_medium = medium_info.first;
auto& info = medium_info.second;
// Do not send notification to the same medium.
if (local_medium == &medium) continue;
// Search advertising mediums and send notification.
if (info.advertising && enabled) {
OnBlePeripheralStateChanged(context, *(info.ble_peripheral),
service_id, enabled);
}
}
});
}
void MediumEnvironment::UpdateBleMediumForAcceptedConnection(
api::BleMedium& medium, const std::string& service_id,
BleAcceptedConnectionCallback callback) {
if (!enabled_) return;
RunOnMediumEnvironmentThread([this, &medium, service_id,
callback = std::move(callback)]() {
auto item = ble_mediums_.find(&medium);
if (item == ble_mediums_.end()) {
NEARBY_LOG(
INFO, "Update Ble medium failed. There is no medium registered.");
return;
}
auto& context = item->second;
context.accepted_connection_callback = std::move(callback);
NEARBY_LOG(INFO,
"Update Ble medium for accepted callback: this=%p; "
"medium=%p; service_id=%s; ",
this, &medium, service_id.c_str());
});
RunOnMediumEnvironmentThread(
[this, &medium, service_id, callback = std::move(callback)]() {
auto item = ble_mediums_.find(&medium);
if (item == ble_mediums_.end()) {
NEARBY_LOG(
INFO, "Update Ble medium failed. There is no medium registered.");
return;
}
auto& context = item->second;
context.accepted_connection_callback = std::move(callback);
NEARBY_LOG(INFO,
"Update Ble medium for accepted callback: this=%p; "
"medium=%p; service_id=%s; ",
this, &medium, service_id.c_str());
});
}
void MediumEnvironment::UnregisterBleMedium(api::BleMedium& medium) {
@@ -465,6 +465,15 @@ void MediumEnvironment::SendWebRtcSignalingMessage(absl::string_view peer_id,
});
}
void MediumEnvironment::SetUseValidPeerConnection(
bool use_valid_peer_connection) {
use_valid_peer_connection_ = use_valid_peer_connection;
}
bool MediumEnvironment::GetUseValidPeerConnection() {
return use_valid_peer_connection_;
}
void MediumEnvironment::RegisterWifiLanMedium(api::WifiLanMedium& medium) {
if (!enabled_) return;
RunOnMediumEnvironmentThread([this, &medium]() {
+9 -1
View File
@@ -138,6 +138,12 @@ class MediumEnvironment {
void SendWebRtcSignalingMessage(absl::string_view peer_id,
const ByteArray& message);
// Used to set if WebRtcMedium should use a valid peer connection or nullptr
// in tests.
void SetUseValidPeerConnection(bool use_valid_peer_connection);
bool GetUseValidPeerConnection();
// Adds medium-related info to allow for scanning/advertising to work.
// This provides acccess to this medium from other mediums, when protocol
// expects they should communicate.
@@ -221,7 +227,7 @@ class MediumEnvironment {
// Returns WiFi LAN service matching IP address and port, or nullptr.
api::WifiLanService* FindWifiLanService(const std::string& ip_address,
int port);
int port);
private:
struct BluetoothMediumContext {
@@ -294,6 +300,8 @@ class MediumEnvironment {
absl::flat_hash_map<api::WifiLanMedium*, WifiLanMediumContext>
wifi_lan_mediums_;
bool use_valid_peer_connection_ = true;
};
} // namespace nearby
+6
View File
@@ -47,6 +47,12 @@ void WebRtcSignalingMessenger::StopReceivingMessages() {
void WebRtcMedium::CreatePeerConnection(
webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) {
auto& env = MediumEnvironment::Instance();
if (!env.GetUseValidPeerConnection()) {
callback(nullptr);
return;
}
webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
webrtc::PeerConnectionDependencies dependencies(observer);
+2 -1
View File
@@ -60,7 +60,8 @@ bool BleMedium::StartScanning(const std::string& service_id,
&context.peripheral, &peripheral,
peripheral.GetName().c_str());
discovered_peripheral_callback_.peripheral_discovered_cb(
context.peripheral, service_id);
context.peripheral, service_id,
/*fast_advertisement=*/false);
}
},
.peripheral_lost_cb =
+3 -2
View File
@@ -86,9 +86,10 @@ class BleMedium final {
using Platform = api::ImplementationPlatform;
struct DiscoveredPeripheralCallback {
std::function<void(BlePeripheral& peripheral,
const std::string& service_id)>
const std::string& service_id,
bool fast_advertisement)>
peripheral_discovered_cb =
DefaultCallback<BlePeripheral&, const std::string&>();
DefaultCallback<BlePeripheral&, const std::string&, bool>();
std::function<void(BlePeripheral& peripheral,
const std::string& service_id)>
peripheral_lost_cb =
+40 -36
View File
@@ -69,13 +69,13 @@ TEST_F(BleMediumTest, CanStartAdvertising) {
ble_a.StartAdvertising(service_id, advertisement_bytes);
EXPECT_TRUE(ble_b.StartScanning(
service_id, DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](BlePeripheral& peripheral,
const std::string& service_id) {
found_latch.CountDown();
},
}));
service_id,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](
BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement) { found_latch.CountDown(); },
}));
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
EXPECT_TRUE(ble_a.StopAdvertising(service_id));
EXPECT_TRUE(ble_b.StopScanning(service_id));
@@ -93,19 +93,19 @@ TEST_F(BleMediumTest, CanStartScanning) {
CountDownLatch found_latch(1);
CountDownLatch lost_latch(1);
ble_a.StartScanning(service_id,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](BlePeripheral& peripheral,
const std::string& service_id) {
found_latch.CountDown();
},
.peripheral_lost_cb =
[&lost_latch](BlePeripheral& peripheral,
const std::string& service_id) {
lost_latch.CountDown();
},
});
ble_a.StartScanning(
service_id,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](
BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement) { found_latch.CountDown(); },
.peripheral_lost_cb =
[&lost_latch](BlePeripheral& peripheral,
const std::string& service_id) {
lost_latch.CountDown();
},
});
EXPECT_TRUE(ble_b.StartAdvertising(service_id, advertisement_bytes));
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
EXPECT_TRUE(ble_b.StopAdvertising(service_id));
@@ -125,19 +125,19 @@ TEST_F(BleMediumTest, CanStopDiscovery) {
CountDownLatch found_latch(1);
CountDownLatch lost_latch(1);
ble_a.StartScanning(service_id,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](BlePeripheral& peripheral,
const std::string& service_id) {
found_latch.CountDown();
},
.peripheral_lost_cb =
[&lost_latch](BlePeripheral& peripheral,
const std::string& service_id) {
lost_latch.CountDown();
},
});
ble_a.StartScanning(
service_id,
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch](
BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement) { found_latch.CountDown(); },
.peripheral_lost_cb =
[&lost_latch](BlePeripheral& peripheral,
const std::string& service_id) {
lost_latch.CountDown();
},
});
EXPECT_TRUE(ble_b.StartAdvertising(service_id, advertisement_bytes));
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
EXPECT_TRUE(ble_a.StopScanning(service_id));
@@ -163,9 +163,13 @@ TEST_F(BleMediumTest, CanStartAcceptingConnectionsAndConnect) {
DiscoveredPeripheralCallback{
.peripheral_discovered_cb =
[&found_latch, &discovered_peripheral](
BlePeripheral& peripheral, const std::string& service_id) {
NEARBY_LOG(INFO, "Peripheral discovered: %s, %p",
peripheral.GetName().c_str(), &peripheral);
BlePeripheral& peripheral, const std::string& service_id,
bool fast_advertisement) {
NEARBY_LOG(
INFO,
"Peripheral discovered: %s, %p, fast advertisement: %d",
peripheral.GetName().c_str(), &peripheral,
fast_advertisement);
discovered_peripheral = &peripheral;
found_latch.CountDown();
},