| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/bluetooth_device_mac.h" | 5 #include "device/bluetooth/bluetooth_classic_device_mac.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/hash.h" | 11 #include "base/hash.h" |
| 12 #include "base/mac/sdk_forward_declarations.h" | 12 #include "base/mac/sdk_forward_declarations.h" |
| 13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 DCHECK_EQ(uuid_str.size(), 32U); | 51 DCHECK_EQ(uuid_str.size(), 32U); |
| 52 uuid_str.insert(8, "-"); | 52 uuid_str.insert(8, "-"); |
| 53 uuid_str.insert(13, "-"); | 53 uuid_str.insert(13, "-"); |
| 54 uuid_str.insert(18, "-"); | 54 uuid_str.insert(18, "-"); |
| 55 uuid_str.insert(23, "-"); | 55 uuid_str.insert(23, "-"); |
| 56 return BluetoothUUID(uuid_str); | 56 return BluetoothUUID(uuid_str); |
| 57 } | 57 } |
| 58 | 58 |
| 59 } // namespace | 59 } // namespace |
| 60 | 60 |
| 61 BluetoothDeviceMac::BluetoothDeviceMac(IOBluetoothDevice* device) | 61 BluetoothClassicDeviceMac::BluetoothClassicDeviceMac(IOBluetoothDevice* device) |
| 62 : device_([device retain]) { | 62 : device_([device retain]) { |
| 63 supports_low_energy_ = false; |
| 64 supports_classic_ = true; |
| 63 } | 65 } |
| 64 | 66 |
| 65 BluetoothDeviceMac::~BluetoothDeviceMac() { | 67 BluetoothClassicDeviceMac::~BluetoothClassicDeviceMac() { |
| 66 } | 68 } |
| 67 | 69 |
| 68 uint32 BluetoothDeviceMac::GetBluetoothClass() const { | 70 uint32 BluetoothClassicDeviceMac::GetBluetoothClass() const { |
| 69 return [device_ classOfDevice]; | 71 return [device_ classOfDevice]; |
| 70 } | 72 } |
| 71 | 73 |
| 72 std::string BluetoothDeviceMac::GetDeviceName() const { | 74 std::string BluetoothClassicDeviceMac::GetDeviceName() const { |
| 73 return base::SysNSStringToUTF8([device_ name]); | 75 return base::SysNSStringToUTF8([device_ name]); |
| 74 } | 76 } |
| 75 | 77 |
| 76 std::string BluetoothDeviceMac::GetAddress() const { | 78 std::string BluetoothClassicDeviceMac::GetAddress() const { |
| 77 return GetDeviceAddress(device_); | 79 return GetDeviceAddress(device_); |
| 78 } | 80 } |
| 79 | 81 |
| 80 BluetoothDevice::VendorIDSource BluetoothDeviceMac::GetVendorIDSource() const { | 82 BluetoothDevice::VendorIDSource BluetoothClassicDeviceMac::GetVendorIDSource() |
| 83 const { |
| 81 return VENDOR_ID_UNKNOWN; | 84 return VENDOR_ID_UNKNOWN; |
| 82 } | 85 } |
| 83 | 86 |
| 84 uint16 BluetoothDeviceMac::GetVendorID() const { | 87 uint16 BluetoothClassicDeviceMac::GetVendorID() const { |
| 85 return 0; | 88 return 0; |
| 86 } | 89 } |
| 87 | 90 |
| 88 uint16 BluetoothDeviceMac::GetProductID() const { | 91 uint16 BluetoothClassicDeviceMac::GetProductID() const { |
| 89 return 0; | 92 return 0; |
| 90 } | 93 } |
| 91 | 94 |
| 92 uint16 BluetoothDeviceMac::GetDeviceID() const { | 95 uint16 BluetoothClassicDeviceMac::GetDeviceID() const { |
| 93 return 0; | 96 return 0; |
| 94 } | 97 } |
| 95 | 98 |
| 96 bool BluetoothDeviceMac::IsPaired() const { | 99 bool BluetoothClassicDeviceMac::IsPaired() const { |
| 97 return [device_ isPaired]; | 100 return [device_ isPaired]; |
| 98 } | 101 } |
| 99 | 102 |
| 100 bool BluetoothDeviceMac::IsConnected() const { | 103 bool BluetoothClassicDeviceMac::IsConnected() const { |
| 101 return [device_ isConnected]; | 104 return [device_ isConnected]; |
| 102 } | 105 } |
| 103 | 106 |
| 104 bool BluetoothDeviceMac::IsConnectable() const { | 107 bool BluetoothClassicDeviceMac::IsConnectable() const { |
| 105 return false; | 108 return false; |
| 106 } | 109 } |
| 107 | 110 |
| 108 bool BluetoothDeviceMac::IsConnecting() const { | 111 bool BluetoothClassicDeviceMac::IsConnecting() const { |
| 109 return false; | 112 return false; |
| 110 } | 113 } |
| 111 | 114 |
| 112 BluetoothDevice::UUIDList BluetoothDeviceMac::GetUUIDs() const { | 115 BluetoothDevice::UUIDList BluetoothClassicDeviceMac::GetUUIDs() const { |
| 113 UUIDList uuids; | 116 UUIDList uuids; |
| 114 for (IOBluetoothSDPServiceRecord* service_record in [device_ services]) { | 117 for (IOBluetoothSDPServiceRecord* service_record in [device_ services]) { |
| 115 IOBluetoothSDPDataElement* service_class_data = | 118 IOBluetoothSDPDataElement* service_class_data = |
| 116 [service_record getAttributeDataElement: | 119 [service_record getAttributeDataElement: |
| 117 kBluetoothSDPAttributeIdentifierServiceClassIDList]; | 120 kBluetoothSDPAttributeIdentifierServiceClassIDList]; |
| 118 if ([service_class_data getTypeDescriptor] == | 121 if ([service_class_data getTypeDescriptor] == |
| 119 kBluetoothSDPDataElementTypeDataElementSequence) { | 122 kBluetoothSDPDataElementTypeDataElementSequence) { |
| 120 BluetoothUUID uuid = ExtractUuid(service_class_data); | 123 BluetoothUUID uuid = ExtractUuid(service_class_data); |
| 121 if (uuid.IsValid()) | 124 if (uuid.IsValid()) |
| 122 uuids.push_back(uuid); | 125 uuids.push_back(uuid); |
| 123 } | 126 } |
| 124 } | 127 } |
| 125 return uuids; | 128 return uuids; |
| 126 } | 129 } |
| 127 | 130 |
| 128 int16 BluetoothDeviceMac::GetInquiryRSSI() const { | 131 int16 BluetoothClassicDeviceMac::GetInquiryRSSI() const { |
| 129 return kUnknownPower; | 132 return kUnknownPower; |
| 130 } | 133 } |
| 131 | 134 |
| 132 int16 BluetoothDeviceMac::GetInquiryTxPower() const { | 135 int16 BluetoothClassicDeviceMac::GetInquiryTxPower() const { |
| 133 NOTIMPLEMENTED(); | 136 NOTIMPLEMENTED(); |
| 134 return kUnknownPower; | 137 return kUnknownPower; |
| 135 } | 138 } |
| 136 | 139 |
| 137 bool BluetoothDeviceMac::ExpectingPinCode() const { | 140 bool BluetoothClassicDeviceMac::ExpectingPinCode() const { |
| 138 NOTIMPLEMENTED(); | 141 NOTIMPLEMENTED(); |
| 139 return false; | 142 return false; |
| 140 } | 143 } |
| 141 | 144 |
| 142 bool BluetoothDeviceMac::ExpectingPasskey() const { | 145 bool BluetoothClassicDeviceMac::ExpectingPasskey() const { |
| 143 NOTIMPLEMENTED(); | 146 NOTIMPLEMENTED(); |
| 144 return false; | 147 return false; |
| 145 } | 148 } |
| 146 | 149 |
| 147 bool BluetoothDeviceMac::ExpectingConfirmation() const { | 150 bool BluetoothClassicDeviceMac::ExpectingConfirmation() const { |
| 148 NOTIMPLEMENTED(); | 151 NOTIMPLEMENTED(); |
| 149 return false; | 152 return false; |
| 150 } | 153 } |
| 151 | 154 |
| 152 void BluetoothDeviceMac::GetConnectionInfo( | 155 void BluetoothClassicDeviceMac::GetConnectionInfo( |
| 153 const ConnectionInfoCallback& callback) { | 156 const ConnectionInfoCallback& callback) { |
| 154 ConnectionInfo connection_info; | 157 ConnectionInfo connection_info; |
| 155 if (![device_ isConnected]) { | 158 if (![device_ isConnected]) { |
| 156 callback.Run(connection_info); | 159 callback.Run(connection_info); |
| 157 return; | 160 return; |
| 158 } | 161 } |
| 159 | 162 |
| 160 connection_info.rssi = [device_ rawRSSI]; | 163 connection_info.rssi = [device_ rawRSSI]; |
| 161 // The API guarantees that +127 is returned in case the RSSI is not readable: | 164 // The API guarantees that +127 is returned in case the RSSI is not readable: |
| 162 // http://goo.gl/bpURYv | 165 // http://goo.gl/bpURYv |
| 163 if (connection_info.rssi == 127) | 166 if (connection_info.rssi == 127) |
| 164 connection_info.rssi = kUnknownPower; | 167 connection_info.rssi = kUnknownPower; |
| 165 | 168 |
| 166 connection_info.transmit_power = | 169 connection_info.transmit_power = |
| 167 GetHostTransmitPower(kReadCurrentTransmitPowerLevel); | 170 GetHostTransmitPower(kReadCurrentTransmitPowerLevel); |
| 168 connection_info.max_transmit_power = | 171 connection_info.max_transmit_power = |
| 169 GetHostTransmitPower(kReadMaximumTransmitPowerLevel); | 172 GetHostTransmitPower(kReadMaximumTransmitPowerLevel); |
| 170 | 173 |
| 171 callback.Run(connection_info); | 174 callback.Run(connection_info); |
| 172 } | 175 } |
| 173 | 176 |
| 174 void BluetoothDeviceMac::Connect( | 177 void BluetoothClassicDeviceMac::Connect( |
| 175 PairingDelegate* pairing_delegate, | 178 PairingDelegate* pairing_delegate, |
| 176 const base::Closure& callback, | 179 const base::Closure& callback, |
| 177 const ConnectErrorCallback& error_callback) { | 180 const ConnectErrorCallback& error_callback) { |
| 178 NOTIMPLEMENTED(); | 181 NOTIMPLEMENTED(); |
| 179 } | 182 } |
| 180 | 183 |
| 181 void BluetoothDeviceMac::SetPinCode(const std::string& pincode) { | 184 void BluetoothClassicDeviceMac::SetPinCode(const std::string& pincode) { |
| 182 NOTIMPLEMENTED(); | 185 NOTIMPLEMENTED(); |
| 183 } | 186 } |
| 184 | 187 |
| 185 void BluetoothDeviceMac::SetPasskey(uint32 passkey) { | 188 void BluetoothClassicDeviceMac::SetPasskey(uint32 passkey) { |
| 186 NOTIMPLEMENTED(); | 189 NOTIMPLEMENTED(); |
| 187 } | 190 } |
| 188 | 191 |
| 189 void BluetoothDeviceMac::ConfirmPairing() { | 192 void BluetoothClassicDeviceMac::ConfirmPairing() { |
| 190 NOTIMPLEMENTED(); | 193 NOTIMPLEMENTED(); |
| 191 } | 194 } |
| 192 | 195 |
| 193 void BluetoothDeviceMac::RejectPairing() { | 196 void BluetoothClassicDeviceMac::RejectPairing() { |
| 194 NOTIMPLEMENTED(); | 197 NOTIMPLEMENTED(); |
| 195 } | 198 } |
| 196 | 199 |
| 197 void BluetoothDeviceMac::CancelPairing() { | 200 void BluetoothClassicDeviceMac::CancelPairing() { |
| 198 NOTIMPLEMENTED(); | 201 NOTIMPLEMENTED(); |
| 199 } | 202 } |
| 200 | 203 |
| 201 void BluetoothDeviceMac::Disconnect(const base::Closure& callback, | 204 void BluetoothClassicDeviceMac::Disconnect( |
| 202 const ErrorCallback& error_callback) { | 205 const base::Closure& callback, |
| 206 const ErrorCallback& error_callback) { |
| 203 NOTIMPLEMENTED(); | 207 NOTIMPLEMENTED(); |
| 204 } | 208 } |
| 205 | 209 |
| 206 void BluetoothDeviceMac::Forget(const ErrorCallback& error_callback) { | 210 void BluetoothClassicDeviceMac::Forget(const ErrorCallback& error_callback) { |
| 207 NOTIMPLEMENTED(); | 211 NOTIMPLEMENTED(); |
| 208 } | 212 } |
| 209 | 213 |
| 210 void BluetoothDeviceMac::ConnectToService( | 214 void BluetoothClassicDeviceMac::ConnectToService( |
| 211 const BluetoothUUID& uuid, | 215 const BluetoothUUID& uuid, |
| 212 const ConnectToServiceCallback& callback, | 216 const ConnectToServiceCallback& callback, |
| 213 const ConnectToServiceErrorCallback& error_callback) { | 217 const ConnectToServiceErrorCallback& error_callback) { |
| 214 scoped_refptr<BluetoothSocketMac> socket = BluetoothSocketMac::CreateSocket(); | 218 scoped_refptr<BluetoothSocketMac> socket = BluetoothSocketMac::CreateSocket(); |
| 215 socket->Connect( | 219 socket->Connect(device_.get(), uuid, base::Bind(callback, socket), |
| 216 device_.get(), uuid, base::Bind(callback, socket), error_callback); | 220 error_callback); |
| 217 } | 221 } |
| 218 | 222 |
| 219 void BluetoothDeviceMac::ConnectToServiceInsecurely( | 223 void BluetoothClassicDeviceMac::ConnectToServiceInsecurely( |
| 220 const BluetoothUUID& uuid, | 224 const BluetoothUUID& uuid, |
| 221 const ConnectToServiceCallback& callback, | 225 const ConnectToServiceCallback& callback, |
| 222 const ConnectToServiceErrorCallback& error_callback) { | 226 const ConnectToServiceErrorCallback& error_callback) { |
| 223 error_callback.Run(kApiUnavailable); | 227 error_callback.Run(kApiUnavailable); |
| 224 } | 228 } |
| 225 | 229 |
| 226 void BluetoothDeviceMac::CreateGattConnection( | 230 void BluetoothClassicDeviceMac::CreateGattConnection( |
| 227 const GattConnectionCallback& callback, | 231 const GattConnectionCallback& callback, |
| 228 const ConnectErrorCallback& error_callback) { | 232 const ConnectErrorCallback& error_callback) { |
| 229 // TODO(armansito): Implement. | 233 // TODO(armansito): Implement. |
| 230 error_callback.Run(ERROR_UNSUPPORTED_DEVICE); | 234 error_callback.Run(ERROR_UNSUPPORTED_DEVICE); |
| 231 } | 235 } |
| 232 | 236 |
| 233 NSDate* BluetoothDeviceMac::GetLastInquiryUpdate() { | 237 NSDate* BluetoothClassicDeviceMac::GetLastInquiryUpdate() { |
| 234 return [device_ getLastInquiryUpdate]; | 238 return [device_ getLastInquiryUpdate]; |
| 235 } | 239 } |
| 236 | 240 |
| 237 int BluetoothDeviceMac::GetHostTransmitPower( | 241 int BluetoothClassicDeviceMac::GetHostTransmitPower( |
| 238 BluetoothHCITransmitPowerLevelType power_level_type) const { | 242 BluetoothHCITransmitPowerLevelType power_level_type) const { |
| 239 IOBluetoothHostController* controller = | 243 IOBluetoothHostController* controller = |
| 240 [IOBluetoothHostController defaultController]; | 244 [IOBluetoothHostController defaultController]; |
| 241 | 245 |
| 242 // Bail if the undocumented API is unavailable on this machine. | 246 // Bail if the undocumented API is unavailable on this machine. |
| 243 SEL selector = @selector( | 247 SEL selector = @selector(BluetoothHCIReadTransmitPowerLevel: |
| 244 BluetoothHCIReadTransmitPowerLevel:inType:outTransmitPowerLevel:); | 248 inType: |
| 249 outTransmitPowerLevel:); |
| 245 if (![controller respondsToSelector:selector]) | 250 if (![controller respondsToSelector:selector]) |
| 246 return kUnknownPower; | 251 return kUnknownPower; |
| 247 | 252 |
| 248 BluetoothHCITransmitPowerLevel power_level; | 253 BluetoothHCITransmitPowerLevel power_level; |
| 249 IOReturn result = | 254 IOReturn result = |
| 250 [controller BluetoothHCIReadTransmitPowerLevel:[device_ connectionHandle] | 255 [controller BluetoothHCIReadTransmitPowerLevel:[device_ connectionHandle] |
| 251 inType:power_level_type | 256 inType:power_level_type |
| 252 outTransmitPowerLevel:&power_level]; | 257 outTransmitPowerLevel:&power_level]; |
| 253 if (result != kIOReturnSuccess) | 258 if (result != kIOReturnSuccess) |
| 254 return kUnknownPower; | 259 return kUnknownPower; |
| 255 | 260 |
| 256 return power_level; | 261 return power_level; |
| 257 } | 262 } |
| 258 | 263 |
| 259 // static | 264 // static |
| 260 std::string BluetoothDeviceMac::GetDeviceAddress(IOBluetoothDevice* device) { | 265 std::string BluetoothClassicDeviceMac::GetDeviceAddress( |
| 266 IOBluetoothDevice* device) { |
| 261 return CanonicalizeAddress(base::SysNSStringToUTF8([device addressString])); | 267 return CanonicalizeAddress(base::SysNSStringToUTF8([device addressString])); |
| 262 } | 268 } |
| 263 | 269 |
| 264 } // namespace device | 270 } // namespace device |
| OLD | NEW |