diff --git a/cpp/core/BUILD b/cpp/core/BUILD index cf0644ba..d0fbde00 100644 --- a/cpp/core/BUILD +++ b/cpp/core/BUILD @@ -23,7 +23,7 @@ cc_library( ], visibility = [ "//googlemac/iPhone/Shared/Nearby/Connections:__subpackages__", - "//location/nearby/connections/windows:__subpackages__", + "//third_party/nearby_connections/windows:__subpackages__", ], deps = [ ":core_types", diff --git a/cpp/platform/impl/windows/BUILD b/cpp/platform/impl/windows/BUILD index f0db1bfa..a45f57e4 100644 --- a/cpp/platform/impl/windows/BUILD +++ b/cpp/platform/impl/windows/BUILD @@ -38,6 +38,9 @@ cc_library( "settable_future.h", "submittable_executor.h", ], + visibility = [ + "//third_party/nearby_connections/windows:__subpackages__", + ], deps = [ "//base", "//platform/api:types", @@ -60,6 +63,7 @@ cc_library( deps = [ "//platform/api:comm", "//platform/base", + "//platform/impl/windows/generated:types", ], ) @@ -95,7 +99,7 @@ cc_library( ], copts = ["-Ithird_party/nearby_connections/cpp/platform/impl/windows/generated"], visibility = [ - "//location/nearby/connections/windows:__subpackages__", + "//third_party/nearby_connections/windows:__subpackages__", ], deps = [ ":comm", @@ -118,7 +122,7 @@ cc_library( "test_utils.h", ], visibility = [ - "//location/nearby/connections/windows:__subpackages__", + "//location/nearby/windows:__subpackages__", ], deps = [ "//platform/base", diff --git a/cpp/platform/impl/windows/generated/BUILD b/cpp/platform/impl/windows/generated/BUILD index ba7eddd2..1bbd6bdb 100644 --- a/cpp/platform/impl/windows/generated/BUILD +++ b/cpp/platform/impl/windows/generated/BUILD @@ -33,5 +33,6 @@ cc_library( textual_hdrs = glob(["**/*.h"]), visibility = [ "//platform/impl/windows:__subpackages__", + "//third_party/nearby_connections/windows:__subpackages__", ], ) diff --git a/windows/BUILD b/windows/BUILD new file mode 100644 index 00000000..e9576671 --- /dev/null +++ b/windows/BUILD @@ -0,0 +1,36 @@ +licenses(["notice"]) +# 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. + +exports_files(["LICENSE"]) # needs to be on a line by itself for PRESUBMIT + +load("//third_party/msvc:build_windows.bzl", "cc_windows_dll") + +# Build with --config=lexan +cc_windows_dll( + name = "core_adapter", + srcs = ["core_adapter.cc"], + hdrs = ["core_adapter.h"], + copts = ["-DCORE_ADAPTER_BUILD_DLL"], + defines = ["CORE_ADAPTER_DLL"], + tags = ["windows-dll"], + deps = [ + "//absl/strings", + "//core", + "//platform/impl/windows", + "//platform/impl/windows:types", + "//platform/impl/windows/generated:types", + "//webrtc/api:create_peerconnection_factory", + ], +) diff --git a/windows/core_adapter.cc b/windows/core_adapter.cc new file mode 100644 index 00000000..78767456 --- /dev/null +++ b/windows/core_adapter.cc @@ -0,0 +1,143 @@ +// 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. + +#include "third_party/nearby_connections/windows/core_adapter.h" +#include "absl/strings/str_format.h" + +namespace location { +namespace nearby { +namespace connections { + +Core* InitCoreWithServiceControllerFactory( + std::function factory) { + return new Core(factory); +} + +Core* InitCore() { return new Core(); } + +void CloseCore(Core* pCore) { + if (pCore) { + pCore->StopAllEndpoints( + {.result_cb = + std::function{ + [](location::nearby::connections::Status) {}}}); + delete pCore; + pCore = nullptr; + } +} + +void StartAdvertising(Core* pCore, absl::string_view service_id, + ConnectionOptions options, ConnectionRequestInfo info, + ResultCallback callback) { + if (pCore) { + pCore->StartAdvertising(service_id, options, info, callback); + } +} + +void StopAdvertising(Core* pCore, ResultCallback callback) { + if (pCore) { + pCore->StopAdvertising(callback); + } +} + +void StartDiscovery(Core* pCore, absl::string_view service_id, + ConnectionOptions options, DiscoveryListener listener, + ResultCallback callback) { + if (pCore) { + pCore->StartDiscovery(service_id, options, listener, callback); + } +} + +void StopDiscovery(Core* pCore, ResultCallback callback) { + if (pCore) { + pCore->StopDiscovery(callback); + } +} + +void InjectEndpoint(Core* pCore, absl::string_view service_id, + OutOfBandConnectionMetadata metadata, + ResultCallback callback) { + if (pCore) { + pCore->InjectEndpoint(service_id, metadata, callback); + } +} + +void RequestConnection(Core* pCore, absl::string_view endpoint_id, + ConnectionRequestInfo info, ConnectionOptions options, + ResultCallback callback) { + if (pCore) { + pCore->RequestConnection(endpoint_id, info, options, callback); + } +} + +void AcceptConnection(Core* pCore, absl::string_view endpoint_id, + PayloadListener listener, ResultCallback callback) { + if (pCore) { + pCore->AcceptConnection(endpoint_id, listener, callback); + } +} + +void RejectConnection(Core* pCore, absl::string_view endpoint_id, + ResultCallback callback) { + if (pCore) { + pCore->RejectConnection(endpoint_id, callback); + } +} + +void SendPayload(Core* pCore, absl::Span endpoint_ids, + Payload payload, ResultCallback callback) { + if (pCore) { + pCore->SendPayload(endpoint_ids, std::move(payload), callback); + } +} + +void CancelPayload(Core* pCore, std::int64_t payload_id, + ResultCallback callback) { + if (pCore) { + pCore->CancelPayload(payload_id, callback); + } +} + +void DisconnectFromEndpoint(Core* pCore, absl::string_view endpoint_id, + ResultCallback callback) { + if (pCore) { + pCore->DisconnectFromEndpoint(endpoint_id, callback); + } +} + +void StopAllEndpoints(Core* pCore, ResultCallback callback) { + if (pCore) { + pCore->StopAllEndpoints(callback); + } +} + +void InitiateBandwidthUpgrade(Core* pCore, absl::string_view endpoint_id, + ResultCallback callback) { + if (pCore) { + pCore->InitiateBandwidthUpgrade(endpoint_id, callback); + } +} + +const char* GetLocalEndpointId(Core* pCore) { + if (pCore) { + std::string endpoint_id = pCore->GetLocalEndpointId(); + char * result = new char[endpoint_id.length() + 1]; + absl::SNPrintF(result, endpoint_id.length() + 1, "%s", endpoint_id); + return result; + } + return "Null-Core"; +} +} // namespace connections +} // namespace nearby +} // namespace location diff --git a/windows/core_adapter.h b/windows/core_adapter.h new file mode 100644 index 00000000..d2b371dd --- /dev/null +++ b/windows/core_adapter.h @@ -0,0 +1,261 @@ +// 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 LOCATION_NEARBY_CONNECTIONS_WINDOWS_CORE_ADAPTER_H_ +#define LOCATION_NEARBY_CONNECTIONS_WINDOWS_CORE_ADAPTER_H_ + +#include "absl/strings/string_view.h" +#include "absl/types/span.h" +#include "core/core.h" +#include "core/internal/client_proxy.h" +#include "core/internal/offline_service_controller.h" +#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" + +#define DLL_API extern "C" __declspec(dllexport) +namespace location { +namespace nearby { +namespace connections { + +// Initizlizes a Core instance, providing the ServiceController factory from +// app side. If no factory is provided, it will initialize a new +// factory creating OffilineServiceController. +// Returns the instance handle to c# client. +DLL_API Core* __stdcall InitCoreWithServiceControllerFactory( + std::function factory = []() { + return new OfflineServiceController; + }); + +// Initializes a default Core instance. +// Returns the instance handle to c# client. +DLL_API Core* __stdcall InitCore(); + +// Closes the core with stopping all endpoints, then free the memory. +DLL_API void __stdcall CloseCore(Core* pCore); + +// Starts advertising an endpoint for a local app. +// +// service_id - An identifier to advertise your app to other endpoints. +// 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. +// info - Connection parameters: +// > name - A human readable name for this endpoint, to appear on +// other devices. +// > listener - A callback notified when remote endpoints request a +// connection to this endpoint. +// callback - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK if advertising started successfully. +// 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. +DLL_API void __stdcall StartAdvertising(Core* pCore, + absl::string_view service_id, + ConnectionOptions options, + ConnectionRequestInfo info, + ResultCallback callback); + +// Stops advertising a local endpoint. Should be called after calling +// StartAdvertising, as soon as the application no longer needs to advertise +// itself or goes inactive. Payloads can still be sent to connected +// endpoints after advertising ends. +// +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK if none of the above errors occurred. +DLL_API void __stdcall StopAdvertising(Core* pCore, ResultCallback callback); + +// Starts discovery for remote endpoints with the specified service ID. +// +// 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. +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK if discovery started successfully. +// Status::STATUS_ALREADY_DISCOVERING if the app is already +// discovering the specified service. +// Status::STATUS_OUT_OF_ORDER_API_CALL if the app is currently +// connected to remote endpoints; call StopAllEndpoints first. +DLL_API void __stdcall StartDiscovery(Core* pCore, absl::string_view service_id, + ConnectionOptions options, + DiscoveryListener listener, + ResultCallback callback); + +// Stops discovery for remote endpoints, after a previous call to +// StartDiscovery, when the client no longer needs to discover endpoints or +// goes inactive. Payloads can still be sent to connected endpoints after +// discovery ends. +// +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK if none of the above errors occurred. +DLL_API void __stdcall StopDiscovery(Core* pCore, ResultCallback callback); + +// Invokes the discovery callback from a previous call to StartDiscovery() +// with the given endpoint info. The previous call to StartDiscovery() must +// have been passed ConnectionOptions with is_out_of_band_connection == true. +// +// service_id - The ID for the service to be discovered, as +// specified in the corresponding call to +// StartDiscovery(). +// metadata - Metadata used in order to inject the endpoint. +// result_cb - to access the status of the operation when +// available. +// Possible status codes include: +// Status::kSuccess if endpoint injection was attempted. +// Status::kError if endpoint_id, endpoint_info, or +// remote_bluetooth_mac_address are malformed. +// Status::kOutOfOrderApiCall if the app is not discovering. +DLL_API void __stdcall InjectEndpoint(Core* pCore, absl::string_view service_id, + OutOfBandConnectionMetadata metadata, + ResultCallback callback); + +// Sends a request to connect to a remote endpoint. +// +// endpoint_id - The identifier for the remote endpoint to which a +// connection request will be sent. Should match the value +// provided in a call to +// DiscoveryListener::endpoint_found_cb() +// info - Connection parameters: +// > name - A human readable name for the local endpoint, to appear on +// the remote endpoint. +// > listener - A callback notified when the remote endpoint sends a +// response to the connection request. +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK if the connection request was sent. +// Status::STATUS_ALREADY_CONNECTED_TO_ENDPOINT if the app already +// has a connection to the specified endpoint. +// 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_API void __stdcall RequestConnection(Core* pCore, + absl::string_view endpoint_id, + ConnectionRequestInfo info, + ConnectionOptions options, + ResultCallback callback); + +// Accepts a connection to a remote endpoint. This method must be called +// before Payloads can be exchanged with the remote endpoint. +// +// endpoint_id - The identifier for the remote endpoint. Should match the +// value provided in a call to +// ConnectionListener::onConnectionInitiated. +// listener - A callback for payloads exchanged with the remote endpoint. +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK if the connection request was accepted. +// Status::STATUS_ALREADY_CONNECTED_TO_ENDPOINT if the app already. +// has a connection to the specified endpoint. +DLL_API void __stdcall AcceptConnection(Core* pCore, + absl::string_view endpoint_id, + PayloadListener listener, + ResultCallback callback); + +// Rejects a connection to a remote endpoint. +// +// endpoint_id - The identifier for the remote endpoint. Should match the +// value provided in a call to +// ConnectionListener::onConnectionInitiated(). +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK} if the connection request was rejected. +// Status::STATUS_ALREADY_CONNECTED_TO_ENDPOINT} if the app already +// has a connection to the specified endpoint. +DLL_API void __stdcall RejectConnection(Core* pCore, + absl::string_view endpoint_id, + ResultCallback callback); + +// Sends a Payload to a remote endpoint. Payloads can only be sent to remote +// endpoints once a notice of connection acceptance has been delivered via +// ConnectionListener::onConnectionResult(). +// +// endpoint_ids - Array of remote endpoint identifiers for the to which the +// payload should be sent. +// payload - The Payload to be sent. +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OUT_OF_ORDER_API_CALL if the device has not first +// performed advertisement or discovery (to set the Strategy. +// Status::STATUS_ENDPOINT_UNKNOWN if there's no active (or pending) +// connection to the remote endpoint. +// Status::STATUS_OK if none of the above errors occurred. Note that this +// indicates that Nearby Connections will attempt to send the Payload, +// but not that the send has successfully completed yet. Errors might +// still occur during transmission (and at different times for +// different endpoints), and will be delivered via +// PayloadCallback#onPayloadTransferUpdate. +DLL_API void __stdcall SendPayload(Core* pCore, + absl::Span endpoint_ids, + Payload payload, ResultCallback callback); + +// Cancels a Payload currently in-flight to or from remote endpoint(s). +// +// payload_id - The identifier for the Payload to be canceled. +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK if none of the above errors occurred. +DLL_API void __stdcall CancelPayload(Core* pCore, std::int64_t payload_id, + ResultCallback callback); + +// Disconnects from a remote endpoint. {@link Payload}s can no longer be sent +// to or received from the endpoint after this method is called. +// +// endpoint_id - The identifier for the remote endpoint to disconnect from. +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK - finished successfully. +DLL_API void __stdcall DisconnectFromEndpoint(Core* pCore, + absl::string_view endpoint_id, + ResultCallback callback); + +// Disconnects from, and removes all traces of, all connected and/or +// discovered endpoints. This call is expected to be preceded by a call to +// StopAdvertising or StartDiscovery as needed. After calling +// StopAllEndpoints, no further operations with remote endpoints will be +// possible until a new call to one of StartAdvertising() or StartDiscovery(). +// +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK - finished successfully. +DLL_API void __stdcall StopAllEndpoints(Core* pCore, ResultCallback callback); + +// Sends a request to initiate connection bandwidth upgrade. +// +// endpoint_id - The identifier for the remote endpoint which will be +// switching to a higher connection data rate and possibly +// different wireless protocol. On success, calls +// ConnectionListener::bandwidth_changed_cb(). +// result_cb - to access the status of the operation when available. +// Possible status codes include: +// Status::STATUS_OK - finished successfully. +DLL_API void __stdcall InitiateBandwidthUpgrade(Core* pCore, + absl::string_view endpoint_id, + ResultCallback callback); + +// Gets the local endpoint generated by Nearby Connections. +// DLL_API std::string __stdcall GetLocalEndpointId(Core* pCore); +DLL_API const char* __stdcall GetLocalEndpointId(Core* pCore); +} // namespace connections +} // namespace nearby +} // namespace location + +#endif // LOCATION_NEARBY_CONNECTIONS_WINDOWS_CORE_ADAPTER_H_