Nearby Connections: Introduce Device wrapper class

We are implementing the Device wrapper for connections to interact with Nearby Presence.

PiperOrigin-RevId: 469796313
This commit is contained in:
Anay Wadhera
2022-08-24 12:34:02 -07:00
committed by Copybara-Service
parent f3c1a76723
commit 6b692c120d
7 changed files with 139 additions and 1 deletions
+1 -1
View File
@@ -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",
],
)
+5
View File
@@ -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.
@@ -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.
@@ -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)
};
+27
View File
@@ -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<const NearbyDevice*> 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;
+47
View File
@@ -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 <string>
#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_
+48
View File
@@ -21,6 +21,7 @@
#include <vector>
#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: