diff --git a/connections/file_share/main.cc b/connections/file_share/main.cc index edeb3970..3f4b13e8 100644 --- a/connections/file_share/main.cc +++ b/connections/file_share/main.cc @@ -25,7 +25,9 @@ #include "connections/v3/discovery_options.h" #include "connections/v3/listeners.h" #include "connections/implementation/service_controller_router.h" +#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "internal/platform/file.h" +#include "internal/flags/nearby_flags.h" namespace { constexpr char kDefaultServiceId[] = "com.google.nearby.fileshare.cli"; @@ -303,7 +305,7 @@ class FileShareApp { LOG(INFO) << "Found endpoint " << remote_device.GetEndpointId() << " service_id=" << service_id; nearby::connections::ConnectionOptions options; - options.strategy = nearby::connections::Strategy::kP2pCluster; + options.strategy = nearby::connections::Strategy::kP2pStar; options.allowed = options_.upgrade_mediums_set ? options_.upgrade_mediums : options_.mediums; @@ -464,6 +466,15 @@ class FileShareApp { int main(int argc, char** argv) { std::srand(static_cast(std::time(nullptr))); + nearby::NearbyFlags::GetInstance().OverrideBoolFlagValue( + ::nearby::connections::config_package_nearby:: + nearby_connections_feature::kEnableBleL2cap, + true); + nearby::NearbyFlags::GetInstance().OverrideBoolFlagValue( + ::nearby::connections::config_package_nearby:: + nearby_connections_feature::kEnableBleV2, + true); + Options options; bool mediums_override = false; diff --git a/connections/implementation/mediums/ble_v2.cc b/connections/implementation/mediums/ble_v2.cc index c3b9125a..b99d3180 100644 --- a/connections/implementation/mediums/ble_v2.cc +++ b/connections/implementation/mediums/ble_v2.cc @@ -186,6 +186,10 @@ ErrorOr BleV2::StartAdvertising(const std::string& service_id, if (it != l2cap_server_sockets_.end()) { psm = it->second.GetPSM(); } + LOG(INFO) << "BLE V2 advertising service_id=" << service_id + << " psm=" << psm + << " (default=" + << (psm == mediums::BleAdvertisementHeader::kDefaultPsmValue) << ")"; mediums::BleAdvertisement medium_advertisement = { mediums::BleAdvertisement::Version::kV2, mediums::BleAdvertisement::SocketVersion::kV2, diff --git a/internal/platform/implementation/linux/BUILD b/internal/platform/implementation/linux/BUILD index 8eec001e..bc4cfd7f 100644 --- a/internal/platform/implementation/linux/BUILD +++ b/internal/platform/implementation/linux/BUILD @@ -12,29 +12,34 @@ # See the License for the specific language governing permissions and # limitations under the License. -licenses(["notice"]) - load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands") +licenses(["notice"]) + refresh_compile_commands( name = "refresh_compile_commands", # Specify the targets of interest. # For example, specify a dict of targets and any flags required to build. targets = { - ":linux": "-s --check_visibility=false --spawn_strategy=standalone --verbose_failures --strip=never --copt=-O0 --copt=-g --copt=-fno-omit-frame-pointer", - "//connections:core": "-s --check_visibility=false --spawn_strategy=standalone --verbose_failures --strip=never --copt=-O0 --copt=-g --copt=-fno-omit-frame-pointer", - "//connections/file_share:file_share": "-s --check_visibility=false --spawn_strategy=standalone --verbose_failures --strip=never --copt=-O0 --copt=-g --copt=-fno-omit-frame-pointer", + ":linux": "-s --check_visibility=false --spawn_strategy=standalone --verbose_failures --strip=never --copt=-O0 --copt=-g --copt=-fno-omit-frame-pointer", + "//connections:core": "-s --check_visibility=false --spawn_strategy=standalone --verbose_failures --strip=never --copt=-O0 --copt=-g --copt=-fno-omit-frame-pointer", + "//connections/file_share:file_share": "-s --check_visibility=false --spawn_strategy=standalone --verbose_failures --strip=never --copt=-O0 --copt=-g --copt=-fno-omit-frame-pointer", }, # No need to add flags already in .bazelrc. They're automatically picked up. # If you don't need flags, a list of targets is also okay, as is a single target string. # Wildcard patterns, like //... for everything, *are* allowed here, just like a build. - # As are additional targets (+) and subtractions (-), like in bazel query https://docs.bazel.build/versions/main/query.html#expressions + # As are additional targets (+) and subtractions (-), like in bazel query https://docs.bazel.build/versions/main/query.html#expressions # And if you're working on a header-only library, specify a test or binary target that compiles it. ) cc_library( name = "types", + srcs = [ + "device_info.cc", + #"log_message.cc", + "timer.cc", + ], hdrs = [ "atomic_boolean.h", "atomic_reference.h", @@ -54,19 +59,14 @@ cc_library( #"log_message.h", "utils.h", ], - srcs = [ - "device_info.cc", - #"log_message.cc", - "timer.cc", - ], copts = ["-lrt"], visibility = ["//third_party/nearby/sharing/internal/impl/linux:__pkg__"], deps = [ ":comm", "//internal/platform/implementation:types", "@com_google_absl//absl/strings", + "@sdbus_cpp", "@sdbus_cpp//:libsystemd", - "@sdbus_cpp//:sdbus_cpp", ], ) @@ -76,10 +76,12 @@ cc_library( "avahi.h", "ble_gatt_server.h", "ble_gatt_client.h", -# "ble_medium.h", + # "ble_medium.h", "ble_v2_medium.h", "ble_v2_server_socket.h", "ble_v2_socket.h", + "ble_l2cap_server_socket.h", + "ble_l2cap_socket.h", "bluetooth_adapter.h", "bluetooth_bluez_profile.h", "bluetooth_classic_device.h", @@ -90,7 +92,7 @@ cc_library( "bluetooth_pairing.h", "bluez.h", "bluez_device.h", -# "bluez_agent.h", + # "bluez_agent.h", "bluez_advertisement_monitor.h", "bluez_advertisement_monitor_manager.h", "bluez_gatt_characteristic_client.h", @@ -118,6 +120,7 @@ cc_library( "wifi_medium.h", "wifi_socket.h", ], + visibility = ["//visibility:private"], deps = [ ":crypto", "//internal/platform:base", @@ -138,10 +141,9 @@ cc_library( "@com_google_absl//absl/synchronization", "@com_google_absl//absl/time", "@com_google_absl//absl/types:optional", + "@sdbus_cpp", "@sdbus_cpp//:libsystemd", - "@sdbus_cpp//:sdbus_cpp", ], - visibility = ["//visibility:private"], ) cc_library( @@ -155,7 +157,8 @@ cc_library( "//internal/platform/implementation:types", "@boringssl//:crypto", "@com_google_absl//absl/strings", -]) + ], +) cc_library( name = "linux", @@ -163,10 +166,12 @@ cc_library( "avahi.cc", "ble_gatt_client.cc", "ble_gatt_server.cc", -# "ble_medium.cc", + # "ble_medium.cc", "ble_v2_medium.cc", "ble_v2_server_socket.cc", "ble_v2_socket.cc", + "ble_l2cap_server_socket.cc", + "ble_l2cap_socket.cc", "bluetooth_adapter.cc", "bluetooth_bluez_profile.cc", "bluetooth_classic_socket.cc", @@ -203,7 +208,10 @@ cc_library( "wifi_lan_server_socket.cc", "wifi_medium.cc", ], - linkopts = ["-lcurl"], + linkopts = [ + "-lcurl", + "-lbluetooth", + ], visibility = [ "//connections:__subpackages__", "//fastpair:__subpackages__", @@ -225,9 +233,9 @@ cc_library( "//internal/platform/implementation:comm", "//internal/platform/implementation:platform", "//internal/platform/implementation:types", + "//internal/platform/implementation/linux/generated:types", "//internal/platform/implementation/shared:count_down_latch", "//internal/platform/implementation/shared:file", - "//internal/platform/implementation/linux/generated:types", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/functional:any_invocable", @@ -241,8 +249,8 @@ cc_library( "@com_google_absl//absl/time", "@com_google_absl//absl/types:optional", "@nlohmann_json//:json", + "@sdbus_cpp", "@sdbus_cpp//:libsystemd", - "@sdbus_cpp//:sdbus_cpp", ], ) @@ -259,9 +267,9 @@ cc_library( "//visibility:private", # Only private by automation, not intent. Owner may accept CLs adding visibility. See go/scheuklappen#explicit-private. ], deps = [ + ":types", "//internal/platform:base", "@nlohmann_json//:json", - ":types", ], ) @@ -290,9 +298,9 @@ cc_test( deps = [ ":comm", ":crypto", + ":linux", ":test_utils", ":types", - ":linux", "//internal/platform:base", "//internal/platform/implementation:comm", "//internal/platform/implementation:platform", diff --git a/internal/platform/implementation/linux/ble_l2cap_server_socket.cc b/internal/platform/implementation/linux/ble_l2cap_server_socket.cc index e6c252ed..f1d0a9a9 100644 --- a/internal/platform/implementation/linux/ble_l2cap_server_socket.cc +++ b/internal/platform/implementation/linux/ble_l2cap_server_socket.cc @@ -65,7 +65,6 @@ std::unique_ptr BleL2capServerSocket::Accept() { std::memset(&addr, 0, sizeof(addr)); addr.l2_family = AF_BLUETOOTH; addr.l2_psm = htobs(psm_); - addr.l2_cid = 0; addr.l2_bdaddr_type = BDADDR_LE_PUBLIC; // Set BDADDR_ANY (all zeros) std::memset(&addr.l2_bdaddr, 0, sizeof(addr.l2_bdaddr)); diff --git a/internal/platform/implementation/linux/ble_v2_medium.cc b/internal/platform/implementation/linux/ble_v2_medium.cc index 31a50420..5b9d9ca8 100644 --- a/internal/platform/implementation/linux/ble_v2_medium.cc +++ b/internal/platform/implementation/linux/ble_v2_medium.cc @@ -31,21 +31,23 @@ // #include "internal/platform/implementation/linux/ble_gatt_server.h" #include "internal/platform/implementation/linux/ble_v2_medium.h" +#include "absl/types/span.h" #include "ble_gatt_client.h" #include "ble_gatt_server.h" +#include "ble_l2cap_server_socket.h" +#include "ble_l2cap_socket.h" +#include "internal/base/observer_list.h" #include "internal/platform/implementation/linux/bluetooth_classic_device.h" #include "internal/platform/implementation/linux/bluetooth_devices.h" #include "internal/platform/implementation/linux/bluez.h" -#include "internal/platform/mac_address.h" -#include "internal/platform/prng.h" -#include "absl/types/span.h" -#include "internal/base/observer_list.h" #include "internal/platform/implementation/linux/bluez_advertisement_monitor.h" #include "internal/platform/implementation/linux/bluez_advertisement_monitor_manager.h" #include "internal/platform/implementation/linux/bluez_le_advertisement.h" #include "internal/platform/implementation/linux/dbus.h" #include "internal/platform/implementation/linux/generated/dbus/bluez/advertisement_monitor_server.h" #include "internal/platform/implementation/linux/generated/dbus/bluez/le_advertisement_manager_client.h" +#include "internal/platform/mac_address.h" +#include "internal/platform/prng.h" namespace nearby { namespace linux { @@ -458,17 +460,17 @@ std::unique_ptr BleV2Medium::OpenServerSocket( std::unique_ptr BleV2Medium::OpenL2capServerSocket(const std::string &service_id) { - return nullptr; - // LOG(INFO) << __func__ << ": Opening L2CAP server socket for service " - // << service_id; - // - // Prng prng; - // auto psm = 0x80 + (prng.NextUint32() % 0x80); - // auto server_socket = std::make_unique(psm); - // - // LOG(INFO) << __func__ << ": L2CAP server socket created with PSM: " - // << server_socket->GetPSM(); - // return server_socket; + // return nullptr; + LOG(INFO) << __func__ << ": Opening L2CAP server socket for service " + << service_id; + + Prng prng; + auto psm = 0x80 + (prng.NextUint32() % 0x80); + auto server_socket = std::make_unique(psm); + + LOG(INFO) << __func__ << ": L2CAP server socket created with PSM: " + << server_socket->GetPSM(); + return server_socket; } // This is supposed to be for a socket on top of Weave protocol. @@ -530,48 +532,51 @@ std::unique_ptr BleV2Medium::ConnectOverL2cap( api::ble_v2::TxPowerLevel tx_power_level, api::ble_v2::BlePeripheral::UniqueId peripheral_id, CancellationFlag *cancellation_flag) { - return nullptr; - // auto device = devices_->get_device_by_unique_id(peripheral_id); - // if (!device) { - // LOG(ERROR) << __func__ << ": Failed to find device with unique ID " - // << peripheral_id; - // return nullptr; - // } - // - // LOG(INFO) << __func__ << ": Connecting to L2CAP PSM " << psm - // << " on device " << device->GetMacAddress(); - // - // - // int fd = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); - // if (fd < 0) { - // LOG(ERROR) << __func__ << ": Failed to create L2CAP socket: " - // << std::strerror(errno); - // return nullptr; - // } - // - // struct sockaddr_l2 addr; - // std::memset(&addr, 0, sizeof(addr)); - // addr.l2_family = AF_BLUETOOTH; - // addr.l2_psm = htobs(psm); - // addr.l2_cid = 0; - // addr.l2_bdaddr_type = BDADDR_LE_PUBLIC; - // - // std::string mac_addr = device->GetMacAddress(); - // if (str2ba(mac_addr.c_str(), &addr.l2_bdaddr) < 0) { - // LOG(ERROR) << __func__ << ": Invalid Bluetooth address: " << mac_addr; - // close(fd); - // return nullptr; - // } - // - // if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { - // LOG(ERROR) << __func__ << ": Failed to connect to L2CAP socket: " - // << std::strerror(errno); - // close(fd); - // return nullptr; - // } - // - // LOG(INFO) << __func__ << ": Successfully connected to L2CAP socket"; - // return std::make_unique(fd, peripheral_id); + // return nullptr; + auto device = devices_->get_device_by_unique_id(peripheral_id); + if (!device) { + LOG(ERROR) << __func__ << ": Failed to find device with unique ID " + << peripheral_id; + return nullptr; + } + + LOG(INFO) << __func__ << ": Connecting to L2CAP PSM " << psm + << " on device " << device->GetMacAddress(); + + + int fd = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP); + if (fd < 0) { + LOG(ERROR) << __func__ << ": Failed to create L2CAP socket: " + << std::strerror(errno); + return nullptr; + } + + struct sockaddr_l2 addr; + std::memset(&addr, 0, sizeof(addr)); + addr.l2_family = AF_BLUETOOTH; + addr.l2_psm = htobs(psm); + if (device -> GetAddressType() == "random") { + addr.l2_bdaddr_type = BDADDR_LE_RANDOM; + }else { + addr.l2_bdaddr_type = BDADDR_LE_PUBLIC; + } + + std::string mac_addr = device->GetMacAddress(); + if (str2ba(mac_addr.c_str(), &addr.l2_bdaddr) < 0) { + LOG(ERROR) << __func__ << ": Invalid Bluetooth address: " << mac_addr; + close(fd); + return nullptr; + } + + if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + LOG(ERROR) << __func__ << ": Failed to connect to L2CAP socket: " + << std::strerror(errno); + close(fd); + return nullptr; + } + + LOG(INFO) << __func__ << ": Successfully connected to L2CAP socket"; + return std::make_unique(fd, peripheral_id); } bool BleV2Medium::StartMultipleServicesScanning(