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