From 6b692c120dcdf34b44d82dc58397ad0f42db1b96 Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Wed, 24 Aug 2022 12:32:40 -0700 Subject: [PATCH] Nearby Connections: Introduce Device wrapper class We are implementing the Device wrapper for connections to interact with Nearby Presence. PiperOrigin-RevId: 469796313 --- connections/BUILD | 2 +- connections/advertising_options.h | 5 ++ .../clients/windows/advertising_options_w.h | 5 ++ .../clients/windows/dart/core_adapter_dart.h | 6 +++ connections/core.h | 27 +++++++++++ connections/device.h | 47 ++++++++++++++++++ connections/implementation/client_proxy.h | 48 +++++++++++++++++++ 7 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 connections/device.h diff --git a/connections/BUILD b/connections/BUILD index b5fed4fd..bdcba000 100644 --- a/connections/BUILD +++ b/connections/BUILD @@ -54,6 +54,7 @@ cc_library( hdrs = [ "advertising_options.h", "connection_options.h", + "device.h", "discovery_options.h", "listeners.h", "medium_selector.h", @@ -80,7 +81,6 @@ cc_library( "//proto:connections_enums_cc_proto", "@com_google_absl//absl/strings", "@com_google_absl//absl/types:variant", - "@com_google_glog//:glog", ], ) diff --git a/connections/advertising_options.h b/connections/advertising_options.h index 8ea3ba09..497da015 100644 --- a/connections/advertising_options.h +++ b/connections/advertising_options.h @@ -40,6 +40,11 @@ struct AdvertisingOptions : public OptionsBase { // TODO(b/229927044): Replaces it as bool once Ble v1 is deprecated. std::string fast_advertisement_service_uuid; + // The information about this device (eg. name, device type), + // to appear on the remote device. + // Defined by client/application. + const char* device_info; + // 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. diff --git a/connections/clients/windows/advertising_options_w.h b/connections/clients/windows/advertising_options_w.h index d02357e0..446656c4 100644 --- a/connections/clients/windows/advertising_options_w.h +++ b/connections/clients/windows/advertising_options_w.h @@ -31,6 +31,11 @@ struct DLL_API AdvertisingOptionsW : public OptionsBaseW { bool is_out_of_band_connection = false; const char* fast_advertisement_service_uuid; + // The information about this device (eg. name, device type), + // to appear on the remote device. + // Defined by client/application. + const char* device_info; + // 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. diff --git a/connections/clients/windows/dart/core_adapter_dart.h b/connections/clients/windows/dart/core_adapter_dart.h index 71d48b5e..6dc9d862 100644 --- a/connections/clients/windows/dart/core_adapter_dart.h +++ b/connections/clients/windows/dart/core_adapter_dart.h @@ -58,6 +58,12 @@ struct AdvertisingOptionsDart { // Whether this is intended to be used in conjunction with InjectEndpoint(). int64_t is_out_of_band_connection = false; const char *fast_advertisement_service_uuid; + + // The information about this device (eg. name, device type), + // to appear on the remote device. + // Defined by client/application. + const char *device_info; + Mediums mediums; // LINT.ThenChange(//depot/google3/location/nearby/apps/helloconnections/flutter/lib/advertising_options.dart) }; diff --git a/connections/core.h b/connections/core.h index 3e39fc17..34715746 100644 --- a/connections/core.h +++ b/connections/core.h @@ -20,6 +20,7 @@ #include "absl/strings/string_view.h" #include "absl/types/span.h" +#include "connections/device.h" #include "connections/implementation/client_proxy.h" #include "connections/implementation/service_controller.h" #include "connections/implementation/service_controller_router.h" @@ -240,6 +241,32 @@ class Core { std::string Dump(); + //******************************* V2 ******************************* + void RequestConnectionV2(const NearbyDevice& device, + const ConnectionRequestInfo& info, + ConnectionOptions& connection_options, + ResultCallback callback); + + void AcceptConnectionV2(const NearbyDevice& device, + const PayloadListener& listener, + ResultCallback callback); + + void RejectConnectionV2(const NearbyDevice& device, ResultCallback callback); + + // Span is being used here as we will not be modifying this block of memory. + // We also do not need to own this block of memory, so we can use Span. + // We are using NearbyDevice* so as to not lose attributes when using a + // vector-like structure of NearbyDevice, as object information is stripped if + // using NearbyDevice or NearbyDevice&. + void SendPayloadV2(absl::Span devices, + const Payload& payload, ResultCallback callback); + + void DisconnectFromDeviceV2(const NearbyDevice& device, + ResultCallback callback); + + void InitiateBandwidthUpgradeV2(const NearbyDevice& device, + ResultCallback callback); + private: ClientProxy client_; ServiceControllerRouter* router_ = nullptr; diff --git a/connections/device.h b/connections/device.h new file mode 100644 index 00000000..39296d38 --- /dev/null +++ b/connections/device.h @@ -0,0 +1,47 @@ +// Copyright 2022 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 THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_DEVICE_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_DEVICE_H_ + +#include + +#include "internal/platform/byte_array.h" + +namespace location { +namespace nearby { +namespace connections { + +class NearbyDevice { + public: + enum Type { + kUnknownDevice = 0, + kConnectionsDevice = 1, + kPresenceDevice = 2, + }; + virtual ~NearbyDevice() = default; + NearbyDevice(NearbyDevice&&) = default; + NearbyDevice& operator=(NearbyDevice&&) = default; + NearbyDevice(const NearbyDevice&) = delete; + NearbyDevice& operator=(const NearbyDevice&) = delete; + virtual std::string GetEndpointId() const = 0; + virtual ByteArray GetEndpointInfo() const = 0; + virtual Type GetType() const { return Type::kUnknownDevice; } +}; + +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_DEVICE_H_ diff --git a/connections/implementation/client_proxy.h b/connections/implementation/client_proxy.h index 5a20592f..cdd32b68 100644 --- a/connections/implementation/client_proxy.h +++ b/connections/implementation/client_proxy.h @@ -21,6 +21,7 @@ #include #include "connections/advertising_options.h" +#include "connections/device.h" #include "connections/discovery_options.h" #include "connections/implementation/analytics/analytics_recorder.h" #include "connections/listeners.h" @@ -190,6 +191,53 @@ class ClientProxy final { std::string Dump(); + //********************************** V2 ********************************** + void OnDeviceFound(const absl::string_view service_id, + const NearbyDevice& device, + proto::connections::Medium medium); + // Proxies to the client's DiscoveryListener::OnEndpointLost() callback. + void OnEndpointLost(absl::string_view service_id, const NearbyDevice& device); + + // Proxies to the client's ConnectionListener::OnInitiated() callback. + void OnConnectionInitiated(const NearbyDevice& device, + const ConnectionResponseInfo& info, + const ConnectionOptions& connection_options, + const ConnectionListener& listener, + absl::string_view connection_token); + + void OnConnectionAccepted(const NearbyDevice& device); + void OnConnectionRejected(const NearbyDevice& device, const Status& status); + + void OnBandwidthChanged(const NearbyDevice& device, Medium new_medium); + + // If notify is true, also calls the client's + // ConnectionListener.disconnected_cb() callback. + void OnDisconnected(const NearbyDevice& device, bool notify); + + BooleanMediumSelector GetUpgradableMediums(const NearbyDevice& device) const; + bool IsConnectedToEndpoint(const NearbyDevice& device) const; + // No payloads should be sent until isConnectedToEndpoint() + // returns true. + bool HasPendingConnectionToEndpoint(const NearbyDevice& device) const; + bool HasLocalEndpointResponded(const NearbyDevice& device) const; + bool HasRemoteEndpointResponded(const NearbyDevice& device) const; + void LocalEndpointAcceptedConnection(const NearbyDevice& device, + const PayloadListener& listener); + void LocalEndpointRejectedConnection(const NearbyDevice& device); + void RemoteEndpointAcceptedConnection(const NearbyDevice& device); + void RemoteEndpointRejectedConnection(const NearbyDevice& device); + bool IsConnectionAccepted(const NearbyDevice& device) const; + bool IsConnectionRejected(const NearbyDevice& device) const; + + void OnPayload(const NearbyDevice& device, Payload payload); + void OnPayloadProgress(const NearbyDevice& device, + const PayloadProgressInfo& info); + bool LocalConnectionIsAccepted(const NearbyDevice& device) const; + bool RemoteConnectionIsAccepted(const NearbyDevice& device) const; + void AddCancellationFlag(const NearbyDevice& device); + CancellationFlag* GetCancellationFlag(const NearbyDevice& device); + void CancelEndpoint(const NearbyDevice& device); + private: struct Connection { // Status: may be either: