diff --git a/cpp/core/BUILD b/cpp/core/BUILD index 80133bb1..3da92495 100644 --- a/cpp/core/BUILD +++ b/cpp/core/BUILD @@ -43,16 +43,23 @@ cc_library( cc_library( name = "core_types", srcs = [ - "options.cc", + "advertising_options.cc", + "connection_options.cc", + "discovery_options.cc", "payload.cc", "strategy.cc", ], hdrs = [ + "advertising_options.h", + "connection_options.h", + "discovery_options.h", "listeners.h", "medium_selector.h", - "options.h", + "options_base.h", + "out_of_band_connection_metadata.h", "params.h", "payload.h", + "power_level.h", "status.h", "strategy.h", ], diff --git a/cpp/core/options.cc b/cpp/core/advertising_options.cc similarity index 55% rename from cpp/core/options.cc rename to cpp/core/advertising_options.cc index 58da2d22..c69677e1 100644 --- a/cpp/core/options.cc +++ b/cpp/core/advertising_options.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "core/options.h" +#include "core/advertising_options.h" #include @@ -20,18 +20,12 @@ namespace location { namespace nearby { namespace connections { -// Verify if ConnectionOptions is in a not-initialized (Empty) state. -bool ConnectionOptions::Empty() const { return strategy.IsNone(); } - -// Bring ConnectionOptions to a not-initialized (Empty) state. -void ConnectionOptions::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. // (2) If no mediums are allowed, allow all mediums. -ConnectionOptions ConnectionOptions::CompatibleOptions() const { - ConnectionOptions result = *this; +AdvertisingOptions AdvertisingOptions::CompatibleOptions() const { + AdvertisingOptions result = *this; // Out-of-band connections initiate connections via an injected endpoint // rather than through the normal discovery flow. These types of connections @@ -53,37 +47,6 @@ ConnectionOptions ConnectionOptions::CompatibleOptions() const { return result; } -std::vector ConnectionOptions::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 ConnectionOptions::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); - } -} - } // namespace connections } // namespace nearby } // namespace location diff --git a/cpp/core/advertising_options.h b/cpp/core/advertising_options.h new file mode 100644 index 00000000..5967b3ab --- /dev/null +++ b/cpp/core/advertising_options.h @@ -0,0 +1,53 @@ +// 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 CORE_ADVERTISING_OPTIONS_H_ +#define CORE_ADVERTISING_OPTIONS_H_ +#include + +#include "core/medium_selector.h" +#include "core/options_base.h" +#include "core/power_level.h" +#include "core/strategy.h" +#include "platform/base/byte_array.h" +#include "proto/connections_enums.pb.h" + +namespace location { +namespace nearby { +namespace connections { + +// Connection Options: used for both Advertising and Discovery. +// All fields are mutable, to make the type copy-assignable. +struct AdvertisingOptions : public OptionsBase { + bool auto_upgrade_bandwidth; + bool enforce_topology_constraints; + 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; + std::string fast_advertisement_service_uuid; + + // 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. + AdvertisingOptions CompatibleOptions() const; +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // CORE_ADVERTISING_OPTIONS_H_ diff --git a/cpp/core/connection_options.cc b/cpp/core/connection_options.cc new file mode 100644 index 00000000..3629efb8 --- /dev/null +++ b/cpp/core/connection_options.cc @@ -0,0 +1,29 @@ +// 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 "core/connection_options.h" + +#include + +namespace location { +namespace nearby { +namespace connections { + +std::vector ConnectionOptions::GetMediums() const { + return allowed.GetMediums(true); +} + +} // namespace connections +} // namespace nearby +} // namespace location diff --git a/cpp/core/connection_options.h b/cpp/core/connection_options.h new file mode 100644 index 00000000..93114507 --- /dev/null +++ b/cpp/core/connection_options.h @@ -0,0 +1,55 @@ +// 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 CORE_CONNECTION_OPTIONS_H_ +#define CORE_CONNECTION_OPTIONS_H_ +#include + +#include "core/medium_selector.h" +#include "core/options_base.h" +#include "core/power_level.h" +#include "core/strategy.h" +#include "platform/base/byte_array.h" +#include "proto/connections_enums.pb.h" + +namespace location { +namespace nearby { +namespace connections { + +// Feature On/Off switch for mediums. +using BooleanMediumSelector = MediumSelector; + +// Connection Options: used for both Advertising and Discovery. +// All fields are mutable, to make the type copy-assignable. +struct ConnectionOptions : public OptionsBase { + bool auto_upgrade_bandwidth; + bool enforce_topology_constraints; + 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; + + std::vector GetMediums() const; +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // CORE_CONNECTION_OPTIONS_H_ diff --git a/cpp/core/core.cc b/cpp/core/core.cc index beb85969..6fa57139 100644 --- a/cpp/core/core.cc +++ b/cpp/core/core.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -20,7 +20,6 @@ #include #include "absl/time/clock.h" -#include "core/options.h" #include "platform/base/feature_flags.h" #include "platform/public/count_down_latch.h" #include "platform/public/logging.h" @@ -50,13 +49,14 @@ Core::Core(Core&&) = default; Core& Core::operator=(Core&&) = default; void Core::StartAdvertising(absl::string_view service_id, - ConnectionOptions options, + AdvertisingOptions advertising_options, ConnectionRequestInfo info, ResultCallback callback) { assert(!service_id.empty()); - assert(options.strategy.IsValid()); + assert(advertising_options.strategy.IsValid()); - router_->StartAdvertising(&client_, service_id, options, info, callback); + router_->StartAdvertising(&client_, service_id, advertising_options, info, + callback); } void Core::StopAdvertising(const ResultCallback callback) { @@ -64,12 +64,13 @@ void Core::StopAdvertising(const ResultCallback callback) { } void Core::StartDiscovery(absl::string_view service_id, - ConnectionOptions options, DiscoveryListener listener, - ResultCallback callback) { + DiscoveryOptions discovery_options, + DiscoveryListener listener, ResultCallback callback) { assert(!service_id.empty()); - assert(options.strategy.IsValid()); + assert(discovery_options.strategy.IsValid()); - router_->StartDiscovery(&client_, service_id, options, listener, callback); + router_->StartDiscovery(&client_, service_id, discovery_options, listener, + callback); } void Core::InjectEndpoint(absl::string_view service_id, @@ -84,27 +85,30 @@ void Core::StopDiscovery(ResultCallback callback) { void Core::RequestConnection(absl::string_view endpoint_id, ConnectionRequestInfo info, - ConnectionOptions options, + ConnectionOptions connection_options, ResultCallback callback) { assert(!endpoint_id.empty()); // Assign the default from feature flags for the keep-alive frame interval and // timeout values if client don't mind them or has the unexpected ones. - if (options.keep_alive_interval_millis == 0 || - options.keep_alive_timeout_millis == 0 || - options.keep_alive_interval_millis >= options.keep_alive_timeout_millis) { + if (connection_options.keep_alive_interval_millis == 0 || + connection_options.keep_alive_timeout_millis == 0 || + connection_options.keep_alive_interval_millis >= + connection_options.keep_alive_timeout_millis) { NEARBY_LOG( WARNING, "Client request connection with keep-alive frame as interval=%d, " "timeout=%d, which is un-expected. Change to default.", - options.keep_alive_interval_millis, options.keep_alive_timeout_millis); - options.keep_alive_interval_millis = + connection_options.keep_alive_interval_millis, + connection_options.keep_alive_timeout_millis); + connection_options.keep_alive_interval_millis = FeatureFlags::GetInstance().GetFlags().keep_alive_interval_millis; - options.keep_alive_timeout_millis = + connection_options.keep_alive_timeout_millis = FeatureFlags::GetInstance().GetFlags().keep_alive_timeout_millis; } - router_->RequestConnection(&client_, endpoint_id, info, options, callback); + router_->RequestConnection(&client_, endpoint_id, info, connection_options, + callback); } void Core::AcceptConnection(absl::string_view endpoint_id, diff --git a/cpp/core/core.h b/cpp/core/core.h index 1166ad48..6233bfa3 100644 --- a/cpp/core/core.h +++ b/cpp/core/core.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -25,7 +25,6 @@ #include "core/internal/service_controller.h" #include "core/internal/service_controller_router.h" #include "core/listeners.h" -#include "core/options.h" #include "core/params.h" namespace location { @@ -49,7 +48,7 @@ class Core { // This can be an arbitrary string, so long as it uniquely // identifies your service. A good default is to use your // app's package name. - // options - The options for advertising. + // advertising_options - The options for advertising. // info - Connection parameters: // > name - A human readable name for this endpoint, to appear on // other devices. @@ -61,7 +60,8 @@ class Core { // Status::STATUS_ALREADY_ADVERTISING if the app is already advertising. // Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently // connected to remote endpoints; call StopAllEndpoints first. - void StartAdvertising(absl::string_view service_id, ConnectionOptions options, + void StartAdvertising(absl::string_view service_id, + AdvertisingOptions advertising_options, ConnectionRequestInfo info, ResultCallback callback); // Stops advertising a local endpoint. Should be called after calling @@ -79,7 +79,7 @@ class Core { // service_id - The ID for the service to be discovered, as specified in // the corresponding call to StartAdvertising. // listener - A callback notified when a remote endpoint is discovered. - // options - The options for discovery. + // discovery_options - The options for discovery. // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK if discovery started successfully. @@ -87,7 +87,8 @@ class Core { // discovering the specified service. // Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently // connected to remote endpoints; call StopAllEndpoints first. - void StartDiscovery(absl::string_view service_id, ConnectionOptions options, + void StartDiscovery(absl::string_view service_id, + DiscoveryOptions discovery_options, DiscoveryListener listener, ResultCallback callback); // Stops discovery for remote endpoints, after a previous call to @@ -139,7 +140,8 @@ class Core { // issue with Bluetooth/WiFi. // Status::STATUS_ERROR if we failed to connect for any other reason. void RequestConnection(absl::string_view endpoint_id, - ConnectionRequestInfo info, ConnectionOptions options, + ConnectionRequestInfo info, + ConnectionOptions connection_options, ResultCallback callback); // Accepts a connection to a remote endpoint. This method must be called diff --git a/cpp/core/discovery_options.cc b/cpp/core/discovery_options.cc new file mode 100644 index 00000000..6ec3d4b8 --- /dev/null +++ b/cpp/core/discovery_options.cc @@ -0,0 +1,52 @@ +// 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 "core/discovery_options.h" + +#include + +namespace location { +namespace nearby { +namespace connections { + +// 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. +DiscoveryOptions DiscoveryOptions::CompatibleOptions() const { + DiscoveryOptions result = *this; + + // Out-of-band connections initiate connections via an injected endpoint + // rather than through the normal discovery flow. These types of connections + // can only be injected via a single medium. + if (is_out_of_band_connection) { + int num_enabled = result.allowed.Count(true); + + // Default to allow only Bluetooth if no single medium is specified. + if (num_enabled != 1) { + result.allowed.SetAll(false); + result.allowed.bluetooth = true; + } + return result; + } + + // Normal connections (i.e., not out-of-band) connections can specify + // multiple mediums. If none are specified, default to allowing all mediums. + if (!allowed.Any(true)) result.allowed.SetAll(true); + return result; +} + +} // namespace connections +} // namespace nearby +} // namespace location diff --git a/cpp/core/discovery_options.h b/cpp/core/discovery_options.h new file mode 100644 index 00000000..556c729e --- /dev/null +++ b/cpp/core/discovery_options.h @@ -0,0 +1,55 @@ +// 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 CORE_DISCOVERY_OPTIONS_H_ +#define CORE_DISCOVERY_OPTIONS_H_ +#include + +#include "core/medium_selector.h" +#include "core/options_base.h" +#include "core/power_level.h" +#include "core/strategy.h" +#include "platform/base/byte_array.h" +#include "proto/connections_enums.pb.h" + +namespace location { +namespace nearby { +namespace connections { + +// Feature On/Off switch for mediums. +using BooleanMediumSelector = MediumSelector; + +// Connection Options: used for both Advertising and Discovery. +// All fields are mutable, to make the type copy-assignable. +struct DiscoveryOptions : OptionsBase { + bool auto_upgrade_bandwidth; + bool enforce_topology_constraints; + int keep_alive_interval_millis = 0; + int keep_alive_timeout_millis = 0; + + // Whether this is intended to be used in conjunction with InjectEndpoint(). + bool is_out_of_band_connection = false; + std::string fast_advertisement_service_uuid; + + // 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. + DiscoveryOptions CompatibleOptions() const; +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // CORE_DISCOVERY_OPTIONS_H_ diff --git a/cpp/core/internal/base_pcp_handler.cc b/cpp/core/internal/base_pcp_handler.cc index db79f07c..58597a70 100644 --- a/cpp/core/internal/base_pcp_handler.cc +++ b/cpp/core/internal/base_pcp_handler.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -28,7 +28,6 @@ #include "core/internal/mediums/utils.h" #include "core/internal/offline_frames.h" #include "core/internal/pcp_handler.h" -#include "core/options.h" #include "platform/base/base64_utils.h" #include "platform/base/bluetooth_utils.h" #include "platform/public/logging.h" @@ -76,45 +75,49 @@ void BasePcpHandler::DisconnectFromEndpointManager() { this); } -Status BasePcpHandler::StartAdvertising(ClientProxy* client, - const std::string& service_id, - const ConnectionOptions& options, - const ConnectionRequestInfo& info) { +Status BasePcpHandler::StartAdvertising( + ClientProxy* client, const std::string& service_id, + const AdvertisingOptions& advertising_options, + const ConnectionRequestInfo& info) { Future response; + NEARBY_LOGS(INFO) << "StartAdvertising with supported mediums: " - << GetStringValueOfSupportedMediums(options); - ConnectionOptions advertising_options = options.CompatibleOptions(); + << GetStringValueOfSupportedMediums(advertising_options); + + AdvertisingOptions compatible_advertising_options = + advertising_options.CompatibleOptions(); + RunOnPcpHandlerThread( "start-advertising", - [this, client, &service_id, &info, &advertising_options, &response]() - RUN_ON_PCP_HANDLER_THREAD() { - // The endpoint id inside of the advertisement is different to high - // visibility and low visibility mode. In order to decide if client - // should grab the high visibility or low visibility id, it needs to - // tell client which one right now, before - // client#StartedAdvertising. - if (ShouldEnterHighVisibilityMode(advertising_options)) { - client->EnterHighVisibilityMode(); - } + [this, client, &service_id, &info, &compatible_advertising_options, + &response]() RUN_ON_PCP_HANDLER_THREAD() { + // The endpoint id inside of the advertisement is different to high + // visibility and low visibility mode. In order to decide if client + // should grab the high visibility or low visibility id, it needs to + // tell client which one right now, before + // client#StartedAdvertising. + if (ShouldEnterHighVisibilityMode(compatible_advertising_options)) { + client->EnterHighVisibilityMode(); + } - auto result = StartAdvertisingImpl( - client, service_id, client->GetLocalEndpointId(), - info.endpoint_info, advertising_options); - if (!result.status.Ok()) { - client->ExitHighVisibilityMode(); - response.Set(result.status); - return; - } + auto result = StartAdvertisingImpl( + client, service_id, client->GetLocalEndpointId(), + info.endpoint_info, compatible_advertising_options); + if (!result.status.Ok()) { + client->ExitHighVisibilityMode(); + response.Set(result.status); + return; + } - // Now that we've succeeded, mark the client as advertising. - // Save the advertising options for local reference in later process - // like upgrading bandwidth. - advertising_listener_ = info.listener; - client->StartedAdvertising(service_id, GetStrategy(), info.listener, - absl::MakeSpan(result.mediums), - advertising_options); - response.Set({Status::kSuccess}); - }); + // Now that we've succeeded, mark the client as advertising. + // Save the advertising options for local reference in later process + // like upgrading bandwidth. + advertising_listener_ = info.listener; + client->StartedAdvertising(service_id, GetStrategy(), info.listener, + absl::MakeSpan(result.mediums), + compatible_advertising_options); + response.Set({Status::kSuccess}); + }); return WaitForResult(absl::StrCat("StartAdvertising(", service_id, ")"), client->GetClientId(), &response); } @@ -133,28 +136,48 @@ void BasePcpHandler::StopAdvertising(ClientProxy* client) { } std::string BasePcpHandler::GetStringValueOfSupportedMediums( - const ConnectionOptions& options) const { + const ConnectionOptions& connection_options) const { std::ostringstream result; - result << "{ "; - if (options.allowed.bluetooth) { - result << proto::connections::Medium_Name(Medium::BLUETOOTH) << " "; - } - if (options.allowed.ble) { - result << proto::connections::Medium_Name(Medium::BLE) << " "; - } - if (options.allowed.web_rtc) { - result << proto::connections::Medium_Name(Medium::WEB_RTC) << " "; - } - if (options.allowed.wifi_lan) { - result << proto::connections::Medium_Name(Medium::WIFI_LAN) << " "; - } - result << "}"; + OptionsAllowed(connection_options.allowed, result); return result.str(); } +std::string BasePcpHandler::GetStringValueOfSupportedMediums( + const AdvertisingOptions& advertising_options) const { + std::ostringstream result; + OptionsAllowed(advertising_options.allowed, result); + return result.str(); +} + +std::string BasePcpHandler::GetStringValueOfSupportedMediums( + const DiscoveryOptions& discovery_options) const { + std::ostringstream result; + OptionsAllowed(discovery_options.allowed, result); + return result.str(); +} + +void BasePcpHandler::OptionsAllowed(const BooleanMediumSelector& allowed, + std::ostringstream& result) const { + result << "{ "; + if (allowed.bluetooth) { + result << proto::connections::Medium_Name(Medium::BLUETOOTH) << " "; + } + if (allowed.ble) { + result << proto::connections::Medium_Name(Medium::BLE) << " "; + } + if (allowed.web_rtc) { + result << proto::connections::Medium_Name(Medium::WEB_RTC) << " "; + } + if (allowed.wifi_lan) { + result << proto::connections::Medium_Name(Medium::WIFI_LAN) << " "; + } + result << "}"; +} + bool BasePcpHandler::ShouldEnterHighVisibilityMode( - const ConnectionOptions& options) { - return !options.low_power && options.allowed.bluetooth; + const AdvertisingOptions& advertising_options) { + return !advertising_options.low_power && + advertising_options.allowed.bluetooth; } BooleanMediumSelector BasePcpHandler::ComputeIntersectionOfSupportedMediums( @@ -173,7 +196,7 @@ BooleanMediumSelector BasePcpHandler::ComputeIntersectionOfSupportedMediums( // We use advertising options as a proxy to whether or not the local // client does want to enable a WebRTC upgrade. if (my_medium == location::nearby::proto::connections::Medium::WEB_RTC) { - ConnectionOptions advertising_options = + AdvertisingOptions advertising_options = connection_info.client->GetAdvertisingOptions(); if (!advertising_options.enable_webrtc_listening && @@ -200,13 +223,12 @@ BooleanMediumSelector BasePcpHandler::ComputeIntersectionOfSupportedMediums( Status BasePcpHandler::StartDiscovery(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, const DiscoveryListener& listener) { Future response; - ConnectionOptions discovery_options = options.CompatibleOptions(); NEARBY_LOGS(INFO) << "StartDiscovery with supported mediums:" - << GetStringValueOfSupportedMediums(options); + << GetStringValueOfSupportedMediums(discovery_options); RunOnPcpHandlerThread( "start-discovery", [this, client, service_id, discovery_options, &listener, &response]() RUN_ON_PCP_HANDLER_THREAD() { @@ -367,27 +389,20 @@ void BasePcpHandler::OnEncryptionSuccessRunnable( .is_incoming_connection = connection_info.is_incoming, }, { - .strategy = connection_info.options.strategy, - .allowed = ComputeIntersectionOfSupportedMediums(connection_info), - .auto_upgrade_bandwidth = - connection_info.options.auto_upgrade_bandwidth, - .enforce_topology_constraints = - connection_info.options.enforce_topology_constraints, - .low_power = connection_info.options.low_power, - .enable_bluetooth_listening = - connection_info.options.enable_bluetooth_listening, - .enable_webrtc_listening = - connection_info.options.enable_webrtc_listening, - .is_out_of_band_connection = - connection_info.options.is_out_of_band_connection, - .remote_bluetooth_mac_address = - connection_info.options.remote_bluetooth_mac_address, - .fast_advertisement_service_uuid = - connection_info.options.fast_advertisement_service_uuid, - .keep_alive_interval_millis = - connection_info.options.keep_alive_interval_millis, - .keep_alive_timeout_millis = - connection_info.options.keep_alive_timeout_millis, + { + connection_info.connection_options.strategy, + ComputeIntersectionOfSupportedMediums(connection_info), + }, + connection_info.connection_options.auto_upgrade_bandwidth, + connection_info.connection_options.enforce_topology_constraints, + connection_info.connection_options.low_power, + connection_info.connection_options.enable_bluetooth_listening, + connection_info.connection_options.enable_webrtc_listening, + connection_info.connection_options.is_out_of_band_connection, + connection_info.connection_options.remote_bluetooth_mac_address, + connection_info.connection_options.fast_advertisement_service_uuid, + connection_info.connection_options.keep_alive_interval_millis, + connection_info.connection_options.keep_alive_timeout_millis, }, std::move(connection_info.channel), connection_info.listener, connection_info.connection_token); @@ -430,14 +445,15 @@ void BasePcpHandler::OnEncryptionFailureRunnable( info.result.lock().get()); } -Status BasePcpHandler::RequestConnection(ClientProxy* client, - const std::string& endpoint_id, - const ConnectionRequestInfo& info, - const ConnectionOptions& options) { +Status BasePcpHandler::RequestConnection( + ClientProxy* client, const std::string& endpoint_id, + const ConnectionRequestInfo& info, + const ConnectionOptions& connection_options) { auto result = std::make_shared>(); RunOnPcpHandlerThread( - "request-connection", [this, client, &info, options, endpoint_id, - result]() RUN_ON_PCP_HANDLER_THREAD() { + "request-connection", + [this, client, &info, connection_options, endpoint_id, + result]() RUN_ON_PCP_HANDLER_THREAD() { absl::Time start_time = SystemClock::ElapsedRealtime(); // If we already have a pending connection, then we shouldn't allow any @@ -472,8 +488,8 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client, return; } - auto remote_bluetooth_mac_address = - BluetoothUtils::ToString(options.remote_bluetooth_mac_address); + auto remote_bluetooth_mac_address = BluetoothUtils::ToString( + connection_options.remote_bluetooth_mac_address); if (!remote_bluetooth_mac_address.empty()) { if (AppendRemoteBluetoothMacAddressEndpoint( endpoint_id, remote_bluetooth_mac_address, @@ -492,7 +508,7 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client, for (auto connect_endpoint : discovered_endpoints) { if (!MediumSupportedByClientOptions(connect_endpoint->medium, - options)) + connection_options)) continue; connect_impl_result = ConnectImpl(client, connect_endpoint); if (connect_impl_result.status.Ok()) { @@ -524,9 +540,9 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client, // endpoint about ourselves. Exception write_exception = WriteConnectionRequestFrame( channel.get(), client->GetLocalEndpointId(), info.endpoint_info, - nonce, GetSupportedConnectionMediumsByPriority(options), - options.keep_alive_interval_millis, - options.keep_alive_timeout_millis); + nonce, GetSupportedConnectionMediumsByPriority(connection_options), + connection_options.keep_alive_interval_millis, + connection_options.keep_alive_timeout_millis); if (!write_exception.Ok()) { NEARBY_LOGS(INFO) << "Failed to send connection request: endpoint_id=" << endpoint_id; @@ -554,7 +570,7 @@ Status BasePcpHandler::RequestConnection(ClientProxy* client, pendingConnectionInfo.is_incoming = false; pendingConnectionInfo.start_time = start_time; pendingConnectionInfo.listener = info.listener; - pendingConnectionInfo.options = options; + pendingConnectionInfo.connection_options = connection_options; pendingConnectionInfo.result = result; pendingConnectionInfo.channel = std::move(channel); @@ -595,10 +611,11 @@ bool BasePcpHandler::MediumSupportedByClientOptions( // option. std::vector BasePcpHandler::GetSupportedConnectionMediumsByPriority( - const ConnectionOptions& local_option) { + const ConnectionOptions& local_connection_option) { std::vector supported_mediums_by_priority; for (auto medium_by_priority : GetConnectionMediumsByPriority()) { - if (MediumSupportedByClientOptions(medium_by_priority, local_option)) { + if (MediumSupportedByClientOptions(medium_by_priority, + local_connection_option)) { supported_mediums_by_priority.push_back(medium_by_priority); } } @@ -719,7 +736,7 @@ void BasePcpHandler::ProcessPreConnectionResultFailure( } bool BasePcpHandler::ShouldEnforceTopologyConstraints( - const ConnectionOptions& local_advertising_options) const { + const AdvertisingOptions& local_advertising_options) const { // Topology constraints only matter for the advertiser. // For discoverers, we'll always enforce them. if (local_advertising_options.strategy.IsNone()) { @@ -730,7 +747,7 @@ bool BasePcpHandler::ShouldEnforceTopologyConstraints( } bool BasePcpHandler::AutoUpgradeBandwidth( - const ConnectionOptions& local_advertising_options) const { + const AdvertisingOptions& local_advertising_options) const { if (local_advertising_options.strategy.IsNone()) { return true; } @@ -1114,26 +1131,27 @@ Exception BasePcpHandler::OnIncomingConnection( // Retrieve the keep-alive frame interval and timeout fields. If the frame // doesn't have those fields, we need to get them as default from feature // flags to prevent 0-values causing thread ill. - ConnectionOptions options = {.keep_alive_interval_millis = 0, - .keep_alive_timeout_millis = 0}; + ConnectionOptions connection_options = {.keep_alive_interval_millis = 0, + .keep_alive_timeout_millis = 0}; if (connection_request.has_keep_alive_interval_millis() && connection_request.has_keep_alive_timeout_millis()) { - options.keep_alive_interval_millis = + connection_options.keep_alive_interval_millis = connection_request.keep_alive_interval_millis(); - options.keep_alive_timeout_millis = + connection_options.keep_alive_timeout_millis = connection_request.keep_alive_timeout_millis(); } - if (options.keep_alive_interval_millis == 0 || - options.keep_alive_timeout_millis == 0 || - options.keep_alive_interval_millis >= options.keep_alive_timeout_millis) { + if (connection_options.keep_alive_interval_millis == 0 || + connection_options.keep_alive_timeout_millis == 0 || + connection_options.keep_alive_interval_millis >= + connection_options.keep_alive_timeout_millis) { NEARBY_LOGS(WARNING) << "Incoming connection has wrong keep-alive frame interval=" - << options.keep_alive_interval_millis - << ", timeout=" << options.keep_alive_timeout_millis + << connection_options.keep_alive_interval_millis + << ", timeout=" << connection_options.keep_alive_timeout_millis << " values; correct them as default.", - options.keep_alive_interval_millis = + connection_options.keep_alive_interval_millis = FeatureFlags::GetInstance().GetFlags().keep_alive_interval_millis; - options.keep_alive_timeout_millis = + connection_options.keep_alive_timeout_millis = FeatureFlags::GetInstance().GetFlags().keep_alive_timeout_millis; } @@ -1150,7 +1168,7 @@ Exception BasePcpHandler::OnIncomingConnection( pendingConnectionInfo.is_incoming = true; pendingConnectionInfo.start_time = start_time; pendingConnectionInfo.listener = advertising_listener_; - pendingConnectionInfo.options = options; + pendingConnectionInfo.connection_options = connection_options; pendingConnectionInfo.supported_mediums = parser::ConnectionRequestMediumsToMediums(connection_request); pendingConnectionInfo.channel = std::move(channel); @@ -1240,7 +1258,7 @@ void BasePcpHandler::ProcessTieBreakLoss( bool BasePcpHandler::AppendRemoteBluetoothMacAddressEndpoint( const std::string& endpoint_id, const std::string& remote_bluetooth_mac_address, - const ConnectionOptions& local_discovery_options) { + const DiscoveryOptions& local_discovery_options) { if (!local_discovery_options.allowed.bluetooth) { return false; } @@ -1283,7 +1301,7 @@ bool BasePcpHandler::AppendRemoteBluetoothMacAddressEndpoint( bool BasePcpHandler::AppendWebRTCEndpoint( const std::string& endpoint_id, - const ConnectionOptions& local_discovery_options) { + const DiscoveryOptions& local_discovery_options) { if (!local_discovery_options.allowed.web_rtc) { return false; } diff --git a/cpp/core/internal/base_pcp_handler.h b/cpp/core/internal/base_pcp_handler.h index 4e69ee2a..481bccf0 100644 --- a/cpp/core/internal/base_pcp_handler.h +++ b/cpp/core/internal/base_pcp_handler.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -39,7 +39,6 @@ #include "core/internal/pcp.h" #include "core/internal/pcp_handler.h" #include "core/listeners.h" -#include "core/options.h" #include "core/status.h" #include "platform/base/byte_array.h" #include "platform/base/prng.h" @@ -91,7 +90,7 @@ class BasePcpHandler : public PcpHandler, // See // cpp/core/listeners.h Status StartAdvertising(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const AdvertisingOptions& advertising_options, const ConnectionRequestInfo& info) override; // Stops Advertising is active, and changes CLientProxy state, @@ -102,7 +101,7 @@ class BasePcpHandler : public PcpHandler, // Updates ClientProxy state once discovery started. // DiscoveryListener will get called in case of any event. Status StartDiscovery(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, const DiscoveryListener& listener) override; // Stops Discovery if it is active, and changes CLientProxy state, @@ -114,9 +113,10 @@ class BasePcpHandler : public PcpHandler, // Requests a newly discovered remote endpoint it to form a connection. // Updates state on ClientProxy. - Status RequestConnection(ClientProxy* client, const std::string& endpoint_id, - const ConnectionRequestInfo& info, - const ConnectionOptions& options) override; + Status RequestConnection( + ClientProxy* client, const std::string& endpoint_id, + const ConnectionRequestInfo& info, + const ConnectionOptions& connection_options) override; // Called by either party to accept connection on their part. // Until both parties call it, connection will not reach a data phase. @@ -230,9 +230,6 @@ class BasePcpHandler : public PcpHandler, BluetoothDevice GetRemoteBluetoothDevice( const std::string& remote_bluetooth_mac_address); - ConnectionOptions GetConnectionOptions() const; - ConnectionOptions GetDiscoveryOptions() const; - void OnEndpointFound(ClientProxy* client, std::shared_ptr endpoint) RUN_ON_PCP_HANDLER_THREAD(); @@ -254,7 +251,8 @@ class BasePcpHandler : public PcpHandler, virtual StartOperationResult StartAdvertisingImpl( ClientProxy* client, const std::string& service_id, const std::string& local_endpoint_id, - const ByteArray& local_endpoint_info, const ConnectionOptions& options) + const ByteArray& local_endpoint_info, + const AdvertisingOptions& advertising_options) RUN_ON_PCP_HANDLER_THREAD() = 0; virtual Status StopAdvertisingImpl(ClientProxy* client) @@ -262,7 +260,8 @@ class BasePcpHandler : public PcpHandler, virtual StartOperationResult StartDiscoveryImpl( ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options) RUN_ON_PCP_HANDLER_THREAD() = 0; + const DiscoveryOptions& discovery_options) + RUN_ON_PCP_HANDLER_THREAD() = 0; virtual Status StopDiscoveryImpl(ClientProxy* client) RUN_ON_PCP_HANDLER_THREAD() = 0; @@ -333,7 +332,7 @@ class BasePcpHandler : public PcpHandler, absl::Time start_time{absl::InfinitePast()}; // Client callbacks. Always valid. ConnectionListener listener; - ConnectionOptions options; + ConnectionOptions connection_options; // Only set for outgoing connections. If set, we must call // result->Set() when connection is established, or rejected. @@ -391,21 +390,18 @@ class BasePcpHandler : public PcpHandler, absl::Seconds(2); static constexpr int kConnectionTokenLength = 8; - void OnConnectionResponse(ClientProxy* client, const std::string& endpoint_id, - const OfflineFrame& frame); - // Returns true if the new endpoint is preferred over the old endpoint. bool IsPreferred(const BasePcpHandler::DiscoveredEndpoint& new_endpoint, const BasePcpHandler::DiscoveredEndpoint& old_endpoint); // Returns true, if connection party should respect the specified topology. bool ShouldEnforceTopologyConstraints( - const ConnectionOptions& local_advertising_options) const; + const AdvertisingOptions& local_advertising_options) const; // Returns true, if connection party should attempt to upgrade itself to // use a higher bandwidth medium, if it is available. bool AutoUpgradeBandwidth( - const ConnectionOptions& local_advertising_options) const; + const AdvertisingOptions& local_advertising_options) const; // Returns true if the incoming connection should be killed. This only // happens when an incoming connection arrives while we have an outgoing @@ -424,12 +420,12 @@ class BasePcpHandler : public PcpHandler, bool AppendRemoteBluetoothMacAddressEndpoint( const std::string& endpoint_id, const std::string& remote_bluetooth_mac_address, - const ConnectionOptions& local_discovery_options); + const DiscoveryOptions& local_discovery_options); // Returns true if the webrtc endpoint is created and appended into // discovered_endpoints_ with key endpoint_id. bool AppendWebRTCEndpoint(const std::string& endpoint_id, - const ConnectionOptions& local_discovery_options); + const DiscoveryOptions& local_discovery_options); void ProcessPreConnectionInitiationFailure( ClientProxy* client, Medium medium, const std::string& endpoint_id, @@ -477,24 +473,32 @@ class BasePcpHandler : public PcpHandler, Future* future); bool MediumSupportedByClientOptions( const proto::connections::Medium& medium, - const ConnectionOptions& client_options) const; + const ConnectionOptions& connection_options) const; std::vector GetSupportedConnectionMediumsByPriority( const ConnectionOptions& local_option); std::string GetStringValueOfSupportedMediums( - const ConnectionOptions& options) const; + const ConnectionOptions& connection_options) const; + std::string GetStringValueOfSupportedMediums( + const AdvertisingOptions& advertising_options) const; + std::string GetStringValueOfSupportedMediums( + const DiscoveryOptions& discovery_options) const; // The endpoint id in high visibility mode is stable for 30 seconds, while in // low visibility mode it always rotates. We assume a client is trying to - // rotate endpoint id when the options is "low power" (3P) or "disable - // Bluetooth classic" (1P). - bool ShouldEnterHighVisibilityMode(const ConnectionOptions& options); + // rotate endpoint id when the advertising options is "low power" (3P) or + // "disable Bluetooth classic" (1P). + bool ShouldEnterHighVisibilityMode( + const AdvertisingOptions& advertising_options); // Returns the intersection of supported mediums based on the mediums reported // by the remote client and the local client's advertising options. BooleanMediumSelector ComputeIntersectionOfSupportedMediums( const PendingConnectionInfo& connection_info); + void OptionsAllowed(const BooleanMediumSelector& allowed, + std::ostringstream& result) const; + ScheduledExecutor alarm_executor_; SingleThreadExecutor serial_executor_; diff --git a/cpp/core/internal/base_pcp_handler_test.cc b/cpp/core/internal/base_pcp_handler_test.cc index 6881c7b5..85d72c61 100644 --- a/cpp/core/internal/base_pcp_handler_test.cc +++ b/cpp/core/internal/base_pcp_handler_test.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -28,7 +28,6 @@ #include "core/internal/encryption_runner.h" #include "core/internal/offline_frames.h" #include "core/listeners.h" -#include "core/options.h" #include "core/params.h" #include "platform/base/byte_array.h" #include "platform/base/exception.h" @@ -127,12 +126,12 @@ class MockPcpHandler : public BasePcpHandler { (ClientProxy * client, const std::string& service_id, const std::string& local_endpoint_id, const ByteArray& local_endpoint_info, - const ConnectionOptions& options), + const AdvertisingOptions& advertising_options), (override)); MOCK_METHOD(Status, StopAdvertisingImpl, (ClientProxy * client), (override)); MOCK_METHOD(StartOperationResult, StartDiscoveryImpl, (ClientProxy * client, const std::string& service_id, - const ConnectionOptions& options), + const DiscoveryOptions& discovery_options), (override)); MOCK_METHOD(Status, StopDiscoveryImpl, (ClientProxy * client), (override)); MOCK_METHOD(Status, InjectEndpointImpl, @@ -236,11 +235,13 @@ class BasePcpHandlerTest void StartAdvertising(ClientProxy* client, MockPcpHandler* pcp_handler, BooleanMediumSelector allowed = GetParam()) { std::string service_id{"service"}; - ConnectionOptions options{ - .strategy = Strategy::kP2pCluster, - .allowed = allowed, - .auto_upgrade_bandwidth = true, - .enforce_topology_constraints = true, + AdvertisingOptions advertising_options{ + { + Strategy::kP2pCluster, + allowed, + }, + true, // auto_upgrade_bandwidth + true, // enforce_topology_constraints }; ConnectionRequestInfo info{ .endpoint_info = ByteArray{"remote_endpoint_name"}, @@ -252,7 +253,8 @@ class BasePcpHandlerTest .status = {Status::kSuccess}, .mediums = pcp_handler->GetMediumsFromSelector(allowed), })); - EXPECT_EQ(pcp_handler->StartAdvertising(client, service_id, options, info), + EXPECT_EQ(pcp_handler->StartAdvertising(client, service_id, + advertising_options, info), Status{Status::kSuccess}); EXPECT_TRUE(client->IsAdvertising()); } @@ -260,20 +262,22 @@ class BasePcpHandlerTest void StartDiscovery(ClientProxy* client, MockPcpHandler* pcp_handler, BooleanMediumSelector allowed = GetParam()) { std::string service_id{"service"}; - ConnectionOptions options{ - .strategy = Strategy::kP2pCluster, - .allowed = allowed, - .auto_upgrade_bandwidth = true, - .enforce_topology_constraints = true, - .keep_alive_interval_millis = 5000, - .keep_alive_timeout_millis = 3000, + DiscoveryOptions discovery_options{ + { + Strategy::kP2pCluster, + allowed, + }, + true, // auto_upgrade_bandwidth + true, // enforce_topology_constraints + 5000, // keep_alive_interval_millis + 3000, // keep_alive_timeout_millis }; EXPECT_CALL(*pcp_handler, StartDiscoveryImpl(client, service_id, _)) .WillOnce(Return(MockPcpHandler::StartOperationResult{ .status = {Status::kSuccess}, .mediums = pcp_handler->GetMediumsFromSelector(allowed), })); - EXPECT_EQ(pcp_handler->StartDiscovery(client, service_id, options, + EXPECT_EQ(pcp_handler->StartDiscovery(client, service_id, discovery_options, discovery_listener_), Status{Status::kSuccess}); EXPECT_TRUE(client->IsDiscovering()); @@ -328,7 +332,7 @@ class BasePcpHandlerTest .endpoint_info = ByteArray{"ABCD"}, .listener = connection_listener_, }; - ConnectionOptions options{ + ConnectionOptions connection_options{ .remote_bluetooth_mac_address = ByteArray{std::string("\x12\x34\x56\x78\x9a\xbc")}, .keep_alive_interval_millis = @@ -381,9 +385,9 @@ class BasePcpHandlerTest encryption_runner->StartServer(other_client.get(), endpoint_id, channel_b, {}); } - EXPECT_EQ( - pcp_handler->RequestConnection(client, endpoint_id, info, options), - expected_result); + EXPECT_EQ(pcp_handler->RequestConnection(client, endpoint_id, info, + connection_options), + expected_result); NEARBY_LOG(INFO, "Stopping Encryption Runner"); } @@ -736,9 +740,15 @@ TEST_F(BasePcpHandlerTest, InjectEndpoint) { BooleanMediumSelector allowed{ .bluetooth = true, }; - ConnectionOptions options{ - .allowed = allowed, - .is_out_of_band_connection = true, + DiscoveryOptions discovery_options{ + { + Strategy::kP2pPointToPoint, + allowed, + }, + false, // auto_upgrade_bandwidth; + false, // enforce_topology_constraints; + 0, // keep_alive_interval_millis; + 0, // keep_alive_timeout_millis; }; EXPECT_CALL(mock_discovery_listener_.endpoint_found_cb, Call); EXPECT_CALL(pcp_handler, StartDiscoveryImpl(&client, service_id, _)) @@ -746,7 +756,7 @@ TEST_F(BasePcpHandlerTest, InjectEndpoint) { .status = {Status::kSuccess}, .mediums = allowed.GetMediums(true), })); - EXPECT_EQ(pcp_handler.StartDiscovery(&client, service_id, options, + EXPECT_EQ(pcp_handler.StartDiscovery(&client, service_id, discovery_options, discovery_listener_), Status{Status::kSuccess}); EXPECT_TRUE(client.IsDiscovering()); diff --git a/cpp/core/internal/bwu_manager.h b/cpp/core/internal/bwu_manager.h index b304a49a..0df53f51 100644 --- a/cpp/core/internal/bwu_manager.h +++ b/cpp/core/internal/bwu_manager.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -26,7 +26,6 @@ #include "core/internal/client_proxy.h" #include "core/internal/endpoint_manager.h" #include "core/internal/mediums/mediums.h" -#include "core/options.h" #include "platform/base/byte_array.h" #include "platform/public/scheduled_executor.h" diff --git a/cpp/core/internal/client_proxy.cc b/cpp/core/internal/client_proxy.cc index 106d4919..9b35d472 100644 --- a/cpp/core/internal/client_proxy.cc +++ b/cpp/core/internal/client_proxy.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -109,7 +109,7 @@ void ClientProxy::StartedAdvertising( const std::string& service_id, Strategy strategy, const ConnectionListener& listener, absl::Span mediums, - const ConnectionOptions& advertising_options) { + const AdvertisingOptions& advertising_options) { MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "ClientProxy [StartedAdvertising]: client=" << GetClientId(); @@ -168,7 +168,7 @@ void ClientProxy::StartedDiscovery( const std::string& service_id, Strategy strategy, const DiscoveryListener& listener, absl::Span mediums, - const ConnectionOptions& discovery_options) { + const DiscoveryOptions& discovery_options) { MutexLock lock(&mutex_); discovery_info_ = DiscoveryInfo{service_id, listener}; discovery_options_ = discovery_options; @@ -263,11 +263,10 @@ void ClientProxy::OnEndpointLost(const std::string& service_id, discovery_info_.listener.endpoint_lost_cb(endpoint_id); } -void ClientProxy::OnConnectionInitiated(const std::string& endpoint_id, - const ConnectionResponseInfo& info, - const ConnectionOptions& options, - const ConnectionListener& listener, - const std::string& connection_token) { +void ClientProxy::OnConnectionInitiated( + const std::string& endpoint_id, const ConnectionResponseInfo& info, + const ConnectionOptions& connection_options, + const ConnectionListener& listener, const std::string& connection_token) { MutexLock lock(&mutex_); // Whether this is incoming or outgoing, the local and remote endpoints both @@ -277,7 +276,7 @@ void ClientProxy::OnConnectionInitiated(const std::string& endpoint_id, endpoint_id, Connection{ .is_incoming = info.is_incoming_connection, .connection_listener = listener, - .connection_options = options, + .connection_options = connection_options, .connection_token = connection_token, }); // Instead of using structured binding which is nice, but banned @@ -691,11 +690,11 @@ void ClientProxy::AppendConnectionStatus(const std::string& endpoint_id, } } -ConnectionOptions ClientProxy::GetAdvertisingOptions() const { +AdvertisingOptions ClientProxy::GetAdvertisingOptions() const { return advertising_options_; } -ConnectionOptions ClientProxy::GetDiscoveryOptions() const { +DiscoveryOptions ClientProxy::GetDiscoveryOptions() const { return discovery_options_; } diff --git a/cpp/core/internal/client_proxy.h b/cpp/core/internal/client_proxy.h index 335adb77..69196e03 100644 --- a/cpp/core/internal/client_proxy.h +++ b/cpp/core/internal/client_proxy.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -20,8 +20,9 @@ #include #include +#include "core/advertising_options.h" +#include "core/discovery_options.h" #include "core/listeners.h" -#include "core/options.h" #include "core/status.h" #include "core/strategy.h" #include "platform/base/byte_array.h" @@ -72,7 +73,7 @@ class ClientProxy final { const std::string& service_id, Strategy strategy, const ConnectionListener& connection_lifecycle_listener, absl::Span mediums, - const ConnectionOptions& advertising_options = ConnectionOptions{}); + const AdvertisingOptions& advertising_options = AdvertisingOptions{}); // Marks this client as not advertising. void StoppedAdvertising(); bool IsAdvertising() const; @@ -87,7 +88,7 @@ class ClientProxy final { const std::string& service_id, Strategy strategy, const DiscoveryListener& discovery_listener, absl::Span mediums, - const ConnectionOptions& discovery_options = ConnectionOptions{}); + const DiscoveryOptions& discovery_options = DiscoveryOptions{}); // Marks this client as not discovering at all. void StoppedDiscovery(); bool IsDiscoveringServiceId(const std::string& service_id) const; @@ -106,7 +107,7 @@ class ClientProxy final { // Proxies to the client's ConnectionListener::OnInitiated() callback. void OnConnectionInitiated(const std::string& endpoint_id, const ConnectionResponseInfo& info, - const ConnectionOptions& options, + const ConnectionOptions& connection_options, const ConnectionListener& listener, const std::string& connection_token); @@ -177,8 +178,8 @@ class ClientProxy final { void CancelEndpoint(const std::string& endpoint_id); // Cancels all CancellationFlags. void CancelAllEndpoints(); - ConnectionOptions GetAdvertisingOptions() const; - ConnectionOptions GetDiscoveryOptions() const; + AdvertisingOptions GetAdvertisingOptions() const; + DiscoveryOptions GetDiscoveryOptions() const; // The endpoint id will be stable for 30 seconds after high visibility mode // (high power and Bluetooth Classic) advertisement stops. @@ -215,6 +216,8 @@ class ClientProxy final { ConnectionListener connection_listener; PayloadListener payload_listener; ConnectionOptions connection_options; + DiscoveryOptions discovery_options; + AdvertisingOptions advertising_options; std::string connection_token; }; @@ -285,13 +288,13 @@ class ClientProxy final { // Note: this is not cleared when the client stops advertising because it // might still be useful downstream of advertising (eg: establishing // connections, performing bandwidth upgrades, etc.) - ConnectionOptions advertising_options_; + AdvertisingOptions advertising_options_; // The active ClientProxy's discovery constraints. Null if the client // hasn't started discovering. Note: this is not cleared when the client // stops discovering because it might still be useful downstream of // discovery (eg: connection speed, etc.) - ConnectionOptions discovery_options_; + DiscoveryOptions discovery_options_; // Maps endpoint_id to endpoint connection state. absl::flat_hash_map connections_; diff --git a/cpp/core/internal/client_proxy_test.cc b/cpp/core/internal/client_proxy_test.cc index 61038279..a4df5f0e 100644 --- a/cpp/core/internal/client_proxy_test.cc +++ b/cpp/core/internal/client_proxy_test.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -23,7 +23,6 @@ #include "absl/time/time.h" #include "absl/types/span.h" #include "core/listeners.h" -#include "core/options.h" #include "core/strategy.h" #include "platform/base/byte_array.h" #include "platform/base/feature_flags.h" @@ -34,20 +33,19 @@ namespace nearby { namespace connections { namespace { -using FeatureFlags = FeatureFlags::Flags; using ::testing::MockFunction; using ::testing::StrictMock; -constexpr FeatureFlags kTestCases[] = { - FeatureFlags{ +constexpr FeatureFlags::Flags kTestCases[] = { + FeatureFlags::Flags{ .enable_cancellation_flag = true, }, - FeatureFlags{ + FeatureFlags::Flags{ .enable_cancellation_flag = false, }, }; -class ClientProxyTest : public ::testing::TestWithParam { +class ClientProxyTest : public ::testing::TestWithParam { protected: struct MockDiscoveryListener { StrictMock { std::string id; }; - bool ShouldEnterHighVisibilityMode(const ConnectionOptions& options) { - return !options.low_power && options.allowed.bluetooth; + bool ShouldEnterHighVisibilityMode( + const AdvertisingOptions& advertising_options) { + return !advertising_options.low_power && + advertising_options.allowed.bluetooth; } Endpoint StartAdvertising( ClientProxy* client, ConnectionListener listener, - ConnectionOptions advertising_options = ConnectionOptions{}) { + AdvertisingOptions advertising_options = AdvertisingOptions{}) { if (ShouldEnterHighVisibilityMode(advertising_options)) { client->EnterHighVisibilityMode(); } @@ -251,10 +251,12 @@ class ClientProxyTest : public ::testing::TestWithParam { mock_discovery_payload_.payload_progress_cb.AsStdFunction(), }; ConnectionOptions connection_options_; + AdvertisingOptions advertising_options_; + DiscoveryOptions discovery_options_; }; TEST_P(ClientProxyTest, CanCancelEndpoint) { - FeatureFlags feature_flags = GetParam(); + FeatureFlags::Flags feature_flags = GetParam(); MediumEnvironment::Instance().SetFeatureFlags(feature_flags); Endpoint advertising_endpoint = @@ -280,7 +282,7 @@ TEST_P(ClientProxyTest, CanCancelEndpoint) { } TEST_P(ClientProxyTest, CanCancelAllEndpoints) { - FeatureFlags feature_flags = GetParam(); + FeatureFlags::Flags feature_flags = GetParam(); MediumEnvironment::Instance().SetFeatureFlags(feature_flags); Endpoint advertising_endpoint = @@ -306,7 +308,7 @@ TEST_P(ClientProxyTest, CanCancelAllEndpoints) { } TEST_P(ClientProxyTest, CanCancelAllEndpointsWithDifferentEndpoint) { - FeatureFlags feature_flags = GetParam(); + FeatureFlags::Flags feature_flags = GetParam(); MediumEnvironment::Instance().SetFeatureFlags(feature_flags); ConnectionListener advertising_connection_listener_2; @@ -495,12 +497,18 @@ TEST_F(ClientProxyTest, OnPayloadProgressChangesState) { TEST_F(ClientProxyTest, EndpointIdCacheWhenHighVizAdvertisementAgainImmediately) { - ConnectionOptions advertising_options{.strategy = strategy_, - .allowed = - { - .bluetooth = true, - }, - .low_power = false}; + BooleanMediumSelector booleanMediumSelector; + booleanMediumSelector.bluetooth = true; + + AdvertisingOptions advertising_options{ + { + strategy_, + booleanMediumSelector, + }, + false, // auto_upgrade_bandwidth + false, // enforce_topology_constraints + false, // low_power + }; Endpoint advertising_endpoint_1 = StartAdvertising( &client1_, advertising_connection_listener_, advertising_options); @@ -516,12 +524,18 @@ TEST_F(ClientProxyTest, TEST_F(ClientProxyTest, EndpointIdRotateWhenHighVizAdvertisementAgainForAWhile) { - ConnectionOptions advertising_options{.strategy = strategy_, - .allowed = - { - .bluetooth = true, - }, - .low_power = false}; + BooleanMediumSelector booleanMediumSelector; + booleanMediumSelector.bluetooth = true; + + AdvertisingOptions advertising_options{ + { + strategy_, + booleanMediumSelector, + }, + false, // auto_upgrade_bandwidth + false, // enforce_topology_constraints + false, // low_power + }; Endpoint advertising_endpoint_1 = StartAdvertising( &client1_, advertising_connection_listener_, advertising_options); @@ -539,20 +553,33 @@ TEST_F(ClientProxyTest, TEST_F(ClientProxyTest, EndpointIdRotateWhenLowVizAdvertisementAfterHighVizAdvertisement) { - ConnectionOptions high_viz_advertising_options{.strategy = strategy_, - .allowed = - { - .bluetooth = true, - }, - .low_power = false}; + BooleanMediumSelector booleanMediumSelector; + booleanMediumSelector.bluetooth = true; + + AdvertisingOptions high_viz_advertising_options{ + { + strategy_, + booleanMediumSelector, + }, + false, // auto_upgrade_bandwidth + false, // enforce_topology_constraints + false, // low_power + }; Endpoint advertising_endpoint_1 = StartAdvertising(&client1_, advertising_connection_listener_, high_viz_advertising_options); StopAdvertising(&client1_); - ConnectionOptions low_viz_advertising_options{.strategy = strategy_, - .low_power = true}; + AdvertisingOptions low_viz_advertising_options{ + { + strategy_, + booleanMediumSelector, + }, + false, // auto_upgrade_bandwidth + false, // enforce_topology_constraints + true, // low_power + }; Endpoint advertising_endpoint_2 = StartAdvertising( &client1_, advertising_connection_listener_, low_viz_advertising_options); @@ -562,12 +589,18 @@ TEST_F(ClientProxyTest, // Tests endpoint_id rotates when discover. TEST_F(ClientProxyTest, EndpointIdRotateWhenStartDiscovery) { - ConnectionOptions advertising_options{.strategy = strategy_, - .allowed = - { - .bluetooth = true, - }, - .low_power = false}; + BooleanMediumSelector booleanMediumSelector; + booleanMediumSelector.bluetooth = true; + + AdvertisingOptions advertising_options{ + { + strategy_, + booleanMediumSelector, + }, + false, // auto_upgrade_bandwidth + false, // enforce_topology_constraints + false, // low_power + }; Endpoint advertising_endpoint_1 = StartAdvertising( &client1_, advertising_connection_listener_, advertising_options); @@ -584,10 +617,18 @@ TEST_F(ClientProxyTest, EndpointIdRotateWhenStartDiscovery) { // Tests the low visibility mode with bluetooth disabled advertisment. TEST_F(ClientProxyTest, EndpointIdRotateWhenLowVizAdvertisementWithBluetoothDisabled) { - ConnectionOptions advertising_options{.strategy = strategy_, - .allowed = { - .bluetooth = false, - }}; + BooleanMediumSelector booleanMediumSelector; + booleanMediumSelector.bluetooth = false; + + AdvertisingOptions advertising_options{ + { + strategy_, + booleanMediumSelector, + }, + false, // auto_upgrade_bandwidth + false, // enforce_topology_constraints + false, // low_power + }; Endpoint advertising_endpoint_1 = StartAdvertising( &client1_, advertising_connection_listener_, advertising_options); @@ -602,9 +643,18 @@ TEST_F(ClientProxyTest, // Tests the low visibility mode with low power advertisment. TEST_F(ClientProxyTest, EndpointIdRotateWhenLowVizAdvertisementWithLowPower) { - ConnectionOptions advertising_options{.strategy = strategy_, - .low_power = true}; + BooleanMediumSelector booleanMediumSelector; + booleanMediumSelector.bluetooth = false; + AdvertisingOptions advertising_options{ + { + strategy_, + booleanMediumSelector, + }, + false, // auto_upgrade_bandwidth + false, // enforce_topology_constraints + true, // low_power + }; Endpoint advertising_endpoint_1 = StartAdvertising( &client1_, advertising_connection_listener_, advertising_options); diff --git a/cpp/core/internal/endpoint_manager.cc b/cpp/core/internal/endpoint_manager.cc index d4df32e2..4faeec9f 100644 --- a/cpp/core/internal/endpoint_manager.cc +++ b/cpp/core/internal/endpoint_manager.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -361,13 +361,12 @@ void EndpointManager::RemoveEndpointState(const std::string& endpoint_id) { } } -void EndpointManager::RegisterEndpoint(ClientProxy* client, - const std::string& endpoint_id, - const ConnectionResponseInfo& info, - const ConnectionOptions& options, - std::unique_ptr channel, - const ConnectionListener& listener, - const std::string& connection_token) { +void EndpointManager::RegisterEndpoint( + ClientProxy* client, const std::string& endpoint_id, + const ConnectionResponseInfo& info, + const ConnectionOptions& connection_options, + std::unique_ptr channel, + const ConnectionListener& listener, const std::string& connection_token) { CountDownLatch latch(1); // NOTE (unique_ptr<> capture): @@ -379,8 +378,8 @@ void EndpointManager::RegisterEndpoint(ClientProxy* client, RunOnEndpointManagerThread("register-endpoint", [this, client, channel = channel.release(), &endpoint_id, &info, - &options, &listener, - &connection_token, + &connection_options, + &listener, &connection_token, &latch]() { if (endpoints_.contains(endpoint_id)) { NEARBY_LOGS(WARNING) << "Registering duplicate endpoint " << endpoint_id; @@ -390,9 +389,9 @@ void EndpointManager::RegisterEndpoint(ClientProxy* client, } absl::Duration keep_alive_interval = - absl::Milliseconds(options.keep_alive_interval_millis); + absl::Milliseconds(connection_options.keep_alive_interval_millis); absl::Duration keep_alive_timeout = - absl::Milliseconds(options.keep_alive_timeout_millis); + absl::Milliseconds(connection_options.keep_alive_timeout_millis); NEARBY_LOGS(INFO) << "Registering endpoint " << endpoint_id << " for client " << client->GetClientId() << " with keep-alive frame as interval=" @@ -459,8 +458,8 @@ void EndpointManager::RegisterEndpoint(ClientProxy* client, // It's now time to let the client know of this new connection so that // they can accept or reject it. - client->OnConnectionInitiated(endpoint_id, info, options, listener, - connection_token); + client->OnConnectionInitiated(endpoint_id, info, connection_options, + listener, connection_token); latch.CountDown(); }); latch.Await(); diff --git a/cpp/core/internal/endpoint_manager.h b/cpp/core/internal/endpoint_manager.h index ec1a1619..19d4c39e 100644 --- a/cpp/core/internal/endpoint_manager.h +++ b/cpp/core/internal/endpoint_manager.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -102,7 +102,7 @@ class EndpointManager { // Blocks until registration is complete. void RegisterEndpoint(ClientProxy* client, const std::string& endpoint_id, const ConnectionResponseInfo& info, - const ConnectionOptions& options, + const ConnectionOptions& connection_options, std::unique_ptr channel, const ConnectionListener& listener, const std::string& connection_token); diff --git a/cpp/core/internal/endpoint_manager_test.cc b/cpp/core/internal/endpoint_manager_test.cc index e5b5c152..d3b49313 100644 --- a/cpp/core/internal/endpoint_manager_test.cc +++ b/cpp/core/internal/endpoint_manager_test.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -24,10 +24,10 @@ #include "absl/synchronization/mutex.h" #include "absl/time/clock.h" #include "absl/time/time.h" +#include "core/connection_options.h" #include "core/internal/client_proxy.h" #include "core/internal/endpoint_channel_manager.h" #include "core/internal/offline_frames.h" -#include "core/options.h" #include "platform/base/byte_array.h" #include "platform/base/exception.h" #include "platform/public/count_down_latch.h" @@ -118,7 +118,7 @@ class EndpointManagerTest : public ::testing::Test { EXPECT_CALL(*channel, GetLastWriteTimestamp()) .WillRepeatedly(Return(start_time_)); EXPECT_CALL(mock_listener_.initiated_cb, Call).Times(1); - em_.RegisterEndpoint(&client_, endpoint_id_, info_, options_, + em_.RegisterEndpoint(&client_, endpoint_id_, info_, connection_options_, std::move(channel), listener_, connection_token); if (should_close) { EXPECT_TRUE(done.Await(absl::Milliseconds(1000)).result()); @@ -126,7 +126,7 @@ class EndpointManagerTest : public ::testing::Test { } ClientProxy client_; - ConnectionOptions options_{ + ConnectionOptions connection_options_{ .keep_alive_interval_millis = 5000, .keep_alive_timeout_millis = 30000, }; diff --git a/cpp/core/internal/mock_service_controller.h b/cpp/core/internal/mock_service_controller.h index 30173eeb..4a9345ec 100644 --- a/cpp/core/internal/mock_service_controller.h +++ b/cpp/core/internal/mock_service_controller.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -34,7 +34,7 @@ class MockServiceController : public ServiceController { MOCK_METHOD(void, Stop, (), (override)); MOCK_METHOD(Status, StartAdvertising, (ClientProxy * client, const std::string& service_id, - const ConnectionOptions& options, + const AdvertisingOptions& advertising_options, const ConnectionRequestInfo& info), (override)); @@ -42,7 +42,7 @@ class MockServiceController : public ServiceController { MOCK_METHOD(Status, StartDiscovery, (ClientProxy * client, const std::string& service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, const DiscoveryListener& listener), (override)); @@ -56,7 +56,7 @@ class MockServiceController : public ServiceController { MOCK_METHOD(Status, RequestConnection, (ClientProxy * client, const std::string& endpoint_id, const ConnectionRequestInfo& info, - const ConnectionOptions& options), + const ConnectionOptions& connection_options), (override)); MOCK_METHOD(Status, AcceptConnection, diff --git a/cpp/core/internal/mock_service_controller_router.h b/cpp/core/internal/mock_service_controller_router.h index 32d8d445..91966511 100644 --- a/cpp/core/internal/mock_service_controller_router.h +++ b/cpp/core/internal/mock_service_controller_router.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -26,7 +26,7 @@ class MockServiceControllerRouter : public ServiceControllerRouter { public: MOCK_METHOD(void, StartAdvertising, (ClientProxy * client, absl::string_view service_id, - const ConnectionOptions& options, + const AdvertisingOptions& advertising_options, const ConnectionRequestInfo& info, const ResultCallback& callback), (override)); @@ -37,7 +37,7 @@ class MockServiceControllerRouter : public ServiceControllerRouter { MOCK_METHOD(void, StartDiscovery, (ClientProxy * client, absl::string_view service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, const DiscoveryListener& listener, const ResultCallback& callback), (override)); @@ -55,7 +55,7 @@ class MockServiceControllerRouter : public ServiceControllerRouter { MOCK_METHOD(void, RequestConnection, (ClientProxy * client, absl::string_view endpoint_id, const ConnectionRequestInfo& info, - const ConnectionOptions& options, + const ConnectionOptions& connection_options, const ResultCallback& callback), (override)); diff --git a/cpp/core/internal/offline_frames.h b/cpp/core/internal/offline_frames.h index a7dc878d..da5b6abb 100644 --- a/cpp/core/internal/offline_frames.h +++ b/cpp/core/internal/offline_frames.h @@ -19,7 +19,7 @@ #include #include "connections/implementation/proto/offline_wire_formats.pb.h" -#include "core/options.h" +#include "core/connection_options.h" #include "platform/base/byte_array.h" #include "platform/base/exception.h" diff --git a/cpp/core/internal/offline_service_controller.cc b/cpp/core/internal/offline_service_controller.cc index a86eea9d..ba320134 100644 --- a/cpp/core/internal/offline_service_controller.cc +++ b/cpp/core/internal/offline_service_controller.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -34,11 +34,13 @@ void OfflineServiceController::Stop() { Status OfflineServiceController::StartAdvertising( ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, const ConnectionRequestInfo& info) { + const AdvertisingOptions& advertising_options, + const ConnectionRequestInfo& info) { if (stop_) return {Status::kOutOfOrderApiCall}; NEARBY_LOGS(INFO) << "Client " << client->GetClientId() << " requested advertising to start."; - return pcp_manager_.StartAdvertising(client, service_id, options, info); + return pcp_manager_.StartAdvertising(client, service_id, advertising_options, + info); } void OfflineServiceController::StopAdvertising(ClientProxy* client) { @@ -50,11 +52,13 @@ void OfflineServiceController::StopAdvertising(ClientProxy* client) { Status OfflineServiceController::StartDiscovery( ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, const DiscoveryListener& listener) { + const DiscoveryOptions& discovery_options, + const DiscoveryListener& listener) { if (stop_) return {Status::kOutOfOrderApiCall}; NEARBY_LOGS(INFO) << "Client " << client->GetClientId() << " requested discovery to start."; - return pcp_manager_.StartDiscovery(client, service_id, options, listener); + return pcp_manager_.StartDiscovery(client, service_id, discovery_options, + listener); } void OfflineServiceController::StopDiscovery(ClientProxy* client) { @@ -73,11 +77,13 @@ void OfflineServiceController::InjectEndpoint( Status OfflineServiceController::RequestConnection( ClientProxy* client, const std::string& endpoint_id, - const ConnectionRequestInfo& info, const ConnectionOptions& options) { + const ConnectionRequestInfo& info, + const ConnectionOptions& connection_options) { if (stop_) return {Status::kOutOfOrderApiCall}; NEARBY_LOGS(INFO) << "Client " << client->GetClientId() << " requested a connection to endpoint_id=" << endpoint_id; - return pcp_manager_.RequestConnection(client, endpoint_id, info, options); + return pcp_manager_.RequestConnection(client, endpoint_id, info, + connection_options); } Status OfflineServiceController::AcceptConnection( diff --git a/cpp/core/internal/offline_service_controller.h b/cpp/core/internal/offline_service_controller.h index 6e397de9..abe67575 100644 --- a/cpp/core/internal/offline_service_controller.h +++ b/cpp/core/internal/offline_service_controller.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -29,7 +29,6 @@ #include "core/internal/pcp_manager.h" #include "core/internal/service_controller.h" #include "core/listeners.h" -#include "core/options.h" #include "core/payload.h" #include "core/status.h" @@ -43,21 +42,23 @@ class OfflineServiceController : public ServiceController { ~OfflineServiceController() override; Status StartAdvertising(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const AdvertisingOptions& advertising_options, const ConnectionRequestInfo& info) override; + void StopAdvertising(ClientProxy* client) override; Status StartDiscovery(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, const DiscoveryListener& listener) override; void StopDiscovery(ClientProxy* client) override; void InjectEndpoint(ClientProxy* client, const std::string& service_id, const OutOfBandConnectionMetadata& metadata) override; - Status RequestConnection(ClientProxy* client, const std::string& endpoint_id, - const ConnectionRequestInfo& info, - const ConnectionOptions& options) override; + Status RequestConnection( + ClientProxy* client, const std::string& endpoint_id, + const ConnectionRequestInfo& info, + const ConnectionOptions& connection_options) override; Status AcceptConnection(ClientProxy* client, const std::string& endpoint_id, const PayloadListener& listener) override; Status RejectConnection(ClientProxy* client, diff --git a/cpp/core/internal/offline_simulation_user.cc b/cpp/core/internal/offline_simulation_user.cc index 6b6db548..da7d7a7b 100644 --- a/cpp/core/internal/offline_simulation_user.cc +++ b/cpp/core/internal/offline_simulation_user.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -120,7 +120,7 @@ Status OfflineSimulationUser::StartAdvertising(const std::string& service_id, .disconnected_cb = absl::bind_front(&OfflineSimulationUser::OnEndpointDisconnect, this), }; - return ctrl_.StartAdvertising(&client_, service_id_, options_, + return ctrl_.StartAdvertising(&client_, service_id_, advertising_options_, { .endpoint_info = info_, .listener = std::move(listener), @@ -142,7 +142,7 @@ Status OfflineSimulationUser::StartDiscovery(const std::string& service_id, .endpoint_lost_cb = absl::bind_front(&OfflineSimulationUser::OnEndpointLost, this), }; - return ctrl_.StartDiscovery(&client_, service_id, options_, + return ctrl_.StartDiscovery(&client_, service_id, discovery_options_, std::move(listener)); } diff --git a/cpp/core/internal/offline_simulation_user.h b/cpp/core/internal/offline_simulation_user.h index 01de6f88..dc848307 100644 --- a/cpp/core/internal/offline_simulation_user.h +++ b/cpp/core/internal/offline_simulation_user.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -21,7 +21,6 @@ #include "absl/strings/string_view.h" #include "core/internal/client_proxy.h" #include "core/internal/offline_service_controller.h" -#include "core/options.h" #include "platform/public/atomic_boolean.h" #include "platform/public/condition_variable.h" #include "platform/public/count_down_latch.h" @@ -50,7 +49,14 @@ class OfflineSimulationUser { explicit OfflineSimulationUser( absl::string_view device_name, BooleanMediumSelector allowed = BooleanMediumSelector()) - : connection_options_{ + : info_{ByteArray{std::string(device_name)}}, + advertising_options_{ + { + Strategy::kP2pCluster, + allowed, + }, + }, + connection_options_{ .keep_alive_interval_millis = FeatureFlags::GetInstance() .GetFlags() .keep_alive_interval_millis, @@ -58,11 +64,10 @@ class OfflineSimulationUser { .GetFlags() .keep_alive_timeout_millis, }, - info_{ByteArray{std::string(device_name)}}, - options_{ - .strategy = Strategy::kP2pCluster, - .allowed = allowed, - } {} + discovery_options_{{ + Strategy::kP2pCluster, + allowed, + }} {} virtual ~OfflineSimulationUser() = default; // Calls PcpManager::StartAdvertising(). @@ -173,8 +178,10 @@ class OfflineSimulationUser { std::string service_id_; DiscoveredInfo discovered_; + ByteArray info_; + AdvertisingOptions advertising_options_; ConnectionOptions connection_options_; - + DiscoveryOptions discovery_options_; Mutex progress_mutex_; ConditionVariable progress_sync_{&progress_mutex_}; PayloadProgressInfo progress_info_; @@ -189,8 +196,6 @@ class OfflineSimulationUser { CountDownLatch* disconnect_latch_ = nullptr; Future* future_ = nullptr; std::function predicate_; - ByteArray info_; - ConnectionOptions options_; ClientProxy client_; OfflineServiceController ctrl_; }; diff --git a/cpp/core/internal/p2p_cluster_pcp_handler.cc b/cpp/core/internal/p2p_cluster_pcp_handler.cc index 3a6e3db7..71e0b95a 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.cc +++ b/cpp/core/internal/p2p_cluster_pcp_handler.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -44,8 +44,8 @@ bool P2pClusterPcpHandler::ShouldAdvertiseBluetoothMacOverBle( } bool P2pClusterPcpHandler::ShouldAcceptBluetoothConnections( - const ConnectionOptions& options) { - return options.enable_bluetooth_listening; + const AdvertisingOptions& advertising_options) { + return advertising_options.enable_bluetooth_listening; } P2pClusterPcpHandler::P2pClusterPcpHandler( @@ -89,12 +89,12 @@ proto::connections::Medium P2pClusterPcpHandler::GetDefaultUpgradeMedium() { BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( ClientProxy* client, const std::string& service_id, const std::string& local_endpoint_id, const ByteArray& local_endpoint_info, - const ConnectionOptions& options) { + const AdvertisingOptions& advertising_options) { std::vector mediums_started_successfully; WebRtcState web_rtc_state{WebRtcState::kUnconnectable}; - if (options.allowed.wifi_lan) { + if (advertising_options.allowed.wifi_lan) { proto::connections::Medium wifi_lan_medium = StartWifiLanAdvertising(client, service_id, local_endpoint_id, local_endpoint_info, web_rtc_state); @@ -105,7 +105,7 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( } } - if (options.allowed.bluetooth) { + if (advertising_options.allowed.bluetooth) { const ByteArray bluetooth_hash = GenerateHash(service_id, BluetoothDeviceName::kServiceIdHashLength); proto::connections::Medium bluetooth_medium = StartBluetoothAdvertising( @@ -118,10 +118,10 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( } } - if (options.allowed.ble) { - proto::connections::Medium ble_medium = - StartBleAdvertising(client, service_id, local_endpoint_id, - local_endpoint_info, options, web_rtc_state); + if (advertising_options.allowed.ble) { + proto::connections::Medium ble_medium = StartBleAdvertising( + client, service_id, local_endpoint_id, local_endpoint_info, + advertising_options, web_rtc_state); if (ble_medium != proto::connections::UNKNOWN_MEDIUM) { NEARBY_LOGS(INFO) << "P2pClusterPcpHandler::StartAdvertisingImpl: Ble added"; @@ -657,17 +657,17 @@ void P2pClusterPcpHandler::WifiLanServiceLostHandler( BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl( ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options) { + const DiscoveryOptions& discovery_options) { // If this is an out-of-band connection, do not start actual discovery, since // this connection is intended to be completed via InjectEndpointImpl(). - if (options.is_out_of_band_connection) { + if (discovery_options.is_out_of_band_connection) { return {.status = {Status::kSuccess}, - .mediums = options.allowed.GetMediums(true)}; + .mediums = discovery_options.allowed.GetMediums(true)}; } std::vector mediums_started_successfully; - if (options.allowed.wifi_lan) { + if (discovery_options.allowed.wifi_lan) { proto::connections::Medium wifi_lan_medium = StartWifiLanDiscovery( { .service_discovered_cb = absl::bind_front( @@ -684,7 +684,7 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl( } } - if (options.allowed.bluetooth) { + if (discovery_options.allowed.bluetooth) { proto::connections::Medium bluetooth_medium = StartBluetoothDiscovery( { .device_discovered_cb = absl::bind_front( @@ -705,7 +705,7 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl( } } - if (options.allowed.ble) { + if (discovery_options.allowed.ble) { proto::connections::Medium ble_medium = StartBleScanning( { .peripheral_discovered_cb = absl::bind_front( @@ -714,7 +714,7 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl( .peripheral_lost_cb = absl::bind_front( &P2pClusterPcpHandler::BlePeripheralLostHandler, this, client), }, - client, service_id, options.fast_advertisement_service_uuid); + client, service_id, discovery_options.fast_advertisement_service_uuid); if (ble_medium != proto::connections::UNKNOWN_MEDIUM) { NEARBY_LOG(INFO, "P2pClusterPcpHandler::StartDiscoveryImpl: Ble added"); mediums_started_successfully.push_back(ble_medium); @@ -979,10 +979,12 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BluetoothConnectImpl( proto::connections::Medium P2pClusterPcpHandler::StartBleAdvertising( ClientProxy* client, const std::string& service_id, const std::string& local_endpoint_id, const ByteArray& local_endpoint_info, - const ConnectionOptions& options, WebRtcState web_rtc_state) { - bool fast_advertisement = !options.fast_advertisement_service_uuid.empty(); - PowerLevel power_level = - options.low_power ? PowerLevel::kLowPower : PowerLevel::kHighPower; + const AdvertisingOptions& advertising_options, WebRtcState web_rtc_state) { + bool fast_advertisement = + !advertising_options.fast_advertisement_service_uuid.empty(); + PowerLevel power_level = advertising_options.low_power + ? PowerLevel::kLowPower + : PowerLevel::kHighPower; // Start listening for connections before advertising in case a connection // request comes in very quickly. BLE allows connecting over BLE itself, as @@ -1039,7 +1041,7 @@ proto::connections::Medium P2pClusterPcpHandler::StartBleAdvertising( } if (ShouldAdvertiseBluetoothMacOverBle(power_level) || - ShouldAcceptBluetoothConnections(options)) { + ShouldAcceptBluetoothConnections(advertising_options)) { if (bluetooth_medium_.IsAvailable() && !bluetooth_medium_.IsAcceptingConnections(service_id)) { if (!bluetooth_radio_.Enable() || @@ -1132,8 +1134,9 @@ proto::connections::Medium P2pClusterPcpHandler::StartBleAdvertising( << " generated BleAdvertisement with service_id=" << service_id; - if (!ble_medium_.StartAdvertising(service_id, advertisement_bytes, - options.fast_advertisement_service_uuid)) { + if (!ble_medium_.StartAdvertising( + service_id, advertisement_bytes, + advertising_options.fast_advertisement_service_uuid)) { NEARBY_LOGS(WARNING) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) diff --git a/cpp/core/internal/p2p_cluster_pcp_handler.h b/cpp/core/internal/p2p_cluster_pcp_handler.h index 16af3a30..667fc4da 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.h +++ b/cpp/core/internal/p2p_cluster_pcp_handler.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -37,7 +37,6 @@ #endif #include "core/internal/pcp.h" #include "core/internal/wifi_lan_service_info.h" -#include "core/options.h" #include "core/strategy.h" #include "platform/base/byte_array.h" #include "platform/public/bluetooth_classic.h" @@ -73,7 +72,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { ClientProxy* client, const std::string& service_id, const std::string& local_endpoint_id, const ByteArray& local_endpoint_info, - const ConnectionOptions& options) override; + const AdvertisingOptions& advertising_options) override; // @PCPHandlerThread Status StopAdvertisingImpl(ClientProxy* client) override; @@ -81,7 +80,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { // @PCPHandlerThread BasePcpHandler::StartOperationResult StartDiscoveryImpl( ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options) override; + const DiscoveryOptions& discovery_options) override; // @PCPHandlerThread Status StopDiscoveryImpl(ClientProxy* client) override; @@ -125,7 +124,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { static ByteArray GenerateHash(const std::string& source, size_t size); static bool ShouldAdvertiseBluetoothMacOverBle(PowerLevel power_level); static bool ShouldAcceptBluetoothConnections( - const ConnectionOptions& options); + const AdvertisingOptions& advertising_options); // Bluetooth bool IsRecognizedBluetoothEndpoint(const std::string& name_string, @@ -165,8 +164,8 @@ class P2pClusterPcpHandler : public BasePcpHandler { proto::connections::Medium StartBleAdvertising( ClientProxy* client, const std::string& service_id, const std::string& local_endpoint_id, - const ByteArray& local_endpoint_info, const ConnectionOptions& options, - WebRtcState web_rtc_state); + const ByteArray& local_endpoint_info, + const AdvertisingOptions& advertising_options, WebRtcState web_rtc_state); proto::connections::Medium StartBleScanning( BleDiscoveredPeripheralCallback callback, ClientProxy* client, const std::string& service_id, diff --git a/cpp/core/internal/p2p_cluster_pcp_handler_test.cc b/cpp/core/internal/p2p_cluster_pcp_handler_test.cc index 526cee66..ead37497 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler_test.cc +++ b/cpp/core/internal/p2p_cluster_pcp_handler_test.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -21,7 +21,6 @@ #include "absl/time/time.h" #include "core/internal/bwu_manager.h" #include "core/internal/injected_bluetooth_device_store.h" -#include "core/options.h" #include "platform/base/medium_environment.h" #include "platform/public/count_down_latch.h" #include "platform/public/logging.h" @@ -50,13 +49,13 @@ class P2pClusterPcpHandlerTest void SetUp() override { NEARBY_LOG(INFO, "SetUp: begin"); env_.Stop(); - if (options_.allowed.bluetooth) { + if (advertising_options_.allowed.bluetooth) { NEARBY_LOG(INFO, "SetUp: BT enabled"); } - if (options_.allowed.wifi_lan) { + if (advertising_options_.allowed.wifi_lan) { NEARBY_LOG(INFO, "SetUp: WifiLan enabled"); } - if (options_.allowed.web_rtc) { + if (advertising_options_.allowed.web_rtc) { NEARBY_LOG(INFO, "SetUp: WebRTC enabled"); } NEARBY_LOG(INFO, "SetUp: end"); @@ -65,9 +64,23 @@ class P2pClusterPcpHandlerTest ClientProxy client_a_; ClientProxy client_b_; std::string service_id_{"service"}; - ConnectionOptions options_{ - .strategy = Strategy::kP2pCluster, - .allowed = GetParam(), + ConnectionOptions connection_options_{ + { + Strategy::kP2pCluster, + GetParam(), + }, + }; + AdvertisingOptions advertising_options_{ + { + Strategy::kP2pCluster, + GetParam(), + }, + }; + DiscoveryOptions discovery_options_{ + { + Strategy::kP2pCluster, + GetParam(), + }, }; MediumEnvironment& env_{MediumEnvironment::Instance()}; }; @@ -110,7 +123,7 @@ TEST_P(P2pClusterPcpHandlerTest, CanAdvertise) { InjectedBluetoothDeviceStore ibds_a; P2pClusterPcpHandler handler_a(&mediums_a, &em_a, &ecm_a, &bwu_a, ibds_a); EXPECT_EQ( - handler_a.StartAdvertising(&client_a_, service_id_, options_, + handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_, {.endpoint_info = ByteArray{endpoint_name}}), Status{Status::kSuccess}); env_.Stop(); @@ -133,11 +146,11 @@ TEST_P(P2pClusterPcpHandlerTest, CanDiscover) { P2pClusterPcpHandler handler_b(&mediums_b, &em_b, &ecm_b, &bwu_b, ibds_b); CountDownLatch latch(1); EXPECT_EQ( - handler_a.StartAdvertising(&client_a_, service_id_, options_, + handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_, {.endpoint_info = ByteArray{endpoint_name}}), Status{Status::kSuccess}); EXPECT_EQ(handler_b.StartDiscovery( - &client_b_, service_id_, options_, + &client_b_, service_id_, discovery_options_, { .endpoint_found_cb = [&latch](const std::string& endpoint_id, @@ -186,7 +199,7 @@ TEST_P(P2pClusterPcpHandlerTest, CanConnect) { } discovered; EXPECT_EQ( handler_a.StartAdvertising( - &client_a_, service_id_, options_, + &client_a_, service_id_, advertising_options_, { .endpoint_info = ByteArray{endpoint_name_a}, .listener = @@ -202,7 +215,7 @@ TEST_P(P2pClusterPcpHandlerTest, CanConnect) { }), Status{Status::kSuccess}); EXPECT_EQ(handler_b.StartDiscovery( - &client_b_, service_id_, options_, + &client_b_, service_id_, discovery_options_, { .endpoint_found_cb = [&discover_latch, &discovered]( @@ -243,7 +256,7 @@ TEST_P(P2pClusterPcpHandlerTest, CanConnect) { }, }, }, - options_); + connection_options_); EXPECT_TRUE(connect_latch.Await(absl::Milliseconds(1000)).result()); bwu_a.Shutdown(); bwu_b.Shutdown(); diff --git a/cpp/core/internal/pcp_handler.h b/cpp/core/internal/pcp_handler.h index b7ad50c0..ef09c142 100644 --- a/cpp/core/internal/pcp_handler.h +++ b/cpp/core/internal/pcp_handler.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -20,7 +20,7 @@ #include "core/internal/client_proxy.h" #include "core/internal/pcp.h" #include "core/listeners.h" -#include "core/options.h" +#include "core/out_of_band_connection_metadata.h" #include "core/params.h" #include "core/status.h" #include "core/strategy.h" @@ -69,7 +69,7 @@ class PcpHandler { // cpp/core/listeners.h virtual Status StartAdvertising(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const AdvertisingOptions& advertising_options, const ConnectionRequestInfo& info) = 0; // If Advertising is active, stop it, and change CLientProxy state, @@ -81,7 +81,7 @@ class PcpHandler { // DiscoveryListener will get called in case of any event. virtual Status StartDiscovery(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, const DiscoveryListener& listener) = 0; // If Discovery is active, stop it, and change CLientProxy state, @@ -96,10 +96,10 @@ class PcpHandler { // If remote endpoint has been successfully discovered, request it to form a // connection, update state on ClientProxy. - virtual Status RequestConnection(ClientProxy* client, - const std::string& endpoint_id, - const ConnectionRequestInfo& info, - const ConnectionOptions& options) = 0; + virtual Status RequestConnection( + ClientProxy* client, const std::string& endpoint_id, + const ConnectionRequestInfo& info, + const ConnectionOptions& connection_options) = 0; // Either party may call this to accept connection on their part. // Until both parties call it, connection will not reach a data phase. diff --git a/cpp/core/internal/pcp_manager.cc b/cpp/core/internal/pcp_manager.cc index 98de1acf..10fc7560 100644 --- a/cpp/core/internal/pcp_manager.cc +++ b/cpp/core/internal/pcp_manager.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -53,15 +53,16 @@ PcpManager::~PcpManager() { NEARBY_LOGS(INFO) << "PcpManager has shut down."; } -Status PcpManager::StartAdvertising(ClientProxy* client, - const string& service_id, - const ConnectionOptions& options, - const ConnectionRequestInfo& info) { - if (!SetCurrentPcpHandler(options.strategy)) { +Status PcpManager::StartAdvertising( + ClientProxy* client, const string& service_id, + const AdvertisingOptions& advertising_options, + const ConnectionRequestInfo& info) { + if (!SetCurrentPcpHandler(advertising_options.strategy)) { return {Status::kError}; } - return current_->StartAdvertising(client, service_id, options, info); + return current_->StartAdvertising(client, service_id, advertising_options, + info); } void PcpManager::StopAdvertising(ClientProxy* client) { @@ -71,13 +72,13 @@ void PcpManager::StopAdvertising(ClientProxy* client) { } Status PcpManager::StartDiscovery(ClientProxy* client, const string& service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, DiscoveryListener listener) { - if (!SetCurrentPcpHandler(options.strategy)) { + if (!SetCurrentPcpHandler(discovery_options.strategy)) { return {Status::kError}; } - return current_->StartDiscovery(client, service_id, options, + return current_->StartDiscovery(client, service_id, discovery_options, std::move(listener)); } @@ -95,15 +96,16 @@ void PcpManager::InjectEndpoint(ClientProxy* client, } } -Status PcpManager::RequestConnection(ClientProxy* client, - const string& endpoint_id, - const ConnectionRequestInfo& info, - const ConnectionOptions& options) { +Status PcpManager::RequestConnection( + ClientProxy* client, const string& endpoint_id, + const ConnectionRequestInfo& info, + const ConnectionOptions& connection_options) { if (!current_) { return {Status::kOutOfOrderApiCall}; } - return current_->RequestConnection(client, endpoint_id, info, options); + return current_->RequestConnection(client, endpoint_id, info, + connection_options); } Status PcpManager::AcceptConnection(ClientProxy* client, diff --git a/cpp/core/internal/pcp_manager.h b/cpp/core/internal/pcp_manager.h index 5c8dd8a9..5bd80f5e 100644 --- a/cpp/core/internal/pcp_manager.h +++ b/cpp/core/internal/pcp_manager.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -26,7 +26,6 @@ #include "core/internal/injected_bluetooth_device_store.h" #include "core/internal/mediums/mediums.h" #include "core/listeners.h" -#include "core/options.h" #include "core/status.h" #include "core/strategy.h" #include "platform/public/atomic_boolean.h" @@ -50,12 +49,12 @@ class PcpManager { ~PcpManager(); Status StartAdvertising(ClientProxy* client, const string& service_id, - const ConnectionOptions& options, + const AdvertisingOptions& advertising_options, const ConnectionRequestInfo& info); void StopAdvertising(ClientProxy* client); Status StartDiscovery(ClientProxy* client, const string& service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, DiscoveryListener listener); void StopDiscovery(ClientProxy* client); @@ -64,7 +63,7 @@ class PcpManager { Status RequestConnection(ClientProxy* client, const string& endpoint_id, const ConnectionRequestInfo& info, - const ConnectionOptions& options); + const ConnectionOptions& connection_options); Status AcceptConnection(ClientProxy* client, const string& endpoint_id, const PayloadListener& payload_listener); Status RejectConnection(ClientProxy* client, const string& endpoint_id); diff --git a/cpp/core/internal/pcp_manager_test.cc b/cpp/core/internal/pcp_manager_test.cc index fd703d16..b5520837 100644 --- a/cpp/core/internal/pcp_manager_test.cc +++ b/cpp/core/internal/pcp_manager_test.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -22,7 +22,6 @@ #include "absl/time/time.h" #include "core/internal/endpoint_channel_manager.h" #include "core/internal/simulation_user.h" -#include "core/options.h" #include "platform/base/medium_environment.h" #include "platform/public/count_down_latch.h" diff --git a/cpp/core/internal/service_controller.h b/cpp/core/internal/service_controller.h index 1fa3f646..1bc5a900 100644 --- a/cpp/core/internal/service_controller.h +++ b/cpp/core/internal/service_controller.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -19,9 +19,10 @@ #include #include +#include "core/advertising_options.h" #include "core/internal/client_proxy.h" #include "core/listeners.h" -#include "core/options.h" +#include "core/out_of_band_connection_metadata.h" #include "core/params.h" #include "core/payload.h" #include "core/status.h" @@ -58,13 +59,14 @@ class ServiceController { // Starts advertising an endpoint for a local app. virtual Status StartAdvertising(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const AdvertisingOptions& advertising_options, const ConnectionRequestInfo& info) = 0; + virtual void StopAdvertising(ClientProxy* client) = 0; virtual Status StartDiscovery(ClientProxy* client, const std::string& service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, const DiscoveryListener& listener) = 0; virtual void StopDiscovery(ClientProxy* client) = 0; @@ -72,10 +74,10 @@ class ServiceController { const std::string& service_id, const OutOfBandConnectionMetadata& metadata) = 0; - virtual Status RequestConnection(ClientProxy* client, - const std::string& endpoint_id, - const ConnectionRequestInfo& info, - const ConnectionOptions& options) = 0; + virtual Status RequestConnection( + ClientProxy* client, const std::string& endpoint_id, + const ConnectionRequestInfo& info, + const ConnectionOptions& connection_options) = 0; virtual Status AcceptConnection(ClientProxy* client, const std::string& endpoint_id, const PayloadListener& listener) = 0; diff --git a/cpp/core/internal/service_controller_router.cc b/cpp/core/internal/service_controller_router.cc index 7c5eaffd..aa2b2a36 100644 --- a/cpp/core/internal/service_controller_router.cc +++ b/cpp/core/internal/service_controller_router.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -23,7 +23,6 @@ #include "core/internal/client_proxy.h" #include "core/internal/offline_service_controller.h" #include "core/listeners.h" -#include "core/options.h" #include "core/params.h" #include "core/payload.h" #include "platform/public/logging.h" @@ -72,19 +71,19 @@ ServiceControllerRouter::~ServiceControllerRouter() { void ServiceControllerRouter::StartAdvertising( ClientProxy* client, absl::string_view service_id, - const ConnectionOptions& options, const ConnectionRequestInfo& info, - const ResultCallback& callback) { + const AdvertisingOptions& advertising_options, + const ConnectionRequestInfo& info, const ResultCallback& callback) { RouteToServiceController( "scr-start-advertising", - [this, client, service_id = std::string(service_id), options, info, - callback]() { + [this, client, service_id = std::string(service_id), advertising_options, + info, callback]() { if (client->IsAdvertising()) { callback.result_cb({Status::kAlreadyAdvertising}); return; } callback.result_cb(GetServiceController()->StartAdvertising( - client, service_id, options, info)); + client, service_id, advertising_options, info)); }); } @@ -98,22 +97,21 @@ void ServiceControllerRouter::StopAdvertising(ClientProxy* client, }); } -void ServiceControllerRouter::StartDiscovery(ClientProxy* client, - absl::string_view service_id, - const ConnectionOptions& options, - const DiscoveryListener& listener, - const ResultCallback& callback) { +void ServiceControllerRouter::StartDiscovery( + ClientProxy* client, absl::string_view service_id, + const DiscoveryOptions& discovery_options, + const DiscoveryListener& listener, const ResultCallback& callback) { RouteToServiceController( "scr-start-discovery", - [this, client, service_id = std::string(service_id), options, listener, - callback]() { + [this, client, service_id = std::string(service_id), discovery_options, + listener, callback]() { if (client->IsDiscovering()) { callback.result_cb({Status::kAlreadyDiscovering}); return; } callback.result_cb(GetServiceController()->StartDiscovery( - client, service_id, options, listener)); + client, service_id, discovery_options, listener)); }); } @@ -166,7 +164,8 @@ void ServiceControllerRouter::InjectEndpoint( void ServiceControllerRouter::RequestConnection( ClientProxy* client, absl::string_view endpoint_id, - const ConnectionRequestInfo& info, const ConnectionOptions& options, + const ConnectionRequestInfo& info, + const ConnectionOptions& connection_options, const ResultCallback& callback) { // Cancellations can be fired from clients anytime, need to add the // CancellationListener as soon as possible. @@ -174,8 +173,8 @@ void ServiceControllerRouter::RequestConnection( RouteToServiceController( "scr-request-connection", - [this, client, endpoint_id = std::string(endpoint_id), info, options, - callback]() { + [this, client, endpoint_id = std::string(endpoint_id), info, + connection_options, callback]() { if (client->HasPendingConnectionToEndpoint(endpoint_id) || client->IsConnectedToEndpoint(endpoint_id)) { callback.result_cb({Status::kAlreadyConnectedToEndpoint}); @@ -183,7 +182,7 @@ void ServiceControllerRouter::RequestConnection( } Status status = GetServiceController()->RequestConnection( - client, endpoint_id, info, options); + client, endpoint_id, info, connection_options); if (!status.Ok()) { client->CancelEndpoint(endpoint_id); } diff --git a/cpp/core/internal/service_controller_router.h b/cpp/core/internal/service_controller_router.h index 54520ac2..09d577a7 100644 --- a/cpp/core/internal/service_controller_router.h +++ b/cpp/core/internal/service_controller_router.h @@ -24,7 +24,6 @@ #include "absl/types/span.h" #include "core/internal/client_proxy.h" #include "core/internal/service_controller.h" -#include "core/options.h" #include "core/params.h" #include "platform/base/runnable.h" #include "platform/public/single_thread_executor.h" @@ -63,14 +62,14 @@ class ServiceControllerRouter { virtual void StartAdvertising(ClientProxy* client, absl::string_view service_id, - const ConnectionOptions& options, + const AdvertisingOptions& advertising_options, const ConnectionRequestInfo& info, const ResultCallback& callback); virtual void StopAdvertising(ClientProxy* client, const ResultCallback& callback); virtual void StartDiscovery(ClientProxy* client, absl::string_view service_id, - const ConnectionOptions& options, + const DiscoveryOptions& discovery_options, const DiscoveryListener& listener, const ResultCallback& callback); virtual void StopDiscovery(ClientProxy* client, @@ -83,7 +82,7 @@ class ServiceControllerRouter { virtual void RequestConnection(ClientProxy* client, absl::string_view endpoint_id, const ConnectionRequestInfo& info, - const ConnectionOptions& options, + const ConnectionOptions& connection_options, const ResultCallback& callback); virtual void AcceptConnection(ClientProxy* client, absl::string_view endpoint_id, diff --git a/cpp/core/internal/service_controller_router_test.cc b/cpp/core/internal/service_controller_router_test.cc index 74ed7c2c..72b74492 100644 --- a/cpp/core/internal/service_controller_router_test.cc +++ b/cpp/core/internal/service_controller_router_test.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -29,7 +29,6 @@ #include "core/internal/mock_service_controller.h" #include "core/internal/service_controller.h" #include "core/listeners.h" -#include "core/options.h" #include "core/params.h" #include "platform/base/byte_array.h" #include "platform/public/condition_variable.h" @@ -40,6 +39,9 @@ namespace location { namespace nearby { namespace connections { +// Feature On/Off switch for mediums. +using BooleanMediumSelector = MediumSelector; + namespace { using ::testing::Return; constexpr std::array kFakeMacAddress = {'a', 'b', 'c', 'd', 'e', 'f'}; @@ -58,19 +60,20 @@ class ServiceControllerRouterTest : public testing::Test { } void StartAdvertising(ClientProxy* client, std::string service_id, - ConnectionOptions options, ConnectionRequestInfo info, - ResultCallback callback) { + AdvertisingOptions advertising_options, + ConnectionRequestInfo info, ResultCallback callback) { EXPECT_CALL(*mock_, StartAdvertising) .WillOnce(Return(Status{Status::kSuccess})); { MutexLock lock(&mutex_); complete_ = false; - router_.StartAdvertising(client, service_id, options, info, callback); + router_.StartAdvertising(client, service_id, advertising_options, info, + callback); while (!complete_) cond_.Wait(); EXPECT_EQ(result_, Status{Status::kSuccess}); } - client->StartedAdvertising(kServiceId, options.strategy, info.listener, - absl::MakeSpan(mediums_)); + client->StartedAdvertising(kServiceId, advertising_options.strategy, + info.listener, absl::MakeSpan(mediums_)); EXPECT_TRUE(client->IsAdvertising()); } @@ -87,7 +90,7 @@ class ServiceControllerRouterTest : public testing::Test { } void StartDiscovery(ClientProxy* client, std::string service_id, - ConnectionOptions options, + DiscoveryOptions discovery_options, const DiscoveryListener& listener, const ResultCallback& callback) { EXPECT_CALL(*mock_, StartDiscovery) @@ -95,11 +98,12 @@ class ServiceControllerRouterTest : public testing::Test { { MutexLock lock(&mutex_); complete_ = false; - router_.StartDiscovery(client, kServiceId, options, listener, callback); + router_.StartDiscovery(client, kServiceId, discovery_options, listener, + callback); while (!complete_) cond_.Wait(); EXPECT_EQ(result_, Status{Status::kSuccess}); } - client->StartedDiscovery(service_id, options.strategy, listener, + client->StartedDiscovery(service_id, discovery_options.strategy, listener, absl::MakeSpan(mediums_)); EXPECT_TRUE(client->IsDiscovering()); } @@ -133,12 +137,12 @@ class ServiceControllerRouterTest : public testing::Test { ResultCallback callback) { EXPECT_CALL(*mock_, RequestConnection) .WillOnce(Return(Status{Status::kSuccess})); - ConnectionOptions options; + ConnectionOptions connection_options; { MutexLock lock(&mutex_); complete_ = false; - router_.RequestConnection(client, endpoint_id, request_info, options, - callback); + router_.RequestConnection(client, endpoint_id, request_info, + connection_options, callback); while (!complete_) cond_.Wait(); EXPECT_EQ(result_, Status{Status::kSuccess}); } @@ -149,8 +153,9 @@ class ServiceControllerRouterTest : public testing::Test { .is_incoming_connection = true, }; std::string connection_token{"conntokn"}; - client->OnConnectionInitiated(endpoint_id, response_info, options, - request_info.listener, connection_token); + client->OnConnectionInitiated(endpoint_id, response_info, + connection_options, request_info.listener, + connection_token); EXPECT_TRUE(client->HasPendingConnectionToEndpoint(endpoint_id)); } @@ -269,10 +274,33 @@ class ServiceControllerRouterTest : public testing::Test { const std::string kRemoteEndpointId = "remote endpoint id"; const std::int64_t kPayloadId = UINT64_C(0x123456789ABCDEF0); const ConnectionOptions kConnectionOptions{ - .strategy = Strategy::kP2pPointToPoint, - .auto_upgrade_bandwidth = true, - .enforce_topology_constraints = true, + { + Strategy::kP2pPointToPoint, + BooleanMediumSelector(), + }, + true, + true, }; + const AdvertisingOptions kAdvertisingOptions{ + { + Strategy::kP2pPointToPoint, + BooleanMediumSelector(), + }, + true, // auto_upgrade_bandwidth + true, // enforce_topology_constraints + false, // low_power + false, // enable_bluetooth_listening + false, // enable_webrtc_listening + }; + const DiscoveryOptions kDiscoveryOptions{ + { + Strategy::kP2pPointToPoint, + BooleanMediumSelector(), + }, + true, + true, + }; + const OutOfBandConnectionMetadata kOutOfBandConnectionMetadata{ .medium = Medium::BLUETOOTH, .endpoint_id = kFakeInejctedEndpointId, @@ -302,29 +330,29 @@ class ServiceControllerRouterTest : public testing::Test { namespace { TEST_F(ServiceControllerRouterTest, StartAdvertisingCalled) { - StartAdvertising(&client_, kServiceId, kConnectionOptions, + StartAdvertising(&client_, kServiceId, kAdvertisingOptions, kConnectionRequestInfo, kCallback); } TEST_F(ServiceControllerRouterTest, StopAdvertisingCalled) { - StartAdvertising(&client_, kServiceId, kConnectionOptions, + StartAdvertising(&client_, kServiceId, kAdvertisingOptions, kConnectionRequestInfo, kCallback); StopAdvertising(&client_, kCallback); } TEST_F(ServiceControllerRouterTest, StartDiscoveryCalled) { - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); } TEST_F(ServiceControllerRouterTest, StopDiscoveryCalled) { - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); StopDiscovery(&client_, kCallback); } TEST_F(ServiceControllerRouterTest, InjectEndpointCalled) { - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); InjectEndpoint(&client_, kServiceId, kOutOfBandConnectionMetadata, kCallback); StopDiscovery(&client_, kCallback); @@ -332,7 +360,7 @@ TEST_F(ServiceControllerRouterTest, InjectEndpointCalled) { TEST_F(ServiceControllerRouterTest, RequestConnectionCalled) { // Either Advertising, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo, kCallback); @@ -340,7 +368,7 @@ TEST_F(ServiceControllerRouterTest, RequestConnectionCalled) { TEST_F(ServiceControllerRouterTest, AcceptConnectionCalled) { // Either Adviertisng, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); // Establish connection. RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo, @@ -351,7 +379,7 @@ TEST_F(ServiceControllerRouterTest, AcceptConnectionCalled) { TEST_F(ServiceControllerRouterTest, RejectConnectionCalled) { // Either Adviertisng, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); // Establish connection. RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo, @@ -362,7 +390,7 @@ TEST_F(ServiceControllerRouterTest, RejectConnectionCalled) { TEST_F(ServiceControllerRouterTest, InitiateBandwidthUpgradeCalled) { // Either Adviertisng, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); // Establish connection. RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo, @@ -375,7 +403,7 @@ TEST_F(ServiceControllerRouterTest, InitiateBandwidthUpgradeCalled) { TEST_F(ServiceControllerRouterTest, SendPayloadCalled) { // Either Adviertisng, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); // Establish connection. RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo, @@ -389,7 +417,7 @@ TEST_F(ServiceControllerRouterTest, SendPayloadCalled) { TEST_F(ServiceControllerRouterTest, CancelPayloadCalled) { // Either Adviertisng, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); // Establish connection. RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo, @@ -404,7 +432,7 @@ TEST_F(ServiceControllerRouterTest, CancelPayloadCalled) { TEST_F(ServiceControllerRouterTest, DisconnectFromEndpointCalled) { // Either Adviertisng, or Discovery should be ongoing. - StartDiscovery(&client_, kServiceId, kConnectionOptions, discovery_listener_, + StartDiscovery(&client_, kServiceId, kDiscoveryOptions, discovery_listener_, kCallback); // Establish connection. RequestConnection(&client_, kRemoteEndpointId, kConnectionRequestInfo, diff --git a/cpp/core/internal/simulation_user.cc b/cpp/core/internal/simulation_user.cc index afeab134..f78c4d1b 100644 --- a/cpp/core/internal/simulation_user.cc +++ b/cpp/core/internal/simulation_user.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -109,7 +109,7 @@ void SimulationUser::StartAdvertising(const std::string& service_id, .rejected_cb = absl::bind_front(&SimulationUser::OnConnectionRejected, this), }; - EXPECT_TRUE(mgr_.StartAdvertising(&client_, service_id_, options_, + EXPECT_TRUE(mgr_.StartAdvertising(&client_, service_id_, advertising_options_, { .endpoint_info = info_, .listener = std::move(listener), @@ -121,7 +121,7 @@ void SimulationUser::StartDiscovery(const std::string& service_id, CountDownLatch* latch) { found_latch_ = latch; EXPECT_TRUE( - mgr_.StartDiscovery(&client_, service_id, options_, + mgr_.StartDiscovery(&client_, service_id, discovery_options_, { .endpoint_found_cb = absl::bind_front( &SimulationUser::OnEndpointFound, this), diff --git a/cpp/core/internal/simulation_user.h b/cpp/core/internal/simulation_user.h index 6e62a6be..159ac8f3 100644 --- a/cpp/core/internal/simulation_user.h +++ b/cpp/core/internal/simulation_user.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -25,7 +25,6 @@ #include "core/internal/injected_bluetooth_device_store.h" #include "core/internal/payload_manager.h" #include "core/internal/pcp_manager.h" -#include "core/options.h" #include "platform/base/medium_environment.h" #include "platform/public/condition_variable.h" #include "platform/public/count_down_latch.h" @@ -54,18 +53,26 @@ class SimulationUser { explicit SimulationUser( const std::string& device_name, BooleanMediumSelector allowed = BooleanMediumSelector()) - : connection_options_{ - .keep_alive_interval_millis = FeatureFlags::GetInstance() - .GetFlags() - .keep_alive_interval_millis, - .keep_alive_timeout_millis = FeatureFlags::GetInstance() - .GetFlags() - .keep_alive_timeout_millis, + : info_{ByteArray{device_name}}, + advertising_options_{ + { + Strategy::kP2pCluster, + allowed, + }, }, - info_{ByteArray{device_name}}, - options_{ - .strategy = Strategy::kP2pCluster, - .allowed = allowed, + connection_options_{ + .keep_alive_interval_millis = FeatureFlags::GetInstance() + .GetFlags() + .keep_alive_interval_millis, + .keep_alive_timeout_millis = FeatureFlags::GetInstance() + .GetFlags() + .keep_alive_timeout_millis, + }, + discovery_options_{ + { + Strategy::kP2pCluster, + allowed, + }, } {} virtual ~SimulationUser() { Stop(); } void Stop() { @@ -140,7 +147,6 @@ class SimulationUser { std::string service_id_; DiscoveredInfo discovered_; - ConnectionOptions connection_options_; Mutex progress_mutex_; ConditionVariable progress_sync_{&progress_mutex_}; PayloadProgressInfo progress_info_; @@ -155,7 +161,9 @@ class SimulationUser { std::function predicate_; ByteArray info_; Mediums mediums_; - ConnectionOptions options_; + AdvertisingOptions advertising_options_; + ConnectionOptions connection_options_; + DiscoveryOptions discovery_options_; ClientProxy client_; EndpointChannelManager ecm_; EndpointManager em_{&ecm_}; diff --git a/cpp/core/listeners.h b/cpp/core/listeners.h index f9c0208d..97a20eba 100644 --- a/cpp/core/listeners.h +++ b/cpp/core/listeners.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -27,7 +27,7 @@ // default-initialized. // - callbacks may be initialized with lambdas; lambda definitions are concize. -#include "core/options.h" +#include "core/connection_options.h" #include "core/payload.h" #include "core/status.h" #include "platform/base/byte_array.h" diff --git a/cpp/core/options_base.h b/cpp/core/options_base.h new file mode 100644 index 00000000..26701eaf --- /dev/null +++ b/cpp/core/options_base.h @@ -0,0 +1,39 @@ +// 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 CORE_OPTIONS_BASE_H_ +#define CORE_OPTIONS_BASE_H_ +#include + +#include "core/medium_selector.h" +#include "core/strategy.h" + +namespace location { +namespace nearby { +namespace connections { + +// Feature On/Off switch for mediums. +using BooleanMediumSelector = MediumSelector; + +// Connection Options: used for both Advertising and Discovery. +// All fields are mutable, to make the type copy-assignable. +struct OptionsBase { + Strategy strategy; + BooleanMediumSelector allowed{BooleanMediumSelector().SetAll(true)}; +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // CORE_OPTIONS_BASE_H_ diff --git a/cpp/core/options.h b/cpp/core/out_of_band_connection_metadata.h similarity index 52% rename from cpp/core/options.h rename to cpp/core/out_of_band_connection_metadata.h index ee5474c8..79711739 100644 --- a/cpp/core/options.h +++ b/cpp/core/out_of_band_connection_metadata.h @@ -1,105 +1,60 @@ -// 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_OPTIONS_H_ -#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" - -namespace location { -namespace nearby { -namespace connections { - -// Feature On/Off switch for mediums. -using BooleanMediumSelector = MediumSelector; - -// Represents the various power levels that can be used, on mediums that support -// it. -enum class PowerLevel { - kHighPower = 0, - kLowPower = 1, -}; - -// Connection Options: used for both Advertising and Discovery. -// All fields are mutable, to make the type copy-assignable. -struct ConnectionOptions { - Strategy strategy; - BooleanMediumSelector allowed{BooleanMediumSelector().SetAll(true)}; - bool auto_upgrade_bandwidth; - bool enforce_topology_constraints; - 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; - - // 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; - - 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 GetMediums(location::nearby::proto::connections::Medium* mediums, - uint32_t* mediumsSize); -}; - -// Metadata injected to facilitate out-of-band connections. The medium field is -// required, and the other fields are only specified for a specific medium. -// Currently, Bluetooth is the only supported medium for out-of-band -// connections. -struct OutOfBandConnectionMetadata { - // Medium to use for the out-of-band connection. - Medium medium; - - // Endpoint ID to use for the injected connection; will be included in the - // endpoint_found_cb callback. Must be exactly 4 bytes and should be randomly- - // generated such that no two IDs are identical. - std::string endpoint_id; - - // Endpoint info to use for the injected connection; will be included in the - // endpoint_found_cb callback. Should uniquely identify the InjectEndpoint() - // call so that the client which made the call can verify the endpoint - // that was found is the one that was injected. - // - // Cannot be empty, and must be <131 bytes. - ByteArray endpoint_info; - - // Used for Bluetooth connections. - ByteArray remote_bluetooth_mac_address; -}; - -} // namespace connections -} // namespace nearby -} // namespace location - -#endif // CORE_OPTIONS_H_ +// 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 CORE_OUT_OF_BAND_CONNECTION_METADATA_H_ +#define CORE_OUT_OF_BAND_CONNECTION_METADATA_H_ + +#include + +#include "core/medium_selector.h" +#include "core/strategy.h" +#include "platform/base/byte_array.h" +#include "proto/connections_enums.pb.h" + +namespace location { +namespace nearby { +namespace connections { + +// Feature On/Off switch for mediums. +using BooleanMediumSelector = MediumSelector; + +// Metadata injected to facilitate out-of-band connections. The medium field is +// required, and the other fields are only specified for a specific medium. +// Currently, Bluetooth is the only supported medium for out-of-band +// connections. +struct OutOfBandConnectionMetadata { + // Medium to use for the out-of-band connection. + Medium medium; + + // Endpoint ID to use for the injected connection; will be included in the + // endpoint_found_cb callback. Must be exactly 4 bytes and should be randomly- + // generated such that no two IDs are identical. + std::string endpoint_id; + + // Endpoint info to use for the injected connection; will be included in the + // endpoint_found_cb callback. Should uniquely identify the InjectEndpoint() + // call so that the client which made the call can verify the endpoint + // that was found is the one that was injected. + // + // Cannot be empty, and must be <131 bytes. + ByteArray endpoint_info; + + // Used for Bluetooth connections. + ByteArray remote_bluetooth_mac_address; +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // CORE_OUT_OF_BAND_CONNECTION_METADATA_H_ diff --git a/cpp/core/power_level.h b/cpp/core/power_level.h new file mode 100644 index 00000000..1e29cdb1 --- /dev/null +++ b/cpp/core/power_level.h @@ -0,0 +1,31 @@ +// 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 CORE_POWER_LEVEL_H_ +#define CORE_POWER_LEVEL_H_ + +namespace location { +namespace nearby { +namespace connections { // Represents the various power levels that can be + // used, on mediums that support it. +enum class PowerLevel { + kHighPower = 0, + kLowPower = 1, +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // CORE_POWER_LEVEL_H_ diff --git a/cpp/platform/impl/ios/Source/Internal/GNCAdvertiser.mm b/cpp/platform/impl/ios/Source/Internal/GNCAdvertiser.mm index ec7c6ba6..461dcd0b 100644 --- a/cpp/platform/impl/ios/Source/Internal/GNCAdvertiser.mm +++ b/cpp/platform/impl/ios/Source/Internal/GNCAdvertiser.mm @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -17,9 +17,9 @@ #include #include "absl/functional/bind_front.h" +#include "core/advertising_options.h" #include "core/core.h" #include "core/listeners.h" -#include "core/options.h" #include "core/params.h" #include "core/status.h" #include "platform/base/byte_array.h" @@ -37,7 +37,7 @@ using ::location::nearby::ByteArrayFromNSData; using ::location::nearby::CppStringFromObjCString; using ::location::nearby::ObjCStringFromCppString; using ::location::nearby::connections::ConnectionListener; -using ::location::nearby::connections::ConnectionOptions; +using ::location::nearby::connections::AdvertisingOptions; using ::location::nearby::connections::ConnectionRequestInfo; using ::location::nearby::connections::ConnectionResponseInfo; using ::location::nearby::connections::GNCStrategyToStrategy; @@ -289,17 +289,21 @@ using ::location::nearby::connections::GNCAdvertiserConnectionListener; advertiser->advertiserListener.get()), }; - advertiser.core->_core->StartAdvertising(CppStringFromObjCString(serviceId), - ConnectionOptions{ - .strategy = GNCStrategyToStrategy(strategy), - .auto_upgrade_bandwidth = true, - .enforce_topology_constraints = true, - }, - ConnectionRequestInfo{ - .endpoint_info = ByteArrayFromNSData(endpointInfo), - .listener = std::move(listener), - }, - ResultListener{}); + advertiser.core->_core->StartAdvertising( + CppStringFromObjCString(serviceId), + AdvertisingOptions{ + { + GNCStrategyToStrategy(strategy), // .strategy + location::nearby::connections::BooleanMediumSelector(), // .allowed + }, + true, // .auto_upgrade_bandwidth + true, // .enforce_topology_constraints + }, + ConnectionRequestInfo{ + .endpoint_info = ByteArrayFromNSData(endpointInfo), + .listener = std::move(listener), + }, + ResultListener{}); return advertiser; } diff --git a/cpp/platform/impl/ios/Source/Internal/GNCDiscoverer.mm b/cpp/platform/impl/ios/Source/Internal/GNCDiscoverer.mm index 21ef1c79..fffb6bdd 100644 --- a/cpp/platform/impl/ios/Source/Internal/GNCDiscoverer.mm +++ b/cpp/platform/impl/ios/Source/Internal/GNCDiscoverer.mm @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -18,9 +18,10 @@ #include #include "absl/functional/bind_front.h" +#include "core/connection_options.h" #include "core/core.h" +#include "core/discovery_options.h" #include "core/listeners.h" -#include "core/options.h" #include "core/status.h" #include "platform/base/byte_array.h" #import "platform/impl/ios/Source/GNCConnection.h" @@ -35,6 +36,7 @@ NS_ASSUME_NONNULL_BEGIN using ::location::nearby::ByteArray; using ::location::nearby::CppStringFromObjCString; +using ::location::nearby::connections::DiscoveryOptions; using ::location::nearby::connections::ConnectionOptions; using ::location::nearby::connections::DiscoveryListener; using ::location::nearby::connections::DistanceInfo; @@ -379,8 +381,10 @@ using ::location::nearby::connections::GNCDiscoveryListener; }; discoverer.core->_core->StartDiscovery(CppStringFromObjCString(serviceId), - ConnectionOptions{ - .strategy = GNCStrategyToStrategy(strategy), + DiscoveryOptions{ + { + GNCStrategyToStrategy(strategy), + }, }, std::move(listener), ResultListener{}); diff --git a/cpp/platform/impl/ios/Source/Internal/GNCUtils.h b/cpp/platform/impl/ios/Source/Internal/GNCUtils.h index 627088a3..04d99124 100644 --- a/cpp/platform/impl/ios/Source/Internal/GNCUtils.h +++ b/cpp/platform/impl/ios/Source/Internal/GNCUtils.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -17,7 +17,6 @@ #include #include "core/listeners.h" -#include "core/options.h" #import "platform/impl/ios/Source/GNCAdvertiser.h" #import "platform/impl/ios/Source/GNCConnection.h" diff --git a/cpp/platform/impl/windows/platform.cc b/cpp/platform/impl/windows/platform.cc index 5d117cf6..2af4af95 100644 --- a/cpp/platform/impl/windows/platform.cc +++ b/cpp/platform/impl/windows/platform.cc @@ -60,11 +60,14 @@ std::string GetPayloadPath(PayloadId payload_id) { // process is responsible for freeing this resource once it // is no longer needed by calling CoTaskMemFree, whether // SHGetKnownFolderPath succeeds or not. - - char* fullpathUTF8 = new char((wcslen(basePath) + 1) * sizeof(char)); - wcstombs(fullpathUTF8, basePath, (wcslen(basePath) + 1) * sizeof(char)); + size_t bufferSize; + wcstombs_s(&bufferSize, NULL, 0, basePath, 0); + char* fullpathUTF8 = new char[bufferSize + 1]; + memset(fullpathUTF8, 0, bufferSize); + wcstombs_s(&bufferSize, fullpathUTF8, bufferSize, basePath, bufferSize - 1); std::string fullPath = std::string(fullpathUTF8); - auto retval = absl::StrCat(fullPath += "/", payload_id); + auto retval = absl::StrCat(fullPath += "\\", payload_id); + delete[] fullpathUTF8; return retval; } } // namespace diff --git a/windows/BUILD b/windows/BUILD index f69c9c11..d3d0bda3 100644 --- a/windows/BUILD +++ b/windows/BUILD @@ -19,16 +19,10 @@ licenses(["notice"]) cc_windows_dll( name = "core_adapter", srcs = [ - "advertising_options.cc", - "connection_options.cc", "core_adapter.cc", - "discovery_options.cc", ], hdrs = [ - "advertising_options.h", - "connection_options.h", "core_adapter.h", - "discovery_options.h", ], copts = ["-DCORE_ADAPTER_BUILD_DLL -DCOMPILING_CORE"], defines = ["CORE_ADAPTER_DLL"], @@ -47,18 +41,12 @@ cc_windows_dll( cc_windows_dll( name = "core_adapter_dart", srcs = [ - "advertising_options.cc", - "connection_options.cc", "core_adapter.cc", "dart/core_adapter_dart.cc", - "discovery_options.cc", ], hdrs = [ - "advertising_options.h", - "connection_options.h", "core_adapter.h", "dart/core_adapter_dart.h", - "discovery_options.h", ], copts = ["-DCORE_ADAPTER_DART_BUILD_DLL -DCORE_ADAPTER_DLL -DCOMPILING_CORE"], defines = ["CORE_ADAPTER_DART_DLL"], diff --git a/windows/advertising_options.cc b/windows/advertising_options.cc deleted file mode 100644 index d65a6bd5..00000000 --- a/windows/advertising_options.cc +++ /dev/null @@ -1,72 +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 "third_party/nearby/windows/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 { -namespace windows { - -AdvertisingOptions::AdvertisingOptions() - : 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) {} - -AdvertisingOptions::operator connections::ConnectionOptions() const { - return *impl_.get(); -} - -// Verify if ConnectionOptions is in a not-initialized (Empty) state. -bool AdvertisingOptions::Empty() const { return impl_->Empty(); } - -// Bring ConnectionOptions to a not-initialized (Empty) state. -void AdvertisingOptions::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. -connections::ConnectionOptions AdvertisingOptions::CompatibleOptions() const { - return impl_->CompatibleOptions(); -} - -// 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 AdvertisingOptions::GetMediums( - location::nearby::proto::connections::Medium *mediums, - uint32_t *mediumsSize) {} - -} // namespace windows -} // namespace connections -} // namespace nearby -} // namespace location diff --git a/windows/advertising_options.h b/windows/advertising_options.h deleted file mode 100644 index 80b54e13..00000000 --- a/windows/advertising_options.h +++ /dev/null @@ -1,92 +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. - -#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 connections -} // namespace proto -namespace connections { -struct ConnectionOptions; -class Strategy; -template -struct MediumSelector; - -using BooleanMediumSelector = MediumSelector; - -namespace windows { - -struct DLL_API AdvertisingOptions { - AdvertisingOptions(); - operator connections::ConnectionOptions() const; - - private: - std::unique_ptr - impl_; - - public: - connections::Strategy &strategy; - connections::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. - connections::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 windows -} // namespace connections -} // namespace nearby -} // namespace location - -#endif // WINDOWS_START_ADVERTISING_OPTIONS_H_ diff --git a/windows/connection_options.cc b/windows/connection_options.cc deleted file mode 100644 index 7d299454..00000000 --- a/windows/connection_options.cc +++ /dev/null @@ -1,74 +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 "third_party/nearby/windows/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 { -namespace windows { - -ConnectionOptions::ConnectionOptions() - : 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) {} - -ConnectionOptions::operator connections::ConnectionOptions() const { - return *impl_.get(); -} - -// Verify if ConnectionOptions is in a not-initialized (Empty) state. -bool ConnectionOptions::Empty() const { return impl_->Empty(); } - -// Bring ConnectionOptions to a not-initialized (Empty) state. -void ConnectionOptions::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. -connections::ConnectionOptions ConnectionOptions::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 ConnectionOptions::GetMediums( - location::nearby::proto::connections::Medium *mediums, - uint32_t *mediumsSize) {} - -} // namespace windows -} // namespace connections -} // namespace nearby -} // namespace location diff --git a/windows/connection_options.h b/windows/connection_options.h deleted file mode 100644 index 6d7f0cfb..00000000 --- a/windows/connection_options.h +++ /dev/null @@ -1,91 +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. - -#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; - -namespace windows { -struct DLL_API ConnectionOptions { - ConnectionOptions(); - operator connections::ConnectionOptions() const; - - private: - std::unique_ptr - impl_; - - public: - connections::Strategy &strategy; - connections::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. - connections::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 windows -} // namespace connections -} // namespace nearby -} // namespace location - -#endif // WINDOWS_REQUEST_CONNECTION_OPTIONS_H_ diff --git a/windows/core_adapter.cc b/windows/core_adapter.cc index 436af081..f05980b7 100644 --- a/windows/core_adapter.cc +++ b/windows/core_adapter.cc @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -32,10 +32,10 @@ void CloseCore(Core *pCore) { } void StartAdvertising(Core *pCore, const char *service_id, - AdvertisingOptions options, ConnectionRequestInfo info, - ResultCallback callback) { + AdvertisingOptions advertising_options, + ConnectionRequestInfo info, ResultCallback callback) { if (pCore) { - pCore->StartAdvertising(service_id, options, info, callback); + pCore->StartAdvertising(service_id, advertising_options, info, callback); } } @@ -46,10 +46,10 @@ void StopAdvertising(Core *pCore, ResultCallback callback) { } void StartDiscovery(Core *pCore, const char *service_id, - DiscoveryOptions options, DiscoveryListener listener, - ResultCallback callback) { + DiscoveryOptions discovery_options, + DiscoveryListener listener, ResultCallback callback) { if (pCore) { - pCore->StartDiscovery(service_id, options, listener, callback); + pCore->StartDiscovery(service_id, discovery_options, listener, callback); } } @@ -68,10 +68,11 @@ void InjectEndpoint(Core *pCore, char *service_id, } void RequestConnection(Core *pCore, const char *endpoint_id, - ConnectionRequestInfo info, ConnectionOptions options, + ConnectionRequestInfo info, + ConnectionOptions connection_options, ResultCallback callback) { if (pCore) { - pCore->RequestConnection(endpoint_id, info, options, callback); + pCore->RequestConnection(endpoint_id, info, connection_options, callback); } } diff --git a/windows/core_adapter.h b/windows/core_adapter.h index 9882f270..339d2b87 100644 --- a/windows/core_adapter.h +++ b/windows/core_adapter.h @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// 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. @@ -18,10 +18,10 @@ #include "absl/types/span.h" // todo(jfcarroll) This cannot remain. It exposes stuff the client doesn't need. +#include "core/advertising_options.h" +#include "core/connection_options.h" +#include "core/discovery_options.h" #include "core/internal/offline_service_controller.h" -#include "third_party/nearby/windows/advertising_options.h" -#include "third_party/nearby/windows/connection_options.h" -#include "third_party/nearby/windows/discovery_options.h" #define DLL_EXPORT extern "C" __declspec(dllexport) @@ -55,7 +55,7 @@ DLL_EXPORT void __stdcall CloseCore(Core *pCore); // This can be an arbitrary string, so long as it uniquely // identifies your service. A good default is to use your // app's package name. -// options - The options for advertising. +// advertising_options - The options for advertising. // info - Connection parameters: // > name - A human readable name for this endpoint, to appear on // other devices. @@ -68,7 +68,7 @@ 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, AdvertisingOptions options, + Core *pCore, const char *service_id, AdvertisingOptions advertising_options, connections::ConnectionRequestInfo info, ResultCallback callback); // Stops advertising a local endpoint. Should be called after calling @@ -86,7 +86,7 @@ DLL_EXPORT void __stdcall StopAdvertising(Core *pCore, ResultCallback callback); // service_id - The ID for the service to be discovered, as specified in // the corresponding call to StartAdvertising. // listener - A callback notified when a remote endpoint is discovered. -// options - The options for discovery. +// discovery_options - The options for discovery. // result_cb - to access the status of the operation when available. // Possible status codes include: // Status::STATUS_OK if discovery started successfully. @@ -95,7 +95,7 @@ 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, - DiscoveryOptions options, + DiscoveryOptions discovery_options, DiscoveryListener listener, ResultCallback callback); @@ -148,11 +148,9 @@ DLL_EXPORT void __stdcall InjectEndpoint(Core *pCore, char *service_id, // Status::STATUS_RADIO_ERROR if we failed to connect because of an // issue with Bluetooth/WiFi. // Status::STATUS_ERROR if we failed to connect for any other reason. -DLL_EXPORT void __stdcall RequestConnection(Core *pCore, - const char *endpoint_id, - ConnectionRequestInfo info, - ConnectionOptions options, - ResultCallback callback); +DLL_EXPORT void __stdcall RequestConnection( + Core *pCore, const char *endpoint_id, ConnectionRequestInfo info, + ConnectionOptions connection_options, ResultCallback callback); // Accepts a connection to a remote endpoint. This method must be called // before Payloads can be exchanged with the remote endpoint. diff --git a/windows/dart/core_adapter_dart.cc b/windows/dart/core_adapter_dart.cc index beaf3564..71f4842f 100644 --- a/windows/dart/core_adapter_dart.cc +++ b/windows/dart/core_adapter_dart.cc @@ -77,20 +77,21 @@ void StartAdvertisingDart(Core *pCore, const char *service_id, return; } - AdvertisingOptions options; - options.strategy = GetStrategy(options_dart.strategy); - options.auto_upgrade_bandwidth = options_dart.auto_upgrade_bandwidth; - options.enforce_topology_constraints = + AdvertisingOptions advertising_options; + advertising_options.strategy = GetStrategy(options_dart.strategy); + advertising_options.auto_upgrade_bandwidth = + options_dart.auto_upgrade_bandwidth; + advertising_options.enforce_topology_constraints = options_dart.enforce_topology_constraints; - options.allowed.bluetooth = options_dart.enable_bluetooth; - options.allowed.ble = options_dart.enable_ble; - options.low_power = options_dart.use_low_power_mode; - options.fast_advertisement_service_uuid = + advertising_options.allowed.bluetooth = options_dart.enable_bluetooth; + advertising_options.allowed.ble = options_dart.enable_ble; + advertising_options.low_power = options_dart.use_low_power_mode; + advertising_options.fast_advertisement_service_uuid = options_dart.discover_fast_advertisements ? "0000FE2C-0000-1000-8000-00805F9B34FB" : ""; - options.allowed.wifi_lan = options_dart.enable_wifi_lan; - options.allowed.web_rtc = options_dart.enable_web_rtc; + advertising_options.allowed.wifi_lan = options_dart.enable_wifi_lan; + advertising_options.allowed.web_rtc = options_dart.enable_web_rtc; ConnectionRequestInfo info; info.endpoint_info = ByteArray(info_dart.endpoint_info); @@ -160,7 +161,8 @@ void StartAdvertisingDart(Core *pCore, const char *service_id, ResultCallback callback; SetResultCallback(callback, result_cb); - StartAdvertising(pCore, service_id, std::move(options), info, callback); + StartAdvertising(pCore, service_id, std::move(advertising_options), info, + callback); } void StopAdvertisingDart(Core *pCore, Dart_Port result_cb) { @@ -183,16 +185,14 @@ void StartDiscoveryDart(Core *pCore, const char *service_id, return; } - DiscoveryOptions options; - options.strategy = GetStrategy(options_dart.strategy); - options.allowed.bluetooth = options_dart.enable_bluetooth; - options.allowed.ble = options_dart.enable_ble; - options.allowed.wifi_lan = options_dart.enable_wifi_lan; - options.allowed.web_rtc = false; - options.low_power = options_dart.use_low_power_mode; - options.enable_bluetooth_listening = options_dart.enable_bluetooth; - options.enforce_topology_constraints = true; - options.fast_advertisement_service_uuid = + DiscoveryOptions discovery_options; + discovery_options.strategy = GetStrategy(options_dart.strategy); + discovery_options.allowed.bluetooth = options_dart.enable_bluetooth; + discovery_options.allowed.ble = options_dart.enable_ble; + discovery_options.allowed.wifi_lan = options_dart.enable_wifi_lan; + discovery_options.allowed.web_rtc = false; + discovery_options.enforce_topology_constraints = true; + discovery_options.fast_advertisement_service_uuid = options_dart.discover_fast_advertisements ? "0000FE2C-0000-1000-8000-00805F9B34FB" : ""; @@ -257,7 +257,8 @@ void StartDiscoveryDart(Core *pCore, const char *service_id, ResultCallback callback; SetResultCallback(callback, result_cb); - StartDiscovery(pCore, service_id, std::move(options), listener, callback); + StartDiscovery(pCore, service_id, std::move(discovery_options), listener, + callback); } void StopDiscoveryDart(Core *pCore, Dart_Port result_cb) { @@ -280,12 +281,12 @@ void RequestConnectionDart(Core *pCore, const char *endpoint_id, return; } - ConnectionOptions options; - options.enforce_topology_constraints = false; - options.allowed.bluetooth = options_dart.enable_bluetooth; - options.allowed.ble = options_dart.enable_ble; - options.allowed.wifi_lan = options_dart.enable_wifi_lan; - options.allowed.web_rtc = options_dart.enable_web_rtc; + ConnectionOptions connection_options; + connection_options.enforce_topology_constraints = false; + connection_options.allowed.bluetooth = options_dart.enable_bluetooth; + connection_options.allowed.ble = options_dart.enable_ble; + connection_options.allowed.wifi_lan = options_dart.enable_wifi_lan; + connection_options.allowed.web_rtc = options_dart.enable_web_rtc; ConnectionRequestInfo info; info.endpoint_info = ByteArray(info_dart.endpoint_info); @@ -355,7 +356,8 @@ void RequestConnectionDart(Core *pCore, const char *endpoint_id, ResultCallback callback; SetResultCallback(callback, result_cb); - RequestConnection(pCore, endpoint_id, info, std::move(options), callback); + RequestConnection(pCore, endpoint_id, info, std::move(connection_options), + callback); } void AcceptConnectionDart(Core *pCore, const char *endpoint_id, diff --git a/windows/discovery_options.cc b/windows/discovery_options.cc deleted file mode 100644 index caf1460c..00000000 --- a/windows/discovery_options.cc +++ /dev/null @@ -1,72 +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 "third_party/nearby/windows/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 { -namespace windows { - -DiscoveryOptions::DiscoveryOptions() - : 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) {} - -DiscoveryOptions::operator connections::ConnectionOptions() const { - return *impl_.get(); -} - -// Verify if ConnectionOptions is in a not-initialized (Empty) state. -bool DiscoveryOptions::Empty() const { return impl_->Empty(); } - -// Bring ConnectionOptions to a not-initialized (Empty) state. -void DiscoveryOptions::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. -connections::ConnectionOptions DiscoveryOptions::CompatibleOptions() const { - return impl_->CompatibleOptions(); -} - -// 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 DiscoveryOptions::GetMediums( - location::nearby::proto::connections::Medium *mediums, - uint32_t *mediumsSize) {} - -} // namespace windows -} // namespace connections -} // namespace nearby -} // namespace location diff --git a/windows/discovery_options.h b/windows/discovery_options.h deleted file mode 100644 index a22e9836..00000000 --- a/windows/discovery_options.h +++ /dev/null @@ -1,92 +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. - -#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; - -namespace windows { -struct DLL_API DiscoveryOptions { - DiscoveryOptions(); - operator connections::ConnectionOptions() const; - - private: - std::unique_ptr - impl_; - - public: - connections::Strategy &strategy; - connections::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. - connections::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 windows -} // namespace connections -} // namespace nearby -} // namespace location - -#endif // WINDOWS_START_DISCOVERY_OPTIONS_H_