mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Merge branch 'master' into release.
Change-Id: Id627ceca5ef60281e024ff80fc1848b3ccb6c14d
This commit is contained in:
@@ -13,40 +13,34 @@
|
||||
# limitations under the License.
|
||||
|
||||
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",
|
||||
@@ -54,8 +48,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",
|
||||
],
|
||||
@@ -63,9 +85,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",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "platform_v2/impl/g3/medium_environment.h"
|
||||
#include "platform_v2/base/medium_environment.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
@@ -25,15 +25,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;
|
||||
}
|
||||
|
||||
@@ -48,13 +55,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;
|
||||
}
|
||||
|
||||
@@ -64,13 +75,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,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.
|
||||
@@ -55,8 +55,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.
|
||||
@@ -85,16 +85,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,46 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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,61 +0,0 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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_
|
||||
@@ -42,6 +42,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"
|
||||
@@ -50,6 +52,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>();
|
||||
@@ -90,6 +98,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>();
|
||||
@@ -116,11 +135,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) {
|
||||
@@ -141,10 +157,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,50 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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,49 @@
|
||||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#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