Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" | |
|
armansito
2015/01/24 18:07:09
Copyright header?
dvh
2015/01/27 00:14:55
Done.
| |
| 2 | |
| 3 #import <CoreFoundation/CoreFoundation.h> | |
| 4 | |
| 5 #include "base/mac/scoped_cftyperef.h" | |
| 6 | |
| 7 using namespace device; | |
| 8 | |
| 9 namespace { | |
|
armansito
2015/01/24 18:07:09
nit: new line here.
dvh
2015/01/27 00:14:55
Done.
| |
| 10 // Converts a CBUUID to a Cocoa string. | |
| 11 // | |
| 12 // The string representation can have the following formats: | |
| 13 // - 16 bit: xxxx | |
| 14 // - 128 bit: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
| 15 // CBUUID supports only 16 bits and 128 bits formats. | |
| 16 // | |
| 17 // In OSX < 10.10, -[uuid UUIDString] method is not implemented. It's why we | |
| 18 // need to provide this function. | |
| 19 NSString* stringWithCBUUID(CBUUID* uuid) { | |
| 20 NSData* data = [uuid data]; | |
| 21 | |
| 22 NSUInteger bytesToConvert = [data length]; | |
| 23 const unsigned char* uuidBytes = (const unsigned char*)[data bytes]; | |
| 24 NSMutableString* outputString = [NSMutableString stringWithCapacity:16]; | |
| 25 | |
| 26 for (NSUInteger currentByteIndex = 0; currentByteIndex < bytesToConvert; | |
| 27 currentByteIndex++) { | |
| 28 switch (currentByteIndex) { | |
| 29 case 3: | |
| 30 case 5: | |
| 31 case 7: | |
| 32 case 9: | |
| 33 [outputString appendFormat:@"%02x-", uuidBytes[currentByteIndex]]; | |
| 34 break; | |
| 35 default: | |
| 36 [outputString appendFormat:@"%02x", uuidBytes[currentByteIndex]]; | |
| 37 } | |
| 38 } | |
| 39 return outputString; | |
| 40 } | |
| 41 | |
| 42 // Converts a CBUUID to a BluetoothUUID. | |
| 43 device::BluetoothUUID BluetoothUUIDWithCBUUID(CBUUID* uuid) { | |
| 44 NSString* uuidString = nil; | |
| 45 if ([uuid respondsToSelector:@selector(UUIDString)]) { | |
| 46 uuidString = [uuid performSelector:@selector(UUIDString)]; | |
| 47 } else { | |
| 48 uuidString = stringWithCBUUID(uuid); | |
| 49 } | |
| 50 std::string uuid_c_string([uuidString UTF8String]); | |
| 51 return BluetoothUUID(uuid_c_string); | |
| 52 } | |
|
armansito
2015/01/24 18:07:10
nit: new line.
| |
| 53 } | |
|
armansito
2015/01/24 18:07:10
nit: Closing brace for namespace should read "} /
dvh
2015/01/27 00:14:55
Done.
| |
| 54 | |
| 55 BluetoothLowEnergyDeviceMac::BluetoothLowEnergyDeviceMac( | |
| 56 CBPeripheral* peripheral, NSDictionary* advertisementData, int rssi) { | |
| 57 Update(peripheral, advertisementData, rssi); | |
| 58 } | |
| 59 | |
| 60 BluetoothLowEnergyDeviceMac::~BluetoothLowEnergyDeviceMac() {} | |
| 61 | |
| 62 void BluetoothLowEnergyDeviceMac::Update(CBPeripheral* peripheral, | |
| 63 NSDictionary* advertisementData, | |
| 64 int rssi) { | |
| 65 peripheral_.reset([peripheral retain]); | |
| 66 rssi_ = rssi; | |
| 67 ClearServiceData(); | |
| 68 NSNumber* nbConnectable = | |
| 69 [advertisementData objectForKey:CBAdvertisementDataIsConnectable]; | |
| 70 connectable_ = [nbConnectable boolValue]; | |
| 71 NSDictionary* serviceData = | |
| 72 [advertisementData objectForKey:CBAdvertisementDataServiceDataKey]; | |
| 73 for (CBUUID* uuid in serviceData) { | |
| 74 NSData* data = [serviceData objectForKey:uuid]; | |
| 75 BluetoothUUID serviceUUID = BluetoothUUIDWithCBUUID(uuid); | |
| 76 SetServiceData(serviceUUID, (const char*)[data bytes], [data length]); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 std::string BluetoothLowEnergyDeviceMac::GetIdentifier() const { | |
| 81 if ([peripheral_ respondsToSelector:@selector(identifier)]) { | |
| 82 // When -[CBPeripheral identifier] is available. | |
| 83 NSUUID* uuid = [peripheral_ performSelector:@selector(identifier)]; | |
| 84 NSString* uuidString = [uuid UUIDString]; | |
| 85 return std::string([uuidString UTF8String]); | |
|
armansito
2015/01/24 18:07:09
nit: Remove else since you already have an early r
dvh
2015/01/27 00:14:55
Done.
| |
| 86 } else { | |
| 87 base::ScopedCFTypeRef<CFStringRef> str( | |
| 88 CFUUIDCreateString(NULL, [peripheral_ UUID])); | |
| 89 base::ScopedCFTypeRef<CFDataRef> data(CFStringCreateExternalRepresentation( | |
| 90 NULL, str, kCFStringEncodingASCII, 0)); | |
| 91 return std::string((const char*)CFDataGetBytePtr(data), | |
| 92 CFDataGetLength(data)); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 uint32 BluetoothLowEnergyDeviceMac::GetBluetoothClass() const { return 0; } | |
| 97 | |
| 98 std::string BluetoothLowEnergyDeviceMac::GetAddress() const { | |
| 99 return std::string(); | |
| 100 } | |
| 101 | |
| 102 BluetoothDevice::VendorIDSource BluetoothLowEnergyDeviceMac::GetVendorIDSource() | |
| 103 const { | |
| 104 return VENDOR_ID_UNKNOWN; | |
| 105 } | |
| 106 | |
| 107 uint16 BluetoothLowEnergyDeviceMac::GetVendorID() const { return 0; } | |
| 108 | |
| 109 uint16 BluetoothLowEnergyDeviceMac::GetProductID() const { return 0; } | |
| 110 | |
| 111 uint16 BluetoothLowEnergyDeviceMac::GetDeviceID() const { return 0; } | |
| 112 | |
| 113 int BluetoothLowEnergyDeviceMac::GetRSSI() const { return rssi_; } | |
| 114 | |
| 115 bool BluetoothLowEnergyDeviceMac::IsPaired() const { return false; } | |
| 116 | |
| 117 bool BluetoothLowEnergyDeviceMac::IsConnected() const { | |
| 118 return [peripheral_ isConnected]; | |
| 119 } | |
| 120 | |
| 121 bool BluetoothLowEnergyDeviceMac::IsConnectable() const { return connectable_; } | |
| 122 | |
| 123 bool BluetoothLowEnergyDeviceMac::IsConnecting() const { return false; } | |
| 124 | |
| 125 BluetoothDevice::UUIDList BluetoothLowEnergyDeviceMac::GetUUIDs() const { | |
| 126 return std::vector<device::BluetoothUUID>(); | |
| 127 } | |
| 128 | |
| 129 bool BluetoothLowEnergyDeviceMac::ExpectingPinCode() const { return false; } | |
| 130 | |
| 131 bool BluetoothLowEnergyDeviceMac::ExpectingPasskey() const { return false; } | |
| 132 | |
| 133 bool BluetoothLowEnergyDeviceMac::ExpectingConfirmation() const { | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 void BluetoothLowEnergyDeviceMac::GetConnectionInfo( | |
| 138 const ConnectionInfoCallback& callback) {} | |
|
armansito
2015/01/24 18:07:10
Add NOTIMPLEMENTED() ?
dvh
2015/01/27 00:14:55
Done.
| |
| 139 | |
| 140 void BluetoothLowEnergyDeviceMac::Connect( | |
| 141 PairingDelegate* pairing_delegate, const base::Closure& callback, | |
| 142 const ConnectErrorCallback& error_callback) { | |
| 143 NOTIMPLEMENTED(); | |
| 144 } | |
| 145 | |
| 146 void BluetoothLowEnergyDeviceMac::SetPinCode(const std::string& pincode) { | |
| 147 NOTIMPLEMENTED(); | |
| 148 } | |
| 149 | |
| 150 void BluetoothLowEnergyDeviceMac::SetPasskey(uint32 passkey) { | |
| 151 NOTIMPLEMENTED(); | |
| 152 } | |
| 153 | |
| 154 void BluetoothLowEnergyDeviceMac::ConfirmPairing() { NOTIMPLEMENTED(); } | |
| 155 | |
| 156 void BluetoothLowEnergyDeviceMac::RejectPairing() { NOTIMPLEMENTED(); } | |
| 157 | |
| 158 void BluetoothLowEnergyDeviceMac::CancelPairing() { NOTIMPLEMENTED(); } | |
| 159 | |
| 160 void BluetoothLowEnergyDeviceMac::Disconnect( | |
| 161 const base::Closure& callback, const ErrorCallback& error_callback) { | |
| 162 NOTIMPLEMENTED(); | |
| 163 } | |
| 164 | |
| 165 void BluetoothLowEnergyDeviceMac::Forget(const ErrorCallback& error_callback) { | |
| 166 NOTIMPLEMENTED(); | |
| 167 } | |
| 168 | |
| 169 void BluetoothLowEnergyDeviceMac::ConnectToService( | |
| 170 const BluetoothUUID& uuid, const ConnectToServiceCallback& callback, | |
| 171 const ConnectToServiceErrorCallback& error_callback) { | |
| 172 NOTIMPLEMENTED(); | |
| 173 } | |
| 174 | |
| 175 void BluetoothLowEnergyDeviceMac::ConnectToServiceInsecurely( | |
| 176 const device::BluetoothUUID& uuid, const ConnectToServiceCallback& callback, | |
| 177 const ConnectToServiceErrorCallback& error_callback) { | |
| 178 NOTIMPLEMENTED(); | |
| 179 } | |
| 180 | |
| 181 void BluetoothLowEnergyDeviceMac::CreateGattConnection( | |
| 182 const GattConnectionCallback& callback, | |
| 183 const ConnectErrorCallback& error_callback) { | |
| 184 NOTIMPLEMENTED(); | |
| 185 } | |
| 186 | |
| 187 std::string BluetoothLowEnergyDeviceMac::GetDeviceName() const { | |
| 188 NSData* data = [[peripheral_ name] dataUsingEncoding:NSUTF8StringEncoding | |
| 189 allowLossyConversion:YES]; | |
| 190 return std::string((const char*)[data bytes], [data length]); | |
| 191 } | |
| OLD | NEW |