From fb9ea31ec00c5cf123380fe5667bffa299f9883e Mon Sep 17 00:00:00 2001 From: Alexey Polyudov Date: Thu, 1 Oct 2020 02:27:28 -0700 Subject: [PATCH 1/2] Add .mm support Signed-off-by: Alexey Polyudov Change-Id: I2cc42ef9d05dfbbac6bb252f769682a43ab4c501 --- script/oss.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/oss.py b/script/oss.py index 4b621b0f..21a70028 100755 --- a/script/oss.py +++ b/script/oss.py @@ -96,7 +96,7 @@ def copy_files_to_oss_project(src_root, dst_root): def detect_file_copy_header_options(fname, lines): if not lines: return None # ignore empty file - suffixes = [".cc", ".cpp", ".cxx", ".c", ".h", ".hpp", ".inc", ".proto"] + suffixes = [".cc", ".cpp", ".cxx", ".c", ".h", ".hpp", ".inc", ".mm", ".proto"] for suffix in suffixes: if fname.endswith(suffix): return ("//", 0) From bcc3f21da2e8fecc1225b236c7bec80e1b821a7e Mon Sep 17 00:00:00 2001 From: Alexey Polyudov Date: Thu, 1 Oct 2020 02:31:10 -0700 Subject: [PATCH 2/2] Roll forward to cl/334770381 Signed-off-by: Alexey Polyudov Change-Id: I22b685b17c37357d6281cedfa7c228e304de8835 --- cpp/core_v2/internal/bwu_manager.cc | 8 +- cpp/core_v2/internal/endpoint_manager.cc | 3 + cpp/core_v2/internal/offline_frames.cc | 11 ++ .../p2p_point_to_point_pcp_handler.cc | 9 ++ cpp/core_v2/internal/p2p_star_pcp_handler.cc | 9 ++ cpp/platform_v2/impl/ios/BUILD | 56 ++++++++ cpp/platform_v2/impl/ios/atomic_boolean.h | 28 ++++ cpp/platform_v2/impl/ios/atomic_reference.h | 33 +++++ cpp/platform_v2/impl/ios/condition_variable.h | 37 ++++++ cpp/platform_v2/impl/ios/count_down_latch.h | 55 ++++++++ cpp/platform_v2/impl/ios/log_message.h | 28 ++++ cpp/platform_v2/impl/ios/log_message.mm | 56 ++++++++ .../impl/ios/multi_thread_executor.h | 57 ++++++++ cpp/platform_v2/impl/ios/mutex.h | 47 +++++++ cpp/platform_v2/impl/ios/platform.mm | 124 ++++++++++++++++++ cpp/platform_v2/impl/ios/scheduled_executor.h | 43 ++++++ .../impl/ios/scheduled_executor.mm | 65 +++++++++ .../impl/ios/single_thread_executor.h | 20 +++ proto/connections/offline_wire_formats.proto | 27 ++++ proto/connections_enums.proto | 10 +- 20 files changed, 718 insertions(+), 8 deletions(-) create mode 100644 cpp/platform_v2/impl/ios/BUILD create mode 100644 cpp/platform_v2/impl/ios/atomic_boolean.h create mode 100644 cpp/platform_v2/impl/ios/atomic_reference.h create mode 100644 cpp/platform_v2/impl/ios/condition_variable.h create mode 100644 cpp/platform_v2/impl/ios/count_down_latch.h create mode 100644 cpp/platform_v2/impl/ios/log_message.h create mode 100644 cpp/platform_v2/impl/ios/log_message.mm create mode 100644 cpp/platform_v2/impl/ios/multi_thread_executor.h create mode 100644 cpp/platform_v2/impl/ios/mutex.h create mode 100644 cpp/platform_v2/impl/ios/platform.mm create mode 100644 cpp/platform_v2/impl/ios/scheduled_executor.h create mode 100644 cpp/platform_v2/impl/ios/scheduled_executor.mm create mode 100644 cpp/platform_v2/impl/ios/single_thread_executor.h diff --git a/cpp/core_v2/internal/bwu_manager.cc b/cpp/core_v2/internal/bwu_manager.cc index 9ab910fe..ab81e570 100644 --- a/cpp/core_v2/internal/bwu_manager.cc +++ b/cpp/core_v2/internal/bwu_manager.cc @@ -30,11 +30,11 @@ BwuManager::BwuManager( if (config_.bandwidth_upgrade_retry_delay == absl::ZeroDuration()) { config_.bandwidth_upgrade_retry_delay = absl::Seconds(5); } - if (config_.bandwidth_upgrade_retry_delay == absl::ZeroDuration()) { - config_.bandwidth_upgrade_retry_delay = absl::Seconds(10); + if (config_.bandwidth_upgrade_retry_max_delay == absl::ZeroDuration()) { + config_.bandwidth_upgrade_retry_max_delay = absl::Seconds(10); } if (config_.allow_upgrade_to.All(false)) { - config.allow_upgrade_to.web_rtc = true; + config_.allow_upgrade_to.web_rtc = true; } if (!handlers.empty()) { handlers_ = std::move(handlers); @@ -43,7 +43,7 @@ BwuManager::BwuManager( } // Register the offline frame processor. - endpoint_manager.RegisterFrameProcessor( + endpoint_manager_->RegisterFrameProcessor( V1Frame::BANDWIDTH_UPGRADE_NEGOTIATION, this); } diff --git a/cpp/core_v2/internal/endpoint_manager.cc b/cpp/core_v2/internal/endpoint_manager.cc index 4e3e5d7a..04bf5890 100644 --- a/cpp/core_v2/internal/endpoint_manager.cc +++ b/cpp/core_v2/internal/endpoint_manager.cc @@ -136,6 +136,9 @@ ExceptionOr EndpointManager::HandleData( // no explicit handler. if (frame_type == V1Frame::KEEP_ALIVE) { NEARBY_LOG(INFO, "KeepAlive message for: id=%s", endpoint_id.c_str()); + } else if (frame_type == V1Frame::DISCONNECTION) { + NEARBY_LOG(INFO, "Disconnect message for: id=%s", endpoint_id.c_str()); + endpoint_channel->Close(); } else { NEARBY_LOG(ERROR, "Unhandled message: id=%s, type=%d", endpoint_id.c_str(), frame_type); diff --git a/cpp/core_v2/internal/offline_frames.cc b/cpp/core_v2/internal/offline_frames.cc index a046486d..9c7f6314 100644 --- a/cpp/core_v2/internal/offline_frames.cc +++ b/cpp/core_v2/internal/offline_frames.cc @@ -254,6 +254,17 @@ ByteArray ForKeepAlive() { return ToBytes(std::move(frame)); } +ByteArray ForDisconnection() { + OfflineFrame frame; + + frame.set_version(OfflineFrame::V1); + auto* v1_frame = frame.mutable_v1(); + v1_frame->set_type(V1Frame::DISCONNECTION); + v1_frame->mutable_disconnection(); + + return ToBytes(std::move(frame)); +} + UpgradePathInfo::Medium MediumToUpgradePathInfoMedium(Medium medium) { switch (medium) { case Medium::MDNS: diff --git a/cpp/core_v2/internal/p2p_point_to_point_pcp_handler.cc b/cpp/core_v2/internal/p2p_point_to_point_pcp_handler.cc index 0b09d8bd..64e05d4a 100644 --- a/cpp/core_v2/internal/p2p_point_to_point_pcp_handler.cc +++ b/cpp/core_v2/internal/p2p_point_to_point_pcp_handler.cc @@ -13,9 +13,18 @@ P2pPointToPointPcpHandler::P2pPointToPointPcpHandler( std::vector P2pPointToPointPcpHandler::GetConnectionMediumsByPriority() { std::vector mediums; + if (mediums_->GetWifiLan().IsAvailable()) { + mediums.push_back(proto::connections::WIFI_LAN); + } + if (mediums_->GetWebRtc().IsAvailable()) { + mediums.push_back(proto::connections::WEB_RTC); + } if (mediums_->GetBluetoothClassic().IsAvailable()) { mediums.push_back(proto::connections::BLUETOOTH); } + if (mediums_->GetBle().IsAvailable()) { + mediums.push_back(proto::connections::BLE); + } return mediums; } diff --git a/cpp/core_v2/internal/p2p_star_pcp_handler.cc b/cpp/core_v2/internal/p2p_star_pcp_handler.cc index 45a20d14..80e773cd 100644 --- a/cpp/core_v2/internal/p2p_star_pcp_handler.cc +++ b/cpp/core_v2/internal/p2p_star_pcp_handler.cc @@ -16,9 +16,18 @@ P2pStarPcpHandler::P2pStarPcpHandler(Mediums& mediums, std::vector P2pStarPcpHandler::GetConnectionMediumsByPriority() { std::vector mediums; + if (mediums_->GetWifiLan().IsAvailable()) { + mediums.push_back(proto::connections::WIFI_LAN); + } + if (mediums_->GetWebRtc().IsAvailable()) { + mediums.push_back(proto::connections::WEB_RTC); + } if (mediums_->GetBluetoothClassic().IsAvailable()) { mediums.push_back(proto::connections::BLUETOOTH); } + if (mediums_->GetBle().IsAvailable()) { + mediums.push_back(proto::connections::BLE); + } return mediums; } diff --git a/cpp/platform_v2/impl/ios/BUILD b/cpp/platform_v2/impl/ios/BUILD new file mode 100644 index 00000000..fa133022 --- /dev/null +++ b/cpp/platform_v2/impl/ios/BUILD @@ -0,0 +1,56 @@ +objc_library( + name = "types", + srcs = [ + "log_message.mm", + "scheduled_executor.mm", + ], + hdrs = [ + "atomic_boolean.h", + "atomic_reference.h", + "condition_variable.h", + "count_down_latch.h", + "log_message.h", + "multi_thread_executor.h", + "mutex.h", + "scheduled_executor.h", + "single_thread_executor.h", + ], + visibility = [ + "//platform_v2/impl/ios:__pkg__", + ], + deps = [ + "//base", + "//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/synchronization", + "//absl/time", + "//thread", + ], +) + +objc_library( + name = "ios", + srcs = [ + "platform.mm", + ], + visibility = [ + "//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__", + "//core_v2:__subpackages__", + "//platform_v2:__subpackages__", + ], + deps = [ + ":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/strings", + "//absl/time", + ], +) diff --git a/cpp/platform_v2/impl/ios/atomic_boolean.h b/cpp/platform_v2/impl/ios/atomic_boolean.h new file mode 100644 index 00000000..37a1d1f1 --- /dev/null +++ b/cpp/platform_v2/impl/ios/atomic_boolean.h @@ -0,0 +1,28 @@ +#ifndef PLATFORM_V2_IMPL_IOS_ATOMIC_BOOLEAN_H_ +#define PLATFORM_V2_IMPL_IOS_ATOMIC_BOOLEAN_H_ + +#include + +#include "platform_v2/api/atomic_boolean.h" + +namespace location { +namespace nearby { +namespace ios { + +class AtomicBoolean : public api::AtomicBoolean { + public: + explicit AtomicBoolean(bool initial_value) : value_(initial_value) {} + ~AtomicBoolean() override = default; + + bool Get() const override { return value_.load(); } + bool Set(bool value) override { return value_.exchange(value); } + + private: + std::atomic_bool value_; +}; + +} // namespace ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_ATOMIC_BOOLEAN_H_ diff --git a/cpp/platform_v2/impl/ios/atomic_reference.h b/cpp/platform_v2/impl/ios/atomic_reference.h new file mode 100644 index 00000000..49bb2849 --- /dev/null +++ b/cpp/platform_v2/impl/ios/atomic_reference.h @@ -0,0 +1,33 @@ +#ifndef PLATFORM_V2_IMPL_IOS_ATOMIC_REFERENCE_H_ +#define PLATFORM_V2_IMPL_IOS_ATOMIC_REFERENCE_H_ + +#include +#include + +#include "platform_v2/api/atomic_reference.h" + +namespace location { +namespace nearby { +namespace ios { + +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 value_; +}; + +} // namespace ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_ATOMIC_REFERENCE_H_ diff --git a/cpp/platform_v2/impl/ios/condition_variable.h b/cpp/platform_v2/impl/ios/condition_variable.h new file mode 100644 index 00000000..4df6893a --- /dev/null +++ b/cpp/platform_v2/impl/ios/condition_variable.h @@ -0,0 +1,37 @@ +#ifndef PLATFORM_V2_IMPL_IOS_CONDITION_VARIABLE_H_ +#define PLATFORM_V2_IMPL_IOS_CONDITION_VARIABLE_H_ + +#include "platform_v2/api/condition_variable.h" +#include "platform_v2/base/exception.h" +#include "platform_v2/impl/ios/mutex.h" +#include "absl/synchronization/mutex.h" + +namespace location { +namespace nearby { +namespace ios { + +class ConditionVariable : public api::ConditionVariable { + public: + explicit ConditionVariable(ios::Mutex* mutex) : mutex_(&mutex->mutex_) {} + ~ConditionVariable() override = default; + + Exception Wait() override { + cond_var_.Wait(mutex_); + return {Exception::kSuccess}; + } + Exception Wait(absl::Duration timeout) override { + cond_var_.WaitWithTimeout(mutex_, timeout); + return {Exception::kSuccess}; + } + void Notify() override { cond_var_.SignalAll(); } + + private: + absl::Mutex* mutex_; + absl::CondVar cond_var_; +}; + +} // namespace ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_CONDITION_VARIABLE_H_ diff --git a/cpp/platform_v2/impl/ios/count_down_latch.h b/cpp/platform_v2/impl/ios/count_down_latch.h new file mode 100644 index 00000000..a06bcc45 --- /dev/null +++ b/cpp/platform_v2/impl/ios/count_down_latch.h @@ -0,0 +1,55 @@ +#ifndef PLATFORM_V2_IMPL_IOS_COUNT_DOWN_LATCH_H_ +#define PLATFORM_V2_IMPL_IOS_COUNT_DOWN_LATCH_H_ + +#include "platform_v2/api/count_down_latch.h" +#include "absl/base/thread_annotations.h" +#include "absl/synchronization/mutex.h" +#include "absl/time/clock.h" + +namespace location { +namespace nearby { +namespace ios { + +class CountDownLatch final : public api::CountDownLatch { + public: + explicit CountDownLatch(int count) : count_(count) {} + CountDownLatch(const CountDownLatch&) = delete; + CountDownLatch& operator=(const CountDownLatch&) = delete; + CountDownLatch(CountDownLatch&&) = delete; + CountDownLatch& operator=(CountDownLatch&&) = delete; + ExceptionOr Await(absl::Duration timeout) override { + absl::MutexLock lock(&mutex_); + absl::Time deadline = absl::Now() + timeout; + while (count_ > 0) { + if (cond_.WaitWithDeadline(&mutex_, deadline)) { + return ExceptionOr(false); + } + } + return ExceptionOr(true); + } + Exception Await() override { + absl::MutexLock lock(&mutex_); + while (count_ > 0) { + cond_.Wait(&mutex_); + } + return {Exception::kSuccess}; + } + void CountDown() override { + absl::MutexLock lock(&mutex_); + if (count_ > 0 && --count_ == 0) { + cond_.SignalAll(); + } + } + + private: + absl::Mutex mutex_; // Mutex to be used with cond_.Wait...() method family. + absl::CondVar cond_; // Condition to synchronize up to N waiting threads. + int count_ + ABSL_GUARDED_BY(mutex_); // When zero, latch should release all waiters. +}; + +} // namespace ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_COUNT_DOWN_LATCH_H_ diff --git a/cpp/platform_v2/impl/ios/log_message.h b/cpp/platform_v2/impl/ios/log_message.h new file mode 100644 index 00000000..dd0a0c2a --- /dev/null +++ b/cpp/platform_v2/impl/ios/log_message.h @@ -0,0 +1,28 @@ +#ifndef PLATFORM_V2_IMPL_IOS_LOG_MESSAGE_H_ +#define PLATFORM_V2_IMPL_IOS_LOG_MESSAGE_H_ + +#include "base/logging.h" +#include "platform_v2/api/log_message.h" + +namespace location { +namespace nearby { +namespace ios { + +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 ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_LOG_MESSAGE_H_ diff --git a/cpp/platform_v2/impl/ios/log_message.mm b/cpp/platform_v2/impl/ios/log_message.mm new file mode 100644 index 00000000..0e6ac13c --- /dev/null +++ b/cpp/platform_v2/impl/ios/log_message.mm @@ -0,0 +1,56 @@ +#include "platform_v2/impl/ios/log_message.h" + +#include + +#include "base/stringprintf.h" + +namespace location { +namespace nearby { +namespace ios { + +api::LogMessage::Severity kMinLogSeverity = 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 ios + +namespace api { + +void LogMessage::SetMinLogSeverity(Severity severity) { + ios::kMinLogSeverity = severity; +} + +bool LogMessage::ShouldCreateLogMessage(Severity severity) { + return severity >= ios::kMinLogSeverity; +} + +} // namespace api +} // namespace nearby +} // namespace location diff --git a/cpp/platform_v2/impl/ios/multi_thread_executor.h b/cpp/platform_v2/impl/ios/multi_thread_executor.h new file mode 100644 index 00000000..e665df62 --- /dev/null +++ b/cpp/platform_v2/impl/ios/multi_thread_executor.h @@ -0,0 +1,57 @@ +#ifndef PLATFORM_V2_IMPL_IOS_MULTI_THREAD_EXECUTOR_H_ +#define PLATFORM_V2_IMPL_IOS_MULTI_THREAD_EXECUTOR_H_ + +#include + +#include "platform_v2/api/submittable_executor.h" +#include "platform_v2/impl/ios/count_down_latch.h" +#include "absl/time/clock.h" +#include "thread/threadpool.h" + +namespace location { +namespace nearby { +namespace ios { + +class MultiThreadExecutor : public api::SubmittableExecutor { + public: + explicit MultiThreadExecutor(int max_parallelism) + : thread_pool_(max_parallelism) { + thread_pool_.StartWorkers(); + } + void Execute(Runnable&& runnable) override { + if (!shutdown_) { + thread_pool_.Schedule(std::move(runnable)); + } + } + bool DoSubmit(Runnable&& runnable) override { + if (shutdown_) return false; + thread_pool_.Schedule(std::move(runnable)); + return true; + } + void Shutdown() override { DoShutdown(); } + ~MultiThreadExecutor() override { DoShutdown(); } + + int GetTid(int index) const override { + const auto* thread = thread_pool_.thread(index); + return thread ? *(int*)(thread->tid()) : 0; + } + + void ScheduleAfter(absl::Duration delay, Runnable&& runnable) { + if (shutdown_) return; + thread_pool_.ScheduleAt(absl::Now() + delay, std::move(runnable)); + } + bool InShutdown() const { return shutdown_; } + + private: + void DoShutdown() { + shutdown_ = true; + } + std::atomic_bool shutdown_ = false; + ThreadPool thread_pool_; +}; + +} // namespace ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_MULTI_THREAD_EXECUTOR_H_ diff --git a/cpp/platform_v2/impl/ios/mutex.h b/cpp/platform_v2/impl/ios/mutex.h new file mode 100644 index 00000000..2986869f --- /dev/null +++ b/cpp/platform_v2/impl/ios/mutex.h @@ -0,0 +1,47 @@ +#ifndef PLATFORM_V2_IMPL_IOS_MUTEX_H_ +#define PLATFORM_V2_IMPL_IOS_MUTEX_H_ + +#include "platform_v2/api/mutex.h" +#include "platform_v2/impl/shared/posix_mutex.h" +#include "absl/synchronization/mutex.h" + +namespace location { +namespace nearby { +namespace ios { + +class ABSL_LOCKABLE Mutex : public api::Mutex { + public: + explicit Mutex(bool check) : check_(check) {} + ~Mutex() override = default; + Mutex(Mutex&&) = delete; + Mutex& operator=(Mutex&&) = delete; + Mutex(const Mutex&) = delete; + Mutex& operator=(const Mutex&) = delete; + + void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION() override { + mutex_.Lock(); + if (!check_) mutex_.ForgetDeadlockInfo(); + } + void Unlock() ABSL_UNLOCK_FUNCTION() override { mutex_.Unlock(); } + + private: + friend class ConditionVariable; + absl::Mutex mutex_; + bool check_; +}; + +class ABSL_LOCKABLE RecursiveMutex : public posix::Mutex { + public: + ~RecursiveMutex() override = default; + RecursiveMutex() = default; + RecursiveMutex(RecursiveMutex&&) = delete; + RecursiveMutex& operator=(RecursiveMutex&&) = delete; + RecursiveMutex(const RecursiveMutex&) = delete; + RecursiveMutex& operator=(const RecursiveMutex&) = delete; +}; + +} // namespace ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_MUTEX_H_ diff --git a/cpp/platform_v2/impl/ios/platform.mm b/cpp/platform_v2/impl/ios/platform.mm new file mode 100644 index 00000000..dabd01d4 --- /dev/null +++ b/cpp/platform_v2/impl/ios/platform.mm @@ -0,0 +1,124 @@ +#include "platform_v2/api/platform.h" + +#include +#include + +#include "platform_v2/api/atomic_boolean.h" +#include "platform_v2/api/atomic_reference.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/submittable_executor.h" +#include "platform_v2/impl/ios/atomic_boolean.h" +#include "platform_v2/impl/ios/atomic_reference.h" +#include "platform_v2/impl/ios/condition_variable.h" +#include "platform_v2/impl/ios/count_down_latch.h" +#include "platform_v2/impl/ios/log_message.h" +#include "platform_v2/impl/ios/multi_thread_executor.h" +#include "platform_v2/impl/ios/mutex.h" +#include "platform_v2/impl/ios/scheduled_executor.h" +#include "platform_v2/impl/ios/single_thread_executor.h" +#include "platform_v2/impl/shared/file.h" +#include "absl/memory/memory.h" + +namespace location { +namespace nearby { +namespace api { + +namespace { +std::string GetPayloadPath(PayloadId payload_id) { + return absl::StrCat("/tmp/", payload_id); +} +} // namespace + +std::unique_ptr ImplementationPlatform::CreateAtomicBoolean(bool initial_value) { + return absl::make_unique(initial_value); +} + +std::unique_ptr ImplementationPlatform::CreateAtomicUint32(std::uint32_t value) { + return absl::make_unique(value); +} + +std::unique_ptr ImplementationPlatform::CreateCountDownLatch( + std::int32_t count) { + return absl::make_unique(count); +} + +std::unique_ptr ImplementationPlatform::CreateMutex(Mutex::Mode mode) { + if (mode == Mutex::Mode::kRecursive) + return absl::make_unique(); + else + return absl::make_unique(mode == Mutex::Mode::kRegular); +} + +std::unique_ptr ImplementationPlatform::CreateConditionVariable(Mutex* mutex) { + return std::unique_ptr( + new ios::ConditionVariable(static_cast(mutex))); +} + +std::unique_ptr ImplementationPlatform::CreateInputFile(PayloadId payload_id, + std::int64_t total_size) { + return absl::make_unique(GetPayloadPath(payload_id), total_size); +} + +std::unique_ptr ImplementationPlatform::CreateOutputFile(PayloadId payload_id) { + return absl::make_unique(GetPayloadPath(payload_id)); +} + +std::unique_ptr ImplementationPlatform::CreateLogMessage( + const char* file, int line, LogMessage::Severity severity) { + return absl::make_unique(file, line, severity); +} + +std::unique_ptr ImplementationPlatform::CreateSingleThreadExecutor() { + return absl::make_unique(); +} + +std::unique_ptr ImplementationPlatform::CreateMultiThreadExecutor( + int max_concurrency) { + return absl::make_unique(max_concurrency); +} + +std::unique_ptr ImplementationPlatform::CreateScheduledExecutor() { + return absl::make_unique(); +} + +std::unique_ptr ImplementationPlatform::CreateBluetoothAdapter() { + return std::unique_ptr(); +} + +std::unique_ptr ImplementationPlatform::CreateBluetoothClassicMedium( + api::BluetoothAdapter& adapter) { + return std::unique_ptr(); +} + +std::unique_ptr ImplementationPlatform::CreateBleMedium(api::BluetoothAdapter& adapter) { + return std::unique_ptr(); +} + +std::unique_ptr ImplementationPlatform::CreateBleV2Medium( + api::BluetoothAdapter& adapter) { + return std::unique_ptr(); +} + +std::unique_ptr ImplementationPlatform::CreateServerSyncMedium() { + return std::unique_ptr(); +} + +std::unique_ptr ImplementationPlatform::CreateWifiMedium() { + return std::unique_ptr(); +} + +std::unique_ptr ImplementationPlatform::CreateWifiLanMedium() { + return std::unique_ptr(); +} + +std::unique_ptr ImplementationPlatform::CreateWebRtcMedium() { + return std::unique_ptr(); +} + +} // namespace api +} // namespace nearby +} // namespace location diff --git a/cpp/platform_v2/impl/ios/scheduled_executor.h b/cpp/platform_v2/impl/ios/scheduled_executor.h new file mode 100644 index 00000000..6fb08fd7 --- /dev/null +++ b/cpp/platform_v2/impl/ios/scheduled_executor.h @@ -0,0 +1,43 @@ +#ifndef PLATFORM_V2_IMPL_IOS_SCHEDULED_EXECUTOR_H_ +#define PLATFORM_V2_IMPL_IOS_SCHEDULED_EXECUTOR_H_ + +#include +#include + +#include "platform_v2/api/cancelable.h" +#include "platform_v2/api/scheduled_executor.h" +#include "platform_v2/base/runnable.h" +#include "platform_v2/impl/ios/single_thread_executor.h" +#include "absl/time/clock.h" +#include "thread/threadpool.h" + +namespace location { +namespace nearby { +namespace ios { + +class ScheduledExecutor final : public api::ScheduledExecutor { + public: + ScheduledExecutor() = default; + ~ScheduledExecutor() override { + executor_.Shutdown(); + } + + void Execute(Runnable&& runnable) override { + executor_.Execute(std::move(runnable)); + } + std::shared_ptr Schedule(Runnable&& runnable, + absl::Duration delay) override; + void Shutdown() override { executor_.Shutdown(); } + + int GetTid(int index) const override { + return executor_.GetTid(index); + } + private: + SingleThreadExecutor executor_; +}; + +} // namespace ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_SCHEDULED_EXECUTOR_H_ diff --git a/cpp/platform_v2/impl/ios/scheduled_executor.mm b/cpp/platform_v2/impl/ios/scheduled_executor.mm new file mode 100644 index 00000000..6d850b06 --- /dev/null +++ b/cpp/platform_v2/impl/ios/scheduled_executor.mm @@ -0,0 +1,65 @@ +#include "platform_v2/impl/ios/scheduled_executor.h" + +#include +#include + +#include "platform_v2/api/cancelable.h" +#include "platform_v2/base/runnable.h" +#include "absl/time/clock.h" + +namespace location { +namespace nearby { +namespace ios { + +namespace { + +class ScheduledCancelable : public api::Cancelable { + public: + bool Cancel() override { + Status expected = kNotRun; + while (expected == kNotRun) { + if (status_.compare_exchange_strong(expected, kCanceled)) { + return true; + } + } + return false; + } + bool MarkExecuted() { + Status expected = kNotRun; + while (expected == kNotRun) { + if (status_.compare_exchange_strong(expected, kExecuted)) { + return true; + } + } + return false; + } + + private: + enum Status { + kNotRun, + kExecuted, + kCanceled, + }; + std::atomic status_ = kNotRun; +}; + +} // namespace + +std::shared_ptr ScheduledExecutor::Schedule( + Runnable&& runnable, absl::Duration delay) { + auto scheduled_cancelable = std::make_shared(); + if (executor_.InShutdown()) { + return scheduled_cancelable; + } + executor_.ScheduleAfter( + delay, [this, scheduled_cancelable, runnable(std::move(runnable))]() { + if (!executor_.InShutdown() && scheduled_cancelable->MarkExecuted()) { + runnable(); + } + }); + return scheduled_cancelable; +} + +} // namespace ios +} // namespace nearby +} // namespace location diff --git a/cpp/platform_v2/impl/ios/single_thread_executor.h b/cpp/platform_v2/impl/ios/single_thread_executor.h new file mode 100644 index 00000000..be5d99e0 --- /dev/null +++ b/cpp/platform_v2/impl/ios/single_thread_executor.h @@ -0,0 +1,20 @@ +#ifndef PLATFORM_V2_IMPL_IOS_SINGLE_THREAD_EXECUTOR_H_ +#define PLATFORM_V2_IMPL_IOS_SINGLE_THREAD_EXECUTOR_H_ + +#include "platform_v2/impl/ios/multi_thread_executor.h" + +namespace location { +namespace nearby { +namespace ios { + +class SingleThreadExecutor final : public MultiThreadExecutor { + public: + SingleThreadExecutor() : MultiThreadExecutor(1) {} + ~SingleThreadExecutor() override = default; +}; + +} // namespace ios +} // namespace nearby +} // namespace location + +#endif // PLATFORM_V2_IMPL_IOS_SINGLE_THREAD_EXECUTOR_H_ diff --git a/proto/connections/offline_wire_formats.proto b/proto/connections/offline_wire_formats.proto index 9cd1728d..f7c1e6dc 100644 --- a/proto/connections/offline_wire_formats.proto +++ b/proto/connections/offline_wire_formats.proto @@ -207,6 +207,7 @@ message BandwidthUpgradeNegotiationFrame { // Accompanies Medium.WEB_RTC message WebRtcCredentials { optional string peer_id = 1; + optional LocationHint location_hint = 2; } optional Medium medium = 1; @@ -261,3 +262,29 @@ message MediumMetadata { // WiFi Lan BSSID optional string bssid = 2; } + +// LocationHint is used to specify a location as well as format. +message LocationHint { + // Location is the location, provided in the format specified by format. + optional string location = 1; + + // the format of location. + optional LocationStandard.Format format = 2; +} + +// Copy from +// https://source.corp.google.com/piper///depot/google3/media/webrtc/server/tachyon/proto/tachyon_enums.proto;rcl=334271491;l=10242 +// These numbers match must match the original definition. +message LocationStandard { + enum Format { + UNKNOWN = 0; + // E164 country codes: + // https://en.wikipedia.org/wiki/List_of_country_calling_codes + // e.g. +1 for USA + E164_CALLING = 1; + + // ISO 3166-1 alpha-2 country codes: + // https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 + ISO_3166_1_ALPHA_2 = 2; + } +} diff --git a/proto/connections_enums.proto b/proto/connections_enums.proto index 3fd7dc8b..c9259767 100644 --- a/proto/connections_enums.proto +++ b/proto/connections_enums.proto @@ -162,7 +162,7 @@ enum PayloadStatus { REMOTE_CANCELLATION = 8; } -// next_id: 17 +// next_id: 18 // Result of an upgrade attempt. enum BandwidthUpgradeResult { UNKNOWN_BANDWIDTH_UPGRADE_RESULT = 0; @@ -191,9 +191,6 @@ enum BandwidthUpgradeResult { // record analytics (e.g. the client disconnected). UNFINISHED_ERROR = 10; - // TODO(b/151833661): add a REMOTE_ERROR when we implement a cancellation - // message, for the case when the remote endpoint had an error on their end. - // Error during setting up Bluetooth. BLUETOOTH_MEDIUM_ERROR = 11; @@ -211,6 +208,9 @@ enum BandwidthUpgradeResult { // Error during setting up WebRTC. WEB_RTC_MEDIUM_ERROR = 16; + + // When the remote endpoint had an error on their end. + RESULT_REMOTE_ERROR = 17; } // next_id: 35 @@ -232,6 +232,8 @@ enum BandwidthUpgradeErrorStage { UPGRADE_UNFINISHED = 7; // Upgrade successfully UPGRADE_SUCCESS = 8; + // Upgrade cancel + UPGRADE_CANCEL = 9; // Medium-specific stages. // TODO(xlythe) Make sure each stage maps to one, and only one, possible