From cce45b745e8b5ec5ca8868d8dd573ba8ae96f11e Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Mon, 6 Oct 2025 09:01:37 -0700 Subject: [PATCH] Internal refactor PiperOrigin-RevId: 815749899 --- Package.swift | 27 +- connections/implementation/BUILD | 6 +- .../implementation/base_pcp_handler.cc | 4 +- connections/implementation/base_pcp_handler.h | 11 +- ...int_channel.cc => ble_endpoint_channel.cc} | 20 +- ...point_channel.h => ble_endpoint_channel.h} | 16 +- .../ble_l2cap_endpoint_channel.cc | 2 +- .../ble_l2cap_endpoint_channel.h | 2 +- connections/implementation/bwu_manager.cc | 10 +- connections/implementation/bwu_manager.h | 4 +- connections/implementation/mediums/BUILD | 18 +- .../mediums/{ble_v2.cc => ble.cc} | 202 +++++----- .../mediums/{ble_v2.h => ble.h} | 62 ++-- .../mediums/{ble_v2 => ble}/BUILD | 6 +- .../advertisement_read_result.cc | 2 +- .../advertisement_read_result.h | 6 +- .../advertisement_read_result_test.cc | 2 +- .../{ble_v2 => ble}/ble_advertisement.cc | 24 +- .../{ble_v2 => ble}/ble_advertisement.h | 44 +-- .../ble_advertisement_header.cc | 2 +- .../ble_advertisement_header.h | 24 +- .../ble_advertisement_header_test.cc | 2 +- .../{ble_v2 => ble}/ble_advertisement_test.cc | 4 +- .../{ble_v2 => ble}/ble_l2cap_packet.cc | 4 +- .../{ble_v2 => ble}/ble_l2cap_packet.h | 8 +- .../{ble_v2 => ble}/ble_l2cap_packet_test.cc | 12 +- .../mediums/{ble_v2 => ble}/ble_packet.cc | 2 +- .../mediums/{ble_v2 => ble}/ble_packet.h | 6 +- .../{ble_v2 => ble}/ble_packet_test.cc | 2 +- .../mediums/{ble_v2 => ble}/ble_utils.cc | 8 +- .../mediums/{ble_v2 => ble}/ble_utils.h | 8 +- .../mediums/{ble_v2 => ble}/ble_utils_test.cc | 2 +- .../mediums/{ble_v2 => ble}/bloom_filter.cc | 2 +- .../mediums/{ble_v2 => ble}/bloom_filter.h | 6 +- .../{ble_v2 => ble}/bloom_filter_test.cc | 2 +- .../discovered_peripheral_callback.h | 20 +- .../discovered_peripheral_tracker.cc | 50 +-- .../discovered_peripheral_tracker.h | 56 +-- .../discovered_peripheral_tracker_test.cc | 235 ++++++------ .../instant_on_lost_advertisement.cc | 4 +- .../instant_on_lost_advertisement.h | 6 +- .../instant_on_lost_advertisement_test.cc | 2 +- .../instant_on_lost_manager.cc | 16 +- .../{ble_v2 => ble}/instant_on_lost_manager.h | 10 +- .../instant_on_lost_manager_test.cc | 2 +- .../mediums/{ble_v2_test.cc => ble_test.cc} | 344 +++++++++--------- connections/implementation/mediums/mediums.cc | 4 +- connections/implementation/mediums/mediums.h | 8 +- .../implementation/p2p_cluster_pcp_handler.cc | 251 +++++++------ .../implementation/p2p_cluster_pcp_handler.h | 65 ++-- .../p2p_cluster_pcp_handler_test.cc | 97 +++-- .../p2p_point_to_point_pcp_handler.cc | 2 +- .../implementation/p2p_star_pcp_handler.cc | 2 +- internal/platform/BUILD | 6 +- internal/platform/{ble_v2.cc => ble.cc} | 125 ++++--- internal/platform/{ble_v2.h => ble.h} | 241 ++++++------ .../platform/{ble_v2_test.cc => ble_test.cc} | 243 ++++++------- internal/platform/implementation/BUILD | 9 +- .../apple/Tests/GNCBLEUtilsTest.mm | 10 +- .../apple/Tests/GNCPlatformTest.mm | 4 +- .../apple/Tests/ble_peripheral_test.mm | 2 +- .../implementation/apple/ble_gatt_client.h | 15 +- .../implementation/apple/ble_gatt_client.mm | 12 +- .../implementation/apple/ble_gatt_server.h | 14 +- .../implementation/apple/ble_gatt_server.mm | 13 +- .../apple/ble_l2cap_server_socket.h | 6 +- .../apple/ble_l2cap_server_socket.mm | 2 +- .../implementation/apple/ble_l2cap_socket.h | 20 +- .../implementation/apple/ble_l2cap_socket.mm | 4 +- .../implementation/apple/ble_medium.h | 77 ++-- .../implementation/apple/ble_medium.mm | 79 ++-- .../implementation/apple/ble_peripheral.h | 6 +- .../implementation/apple/ble_peripheral.mm | 10 +- .../implementation/apple/ble_server_socket.h | 21 +- .../implementation/apple/ble_server_socket.mm | 2 +- .../implementation/apple/ble_socket.h | 10 +- .../implementation/apple/ble_socket.mm | 5 +- .../platform/implementation/apple/ble_utils.h | 40 +- .../implementation/apple/ble_utils.mm | 10 +- .../platform/implementation/apple/platform.mm | 10 +- .../implementation/{ble_v2.h => ble.h} | 12 +- internal/platform/implementation/g3/BUILD | 6 +- .../implementation/g3/{ble_v2.cc => ble.cc} | 284 +++++++-------- .../implementation/g3/{ble_v2.h => ble.h} | 165 +++++---- .../g3/{ble_v2_test.cc => ble_test.cc} | 88 +++-- .../implementation/g3/bluetooth_adapter.cc | 6 +- .../implementation/g3/bluetooth_adapter.h | 14 +- .../platform/implementation/g3/platform.cc | 10 +- internal/platform/implementation/platform.h | 4 +- .../platform/implementation/windows/BUILD | 18 +- .../windows/{ble_v2.cc => ble.cc} | 152 ++++---- .../windows/{ble_v2.h => ble.h} | 63 ++-- .../implementation/windows/ble_gatt_client.cc | 27 +- .../implementation/windows/ble_gatt_client.h | 21 +- .../implementation/windows/ble_gatt_server.cc | 29 +- .../implementation/windows/ble_gatt_server.h | 28 +- .../windows/ble_gatt_server_test.cc | 50 +-- ..._server_socket.cc => ble_server_socket.cc} | 18 +- ...v2_server_socket.h => ble_server_socket.h} | 20 +- .../{ble_v2_socket.cc => ble_socket.cc} | 20 +- .../windows/{ble_v2_socket.h => ble_socket.h} | 16 +- .../windows/{ble_v2_test.cc => ble_test.cc} | 104 +++--- .../implementation/windows/platform.cc | 18 +- internal/platform/medium_environment.cc | 147 ++++---- internal/platform/medium_environment.h | 68 ++-- presence/implementation/broadcast_manager.cc | 7 +- presence/implementation/broadcast_manager.h | 9 +- .../implementation/broadcast_manager_test.cc | 2 +- presence/implementation/mediums/BUILD | 6 + presence/implementation/mediums/ble.h | 26 +- presence/implementation/mediums/ble_test.cc | 32 +- presence/implementation/scan_manager.cc | 10 +- presence/implementation/scan_manager.h | 19 +- presence/implementation/scan_manager_test.cc | 6 +- 114 files changed, 2096 insertions(+), 2141 deletions(-) rename connections/implementation/{ble_v2_endpoint_channel.cc => ble_endpoint_channel.cc} (73%) rename connections/implementation/{ble_v2_endpoint_channel.h => ble_endpoint_channel.h} (70%) rename connections/implementation/mediums/{ble_v2.cc => ble.cc} (89%) rename connections/implementation/mediums/{ble_v2.h => ble.h} (88%) rename connections/implementation/mediums/{ble_v2 => ble}/BUILD (98%) rename connections/implementation/mediums/{ble_v2 => ble}/advertisement_read_result.cc (98%) rename connections/implementation/mediums/{ble_v2 => ble}/advertisement_read_result.h (95%) rename connections/implementation/mediums/{ble_v2 => ble}/advertisement_read_result_test.cc (98%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_advertisement.cc (94%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_advertisement.h (84%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_advertisement_header.cc (98%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_advertisement_header.h (82%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_advertisement_header_test.cc (98%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_advertisement_test.cc (99%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_l2cap_packet.cc (98%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_l2cap_packet.h (95%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_l2cap_packet_test.cc (95%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_packet.cc (98%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_packet.h (95%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_packet_test.cc (99%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_utils.cc (93%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_utils.h (91%) rename connections/implementation/mediums/{ble_v2 => ble}/ble_utils_test.cc (98%) rename connections/implementation/mediums/{ble_v2 => ble}/bloom_filter.cc (98%) rename connections/implementation/mediums/{ble_v2 => ble}/bloom_filter.h (95%) rename connections/implementation/mediums/{ble_v2 => ble}/bloom_filter_test.cc (99%) rename connections/implementation/mediums/{ble_v2 => ble}/discovered_peripheral_callback.h (67%) rename connections/implementation/mediums/{ble_v2 => ble}/discovered_peripheral_tracker.cc (96%) rename connections/implementation/mediums/{ble_v2 => ble}/discovered_peripheral_tracker.h (90%) rename connections/implementation/mediums/{ble_v2 => ble}/discovered_peripheral_tracker_test.cc (91%) rename connections/implementation/mediums/{ble_v2 => ble}/instant_on_lost_advertisement.cc (96%) rename connections/implementation/mediums/{ble_v2 => ble}/instant_on_lost_advertisement.h (95%) rename connections/implementation/mediums/{ble_v2 => ble}/instant_on_lost_advertisement_test.cc (97%) rename connections/implementation/mediums/{ble_v2 => ble}/instant_on_lost_manager.cc (95%) rename connections/implementation/mediums/{ble_v2 => ble}/instant_on_lost_manager.h (94%) rename connections/implementation/mediums/{ble_v2 => ble}/instant_on_lost_manager_test.cc (98%) rename connections/implementation/mediums/{ble_v2_test.cc => ble_test.cc} (81%) rename internal/platform/{ble_v2.cc => ble.cc} (65%) rename internal/platform/{ble_v2.h => ble.h} (70%) rename internal/platform/{ble_v2_test.cc => ble_test.cc} (79%) rename internal/platform/implementation/{ble_v2.h => ble.h} (99%) rename internal/platform/implementation/g3/{ble_v2.cc => ble.cc} (71%) rename internal/platform/implementation/g3/{ble_v2.h => ble.h} (61%) rename internal/platform/implementation/g3/{ble_v2_test.cc => ble_test.cc} (63%) rename internal/platform/implementation/windows/{ble_v2.cc => ble.cc} (91%) rename internal/platform/implementation/windows/{ble_v2.h => ble.h} (81%) rename internal/platform/implementation/windows/{ble_v2_server_socket.cc => ble_server_socket.cc} (78%) rename internal/platform/implementation/windows/{ble_v2_server_socket.h => ble_server_socket.h} (72%) rename internal/platform/implementation/windows/{ble_v2_socket.cc => ble_socket.cc} (74%) rename internal/platform/implementation/windows/{ble_v2_socket.h => ble_socket.h} (84%) rename internal/platform/implementation/windows/{ble_v2_test.cc => ble_test.cc} (56%) diff --git a/Package.swift b/Package.swift index dfef5a7d..54d38762 100644 --- a/Package.swift +++ b/Package.swift @@ -253,7 +253,7 @@ let package = Package( "connections/implementation/analytics/BUILD", "connections/implementation/flags/BUILD", "connections/implementation/mediums/advertisements/BUILD", - "connections/implementation/mediums/ble_v2/BUILD", + "connections/implementation/mediums/ble/BUILD", "connections/implementation/mediums/multiplex/BUILD", "connections/implementation/mediums/BUILD", "connections/implementation/BUILD", @@ -309,17 +309,17 @@ let package = Package( "connections/implementation/mediums/advertisements/dct_advertisement_test.cc", "connections/implementation/mediums/advertisements/advertisement_util_test.cc", "connections/implementation/mediums/awdl_test.cc", - "connections/implementation/mediums/ble_v2_test.cc", - "connections/implementation/mediums/ble_v2/bloom_filter_test.cc", - "connections/implementation/mediums/ble_v2/ble_l2cap_packet_test.cc", - "connections/implementation/mediums/ble_v2/ble_packet_test.cc", - "connections/implementation/mediums/ble_v2/ble_advertisement_test.cc", - "connections/implementation/mediums/ble_v2/advertisement_read_result_test.cc", - "connections/implementation/mediums/ble_v2/ble_advertisement_header_test.cc", - "connections/implementation/mediums/ble_v2/ble_utils_test.cc", - "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker_test.cc", - "connections/implementation/mediums/ble_v2/instant_on_lost_advertisement_test.cc", - "connections/implementation/mediums/ble_v2/instant_on_lost_manager_test.cc", + "connections/implementation/mediums/ble_test.cc", + "connections/implementation/mediums/ble/bloom_filter_test.cc", + "connections/implementation/mediums/ble/ble_l2cap_packet_test.cc", + "connections/implementation/mediums/ble/ble_packet_test.cc", + "connections/implementation/mediums/ble/ble_advertisement_test.cc", + "connections/implementation/mediums/ble/advertisement_read_result_test.cc", + "connections/implementation/mediums/ble/ble_advertisement_header_test.cc", + "connections/implementation/mediums/ble/ble_utils_test.cc", + "connections/implementation/mediums/ble/discovered_peripheral_tracker_test.cc", + "connections/implementation/mediums/ble/instant_on_lost_advertisement_test.cc", + "connections/implementation/mediums/ble/instant_on_lost_manager_test.cc", "connections/implementation/mediums/multiplex/multiplex_frames_test.cc", "connections/implementation/mediums/multiplex/multiplex_socket_test.cc", "connections/implementation/mediums/multiplex/multiplex_output_stream_test.cc", @@ -375,7 +375,7 @@ let package = Package( "internal/platform/byte_array_test.cc", "internal/platform/credential_storage_impl_test.cc", "internal/platform/implementation/g3/awdl_test.cc", - "internal/platform/implementation/g3/ble_v2_test.cc", + "internal/platform/implementation/g3/ble_test.cc", "internal/platform/input_stream_test.cc", "internal/platform/mac_address_test.cc", "internal/platform/single_thread_executor_test.cc", @@ -402,7 +402,6 @@ let package = Package( "internal/platform/multi_thread_executor_test.cc", "internal/platform/ble_connection_info_test.cc", "internal/platform/ble_test.cc", - "internal/platform/ble_v2_test.cc", "internal/platform/prng_test.cc", "internal/platform/implementation/apple/count_down_latch_test.cc", "internal/platform/implementation/apple/condition_variable_test.cc", diff --git a/connections/implementation/BUILD b/connections/implementation/BUILD index 4564f61e..308741e1 100644 --- a/connections/implementation/BUILD +++ b/connections/implementation/BUILD @@ -56,8 +56,8 @@ cc_library( "base_bwu_handler.cc", "base_endpoint_channel.cc", "base_pcp_handler.cc", + "ble_endpoint_channel.cc", "ble_l2cap_endpoint_channel.cc", - "ble_v2_endpoint_channel.cc", "bluetooth_bwu_handler.cc", "bluetooth_device_name.cc", "bluetooth_endpoint_channel.cc", @@ -97,8 +97,8 @@ cc_library( "base_bwu_handler.h", "base_endpoint_channel.h", "base_pcp_handler.h", + "ble_endpoint_channel.h", "ble_l2cap_endpoint_channel.h", - "ble_v2_endpoint_channel.h", "bluetooth_bwu_handler.h", "bluetooth_device_name.h", "bluetooth_endpoint_channel.h", @@ -160,7 +160,7 @@ cc_library( "//connections/implementation/mediums:webrtc_utils", "//connections/implementation/mediums/advertisements:dct_advertisement", "//connections/implementation/mediums/advertisements:util", - "//connections/implementation/mediums/ble_v2:ble_advertisement_header", + "//connections/implementation/mediums/ble:ble_advertisement_header", "//connections/implementation/proto:offline_wire_formats_cc_proto", "//connections/v3:v3_types", "//internal/analytics:event_logger", diff --git a/connections/implementation/base_pcp_handler.cc b/connections/implementation/base_pcp_handler.cc index 5418f4f2..c2a52ba4 100644 --- a/connections/implementation/base_pcp_handler.cc +++ b/connections/implementation/base_pcp_handler.cc @@ -1159,7 +1159,7 @@ void BasePcpHandler::StripOutUnavailableMediums( allowed.bluetooth = mediums_->GetBluetoothClassic().IsAvailable(); } if (allowed.ble) { - allowed.ble = mediums_->GetBleV2().IsAvailable(); + allowed.ble = mediums_->GetBle().IsAvailable(); } if (allowed.web_rtc) { allowed.web_rtc = mediums_->GetWebRtc().IsAvailable(); @@ -1206,7 +1206,7 @@ void BasePcpHandler::StripOutUnavailableMediums( allowed.bluetooth = mediums_->GetBluetoothClassic().IsAvailable(); } if (allowed.ble) { - allowed.ble = mediums_->GetBleV2().IsAvailable(); + allowed.ble = mediums_->GetBle().IsAvailable(); } if (allowed.web_rtc) { allowed.web_rtc = mediums_->GetWebRtc().IsAvailable(); diff --git a/connections/implementation/base_pcp_handler.h b/connections/implementation/base_pcp_handler.h index 6403882b..7a83de3d 100644 --- a/connections/implementation/base_pcp_handler.h +++ b/connections/implementation/base_pcp_handler.h @@ -55,7 +55,7 @@ #include "internal/interop/device.h" #include "internal/interop/device_provider.h" #include "internal/platform/atomic_boolean.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancelable_alarm.h" @@ -236,12 +236,12 @@ class BasePcpHandler : public PcpHandler, BluetoothDevice bluetooth_device; }; - struct BleV2Endpoint : public BasePcpHandler::DiscoveredEndpoint { - BleV2Endpoint(DiscoveredEndpoint endpoint, BleV2Peripheral peripheral) + struct BleEndpoint : public BasePcpHandler::DiscoveredEndpoint { + BleEndpoint(DiscoveredEndpoint endpoint, BlePeripheral peripheral) : DiscoveredEndpoint(std::move(endpoint)), ble_peripheral(std::move(peripheral)) {} - BleV2Peripheral ble_peripheral; + BlePeripheral ble_peripheral; }; struct AwdlEndpoint : public DiscoveredEndpoint { @@ -561,8 +561,7 @@ class BasePcpHandler : public PcpHandler, // address is created and appended into discovered_endpoints_ with key // endpoint_id. bool AppendRemoteBluetoothMacAddressEndpoint( - const std::string& endpoint_id, - MacAddress remote_bluetooth_mac_address, + const std::string& endpoint_id, MacAddress remote_bluetooth_mac_address, const DiscoveryOptions& local_discovery_options) ABSL_LOCKS_EXCLUDED(discovered_endpoint_mutex_); diff --git a/connections/implementation/ble_v2_endpoint_channel.cc b/connections/implementation/ble_endpoint_channel.cc similarity index 73% rename from connections/implementation/ble_v2_endpoint_channel.cc rename to connections/implementation/ble_endpoint_channel.cc index 9a36d761..c8752a10 100644 --- a/connections/implementation/ble_v2_endpoint_channel.cc +++ b/connections/implementation/ble_endpoint_channel.cc @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/ble_v2_endpoint_channel.h" +#include "connections/implementation/ble_endpoint_channel.h" #include #include #include "connections/implementation/base_endpoint_channel.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/exception.h" #include "internal/platform/input_stream.h" #include "internal/platform/logging.h" @@ -29,14 +29,14 @@ namespace connections { namespace { -OutputStream* GetOutputStreamOrNull(BleV2Socket& socket) { +OutputStream* GetOutputStreamOrNull(BleSocket& socket) { if (socket.IsValid()) { return &socket.GetOutputStream(); } return nullptr; } -InputStream* GetInputStreamOrNull(BleV2Socket& socket) { +InputStream* GetInputStreamOrNull(BleSocket& socket) { if (socket.IsValid()) { return &socket.GetInputStream(); } @@ -45,24 +45,24 @@ InputStream* GetInputStreamOrNull(BleV2Socket& socket) { } // namespace -BleV2EndpointChannel::BleV2EndpointChannel(const std::string& service_id, - const std::string& channel_name, - BleV2Socket socket) +BleEndpointChannel::BleEndpointChannel(const std::string& service_id, + const std::string& channel_name, + BleSocket socket) : BaseEndpointChannel(service_id, channel_name, GetInputStreamOrNull(socket), GetOutputStreamOrNull(socket)), ble_socket_(std::move(socket)) {} -location::nearby::proto::connections::Medium BleV2EndpointChannel::GetMedium() +location::nearby::proto::connections::Medium BleEndpointChannel::GetMedium() const { return location::nearby::proto::connections::Medium::BLE; } -int BleV2EndpointChannel::GetMaxTransmitPacketSize() const { +int BleEndpointChannel::GetMaxTransmitPacketSize() const { return kDefaultBleMaxTransmitPacketSize; } -void BleV2EndpointChannel::CloseImpl() { +void BleEndpointChannel::CloseImpl() { Exception status = ble_socket_.Close(); if (!status.Ok()) { LOG(WARNING) << "Failed to close underlying socket for BleEndpointChannel " diff --git a/connections/implementation/ble_v2_endpoint_channel.h b/connections/implementation/ble_endpoint_channel.h similarity index 70% rename from connections/implementation/ble_v2_endpoint_channel.h rename to connections/implementation/ble_endpoint_channel.h index f92cd10d..1ad8e964 100644 --- a/connections/implementation/ble_v2_endpoint_channel.h +++ b/connections/implementation/ble_endpoint_channel.h @@ -12,22 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CONNECTIONS_IMPLEMENTATION_BLE_V2_ENDPOINT_CHANNEL_H_ -#define CONNECTIONS_IMPLEMENTATION_BLE_V2_ENDPOINT_CHANNEL_H_ +#ifndef CONNECTIONS_IMPLEMENTATION_BLE_ENDPOINT_CHANNEL_H_ +#define CONNECTIONS_IMPLEMENTATION_BLE_ENDPOINT_CHANNEL_H_ #include #include "connections/implementation/base_endpoint_channel.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" namespace nearby { namespace connections { -class BleV2EndpointChannel final : public BaseEndpointChannel { +class BleEndpointChannel final : public BaseEndpointChannel { public: // Creates both outgoing and incoming Ble channels. - BleV2EndpointChannel(const std::string& service_id, - const std::string& channel_name, BleV2Socket socket); + BleEndpointChannel(const std::string& service_id, + const std::string& channel_name, BleSocket socket); location::nearby::proto::connections::Medium GetMedium() const override; @@ -38,10 +38,10 @@ class BleV2EndpointChannel final : public BaseEndpointChannel { void CloseImpl() override; - BleV2Socket ble_socket_; + BleSocket ble_socket_; }; } // namespace connections } // namespace nearby -#endif // CONNECTIONS_IMPLEMENTATION_BLE_V2_ENDPOINT_CHANNEL_H_ +#endif // CONNECTIONS_IMPLEMENTATION_BLE_ENDPOINT_CHANNEL_H_ diff --git a/connections/implementation/ble_l2cap_endpoint_channel.cc b/connections/implementation/ble_l2cap_endpoint_channel.cc index 3a8b220f..1dc124ea 100644 --- a/connections/implementation/ble_l2cap_endpoint_channel.cc +++ b/connections/implementation/ble_l2cap_endpoint_channel.cc @@ -18,7 +18,7 @@ #include #include "connections/implementation/base_endpoint_channel.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/exception.h" #include "internal/platform/input_stream.h" #include "internal/platform/logging.h" diff --git a/connections/implementation/ble_l2cap_endpoint_channel.h b/connections/implementation/ble_l2cap_endpoint_channel.h index 1211578c..bb6ec665 100644 --- a/connections/implementation/ble_l2cap_endpoint_channel.h +++ b/connections/implementation/ble_l2cap_endpoint_channel.h @@ -18,7 +18,7 @@ #include #include "connections/implementation/base_endpoint_channel.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" namespace nearby { namespace connections { diff --git a/connections/implementation/bwu_manager.cc b/connections/implementation/bwu_manager.cc index f20c514c..14c87503 100644 --- a/connections/implementation/bwu_manager.cc +++ b/connections/implementation/bwu_manager.cc @@ -977,11 +977,11 @@ BwuManager::ProcessBwuPathAvailableEventInternal( location::nearby::connections::OsInfo::APPLE && old_medium == Medium::BLE && medium == Medium::WIFI_HOTSPOT) { disable_ble_scanning = true; - LOG(INFO) << "For Apple OS, if upgrade from BLE_V2 to WIFI_HOTSPOT, " + LOG(INFO) << "For Apple OS, if upgrade from BLE to WIFI_HOTSPOT, " "we need to pause " - "BLE_V2 scanning because it can interfere with WIFI " + "BLE scanning because it can interfere with WIFI " "Hotspot scanning and connection."; - ble_v2_medium_.PauseMediumScanning(); + ble_medium_.PauseMediumScanning(); } } @@ -993,8 +993,8 @@ BwuManager::ProcessBwuPathAvailableEventInternal( config_package_nearby::nearby_connections_feature:: kEnableStopBleScanningOnWifiUpgrade)) { if (disable_ble_scanning) { - LOG(INFO) << "Resume BLE_V2 scanning."; - ble_v2_medium_.ResumeMediumScanning(); + LOG(INFO) << "Resume BLE scanning."; + ble_medium_.ResumeMediumScanning(); } } diff --git a/connections/implementation/bwu_manager.h b/connections/implementation/bwu_manager.h index af154f46..15e66f4e 100644 --- a/connections/implementation/bwu_manager.h +++ b/connections/implementation/bwu_manager.h @@ -28,7 +28,7 @@ #include "connections/implementation/endpoint_channel.h" #include "connections/implementation/endpoint_channel_manager.h" #include "connections/implementation/endpoint_manager.h" -#include "connections/implementation/mediums/ble_v2.h" +#include "connections/implementation/mediums/ble.h" #include "connections/implementation/mediums/mediums.h" #include "connections/medium_selector.h" #include "internal/platform/cancelable_alarm.h" @@ -234,7 +234,7 @@ class BwuManager : public EndpointManager::FrameProcessor { Mediums* mediums_; absl::flat_hash_map> handlers_; - BleV2& ble_v2_medium_{mediums_->GetBleV2()}; + Ble& ble_medium_{mediums_->GetBle()}; EndpointManager* endpoint_manager_; EndpointChannelManager* channel_manager_; diff --git a/connections/implementation/mediums/BUILD b/connections/implementation/mediums/BUILD index a666c989..e73caa5e 100644 --- a/connections/implementation/mediums/BUILD +++ b/connections/implementation/mediums/BUILD @@ -21,7 +21,7 @@ cc_library( name = "mediums", srcs = [ "awdl.cc", - "ble_v2.cc", + "ble.cc", "bluetooth_classic.cc", "bluetooth_radio.cc", "mediums.cc", @@ -33,7 +33,7 @@ cc_library( ], hdrs = [ "awdl.h", - "ble_v2.h", + "ble.h", "bluetooth_classic.h", "bluetooth_radio.h", "mediums.h", @@ -54,9 +54,9 @@ cc_library( "//connections:core_types", "//connections/implementation:types", "//connections/implementation/flags:connections_flags", - "//connections/implementation/mediums/ble_v2", - "//connections/implementation/mediums/ble_v2:ble_advertisement_header", - "//connections/implementation/mediums/ble_v2:bloom_filter", + "//connections/implementation/mediums/ble", + "//connections/implementation/mediums/ble:ble_advertisement_header", + "//connections/implementation/mediums/ble:bloom_filter", "//connections/implementation/mediums/multiplex", "//connections/implementation/mediums/webrtc", "//connections/implementation/proto:offline_wire_formats_cc_proto", @@ -115,7 +115,7 @@ cc_library( name = "lost_entity_tracker", hdrs = ["lost_entity_tracker.h"], visibility = [ - "//connections/implementation/mediums/ble_v2:__pkg__", + "//connections/implementation/mediums/ble:__pkg__", ], deps = [ "//internal/platform:types", @@ -131,7 +131,7 @@ cc_library( visibility = [ "//connections/implementation:__pkg__", "//connections/implementation/mediums/advertisements:__pkg__", - "//connections/implementation/mediums/ble_v2:__subpackages__", + "//connections/implementation/mediums/ble:__subpackages__", "//connections/implementation/mediums/multiplex:__pkg__", "//internal/platform/implementation/windows:__pkg__", ], @@ -146,7 +146,7 @@ cc_test( size = "small", srcs = [ "awdl_test.cc", - "ble_v2_test.cc", + "ble_test.cc", "bluetooth_classic_test.cc", "bluetooth_radio_test.cc", "lost_entity_tracker_test.cc", @@ -163,7 +163,7 @@ cc_test( "//connections:core_types", "//connections/implementation:types", "//connections/implementation/flags:connections_flags", - "//connections/implementation/mediums/ble_v2", + "//connections/implementation/mediums/ble", "//internal/flags:nearby_flags", "//internal/platform:base", "//internal/platform:cancellation_flag", diff --git a/connections/implementation/mediums/ble_v2.cc b/connections/implementation/mediums/ble.cc similarity index 89% rename from connections/implementation/mediums/ble_v2.cc rename to connections/implementation/mediums/ble.cc index c3b9125a..1686ebb7 100644 --- a/connections/implementation/mediums/ble_v2.cc +++ b/connections/implementation/mediums/ble.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2.h" +#include "connections/implementation/mediums/ble.h" #include #include @@ -32,24 +32,24 @@ #include "absl/time/time.h" #include "absl/types/optional.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h" -#include "connections/implementation/mediums/ble_v2/advertisement_read_result.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" -#include "connections/implementation/mediums/ble_v2/ble_utils.h" -#include "connections/implementation/mediums/ble_v2/bloom_filter.h" -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h" +#include "connections/implementation/mediums/ble/advertisement_read_result.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_utils.h" +#include "connections/implementation/mediums/ble/bloom_filter.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_tracker.h" #include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/mediums/utils.h" #include "connections/implementation/pcp.h" #include "connections/power_level.h" #include "internal/flags/nearby_flags.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancelable_alarm.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/expected.h" #include "internal/platform/feature_flags.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/platform.h" #include "internal/platform/logging.h" #include "internal/platform/mutex.h" @@ -63,9 +63,9 @@ namespace connections { namespace { using ::location::nearby::proto::connections::OperationResultCode; -using ::nearby::api::ble_v2::BleAdvertisementData; -using ::nearby::api::ble_v2::GattCharacteristic; -using ::nearby::api::ble_v2::TxPowerLevel; +using ::nearby::api::ble::BleAdvertisementData; +using ::nearby::api::ble::GattCharacteristic; +using ::nearby::api::ble::TxPowerLevel; constexpr int kMaxAdvertisementLength = 512; constexpr int kDummyServiceIdLength = 128; @@ -76,10 +76,10 @@ void AssumeHeld(Mutex& m) ABSL_ASSERT_EXCLUSIVE_LOCK(m) {} } // namespace -BleV2::BleV2(BluetoothRadio& radio) +Ble::Ble(BluetoothRadio& radio) : radio_(radio), adapter_(radio_.GetBluetoothAdapter()) {} -BleV2::~BleV2() { +Ble::~Ble() { // Destructor is not taking locks, but methods it is calling are. if (FeatureFlags::GetInstance().GetFlags().enable_ble_v2_async_scanning) { // If using asynchronous scanning, check the corresponding map. @@ -115,16 +115,16 @@ BleV2::~BleV2() { } } -bool BleV2::IsAvailable() const { +bool Ble::IsAvailable() const { MutexLock lock(&mutex_); return IsAvailableLocked(); } -ErrorOr BleV2::StartAdvertising(const std::string& service_id, - PowerLevel power_level, - AdvertisingType advertising_type, - const ByteArray& advertisement_bytes) { +ErrorOr Ble::StartAdvertising(const std::string& service_id, + PowerLevel power_level, + AdvertisingType advertising_type, + const ByteArray& advertisement_bytes) { MutexLock lock(&mutex_); if (advertisement_bytes.Empty()) { @@ -222,7 +222,7 @@ ErrorOr BleV2::StartAdvertising(const std::string& service_id, return {true}; } -bool BleV2::StopAdvertising(const std::string& service_id) { +bool Ble::StopAdvertising(const std::string& service_id) { MutexLock lock(&mutex_); if (!IsAdvertisingLocked(service_id)) { LOG(INFO) << "Cannot stop BLE advertising for service_id=" << service_id @@ -285,19 +285,19 @@ bool BleV2::StopAdvertising(const std::string& service_id) { return true; } -bool BleV2::IsAdvertising(const std::string& service_id) const { +bool Ble::IsAdvertising(const std::string& service_id) const { MutexLock lock(&mutex_); return IsAdvertisingLocked(service_id); } -bool BleV2::IsAdvertisingForLegacyDevice(const std::string& service_id) const { +bool Ble::IsAdvertisingForLegacyDevice(const std::string& service_id) const { MutexLock lock(&mutex_); return IsAdvertisingForLegacyDeviceLocked(service_id); } -ErrorOr BleV2::StartLegacyAdvertising( +ErrorOr Ble::StartLegacyAdvertising( const std::string& input_service_id, const std::string& local_endpoint_id, const std::string& fast_advertisement_service_uuid) { LOG(INFO) << "StartLegacyAdvertising: " << input_service_id @@ -305,13 +305,12 @@ ErrorOr BleV2::StartLegacyAdvertising( MutexLock lock(&mutex_); if (!radio_.IsEnabled()) { - LOG(INFO) << "Can't start BLE v2 legacy advertising because " + LOG(INFO) << "Can't start BLE legacy advertising because " "Bluetooth was never turned on"; return {Error(OperationResultCode::MISCELLEANEOUS_BT_SYSTEM_SERVICE_NULL)}; } if (!IsAvailableLocked()) { - LOG(INFO) - << "Can't turn on BLE v2 legacy advertising. BLE is not available."; + LOG(INFO) << "Can't turn on BLE legacy advertising. BLE is not available."; return {Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLE_NOT_AVAILABLE)}; } if (medium_.IsExtendedAdvertisementsAvailable()) { @@ -325,21 +324,20 @@ ErrorOr BleV2::StartLegacyAdvertising( return {Error(OperationResultCode::CLIENT_BLE_DUPLICATE_ADVERTISING)}; } - std::unique_ptr + std::unique_ptr legacy_device_advertizing_session = medium_.StartAdvertising( CreateAdvertisingDataForLegacyDevice(), {.tx_power_level = TxPowerLevel::kMedium, .is_connectable = true}, - api::ble_v2::BleMedium::AdvertisingCallback{ + api::ble::BleMedium::AdvertisingCallback{ .start_advertising_result = [this, &service_id](absl::Status status) mutable { AssumeHeld(mutex_); if (status.ok()) { - LOG(INFO) - << "BLE V2 advertising for legacy device started " - "successfully for service ID " - << &service_id; + LOG(INFO) << "BLE advertising for legacy device started " + "successfully for service ID " + << &service_id; } else { - LOG(ERROR) << "BLE V2 advertising for legacy " + LOG(ERROR) << "BLE advertising for legacy " "device failed for service ID " << &service_id << ": " << status; service_ids_to_advertising_sessions_.erase(service_id); @@ -347,7 +345,7 @@ ErrorOr BleV2::StartLegacyAdvertising( }, }); if (legacy_device_advertizing_session == nullptr) { - LOG(ERROR) << "Failed to turn on BLE v2 advertising for legacy " + LOG(ERROR) << "Failed to turn on BLE advertising for legacy " "device for service ID " << service_id; return { @@ -359,7 +357,7 @@ ErrorOr BleV2::StartLegacyAdvertising( return {true}; } -bool BleV2::StopLegacyAdvertising(const std::string& input_service_id) { +bool Ble::StopLegacyAdvertising(const std::string& input_service_id) { LOG(INFO) << "StopLegacyAdvertising:" << input_service_id; MutexLock lock(&mutex_); @@ -384,16 +382,16 @@ bool BleV2::StopLegacyAdvertising(const std::string& input_service_id) { return status.ok(); } -void BleV2::AddAlternateUuidForService(uint16_t uuid, - const std::string& service_id) { +void Ble::AddAlternateUuidForService(uint16_t uuid, + const std::string& service_id) { MutexLock lock(&mutex_); medium_.AddAlternateUuidForService(uuid, service_id); } -ErrorOr BleV2::StartScanning(const std::string& service_id, Pcp pcp, - PowerLevel power_level, - bool include_dct_advertisement, - DiscoveredPeripheralCallback callback) { +ErrorOr Ble::StartScanning(const std::string& service_id, Pcp pcp, + PowerLevel power_level, + bool include_dct_advertisement, + DiscoveredPeripheralCallback callback) { MutexLock lock(&mutex_); if (service_id.empty()) { @@ -446,7 +444,7 @@ ErrorOr BleV2::StartScanning(const std::string& service_id, Pcp pcp, service_uuids, PowerLevelToTxPowerLevel(power_level), { .advertisement_found_cb = - [this](BleV2Peripheral peripheral, + [this](BlePeripheral peripheral, BleAdvertisementData advertisement_data) { RunOnBleThread([this, peripheral = std::move(peripheral), advertisement_data]() { @@ -454,8 +452,8 @@ ErrorOr BleV2::StartScanning(const std::string& service_id, Pcp pcp, discovered_peripheral_tracker_ .ProcessFoundBleAdvertisement( std::move(peripheral), advertisement_data, - [this](BleV2Peripheral peripheral, - int num_slots, int psm, + [this](BlePeripheral peripheral, int num_slots, + int psm, const std::vector& interesting_service_ids, mediums::AdvertisementReadResult& @@ -487,7 +485,7 @@ ErrorOr BleV2::StartScanning(const std::string& service_id, Pcp pcp, PowerLevelToTxPowerLevel(power_level), { .advertisement_found_cb = - [this](BleV2Peripheral peripheral, + [this](BlePeripheral peripheral, BleAdvertisementData advertisement_data) { RunOnBleThread([this, peripheral = std::move(peripheral), advertisement_data]() { @@ -495,8 +493,8 @@ ErrorOr BleV2::StartScanning(const std::string& service_id, Pcp pcp, discovered_peripheral_tracker_ .ProcessFoundBleAdvertisement( std::move(peripheral), advertisement_data, - [this](BleV2Peripheral peripheral, - int num_slots, int psm, + [this](BlePeripheral peripheral, int num_slots, + int psm, const std::vector& interesting_service_ids, mediums::AdvertisementReadResult& @@ -540,7 +538,7 @@ ErrorOr BleV2::StartScanning(const std::string& service_id, Pcp pcp, return {true}; } -bool BleV2::StopScanning(const std::string& service_id) { +bool Ble::StopScanning(const std::string& service_id) { MutexLock lock(&mutex_); if (FeatureFlags::GetInstance().GetFlags().enable_ble_v2_async_scanning) { return StopAsyncScanningLocked(service_id); @@ -570,23 +568,23 @@ bool BleV2::StopScanning(const std::string& service_id) { return medium_.StopScanning(); } -bool BleV2::PauseMediumScanning() { +bool Ble::PauseMediumScanning() { MutexLock lock(&mutex_); return medium_.PauseMediumScanning(); } -bool BleV2::ResumeMediumScanning() { +bool Ble::ResumeMediumScanning() { MutexLock lock(&mutex_); return medium_.ResumeMediumScanning(); } -bool BleV2::IsScanning(const std::string& service_id) const { +bool Ble::IsScanning(const std::string& service_id) const { MutexLock lock(&mutex_); return IsScanningLocked(service_id); } -ErrorOr BleV2::StartAcceptingConnections( +ErrorOr Ble::StartAcceptingConnections( const std::string& service_id, AcceptedConnectionCallback callback) { MutexLock lock(&mutex_); @@ -616,7 +614,7 @@ ErrorOr BleV2::StartAcceptingConnections( return {Error(OperationResultCode::MEDIUM_UNAVAILABLE_BLE_NOT_AVAILABLE)}; } - BleV2ServerSocket server_socket = medium_.OpenServerSocket(service_id); + BleServerSocket server_socket = medium_.OpenServerSocket(service_id); if (!server_socket.IsValid()) { LOG(ERROR) << "Failed to start accepting Ble GATT connections for service_id=" @@ -633,11 +631,10 @@ ErrorOr BleV2::StartAcceptingConnections( // listening for new incoming connections until // StopAcceptingL2capConnections() is invoked. accept_loops_runner_.Execute( - "ble-accept", - [this, service_id, callback = std::move(callback), - server_socket = std::move(owned_server_socket)]() mutable { + "ble-accept", [this, service_id, callback = std::move(callback), + server_socket = std::move(owned_server_socket)]() mutable { while (true) { - BleV2Socket client_socket = server_socket.Accept(); + BleSocket client_socket = server_socket.Accept(); if (!client_socket.IsValid()) { LOG(WARNING) << "The client Ble GATT socket to accept is invalid."; server_socket.Close(); @@ -663,7 +660,7 @@ ErrorOr BleV2::StartAcceptingConnections( } // TODO(mingshiouwu): Add unit test for ble_l2cap flow -ErrorOr BleV2::StartAcceptingL2capConnections( +ErrorOr Ble::StartAcceptingL2capConnections( const std::string& service_id, AcceptedL2capConnectionCallback callback) { MutexLock lock(&mutex_); if (service_id.empty()) { @@ -746,7 +743,7 @@ ErrorOr BleV2::StartAcceptingL2capConnections( return {psm}; } -bool BleV2::StopAcceptingConnections(const std::string& service_id) { +bool Ble::StopAcceptingConnections(const std::string& service_id) { MutexLock lock(&mutex_); const auto it = server_sockets_.find(service_id); @@ -765,7 +762,7 @@ bool BleV2::StopAcceptingConnections(const std::string& service_id) { // Store a handle to the BleServerSocket, so we can use it after // removing the entry from server_sockets_; making it scoped // is a bonus that takes care of deallocation before we leave this method. - BleV2ServerSocket& listening_socket = item.mapped(); + BleServerSocket& listening_socket = item.mapped(); // Regardless of whether or not we fail to close the existing // BleServerSocket, remove it from server_sockets_ so that it @@ -781,7 +778,7 @@ bool BleV2::StopAcceptingConnections(const std::string& service_id) { return true; } -bool BleV2::StopAcceptingL2capConnections(const std::string& service_id) { +bool Ble::StopAcceptingL2capConnections(const std::string& service_id) { MutexLock lock(&mutex_); const auto it = l2cap_server_sockets_.find(service_id); @@ -816,22 +813,22 @@ bool BleV2::StopAcceptingL2capConnections(const std::string& service_id) { return true; } -bool BleV2::IsAcceptingConnections(const std::string& service_id) { +bool Ble::IsAcceptingConnections(const std::string& service_id) { MutexLock lock(&mutex_); return IsAcceptingConnectionsLocked(service_id); } -bool BleV2::IsAcceptingL2capConnections(const std::string& service_id) { +bool Ble::IsAcceptingL2capConnections(const std::string& service_id) { MutexLock lock(&mutex_); return IsAcceptingL2capConnectionsLocked(service_id); } -ErrorOr BleV2::Connect(const std::string& service_id, - const BleV2Peripheral& peripheral, - CancellationFlag* cancellation_flag) { +ErrorOr Ble::Connect(const std::string& service_id, + const BlePeripheral& peripheral, + CancellationFlag* cancellation_flag) { MutexLock lock(&mutex_); // Socket to return. To allow for NRVO to work, it has to be a single object. - BleV2Socket socket; + BleSocket socket; if (service_id.empty()) { LOG(INFO) << "Refusing to create client Ble socket because " @@ -863,8 +860,8 @@ ErrorOr BleV2::Connect(const std::string& service_id, return socket; } -ErrorOr BleV2::ConnectOverL2cap( - const std::string& service_id, const BleV2Peripheral& peripheral, +ErrorOr Ble::ConnectOverL2cap( + const std::string& service_id, const BlePeripheral& peripheral, CancellationFlag* cancellation_flag) { MutexLock lock(&mutex_); @@ -900,18 +897,18 @@ ErrorOr BleV2::ConnectOverL2cap( return socket; } -bool BleV2::IsAvailableLocked() const { return medium_.IsValid(); } +bool Ble::IsAvailableLocked() const { return medium_.IsValid(); } -bool BleV2::IsAdvertisingLocked(const std::string& service_id) const { +bool Ble::IsAdvertisingLocked(const std::string& service_id) const { return advertising_infos_.contains(service_id); } -bool BleV2::IsAdvertisingForLegacyDeviceLocked( +bool Ble::IsAdvertisingForLegacyDeviceLocked( const std::string& service_id) const { return service_ids_to_advertising_sessions_.contains(service_id + "-Legacy"); } -bool BleV2::IsScanningLocked(const std::string& service_id) const { +bool Ble::IsScanningLocked(const std::string& service_id) const { if (FeatureFlags::GetInstance().GetFlags().enable_ble_v2_async_scanning) { // If using asynchronous scanning, check the corresponding map. auto it = service_ids_to_scanning_sessions_.find(service_id); @@ -920,19 +917,19 @@ bool BleV2::IsScanningLocked(const std::string& service_id) const { return scanned_service_ids_.contains(service_id); } -bool BleV2::IsAcceptingConnectionsLocked(const std::string& service_id) { +bool Ble::IsAcceptingConnectionsLocked(const std::string& service_id) { return server_sockets_.contains(service_id); } -bool BleV2::IsAcceptingL2capConnectionsLocked(const std::string& service_id) { +bool Ble::IsAcceptingL2capConnectionsLocked(const std::string& service_id) { return l2cap_server_sockets_.contains(service_id); } -bool BleV2::IsAdvertisementGattServerRunningLocked() { +bool Ble::IsAdvertisementGattServerRunningLocked() { return gatt_server_ && gatt_server_->IsValid(); } -bool BleV2::StartAdvertisementGattServerLocked( +bool Ble::StartAdvertisementGattServerLocked( const std::string& service_id, const ByteArray& gatt_advertisement) { if (IsAdvertisementGattServerRunningLocked()) { LOG(INFO) << "Advertisement GATT server is not started because one " @@ -961,7 +958,7 @@ bool BleV2::StartAdvertisementGattServerLocked( return true; } -bool BleV2::GenerateAdvertisementCharacteristic( +bool Ble::GenerateAdvertisementCharacteristic( int slot, const ByteArray& gatt_advertisement, GattServer& gatt_server) { GattCharacteristic::Permission permission = GattCharacteristic::Permission::kRead; @@ -994,8 +991,8 @@ bool BleV2::GenerateAdvertisementCharacteristic( return true; } -void BleV2::ProcessFetchGattAdvertisementsRequest( - BleV2Peripheral peripheral, int num_slots, int psm, +void Ble::ProcessFetchGattAdvertisementsRequest( + BlePeripheral peripheral, int num_slots, int psm, const std::vector& interesting_service_ids, mediums::AdvertisementReadResult& advertisement_read_result) { if (!peripheral.IsValid()) { @@ -1105,7 +1102,7 @@ void BleV2::ProcessFetchGattAdvertisementsRequest( advertisement_read_result.RecordLastReadStatus(read_success); } -bool BleV2::StopAdvertisementGattServerLocked() { +bool Ble::StopAdvertisementGattServerLocked() { if (!IsAdvertisementGattServerRunningLocked()) { VLOG(1) << "Unable to stop the advertisement GATT server because it's not " "running."; @@ -1116,7 +1113,7 @@ bool BleV2::StopAdvertisementGattServerLocked() { return true; } -ByteArray BleV2::CreateAdvertisementHeader( +ByteArray Ble::CreateAdvertisementHeader( int psm, bool extended_advertisement_advertised) { // Create a randomized dummy service id to anonymize the header with. ByteArray dummy_service_id_bytes = @@ -1150,8 +1147,7 @@ ByteArray BleV2::CreateAdvertisementHeader( advertisement_hash, psm)); } -api::ble_v2::BleAdvertisementData -BleV2::CreateAdvertisingDataForLegacyDevice() { +api::ble::BleAdvertisementData Ble::CreateAdvertisingDataForLegacyDevice() { BleAdvertisementData advertising_data; advertising_data.is_extended_advertisement = false; @@ -1163,7 +1159,7 @@ BleV2::CreateAdvertisingDataForLegacyDevice() { return advertising_data; } -bool BleV2::StartAdvertisingLocked(const std::string& service_id) { +bool Ble::StartAdvertisingLocked(const std::string& service_id) { const auto it = advertising_infos_.find(service_id); if (it == advertising_infos_.end()) { LOG(WARNING) << "Failed to BLE advertise with service_id=" << service_id; @@ -1180,7 +1176,7 @@ bool BleV2::StartAdvertisingLocked(const std::string& service_id) { } } -bool BleV2::StartFastAdvertisingLocked( +bool Ble::StartFastAdvertisingLocked( const std::string& service_id, PowerLevel power_level, const mediums::BleAdvertisement& medium_advertisement) { // Begin building the fast BLE advertisement. @@ -1221,7 +1217,7 @@ bool BleV2::StartFastAdvertisingLocked( return true; } -bool BleV2::StartRegularAdvertisingLocked( +bool Ble::StartRegularAdvertisingLocked( const std::string& service_id, PowerLevel power_level, const mediums::BleAdvertisement& medium_advertisement) { // Begin building the regular BLE advertisement. @@ -1269,7 +1265,7 @@ bool BleV2::StartRegularAdvertisingLocked( return extended_regular_advertisement_success || gatt_advertisement_success; } -bool BleV2::StartGattAdvertisingLocked( +bool Ble::StartGattAdvertisingLocked( const std::string& service_id, PowerLevel power_level, int psm, const ByteArray& medium_advertisement_bytes, bool extended_advertisement_advertised) { @@ -1349,9 +1345,9 @@ bool BleV2::StartGattAdvertisingLocked( return true; } -bool BleV2::StartDctAdvertisingLocked(const std::string& service_id, - PowerLevel power_level, - const ByteArray& dct_advertisement) { +bool Ble::StartDctAdvertisingLocked(const std::string& service_id, + PowerLevel power_level, + const ByteArray& dct_advertisement) { BleAdvertisementData advertising_data; advertising_data.is_extended_advertisement = false; advertising_data.service_data.insert( @@ -1363,8 +1359,8 @@ bool BleV2::StartDctAdvertisingLocked(const std::string& service_id, .is_connectable = false}); } -bool BleV2::StartAsyncScanningLocked(absl::string_view service_id, - PowerLevel power_level) { +bool Ble::StartAsyncScanningLocked(absl::string_view service_id, + PowerLevel power_level) { CHECK(FeatureFlags::GetInstance().GetFlags().enable_ble_v2_async_scanning); // Use the asynchronous StartScanning method instead of the synchronous one. @@ -1377,7 +1373,7 @@ bool BleV2::StartAsyncScanningLocked(absl::string_view service_id, auto scanning_session = medium_.StartScanning( mediums::bleutils::kCopresenceServiceUuid, PowerLevelToTxPowerLevel(power_level), - api::ble_v2::BleMedium::ScanningCallback{ + api::ble::BleMedium::ScanningCallback{ .start_scanning_result = [this, &service_id](absl::Status status) mutable { // The `mutex_` is already held here. Use @@ -1389,27 +1385,27 @@ bool BleV2::StartAsyncScanningLocked(absl::string_view service_id, // attempted to acquire the lock here I hit a deadlock. AssumeHeld(mutex_); if (status.ok()) { - LOG(INFO) << "BLE V2 async StartScanning started " + LOG(INFO) << "BLE async StartScanning started " "successfully for service ID" << &service_id; } else { - LOG(ERROR) << "BLE V2 async StartScanning " + LOG(ERROR) << "BLE async StartScanning " "failed for service ID" << &service_id << ": " << status; service_ids_to_scanning_sessions_.erase(service_id); } }, .advertisement_found_cb = - [this](api::ble_v2::BlePeripheral::UniqueId peripheral_id, + [this](api::ble::BlePeripheral::UniqueId peripheral_id, BleAdvertisementData advertisement_data) { AssumeHeld(mutex_); - BleV2Peripheral proxy(medium_, peripheral_id); + BlePeripheral proxy(medium_, peripheral_id); RunOnBleThread([this, proxy = std::move(proxy), advertisement_data]() { MutexLock lock(&mutex_); discovered_peripheral_tracker_.ProcessFoundBleAdvertisement( std::move(proxy), advertisement_data, - [this](BleV2Peripheral proxy, int num_slots, int psm, + [this](BlePeripheral proxy, int num_slots, int psm, const std::vector& interesting_service_ids, mediums::AdvertisementReadResult& @@ -1427,7 +1423,7 @@ bool BleV2::StartAsyncScanningLocked(absl::string_view service_id, }); }, .advertisement_lost_cb = - [](api::ble_v2::BlePeripheral::UniqueId peripheral_id) { + [](api::ble::BlePeripheral::UniqueId peripheral_id) { // TODO(b/345514862): Implement. }, }); @@ -1457,7 +1453,7 @@ bool BleV2::StartAsyncScanningLocked(absl::string_view service_id, return true; } -bool BleV2::StopAsyncScanningLocked(absl::string_view service_id) { +bool Ble::StopAsyncScanningLocked(absl::string_view service_id) { CHECK(FeatureFlags::GetInstance().GetFlags().enable_ble_v2_async_scanning); // If using asynchronous scanning, check the corresponding map. auto scanning_session = service_ids_to_scanning_sessions_.find(service_id); @@ -1479,7 +1475,7 @@ bool BleV2::StopAsyncScanningLocked(absl::string_view service_id) { return true; } -TxPowerLevel BleV2::PowerLevelToTxPowerLevel(PowerLevel power_level) { +TxPowerLevel Ble::PowerLevelToTxPowerLevel(PowerLevel power_level) { switch (power_level) { case PowerLevel::kHighPower: return TxPowerLevel::kHigh; @@ -1492,7 +1488,7 @@ TxPowerLevel BleV2::PowerLevelToTxPowerLevel(PowerLevel power_level) { } } -void BleV2::RunOnBleThread(Runnable runnable) { +void Ble::RunOnBleThread(Runnable runnable) { serial_executor_.Execute(std::move(runnable)); } diff --git a/connections/implementation/mediums/ble_v2.h b/connections/implementation/mediums/ble.h similarity index 88% rename from connections/implementation/mediums/ble_v2.h rename to connections/implementation/mediums/ble.h index 263033d9..793cac0b 100644 --- a/connections/implementation/mediums/ble_v2.h +++ b/connections/implementation/mediums/ble.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_H_ #include #include @@ -27,21 +27,21 @@ #include "absl/container/flat_hash_set.h" #include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" -#include "connections/implementation/mediums/ble_v2/advertisement_read_result.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h" -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h" -#include "connections/implementation/mediums/ble_v2/instant_on_lost_manager.h" +#include "connections/implementation/mediums/ble/advertisement_read_result.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_callback.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_tracker.h" +#include "connections/implementation/mediums/ble/instant_on_lost_manager.h" #include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/pcp.h" #include "connections/power_level.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancelable_alarm.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/expected.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/multi_thread_executor.h" #include "internal/platform/mutex.h" #include "internal/platform/mutex_lock.h" @@ -54,13 +54,13 @@ namespace connections { // Provides the operations that can be performed on the Bluetooth Low Energy // (BLE) medium. -class BleV2 final { +class Ble final { public: using DiscoveredPeripheralCallback = mediums::DiscoveredPeripheralCallback; // Callback that is invoked when a new connection is accepted. - using AcceptedConnectionCallback = absl::AnyInvocable; + using AcceptedConnectionCallback = + absl::AnyInvocable; // Callback that is invoked when a new l2cap connection is accepted. using AcceptedL2capConnectionCallback = absl::AnyInvocable Connect(const std::string& service_id, - const BleV2Peripheral& peripheral, - CancellationFlag* cancellation_flag) + ErrorOr Connect(const std::string& service_id, + const BlePeripheral& peripheral, + CancellationFlag* cancellation_flag) ABSL_LOCKS_EXCLUDED(mutex_); // Establishes connection to Ble peripheral. // Returns socket instance. On success, BleSocket.IsValid() return true. ErrorOr ConnectOverL2cap(const std::string& service_id, - const BleV2Peripheral& peripheral, + const BlePeripheral& peripheral, CancellationFlag* cancellation_flag) ABSL_LOCKS_EXCLUDED(mutex_); @@ -251,7 +251,7 @@ class BleV2 final { GattServer& gatt_server) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); void ProcessFetchGattAdvertisementsRequest( - BleV2Peripheral peripheral, int num_slots, int psm, + BlePeripheral peripheral, int num_slots, int psm, const std::vector& interesting_service_ids, mediums::AdvertisementReadResult& advertisement_read_result) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); @@ -261,7 +261,7 @@ class BleV2 final { bool extended_advertisement_advertised) ABSL_SHARED_LOCKS_REQUIRED(mutex_); // For devices that don't have extended nor gatt adverting. - api::ble_v2::BleAdvertisementData CreateAdvertisingDataForLegacyDevice(); + api::ble::BleAdvertisementData CreateAdvertisingDataForLegacyDevice(); bool StartAdvertisingLocked(const std::string& service_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); bool StartFastAdvertisingLocked( @@ -288,7 +288,7 @@ class BleV2 final { // Called by StartScanning when using the async methods. bool StopAsyncScanningLocked(absl::string_view service_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); - api::ble_v2::TxPowerLevel PowerLevelToTxPowerLevel(PowerLevel power_level); + api::ble::TxPowerLevel PowerLevelToTxPowerLevel(PowerLevel power_level); void RunOnBleThread(Runnable runnable); static constexpr int kMaxConcurrentAcceptLoops = 5; @@ -299,24 +299,24 @@ class BleV2 final { mutable Mutex mutex_; BluetoothRadio& radio_ ABSL_GUARDED_BY(mutex_); BluetoothAdapter& adapter_ ABSL_GUARDED_BY(mutex_); - BleV2Medium medium_ ABSL_GUARDED_BY(mutex_){adapter_}; + BleMedium medium_ ABSL_GUARDED_BY(mutex_){adapter_}; absl::btree_map advertising_infos_ ABSL_GUARDED_BY(mutex_); std::unique_ptr gatt_server_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map> gatt_advertisements_ ABSL_GUARDED_BY(mutex_); - absl::flat_hash_set - hosted_gatt_characteristics_ ABSL_GUARDED_BY(mutex_); + absl::flat_hash_set hosted_gatt_characteristics_ + ABSL_GUARDED_BY(mutex_); absl::flat_hash_set scanned_service_ids_ ABSL_GUARDED_BY(mutex_); // This map has the same purpose as the set above, but is used only by // the async StartScanning method. absl::flat_hash_map> + std::unique_ptr> service_ids_to_scanning_sessions_ ABSL_GUARDED_BY(mutex_); // Save advertising sessions by service id, used by the async StartAdvertising // method. - absl::flat_hash_map< - std::string, std::unique_ptr> + absl::flat_hash_map> service_ids_to_advertising_sessions_ ABSL_GUARDED_BY(mutex_); std::unique_ptr lost_alarm_; mediums::DiscoveredPeripheralTracker discovered_peripheral_tracker_ @@ -328,12 +328,12 @@ class BleV2 final { // A map of service_id -> ServerSocket. If map is non-empty, we // are currently listening for incoming connections. - absl::flat_hash_map server_sockets_ + absl::flat_hash_map server_sockets_ ABSL_GUARDED_BY(mutex_); // Tracks currently connected incoming sockets. This lets the device know when // it's okay to restart GATT server related operations. - absl::flat_hash_map incoming_sockets_ + absl::flat_hash_map incoming_sockets_ ABSL_GUARDED_BY(mutex_); mediums::InstantOnLostManager instant_on_lost_manager_; @@ -353,4 +353,4 @@ class BleV2 final { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_H_ diff --git a/connections/implementation/mediums/ble_v2/BUILD b/connections/implementation/mediums/ble/BUILD similarity index 98% rename from connections/implementation/mediums/ble_v2/BUILD rename to connections/implementation/mediums/ble/BUILD index 59723d58..b1f8691c 100644 --- a/connections/implementation/mediums/ble_v2/BUILD +++ b/connections/implementation/mediums/ble/BUILD @@ -53,7 +53,7 @@ cc_library( ) cc_library( - name = "ble_v2", + name = "ble", srcs = [ "advertisement_read_result.cc", "ble_advertisement.cc", @@ -111,7 +111,7 @@ cc_library( ) cc_test( - name = "ble_v2_test", + name = "ble_test", srcs = [ "advertisement_read_result_test.cc", "ble_advertisement_header_test.cc", @@ -125,8 +125,8 @@ cc_test( "instant_on_lost_manager_test.cc", ], deps = [ + ":ble", ":ble_advertisement_header", - ":ble_v2", ":bloom_filter", "//connections/implementation:types", "//connections/implementation/flags:connections_flags", diff --git a/connections/implementation/mediums/ble_v2/advertisement_read_result.cc b/connections/implementation/mediums/ble/advertisement_read_result.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/advertisement_read_result.cc rename to connections/implementation/mediums/ble/advertisement_read_result.cc index 594b98b2..34a56e7f 100644 --- a/connections/implementation/mediums/ble_v2/advertisement_read_result.cc +++ b/connections/implementation/mediums/ble/advertisement_read_result.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/advertisement_read_result.h" +#include "connections/implementation/mediums/ble/advertisement_read_result.h" #include #include diff --git a/connections/implementation/mediums/ble_v2/advertisement_read_result.h b/connections/implementation/mediums/ble/advertisement_read_result.h similarity index 95% rename from connections/implementation/mediums/ble_v2/advertisement_read_result.h rename to connections/implementation/mediums/ble/advertisement_read_result.h index 6875af34..6a32415a 100644 --- a/connections/implementation/mediums/ble_v2/advertisement_read_result.h +++ b/connections/implementation/mediums/ble/advertisement_read_result.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_ADVERTISEMENT_READ_RESULT_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_ADVERTISEMENT_READ_RESULT_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_ADVERTISEMENT_READ_RESULT_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_ADVERTISEMENT_READ_RESULT_H_ #include #include @@ -119,4 +119,4 @@ class AdvertisementReadResult { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_ADVERTISEMENT_READ_RESULT_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_ADVERTISEMENT_READ_RESULT_H_ diff --git a/connections/implementation/mediums/ble_v2/advertisement_read_result_test.cc b/connections/implementation/mediums/ble/advertisement_read_result_test.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/advertisement_read_result_test.cc rename to connections/implementation/mediums/ble/advertisement_read_result_test.cc index 55102560..ff077f0b 100644 --- a/connections/implementation/mediums/ble_v2/advertisement_read_result_test.cc +++ b/connections/implementation/mediums/ble/advertisement_read_result_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/advertisement_read_result.h" +#include "connections/implementation/mediums/ble/advertisement_read_result.h" #include "gtest/gtest.h" #include "absl/time/clock.h" diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement.cc b/connections/implementation/mediums/ble/ble_advertisement.cc similarity index 94% rename from connections/implementation/mediums/ble_v2/ble_advertisement.cc rename to connections/implementation/mediums/ble/ble_advertisement.cc index 40a037b0..4405e108 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement.cc +++ b/connections/implementation/mediums/ble/ble_advertisement.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" #include #include @@ -23,7 +23,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/str_cat.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" #include "internal/platform/byte_array.h" #include "internal/platform/logging.h" #include "internal/platform/stream_reader.h" @@ -46,18 +46,18 @@ bool HasField(uint8_t field_mask, uint8_t psm_bit) { BleAdvertisement::BleAdvertisement(Version version, SocketVersion socket_version, - const ByteArray &service_id_hash, - const ByteArray &data, - const ByteArray &device_token, int psm) { + const ByteArray& service_id_hash, + const ByteArray& data, + const ByteArray& device_token, int psm) { DoInitialize(/*fast_advertisement=*/service_id_hash.Empty(), version, socket_version, service_id_hash, data, device_token, psm); } void BleAdvertisement::DoInitialize(bool fast_advertisement, Version version, SocketVersion socket_version, - const ByteArray &service_id_hash, - const ByteArray &data, - const ByteArray &device_token, int psm) { + const ByteArray& service_id_hash, + const ByteArray& data, + const ByteArray& device_token, int psm) { // Check that the given input is valid. fast_advertisement_ = fast_advertisement; if (!fast_advertisement_) { @@ -87,7 +87,7 @@ void BleAdvertisement::DoInitialize(bool fast_advertisement, Version version, } absl::StatusOr BleAdvertisement::CreateBleAdvertisement( - const ByteArray &ble_advertisement_bytes) { + const ByteArray& ble_advertisement_bytes) { if (ble_advertisement_bytes.Empty()) { return absl::InvalidArgumentError( "Cannot deserialize BleAdvertisement: null bytes passed in."); @@ -277,7 +277,7 @@ ByteArray BleAdvertisement::ByteArrayWithExtraField() const { return ByteArray(std::move(advertisement_with_extra_fields_bytes)); } -bool BleAdvertisement::operator==(const BleAdvertisement &rhs) const { +bool BleAdvertisement::operator==(const BleAdvertisement& rhs) const { return this->GetVersion() == rhs.GetVersion() && this->GetSocketVersion() == rhs.GetSocketVersion() && this->IsFastAdvertisement() == rhs.IsFastAdvertisement() && @@ -298,11 +298,11 @@ bool BleAdvertisement::IsSupportedSocketVersion(SocketVersion socket_version) { } BleAdvertisement::BleExtraFields::BleExtraFields( - int psm, const ByteArray &rx_instant_connection_adv) + int psm, const ByteArray& rx_instant_connection_adv) : psm_(psm), rx_instant_connection_adv_(rx_instant_connection_adv) {} BleAdvertisement::BleExtraFields::BleExtraFields( - const ByteArray &ble_extra_fields_bytes) { + const ByteArray& ble_extra_fields_bytes) { if (ble_extra_fields_bytes.Empty()) { return; } diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement.h b/connections/implementation/mediums/ble/ble_advertisement.h similarity index 84% rename from connections/implementation/mediums/ble_v2/ble_advertisement.h rename to connections/implementation/mediums/ble/ble_advertisement.h index 52fbd73e..094ab567 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement.h +++ b/connections/implementation/mediums/ble/ble_advertisement.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_ADVERTISEMENT_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_ADVERTISEMENT_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_BLE_ADVERTISEMENT_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_BLE_ADVERTISEMENT_H_ #include #include @@ -21,7 +21,7 @@ #include "absl/status/statusor.h" #include "absl/strings/escaping.h" #include "absl/strings/str_format.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" #include "internal/platform/byte_array.h" namespace nearby { @@ -83,9 +83,9 @@ class BleAdvertisement { static constexpr int kMaxFastAdvertisementLength = 27; static constexpr int kExtraFieldsMaskLength = 1; // Hashable - bool operator==(const BleAdvertisement &rhs) const; + bool operator==(const BleAdvertisement& rhs) const; template - friend H AbslHashValue(H h, const BleAdvertisement &b) { + friend H AbslHashValue(H h, const BleAdvertisement& b) { return H::combine(std::move(h), b.version_, b.socket_version_, b.fast_advertisement_, b.service_id_hash_, b.data_, b.device_token_, b.psm_); @@ -93,16 +93,16 @@ class BleAdvertisement { BleAdvertisement() = default; BleAdvertisement(Version version, SocketVersion socket_version, - const ByteArray &service_id_hash, const ByteArray &data, - const ByteArray &device_token, + const ByteArray& service_id_hash, const ByteArray& data, + const ByteArray& device_token, int psm = BleAdvertisementHeader::kDefaultPsmValue); static absl::StatusOr CreateBleAdvertisement( - const ByteArray &ble_advertisement_bytes); - BleAdvertisement(const BleAdvertisement &) = default; - BleAdvertisement &operator=(const BleAdvertisement &) = default; - BleAdvertisement(BleAdvertisement &&) = default; - BleAdvertisement &operator=(BleAdvertisement &&) = default; + const ByteArray& ble_advertisement_bytes); + BleAdvertisement(const BleAdvertisement&) = default; + BleAdvertisement& operator=(const BleAdvertisement&) = default; + BleAdvertisement(BleAdvertisement&&) = default; + BleAdvertisement& operator=(BleAdvertisement&&) = default; ~BleAdvertisement() = default; // Returns ByteArray for legacy advertisement. @@ -117,17 +117,17 @@ class BleAdvertisement { bool IsFastAdvertisement() const { return fast_advertisement_; } bool IsSecondProfile() const { return is_second_profile_; } ByteArray GetServiceIdHash() const { return service_id_hash_; } - ByteArray &GetData() & { return data_; } - const ByteArray &GetData() const & { return data_; } - ByteArray &&GetData() && { return std::move(data_); } - const ByteArray &&GetData() const && { return std::move(data_); } + ByteArray& GetData() & { return data_; } + const ByteArray& GetData() const& { return data_; } + ByteArray&& GetData() && { return std::move(data_); } + const ByteArray&& GetData() const&& { return std::move(data_); } ByteArray GetDeviceToken() const { return device_token_; } int GetPsm() const { return psm_; } void SetPsm(int psm) { psm_ = psm; } ByteArray GetRxInstantConnectionAdv() const { return rx_instant_connection_adv_; } - void SetRxInstantConnectionAdv(const ByteArray &rx_instant_connection_adv) { + void SetRxInstantConnectionAdv(const ByteArray& rx_instant_connection_adv) { rx_instant_connection_adv_ = rx_instant_connection_adv; } std::string ToReadableString() const { @@ -161,8 +161,8 @@ class BleAdvertisement { static constexpr int kRxInstantConnectionAdvSizeLength = 1; explicit BleExtraFields(int psm, - const ByteArray &rx_instant_connection_adv); - explicit BleExtraFields(const ByteArray &ble_extra_fields_bytes); + const ByteArray& rx_instant_connection_adv); + explicit BleExtraFields(const ByteArray& ble_extra_fields_bytes); explicit operator ByteArray() const; int GetPsm() const { return psm_; } @@ -178,8 +178,8 @@ class BleAdvertisement { void DoInitialize(bool fast_advertisement, Version version, SocketVersion socket_version, - const ByteArray &service_id_hash, const ByteArray &data, - const ByteArray &device_token, int psm); + const ByteArray& service_id_hash, const ByteArray& data, + const ByteArray& device_token, int psm); static bool IsSupportedVersion(Version version); static bool IsSupportedSocketVersion(SocketVersion socket_version); int ComputeAdvertisementLength(int data_length, int total_optional_length, @@ -207,4 +207,4 @@ class BleAdvertisement { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_ADVERTISEMENT_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_BLE_ADVERTISEMENT_H_ diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc b/connections/implementation/mediums/ble/ble_advertisement_header.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/ble_advertisement_header.cc rename to connections/implementation/mediums/ble/ble_advertisement_header.cc index 1b441d10..5f9fa1c9 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement_header.cc +++ b/connections/implementation/mediums/ble/ble_advertisement_header.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" #include #include diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement_header.h b/connections/implementation/mediums/ble/ble_advertisement_header.h similarity index 82% rename from connections/implementation/mediums/ble_v2/ble_advertisement_header.h rename to connections/implementation/mediums/ble/ble_advertisement_header.h index 440586c3..e1f88f44 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement_header.h +++ b/connections/implementation/mediums/ble/ble_advertisement_header.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_ADVERTISEMENT_HEADER_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_ADVERTISEMENT_HEADER_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_BLE_ADVERTISEMENT_HEADER_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_BLE_ADVERTISEMENT_HEADER_H_ #include @@ -56,9 +56,9 @@ class BleAdvertisementHeader { static constexpr int kPsmValueByteLength = 2; // Hashable - bool operator==(const BleAdvertisementHeader &rhs) const; + bool operator==(const BleAdvertisementHeader& rhs) const; template - friend H AbslHashValue(H h, const BleAdvertisementHeader &b) { + friend H AbslHashValue(H h, const BleAdvertisementHeader& b) { return H::combine(std::move(h), b.version_, b.support_extended_advertisement_, b.num_slots_, b.service_id_bloom_filter_, b.advertisement_hash_, @@ -68,14 +68,14 @@ class BleAdvertisementHeader { BleAdvertisementHeader() = default; BleAdvertisementHeader(Version version, bool support_extended_advertisement, int num_slots, - const ByteArray &service_id_bloom_filter, - const ByteArray &advertisement_hash, int psm); + const ByteArray& service_id_bloom_filter, + const ByteArray& advertisement_hash, int psm); explicit BleAdvertisementHeader( - const ByteArray &ble_advertisement_header_bytes); - BleAdvertisementHeader(const BleAdvertisementHeader &) = default; - BleAdvertisementHeader &operator=(const BleAdvertisementHeader &) = default; - BleAdvertisementHeader(BleAdvertisementHeader &&) = default; - BleAdvertisementHeader &operator=(BleAdvertisementHeader &&) = default; + const ByteArray& ble_advertisement_header_bytes); + BleAdvertisementHeader(const BleAdvertisementHeader&) = default; + BleAdvertisementHeader& operator=(const BleAdvertisementHeader&) = default; + BleAdvertisementHeader(BleAdvertisementHeader&&) = default; + BleAdvertisementHeader& operator=(BleAdvertisementHeader&&) = default; ~BleAdvertisementHeader() = default; explicit operator ByteArray() const; @@ -112,4 +112,4 @@ class BleAdvertisementHeader { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_ADVERTISEMENT_HEADER_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_BLE_ADVERTISEMENT_HEADER_H_ diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement_header_test.cc b/connections/implementation/mediums/ble/ble_advertisement_header_test.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/ble_advertisement_header_test.cc rename to connections/implementation/mediums/ble/ble_advertisement_header_test.cc index 216cd1e0..aad6387c 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement_header_test.cc +++ b/connections/implementation/mediums/ble/ble_advertisement_header_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" #include diff --git a/connections/implementation/mediums/ble_v2/ble_advertisement_test.cc b/connections/implementation/mediums/ble/ble_advertisement_test.cc similarity index 99% rename from connections/implementation/mediums/ble_v2/ble_advertisement_test.cc rename to connections/implementation/mediums/ble/ble_advertisement_test.cc index 4097d78a..911c08df 100644 --- a/connections/implementation/mediums/ble_v2/ble_advertisement_test.cc +++ b/connections/implementation/mediums/ble/ble_advertisement_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" #include #include @@ -26,7 +26,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/string_view.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" #include "internal/platform/byte_array.h" namespace nearby { diff --git a/connections/implementation/mediums/ble_v2/ble_l2cap_packet.cc b/connections/implementation/mediums/ble/ble_l2cap_packet.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/ble_l2cap_packet.cc rename to connections/implementation/mediums/ble/ble_l2cap_packet.cc index 4647ac22..4aa0f75d 100644 --- a/connections/implementation/mediums/ble_v2/ble_l2cap_packet.cc +++ b/connections/implementation/mediums/ble/ble_l2cap_packet.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_l2cap_packet.h" +#include "connections/implementation/mediums/ble/ble_l2cap_packet.h" #include #include @@ -20,7 +20,7 @@ #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/strings/str_cat.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" #include "connections/implementation/mediums/utils.h" #include "internal/platform/byte_array.h" #include "internal/platform/logging.h" diff --git a/connections/implementation/mediums/ble_v2/ble_l2cap_packet.h b/connections/implementation/mediums/ble/ble_l2cap_packet.h similarity index 95% rename from connections/implementation/mediums/ble_v2/ble_l2cap_packet.h rename to connections/implementation/mediums/ble/ble_l2cap_packet.h index 67f74885..da6ee1e4 100644 --- a/connections/implementation/mediums/ble_v2/ble_l2cap_packet.h +++ b/connections/implementation/mediums/ble/ble_l2cap_packet.h @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_L2CAP_PACKET_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_L2CAP_PACKET_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_BLE_L2CAP_PACKET_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_BLE_L2CAP_PACKET_H_ #include #include "absl/status/statusor.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" #include "internal/platform/byte_array.h" namespace nearby { @@ -132,4 +132,4 @@ class BleL2capPacket { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_L2CAP_PACKET_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_BLE_L2CAP_PACKET_H_ diff --git a/connections/implementation/mediums/ble_v2/ble_l2cap_packet_test.cc b/connections/implementation/mediums/ble/ble_l2cap_packet_test.cc similarity index 95% rename from connections/implementation/mediums/ble_v2/ble_l2cap_packet_test.cc rename to connections/implementation/mediums/ble/ble_l2cap_packet_test.cc index 6103bb59..2084df2b 100644 --- a/connections/implementation/mediums/ble_v2/ble_l2cap_packet_test.cc +++ b/connections/implementation/mediums/ble/ble_l2cap_packet_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_l2cap_packet.h" +#include "connections/implementation/mediums/ble/ble_l2cap_packet.h" #include @@ -140,7 +140,7 @@ TEST(BleL2capPacketTest, CreateFromBytesWithInvalidCommand) { TEST(BleL2capPacketTest, CreateFromBytesWithoutDataLength) { ByteArray byte_array{1}; - char *byte_array_data = byte_array.data(); + char* byte_array_data = byte_array.data(); byte_array_data[0] = static_cast(BleL2capPacket::Command::kRequestAdvertisement); @@ -150,7 +150,7 @@ TEST(BleL2capPacketTest, CreateFromBytesWithoutDataLength) { TEST(BleL2capPacketTest, CreateFromBytesWithEmptyDataLength) { ByteArray byte_array{3}; - char *byte_array_data = byte_array.data(); + char* byte_array_data = byte_array.data(); byte_array_data[0] = static_cast(BleL2capPacket::Command::kRequestAdvertisement); byte_array_data[1] = 0x00; @@ -162,7 +162,7 @@ TEST(BleL2capPacketTest, CreateFromBytesWithEmptyDataLength) { TEST(BleL2capPacketTest, CreateFromBytesWithInvalidServiceIdHashLength) { ByteArray byte_array{3}; - char *byte_array_data = byte_array.data(); + char* byte_array_data = byte_array.data(); byte_array_data[0] = static_cast(BleL2capPacket::Command::kRequestAdvertisement); byte_array_data[1] = 0x00; @@ -174,7 +174,7 @@ TEST(BleL2capPacketTest, CreateFromBytesWithInvalidServiceIdHashLength) { TEST(BleL2capPacketTest, CreateFromBytesWithTooLongAdvertisementLength) { ByteArray byte_array{3}; - char *byte_array_data = byte_array.data(); + char* byte_array_data = byte_array.data(); byte_array_data[0] = static_cast(BleL2capPacket::Command::kResponseAdvertisement); byte_array_data[1] = 0xFF; @@ -186,7 +186,7 @@ TEST(BleL2capPacketTest, CreateFromBytesWithTooLongAdvertisementLength) { TEST(BleL2capPacketTest, CreateFromBytesWithInvalidLengthAdvertisement) { ByteArray byte_array{4}; - char *byte_array_data = byte_array.data(); + char* byte_array_data = byte_array.data(); byte_array_data[0] = static_cast(BleL2capPacket::Command::kResponseAdvertisement); byte_array_data[1] = 0x00; diff --git a/connections/implementation/mediums/ble_v2/ble_packet.cc b/connections/implementation/mediums/ble/ble_packet.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/ble_packet.cc rename to connections/implementation/mediums/ble/ble_packet.cc index bef94669..2554d690 100644 --- a/connections/implementation/mediums/ble_v2/ble_packet.cc +++ b/connections/implementation/mediums/ble/ble_packet.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_packet.h" +#include "connections/implementation/mediums/ble/ble_packet.h" #include #include diff --git a/connections/implementation/mediums/ble_v2/ble_packet.h b/connections/implementation/mediums/ble/ble_packet.h similarity index 95% rename from connections/implementation/mediums/ble_v2/ble_packet.h rename to connections/implementation/mediums/ble/ble_packet.h index b1fed67c..857d7e59 100644 --- a/connections/implementation/mediums/ble_v2/ble_packet.h +++ b/connections/implementation/mediums/ble/ble_packet.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PACKET_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PACKET_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_BLE_PACKET_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_BLE_PACKET_H_ #include "absl/status/statusor.h" #include "internal/platform/byte_array.h" @@ -105,4 +105,4 @@ class BlePacket { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_BLE_PACKET_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_BLE_PACKET_H_ diff --git a/connections/implementation/mediums/ble_v2/ble_packet_test.cc b/connections/implementation/mediums/ble/ble_packet_test.cc similarity index 99% rename from connections/implementation/mediums/ble_v2/ble_packet_test.cc rename to connections/implementation/mediums/ble/ble_packet_test.cc index 9a08d53c..811b88ea 100644 --- a/connections/implementation/mediums/ble_v2/ble_packet_test.cc +++ b/connections/implementation/mediums/ble/ble_packet_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_packet.h" +#include "connections/implementation/mediums/ble/ble_packet.h" #include #include diff --git a/connections/implementation/mediums/ble_v2/ble_utils.cc b/connections/implementation/mediums/ble/ble_utils.cc similarity index 93% rename from connections/implementation/mediums/ble_v2/ble_utils.cc rename to connections/implementation/mediums/ble/ble_utils.cc index 76edd5b0..214f81eb 100644 --- a/connections/implementation/mediums/ble_v2/ble_utils.cc +++ b/connections/implementation/mediums/ble/ble_utils.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_utils.h" +#include "connections/implementation/mediums/ble/ble_utils.h" #include #include @@ -22,9 +22,9 @@ #include "absl/base/attributes.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" -#include "connections/implementation/mediums/ble_v2/ble_packet.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_packet.h" #include "connections/implementation/mediums/utils.h" #include "internal/platform/byte_array.h" #include "internal/platform/prng.h" diff --git a/connections/implementation/mediums/ble_v2/ble_utils.h b/connections/implementation/mediums/ble/ble_utils.h similarity index 91% rename from connections/implementation/mediums/ble_v2/ble_utils.h rename to connections/implementation/mediums/ble/ble_utils.h index 9db8e8a1..bb4968eb 100644 --- a/connections/implementation/mediums/ble_v2/ble_utils.h +++ b/connections/implementation/mediums/ble/ble_utils.h @@ -12,15 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_UTILS_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_UTILS_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_UTILS_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_UTILS_H_ #include #include #include #include "absl/base/attributes.h" -#include "connections/implementation/mediums/ble_v2//ble_advertisement.h" +#include "connections/implementation/mediums/ble//ble_advertisement.h" #include "internal/platform/byte_array.h" #include "internal/platform/uuid.h" @@ -68,4 +68,4 @@ std::optional GenerateAdvertisementUuid(int slot); } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_UTILS_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_UTILS_H_ diff --git a/connections/implementation/mediums/ble_v2/ble_utils_test.cc b/connections/implementation/mediums/ble/ble_utils_test.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/ble_utils_test.cc rename to connections/implementation/mediums/ble/ble_utils_test.cc index 3394c44e..c8a73ecc 100644 --- a/connections/implementation/mediums/ble_v2/ble_utils_test.cc +++ b/connections/implementation/mediums/ble/ble_utils_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/ble_utils.h" +#include "connections/implementation/mediums/ble/ble_utils.h" #include #include diff --git a/connections/implementation/mediums/ble_v2/bloom_filter.cc b/connections/implementation/mediums/ble/bloom_filter.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/bloom_filter.cc rename to connections/implementation/mediums/ble/bloom_filter.cc index e389029e..a5f06e20 100644 --- a/connections/implementation/mediums/ble_v2/bloom_filter.cc +++ b/connections/implementation/mediums/ble/bloom_filter.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/bloom_filter.h" +#include "connections/implementation/mediums/ble/bloom_filter.h" #include "absl/numeric/int128.h" #include "absl/strings/numbers.h" diff --git a/connections/implementation/mediums/ble_v2/bloom_filter.h b/connections/implementation/mediums/ble/bloom_filter.h similarity index 95% rename from connections/implementation/mediums/ble_v2/bloom_filter.h rename to connections/implementation/mediums/ble/bloom_filter.h index 606a5106..f33da62e 100644 --- a/connections/implementation/mediums/ble_v2/bloom_filter.h +++ b/connections/implementation/mediums/ble/bloom_filter.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_BLOOM_FILTER_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_BLOOM_FILTER_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_BLOOM_FILTER_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_BLOOM_FILTER_H_ #include #include @@ -96,4 +96,4 @@ class BitSetImpl final : public BitSet { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_BLOOM_FILTER_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_BLOOM_FILTER_H_ diff --git a/connections/implementation/mediums/ble_v2/bloom_filter_test.cc b/connections/implementation/mediums/ble/bloom_filter_test.cc similarity index 99% rename from connections/implementation/mediums/ble_v2/bloom_filter_test.cc rename to connections/implementation/mediums/ble/bloom_filter_test.cc index 971a3b63..958662ff 100644 --- a/connections/implementation/mediums/ble_v2/bloom_filter_test.cc +++ b/connections/implementation/mediums/ble/bloom_filter_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/bloom_filter.h" +#include "connections/implementation/mediums/ble/bloom_filter.h" #include diff --git a/connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h b/connections/implementation/mediums/ble/discovered_peripheral_callback.h similarity index 67% rename from connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h rename to connections/implementation/mediums/ble/discovered_peripheral_callback.h index 63378db6..a57a95c6 100644 --- a/connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h +++ b/connections/implementation/mediums/ble/discovered_peripheral_callback.h @@ -12,13 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_CALLBACK_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_CALLBACK_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_DISCOVERED_PERIPHERAL_CALLBACK_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_DISCOVERED_PERIPHERAL_CALLBACK_H_ #include #include "absl/functional/any_invocable.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/byte_array.h" namespace nearby { @@ -28,20 +28,20 @@ namespace mediums { // Callback that is invoked when a {@link BlePeripheral} is discovered. struct DiscoveredPeripheralCallback { absl::AnyInvocable peripheral_discovered_cb = - [](BleV2Peripheral, const std::string&, const ByteArray&, bool) {}; + [](BlePeripheral, const std::string&, const ByteArray&, bool) {}; absl::AnyInvocable peripheral_lost_cb = - [](BleV2Peripheral, const std::string&, const ByteArray&, bool) {}; + [](BlePeripheral, const std::string&, const ByteArray&, bool) {}; absl::AnyInvocable instant_lost_cb = - [](BleV2Peripheral, const std::string&, const ByteArray&, bool) {}; + [](BlePeripheral, const std::string&, const ByteArray&, bool) {}; absl::AnyInvocable legacy_device_discovered_cb = []() {}; }; @@ -49,4 +49,4 @@ struct DiscoveredPeripheralCallback { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_CALLBACK_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_DISCOVERED_PERIPHERAL_CALLBACK_H_ diff --git a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.cc b/connections/implementation/mediums/ble/discovered_peripheral_tracker.cc similarity index 96% rename from connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.cc rename to connections/implementation/mediums/ble/discovered_peripheral_tracker.cc index a874acd2..9b3efa0d 100644 --- a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.cc +++ b/connections/implementation/mediums/ble/discovered_peripheral_tracker.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_tracker.h" #include #include @@ -30,28 +30,28 @@ #include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/mediums/advertisements/advertisement_util.h" #include "connections/implementation/mediums/advertisements/dct_advertisement.h" -#include "connections/implementation/mediums/ble_v2/advertisement_read_result.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" -#include "connections/implementation/mediums/ble_v2/ble_utils.h" -#include "connections/implementation/mediums/ble_v2/bloom_filter.h" -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h" -#include "connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h" +#include "connections/implementation/mediums/ble/advertisement_read_result.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_utils.h" +#include "connections/implementation/mediums/ble/bloom_filter.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_callback.h" +#include "connections/implementation/mediums/ble/instant_on_lost_advertisement.h" #include "connections/implementation/mediums/lost_entity_tracker.h" #include "connections/implementation/pcp.h" #include "connections/implementation/webrtc_state.h" #include "internal/flags/nearby_flags.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/byte_array.h" #include "internal/platform/feature_flags.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/system_clock.h" #include "internal/platform/logging.h" #include "internal/platform/multi_thread_executor.h" #include "internal/platform/mutex_lock.h" #include "internal/platform/uuid.h" -using ::nearby::api::ble_v2::BleAdvertisementData; +using ::nearby::api::ble::BleAdvertisementData; namespace nearby { namespace connections { @@ -163,7 +163,7 @@ void DiscoveredPeripheralTracker::StopTracking(const std::string& service_id) { } void DiscoveredPeripheralTracker::ProcessFoundBleAdvertisement( - BleV2Peripheral peripheral, BleAdvertisementData advertisement_data, + BlePeripheral peripheral, BleAdvertisementData advertisement_data, AdvertisementFetcher advertisement_fetcher) { MutexLock lock(&mutex_); @@ -245,7 +245,7 @@ bool DiscoveredPeripheralTracker::HandleOnLostAdvertisementLocked( // Need to report OnLost for each gatt_advertisement. for (const auto& gatt_advertisement : gatt_advertisements) { - BleV2Peripheral lost_peripheral = it.second.peripheral; + BlePeripheral lost_peripheral = it.second.peripheral; lost_peripheral.SetId(ByteArray(gatt_advertisement)); if (gatt_advertisement.IsValid()) { if (NearbyFlags::GetInstance().GetBoolFlag( @@ -293,7 +293,7 @@ void DiscoveredPeripheralTracker::ProcessLostGattAdvertisements() { for (const auto& gatt_advertisement : lost_gatt_advertisements) { const auto it = gatt_advertisement_infos_.find(gatt_advertisement); if (it != gatt_advertisement_infos_.end()) { - BleV2Peripheral lost_peripheral = it->second.peripheral; + BlePeripheral lost_peripheral = it->second.peripheral; if (lost_peripheral.IsValid()) { lost_peripheral.SetId(ByteArray(gatt_advertisement)); service_id_info.discovered_peripheral_callback.peripheral_lost_cb( @@ -326,7 +326,7 @@ void DiscoveredPeripheralTracker::ClearDataForServiceId( } bool DiscoveredPeripheralTracker::IsSkippableGattAdvertisement( - const api::ble_v2::BleAdvertisementData& advertisement_data) { + const api::ble::BleAdvertisementData& advertisement_data) { if (!is_extended_advertisement_available_) { // Don't skip any advertisement if the scanner doesn't support extended // advertisement. @@ -384,8 +384,8 @@ void DiscoveredPeripheralTracker::ClearGattAdvertisement( } void DiscoveredPeripheralTracker::HandleAdvertisement( - BleV2Peripheral peripheral, - const nearby::api::ble_v2::BleAdvertisementData& advertisement_data) { + BlePeripheral peripheral, + const nearby::api::ble::BleAdvertisementData& advertisement_data) { ByteArray advertisement_bytes = ExtractInterestingAdvertisementBytes(advertisement_data); if (advertisement_bytes.Empty()) { @@ -430,7 +430,7 @@ void DiscoveredPeripheralTracker::HandleAdvertisement( } ByteArray DiscoveredPeripheralTracker::ExtractInterestingAdvertisementBytes( - const nearby::api::ble_v2::BleAdvertisementData& advertisement_data) { + const nearby::api::ble::BleAdvertisementData& advertisement_data) { // Iterate through all tracked service IDs to see if any of their fast // advertisements are contained within this BLE advertisement. for (const auto& item : service_id_infos_) { @@ -463,7 +463,7 @@ BleAdvertisementHeader DiscoveredPeripheralTracker::CreateAdvertisementHeader( } BleAdvertisementHeader DiscoveredPeripheralTracker::HandleRawGattAdvertisements( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementHeader& advertisement_header, const std::vector& gatt_advertisement_bytes_list, const Uuid& service_uuid) { @@ -511,7 +511,7 @@ BleAdvertisementHeader DiscoveredPeripheralTracker::HandleRawGattAdvertisements( } if (peripheral.IsValid()) { peripheral.SetPsm(new_psm); - BleV2Peripheral discovered_peripheral = peripheral; + BlePeripheral discovered_peripheral = peripheral; discovered_peripheral.SetId(ByteArray(gatt_advertisement)); if (IsInstantLostAdvertisement(new_advertisement_header)) { @@ -733,8 +733,8 @@ DiscoveredPeripheralTracker::HandleDctAdvertisement( } void DiscoveredPeripheralTracker::HandleAdvertisementHeader( - BleV2Peripheral peripheral, - const nearby::api::ble_v2::BleAdvertisementData& advertisement_data, + BlePeripheral peripheral, + const nearby::api::ble::BleAdvertisementData& advertisement_data, AdvertisementFetcher advertisement_fetcher) { // Attempt to parse the advertisement header. BleAdvertisementHeader advertisement_header( @@ -847,7 +847,7 @@ void DiscoveredPeripheralTracker::HandleAdvertisementHeader( } ByteArray DiscoveredPeripheralTracker::ExtractAdvertisementHeaderBytes( - const nearby::api::ble_v2::BleAdvertisementData& advertisement_data) { + const nearby::api::ble::BleAdvertisementData& advertisement_data) { const auto it = advertisement_data.service_data.find(bleutils::kCopresenceServiceUuid); if (it != advertisement_data.service_data.end()) { @@ -930,7 +930,7 @@ bool DiscoveredPeripheralTracker::ShouldReadRawAdvertisementFromServer( std::vector DiscoveredPeripheralTracker::FetchRawAdvertisements( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementHeader& advertisement_header, AdvertisementFetcher advertisement_fetcher) { // Fetch the raw GATT advertisements and store the results. @@ -952,7 +952,7 @@ DiscoveredPeripheralTracker::FetchRawAdvertisements( } void DiscoveredPeripheralTracker::FetchRawAdvertisementsInThread( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementHeader& advertisement_header, AdvertisementFetcher advertisement_fetcher) { std::vector service_ids; diff --git a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h b/connections/implementation/mediums/ble/discovered_peripheral_tracker.h similarity index 90% rename from connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h rename to connections/implementation/mediums/ble/discovered_peripheral_tracker.h index 1c5e22a5..b0b76a15 100644 --- a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h +++ b/connections/implementation/mediums/ble/discovered_peripheral_tracker.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_TRACKER_H_ -#define CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_TRACKER_H_ +#ifndef CORE_INTERNAL_MEDIUMS_BLE_DISCOVERED_PERIPHERAL_TRACKER_H_ +#define CORE_INTERNAL_MEDIUMS_BLE_DISCOVERED_PERIPHERAL_TRACKER_H_ #include #include @@ -28,17 +28,17 @@ #include "absl/functional/any_invocable.h" #include "absl/time/time.h" #include "connections/implementation/mediums//lost_entity_tracker.h" -#include "connections/implementation/mediums/ble_v2/advertisement_read_result.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h" +#include "connections/implementation/mediums/ble/advertisement_read_result.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_callback.h" #include "connections/implementation/mediums/lost_entity_tracker.h" #include "connections/implementation/pcp.h" #include "internal/platform/atomic_boolean.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/byte_array.h" #include "internal/platform/condition_variable.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/multi_thread_executor.h" #include "internal/platform/mutex.h" #include "internal/platform/uuid.h" @@ -65,7 +65,7 @@ class DiscoveredPeripheralTracker { // `advertisement_read_result` is in/out mutable reference that the caller // should take of its life cycle and pass a valid reference. using AdvertisementFetcher = absl::AnyInvocable& interesting_service_ids, mediums::AdvertisementReadResult& advertisement_read_result)>; @@ -101,8 +101,8 @@ class DiscoveredPeripheralTracker { // advertisement if the advertisement header is been // processed. void ProcessFoundBleAdvertisement( - BleV2Peripheral peripheral, - api::ble_v2::BleAdvertisementData advertisement_data, + BlePeripheral peripheral, + api::ble::BleAdvertisementData advertisement_data, AdvertisementFetcher advertisement_fetcher) ABSL_LOCKS_EXCLUDED(mutex_); // Processes the set of lost GATT advertisements and notifies the client of @@ -148,14 +148,14 @@ class DiscoveredPeripheralTracker { BleAdvertisementHeader advertisement_header; // A proxy BlePeripheral for found/lost discovery callback. - BleV2Peripheral peripheral; + BlePeripheral peripheral; // Hash for instant on lost. ByteArray instant_on_lost_hash; }; struct GattFetchTask { - BleV2Peripheral peripheral; + BlePeripheral peripheral; BleAdvertisementHeader advertisement_header; AdvertisementFetcher advertisement_fetcher; absl::Time scheduled_time; @@ -169,7 +169,7 @@ class DiscoveredPeripheralTracker { // as exented_advertisement. This is to avoid reading advertisement from GATT // connection, which has been advertised by extended advertisement. bool IsSkippableGattAdvertisement( - const api::ble_v2::BleAdvertisementData& advertisement_data) + const api::ble::BleAdvertisementData& advertisement_data) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Clears out all data related to the provided GATT advertisement. This @@ -187,13 +187,13 @@ class DiscoveredPeripheralTracker { // Handles the legacy fast advertisement or the extended fast/regular // advertisement. void HandleAdvertisement( - BleV2Peripheral peripheral, - const api::ble_v2::BleAdvertisementData& advertisement_data) + BlePeripheral peripheral, + const api::ble::BleAdvertisementData& advertisement_data) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Extracts the advertisement byte array from `AdvertisementData`. ByteArray ExtractInterestingAdvertisementBytes( - const api::ble_v2::BleAdvertisementData& advertisement_data) + const api::ble::BleAdvertisementData& advertisement_data) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Creates an advertisement header that's purely a hash of the fast/regular @@ -204,7 +204,7 @@ class DiscoveredPeripheralTracker { // Returns BleAdvertisementHeader, it may be replaced if the header is mock // and there's a psm value in advertisement. BleAdvertisementHeader HandleRawGattAdvertisements( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementHeader& advertisement_header, const std::vector& gatt_advertisement_bytes_list, const Uuid& service_uuid) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); @@ -229,20 +229,20 @@ class DiscoveredPeripheralTracker { bool IsDummyAdvertisementHeader( const BleAdvertisementHeader& advertisement_header); - std::optional HandleDctAdvertisement( - const api::ble_v2::BleAdvertisementData& advertisement_data) + std::optional HandleDctAdvertisement( + const api::ble::BleAdvertisementData& advertisement_data) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Handles the advertisement header for regular advertisement. void HandleAdvertisementHeader( - BleV2Peripheral peripheral, - const api::ble_v2::BleAdvertisementData& advertisement_data, + BlePeripheral peripheral, + const api::ble::BleAdvertisementData& advertisement_data, AdvertisementFetcher advertisement_fetcher) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Extracts the advertisement header byte array from `AdvertisementData`. ByteArray ExtractAdvertisementHeaderBytes( - const api::ble_v2::BleAdvertisementData& advertisement_data) + const api::ble::BleAdvertisementData& advertisement_data) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Returns true if the advertisement header contains a service ID we're @@ -263,13 +263,13 @@ class DiscoveredPeripheralTracker { // advertisement_fetcher : a fetcher passed from BLE medium to read the // advertisement from BLE characteristics by GATT server. std::vector FetchRawAdvertisements( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementHeader& advertisement_header, AdvertisementFetcher advertisement_fetcher) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); void FetchRawAdvertisementsInThread( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementHeader& advertisement_header, AdvertisementFetcher advertisement_fetcher); @@ -287,13 +287,13 @@ class DiscoveredPeripheralTracker { // 2. Matches a peripheral's advertisement hash that has previously been // discovered. bool HandleOnLostAdvertisementLocked( - const ::nearby::api::ble_v2::BleAdvertisementData& advertisement_data) + const ::nearby::api::ble::BleAdvertisementData& advertisement_data) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); // Returns true if the advertisement header met the special conditions of // a legacy device dummy advertisement. static bool IsLegacyDeviceAdvertisementData( - const api::ble_v2::BleAdvertisementData& advertisement_data); + const api::ble::BleAdvertisementData& advertisement_data); // Helps to handle advertisement for Instant On lost. bool IsInstantLostAdvertisement( @@ -377,4 +377,4 @@ class DiscoveredPeripheralTracker { } // namespace connections } // namespace nearby -#endif // CORE_INTERNAL_MEDIUMS_BLE_V2_DISCOVERED_PERIPHERAL_TRACKER_H_ +#endif // CORE_INTERNAL_MEDIUMS_BLE_DISCOVERED_PERIPHERAL_TRACKER_H_ diff --git a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker_test.cc b/connections/implementation/mediums/ble/discovered_peripheral_tracker_test.cc similarity index 91% rename from connections/implementation/mediums/ble_v2/discovered_peripheral_tracker_test.cc rename to connections/implementation/mediums/ble/discovered_peripheral_tracker_test.cc index ec812fbe..52eed33e 100644 --- a/connections/implementation/mediums/ble_v2/discovered_peripheral_tracker_test.cc +++ b/connections/implementation/mediums/ble/discovered_peripheral_tracker_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_tracker.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_tracker.h" #include #include @@ -32,22 +32,22 @@ #include "absl/time/time.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/mediums/advertisements/dct_advertisement.h" -#include "connections/implementation/mediums/ble_v2/advertisement_read_result.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" -#include "connections/implementation/mediums/ble_v2/ble_utils.h" -#include "connections/implementation/mediums/ble_v2/bloom_filter.h" -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h" -#include "connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h" +#include "connections/implementation/mediums/ble/advertisement_read_result.h" +#include "connections/implementation/mediums/ble/ble_advertisement.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_utils.h" +#include "connections/implementation/mediums/ble/bloom_filter.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_callback.h" +#include "connections/implementation/mediums/ble/instant_on_lost_advertisement.h" #include "connections/implementation/mediums/utils.h" #include "connections/implementation/pcp.h" #include "internal/flags/nearby_flags.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/byte_array.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/feature_flags.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/mac_address.h" #include "internal/platform/medium_environment.h" #include "internal/platform/mutex.h" @@ -173,14 +173,11 @@ ByteArray GenerateRandomAdvertisementHash() { class MockDiscoveredPeripheralCallback : public DiscoveredPeripheralCallback { public: MOCK_METHOD(void, OnPeripheralDiscovered, - (BleV2Peripheral, const std::string&, const ByteArray&, bool), - ()); + (BlePeripheral, const std::string&, const ByteArray&, bool), ()); MOCK_METHOD(void, OnPeripheralLost, - (BleV2Peripheral, const std::string&, const ByteArray&, bool), - ()); + (BlePeripheral, const std::string&, const ByteArray&, bool), ()); MOCK_METHOD(void, OnInstantLost, - (BleV2Peripheral, const std::string&, const ByteArray&, bool), - ()); + (BlePeripheral, const std::string&, const ByteArray&, bool), ()); MOCK_METHOD(void, OnLegacyDeviceDiscovered, (), ()); }; @@ -215,8 +212,8 @@ class DiscoveredPeripheralTrackerTest enable_read_gatt_for_extended_advertisement); adapter_peripheral_ = std::make_unique(); adapter_central_ = std::make_unique(); - ble_peripheral_ = std::make_unique(*adapter_peripheral_); - ble_central_ = std::make_unique(*adapter_central_); + ble_peripheral_ = std::make_unique(*adapter_peripheral_); + ble_central_ = std::make_unique(*adapter_central_); } void TearDown() override { @@ -225,17 +222,17 @@ class DiscoveredPeripheralTrackerTest NearbyFlags::GetInstance().ResetOverridedValues(); } - BleV2Peripheral CreateBlePeripheral() { - return BleV2Peripheral(*ble_central_, - adapter_peripheral_->GetAddress().address()); + BlePeripheral CreateBlePeripheral() { + return BlePeripheral(*ble_central_, + adapter_peripheral_->GetAddress().address()); } // Simulates to see a fast advertisement. void FindFastAdvertisement( - const api::ble_v2::BleAdvertisementData& advertisement_data, + const api::ble::BleAdvertisementData& advertisement_data, const std::vector& advertisement_bytes_list, CountDownLatch& fetch_latch) { - BleV2Peripheral peripheral = CreateBlePeripheral(); + BlePeripheral peripheral = CreateBlePeripheral(); discovered_peripheral_tracker_->ProcessFoundBleAdvertisement( peripheral, advertisement_data, @@ -245,11 +242,11 @@ class DiscoveredPeripheralTrackerTest // Simulates to see an extended advertisement, which don't need to fetch // advertisement from GATT. void FindExtendedAdvertisement( - const api::ble_v2::BleAdvertisementData& advertisement_data, + const api::ble::BleAdvertisementData& advertisement_data, CountDownLatch& fetch_latch) { - BleV2Peripheral peripheral = CreateBlePeripheral(); + BlePeripheral peripheral = CreateBlePeripheral(); DiscoveredPeripheralTracker::AdvertisementFetcher placeholder_fetcher = - [](BleV2Peripheral, int, int, const std::vector&, + [](BlePeripheral, int, int, const std::vector&, mediums::AdvertisementReadResult&) {}; discovered_peripheral_tracker_->ProcessFoundBleAdvertisement( peripheral, advertisement_data, std::move(placeholder_fetcher)); @@ -258,10 +255,10 @@ class DiscoveredPeripheralTrackerTest // Simulates to see a regular advertisement. void FindAdvertisement( - const api::ble_v2::BleAdvertisementData& advertisement_data, + const api::ble::BleAdvertisementData& advertisement_data, const std::vector& advertisement_bytes_list, CountDownLatch& fetch_latch) { - BleV2Peripheral peripheral = CreateBlePeripheral(); + BlePeripheral peripheral = CreateBlePeripheral(); discovered_peripheral_tracker_->ProcessFoundBleAdvertisement( peripheral, advertisement_data, @@ -269,10 +266,10 @@ class DiscoveredPeripheralTrackerTest } void FindAdvertisementWithDelay( - const api::ble_v2::BleAdvertisementData& advertisement_data, + const api::ble::BleAdvertisementData& advertisement_data, const std::vector& advertisement_bytes_list, CountDownLatch& fetch_latch, absl::Duration delay) { - BleV2Peripheral peripheral = CreateBlePeripheral(); + BlePeripheral peripheral = CreateBlePeripheral(); discovered_peripheral_tracker_->ProcessFoundBleAdvertisement( peripheral, advertisement_data, @@ -304,7 +301,7 @@ class DiscoveredPeripheralTrackerTest CountDownLatch& fetch_latch, const std::vector& advertisement_bytes_list) { return [this, &fetch_latch, advertisement_bytes_list]( - BleV2Peripheral peripheral, int num_slots, int psm, + BlePeripheral peripheral, int num_slots, int psm, const std::vector& interesting_service_ids, mediums::AdvertisementReadResult& advertisement_read_result) { MutexLock lock(&mutex_); @@ -325,7 +322,7 @@ class DiscoveredPeripheralTrackerTest const std::vector& advertisement_bytes_list, absl::Duration delay) { return [this, &fetch_latch, advertisement_bytes_list, delay]( - BleV2Peripheral peripheral, int num_slots, int psm, + BlePeripheral peripheral, int num_slots, int psm, const std::vector& interesting_service_ids, mediums::AdvertisementReadResult& advertisement_read_result) { MutexLock lock(&mutex_); @@ -344,8 +341,8 @@ class DiscoveredPeripheralTrackerTest std::unique_ptr adapter_peripheral_; std::unique_ptr adapter_central_; - std::unique_ptr ble_peripheral_; - std::unique_ptr ble_central_; + std::unique_ptr ble_peripheral_; + std::unique_ptr ble_central_; mutable Mutex mutex_; int fetch_count_ ABSL_GUARDED_BY(mutex_) = 0; std::unique_ptr discovered_peripheral_tracker_; @@ -362,7 +359,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -373,7 +370,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, Uuid(kFastAdvertisementServiceUuid)); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!fast_advertisement_bytes.Empty()) { advertisement_data.service_data.insert( {Uuid(kFastAdvertisementServiceUuid), fast_advertisement_bytes}); @@ -398,7 +395,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, DctAdvertisementPeripheralDiscovered) { std::string(kServiceIdA), true, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -408,7 +405,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, DctAdvertisementPeripheralDiscovered) { }, Uuid(kFastAdvertisementServiceUuid)); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; advertisement_data.service_data.insert( {bleutils::kDctServiceUuid, dct_advertisement_bytes}); @@ -438,7 +435,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, {.peripheral_discovered_cb = [&found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { EXPECT_EQ(advertisement_bytes, ByteArray(std::string(kData))); EXPECT_FALSE(fast_advertisement); @@ -448,7 +445,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, [&legacy_found_latch]() { legacy_found_latch.CountDown(); }}, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -477,7 +474,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&callback_times, &found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { callback_times++; @@ -488,7 +485,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, Uuid(kFastAdvertisementServiceUuid)); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!fast_advertisement_bytes.Empty()) { advertisement_data.service_data.insert( {Uuid(kFastAdvertisementServiceUuid), fast_advertisement_bytes}); @@ -502,7 +499,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&callback_times, &found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { callback_times++; @@ -521,7 +518,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&callback_times, &found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { callback_times++; @@ -548,7 +545,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&callback_times, &found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { callback_times++; @@ -559,7 +556,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, Uuid(kFastAdvertisementServiceUuid)); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!fast_advertisement_bytes.Empty()) { advertisement_data.service_data.insert( {Uuid(kFastAdvertisementServiceUuid), fast_advertisement_bytes}); @@ -592,7 +589,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -603,7 +600,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, Uuid("FE3C")); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!fast_advertisement_bytes.Empty()) { advertisement_data.service_data.insert( {Uuid(kFastAdvertisementServiceUuid), fast_advertisement_bytes}); @@ -635,7 +632,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch_a](BleV2Peripheral peripheral, + [&found_latch_a](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -649,7 +646,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdB), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch_b](BleV2Peripheral peripheral, + [&found_latch_b](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -660,7 +657,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -694,7 +691,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -705,7 +702,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -735,13 +732,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { found_latch.CountDown(); }, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -776,7 +773,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&callback_times, &found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { callback_times++; @@ -787,7 +784,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -822,7 +819,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&callback_times, &found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { callback_times++; @@ -833,7 +830,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -870,13 +867,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&found_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { found_latch.CountDown(); }, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -906,7 +903,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -916,7 +913,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, .peripheral_lost_cb = [&lost_latch, &lost_callback_times]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_callback_times++; @@ -925,7 +922,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, Uuid(kFastAdvertisementServiceUuid)); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -969,7 +966,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -979,13 +976,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }, Uuid(kFastAdvertisementServiceUuid)); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1026,7 +1023,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForAdvertisementLost) { std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1036,13 +1033,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForAdvertisementLost) { }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1085,7 +1082,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch_a](BleV2Peripheral peripheral, + [&found_latch_a](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1095,7 +1092,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, .peripheral_lost_cb = [&lost_latch_a]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch_a.CountDown(); }, }, @@ -1104,7 +1101,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdB), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch_b](BleV2Peripheral peripheral, + [&found_latch_b](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1114,13 +1111,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, .peripheral_lost_cb = [&lost_latch_b]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch_b.CountDown(); }, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1166,7 +1163,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1176,13 +1173,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1220,7 +1217,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForInstantOnLost) { std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1230,13 +1227,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForInstantOnLost) { }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1253,7 +1250,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, LostPeripheralForInstantOnLost) { std::list({std::string( bleutils::GenerateAdvertisementHash(advertisement_bytes))})); ASSERT_OK(advertisement); - api::ble_v2::BleAdvertisementData loss_advertisement_data{}; + api::ble::BleAdvertisementData loss_advertisement_data{}; loss_advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, ByteArray(advertisement->ToBytes())}); @@ -1286,7 +1283,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, InstantLostPeripheralForInstantOnLost) { std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1296,13 +1293,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, InstantLostPeripheralForInstantOnLost) { }, .instant_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1319,7 +1316,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, InstantLostPeripheralForInstantOnLost) { std::list({std::string( bleutils::GenerateAdvertisementHash(advertisement_bytes))})); ASSERT_OK(advertisement); - api::ble_v2::BleAdvertisementData loss_advertisement_data{}; + api::ble::BleAdvertisementData loss_advertisement_data{}; loss_advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, ByteArray(advertisement->ToBytes())}); @@ -1353,7 +1350,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&mock_callback](BleV2Peripheral peripheral, + [&mock_callback](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1362,7 +1359,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, fast_advertisement); }, .instant_lost_cb = - [&mock_callback](BleV2Peripheral peripheral, + [&mock_callback](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1373,7 +1370,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1391,7 +1388,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::list({std::string( bleutils::GenerateAdvertisementHash(advertisement_bytes))})); ASSERT_OK(advertisement); - api::ble_v2::BleAdvertisementData loss_advertisement_data{}; + api::ble::BleAdvertisementData loss_advertisement_data{}; loss_advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, ByteArray(advertisement->ToBytes())}); @@ -1432,7 +1429,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1442,13 +1439,13 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }, Uuid(kFastAdvertisementServiceUuid)); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!fast_advertisement_bytes.Empty()) { advertisement_data.service_data.insert( {Uuid(kFastAdvertisementServiceUuid), fast_advertisement_bytes}); @@ -1463,7 +1460,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, auto advertisement = InstantOnLostAdvertisement::CreateFromHashes( std::list({std::string(advertisement_hash)})); ASSERT_OK(advertisement); - api::ble_v2::BleAdvertisementData loss_advertisement_data{}; + api::ble::BleAdvertisementData loss_advertisement_data{}; loss_advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, ByteArray(advertisement->ToBytes())}); @@ -1479,7 +1476,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, HandleDummyAdvertisement) { .enable_invoking_legacy_device_discovered_cb = true, }; MediumEnvironment::Instance().SetFeatureFlags(flag); - api::ble_v2::BleAdvertisementData advertising_data{}; + api::ble::BleAdvertisementData advertising_data{}; advertising_data.is_extended_advertisement = false; ByteArray encoded_bytes{ DiscoveredPeripheralTracker::kDummyAdvertisementValue}; @@ -1492,7 +1489,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, HandleDummyAdvertisement) { std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [](BleV2Peripheral peripheral, const std::string& service_id, + [](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { FAIL() << "Should NOT report found for dummy advertisement"; @@ -1515,7 +1512,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, SkipDummyAdvertisement) { .enable_invoking_legacy_device_discovered_cb = false, }; MediumEnvironment::Instance().SetFeatureFlags(flag); - api::ble_v2::BleAdvertisementData advertising_data{}; + api::ble::BleAdvertisementData advertising_data{}; advertising_data.is_extended_advertisement = false; ByteArray encoded_bytes{ DiscoveredPeripheralTracker::kDummyAdvertisementValue}; @@ -1528,7 +1525,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, SkipDummyAdvertisement) { std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [](BleV2Peripheral peripheral, const std::string& service_id, + [](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { FAIL() << "Should NOT report found for dummy advertisement"; @@ -1565,7 +1562,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, FetchGattAdvertisementInThread) { std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1576,7 +1573,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, FetchGattAdvertisementInThread) { }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1612,7 +1609,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1623,7 +1620,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1665,7 +1662,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1675,7 +1672,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, }, {}); - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1684,7 +1681,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, FindAdvertisementWithDelay(advertisement_data, {advertisement_bytes}, fetch_latch, kDefaultGattFetchDelay); - api::ble_v2::BleAdvertisementData advertisement_data_2{}; + api::ble::BleAdvertisementData advertisement_data_2{}; if (!advertisement_header_bytes_2.Empty()) { advertisement_data_2.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes_2}); @@ -1724,7 +1721,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1736,7 +1733,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, bleutils::kCopresenceServiceUuid); // 1. First receive a GATT advertisement data, it should be skipped. - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1745,7 +1742,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, FindAdvertisement(advertisement_data, {advertisement_bytes}, fetch_latch); // 2. Receive extended advertisement data first. - api::ble_v2::BleAdvertisementData extended_advertisement_data{}; + api::ble::BleAdvertisementData extended_advertisement_data{}; extended_advertisement_data.is_extended_advertisement = true; extended_advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_bytes}); @@ -1786,7 +1783,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1798,7 +1795,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, bleutils::kCopresenceServiceUuid); // 1. First receive a GATT advertisement data, it should be skipped. - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1844,7 +1841,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, SkipExpiredGattAdvertisement) { std::string(kServiceIdA), false, Pcp::kP2pPointToPoint, { .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1856,7 +1853,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, SkipExpiredGattAdvertisement) { bleutils::kCopresenceServiceUuid); // 1. First receive a GATT advertisement data, it should be skipped. - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1905,7 +1902,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&found_latch, &callback_count]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { ++callback_count; @@ -1916,7 +1913,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, (*fake_clock)->FastForward(absl::Seconds(4)); // 1. Received extended advertisement. - api::ble_v2::BleAdvertisementData extended_advertisement_data{}; + api::ble::BleAdvertisementData extended_advertisement_data{}; extended_advertisement_data.is_extended_advertisement = true; extended_advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_bytes}); @@ -1924,7 +1921,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, FindExtendedAdvertisement(extended_advertisement_data, fetch_latch); // 2. Received GATT advertisement. - api::ble_v2::BleAdvertisementData advertisement_data{}; + api::ble::BleAdvertisementData advertisement_data{}; if (!advertisement_header_bytes.Empty()) { advertisement_data.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_bytes}); @@ -1979,7 +1976,7 @@ TEST_P(DiscoveredPeripheralTrackerTest, { .peripheral_discovered_cb = [&found_latch, &discovered_peripheral_order]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { discovered_peripheral_order.push_back( @@ -1991,20 +1988,20 @@ TEST_P(DiscoveredPeripheralTrackerTest, (*fake_clock)->FastForward(absl::Seconds(4)); // 1. Find peripheral A with GATT advertisement. - api::ble_v2::BleAdvertisementData advertisement_data_a{}; + api::ble::BleAdvertisementData advertisement_data_a{}; advertisement_data_a.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_a}); FindAdvertisementWithDelay(advertisement_data_a, {advertisement_a}, fetch_latch, kDefaultGattFetchDelay); // 2. Find peripheral B with extended advertisement. - api::ble_v2::BleAdvertisementData advertisement_data_b{}; + api::ble::BleAdvertisementData advertisement_data_b{}; advertisement_data_b.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_b}); FindAdvertisement(advertisement_data_b, {advertisement_b}, fetch_latch); // 3. Find peripheral C with GATT advertisement. - api::ble_v2::BleAdvertisementData advertisement_data_c{}; + api::ble::BleAdvertisementData advertisement_data_c{}; advertisement_data_c.service_data.insert( {bleutils::kCopresenceServiceUuid, advertisement_header_c}); FindAdvertisement(advertisement_data_c, {advertisement_c}, fetch_latch); diff --git a/connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.cc b/connections/implementation/mediums/ble/instant_on_lost_advertisement.cc similarity index 96% rename from connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.cc rename to connections/implementation/mediums/ble/instant_on_lost_advertisement.cc index 43b147f4..8d1e591d 100644 --- a/connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.cc +++ b/connections/implementation/mediums/ble/instant_on_lost_advertisement.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h" +#include "connections/implementation/mediums/ble/instant_on_lost_advertisement.h" #include #include @@ -24,7 +24,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" #include "internal/platform/logging.h" namespace nearby { diff --git a/connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h b/connections/implementation/mediums/ble/instant_on_lost_advertisement.h similarity index 95% rename from connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h rename to connections/implementation/mediums/ble/instant_on_lost_advertisement.h index 42104265..6faae829 100644 --- a/connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h +++ b/connections/implementation/mediums/ble/instant_on_lost_advertisement.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_V2_INSTANT_ON_LOST_ADVERTISEMENT_H_ -#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_V2_INSTANT_ON_LOST_ADVERTISEMENT_H_ +#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_INSTANT_ON_LOST_ADVERTISEMENT_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_INSTANT_ON_LOST_ADVERTISEMENT_H_ #include #include @@ -64,4 +64,4 @@ class InstantOnLostAdvertisement { } // namespace connections } // namespace nearby -#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_V2_INSTANT_ON_LOST_ADVERTISEMENT_H_ +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_INSTANT_ON_LOST_ADVERTISEMENT_H_ diff --git a/connections/implementation/mediums/ble_v2/instant_on_lost_advertisement_test.cc b/connections/implementation/mediums/ble/instant_on_lost_advertisement_test.cc similarity index 97% rename from connections/implementation/mediums/ble_v2/instant_on_lost_advertisement_test.cc rename to connections/implementation/mediums/ble/instant_on_lost_advertisement_test.cc index a3b721f3..690314e9 100644 --- a/connections/implementation/mediums/ble_v2/instant_on_lost_advertisement_test.cc +++ b/connections/implementation/mediums/ble/instant_on_lost_advertisement_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h" +#include "connections/implementation/mediums/ble/instant_on_lost_advertisement.h" #include #include diff --git a/connections/implementation/mediums/ble_v2/instant_on_lost_manager.cc b/connections/implementation/mediums/ble/instant_on_lost_manager.cc similarity index 95% rename from connections/implementation/mediums/ble_v2/instant_on_lost_manager.cc rename to connections/implementation/mediums/ble/instant_on_lost_manager.cc index 6dea81b9..6fd17c46 100644 --- a/connections/implementation/mediums/ble_v2/instant_on_lost_manager.cc +++ b/connections/implementation/mediums/ble/instant_on_lost_manager.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/instant_on_lost_manager.h" +#include "connections/implementation/mediums/ble/instant_on_lost_manager.h" #include #include @@ -24,15 +24,15 @@ #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h" -#include "connections/implementation/mediums/ble_v2/ble_utils.h" -#include "connections/implementation/mediums/ble_v2/instant_on_lost_advertisement.h" +#include "connections/implementation/mediums/ble/ble_utils.h" +#include "connections/implementation/mediums/ble/instant_on_lost_advertisement.h" #include "internal/flags/nearby_flags.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancelable_alarm.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/system_clock.h" #include "internal/platform/logging.h" @@ -185,10 +185,10 @@ bool InstantOnLostManager::IsOnLostAdvertisingForTesting() { } bool InstantOnLostManager::StartInstantOnLostAdvertisement() { - api::ble_v2::BleAdvertisementData advertisement_data; - api::ble_v2::AdvertiseParameters advertise_parameters; + api::ble::BleAdvertisementData advertisement_data; + api::ble::AdvertiseParameters advertise_parameters; advertise_parameters.is_connectable = false; - advertise_parameters.tx_power_level = api::ble_v2::TxPowerLevel::kHigh; + advertise_parameters.tx_power_level = api::ble::TxPowerLevel::kHigh; RemoveExpiredOnLostAdvertisements(); absl::StatusOr on_lost_advertisement = diff --git a/connections/implementation/mediums/ble_v2/instant_on_lost_manager.h b/connections/implementation/mediums/ble/instant_on_lost_manager.h similarity index 94% rename from connections/implementation/mediums/ble_v2/instant_on_lost_manager.h rename to connections/implementation/mediums/ble/instant_on_lost_manager.h index ea335d98..6eed79a6 100644 --- a/connections/implementation/mediums/ble_v2/instant_on_lost_manager.h +++ b/connections/implementation/mediums/ble/instant_on_lost_manager.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_V2_INSTANT_ON_LOST_MANAGER_H_ -#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_V2_INSTANT_ON_LOST_MANAGER_H_ +#ifndef THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_INSTANT_ON_LOST_MANAGER_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_INSTANT_ON_LOST_MANAGER_H_ #include #include @@ -23,7 +23,7 @@ #include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancelable_alarm.h" @@ -74,7 +74,7 @@ class InstantOnLostManager { // BLE medium used for lost packet advertising. BluetoothAdapter adapter_; - BleV2Medium ble_medium_ = BleV2Medium{adapter_}; + BleMedium ble_medium_ = BleMedium{adapter_}; bool is_on_lost_advertising_ = false; absl::Time last_advertising_start_time_ = absl::InfinitePast(); @@ -89,4 +89,4 @@ class InstantOnLostManager { } // namespace connections } // namespace nearby -#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_V2_INSTANT_ON_LOST_MANAGER_H_ +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_BLE_INSTANT_ON_LOST_MANAGER_H_ diff --git a/connections/implementation/mediums/ble_v2/instant_on_lost_manager_test.cc b/connections/implementation/mediums/ble/instant_on_lost_manager_test.cc similarity index 98% rename from connections/implementation/mediums/ble_v2/instant_on_lost_manager_test.cc rename to connections/implementation/mediums/ble/instant_on_lost_manager_test.cc index 8ce7775b..2ea26152 100644 --- a/connections/implementation/mediums/ble_v2/instant_on_lost_manager_test.cc +++ b/connections/implementation/mediums/ble/instant_on_lost_manager_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2/instant_on_lost_manager.h" +#include "connections/implementation/mediums/ble/instant_on_lost_manager.h" #include #include diff --git a/connections/implementation/mediums/ble_v2_test.cc b/connections/implementation/mediums/ble_test.cc similarity index 81% rename from connections/implementation/mediums/ble_v2_test.cc rename to connections/implementation/mediums/ble_test.cc index f6a04307..de0e98e9 100644 --- a/connections/implementation/mediums/ble_v2_test.cc +++ b/connections/implementation/mediums/ble_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "connections/implementation/mediums/ble_v2.h" +#include "connections/implementation/mediums/ble.h" #include #include @@ -22,12 +22,12 @@ #include "absl/strings/string_view.h" #include "absl/time/time.h" #include "connections/implementation/flags/nearby_connections_feature_flags.h" -#include "connections/implementation/mediums/ble_v2/discovered_peripheral_callback.h" +#include "connections/implementation/mediums/ble/discovered_peripheral_callback.h" #include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/pcp.h" #include "connections/power_level.h" #include "internal/flags/nearby_flags.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/count_down_latch.h" @@ -63,7 +63,7 @@ constexpr absl::string_view kAdvertisementStringB = "\x01\x02\x03\x04"; constexpr absl::string_view kLocalEndpointId = "local_endpoint_id"; constexpr absl::string_view kFastAdvertisementServiceUuid{"\xf3\xfe"}; -class BleV2Test : public testing::TestWithParam { +class BleTest : public testing::TestWithParam { public: void SetUp() override { NearbyFlags::GetInstance().OverrideInt64FlagValue( @@ -76,14 +76,14 @@ class BleV2Test : public testing::TestWithParam { MediumEnvironment& env_{MediumEnvironment::Instance()}; }; -TEST_P(BleV2Test, CanConnect) { +TEST_P(BleTest, CanConnect) { FeatureFlags feature_flags = GetParam(); env_.SetFeatureFlags(feature_flags); env_.Start(); BluetoothRadio radio_client; BluetoothRadio radio_server; - BleV2 ble_client{radio_client}; - BleV2 ble_server{radio_server}; + Ble ble_client{radio_client}; + Ble ble_server{radio_server}; radio_client.Enable(); radio_server.Enable(); std::string service_id(kServiceIDA); @@ -91,25 +91,24 @@ TEST_P(BleV2Test, CanConnect) { CountDownLatch discovered_latch(1); CountDownLatch accept_latch(1); - BleV2Socket socket_for_server; + BleSocket socket_for_server; EXPECT_TRUE(ble_server.StartAcceptingConnections( - service_id, [&](BleV2Socket socket, const std::string&) { + service_id, [&](BleSocket socket, const std::string&) { socket_for_server = std::move(socket); accept_latch.CountDown(); })); ble_server.StartAdvertising(service_id, PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, - advertisement_bytes); + Ble::AdvertisingType::kFast, advertisement_bytes); - BleV2Peripheral discovered_peripheral; + BlePeripheral discovered_peripheral; ble_client.StartScanning( service_id, Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = [&discovered_latch, &discovered_peripheral]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { discovered_peripheral = peripheral; @@ -122,7 +121,7 @@ TEST_P(BleV2Test, CanConnect) { ASSERT_TRUE(discovered_peripheral.IsValid()); CancellationFlag flag; - ErrorOr socket_for_client_result = + ErrorOr socket_for_client_result = ble_client.Connect(service_id, discovered_peripheral, &flag); EXPECT_TRUE(accept_latch.Await(kWaitDuration).result()); EXPECT_TRUE(ble_server.StopAcceptingConnections(service_id)); @@ -135,14 +134,14 @@ TEST_P(BleV2Test, CanConnect) { env_.Stop(); } -TEST_P(BleV2Test, CanCancelConnect) { +TEST_P(BleTest, CanCancelConnect) { FeatureFlags feature_flags = GetParam(); env_.SetFeatureFlags(feature_flags); env_.Start(); BluetoothRadio radio_client; BluetoothRadio radio_server; - BleV2 ble_client{radio_client}; - BleV2 ble_server{radio_server}; + Ble ble_client{radio_client}; + Ble ble_server{radio_server}; radio_client.Enable(); radio_server.Enable(); std::string service_id(kServiceIDA); @@ -150,25 +149,24 @@ TEST_P(BleV2Test, CanCancelConnect) { CountDownLatch discovered_latch(1); CountDownLatch accept_latch(1); - BleV2Socket socket_for_server; + BleSocket socket_for_server; EXPECT_TRUE(ble_server.StartAcceptingConnections( - service_id, [&](BleV2Socket socket, const std::string&) { + service_id, [&](BleSocket socket, const std::string&) { socket_for_server = std::move(socket); accept_latch.CountDown(); })); ble_server.StartAdvertising(service_id, PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, - advertisement_bytes); + Ble::AdvertisingType::kFast, advertisement_bytes); - BleV2Peripheral discovered_peripheral; + BlePeripheral discovered_peripheral; ble_client.StartScanning( service_id, Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = [&discovered_latch, &discovered_peripheral]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { discovered_peripheral = peripheral; @@ -181,7 +179,7 @@ TEST_P(BleV2Test, CanCancelConnect) { ASSERT_TRUE(discovered_peripheral.IsValid()); CancellationFlag flag(true); - ErrorOr socket_for_client_result = + ErrorOr socket_for_client_result = ble_client.Connect(service_id, discovered_peripheral, &flag); // If FeatureFlag is disabled, Cancelled is false as no-op. if (!feature_flags.enable_cancellation_flag) { @@ -204,15 +202,15 @@ TEST_P(BleV2Test, CanCancelConnect) { env_.Stop(); } -INSTANTIATE_TEST_SUITE_P(ParametrisedBleTest, BleV2Test, +INSTANTIATE_TEST_SUITE_P(ParametrisedBleTest, BleTest, ::testing::ValuesIn(kTestCases)); -TEST_F(BleV2Test, CanConstructValidObject) { +TEST_F(BleTest, CanConstructValidObject) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); EXPECT_TRUE(ble_a.IsMediumValid()); EXPECT_TRUE(ble_a.IsAvailable()); @@ -222,74 +220,74 @@ TEST_F(BleV2Test, CanConstructValidObject) { env_.Stop(); } -TEST_F(BleV2Test, CanNotStartAdvertisingWithEmptyAdvertisementBytes) { +TEST_F(BleTest, CanNotStartAdvertisingWithEmptyAdvertisementBytes) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a(radio_a); + Ble ble_a(radio_a); radio_a.Enable(); ByteArray advertisement_bytes; EXPECT_FALSE(ble_a.StartAdvertising( std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes)); + Ble::AdvertisingType::kRegular, advertisement_bytes)); env_.Stop(); } -TEST_F(BleV2Test, CanNotStartAdvertisingWhenRadioNotEnabled) { +TEST_F(BleTest, CanNotStartAdvertisingWhenRadioNotEnabled) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a(radio_a); + Ble ble_a(radio_a); radio_a.Disable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); EXPECT_FALSE(ble_a.StartAdvertising( std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes)); + Ble::AdvertisingType::kRegular, advertisement_bytes)); env_.Stop(); } -TEST_F(BleV2Test, CanStartAdvertisingWithDifferentPowerLevels) { +TEST_F(BleTest, CanStartAdvertisingWithDifferentPowerLevels) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a(radio_a); + Ble ble_a(radio_a); radio_a.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); EXPECT_TRUE(ble_a.StartAdvertising( std::string(kServiceIDA), PowerLevel::kLowPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes)); + Ble::AdvertisingType::kRegular, advertisement_bytes)); EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); EXPECT_TRUE(ble_a.StartAdvertising( std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes)); + Ble::AdvertisingType::kRegular, advertisement_bytes)); EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); env_.Stop(); } -TEST_F(BleV2Test, CanStartAdvertisingWithDifferentAdvertisingTypes) { +TEST_F(BleTest, CanStartAdvertisingWithDifferentAdvertisingTypes) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a(radio_a); + Ble ble_a(radio_a); radio_a.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); EXPECT_TRUE(ble_a.StartAdvertising( std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes)); + Ble::AdvertisingType::kRegular, advertisement_bytes)); EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); - EXPECT_TRUE(ble_a.StartAdvertising( - std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, advertisement_bytes)); + EXPECT_TRUE( + ble_a.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, + Ble::AdvertisingType::kFast, advertisement_bytes)); EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); env_.Stop(); } -TEST_F(BleV2Test, CanStartFastAdvertising) { +TEST_F(BleTest, CanStartFastAdvertising) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -300,7 +298,7 @@ TEST_F(BleV2Test, CanStartFastAdvertising) { /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -309,35 +307,35 @@ TEST_F(BleV2Test, CanStartFastAdvertising) { }, }); - EXPECT_TRUE(ble_a.StartAdvertising( - std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, advertisement_bytes)); + EXPECT_TRUE( + ble_a.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, + Ble::AdvertisingType::kFast, advertisement_bytes)); EXPECT_TRUE(found_latch.Await(kWaitDuration).result()); EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); ble_b.StopScanning(std::string(kServiceIDA)); env_.Stop(); } -TEST_F(BleV2Test, CanStartFastScanning) { +TEST_F(BleTest, CanStartFastScanning) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); CountDownLatch found_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, advertisement_bytes); + Ble::AdvertisingType::kFast, advertisement_bytes); EXPECT_TRUE(ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -352,12 +350,12 @@ TEST_F(BleV2Test, CanStartFastScanning) { env_.Stop(); } -TEST_F(BleV2Test, CanStartAdvertising) { +TEST_F(BleTest, CanStartAdvertising) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -368,7 +366,7 @@ TEST_F(BleV2Test, CanStartAdvertising) { /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -379,33 +377,33 @@ TEST_F(BleV2Test, CanStartAdvertising) { EXPECT_TRUE(ble_a.StartAdvertising( std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes)); + Ble::AdvertisingType::kRegular, advertisement_bytes)); EXPECT_TRUE(found_latch.Await(kWaitDuration).result()); EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA))); ble_b.StopScanning(std::string(kServiceIDA)); env_.Stop(); } -TEST_F(BleV2Test, CanStartScanning) { +TEST_F(BleTest, CanStartScanning) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); CountDownLatch found_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes); + Ble::AdvertisingType::kRegular, advertisement_bytes); EXPECT_TRUE(ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -420,10 +418,10 @@ TEST_F(BleV2Test, CanStartScanning) { env_.Stop(); } -TEST_F(BleV2Test, CanNotStartScanningWhenRadioNotEnabled) { +TEST_F(BleTest, CanNotStartScanningWhenRadioNotEnabled) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a(radio_a); + Ble ble_a(radio_a); radio_a.Disable(); EXPECT_FALSE(ble_a.StartScanning( @@ -433,22 +431,21 @@ TEST_F(BleV2Test, CanNotStartScanningWhenRadioNotEnabled) { env_.Stop(); } -TEST_F(BleV2Test, CanNotStartAcceptingConnectionsWhenRadioNotEnabled) { +TEST_F(BleTest, CanNotStartAcceptingConnectionsWhenRadioNotEnabled) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a(radio_a); + Ble ble_a(radio_a); radio_a.Disable(); EXPECT_FALSE(ble_a.StartAcceptingConnections( - std::string(kServiceIDA), - [&](BleV2Socket socket, const std::string&) {})); + std::string(kServiceIDA), [&](BleSocket socket, const std::string&) {})); env_.Stop(); } -TEST_F(BleV2Test, CanStartScanningWithDifferentPowerLevels) { +TEST_F(BleTest, CanStartScanningWithDifferentPowerLevels) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a(radio_a); + Ble ble_a(radio_a); radio_a.Enable(); EXPECT_TRUE(ble_a.StartScanning(std::string(kServiceIDA), @@ -464,40 +461,40 @@ TEST_F(BleV2Test, CanStartScanningWithDifferentPowerLevels) { env_.Stop(); } -TEST_F(BleV2Test, CanNotConnectWithInvalidPeripheral) { +TEST_F(BleTest, CanNotConnectWithInvalidPeripheral) { env_.Start(); BluetoothRadio radio_client; - BleV2 ble_client{radio_client}; + Ble ble_client{radio_client}; radio_client.Enable(); std::string service_id(kServiceIDA); - BleV2Peripheral invalid_peripheral; + BlePeripheral invalid_peripheral; CancellationFlag flag; - ErrorOr socket_for_client_result = + ErrorOr socket_for_client_result = ble_client.Connect(service_id, invalid_peripheral, &flag); EXPECT_TRUE(socket_for_client_result.has_error()); env_.Stop(); } -TEST_F(BleV2Test, CanNotConnectWithEmptyServiceId) { +TEST_F(BleTest, CanNotConnectWithEmptyServiceId) { env_.Start(); BluetoothRadio radio_client; - BleV2 ble_client{radio_client}; + Ble ble_client{radio_client}; radio_client.Enable(); std::string empty_service_id; - BleV2Peripheral peripheral; + BlePeripheral peripheral; CancellationFlag flag; - ErrorOr socket_for_client_result = + ErrorOr socket_for_client_result = ble_client.Connect(empty_service_id, peripheral, &flag); EXPECT_TRUE(socket_for_client_result.has_error()); env_.Stop(); } -TEST_F(BleV2Test, CanStartStopMultipleScanningWithDifferentServiceIds) { +TEST_F(BleTest, CanStartStopMultipleScanningWithDifferentServiceIds) { env_.Start(); BluetoothRadio radio; - BleV2 ble(radio); + Ble ble(radio); radio.Enable(); EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDA), Pcp::kP2pPointToPoint, @@ -519,24 +516,24 @@ TEST_F(BleV2Test, CanStartStopMultipleScanningWithDifferentServiceIds) { env_.Stop(); } -TEST_F(BleV2Test, DestructWorksForStartAdvertisingAndScanningWithoutStop) { +TEST_F(BleTest, DestructWorksForStartAdvertisingAndScanningWithoutStop) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); // Device A starts advertising with service IDA and IDB. - EXPECT_TRUE(ble_a.StartAdvertising( - std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, advertisement_bytes)); - EXPECT_TRUE(ble_a.StartAdvertising( - std::string(kServiceIDB), PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, advertisement_bytes)); + EXPECT_TRUE( + ble_a.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, + Ble::AdvertisingType::kFast, advertisement_bytes)); + EXPECT_TRUE( + ble_a.StartAdvertising(std::string(kServiceIDB), PowerLevel::kHighPower, + Ble::AdvertisingType::kFast, advertisement_bytes)); // Device B starts scanning with service IDA and IDB EXPECT_TRUE(ble_b.StartScanning(std::string(kServiceIDA), @@ -550,12 +547,12 @@ TEST_F(BleV2Test, DestructWorksForStartAdvertisingAndScanningWithoutStop) { env_.Stop(); } -TEST_F(BleV2Test, StartFastScanningDiscoverAndLostPeripheral) { +TEST_F(BleTest, StartFastScanningDiscoverAndLostPeripheral) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -563,14 +560,14 @@ TEST_F(BleV2Test, StartFastScanningDiscoverAndLostPeripheral) { CountDownLatch lost_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, advertisement_bytes); + Ble::AdvertisingType::kFast, advertisement_bytes); ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -579,7 +576,7 @@ TEST_F(BleV2Test, StartFastScanningDiscoverAndLostPeripheral) { }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }); @@ -598,13 +595,12 @@ TEST_F(BleV2Test, StartFastScanningDiscoverAndLostPeripheral) { env_.Stop(); } -TEST_F(BleV2Test, - StartFastScanningDiscoverButNoPeripheralLostAfterStopScanning) { +TEST_F(BleTest, StartFastScanningDiscoverButNoPeripheralLostAfterStopScanning) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -612,14 +608,14 @@ TEST_F(BleV2Test, CountDownLatch lost_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kFast, advertisement_bytes); + Ble::AdvertisingType::kFast, advertisement_bytes); ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -628,7 +624,7 @@ TEST_F(BleV2Test, }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }); @@ -645,12 +641,12 @@ TEST_F(BleV2Test, env_.Stop(); } -TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) { +TEST_F(BleTest, StartScanningDiscoverAndLostPeripheral) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -658,14 +654,14 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) { CountDownLatch lost_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes); + Ble::AdvertisingType::kRegular, advertisement_bytes); ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -674,7 +670,7 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) { }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }); @@ -693,12 +689,12 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) { env_.Stop(); } -TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) { +TEST_F(BleTest, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) { env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -706,14 +702,14 @@ TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) { CountDownLatch lost_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes); + Ble::AdvertisingType::kRegular, advertisement_bytes); ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -722,7 +718,7 @@ TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) { }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, }); @@ -739,10 +735,10 @@ TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) { env_.Stop(); } -TEST_F(BleV2Test, CanStartAndStopLegacyAdvertising) { +TEST_F(BleTest, CanStartAndStopLegacyAdvertising) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a{radio_a}; + Ble ble_a{radio_a}; radio_a.Enable(); std::string service_id(kServiceIDA); EXPECT_TRUE( @@ -755,10 +751,10 @@ TEST_F(BleV2Test, CanStartAndStopLegacyAdvertising) { env_.Stop(); } -TEST_F(BleV2Test, CanNotStartLegacyAdvertisingWhenRadioNotEnabled) { +TEST_F(BleTest, CanNotStartLegacyAdvertisingWhenRadioNotEnabled) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a{radio_a}; + Ble ble_a{radio_a}; radio_a.Disable(); std::string service_id(kServiceIDA); EXPECT_FALSE( @@ -768,10 +764,10 @@ TEST_F(BleV2Test, CanNotStartLegacyAdvertisingWhenRadioNotEnabled) { env_.Stop(); } -TEST_F(BleV2Test, CanNotStopLegacyAdvertisingForNonExistingServiceId) { +TEST_F(BleTest, CanNotStopLegacyAdvertisingForNonExistingServiceId) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a{radio_a}; + Ble ble_a{radio_a}; radio_a.Enable(); std::string service_id(kServiceIDA); EXPECT_FALSE(ble_a.IsAdvertisingForLegacyDevice(service_id)); @@ -785,16 +781,16 @@ TEST_F(BleV2Test, CanNotStopLegacyAdvertisingForNonExistingServiceId) { env_.Stop(); } -TEST_F(BleV2Test, StartLegacyAdvertisingNotBlockedByRegularAdvertising) { +TEST_F(BleTest, StartLegacyAdvertisingNotBlockedByRegularAdvertising) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a{radio_a}; + Ble ble_a{radio_a}; radio_a.Enable(); std::string service_id(kServiceIDA); ByteArray advertisement_bytes((std::string(kAdvertisementString))); ble_a.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes); + Ble::AdvertisingType::kRegular, advertisement_bytes); EXPECT_TRUE(ble_a.IsAdvertising(service_id)); EXPECT_TRUE( ble_a.StartLegacyAdvertising(service_id, std::string(kLocalEndpointId), @@ -804,10 +800,10 @@ TEST_F(BleV2Test, StartLegacyAdvertisingNotBlockedByRegularAdvertising) { env_.Stop(); } -TEST_F(BleV2Test, DuplicateStartLegacyAdvertisingReturnsFalse) { +TEST_F(BleTest, DuplicateStartLegacyAdvertisingReturnsFalse) { env_.Start(); BluetoothRadio radio_a; - BleV2 ble_a{radio_a}; + Ble ble_a{radio_a}; radio_a.Enable(); std::string service_id(kServiceIDA); EXPECT_FALSE(ble_a.IsAdvertisingForLegacyDevice(service_id)); @@ -823,14 +819,14 @@ TEST_F(BleV2Test, DuplicateStartLegacyAdvertisingReturnsFalse) { env_.Stop(); } -TEST_F(BleV2Test, HandleLegacyAdvertising) { +TEST_F(BleTest, HandleLegacyAdvertising) { env_.SetFeatureFlags( {FeatureFlags{.enable_invoking_legacy_device_discovered_cb = true}}); env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -846,7 +842,7 @@ TEST_F(BleV2Test, HandleLegacyAdvertising) { /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [](BleV2Peripheral peripheral, const std::string& service_id, + [](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { FAIL() << "Legacy device shouldn't be reported here."; @@ -863,27 +859,27 @@ TEST_F(BleV2Test, HandleLegacyAdvertising) { env_.Stop(); } -TEST_F(BleV2Test, CanStartAsyncScanning) { +TEST_F(BleTest, CanStartAsyncScanning) { env_.SetFeatureFlags({FeatureFlags{.enable_ble_v2_async_scanning = true}}); env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); CountDownLatch found_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes); + Ble::AdvertisingType::kRegular, advertisement_bytes); EXPECT_TRUE(ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -898,20 +894,20 @@ TEST_F(BleV2Test, CanStartAsyncScanning) { env_.Stop(); } -TEST_F(BleV2Test, StartAsyncScanningWithPlatformErrors) { +TEST_F(BleTest, StartAsyncScanningWithPlatformErrors) { env_.SetFeatureFlags({FeatureFlags{.enable_ble_v2_async_scanning = true}}); env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); CountDownLatch found_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes); + Ble::AdvertisingType::kRegular, advertisement_bytes); // Disable radio a to simulate platform error. radio_a.Disable(); @@ -920,7 +916,7 @@ TEST_F(BleV2Test, StartAsyncScanningWithPlatformErrors) { /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -935,7 +931,7 @@ TEST_F(BleV2Test, StartAsyncScanningWithPlatformErrors) { /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -954,13 +950,13 @@ TEST_F(BleV2Test, StartAsyncScanningWithPlatformErrors) { env_.Stop(); } -TEST_F(BleV2Test, StartAsyncScanningDiscoverAndLostPeripheral) { +TEST_F(BleTest, StartAsyncScanningDiscoverAndLostPeripheral) { env_.SetFeatureFlags({FeatureFlags{.enable_ble_v2_async_scanning = true}}); env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -968,14 +964,14 @@ TEST_F(BleV2Test, StartAsyncScanningDiscoverAndLostPeripheral) { CountDownLatch lost_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes); + Ble::AdvertisingType::kRegular, advertisement_bytes); EXPECT_TRUE(ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -984,7 +980,7 @@ TEST_F(BleV2Test, StartAsyncScanningDiscoverAndLostPeripheral) { }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, })); @@ -1002,14 +998,14 @@ TEST_F(BleV2Test, StartAsyncScanningDiscoverAndLostPeripheral) { env_.Stop(); } -TEST_F(BleV2Test, +TEST_F(BleTest, StartAsyncScanningDiscoverButNoPeripheralLostAfterStopScanning) { env_.SetFeatureFlags({FeatureFlags{.enable_ble_v2_async_scanning = true}}); env_.Start(); BluetoothRadio radio_a; BluetoothRadio radio_b; - BleV2 ble_a(radio_a); - BleV2 ble_b(radio_b); + Ble ble_a(radio_a); + Ble ble_b(radio_b); radio_a.Enable(); radio_b.Enable(); ByteArray advertisement_bytes((std::string(kAdvertisementString))); @@ -1017,14 +1013,14 @@ TEST_F(BleV2Test, CountDownLatch lost_latch(1); ble_b.StartAdvertising(std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes); + Ble::AdvertisingType::kRegular, advertisement_bytes); EXPECT_TRUE(ble_a.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1033,7 +1029,7 @@ TEST_F(BleV2Test, }, .peripheral_lost_cb = [&lost_latch]( - BleV2Peripheral peripheral, const std::string& service_id, + BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { lost_latch.CountDown(); }, })); @@ -1050,15 +1046,15 @@ TEST_F(BleV2Test, env_.Stop(); } -TEST_F(BleV2Test, CanStartStopMultipleAsyncScanningWithDifferentServiceIds) { +TEST_F(BleTest, CanStartStopMultipleAsyncScanningWithDifferentServiceIds) { env_.SetFeatureFlags({FeatureFlags{.enable_ble_v2_async_scanning = true}}); env_.Start(); BluetoothRadio radio_scanner; BluetoothRadio radio_advertiser_a; BluetoothRadio radio_advertiser_b; - BleV2 ble_scanner(radio_scanner); - BleV2 ble_advertiser_a(radio_advertiser_a); - BleV2 ble_advertiser_b(radio_advertiser_b); + Ble ble_scanner(radio_scanner); + Ble ble_advertiser_a(radio_advertiser_a); + Ble ble_advertiser_b(radio_advertiser_b); radio_scanner.Enable(); radio_advertiser_a.Enable(); radio_advertiser_b.Enable(); @@ -1069,17 +1065,17 @@ TEST_F(BleV2Test, CanStartStopMultipleAsyncScanningWithDifferentServiceIds) { ble_advertiser_a.StartAdvertising( std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes_a); + Ble::AdvertisingType::kRegular, advertisement_bytes_a); ble_advertiser_b.StartAdvertising( std::string(kServiceIDB), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes_b); + Ble::AdvertisingType::kRegular, advertisement_bytes_b); ble_scanner.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch_a](BleV2Peripheral peripheral, + [&found_latch_a](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1094,7 +1090,7 @@ TEST_F(BleV2Test, CanStartStopMultipleAsyncScanningWithDifferentServiceIds) { /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch_b](BleV2Peripheral peripheral, + [&found_latch_b](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1111,15 +1107,15 @@ TEST_F(BleV2Test, CanStartStopMultipleAsyncScanningWithDifferentServiceIds) { env_.Stop(); } -TEST_F(BleV2Test, StartMultipleAsyncScanningDiscoverAndLostPeripheral) { +TEST_F(BleTest, StartMultipleAsyncScanningDiscoverAndLostPeripheral) { env_.SetFeatureFlags({FeatureFlags{.enable_ble_v2_async_scanning = true}}); env_.Start(); BluetoothRadio radio_scanner; BluetoothRadio radio_advertiser_a; BluetoothRadio radio_advertiser_b; - BleV2 ble_scanner(radio_scanner); - BleV2 ble_advertiser_a(radio_advertiser_a); - BleV2 ble_advertiser_b(radio_advertiser_b); + Ble ble_scanner(radio_scanner); + Ble ble_advertiser_a(radio_advertiser_a); + Ble ble_advertiser_b(radio_advertiser_b); radio_scanner.Enable(); radio_advertiser_a.Enable(); radio_advertiser_b.Enable(); @@ -1132,17 +1128,17 @@ TEST_F(BleV2Test, StartMultipleAsyncScanningDiscoverAndLostPeripheral) { ble_advertiser_a.StartAdvertising( std::string(kServiceIDA), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes_a); + Ble::AdvertisingType::kRegular, advertisement_bytes_a); ble_advertiser_b.StartAdvertising( std::string(kServiceIDB), PowerLevel::kHighPower, - BleV2::AdvertisingType::kRegular, advertisement_bytes_b); + Ble::AdvertisingType::kRegular, advertisement_bytes_b); ble_scanner.StartScanning( std::string(kServiceIDA), Pcp::kP2pPointToPoint, PowerLevel::kHighPower, /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch_a](BleV2Peripheral peripheral, + [&found_latch_a](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1151,7 +1147,7 @@ TEST_F(BleV2Test, StartMultipleAsyncScanningDiscoverAndLostPeripheral) { found_latch_a.CountDown(); }, .peripheral_lost_cb = - [&lost_latch_a](BleV2Peripheral peripheral, + [&lost_latch_a](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1166,7 +1162,7 @@ TEST_F(BleV2Test, StartMultipleAsyncScanningDiscoverAndLostPeripheral) { /*include_dct_advertisement=*/false, mediums::DiscoveredPeripheralCallback{ .peripheral_discovered_cb = - [&found_latch_b](BleV2Peripheral peripheral, + [&found_latch_b](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { @@ -1175,7 +1171,7 @@ TEST_F(BleV2Test, StartMultipleAsyncScanningDiscoverAndLostPeripheral) { found_latch_b.CountDown(); }, .peripheral_lost_cb = - [&lost_latch_b](BleV2Peripheral peripheral, + [&lost_latch_b](BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { diff --git a/connections/implementation/mediums/mediums.cc b/connections/implementation/mediums/mediums.cc index 14bcbfbc..c1a3be59 100644 --- a/connections/implementation/mediums/mediums.cc +++ b/connections/implementation/mediums/mediums.cc @@ -13,7 +13,9 @@ // limitations under the License. #include "connections/implementation/mediums/mediums.h" + #include "connections/implementation/mediums/awdl.h" +#include "connections/implementation/mediums/ble.h" namespace nearby { namespace connections { @@ -22,7 +24,7 @@ BluetoothRadio& Mediums::GetBluetoothRadio() { return bluetooth_radio_; } BluetoothClassic& Mediums::GetBluetoothClassic() { return bluetooth_classic_; } -BleV2& Mediums::GetBleV2() { return ble_v2_; } +Ble& Mediums::GetBle() { return ble_; } Wifi& Mediums::GetWifi() { return wifi_; } diff --git a/connections/implementation/mediums/mediums.h b/connections/implementation/mediums/mediums.h index e84f8f6a..9cacfc31 100644 --- a/connections/implementation/mediums/mediums.h +++ b/connections/implementation/mediums/mediums.h @@ -16,7 +16,7 @@ #define CORE_INTERNAL_MEDIUMS_MEDIUMS_H_ #include "connections/implementation/mediums/awdl.h" -#include "connections/implementation/mediums/ble_v2.h" +#include "connections/implementation/mediums/ble.h" #include "connections/implementation/mediums/bluetooth_classic.h" #include "connections/implementation/mediums/bluetooth_radio.h" #ifdef NO_WEBRTC @@ -25,8 +25,8 @@ #include "connections/implementation/mediums/webrtc.h" #endif #include "connections/implementation/mediums/wifi.h" -#include "connections/implementation/mediums/wifi_hotspot.h" #include "connections/implementation/mediums/wifi_direct.h" +#include "connections/implementation/mediums/wifi_hotspot.h" #include "connections/implementation/mediums/wifi_lan.h" namespace nearby { @@ -45,7 +45,7 @@ class Mediums { BluetoothClassic& GetBluetoothClassic(); // Returns a handle to the Ble medium. - BleV2& GetBleV2(); + Ble& GetBle(); // Returns a handle to the Wifi medium. Wifi& GetWifi(); @@ -76,7 +76,7 @@ class Mediums { // corresponding radio. BluetoothRadio bluetooth_radio_; BluetoothClassic bluetooth_classic_{bluetooth_radio_}; - BleV2 ble_v2_{bluetooth_radio_}; + Ble ble_{bluetooth_radio_}; Wifi wifi_; WifiLan wifi_lan_; WifiHotspot wifi_hotspot_; diff --git a/connections/implementation/p2p_cluster_pcp_handler.cc b/connections/implementation/p2p_cluster_pcp_handler.cc index 4f988ca3..c882bf51 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.cc +++ b/connections/implementation/p2p_cluster_pcp_handler.cc @@ -32,8 +32,8 @@ #include "connections/implementation/awdl_endpoint_channel.h" #include "connections/implementation/base_pcp_handler.h" #include "connections/implementation/ble_advertisement.h" +#include "connections/implementation/ble_endpoint_channel.h" #include "connections/implementation/ble_l2cap_endpoint_channel.h" -#include "connections/implementation/ble_v2_endpoint_channel.h" #include "connections/implementation/bluetooth_device_name.h" #include "connections/implementation/bluetooth_endpoint_channel.h" #include "connections/implementation/bwu_manager.h" @@ -44,8 +44,8 @@ #include "connections/implementation/injected_bluetooth_device_store.h" #include "connections/implementation/mediums/advertisements/advertisement_util.h" #include "connections/implementation/mediums/advertisements/dct_advertisement.h" -#include "connections/implementation/mediums/ble_v2.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" #include "connections/implementation/mediums/bluetooth_classic.h" #include "connections/implementation/mediums/mediums.h" #include "connections/implementation/mediums/utils.h" @@ -62,7 +62,7 @@ #include "internal/flags/nearby_flags.h" #include "internal/interop/device.h" #include "internal/platform/awdl.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_classic.h" #include "internal/platform/byte_array.h" @@ -115,7 +115,7 @@ P2pClusterPcpHandler::P2pClusterPcpHandler( awdl_medium_(mediums->GetAwdl()), bluetooth_radio_(mediums->GetBluetoothRadio()), bluetooth_medium_(mediums->GetBluetoothClassic()), - ble_v2_medium_(mediums->GetBleV2()), + ble_medium_(mediums->GetBle()), wifi_lan_medium_(mediums->GetWifiLan()), wifi_hotspot_medium_(mediums->GetWifiHotspot()), wifi_direct_medium_(mediums->GetWifiDirect()), @@ -141,7 +141,7 @@ std::vector P2pClusterPcpHandler::GetConnectionMediumsByPriority() { if (bluetooth_medium_.IsAvailable()) { mediums.push_back(BLUETOOTH); } - if (ble_v2_medium_.IsAvailable()) { + if (ble_medium_.IsAvailable()) { mediums.push_back(BLE); } @@ -223,13 +223,13 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( if (api::ImplementationPlatform::GetCurrentOS() == api::OSName::kChromeOS || api::ImplementationPlatform::GetCurrentOS() == api::OSName::kLinux) { - if (ble_v2_medium_.StartLegacyAdvertising( + if (ble_medium_.StartLegacyAdvertising( service_id, local_endpoint_id, advertising_options.fast_advertisement_service_uuid)) { LOG(INFO) << __func__ - << "Ble v2 started advertising for legacy device."; + << "Ble started advertising for legacy device."; mediums_started_successfully.push_back(bluetooth_medium); - VLOG(1) << __func__ << "After Ble v2, BT added"; + VLOG(1) << __func__ << "After Ble, BT added"; bluetooth_classic_advertiser_client_id_ = client->GetClientId(); } else { LOG(WARNING) << "P2pClusterPcpHandler::StartAdvertisingImpl: " @@ -255,9 +255,9 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( if (advertising_options.allowed.ble) { ErrorOr ble_result = {Error(OperationResultCode::DETAIL_UNKNOWN)}; - ble_result = StartBleV2Advertising(client, service_id, local_endpoint_id, - local_endpoint_info, advertising_options, - web_rtc_state); + ble_result = StartBleAdvertising(client, service_id, local_endpoint_id, + local_endpoint_info, advertising_options, + web_rtc_state); if (ble_result.has_value() && ble_result.value() != UNKNOWN_MEDIUM) { VLOG(1) << "P2pClusterPcpHandler::StartAdvertisingImpl: Ble added"; mediums_started_successfully.push_back(ble_result.value()); @@ -299,7 +299,7 @@ Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) { bluetooth_medium_.TurnOffDiscoverability(); if (api::ImplementationPlatform::GetCurrentOS() == api::OSName::kChromeOS || api::ImplementationPlatform::GetCurrentOS() == api::OSName::kLinux) { - ble_v2_medium_.StopLegacyAdvertising(client->GetAdvertisingServiceId()); + ble_medium_.StopLegacyAdvertising(client->GetAdvertisingServiceId()); } bluetooth_classic_advertiser_client_id_ = 0; } else { @@ -310,11 +310,11 @@ Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) { } bluetooth_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); - ble_v2_medium_.StopAdvertising(client->GetAdvertisingServiceId()); - ble_v2_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); + ble_medium_.StopAdvertising(client->GetAdvertisingServiceId()); + ble_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); if (NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature::kEnableBleL2cap)) { - ble_v2_medium_.StopAcceptingL2capConnections( + ble_medium_.StopAcceptingL2capConnections( client->GetAdvertisingServiceId()); } @@ -529,7 +529,7 @@ void P2pClusterPcpHandler::BluetoothDeviceLostHandler( }); } -bool P2pClusterPcpHandler::IsRecognizedBleV2Endpoint( +bool P2pClusterPcpHandler::IsRecognizedBleEndpoint( absl::string_view service_id, const BleAdvertisement& advertisement) const { if (!advertisement.IsValid()) { LOG(INFO) << "BleAdvertisement doesn't conform to the format, discarding."; @@ -570,8 +570,8 @@ bool P2pClusterPcpHandler::IsRecognizedBleV2Endpoint( return true; } -void P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler( - ClientProxy* client, BleV2Peripheral peripheral, +void P2pClusterPcpHandler::BlePeripheralDiscoveredHandler( + ClientProxy* client, BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { RunOnPcpHandlerThread( @@ -605,13 +605,13 @@ void P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler( // Make sure the BLE advertisement points to a valid // endpoint we're discovering. - if (!IsRecognizedBleV2Endpoint(service_id, advertisement)) { - LOG(ERROR) << "IsRecognizedBleV2Endpoint failed"; + if (!IsRecognizedBleEndpoint(service_id, advertisement)) { + LOG(ERROR) << "IsRecognizedBleEndpoint failed"; return; } // Report the discovered endpoint to the client. - BleV2EndpointState ble_endpoint_state; + BleEndpointFeatureState ble_endpoint_state; ByteArray peripheral_id = peripheral.GetId(); found_endpoints_in_ble_discover_cb_.insert( {peripheral_id, ble_endpoint_state}); @@ -622,7 +622,7 @@ void P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler( StopEndpointLostByMediumAlarm(advertisement.GetEndpointId(), BLE); OnEndpointFound( client, - std::make_shared(BleV2Endpoint{ + std::make_shared(BleEndpoint{ {advertisement.GetEndpointId(), advertisement.GetEndpointInfo(), service_id, BLE, advertisement.GetWebRtcState()}, std::move(peripheral), @@ -666,8 +666,8 @@ void P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler( } // TODO(b/222392304): More test coverage. -void P2pClusterPcpHandler::BleV2PeripheralLostHandler( - ClientProxy* client, BleV2Peripheral peripheral, +void P2pClusterPcpHandler::BlePeripheralLostHandler( + ClientProxy* client, BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { RunOnPcpHandlerThread( @@ -692,7 +692,7 @@ void P2pClusterPcpHandler::BleV2PeripheralLostHandler( // Make sure the BLE advertisement points to a valid // endpoint we're discovering. - if (!IsRecognizedBleV2Endpoint(service_id, advertisement)) return; + if (!IsRecognizedBleEndpoint(service_id, advertisement)) return; // Remove this BlePeripheral from found_ble_endpoints_, and // report the endpoint as lost to the client. @@ -701,7 +701,7 @@ void P2pClusterPcpHandler::BleV2PeripheralLostHandler( if (item == found_endpoints_in_ble_discover_cb_.end()) { return; } - BleV2EndpointState ble_endpoint_state(item->second); + BleEndpointFeatureState ble_endpoint_state(item->second); found_endpoints_in_ble_discover_cb_.erase(item); if (ble_endpoint_state.ble) { @@ -741,8 +741,8 @@ void P2pClusterPcpHandler::BleV2PeripheralLostHandler( }); } -void P2pClusterPcpHandler::BleV2InstantLostHandler( - ClientProxy* client, BleV2Peripheral peripheral, +void P2pClusterPcpHandler::BleInstantLostHandler( + ClientProxy* client, BlePeripheral peripheral, const std::string& service_id, const ByteArray& advertisement_bytes, bool fast_advertisement) { RunOnPcpHandlerThread( @@ -770,7 +770,7 @@ void P2pClusterPcpHandler::BleV2InstantLostHandler( // Make sure the BLE advertisement points to a valid // endpoint we're discovering. - if (!IsRecognizedBleV2Endpoint(service_id, advertisement)) return; + if (!IsRecognizedBleEndpoint(service_id, advertisement)) return; // Remove this BlePeripheral from found_ble_endpoints_, and // report the endpoint as lost to the client. @@ -788,7 +788,7 @@ void P2pClusterPcpHandler::BleV2InstantLostHandler( }); } -void P2pClusterPcpHandler::BleV2LegacyDeviceDiscoveredHandler() { +void P2pClusterPcpHandler::BleLegacyDeviceDiscoveredHandler() { if (!NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature:: kDisableBluetoothClassicScanning)) { @@ -1098,14 +1098,14 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartDiscoveryImpl( if (discovery_options.allowed.ble) { ErrorOr ble_result = {Error(OperationResultCode::DETAIL_UNKNOWN)}; - ble_result = StartBleV2Scanning(client, service_id, discovery_options); - Medium ble_v2_medium = UNKNOWN_MEDIUM; + ble_result = StartBleScanning(client, service_id, discovery_options); + Medium ble_medium = UNKNOWN_MEDIUM; if (ble_result.has_value()) { - ble_v2_medium = ble_result.value(); + ble_medium = ble_result.value(); } - if (ble_v2_medium != UNKNOWN_MEDIUM) { - LOG(INFO) << "P2pClusterPcpHandler::StartDiscoveryImpl: Ble v2 added"; - mediums_started_successfully.push_back(ble_v2_medium); + if (ble_medium != UNKNOWN_MEDIUM) { + LOG(INFO) << "P2pClusterPcpHandler::StartDiscoveryImpl: Ble added"; + mediums_started_successfully.push_back(ble_medium); } std::unique_ptr @@ -1184,7 +1184,7 @@ Status P2pClusterPcpHandler::StopDiscoveryImpl(ClientProxy* client) { << " because it is not in discovery."; } - ble_v2_medium_.StopScanning(client->GetDiscoveryServiceId()); + ble_medium_.StopScanning(client->GetDiscoveryServiceId()); if (NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature:: @@ -1245,9 +1245,9 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::ConnectImpl( break; } case BLE: { - auto* ble_v2_endpoint = down_cast(endpoint); - if (ble_v2_endpoint) { - return BleV2ConnectImpl(client, ble_v2_endpoint); + auto* ble_endpoint = down_cast(endpoint); + if (ble_endpoint) { + return BleConnectImpl(client, ble_endpoint); } break; @@ -1318,30 +1318,30 @@ P2pClusterPcpHandler::StartListeningForIncomingConnectionsImpl( if (options.enable_ble_listening && NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature::kEnableBleL2cap) && - !ble_v2_medium_.IsAcceptingL2capConnections(std::string(service_id))) { - if (!ble_v2_medium_.StartAcceptingL2capConnections( + !ble_medium_.IsAcceptingL2capConnections(std::string(service_id))) { + if (!ble_medium_.StartAcceptingL2capConnections( std::string(service_id), absl::bind_front( &P2pClusterPcpHandler::BleL2capConnectionAcceptedHandler, this, client_proxy, local_endpoint_id, options.listening_endpoint_type))) { LOG(WARNING) << "Failed to start listening for incoming L2CAP " - "connections on ble_v2"; + "connections on ble"; } else { accepting_ble_connections_success = true; } } if (options.enable_ble_listening && - !ble_v2_medium_.IsAcceptingConnections(std::string(service_id))) { - if (!ble_v2_medium_.StartAcceptingConnections( + !ble_medium_.IsAcceptingConnections(std::string(service_id))) { + if (!ble_medium_.StartAcceptingConnections( std::string(service_id), absl::bind_front( - &P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, this, + &P2pClusterPcpHandler::BleConnectionAcceptedHandler, this, client_proxy, local_endpoint_id, options.listening_endpoint_type))) { LOG(WARNING) - << "Failed to start listening for incoming connections on ble_v2"; + << "Failed to start listening for incoming connections on ble"; } else { accepting_ble_connections_success = true; } @@ -1409,12 +1409,11 @@ void P2pClusterPcpHandler::StopListeningForIncomingConnectionsImpl( } } - if (ble_v2_medium_.IsAcceptingConnections( + if (ble_medium_.IsAcceptingConnections( client->GetListeningForIncomingConnectionsServiceId())) { - if (!ble_v2_medium_.StopAcceptingConnections( + if (!ble_medium_.StopAcceptingConnections( client->GetListeningForIncomingConnectionsServiceId())) { - LOG(WARNING) - << "Unable to stop ble_v2 medium from accepting connections."; + LOG(WARNING) << "Unable to stop ble medium from accepting connections."; } } } @@ -1429,8 +1428,8 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl( // ble if (NeedsToTurnOffAdvertisingMedium(BLE, old_options, advertising_options) || needs_restart) { - mediums_->GetBleV2().StopAdvertising(std::string(service_id)); - mediums_->GetBleV2().StopAcceptingConnections(std::string(service_id)); + mediums_->GetBle().StopAdvertising(std::string(service_id)); + mediums_->GetBle().StopAcceptingConnections(std::string(service_id)); } // awdl if (NearbyFlags::GetInstance().GetBoolFlag( @@ -1458,7 +1457,7 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl( std::string(service_id)); if (api::ImplementationPlatform::GetCurrentOS() == api::OSName::kChromeOS || api::ImplementationPlatform::GetCurrentOS() == api::OSName::kLinux) { - mediums_->GetBleV2().StopLegacyAdvertising( + mediums_->GetBle().StopLegacyAdvertising( client->GetAdvertisingServiceId()); } } @@ -1487,7 +1486,7 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl( operation_result_with_mediums.push_back(*operation_result_with_medium); } else { ErrorOr ble_result = {Error(OperationResultCode::DETAIL_UNKNOWN)}; - ble_result = StartBleV2Advertising( + ble_result = StartBleAdvertising( client, std::string(service_id), std::string(local_endpoint_id), ByteArray(std::string(local_endpoint_info)), advertising_options, web_rtc_state); @@ -1591,19 +1590,19 @@ P2pClusterPcpHandler::UpdateAdvertisingOptionsImpl( api::OSName::kChromeOS || api::ImplementationPlatform::GetCurrentOS() == api::OSName::kLinux) { - if (ble_v2_medium_.StartLegacyAdvertising( + if (ble_medium_.StartLegacyAdvertising( std::string(service_id), std::string(local_endpoint_id), advertising_options.fast_advertisement_service_uuid)) { LOG(INFO) << __func__ - << "Ble v2 started advertising for legacy device."; + << "Ble started advertising for legacy device."; restarted_mediums.push_back(BLUETOOTH); LOG(INFO) << __func__ - << "After Ble v2 started advertising, for " + << "After Ble started advertising, for " "legacy, BT added to restarted mediums"; } else { LOG(WARNING) << __func__ - << "BLE v2 failed advertising for legacy device, revert BTC"; + << "BLE failed advertising for legacy device, revert BTC"; bluetooth_medium_.TurnOffDiscoverability(); bluetooth_medium_.StopAcceptingConnections(std::string(service_id)); } @@ -1654,7 +1653,7 @@ P2pClusterPcpHandler::UpdateDiscoveryOptionsImpl( bool needs_restart = old_options.low_power != discovery_options.low_power; // ble if (NeedsToTurnOffDiscoveryMedium(BLE, old_options, discovery_options)) { - ble_v2_medium_.StopScanning(std::string(service_id)); + ble_medium_.StopScanning(std::string(service_id)); StartEndpointLostByMediumAlarms(client, BLE); } // bt classic @@ -1700,13 +1699,13 @@ P2pClusterPcpHandler::UpdateDiscoveryOptionsImpl( operation_result_with_mediums.push_back(*operation_result_with_medium); } else { ErrorOr ble_result = {Error(OperationResultCode::DETAIL_UNKNOWN)}; - ble_result = StartBleV2Scanning(client, std::string(service_id), - discovery_options); + ble_result = + StartBleScanning(client, std::string(service_id), discovery_options); if (ble_result.has_value()) { restarted_mediums.push_back(BLE); } else { LOG(WARNING) << "UpdateDiscoveryOptionsImpl: unable to " - "restart blev2 scanning"; + "restart ble scanning"; } std::unique_ptr @@ -1995,7 +1994,7 @@ void P2pClusterPcpHandler::StartBluetoothDiscoveryWithPause( operation_result_with_mediums, int update_index) { if (bluetooth_radio_.IsEnabled()) { - if (ble_v2_medium_.IsExtendedAdvertisementsAvailable() && + if (ble_medium_.IsExtendedAdvertisementsAvailable() && std::find(mediums_started_successfully.begin(), mediums_started_successfully.end(), BLE) != mediums_started_successfully.end()) { @@ -2088,9 +2087,9 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BluetoothConnectImpl( .endpoint_channel = std::move(channel)}; } -void P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler( +void P2pClusterPcpHandler::BleConnectionAcceptedHandler( ClientProxy* client, absl::string_view local_endpoint_info, - NearbyDevice::Type device_type, BleV2Socket socket, + NearbyDevice::Type device_type, BleSocket socket, const std::string& service_id) { if (!socket.IsValid()) { LOG(WARNING) << "Invalid socket in accept callback(" @@ -2103,7 +2102,7 @@ void P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler( [this, client, service_id, device_type, socket = std::move(socket)]() RUN_ON_PCP_HANDLER_THREAD() mutable { ByteArray remote_peripheral_info = socket.GetRemotePeripheral().GetId(); - auto channel = std::make_unique( + auto channel = std::make_unique( service_id, std::string(remote_peripheral_info), socket); OnIncomingConnection(client, remote_peripheral_info, std::move(channel), @@ -2134,7 +2133,7 @@ void P2pClusterPcpHandler::BleL2capConnectionAcceptedHandler( }); } -ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( +ErrorOr P2pClusterPcpHandler::StartBleAdvertising( ClientProxy* client, const std::string& service_id, const std::string& local_endpoint_id, const ByteArray& local_endpoint_info, const AdvertisingOptions& advertising_options, WebRtcState web_rtc_state) { @@ -2142,17 +2141,17 @@ ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( // request comes in very quickly. BLE allows connecting over BLE itself, as // well as advertising the Bluetooth MAC address to allow connecting over // Bluetooth Classic. - LOG(INFO) << "P2pClusterPcpHandler::StartBleV2Advertising: service_id=" + LOG(INFO) << "P2pClusterPcpHandler::StartBleAdvertising: service_id=" << service_id << " : start"; ErrorOr ble_l2cap_result = 0; - if (!ble_v2_medium_.IsAcceptingConnections(service_id)) { + if (!ble_medium_.IsAcceptingConnections(service_id)) { // TODO(b/380411884): Remove this check since we shouldn't enable radio by // NC. if (!bluetooth_radio_.Enable()) { LOG(WARNING) - << "In StartBleV2Advertising(" + << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " failed to start accepting for incoming BLE connections to " @@ -2163,7 +2162,7 @@ ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( if (NearbyFlags::GetInstance().GetBoolFlag( config_package_nearby::nearby_connections_feature:: kEnableBleL2cap)) { - ble_l2cap_result = ble_v2_medium_.StartAcceptingL2capConnections( + ble_l2cap_result = ble_medium_.StartAcceptingL2capConnections( service_id, absl::bind_front( &P2pClusterPcpHandler::BleL2capConnectionAcceptedHandler, this, @@ -2171,23 +2170,23 @@ ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( NearbyDevice::Type::kConnectionsDevice)); } - ErrorOr ble_v2_result = ble_v2_medium_.StartAcceptingConnections( + ErrorOr ble_result = ble_medium_.StartAcceptingConnections( service_id, - absl::bind_front(&P2pClusterPcpHandler::BleV2ConnectionAcceptedHandler, + absl::bind_front(&P2pClusterPcpHandler::BleConnectionAcceptedHandler, this, client, local_endpoint_info.AsStringView(), NearbyDevice::Type::kConnectionsDevice)); - if (ble_v2_result.has_error() && ble_l2cap_result.has_error()) { + if (ble_result.has_error() && ble_l2cap_result.has_error()) { LOG(WARNING) - << "In StartBleV2Advertising(" + << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " failed to start accepting for incoming BLE connections to " "service_id=" << service_id; - return {Error(ble_v2_result.error().operation_result_code().value())}; + return {Error(ble_result.error().operation_result_code().value())}; } LOG(INFO) - << "In StartBleV2Advertising(" + << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " started accepting for incoming BLE connections to service_id=" @@ -2205,7 +2204,7 @@ ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( // NC. if (!bluetooth_radio_.Enable()) { LOG(WARNING) - << "In BT StartBleV2Advertising(" + << "In BT StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " failed to start accepting for incoming BLE connections to " @@ -2222,17 +2221,17 @@ ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( NearbyDevice::Type::kConnectionsDevice)); if (accept_result.has_error()) { LOG(WARNING) - << "In BT StartBleV2Advertising(" + << "In BT StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " failed to start accepting for incoming BLE connections to " "service_id=" << service_id; - ble_v2_medium_.StopAcceptingConnections(service_id); + ble_medium_.StopAcceptingConnections(service_id); return {Error(accept_result.error().operation_result_code().value())}; } LOG(INFO) - << "In BT StartBleV2Advertising(" + << "In BT StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " started accepting for incoming BLE connections to service_id=" @@ -2240,7 +2239,7 @@ ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( } } - LOG(INFO) << "In StartBleV2Advertising(" + LOG(INFO) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " start to generate BleAdvertisement with service_id=" @@ -2284,42 +2283,42 @@ ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( } } if (advertisement_bytes.Empty() && dct_advertisement_bytes.Empty()) { - LOG(WARNING) << "In StartBleV2Advertising(" + LOG(WARNING) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " failed to create an advertisement."; - ble_v2_medium_.StopAcceptingConnections(service_id); + ble_medium_.StopAcceptingConnections(service_id); return {Error(OperationResultCode::NEARBY_BLE_ADVERTISE_TO_BYTES_FAILURE)}; } - LOG(INFO) << "In StartBleV2Advertising(" + LOG(INFO) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " generated BleAdvertisement with service_id=" << service_id; - ErrorOr ble_v2_result = false; + ErrorOr ble_result = false; if (dct_advertisement_bytes.Empty()) { - ble_v2_result = ble_v2_medium_.StartAdvertising( + ble_result = ble_medium_.StartAdvertising( service_id, power_level, advertising_options.fast_advertisement_service_uuid.empty() - ? BleV2::AdvertisingType::kRegular - : BleV2::AdvertisingType::kFast, + ? Ble::AdvertisingType::kRegular + : Ble::AdvertisingType::kFast, advertisement_bytes); } else { - ble_v2_result = ble_v2_medium_.StartAdvertising( - service_id, power_level, BleV2::AdvertisingType::kDct, - dct_advertisement_bytes); + ble_result = ble_medium_.StartAdvertising(service_id, power_level, + Ble::AdvertisingType::kDct, + dct_advertisement_bytes); } - if (ble_v2_result.has_error()) { - LOG(WARNING) << "In StartBleV2Advertising(" + if (ble_result.has_error()) { + LOG(WARNING) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " couldn't start BLE Advertising with BleAdvertisement " << absl::BytesToHexString(advertisement_bytes.data()); - ble_v2_medium_.StopAcceptingConnections(service_id); - return {Error(ble_v2_result.error().operation_result_code().value())}; + ble_medium_.StopAcceptingConnections(service_id); + return {Error(ble_result.error().operation_result_code().value())}; } - LOG(INFO) << "In StartBleV2Advertising(" + LOG(INFO) << "In StartBleAdvertising(" << absl::BytesToHexString(local_endpoint_info.data()) << "), client=" << client->GetClientId() << " started BLE Advertising with BleAdvertisement " @@ -2327,50 +2326,50 @@ ErrorOr P2pClusterPcpHandler::StartBleV2Advertising( return {BLE}; } -ErrorOr P2pClusterPcpHandler::StartBleV2Scanning( +ErrorOr P2pClusterPcpHandler::StartBleScanning( ClientProxy* client, const std::string& service_id, const DiscoveryOptions& discovery_options) { PowerLevel power_level = discovery_options.low_power ? PowerLevel::kLowPower : PowerLevel::kHighPower; // TODO(b/380411884): Remove this check since we shouldn't enable radio by NC. if (!bluetooth_radio_.Enable()) { - LOG(INFO) << "In StartBleV2Scanning(), client=" << client->GetClientId() + LOG(INFO) << "In StartBleScanning(), client=" << client->GetClientId() << " couldn't start scanning on BLE for service_id=" << service_id; return {Error(OperationResultCode::DEVICE_STATE_RADIO_ENABLING_FAILURE)}; } if (discovery_options.ble_options.alternate_uuid.has_value()) { - ble_v2_medium_.AddAlternateUuidForService( + ble_medium_.AddAlternateUuidForService( *discovery_options.ble_options.alternate_uuid, service_id); } - ErrorOr ble_v2_result = ble_v2_medium_.StartScanning( + ErrorOr ble_result = ble_medium_.StartScanning( service_id, GetPcp(), power_level, client->IsDctEnabled(), { .peripheral_discovered_cb = absl::bind_front( - &P2pClusterPcpHandler::BleV2PeripheralDiscoveredHandler, this, + &P2pClusterPcpHandler::BlePeripheralDiscoveredHandler, this, client), .peripheral_lost_cb = absl::bind_front( - &P2pClusterPcpHandler::BleV2PeripheralLostHandler, this, client), + &P2pClusterPcpHandler::BlePeripheralLostHandler, this, client), .instant_lost_cb = absl::bind_front( - &P2pClusterPcpHandler::BleV2InstantLostHandler, this, client), + &P2pClusterPcpHandler::BleInstantLostHandler, this, client), .legacy_device_discovered_cb = absl::bind_front( - &P2pClusterPcpHandler::BleV2LegacyDeviceDiscoveredHandler, this), + &P2pClusterPcpHandler::BleLegacyDeviceDiscoveredHandler, this), }); - if (!ble_v2_result.has_error()) { - LOG(INFO) << "In StartBleV2Scanning(), client=" << client->GetClientId() + if (!ble_result.has_error()) { + LOG(INFO) << "In StartBleScanning(), client=" << client->GetClientId() << " started scanning for BLE advertisements for service_id=" << service_id; return {BLE}; } - LOG(INFO) << "In StartBleV2Scanning(), client=" << client->GetClientId() + LOG(INFO) << "In StartBleScanning(), client=" << client->GetClientId() << " couldn't start scanning on BLE for service_id=" << service_id; - return {Error(ble_v2_result.error().operation_result_code().value())}; + return {Error(ble_result.error().operation_result_code().value())}; } -BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleV2ConnectImpl( - ClientProxy* client, BleV2Endpoint* endpoint) { - BleV2Peripheral& peripheral = endpoint->ble_peripheral; +BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleConnectImpl( + ClientProxy* client, BleEndpoint* endpoint) { + BlePeripheral& peripheral = endpoint->ble_peripheral; VLOG(1) << "Client " << client->GetClientId() << " is attempting to connect to (" << peripheral.ToReadableString() @@ -2381,11 +2380,11 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleV2ConnectImpl( peripheral.GetPsm() != mediums::BleAdvertisementHeader::kDefaultPsmValue) { ErrorOr ble_l2cap_socket_result = - ble_v2_medium_.ConnectOverL2cap( + ble_medium_.ConnectOverL2cap( endpoint->service_id, peripheral, client->GetCancellationFlag(endpoint->endpoint_id)); if (!ble_l2cap_socket_result.has_error()) { - LOG(INFO) << "In BleV2ConnectImpl(), connected to Ble L2CAP device " + LOG(INFO) << "In BleConnectImpl(), connected to Ble L2CAP device " << absl::BytesToHexString(peripheral.GetId().data()) << " for endpoint(id=" << endpoint->endpoint_id << ")."; auto channel = std::make_unique( @@ -2398,18 +2397,18 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleV2ConnectImpl( .endpoint_channel = std::move(channel), }; } else { - LOG(WARNING) << "In BleV2ConnectImpl(), failed to connect to Ble L2CAP " + LOG(WARNING) << "In BleConnectImpl(), failed to connect to Ble L2CAP " "device " << absl::BytesToHexString(peripheral.GetId().data()) << " for endpoint(id=" << endpoint->endpoint_id << ")."; } } - ErrorOr ble_socket_result = ble_v2_medium_.Connect( - endpoint->service_id, peripheral, - client->GetCancellationFlag(endpoint->endpoint_id)); + ErrorOr ble_socket_result = + ble_medium_.Connect(endpoint->service_id, peripheral, + client->GetCancellationFlag(endpoint->endpoint_id)); if (ble_socket_result.has_error()) { - LOG(ERROR) << "In BleV2ConnectImpl(), failed to connect to BLE device " + LOG(ERROR) << "In BleConnectImpl(), failed to connect to BLE device " << absl::BytesToHexString(peripheral.GetId().data()) << " for endpoint(id=" << endpoint->endpoint_id << ")."; return BasePcpHandler::ConnectImplResult{ @@ -2419,7 +2418,7 @@ BasePcpHandler::ConnectImplResult P2pClusterPcpHandler::BleV2ConnectImpl( }; } - auto channel = std::make_unique( + auto channel = std::make_unique( endpoint->service_id, /*channel_name=*/endpoint->endpoint_id, ble_socket_result.value()); @@ -2614,10 +2613,10 @@ ErrorOr P2pClusterPcpHandler::StartWifiLanAdvertising( NearbyDevice::Type::kConnectionsDevice)); if (wifi_lan_result.has_error()) { LOG(WARNING) << "In StartWifiLanAdvertising(" - << absl::BytesToHexString(local_endpoint_info.data()) - << "), client=" << client->GetClientId() - << " couldn't advertise with WifiLanServiceInfo " - << nsd_service_info.GetServiceName(); + << absl::BytesToHexString(local_endpoint_info.data()) + << "), client=" << client->GetClientId() + << " couldn't advertise with WifiLanServiceInfo " + << nsd_service_info.GetServiceName(); return {Error(wifi_lan_result.error().operation_result_code().value())}; } VLOG(1) << "In StartWifiLanAdvertising(" diff --git a/connections/implementation/p2p_cluster_pcp_handler.h b/connections/implementation/p2p_cluster_pcp_handler.h index fac5225c..18a04815 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.h +++ b/connections/implementation/p2p_cluster_pcp_handler.h @@ -33,7 +33,7 @@ #include "connections/implementation/endpoint_manager.h" #include "connections/implementation/injected_bluetooth_device_store.h" #include "connections/implementation/mediums/awdl.h" -#include "connections/implementation/mediums/ble_v2.h" +#include "connections/implementation/mediums/ble.h" #include "connections/implementation/mediums/bluetooth_classic.h" #include "connections/implementation/mediums/bluetooth_radio.h" #include "connections/implementation/mediums/mediums.h" @@ -46,7 +46,7 @@ #include "connections/status.h" #include "connections/v3/connection_listening_options.h" #include "internal/interop/device.h" -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_classic.h" #include "internal/platform/nsd_service_info.h" @@ -154,7 +154,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { ByteArray endpoint_info; }; - struct BleV2EndpointState { + struct BleEndpointFeatureState { bool ble = false; bool l2cap = false; bool bt = false; @@ -162,7 +162,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { using BluetoothDiscoveredDeviceCallback = BluetoothClassic::DiscoveredDeviceCallback; - using BleV2DiscoveredPeripheralCallback = BleV2::DiscoveredPeripheralCallback; + using BleDiscoveredPeripheralCallback = Ble::DiscoveredPeripheralCallback; using WifiLanDiscoveredServiceCallback = WifiLan::DiscoveredServiceCallback; using AwdlDiscoveredServiceCallback = Awdl::DiscoveredServiceCallback; @@ -214,44 +214,43 @@ class P2pClusterPcpHandler : public BasePcpHandler { BasePcpHandler::ConnectImplResult BluetoothConnectImpl( ClientProxy* client, BluetoothEndpoint* endpoint); - // BleV2 - bool IsRecognizedBleV2Endpoint(absl::string_view service_id, - const BleAdvertisement& advertisement) const; - void BleV2PeripheralDiscoveredHandler(ClientProxy* client, - BleV2Peripheral peripheral, - const std::string& service_id, - const ByteArray& advertisement_bytes, - bool fast_advertisement); - void BleV2PeripheralLostHandler(ClientProxy* client, - BleV2Peripheral peripheral, - const std::string& service_id, - const ByteArray& advertisement_bytes, - bool fast_advertisement); - void BleV2InstantLostHandler(ClientProxy* client, BleV2Peripheral peripheral, - const std::string& service_id, - const ByteArray& advertisement_bytes, - bool fast_advertisement); - void BleV2LegacyDeviceDiscoveredHandler(); - void BleV2ConnectionAcceptedHandler(ClientProxy* client, - absl::string_view local_endpoint_info, - NearbyDevice::Type device_type, - BleV2Socket socket, - const std::string& service_id); + // Ble + bool IsRecognizedBleEndpoint(absl::string_view service_id, + const BleAdvertisement& advertisement) const; + void BlePeripheralDiscoveredHandler(ClientProxy* client, + BlePeripheral peripheral, + const std::string& service_id, + const ByteArray& advertisement_bytes, + bool fast_advertisement); + void BlePeripheralLostHandler(ClientProxy* client, BlePeripheral peripheral, + const std::string& service_id, + const ByteArray& advertisement_bytes, + bool fast_advertisement); + void BleInstantLostHandler(ClientProxy* client, BlePeripheral peripheral, + const std::string& service_id, + const ByteArray& advertisement_bytes, + bool fast_advertisement); + void BleLegacyDeviceDiscoveredHandler(); + void BleConnectionAcceptedHandler(ClientProxy* client, + absl::string_view local_endpoint_info, + NearbyDevice::Type device_type, + BleSocket socket, + const std::string& service_id); void BleL2capConnectionAcceptedHandler(ClientProxy* client, absl::string_view local_endpoint_info, NearbyDevice::Type device_type, BleL2capSocket socket, const std::string& service_id); - ErrorOr StartBleV2Advertising( + ErrorOr StartBleAdvertising( ClientProxy* client, const std::string& service_id, const std::string& local_endpoint_id, const ByteArray& local_endpoint_info, const AdvertisingOptions& advertising_options, WebRtcState web_rtc_state); - ErrorOr StartBleV2Scanning( + ErrorOr StartBleScanning( ClientProxy* client, const std::string& service_id, const DiscoveryOptions& discovery_options); - BasePcpHandler::ConnectImplResult BleV2ConnectImpl(ClientProxy* client, - BleV2Endpoint* endpoint); + BasePcpHandler::ConnectImplResult BleConnectImpl(ClientProxy* client, + BleEndpoint* endpoint); // Awdl void AwdlServiceDiscoveredHandler(ClientProxy* client, NsdServiceInfo service_info, @@ -301,7 +300,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { Awdl& awdl_medium_; BluetoothRadio& bluetooth_radio_; BluetoothClassic& bluetooth_medium_; - BleV2& ble_v2_medium_; + Ble& ble_medium_; WifiLan& wifi_lan_medium_; WifiHotspot& wifi_hotspot_medium_; WifiDirect& wifi_direct_medium_; @@ -317,7 +316,7 @@ class P2pClusterPcpHandler : public BasePcpHandler { absl::flat_hash_map found_ble_endpoints_; // Maps a BlePeripheral.Id_ to its corresponding BleEndpointState. - absl::flat_hash_map + absl::flat_hash_map found_endpoints_in_ble_discover_cb_; // Maps service id to its client. diff --git a/connections/implementation/p2p_cluster_pcp_handler_test.cc b/connections/implementation/p2p_cluster_pcp_handler_test.cc index a59fc12f..5932f8fe 100644 --- a/connections/implementation/p2p_cluster_pcp_handler_test.cc +++ b/connections/implementation/p2p_cluster_pcp_handler_test.cc @@ -175,7 +175,7 @@ TEST_F(P2pClusterPcpHandlerTest, NoBluetoothDiscoveryWhenRadioIsOff) { handler.StartDiscovery(&client_a_, service_id_, GetBluetoothAndBleDiscoveryOptions(), {}); EXPECT_FALSE(mediums.GetBluetoothClassic().IsDiscovering(service_id_)); - EXPECT_TRUE(mediums.GetBleV2().IsScanning(service_id_)); + EXPECT_TRUE(mediums.GetBle().IsScanning(service_id_)); mediums.GetBluetoothRadio().Enable(); handler.StopDiscovery(&client_a_); @@ -187,7 +187,7 @@ TEST_F(P2pClusterPcpHandlerTest, std::string endpoint_name{"endpoint_name"}; env_.Start(); - // Enable BLE V2 extended advertisement for client_a_. + // Enable BLE extended advertisement for client_a_. env_.SetBleExtendedAdvertisementsAvailable(true); Mediums mediums_a; EndpointChannelManager ecm_a; @@ -196,7 +196,7 @@ TEST_F(P2pClusterPcpHandlerTest, BwuManager bwu_a(mediums_a, em_a, ecm_a, {}, {}); P2pClusterPcpHandler handler_a(&mediums_a, &em_a, &ecm_a, &bwu_a, ibds_a); - // Disable BLE V2 extended advertisement for client_b_. + // Disable BLE extended advertisement for client_b_. env_.SetBleExtendedAdvertisementsAvailable(false); Mediums mediums_b; EndpointChannelManager ecm_b; @@ -359,14 +359,13 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, AdvertiseForLegacyDeviceWithBt) { handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_, {.endpoint_info = ByteArray{endpoint_name}}), Status{Status::kSuccess}); - // advertising for legacy device depends on both BT and BLE V2 enabled. + // advertising for legacy device depends on both BT and BLE enabled. if (std::get<0>(GetParam()).bluetooth) { - EXPECT_TRUE(mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_TRUE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } handler_a.StopAdvertising(&client_a_); if (std::get<0>(GetParam()).bluetooth) { - EXPECT_FALSE( - mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_FALSE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } env_.Stop(); } @@ -390,20 +389,19 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateAdvertisingOptions) { ASSERT_FALSE(mediums_a.GetWifiLan().IsAcceptingConnections(service_id_)); mediums_a.GetWifiLan().StopAdvertising(service_id_); ASSERT_FALSE(mediums_a.GetWifiLan().IsAdvertising(service_id_)); - mediums_a.GetBleV2().StopAcceptingConnections(service_id_); - ASSERT_FALSE(mediums_a.GetBleV2().IsAcceptingConnections(service_id_)); - mediums_a.GetBleV2().StopAdvertising(service_id_); - ASSERT_FALSE(mediums_a.GetBleV2().IsAdvertising(service_id_)); + mediums_a.GetBle().StopAcceptingConnections(service_id_); + ASSERT_FALSE(mediums_a.GetBle().IsAcceptingConnections(service_id_)); + mediums_a.GetBle().StopAdvertising(service_id_); + ASSERT_FALSE(mediums_a.GetBle().IsAdvertising(service_id_)); BooleanMediumSelector enabled = advertising_options_.allowed; if (enabled.bluetooth) { - EXPECT_FALSE( - mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_FALSE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } EXPECT_EQ( handler_a.StartAdvertising(&client_a_, service_id_, advertising_options_, {.endpoint_info = ByteArray{endpoint_name}}), Status{Status::kSuccess}); - EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsAdvertising(service_id_)); + EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsAdvertising(service_id_)); EXPECT_EQ(enabled.wifi_lan, mediums_a.GetWifiLan().IsAdvertising(service_id_)); EXPECT_EQ( @@ -412,7 +410,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateAdvertisingOptions) { EXPECT_EQ(enabled.bluetooth, mediums_a.GetBluetoothClassic().TurnOffDiscoverability()); if (enabled.bluetooth) { - EXPECT_TRUE(mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_TRUE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } // Turn discoverability back on mediums_a.GetBluetoothClassic().TurnOnDiscoverability(service_id_); @@ -428,11 +426,10 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateAdvertisingOptions) { EXPECT_EQ( handler_a.UpdateAdvertisingOptions(&client_a_, service_id_, new_options), Status{Status::kSuccess}); - EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsAdvertising(service_id_)); + EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsAdvertising(service_id_)); // Low power won't restart BT, nor BLE advertising for legacy device. if (enabled.bluetooth) { - EXPECT_FALSE( - mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_FALSE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } EXPECT_FALSE(mediums_a.GetWifiLan().IsAdvertising(service_id_)); EXPECT_FALSE(mediums_a.GetBluetoothClassic().TurnOffDiscoverability()); @@ -463,10 +460,10 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ASSERT_FALSE(mediums_a.GetWifiLan().IsAcceptingConnections(service_id_)); mediums_a.GetWifiLan().StopAdvertising(service_id_); ASSERT_FALSE(mediums_a.GetWifiLan().IsAdvertising(service_id_)); - mediums_a.GetBleV2().StopAcceptingConnections(service_id_); - ASSERT_FALSE(mediums_a.GetBleV2().IsAcceptingConnections(service_id_)); - mediums_a.GetBleV2().StopAdvertising(service_id_); - ASSERT_FALSE(mediums_a.GetBleV2().IsAdvertising(service_id_)); + mediums_a.GetBle().StopAcceptingConnections(service_id_); + ASSERT_FALSE(mediums_a.GetBle().IsAcceptingConnections(service_id_)); + mediums_a.GetBle().StopAdvertising(service_id_); + ASSERT_FALSE(mediums_a.GetBle().IsAdvertising(service_id_)); AdvertisingOptions old_options{ { Strategy::kP2pCluster, @@ -478,30 +475,29 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, false, // enable_bluetooth_listening }; if (enabled.bluetooth) { - EXPECT_FALSE( - mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_FALSE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } EXPECT_EQ( handler_a.StartAdvertising(&client_a_, service_id_, old_options, {.endpoint_info = ByteArray{endpoint_name}}), Status{Status::kSuccess}); - EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsAdvertising(service_id_)); + EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsAdvertising(service_id_)); EXPECT_EQ(enabled.wifi_lan, mediums_a.GetWifiLan().IsAdvertising(service_id_)); EXPECT_EQ( enabled.bluetooth, mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); if (enabled.bluetooth) { - EXPECT_TRUE(mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_TRUE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } EXPECT_EQ(enabled.bluetooth, mediums_a.GetBluetoothClassic().TurnOffDiscoverability()); EXPECT_EQ(handler_a.UpdateAdvertisingOptions(&client_a_, service_id_, advertising_options_), Status{Status::kSuccess}); - EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsAdvertising(service_id_)); + EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsAdvertising(service_id_)); if (enabled.bluetooth) { - EXPECT_TRUE(mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_TRUE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } EXPECT_EQ(enabled.wifi_lan, mediums_a.GetWifiLan().IsAdvertising(service_id_)); @@ -512,8 +508,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); handler_a.StopAdvertising(&client_a_); if (enabled.bluetooth) { - EXPECT_FALSE( - mediums_a.GetBleV2().IsAdvertisingForLegacyDevice(service_id_)); + EXPECT_FALSE(mediums_a.GetBle().IsAdvertisingForLegacyDevice(service_id_)); } env_.Stop(); } @@ -589,7 +584,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanDiscoverLegacy) { }, }), Status{Status::kSuccess}); - // advertising for legacy device depends on both BT and BLE V2 enabled. + // advertising for legacy device depends on both BT and BLE enabled. EXPECT_TRUE(latch.Await(absl::Milliseconds(1000)).result()); // We discovered endpoint over one medium. Before we finish the test, we have // to stop discovery for other mediums that may be still ongoing. @@ -618,7 +613,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, PauseBluetoothClassicDiscovery) { handler_a.StartDiscovery(&client_a_, service_id_, discovery_options_, {}), Status{Status::kSuccess}); - EXPECT_TRUE(mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_TRUE(mediums_a.GetBle().IsScanning(service_id_)); EXPECT_FALSE(mediums_a.GetBluetoothClassic().IsDiscovering(service_id_)); // Before we finish the test, we have to stop discovery for other mediums that // may be still ongoing. @@ -636,7 +631,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ResumeBluetoothClassicDiscovery) { std::string endpoint_name{"endpoint_name"}; env_.Start(); - // Enable BLE V2 extended advertisement for client_a_. + // Enable BLE extended advertisement for client_a_. env_.SetBleExtendedAdvertisementsAvailable(true); Mediums mediums_a; EndpointChannelManager ecm_a; @@ -645,7 +640,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ResumeBluetoothClassicDiscovery) { BwuManager bwu_a(mediums_a, em_a, ecm_a, {}, {}); P2pClusterPcpHandler handler_a(&mediums_a, &em_a, &ecm_a, &bwu_a, ibds_a); - // Disable BLE V2 extended advertisement for client_b_. + // Disable BLE extended advertisement for client_b_. env_.SetBleExtendedAdvertisementsAvailable(false); Mediums mediums_b; EndpointChannelManager ecm_b; @@ -668,7 +663,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ResumeBluetoothClassicDiscovery) { }), Status{Status::kSuccess}); - EXPECT_TRUE(mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_TRUE(mediums_a.GetBle().IsScanning(service_id_)); EXPECT_FALSE(mediums_a.GetBluetoothClassic().IsDiscovering(service_id_)); EXPECT_EQ( @@ -679,7 +674,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ResumeBluetoothClassicDiscovery) { EXPECT_TRUE(latch.Await(absl::Milliseconds(1000)).result()); absl::SleepFor(absl::Milliseconds(100)); - EXPECT_TRUE(mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_TRUE(mediums_a.GetBle().IsScanning(service_id_)); EXPECT_TRUE(mediums_a.GetBluetoothClassic().IsDiscovering(service_id_)); // Before we finish the test, we have to stop discovery for other mediums that @@ -783,7 +778,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateDiscoveryOptions) { handler_a.StartDiscovery(&client_a_, service_id_, discovery_options_, {}), Status{Status::kSuccess}); BooleanMediumSelector enabled = std::get<0>(GetParam()); - EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsScanning(service_id_)); EXPECT_EQ(enabled.wifi_lan, mediums_a.GetWifiLan().IsDiscovering(service_id_)); DiscoveryOptions new_options{ @@ -800,7 +795,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateDiscoveryOptions) { EXPECT_EQ( handler_a.UpdateDiscoveryOptions(&client_a_, service_id_, new_options), Status{Status::kSuccess}); - EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsScanning(service_id_)); EXPECT_FALSE(mediums_a.GetWifiLan().IsDiscovering(service_id_)); handler_a.StopDiscovery(&client_a_); env_.Stop(); @@ -825,8 +820,8 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateDiscoveryOptionsNoLowPower) { ASSERT_FALSE(mediums_a.GetBluetoothClassic().TurnOffDiscoverability()); mediums_a.GetWifiLan().StopDiscovery(service_id_); ASSERT_FALSE(mediums_a.GetWifiLan().IsDiscovering(service_id_)); - mediums_a.GetBleV2().StopScanning(service_id_); - ASSERT_FALSE(mediums_a.GetBleV2().IsScanning(service_id_)); + mediums_a.GetBle().StopScanning(service_id_); + ASSERT_FALSE(mediums_a.GetBle().IsScanning(service_id_)); DiscoveryOptions old_options = discovery_options_; old_options.low_power = true; old_options.allowed = old_enabled; @@ -835,7 +830,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateDiscoveryOptionsNoLowPower) { // Start discovery EXPECT_EQ(handler_a.StartDiscovery(&client_a_, service_id_, old_options, {}), Status{Status::kSuccess}); - EXPECT_EQ(old_enabled.ble, mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_EQ(old_enabled.ble, mediums_a.GetBle().IsScanning(service_id_)); EXPECT_EQ(old_enabled.wifi_lan, mediums_a.GetWifiLan().IsDiscovering(service_id_)); EXPECT_EQ(old_enabled.bluetooth, @@ -846,7 +841,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, CanUpdateDiscoveryOptionsNoLowPower) { handler_a.UpdateDiscoveryOptions(&client_a_, service_id_, new_options) .Ok()); LOG(INFO) << "updated discovery options"; - EXPECT_EQ(new_enabled.ble, mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_EQ(new_enabled.ble, mediums_a.GetBle().IsScanning(service_id_)); EXPECT_EQ(new_enabled.wifi_lan, mediums_a.GetWifiLan().IsDiscovering(service_id_)); EXPECT_EQ(new_enabled.bluetooth, @@ -871,13 +866,13 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ASSERT_FALSE(mediums_a.GetBluetoothClassic().TurnOffDiscoverability()); mediums_a.GetWifiLan().StopDiscovery(service_id_); ASSERT_FALSE(mediums_a.GetWifiLan().IsDiscovering(service_id_)); - mediums_a.GetBleV2().StopScanning(service_id_); - ASSERT_FALSE(mediums_a.GetBleV2().IsScanning(service_id_)); + mediums_a.GetBle().StopScanning(service_id_); + ASSERT_FALSE(mediums_a.GetBle().IsScanning(service_id_)); // Start discovery EXPECT_EQ( handler_a.StartDiscovery(&client_a_, service_id_, discovery_options_, {}), Status{Status::kSuccess}); - EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsScanning(service_id_)); EXPECT_EQ(enabled.wifi_lan, mediums_a.GetWifiLan().IsDiscovering(service_id_)); @@ -888,7 +883,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, discovery_options_); EXPECT_TRUE(result.Ok()); LOG(INFO) << "updated discovery options"; - EXPECT_EQ(enabled.ble, mediums_a.GetBleV2().IsScanning(service_id_)); + EXPECT_EQ(enabled.ble, mediums_a.GetBle().IsScanning(service_id_)); EXPECT_EQ(enabled.wifi_lan, mediums_a.GetWifiLan().IsDiscovering(service_id_)); // We didn't restart the medium. @@ -1174,7 +1169,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ASSERT_FALSE( mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); ASSERT_FALSE(mediums_a.GetWifiLan().IsAcceptingConnections(service_id_)); - ASSERT_FALSE(mediums_a.GetBleV2().IsAcceptingConnections(service_id_)); + ASSERT_FALSE(mediums_a.GetBle().IsAcceptingConnections(service_id_)); // call handler. auto result = handler_a.StartListeningForIncomingConnections( @@ -1183,7 +1178,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, EXPECT_TRUE( mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); EXPECT_TRUE(mediums_a.GetWifiLan().IsAcceptingConnections(service_id_)); - EXPECT_TRUE(mediums_a.GetBleV2().IsAcceptingConnections(service_id_)); + EXPECT_TRUE(mediums_a.GetBle().IsAcceptingConnections(service_id_)); EXPECT_EQ(result.second.size(), 3); ASSERT_TRUE(client_a_.IsListeningForIncomingConnections()); EXPECT_EQ(client_a_.GetListeningForIncomingConnectionsServiceId(), @@ -1221,7 +1216,7 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ASSERT_FALSE( mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); ASSERT_FALSE(mediums_a.GetWifiLan().IsAcceptingConnections(service_id_)); - ASSERT_FALSE(mediums_a.GetBleV2().IsAcceptingConnections(service_id_)); + ASSERT_FALSE(mediums_a.GetBle().IsAcceptingConnections(service_id_)); // call handler. auto result = handler_a.StartListeningForIncomingConnections( &client_a_, service_id_, v3_options, {}); @@ -1229,13 +1224,13 @@ TEST_P(P2pClusterPcpHandlerTestWithParam, ASSERT_TRUE( mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); ASSERT_TRUE(mediums_a.GetWifiLan().IsAcceptingConnections(service_id_)); - ASSERT_TRUE(mediums_a.GetBleV2().IsAcceptingConnections(service_id_)); + ASSERT_TRUE(mediums_a.GetBle().IsAcceptingConnections(service_id_)); // stop. handler_a.StopListeningForIncomingConnections(&client_a_); EXPECT_FALSE( mediums_a.GetBluetoothClassic().IsAcceptingConnections(service_id_)); EXPECT_FALSE(mediums_a.GetWifiLan().IsAcceptingConnections(service_id_)); - EXPECT_FALSE(mediums_a.GetBleV2().IsAcceptingConnections(service_id_)); + EXPECT_FALSE(mediums_a.GetBle().IsAcceptingConnections(service_id_)); env_.Stop(); } diff --git a/connections/implementation/p2p_point_to_point_pcp_handler.cc b/connections/implementation/p2p_point_to_point_pcp_handler.cc index b3a97e5f..7f639f6e 100644 --- a/connections/implementation/p2p_point_to_point_pcp_handler.cc +++ b/connections/implementation/p2p_point_to_point_pcp_handler.cc @@ -63,7 +63,7 @@ P2pPointToPointPcpHandler::GetConnectionMediumsByPriority() { if (mediums_->GetBluetoothClassic().IsAvailable()) { mediums.push_back(location::nearby::proto::connections::BLUETOOTH); } - if (mediums_->GetBleV2().IsAvailable()) { + if (mediums_->GetBle().IsAvailable()) { mediums.push_back(location::nearby::proto::connections::BLE); } return mediums; diff --git a/connections/implementation/p2p_star_pcp_handler.cc b/connections/implementation/p2p_star_pcp_handler.cc index 26a86b35..e1b5c728 100644 --- a/connections/implementation/p2p_star_pcp_handler.cc +++ b/connections/implementation/p2p_star_pcp_handler.cc @@ -58,7 +58,7 @@ P2pStarPcpHandler::GetConnectionMediumsByPriority() { if (mediums_->GetBluetoothClassic().IsAvailable()) { mediums.push_back(location::nearby::proto::connections::BLUETOOTH); } - if (mediums_->GetBleV2().IsAvailable()) { + if (mediums_->GetBle().IsAvailable()) { mediums.push_back(location::nearby::proto::connections::BLE); } return mediums; diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 67d6a8cd..8c5b9147 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -288,7 +288,7 @@ cc_library( name = "comm", srcs = [ "awdl.cc", - "ble_v2.cc", + "ble.cc", "bluetooth_classic.cc", "credential_storage_impl.cc", "file.cc", @@ -298,7 +298,7 @@ cc_library( ], hdrs = [ "awdl.h", - "ble_v2.h", + "ble.h", "bluetooth_adapter.h", "bluetooth_classic.h", "credential_storage_impl.h", @@ -471,7 +471,7 @@ cc_test( timeout = "moderate", srcs = [ "ble_connection_info_test.cc", - "ble_v2_test.cc", + "ble_test.cc", "blocking_queue_stream_test.cc", "bluetooth_adapter_test.cc", "bluetooth_classic_test.cc", diff --git a/internal/platform/ble_v2.cc b/internal/platform/ble.cc similarity index 65% rename from internal/platform/ble_v2.cc rename to internal/platform/ble.cc index a8330e9b..ab0bce21 100644 --- a/internal/platform/ble_v2.cc +++ b/internal/platform/ble.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include #include @@ -24,7 +24,7 @@ #include "absl/status/status.h" #include "absl/strings/string_view.h" #include "internal/platform/cancellation_flag.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/logging.h" #include "internal/platform/mutex_lock.h" #include "internal/platform/uuid.h" @@ -32,35 +32,35 @@ namespace nearby { namespace { -using ::nearby::api::ble_v2::BleAdvertisementData; -using ::nearby::api::ble_v2::GattCharacteristic; -using ::nearby::api::ble_v2::TxPowerLevel; +using ::nearby::api::ble::BleAdvertisementData; +using ::nearby::api::ble::GattCharacteristic; +using ::nearby::api::ble::TxPowerLevel; using ReadValueCallback = - ::nearby::api::ble_v2::ServerGattConnectionCallback::ReadValueCallback; + ::nearby::api::ble::ServerGattConnectionCallback::ReadValueCallback; using WriteValueCallback = - ::nearby::api::ble_v2::ServerGattConnectionCallback::WriteValueCallback; + ::nearby::api::ble::ServerGattConnectionCallback::WriteValueCallback; } // namespace -bool BleV2Medium::StartAdvertising( +bool BleMedium::StartAdvertising( const BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_parameters) { + api::ble::AdvertiseParameters advertise_parameters) { return impl_->StartAdvertising(advertising_data, advertise_parameters); } -bool BleV2Medium::StopAdvertising() { return impl_->StopAdvertising(); } +bool BleMedium::StopAdvertising() { return impl_->StopAdvertising(); } -std::unique_ptr -BleV2Medium::StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters, - api::ble_v2::BleMedium::AdvertisingCallback callback) { +std::unique_ptr +BleMedium::StartAdvertising( + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertise_set_parameters, + api::ble::BleMedium::AdvertisingCallback callback) { return impl_->StartAdvertising(advertising_data, advertise_set_parameters, std::move(callback)); } -bool BleV2Medium::StartScanning(const Uuid& service_uuid, - TxPowerLevel tx_power_level, - ScanCallback callback) { +bool BleMedium::StartScanning(const Uuid& service_uuid, + TxPowerLevel tx_power_level, + ScanCallback callback) { MutexLock lock(&mutex_); if (scanning_enabled_) { LOG(INFO) << "Ble Scanning already enabled"; @@ -68,12 +68,12 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid, } bool success = impl_->StartScanning( service_uuid, tx_power_level, - api::ble_v2::BleMedium::ScanCallback{ + api::ble::BleMedium::ScanCallback{ .advertisement_found_cb = - [this](api::ble_v2::BlePeripheral::UniqueId peripheral_id, + [this](api::ble::BlePeripheral::UniqueId peripheral_id, BleAdvertisementData advertisement_data) { MutexLock lock(&mutex_); - BleV2Peripheral proxy(*this, peripheral_id); + BlePeripheral proxy(*this, peripheral_id); if (!scanning_enabled_) return; scan_callback_.advertisement_found_cb(std::move(proxy), advertisement_data); @@ -87,9 +87,9 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid, return success; } -bool BleV2Medium::StartMultipleServicesScanning( +bool BleMedium::StartMultipleServicesScanning( const std::vector& service_uuids, - api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) { + api::ble::TxPowerLevel tx_power_level, ScanCallback callback) { MutexLock lock(&mutex_); if (scanning_enabled_) { LOG(INFO) << "Ble Scanning already enabled"; @@ -97,12 +97,12 @@ bool BleV2Medium::StartMultipleServicesScanning( } bool success = impl_->StartMultipleServicesScanning( service_uuids, tx_power_level, - api::ble_v2::BleMedium::ScanCallback{ + api::ble::BleMedium::ScanCallback{ .advertisement_found_cb = - [this](api::ble_v2::BlePeripheral::UniqueId peripheral_id, + [this](api::ble::BlePeripheral::UniqueId peripheral_id, BleAdvertisementData advertisement_data) { MutexLock lock(&mutex_); - BleV2Peripheral proxy(*this, peripheral_id); + BlePeripheral proxy(*this, peripheral_id); if (!scanning_enabled_) return; scan_callback_.advertisement_found_cb(std::move(proxy), advertisement_data); @@ -116,9 +116,9 @@ bool BleV2Medium::StartMultipleServicesScanning( return success; } -BleV2Medium::~BleV2Medium() { StopScanning(); } +BleMedium::~BleMedium() { StopScanning(); } -bool BleV2Medium::StopScanning() { +bool BleMedium::StopScanning() { MutexLock lock(&mutex_); if (!scanning_enabled_) { return true; @@ -128,28 +128,27 @@ bool BleV2Medium::StopScanning() { LOG(INFO) << "Ble Scanning disabled"; return impl_->StopScanning(); } -bool BleV2Medium::PauseMediumScanning() { +bool BleMedium::PauseMediumScanning() { MutexLock lock(&mutex_); if (!scanning_enabled_) { return true; } - LOG(INFO) << "Pause Medium level BLE_V2 Scanning"; + LOG(INFO) << "Pause Medium level BLE Scanning"; return impl_->PauseMediumScanning(); } -bool BleV2Medium::ResumeMediumScanning() { +bool BleMedium::ResumeMediumScanning() { MutexLock lock(&mutex_); return impl_->ResumeMediumScanning(); } -std::unique_ptr -BleV2Medium::StartScanning(const Uuid& service_uuid, - api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BleMedium::ScanningCallback callback) { +std::unique_ptr BleMedium::StartScanning( + const Uuid& service_uuid, api::ble::TxPowerLevel tx_power_level, + api::ble::BleMedium::ScanningCallback callback) { LOG(INFO) << "platform mutex: " << &mutex_; return impl_->StartScanning( service_uuid, tx_power_level, - api::ble_v2::BleMedium::ScanningCallback{ + api::ble::BleMedium::ScanningCallback{ .start_scanning_result = [this, start_scanning_result = std::move(callback.start_scanning_result)]( @@ -167,14 +166,14 @@ BleV2Medium::StartScanning(const Uuid& service_uuid, }); } -std::unique_ptr BleV2Medium::StartGattServer( +std::unique_ptr BleMedium::StartGattServer( ServerGattConnectionCallback callback) { { MutexLock lock(&mutex_); server_gatt_connection_callback_ = std::move(callback); } - std::unique_ptr api_gatt_server = + std::unique_ptr api_gatt_server = impl_->StartGattServer({ .characteristic_subscription_cb = [this](const GattCharacteristic& characteristic) { @@ -189,10 +188,9 @@ std::unique_ptr BleV2Medium::StartGattServer( .characteristic_unsubscription_cb(characteristic); }, .on_characteristic_read_cb = - [this]( - const api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const GattCharacteristic& characteristic, int offset, - ReadValueCallback callback) { + [this](const api::ble::BlePeripheral::UniqueId remote_device_id, + const GattCharacteristic& characteristic, int offset, + ReadValueCallback callback) { MutexLock lock(&mutex_); if (server_gatt_connection_callback_ .on_characteristic_read_cb) { @@ -205,10 +203,9 @@ std::unique_ptr BleV2Medium::StartGattServer( } }, .on_characteristic_write_cb = - [this]( - const api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const GattCharacteristic& characteristic, int offset, - absl::string_view data, WriteValueCallback callback) { + [this](const api::ble::BlePeripheral::UniqueId remote_device_id, + const GattCharacteristic& characteristic, int offset, + absl::string_view data, WriteValueCallback callback) { MutexLock lock(&mutex_); if (server_gatt_connection_callback_ .on_characteristic_write_cb) { @@ -224,10 +221,10 @@ std::unique_ptr BleV2Medium::StartGattServer( return std::make_unique(std::move(api_gatt_server)); } -std::unique_ptr BleV2Medium::ConnectToGattServer( - BleV2Peripheral peripheral, TxPowerLevel tx_power_level, +std::unique_ptr BleMedium::ConnectToGattServer( + BlePeripheral peripheral, TxPowerLevel tx_power_level, ClientGattConnectionCallback callback) { - std::optional id = + std::optional id = peripheral.GetUniqueId(); if (!id.has_value()) { LOG(ERROR) << "Failed to connect to GattServer, invalid peripheral"; @@ -243,33 +240,33 @@ std::unique_ptr BleV2Medium::ConnectToGattServer( })); } -BleV2ServerSocket BleV2Medium::OpenServerSocket(const std::string& service_id) { - return BleV2ServerSocket(*this, impl_->OpenServerSocket(service_id)); +BleServerSocket BleMedium::OpenServerSocket(const std::string& service_id) { + return BleServerSocket(*this, impl_->OpenServerSocket(service_id)); } -BleL2capServerSocket BleV2Medium::OpenL2capServerSocket( +BleL2capServerSocket BleMedium::OpenL2capServerSocket( const std::string& service_id) { return BleL2capServerSocket(*this, impl_->OpenL2capServerSocket(service_id)); } -BleV2Socket BleV2Medium::Connect(const std::string& service_id, - TxPowerLevel tx_power_level, - const BleV2Peripheral& peripheral, - CancellationFlag* cancellation_flag) { - std::optional id = +BleSocket BleMedium::Connect(const std::string& service_id, + TxPowerLevel tx_power_level, + const BlePeripheral& peripheral, + CancellationFlag* cancellation_flag) { + std::optional id = peripheral.GetUniqueId(); if (!id.has_value()) { LOG(ERROR) << "Failed to connect, invalid peripheral"; return {}; } - return BleV2Socket(peripheral, impl_->Connect(service_id, tx_power_level, *id, - cancellation_flag)); + return BleSocket(peripheral, impl_->Connect(service_id, tx_power_level, *id, + cancellation_flag)); } -BleL2capSocket BleV2Medium::ConnectOverL2cap( +BleL2capSocket BleMedium::ConnectOverL2cap( const std::string& service_id, TxPowerLevel tx_power_level, - const BleV2Peripheral& peripheral, CancellationFlag* cancellation_flag) { - std::optional id = + const BlePeripheral& peripheral, CancellationFlag* cancellation_flag) { + std::optional id = peripheral.GetUniqueId(); if (!id.has_value()) { LOG(ERROR) << "Failed to connect over L2cap, invalid peripheral"; @@ -281,10 +278,10 @@ BleL2capSocket BleV2Medium::ConnectOverL2cap( *id, cancellation_flag)); } -bool BleV2Medium::IsExtendedAdvertisementsAvailable() { +bool BleMedium::IsExtendedAdvertisementsAvailable() { return IsValid() && impl_->IsExtendedAdvertisementsAvailable(); } -bool BleV2Peripheral::IsValid() const { return unique_id_.has_value(); } +bool BlePeripheral::IsValid() const { return unique_id_.has_value(); } } // namespace nearby diff --git a/internal/platform/ble_v2.h b/internal/platform/ble.h similarity index 70% rename from internal/platform/ble_v2.h rename to internal/platform/ble.h index 59206e4d..82360d28 100644 --- a/internal/platform/ble_v2.h +++ b/internal/platform/ble.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef PLATFORM_PUBLIC_BLE_V2_H_ -#define PLATFORM_PUBLIC_BLE_V2_H_ +#ifndef PLATFORM_PUBLIC_BLE_H_ +#define PLATFORM_PUBLIC_BLE_H_ #include #include @@ -32,7 +32,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/platform.h" #include "internal/platform/input_stream.h" #include "internal/platform/listeners.h" @@ -43,22 +43,21 @@ namespace nearby { -class BleV2Medium; +class BleMedium; // TODO: b/399815436 - Remove all the shared_ptr in this file. // Opaque wrapper over a BLE peripheral. Must contain enough data about a // particular BLE peripheral to connect to its GATT server. -class BleV2Peripheral final { +class BlePeripheral final { public: - BleV2Peripheral() = default; - BleV2Peripheral(BleV2Medium& medium, - api::ble_v2::BlePeripheral::UniqueId unique_id) + BlePeripheral() = default; + BlePeripheral(BleMedium& medium, api::ble::BlePeripheral::UniqueId unique_id) : medium_(&medium), unique_id_(unique_id) {} - BleV2Peripheral(const BleV2Peripheral&) = default; - BleV2Peripheral& operator=(const BleV2Peripheral&) = default; - BleV2Peripheral(BleV2Peripheral&& other) = default; + BlePeripheral(const BlePeripheral&) = default; + BlePeripheral& operator=(const BlePeripheral&) = default; + BlePeripheral(BlePeripheral&& other) = default; - BleV2Peripheral& operator=(BleV2Peripheral&& other) = default; + BlePeripheral& operator=(BlePeripheral&& other) = default; ByteArray GetId() const { return id_; } void SetId(const ByteArray& id) { id_ = id; } @@ -68,23 +67,23 @@ class BleV2Peripheral final { bool IsValid() const; - std::optional GetUniqueId() const { + std::optional GetUniqueId() const { return unique_id_; } - api::ble_v2::BlePeripheral* GetImpl() const; + api::ble::BlePeripheral* GetImpl() const; std::string ToReadableString() const { if (!IsValid()) { - return "BleV2Peripheral { invalid }"; + return "BlePeripheral { invalid }"; } - return absl::StrFormat("BleV2Peripheral { id=%s, psm=%d}", + return absl::StrFormat("BlePeripheral { id=%s, psm=%d}", absl::BytesToHexString(GetId().AsStringView()), GetPsm()); } private: - BleV2Medium* medium_ = nullptr; - std::optional unique_id_; + BleMedium* medium_ = nullptr; + std::optional unique_id_; // A unique identifier for this peripheral. It is the BLE advertisement bytes // it was found on. @@ -99,16 +98,16 @@ class BleV2Peripheral final { // Container of operations that can be performed over the BLE GATT client // socket. // This class is copyable but not moveable. -class BleV2Socket final { +class BleSocket final { public: - BleV2Socket() = default; - BleV2Socket(BleV2Peripheral peripheral, - std::unique_ptr socket) + BleSocket() = default; + BleSocket(BlePeripheral peripheral, + std::unique_ptr socket) : peripheral_(peripheral) { state_->socket = std::move(socket); } - BleV2Socket(const BleV2Socket&) = default; - BleV2Socket& operator=(const BleV2Socket&) = default; + BleSocket(const BleSocket&) = default; + BleSocket& operator=(const BleSocket&) = default; // Returns the InputStream of the BleSocket. // On error, returned stream will report Exception::kIo on any operation. @@ -139,7 +138,7 @@ class BleV2Socket final { } // Returns BlePeripheral object which wraps a valid BlePeripheral pointer. - BleV2Peripheral& GetRemotePeripheral() { return peripheral_; } + BlePeripheral& GetRemotePeripheral() { return peripheral_; } // Returns true if a socket is usable. If this method returns false, // it is not safe to call any other method. @@ -157,27 +156,27 @@ class BleV2Socket final { // Returned reference will remain valid for while BleSocket object is // itself valid. Typically BleSocket lifetime matches duration of the // connection, and is controlled by end user, since they hold the instance. - api::ble_v2::BleSocket& GetImpl() { return *state_->socket; } + api::ble::BleSocket& GetImpl() { return *state_->socket; } private: struct SharedState { - std::unique_ptr socket; + std::unique_ptr socket; absl::AnyInvocable close_notifier; }; std::shared_ptr state_ = std::make_shared(); - BleV2Peripheral peripheral_; + BlePeripheral peripheral_; }; // Container of operations that can be performed over the BLE GATT server // socket. // This class is copyable but not moveable. -class BleV2ServerSocket final { +class BleServerSocket final { public: - BleV2ServerSocket(BleV2Medium& medium, - std::unique_ptr socket) + BleServerSocket(BleMedium& medium, + std::unique_ptr socket) : medium_(&medium), impl_(std::move(socket)) {} - BleV2ServerSocket(const BleV2ServerSocket&) = default; - BleV2ServerSocket& operator=(const BleV2ServerSocket&) = default; + BleServerSocket(const BleServerSocket&) = default; + BleServerSocket& operator=(const BleServerSocket&) = default; // Blocks until either: // - at least one incoming connection request is available, or @@ -187,15 +186,15 @@ class BleV2ServerSocket final { // member function "IsValid()" // Once error is reported, it is permanent, and // ServerSocket has to be closed by caller. - BleV2Socket Accept() { - std::unique_ptr socket = impl_->Accept(); - BleV2Peripheral peripheral; + BleSocket Accept() { + std::unique_ptr socket = impl_->Accept(); + BlePeripheral peripheral; if (socket == nullptr) { LOG(INFO) << "BleServerSocket Accept() failed on server socket: " << this; } else { - peripheral = BleV2Peripheral(*medium_, socket->GetRemotePeripheralId()); + peripheral = BlePeripheral(*medium_, socket->GetRemotePeripheralId()); } - return BleV2Socket(peripheral, std::move(socket)); + return BleSocket(peripheral, std::move(socket)); } // Returns Exception::kIo on error, Exception::kSuccess otherwise. @@ -205,11 +204,11 @@ class BleV2ServerSocket final { } bool IsValid() const { return impl_ != nullptr; } - api::ble_v2::BleServerSocket& GetImpl() { return *impl_; } + api::ble::BleServerSocket& GetImpl() { return *impl_; } private: - BleV2Medium* medium_; - std::shared_ptr impl_; + BleMedium* medium_; + std::shared_ptr impl_; }; // Opaque wrapper over a GattServer. @@ -219,27 +218,26 @@ class BleV2ServerSocket final { // of std::optional, because iOS platform is still in C++14. class GattServer final { public: - explicit GattServer(std::unique_ptr gatt_server) + explicit GattServer(std::unique_ptr gatt_server) : impl_(std::move(gatt_server)) {} ~GattServer() { Stop(); } // NOLINTNEXTLINE(google3-legacy-absl-backports) - absl::optional CreateCharacteristic( + absl::optional CreateCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid, - const api::ble_v2::GattCharacteristic::Permission permission, - const api::ble_v2::GattCharacteristic::Property property) { + const api::ble::GattCharacteristic::Permission permission, + const api::ble::GattCharacteristic::Property property) { return impl_->CreateCharacteristic(service_uuid, characteristic_uuid, permission, property); } - bool UpdateCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, - const ByteArray& value) { + bool UpdateCharacteristic(const api::ble::GattCharacteristic& characteristic, + const ByteArray& value) { return impl_->UpdateCharacteristic(characteristic, value); } absl::Status NotifyCharacteristicChanged( - const api::ble_v2::GattCharacteristic& characteristic, bool confirm, + const api::ble::GattCharacteristic& characteristic, bool confirm, const ByteArray& new_value) { return impl_->NotifyCharacteristicChanged(characteristic, confirm, new_value); @@ -256,10 +254,10 @@ class GattServer final { // Returns reference to platform implementation. // This is used to communicate with platform code, and for debugging // purposes. - api::ble_v2::GattServer* GetImpl() { return impl_.get(); } + api::ble::GattServer* GetImpl() { return impl_.get(); } private: - std::unique_ptr impl_; + std::unique_ptr impl_; }; // Opaque wrapper for a GattClient. @@ -269,7 +267,7 @@ class GattServer final { class GattClient final { public: explicit GattClient( - std::unique_ptr client_gatt_connection) + std::unique_ptr client_gatt_connection) : impl_(std::move(client_gatt_connection)) {} bool DiscoverServiceAndCharacteristics( @@ -279,21 +277,21 @@ class GattClient final { } // NOLINTNEXTLINE(google3-legacy-absl-backports) - absl::optional GetCharacteristic( + absl::optional GetCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid) { return impl_->GetCharacteristic(service_uuid, characteristic_uuid); } // NOLINTNEXTLINE(google3-legacy-absl-backports) absl::optional ReadCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic) { + const api::ble::GattCharacteristic& characteristic) { return impl_->ReadCharacteristic(characteristic); } // NOLINTNEXTLINE(google3-legacy-absl-backports) - bool WriteCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, - absl::string_view value, api::ble_v2::GattClient::WriteType write_type) { + bool WriteCharacteristic(const api::ble::GattCharacteristic& characteristic, + absl::string_view value, + api::ble::GattClient::WriteType write_type) { return impl_->WriteCharacteristic(characteristic, value, write_type); } @@ -301,7 +299,7 @@ class GattClient final { // unsubscribing. // NOLINTNEXTLINE(google3-legacy-absl-backports) bool SetCharacteristicSubscription( - const api::ble_v2::GattCharacteristic& characteristic, bool enable, + const api::ble::GattCharacteristic& characteristic, bool enable, absl::AnyInvocable on_characteristic_changed_cb) { return impl_->SetCharacteristicSubscription( @@ -317,17 +315,17 @@ class GattClient final { // Returns reference to platform implementation. // This is used to communicate with platform code, and for debugging // purposes. - api::ble_v2::GattClient* GetImpl() { return impl_.get(); } + api::ble::GattClient* GetImpl() { return impl_.get(); } private: - std::unique_ptr impl_; + std::unique_ptr impl_; }; class BleL2capSocket final { public: BleL2capSocket() = default; - BleL2capSocket(BleV2Peripheral peripheral, - std::unique_ptr socket) + BleL2capSocket(BlePeripheral peripheral, + std::unique_ptr socket) : peripheral_(peripheral) { state_->socket = std::move(socket); } @@ -363,7 +361,7 @@ class BleL2capSocket final { } // Returns BlePeripheral object which wraps a valid BlePeripheral pointer. - BleV2Peripheral& GetRemotePeripheral() { return peripheral_; } + BlePeripheral& GetRemotePeripheral() { return peripheral_; } // Returns true if a socket is usable. If this method returns false, // it is not safe to call any other method. @@ -381,22 +379,21 @@ class BleL2capSocket final { // Returned reference will remain valid for while BleSocket object is // itself valid. Typically BleSocket lifetime matches duration of the // connection, and is controlled by end user, since they hold the instance. - api::ble_v2::BleL2capSocket& GetImpl() { return *state_->socket; } + api::ble::BleL2capSocket& GetImpl() { return *state_->socket; } private: struct SharedState { - std::unique_ptr socket; + std::unique_ptr socket; absl::AnyInvocable close_notifier; }; std::shared_ptr state_ = std::make_shared(); - BleV2Peripheral peripheral_; + BlePeripheral peripheral_; }; class BleL2capServerSocket final { public: - BleL2capServerSocket( - BleV2Medium& medium, - std::unique_ptr socket) + BleL2capServerSocket(BleMedium& medium, + std::unique_ptr socket) : medium_(&medium), impl_(std::move(socket)) {} BleL2capServerSocket(const BleL2capServerSocket&) = default; BleL2capServerSocket& operator=(const BleL2capServerSocket&) = default; @@ -412,14 +409,13 @@ class BleL2capServerSocket final { // Once error is reported, it is permanent, and // ServerSocket has to be closed by caller. BleL2capSocket Accept() { - std::unique_ptr socket = impl_->Accept(); - BleV2Peripheral peripheral; + std::unique_ptr socket = impl_->Accept(); + BlePeripheral peripheral; if (socket == nullptr) { LOG(INFO) << "BleL2capServerSocket Accept() failed on server socket: " << this; } else { - peripheral = - BleV2Peripheral(*medium_, socket->GetRemotePeripheralId()); + peripheral = BlePeripheral(*medium_, socket->GetRemotePeripheralId()); } return BleL2capSocket(peripheral, std::move(socket)); } @@ -433,15 +429,15 @@ class BleL2capServerSocket final { // Returns true if a BleL2capServerSocket is usable. If this method returns // false, it is not safe to call any other method. bool IsValid() const { return impl_ != nullptr; } - api::ble_v2::BleL2capServerSocket& GetImpl() { return *impl_; } + api::ble::BleL2capServerSocket& GetImpl() { return *impl_; } private: - BleV2Medium* medium_ = nullptr; - std::shared_ptr impl_; + BleMedium* medium_ = nullptr; + std::shared_ptr impl_; }; // Container of operations that can be performed over the BLE medium. -class BleV2Medium final { +class BleMedium final { public: // A wrapper callback for BLE scan results. // @@ -453,32 +449,30 @@ class BleV2Medium final { // instance. struct ScanCallback { absl::AnyInvocable + BlePeripheral peripheral, + const api::ble::BleAdvertisementData& advertisement_data)> advertisement_found_cb = - nearby::DefaultCallback(); + nearby::DefaultCallback(); }; struct ServerGattConnectionCallback { - absl::AnyInvocable + absl::AnyInvocable characteristic_subscription_cb = - nearby::DefaultCallback(); - absl::AnyInvocable + nearby::DefaultCallback(); + absl::AnyInvocable characteristic_unsubscription_cb = - nearby::DefaultCallback(); + nearby::DefaultCallback(); absl::AnyInvocable + const api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic, int offset, + api::ble::ServerGattConnectionCallback::ReadValueCallback callback)> on_characteristic_read_cb; absl::AnyInvocable + api::ble::ServerGattConnectionCallback::WriteValueCallback callback)> on_characteristic_write_cb; }; // TODO(b/231318879): Remove this wrapper callback and use impl callback if @@ -487,38 +481,37 @@ class BleV2Medium final { absl::AnyInvocable disconnected_cb = nearby::DefaultCallback<>(); }; - explicit BleV2Medium(BluetoothAdapter& adapter) - : impl_( - api::ImplementationPlatform::CreateBleV2Medium(adapter.GetImpl())), + explicit BleMedium(BluetoothAdapter& adapter) + : impl_(api::ImplementationPlatform::CreateBleMedium(adapter.GetImpl())), adapter_(adapter) {} - ~BleV2Medium(); + ~BleMedium(); // Returns true once the BLE advertising has been initiated. // This interface will be deprecated soon. // TODO(b/271305977) remove this function. // Use 'unique_ptr StartAdvertising' instead. - bool StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_parameters); + bool StartAdvertising(const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertise_parameters); // This interface will be deprecated soon. // TODO(b/271305977) remove this function. bool StopAdvertising(); - std::unique_ptr StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters, - api::ble_v2::BleMedium::AdvertisingCallback callback); + std::unique_ptr StartAdvertising( + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertise_set_parameters, + api::ble::BleMedium::AdvertisingCallback callback); // Returns true once the BLE scan has been initiated. // This interface will be deprecated soon. bool StartScanning(const Uuid& service_uuid, - api::ble_v2::TxPowerLevel tx_power_level, - ScanCallback callback); + api::ble::TxPowerLevel tx_power_level, + ScanCallback callback) ABSL_LOCKS_EXCLUDED(mutex_); // Returns true once the BLE multiple services scan has been initiated. bool StartMultipleServicesScanning(const std::vector& service_uuids, - api::ble_v2::TxPowerLevel tx_power_level, - ScanCallback callback); + api::ble::TxPowerLevel tx_power_level, + ScanCallback callback) + ABSL_LOCKS_EXCLUDED(mutex_); // This interface will be deprecated soon. // TODO(b/271305977) remove this function. @@ -530,9 +523,9 @@ class BleV2Medium final { // Resume BLE scanning at platform Medium level. bool ResumeMediumScanning(); - std::unique_ptr StartScanning( - const Uuid& service_uuid, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BleMedium::ScanningCallback callback); + std::unique_ptr StartScanning( + const Uuid& service_uuid, api::ble::TxPowerLevel tx_power_level, + api::ble::BleMedium::ScanningCallback callback); // Starts Gatt Server for waiting to client connection. std::unique_ptr StartGattServer( @@ -541,36 +534,36 @@ class BleV2Medium final { // Returns a new GattClient connection to a gatt server. // There is only one instance of GattServer can run at a time. std::unique_ptr ConnectToGattServer( - BleV2Peripheral peripheral, api::ble_v2::TxPowerLevel tx_power_level, + BlePeripheral peripheral, api::ble::TxPowerLevel tx_power_level, ClientGattConnectionCallback callback); // Returns a new BleServerSocket. // On Success, BleServerSocket::IsValid() returns true. - BleV2ServerSocket OpenServerSocket(const std::string& service_id); + BleServerSocket OpenServerSocket(const std::string& service_id); // Returns a new BleL2capServerSocket. // On Success, BleL2capServerSocket::IsValid() returns true. BleL2capServerSocket OpenL2capServerSocket(const std::string& service_id); - // Returns a new BleV2Socket. - // On Success, BleV2Socket::IsValid() returns true. - BleV2Socket Connect(const std::string& service_id, - api::ble_v2::TxPowerLevel tx_power_level, - const BleV2Peripheral& peripheral, - CancellationFlag* cancellation_flag); + // Returns a new BleSocket. + // On Success, BleSocket::IsValid() returns true. + BleSocket Connect(const std::string& service_id, + api::ble::TxPowerLevel tx_power_level, + const BlePeripheral& peripheral, + CancellationFlag* cancellation_flag); // Returns a new BleL2capSocket. // On Success, BleL2capSocket::IsValid() returns true. BleL2capSocket ConnectOverL2cap(const std::string& service_id, - api::ble_v2::TxPowerLevel tx_power_level, - const BleV2Peripheral& peripheral, + api::ble::TxPowerLevel tx_power_level, + const BlePeripheral& peripheral, CancellationFlag* cancellation_flag); bool IsExtendedAdvertisementsAvailable(); bool IsValid() const { return impl_ != nullptr; } - api::ble_v2::BleMedium* GetImpl() const { return impl_.get(); } + api::ble::BleMedium* GetImpl() const { return impl_.get(); } BluetoothAdapter& GetAdapter() { return adapter_; } void AddAlternateUuidForService(uint16_t uuid, const std::string& service_id) { @@ -579,7 +572,7 @@ class BleV2Medium final { private: Mutex mutex_; - std::unique_ptr impl_; + std::unique_ptr impl_; BluetoothAdapter& adapter_; ServerGattConnectionCallback server_gatt_connection_callback_ ABSL_GUARDED_BY(mutex_); @@ -589,4 +582,4 @@ class BleV2Medium final { } // namespace nearby -#endif // PLATFORM_PUBLIC_BLE_V2_H_ +#endif // PLATFORM_PUBLIC_BLE_H_ diff --git a/internal/platform/ble_v2_test.cc b/internal/platform/ble_test.cc similarity index 79% rename from internal/platform/ble_v2_test.cc rename to internal/platform/ble_test.cc index c5500d6c..9b43629d 100644 --- a/internal/platform/ble_v2_test.cc +++ b/internal/platform/ble_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include #include @@ -31,7 +31,7 @@ #include "internal/platform/cancellation_flag.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/feature_flags.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/mac_address.h" #include "internal/platform/medium_environment.h" #include "internal/platform/single_thread_executor.h" @@ -51,9 +51,9 @@ constexpr FeatureFlags kTestCases[] = { }, }; -using ::nearby::api::ble_v2::BleAdvertisementData; -using ::nearby::api::ble_v2::GattCharacteristic; -using ::nearby::api::ble_v2::TxPowerLevel; +using ::nearby::api::ble::BleAdvertisementData; +using ::nearby::api::ble::GattCharacteristic; +using ::nearby::api::ble::TxPowerLevel; using ::testing::Optional; using ::testing::status::StatusIs; @@ -64,26 +64,26 @@ constexpr TxPowerLevel kTxPowerLevel(TxPowerLevel::kHigh); constexpr absl::string_view kServiceIDA{ "com.google.location.nearby.apps.test.a"}; -class BleV2MediumTest : public ::testing::TestWithParam { +class BleMediumTest : public ::testing::TestWithParam { protected: MediumEnvironment& env_{MediumEnvironment::Instance()}; }; -TEST_P(BleV2MediumTest, CanConnectToService) { +TEST_P(BleMediumTest, CanConnectToService) { FeatureFlags feature_flags = GetParam(); env_.SetFeatureFlags(feature_flags); env_.Start(); BluetoothAdapter adapter_a_; BluetoothAdapter adapter_b_; - BleV2Medium ble_a(adapter_a_); - BleV2Medium ble_b(adapter_b_); + BleMedium ble_a(adapter_a_); + BleMedium ble_b(adapter_b_); Uuid service_uuid(1234, 5678); std::string service_id(kServiceIDA); ByteArray advertisement_bytes{std::string(kAdvertisementString)}; CountDownLatch found_latch(1); CountDownLatch lost_latch(1); - BleV2ServerSocket server_socket = ble_b.OpenServerSocket(service_id); + BleServerSocket server_socket = ble_b.OpenServerSocket(service_id); EXPECT_TRUE(server_socket.IsValid()); // Assemble regular advertisement data @@ -93,21 +93,21 @@ TEST_P(BleV2MediumTest, CanConnectToService) { (ble_b.StartAdvertising(advertising_data, {.tx_power_level = kTxPowerLevel, .is_connectable = true})); - BleV2Peripheral discovered_peripheral; + BlePeripheral discovered_peripheral; ble_a.StartScanning( service_uuid, kTxPowerLevel, { .advertisement_found_cb = [&found_latch, &discovered_peripheral]( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementData& advertisement_data) { discovered_peripheral = std::move(peripheral); found_latch.CountDown(); }, }); EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result()); - BleV2Socket socket_a; - BleV2Socket socket_b; + BleSocket socket_a; + BleSocket socket_b; EXPECT_FALSE(socket_a.IsValid()); EXPECT_FALSE(socket_b.IsValid()); { @@ -137,21 +137,21 @@ TEST_P(BleV2MediumTest, CanConnectToService) { env_.Stop(); } -TEST_P(BleV2MediumTest, CanConnectToServiceWithMultipleServices) { +TEST_P(BleMediumTest, CanConnectToServiceWithMultipleServices) { FeatureFlags feature_flags = GetParam(); env_.SetFeatureFlags(feature_flags); env_.Start(); BluetoothAdapter adapter_a_; BluetoothAdapter adapter_b_; - BleV2Medium ble_a(adapter_a_); - BleV2Medium ble_b(adapter_b_); + BleMedium ble_a(adapter_a_); + BleMedium ble_b(adapter_b_); Uuid service_uuid(1234, 5678); std::string service_id(kServiceIDA); ByteArray advertisement_bytes{std::string(kAdvertisementString)}; CountDownLatch found_latch(1); CountDownLatch lost_latch(1); - BleV2ServerSocket server_socket = ble_b.OpenServerSocket(service_id); + BleServerSocket server_socket = ble_b.OpenServerSocket(service_id); EXPECT_TRUE(server_socket.IsValid()); // Assemble regular advertisement data @@ -161,21 +161,21 @@ TEST_P(BleV2MediumTest, CanConnectToServiceWithMultipleServices) { (ble_b.StartAdvertising(advertising_data, {.tx_power_level = kTxPowerLevel, .is_connectable = true})); - BleV2Peripheral discovered_peripheral; + BlePeripheral discovered_peripheral; ble_a.StartMultipleServicesScanning( std::vector{service_uuid}, kTxPowerLevel, { .advertisement_found_cb = [&found_latch, &discovered_peripheral]( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementData& advertisement_data) { discovered_peripheral = std::move(peripheral); found_latch.CountDown(); }, }); EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result()); - BleV2Socket socket_a; - BleV2Socket socket_b; + BleSocket socket_a; + BleSocket socket_b; EXPECT_FALSE(socket_a.IsValid()); EXPECT_FALSE(socket_b.IsValid()); { @@ -205,16 +205,16 @@ TEST_P(BleV2MediumTest, CanConnectToServiceWithMultipleServices) { env_.Stop(); } -TEST_P(BleV2MediumTest, CanDiscoverMultipleServices) { +TEST_P(BleMediumTest, CanDiscoverMultipleServices) { FeatureFlags feature_flags = GetParam(); env_.SetFeatureFlags(feature_flags); env_.Start(); BluetoothAdapter adapter_a_; BluetoothAdapter adapter_b_; BluetoothAdapter adapter_c_; - BleV2Medium ble_a(adapter_a_); - BleV2Medium ble_b(adapter_b_); - BleV2Medium ble_c(adapter_c_); + BleMedium ble_a(adapter_a_); + BleMedium ble_b(adapter_b_); + BleMedium ble_c(adapter_c_); Uuid service_uuid_a(1234, 5678); Uuid service_uuid_b(1234, 5679); std::string service_id(kServiceIDA); @@ -241,7 +241,7 @@ TEST_P(BleV2MediumTest, CanDiscoverMultipleServices) { std::vector{service_uuid_a, service_uuid_b}, kTxPowerLevel, {.advertisement_found_cb = [&found_latch, &found_service_a, &found_service_b, &service_uuid_a, - &service_uuid_b](BleV2Peripheral peripheral, + &service_uuid_b](BlePeripheral peripheral, const BleAdvertisementData& advertisement_data) { if (advertisement_data.service_data.contains(service_uuid_a)) { found_service_a = true; @@ -259,21 +259,21 @@ TEST_P(BleV2MediumTest, CanDiscoverMultipleServices) { env_.Stop(); } -TEST_P(BleV2MediumTest, CanCancelConnect) { +TEST_P(BleMediumTest, CanCancelConnect) { FeatureFlags feature_flags = GetParam(); env_.SetFeatureFlags(feature_flags); env_.Start(); BluetoothAdapter adapter_a_; BluetoothAdapter adapter_b_; - BleV2Medium ble_a(adapter_a_); - BleV2Medium ble_b(adapter_b_); + BleMedium ble_a(adapter_a_); + BleMedium ble_b(adapter_b_); Uuid service_uuid(1234, 5678); std::string service_id(kServiceIDA); ByteArray advertisement_bytes((std::string(kAdvertisementString))); CountDownLatch found_latch(1); CountDownLatch lost_latch(1); - BleV2ServerSocket server_socket = ble_b.OpenServerSocket(service_id); + BleServerSocket server_socket = ble_b.OpenServerSocket(service_id); EXPECT_TRUE(server_socket.IsValid()); // Assemble regular advertisement data. @@ -283,21 +283,21 @@ TEST_P(BleV2MediumTest, CanCancelConnect) { (ble_b.StartAdvertising(advertising_data, {.tx_power_level = kTxPowerLevel, .is_connectable = true})); - BleV2Peripheral discovered_peripheral; + BlePeripheral discovered_peripheral; ble_a.StartScanning( service_uuid, kTxPowerLevel, { .advertisement_found_cb = [&found_latch, &discovered_peripheral]( - BleV2Peripheral peripheral, + BlePeripheral peripheral, const BleAdvertisementData& advertisement_data) { discovered_peripheral = std::move(peripheral); found_latch.CountDown(); }, }); EXPECT_TRUE(found_latch.Await(absl::Milliseconds(1000)).result()); - BleV2Socket socket_a; - BleV2Socket socket_b; + BleSocket socket_a; + BleSocket socket_b; EXPECT_FALSE(socket_a.IsValid()); EXPECT_FALSE(socket_b.IsValid()); { @@ -333,15 +333,15 @@ TEST_P(BleV2MediumTest, CanCancelConnect) { env_.Stop(); } -INSTANTIATE_TEST_SUITE_P(ParametrisedBleMediumTest, BleV2MediumTest, +INSTANTIATE_TEST_SUITE_P(ParametrisedBleMediumTest, BleMediumTest, ::testing::ValuesIn(kTestCases)); -TEST_F(BleV2MediumTest, ConstructorDestructorWorks) { +TEST_F(BleMediumTest, ConstructorDestructorWorks) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); // Make sure we can create functional mediums. ASSERT_TRUE(ble_a.IsValid()); @@ -352,12 +352,12 @@ TEST_F(BleV2MediumTest, ConstructorDestructorWorks) { env_.Stop(); } -TEST_F(BleV2MediumTest, CanStartFastScanningAndFastAdvertising) { +TEST_F(BleMediumTest, CanStartFastScanningAndFastAdvertising) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); Uuid service_uuid(1234, 5678); ByteArray advertisement_bytes((std::string(kAdvertisementString))); CountDownLatch found_latch(1); @@ -366,7 +366,7 @@ TEST_F(BleV2MediumTest, CanStartFastScanningAndFastAdvertising) { service_uuid, kTxPowerLevel, { .advertisement_found_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const BleAdvertisementData& advertisement_data) { found_latch.CountDown(); }, @@ -387,24 +387,23 @@ TEST_F(BleV2MediumTest, CanStartFastScanningAndFastAdvertising) { advertising_data, {.tx_power_level = kTxPowerLevel, .is_connectable = true})); - EXPECT_TRUE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); - EXPECT_TRUE( - env_.GetBleV2MediumStatus(*ble_b.GetImpl()).value().is_advertising); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_b.GetImpl()).value().is_advertising); EXPECT_TRUE(found_latch.Await(kWaitDuration).result()); EXPECT_TRUE(ble_a.StopScanning()); EXPECT_TRUE(ble_b.StopAdvertising()); - EXPECT_FALSE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_FALSE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); EXPECT_FALSE( - env_.GetBleV2MediumStatus(*ble_b.GetImpl()).value().is_advertising); + env_.GetBleMediumStatus(*ble_b.GetImpl()).value().is_advertising); env_.Stop(); } -TEST_F(BleV2MediumTest, CanStartScanningAndAdvertising) { +TEST_F(BleMediumTest, CanStartScanningAndAdvertising) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); Uuid service_uuid(1234, 5678); ByteArray advertisement_bytes{std::string(kAdvertisementString)}; ByteArray advertisement_header_bytes{std::string(kAdvertisementHeaderString)}; @@ -414,7 +413,7 @@ TEST_F(BleV2MediumTest, CanStartScanningAndAdvertising) { service_uuid, kTxPowerLevel, { .advertisement_found_cb = - [&found_latch](BleV2Peripheral peripheral, + [&found_latch](BlePeripheral peripheral, const BleAdvertisementData& advertisement_data) { found_latch.CountDown(); }, @@ -435,105 +434,104 @@ TEST_F(BleV2MediumTest, CanStartScanningAndAdvertising) { advertising_data, {.tx_power_level = kTxPowerLevel, .is_connectable = true})); - EXPECT_TRUE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); - EXPECT_TRUE( - env_.GetBleV2MediumStatus(*ble_b.GetImpl()).value().is_advertising); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_b.GetImpl()).value().is_advertising); EXPECT_TRUE(found_latch.Await(kWaitDuration).result()); EXPECT_TRUE(ble_a.StopScanning()); EXPECT_TRUE(ble_b.StopAdvertising()); - EXPECT_FALSE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_FALSE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); EXPECT_FALSE( - env_.GetBleV2MediumStatus(*ble_b.GetImpl()).value().is_advertising); - env_.UnregisterBleV2Medium(*ble_a.GetImpl()); - env_.UnregisterBleV2Medium(*ble_b.GetImpl()); - EXPECT_EQ(env_.GetBleV2MediumStatus(*ble_a.GetImpl()), std::nullopt); - EXPECT_EQ(env_.GetBleV2MediumStatus(*ble_b.GetImpl()), std::nullopt); + env_.GetBleMediumStatus(*ble_b.GetImpl()).value().is_advertising); + env_.UnregisterBleMedium(*ble_a.GetImpl()); + env_.UnregisterBleMedium(*ble_b.GetImpl()); + EXPECT_EQ(env_.GetBleMediumStatus(*ble_a.GetImpl()), std::nullopt); + EXPECT_EQ(env_.GetBleMediumStatus(*ble_b.GetImpl()), std::nullopt); env_.Stop(); } -TEST_F(BleV2MediumTest, StartThenStopAsyncScanning) { +TEST_F(BleMediumTest, StartThenStopAsyncScanning) { env_.Start(); BluetoothAdapter adapter_a; - BleV2Medium ble_a(adapter_a); + BleMedium ble_a(adapter_a); Uuid service_uuid(1234, 5678); CountDownLatch found_latch_a(1); - std::unique_ptr scanning_session_a = + std::unique_ptr scanning_session_a = ble_a.StartScanning( service_uuid, kTxPowerLevel, - api::ble_v2::BleMedium::ScanningCallback{ + api::ble::BleMedium::ScanningCallback{ .advertisement_found_cb = - [&](api::ble_v2::BlePeripheral::UniqueId peripheral_id, + [&](api::ble::BlePeripheral::UniqueId peripheral_id, BleAdvertisementData advertisement_data) -> void { found_latch_a.CountDown(); }, }); - EXPECT_TRUE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); EXPECT_OK(scanning_session_a->stop_scanning()); - EXPECT_FALSE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_FALSE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); env_.Stop(); } -TEST_F(BleV2MediumTest, CanStartMultipleAsyncScanning) { +TEST_F(BleMediumTest, CanStartMultipleAsyncScanning) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); Uuid service_uuid(1234, 5678); CountDownLatch found_latch_a(1); CountDownLatch found_latch_b(1); - std::unique_ptr scanning_session_a = + std::unique_ptr scanning_session_a = ble_a.StartScanning( service_uuid, kTxPowerLevel, - api::ble_v2::BleMedium::ScanningCallback{ + api::ble::BleMedium::ScanningCallback{ .advertisement_found_cb = - [&](api::ble_v2::BlePeripheral::UniqueId peripheral_id, + [&](api::ble::BlePeripheral::UniqueId peripheral_id, BleAdvertisementData advertisement_data) -> void { found_latch_a.CountDown(); }, }); - std::unique_ptr scanning_session_b = + std::unique_ptr scanning_session_b = ble_b.StartScanning( service_uuid, kTxPowerLevel, - api::ble_v2::BleMedium::ScanningCallback{ + api::ble::BleMedium::ScanningCallback{ .advertisement_found_cb = - [&](api::ble_v2::BlePeripheral::UniqueId peripheral_id, + [&](api::ble::BlePeripheral::UniqueId peripheral_id, BleAdvertisementData advertisement_data) -> void { found_latch_b.CountDown(); }, }); - EXPECT_TRUE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); - EXPECT_TRUE(env_.GetBleV2MediumStatus(*ble_b.GetImpl()).value().is_scanning); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_b.GetImpl()).value().is_scanning); EXPECT_OK(scanning_session_a->stop_scanning()); EXPECT_OK(scanning_session_b->stop_scanning()); - EXPECT_FALSE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); - EXPECT_FALSE(env_.GetBleV2MediumStatus(*ble_b.GetImpl()).value().is_scanning); + EXPECT_FALSE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_FALSE(env_.GetBleMediumStatus(*ble_b.GetImpl()).value().is_scanning); env_.Stop(); } -TEST_F(BleV2MediumTest, CanStartAsyncScanningAndAdvertising) { +TEST_F(BleMediumTest, CanStartAsyncScanningAndAdvertising) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); Uuid service_uuid(1234, 5678); ByteArray advertisement_bytes{std::string(kAdvertisementString)}; ByteArray advertisement_header_bytes{std::string(kAdvertisementHeaderString)}; CountDownLatch found_latch(1); - std::unique_ptr scanning_session = + std::unique_ptr scanning_session = ble_a.StartScanning( service_uuid, kTxPowerLevel, - api::ble_v2::BleMedium::ScanningCallback{ + api::ble::BleMedium::ScanningCallback{ .advertisement_found_cb = - [&](api::ble_v2::BlePeripheral::UniqueId peripheral_id, + [&](api::ble::BlePeripheral::UniqueId peripheral_id, BleAdvertisementData advertisement_data) -> void { found_latch.CountDown(); }, @@ -547,27 +545,26 @@ TEST_F(BleV2MediumTest, CanStartAsyncScanningAndAdvertising) { advertising_data, {.tx_power_level = kTxPowerLevel, .is_connectable = true})); - EXPECT_TRUE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); - EXPECT_TRUE( - env_.GetBleV2MediumStatus(*ble_b.GetImpl()).value().is_advertising); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_TRUE(env_.GetBleMediumStatus(*ble_b.GetImpl()).value().is_advertising); EXPECT_TRUE(found_latch.Await(kWaitDuration).result()); EXPECT_OK(scanning_session->stop_scanning()); EXPECT_TRUE(ble_b.StopAdvertising()); - EXPECT_FALSE(env_.GetBleV2MediumStatus(*ble_a.GetImpl()).value().is_scanning); + EXPECT_FALSE(env_.GetBleMediumStatus(*ble_a.GetImpl()).value().is_scanning); EXPECT_FALSE( - env_.GetBleV2MediumStatus(*ble_b.GetImpl()).value().is_advertising); - env_.UnregisterBleV2Medium(*ble_a.GetImpl()); - env_.UnregisterBleV2Medium(*ble_b.GetImpl()); - EXPECT_EQ(env_.GetBleV2MediumStatus(*ble_a.GetImpl()), std::nullopt); - EXPECT_EQ(env_.GetBleV2MediumStatus(*ble_b.GetImpl()), std::nullopt); + env_.GetBleMediumStatus(*ble_b.GetImpl()).value().is_advertising); + env_.UnregisterBleMedium(*ble_a.GetImpl()); + env_.UnregisterBleMedium(*ble_b.GetImpl()); + EXPECT_EQ(env_.GetBleMediumStatus(*ble_a.GetImpl()), std::nullopt); + EXPECT_EQ(env_.GetBleMediumStatus(*ble_b.GetImpl()), std::nullopt); env_.Stop(); } -TEST_F(BleV2MediumTest, CanStartGattServer) { +TEST_F(BleMediumTest, CanStartGattServer) { env_.Start(); BluetoothAdapter adapter; - BleV2Medium ble(adapter); + BleMedium ble(adapter); Uuid service_uuid(1234, 5678); Uuid characteristic_uuid(5678, 1234); @@ -596,12 +593,12 @@ TEST_F(BleV2MediumTest, CanStartGattServer) { env_.Stop(); } -TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) { +TEST_F(BleMediumTest, GattClientConnectToGattServerWorks) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); Uuid service_uuid(1234, 5678); Uuid characteristic_uuid(5678, 1234); @@ -628,7 +625,7 @@ TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) { MacAddress mac_address = adapter_a.GetAddress(); EXPECT_TRUE(mac_address.IsSet()); std::unique_ptr gatt_client = ble_b.ConnectToGattServer( - BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + BlePeripheral(ble_b, mac_address.address()), kTxPowerLevel, /*ClientGattConnectionCallback=*/{}); ASSERT_NE(gatt_client, nullptr); @@ -651,12 +648,12 @@ TEST_F(BleV2MediumTest, GattClientConnectToGattServerWorks) { env_.Stop(); } -TEST_F(BleV2MediumTest, GattClientConnectToStoppedGattServerFails) { +TEST_F(BleMediumTest, GattClientConnectToStoppedGattServerFails) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); std::unique_ptr gatt_server = ble_a.StartGattServer(/*ServerGattConnectionCallback=*/{}); ASSERT_NE(gatt_server, nullptr); @@ -664,7 +661,7 @@ TEST_F(BleV2MediumTest, GattClientConnectToStoppedGattServerFails) { gatt_server->Stop(); std::unique_ptr gatt_client = ble_b.ConnectToGattServer( - BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + BlePeripheral(ble_b, mac_address.address()), kTxPowerLevel, /*ClientGattConnectionCallback=*/{}); ASSERT_NE(gatt_client, nullptr); @@ -672,12 +669,12 @@ TEST_F(BleV2MediumTest, GattClientConnectToStoppedGattServerFails) { env_.Stop(); } -TEST_F(BleV2MediumTest, GattClientNotifiedWhenServerDisconnects) { +TEST_F(BleMediumTest, GattClientNotifiedWhenServerDisconnects) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); std::unique_ptr gatt_server = ble_a.StartGattServer(/*ServerGattConnectionCallback=*/{}); ASSERT_NE(gatt_server, nullptr); @@ -685,7 +682,7 @@ TEST_F(BleV2MediumTest, GattClientNotifiedWhenServerDisconnects) { // Start GattClient MacAddress mac_address = adapter_a.GetAddress(); std::unique_ptr gatt_client = ble_b.ConnectToGattServer( - BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + BlePeripheral(ble_b, mac_address.address()), kTxPowerLevel, /*ClientGattConnectionCallback=*/{.disconnected_cb = [&]() { disconnected_latch.CountDown(); }}); @@ -697,12 +694,12 @@ TEST_F(BleV2MediumTest, GattClientNotifiedWhenServerDisconnects) { env_.Stop(); } -TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) { +TEST_F(BleMediumTest, GattClientOperatiosOnCharacteristic) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); Uuid service_uuid(1234, 5678); Uuid characteristic_uuid(5678, 1234); std::string written_data; @@ -710,10 +707,10 @@ TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) { std::unique_ptr gatt_server = ble_a.StartGattServer(/*ServerGattConnectionCallback=*/{ .on_characteristic_write_cb = - [&](const api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic, + [&](const api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic, int offset, absl::string_view data, - api::ble_v2::ServerGattConnectionCallback::WriteValueCallback + api::ble::ServerGattConnectionCallback::WriteValueCallback callback) { written_data = data; callback(absl::OkStatus()); @@ -723,7 +720,7 @@ TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) { // Start GattClient. MacAddress mac_address = adapter_a.GetAddress(); std::unique_ptr gatt_client = ble_b.ConnectToGattServer( - BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + BlePeripheral(ble_b, mac_address.address()), kTxPowerLevel, /*ClientGattConnectionCallback=*/{}); ASSERT_NE(gatt_client, nullptr); @@ -759,7 +756,7 @@ TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) { // Can write the characteristic value. EXPECT_TRUE(gatt_client->WriteCharacteristic( *client_characteristic, "hello", - api::ble_v2::GattClient::WriteType::kWithResponse)); + api::ble::GattClient::WriteType::kWithResponse)); EXPECT_EQ(written_data, "hello"); gatt_client->Disconnect(); @@ -767,19 +764,19 @@ TEST_F(BleV2MediumTest, GattClientOperatiosOnCharacteristic) { // Failed to write/read characteristic value as gatt is disconnected. EXPECT_FALSE(gatt_client->WriteCharacteristic( *client_characteristic, "any", - api::ble_v2::GattClient::WriteType::kWithResponse)); + api::ble::GattClient::WriteType::kWithResponse)); EXPECT_THAT(gatt_client->ReadCharacteristic(*client_characteristic), std::nullopt); gatt_server->Stop(); env_.Stop(); } -TEST_F(BleV2MediumTest, GattClientSubscribeNotificationGattServerCanNotify) { +TEST_F(BleMediumTest, GattClientSubscribeNotificationGattServerCanNotify) { env_.Start(); BluetoothAdapter adapter_a; BluetoothAdapter adapter_b; - BleV2Medium ble_a(adapter_a); - BleV2Medium ble_b(adapter_b); + BleMedium ble_a(adapter_a); + BleMedium ble_b(adapter_b); Uuid service_uuid(1234, 5678); Uuid characteristic_uuid(5678, 1234); GattCharacteristic::Permission permissions = @@ -804,7 +801,7 @@ TEST_F(BleV2MediumTest, GattClientSubscribeNotificationGattServerCanNotify) { // Start GattClient MacAddress mac_address = adapter_a.GetAddress(); std::unique_ptr gatt_client = ble_b.ConnectToGattServer( - BleV2Peripheral(ble_b, mac_address.address()), kTxPowerLevel, + BlePeripheral(ble_b, mac_address.address()), kTxPowerLevel, /*ClientGattConnectionCallback=*/{}); ASSERT_NE(gatt_client, nullptr); diff --git a/internal/platform/implementation/BUILD b/internal/platform/implementation/BUILD index 45744e3e..3d9a8689 100644 --- a/internal/platform/implementation/BUILD +++ b/internal/platform/implementation/BUILD @@ -1,6 +1,3 @@ -load("@rules_cc//cc:cc_library.bzl", "cc_library") -load("@rules_cc//cc:cc_test.bzl", "cc_test") - # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +11,10 @@ load("@rules_cc//cc:cc_test.bzl", "cc_test") # 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. + +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("@rules_cc//cc:cc_test.bzl", "cc_test") + licenses(["notice"]) cc_library( @@ -152,7 +153,7 @@ cc_library( name = "comm", hdrs = [ "awdl.h", - "ble_v2.h", + "ble.h", "bluetooth_adapter.h", "bluetooth_classic.h", "credential_callbacks.h", diff --git a/internal/platform/implementation/apple/Tests/GNCBLEUtilsTest.mm b/internal/platform/implementation/apple/Tests/GNCBLEUtilsTest.mm index eff61163..138b4a76 100644 --- a/internal/platform/implementation/apple/Tests/GNCBLEUtilsTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCBLEUtilsTest.mm @@ -19,12 +19,12 @@ #import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEGATTCharacteristic.h" #import "internal/platform/implementation/apple/ble_utils.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" -using GattCharacteristic = ::nearby::api::ble_v2::GattCharacteristic; -using Property = ::nearby::api::ble_v2::GattCharacteristic::Property; -using Permission = ::nearby::api::ble_v2::GattCharacteristic::Permission; -using WriteType = ::nearby::api::ble_v2::GattClient::WriteType; +using GattCharacteristic = ::nearby::api::ble::GattCharacteristic; +using Property = ::nearby::api::ble::GattCharacteristic::Property; +using Permission = ::nearby::api::ble::GattCharacteristic::Permission; +using WriteType = ::nearby::api::ble::GattClient::WriteType; using Uuid = ::nearby::Uuid; using ByteArray = ::nearby::ByteArray; diff --git a/internal/platform/implementation/apple/Tests/GNCPlatformTest.mm b/internal/platform/implementation/apple/Tests/GNCPlatformTest.mm index 93c36dd7..9a307730 100644 --- a/internal/platform/implementation/apple/Tests/GNCPlatformTest.mm +++ b/internal/platform/implementation/apple/Tests/GNCPlatformTest.mm @@ -308,9 +308,9 @@ void GNCEnsureFileAtPath(std::string path) { XCTAssertEqual(bluetooth_classic_medium.get(), nullptr); } -- (void)testCreateBleV2Medium { +- (void)testCreateBleMedium { auto bluetooth_adapter = nearby::api::ImplementationPlatform::CreateBluetoothAdapter(); - auto ble_medium = nearby::api::ImplementationPlatform::CreateBleV2Medium(*bluetooth_adapter); + auto ble_medium = nearby::api::ImplementationPlatform::CreateBleMedium(*bluetooth_adapter); XCTAssertNotEqual(ble_medium.get(), nullptr); } diff --git a/internal/platform/implementation/apple/Tests/ble_peripheral_test.mm b/internal/platform/implementation/apple/Tests/ble_peripheral_test.mm index efa9ea3b..9d8f5249 100644 --- a/internal/platform/implementation/apple/Tests/ble_peripheral_test.mm +++ b/internal/platform/implementation/apple/Tests/ble_peripheral_test.mm @@ -95,7 +95,7 @@ } - (void)testDefaultBlePeripheral { - nearby::api::ble_v2::BlePeripheral &defaultPeripheral = + nearby::api::ble::BlePeripheral &defaultPeripheral = nearby::apple::BlePeripheral::DefaultBlePeripheral(); // 0xffffffffffffffff is the default value from // third_party/nearby/internal/platform/implementation/apple/ble_peripheral.mm when create static diff --git a/internal/platform/implementation/apple/ble_gatt_client.h b/internal/platform/implementation/apple/ble_gatt_client.h index b9bf9ce7..f375586c 100644 --- a/internal/platform/implementation/apple/ble_gatt_client.h +++ b/internal/platform/implementation/apple/ble_gatt_client.h @@ -24,7 +24,7 @@ #include #include -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/uuid.h" @class GNCBLEGATTClient; @@ -32,7 +32,7 @@ namespace nearby { namespace apple { -class GattClient : public api::ble_v2::GattClient { +class GattClient : public api::ble::GattClient { public: explicit GattClient(GNCBLEGATTClient *gatt_client_); ~GattClient() override = default; @@ -51,21 +51,20 @@ class GattClient : public api::ble_v2::GattClient { // services and characteristics first. // // On success, returns the characteristic. On error, returns nullptr. - std::optional GetCharacteristic( + std::optional GetCharacteristic( const Uuid &service_uuid, const Uuid &characteristic_uuid) override; // Reads the requested characteristic from the associated remote device. // // On success, returns the characteristic's value. On error, returns nullptr. std::optional ReadCharacteristic( - const api::ble_v2::GattCharacteristic &characteristic) override; + const api::ble::GattCharacteristic &characteristic) override; // Writes a given characteristic and its values to the associated remote device. // // Returns whether or not the write was successful. - bool WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, - absl::string_view value, - api::ble_v2::GattClient::WriteType type) override; + bool WriteCharacteristic(const api::ble::GattCharacteristic &characteristic, + absl::string_view value, api::ble::GattClient::WriteType type) override; // Enable or disable notifications/indications for a given characteristic. // @@ -74,7 +73,7 @@ class GattClient : public api::ble_v2::GattClient { // // Returns whether or not the subscription was successful. bool SetCharacteristicSubscription( - const api::ble_v2::GattCharacteristic &characteristic, bool enable, + const api::ble::GattCharacteristic &characteristic, bool enable, absl::AnyInvocable on_characteristic_changed_cb) override; // Disconnects an established connection, or cancels a connection attempt currently in progress. diff --git a/internal/platform/implementation/apple/ble_gatt_client.mm b/internal/platform/implementation/apple/ble_gatt_client.mm index 94e5cd66..655878cf 100644 --- a/internal/platform/implementation/apple/ble_gatt_client.mm +++ b/internal/platform/implementation/apple/ble_gatt_client.mm @@ -21,7 +21,7 @@ #include #include -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #import "internal/platform/implementation/apple/Flags/GNCFeatureFlags.h" #import "internal/platform/implementation/apple/Log/GNCLogger.h" @@ -60,7 +60,7 @@ bool GattClient::DiscoverServiceAndCharacteristics(const Uuid &service_uuid, return blockError == nil; } -std::optional GattClient::GetCharacteristic( +std::optional GattClient::GetCharacteristic( const Uuid &service_uuid, const Uuid &characteristic_uuid) { CBUUID *serviceUUID = CBUUID128FromCPP(service_uuid); CBUUID *characteristicUUID = CBUUID128FromCPP(characteristic_uuid); @@ -90,7 +90,7 @@ std::optional GattClient::GetCharacteristic( } std::optional GattClient::ReadCharacteristic( - const api::ble_v2::GattCharacteristic &characteristic) { + const api::ble::GattCharacteristic &characteristic) { NSCondition *condition = [[NSCondition alloc] init]; [condition lock]; __block NSData *blockValue = nil; @@ -115,15 +115,15 @@ std::optional GattClient::ReadCharacteristic( } // TODO(b/290385712): Implement. -bool GattClient::WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, +bool GattClient::WriteCharacteristic(const api::ble::GattCharacteristic &characteristic, absl::string_view value, - api::ble_v2::GattClient::WriteType type) { + api::ble::GattClient::WriteType type) { return false; } // TODO(b/290385712): Implement. bool GattClient::SetCharacteristicSubscription( - const api::ble_v2::GattCharacteristic &characteristic, bool enable, + const api::ble::GattCharacteristic &characteristic, bool enable, absl::AnyInvocable on_characteristic_changed_cb) { return false; } diff --git a/internal/platform/implementation/apple/ble_gatt_server.h b/internal/platform/implementation/apple/ble_gatt_server.h index 836db4c0..2b1c159a 100644 --- a/internal/platform/implementation/apple/ble_gatt_server.h +++ b/internal/platform/implementation/apple/ble_gatt_server.h @@ -21,7 +21,7 @@ #import -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/uuid.h" #import "internal/platform/implementation/apple/ble_peripheral.h" @@ -31,7 +31,7 @@ namespace nearby { namespace apple { -class GattServer : public api::ble_v2::GattServer { +class GattServer : public api::ble::GattServer { public: explicit GattServer(GNCBLEGATTServer *gatt_server_); ~GattServer() override = default; @@ -43,21 +43,21 @@ class GattServer : public api::ble_v2::GattServer { // services with the same UUID. // // Returns no value upon error. - std::optional CreateCharacteristic( + std::optional CreateCharacteristic( const Uuid &service_uuid, const Uuid &characteristic_uuid, - api::ble_v2::GattCharacteristic::Permission permission, - api::ble_v2::GattCharacteristic::Property property) override; + api::ble::GattCharacteristic::Permission permission, + api::ble::GattCharacteristic::Property property) override; // Updates a local characteristic with the provided value. // // Returns whether or not the update was successful. - bool UpdateCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, + bool UpdateCharacteristic(const api::ble::GattCharacteristic &characteristic, const nearby::ByteArray &value) override; // Send a notification or indication that a local characteristic has been updated. // // Returns an absl::Status indicating success or what went wrong. - absl::Status NotifyCharacteristicChanged(const api::ble_v2::GattCharacteristic &characteristic, + absl::Status NotifyCharacteristicChanged(const api::ble::GattCharacteristic &characteristic, bool confirm, const ByteArray &new_value) override; // Stops a GATT server. diff --git a/internal/platform/implementation/apple/ble_gatt_server.mm b/internal/platform/implementation/apple/ble_gatt_server.mm index 64af45f9..92cdff3d 100644 --- a/internal/platform/implementation/apple/ble_gatt_server.mm +++ b/internal/platform/implementation/apple/ble_gatt_server.mm @@ -17,7 +17,7 @@ #import #import -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #import "internal/platform/implementation/apple/Log/GNCLogger.h" #import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEGATTServer.h" @@ -29,10 +29,10 @@ namespace apple { GattServer::GattServer(GNCBLEGATTServer *gatt_server) : gatt_server_(gatt_server) {} -std::optional GattServer::CreateCharacteristic( +std::optional GattServer::CreateCharacteristic( const Uuid &service_uuid, const Uuid &characteristic_uuid, - api::ble_v2::GattCharacteristic::Permission permission, - api::ble_v2::GattCharacteristic::Property property) { + api::ble::GattCharacteristic::Permission permission, + api::ble::GattCharacteristic::Property property) { CBUUID *serviceUUID = CBUUID128FromCPP(service_uuid); CBUUID *characteristicUUID = CBUUID128FromCPP(characteristic_uuid); CBAttributePermissions permissions = CBAttributePermissionsFromCPP(permission); @@ -63,7 +63,7 @@ std::optional GattServer::CreateCharacteristic( return CPPGATTCharacteristicFromObjC(blockCharacteristic); } -bool GattServer::UpdateCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, +bool GattServer::UpdateCharacteristic(const api::ble::GattCharacteristic &characteristic, const nearby::ByteArray &value) { NSCondition *condition = [[NSCondition alloc] init]; [condition lock]; @@ -86,8 +86,7 @@ bool GattServer::UpdateCharacteristic(const api::ble_v2::GattCharacteristic &cha // TODO(b/290385712): Implement. absl::Status GattServer::NotifyCharacteristicChanged( - const api::ble_v2::GattCharacteristic &characteristic, bool confirm, - const ByteArray &new_value) { + const api::ble::GattCharacteristic &characteristic, bool confirm, const ByteArray &new_value) { return absl::UnimplementedError(""); } diff --git a/internal/platform/implementation/apple/ble_l2cap_server_socket.h b/internal/platform/implementation/apple/ble_l2cap_server_socket.h index 9a5b664c..af63c142 100644 --- a/internal/platform/implementation/apple/ble_l2cap_server_socket.h +++ b/internal/platform/implementation/apple/ble_l2cap_server_socket.h @@ -16,14 +16,14 @@ #include "absl/functional/any_invocable.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/ble_v2.h" #import "internal/platform/implementation/apple/ble_l2cap_socket.h" +#include "internal/platform/implementation/ble.h" namespace nearby { namespace apple { // A BLE L2CAP server socket for listening incoming L2CAP socket. -class BleL2capServerSocket : public api::ble_v2::BleL2capServerSocket { +class BleL2capServerSocket : public api::ble::BleL2capServerSocket { public: BleL2capServerSocket() = default; ~BleL2capServerSocket() override; @@ -43,7 +43,7 @@ class BleL2capServerSocket : public api::ble_v2::BleL2capServerSocket { // Returns nullptr on error. // Once error is reported, it is permanent, and L2CAP ServerSocket has to be // closed. - std::unique_ptr Accept() override + std::unique_ptr Accept() override ABSL_LOCKS_EXCLUDED(mutex_); // Closes the L2CAP server socket. diff --git a/internal/platform/implementation/apple/ble_l2cap_server_socket.mm b/internal/platform/implementation/apple/ble_l2cap_server_socket.mm index d4680f2b..557fda38 100644 --- a/internal/platform/implementation/apple/ble_l2cap_server_socket.mm +++ b/internal/platform/implementation/apple/ble_l2cap_server_socket.mm @@ -35,7 +35,7 @@ int BleL2capServerSocket::GetPSM() const { return psm_; } void BleL2capServerSocket::SetPSM(int psm) { psm_ = psm; } // TODO: b/399815436 - Refactor Accept() and AddPendingSocket() for better readability. -std::unique_ptr BleL2capServerSocket::Accept() { +std::unique_ptr BleL2capServerSocket::Accept() { absl::MutexLock lock(&mutex_); while (!closed_ && pending_sockets_.empty()) { cond_.Wait(&mutex_); diff --git a/internal/platform/implementation/apple/ble_l2cap_socket.h b/internal/platform/implementation/apple/ble_l2cap_socket.h index af0ed876..2a95ad62 100644 --- a/internal/platform/implementation/apple/ble_l2cap_socket.h +++ b/internal/platform/implementation/apple/ble_l2cap_socket.h @@ -14,7 +14,7 @@ #include -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #import "internal/platform/implementation/apple/ble_peripheral.h" @@ -27,7 +27,7 @@ namespace apple { /** A readable stream of bytes. */ class BleL2capInputStream : public InputStream { public: - explicit BleL2capInputStream(GNCBLEL2CAPConnection* connection); + explicit BleL2capInputStream(GNCBLEL2CAPConnection *connection); ~BleL2capInputStream() override; // Reads at most `size` bytes from the input stream. @@ -42,7 +42,7 @@ class BleL2capInputStream : public InputStream { private: GNCMConnectionHandlers *connectionHandlers_; - GNCBLEL2CAPConnection* connection_; + GNCBLEL2CAPConnection *connection_; NSMutableArray *newDataPackets_; NSMutableData *accumulatedData_; NSCondition *condition_; @@ -51,7 +51,7 @@ class BleL2capInputStream : public InputStream { /** A writable stream of bytes. */ class BleL2capOutputStream : public OutputStream { public: - explicit BleL2capOutputStream(GNCBLEL2CAPConnection* connection) + explicit BleL2capOutputStream(GNCBLEL2CAPConnection *connection) : connection_(connection), condition_([[NSCondition alloc] init]) {} ~BleL2capOutputStream() override; @@ -71,21 +71,21 @@ class BleL2capOutputStream : public OutputStream { Exception Close() override; private: - GNCBLEL2CAPConnection* connection_; + GNCBLEL2CAPConnection *connection_; NSCondition *condition_; }; /** * Concrete BleL2capSocket implementation. */ -class BleL2capSocket : public api::ble_v2::BleL2capSocket { +class BleL2capSocket : public api::ble::BleL2capSocket { public: - explicit BleL2capSocket(GNCBLEL2CAPConnection* connection); + explicit BleL2capSocket(GNCBLEL2CAPConnection *connection); // The peripheral used to create the socket must outlive the socket or undefined behavior will // occur. BleL2capSocket(GNCBLEL2CAPConnection *connection, - api::ble_v2::BlePeripheral::UniqueId peripheral_id); + api::ble::BlePeripheral::UniqueId peripheral_id); ~BleL2capSocket() override; // Returns the InputStream of the BleL2capSocket. @@ -109,7 +109,7 @@ class BleL2capSocket : public api::ble_v2::BleL2capSocket { // Returns valid BlePeripheral pointer if there is a connection, and // nullptr otherwise. - api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; } + api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; } bool IsClosed() const ABSL_LOCKS_EXCLUDED(mutex_); @@ -120,7 +120,7 @@ class BleL2capSocket : public api::ble_v2::BleL2capSocket { bool closed_ ABSL_GUARDED_BY(mutex_) = false; std::unique_ptr input_stream_; std::unique_ptr output_stream_; - api::ble_v2::BlePeripheral::UniqueId peripheral_id_; + api::ble::BlePeripheral::UniqueId peripheral_id_; }; } // namespace apple diff --git a/internal/platform/implementation/apple/ble_l2cap_socket.mm b/internal/platform/implementation/apple/ble_l2cap_socket.mm index 833d7b57..170764c4 100644 --- a/internal/platform/implementation/apple/ble_l2cap_socket.mm +++ b/internal/platform/implementation/apple/ble_l2cap_socket.mm @@ -17,7 +17,7 @@ #import "internal/platform/implementation/apple/Log/GNCLogger.h" #import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEL2CAPConnection.h" #import "internal/platform/implementation/apple/utils.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" namespace nearby { namespace apple { @@ -166,7 +166,7 @@ BleL2capSocket::BleL2capSocket(GNCBLEL2CAPConnection *connection) : BleL2capSocket(connection, BlePeripheral::DefaultBlePeripheral().GetUniqueId()) {} BleL2capSocket::BleL2capSocket(GNCBLEL2CAPConnection *connection, - api::ble_v2::BlePeripheral::UniqueId peripheral_id) + api::ble::BlePeripheral::UniqueId peripheral_id) : input_stream_(std::make_unique(connection)), output_stream_(std::make_unique(connection)), peripheral_id_(peripheral_id) {} diff --git a/internal/platform/implementation/apple/ble_medium.h b/internal/platform/implementation/apple/ble_medium.h index a5ba76ab..a42fca59 100644 --- a/internal/platform/implementation/apple/ble_medium.h +++ b/internal/platform/implementation/apple/ble_medium.h @@ -30,7 +30,7 @@ #import "internal/platform/implementation/apple/ble_server_socket.h" #import "internal/platform/implementation/apple/bluetooth_adapter_v2.h" #import "internal/platform/implementation/apple/single_thread_executor.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/uuid.h" @@ -44,7 +44,7 @@ namespace apple { // The main BLE medium used inside of Nearby. This serves as the entry point for all BLE and GATT // related operations. -class BleMedium : public api::ble_v2::BleMedium { +class BleMedium : public api::ble::BleMedium { public: BleMedium(); ~BleMedium() override; @@ -55,16 +55,16 @@ class BleMedium : public api::ble_v2::BleMedium { // the stop_advertising callback in AdvertisingSession. // // Advertising must be stopped before attempting to start advertising again. - std::unique_ptr StartAdvertising( - const api::ble_v2::BleAdvertisementData &advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters, - api::ble_v2::BleMedium::AdvertisingCallback callback) override; + std::unique_ptr StartAdvertising( + const api::ble::BleAdvertisementData &advertising_data, + api::ble::AdvertiseParameters advertise_set_parameters, + api::ble::BleMedium::AdvertisingCallback callback) override; // Starts BLE advertising and returns whether or not it was successful. // // Advertising must be stopped before attempting to start advertising again. - bool StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters) override; + bool StartAdvertising(const api::ble::BleAdvertisementData &advertising_data, + api::ble::AdvertiseParameters advertise_set_parameters) override; // Stops advertising. // @@ -77,20 +77,20 @@ class BleMedium : public api::ble_v2::BleMedium { // scanning, invoke the stop_scanning callback in ScanningSession. // // Scanning must be stopped before attempting to start scanning again. - std::unique_ptr StartScanning( - const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BleMedium::ScanningCallback callback) override; + std::unique_ptr StartScanning( + const Uuid &service_uuid, api::ble::TxPowerLevel tx_power_level, + api::ble::BleMedium::ScanningCallback callback) override; // Starts scanning and returns whether or not it was successful. // // Scanning must be stopped before attempting to start scanning again. - bool StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BleMedium::ScanCallback callback) override; + bool StartScanning(const Uuid &service_uuid, api::ble::TxPowerLevel tx_power_level, + api::ble::BleMedium::ScanCallback callback) override; // Starts scanning on multiple services and returns whether or not it was successful. bool StartMultipleServicesScanning(const std::vector &service_uuids, - api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BleMedium::ScanCallback callback) override; + api::ble::TxPowerLevel tx_power_level, + api::ble::BleMedium::ScanCallback callback) override; // Stops scanning. // @@ -110,28 +110,28 @@ class BleMedium : public api::ble_v2::BleMedium { // TODO(b/290385712): ServerGattConnectionCallback methods are not yet implemented. // // Starts a GATT server. Returns a nullptr upon error. - std::unique_ptr StartGattServer( - api::ble_v2::ServerGattConnectionCallback callback) override; + std::unique_ptr StartGattServer( + api::ble::ServerGattConnectionCallback callback) override; // Connects to a GATT server and negotiates the specified connection parameters. Returns nullptr // upon error. // // The peripheral must outlive the GATT client or undefined behavior will occur. The peripheral // should not be modified by this method. - std::unique_ptr ConnectToGattServer( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::ClientGattConnectionCallback callback) override; + std::unique_ptr ConnectToGattServer( + api::ble::BlePeripheral::UniqueId peripheral_id, api::ble::TxPowerLevel tx_power_level, + api::ble::ClientGattConnectionCallback callback) override; // Opens a BLE server socket based on service ID. // // On success, returns a new BleServerSocket. On error, returns nullptr. - std::unique_ptr OpenServerSocket( + std::unique_ptr OpenServerSocket( const std::string &service_id) override; // Opens a L2CAP server socket based on service ID. // // On success, returns a new BleL2capServerSocket. On error, returns nullptr. - std::unique_ptr OpenL2capServerSocket( + std::unique_ptr OpenL2capServerSocket( const std::string &service_id) override; // TODO(b/290385712): cancellation_flag support is not yet implemented. @@ -142,10 +142,10 @@ class BleMedium : public api::ble_v2::BleMedium { // should not be modified by this method. // // On success, returns a new BleSocket. On error, returns nullptr. - std::unique_ptr Connect( - const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - CancellationFlag *cancellation_flag) override; + std::unique_ptr Connect(const std::string &service_id, + api::ble::TxPowerLevel tx_power_level, + api::ble::BlePeripheral::UniqueId peripheral_id, + CancellationFlag *cancellation_flag) override; // TODO(b/290385712): cancellation_flag support is not yet implemented. // @@ -155,9 +155,9 @@ class BleMedium : public api::ble_v2::BleMedium { // should not be modified by this method. // // On success, returns a new BleL2capSocket. On error, returns nullptr. - std::unique_ptr ConnectOverL2cap( - int psm, const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, + std::unique_ptr ConnectOverL2cap( + int psm, const std::string &service_id, api::ble::TxPowerLevel tx_power_level, + api::ble::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) override; // Returns whether the hardware supports BOTH advertising extensions and extended scans. @@ -171,7 +171,7 @@ class BleMedium : public api::ble_v2::BleMedium { // // Returns std::nullopt if cannot retrieve the BlePeripheral from the native // BLE peripheral id. - std::optional RetrieveBlePeripheralIdFromNativeId( + std::optional RetrieveBlePeripheralIdFromNativeId( const std::string &ble_peripheral_native_id) override; private: @@ -181,15 +181,14 @@ class BleMedium : public api::ble_v2::BleMedium { PeripheralsMap() = default; ~PeripheralsMap() = default; - api::ble_v2::BlePeripheral::UniqueId Add(id peripheral) - ABSL_LOCKS_EXCLUDED(mutex_); - id Get(api::ble_v2::BlePeripheral::UniqueId peripheral_id) + api::ble::BlePeripheral::UniqueId Add(id peripheral) ABSL_LOCKS_EXCLUDED(mutex_); + id Get(api::ble::BlePeripheral::UniqueId peripheral_id) ABSL_LOCKS_EXCLUDED(mutex_); void Clear() ABSL_LOCKS_EXCLUDED(mutex_); private: absl::Mutex mutex_; - absl::flat_hash_map> peripherals_ + absl::flat_hash_map> peripherals_ ABSL_GUARDED_BY(mutex_); }; @@ -197,9 +196,9 @@ class BleMedium : public api::ble_v2::BleMedium { NSDictionary *serviceData); void ClearAdvertisementPacketsMap(); void CleanUpExpiredAdvertisementPackets(NSDate *now); - bool ShouldReportAdvertisement(NSDate *now, api::ble_v2::BlePeripheral::UniqueId peripheral_id, + bool ShouldReportAdvertisement(NSDate *now, api::ble::BlePeripheral::UniqueId peripheral_id, NSDictionary *service_data); - void AddAdvertisementPacketInfo(api::ble_v2::BlePeripheral::UniqueId peripheral_id, + void AddAdvertisementPacketInfo(api::ble::BlePeripheral::UniqueId peripheral_id, NSDictionary *service_data); NSDate *GetLastTimestampToCleanExpiredAdvertisementPackets(); @@ -216,7 +215,7 @@ class BleMedium : public api::ble_v2::BleMedium { absl::Mutex advertisement_packets_mutex_; // A map for maintaining the set of advertisement packets that should be tracked. - absl::flat_hash_map + absl::flat_hash_map advertisement_packets_map_ ABSL_GUARDED_BY(advertisement_packets_mutex_); // The timestamp of the last time to clean up expired advertisement packets. This is used to // prevent the map from growing indefinitely. @@ -228,10 +227,10 @@ class BleMedium : public api::ble_v2::BleMedium { GNSCentralManager *socketCentralManager_; // Used for the blocking version of StartAdvertising and only has an advertisement found callback. - api::ble_v2::BleMedium::ScanCallback scan_cb_; + api::ble::BleMedium::ScanCallback scan_cb_; // Used for the async version of StartAdvertising and has both an advertisement found and result // callback. - api::ble_v2::BleMedium::ScanningCallback scanning_cb_; + api::ble::BleMedium::ScanningCallback scanning_cb_; absl::Mutex l2cap_server_socket_mutex_; BleL2capServerSocket *l2cap_server_socket_ptr_ = nullptr; diff --git a/internal/platform/implementation/apple/ble_medium.mm b/internal/platform/implementation/apple/ble_medium.mm index 6750f3a9..49f7700d 100644 --- a/internal/platform/implementation/apple/ble_medium.mm +++ b/internal/platform/implementation/apple/ble_medium.mm @@ -24,7 +24,7 @@ #include "internal/platform/implementation/apple/ble_utils.h" #include "internal/platform/implementation/apple/utils.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #import "internal/platform/implementation/apple/Mediums/BLE/GNCBLEGATTCharacteristic.h" @@ -92,14 +92,14 @@ BleMedium::BleMedium() : medium_([[GNCBLEMedium alloc] init]) { // ble_medium.mm BleMedium::~BleMedium() { [medium_ stop]; } -std::unique_ptr BleMedium::StartAdvertising( - const api::ble_v2::BleAdvertisementData &advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters, - api::ble_v2::BleMedium::AdvertisingCallback callback) { +std::unique_ptr BleMedium::StartAdvertising( + const api::ble::BleAdvertisementData &advertising_data, + api::ble::AdvertiseParameters advertise_set_parameters, + api::ble::BleMedium::AdvertisingCallback callback) { NSMutableDictionary *serviceData = ObjCServiceDataFromCPP(advertising_data.service_data); - __block api::ble_v2::BleMedium::AdvertisingCallback blockCallback = std::move(callback); + __block api::ble::BleMedium::AdvertisingCallback blockCallback = std::move(callback); [socketPeripheralManager_ start]; @@ -115,8 +115,8 @@ std::unique_ptr BleMedium::StartAdve }}); } -bool BleMedium::StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters) { +bool BleMedium::StartAdvertising(const api::ble::BleAdvertisementData &advertising_data, + api::ble::AdvertiseParameters advertise_set_parameters) { NSMutableDictionary *serviceData = ObjCServiceDataFromCPP(advertising_data.service_data); @@ -158,7 +158,7 @@ bool BleMedium::StopAdvertising() { void BleMedium::HandleAdvertisementFound(id peripheral, NSDictionary *serviceData) { - api::ble_v2::BleAdvertisementData data; + api::ble::BleAdvertisementData data; NSDate *now = [NSDate date]; if ([now timeIntervalSinceDate:GetLastTimestampToCleanExpiredAdvertisementPackets()] > @@ -166,7 +166,7 @@ void BleMedium::HandleAdvertisementFound(id peripheral, ClearAdvertisementPacketsMap(); } - api::ble_v2::BlePeripheral::UniqueId peripheral_id = peripheral.identifier.hash; + api::ble::BlePeripheral::UniqueId peripheral_id = peripheral.identifier.hash; if (!ShouldReportAdvertisement(now, peripheral_id, serviceData)) { return; @@ -177,7 +177,7 @@ void BleMedium::HandleAdvertisementFound(id peripheral, } // Add the peripheral to the map if we haven't discovered it yet. - api::ble_v2::BlePeripheral::UniqueId unique_id = peripherals_.Add(peripheral); + api::ble::BlePeripheral::UniqueId unique_id = peripherals_.Add(peripheral); AddAdvertisementPacketInfo(unique_id, serviceData); #if DEBUG @@ -196,9 +196,9 @@ void BleMedium::HandleAdvertisementFound(id peripheral, } } -std::unique_ptr BleMedium::StartScanning( - const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BleMedium::ScanningCallback callback) { +std::unique_ptr BleMedium::StartScanning( + const Uuid &service_uuid, api::ble::TxPowerLevel tx_power_level, + api::ble::BleMedium::ScanningCallback callback) { CBUUID *serviceUUID = CBUUID128FromCPP(service_uuid); scanning_cb_ = std::move(callback); @@ -230,8 +230,8 @@ std::unique_ptr BleMedium::StartScannin }}); } -bool BleMedium::StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BleMedium::ScanCallback callback) { +bool BleMedium::StartScanning(const Uuid &service_uuid, api::ble::TxPowerLevel tx_power_level, + api::ble::BleMedium::ScanCallback callback) { CBUUID *serviceUUID = CBUUID128FromCPP(service_uuid); scan_cb_ = std::move(callback); @@ -268,8 +268,8 @@ bool BleMedium::StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLeve } bool BleMedium::StartMultipleServicesScanning(const std::vector &service_uuids, - api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BleMedium::ScanCallback callback) { + api::ble::TxPowerLevel tx_power_level, + api::ble::BleMedium::ScanCallback callback) { if (service_uuids.empty()) { GNCLoggerError(@"No service UUIDs provided"); return false; @@ -355,8 +355,8 @@ bool BleMedium::ResumeMediumScanning() { } // TODO(b/290385712): Add implementation that calls ServerGattConnectionCallback methods. -std::unique_ptr BleMedium::StartGattServer( - api::ble_v2::ServerGattConnectionCallback callback) { +std::unique_ptr BleMedium::StartGattServer( + api::ble::ServerGattConnectionCallback callback) { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); __block GNCBLEGATTServer *blockServer = nil; __block NSError *blockError = nil; @@ -381,16 +381,16 @@ std::unique_ptr BleMedium::StartGattServer( return std::make_unique(blockServer); } -std::unique_ptr BleMedium::ConnectToGattServer( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::ClientGattConnectionCallback callback) { +std::unique_ptr BleMedium::ConnectToGattServer( + api::ble::BlePeripheral::UniqueId peripheral_id, api::ble::TxPowerLevel tx_power_level, + api::ble::ClientGattConnectionCallback callback) { id peripheral = peripherals_.Get(peripheral_id); if (!peripheral) { GNCLoggerError(@"[NEARBY] Failed to connect to Gatt server: peripheral is not found."); return nullptr; } - api::ble_v2::ClientGattConnectionCallback thread_callback = { + api::ble::ClientGattConnectionCallback thread_callback = { .on_characteristic_changed_cb = [this, call_on_characteristic_changed_cb = std::move(callback.on_characteristic_changed_cb)]( @@ -409,7 +409,7 @@ std::unique_ptr BleMedium::ConnectToGattServer( }); }}; - __block api::ble_v2::ClientGattConnectionCallback blockCallback = std::move(thread_callback); + __block api::ble::ClientGattConnectionCallback blockCallback = std::move(thread_callback); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); __block GNCBLEGATTClient *blockClient = nil; @@ -440,7 +440,7 @@ std::unique_ptr BleMedium::ConnectToGattServer( } // TODO(b/293336684): Old Weave code that need to be deleted once shared Weave is complete. -std::unique_ptr BleMedium::OpenServerSocket( +std::unique_ptr BleMedium::OpenServerSocket( const std::string &service_id) { auto server_socket = std::make_unique(); __block auto server_socket_ptr = server_socket.get(); @@ -512,7 +512,7 @@ std::unique_ptr BleMedium::OpenServerSocket( return std::move(server_socket); } -std::unique_ptr BleMedium::OpenL2capServerSocket( +std::unique_ptr BleMedium::OpenL2capServerSocket( const std::string &service_id) { dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); __block NSError *blockPSMPublishedError = nil; @@ -575,9 +575,9 @@ std::unique_ptr BleMedium::OpenL2capServerSoc // TODO(b/290385712): Add support for @c cancellation_flag. // TODO(b/293336684): Old Weave code that need to be deleted once shared Weave is complete. -std::unique_ptr BleMedium::Connect( - const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) { +std::unique_ptr BleMedium::Connect( + const std::string &service_id, api::ble::TxPowerLevel tx_power_level, + api::ble::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) { id peripheral = peripherals_.Get(peripheral_id); if (!peripheral) { GNCLoggerError(@"Failed to connect to Gatt server: peripheral is not found."); @@ -630,9 +630,9 @@ std::unique_ptr BleMedium::Connect( return std::move(socket); } -std::unique_ptr BleMedium::ConnectOverL2cap( - int psm, const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) { +std::unique_ptr BleMedium::ConnectOverL2cap( + int psm, const std::string &service_id, api::ble::TxPowerLevel tx_power_level, + api::ble::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) { id peripheral = peripherals_.Get(peripheral_id); if (!peripheral) { GNCLoggerError(@"Failed to connect over L2CAP: peripheral is not found."); @@ -686,7 +686,7 @@ bool BleMedium::IsExtendedAdvertisementsAvailable() { return [medium_ supportsExtendedAdvertisements]; } -std::optional BleMedium::RetrieveBlePeripheralIdFromNativeId( +std::optional BleMedium::RetrieveBlePeripheralIdFromNativeId( const std::string &ble_peripheral_native_id) { NSString *uuidString = [[NSString alloc] initWithUTF8String:ble_peripheral_native_id.c_str()]; if (uuidString == nil) { @@ -736,7 +736,7 @@ void BleMedium::CleanUpExpiredAdvertisementPackets(NSDate *now) { } bool BleMedium::ShouldReportAdvertisement(NSDate *now, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, + api::ble::BlePeripheral::UniqueId peripheral_id, NSDictionary *service_data) { absl::MutexLock lock(&advertisement_packets_mutex_); if (service_data == nil || service_data.count == 0) { @@ -768,21 +768,20 @@ bool BleMedium::ShouldReportAdvertisement(NSDate *now, return true; } -void BleMedium::AddAdvertisementPacketInfo(api::ble_v2::BlePeripheral::UniqueId peripheral_id, +void BleMedium::AddAdvertisementPacketInfo(api::ble::BlePeripheral::UniqueId peripheral_id, NSDictionary *service_data) { absl::MutexLock lock(&advertisement_packets_mutex_); advertisement_packets_map_[peripheral_id] = {[NSDate date], service_data}; } -api::ble_v2::BlePeripheral::UniqueId BleMedium::PeripheralsMap::Add(id peripheral) { +api::ble::BlePeripheral::UniqueId BleMedium::PeripheralsMap::Add(id peripheral) { absl::MutexLock lock(&mutex_); - api::ble_v2::BlePeripheral::UniqueId peripheral_id = peripheral.identifier.hash; + api::ble::BlePeripheral::UniqueId peripheral_id = peripheral.identifier.hash; peripherals_.insert({peripheral_id, peripheral}); return peripheral_id; } -id BleMedium::PeripheralsMap::Get( - api::ble_v2::BlePeripheral::UniqueId peripheral_id) { +id BleMedium::PeripheralsMap::Get(api::ble::BlePeripheral::UniqueId peripheral_id) { absl::MutexLock lock(&mutex_); auto peripheral_it = peripherals_.find(peripheral_id); if (peripheral_it == peripherals_.end()) { diff --git a/internal/platform/implementation/apple/ble_peripheral.h b/internal/platform/implementation/apple/ble_peripheral.h index 66e48602..9ae1bfba 100644 --- a/internal/platform/implementation/apple/ble_peripheral.h +++ b/internal/platform/implementation/apple/ble_peripheral.h @@ -24,7 +24,7 @@ #include -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" @protocol GNCPeripheral; @@ -37,10 +37,10 @@ namespace apple { // Many times a CoreBluetooth peripheral is not available, namely, when the // remote device is a central. For these cases, an EmptyBlePeripheral should be // used instead. -class BlePeripheral : public api::ble_v2::BlePeripheral { +class BlePeripheral : public api::ble::BlePeripheral { public: // Returns a reference to a default BlePeripheral. - static api::ble_v2::BlePeripheral& DefaultBlePeripheral(); + static api::ble::BlePeripheral& DefaultBlePeripheral(); explicit BlePeripheral(id peripheral); ~BlePeripheral() override = default; diff --git a/internal/platform/implementation/apple/ble_peripheral.mm b/internal/platform/implementation/apple/ble_peripheral.mm index 8f7e7971..d5f82c54 100644 --- a/internal/platform/implementation/apple/ble_peripheral.mm +++ b/internal/platform/implementation/apple/ble_peripheral.mm @@ -19,7 +19,7 @@ #include -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #import "internal/platform/implementation/apple/Mediums/BLE/GNCPeripheral.h" @@ -29,11 +29,11 @@ namespace apple { #pragma mark - BlePeripheral BlePeripheral::BlePeripheral(id peripheral) - : api::ble_v2::BlePeripheral(peripheral.identifier.hash), peripheral_(peripheral) {} + : api::ble::BlePeripheral(peripheral.identifier.hash), peripheral_(peripheral) {} -api::ble_v2::BlePeripheral& BlePeripheral::DefaultBlePeripheral() { - static api::ble_v2::BlePeripheral* default_peripheral = - new api::ble_v2::BlePeripheral(0xffffffffffffffff); +api::ble::BlePeripheral& BlePeripheral::DefaultBlePeripheral() { + static api::ble::BlePeripheral* default_peripheral = + new api::ble::BlePeripheral(0xffffffffffffffff); return *default_peripheral; } diff --git a/internal/platform/implementation/apple/ble_server_socket.h b/internal/platform/implementation/apple/ble_server_socket.h index f4758e0c..a205299f 100644 --- a/internal/platform/implementation/apple/ble_server_socket.h +++ b/internal/platform/implementation/apple/ble_server_socket.h @@ -12,12 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Note: File language is detected using heuristics. Many Objective-C++ headers are incorrectly -// classified as C++ resulting in invalid linter errors. The use of "NSArray" and other Foundation -// classes like "NSData", "NSDictionary" and "NSUUID" are highly weighted for Objective-C and -// Objective-C++ scores. Oddly, "#import " does not contribute any points. -// This comment alone should be enough to trick the IDE in to believing this is actually some sort -// of Objective-C file. See: cs/google3/devtools/search/lang/recognize_language_classifiers_data +// Note: File language is detected using heuristics. Many Objective-C++ headers +// are incorrectly classified as C++ resulting in invalid linter errors. The use +// of "NSArray" and other Foundation classes like "NSData", "NSDictionary" and +// "NSUUID" are highly weighted for Objective-C and Objective-C++ scores. Oddly, +// "#import " does not contribute any points. This +// comment alone should be enough to trick the IDE in to believing this is +// actually some sort of Objective-C file. See: +// cs/google3/devtools/search/lang/recognize_language_classifiers_data // TODO(b/293336684): Remove this file when shared Weave is complete. @@ -27,15 +29,14 @@ #include "absl/functional/any_invocable.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/ble_v2.h" - #import "internal/platform/implementation/apple/ble_socket.h" +#include "internal/platform/implementation/ble.h" namespace nearby { namespace apple { // A BLE server socket for listening for incoming Weave sockets. -class BleServerSocket : public api::ble_v2::BleServerSocket { +class BleServerSocket : public api::ble::BleServerSocket { public: BleServerSocket() = default; ~BleServerSocket() override; @@ -49,7 +50,7 @@ class BleServerSocket : public api::ble_v2::BleServerSocket { // On success, returns connected socket, ready to exchange data or nullptr on // error. Once error is reported, it is permanent, and ServerSocket must be // closed. - std::unique_ptr Accept() override + std::unique_ptr Accept() override ABSL_LOCKS_EXCLUDED(mutex_); // Close the server socket. diff --git a/internal/platform/implementation/apple/ble_server_socket.mm b/internal/platform/implementation/apple/ble_server_socket.mm index 42c2d1ef..2bf3d898 100644 --- a/internal/platform/implementation/apple/ble_server_socket.mm +++ b/internal/platform/implementation/apple/ble_server_socket.mm @@ -29,7 +29,7 @@ BleServerSocket::~BleServerSocket() { DoClose(); } -std::unique_ptr BleServerSocket::Accept() { +std::unique_ptr BleServerSocket::Accept() { absl::MutexLock lock(&mutex_); while (!closed_ && pending_sockets_.empty()) { cond_.Wait(&mutex_); diff --git a/internal/platform/implementation/apple/ble_socket.h b/internal/platform/implementation/apple/ble_socket.h index cd877066..e0ee8ad4 100644 --- a/internal/platform/implementation/apple/ble_socket.h +++ b/internal/platform/implementation/apple/ble_socket.h @@ -25,7 +25,7 @@ #include -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #import "internal/platform/implementation/apple/ble_peripheral.h" @@ -88,13 +88,13 @@ class BleOutputStream : public OutputStream { }; // A BLE Weave socket. -class BleSocket : public api::ble_v2::BleSocket { +class BleSocket : public api::ble::BleSocket { public: explicit BleSocket(id connection); // The peripheral used to create the socket must outlive the socket or undefined behavior will // occur. - BleSocket(id connection, api::ble_v2::BlePeripheral::UniqueId peripheral_id); + BleSocket(id connection, api::ble::BlePeripheral::UniqueId peripheral_id); ~BleSocket() override; // Returns the InputStream of the BleSocket. @@ -116,7 +116,7 @@ class BleSocket : public api::ble_v2::BleSocket { // Returns valid BlePeripheral pointer if there is a connection, and // nullptr otherwise. - api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; } + api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; } bool IsClosed() const ABSL_LOCKS_EXCLUDED(mutex_); @@ -130,7 +130,7 @@ class BleSocket : public api::ble_v2::BleSocket { bool closed_ ABSL_GUARDED_BY(mutex_) = false; std::unique_ptr input_stream_; std::unique_ptr output_stream_; - api::ble_v2::BlePeripheral::UniqueId peripheral_id_; + api::ble::BlePeripheral::UniqueId peripheral_id_; absl::AnyInvocable close_notifier_; }; diff --git a/internal/platform/implementation/apple/ble_socket.mm b/internal/platform/implementation/apple/ble_socket.mm index 230b1f9c..29fe7917 100644 --- a/internal/platform/implementation/apple/ble_socket.mm +++ b/internal/platform/implementation/apple/ble_socket.mm @@ -14,7 +14,7 @@ #import "internal/platform/implementation/apple/ble_socket.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #import "internal/platform/implementation/apple/Mediums/BLE/GNCMBleConnection.h" #import "internal/platform/implementation/apple/ble_peripheral.h" @@ -179,8 +179,7 @@ BleSocket::BleSocket(id connection) output_stream_(new BleOutputStream(connection)), peripheral_id_(BlePeripheral::DefaultBlePeripheral().GetUniqueId()) {} -BleSocket::BleSocket(id connection, - api::ble_v2::BlePeripheral::UniqueId peripheral_id) +BleSocket::BleSocket(id connection, api::ble::BlePeripheral::UniqueId peripheral_id) : input_stream_(new BleInputStream()), output_stream_(new BleOutputStream(connection)), peripheral_id_(peripheral_id) {} diff --git a/internal/platform/implementation/apple/ble_utils.h b/internal/platform/implementation/apple/ble_utils.h index 4477c403..900d521f 100644 --- a/internal/platform/implementation/apple/ble_utils.h +++ b/internal/platform/implementation/apple/ble_utils.h @@ -22,7 +22,7 @@ #import #import -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" @class GNCBLEGATTCharacteristic; @@ -36,25 +36,25 @@ CBUUID *CBUUID16FromCPP(const Uuid &uuid); CBUUID *CBUUID128FromCPP(const Uuid &uuid); /** Converts a C++ write type enum to a CoreBluetooth write type. */ -CBCharacteristicWriteType CBCharacteristicWriteTypeFromCPP(api::ble_v2::GattClient::WriteType wt); +CBCharacteristicWriteType CBCharacteristicWriteTypeFromCPP(api::ble::GattClient::WriteType wt); -/** - * Converts a C++ permission enum to CoreBluetooth permissions. - * +/** + * Converts a C++ permission enum to CoreBluetooth permissions. + * * This conversion keeps multiple permission values intact. */ -CBAttributePermissions CBAttributePermissionsFromCPP(api::ble_v2::GattCharacteristic::Permission p); +CBAttributePermissions CBAttributePermissionsFromCPP(api::ble::GattCharacteristic::Permission p); -/** - * Converts a C++ property enum to CoreBluetooth properties. - * +/** + * Converts a C++ property enum to CoreBluetooth properties. + * * This conversion keeps multiple property values intact. */ CBCharacteristicProperties CBCharacteristicPropertiesFromCPP( - api::ble_v2::GattCharacteristic::Property p); + api::ble::GattCharacteristic::Property p); /** Converts a C++ characteristic to an Objective-C characteristic. */ -GNCBLEGATTCharacteristic *ObjCGATTCharacteristicFromCPP(const api::ble_v2::GattCharacteristic &c); +GNCBLEGATTCharacteristic *ObjCGATTCharacteristicFromCPP(const api::ble::GattCharacteristic &c); NSMutableDictionary *ObjCServiceDataFromCPP( const absl::flat_hash_map &sd); @@ -62,24 +62,24 @@ NSMutableDictionary *ObjCServiceDataFromCPP( /** Converts a 16 or 128 bit CoreBluetooth UUID to a C++ UUID. */ Uuid CPPUUIDFromObjC(CBUUID *uuid); -/** - * Converts CoreBluetooth permissions to a C++ permission enum. - * +/** + * Converts CoreBluetooth permissions to a C++ permission enum. + * * This conversion keeps multiple permission values intact. */ -api::ble_v2::GattCharacteristic::Permission CPPGATTCharacteristicPermissionFromObjC( +api::ble::GattCharacteristic::Permission CPPGATTCharacteristicPermissionFromObjC( CBAttributePermissions p); -/** - * Converts CoreBluetooth properties to a C++ property enum. - * +/** + * Converts CoreBluetooth properties to a C++ property enum. + * * This conversion keeps multiple property values intact. */ -api::ble_v2::GattCharacteristic::Property CPPGATTCharacteristicPropertyFromObjC( +api::ble::GattCharacteristic::Property CPPGATTCharacteristicPropertyFromObjC( CBCharacteristicProperties p); /** Converts an Objective-C characteristic to a C++ characteristic. */ -api::ble_v2::GattCharacteristic CPPGATTCharacteristicFromObjC(GNCBLEGATTCharacteristic *c); +api::ble::GattCharacteristic CPPGATTCharacteristicFromObjC(GNCBLEGATTCharacteristic *c); } // namespace apple } // namespace nearby diff --git a/internal/platform/implementation/apple/ble_utils.mm b/internal/platform/implementation/apple/ble_utils.mm index 45e2ebe2..901bfa80 100644 --- a/internal/platform/implementation/apple/ble_utils.mm +++ b/internal/platform/implementation/apple/ble_utils.mm @@ -25,14 +25,14 @@ namespace nearby { namespace apple { -using GattCharacteristic = api::ble_v2::GattCharacteristic; -using Permission = api::ble_v2::GattCharacteristic::Permission; -using Property = api::ble_v2::GattCharacteristic::Property; -using WriteType = api::ble_v2::GattClient::WriteType; +using GattCharacteristic = api::ble::GattCharacteristic; +using Permission = api::ble::GattCharacteristic::Permission; +using Property = api::ble::GattCharacteristic::Property; +using WriteType = api::ble::GattClient::WriteType; /** * Checks if enum `a` contains enum `b` - * + * * For example, `Contains(Permission::kRead | Permission::kWrite, Permission::kRead)` should return * `YES`. */ diff --git a/internal/platform/implementation/apple/platform.mm b/internal/platform/implementation/apple/platform.mm index ee044627..8be11d6f 100644 --- a/internal/platform/implementation/apple/platform.mm +++ b/internal/platform/implementation/apple/platform.mm @@ -16,19 +16,19 @@ #import -#include #include +#include #include "absl/strings/string_view.h" #include "internal/base/files.h" -#include "internal/platform/implementation/apple/awdl.h" +#import "internal/platform/implementation/apple/Log/GNCLogger.h" #include "internal/platform/implementation/apple/atomic_boolean.h" #include "internal/platform/implementation/apple/atomic_uint32.h" +#include "internal/platform/implementation/apple/awdl.h" #include "internal/platform/implementation/apple/ble.h" #include "internal/platform/implementation/apple/condition_variable.h" #include "internal/platform/implementation/apple/count_down_latch.h" #include "internal/platform/implementation/apple/device_info.h" -#import "internal/platform/implementation/apple/Log/GNCLogger.h" #import "internal/platform/implementation/apple/multi_thread_executor.h" #include "internal/platform/implementation/apple/mutex.h" #include "internal/platform/implementation/apple/preferences_manager.h" @@ -172,7 +172,7 @@ std::unique_ptr ImplementationPlatform::CreateBluetoothC return nullptr; } -std::unique_ptr ImplementationPlatform::CreateBleV2Medium( +std::unique_ptr ImplementationPlatform::CreateBleMedium( api::BluetoothAdapter& adapter) { return std::make_unique(); } @@ -256,7 +256,7 @@ absl::StatusOr ImplementationPlatform::SendRequest(const WebRequest } if (blockData != nil) { // Body is not a UTF-8 encoded string and is just using `std::string` as a container for data. - webResponse.body = std::string((char *)blockData.bytes, blockData.length); + webResponse.body = std::string((char*)blockData.bytes, blockData.length); } return webResponse; } diff --git a/internal/platform/implementation/ble_v2.h b/internal/platform/implementation/ble.h similarity index 99% rename from internal/platform/implementation/ble_v2.h rename to internal/platform/implementation/ble.h index e3ea58ba..00866e1e 100644 --- a/internal/platform/implementation/ble_v2.h +++ b/internal/platform/implementation/ble.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef PLATFORM_API_BLE_V2_H_ -#define PLATFORM_API_BLE_V2_H_ +#ifndef PLATFORM_API_BLE_H_ +#define PLATFORM_API_BLE_H_ #include #include @@ -40,7 +40,7 @@ namespace nearby { namespace api { -namespace ble_v2 { +namespace ble { // Coarse representation of power settings throughout all BLE operations. enum class TxPowerLevel { @@ -304,7 +304,7 @@ class GattServer { // Stops a GATT server. virtual void Stop() = 0; }; -// LINT.ThenChange(//depot/google3/third_party/nearby/internal/platform/ble_v2.h) +// LINT.ThenChange(//depot/google3/third_party/nearby/internal/platform/ble.h) // Callback for asynchronous events on the client side of a GATT connection. struct ClientGattConnectionCallback { @@ -641,8 +641,8 @@ class BleMedium { } }; -} // namespace ble_v2 +} // namespace ble } // namespace api } // namespace nearby -#endif // PLATFORM_API_BLE_V2_H_ +#endif // PLATFORM_API_BLE_H_ diff --git a/internal/platform/implementation/g3/BUILD b/internal/platform/implementation/g3/BUILD index 6ff8fe05..c3247e9c 100644 --- a/internal/platform/implementation/g3/BUILD +++ b/internal/platform/implementation/g3/BUILD @@ -80,7 +80,7 @@ cc_library( testonly = True, srcs = [ "awdl.cc", - "ble_v2.cc", + "ble.cc", "bluetooth_adapter.cc", "bluetooth_classic.cc", "credential_storage_impl.cc", @@ -90,7 +90,7 @@ cc_library( ], hdrs = [ "awdl.h", - "ble_v2.h", + "ble.h", "bluetooth_adapter.h", "bluetooth_classic.h", "credential_storage_impl.h", @@ -133,7 +133,7 @@ cc_test( name = "comm_test", srcs = [ "awdl_test.cc", - "ble_v2_test.cc", + "ble_test.cc", ], deps = [ ":comm", diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble.cc similarity index 71% rename from internal/platform/implementation/g3/ble_v2.cc rename to internal/platform/implementation/g3/ble.cc index 46453588..6ce570ea 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/platform/implementation/g3/ble_v2.h" +#include "internal/platform/implementation/g3/ble.h" #include #include @@ -34,7 +34,7 @@ #include "internal/platform/cancellation_flag_listener.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/g3/bluetooth_adapter.h" #include "internal/platform/logging.h" @@ -47,8 +47,8 @@ namespace g3 { namespace { -using ::nearby::api::ble_v2::BleAdvertisementData; -using ::nearby::api::ble_v2::TxPowerLevel; +using ::nearby::api::ble::BleAdvertisementData; +using ::nearby::api::ble::TxPowerLevel; std::string TxPowerLevelToName(TxPowerLevel power_mode) { switch (power_mode) { @@ -67,21 +67,21 @@ std::string TxPowerLevelToName(TxPowerLevel power_mode) { } // namespace -api::ble_v2::BlePeripheral::UniqueId BleV2Socket::GetRemotePeripheralId() { - BleV2Socket* remote_socket = GetRemoteSocket(); +api::ble::BlePeripheral::UniqueId BleSocket::GetRemotePeripheralId() { + BleSocket* remote_socket = GetRemoteSocket(); if (remote_socket == nullptr || remote_socket->adapter_ == nullptr || - remote_socket->adapter_->GetBleV2Medium() == nullptr) { + remote_socket->adapter_->GetBleMedium() == nullptr) { return 0LL; } - BleV2Medium* medium = - dynamic_cast(remote_socket->adapter_->GetBleV2Medium()); + BleMedium* medium = + dynamic_cast(remote_socket->adapter_->GetBleMedium()); if (medium == nullptr) { return 0LL; } return medium->GetAdapter().GetUniqueId(); } -std::unique_ptr BleV2ServerSocket::Accept() { +std::unique_ptr BleServerSocket::Accept() { absl::MutexLock lock(mutex_); while (!closed_ && pending_sockets_.empty()) { cond_.Wait(&mutex_); @@ -92,14 +92,14 @@ std::unique_ptr BleV2ServerSocket::Accept() { pending_sockets_.extract(pending_sockets_.begin()).value(); CHECK(remote_socket); - auto local_socket = std::make_unique(adapter_); + auto local_socket = std::make_unique(adapter_); local_socket->Connect(*remote_socket); remote_socket->Connect(*local_socket); cond_.SignalAll(); return local_socket; } -bool BleV2ServerSocket::Connect(BleV2Socket& socket) { +bool BleServerSocket::Connect(BleSocket& socket) { absl::MutexLock lock(mutex_); if (closed_) return false; if (socket.IsConnected()) { @@ -116,22 +116,22 @@ bool BleV2ServerSocket::Connect(BleV2Socket& socket) { return true; } -void BleV2ServerSocket::SetCloseNotifier(absl::AnyInvocable notifier) { +void BleServerSocket::SetCloseNotifier(absl::AnyInvocable notifier) { absl::MutexLock lock(mutex_); close_notifier_ = std::move(notifier); } -BleV2ServerSocket::~BleV2ServerSocket() { +BleServerSocket::~BleServerSocket() { absl::MutexLock lock(mutex_); DoClose(); } -Exception BleV2ServerSocket::Close() { +Exception BleServerSocket::Close() { absl::MutexLock lock(mutex_); return DoClose(); } -Exception BleV2ServerSocket::DoClose() { +Exception BleServerSocket::DoClose() { bool should_notify = !closed_; closed_ = true; if (should_notify) { @@ -148,24 +148,24 @@ Exception BleV2ServerSocket::DoClose() { return {Exception::kSuccess}; } -BleV2Medium::BleV2Medium(BluetoothAdapter& adapter) : adapter_(adapter) { +BleMedium::BleMedium(BluetoothAdapter& adapter) : adapter_(adapter) { CHECK(&adapter_); - adapter_.SetBleV2Medium(this); + adapter_.SetBleMedium(this); is_extended_advertisements_available_ = MediumEnvironment::Instance().IsBleExtendedAdvertisementsAvailable(); - MediumEnvironment::Instance().RegisterBleV2Medium(*this, - adapter_.GetUniqueId()); + MediumEnvironment::Instance().RegisterBleMedium(*this, + adapter_.GetUniqueId()); } -BleV2Medium::~BleV2Medium() { - adapter_.SetBleV2Medium(nullptr); - MediumEnvironment::Instance().UnregisterBleV2Medium(*this); +BleMedium::~BleMedium() { + adapter_.SetBleMedium(nullptr); + MediumEnvironment::Instance().UnregisterBleMedium(*this); } -bool BleV2Medium::StartAdvertising( +bool BleMedium::StartAdvertising( const BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_parameters) { + api::ble::AdvertiseParameters advertise_parameters) { LOG(INFO) << "G3 Ble StartAdvertising: advertising_data.is_extended_advertisement=" << advertising_data.is_extended_advertisement @@ -181,26 +181,26 @@ bool BleV2Medium::StartAdvertising( } absl::MutexLock lock(mutex_); - MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising( + MediumEnvironment::Instance().UpdateBleMediumForAdvertising( /*enabled=*/true, *this, adapter_.GetUniqueId(), advertising_data); return true; } -bool BleV2Medium::StopAdvertising() { +bool BleMedium::StopAdvertising() { LOG(INFO) << "G3 Ble StopAdvertising"; absl::MutexLock lock(mutex_); BleAdvertisementData empty_advertisement_data = {}; - MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising( + MediumEnvironment::Instance().UpdateBleMediumForAdvertising( /*enabled=*/false, *this, adapter_.GetUniqueId(), empty_advertisement_data); return true; } -std::unique_ptr BleV2Medium::StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_parameters, - BleV2Medium::AdvertisingCallback callback) { +std::unique_ptr BleMedium::StartAdvertising( + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertise_parameters, + BleMedium::AdvertisingCallback callback) { LOG(INFO) << "G3 Ble StartAdvertising: advertising_data.is_extended_advertisement=" << advertising_data.is_extended_advertisement @@ -219,7 +219,7 @@ std::unique_ptr BleV2Medium::StartAdvertising( callback.start_advertising_result(absl::OkStatus()); } absl::MutexLock lock(mutex_); - MediumEnvironment::Instance().UpdateBleV2MediumForAdvertising( + MediumEnvironment::Instance().UpdateBleMediumForAdvertising( /*enabled=*/true, *this, adapter_.GetUniqueId(), advertising_data); return std::make_unique( AdvertisingSession{.stop_advertising = [this] { @@ -229,13 +229,13 @@ std::unique_ptr BleV2Medium::StartAdvertising( }}); } -bool BleV2Medium::StartScanning(const Uuid& service_uuid, - TxPowerLevel tx_power_level, - ScanCallback callback) { +bool BleMedium::StartScanning(const Uuid& service_uuid, + TxPowerLevel tx_power_level, + ScanCallback callback) { LOG(INFO) << "G3 Ble StartScanning"; auto internal_session_id = Prng().NextUint32(); absl::MutexLock lock(mutex_); - MediumEnvironment::Instance().UpdateBleV2MediumForScanning( + MediumEnvironment::Instance().UpdateBleMediumForScanning( /*enabled=*/true, service_uuid, internal_session_id, {.advertisement_found_cb = std::move(callback.advertisement_found_cb)}, *this); @@ -243,9 +243,9 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid, return true; } -bool BleV2Medium::StartMultipleServicesScanning( +bool BleMedium::StartMultipleServicesScanning( const std::vector& service_uuids, - api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) { + api::ble::TxPowerLevel tx_power_level, ScanCallback callback) { LOG(INFO) << "G3 Ble StartMultipleServicesScanning"; absl::MutexLock lock(mutex_); @@ -254,13 +254,13 @@ bool BleV2Medium::StartMultipleServicesScanning( auto internal_session_id = Prng().NextUint32(); ScanCallback multiple_scan_callback = { .advertisement_found_cb = - [this](api::ble_v2::BlePeripheral::UniqueId peripheral_id, + [this](api::ble::BlePeripheral::UniqueId peripheral_id, BleAdvertisementData advertisement_data) { scan_callback_.advertisement_found_cb(peripheral_id, advertisement_data); }}; - MediumEnvironment::Instance().UpdateBleV2MediumForScanning( + MediumEnvironment::Instance().UpdateBleMediumForScanning( /*enabled=*/true, service_uuid, internal_session_id, {.advertisement_found_cb = std::move(multiple_scan_callback.advertisement_found_cb)}, @@ -271,11 +271,11 @@ bool BleV2Medium::StartMultipleServicesScanning( return true; } -bool BleV2Medium::StopScanning() { +bool BleMedium::StopScanning() { LOG(INFO) << "G3 Ble StopScanning"; absl::MutexLock lock(mutex_); for (auto element : scanning_internal_session_ids_) { - MediumEnvironment::Instance().UpdateBleV2MediumForScanning( + MediumEnvironment::Instance().UpdateBleMediumForScanning( /*enabled=*/false, /*service_uuid=*/element.first, /*internal_session_id*/ element.second, @@ -284,16 +284,16 @@ bool BleV2Medium::StopScanning() { return true; } -std::unique_ptr BleV2Medium::StartScanning( +std::unique_ptr BleMedium::StartScanning( const Uuid& service_uuid, TxPowerLevel tx_power_level, - BleV2Medium::ScanningCallback callback) { + BleMedium::ScanningCallback callback) { LOG(INFO) << "G3 Ble StartScanning"; auto internal_session_id = Prng().NextUint32(); { absl::MutexLock lock(mutex_); - MediumEnvironment::Instance().UpdateBleV2MediumForScanning( + MediumEnvironment::Instance().UpdateBleMediumForScanning( /*enabled=*/true, service_uuid, internal_session_id, std::move(callback), *this); scanning_internal_session_ids_.insert({service_uuid, internal_session_id}); @@ -310,7 +310,7 @@ std::unique_ptr BleV2Medium::StartScanning( return absl::NotFoundError( "can't find the provided internal session"); } - MediumEnvironment::Instance().UpdateBleV2MediumForScanning( + MediumEnvironment::Instance().UpdateBleMediumForScanning( /*enabled=*/false, service_uuid, internal_session_id, /*callback=*/{}, *this); scanning_internal_session_ids_.erase( @@ -320,25 +320,25 @@ std::unique_ptr BleV2Medium::StartScanning( }); } -std::unique_ptr BleV2Medium::StartGattServer( - api::ble_v2::ServerGattConnectionCallback callback) { +std::unique_ptr BleMedium::StartGattServer( + api::ble::ServerGattConnectionCallback callback) { return std::make_unique(*this, std::move(callback)); } -bool BleV2Medium::IsStopped(Borrowable server) { +bool BleMedium::IsStopped(Borrowable server) { auto borrowed = server.Borrow(); if (!borrowed) { return true; } - BleV2Medium::GattServer* gatt_server = static_cast(*borrowed); + BleMedium::GattServer* gatt_server = static_cast(*borrowed); return gatt_server->IsStopped(); } -std::unique_ptr BleV2Medium::ConnectToGattServer( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, +std::unique_ptr BleMedium::ConnectToGattServer( + api::ble::BlePeripheral::UniqueId peripheral_id, TxPowerLevel tx_power_level, - api::ble_v2::ClientGattConnectionCallback callback) { - Borrowable server = + api::ble::ClientGattConnectionCallback callback) { + Borrowable server = MediumEnvironment::Instance().GetGattServer(peripheral_id); if (IsStopped(server)) { LOG(WARNING) << "No GATT server found for " << peripheral_id; @@ -348,36 +348,36 @@ std::unique_ptr BleV2Medium::ConnectToGattServer( std::move(callback)); } -bool BleV2Medium::IsExtendedAdvertisementsAvailable() { +bool BleMedium::IsExtendedAdvertisementsAvailable() { return is_extended_advertisements_available_; } -BleV2Medium::GattServer::GattServer( - BleV2Medium& medium, api::ble_v2::ServerGattConnectionCallback callback) +BleMedium::GattServer::GattServer( + BleMedium& medium, api::ble::ServerGattConnectionCallback callback) : medium_(medium), callback_(std::move(callback)) { BluetoothAdapter& adapter = medium.GetAdapter(); MediumEnvironment::Instance().RegisterGattServer( medium_, adapter.GetUniqueId(), lender_.GetBorrowable()); } -BleV2Medium::GattServer::~GattServer() { +BleMedium::GattServer::~GattServer() { Stop(); lender_.Release(); MediumEnvironment::Instance().UnregisterGattServer(medium_); } -std::optional -BleV2Medium::GattServer::CreateCharacteristic( +std::optional +BleMedium::GattServer::CreateCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid, - api::ble_v2::GattCharacteristic::Permission permission, - api::ble_v2::GattCharacteristic::Property property) { + api::ble::GattCharacteristic::Permission permission, + api::ble::GattCharacteristic::Property property) { absl::MutexLock lock(mutex_); - api::ble_v2::GattCharacteristic characteristic = { - .uuid = characteristic_uuid, .service_uuid = service_uuid}; + api::ble::GattCharacteristic characteristic = {.uuid = characteristic_uuid, + .service_uuid = service_uuid}; characteristics_[characteristic] = absl::NotFoundError("value not set"); return characteristic; } -bool BleV2Medium::GattServer::DiscoverBleV2MediumGattCharacteristics( +bool BleMedium::GattServer::DiscoverBleMediumGattCharacteristics( const Uuid& service_uuid, const std::vector& characteristic_uuids) { absl::MutexLock lock(mutex_); auto contains = [&](const Uuid& characteristic_uuid) { @@ -385,7 +385,7 @@ bool BleV2Medium::GattServer::DiscoverBleV2MediumGattCharacteristics( characteristic_uuid) != characteristic_uuids.end(); }; for (const auto& it : characteristics_) { - const api::ble_v2::GattCharacteristic& characteristic = it.first; + const api::ble::GattCharacteristic& characteristic = it.first; if (characteristic.service_uuid == service_uuid && contains(characteristic.uuid)) { return true; @@ -394,8 +394,8 @@ bool BleV2Medium::GattServer::DiscoverBleV2MediumGattCharacteristics( return false; } -bool BleV2Medium::GattServer::UpdateCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, +bool BleMedium::GattServer::UpdateCharacteristic( + const api::ble::GattCharacteristic& characteristic, const nearby::ByteArray& value) { absl::MutexLock lock(mutex_); LOG(INFO) << "G3 Ble GattServer UpdateCharacteristic, characteristic=(" @@ -406,8 +406,8 @@ bool BleV2Medium::GattServer::UpdateCharacteristic( return true; } -absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged( - const api::ble_v2::GattCharacteristic& characteristic, bool confirm, +absl::Status BleMedium::GattServer::NotifyCharacteristicChanged( + const api::ble::GattCharacteristic& characteristic, bool confirm, const ByteArray& new_value) { absl::MutexLock lock(mutex_); LOG(INFO) << "G3 Ble GattServer NotifyCharacteristicChanged, characteristic=(" @@ -426,9 +426,9 @@ absl::Status BleV2Medium::GattServer::NotifyCharacteristicChanged( return status; } -absl::StatusOr BleV2Medium::GattServer::ReadCharacteristic( - const api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic, int offset) { +absl::StatusOr BleMedium::GattServer::ReadCharacteristic( + const api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic, int offset) { { absl::MutexLock lock(mutex_); const auto it = characteristics_.find(characteristic); @@ -455,9 +455,9 @@ absl::StatusOr BleV2Medium::GattServer::ReadCharacteristic( return result; } -absl::Status BleV2Medium::GattServer::WriteCharacteristic( - const api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic, int offset, +absl::Status BleMedium::GattServer::WriteCharacteristic( + const api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic, int offset, absl::string_view data) { if (HasCharacteristic(characteristic)) { absl::Status result; @@ -475,9 +475,9 @@ absl::Status BleV2Medium::GattServer::WriteCharacteristic( absl::StrCat(characteristic, " not found")); } -bool BleV2Medium::GattServer::AddCharacteristicSubscription( - const api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic, +bool BleMedium::GattServer::AddCharacteristicSubscription( + const api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic, absl::AnyInvocable callback) { absl::MutexLock lock(mutex_); const auto it = characteristics_.find(characteristic); @@ -489,9 +489,9 @@ bool BleV2Medium::GattServer::AddCharacteristicSubscription( return false; } -bool BleV2Medium::GattServer::RemoveCharacteristicSubscription( - const api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic) { +bool BleMedium::GattServer::RemoveCharacteristicSubscription( + const api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic) { absl::MutexLock lock(mutex_); const auto it = characteristics_.find(characteristic); if (it != characteristics_.end()) { @@ -501,25 +501,25 @@ bool BleV2Medium::GattServer::RemoveCharacteristicSubscription( return false; } -bool BleV2Medium::GattServer::HasCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic) { +bool BleMedium::GattServer::HasCharacteristic( + const api::ble::GattCharacteristic& characteristic) { absl::MutexLock lock(mutex_); return characteristics_.find(characteristic) != characteristics_.end(); } -void BleV2Medium::GattServer::Connect(GattClient* client) { +void BleMedium::GattServer::Connect(GattClient* client) { absl::MutexLock lock(mutex_); connected_clients_.push_back(client); } -void BleV2Medium::GattServer::Disconnect(GattClient* client) { +void BleMedium::GattServer::Disconnect(GattClient* client) { absl::MutexLock lock(mutex_); connected_clients_.erase( std::remove(connected_clients_.begin(), connected_clients_.end(), client), connected_clients_.end()); } -void BleV2Medium::GattServer::Stop() { +void BleMedium::GattServer::Stop() { if (stopped_) return; absl::MutexLock lock(mutex_); stopped_ = true; @@ -529,31 +529,31 @@ void BleV2Medium::GattServer::Stop() { characteristics_.clear(); } -BleV2Medium::GattClient::GattClient( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - Borrowable gatt_server, - api::ble_v2::ClientGattConnectionCallback callback) +BleMedium::GattClient::GattClient( + api::ble::BlePeripheral::UniqueId peripheral_id, + Borrowable gatt_server, + api::ble::ClientGattConnectionCallback callback) : peripheral_id_(peripheral_id), gatt_server_(gatt_server), callback_(std::move(callback)) { - Borrowed borrowed = gatt_server_.Borrow(); + Borrowed borrowed = gatt_server_.Borrow(); if (borrowed) { - BleV2Medium::GattServer* gatt_server = - static_cast(*borrowed); + BleMedium::GattServer* gatt_server = + static_cast(*borrowed); gatt_server->Connect(this); } } -BleV2Medium::GattClient::~GattClient() { - Borrowed borrowed = gatt_server_.Borrow(); +BleMedium::GattClient::~GattClient() { + Borrowed borrowed = gatt_server_.Borrow(); if (borrowed) { - BleV2Medium::GattServer* gatt_server = - static_cast(*borrowed); + BleMedium::GattServer* gatt_server = + static_cast(*borrowed); gatt_server->Disconnect(this); } } -bool BleV2Medium::GattClient::DiscoverServiceAndCharacteristics( +bool BleMedium::GattClient::DiscoverServiceAndCharacteristics( const Uuid& service_uuid, const std::vector& characteristic_uuids) { LOG(INFO) << "G3 Ble GattClient DiscoverServiceAndCharacteristics, service_uuid=" @@ -562,19 +562,19 @@ bool BleV2Medium::GattClient::DiscoverServiceAndCharacteristics( if (!is_connection_alive_) { return false; } - Borrowed borrowed = gatt_server_.Borrow(); + Borrowed borrowed = gatt_server_.Borrow(); if (!borrowed) { return false; } - BleV2Medium::GattServer* gatt_server = - static_cast(*borrowed); - return gatt_server->DiscoverBleV2MediumGattCharacteristics( + BleMedium::GattServer* gatt_server = + static_cast(*borrowed); + return gatt_server->DiscoverBleMediumGattCharacteristics( service_uuid, characteristic_uuids); } -std::optional -BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid, - const Uuid& characteristic_uuid) { +std::optional +BleMedium::GattClient::GetCharacteristic(const Uuid& service_uuid, + const Uuid& characteristic_uuid) { absl::MutexLock lock(mutex_); LOG(INFO) << "G3 Ble GattClient GetCharacteristic, service_uuid=" << service_uuid.Get16BitAsString() @@ -583,16 +583,16 @@ BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid, return std::nullopt; } // clang-format off - api::ble_v2::GattCharacteristic characteristic = { + api::ble::GattCharacteristic characteristic = { .uuid = characteristic_uuid, .service_uuid = service_uuid}; // clang-format on - Borrowed borrowed = gatt_server_.Borrow(); + Borrowed borrowed = gatt_server_.Borrow(); if (!borrowed) { return std::nullopt; } - BleV2Medium::GattServer* gatt_server = - static_cast(*borrowed); + BleMedium::GattServer* gatt_server = + static_cast(*borrowed); if (!gatt_server->HasCharacteristic(characteristic)) { LOG(WARNING) << "G3 Ble GattClient GetCharacteristic, characteristic=(" << characteristic.service_uuid.Get16BitAsString() << "," @@ -607,18 +607,18 @@ BleV2Medium::GattClient::GetCharacteristic(const Uuid& service_uuid, return characteristic; } -std::optional BleV2Medium::GattClient::ReadCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic) { +std::optional BleMedium::GattClient::ReadCharacteristic( + const api::ble::GattCharacteristic& characteristic) { absl::MutexLock lock(mutex_); if (!is_connection_alive_) { return std::nullopt; } - Borrowed borrowed = gatt_server_.Borrow(); + Borrowed borrowed = gatt_server_.Borrow(); if (!borrowed) { return std::nullopt; } - BleV2Medium::GattServer* gatt_server = - static_cast(*borrowed); + BleMedium::GattServer* gatt_server = + static_cast(*borrowed); absl::StatusOr value = gatt_server->ReadCharacteristic(peripheral_id_, characteristic, /*offset=*/0); @@ -635,19 +635,19 @@ std::optional BleV2Medium::GattClient::ReadCharacteristic( return value->string_data(); } -bool BleV2Medium::GattClient::WriteCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, - absl::string_view value, api::ble_v2::GattClient::WriteType write_type) { +bool BleMedium::GattClient::WriteCharacteristic( + const api::ble::GattCharacteristic& characteristic, absl::string_view value, + api::ble::GattClient::WriteType write_type) { absl::MutexLock lock(mutex_); if (!is_connection_alive_) { return false; } - Borrowed borrowed = gatt_server_.Borrow(); + Borrowed borrowed = gatt_server_.Borrow(); if (!borrowed) { return false; } - BleV2Medium::GattServer* gatt_server = - static_cast(*borrowed); + BleMedium::GattServer* gatt_server = + static_cast(*borrowed); LOG(INFO) << "G3 Ble WriteCharacteristic, characteristic=(" << characteristic.service_uuid.Get16BitAsString() << "," << std::string(characteristic.uuid) @@ -661,20 +661,20 @@ bool BleV2Medium::GattClient::WriteCharacteristic( return status.ok(); } -bool BleV2Medium::GattClient::SetCharacteristicSubscription( - const api::ble_v2::GattCharacteristic& characteristic, bool enable, +bool BleMedium::GattClient::SetCharacteristicSubscription( + const api::ble::GattCharacteristic& characteristic, bool enable, absl::AnyInvocable on_characteristic_changed_cb) { absl::MutexLock lock(mutex_); if (!is_connection_alive_) { return false; } - Borrowed borrowed = gatt_server_.Borrow(); + Borrowed borrowed = gatt_server_.Borrow(); if (!borrowed) { return false; } - BleV2Medium::GattServer* gatt_server = - static_cast(*borrowed); + BleMedium::GattServer* gatt_server = + static_cast(*borrowed); LOG(INFO) << "G3 Ble SetCharacteristicSubscription, characteristic=(" << characteristic.service_uuid.Get16BitAsString() << "," << std::string(characteristic.uuid) << "), enable = " << enable; @@ -688,19 +688,19 @@ bool BleV2Medium::GattClient::SetCharacteristicSubscription( } } -void BleV2Medium::GattClient::Disconnect() { +void BleMedium::GattClient::Disconnect() { bool was_alive = is_connection_alive_.exchange(false); if (!was_alive) return; LOG(INFO) << "G3 Ble GattClient Disconnect"; - Borrowed borrowed = gatt_server_.Borrow(); + Borrowed borrowed = gatt_server_.Borrow(); if (borrowed) { - BleV2Medium::GattServer* gatt_server = - static_cast(*borrowed); + BleMedium::GattServer* gatt_server = + static_cast(*borrowed); gatt_server->Disconnect(this); } } -void BleV2Medium::GattClient::OnServerDisconnected() { +void BleMedium::GattClient::OnServerDisconnected() { bool was_alive = is_connection_alive_.exchange(false); if (!was_alive) return; LOG(INFO) << "G3 Ble GattServer disconnected"; @@ -709,9 +709,9 @@ void BleV2Medium::GattClient::OnServerDisconnected() { } } -std::unique_ptr BleV2Medium::OpenServerSocket( +std::unique_ptr BleMedium::OpenServerSocket( const std::string& service_id) { - auto server_socket = std::make_unique(&GetAdapter()); + auto server_socket = std::make_unique(&GetAdapter()); server_socket->SetCloseNotifier([this, service_id]() { absl::MutexLock lock(mutex_); server_sockets_.erase(service_id); @@ -723,29 +723,29 @@ std::unique_ptr BleV2Medium::OpenServerSocket( return server_socket; } -std::unique_ptr -BleV2Medium::OpenL2capServerSocket(const std::string& service_id) { +std::unique_ptr +BleMedium::OpenL2capServerSocket(const std::string& service_id) { // TODO(mingshiouwu): add more codes for g3 testing. return nullptr; } -std::unique_ptr BleV2Medium::Connect( +std::unique_ptr BleMedium::Connect( const std::string& service_id, TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id, + api::ble::BlePeripheral::UniqueId remote_peripheral_id, CancellationFlag* cancellation_flag) { LOG(INFO) << "G3 Ble Connect [self]: medium=" << this << ", adapter=" << &GetAdapter() << ", peripheral id=" << adapter_.GetUniqueId() << ", service_id=" << service_id; // First, find an instance of remote medium, that exposed this peripheral. - BleV2Medium* remote_medium = dynamic_cast( - MediumEnvironment::Instance().FindBleV2Medium(remote_peripheral_id)); + BleMedium* remote_medium = dynamic_cast( + MediumEnvironment::Instance().FindBleMedium(remote_peripheral_id)); if (remote_medium == nullptr) { LOG(INFO) << "Peripheral not found, id= " << remote_peripheral_id; return nullptr; } - BleV2ServerSocket* remote_server_socket = nullptr; + BleServerSocket* remote_server_socket = nullptr; LOG(INFO) << "G3 Ble Connect [peer]: medium=" << remote_medium << ", peripheral=" << &remote_peripheral_id << ", service_id=" << service_id; @@ -777,7 +777,7 @@ std::unique_ptr BleV2Medium::Connect( } }); - auto socket = std::make_unique(&GetAdapter()); + auto socket = std::make_unique(&GetAdapter()); // Finally, Request to connect to this socket. if (!remote_server_socket->Connect(*socket)) { LOG(ERROR) << "G3 Ble Failed to connect to existing Ble " diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble.h similarity index 61% rename from internal/platform/implementation/g3/ble_v2.h rename to internal/platform/implementation/g3/ble.h index 879aff4a..d54dc974 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef PLATFORM_IMPL_G3_BLE_V2_H_ -#define PLATFORM_IMPL_G3_BLE_V2_H_ +#ifndef PLATFORM_IMPL_G3_BLE_H_ +#define PLATFORM_IMPL_G3_BLE_H_ #include #include @@ -35,7 +35,7 @@ #include "internal/platform/byte_array.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/g3/bluetooth_adapter.h" #include "internal/platform/implementation/g3/socket_base.h" @@ -46,9 +46,9 @@ namespace nearby { namespace g3 { -class BleV2Socket : public api::ble_v2::BleSocket, public SocketBase { +class BleSocket : public api::ble::BleSocket, public SocketBase { public: - explicit BleV2Socket(BluetoothAdapter* adapter) : adapter_(adapter) {} + explicit BleSocket(BluetoothAdapter* adapter) : adapter_(adapter) {} // Returns the InputStream of this connected BleSocket. InputStream& GetInputStream() override { @@ -62,28 +62,28 @@ class BleV2Socket : public api::ble_v2::BleSocket, public SocketBase { } // Returns address of a remote BleSocket or nullptr. - BleV2Socket* GetRemoteSocket() { - return static_cast(SocketBase::GetRemoteSocket()); + BleSocket* GetRemoteSocket() { + return static_cast(SocketBase::GetRemoteSocket()); } // Returns Exception::kIo on error, Exception::kSuccess otherwise. Exception Close() override { return SocketBase::Close(); } - api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override + api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override ABSL_LOCKS_EXCLUDED(mutex_); private: BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only. }; -class BleV2ServerSocket : public api::ble_v2::BleServerSocket { +class BleServerSocket : public api::ble::BleServerSocket { public: - explicit BleV2ServerSocket(BluetoothAdapter* adapter) : adapter_(adapter) {} - BleV2ServerSocket(const BleV2ServerSocket&) = default; - BleV2ServerSocket& operator=(const BleV2ServerSocket&) = default; - BleV2ServerSocket(BleV2ServerSocket&&) = default; - BleV2ServerSocket& operator=(BleV2ServerSocket&&) = default; - ~BleV2ServerSocket() override; + explicit BleServerSocket(BluetoothAdapter* adapter) : adapter_(adapter) {} + BleServerSocket(const BleServerSocket&) = default; + BleServerSocket& operator=(const BleServerSocket&) = default; + BleServerSocket(BleServerSocket&&) = default; + BleServerSocket& operator=(BleServerSocket&&) = default; + ~BleServerSocket() override; // Blocks until either: // - at least one incoming connection request is available, or @@ -95,7 +95,7 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket { // Called by the server side of a connection. // Returns BleSocket to the server side. // If not null, returned socket is connected to its remote (client-side) peer. - std::unique_ptr Accept() override + std::unique_ptr Accept() override ABSL_LOCKS_EXCLUDED(mutex_); // Blocks until either: @@ -105,7 +105,7 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket { // // Called by the client side of a connection. // Returns true, if socket is successfully connected. - bool Connect(BleV2Socket& socket) ABSL_LOCKS_EXCLUDED(mutex_); + bool Connect(BleSocket& socket) ABSL_LOCKS_EXCLUDED(mutex_); // Called by the server side of a connection before passing ownership of // BleServerSocker to user, to track validity of a pointer to this @@ -123,67 +123,66 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket { mutable absl::Mutex mutex_; absl::CondVar cond_; BluetoothAdapter* adapter_ = nullptr; // Our Adapter. Read only. - absl::flat_hash_set pending_sockets_ ABSL_GUARDED_BY(mutex_); + absl::flat_hash_set pending_sockets_ ABSL_GUARDED_BY(mutex_); absl::AnyInvocable close_notifier_ ABSL_GUARDED_BY(mutex_); bool closed_ ABSL_GUARDED_BY(mutex_) = false; }; // Container of operations that can be performed over the BLE medium. -class BleV2Medium : public api::ble_v2::BleMedium { +class BleMedium : public api::ble::BleMedium { public: - explicit BleV2Medium(BluetoothAdapter& adapter); - ~BleV2Medium() override; + explicit BleMedium(BluetoothAdapter& adapter); + ~BleMedium() override; // Returns true once the Ble advertising has been initiated. - bool StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_parameters) override - ABSL_LOCKS_EXCLUDED(mutex_); + bool StartAdvertising(const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertise_parameters) + override ABSL_LOCKS_EXCLUDED(mutex_); bool StopAdvertising() override ABSL_LOCKS_EXCLUDED(mutex_); std::unique_ptr StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_parameters, + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertise_parameters, AdvertisingCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); bool StartScanning(const Uuid& service_uuid, - api::ble_v2::TxPowerLevel tx_power_level, + api::ble::TxPowerLevel tx_power_level, ScanCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); bool StartMultipleServicesScanning(const std::vector& service_uuids, - api::ble_v2::TxPowerLevel tx_power_level, - ScanCallback callback) - override ABSL_LOCKS_EXCLUDED(mutex_); + api::ble::TxPowerLevel tx_power_level, + ScanCallback callback) override + ABSL_LOCKS_EXCLUDED(mutex_); bool StopScanning() override ABSL_LOCKS_EXCLUDED(mutex_); std::unique_ptr StartScanning( - const Uuid& service_uuid, api::ble_v2::TxPowerLevel tx_power_level, + const Uuid& service_uuid, api::ble::TxPowerLevel tx_power_level, ScanningCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); - std::unique_ptr StartGattServer( - api::ble_v2::ServerGattConnectionCallback callback) override + std::unique_ptr StartGattServer( + api::ble::ServerGattConnectionCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); - std::unique_ptr ConnectToGattServer( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::ClientGattConnectionCallback callback) override + std::unique_ptr ConnectToGattServer( + api::ble::BlePeripheral::UniqueId peripheral_id, + api::ble::TxPowerLevel tx_power_level, + api::ble::ClientGattConnectionCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); // Open server socket to listen for incoming connection. // // On success, returns a new BleServerSocket. // On error, returns nullptr. - std::unique_ptr OpenServerSocket( + std::unique_ptr OpenServerSocket( const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_); - std::unique_ptr OpenL2capServerSocket( + std::unique_ptr OpenL2capServerSocket( const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_); // Connects to existing remote Ble peripheral. // // On success, returns a new BleSocket. // On error, returns nullptr. - std::unique_ptr Connect( - const std::string& service_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id, + std::unique_ptr Connect( + const std::string& service_id, api::ble::TxPowerLevel tx_power_level, + api::ble::BlePeripheral::UniqueId remote_peripheral_id, CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_); bool IsExtendedAdvertisementsAvailable() override; @@ -193,96 +192,94 @@ class BleV2Medium : public api::ble_v2::BleMedium { private: class GattClient; // A concrete implementation for GattServer. - class GattServer : public api::ble_v2::GattServer { + class GattServer : public api::ble::GattServer { public: - GattServer(BleV2Medium& medium, - api::ble_v2::ServerGattConnectionCallback callback); + GattServer(BleMedium& medium, + api::ble::ServerGattConnectionCallback callback); ~GattServer() override; - std::optional CreateCharacteristic( + std::optional CreateCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid, - api::ble_v2::GattCharacteristic::Permission permission, - api::ble_v2::GattCharacteristic::Property property) override; + api::ble::GattCharacteristic::Permission permission, + api::ble::GattCharacteristic::Property property) override; bool UpdateCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, + const api::ble::GattCharacteristic& characteristic, const nearby::ByteArray& value) override; absl::Status NotifyCharacteristicChanged( - const api::ble_v2::GattCharacteristic& characteristic, bool confirm, + const api::ble::GattCharacteristic& characteristic, bool confirm, const ByteArray& new_value) override; void Stop() override; bool IsStopped() { return stopped_; } - bool DiscoverBleV2MediumGattCharacteristics( + bool DiscoverBleMediumGattCharacteristics( const Uuid& service_uuid, const std::vector& characteristic_uuids); absl::StatusOr ReadCharacteristic( - api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic, int offset); + api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic, int offset); absl::Status WriteCharacteristic( - api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic, int offset, + api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic, int offset, absl::string_view data); bool AddCharacteristicSubscription( - api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic, + api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic, absl::AnyInvocable); bool RemoveCharacteristicSubscription( - api::ble_v2::BlePeripheral::UniqueId remote_device_id, - const api::ble_v2::GattCharacteristic& characteristic); + api::ble::BlePeripheral::UniqueId remote_device_id, + const api::ble::GattCharacteristic& characteristic); - bool HasCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic); + bool HasCharacteristic(const api::ble::GattCharacteristic& characteristic); void Connect(GattClient* client); void Disconnect(GattClient* client); private: - using SubscriberKey = std::pair; + using SubscriberKey = std::pair; using SubscriberCallback = absl::AnyInvocable; absl::Mutex mutex_; - BleV2Medium& medium_; - api::ble_v2::ServerGattConnectionCallback callback_; - absl::flat_hash_map> + BleMedium& medium_; + api::ble::ServerGattConnectionCallback callback_; + absl::flat_hash_map> characteristics_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_map subscribers_ ABSL_GUARDED_BY(mutex_); std::vector connected_clients_ ABSL_GUARDED_BY(mutex_); std::atomic_bool stopped_ = false; - Lender lender_{this}; + Lender lender_{this}; }; // A concrete implementation for GattClient. - class GattClient : public api::ble_v2::GattClient { + class GattClient : public api::ble::GattClient { public: - GattClient(api::ble_v2::BlePeripheral::UniqueId peripheral_id, - Borrowable gatt_server, - api::ble_v2::ClientGattConnectionCallback callback); + GattClient(api::ble::BlePeripheral::UniqueId peripheral_id, + Borrowable gatt_server, + api::ble::ClientGattConnectionCallback callback); ~GattClient() override; bool DiscoverServiceAndCharacteristics( const Uuid& service_uuid, const std::vector& characteristic_uuids) override; - std::optional GetCharacteristic( + std::optional GetCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid) override; std::optional ReadCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic) override; + const api::ble::GattCharacteristic& characteristic) override; bool WriteCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, + const api::ble::GattCharacteristic& characteristic, absl::string_view value, - api::ble_v2::GattClient::WriteType write_type) override; + api::ble::GattClient::WriteType write_type) override; bool SetCharacteristicSubscription( - const api::ble_v2::GattCharacteristic& characteristic, bool enable, + const api::ble::GattCharacteristic& characteristic, bool enable, absl::AnyInvocable on_characteristic_changed_cb) override; @@ -297,15 +294,15 @@ class BleV2Medium : public api::ble_v2::BleMedium { // disconnected/*false*/, the instance needs to be created again to bring // it alive. std::atomic_bool is_connection_alive_ = true; - api::ble_v2::BlePeripheral::UniqueId peripheral_id_; - Borrowable gatt_server_; - api::ble_v2::ClientGattConnectionCallback callback_; + api::ble::BlePeripheral::UniqueId peripheral_id_; + Borrowable gatt_server_; + api::ble::ClientGattConnectionCallback callback_; }; - bool IsStopped(Borrowable server); + bool IsStopped(Borrowable server); absl::Mutex mutex_; BluetoothAdapter& adapter_; // Our device adapter; read-only. - absl::flat_hash_map server_sockets_ + absl::flat_hash_map server_sockets_ ABSL_GUARDED_BY(mutex_); absl::flat_hash_set> scanning_internal_session_ids_ ABSL_GUARDED_BY(mutex_); @@ -316,4 +313,4 @@ class BleV2Medium : public api::ble_v2::BleMedium { } // namespace g3 } // namespace nearby -#endif // PLATFORM_IMPL_G3_BLE_V2_H_ +#endif // PLATFORM_IMPL_G3_BLE_H_ diff --git a/internal/platform/implementation/g3/ble_v2_test.cc b/internal/platform/implementation/g3/ble_test.cc similarity index 63% rename from internal/platform/implementation/g3/ble_v2_test.cc rename to internal/platform/implementation/g3/ble_test.cc index 20ab43b8..545567b2 100644 --- a/internal/platform/implementation/g3/ble_v2_test.cc +++ b/internal/platform/implementation/g3/ble_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/platform/implementation/g3/ble_v2.h" +#include "internal/platform/implementation/g3/ble.h" #include @@ -22,7 +22,7 @@ #include "absl/status/status.h" #include "absl/strings/string_view.h" #include "internal/platform/byte_array.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/g3/bluetooth_adapter.h" #include "internal/platform/uuid.h" @@ -53,117 +53,115 @@ class MockBluetoothAdapter : public BluetoothAdapter { MOCK_METHOD(api::BluetoothAdapter::ScanMode, GetScanMode, (), (const, override)); MOCK_METHOD(int, GetNumSlots, (), (const)); - MOCK_METHOD(api::ble_v2::BlePeripheral::UniqueId, GetUniqueId, (), (const)); - MOCK_METHOD(void, SetBleV2Medium, (api::ble_v2::BleMedium * medium)); + MOCK_METHOD(api::ble::BlePeripheral::UniqueId, GetUniqueId, (), (const)); + MOCK_METHOD(void, SetBleMedium, (api::ble::BleMedium * medium)); }; -TEST(BleV2MediumTest, StartAdvertising) { +TEST(BleMediumTest, StartAdvertising) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - api::ble_v2::BleAdvertisementData advertising_data; + BleMedium ble_medium(mock_bluetooth_adapter); + api::ble::BleAdvertisementData advertising_data; advertising_data.is_extended_advertisement = false; advertising_data.service_data.insert( {Uuid(kTestUuid), ByteArray("test_service_data")}); - auto advertising_session = ble_v2_medium.StartAdvertising( + auto advertising_session = ble_medium.StartAdvertising( advertising_data, - {.tx_power_level = api::ble_v2::TxPowerLevel::kLow, - .is_connectable = true}, - api::ble_v2::BleMedium::AdvertisingCallback{ + {.tx_power_level = api::ble::TxPowerLevel::kLow, .is_connectable = true}, + api::ble::BleMedium::AdvertisingCallback{ .start_advertising_result = [](absl::Status) {}, }); EXPECT_TRUE(advertising_session != nullptr); } -TEST(BleV2MediumTest, StopAdvertising) { +TEST(BleMediumTest, StopAdvertising) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - EXPECT_TRUE(ble_v2_medium.StopAdvertising()); + BleMedium ble_medium(mock_bluetooth_adapter); + EXPECT_TRUE(ble_medium.StopAdvertising()); } -TEST(BleV2MediumTest, StartScanning) { +TEST(BleMediumTest, StartScanning) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - auto scanning_session = ble_v2_medium.StartScanning( - Uuid(kTestUuid), api::ble_v2::TxPowerLevel::kLow, - api::ble_v2::BleMedium::ScanningCallback{ + BleMedium ble_medium(mock_bluetooth_adapter); + auto scanning_session = ble_medium.StartScanning( + Uuid(kTestUuid), api::ble::TxPowerLevel::kLow, + api::ble::BleMedium::ScanningCallback{ .start_scanning_result = [](absl::Status) {}, - .advertisement_found_cb = [](api::ble_v2::BlePeripheral::UniqueId, - api::ble_v2::BleAdvertisementData) {}, + .advertisement_found_cb = [](api::ble::BlePeripheral::UniqueId, + api::ble::BleAdvertisementData) {}, }); EXPECT_TRUE(scanning_session != nullptr); } -TEST(BleV2MediumTest, StopScanning) { +TEST(BleMediumTest, StopScanning) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - EXPECT_TRUE(ble_v2_medium.StopScanning()); + BleMedium ble_medium(mock_bluetooth_adapter); + EXPECT_TRUE(ble_medium.StopScanning()); } -TEST(BleV2MediumTest, StartGattServer) { +TEST(BleMediumTest, StartGattServer) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - auto gatt_server = ble_v2_medium.StartGattServer({}); + BleMedium ble_medium(mock_bluetooth_adapter); + auto gatt_server = ble_medium.StartGattServer({}); EXPECT_TRUE(gatt_server != nullptr); } -TEST(BleV2MediumTest, ConnectToGattServer) { +TEST(BleMediumTest, ConnectToGattServer) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - auto gatt_client = ble_v2_medium.ConnectToGattServer( - /*peripheral_id=*/1234, api::ble_v2::TxPowerLevel::kLow, {}); + BleMedium ble_medium(mock_bluetooth_adapter); + auto gatt_client = ble_medium.ConnectToGattServer( + /*peripheral_id=*/1234, api::ble::TxPowerLevel::kLow, {}); EXPECT_TRUE(gatt_client == nullptr); } -TEST(BleV2MediumTest, OpenServerSocket) { +TEST(BleMediumTest, OpenServerSocket) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - auto server_socket = - ble_v2_medium.OpenServerSocket(std::string(kTestServiceId)); + BleMedium ble_medium(mock_bluetooth_adapter); + auto server_socket = ble_medium.OpenServerSocket(std::string(kTestServiceId)); EXPECT_TRUE(server_socket != nullptr); } -TEST(BleV2MediumTest, Connect) { +TEST(BleMediumTest, Connect) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - auto socket = ble_v2_medium.Connect( - std::string(kTestServiceId), api::ble_v2::TxPowerLevel::kLow, + BleMedium ble_medium(mock_bluetooth_adapter); + auto socket = ble_medium.Connect( + std::string(kTestServiceId), api::ble::TxPowerLevel::kLow, /*remote_peripheral_id=*/1234, /*cancellation_flag=*/nullptr); EXPECT_TRUE(socket == nullptr); } -TEST(BleV2MediumTest, OpenL2capServerSocket) { +TEST(BleMediumTest, OpenL2capServerSocket) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); + BleMedium ble_medium(mock_bluetooth_adapter); auto server_socket = - ble_v2_medium.OpenL2capServerSocket(std::string(kTestServiceId)); + ble_medium.OpenL2capServerSocket(std::string(kTestServiceId)); EXPECT_TRUE(server_socket == nullptr); } -TEST(BleV2MediumTest, IsExtendedAdvertisementsAvailable) { +TEST(BleMediumTest, IsExtendedAdvertisementsAvailable) { MockBluetoothAdapter mock_bluetooth_adapter; EXPECT_CALL(mock_bluetooth_adapter, IsEnabled()).WillRepeatedly(Return(true)); - BleV2Medium ble_v2_medium(mock_bluetooth_adapter); - EXPECT_FALSE(ble_v2_medium.IsExtendedAdvertisementsAvailable()); + BleMedium ble_medium(mock_bluetooth_adapter); + EXPECT_FALSE(ble_medium.IsExtendedAdvertisementsAvailable()); } } // namespace diff --git a/internal/platform/implementation/g3/bluetooth_adapter.cc b/internal/platform/implementation/g3/bluetooth_adapter.cc index 9b5daf61..76c0a845 100644 --- a/internal/platform/implementation/g3/bluetooth_adapter.cc +++ b/internal/platform/implementation/g3/bluetooth_adapter.cc @@ -20,7 +20,7 @@ #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/mac_address.h" #include "internal/platform/medium_environment.h" @@ -60,8 +60,8 @@ void BluetoothAdapter::SetBluetoothClassicMedium( bluetooth_classic_medium_ = medium; } -void BluetoothAdapter::SetBleV2Medium(api::ble_v2::BleMedium* medium) { - ble_v2_medium_ = medium; +void BluetoothAdapter::SetBleMedium(api::ble::BleMedium* medium) { + ble_medium_ = medium; } bool BluetoothAdapter::SetStatus(Status status) { diff --git a/internal/platform/implementation/g3/bluetooth_adapter.h b/internal/platform/implementation/g3/bluetooth_adapter.h index 0c4a8f19..eb67fe7c 100644 --- a/internal/platform/implementation/g3/bluetooth_adapter.h +++ b/internal/platform/implementation/g3/bluetooth_adapter.h @@ -21,7 +21,7 @@ #include "absl/base/thread_annotations.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/mac_address.h" @@ -89,7 +89,7 @@ class BluetoothAdapter : public api::BluetoothAdapter { // Returns BT MAC address assigned to this adapter. std::string GetMacAddress() const override { return mac_address_.ToString(); } - MacAddress GetAddress() const override { return mac_address_; } + MacAddress GetAddress() const override { return mac_address_; } BluetoothDevice& GetDevice() { return device_; } @@ -98,12 +98,10 @@ class BluetoothAdapter : public api::BluetoothAdapter { return bluetooth_classic_medium_; } - void SetBleV2Medium(api::ble_v2::BleMedium* medium); - api::ble_v2::BleMedium* GetBleV2Medium() { return ble_v2_medium_; } + void SetBleMedium(api::ble::BleMedium* medium); + api::ble::BleMedium* GetBleMedium() { return ble_medium_; } - void SetMacAddress(MacAddress mac_address) { - mac_address_ = mac_address; - } + void SetMacAddress(MacAddress mac_address) { mac_address_ = mac_address; } std::uint64_t GetUniqueId() { return unique_id_; } @@ -111,7 +109,7 @@ class BluetoothAdapter : public api::BluetoothAdapter { mutable absl::Mutex mutex_; BluetoothDevice device_{this}; api::BluetoothClassicMedium* bluetooth_classic_medium_ = nullptr; - api::ble_v2::BleMedium* ble_v2_medium_ = nullptr; + api::ble::BleMedium* ble_medium_ = nullptr; MacAddress mac_address_; ScanMode mode_ ABSL_GUARDED_BY(mutex_) = ScanMode::kNone; std::string name_ ABSL_GUARDED_BY(mutex_) = "unknown G3 BT device"; diff --git a/internal/platform/implementation/g3/platform.cc b/internal/platform/implementation/g3/platform.cc index bdec23a0..66443d04 100644 --- a/internal/platform/implementation/g3/platform.cc +++ b/internal/platform/implementation/g3/platform.cc @@ -29,7 +29,7 @@ #include "internal/platform/implementation/atomic_boolean.h" #include "internal/platform/implementation/atomic_reference.h" #include "internal/platform/implementation/awdl.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/condition_variable.h" @@ -61,7 +61,7 @@ #endif #include "internal/platform/implementation/g3/atomic_boolean.h" #include "internal/platform/implementation/g3/atomic_reference.h" -#include "internal/platform/implementation/g3/ble_v2.h" +#include "internal/platform/implementation/g3/ble.h" #include "internal/platform/implementation/g3/bluetooth_adapter.h" #include "internal/platform/implementation/g3/bluetooth_classic.h" #include "internal/platform/implementation/g3/condition_variable.h" @@ -185,9 +185,9 @@ ImplementationPlatform::CreateBluetoothClassicMedium( return std::make_unique(adapter); } -std::unique_ptr -ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter& adapter) { - return std::make_unique( +std::unique_ptr +ImplementationPlatform::CreateBleMedium(api::BluetoothAdapter& adapter) { + return std::make_unique( dynamic_cast(adapter)); } diff --git a/internal/platform/implementation/platform.h b/internal/platform/implementation/platform.h index 3db0e10b..2ddf16b7 100644 --- a/internal/platform/implementation/platform.h +++ b/internal/platform/implementation/platform.h @@ -24,7 +24,7 @@ #include "internal/platform/implementation/atomic_boolean.h" #include "internal/platform/implementation/atomic_reference.h" #include "internal/platform/implementation/awdl.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/condition_variable.h" @@ -130,7 +130,7 @@ class ImplementationPlatform { static std::unique_ptr CreateBluetoothAdapter(); static std::unique_ptr CreateBluetoothClassicMedium( BluetoothAdapter&); - static std::unique_ptr CreateBleV2Medium( + static std::unique_ptr CreateBleMedium( api::BluetoothAdapter&); static std::unique_ptr CreateCredentialStorage(); static std::unique_ptr CreateServerSyncMedium(); diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index 6c823c42..f77bc800 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -101,11 +101,11 @@ cc_library( cc_library( name = "comm", hdrs = [ + "ble.h", "ble_gatt_client.h", "ble_gatt_server.h", - "ble_v2.h", - "ble_v2_server_socket.h", - "ble_v2_socket.h", + "ble_server_socket.h", + "ble_socket.h", "bluetooth_adapter.h", "bluetooth_classic.h", "bluetooth_classic_device.h", @@ -202,11 +202,11 @@ cc_library( cc_library( name = "windows", srcs = [ + "ble.cc", "ble_gatt_client.cc", "ble_gatt_server.cc", - "ble_v2.cc", - "ble_v2_server_socket.cc", - "ble_v2_socket.cc", + "ble_server_socket.cc", + "ble_socket.cc", "bluetooth_adapter.cc", "bluetooth_classic_device.cc", "bluetooth_classic_medium.cc", @@ -271,8 +271,8 @@ cc_library( ":types", "//connections/implementation/flags:connections_flags", "//connections/implementation/mediums:utils", - "//connections/implementation/mediums/ble_v2:ble_advertisement_header", - "//connections/implementation/mediums/ble_v2:bloom_filter", + "//connections/implementation/mediums/ble:ble_advertisement_header", + "//connections/implementation/mediums/ble:bloom_filter", "//internal/account", "//internal/base:file_path", "//internal/base:files", @@ -336,7 +336,7 @@ cc_test( "atomic_boolean_test.cc", "atomic_reference_test.cc", "ble_gatt_server_test.cc", - "ble_v2_test.cc", + "ble_test.cc", "bluetooth_adapter_test.cc", "count_down_latch_test.cc", "crypto_test.cc", diff --git a/internal/platform/implementation/windows/ble_v2.cc b/internal/platform/implementation/windows/ble.cc similarity index 91% rename from internal/platform/implementation/windows/ble_v2.cc rename to internal/platform/implementation/windows/ble.cc index 82192b8e..ea91ffe8 100644 --- a/internal/platform/implementation/windows/ble_v2.cc +++ b/internal/platform/implementation/windows/ble.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/platform/implementation/windows/ble_v2.h" +#include "internal/platform/implementation/windows/ble.h" #include #include // NOLINT @@ -34,20 +34,20 @@ #include "absl/synchronization/mutex.h" #include "absl/time/clock.h" #include "absl/time/time.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" -#include "connections/implementation/mediums/ble_v2/bloom_filter.h" +#include "connections/implementation/mediums/ble/ble_advertisement_header.h" +#include "connections/implementation/mediums/ble/bloom_filter.h" #include "connections/implementation/mediums/utils.h" #include "internal/flags/nearby_flags.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/cancellation_flag_listener.h" #include "internal/platform/flags/nearby_platform_feature_flags.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/windows/ble_gatt_client.h" #include "internal/platform/implementation/windows/ble_gatt_server.h" -#include "internal/platform/implementation/windows/ble_v2_server_socket.h" -#include "internal/platform/implementation/windows/ble_v2_socket.h" +#include "internal/platform/implementation/windows/ble_server_socket.h" +#include "internal/platform/implementation/windows/ble_socket.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/implementation/windows/utils.h" #include "internal/platform/logging.h" @@ -67,13 +67,13 @@ namespace { using ::absl::Milliseconds; using ::absl::Minutes; -using ::nearby::api::ble_v2::AdvertiseParameters; -using ::nearby::api::ble_v2::BleAdvertisementData; -using ::nearby::api::ble_v2::BleServerSocket; -using ::nearby::api::ble_v2::BleSocket; -using ::nearby::api::ble_v2::GattClient; -using ::nearby::api::ble_v2::ServerGattConnectionCallback; -using ::nearby::api::ble_v2::TxPowerLevel; +using ::nearby::api::ble::AdvertiseParameters; +using ::nearby::api::ble::BleAdvertisementData; +using ::nearby::api::ble::BleServerSocket; +using ::nearby::api::ble::BleSocket; +using ::nearby::api::ble::GattClient; +using ::nearby::api::ble::ServerGattConnectionCallback; +using ::nearby::api::ble::TxPowerLevel; using ::nearby::connections::mediums::BitSetImpl; using ::nearby::connections::mediums::BleAdvertisementHeader; using ::nearby::connections::mediums::BloomFilter; @@ -133,8 +133,7 @@ std::string TxPowerLevelToName(TxPowerLevel tx_power_level) { // Use the device MAC address as the advertisement hash so that we have a unique // advertisement header for each device. BleAdvertisementHeader CreateAdvertisementHeader( - MacAddress mac_address, - const std::vector& service_ids) { + MacAddress mac_address, const std::vector& service_ids) { BloomFilter bloom_filter( std::make_unique>()); @@ -165,9 +164,9 @@ constexpr absl::Duration kMediumCheckInterval = Milliseconds(50); constexpr uint8_t kUuid16ServiceDataType = 0x16; } // namespace -BleV2Medium::AdvertisementWatcher::~AdvertisementWatcher() { Stop(); } +BleMedium::AdvertisementWatcher::~AdvertisementWatcher() { Stop(); } -void BleV2Medium::AdvertisementWatcher::Initialize( +void BleMedium::AdvertisementWatcher::Initialize( const TypedEventHandler& received_handler, @@ -199,7 +198,7 @@ void BleV2Medium::AdvertisementWatcher::Initialize( } } -void BleV2Medium::AdvertisementWatcher::InitializeWithServiceFilter( +void BleMedium::AdvertisementWatcher::InitializeWithServiceFilter( uint16_t service_uuid, const TypedEventHandler& @@ -240,7 +239,7 @@ void BleV2Medium::AdvertisementWatcher::InitializeWithServiceFilter( } } -bool BleV2Medium::AdvertisementWatcher::Start() { +bool BleMedium::AdvertisementWatcher::Start() { absl::MutexLock lock(&mutex_); if (!initialized_) { LOG(ERROR) << __func__ << ": Failed to start BLE watcher: not initialized."; @@ -277,7 +276,7 @@ bool BleV2Medium::AdvertisementWatcher::Start() { return true; } -void BleV2Medium::AdvertisementWatcher::Stop() { +void BleMedium::AdvertisementWatcher::Stop() { absl::MutexLock lock(&mutex_); try { watcher_.Stop(); @@ -300,12 +299,12 @@ void BleV2Medium::AdvertisementWatcher::Stop() { } } -BleV2Medium::BleV2Medium(api::BluetoothAdapter& adapter) +BleMedium::BleMedium(api::BluetoothAdapter& adapter) : adapter_(dynamic_cast(&adapter)) {} // Advertisement packet and populate accordingly. -bool BleV2Medium::StartAdvertising(const BleAdvertisementData& advertising_data, - AdvertiseParameters advertising_parameters) { +bool BleMedium::StartAdvertising(const BleAdvertisementData& advertising_data, + AdvertiseParameters advertising_parameters) { absl::MutexLock lock(&mutex_); std::string service_data_info; for (const auto& it : advertising_data.service_data) { @@ -321,14 +320,14 @@ bool BleV2Medium::StartAdvertising(const BleAdvertisementData& advertising_data, if (advertising_data.is_extended_advertisement || ble_gatt_server_ == nullptr) { - // In BLE v2, the flag is set when the Bluetooth adapter supports extended + // In BLE, the flag is set when the Bluetooth adapter supports extended // advertising and GATT server is using. return StartBleAdvertising(advertising_data, advertising_parameters); } return StartGattAdvertising(advertising_data, advertising_parameters); } -bool BleV2Medium::StopAdvertising() { +bool BleMedium::StopAdvertising() { absl::MutexLock lock(&mutex_); LOG(INFO) << __func__ << ": Stop advertising."; bool result = true; @@ -353,10 +352,10 @@ bool BleV2Medium::StopAdvertising() { } // TODO(hais) manually verify the new api before switching async flags. -std::unique_ptr BleV2Medium::StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters, - BleV2Medium::AdvertisingCallback callback) { +std::unique_ptr BleMedium::StartAdvertising( + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertise_set_parameters, + BleMedium::AdvertisingCallback callback) { LOG(INFO) << __func__ << ": advertising_data.is_extended_advertisement=" << advertising_data.is_extended_advertisement << ", advertising_data.service_data size=" @@ -375,8 +374,8 @@ std::unique_ptr BleV2Medium::StartAdvertising( return nullptr; } - return std::make_unique( - BleV2Medium::AdvertisingSession{ + return std::make_unique( + BleMedium::AdvertisingSession{ .stop_advertising = [this]() { if (StopAdvertising()) { @@ -388,12 +387,12 @@ std::unique_ptr BleV2Medium::StartAdvertising( }); } -std::unique_ptr -BleV2Medium::CreateBleWatcher(uint16_t service_uuid) { +std::unique_ptr BleMedium::CreateBleWatcher( + uint16_t service_uuid) { auto watcher = std::make_unique(); watcher->InitializeWithServiceFilter( - service_uuid, {this, &BleV2Medium::AdvertisementReceivedHandler}, - {this, &BleV2Medium::WatcherHandler}); + service_uuid, {this, &BleMedium::AdvertisementReceivedHandler}, + {this, &BleMedium::WatcherHandler}); if (!watcher->Start()) { return nullptr; @@ -403,9 +402,9 @@ BleV2Medium::CreateBleWatcher(uint16_t service_uuid) { return watcher; } -bool BleV2Medium::StartScanning(const Uuid& service_uuid, - TxPowerLevel tx_power_level, - ScanCallback callback) { +bool BleMedium::StartScanning(const Uuid& service_uuid, + TxPowerLevel tx_power_level, + ScanCallback callback) { absl::MutexLock lock(&mutex_); LOG(INFO) << __func__ << ": service UUID: " << std::string(service_uuid) << ", TxPowerLevel: " << TxPowerLevelToName(tx_power_level); @@ -463,9 +462,9 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid, return true; } -std::unique_ptr BleV2Medium::StartScanning( +std::unique_ptr BleMedium::StartScanning( const Uuid& service_uuid, TxPowerLevel tx_power_level, - BleV2Medium::ScanningCallback callback) { + BleMedium::ScanningCallback callback) { absl::MutexLock lock(&mutex_); LOG(INFO) << __func__ << ": service UUID: " << std::string(service_uuid) << ", TxPowerLevel: " << TxPowerLevelToName(tx_power_level); @@ -478,8 +477,8 @@ std::unique_ptr BleV2Medium::StartScanning( if (watchers_.empty()) { LOG(WARNING) << __func__ << ": Starting BLE Scanning."; auto watcher = std::make_unique(); - watcher->Initialize({this, &BleV2Medium::AdvertisementFoundHandler}, - {this, &BleV2Medium::WatcherHandler}); + watcher->Initialize({this, &BleMedium::AdvertisementFoundHandler}, + {this, &BleMedium::WatcherHandler}); if (!watcher->Start()) { return nullptr; } @@ -499,8 +498,8 @@ std::unique_ptr BleV2Medium::StartScanning( } // Generate and return ScanningSession. - return std::make_unique( - BleV2Medium::ScanningSession{ + return std::make_unique( + BleMedium::ScanningSession{ .stop_scanning = [this, session_id, service_uuid]() { size_t num_erased_from_service_and_session_map = 0u; @@ -552,8 +551,8 @@ std::unique_ptr BleV2Medium::StartScanning( }); } -std::unique_ptr BleV2Medium::StartGattServer( - api::ble_v2::ServerGattConnectionCallback callback) { +std::unique_ptr BleMedium::StartGattServer( + api::ble::ServerGattConnectionCallback callback) { absl::MutexLock lock(&mutex_); LOG(INFO) << __func__ << ": Start GATT server."; @@ -573,10 +572,10 @@ std::unique_ptr BleV2Medium::StartGattServer( return gatt_server; } -std::unique_ptr BleV2Medium::ConnectToGattServer( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, +std::unique_ptr BleMedium::ConnectToGattServer( + api::ble::BlePeripheral::UniqueId peripheral_id, TxPowerLevel tx_power_level, - api::ble_v2::ClientGattConnectionCallback callback) { + api::ble::ClientGattConnectionCallback callback) { absl::MutexLock lock(&mutex_); LOG(INFO) << "ConnectToGattServer is called with peripheral id: " << peripheral_id @@ -585,8 +584,7 @@ std::unique_ptr BleV2Medium::ConnectToGattServer( try { // In windows, peripheral unique id is the same as the bluetooth address. BluetoothLEDevice ble_device = - BluetoothLEDevice::FromBluetoothAddressAsync(peripheral_id) - .get(); + BluetoothLEDevice::FromBluetoothAddressAsync(peripheral_id).get(); return std::make_unique(ble_device); } catch (std::exception exception) { @@ -601,7 +599,7 @@ std::unique_ptr BleV2Medium::ConnectToGattServer( return nullptr; } -bool BleV2Medium::StopScanning() { +bool BleMedium::StopScanning() { absl::MutexLock lock(&mutex_); alternate_uuids_for_service_.clear(); @@ -631,11 +629,11 @@ bool BleV2Medium::StopScanning() { return true; } -std::unique_ptr BleV2Medium::OpenServerSocket( +std::unique_ptr BleMedium::OpenServerSocket( const std::string& service_id) { VLOG(1) << "OpenServerSocket is called"; - auto server_socket = std::make_unique(adapter_); + auto server_socket = std::make_unique(adapter_); if (!server_socket->Bind()) { LOG(ERROR) << __func__ << ": Failed to bing socket."; @@ -645,9 +643,9 @@ std::unique_ptr BleV2Medium::OpenServerSocket( return server_socket; } -std::unique_ptr BleV2Medium::Connect( +std::unique_ptr BleMedium::Connect( const std::string& service_id, TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id, + api::ble::BlePeripheral::UniqueId remote_peripheral_id, CancellationFlag* cancellation_flag) { LOG(INFO) << __func__ << ": Connect to service_id=" << service_id; @@ -662,7 +660,7 @@ std::unique_ptr BleV2Medium::Connect( return nullptr; } - auto ble_socket = std::make_unique(); + auto ble_socket = std::make_unique(); nearby::CancellationFlagListener cancellation_flag_listener( cancellation_flag, [socket = ble_socket.get()]() { socket->Close(); }); @@ -676,13 +674,13 @@ std::unique_ptr BleV2Medium::Connect( return ble_socket; } -bool BleV2Medium::IsExtendedAdvertisementsAvailable() { +bool BleMedium::IsExtendedAdvertisementsAvailable() { return adapter_->IsExtendedAdvertisingSupported(); } -bool BleV2Medium::StartBleAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertising_parameters) { +bool BleMedium::StartBleAdvertising( + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertising_parameters) { LOG(INFO) << __func__ << ": Start BLE advertising, extended:" << advertising_data.is_extended_advertisement; try { @@ -762,7 +760,7 @@ bool BleV2Medium::StartBleAdvertising( publisher_.UseExtendedAdvertisement(false); } publisher_token_ = - publisher_.StatusChanged({this, &BleV2Medium::PublisherHandler}); + publisher_.StatusChanged({this, &BleMedium::PublisherHandler}); publisher_.Start(); @@ -802,7 +800,7 @@ bool BleV2Medium::StartBleAdvertising( } } -bool BleV2Medium::StopBleAdvertising() { +bool BleMedium::StopBleAdvertising() { LOG(INFO) << __func__ << ": Stop BLE advertising."; try { if (!is_ble_publisher_started_) { @@ -863,9 +861,9 @@ bool BleV2Medium::StopBleAdvertising() { return false; } -bool BleV2Medium::StartGattAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertising_parameters) { +bool BleMedium::StartGattAdvertising( + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertising_parameters) { LOG(INFO) << __func__ << ": Start GATT advertising."; try { if (!adapter_->IsEnabled()) { @@ -927,7 +925,7 @@ bool BleV2Medium::StartGattAdvertising( } } -bool BleV2Medium::StopGattAdvertising() { +bool BleMedium::StopGattAdvertising() { try { LOG(INFO) << __func__ << ": Stop GATT advertising."; @@ -968,7 +966,7 @@ bool BleV2Medium::StopGattAdvertising() { return false; } -void BleV2Medium::PublisherHandler( +void BleMedium::PublisherHandler( BluetoothLEAdvertisementPublisher publisher, BluetoothLEAdvertisementPublisherStatusChangedEventArgs args) { // This method is called when publisher's status is changed. @@ -1049,7 +1047,7 @@ void BleV2Medium::PublisherHandler( } } -void BleV2Medium::WatcherHandler( +void BleMedium::WatcherHandler( BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementWatcherStoppedEventArgs args) { // This method is called when watcher stopped. Args give more detailed @@ -1096,7 +1094,7 @@ void BleV2Medium::WatcherHandler( } } -void BleV2Medium::AdvertisementReceivedHandler( +void BleMedium::AdvertisementReceivedHandler( BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs args) { // Handle all BLE advertisements and determine whether the BLE Medium @@ -1139,7 +1137,7 @@ void BleV2Medium::AdvertisementReceivedHandler( << " peripheral address: " << bluetooth_address.ToString() << " unconsumed_buffer_length: " << unconsumed_buffer_length; - api::ble_v2::BleAdvertisementData ble_advertisement_data; + api::ble::BleAdvertisementData ble_advertisement_data; if (unconsumed_buffer_length <= 27) { ble_advertisement_data.is_extended_advertisement = false; } else { @@ -1166,7 +1164,7 @@ void BleV2Medium::AdvertisementReceivedHandler( << bluetooth_address.ToString() << " for services: " << absl::StrJoin(alt_service_ids, ","); // Create fake advertisement data. - api::ble_v2::BleAdvertisementData ble_advertisement_data; + api::ble::BleAdvertisementData ble_advertisement_data; ble_advertisement_data.is_extended_advertisement = false; BleAdvertisementHeader header = CreateAdvertisementHeader(bluetooth_address, alt_service_ids); @@ -1176,7 +1174,7 @@ void BleV2Medium::AdvertisementReceivedHandler( } } -void BleV2Medium::AdvertisementFoundHandler( +void BleMedium::AdvertisementFoundHandler( BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs args) { BluetoothLEAdvertisement advertisement = args.Advertisement(); @@ -1199,7 +1197,7 @@ void BleV2Medium::AdvertisementFoundHandler( } // Construct BleAdvertisementData from the windows Advertisement. - api::ble_v2::BleAdvertisementData ble_advertisement_data; + api::ble::BleAdvertisementData ble_advertisement_data; for (BluetoothLEAdvertisementDataSection service_data : advertisement.GetSectionsByType(kUuid16ServiceDataType)) { // Parse Advertisement Data for Section 0x16 (Service Data) @@ -1260,7 +1258,7 @@ void BleV2Medium::AdvertisementFoundHandler( } } -uint64_t BleV2Medium::GenerateSessionId() { +uint64_t BleMedium::GenerateSessionId() { for (int i = 0; i < kGenerateSessionIdRetryLimit; i++) { uint64_t session_id = Prng().NextInt64(); if (session_id == kFailedGenerateSessionId) continue; @@ -1272,8 +1270,8 @@ uint64_t BleV2Medium::GenerateSessionId() { return kFailedGenerateSessionId; } -void BleV2Medium::AddAlternateUuidForService(uint16_t uuid, - const std::string& service_id) { +void BleMedium::AddAlternateUuidForService(uint16_t uuid, + const std::string& service_id) { absl::MutexLock lock(&mutex_); alternate_uuids_for_service_[uuid] = service_id; } diff --git a/internal/platform/implementation/windows/ble_v2.h b/internal/platform/implementation/windows/ble.h similarity index 81% rename from internal/platform/implementation/windows/ble_v2.h rename to internal/platform/implementation/windows/ble.h index 64d383cb..6ebe5a32 100644 --- a/internal/platform/implementation/windows/ble_v2.h +++ b/internal/platform/implementation/windows/ble.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_H_ -#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_H_ +#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_H_ #include #include @@ -25,7 +25,7 @@ #include "absl/container/flat_hash_map.h" #include "absl/synchronization/mutex.h" #include "internal/platform/cancellation_flag.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/windows/ble_gatt_server.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" @@ -36,44 +36,43 @@ namespace nearby { namespace windows { // Container of operations that can be performed over the BLE medium. -class BleV2Medium : public api::ble_v2::BleMedium { +class BleMedium : public api::ble::BleMedium { public: - explicit BleV2Medium(api::BluetoothAdapter& adapter); - ~BleV2Medium() override = default; + explicit BleMedium(api::BluetoothAdapter& adapter); + ~BleMedium() override = default; // Returns true once the Ble advertising has been initiated. - bool StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertising_parameters) override - ABSL_LOCKS_EXCLUDED(mutex_); + bool StartAdvertising(const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertising_parameters) + override ABSL_LOCKS_EXCLUDED(mutex_); bool StopAdvertising() override ABSL_LOCKS_EXCLUDED(mutex_); std::unique_ptr StartAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertise_set_parameters, + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertise_set_parameters, AdvertisingCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); bool StartScanning(const Uuid& service_uuid, - api::ble_v2::TxPowerLevel tx_power_level, + api::ble::TxPowerLevel tx_power_level, ScanCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); bool StopScanning() override ABSL_LOCKS_EXCLUDED(mutex_); std::unique_ptr StartScanning( - const Uuid& service_uuid, api::ble_v2::TxPowerLevel tx_power_level, + const Uuid& service_uuid, api::ble::TxPowerLevel tx_power_level, ScanningCallback callback) override; - std::unique_ptr StartGattServer( - api::ble_v2::ServerGattConnectionCallback callback) override + std::unique_ptr StartGattServer( + api::ble::ServerGattConnectionCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); - std::unique_ptr ConnectToGattServer( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::ClientGattConnectionCallback callback) override + std::unique_ptr ConnectToGattServer( + api::ble::BlePeripheral::UniqueId peripheral_id, + api::ble::TxPowerLevel tx_power_level, + api::ble::ClientGattConnectionCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); - std::unique_ptr OpenServerSocket( + std::unique_ptr OpenServerSocket( const std::string& service_id) override ABSL_LOCKS_EXCLUDED(mutex_); - std::unique_ptr Connect( - const std::string& service_id, api::ble_v2::TxPowerLevel tx_power_level, - api::ble_v2::BlePeripheral::UniqueId remote_peripheral_id, + std::unique_ptr Connect( + const std::string& service_id, api::ble::TxPowerLevel tx_power_level, + api::ble::BlePeripheral::UniqueId remote_peripheral_id, CancellationFlag* cancellation_flag) override ABSL_LOCKS_EXCLUDED(mutex_); bool IsExtendedAdvertisementsAvailable() override; @@ -81,7 +80,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { const std::string& service_id) override; private: - friend class BleV2MediumTest; + friend class BleMediumTest; // A wrapper class for BluetoothLEAdvertisementWatcher. class AdvertisementWatcher { @@ -136,14 +135,14 @@ class BleV2Medium : public api::ble_v2::BleMedium { }; bool StartBleAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertising_parameters) + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertising_parameters) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); bool StopBleAdvertising() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); bool StartGattAdvertising( - const api::ble_v2::BleAdvertisementData& advertising_data, - api::ble_v2::AdvertiseParameters advertising_parameters) + const api::ble::BleAdvertisementData& advertising_data, + api::ble::AdvertiseParameters advertising_parameters) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); bool StopGattAdvertising() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); @@ -172,7 +171,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { uint64_t GenerateSessionId() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); - std::unique_ptr CreateBleWatcher( + std::unique_ptr CreateBleWatcher( uint16_t service_uuid); absl::Mutex mutex_; @@ -180,7 +179,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { BluetoothAdapter* const adapter_; Uuid service_uuid_; uint16_t service_uuid16_ = 0; - api::ble_v2::TxPowerLevel tx_power_level_; + api::ble::TxPowerLevel tx_power_level_; ScanCallback scan_callback_; std::vector> watchers_ ABSL_GUARDED_BY(mutex_); @@ -214,4 +213,4 @@ class BleV2Medium : public api::ble_v2::BleMedium { } // namespace windows } // namespace nearby -#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_H_ +#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_H_ diff --git a/internal/platform/implementation/windows/ble_gatt_client.cc b/internal/platform/implementation/windows/ble_gatt_client.cc index f92d1969..bbe4f233 100644 --- a/internal/platform/implementation/windows/ble_gatt_client.cc +++ b/internal/platform/implementation/windows/ble_gatt_client.cc @@ -36,7 +36,7 @@ #include "internal/flags/nearby_flags.h" #include "internal/platform/byte_array.h" #include "internal/platform/flags/nearby_platform_feature_flags.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/implementation/windows/utils.h" #include "internal/platform/logging.h" @@ -79,9 +79,9 @@ using ::winrt::Windows::Foundation::Collections::IVectorView; using ::winrt::Windows::Storage::Streams::Buffer; using ::winrt::Windows::Storage::Streams::DataReader; using ::winrt::Windows::Storage::Streams::IBuffer; -using Property = api::ble_v2::GattCharacteristic::Property; -using Permission = api::ble_v2::GattCharacteristic::Permission; -using WriteType = api::ble_v2::GattClient::WriteType; +using Property = api::ble::GattCharacteristic::Property; +using Permission = api::ble::GattCharacteristic::Permission; +using WriteType = api::ble::GattClient::WriteType; constexpr int kGattTimeoutInSeconds = 5; @@ -269,9 +269,8 @@ bool BleGattClient::DiscoverServiceAndCharacteristics( return false; } -absl::optional -BleGattClient::GetCharacteristic(const Uuid& service_uuid, - const Uuid& characteristic_uuid) { +absl::optional BleGattClient::GetCharacteristic( + const Uuid& service_uuid, const Uuid& characteristic_uuid) { absl::MutexLock lock(&mutex_); VLOG(1) << __func__ << ": Stared to get characteristic UUID=" << std::string(characteristic_uuid) @@ -285,12 +284,12 @@ BleGattClient::GetCharacteristic(const Uuid& service_uuid, return absl::nullopt; } - api::ble_v2::GattCharacteristic result; + api::ble::GattCharacteristic result; result.service_uuid = service_uuid; result.uuid = characteristic_uuid; // Note: Windows has protection level on GattCharacteristic. cannot - // find a way to map it to api::ble_v2::GattCharacteristic. + // find a way to map it to api::ble::GattCharacteristic. GattCharacteristicProperties properties = gatt_characteristic->CharacteristicProperties(); result.permission = Permission::kNone; @@ -336,7 +335,7 @@ BleGattClient::GetCharacteristic(const Uuid& service_uuid, } absl::optional BleGattClient::ReadCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic) { + const api::ble::GattCharacteristic& characteristic) { absl::MutexLock lock(&mutex_); VLOG(1) << __func__ << ": Read characteristic=" << std::string(characteristic.uuid); @@ -390,8 +389,8 @@ absl::optional BleGattClient::ReadCharacteristic( } bool BleGattClient::WriteCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, - absl::string_view value, api::ble_v2::GattClient::WriteType write_type) { + const api::ble::GattCharacteristic& characteristic, absl::string_view value, + api::ble::GattClient::WriteType write_type) { absl::MutexLock lock(&mutex_); VLOG(1) << __func__ << ": write characteristic: " << std::string(characteristic.uuid); @@ -441,7 +440,7 @@ bool BleGattClient::WriteCharacteristic( } bool BleGattClient::SetCharacteristicSubscription( - const api::ble_v2::GattCharacteristic& characteristic, bool enable, + const api::ble::GattCharacteristic& characteristic, bool enable, absl::AnyInvocable on_characteristic_changed_cb) { absl::MutexLock lock(&mutex_); @@ -629,7 +628,7 @@ bool BleGattClient::WriteCharacteristicConfigurationDescriptor( } void BleGattClient::OnCharacteristicValueChanged( - const api::ble_v2::GattCharacteristic& characteristic, + const api::ble::GattCharacteristic& characteristic, GattValueChangedEventArgs args) { VLOG(1) << __func__ << ": Gatt Characteristic value changed."; IBuffer buffer = args.CharacteristicValue(); diff --git a/internal/platform/implementation/windows/ble_gatt_client.h b/internal/platform/implementation/windows/ble_gatt_client.h index 473e5e2f..651001e1 100644 --- a/internal/platform/implementation/windows/ble_gatt_client.h +++ b/internal/platform/implementation/windows/ble_gatt_client.h @@ -30,7 +30,7 @@ #include "absl/synchronization/mutex.h" #include "absl/types/optional.h" #include "internal/platform/byte_array.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/uuid.h" #include "winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h" #include "winrt/Windows.Devices.Bluetooth.h" @@ -39,7 +39,7 @@ namespace nearby { namespace windows { -class BleGattClient : public api::ble_v2::GattClient { +class BleGattClient : public api::ble::GattClient { public: explicit BleGattClient( ::winrt::Windows::Devices::Bluetooth::BluetoothLEDevice ble_device); @@ -50,22 +50,21 @@ class BleGattClient : public api::ble_v2::GattClient { const std::vector& characteristic_uuids) override ABSL_LOCKS_EXCLUDED(mutex_); - absl::optional GetCharacteristic( + absl::optional GetCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid) override ABSL_LOCKS_EXCLUDED(mutex_); absl::optional ReadCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic) override + const api::ble::GattCharacteristic& characteristic) override ABSL_LOCKS_EXCLUDED(mutex_); - bool WriteCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, - absl::string_view value, - api::ble_v2::GattClient::WriteType write_type) override + bool WriteCharacteristic(const api::ble::GattCharacteristic& characteristic, + absl::string_view value, + api::ble::GattClient::WriteType write_type) override ABSL_LOCKS_EXCLUDED(mutex_); bool SetCharacteristicSubscription( - const api::ble_v2::GattCharacteristic& characteristic, bool enable, + const api::ble::GattCharacteristic& characteristic, bool enable, absl::AnyInvocable on_characteristic_changed_cb) override ABSL_LOCKS_EXCLUDED(mutex_); @@ -95,7 +94,7 @@ class BleGattClient : public api::ble_v2::GattClient { ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); void OnCharacteristicValueChanged( - const api::ble_v2::GattCharacteristic& characteristic, + const api::ble::GattCharacteristic& characteristic, ::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile:: GattValueChangedEventArgs args); @@ -107,7 +106,7 @@ class BleGattClient : public api::ble_v2::GattClient { GattDeviceServicesResult gatt_devices_services_result_ ABSL_GUARDED_BY(mutex_) = nullptr; - absl::flat_hash_map + absl::flat_hash_map native_characteristic_map_ ABSL_GUARDED_BY(mutex_); }; diff --git a/internal/platform/implementation/windows/ble_gatt_server.cc b/internal/platform/implementation/windows/ble_gatt_server.cc index 97b8addf..9f394e44 100644 --- a/internal/platform/implementation/windows/ble_gatt_server.cc +++ b/internal/platform/implementation/windows/ble_gatt_server.cc @@ -35,7 +35,7 @@ #include "absl/time/time.h" #include "absl/types/optional.h" #include "internal/platform/byte_array.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/implementation/windows/utils.h" @@ -85,8 +85,8 @@ using ::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile:: using ::winrt::Windows::Foundation::Collections::IVectorView; using ::winrt::Windows::Storage::Streams::Buffer; using ::winrt::Windows::Storage::Streams::DataWriter; -using Permission = api::ble_v2::GattCharacteristic::Permission; -using Property = api::ble_v2::GattCharacteristic::Property; +using Permission = api::ble::GattCharacteristic::Permission; +using Property = api::ble::GattCharacteristic::Property; constexpr absl::Duration kGattServerTimeout = absl::Milliseconds(500); constexpr int kGattServerCheckIntervalInMills = 50; @@ -113,17 +113,17 @@ std::string ConvertGattStatusToString( } // namespace BleGattServer::BleGattServer(api::BluetoothAdapter* adapter, - api::ble_v2::ServerGattConnectionCallback callback) + api::ble::ServerGattConnectionCallback callback) : adapter_(dynamic_cast(adapter)), gatt_connection_callback_(std::move(callback)) { DCHECK(adapter_ != nullptr); } -absl::optional +absl::optional BleGattServer::CreateCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid, - api::ble_v2::GattCharacteristic::Permission permission, - api::ble_v2::GattCharacteristic::Property property) { + api::ble::GattCharacteristic::Permission permission, + api::ble::GattCharacteristic::Property property) { absl::MutexLock lock(&mutex_); LOG(INFO) << __func__ << ": create characteristic, service_uuid: " << std::string(service_uuid) @@ -136,7 +136,7 @@ BleGattServer::CreateCharacteristic( service_uuid_ = service_uuid; - api::ble_v2::GattCharacteristic gatt_characteristic; + api::ble::GattCharacteristic gatt_characteristic; gatt_characteristic.uuid = characteristic_uuid; gatt_characteristic.service_uuid = service_uuid; gatt_characteristic.permission = permission; @@ -151,7 +151,7 @@ BleGattServer::CreateCharacteristic( } bool BleGattServer::UpdateCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, + const api::ble::GattCharacteristic& characteristic, const nearby::ByteArray& value) { absl::MutexLock lock(&mutex_); LOG(INFO) << __func__ @@ -192,7 +192,7 @@ bool BleGattServer::UpdateCharacteristic( } absl::Status BleGattServer::NotifyCharacteristicChanged( - const api::ble_v2::GattCharacteristic& characteristic, bool confirm, + const api::ble::GattCharacteristic& characteristic, bool confirm, const ByteArray& new_value) { absl::MutexLock lock(&mutex_); // Currently, the method is not hooked up at platform layer. @@ -598,9 +598,8 @@ void BleGattServer::Characteristic_SubscribedClientsChanged( ::winrt::to_hstring(gatt_local_characteristic.Uuid())); try { - std::vector - added_subscribed_characteristics; - std::vector + std::vector added_subscribed_characteristics; + std::vector removed_subscribed_characteristics; GattCharacteristicData* characteristic_data = @@ -685,7 +684,7 @@ void BleGattServer::ServiceProvider_AdvertisementStatusChanged( } void BleGattServer::NotifyValueChanged( - const api::ble_v2::GattCharacteristic& gatt_characteristic) { + const api::ble::GattCharacteristic& gatt_characteristic) { try { GattCharacteristicData* characteristic_data = FindGattCharacteristicData(gatt_characteristic); @@ -743,7 +742,7 @@ BleGattServer::FindGattCharacteristicData( BleGattServer::GattCharacteristicData* BleGattServer::FindGattCharacteristicData( - const api::ble_v2::GattCharacteristic& gatt_characteristic) { + const api::ble::GattCharacteristic& gatt_characteristic) { for (auto& characteristic_data : gatt_characteristic_datas_) { if (gatt_characteristic.uuid == characteristic_data.gatt_characteristic.uuid) { diff --git a/internal/platform/implementation/windows/ble_gatt_server.h b/internal/platform/implementation/windows/ble_gatt_server.h index 75b02e2c..78ed86b1 100644 --- a/internal/platform/implementation/windows/ble_gatt_server.h +++ b/internal/platform/implementation/windows/ble_gatt_server.h @@ -29,7 +29,7 @@ #include "absl/synchronization/notification.h" #include "absl/types/optional.h" #include "internal/platform/byte_array.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/uuid.h" @@ -41,24 +41,24 @@ namespace nearby { namespace windows { -class BleGattServer : public api::ble_v2::GattServer { +class BleGattServer : public api::ble::GattServer { public: // Make sure the adapter parameter is not null. BleGattServer(api::BluetoothAdapter* adapter, - api::ble_v2::ServerGattConnectionCallback callback); + api::ble::ServerGattConnectionCallback callback); ~BleGattServer() override = default; - absl::optional CreateCharacteristic( + absl::optional CreateCharacteristic( const Uuid& service_uuid, const Uuid& characteristic_uuid, - api::ble_v2::GattCharacteristic::Permission permission, - api::ble_v2::GattCharacteristic::Property property) override + api::ble::GattCharacteristic::Permission permission, + api::ble::GattCharacteristic::Property property) override ABSL_LOCKS_EXCLUDED(mutex_); - bool UpdateCharacteristic( - const api::ble_v2::GattCharacteristic& characteristic, - const nearby::ByteArray& value) override ABSL_LOCKS_EXCLUDED(mutex_); + bool UpdateCharacteristic(const api::ble::GattCharacteristic& characteristic, + const nearby::ByteArray& value) override + ABSL_LOCKS_EXCLUDED(mutex_); absl::Status NotifyCharacteristicChanged( - const api::ble_v2::GattCharacteristic& characteristic, bool confirm, + const api::ble::GattCharacteristic& characteristic, bool confirm, const ByteArray& new_value) override ABSL_LOCKS_EXCLUDED(mutex_); void Stop() override ABSL_LOCKS_EXCLUDED(mutex_); @@ -73,7 +73,7 @@ class BleGattServer : public api::ble_v2::GattServer { private: // Used to save native data related to the GATT characteristic. struct GattCharacteristicData { - api::ble_v2::GattCharacteristic gatt_characteristic; + api::ble::GattCharacteristic gatt_characteristic; ByteArray data; ::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile:: GattLocalCharacteristic local_characteristic = nullptr; @@ -88,7 +88,7 @@ class BleGattServer : public api::ble_v2::GattServer { bool InitializeGattServer() ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); void NotifyValueChanged( - const api::ble_v2::GattCharacteristic& gatt_characteristic) + const api::ble::GattCharacteristic& gatt_characteristic) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); GattCharacteristicData* FindGattCharacteristicData( @@ -96,7 +96,7 @@ class BleGattServer : public api::ble_v2::GattServer { GattLocalCharacteristic& gatt_local_characteristic) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); GattCharacteristicData* FindGattCharacteristicData( - const api::ble_v2::GattCharacteristic& gatt_characteristic) + const api::ble::GattCharacteristic& gatt_characteristic) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_); ::winrt::fire_and_forget Characteristic_ReadRequestedAsync( @@ -127,7 +127,7 @@ class BleGattServer : public api::ble_v2::GattServer { absl::Mutex mutex_; BluetoothAdapter* const adapter_ = nullptr; - api::ble_v2::ServerGattConnectionCallback gatt_connection_callback_{}; + api::ble::ServerGattConnectionCallback gatt_connection_callback_{}; ::winrt::Windows::Devices::Bluetooth::GenericAttributeProfile:: GattServiceProvider gatt_service_provider_ ABSL_GUARDED_BY(mutex_) = diff --git a/internal/platform/implementation/windows/ble_gatt_server_test.cc b/internal/platform/implementation/windows/ble_gatt_server_test.cc index b374ef09..619f65d7 100644 --- a/internal/platform/implementation/windows/ble_gatt_server_test.cc +++ b/internal/platform/implementation/windows/ble_gatt_server_test.cc @@ -21,7 +21,8 @@ #include "gtest/gtest.h" #include "absl/synchronization/notification.h" #include "absl/time/time.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" namespace nearby { @@ -30,26 +31,25 @@ namespace { TEST(BleV2GattServer, DISABLED_StartStopAdvertising) { BluetoothAdapter bluetoothAdapter; - BleGattServer blev2_gatt_server(&bluetoothAdapter, {}); + BleGattServer ble_gatt_server(&bluetoothAdapter, {}); - ByteArray - service_data; // {.tx_power_level = api::ble_v2::TxPowerLevel::kHigh} + ByteArray service_data; // {.tx_power_level = api::ble::TxPowerLevel::kHigh} - EXPECT_TRUE(blev2_gatt_server.StartAdvertisement(service_data, true)); + EXPECT_TRUE(ble_gatt_server.StartAdvertisement(service_data, true)); - EXPECT_TRUE(blev2_gatt_server.StopAdvertisement()); + EXPECT_TRUE(ble_gatt_server.StopAdvertisement()); } TEST(BleV2GattServer, DISABLED_Stop) { BluetoothAdapter bluetoothAdapter; - BleGattServer blev2_gatt_server(&bluetoothAdapter, {}); + BleGattServer ble_gatt_server(&bluetoothAdapter, {}); - blev2_gatt_server.Stop(); + ble_gatt_server.Stop(); } TEST(BleV2GattServer, DISABLED_StopNotifierIsCalled) { BluetoothAdapter bluetoothAdapter; - BleGattServer blev2_gatt_server(&bluetoothAdapter, {}); + BleGattServer ble_gatt_server(&bluetoothAdapter, {}); bool is_close_notifier_called = false; absl::Notification notification; std::function notifier = [&is_close_notifier_called, @@ -57,25 +57,25 @@ TEST(BleV2GattServer, DISABLED_StopNotifierIsCalled) { is_close_notifier_called = true; notification.Notify(); }; - blev2_gatt_server.SetCloseNotifier(std::move(notifier)); + ble_gatt_server.SetCloseNotifier(std::move(notifier)); - blev2_gatt_server.Stop(); + ble_gatt_server.Stop(); notification.WaitForNotificationWithTimeout(absl::Seconds(1)); EXPECT_TRUE(is_close_notifier_called); } TEST(BleV2GattServer, DISABLED_CreateCharacteristic) { BluetoothAdapter bluetoothAdapter; - BleGattServer blev2_gatt_server(&bluetoothAdapter, {}); + BleGattServer ble_gatt_server(&bluetoothAdapter, {}); Uuid service_uuid; Uuid characteristic_uuid; - const api::ble_v2::GattCharacteristic::Permission permissions = - api::ble_v2::GattCharacteristic::Permission::kNone; - const api::ble_v2::GattCharacteristic::Property properties = - api::ble_v2::GattCharacteristic::Property::kNone; + const api::ble::GattCharacteristic::Permission permissions = + api::ble::GattCharacteristic::Permission::kNone; + const api::ble::GattCharacteristic::Property properties = + api::ble::GattCharacteristic::Property::kNone; - EXPECT_TRUE(blev2_gatt_server + EXPECT_TRUE(ble_gatt_server .CreateCharacteristic(service_uuid, characteristic_uuid, permissions, properties) .has_value()); @@ -83,21 +83,21 @@ TEST(BleV2GattServer, DISABLED_CreateCharacteristic) { TEST(BleV2GattServer, DISABLED_UpdateCharacteristic) { BluetoothAdapter bluetoothAdapter; - BleGattServer blev2_gatt_server(&bluetoothAdapter, {}); + BleGattServer ble_gatt_server(&bluetoothAdapter, {}); Uuid service_uuid; Uuid characteristic_uuid; - const api::ble_v2::GattCharacteristic::Permission permissions = - api::ble_v2::GattCharacteristic::Permission::kNone; - const api::ble_v2::GattCharacteristic::Property properties = - api::ble_v2::GattCharacteristic::Property::kNone; + const api::ble::GattCharacteristic::Permission permissions = + api::ble::GattCharacteristic::Permission::kNone; + const api::ble::GattCharacteristic::Property properties = + api::ble::GattCharacteristic::Property::kNone; - auto gatt_characteristic = blev2_gatt_server.CreateCharacteristic( + auto gatt_characteristic = ble_gatt_server.CreateCharacteristic( service_uuid, characteristic_uuid, permissions, properties); ByteArray value; - EXPECT_TRUE(blev2_gatt_server.UpdateCharacteristic( - gatt_characteristic.value(), value)); + EXPECT_TRUE( + ble_gatt_server.UpdateCharacteristic(gatt_characteristic.value(), value)); } } // namespace diff --git a/internal/platform/implementation/windows/ble_v2_server_socket.cc b/internal/platform/implementation/windows/ble_server_socket.cc similarity index 78% rename from internal/platform/implementation/windows/ble_v2_server_socket.cc rename to internal/platform/implementation/windows/ble_server_socket.cc index 0f3a9a32..222d39c2 100644 --- a/internal/platform/implementation/windows/ble_v2_server_socket.cc +++ b/internal/platform/implementation/windows/ble_server_socket.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/platform/implementation/windows/ble_v2_server_socket.h" +#include "internal/platform/implementation/windows/ble_server_socket.h" #include #include @@ -21,9 +21,9 @@ #include "absl/synchronization/mutex.h" #include "absl/synchronization/notification.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" -#include "internal/platform/implementation/windows/ble_v2_socket.h" +#include "internal/platform/implementation/windows/ble_socket.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/implementation/windows/utils.h" #include "internal/platform/logging.h" @@ -31,12 +31,12 @@ namespace nearby { namespace windows { -BleV2ServerSocket::BleV2ServerSocket(api::BluetoothAdapter* adapter) +BleServerSocket::BleServerSocket(api::BluetoothAdapter* adapter) : adapter_(dynamic_cast(adapter)) { DCHECK_NE(adapter_, nullptr); } -std::unique_ptr BleV2ServerSocket::Accept() { +std::unique_ptr BleServerSocket::Accept() { absl::MutexLock lock(&mutex_); VLOG(1) << __func__ << ": Accept is called."; @@ -45,14 +45,14 @@ std::unique_ptr BleV2ServerSocket::Accept() { } if (closed_) return nullptr; - BleV2Socket ble_socket = pending_sockets_.front(); + BleSocket ble_socket = pending_sockets_.front(); pending_sockets_.pop_front(); LOG(INFO) << __func__ << ": Accepted a remote connection."; - return std::make_unique(ble_socket); + return std::make_unique(ble_socket); } -Exception BleV2ServerSocket::Close() { +Exception BleServerSocket::Close() { // TODO(b/271031645): implement BLE socket using weave absl::MutexLock lock(&mutex_); VLOG(1) << __func__ << ": Close is called."; @@ -67,7 +67,7 @@ Exception BleV2ServerSocket::Close() { return {Exception::kSuccess}; } -bool BleV2ServerSocket::Bind() { +bool BleServerSocket::Bind() { // TODO(b/271031645): implement BLE socket using weave LOG(ERROR) << __func__ << ": GATT socket started."; return true; diff --git a/internal/platform/implementation/windows/ble_v2_server_socket.h b/internal/platform/implementation/windows/ble_server_socket.h similarity index 72% rename from internal/platform/implementation/windows/ble_v2_server_socket.h rename to internal/platform/implementation/windows/ble_server_socket.h index 683e2cf9..b9e88bc3 100644 --- a/internal/platform/implementation/windows/ble_v2_server_socket.h +++ b/internal/platform/implementation/windows/ble_server_socket.h @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_ -#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_ +#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SERVER_SOCKET_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SERVER_SOCKET_H_ #include #include @@ -21,19 +21,19 @@ #include "absl/synchronization/mutex.h" #include "absl/synchronization/notification.h" -#include "internal/platform/implementation/ble_v2.h" -#include "internal/platform/implementation/windows/ble_v2_socket.h" +#include "internal/platform/implementation/ble.h" +#include "internal/platform/implementation/windows/ble_socket.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" namespace nearby { namespace windows { -class BleV2ServerSocket : public api::ble_v2::BleServerSocket { +class BleServerSocket : public api::ble::BleServerSocket { public: - explicit BleV2ServerSocket(api::BluetoothAdapter* adapter); - ~BleV2ServerSocket() override = default; + explicit BleServerSocket(api::BluetoothAdapter* adapter); + ~BleServerSocket() override = default; - std::unique_ptr<::nearby::api::ble_v2::BleSocket> Accept() override; + std::unique_ptr<::nearby::api::ble::BleSocket> Accept() override; Exception Close() override; @@ -47,10 +47,10 @@ class BleV2ServerSocket : public api::ble_v2::BleServerSocket { absl::Mutex mutex_; absl::CondVar cond_; bool closed_ = false; - std::deque pending_sockets_ ABSL_GUARDED_BY(mutex_); + std::deque pending_sockets_ ABSL_GUARDED_BY(mutex_); }; } // namespace windows } // namespace nearby -#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_ +#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SERVER_SOCKET_H_ diff --git a/internal/platform/implementation/windows/ble_v2_socket.cc b/internal/platform/implementation/windows/ble_socket.cc similarity index 74% rename from internal/platform/implementation/windows/ble_v2_socket.cc rename to internal/platform/implementation/windows/ble_socket.cc index dc640bac..587f2e11 100644 --- a/internal/platform/implementation/windows/ble_v2_socket.cc +++ b/internal/platform/implementation/windows/ble_socket.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/platform/implementation/windows/ble_v2_socket.h" +#include "internal/platform/implementation/windows/ble_socket.h" #include @@ -25,43 +25,43 @@ namespace nearby { namespace windows { -InputStream& BleV2Socket::GetInputStream() { return input_stream_; } +InputStream& BleSocket::GetInputStream() { return input_stream_; } -OutputStream& BleV2Socket::GetOutputStream() { return output_stream_; } +OutputStream& BleSocket::GetOutputStream() { return output_stream_; } -Exception BleV2Socket::Close() { return {Exception::kSuccess}; } +Exception BleSocket::Close() { return {Exception::kSuccess}; } -bool BleV2Socket::Connect() { +bool BleSocket::Connect() { // TODO(b/271031645): implement BLE socket using weave VLOG(1) << __func__ << ": Connect to BLE peripheral"; return false; } -ExceptionOr BleV2Socket::BleInputStream::Read(std::int64_t size) { +ExceptionOr BleSocket::BleInputStream::Read(std::int64_t size) { // TODO(b/271031645): implement BLE socket using weave VLOG(1) << __func__ << ": Read data size=" << size; return ExceptionOr(Exception::kIo); } -Exception BleV2Socket::BleInputStream::Close() { +Exception BleSocket::BleInputStream::Close() { // TODO(b/271031645): implement BLE socket using weave VLOG(1) << __func__ << ": Close BLE input stream."; return {Exception::kSuccess}; } -Exception BleV2Socket::BleOutputStream::Write(const ByteArray& data) { +Exception BleSocket::BleOutputStream::Write(const ByteArray& data) { // TODO(b/271031645): implement BLE socket using weave VLOG(1) << __func__ << ": Write data size=" << data.size(); return {Exception::kIo}; } -Exception BleV2Socket::BleOutputStream::Flush() { +Exception BleSocket::BleOutputStream::Flush() { // TODO(b/271031645): implement BLE socket using weave LOG(INFO) << __func__ << ": Flush is called."; return {Exception::kSuccess}; } -Exception BleV2Socket::BleOutputStream::Close() { +Exception BleSocket::BleOutputStream::Close() { // TODO(b/271031645): implement BLE socket using weave LOG(INFO) << __func__ << ": close is called."; return {Exception::kSuccess}; diff --git a/internal/platform/implementation/windows/ble_v2_socket.h b/internal/platform/implementation/windows/ble_socket.h similarity index 84% rename from internal/platform/implementation/windows/ble_v2_socket.h rename to internal/platform/implementation/windows/ble_socket.h index 0a19af88..5397e5e1 100644 --- a/internal/platform/implementation/windows/ble_v2_socket.h +++ b/internal/platform/implementation/windows/ble_socket.h @@ -12,24 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_ -#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_ +#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SOCKET_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SOCKET_H_ #include #include "internal/platform/byte_array.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/input_stream.h" #include "internal/platform/output_stream.h" namespace nearby { namespace windows { -class BleV2Socket : public api::ble_v2::BleSocket { +class BleSocket : public api::ble::BleSocket { public: - BleV2Socket() = default; - ~BleV2Socket() override = default; + BleSocket() = default; + ~BleSocket() override = default; InputStream& GetInputStream() override; @@ -37,7 +37,7 @@ class BleV2Socket : public api::ble_v2::BleSocket { Exception Close() override; - api::ble_v2::BlePeripheral::UniqueId GetRemotePeripheralId() override { + api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override { return 0LL; }; @@ -67,4 +67,4 @@ class BleV2Socket : public api::ble_v2::BleSocket { } // namespace windows } // namespace nearby -#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_ +#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_SOCKET_H_ diff --git a/internal/platform/implementation/windows/ble_v2_test.cc b/internal/platform/implementation/windows/ble_test.cc similarity index 56% rename from internal/platform/implementation/windows/ble_v2_test.cc rename to internal/platform/implementation/windows/ble_test.cc index 4073a3b7..acf3b734 100644 --- a/internal/platform/implementation/windows/ble_v2_test.cc +++ b/internal/platform/implementation/windows/ble_test.cc @@ -12,12 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "internal/platform/implementation/windows/ble_v2.h" +#include "internal/platform/implementation/windows/ble.h" #include #include -#include #include +#include #include #include "gmock/gmock.h" @@ -26,7 +26,7 @@ #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "absl/synchronization/notification.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/uuid.h" @@ -55,130 +55,132 @@ class MockBluetoothAdapter : public BluetoothAdapter { } // namespace -class BleV2MediumTest : public ::testing::Test { +class BleMediumTest : public ::testing::Test { protected: - BleV2MediumTest() { - blev2_medium_ = std::make_unique(bluetooth_adapter_); + BleMediumTest() { + ble_medium_ = std::make_unique(bluetooth_adapter_); } protected: size_t GetWatchersCount() { - absl::MutexLock lock(&blev2_medium_->mutex_); - return blev2_medium_->watchers_.size(); + absl::MutexLock lock(&ble_medium_->mutex_); + return ble_medium_->watchers_.size(); } MockBluetoothAdapter bluetooth_adapter_; - std::unique_ptr blev2_medium_; + std::unique_ptr ble_medium_; }; namespace { -TEST_F(BleV2MediumTest, StartScanningAdapterDisabled) { +TEST_F(BleMediumTest, StartScanningAdapterDisabled) { EXPECT_CALL(bluetooth_adapter_, IsEnabled()).WillOnce(Return(false)); Uuid service_uuid = kTestServiceUuid; - api::ble_v2::BleMedium::ScanCallback callback; + api::ble::BleMedium::ScanCallback callback; - EXPECT_EQ(false, blev2_medium_->StartScanning( - service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback))); + EXPECT_EQ(false, ble_medium_->StartScanning(service_uuid, + api::ble::TxPowerLevel::kHigh, + std::move(callback))); EXPECT_EQ(GetWatchersCount(), 0); } -TEST_F(BleV2MediumTest, StartScanningAdapterNoAdapterOnForge) { +TEST_F(BleMediumTest, StartScanningAdapterNoAdapterOnForge) { EXPECT_CALL(bluetooth_adapter_, IsEnabled()).WillOnce(Return(true)); Uuid service_uuid = kTestServiceUuid; - api::ble_v2::BleMedium::ScanCallback callback; + api::ble::BleMedium::ScanCallback callback; - EXPECT_EQ(false, blev2_medium_->StartScanning( - service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback))); + EXPECT_EQ(false, ble_medium_->StartScanning(service_uuid, + api::ble::TxPowerLevel::kHigh, + std::move(callback))); EXPECT_EQ(GetWatchersCount(), 0); } -TEST(BleV2Medium, DISABLED_StartAdvertising) { +TEST(BleMedium, DISABLED_StartAdvertising) { BluetoothAdapter bluetoothAdapter; - BleV2Medium blev2_medium(bluetoothAdapter); + BleMedium ble_medium(bluetoothAdapter); - api::ble_v2::BleAdvertisementData advertising_data; - api::ble_v2::BleAdvertisementData scan_response_data; + api::ble::BleAdvertisementData advertising_data; + api::ble::BleAdvertisementData scan_response_data; - EXPECT_TRUE(blev2_medium.StartAdvertising( - advertising_data, {.tx_power_level = api::ble_v2::TxPowerLevel::kHigh, + EXPECT_TRUE(ble_medium.StartAdvertising( + advertising_data, {.tx_power_level = api::ble::TxPowerLevel::kHigh, .is_connectable = true})); } -TEST(BleV2Medium, DISABLED_StopAdvertising) { +TEST(BleMedium, DISABLED_StopAdvertising) { BluetoothAdapter bluetoothAdapter; - BleV2Medium blev2_medium(bluetoothAdapter); + BleMedium ble_medium(bluetoothAdapter); - api::ble_v2::BleAdvertisementData advertising_data; + api::ble::BleAdvertisementData advertising_data; - EXPECT_TRUE(blev2_medium.StartAdvertising( - advertising_data, {.tx_power_level = api::ble_v2::TxPowerLevel::kHigh, + EXPECT_TRUE(ble_medium.StartAdvertising( + advertising_data, {.tx_power_level = api::ble::TxPowerLevel::kHigh, .is_connectable = true})); - EXPECT_TRUE(blev2_medium.StopAdvertising()); + EXPECT_TRUE(ble_medium.StopAdvertising()); } -TEST(BleV2Medium, DISABLED_StartScanning) { +TEST(BleMedium, DISABLED_StartScanning) { bool scan_response_received = false; absl::Notification scan_response_notification; BluetoothAdapter bluetoothAdapter; - BleV2Medium blev2_medium(bluetoothAdapter); + BleMedium ble_medium(bluetoothAdapter); Uuid service_uuid; - api::ble_v2::BleMedium::ScanCallback callback; + api::ble::BleMedium::ScanCallback callback; callback.advertisement_found_cb = [&scan_response_received, &scan_response_notification]( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - const api::ble_v2::BleAdvertisementData& advertisement_data) { + api::ble::BlePeripheral::UniqueId peripheral_id, + const api::ble::BleAdvertisementData& advertisement_data) { scan_response_received = true; scan_response_notification.Notify(); }; - EXPECT_TRUE(blev2_medium.StartScanning( - service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback))); + EXPECT_TRUE(ble_medium.StartScanning( + service_uuid, api::ble::TxPowerLevel::kHigh, std::move(callback))); EXPECT_TRUE(scan_response_notification.WaitForNotificationWithTimeout( absl::Seconds(5))); EXPECT_TRUE(scan_response_received); } -TEST(BleV2Medium, DISABLED_StopScanning) { +TEST(BleMedium, DISABLED_StopScanning) { BluetoothAdapter bluetoothAdapter; - BleV2Medium blev2_medium(bluetoothAdapter); + BleMedium ble_medium(bluetoothAdapter); Uuid service_uuid; - api::ble_v2::BleMedium::ScanCallback callback; + api::ble::BleMedium::ScanCallback callback; callback.advertisement_found_cb = - [](api::ble_v2::BlePeripheral::UniqueId peripheral_id, - const api::ble_v2::BleAdvertisementData& advertisement_data) {}; + [](api::ble::BlePeripheral::UniqueId peripheral_id, + const api::ble::BleAdvertisementData& advertisement_data) {}; - EXPECT_TRUE(blev2_medium.StartScanning( - service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback))); + EXPECT_TRUE(ble_medium.StartScanning( + service_uuid, api::ble::TxPowerLevel::kHigh, std::move(callback))); - EXPECT_TRUE(blev2_medium.StopScanning()); + EXPECT_TRUE(ble_medium.StopScanning()); } -TEST(BleV2Medium, DISABLED_StartThenStopScanning) { +TEST(BleMedium, DISABLED_StartThenStopScanning) { bool scan_response_received = false; absl::Notification scan_response_notification; BluetoothAdapter bluetoothAdapter; - BleV2Medium blev2_medium(bluetoothAdapter); + BleMedium ble_medium(bluetoothAdapter); Uuid service_uuid; - api::ble_v2::BleMedium::ScanningCallback callback; + api::ble::BleMedium::ScanningCallback callback; callback.advertisement_found_cb = [&scan_response_received, &scan_response_notification]( - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - const api::ble_v2::BleAdvertisementData& advertisement_data) { + api::ble::BlePeripheral::UniqueId peripheral_id, + const api::ble::BleAdvertisementData& advertisement_data) { scan_response_received = true; scan_response_notification.Notify(); }; - auto scanning_session = blev2_medium.StartScanning( - service_uuid, api::ble_v2::TxPowerLevel::kHigh, std::move(callback)); + auto scanning_session = ble_medium.StartScanning( + service_uuid, api::ble::TxPowerLevel::kHigh, std::move(callback)); EXPECT_NE(scanning_session, nullptr); EXPECT_TRUE(scanning_session->stop_scanning().ok()); } diff --git a/internal/platform/implementation/windows/platform.cc b/internal/platform/implementation/windows/platform.cc index 947c2fdc..c8e4ff20 100644 --- a/internal/platform/implementation/windows/platform.cc +++ b/internal/platform/implementation/windows/platform.cc @@ -36,12 +36,12 @@ #undef StrCat // Remove the Windows macro definition #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" -#include "internal/base/files.h" #include "internal/base/file_path.h" +#include "internal/base/files.h" #include "internal/platform/implementation/atomic_boolean.h" #include "internal/platform/implementation/atomic_reference.h" #include "internal/platform/implementation/awdl.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/condition_variable.h" @@ -59,7 +59,7 @@ #include "internal/platform/implementation/wifi_lan.h" #include "internal/platform/implementation/windows/atomic_boolean.h" #include "internal/platform/implementation/windows/atomic_reference.h" -#include "internal/platform/implementation/windows/ble_v2.h" +#include "internal/platform/implementation/windows/ble.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/implementation/windows/bluetooth_classic_medium.h" #include "internal/platform/implementation/windows/condition_variable.h" @@ -158,8 +158,7 @@ std::string ImplementationPlatform::GetAppDataPath( std::string app_data_path_utf8 = windows::string_utils::WideStringToString(app_data_path); std::replace(app_data_path_utf8.begin(), app_data_path_utf8.end(), '\\', '/'); - return absl::StrCat(app_data_path_utf8, "/", kNCRelativePath, "/", - file_name); + return absl::StrCat(app_data_path_utf8, "/", kNCRelativePath, "/", file_name); } OSName ImplementationPlatform::GetCurrentOS() { return OSName::kWindows; } @@ -220,8 +219,7 @@ std::unique_ptr ImplementationPlatform::CreateOutputFile( // Verifies that a path is a valid directory. if (!Files::DirectoryExists(folder_path)) { if (!Files::CreateDirectories(folder_path)) { - LOG(ERROR) << "Failed to create directory: " - << folder_path.ToString(); + LOG(ERROR) << "Failed to create directory: " << folder_path.ToString(); return nullptr; } } @@ -256,9 +254,9 @@ ImplementationPlatform::CreateBluetoothClassicMedium( } // TODO(b/184975123): replace with real implementation. -std::unique_ptr -ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter& adapter) { - return std::make_unique(adapter); +std::unique_ptr ImplementationPlatform::CreateBleMedium( + api::BluetoothAdapter& adapter) { + return std::make_unique(adapter); } std::unique_ptr diff --git a/internal/platform/medium_environment.cc b/internal/platform/medium_environment.cc index 008cde68..581ff972 100644 --- a/internal/platform/medium_environment.cc +++ b/internal/platform/medium_environment.cc @@ -33,7 +33,7 @@ #include "internal/platform/count_down_latch.h" #include "internal/platform/feature_flags.h" #include "internal/platform/implementation/awdl.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/wifi_direct.h" @@ -87,7 +87,7 @@ void MediumEnvironment::Reset() { LOG(INFO) << "MediumEnvironment::Reset()"; bluetooth_adapters_.clear(); bluetooth_mediums_.clear(); - ble_v2_mediums_.clear(); + ble_mediums_.clear(); #ifndef NO_WEBRTC webrtc_signaling_message_callback_.clear(); webrtc_signaling_complete_callback_.clear(); @@ -243,13 +243,13 @@ api::BluetoothDevice* MediumEnvironment::FindBluetoothDevice( return device; } -api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium( - api::ble_v2::BlePeripheral::UniqueId id) { - api::ble_v2::BleMedium* device = nullptr; +api::ble::BleMedium* MediumEnvironment::FindBleMedium( + api::ble::BlePeripheral::UniqueId id) { + api::ble::BleMedium* device = nullptr; CountDownLatch latch(1); - LOG(INFO) << "FindBleV2Medium " << id; + LOG(INFO) << "FindBleMedium " << id; RunOnMediumEnvironmentThread([&]() { - for (auto& item : ble_v2_mediums_) { + for (auto& item : ble_mediums_) { auto* medium = item.first; if (item.second.ble_peripheral_id == id) { device = medium; @@ -260,15 +260,15 @@ api::ble_v2::BleMedium* MediumEnvironment::FindBleV2Medium( }); latch.Await(); if (device == nullptr) { - LOG(INFO) << "FindBleV2Medium, not found: " << id; + LOG(INFO) << "FindBleMedium, not found: " << id; } return device; } -void MediumEnvironment::OnBleV2PeripheralStateChanged( - bool enabled, BleV2MediumContext& context, const Uuid& service_id, - const api::ble_v2::BleAdvertisementData& ble_advertisement_data, - api::ble_v2::BlePeripheral::UniqueId peripheral_id) { +void MediumEnvironment::OnBlePeripheralStateChanged( + bool enabled, BleMediumContext& context, const Uuid& service_id, + const api::ble::BleAdvertisementData& ble_advertisement_data, + api::ble::BlePeripheral::UniqueId peripheral_id) { if (!enabled_) return; LOG(INFO) << "OnBleServiceStateChanged [peripheral id=" << peripheral_id << "]; medium_context=" << &context @@ -473,28 +473,28 @@ void MediumEnvironment::UnregisterBluetoothMedium( latch.Await(); } -void MediumEnvironment::RegisterBleV2Medium( - api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral::UniqueId peripheral_id) { +void MediumEnvironment::RegisterBleMedium( + api::ble::BleMedium& medium, + api::ble::BlePeripheral::UniqueId peripheral_id) { if (!enabled_) return; RunOnMediumEnvironmentThread([this, &medium, peripheral_id]() { - ble_v2_mediums_.insert( - {&medium, BleV2MediumContext{.ble_peripheral_id = peripheral_id}}); - LOG(INFO) << "Registered: BLE V2 medium:" << &medium; + ble_mediums_.insert( + {&medium, BleMediumContext{.ble_peripheral_id = peripheral_id}}); + LOG(INFO) << "Registered: BLE medium:" << &medium; }); } -void MediumEnvironment::UpdateBleV2MediumForAdvertising( - bool enabled, api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - const api::ble_v2::BleAdvertisementData& advertisement_data) { +void MediumEnvironment::UpdateBleMediumForAdvertising( + bool enabled, api::ble::BleMedium& medium, + api::ble::BlePeripheral::UniqueId peripheral_id, + const api::ble::BleAdvertisementData& advertisement_data) { if (!enabled_) return; RunOnMediumEnvironmentThread([this, &medium, peripheral_id, advertisement_data = advertisement_data, enabled]() { - auto it = ble_v2_mediums_.find(&medium); - if (it == ble_v2_mediums_.end()) { - LOG(INFO) << "UpdateBleV2MediumForAdvertising failed. There is no " + auto it = ble_mediums_.find(&medium); + if (it == ble_mediums_.end()) { + LOG(INFO) << "UpdateBleMediumForAdvertising failed. There is no " "medium registered."; return; } @@ -503,13 +503,13 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising( context.advertising = enabled; context.advertisement_data = advertisement_data; - LOG(INFO) << "UpdateBleV2MediumForAdvertising: this=" << this + LOG(INFO) << "UpdateBleMediumForAdvertising: this=" << this << ", medium=" << &medium << ", medium_context=" << &context << ", peripheral id=" << peripheral_id << ", enabled=" << enabled; - for (auto& medium_info : ble_v2_mediums_) { - const api::ble_v2::BleMedium* remote_medium = medium_info.first; - BleV2MediumContext& remote_context = medium_info.second; + for (auto& medium_info : ble_mediums_) { + const api::ble::BleMedium* remote_medium = medium_info.first; + BleMediumContext& remote_context = medium_info.second; // Do not send notification to the same medium. if (remote_medium == &medium) continue; @@ -531,13 +531,13 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising( if (it == context.advertisement_data.service_data.end() && enabled) continue; - LOG(INFO) << "UpdateBleV2MediumForAdvertising, found other medium=" + LOG(INFO) << "UpdateBleMediumForAdvertising, found other medium=" << remote_medium << ", remote_medium_context=" << &remote_context << ", remote_context.peripheral=" << remote_context.ble_peripheral_id - << ". Ready to call OnBleV2PeripheralStateChanged."; - OnBleV2PeripheralStateChanged( + << ". Ready to call OnBlePeripheralStateChanged."; + OnBlePeripheralStateChanged( enabled, remote_context, remote_scanning_service_uuid, context.advertisement_data, context.ble_peripheral_id); } @@ -545,24 +545,24 @@ void MediumEnvironment::UpdateBleV2MediumForAdvertising( }); } -void MediumEnvironment::UpdateBleV2MediumForScanning( +void MediumEnvironment::UpdateBleMediumForScanning( bool enabled, const Uuid& scanning_service_uuid, std::uint32_t internal_session_id, BleScanCallback callback, - api::ble_v2::BleMedium& medium) { + api::ble::BleMedium& medium) { if (!enabled_) return; RunOnMediumEnvironmentThread([this, &medium, scanning_service_uuid = scanning_service_uuid, internal_session_id = internal_session_id, callback = std::move(callback), enabled]() mutable { - auto it = ble_v2_mediums_.find(&medium); - if (it == ble_v2_mediums_.end()) { - LOG(INFO) << "UpdateBleV2MediumForScanning failed. There is no medium " + auto it = ble_mediums_.find(&medium); + if (it == ble_mediums_.end()) { + LOG(INFO) << "UpdateBleMediumForScanning failed. There is no medium " "registered."; return; } - BleV2MediumContext& context = it->second; - LOG(INFO) << "UpdateBleV2MediumForScanning: this=" << this + BleMediumContext& context = it->second; + LOG(INFO) << "UpdateBleMediumForScanning: this=" << this << ", medium=" << &medium << ", medium_context=" << &context << ", enabled=" << enabled; if (enabled) { @@ -574,9 +574,9 @@ void MediumEnvironment::UpdateBleV2MediumForScanning( for (auto& element : context.scan_callback_map) { scanning_service_uuids.insert(element.first.first); } - for (const auto& medium_info : ble_v2_mediums_) { - const api::ble_v2::BleMedium* remote_medium = medium_info.first; - const BleV2MediumContext& remote_context = medium_info.second; + for (const auto& medium_info : ble_mediums_) { + const api::ble::BleMedium* remote_medium = medium_info.first; + const BleMediumContext& remote_context = medium_info.second; // Do not send notification to the same or the non-advertising // medium. if (remote_medium == &medium || !remote_context.advertising) continue; @@ -585,15 +585,15 @@ void MediumEnvironment::UpdateBleV2MediumForScanning( scanning_service_uuid); if (it == remote_context.advertisement_data.service_data.end()) continue; - LOG(INFO) << "UpdateBleV2MediumForScanning, found other medium=" + LOG(INFO) << "UpdateBleMediumForScanning, found other medium=" << remote_medium << ", remote_medium_context=" << &remote_context << ", scanning_service_uuid=" << scanning_service_uuid.Get16BitAsString() - << ". Ready to call OnBleV2PeripheralStateChanged."; - OnBleV2PeripheralStateChanged(enabled, context, scanning_service_uuid, - remote_context.advertisement_data, - remote_context.ble_peripheral_id); + << ". Ready to call OnBlePeripheralStateChanged."; + OnBlePeripheralStateChanged(enabled, context, scanning_service_uuid, + remote_context.advertisement_data, + remote_context.ble_peripheral_id); } } } else { @@ -606,32 +606,32 @@ void MediumEnvironment::UpdateBleV2MediumForScanning( }); } -void MediumEnvironment::UnregisterBleV2Medium(api::ble_v2::BleMedium& medium) { +void MediumEnvironment::UnregisterBleMedium(api::ble::BleMedium& medium) { if (!enabled_) return; RunOnMediumEnvironmentThread([this, &medium]() { - auto item = ble_v2_mediums_.extract(&medium); + auto item = ble_mediums_.extract(&medium); if (item.empty()) return; - LOG(INFO) << "Unregistered BLE V2 medium:" << &medium; + LOG(INFO) << "Unregistered BLE medium:" << &medium; }); } -std::optional -MediumEnvironment::GetBleV2MediumStatus(const api::ble_v2::BleMedium& medium) { +std::optional +MediumEnvironment::GetBleMediumStatus(const api::ble::BleMedium& medium) { if (!enabled_) return std::nullopt; - std::optional result; + std::optional result; CountDownLatch latch(1); RunOnMediumEnvironmentThread([this, &medium, &latch, &result]() { - auto it = ble_v2_mediums_.find(&medium); - if (it == ble_v2_mediums_.end()) { + auto it = ble_mediums_.find(&medium); + if (it == ble_mediums_.end()) { result = std::nullopt; latch.CountDown(); return; } - BleV2MediumContext& context = it->second; + BleMediumContext& context = it->second; - result = BleV2MediumStatus{.is_advertising = context.advertising, - .is_scanning = context.scanning}; + result = BleMediumStatus{.is_advertising = context.advertising, + .is_scanning = context.scanning}; latch.CountDown(); }); latch.Await(); @@ -751,8 +751,7 @@ void MediumEnvironment::RegisterAwdlMedium(api::AwdlMedium& medium) { void MediumEnvironment::UpdateWifiLanMediumForAdvertising( api::WifiLanMedium& medium, const NsdServiceInfo& service_info, - const std::string& ip_address, - bool enabled) { + const std::string& ip_address, bool enabled) { if (!enabled_) return; RunOnMediumEnvironmentThread([this, &medium, service_info = service_info, ip_address, enabled]() mutable { @@ -1126,13 +1125,13 @@ std::optional MediumEnvironment::GetSimulatedClock() { } void MediumEnvironment::RegisterGattServer( - api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - Borrowable gatt_server) { + api::ble::BleMedium& medium, + api::ble::BlePeripheral::UniqueId peripheral_id, + Borrowable gatt_server) { if (!enabled_) return; RunOnMediumEnvironmentThread([this, &medium, peripheral_id, gatt_server]() { - auto it = ble_v2_mediums_.find(&medium); - if (it == ble_v2_mediums_.end()) { + auto it = ble_mediums_.find(&medium); + if (it == ble_mediums_.end()) { LOG(WARNING) << "Register GattServer failed. There is no medium" " registered."; return; @@ -1140,19 +1139,19 @@ void MediumEnvironment::RegisterGattServer( auto& context = it->second; CHECK_EQ(context.gatt_server, nullptr); context.gatt_server = - std::make_unique>(gatt_server); + std::make_unique>(gatt_server); context.ble_peripheral_id = peripheral_id; LOG(INFO) << "Registered: GattServer for peripheral id:" << peripheral_id << " on medium:" << &medium; }); } -void MediumEnvironment::UnregisterGattServer(api::ble_v2::BleMedium& medium) { +void MediumEnvironment::UnregisterGattServer(api::ble::BleMedium& medium) { if (!enabled_) return; CountDownLatch latch(1); RunOnMediumEnvironmentThread([&]() { - auto it = ble_v2_mediums_.find(&medium); - if (it == ble_v2_mediums_.end()) { + auto it = ble_mediums_.find(&medium); + if (it == ble_mediums_.end()) { LOG(INFO) << "Unregister GattServer failed. There is no " "medium registered on medium:" << &medium; @@ -1169,14 +1168,14 @@ void MediumEnvironment::UnregisterGattServer(api::ble_v2::BleMedium& medium) { latch.Await(); } -Borrowable MediumEnvironment::GetGattServer( - api::ble_v2::BlePeripheral::UniqueId peripheral_id) { - Borrowable result; +Borrowable MediumEnvironment::GetGattServer( + api::ble::BlePeripheral::UniqueId peripheral_id) { + Borrowable result; bool found_server = false; CountDownLatch latch(1); RunOnMediumEnvironmentThread([&]() { - for (const auto& medium_info : ble_v2_mediums_) { - const BleV2MediumContext& remote_context = medium_info.second; + for (const auto& medium_info : ble_mediums_) { + const BleMediumContext& remote_context = medium_info.second; if (remote_context.gatt_server != nullptr && remote_context.ble_peripheral_id == peripheral_id) { if (remote_context.gatt_server == nullptr) { diff --git a/internal/platform/medium_environment.h b/internal/platform/medium_environment.h index 857b414f..d64e795f 100644 --- a/internal/platform/medium_environment.h +++ b/internal/platform/medium_environment.h @@ -29,7 +29,7 @@ #include "internal/base/observer_list.h" #include "internal/platform/borrowable.h" #include "internal/platform/implementation/awdl.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/bluetooth_adapter.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/mac_address.h" @@ -74,7 +74,7 @@ class MediumEnvironment { public: using BluetoothDiscoveryCallback = api::BluetoothClassicMedium::DiscoveryCallback; - using BleScanCallback = api::ble_v2::BleMedium::ScanningCallback; + using BleScanCallback = api::ble::BleMedium::ScanningCallback; #ifndef NO_WEBRTC using OnSignalingMessageCallback = api::WebRtcSignalingMessenger::OnSignalingMessageCallback; @@ -86,7 +86,7 @@ class MediumEnvironment { using AwdlDiscoveredServiceCallback = api::AwdlMedium::DiscoveredServiceCallback; - struct BleV2MediumStatus { + struct BleMediumStatus { bool is_advertising; bool is_scanning; }; @@ -193,15 +193,15 @@ class MediumEnvironment { // expects they should communicate. // The registered `medium` must refer to a valid instance that outlives this // object. - void RegisterBleV2Medium(api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral::UniqueId peripheral_id); + void RegisterBleMedium(api::ble::BleMedium& medium, + api::ble::BlePeripheral::UniqueId peripheral_id); // Updates advertising info to indicate the current medium is exposing // advertising event. - void UpdateBleV2MediumForAdvertising( - bool enabled, api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - const api::ble_v2::BleAdvertisementData& advertisement_data); + void UpdateBleMediumForAdvertising( + bool enabled, api::ble::BleMedium& medium, + api::ble::BlePeripheral::UniqueId peripheral_id, + const api::ble::BleAdvertisementData& advertisement_data); // Updates discovery callback info to allow for dispatch of discovery events. // @@ -211,19 +211,19 @@ class MediumEnvironment { // This should be called when discoverable state changes. // The `callback` argument should be non-empty if `enabled` is true or empty // if `enabled` is false. - void UpdateBleV2MediumForScanning(bool enabled, - const Uuid& scanning_service_uuid, - std::uint32_t internal_session_id, - BleScanCallback callback, - api::ble_v2::BleMedium& medium); + void UpdateBleMediumForScanning(bool enabled, + const Uuid& scanning_service_uuid, + std::uint32_t internal_session_id, + BleScanCallback callback, + api::ble::BleMedium& medium); // Removes medium-related info. This should correspond to device power off. - void UnregisterBleV2Medium(api::ble_v2::BleMedium& mediumum); + void UnregisterBleMedium(api::ble::BleMedium& mediumum); // Collects the status for the given BleMedium. Mainly used in unit tests // to verify if the BleMedum is in expected status after operations. - std::optional GetBleV2MediumStatus( - const api::ble_v2::BleMedium& medium); + std::optional GetBleMediumStatus( + const api::ble::BleMedium& medium); // Adds medium-related info to allow for discovery/advertising to work. // This provides access to this medium from other mediums, when protocol @@ -332,16 +332,15 @@ class MediumEnvironment { std::optional GetSimulatedClock(); - api::ble_v2::BleMedium* FindBleV2Medium( - api::ble_v2::BlePeripheral::UniqueId id); + api::ble::BleMedium* FindBleMedium(api::ble::BlePeripheral::UniqueId id); - void RegisterGattServer(api::ble_v2::BleMedium& medium, - api::ble_v2::BlePeripheral::UniqueId peripheral_id, - Borrowable gatt_server); - void UnregisterGattServer(api::ble_v2::BleMedium& medium); + void RegisterGattServer(api::ble::BleMedium& medium, + api::ble::BlePeripheral::UniqueId peripheral_id, + Borrowable gatt_server); + void UnregisterGattServer(api::ble::BleMedium& medium); - Borrowable GetGattServer( - api::ble_v2::BlePeripheral::UniqueId peripheral_id); + Borrowable GetGattServer( + api::ble::BlePeripheral::UniqueId peripheral_id); // Configures the BluetoothPairingContext for remote BluetoothDevice. void ConfigBluetoothPairingContext(api::BluetoothDevice* device, @@ -388,15 +387,15 @@ class MediumEnvironment { absl::flat_hash_map devices; }; - struct BleV2MediumContext { + struct BleMediumContext { absl::flat_hash_map, BleScanCallback> scan_callback_map; // using the same ble peripheral for different advertisement. - api::ble_v2::BlePeripheral::UniqueId ble_peripheral_id; - api::ble_v2::BleAdvertisementData advertisement_data; + api::ble::BlePeripheral::UniqueId ble_peripheral_id; + api::ble::BleAdvertisementData advertisement_data; bool advertising = false; bool scanning = false; - std::unique_ptr> gatt_server = nullptr; + std::unique_ptr> gatt_server = nullptr; }; struct WifiLanMediumContext { @@ -455,10 +454,10 @@ class MediumEnvironment { api::BluetoothAdapter::ScanMode mode, bool enabled); - void OnBleV2PeripheralStateChanged( - bool enabled, BleV2MediumContext& context, const Uuid& service_id, - const api::ble_v2::BleAdvertisementData& ble_advertisement_data, - api::ble_v2::BlePeripheral::UniqueId peripheral_id); + void OnBlePeripheralStateChanged( + bool enabled, BleMediumContext& context, const Uuid& service_id, + const api::ble::BleAdvertisementData& ble_advertisement_data, + api::ble::BlePeripheral::UniqueId peripheral_id); void OnWifiLanServiceStateChanged(WifiLanMediumContext& info, const NsdServiceInfo& service_info, @@ -483,8 +482,7 @@ class MediumEnvironment { absl::flat_hash_map bluetooth_mediums_; - absl::flat_hash_map - ble_v2_mediums_; + absl::flat_hash_map ble_mediums_; absl::flat_hash_map devices_pairing_contexts_; #ifndef NO_WEBRTC diff --git a/presence/implementation/broadcast_manager.cc b/presence/implementation/broadcast_manager.cc index 460875b0..088083b3 100644 --- a/presence/implementation/broadcast_manager.cc +++ b/presence/implementation/broadcast_manager.cc @@ -26,7 +26,7 @@ #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/credential_callbacks.h" #include "internal/platform/implementation/crypto.h" #include "internal/platform/logging.h" @@ -40,9 +40,8 @@ namespace nearby { namespace presence { namespace { -using AdvertisingCallback = - ::nearby::api::ble_v2::BleMedium::AdvertisingCallback; -using AdvertisingSession = ::nearby::api::ble_v2::BleMedium::AdvertisingSession; +using AdvertisingCallback = ::nearby::api::ble::BleMedium::AdvertisingCallback; +using AdvertisingSession = ::nearby::api::ble::BleMedium::AdvertisingSession; using LocalCredential = internal::LocalCredential; uint16_t SaltToInt(absl::string_view salt) { diff --git a/presence/implementation/broadcast_manager.h b/presence/implementation/broadcast_manager.h index 7d274907..aedf2fec 100644 --- a/presence/implementation/broadcast_manager.h +++ b/presence/implementation/broadcast_manager.h @@ -26,7 +26,7 @@ #include "absl/status/statusor.h" #include "absl/strings/string_view.h" #include "absl/types/optional.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/runnable.h" #include "internal/platform/single_thread_executor.h" #include "presence/broadcast_request.h" @@ -45,8 +45,7 @@ namespace presence { class BroadcastManager { public: using SingleThreadExecutor = ::nearby::SingleThreadExecutor; - using AdvertisingSession = - ::nearby::api::ble_v2::BleMedium::AdvertisingSession; + using AdvertisingSession = ::nearby::api::ble::BleMedium::AdvertisingSession; using Runnable = ::nearby::Runnable; using LocalCredential = internal::LocalCredential; BroadcastManager(Mediums& mediums, CredentialManager& credential_manager, @@ -89,14 +88,14 @@ class BroadcastManager { void FetchCredentials(BroadcastSessionId id, BaseBroadcastRequest broadcast_request) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_); - absl::optional SelectCredential( //NOLINT + absl::optional SelectCredential( // NOLINT BaseBroadcastRequest& broadcast_request, std::vector credentials); // Returns the private credential, if any, selected to generate the // advertisement. A salt used in the advertisement is added to the returned // private credential. The caller must save it in the storage. - absl::optional Advertise( //NOLINT + absl::optional Advertise( // NOLINT BroadcastSessionId id, BaseBroadcastRequest broadcast_request, std::vector credentials) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_); diff --git a/presence/implementation/broadcast_manager_test.cc b/presence/implementation/broadcast_manager_test.cc index 0e777768..32effaf9 100644 --- a/presence/implementation/broadcast_manager_test.cc +++ b/presence/implementation/broadcast_manager_test.cc @@ -74,7 +74,7 @@ class BroadcastManagerTest : public testing::TestWithParam { WaitForServiceControllerTasks(); MediumEnvironment::Instance().Sync(); return MediumEnvironment::Instance() - .GetBleV2MediumStatus(*mediums_.GetBle().GetImpl()) + .GetBleMediumStatus(*mediums_.GetBle().GetImpl()) ->is_advertising; } BroadcastCallback CreateBroadcastCallback() { diff --git a/presence/implementation/mediums/BUILD b/presence/implementation/mediums/BUILD index a635e913..80dc9214 100644 --- a/presence/implementation/mediums/BUILD +++ b/presence/implementation/mediums/BUILD @@ -30,8 +30,10 @@ cc_library( "//presence:__subpackages__", ], deps = [ + "//internal/platform:base", "//internal/platform:comm", "//internal/platform:uuid", + "//internal/platform/implementation:comm", "//presence:types", ], ) @@ -45,6 +47,7 @@ cc_test( shard_count = 16, deps = [ ":mediums", + "//internal/platform:base", "//internal/platform:comm", "//internal/platform:test_util", "//internal/platform:types", @@ -52,6 +55,9 @@ cc_test( "//internal/platform/implementation:comm", "//presence:types", "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_absl//absl/status", + "@com_google_absl//absl/time", + "@com_google_absl//absl/types:variant", "@com_google_googletest//:gtest_main", ] + select({ "@platforms//os:windows": [ diff --git a/presence/implementation/mediums/ble.h b/presence/implementation/mediums/ble.h index 7bd43c45..715c0b9b 100644 --- a/presence/implementation/mediums/ble.h +++ b/presence/implementation/mediums/ble.h @@ -16,11 +16,12 @@ #define THIRD_PARTY_NEARBY_PRESENCE_IMPLEMENTATION_MEDIUMS_BLE_H_ #include -#include #include -#include "internal/platform/ble_v2.h" +#include "internal/platform/ble.h" #include "internal/platform/bluetooth_adapter.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/uuid.h" #include "presence/implementation/mediums/advertisement_data.h" #include "presence/power_mode.h" @@ -34,23 +35,22 @@ ABSL_CONST_INIT const nearby::Uuid kPresenceServiceUuid(0x0000fcf100001000, 0x800000805f9b34fb); /* - * This Ble class utilizes platform/ble_v2 BleV2Medium, provides ble functions + * This Ble class utilizes platform/ble BleMedium, provides ble functions * for presence logic layer to invoke. * This class would have states like if ble is available or not, if it's doing * broadcast/scan. */ class Ble { public: - using TxPowerLevel = ::nearby::api::ble_v2::TxPowerLevel; - using ScanningSession = ::nearby::api::ble_v2::BleMedium::ScanningSession; - using ScanningCallback = ::nearby::api::ble_v2::BleMedium::ScanningCallback; - using AdvertiseParameters = ::nearby::api::ble_v2::AdvertiseParameters; - using AdvertisingSession = - ::nearby::api::ble_v2::BleMedium::AdvertisingSession; + using TxPowerLevel = ::nearby::api::ble::TxPowerLevel; + using ScanningSession = ::nearby::api::ble::BleMedium::ScanningSession; + using ScanningCallback = ::nearby::api::ble::BleMedium::ScanningCallback; + using AdvertiseParameters = ::nearby::api::ble::AdvertiseParameters; + using AdvertisingSession = ::nearby::api::ble::BleMedium::AdvertisingSession; using AdvertisingCallback = - ::nearby::api::ble_v2::BleMedium::AdvertisingCallback; - using BleAdvertisementData = ::nearby::api::ble_v2::BleAdvertisementData; - using BleMedium = ::nearby::api::ble_v2::BleMedium; + ::nearby::api::ble::BleMedium::AdvertisingCallback; + using BleAdvertisementData = ::nearby::api::ble::BleAdvertisementData; + using BleMedium = ::nearby::api::ble::BleMedium; explicit Ble(nearby::BluetoothAdapter& bluetooth_adapter) : medium_(bluetooth_adapter) {} @@ -102,7 +102,7 @@ class Ble { return TxPowerLevel::kUnknown; } - nearby::BleV2Medium medium_; + nearby::BleMedium medium_; }; } // namespace presence diff --git a/presence/implementation/mediums/ble_test.cc b/presence/implementation/mediums/ble_test.cc index 8afd24f7..70633ad7 100644 --- a/presence/implementation/mediums/ble_test.cc +++ b/presence/implementation/mediums/ble_test.cc @@ -15,20 +15,25 @@ #include "presence/implementation/mediums/ble.h" #include +#include #include #include #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" -#include "internal/platform/ble_v2.h" +#include "absl/status/status.h" +#include "absl/time/time.h" +#include "absl/types/variant.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/count_down_latch.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/feature_flags.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/medium_environment.h" #include "internal/platform/uuid.h" #include "presence/data_element.h" #include "presence/implementation/mediums/advertisement_data.h" +#include "presence/power_mode.h" #include "presence/scan_request.h" namespace nearby { @@ -36,16 +41,15 @@ namespace presence { namespace { using FeatureFlags = ::nearby::FeatureFlags::Flags; -using BleV2MediumStatus = ::nearby::MediumEnvironment::BleV2MediumStatus; -using ScanningSession = ::nearby::api::ble_v2::BleMedium::ScanningSession; -using TxPowerLevel = ::nearby::api::ble_v2::TxPowerLevel; -using ScanningCallback = ::nearby::api::ble_v2::BleMedium::ScanningCallback; +using BleMediumStatus = ::nearby::MediumEnvironment::BleMediumStatus; +using ScanningSession = ::nearby::api::ble::BleMedium::ScanningSession; +using TxPowerLevel = ::nearby::api::ble::TxPowerLevel; +using ScanningCallback = ::nearby::api::ble::BleMedium::ScanningCallback; using Uuid = ::nearby::Uuid; -using ::nearby::api::ble_v2::BleAdvertisementData; -using ::nearby::api::ble_v2::BlePeripheral; -using AdvertisingCallback = - ::nearby::api::ble_v2::BleMedium::AdvertisingCallback; -using AdvertisingSession = ::nearby::api::ble_v2::BleMedium::AdvertisingSession; +using ::nearby::api::ble::BleAdvertisementData; +using ::nearby::api::ble::BlePeripheral; +using AdvertisingCallback = ::nearby::api::ble::BleMedium::AdvertisingCallback; +using AdvertisingSession = ::nearby::api::ble::BleMedium::AdvertisingSession; constexpr FeatureFlags kTestCases[] = { FeatureFlags{}, @@ -82,8 +86,8 @@ class BleTest : public testing::TestWithParam { }; protected: - absl::optional GetBleStatus(const Ble& ble) { - return env_.GetBleV2MediumStatus(*ble.GetImpl()); + std::optional GetBleStatus(const Ble& ble) { + return env_.GetBleMediumStatus(*ble.GetImpl()); } nearby::MediumEnvironment& env_{nearby::MediumEnvironment::Instance()}; }; @@ -148,7 +152,7 @@ TEST_P(BleTest, AdvertiseAndScan) { advertisements.push_back(advertisement_data); scan_latch.CountDown(); }}); - std::unique_ptr + std::unique_ptr advertising_session = server.StartAdvertising( advert_data, PowerMode::kBalanced, AdvertisingCallback{ diff --git a/presence/implementation/scan_manager.cc b/presence/implementation/scan_manager.cc index 0a72a2c7..3f6d7e03 100644 --- a/presence/implementation/scan_manager.cc +++ b/presence/implementation/scan_manager.cc @@ -27,7 +27,7 @@ #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "internal/platform/future.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/credential_callbacks.h" #include "internal/platform/implementation/crypto.h" #include "internal/platform/logging.h" @@ -45,10 +45,10 @@ namespace nearby { namespace presence { namespace { -using BleAdvertisementData = ::nearby::api::ble_v2::BleAdvertisementData; -using BlePeripheral = ::nearby::api::ble_v2::BlePeripheral; -using ScanningSession = ::nearby::api::ble_v2::BleMedium::ScanningSession; -using ScanningCallback = ::nearby::api::ble_v2::BleMedium::ScanningCallback; +using BleAdvertisementData = ::nearby::api::ble::BleAdvertisementData; +using BlePeripheral = ::nearby::api::ble::BlePeripheral; +using ScanningSession = ::nearby::api::ble::BleMedium::ScanningSession; +using ScanningCallback = ::nearby::api::ble::BleMedium::ScanningCallback; } // namespace ScanSessionId ScanManager::StartScan(ScanRequest scan_request, diff --git a/presence/implementation/scan_manager.h b/presence/implementation/scan_manager.h index 350689a1..a174884f 100644 --- a/presence/implementation/scan_manager.h +++ b/presence/implementation/scan_manager.h @@ -23,7 +23,7 @@ #include "absl/base/thread_annotations.h" #include "absl/container/flat_hash_map.h" #include "absl/strings/string_view.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/mutex.h" #include "internal/platform/mutex_lock.h" #include "internal/platform/runnable.h" @@ -41,7 +41,6 @@ #include "presence/implementation/advertisement_decoder_impl.h" #endif - namespace nearby { namespace presence { @@ -52,9 +51,9 @@ class ScanManager { using SingleThreadExecutor = ::nearby::SingleThreadExecutor; using Mutex = ::nearby::Mutex; using MutexLock = ::nearby::MutexLock; - using ScanningSession = ::nearby::api::ble_v2::BleMedium::ScanningSession; + using ScanningSession = ::nearby::api::ble::BleMedium::ScanningSession; using Runnable = ::nearby::Runnable; - using BleAdvertisementData = ::nearby::api::ble_v2::BleAdvertisementData; + using BleAdvertisementData = ::nearby::api::ble::BleAdvertisementData; using SharedCredential = ::nearby::internal::SharedCredential; using IdentityType = ::nearby::internal::IdentityType; @@ -81,12 +80,11 @@ class ScanManager { AdvertisementFilter advertisement_filter; std::unique_ptr scanning_session; }; - void NotifyFoundBle( - ScanSessionId id, BleAdvertisementData data, - nearby::api::ble_v2::BlePeripheral::UniqueId peripheral_id) + void NotifyFoundBle(ScanSessionId id, BleAdvertisementData data, + nearby::api::ble::BlePeripheral::UniqueId peripheral_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_); void NotifyLostBle(ScanSessionId id, - nearby::api::ble_v2::BlePeripheral::UniqueId peripheral_id) + nearby::api::ble::BlePeripheral::UniqueId peripheral_id) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_); void FetchCredentials(ScanSessionId id, const ScanRequest& scan_request) ABSL_EXCLUSIVE_LOCKS_REQUIRED(*executor_); @@ -100,9 +98,8 @@ class ScanManager { CredentialManager* credential_manager_; absl::flat_hash_map scan_sessions_ ABSL_GUARDED_BY(*executor_); - absl::flat_hash_map - device_unique_id_to_endpoint_id_map_ - ABSL_GUARDED_BY(*executor_); + absl::flat_hash_map + device_unique_id_to_endpoint_id_map_ ABSL_GUARDED_BY(*executor_); SingleThreadExecutor* executor_; }; diff --git a/presence/implementation/scan_manager_test.cc b/presence/implementation/scan_manager_test.cc index 2a8b84d7..4ddee9ee 100644 --- a/presence/implementation/scan_manager_test.cc +++ b/presence/implementation/scan_manager_test.cc @@ -29,7 +29,7 @@ #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/byte_array.h" #include "internal/platform/count_down_latch.h" -#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/ble.h" #include "internal/platform/implementation/credential_callbacks.h" #include "internal/platform/logging.h" #include "internal/platform/mac_address.h" @@ -52,9 +52,9 @@ namespace nearby { namespace presence { namespace { -using AdvertisingSession = ::nearby::api::ble_v2::BleMedium::AdvertisingSession; +using AdvertisingSession = ::nearby::api::ble::BleMedium::AdvertisingSession; using AdvertisingCallback = - ::nearby::api::ble_v2::BleMedium::AdvertisingCallback; + ::nearby::api::ble::BleMedium::AdvertisingCallback; using ::nearby::SingleThreadExecutor; using CountDownLatch = ::nearby::CountDownLatch;