diff options
author | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-12 21:45:30 +0000 |
---|---|---|
committer | youngki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-04-12 21:45:30 +0000 |
commit | 1533d806f2d512899ea9bf5e1ed8a93d8720cbea (patch) | |
tree | 0e46be2201a3d2cd62f090d7c7140b5b555bfb8c /device | |
parent | 59f1766cb69570f2b83a98c9bc7fd72b3da648c8 (diff) | |
download | chromium_src-1533d806f2d512899ea9bf5e1ed8a93d8720cbea.zip chromium_src-1533d806f2d512899ea9bf5e1ed8a93d8720cbea.tar.gz chromium_src-1533d806f2d512899ea9bf5e1ed8a93d8720cbea.tar.bz2 |
Simplified BluetoothDeviceMac.
BluetoothDeviceMac holds IOBluetoothDevice, an instance to a single remote Bluetooth device in OSX. BluetoothDeviceMac will use IOBluetoothDevice instance to run all the device-related functions instead of using cached data.
Also fixed rfcomm_channel bug in BluetoothSocketMac.
BUG=229636
Review URL: https://chromiumcodereview.appspot.com/13983004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194020 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'device')
-rw-r--r-- | device/bluetooth/bluetooth_device_mac.h | 31 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_device_mac.mm | 126 | ||||
-rw-r--r-- | device/bluetooth/bluetooth_socket_mac.mm | 2 |
3 files changed, 67 insertions, 92 deletions
diff --git a/device/bluetooth/bluetooth_device_mac.h b/device/bluetooth/bluetooth_device_mac.h index 653c246..66ab5d6 100644 --- a/device/bluetooth/bluetooth_device_mac.h +++ b/device/bluetooth/bluetooth_device_mac.h @@ -20,7 +20,7 @@ namespace device { class BluetoothDeviceMac : public BluetoothDevice { public: - explicit BluetoothDeviceMac(const IOBluetoothDevice* device); + explicit BluetoothDeviceMac(IOBluetoothDevice* device); virtual ~BluetoothDeviceMac(); // BluetoothDevice override @@ -71,34 +71,7 @@ class BluetoothDeviceMac : public BluetoothDevice { private: friend class BluetoothAdapterMac; - // Computes the fingerprint that can be used to compare the devices. - static uint32 ComputeDeviceFingerprint(const IOBluetoothDevice* device); - - uint32 device_fingerprint() const { - return device_fingerprint_; - } - - // The Bluetooth class of the device, a bitmask that may be decoded using - // https://www.bluetooth.org/Technical/AssignedNumbers/baseband.htm - uint32 bluetooth_class_; - - // The name of the device, as supplied by the remote device. - std::string name_; - - // The Bluetooth address of the device. - std::string address_; - - // Tracked device state, updated by the adapter managing the lifecyle of - // the device. - bool paired_; - bool connected_; - - // The services (identified by UUIDs) that this device provides. - ServiceList service_uuids_; - - // Used to compare the devices. - const uint32 device_fingerprint_; - ServiceRecordList service_record_list_; + IOBluetoothDevice* device_; DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceMac); }; diff --git a/device/bluetooth/bluetooth_device_mac.mm b/device/bluetooth/bluetooth_device_mac.mm index df0b0af..51ef722 100644 --- a/device/bluetooth/bluetooth_device_mac.mm +++ b/device/bluetooth/bluetooth_device_mac.mm @@ -7,11 +7,13 @@ #include <IOBluetooth/Bluetooth.h> #import <IOBluetooth/objc/IOBluetoothDevice.h> #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h> +#import <IOBluetooth/objc/IOBluetoothSDPUUID.h> #include <string> #include "base/basictypes.h" #include "base/hash.h" +#include "base/string_number_conversions.h" #include "base/stringprintf.h" #include "base/strings/sys_string_conversions.h" #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" @@ -31,46 +33,60 @@ MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 #endif // MAC_OS_X_VERSION_10_7 -namespace device { +namespace { -BluetoothDeviceMac::BluetoothDeviceMac(const IOBluetoothDevice* device) - : BluetoothDevice(), - device_fingerprint_(ComputeDeviceFingerprint(device)) { - name_ = base::SysNSStringToUTF8([device name]); - address_ = base::SysNSStringToUTF8([device addressString]); - bluetooth_class_ = [device classOfDevice]; - connected_ = [device isConnected]; - paired_ = [device isPaired]; +// Converts |uuid| to a IOBluetoothSDPUUID instance. +// +// |uuid| must be in the format of XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. +IOBluetoothSDPUUID* GetIOBluetoothSDPUUID(const std::string& uuid) { + DCHECK(uuid.size() == 36); + DCHECK(uuid[8] == '-'); + DCHECK(uuid[13] == '-'); + DCHECK(uuid[18] == '-'); + DCHECK(uuid[23] == '-'); + std::string numbers_only = uuid; + numbers_only.erase(23, 1); + numbers_only.erase(18, 1); + numbers_only.erase(13, 1); + numbers_only.erase(8, 1); + std::vector<uint8> uuid_bytes_vector; + base::HexStringToBytes(numbers_only, &uuid_bytes_vector); + DCHECK(uuid_bytes_vector.size() == 16); - for (IOBluetoothSDPServiceRecord* service in [device services]) { - BluetoothServiceRecord* service_record = - new BluetoothServiceRecordMac(service); - service_record_list_.push_back(service_record); - service_uuids_.push_back(service_record->uuid()); - } + return [IOBluetoothSDPUUID uuidWithBytes:&uuid_bytes_vector[0] + length:uuid_bytes_vector.size()]; +} + +} // namespace + +namespace device { + +BluetoothDeviceMac::BluetoothDeviceMac(IOBluetoothDevice* device) + : BluetoothDevice(), device_([device retain]) { } BluetoothDeviceMac::~BluetoothDeviceMac() { + [device_ release]; } uint32 BluetoothDeviceMac::GetBluetoothClass() const { - return bluetooth_class_; + return [device_ classOfDevice]; } std::string BluetoothDeviceMac::GetDeviceName() const { - return name_; + return base::SysNSStringToUTF8([device_ name]); } std::string BluetoothDeviceMac::GetAddress() const { - return address_; + return base::SysNSStringToUTF8([device_ addressString]); } bool BluetoothDeviceMac::IsPaired() const { - return paired_; + return [device_ isPaired]; } bool BluetoothDeviceMac::IsConnected() const { - return connected_; + return [device_ isConnected]; } bool BluetoothDeviceMac::IsConnectable() const { @@ -81,27 +97,36 @@ bool BluetoothDeviceMac::IsConnecting() const { return false; } +// TODO(youngki): BluetoothServiceRecord is deprecated; implement this method +// without using BluetoothServiceRecord. BluetoothDevice::ServiceList BluetoothDeviceMac::GetServices() const { - return service_uuids_; + ServiceList service_uuids; + for (IOBluetoothSDPServiceRecord* service in [device_ services]) { + BluetoothServiceRecordMac service_record(service); + service_uuids.push_back(service_record.uuid()); + } + return service_uuids; } +// NOTE(youngki): This method is deprecated; it will be removed soon. void BluetoothDeviceMac::GetServiceRecords( const ServiceRecordsCallback& callback, const ErrorCallback& error_callback) { - callback.Run(service_record_list_); + ServiceRecordList service_record_list; + for (IOBluetoothSDPServiceRecord* service in [device_ services]) { + BluetoothServiceRecord* service_record = + new BluetoothServiceRecordMac(service); + service_record_list.push_back(service_record); + } + + callback.Run(service_record_list); } +// NOTE(youngki): This method is deprecated; it will be removed soon. void BluetoothDeviceMac::ProvidesServiceWithName( const std::string& name, const ProvidesServiceCallback& callback) { - for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); - iter != service_record_list_.end(); - ++iter) { - if ((*iter)->name() == name) { - callback.Run(true); - return; - } - } + NOTIMPLEMENTED(); callback.Run(false); } @@ -160,18 +185,14 @@ void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) { void BluetoothDeviceMac::ConnectToService( const std::string& service_uuid, const SocketCallback& callback) { - for (ServiceRecordList::const_iterator iter = service_record_list_.begin(); - iter != service_record_list_.end(); - ++iter) { - if ((*iter)->uuid() == service_uuid) { - // If multiple service records are found, use the first one that works. - scoped_refptr<BluetoothSocket> socket( - BluetoothSocketMac::CreateBluetoothSocket(**iter)); - if (socket.get() != NULL) { - callback.Run(socket); - return; - } - } + IOBluetoothSDPServiceRecord* record = + [device_ getServiceRecordForUUID:GetIOBluetoothSDPUUID(service_uuid)]; + if (record != nil) { + BluetoothServiceRecordMac service_record(record); + scoped_refptr<BluetoothSocket> socket( + BluetoothSocketMac::CreateBluetoothSocket(service_record)); + if (socket.get() != NULL) + callback.Run(socket); } } @@ -188,25 +209,4 @@ void BluetoothDeviceMac::ClearOutOfBandPairingData( NOTIMPLEMENTED(); } -// static -uint32 BluetoothDeviceMac::ComputeDeviceFingerprint( - const IOBluetoothDevice* device) { - std::string device_string = base::StringPrintf("%s|%s|%u|%d|%d", - base::SysNSStringToUTF8([device name]).c_str(), - base::SysNSStringToUTF8([device addressString]).c_str(), - [device classOfDevice], - [device isConnected], - [device isPaired]); - - for (IOBluetoothSDPServiceRecord* record in [device services]) { - base::StringAppendF( - &device_string, - "|%s|%lu", - base::SysNSStringToUTF8([record getServiceName]).c_str(), - static_cast<unsigned long>([[record attributes] count])); - } - - return base::Hash(device_string); -} - } // namespace device diff --git a/device/bluetooth/bluetooth_socket_mac.mm b/device/bluetooth/bluetooth_socket_mac.mm index 83b30a8..31a797c 100644 --- a/device/bluetooth/bluetooth_socket_mac.mm +++ b/device/bluetooth/bluetooth_socket_mac.mm @@ -53,6 +53,8 @@ BluetoothSocketMac::BluetoothSocketMac(IOBluetoothRFCOMMChannel* rfcomm_channel) } BluetoothSocketMac::~BluetoothSocketMac() { + [rfcomm_channel_ setDelegate:nil]; + [rfcomm_channel_ closeChannel]; [rfcomm_channel_ release]; [delegate_ release]; } |