diff --git a/cpp/core/BUILD b/cpp/core/BUILD index c4c69762..f91ef6b1 100644 --- a/cpp/core/BUILD +++ b/cpp/core/BUILD @@ -49,6 +49,7 @@ cc_library( ], hdrs = [ "listeners.h", + "medium_selector.h", "options.h", "params.h", "payload.h", diff --git a/cpp/core/medium_selector.h b/cpp/core/medium_selector.h new file mode 100644 index 00000000..ff7fdba0 --- /dev/null +++ b/cpp/core/medium_selector.h @@ -0,0 +1,78 @@ +// 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 CORE_MEDIUM_SELECTOR_H_ +#define CORE_MEDIUM_SELECTOR_H_ + +#include "proto/connections_enums.pb.h" + +namespace location { +namespace nearby { +namespace connections { + +using Medium = location::nearby::proto::connections::Medium; + +// Generic type: allows definition of a feature T for every Medium. +template +struct MediumSelector { + T bluetooth; + T ble; + T web_rtc; + T wifi_lan; + + 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; + } + + constexpr bool All(T value) const { + return bluetooth == value && ble == value && web_rtc == value && + wifi_lan == value; + } + + constexpr int Count(T value) const { + int count = 0; + if (bluetooth == value) count++; + if (ble == value) count++; + if (wifi_lan == value) count++; + if (web_rtc == value) count++; + return count; + } + + constexpr MediumSelector& SetAll(T value) { + bluetooth = value; + ble = value; + web_rtc = value; + wifi_lan = value; + return *this; + } + + std::vector GetMediums(T value) const { + std::vector mediums; + // Mediums are sorted in order of decreasing preference. + if (wifi_lan == value) mediums.push_back(Medium::WIFI_LAN); + if (web_rtc == value) mediums.push_back(Medium::WEB_RTC); + if (bluetooth == value) mediums.push_back(Medium::BLUETOOTH); + if (ble == value) mediums.push_back(Medium::BLE); + return mediums; + } +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // CORE_MEDIUM_SELECTOR_H_ diff --git a/cpp/core/options.h b/cpp/core/options.h index ff1ca03b..ee5474c8 100644 --- a/cpp/core/options.h +++ b/cpp/core/options.h @@ -15,6 +15,7 @@ #define CORE_OPTIONS_H_ #include +#include "core/medium_selector.h" #include "core/strategy.h" #include "platform/base/byte_array.h" #include "proto/connections_enums.pb.h" @@ -23,57 +24,6 @@ namespace location { namespace nearby { namespace connections { -using Medium = location::nearby::proto::connections::Medium; - -// Generic type: allows definition of a feature T for every Medium. -template -struct MediumSelector { - T bluetooth; - T ble; - T web_rtc; - T wifi_lan; - - 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; - } - - constexpr bool All(T value) const { - return bluetooth == value && ble == value && web_rtc == value && - wifi_lan == value; - } - - constexpr int Count(T value) const { - int count = 0; - if (bluetooth == value) count++; - if (ble == value) count++; - if (wifi_lan == value) count++; - if (web_rtc == value) count++; - return count; - } - - constexpr MediumSelector& SetAll(T value) { - bluetooth = value; - ble = value; - web_rtc = value; - wifi_lan = value; - return *this; - } - - std::vector GetMediums(T value) const { - std::vector mediums; - // Mediums are sorted in order of decreasing preference. - if (wifi_lan == value) mediums.push_back(Medium::WIFI_LAN); - if (web_rtc == value) mediums.push_back(Medium::WEB_RTC); - if (bluetooth == value) mediums.push_back(Medium::BLUETOOTH); - if (ble == value) mediums.push_back(Medium::BLE); - return mediums; - } -}; - // Feature On/Off switch for mediums. using BooleanMediumSelector = MediumSelector; @@ -86,7 +36,7 @@ enum class PowerLevel { // Connection Options: used for both Advertising and Discovery. // All fields are mutable, to make the type copy-assignable. -struct DLL_API ConnectionOptions { +struct ConnectionOptions { Strategy strategy; BooleanMediumSelector allowed{BooleanMediumSelector().SetAll(true)}; bool auto_upgrade_bandwidth; diff --git a/windows/BUILD b/windows/BUILD index f7906412..e1a4fc3e 100644 --- a/windows/BUILD +++ b/windows/BUILD @@ -18,8 +18,18 @@ licenses(["notice"]) # Build with --config=lexan cc_windows_dll( name = "core_adapter", - srcs = ["core_adapter.cc"], - hdrs = ["core_adapter.h"], + srcs = [ + "core_adapter.cc", + "request_connection_options.cc", + "start_advertising_options.cc", + "start_discovery_options.cc", + ], + hdrs = [ + "core_adapter.h", + "request_connection_options.h", + "start_advertising_options.h", + "start_discovery_options.h", + ], copts = ["-DCORE_ADAPTER_BUILD_DLL -DCOMPILING_CORE"], defines = ["CORE_ADAPTER_DLL"], tags = ["windows-dll"], @@ -39,10 +49,16 @@ cc_windows_dll( srcs = [ "core_adapter.cc", "dart/core_adapter_dart.cc", + "request_connection_options.cc", + "start_advertising_options.cc", + "start_discovery_options.cc", ], hdrs = [ "core_adapter.h", "dart/core_adapter_dart.h", + "request_connection_options.h", + "start_advertising_options.h", + "start_discovery_options.h", ], copts = ["-DCORE_ADAPTER_DART_BUILD_DLL -DCORE_ADAPTER_DLL -DCOMPILING_CORE"], defines = ["CORE_ADAPTER_DART_DLL"], diff --git a/windows/core_adapter.cc b/windows/core_adapter.cc index 272b97ba..0e7cfd9d 100644 --- a/windows/core_adapter.cc +++ b/windows/core_adapter.cc @@ -35,9 +35,8 @@ void CloseCore(Core* pCore) { } void StartAdvertising(Core* pCore, const char* service_id, - ConnectionOptions options, - ConnectionRequestInfo info, - ResultCallback callback) { + StartAdvertisingOptions options, + ConnectionRequestInfo info, ResultCallback callback) { if (pCore) { pCore->StartAdvertising(service_id, options, info, callback); } @@ -50,9 +49,8 @@ void StopAdvertising(Core* pCore, ResultCallback callback) { } void StartDiscovery(Core* pCore, const char* service_id, - ConnectionOptions options, - DiscoveryListener listener, - ResultCallback callback) { + StartDiscoveryOptions options, DiscoveryListener listener, + ResultCallback callback) { if (pCore) { pCore->StartDiscovery(service_id, options, listener, callback); } @@ -65,56 +63,55 @@ void StopDiscovery(Core* pCore, ResultCallback callback) { } void InjectEndpoint(Core* pCore, char* service_id, - OutOfBandConnectionMetadata metadata, - ResultCallback callback) { + OutOfBandConnectionMetadata metadata, + ResultCallback callback) { if (pCore) { pCore->InjectEndpoint(service_id, metadata, callback); } } void RequestConnection(Core* pCore, const char* endpoint_id, - ConnectionRequestInfo info, - ConnectionOptions options, - ResultCallback callback) { + ConnectionRequestInfo info, + RequestConnectionOptions options, + ResultCallback callback) { if (pCore) { pCore->RequestConnection(endpoint_id, info, options, callback); } } void AcceptConnection(Core* pCore, const char* endpoint_id, - PayloadListener listener, - ResultCallback callback) { + PayloadListener listener, ResultCallback callback) { if (pCore) { pCore->AcceptConnection(endpoint_id, listener, callback); } } void RejectConnection(Core* pCore, const char* endpoint_id, - ResultCallback callback) { + ResultCallback callback) { if (pCore) { pCore->RejectConnection(endpoint_id, callback); } } void SendPayload(Core* pCore, - // todo(jfcarroll) this is being exported, needs to be - // refactored to return a plain old c type - absl::Span endpoint_ids, - Payload payload, ResultCallback callback) { + // todo(jfcarroll) this is being exported, needs to be + // refactored to return a plain old c type + absl::Span endpoint_ids, Payload payload, + ResultCallback callback) { if (pCore) { pCore->SendPayload(endpoint_ids, std::move(payload), callback); } } void CancelPayload(Core* pCore, std::int64_t payload_id, - ResultCallback callback) { + ResultCallback callback) { if (pCore) { pCore->CancelPayload(payload_id, callback); } } void DisconnectFromEndpoint(Core* pCore, char* endpoint_id, - ResultCallback callback) { + ResultCallback callback) { if (pCore) { pCore->DisconnectFromEndpoint(endpoint_id, callback); } @@ -127,7 +124,7 @@ void StopAllEndpoints(Core* pCore, ResultCallback callback) { } void InitiateBandwidthUpgrade(Core* pCore, char* endpoint_id, - ResultCallback callback) { + ResultCallback callback) { if (pCore) { pCore->InitiateBandwidthUpgrade(endpoint_id, callback); } diff --git a/windows/core_adapter.h b/windows/core_adapter.h index 7c2c5c96..4576df0e 100644 --- a/windows/core_adapter.h +++ b/windows/core_adapter.h @@ -18,8 +18,10 @@ #include "absl/types/span.h" // todo(jfcarroll) This cannot remain. It exposes stuff the client doesn't need. -#include \ -"third_party/nearby_connections/cpp/core/internal/offline_service_controller.h" +#include "core/internal/offline_service_controller.h" +#include "third_party/nearby_connections/windows/request_connection_options.h" +#include "third_party/nearby_connections/windows/start_advertising_options.h" +#include "third_party/nearby_connections/windows/start_discovery_options.h" #define DLL_EXPORT extern "C" __declspec(dllexport) @@ -65,9 +67,9 @@ DLL_EXPORT void __stdcall CloseCore(Core* pCore); // Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently // connected to remote endpoints; call StopAllEndpoints first. DLL_EXPORT void __stdcall StartAdvertising(Core* pCore, const char* service_id, - ConnectionOptions options, - ConnectionRequestInfo info, - ResultCallback callback); + StartAdvertisingOptions options, + ConnectionRequestInfo info, + ResultCallback callback); // Stops advertising a local endpoint. Should be called after calling // StartAdvertising, as soon as the application no longer needs to advertise @@ -93,9 +95,9 @@ DLL_EXPORT void __stdcall StopAdvertising(Core* pCore, ResultCallback callback); // Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently // connected to remote endpoints; call StopAllEndpoints first. DLL_EXPORT void __stdcall StartDiscovery(Core* pCore, const char* service_id, - ConnectionOptions options, - DiscoveryListener listener, - ResultCallback callback); + StartDiscoveryOptions options, + DiscoveryListener listener, + ResultCallback callback); // Stops discovery for remote endpoints, after a previous call to // StartDiscovery, when the client no longer needs to discover endpoints or @@ -148,7 +150,7 @@ DLL_EXPORT void __stdcall InjectEndpoint(Core* pCore, char* service_id, DLL_EXPORT void __stdcall RequestConnection(Core* pCore, const char* endpoint_id, ConnectionRequestInfo info, - ConnectionOptions options, + RequestConnectionOptions options, ResultCallback callback); // Accepts a connection to a remote endpoint. This method must be called diff --git a/windows/dart/core_adapter_dart.cc b/windows/dart/core_adapter_dart.cc index 9d087202..8ecd6d9e 100644 --- a/windows/dart/core_adapter_dart.cc +++ b/windows/dart/core_adapter_dart.cc @@ -71,7 +71,7 @@ void StartAdvertisingDart(Core* pCore, return; } - ConnectionOptions options; + StartAdvertisingOptions options; options.strategy = GetStrategy(options_dart.strategy); options.auto_upgrade_bandwidth = options_dart.auto_upgrade_bandwidth; @@ -171,7 +171,7 @@ void StartAdvertisingDart(Core* pCore, ResultCallback callback; SetResultCallback(callback, result_cb); - StartAdvertising(pCore, service_id, options, info, callback); + StartAdvertising(pCore, service_id, std::move(options), info, callback); } void StopAdvertisingDart(Core* pCore, Dart_Port result_cb) { @@ -195,7 +195,7 @@ void StartDiscoveryDart(Core* pCore, return; } - ConnectionOptions options; + StartDiscoveryOptions options; options.strategy = GetStrategy(options_dart.strategy); options.allowed.bluetooth = options_dart.enable_bluetooth; options.allowed.ble = options_dart.enable_ble; @@ -285,7 +285,7 @@ void StartDiscoveryDart(Core* pCore, ResultCallback callback; SetResultCallback(callback, result_cb); - StartDiscovery(pCore, service_id, options, listener, callback); + StartDiscovery(pCore, service_id, std::move(options), listener, callback); } void StopDiscoveryDart(Core* pCore, Dart_Port result_cb) { @@ -309,7 +309,7 @@ void RequestConnectionDart(Core* pCore, return; } - ConnectionOptions options; + RequestConnectionOptions options; options.enforce_topology_constraints = false; options.allowed.bluetooth = options_dart.enable_bluetooth; options.allowed.ble = options_dart.enable_ble; @@ -401,7 +401,7 @@ void RequestConnectionDart(Core* pCore, ResultCallback callback; SetResultCallback(callback, result_cb); - RequestConnection(pCore, endpoint_id, info, options, callback); + RequestConnection(pCore, endpoint_id, info, std::move(options), callback); } void AcceptConnectionDart(Core* pCore, diff --git a/windows/request_connection_options.cc b/windows/request_connection_options.cc new file mode 100644 index 00000000..7ea2a94c --- /dev/null +++ b/windows/request_connection_options.cc @@ -0,0 +1,72 @@ +// 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 "third_party/nearby_connections/windows/request_connection_options.h" + +#include "core/options.h" +#include "core/strategy.h" +#include "platform/base/byte_array.h" +#include "platform/public/core_config.h" + +namespace location { +namespace nearby { +namespace connections { + +RequestConnectionOptions::RequestConnectionOptions() + : impl_(new connections::ConnectionOptions(), + [](connections::ConnectionOptions* impl) { delete impl; }), + strategy(impl_->strategy), + allowed(impl_->allowed), + auto_upgrade_bandwidth(impl_->auto_upgrade_bandwidth), + enable_bluetooth_listening(impl_->enable_bluetooth_listening), + enable_webrtc_listening(impl_->enable_webrtc_listening), + low_power(impl_->low_power), + enforce_topology_constraints(impl_->enforce_topology_constraints), + is_out_of_band_connection(impl_->is_out_of_band_connection), + remote_bluetooth_mac_address(impl_->remote_bluetooth_mac_address), + fast_advertisement_service_uuid( + impl_->fast_advertisement_service_uuid.c_str()), + keep_alive_interval_millis(impl_->keep_alive_interval_millis), + keep_alive_timeout_millis(impl_->keep_alive_timeout_millis) {} + +RequestConnectionOptions::operator connections::ConnectionOptions() const { + return *impl_.get(); +} + +// Verify if ConnectionOptions is in a not-initialized (Empty) state. +bool RequestConnectionOptions::Empty() const { return impl_->Empty(); } + +// Bring ConnectionOptions to a not-initialized (Empty) state. +void RequestConnectionOptions::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. +// (2) If no mediums are allowed, allow all mediums. +ConnectionOptions RequestConnectionOptions::CompatibleOptions() const { + return impl_->CompatibleOptions(); +} + +// std::vector GetMediums() const; + +// 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 RequestConnectionOptions::GetMediums( + location::nearby::proto::connections::Medium* mediums, + uint32_t* mediumsSize) {} + +} // namespace connections +} // namespace nearby +} // namespace location diff --git a/windows/request_connection_options.h b/windows/request_connection_options.h new file mode 100644 index 00000000..d41b38c5 --- /dev/null +++ b/windows/request_connection_options.h @@ -0,0 +1,89 @@ +// 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. + +#ifndef WINDOWS_REQUEST_CONNECTION_OPTIONS_H_ +#define WINDOWS_REQUEST_CONNECTION_OPTIONS_H_ + +#include + +#include + +#include "platform/public/core_config.h" + +namespace location { +namespace nearby { +class ByteArray; +namespace proto { +namespace connections { +enum Medium : int; +} +} // namespace proto +namespace connections { +struct ConnectionOptions; +class Strategy; +template +struct MediumSelector; + +using BooleanMediumSelector = MediumSelector; + +struct DLL_API RequestConnectionOptions { + RequestConnectionOptions(); + operator connections::ConnectionOptions() const; + + private: + std::unique_ptr + impl_; + + public: + Strategy& strategy; + BooleanMediumSelector& allowed; + + bool& auto_upgrade_bandwidth; + bool& enable_bluetooth_listening; + bool& enable_webrtc_listening; + bool& low_power; + bool& enforce_topology_constraints; + + // Whether this is intended to be used in conjunction with InjectEndpoint(). + bool& is_out_of_band_connection; + ByteArray& remote_bluetooth_mac_address; + const char* fast_advertisement_service_uuid; + int& keep_alive_interval_millis; + int& keep_alive_timeout_millis; + + // Verify if ConnectionOptions is in a not-initialized (Empty) state. + bool Empty() const; + + // Bring ConnectionOptions to a not-initialized (Empty) state. + void 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. + // (2) If no mediums are allowed, allow all mediums. + ConnectionOptions CompatibleOptions() const; + + // 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); +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // WINDOWS_REQUEST_CONNECTION_OPTIONS_H_ diff --git a/windows/start_advertising_options.cc b/windows/start_advertising_options.cc new file mode 100644 index 00000000..4813eeea --- /dev/null +++ b/windows/start_advertising_options.cc @@ -0,0 +1,72 @@ +// 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 "third_party/nearby_connections/windows/start_advertising_options.h" + +#include "core/options.h" +#include "core/strategy.h" +#include "platform/base/byte_array.h" +#include "platform/public/core_config.h" + +namespace location { +namespace nearby { +namespace connections { + +StartAdvertisingOptions::StartAdvertisingOptions() + : impl_(new connections::ConnectionOptions(), + [](connections::ConnectionOptions* impl) { delete impl; }), + strategy(impl_->strategy), + allowed(impl_->allowed), + auto_upgrade_bandwidth(impl_->auto_upgrade_bandwidth), + enable_bluetooth_listening(impl_->enable_bluetooth_listening), + enable_webrtc_listening(impl_->enable_webrtc_listening), + low_power(impl_->low_power), + enforce_topology_constraints(impl_->enforce_topology_constraints), + is_out_of_band_connection(impl_->is_out_of_band_connection), + remote_bluetooth_mac_address(impl_->remote_bluetooth_mac_address), + fast_advertisement_service_uuid( + impl_->fast_advertisement_service_uuid.c_str()), + keep_alive_interval_millis(impl_->keep_alive_interval_millis), + keep_alive_timeout_millis(impl_->keep_alive_timeout_millis) {} + +StartAdvertisingOptions::operator connections::ConnectionOptions() const { + return *impl_.get(); +} + +// Verify if ConnectionOptions is in a not-initialized (Empty) state. +bool StartAdvertisingOptions::Empty() const { return impl_->Empty(); } + +// Bring ConnectionOptions to a not-initialized (Empty) state. +void StartAdvertisingOptions::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. +// (2) If no mediums are allowed, allow all mediums. +ConnectionOptions StartAdvertisingOptions::CompatibleOptions() const { + return impl_->CompatibleOptions(); +} + +// std::vector GetMediums() const; + +// 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 StartAdvertisingOptions::GetMediums( + location::nearby::proto::connections::Medium* mediums, + uint32_t* mediumsSize) {} + +} // namespace connections +} // namespace nearby +} // namespace location diff --git a/windows/start_advertising_options.h b/windows/start_advertising_options.h new file mode 100644 index 00000000..6c3a9854 --- /dev/null +++ b/windows/start_advertising_options.h @@ -0,0 +1,90 @@ +// 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. + +#ifndef WINDOWS_START_ADVERTISING_OPTIONS_H_ +#define WINDOWS_START_ADVERTISING_OPTIONS_H_ + +#include + +#include + +#include "platform/public/core_config.h" + +namespace location { +namespace nearby { +class ByteArray; +namespace proto { +namespace connections { +enum Medium : int; +} +} // namespace proto +namespace connections { + +struct ConnectionOptions; +class Strategy; +template +struct MediumSelector; + +using BooleanMediumSelector = MediumSelector; + +struct DLL_API StartAdvertisingOptions { + StartAdvertisingOptions(); + operator connections::ConnectionOptions() const; + + private: + std::unique_ptr + impl_; + + public: + Strategy& strategy; + BooleanMediumSelector& allowed; + + bool& auto_upgrade_bandwidth; + bool& enable_bluetooth_listening; + bool& enable_webrtc_listening; + bool& low_power; + bool& enforce_topology_constraints; + + // Whether this is intended to be used in conjunction with InjectEndpoint(). + bool& is_out_of_band_connection; + ByteArray& remote_bluetooth_mac_address; + const char* fast_advertisement_service_uuid; + int& keep_alive_interval_millis; + int& keep_alive_timeout_millis; + + // Verify if ConnectionOptions is in a not-initialized (Empty) state. + bool Empty() const; + + // Bring ConnectionOptions to a not-initialized (Empty) state. + void 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. + // (2) If no mediums are allowed, allow all mediums. + ConnectionOptions CompatibleOptions() const; + + // 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); +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // WINDOWS_START_ADVERTISING_OPTIONS_H_ diff --git a/windows/start_discovery_options.cc b/windows/start_discovery_options.cc new file mode 100644 index 00000000..bbcb6a5d --- /dev/null +++ b/windows/start_discovery_options.cc @@ -0,0 +1,72 @@ +// 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 "third_party/nearby_connections/windows/start_discovery_options.h" + +#include "core/options.h" +#include "core/strategy.h" +#include "platform/base/byte_array.h" +#include "platform/public/core_config.h" + +namespace location { +namespace nearby { +namespace connections { + +StartDiscoveryOptions::StartDiscoveryOptions() + : impl_(new connections::ConnectionOptions(), + [](connections::ConnectionOptions* impl) { delete impl; }), + strategy(impl_->strategy), + allowed(impl_->allowed), + auto_upgrade_bandwidth(impl_->auto_upgrade_bandwidth), + enable_bluetooth_listening(impl_->enable_bluetooth_listening), + enable_webrtc_listening(impl_->enable_webrtc_listening), + low_power(impl_->low_power), + enforce_topology_constraints(impl_->enforce_topology_constraints), + is_out_of_band_connection(impl_->is_out_of_band_connection), + remote_bluetooth_mac_address(impl_->remote_bluetooth_mac_address), + fast_advertisement_service_uuid( + impl_->fast_advertisement_service_uuid.c_str()), + keep_alive_interval_millis(impl_->keep_alive_interval_millis), + keep_alive_timeout_millis(impl_->keep_alive_timeout_millis) {} + +StartDiscoveryOptions::operator connections::ConnectionOptions() const { + return *impl_.get(); +} + +// Verify if ConnectionOptions is in a not-initialized (Empty) state. +bool StartDiscoveryOptions::Empty() const { return impl_->Empty(); } + +// Bring ConnectionOptions to a not-initialized (Empty) state. +void StartDiscoveryOptions::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. +// (2) If no mediums are allowed, allow all mediums. +ConnectionOptions StartDiscoveryOptions::CompatibleOptions() const { + return impl_->CompatibleOptions(); +} + +// std::vector GetMediums() const; + +// 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 StartDiscoveryOptions::GetMediums( + location::nearby::proto::connections::Medium* mediums, + uint32_t* mediumsSize) {} + +} // namespace connections +} // namespace nearby +} // namespace location diff --git a/windows/start_discovery_options.h b/windows/start_discovery_options.h new file mode 100644 index 00000000..d1d616e0 --- /dev/null +++ b/windows/start_discovery_options.h @@ -0,0 +1,90 @@ +// 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. + +#ifndef WINDOWS_START_DISCOVERY_OPTIONS_H_ +#define WINDOWS_START_DISCOVERY_OPTIONS_H_ + +#include + +#include + +#include "platform/public/core_config.h" + +namespace location { +namespace nearby { +class ByteArray; +namespace proto { +namespace connections { +enum Medium : int; +} +} // namespace proto +namespace connections { + +struct ConnectionOptions; +class Strategy; +template +struct MediumSelector; + +using BooleanMediumSelector = MediumSelector; + +struct DLL_API StartDiscoveryOptions { + StartDiscoveryOptions(); + operator connections::ConnectionOptions() const; + + private: + std::unique_ptr + impl_; + + public: + Strategy& strategy; + BooleanMediumSelector& allowed; + + bool& auto_upgrade_bandwidth; + bool& enable_bluetooth_listening; + bool& enable_webrtc_listening; + bool& low_power; + bool& enforce_topology_constraints; + + // Whether this is intended to be used in conjunction with InjectEndpoint(). + bool& is_out_of_band_connection; + ByteArray& remote_bluetooth_mac_address; + const char* fast_advertisement_service_uuid; + int& keep_alive_interval_millis; + int& keep_alive_timeout_millis; + + // Verify if ConnectionOptions is in a not-initialized (Empty) state. + bool Empty() const; + + // Bring ConnectionOptions to a not-initialized (Empty) state. + void 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. + // (2) If no mediums are allowed, allow all mediums. + ConnectionOptions CompatibleOptions() const; + + // 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); +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // WINDOWS_START_DISCOVERY_OPTIONS_H_