Skip to content

Commit

Permalink
[Bluetooth, Mac] Normalize device addresses to have the format XX:XX:…
Browse files Browse the repository at this point in the history
…XX:XX:XX:XX

BUG=368902
TEST=(see bug)
R=rpaquay@chromium.org

Review URL: https://codereview.chromium.org/266693002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267704 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
isherman@chromium.org committed May 2, 2014
1 parent b4d4bbb commit 2a53aef
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 41 deletions.
14 changes: 4 additions & 10 deletions device/bluetooth/bluetooth_adapter_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ - (NSString*)nameAsString;
- (BluetoothHCIPowerState)powerState;
@end

@interface IOBluetoothDevice (LionSDKDeclarations)
- (NSString*)addressString;
@end

@protocol IOBluetoothDeviceInquiryDelegate
- (void)deviceInquiryStarted:(IOBluetoothDeviceInquiry*)sender;
- (void)deviceInquiryDeviceFound:(IOBluetoothDeviceInquiry*)sender
Expand Down Expand Up @@ -232,9 +228,8 @@ - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender

if (controller != nil) {
name = base::SysNSStringToUTF8([controller nameAsString]);
// TODO(isherman): Convert the address format to XX:XX:XX:XX:XX:XX rather
// than xx-xx-xx-xx-xx-xx.
address = base::SysNSStringToUTF8([controller addressAsString]);
address = BluetoothDeviceMac::NormalizeAddress(
base::SysNSStringToUTF8([controller addressAsString]));
powered = ([controller powerState] == kBluetoothHCIPowerStateON);
}

Expand Down Expand Up @@ -273,8 +268,7 @@ - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
void BluetoothAdapterMac::UpdateDevices(NSArray* devices) {
STLDeleteValues(&devices_);
for (IOBluetoothDevice* device in devices) {
std::string device_address =
base::SysNSStringToUTF8([device addressString]);
std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device);
devices_[device_address] = new BluetoothDeviceMac(device);
}
}
Expand All @@ -298,7 +292,7 @@ - (void)deviceInquiryComplete:(IOBluetoothDeviceInquiry*)sender
void BluetoothAdapterMac::DeviceFound(IOBluetoothDeviceInquiry* inquiry,
IOBluetoothDevice* device) {
DCHECK_EQ(device_inquiry_, inquiry);
std::string device_address = base::SysNSStringToUTF8([device addressString]);
std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device);
if (discovered_devices_.find(device_address) == discovered_devices_.end()) {
BluetoothDeviceMac device_mac(device);
FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_,
Expand Down
10 changes: 10 additions & 0 deletions device/bluetooth/bluetooth_device_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class BluetoothDeviceMac : public BluetoothDevice {
const base::Closure& callback,
const ErrorCallback& error_callback) OVERRIDE;

// Returns the Bluetooth address for the |device|. The returned address has a
// normalized format (see below).
static std::string GetDeviceAddress(IOBluetoothDevice* device);

// Returns the address formatted according to Chrome's expectations rather
// than per the system convention: octets are separated by colons rather than
// by dashes. That is, the returned format is XX:XX:XX:XX:XX:XX rather than
// xx-xx-xx-xx-xx-xx.
static std::string NormalizeAddress(const std::string& address);

protected:
// BluetoothDevice override
virtual std::string GetDeviceName() const OVERRIDE;
Expand Down
16 changes: 15 additions & 1 deletion device/bluetooth/bluetooth_device_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/hash.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
Expand Down Expand Up @@ -70,7 +71,7 @@ @interface IOBluetoothHostController (UndocumentedAPI)
}

std::string BluetoothDeviceMac::GetAddress() const {
return base::SysNSStringToUTF8([device_ addressString]);
return GetDeviceAddress(device_);
}

BluetoothDevice::VendorIDSource BluetoothDeviceMac::GetVendorIDSource() const {
Expand Down Expand Up @@ -234,4 +235,17 @@ @interface IOBluetoothHostController (UndocumentedAPI)
return power_level;
}

// static
std::string BluetoothDeviceMac::GetDeviceAddress(IOBluetoothDevice* device) {
return NormalizeAddress(base::SysNSStringToUTF8([device addressString]));
}

// static
std::string BluetoothDeviceMac::NormalizeAddress(const std::string& address) {
std::string normalized;
base::ReplaceChars(address, "-", ":", &normalized);
StringToUpperASCII(&normalized);
return normalized;
}

} // namespace device
14 changes: 2 additions & 12 deletions device/bluetooth/bluetooth_profile_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@
#include "device/bluetooth/bluetooth_device_mac.h"
#include "device/bluetooth/bluetooth_socket_mac.h"

// Replicate specific 10.7 SDK declarations for building with prior SDKs.
#if !defined(MAC_OS_X_VERSION_10_7) || \
MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7

@interface IOBluetoothDevice (LionSDKDeclarations)
- (NSString*)addressString;
@end

#endif // MAC_OS_X_VERSION_10_7

namespace device {
namespace {

Expand Down Expand Up @@ -281,7 +271,7 @@ - (void)rfcommChannelOpened:(IOBluetoothUserNotification*)notification
return;
}

std::string device_address = base::SysNSStringToUTF8([device addressString]);
std::string device_address = BluetoothDeviceMac::GetDeviceAddress(device);
BluetoothSocketMac::Connect(
record,
base::Bind(OnConnectSuccess,
Expand All @@ -295,7 +285,7 @@ - (void)rfcommChannelOpened:(IOBluetoothUserNotification*)notification
IOBluetoothRFCOMMChannel* rfcomm_channel) {
DCHECK_EQ([rfcomm_channel getChannelID], rfcomm_channel_id_);
std::string device_address =
base::SysNSStringToUTF8([[rfcomm_channel getDevice] addressString]);
BluetoothDeviceMac::GetDeviceAddress([rfcomm_channel getDevice]);
BluetoothSocketMac::AcceptConnection(
rfcomm_channel,
base::Bind(OnConnectSuccess,
Expand Down
24 changes: 6 additions & 18 deletions device/bluetooth/bluetooth_socket_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,11 @@
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "device/bluetooth/bluetooth_device_mac.h"
#include "device/bluetooth/bluetooth_service_record.h"
#include "device/bluetooth/bluetooth_service_record_mac.h"
#include "net/base/io_buffer.h"

// Replicate specific 10.7 SDK declarations for building with prior SDKs.
#if !defined(MAC_OS_X_VERSION_10_7) || \
MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7

@interface IOBluetoothDevice (LionSDKDeclarations)
- (NSString*)addressString;
@end

#endif // MAC_OS_X_VERSION_10_7

@interface BluetoothRFCOMMChannelDelegate
: NSObject <IOBluetoothRFCOMMChannelDelegate> {
@private
Expand Down Expand Up @@ -181,7 +172,7 @@ void empty_queue(std::queue<T>& queue) {
if (status != kIOReturnSuccess) {
std::stringstream error;
error << "Failed to connect bluetooth socket ("
<< base::SysNSStringToUTF8([device addressString]) << "): (" << status
<< BluetoothDeviceMac::GetDeviceAddress(device) << "): (" << status
<< ")";
error_callback.Run(error.str());
return;
Expand Down Expand Up @@ -218,9 +209,8 @@ void empty_queue(std::queue<T>& queue) {
ReleaseChannel();
std::stringstream error;
error << "Failed to connect bluetooth socket ("
<< base::SysNSStringToUTF8(
[[rfcomm_channel_ getDevice] addressString]) << "): ("
<< status << ")";
<< BluetoothDeviceMac::GetDeviceAddress([rfcomm_channel_ getDevice])
<< "): (" << status << ")";
temp->error_callback.Run(error.str());
return;
}
Expand Down Expand Up @@ -339,8 +329,7 @@ void empty_queue(std::queue<T>& queue) {
if (status != kIOReturnSuccess) {
std::stringstream error;
error << "Failed to connect bluetooth socket ("
<< base::SysNSStringToUTF8(
[[rfcomm_channel_ getDevice] addressString])
<< BluetoothDeviceMac::GetDeviceAddress([rfcomm_channel_ getDevice])
<< "): (" << status << ")";
// Remember the first error only
if (request->status == kIOReturnSuccess)
Expand Down Expand Up @@ -395,8 +384,7 @@ void empty_queue(std::queue<T>& queue) {
if (!request->error_signaled) {
std::stringstream error;
error << "Failed to connect bluetooth socket ("
<< base::SysNSStringToUTF8(
[[rfcomm_channel_ getDevice] addressString])
<< BluetoothDeviceMac::GetDeviceAddress([rfcomm_channel_ getDevice])
<< "): (" << status << ")";
request->error_signaled = true;
request->error_callback.Run(error.str());
Expand Down

0 comments on commit 2a53aef

Please sign in to comment.