mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Roll forward to cl/314549634
Change-Id: I4d1e7eacd5fa4078a094571ad6d5f51e422535ff
This commit is contained in:
committed by
Alexey Polyudov
parent
ae1c427b99
commit
cffbc04508
@@ -1,38 +1,32 @@
|
||||
cc_library(
|
||||
name = "g3",
|
||||
name = "types",
|
||||
testonly = True,
|
||||
srcs = [
|
||||
"scheduled_executor.cc",
|
||||
"system_clock.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"atomic_boolean.h",
|
||||
"atomic_reference_any.h",
|
||||
"bluetooth_adapter.cc",
|
||||
"bluetooth_adapter.h",
|
||||
"condition_variable.h",
|
||||
"count_down_latch.h",
|
||||
"medium_environment.cc",
|
||||
"medium_environment.h",
|
||||
"multi_thread_executor.h",
|
||||
"mutex.h",
|
||||
"platform.cc",
|
||||
"scheduled_executor.cc",
|
||||
"pipe.h",
|
||||
"scheduled_executor.h",
|
||||
"settable_future_any.h",
|
||||
"single_thread_executor.h",
|
||||
"system_clock.cc",
|
||||
],
|
||||
visibility = [
|
||||
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
|
||||
"//core_v2:__subpackages__",
|
||||
"//platform_v2:__subpackages__",
|
||||
"//platform_v2/impl/g3:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":crypto", # build_cleaner: keep
|
||||
"//platform_v2/api",
|
||||
"//platform_v2/api:platform",
|
||||
"//platform_v2/api:types",
|
||||
"//platform_v2/base",
|
||||
"//platform_v2/base:util",
|
||||
"//platform_v2/impl/shared:posix_mutex",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/container:flat_hash_map",
|
||||
"//absl/container:flat_hash_set",
|
||||
"//absl/memory",
|
||||
"//absl/strings",
|
||||
"//absl/synchronization",
|
||||
"//absl/time",
|
||||
"//absl/types:any",
|
||||
@@ -40,8 +34,36 @@ cc_library(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "comm",
|
||||
testonly = True,
|
||||
srcs = [
|
||||
"bluetooth_adapter.cc",
|
||||
"webrtc.cc",
|
||||
],
|
||||
hdrs = [
|
||||
"bluetooth_adapter.h",
|
||||
"webrtc.h",
|
||||
],
|
||||
visibility = [
|
||||
"//platform_v2/impl/g3:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
":types",
|
||||
"//platform_v2/api:comm",
|
||||
"//platform_v2/base:test_util",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/strings",
|
||||
"//absl/synchronization",
|
||||
"//webrtc/api:create_peerconnection_factory", #buildcleaner: keep
|
||||
"//webrtc/api:libjingle_peerconnection_api",
|
||||
"//webrtc/api/task_queue:default_task_queue_factory",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "crypto",
|
||||
testonly = True,
|
||||
srcs = [
|
||||
"crypto.cc",
|
||||
],
|
||||
@@ -49,9 +71,34 @@ cc_library(
|
||||
"//platform_v2/g3:__pkg__",
|
||||
],
|
||||
deps = [
|
||||
"//platform_v2/api",
|
||||
"//platform_v2/api:types",
|
||||
"//platform_v2/base",
|
||||
"//absl/strings",
|
||||
"//openssl:crypto",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "g3",
|
||||
testonly = True,
|
||||
srcs = [
|
||||
"platform.cc",
|
||||
],
|
||||
visibility = [
|
||||
"//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__",
|
||||
"//core_v2:__subpackages__",
|
||||
"//platform_v2:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":comm",
|
||||
":crypto", # build_cleaner: keep
|
||||
":types",
|
||||
"//platform_v2/api:comm",
|
||||
"//platform_v2/api:platform",
|
||||
"//platform_v2/api:types",
|
||||
"//platform_v2/impl/shared:file",
|
||||
"//absl/base:core_headers",
|
||||
"//absl/memory",
|
||||
"//absl/time",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "platform_v2/impl/g3/medium_environment.h"
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -11,15 +11,22 @@ namespace g3 {
|
||||
BluetoothDevice::BluetoothDevice(BluetoothAdapter* adapter)
|
||||
: adapter_(*adapter) {}
|
||||
|
||||
BluetoothAdapter::~BluetoothAdapter() { SetStatus(Status::kDisabled); }
|
||||
|
||||
std::string BluetoothDevice::GetName() const { return adapter_.GetName(); }
|
||||
|
||||
bool BluetoothAdapter::SetStatus(Status status) ABSL_LOCKS_EXCLUDED(mutex_) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
enabled_ = (status == Status::kEnabled);
|
||||
RunOnCallbackThread([this]() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.OnBluetoothAdapterChangedState(*this);
|
||||
});
|
||||
bool BluetoothAdapter::SetStatus(Status status) {
|
||||
BluetoothAdapter::ScanMode mode;
|
||||
bool enabled = status == Status::kEnabled;
|
||||
std::string name;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
enabled_ = enabled;
|
||||
name = name_;
|
||||
mode = mode_;
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.OnBluetoothAdapterChangedState(*this, device_, name, enabled, mode);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -34,13 +41,17 @@ BluetoothAdapter::ScanMode BluetoothAdapter::GetScanMode() const {
|
||||
}
|
||||
|
||||
bool BluetoothAdapter::SetScanMode(BluetoothAdapter::ScanMode mode) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (enabled_) return false;
|
||||
mode_ = mode;
|
||||
RunOnCallbackThread([this]() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.OnBluetoothAdapterChangedState(*this);
|
||||
});
|
||||
bool enabled;
|
||||
std::string name;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
mode_ = mode;
|
||||
name = name_;
|
||||
enabled = enabled_;
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.OnBluetoothAdapterChangedState(*this, device_, std::move(name), enabled,
|
||||
mode);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -50,13 +61,17 @@ std::string BluetoothAdapter::GetName() const {
|
||||
}
|
||||
|
||||
bool BluetoothAdapter::SetName(absl::string_view name) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
if (enabled_) return false;
|
||||
name_ = name;
|
||||
RunOnCallbackThread([this]() {
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.OnBluetoothAdapterChangedState(*this);
|
||||
});
|
||||
BluetoothAdapter::ScanMode mode;
|
||||
bool enabled;
|
||||
{
|
||||
absl::MutexLock lock(&mutex_);
|
||||
name_ = name;
|
||||
enabled = enabled_;
|
||||
mode = mode_;
|
||||
}
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
env.OnBluetoothAdapterChangedState(*this, device_, std::string(name), enabled,
|
||||
mode);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ class BluetoothDevice : public api::BluetoothDevice {
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getName()
|
||||
std::string GetName() const override;
|
||||
BluetoothAdapter& GetAdapter();
|
||||
BluetoothAdapter& GetAdapter() { return adapter_; }
|
||||
|
||||
private:
|
||||
// Only BluetoothAdapter may instantiate BluetoothDevice.
|
||||
@@ -41,8 +41,8 @@ class BluetoothAdapter : public api::BluetoothAdapter {
|
||||
using Status = api::BluetoothAdapter::Status;
|
||||
using ScanMode = api::BluetoothAdapter::ScanMode;
|
||||
|
||||
BluetoothAdapter() = default;
|
||||
~BluetoothAdapter() override = default;
|
||||
explicit BluetoothAdapter() = default;
|
||||
~BluetoothAdapter() override;
|
||||
|
||||
// Synchronously sets the status of the BluetoothAdapter to 'status', and
|
||||
// returns true if the operation was a success.
|
||||
@@ -71,16 +71,11 @@ class BluetoothAdapter : public api::BluetoothAdapter {
|
||||
BluetoothDevice& GetDevice() { return device_; }
|
||||
|
||||
private:
|
||||
void RunOnCallbackThread(std::function<void()> runnable) {
|
||||
serial_executor_.Execute(std::move(runnable));
|
||||
}
|
||||
|
||||
mutable absl::Mutex mutex_;
|
||||
BluetoothDevice device_{this};
|
||||
ScanMode mode_ ABSL_GUARDED_BY(mutex_) = ScanMode::kNone;
|
||||
std::string name_ ABSL_GUARDED_BY(mutex_) = "unknown G3 BT device";
|
||||
bool enabled_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
SingleThreadExecutor serial_executor_;
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
#include "platform_v2/impl/g3/medium_environment.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
|
||||
MediumEnvironment& MediumEnvironment::Instance() {
|
||||
static std::aligned_storage_t<sizeof(MediumEnvironment),
|
||||
alignof(MediumEnvironment)>
|
||||
storage;
|
||||
static MediumEnvironment* env = new (&storage) MediumEnvironment();
|
||||
return *env;
|
||||
}
|
||||
|
||||
void MediumEnvironment::Reset() {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
bluetooth_adapters_.clear();
|
||||
}
|
||||
|
||||
void MediumEnvironment::OnBluetoothAdapterChangedState(
|
||||
BluetoothAdapter& adapter) {
|
||||
absl::MutexLock lock(&mutex_);
|
||||
// We don't care if there is an adapter already since all we store is a
|
||||
// pointer.
|
||||
bluetooth_adapters_.emplace(&adapter);
|
||||
// TODO(apolyudov): Add event propagation code when Medium registration is
|
||||
// implemented.
|
||||
}
|
||||
|
||||
} // namespace g3
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -1,47 +0,0 @@
|
||||
#ifndef PLATFORM_V2_IMPL_G3_MEDIUM_ENVIRONMENT_H_
|
||||
#define PLATFORM_V2_IMPL_G3_MEDIUM_ENVIRONMENT_H_
|
||||
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "platform_v2/api/bluetooth_classic.h"
|
||||
#include "platform_v2/impl/g3/bluetooth_adapter.h"
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
|
||||
// MediumEnvironment is a simulated environment which allowes multiple instances
|
||||
// of simulated HW devices to "work" together as if they are physical.
|
||||
// For each medium type it provides necessary methods to implement
|
||||
// advertising, discovery and establishment of a data link.
|
||||
class MediumEnvironment {
|
||||
public:
|
||||
~MediumEnvironment() = default;
|
||||
// Singleton constructor/accessor.
|
||||
static MediumEnvironment& Instance();
|
||||
|
||||
// Clear state. No notifications are sent.
|
||||
void Reset() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Add an adapter to internal container.
|
||||
// Notify BluetoothClassicMediums if any that adapter state has changed.
|
||||
void OnBluetoothAdapterChangedState(BluetoothAdapter& adapter)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
MediumEnvironment() = default;
|
||||
absl::Mutex mutex_;
|
||||
absl::flat_hash_set<BluetoothAdapter*> bluetooth_adapters_
|
||||
ABSL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_V2_IMPL_G3_MEDIUM_ENVIRONMENT_H_
|
||||
@@ -28,6 +28,8 @@
|
||||
#include "platform_v2/impl/g3/scheduled_executor.h"
|
||||
#include "platform_v2/impl/g3/settable_future_any.h"
|
||||
#include "platform_v2/impl/g3/single_thread_executor.h"
|
||||
#include "platform_v2/impl/g3/webrtc.h"
|
||||
#include "platform_v2/impl/shared/file.h"
|
||||
#include "absl/base/integral_types.h"
|
||||
#include "absl/memory/memory.h"
|
||||
#include "absl/time/time.h"
|
||||
@@ -36,6 +38,12 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
|
||||
namespace {
|
||||
std::string GetPayloadPath(std::int64_t payload_id) {
|
||||
return "/tmp/" + std::to_string(payload_id);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<SubmittableExecutor>
|
||||
ImplementationPlatform::CreateSingleThreadExecutor() {
|
||||
return absl::make_unique<g3::SingleThreadExecutor>();
|
||||
@@ -76,6 +84,17 @@ std::unique_ptr<AtomicBoolean> ImplementationPlatform::CreateAtomicBoolean(
|
||||
return absl::make_unique<g3::AtomicBoolean>(initial_value);
|
||||
}
|
||||
|
||||
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
|
||||
std::int64_t payload_id, std::int64_t total_size) {
|
||||
return absl::make_unique<shared::InputFile>(GetPayloadPath(payload_id),
|
||||
total_size);
|
||||
}
|
||||
|
||||
std::unique_ptr<OutputFile> ImplementationPlatform::CreateOutputFile(
|
||||
std::int64_t payload_id) {
|
||||
return absl::make_unique<shared::OutputFile>(GetPayloadPath(payload_id));
|
||||
}
|
||||
|
||||
std::unique_ptr<BluetoothClassicMedium>
|
||||
ImplementationPlatform::CreateBluetoothClassicMedium() {
|
||||
return std::unique_ptr<BluetoothClassicMedium>();
|
||||
@@ -102,11 +121,8 @@ std::unique_ptr<WifiLanMedium> ImplementationPlatform::CreateWifiLanMedium() {
|
||||
return std::unique_ptr<WifiLanMedium>();
|
||||
}
|
||||
|
||||
std::unique_ptr<WebRtcSignalingMessenger>
|
||||
ImplementationPlatform::CreateWebRtcSignalingMessenger(
|
||||
absl::string_view self_id) {
|
||||
return std::unique_ptr<WebRtcSignalingMessenger>(
|
||||
/*new FCMSignalingMessenger()*/);
|
||||
std::unique_ptr<WebRtcMedium> ImplementationPlatform::CreateWebRtcMedium() {
|
||||
return absl::make_unique<g3::WebRtcMedium>();
|
||||
}
|
||||
|
||||
std::unique_ptr<Mutex> ImplementationPlatform::CreateMutex(Mutex::Mode mode) {
|
||||
@@ -127,10 +143,6 @@ std::string ImplementationPlatform::GetDeviceId() {
|
||||
return "google3";
|
||||
}
|
||||
|
||||
std::string ImplementationPlatform::GetPayloadPath(int64_t payload_id) {
|
||||
return "/tmp/" + std::to_string(payload_id);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "platform_v2/impl/g3/webrtc.h"
|
||||
|
||||
#include "webrtc/api/task_queue/default_task_queue_factory.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
|
||||
void WebRtcMedium::CreatePeerConnection(
|
||||
webrtc::PeerConnectionObserver* observer, PeerConnectionCallback callback) {
|
||||
webrtc::PeerConnectionInterface::RTCConfiguration rtc_config;
|
||||
webrtc::PeerConnectionDependencies dependencies(observer);
|
||||
|
||||
std::unique_ptr<rtc::Thread> signaling_thread = rtc::Thread::Create();
|
||||
signaling_thread->SetName("signaling_thread", nullptr);
|
||||
RTC_CHECK(signaling_thread->Start()) << "Failed to start thread";
|
||||
|
||||
webrtc::PeerConnectionFactoryDependencies factory_dependencies;
|
||||
factory_dependencies.task_queue_factory =
|
||||
webrtc::CreateDefaultTaskQueueFactory();
|
||||
factory_dependencies.signaling_thread = signaling_thread.release();
|
||||
|
||||
callback(webrtc::CreateModularPeerConnectionFactory(
|
||||
std::move(factory_dependencies))
|
||||
->CreatePeerConnection(rtc_config, std::move(dependencies)));
|
||||
}
|
||||
|
||||
std::unique_ptr<api::WebRtcSignalingMessenger>
|
||||
WebRtcMedium::GetSignalingMessenger(absl::string_view self_id) {
|
||||
// TODO(bfranz): Implement
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace g3
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
@@ -0,0 +1,35 @@
|
||||
#ifndef PLATFORM_V2_IMPL_G3_WEBRTC_H_
|
||||
#define PLATFORM_V2_IMPL_G3_WEBRTC_H_
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "platform_v2/api/webrtc.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "webrtc/api/peer_connection_interface.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace g3 {
|
||||
|
||||
class WebRtcMedium : public api::WebRtcMedium {
|
||||
public:
|
||||
using PeerConnectionCallback = api::WebRtcMedium::PeerConnectionCallback;
|
||||
|
||||
WebRtcMedium() = default;
|
||||
~WebRtcMedium() override = default;
|
||||
|
||||
// Creates and returns a new webrtc::PeerConnectionInterface object via
|
||||
// |callback|.
|
||||
void CreatePeerConnection(webrtc::PeerConnectionObserver* observer,
|
||||
PeerConnectionCallback callback) override;
|
||||
|
||||
// Returns a signaling messenger for sending WebRTC signaling messages.
|
||||
std::unique_ptr<api::WebRtcSignalingMessenger> GetSignalingMessenger(
|
||||
absl::string_view self_id) override;
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
#endif // PLATFORM_V2_IMPL_G3_WEBRTC_H_
|
||||
Reference in New Issue
Block a user