Exporting symbols for consumers of the dll - and deleted old code

PiperOrigin-RevId: 401098019
This commit is contained in:
jfcarroll
2021-10-05 16:27:15 -07:00
committed by Copybara-Service
parent 679838223c
commit e0b1dd8dfc
6 changed files with 101 additions and 186 deletions
+33 -6
View File
@@ -11,10 +11,8 @@
// 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 CORE_OPTIONS_H_
#define CORE_OPTIONS_H_
#include <string>
#include "core/strategy.h"
@@ -25,7 +23,7 @@ namespace location {
namespace nearby {
namespace connections {
using Medium = ::location::nearby::proto::connections::Medium;
using Medium = location::nearby::proto::connections::Medium;
// Generic type: allows definition of a feature T for every Medium.
template <typename T>
@@ -38,7 +36,6 @@ struct MediumSelector {
constexpr MediumSelector() = default;
constexpr MediumSelector(const MediumSelector&) = default;
constexpr MediumSelector& operator=(const MediumSelector&) = default;
constexpr bool Any(T value) const {
return bluetooth == value || ble == value || web_rtc == value ||
wifi_lan == value;
@@ -89,7 +86,7 @@ enum class PowerLevel {
// Connection Options: used for both Advertising and Discovery.
// All fields are mutable, to make the type copy-assignable.
struct ConnectionOptions {
struct DLL_API ConnectionOptions {
Strategy strategy;
BooleanMediumSelector allowed{BooleanMediumSelector().SetAll(true)};
bool auto_upgrade_bandwidth;
@@ -97,16 +94,20 @@ struct ConnectionOptions {
bool low_power;
bool enable_bluetooth_listening;
bool enable_webrtc_listening;
// Whether this is intended to be used in conjunction with InjectEndpoint().
bool is_out_of_band_connection = false;
ByteArray remote_bluetooth_mac_address;
std::string fast_advertisement_service_uuid;
int keep_alive_interval_millis = 0;
int keep_alive_timeout_millis = 0;
// Verify if ConnectionOptions is in a not-initialized (Empty) state.
bool Empty() const { return strategy.IsNone(); }
// Bring ConnectionOptions to a not-initialized (Empty) state.
void Clear() { strategy.Clear(); }
// Returns a copy and normalizes allowed mediums:
// (1) If is_out_of_band_connection is true, verifies that there is only one
// medium allowed, defaulting to only Bluetooth if unspecified.
@@ -125,7 +126,6 @@ struct ConnectionOptions {
result.allowed.SetAll(false);
result.allowed.bluetooth = true;
}
return result;
}
@@ -134,7 +134,34 @@ struct ConnectionOptions {
if (!allowed.Any(true)) result.allowed.SetAll(true);
return result;
}
std::vector<Medium> GetMediums() const { return allowed.GetMediums(true); }
// This call follows the standard Microsoft calling pattern of calling first
// to get the size of the array. Caller then allocates memory for the array,
// and makes this call again to copy the array into the provided location.
void GetMediums(location::nearby::proto::connections::Medium* mediums,
uint32_t* mediumsSize) {
auto size = GetMediums().size();
// Caller is seeking the size of mediums
if (mediums == nullptr) {
*mediumsSize = size;
return;
}
// Caller has not specified enough memory
if (*mediumsSize < size) {
mediums = nullptr; // ensure nullptr return
*mediumsSize = size; // update the size to indicate the correct size
return;
}
for (uint32_t i = 0; i < size; i++) {
// Construct an array at the given location
mediums[i] = GetMediums().at(i);
}
}
};
// Metadata injected to facilitate out-of-band connections. The medium field is
+17 -7
View File
@@ -11,40 +11,52 @@
// 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 CORE_STRATEGY_H_
#define CORE_STRATEGY_H_
#include <string>
#ifdef _WIN32 // These storage class specifiers only matter to win32 dll
// builds.
#ifdef CORE_ADAPTER_DLL
#define DLL_API \
__declspec(dllexport) // If we're building the core, we're exporting.
#else // !CORE_ADAPTER_DLL
#define DLL_API \
__declspec(dllimport) // If we're not building the core, we're importing.
#endif // CORE_ADAPTER_DLL
#else // !_WIN32
#define DLL_API // We're not building a win32 dll, leave the source unchanged.
#endif // _WIN32
namespace location {
namespace nearby {
namespace connections {
// Defines a copyable, comparable connection strategy type.
// It is one of: kP2pCluster, kP2pStar, kP2pPointToPoint.
class Strategy {
class DLL_API Strategy {
public:
static const Strategy kNone;
static const Strategy kP2pCluster;
static const Strategy kP2pStar;
static const Strategy kP2pPointToPoint;
constexpr Strategy() : Strategy(kNone) {}
constexpr Strategy(const Strategy&) = default;
constexpr Strategy& operator=(const Strategy&) = default;
// Returns true, if strategy is kNone, false otherwise.
bool IsNone() const;
// Returns true, if a strategy is one of the supported strategies,
// false otherwise.
bool IsValid() const;
// Returns a string representing given strategy, for every valid strategy.
std::string GetName() const;
// Undefine strategy.
void Clear() { *this = kNone; }
friend bool operator==(const Strategy& lhs, const Strategy& rhs);
friend bool operator!=(const Strategy& lhs, const Strategy& rhs);
@@ -61,7 +73,6 @@ class Strategy {
};
constexpr Strategy(ConnectionType connection_type, TopologyType topology_type)
: connection_type_(connection_type), topology_type_(topology_type) {}
ConnectionType connection_type_;
TopologyType topology_type_;
};
@@ -69,5 +80,4 @@ class Strategy {
} // namespace connections
} // namespace nearby
} // namespace location
#endif // CORE_STRATEGY_H_
@@ -1,70 +0,0 @@
// Copyright 2021 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/impl/windows/condition_variable.h"
namespace location {
namespace nearby {
namespace windows {
// The ConditionVariable class is a synchronization primitive that can be used
// to block a thread, or multiple threads at the same time, until another thread
// both modifies a shared variable (the condition), and notifies the
// ConditionVariable.
ConditionVariable::ConditionVariable(api::Mutex* mutex)
: mutex_(dynamic_cast<windows::Mutex&>(*mutex)) {
InitializeCriticalSection(&critical_section_);
}
// Notifies all the waiters that condition state has changed.
// TODO(b/184975123): replace with real implementation.
void ConditionVariable::Notify() {
EnterCriticalSection(&critical_section_);
condition_variable_actual_.notify_all();
LeaveCriticalSection(&critical_section_);
}
// Waits indefinitely for Notify to be called.
// May return prematurely in case of interrupt, if supported by platform.
// Returns kSuccess, or kInterrupted on interrupt.
Exception ConditionVariable::Wait() {
std::unique_lock<std::mutex> lock =
std::unique_lock<std::mutex>(mutex_.GetWindowsMutex());
condition_variable_actual_.wait(lock);
return Exception{Exception::kSuccess};
}
// Waits while timeout has not expired for Notify to be called.
// May return prematurely in case of interrupt, if supported by platform.
// Returns kSuccess, or kInterrupted on interrupt.
Exception ConditionVariable::Wait(absl::Duration timeout) {
auto now = std::chrono::system_clock::now();
std::unique_lock<std::mutex> lock =
std::unique_lock<std::mutex>(mutex_.GetWindowsMutex());
if (condition_variable_actual_.wait_until(
lock, now + absl::ToChronoMilliseconds(timeout)) ==
std::cv_status::timeout) {
return Exception{Exception::kInterrupted};
} else {
return Exception{Exception::kSuccess};
}
}
} // namespace windows
} // namespace nearby
} // namespace location
-51
View File
@@ -1,51 +0,0 @@
// Copyright 2021 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/impl/windows/mutex.h"
namespace location {
namespace nearby {
namespace windows {
// A lock is a tool for controlling access to a shared resource by multiple
// threads.
//
// https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/Lock.html
Mutex::Mutex(Mutex::Mode mode) : mode_(mode) {}
void Mutex::Lock() {
if (mode_ == Mutex::Mode::kRegular || mode_ == Mutex::Mode::kRegularNoCheck) {
mutex_impl_.lock();
} else if (mode_ == Mutex::Mode::kRecursive) {
recursive_mutex_impl_.lock();
}
}
void Mutex::Unlock() {
if (mode_ == Mutex::Mode::kRegular || mode_ == Mutex::Mode::kRegularNoCheck) {
mutex_impl_.unlock();
} else if (mode_ == Mutex::Mode::kRecursive) {
recursive_mutex_impl_.unlock();
}
}
std::mutex& Mutex::GetWindowsMutex() { return mutex_; }
std::recursive_mutex& Mutex::GetWindowsRecursiveMutex() {
return recursive_mutex_;
}
} // namespace windows
} // namespace nearby
} // namespace location