Merge branch 'master' into release

Change-Id: I56ea2217899e92bdd9d6cb56797ac9895e194fff
This commit is contained in:
Alexey Polyudov
2020-06-24 11:01:46 -07:00
147 changed files with 8642 additions and 1214 deletions
+7 -2
View File
@@ -16,25 +16,27 @@ cc_library(
name = "types",
testonly = True,
srcs = [
"log_message.cc",
"scheduled_executor.cc",
"system_clock.cc",
],
hdrs = [
"atomic_boolean.h",
"atomic_reference_any.h",
"atomic_reference.h",
"condition_variable.h",
"count_down_latch.h",
"log_message.h",
"multi_thread_executor.h",
"mutex.h",
"pipe.h",
"scheduled_executor.h",
"settable_future_any.h",
"single_thread_executor.h",
],
visibility = [
"//platform_v2/impl/g3:__pkg__",
],
deps = [
"//base",
"//platform_v2/api:platform",
"//platform_v2/api:types",
"//platform_v2/base",
@@ -55,11 +57,13 @@ cc_library(
"bluetooth_adapter.cc",
"bluetooth_classic.cc",
"webrtc.cc",
"wifi_lan.cc",
],
hdrs = [
"bluetooth_adapter.h",
"bluetooth_classic.h",
"webrtc.h",
"wifi_lan.h",
],
visibility = [
"//platform_v2/impl/g3:__pkg__",
@@ -119,6 +123,7 @@ cc_library(
"//platform_v2/impl/shared:file",
"//absl/base:core_headers",
"//absl/memory",
"//absl/strings",
"//absl/time",
],
)
@@ -0,0 +1,33 @@
#ifndef PLATFORM_V2_IMPL_G3_ATOMIC_REFERENCE_H_
#define PLATFORM_V2_IMPL_G3_ATOMIC_REFERENCE_H_
#include <atomic>
#include <cstdint>
#include "platform_v2/api/atomic_reference.h"
namespace location {
namespace nearby {
namespace g3 {
class AtomicUint32 : public api::AtomicUint32 {
public:
explicit AtomicUint32(std::int32_t value) : value_(value) {}
~AtomicUint32() override = default;
std::uint32_t Get() const override {
return value_;
}
void Set(std::uint32_t value) override {
value_ = value;
}
private:
std::atomic<std::uint32_t> value_;
};
} // namespace g3
} // namespace nearby
} // namespace location
#endif // PLATFORM_V2_IMPL_G3_ATOMIC_REFERENCE_H_
@@ -1,60 +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_ATOMIC_REFERENCE_ANY_H_
#define PLATFORM_V2_IMPL_G3_ATOMIC_REFERENCE_ANY_H_
#include "platform_v2/api/atomic_reference.h"
#include "absl/base/integral_types.h"
#include "absl/synchronization/mutex.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
namespace g3 {
// Provide implementation for absl::any.
class AtomicReferenceAny : public api::AtomicReference<absl::any> {
public:
explicit AtomicReferenceAny(absl::any initial_value)
: value_(std::move(initial_value)) {}
~AtomicReferenceAny() override = default;
absl::any Get() const & override {
absl::MutexLock lock(&mutex_);
return value_;
}
absl::any Get() && override {
absl::MutexLock lock(&mutex_);
return std::move(value_);
}
void Set(const absl::any& value) override {
absl::MutexLock lock(&mutex_);
value_ = value;
}
void Set(absl::any&& value) override {
absl::MutexLock lock(&mutex_);
value_ = std::move(value);
}
private:
mutable absl::Mutex mutex_;
absl::any value_;
};
} // namespace g3
} // namespace nearby
} // namespace location
#endif // PLATFORM_V2_IMPL_G3_ATOMIC_REFERENCE_ANY_H_
+22 -16
View File
@@ -27,9 +27,15 @@ namespace location {
namespace nearby {
namespace g3 {
BluetoothSocket::~BluetoothSocket() {
absl::MutexLock lock(&mutex_);
DoClose();
}
void BluetoothSocket::Connect(BluetoothSocket& other) {
absl::MutexLock lock(&mutex_);
remote_socket_ = &other;
input_ = other.output_;
}
bool BluetoothSocket::IsConnected() const {
@@ -43,7 +49,7 @@ bool BluetoothSocket::IsClosed() const {
}
bool BluetoothSocket::IsConnectedLocked() const {
return remote_socket_ != nullptr;
return input_ != nullptr;
}
InputStream& BluetoothSocket::GetInputStream() {
@@ -58,31 +64,31 @@ OutputStream& BluetoothSocket::GetOutputStream() {
InputStream& BluetoothSocket::GetLocalInputStream() {
absl::MutexLock lock(&mutex_);
return output_.GetInputStream();
return output_->GetInputStream();
}
OutputStream& BluetoothSocket::GetLocalOutputStream() {
absl::MutexLock lock(&mutex_);
return output_.GetOutputStream();
return output_->GetOutputStream();
}
Exception BluetoothSocket::Close() {
BluetoothSocket* remote_socket = nullptr;
{
absl::MutexLock lock(&mutex_);
if (!closed_) {
remote_socket = remote_socket_;
output_.GetOutputStream().Close();
output_.GetInputStream().Close();
closed_ = true;
}
}
if (remote_socket != nullptr) {
remote_socket->Close();
}
absl::MutexLock lock(&mutex_);
DoClose();
return {Exception::kSuccess};
}
void BluetoothSocket::DoClose() {
if (!closed_) {
remote_socket_ = nullptr;
output_->GetOutputStream().Close();
output_->GetInputStream().Close();
input_->GetOutputStream().Close();
input_->GetInputStream().Close();
closed_ = true;
}
}
BluetoothSocket* BluetoothSocket::GetRemoteSocket() {
absl::MutexLock lock(&mutex_);
return remote_socket_;
+5 -2
View File
@@ -39,7 +39,7 @@ class BluetoothSocket : public api::BluetoothSocket {
public:
BluetoothSocket() = default;
explicit BluetoothSocket(BluetoothAdapter* adapter) : adapter_(adapter) {}
~BluetoothSocket() override = default;
~BluetoothSocket() override;
// Connects to another BluetoothSocket, to form a functional low-level
// channel. From this point on, and until Close is called, connection exists.
@@ -78,6 +78,8 @@ class BluetoothSocket : public api::BluetoothSocket {
BluetoothDevice* GetRemoteDevice() override ABSL_LOCKS_EXCLUDED(mutex_);
private:
void DoClose() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Returns true if connection exists to the (possibly closed) remote socket.
bool IsConnectedLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -94,7 +96,8 @@ class BluetoothSocket : public api::BluetoothSocket {
// Output pipe is initialized by constructor, it remains always valid, until
// it is closed. it represents output part of a local socket. Input part of a
// local socket comes from the peer socket, after connection.
Pipe output_;
std::shared_ptr<Pipe> output_ {new Pipe};
std::shared_ptr<Pipe> input_;
mutable absl::Mutex mutex_;
BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only.
BluetoothSocket* remote_socket_ ABSL_GUARDED_BY(mutex_) = nullptr;
@@ -33,6 +33,11 @@ class ConditionVariable : public api::ConditionVariable {
cond_var_.Wait(mutex_);
return {Exception::kSuccess};
}
Exception Wait(absl::Duration timeout) override {
return cond_var_.WaitWithTimeout(mutex_, timeout)
? Exception{Exception::kTimeout}
: Exception{Exception::kSuccess};
}
void Notify() override { cond_var_.SignalAll(); }
private:
+56
View File
@@ -0,0 +1,56 @@
#include "platform_v2/impl/g3/log_message.h"
#include <algorithm>
#include "base/stringprintf.h"
namespace location {
namespace nearby {
namespace g3 {
api::LogMessage::Severity g_min_log_severity = api::LogMessage::Severity::kInfo;
inline absl::LogSeverity ConvertSeverity(api::LogMessage::Severity severity) {
switch (severity) {
case api::LogMessage::Severity::kInfo:
return absl::LogSeverity::kInfo;
case api::LogMessage::Severity::kWarning:
return absl::LogSeverity::kWarning;
case api::LogMessage::Severity::kError:
return absl::LogSeverity::kError;
case api::LogMessage::Severity::kFatal:
return absl::LogSeverity::kFatal;
}
}
LogMessage::LogMessage(const char* file, int line, Severity severity)
: log_streamer_(ConvertSeverity(severity), file, line) {}
LogMessage::~LogMessage() = default;
void LogMessage::Print(const char* format, ...) {
va_list ap;
va_start(ap, format);
std::string result;
StringAppendV(&result, format, ap);
log_streamer_.stream() << result;
va_end(ap);
}
std::ostream& LogMessage::Stream() { return log_streamer_.stream(); }
} // namespace g3
namespace api {
void LogMessage::SetMinLogSeverity(Severity severity) {
g3::g_min_log_severity = severity;
}
bool LogMessage::ShouldCreateLogMessage(Severity severity) {
return severity >= g3::g_min_log_severity;
}
} // namespace api
} // namespace nearby
} // namespace location
+29
View File
@@ -0,0 +1,29 @@
#ifndef PLATFORM_V2_IMPL_G3_LOG_MESSAGE_H_
#define PLATFORM_V2_IMPL_G3_LOG_MESSAGE_H_
#include "base/logging.h"
#include "platform_v2/api/log_message.h"
namespace location {
namespace nearby {
namespace g3 {
// See documentation in cpp/platform_v2/api/log_message.h
class LogMessage : public api::LogMessage {
public:
LogMessage(const char* file, int line, Severity severity);
~LogMessage() override;
void Print(const char* format, ...) override;
std::ostream& Stream() override;
private:
absl::LogStreamer log_streamer_;
};
} // namespace g3
} // namespace nearby
} // namespace location
#endif // PLATFORM_V2_IMPL_G3_LOG_MESSAGE_H_
+18 -21
View File
@@ -25,28 +25,30 @@
#include "platform_v2/api/bluetooth_classic.h"
#include "platform_v2/api/condition_variable.h"
#include "platform_v2/api/count_down_latch.h"
#include "platform_v2/api/log_message.h"
#include "platform_v2/api/mutex.h"
#include "platform_v2/api/scheduled_executor.h"
#include "platform_v2/api/server_sync.h"
#include "platform_v2/api/settable_future.h"
#include "platform_v2/api/submittable_executor.h"
#include "platform_v2/api/webrtc.h"
#include "platform_v2/api/wifi.h"
#include "platform_v2/impl/g3/atomic_boolean.h"
#include "platform_v2/impl/g3/atomic_reference_any.h"
#include "platform_v2/impl/g3/atomic_reference.h"
#include "platform_v2/impl/g3/bluetooth_adapter.h"
#include "platform_v2/impl/g3/bluetooth_classic.h"
#include "platform_v2/impl/g3/condition_variable.h"
#include "platform_v2/impl/g3/count_down_latch.h"
#include "platform_v2/impl/g3/log_message.h"
#include "platform_v2/impl/g3/multi_thread_executor.h"
#include "platform_v2/impl/g3/mutex.h"
#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/g3/wifi_lan.h"
#include "platform_v2/impl/shared/file.h"
#include "absl/base/integral_types.h"
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/time/time.h"
namespace location {
@@ -54,8 +56,8 @@ namespace nearby {
namespace api {
namespace {
std::string GetPayloadPath(std::int64_t payload_id) {
return "/tmp/" + std::to_string(payload_id);
std::string GetPayloadPath(PayloadId payload_id) {
return absl::StrCat("/tmp/", payload_id);
}
} // namespace
@@ -74,14 +76,9 @@ ImplementationPlatform::CreateScheduledExecutor() {
return absl::make_unique<g3::ScheduledExecutor>();
}
std::unique_ptr<AtomicReference<absl::any>>
ImplementationPlatform::CreateAtomicReferenceAny(absl::any initial_value) {
return absl::make_unique<g3::AtomicReferenceAny>(initial_value);
}
std::unique_ptr<SettableFuture<absl::any>>
ImplementationPlatform::CreateSettableFutureAny() {
return absl::make_unique<g3::SettableFutureAny>();
std::unique_ptr<AtomicUint32>
ImplementationPlatform::CreateAtomicUint32(std::uint32_t value) {
return absl::make_unique<g3::AtomicUint32>(value);
}
std::unique_ptr<BluetoothAdapter>
@@ -100,16 +97,21 @@ std::unique_ptr<AtomicBoolean> ImplementationPlatform::CreateAtomicBoolean(
}
std::unique_ptr<InputFile> ImplementationPlatform::CreateInputFile(
std::int64_t payload_id, std::int64_t total_size) {
PayloadId 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) {
PayloadId payload_id) {
return absl::make_unique<shared::OutputFile>(GetPayloadPath(payload_id));
}
std::unique_ptr<LogMessage> ImplementationPlatform::CreateLogMessage(
const char* file, int line, LogMessage::Severity severity) {
return absl::make_unique<g3::LogMessage>(file, line, severity);
}
std::unique_ptr<BluetoothClassicMedium>
ImplementationPlatform::CreateBluetoothClassicMedium(
api::BluetoothAdapter& adapter) {
@@ -136,7 +138,7 @@ std::unique_ptr<WifiMedium> ImplementationPlatform::CreateWifiMedium() {
}
std::unique_ptr<WifiLanMedium> ImplementationPlatform::CreateWifiLanMedium() {
return std::unique_ptr<WifiLanMedium>();
return absl::make_unique<g3::WifiLanMedium>();
}
std::unique_ptr<WebRtcMedium> ImplementationPlatform::CreateWebRtcMedium() {
@@ -156,11 +158,6 @@ ImplementationPlatform::CreateConditionVariable(Mutex* mutex) {
new g3::ConditionVariable(static_cast<g3::Mutex*>(mutex)));
}
std::string ImplementationPlatform::GetDeviceId() {
// TODO(alexchau): Get deviceId from base
return "google3";
}
} // namespace api
} // namespace nearby
} // namespace location
@@ -1,118 +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_SETTABLE_FUTURE_ANY_H_
#define PLATFORM_V2_IMPL_G3_SETTABLE_FUTURE_ANY_H_
#include <utility>
#include "platform_v2/api/platform.h"
#include "platform_v2/api/settable_future.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/types/any.h"
namespace location {
namespace nearby {
namespace g3 {
class SettableFutureAny : public api::SettableFuture<absl::any> {
public:
SettableFutureAny() = default;
~SettableFutureAny() override = default;
bool Set(const absl::any& value) override {
absl::MutexLock lock(&mutex_);
if (!done_) {
value_ = value;
done_ = true;
exception_ = {Exception::kSuccess};
completed_.SignalAll();
}
return true;
}
bool Set(absl::any&& value) override {
absl::MutexLock lock(&mutex_);
if (!done_) {
value_ = std::move(value);
done_ = true;
exception_ = {Exception::kSuccess};
completed_.SignalAll();
}
return true;
}
bool SetException(Exception exception) override {
absl::MutexLock lock(&mutex_);
return SetExceptionLocked(exception);
}
void AddListener(Runnable runnable, api::Executor* executor) override {}
ExceptionOr<std::any> Get() override {
absl::MutexLock lock(&mutex_);
while (!done_) {
completed_.Wait(&mutex_);
}
return exception_.value != Exception::kSuccess
? ExceptionOr<std::any>{exception_.value}
: ExceptionOr<std::any>{value_};
}
ExceptionOr<std::any> Get(absl::Duration timeout) override {
absl::MutexLock lock(&mutex_);
while (!done_) {
absl::Time start_time = absl::Now();
if (completed_.WaitWithTimeout(&mutex_, timeout)) {
SetExceptionLocked({Exception::kTimeout});
break;
}
absl::Duration spent = absl::Now() - start_time;
if (spent < timeout) {
timeout -= spent;
} else if (!done_) {
SetExceptionLocked({Exception::kTimeout});
break;
}
}
return exception_.value != Exception::kSuccess
? ExceptionOr<std::any>{exception_.value}
: ExceptionOr<std::any>{value_};
}
private:
bool SetExceptionLocked(Exception exception) {
if (!done_) {
exception_ = exception.value != Exception::kSuccess
? exception
: Exception{Exception::kFailed};
done_ = true;
completed_.SignalAll();
}
return true;
}
absl::Mutex mutex_;
absl::CondVar completed_;
bool done_{false};
absl::any value_;
Exception exception_{Exception::kFailed};
};
} // namespace g3
} // namespace nearby
} // namespace location
#endif // PLATFORM_V2_IMPL_G3_SETTABLE_FUTURE_ANY_H_
+30 -6
View File
@@ -14,25 +14,50 @@
#include "platform_v2/impl/g3/webrtc.h"
#include <memory>
#include "platform_v2/base/medium_environment.h"
#include "webrtc/api/task_queue/default_task_queue_factory.h"
namespace location {
namespace nearby {
namespace g3 {
WebRtcSignalingMessenger::WebRtcSignalingMessenger(absl::string_view self_id)
: self_id_(self_id) {}
bool WebRtcSignalingMessenger::SendMessage(absl::string_view peer_id,
const ByteArray& message) {
auto& env = MediumEnvironment::Instance();
env.SendWebRtcSignalingMessage(peer_id, message);
return true;
}
bool WebRtcSignalingMessenger::StartReceivingMessages(
OnSignalingMessageCallback listener) {
auto& env = MediumEnvironment::Instance();
env.RegisterWebRtcSignalingMessenger(self_id_, listener);
return true;
}
void WebRtcSignalingMessenger::StopReceivingMessages() {
auto& env = MediumEnvironment::Instance();
env.UnregisterWebRtcSignalingMessenger(self_id_);
}
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";
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();
factory_dependencies.signaling_thread = signaling_thread_.get();
callback(webrtc::CreateModularPeerConnectionFactory(
std::move(factory_dependencies))
@@ -41,8 +66,7 @@ void WebRtcMedium::CreatePeerConnection(
std::unique_ptr<api::WebRtcSignalingMessenger>
WebRtcMedium::GetSignalingMessenger(absl::string_view self_id) {
// TODO(bfranz): Implement
return nullptr;
return std::make_unique<WebRtcSignalingMessenger>(self_id);
}
} // namespace g3
+19
View File
@@ -25,6 +25,23 @@ namespace location {
namespace nearby {
namespace g3 {
class WebRtcSignalingMessenger : public api::WebRtcSignalingMessenger {
public:
using OnSignalingMessageCallback =
api::WebRtcSignalingMessenger::OnSignalingMessageCallback;
explicit WebRtcSignalingMessenger(absl::string_view self_id);
~WebRtcSignalingMessenger() override = default;
bool SendMessage(absl::string_view peer_id,
const ByteArray& message) override;
bool StartReceivingMessages(OnSignalingMessageCallback listener) override;
void StopReceivingMessages() override;
private:
absl::string_view self_id_;
};
class WebRtcMedium : public api::WebRtcMedium {
public:
using PeerConnectionCallback = api::WebRtcMedium::PeerConnectionCallback;
@@ -40,6 +57,8 @@ class WebRtcMedium : public api::WebRtcMedium {
// Returns a signaling messenger for sending WebRTC signaling messages.
std::unique_ptr<api::WebRtcSignalingMessenger> GetSignalingMessenger(
absl::string_view self_id) override;
private:
std::unique_ptr<rtc::Thread> signaling_thread_;
};
} // namespace g3
+114
View File
@@ -0,0 +1,114 @@
#include "platform_v2/impl/g3/wifi_lan.h"
#include <memory>
#include <string>
#include "platform_v2/api/wifi_lan.h"
#include "platform_v2/base/logging.h"
#include "platform_v2/base/medium_environment.h"
#include "absl/synchronization/mutex.h"
namespace location {
namespace nearby {
namespace g3 {
InputStream& WifiLanSocket::GetInputStream() {
absl::MutexLock lock(&mutex_);
return pipe_.GetInputStream();
}
OutputStream& WifiLanSocket::GetOutputStream() {
absl::MutexLock lock(&mutex_);
return pipe_.GetOutputStream();
}
Exception WifiLanSocket::Close() {
absl::MutexLock lock(&mutex_);
pipe_.GetOutputStream().Close();
pipe_.GetInputStream().Close();
return {Exception::kSuccess};
}
WifiLanService* WifiLanSocket::GetRemoteWifiLanService() {
absl::MutexLock lock(&mutex_);
return service_;
}
WifiLanMedium::WifiLanMedium() {
auto& env = MediumEnvironment::Instance();
env.RegisterWifiLanMedium(*this);
}
WifiLanMedium::~WifiLanMedium() {
auto& env = MediumEnvironment::Instance();
env.UnregisterWifiLanMedium(*this);
}
bool WifiLanMedium::StartAdvertising(
const std::string& service_id,
const std::string& wifi_lan_service_info_name) {
// TODO(edwinwu): Integrate medium_environment.
// steps:
// 1. create wifi_lan_service as the parameter to create wifi_lan_socket
// auto service = std::make_unique<WifiLanService>();
// auto socket = std::make_unique<WifiLanSocket>(service);
// 2. callback for accepting connection; otherwise don't callback if not
// accepted connection.
// accepted_connection_callback_.accepted_cb(socket, service_id);
return true;
}
bool WifiLanMedium::StopAdvertising(const std::string& service_id) {
// TODO(edwinwu): Integrate medium_environment.
return true;
}
bool WifiLanMedium::StartDiscovery(const std::string& service_id,
DiscoveredServiceCallback callback) {
auto& env = MediumEnvironment::Instance();
NEARBY_LOG(INFO, "G3 StartDiscovery: service_id=%s", service_id.c_str());
env.UpdateWifiLanMediumForDiscovery(*this, service_, service_id,
std::move(callback), true);
return true;
}
bool WifiLanMedium::StopDiscovery(const std::string& service_id) {
auto& env = MediumEnvironment::Instance();
env.UpdateWifiLanMediumForDiscovery(*this, service_, service_id, {}, false);
return true;
}
bool WifiLanMedium::StartAcceptingConnections(
const std::string& service_id, AcceptedConnectionCallback callback) {
// TODO(edwinwu): Integrate medium_environment.
// steps:
auto& env = MediumEnvironment::Instance();
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_id, callback);
return true;
}
bool WifiLanMedium::StopAcceptingConnections(const std::string& service_id) {
// TODO(edwinwu): Integrate medium_environment.
auto& env = MediumEnvironment::Instance();
env.UpdateWifiLanMediumForAcceptedConnection(*this, service_id, {});
return true;
}
std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
api::WifiLanService& service, const std::string& service_id) {
auto socket = std::make_unique<WifiLanSocket>();
NEARBY_LOG(INFO, "G3 Connect: medium=%p, service_id=%s", this,
service_id.c_str());
return socket;
// TODO(edwinwu): Integrate medium_environment.
// steps:
// Request a connection, and block until the socket is provided via the
// callback.
// 1. connection = wifi_lan_service.requestConnection_();
// 2. create wifi_lan_socket with wifi_lan_service and connection
// return wifi_lan_socket;
}
} // namespace g3
} // namespace nearby
} // namespace location
+109
View File
@@ -0,0 +1,109 @@
#ifndef PLATFORM_V2_IMPL_G3_WIFI_LAN_H_
#define PLATFORM_V2_IMPL_G3_WIFI_LAN_H_
#include <string>
#include "platform_v2/api/wifi_lan.h"
#include "platform_v2/base/byte_array.h"
#include "platform_v2/base/input_stream.h"
#include "platform_v2/base/output_stream.h"
#include "platform_v2/impl/g3/pipe.h"
#include "absl/container/flat_hash_map.h"
#include "absl/synchronization/mutex.h"
namespace location {
namespace nearby {
namespace g3 {
// Opaque wrapper over a WifiLan service which contains encoded WifiLan service
// info name.
class WifiLanService : public api::WifiLanService {
public:
explicit WifiLanService(std::string name) : name_(std::move(name)) {}
~WifiLanService() override = default;
void SetName(std::string name) { name_ = std::move(name); }
std::string GetName() const override { return name_; }
private:
std::string name_;
};
class WifiLanSocket : public api::WifiLanSocket {
public:
WifiLanSocket() = default;
explicit WifiLanSocket(WifiLanService* service) : service_(service) {}
~WifiLanSocket() override = default;
// Connect to another WifiLanSocket, to form a functional low-level channel.
// from this point on, and until Close is called, connection exists.
void ConnectTo(WifiLanSocket* other) ABSL_LOCKS_EXCLUDED(mutex_);
// Returns the InputStream of this connected WifiLanSocket.
InputStream& GetInputStream() override ABSL_LOCKS_EXCLUDED(mutex_);
// Returns the OutputStream of this connected WifiLanSocket.
// This stream is for local side to write.
OutputStream& GetOutputStream() override ABSL_LOCKS_EXCLUDED(mutex_);
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
// Returns valid WifiLanService pointer if there is a connection, and
// nullptr otherwise.
WifiLanService* GetRemoteWifiLanService() override
ABSL_LOCKS_EXCLUDED(mutex_);
private:
Pipe pipe_;
WifiLanService* service_;
mutable absl::Mutex mutex_;
};
// Container of operations that can be performed over the WifiLan medium.
class WifiLanMedium : public api::WifiLanMedium {
public:
WifiLanMedium();
~WifiLanMedium() override;
bool StartAdvertising(const std::string& service_id,
const std::string& wifi_lan_service_info_name) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopAdvertising(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns true once the WifiLan discovery has been initiated.
bool StartDiscovery(const std::string& service_id,
DiscoveredServiceCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns true once WifiLan discovery for service_id is well and truly
// stopped; after this returns, there must be no more invocations of the
// DiscoveredServiceCallback passed in to StartDiscovery() for service_id.
bool StopDiscovery(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns true once WifiLan socket connection requests to service_id can be
// accepted.
bool StartAcceptingConnections(const std::string& service_id,
AcceptedConnectionCallback callback) override
ABSL_LOCKS_EXCLUDED(mutex_);
bool StopAcceptingConnections(const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
// Returns a new WifiLanSocket. On Success, WifiLanSocket::IsValid()
// returns true.
std::unique_ptr<api::WifiLanSocket> Connect(
api::WifiLanService& service, const std::string& service_id) override
ABSL_LOCKS_EXCLUDED(mutex_);
private:
absl::Mutex mutex_;
WifiLanService service_{"wifi_lan_service_info_name"};
};
} // namespace g3
} // namespace nearby
} // namespace location
#endif // PLATFORM_V2_IMPL_G3_WIFI_LAN_H_