Commit 0a3e48db authored by Giovanni Ortuño Urquidi's avatar Giovanni Ortuño Urquidi Committed by Commit Bot

bluetooth: Use an array instead of a string for the address

Use a array<uint8, 6> in the BluetoothSystem mojom and
std::array<uint8_t, 6> in the C++ Bindings.

Bug: 870192
Change-Id: I3467a770646b9ea2587a8cbc656c2ff510e0149c
Reviewed-on: https://chromium-review.googlesource.com/c/1341291Reviewed-by: default avatarKen Rockot <rockot@google.com>
Reviewed-by: default avatarTetsui Ohkubo <tetsui@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Reviewed-by: default avatarOvidio Henriquez <odejesush@chromium.org>
Commit-Queue: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612516}
parent 64f27d56
...@@ -4,6 +4,10 @@ ...@@ -4,6 +4,10 @@
#include "ash/system/bluetooth/bluetooth_detailed_view.h" #include "ash/system/bluetooth/bluetooth_detailed_view.h"
#include <map>
#include <memory>
#include <utility>
#include "ash/resources/vector_icons/vector_icons.h" #include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h" #include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h" #include "ash/strings/grit/ash_strings.h"
...@@ -167,7 +171,8 @@ void BluetoothDetailedView::UpdateDeviceScrollList( ...@@ -167,7 +171,8 @@ void BluetoothDetailedView::UpdateDeviceScrollList(
for (const auto& device : paired_not_connected_devices) for (const auto& device : paired_not_connected_devices)
paired_not_connected_devices_.push_back(device->Clone()); paired_not_connected_devices_.push_back(device->Clone());
std::string focused_device_address = GetFocusedDeviceAddress(); base::Optional<BluetoothAddress> focused_device_address =
GetFocusedDeviceAddress();
device_map_.clear(); device_map_.clear();
scroll_content()->RemoveAllChildViews(true); scroll_content()->RemoveAllChildViews(true);
...@@ -200,8 +205,8 @@ void BluetoothDetailedView::UpdateDeviceScrollList( ...@@ -200,8 +205,8 @@ void BluetoothDetailedView::UpdateDeviceScrollList(
} }
// Focus the device which was focused before the device-list update. // Focus the device which was focused before the device-list update.
if (!focused_device_address.empty()) if (focused_device_address)
FocusDeviceByAddress(focused_device_address); FocusDeviceByAddress(focused_device_address.value());
scroll_content()->InvalidateLayout(); scroll_content()->InvalidateLayout();
...@@ -242,7 +247,7 @@ void BluetoothDetailedView::AppendSameTypeDevicesToScrollList( ...@@ -242,7 +247,7 @@ void BluetoothDetailedView::AppendSameTypeDevicesToScrollList(
} }
bool BluetoothDetailedView::FoundDevice( bool BluetoothDetailedView::FoundDevice(
const std::string& device_address, const BluetoothAddress& device_address,
const BluetoothDeviceList& device_list) const { const BluetoothDeviceList& device_list) const {
for (const auto& device : device_list) { for (const auto& device : device_list) {
if (device->address == device_address) if (device->address == device_address)
...@@ -252,7 +257,7 @@ bool BluetoothDetailedView::FoundDevice( ...@@ -252,7 +257,7 @@ bool BluetoothDetailedView::FoundDevice(
} }
void BluetoothDetailedView::UpdateClickedDevice( void BluetoothDetailedView::UpdateClickedDevice(
const std::string& device_address, const BluetoothAddress& device_address,
views::View* item_container) { views::View* item_container) {
if (FoundDevice(device_address, paired_not_connected_devices_)) { if (FoundDevice(device_address, paired_not_connected_devices_)) {
HoverHighlightView* container = HoverHighlightView* container =
...@@ -270,16 +275,17 @@ void BluetoothDetailedView::ShowSettings() { ...@@ -270,16 +275,17 @@ void BluetoothDetailedView::ShowSettings() {
} }
} }
std::string BluetoothDetailedView::GetFocusedDeviceAddress() const { base::Optional<BluetoothAddress>
BluetoothDetailedView::GetFocusedDeviceAddress() const {
for (const auto& view_and_address : device_map_) { for (const auto& view_and_address : device_map_) {
if (view_and_address.first->HasFocus()) if (view_and_address.first->HasFocus())
return view_and_address.second; return view_and_address.second;
} }
return std::string(); return base::nullopt;
} }
void BluetoothDetailedView::FocusDeviceByAddress( void BluetoothDetailedView::FocusDeviceByAddress(
const std::string& address) const { const BluetoothAddress& address) const {
for (auto& view_and_address : device_map_) { for (auto& view_and_address : device_map_) {
if (view_and_address.second == address) { if (view_and_address.second == address) {
view_and_address.first->RequestFocus(); view_and_address.first->RequestFocus();
...@@ -293,12 +299,12 @@ void BluetoothDetailedView::HandleViewClicked(views::View* view) { ...@@ -293,12 +299,12 @@ void BluetoothDetailedView::HandleViewClicked(views::View* view) {
if (helper->GetBluetoothState() != BluetoothSystem::State::kPoweredOn) if (helper->GetBluetoothState() != BluetoothSystem::State::kPoweredOn)
return; return;
std::map<views::View*, std::string>::iterator find; std::map<views::View*, BluetoothAddress>::iterator find;
find = device_map_.find(view); find = device_map_.find(view);
if (find == device_map_.end()) if (find == device_map_.end())
return; return;
const std::string device_address = find->second; const BluetoothAddress& device_address = find->second;
if (FoundDevice(device_address, connecting_devices_)) if (FoundDevice(device_address, connecting_devices_))
return; return;
......
...@@ -5,9 +5,12 @@ ...@@ -5,9 +5,12 @@
#ifndef ASH_SYSTEM_BLUETOOTH_BLUETOOTH_DETAILED_VIEW_H_ #ifndef ASH_SYSTEM_BLUETOOTH_BLUETOOTH_DETAILED_VIEW_H_
#define ASH_SYSTEM_BLUETOOTH_BLUETOOTH_DETAILED_VIEW_H_ #define ASH_SYSTEM_BLUETOOTH_BLUETOOTH_DETAILED_VIEW_H_
#include <map>
#include "ash/login_status.h" #include "ash/login_status.h"
#include "ash/system/bluetooth/tray_bluetooth_helper.h" #include "ash/system/bluetooth/tray_bluetooth_helper.h"
#include "ash/system/tray/tray_detailed_view.h" #include "ash/system/tray/tray_detailed_view.h"
#include "base/optional.h"
namespace views { namespace views {
class ToggleButton; class ToggleButton;
...@@ -52,18 +55,18 @@ class BluetoothDetailedView : public TrayDetailedView { ...@@ -52,18 +55,18 @@ class BluetoothDetailedView : public TrayDetailedView {
bool checked); bool checked);
// Returns true if the device with |device_id| is found in |device_list|. // Returns true if the device with |device_id| is found in |device_list|.
bool FoundDevice(const std::string& device_id, bool FoundDevice(const BluetoothAddress& device_id,
const BluetoothDeviceList& device_list) const; const BluetoothDeviceList& device_list) const;
// Updates UI of the clicked bluetooth device to show it is being connected // Updates UI of the clicked bluetooth device to show it is being connected
// or disconnected if such an operation is going to be performed underway. // or disconnected if such an operation is going to be performed underway.
void UpdateClickedDevice(const std::string& device_id, void UpdateClickedDevice(const BluetoothAddress& device_id,
views::View* item_container); views::View* item_container);
void ShowSettings(); void ShowSettings();
std::string GetFocusedDeviceAddress() const; base::Optional<BluetoothAddress> GetFocusedDeviceAddress() const;
void FocusDeviceByAddress(const std::string& address) const; void FocusDeviceByAddress(const BluetoothAddress& address) const;
// TrayDetailedView: // TrayDetailedView:
void HandleViewClicked(views::View* view) override; void HandleViewClicked(views::View* view) override;
...@@ -74,7 +77,7 @@ class BluetoothDetailedView : public TrayDetailedView { ...@@ -74,7 +77,7 @@ class BluetoothDetailedView : public TrayDetailedView {
// TODO(jamescook): Don't cache this. // TODO(jamescook): Don't cache this.
LoginStatus login_; LoginStatus login_;
std::map<views::View*, std::string> device_map_; std::map<views::View*, BluetoothAddress> device_map_;
BluetoothDeviceList connecting_devices_; BluetoothDeviceList connecting_devices_;
BluetoothDeviceList paired_not_connected_devices_; BluetoothDeviceList paired_not_connected_devices_;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
namespace ash { namespace ash {
using BluetoothAddress = std::array<uint8_t, 6>;
using BluetoothDeviceList = std::vector<device::mojom::BluetoothDeviceInfoPtr>; using BluetoothDeviceList = std::vector<device::mojom::BluetoothDeviceInfoPtr>;
// Maps UI concepts from the Bluetooth system tray (e.g. "Bluetooth is on") into // Maps UI concepts from the Bluetooth system tray (e.g. "Bluetooth is on") into
...@@ -57,7 +58,7 @@ class ASH_EXPORT TrayBluetoothHelper { ...@@ -57,7 +58,7 @@ class ASH_EXPORT TrayBluetoothHelper {
virtual void StopBluetoothDiscovering() = 0; virtual void StopBluetoothDiscovering() = 0;
// Connect to a specific bluetooth device. // Connect to a specific bluetooth device.
virtual void ConnectToBluetoothDevice(const std::string& address) = 0; virtual void ConnectToBluetoothDevice(const BluetoothAddress& address) = 0;
// Returns the state of Bluetooth in the system e.g. has hardware support, // Returns the state of Bluetooth in the system e.g. has hardware support,
// is enabled, etc. // is enabled, etc.
......
...@@ -57,7 +57,7 @@ void TrayBluetoothHelperExperimental::StopBluetoothDiscovering() { ...@@ -57,7 +57,7 @@ void TrayBluetoothHelperExperimental::StopBluetoothDiscovering() {
} }
void TrayBluetoothHelperExperimental::ConnectToBluetoothDevice( void TrayBluetoothHelperExperimental::ConnectToBluetoothDevice(
const std::string& address) { const BluetoothAddress& address) {
NOTIMPLEMENTED(); NOTIMPLEMENTED();
} }
......
...@@ -33,7 +33,7 @@ class TrayBluetoothHelperExperimental ...@@ -33,7 +33,7 @@ class TrayBluetoothHelperExperimental
void Initialize() override; void Initialize() override;
void StartBluetoothDiscovering() override; void StartBluetoothDiscovering() override;
void StopBluetoothDiscovering() override; void StopBluetoothDiscovering() override;
void ConnectToBluetoothDevice(const std::string& address) override; void ConnectToBluetoothDevice(const BluetoothAddress& address) override;
device::mojom::BluetoothSystem::State GetBluetoothState() override; device::mojom::BluetoothSystem::State GetBluetoothState() override;
void SetBluetoothEnabled(bool enabled) override; void SetBluetoothEnabled(bool enabled) override;
bool HasBluetoothDiscoverySession() override; bool HasBluetoothDiscoverySession() override;
......
...@@ -13,11 +13,15 @@ ...@@ -13,11 +13,15 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/metrics/user_metrics.h" #include "base/metrics/user_metrics.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "device/bluetooth/bluetooth_adapter.h" #include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h" #include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_device.h" #include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_discovery_session.h" #include "device/bluetooth/bluetooth_discovery_session.h"
#include "device/bluetooth/chromeos/bluetooth_utils.h" #include "device/bluetooth/chromeos/bluetooth_utils.h"
#include "services/device/public/cpp/bluetooth/bluetooth_utils.h"
using device::mojom::BluetoothSystem; using device::mojom::BluetoothSystem;
using device::mojom::BluetoothDeviceInfo; using device::mojom::BluetoothDeviceInfo;
...@@ -36,9 +40,37 @@ void BluetoothSetDiscoveringError() { ...@@ -36,9 +40,37 @@ void BluetoothSetDiscoveringError() {
void BluetoothDeviceConnectError( void BluetoothDeviceConnectError(
device::BluetoothDevice::ConnectErrorCode error_code) {} device::BluetoothDevice::ConnectErrorCode error_code) {}
std::string BluetoothAddressToStr(const BluetoothAddress& address) {
static constexpr char kAddressFormat[] =
"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX";
return base::StringPrintf(kAddressFormat, address[0], address[1], address[2],
address[3], address[4], address[5]);
}
// Converts a MAC Address string e.g. "00:11:22:33:44:55" into an
// BluetoothAddress e.g. {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}.
BluetoothAddress AddressStrToBluetoothAddress(const std::string& address_str) {
std::string numbers;
bool success = base::ReplaceChars(address_str, ":", "", &numbers);
DCHECK(success);
std::vector<uint8_t> address_vector;
success = base::HexStringToBytes(numbers, &address_vector);
DCHECK(success);
// If the size is not 6, then the underlying Bluetooth API returned an
// incorrect value.
CHECK_EQ(6u, address_vector.size());
BluetoothAddress address_array;
std::copy_n(address_vector.begin(), 6, address_array.begin());
return address_array;
}
BluetoothDeviceInfoPtr GetBluetoothDeviceInfo(device::BluetoothDevice* device) { BluetoothDeviceInfoPtr GetBluetoothDeviceInfo(device::BluetoothDevice* device) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = device->GetAddress(); info->address = AddressStrToBluetoothAddress(device->GetAddress());
info->name = device->GetName(); info->name = device->GetName();
info->is_paired = device->IsPaired(); info->is_paired = device->IsPaired();
...@@ -150,8 +182,9 @@ void TrayBluetoothHelperLegacy::StopBluetoothDiscovering() { ...@@ -150,8 +182,9 @@ void TrayBluetoothHelperLegacy::StopBluetoothDiscovering() {
} }
void TrayBluetoothHelperLegacy::ConnectToBluetoothDevice( void TrayBluetoothHelperLegacy::ConnectToBluetoothDevice(
const std::string& address) { const BluetoothAddress& address) {
device::BluetoothDevice* device = adapter_->GetDevice(address); device::BluetoothDevice* device =
adapter_->GetDevice(BluetoothAddressToStr(address));
if (!device || device->IsConnecting() || if (!device || device->IsConnecting() ||
(device->IsConnected() && device->IsPaired())) { (device->IsConnected() && device->IsPaired())) {
return; return;
......
...@@ -40,7 +40,7 @@ class ASH_EXPORT TrayBluetoothHelperLegacy ...@@ -40,7 +40,7 @@ class ASH_EXPORT TrayBluetoothHelperLegacy
void Initialize() override; void Initialize() override;
void StartBluetoothDiscovering() override; void StartBluetoothDiscovering() override;
void StopBluetoothDiscovering() override; void StopBluetoothDiscovering() override;
void ConnectToBluetoothDevice(const std::string& address) override; void ConnectToBluetoothDevice(const BluetoothAddress& address) override;
device::mojom::BluetoothSystem::State GetBluetoothState() override; device::mojom::BluetoothSystem::State GetBluetoothState() override;
void SetBluetoothEnabled(bool enabled) override; void SetBluetoothEnabled(bool enabled) override;
bool HasBluetoothDiscoverySession() override; bool HasBluetoothDiscoverySession() override;
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#include "device/bluetooth/dbus/bluez_dbus_manager.h" #include "device/bluetooth/dbus/bluez_dbus_manager.h"
#include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_device_client.h" #include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
#include "services/device/public/cpp/bluetooth/bluetooth_utils.h"
#include "services/device/public/mojom/bluetooth_system.mojom.h"
using bluez::BluezDBusManager; using bluez::BluezDBusManager;
using bluez::FakeBluetoothAdapterClient; using bluez::FakeBluetoothAdapterClient;
...@@ -24,9 +26,16 @@ using device::mojom::BluetoothSystem; ...@@ -24,9 +26,16 @@ using device::mojom::BluetoothSystem;
namespace ash { namespace ash {
namespace { namespace {
// FakeBluetoothDeviceClient::kDisplayPinCodeAddress but in a BluetoothAddress.
constexpr BluetoothAddress kDisplayPinCodeAddress = {0x28, 0x37, 0x37,
0x00, 0x00, 0x00};
// FakeBluetoothDeviceClient::kLowEnergyAddress but in a BluetoothAddress.
constexpr BluetoothAddress kLowEnergyAddress = {0x00, 0x1A, 0x11,
0x00, 0x15, 0x30};
// Returns true if device with |address| exists in the filtered device list. // Returns true if device with |address| exists in the filtered device list.
// Returns false otherwise. // Returns false otherwise.
bool ExistInFilteredDevices(const std::string& address, bool ExistInFilteredDevices(const BluetoothAddress& address,
const BluetoothDeviceList& filtered_devices) { const BluetoothDeviceList& filtered_devices) {
for (const auto& device : filtered_devices) { for (const auto& device : filtered_devices) {
if (device->address == address) if (device->address == address)
...@@ -106,10 +115,8 @@ TEST_F(TrayBluetoothHelperLegacyTest, Basics) { ...@@ -106,10 +115,8 @@ TEST_F(TrayBluetoothHelperLegacyTest, Basics) {
const BluetoothDeviceList& devices = helper.GetAvailableBluetoothDevices(); const BluetoothDeviceList& devices = helper.GetAvailableBluetoothDevices();
// The devices are fake in tests, so don't assume any particular number. // The devices are fake in tests, so don't assume any particular number.
EXPECT_FALSE(devices.empty()); EXPECT_FALSE(devices.empty());
EXPECT_TRUE(ExistInFilteredDevices( EXPECT_TRUE(ExistInFilteredDevices(kDisplayPinCodeAddress, devices));
FakeBluetoothDeviceClient::kDisplayPinCodeAddress, devices)); EXPECT_FALSE(ExistInFilteredDevices(kLowEnergyAddress, devices));
EXPECT_FALSE(ExistInFilteredDevices(
FakeBluetoothDeviceClient::kLowEnergyAddress, devices));
helper.StartBluetoothDiscovering(); helper.StartBluetoothDiscovering();
RunAllPendingInMessageLoop(); RunAllPendingInMessageLoop();
...@@ -282,10 +289,36 @@ TEST_F(TrayBluetoothHelperLegacyTest, UnfilteredBluetoothDevices) { ...@@ -282,10 +289,36 @@ TEST_F(TrayBluetoothHelperLegacyTest, UnfilteredBluetoothDevices) {
const BluetoothDeviceList& devices = helper.GetAvailableBluetoothDevices(); const BluetoothDeviceList& devices = helper.GetAvailableBluetoothDevices();
// The devices are fake in tests, so don't assume any particular number. // The devices are fake in tests, so don't assume any particular number.
EXPECT_TRUE(ExistInFilteredDevices( EXPECT_TRUE(ExistInFilteredDevices(kDisplayPinCodeAddress, devices));
FakeBluetoothDeviceClient::kDisplayPinCodeAddress, devices)); EXPECT_TRUE(ExistInFilteredDevices(kLowEnergyAddress, devices));
EXPECT_TRUE(ExistInFilteredDevices( }
FakeBluetoothDeviceClient::kLowEnergyAddress, devices));
TEST_F(TrayBluetoothHelperLegacyTest, BluetoothAddress) {
// Set Bluetooth discovery simulation delay to 0 so the test doesn't have to
// wait or use timers.
FakeBluetoothAdapterClient* adapter_client =
static_cast<FakeBluetoothAdapterClient*>(
BluezDBusManager::Get()->GetBluetoothAdapterClient());
adapter_client->SetSimulationIntervalMs(0);
adapter_client
->GetProperties(
dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath))
->powered.ReplaceValue(true);
FakeBluetoothDeviceClient* device_client =
static_cast<FakeBluetoothDeviceClient*>(
BluezDBusManager::Get()->GetBluetoothDeviceClient());
device_client->CreateDevice(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath));
TrayBluetoothHelperLegacy helper;
helper.Initialize();
base::RunLoop().RunUntilIdle();
const BluetoothDeviceList& devices = helper.GetAvailableBluetoothDevices();
ASSERT_EQ(1u, devices.size());
EXPECT_EQ(kDisplayPinCodeAddress, devices[0]->address);
} }
} // namespace } // namespace
......
...@@ -43,7 +43,7 @@ void UpdateBluetoothDeviceListHelper(BluetoothDeviceList* list, ...@@ -43,7 +43,7 @@ void UpdateBluetoothDeviceListHelper(BluetoothDeviceList* list,
// |new_device_address_list|. // |new_device_address_list|.
void RemoveObsoleteBluetoothDevicesFromList( void RemoveObsoleteBluetoothDevicesFromList(
BluetoothDeviceList* device_list, BluetoothDeviceList* device_list,
const std::set<std::string>& new_device_address_list) { const std::set<BluetoothAddress>& new_device_address_list) {
base::EraseIf(*device_list, [&new_device_address_list]( base::EraseIf(*device_list, [&new_device_address_list](
const BluetoothDeviceInfoPtr& info) { const BluetoothDeviceInfoPtr& info) {
return !base::ContainsKey(new_device_address_list, info->address); return !base::ContainsKey(new_device_address_list, info->address);
...@@ -134,10 +134,10 @@ void UnifiedBluetoothDetailedViewController::UpdateDeviceListAndUI() { ...@@ -134,10 +134,10 @@ void UnifiedBluetoothDetailedViewController::UpdateDeviceListAndUI() {
} }
void UnifiedBluetoothDetailedViewController::UpdateBluetoothDeviceList() { void UnifiedBluetoothDetailedViewController::UpdateBluetoothDeviceList() {
std::set<std::string> new_connecting_devices; std::set<BluetoothAddress> new_connecting_devices;
std::set<std::string> new_connected_devices; std::set<BluetoothAddress> new_connected_devices;
std::set<std::string> new_paired_not_connected_devices; std::set<BluetoothAddress> new_paired_not_connected_devices;
std::set<std::string> new_discovered_not_paired_devices; std::set<BluetoothAddress> new_discovered_not_paired_devices;
for (const auto& device : for (const auto& device :
Shell::Get()->tray_bluetooth_helper()->GetAvailableBluetoothDevices()) { Shell::Get()->tray_bluetooth_helper()->GetAvailableBluetoothDevices()) {
......
odejesush@chromium.org
ortuno@chromium.org
reillyg@chromium.org
per-file *_mojom_traits*.*=set noparent
per-file *_mojom_traits*.*=file://ipc/SECURITY_OWNERS
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/device/public/cpp/bluetooth/bluetooth_system_mojom_traits.h"
namespace mojo {
// static
bool StructTraits<
device::mojom::BluetoothAddressDataView,
std::array<uint8_t, 6>>::Read(device::mojom::BluetoothAddressDataView data,
std::array<uint8_t, 6>* out_address) {
ArrayDataView<uint8_t> address;
data.GetAddressDataView(&address);
if (address.is_null())
return false;
// The size is validated by the generated validation code.
DCHECK_EQ(6u, address.size());
std::copy_n(address.data(), 6, std::begin(*out_address));
return true;
}
} // namespace mojo
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_DEVICE_PUBLIC_CPP_BLUETOOTH_BLUETOOTH_SYSTEM_MOJOM_TRAITS_H_
#define SERVICES_DEVICE_PUBLIC_CPP_BLUETOOTH_BLUETOOTH_SYSTEM_MOJOM_TRAITS_H_
#include <vector>
#include "base/containers/span.h"
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "services/device/public/mojom/bluetooth_system.mojom.h"
namespace mojo {
template <>
class StructTraits<device::mojom::BluetoothAddressDataView,
std::array<uint8_t, 6>> {
public:
static base::span<const uint8_t> address(const std::array<uint8_t, 6>& addr) {
return base::make_span(addr);
}
static bool Read(device::mojom::BluetoothAddressDataView data,
std::array<uint8_t, 6>* out_address);
};
} // namespace mojo
#endif // SERVICES_DEVICE_PUBLIC_CPP_BLUETOOTH_BLUETOOTH_SYSTEM_MOJOM_TRAITS_H_
...@@ -4,7 +4,11 @@ ...@@ -4,7 +4,11 @@
#include "services/device/public/cpp/bluetooth/bluetooth_utils.h" #include "services/device/public/cpp/bluetooth/bluetooth_utils.h"
#include <string>
#include "base/optional.h" #include "base/optional.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "device/bluetooth/string_util_icu.h" #include "device/bluetooth/string_util_icu.h"
#include "device/bluetooth/strings/grit/bluetooth_strings.h" #include "device/bluetooth/strings/grit/bluetooth_strings.h"
...@@ -14,6 +18,16 @@ namespace device { ...@@ -14,6 +18,16 @@ namespace device {
using DeviceType = mojom::BluetoothDeviceInfo::DeviceType; using DeviceType = mojom::BluetoothDeviceInfo::DeviceType;
base::string16 GetBluetoothAddressForDisplay(
const std::array<uint8_t, 6>& address) {
static constexpr char kAddressFormat[] =
"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX";
return base::UTF8ToUTF16(
base::StringPrintf(kAddressFormat, address[0], address[1], address[2],
address[3], address[4], address[5]));
}
base::string16 GetBluetoothDeviceNameForDisplay( base::string16 GetBluetoothDeviceNameForDisplay(
const mojom::BluetoothDeviceInfoPtr& device_info) { const mojom::BluetoothDeviceInfoPtr& device_info) {
if (device_info->name) { if (device_info->name) {
...@@ -22,7 +36,7 @@ base::string16 GetBluetoothDeviceNameForDisplay( ...@@ -22,7 +36,7 @@ base::string16 GetBluetoothDeviceNameForDisplay(
return base::UTF8ToUTF16(device_name); return base::UTF8ToUTF16(device_name);
} }
auto address_utf16 = base::UTF8ToUTF16(device_info->address); auto address_utf16 = GetBluetoothAddressForDisplay(device_info->address);
auto device_type = device_info->device_type; auto device_type = device_info->device_type;
switch (device_type) { switch (device_type) {
case DeviceType::kUnknown: case DeviceType::kUnknown:
......
...@@ -10,6 +10,10 @@ ...@@ -10,6 +10,10 @@
namespace device { namespace device {
// Returns the address suitable for displaying e.g. "AA:BB:CC:DD:00:11".
base::string16 GetBluetoothAddressForDisplay(
const std::array<uint8_t, 6>& address);
// Returns the name of the device suitable for displaying, this may // Returns the name of the device suitable for displaying, this may
// be a synthesized string containing the address and localized type name // be a synthesized string containing the address and localized type name
// if the device has no obtained name. // if the device has no obtained name.
......
...@@ -16,14 +16,16 @@ namespace device { ...@@ -16,14 +16,16 @@ namespace device {
using mojom::BluetoothDeviceInfo; using mojom::BluetoothDeviceInfo;
using mojom::BluetoothDeviceInfoPtr; using mojom::BluetoothDeviceInfoPtr;
constexpr char kAddress[] = "00:00:00:00:00:00"; constexpr std::array<uint8_t, 6> kAddress = {0x00, 0x00, 0x00,
0x00, 0x00, 0x00};
constexpr char kName[] = "Foo Bar"; constexpr char kName[] = "Foo Bar";
constexpr char kUnicodeName[] = "❤❤❤❤"; constexpr char kUnicodeName[] = "❤❤❤❤";
constexpr char kEmptyName[] = ""; constexpr char kEmptyName[] = "";
constexpr char kWhitespaceName[] = " "; constexpr char kWhitespaceName[] = " ";
constexpr char kUnicodeWhitespaceName[] = "    "; constexpr char kUnicodeWhitespaceName[] = "    ";
TEST(BluetoothUtilsTest, NoNameAndNoUnknownDeviceType) { TEST(BluetoothUtilsTest,
GetBluetoothDeviceNameForDisplay_NoNameAndNoUnknownDeviceType) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = kAddress; info->address = kAddress;
info->name = base::nullopt; info->name = base::nullopt;
...@@ -33,7 +35,8 @@ TEST(BluetoothUtilsTest, NoNameAndNoUnknownDeviceType) { ...@@ -33,7 +35,8 @@ TEST(BluetoothUtilsTest, NoNameAndNoUnknownDeviceType) {
GetBluetoothDeviceNameForDisplay(info)); GetBluetoothDeviceNameForDisplay(info));
} }
TEST(BluetoothUtilsTest, NoNameAndComputerDeviceType) { TEST(BluetoothUtilsTest,
GetBluetoothDeviceNameForDisplay_NoNameAndComputerDeviceType) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = kAddress; info->address = kAddress;
info->name = base::nullopt; info->name = base::nullopt;
...@@ -42,7 +45,8 @@ TEST(BluetoothUtilsTest, NoNameAndComputerDeviceType) { ...@@ -42,7 +45,8 @@ TEST(BluetoothUtilsTest, NoNameAndComputerDeviceType) {
GetBluetoothDeviceNameForDisplay(info)); GetBluetoothDeviceNameForDisplay(info));
} }
TEST(BluetoothUtilsTest, NameAndUnknownDeviceType) { TEST(BluetoothUtilsTest,
GetBluetoothDeviceNameForDisplay_NameAndUnknownDeviceType) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = kAddress; info->address = kAddress;
info->name = kName; info->name = kName;
...@@ -50,7 +54,8 @@ TEST(BluetoothUtilsTest, NameAndUnknownDeviceType) { ...@@ -50,7 +54,8 @@ TEST(BluetoothUtilsTest, NameAndUnknownDeviceType) {
EXPECT_EQ(base::UTF8ToUTF16(kName), GetBluetoothDeviceNameForDisplay(info)); EXPECT_EQ(base::UTF8ToUTF16(kName), GetBluetoothDeviceNameForDisplay(info));
} }
TEST(BluetoothUtilsTest, NameAndComputerDeviceType) { TEST(BluetoothUtilsTest,
GetBluetoothDeviceNameForDisplay_NameAndComputerDeviceType) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = kAddress; info->address = kAddress;
info->name = kName; info->name = kName;
...@@ -58,7 +63,7 @@ TEST(BluetoothUtilsTest, NameAndComputerDeviceType) { ...@@ -58,7 +63,7 @@ TEST(BluetoothUtilsTest, NameAndComputerDeviceType) {
EXPECT_EQ(base::UTF8ToUTF16(kName), GetBluetoothDeviceNameForDisplay(info)); EXPECT_EQ(base::UTF8ToUTF16(kName), GetBluetoothDeviceNameForDisplay(info));
} }
TEST(BluetoothUtilsTests, UnicodeName) { TEST(BluetoothUtilsTest, GetBluetoothDeviceNameForDisplay_UnicodeName) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = kAddress; info->address = kAddress;
info->name = kUnicodeName; info->name = kUnicodeName;
...@@ -67,7 +72,7 @@ TEST(BluetoothUtilsTests, UnicodeName) { ...@@ -67,7 +72,7 @@ TEST(BluetoothUtilsTests, UnicodeName) {
GetBluetoothDeviceNameForDisplay(info)); GetBluetoothDeviceNameForDisplay(info));
} }
TEST(BluetoothUtilsTests, EmptyName) { TEST(BluetoothUtilsTest, GetBluetoothDeviceNameForDisplay_EmptyName) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = kAddress; info->address = kAddress;
info->name = kEmptyName; info->name = kEmptyName;
...@@ -76,7 +81,7 @@ TEST(BluetoothUtilsTests, EmptyName) { ...@@ -76,7 +81,7 @@ TEST(BluetoothUtilsTests, EmptyName) {
GetBluetoothDeviceNameForDisplay(info)); GetBluetoothDeviceNameForDisplay(info));
} }
TEST(BluetoothUtilsTests, WhitespaceName) { TEST(BluetoothUtilsTest, GetBluetoothDeviceNameForDisplay_WhitespaceName) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = kAddress; info->address = kAddress;
info->name = kWhitespaceName; info->name = kWhitespaceName;
...@@ -85,7 +90,8 @@ TEST(BluetoothUtilsTests, WhitespaceName) { ...@@ -85,7 +90,8 @@ TEST(BluetoothUtilsTests, WhitespaceName) {
GetBluetoothDeviceNameForDisplay(info)); GetBluetoothDeviceNameForDisplay(info));
} }
TEST(BluetoothUtilsTests, UnicodeWhitespaceName) { TEST(BluetoothUtilsTest,
GetBluetoothDeviceNameForDisplay_UnicodeWhitespaceName) {
BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New(); BluetoothDeviceInfoPtr info = BluetoothDeviceInfo::New();
info->address = kAddress; info->address = kAddress;
info->name = kUnicodeWhitespaceName; info->name = kUnicodeWhitespaceName;
...@@ -94,4 +100,10 @@ TEST(BluetoothUtilsTests, UnicodeWhitespaceName) { ...@@ -94,4 +100,10 @@ TEST(BluetoothUtilsTests, UnicodeWhitespaceName) {
GetBluetoothDeviceNameForDisplay(info)); GetBluetoothDeviceNameForDisplay(info));
} }
TEST(BluetoothUtilsTest, GetBluetoothAddressForDisplay) {
EXPECT_EQ(
base::UTF8ToUTF16("AA:BB:CC:00:11:22"),
GetBluetoothAddressForDisplay({0xAA, 0xBB, 0xCC, 0x00, 0x11, 0x22}));
}
} // namespace device } // namespace device
...@@ -4,6 +4,13 @@ ...@@ -4,6 +4,13 @@
module device.mojom; module device.mojom;
// MAC Address for Bluetooth devices.
// Bluetooth Device Address (or BD_ADDR) is a unique 48-bit identifier assigned
// to each Bluetooth device by the manufacturer.
struct BluetoothAddress {
array<uint8, 6> address;
};
// Holds information about a Bluetooth Device. // Holds information about a Bluetooth Device.
struct BluetoothDeviceInfo { struct BluetoothDeviceInfo {
enum ConnectionState { enum ConnectionState {
...@@ -33,8 +40,7 @@ struct BluetoothDeviceInfo { ...@@ -33,8 +40,7 @@ struct BluetoothDeviceInfo {
}; };
// The MAC Address of the device e.g. AA:BB:CC:00:11:22. // The MAC Address of the device e.g. AA:BB:CC:00:11:22.
// TODO(ortuno): Validate address. BluetoothAddress address;
string address;
// Name of the device. Retrieved during the inquiry response for Classic // Name of the device. Retrieved during the inquiry response for Classic
// devices, from the Advertisement from LE devices, or from the devices itself // devices, from the Advertisement from LE devices, or from the devices itself
......
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
mojom = "//services/device/public/mojom/bluetooth_system.mojom"
public_headers = []
traits_headers =
[ "//services/device/public/cpp/bluetooth/bluetooth_system_mojom_traits.h" ]
sources = [
"//services/device/public/cpp/bluetooth/bluetooth_system_mojom_traits.cc",
]
deps = [
"//base",
]
type_mappings = [ "device.mojom.BluetoothAddress=std::array<uint8_t, 6>" ]
...@@ -2,4 +2,7 @@ ...@@ -2,4 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
typemaps = [ "//services/device/public/mojom/sensor.typemap" ] typemaps = [
"//services/device/public/mojom/sensor.typemap",
"//services/device/public/mojom/bluetooth_system.typemap",
]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment