| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_remote_gatt_descriptor_mac.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_mac.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 8 #import "base/mac/foundation_util.h" |
| 7 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 8 #include "device/bluetooth/bluetooth_adapter_mac.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | 11 #import "device/bluetooth/bluetooth_adapter_mac.h" |
| 12 #import "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| 13 |
| 14 using base::mac::ObjCCast; |
| 10 | 15 |
| 11 namespace device { | 16 namespace device { |
| 12 | 17 |
| 18 std::vector<uint8_t> VectorValueFromObjC(id objc_value) { |
| 19 // According to |
| 20 // https://developer.apple.com/reference/corebluetooth/cbdescriptor some |
| 21 // descriptor values can be NSData, NSString or NSNumber. |
| 22 std::vector<uint8_t> value; |
| 23 NSData* data = ObjCCast<NSData>(objc_value); |
| 24 NSString* as_string = ObjCCast<NSString>(objc_value); |
| 25 NSNumber* as_number = ObjCCast<NSNumber>(objc_value); |
| 26 |
| 27 if (!data && !as_string && as_number) { |
| 28 unsigned short descriptor = [as_number shortValue]; |
| 29 data = [NSData dataWithBytes:&descriptor length:sizeof(descriptor)]; |
| 30 } |
| 31 |
| 32 if (!data && as_string) |
| 33 data = [as_string dataUsingEncoding:NSUTF8StringEncoding]; |
| 34 |
| 35 if (data) { |
| 36 value.resize([data length]); |
| 37 [data getBytes:value.data() length:value.size()]; |
| 38 } else { |
| 39 LOG(WARNING) << "Unexpected value: " |
| 40 << base::SysNSStringToUTF8([objc_value description]); |
| 41 } |
| 42 return value; |
| 43 } |
| 44 |
| 13 BluetoothRemoteGattDescriptorMac::BluetoothRemoteGattDescriptorMac( | 45 BluetoothRemoteGattDescriptorMac::BluetoothRemoteGattDescriptorMac( |
| 14 BluetoothRemoteGattCharacteristicMac* characteristic, | 46 BluetoothRemoteGattCharacteristicMac* characteristic, |
| 15 CBDescriptor* descriptor) | 47 CBDescriptor* descriptor) |
| 16 : gatt_characteristic_(characteristic), | 48 : gatt_characteristic_(characteristic), |
| 17 cb_descriptor_(descriptor, base::scoped_policy::RETAIN) { | 49 cb_descriptor_(descriptor, base::scoped_policy::RETAIN), |
| 18 uuid_ = | 50 value_read_or_write_in_progress_(false) { |
| 19 BluetoothAdapterMac::BluetoothUUIDWithCBUUID([cb_descriptor_.get() UUID]); | 51 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID([cb_descriptor_ UUID]); |
| 20 identifier_ = base::SysNSStringToUTF8( | 52 identifier_ = base::SysNSStringToUTF8( |
| 21 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), | 53 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), |
| 22 (void*)cb_descriptor_]); | 54 (void*)cb_descriptor_]); |
| 23 } | 55 } |
| 24 | 56 |
| 25 std::string BluetoothRemoteGattDescriptorMac::GetIdentifier() const { | 57 std::string BluetoothRemoteGattDescriptorMac::GetIdentifier() const { |
| 26 return identifier_; | 58 return identifier_; |
| 27 } | 59 } |
| 28 | 60 |
| 29 BluetoothUUID BluetoothRemoteGattDescriptorMac::GetUUID() const { | 61 BluetoothUUID BluetoothRemoteGattDescriptorMac::GetUUID() const { |
| 30 return uuid_; | 62 return uuid_; |
| 31 } | 63 } |
| 32 | 64 |
| 33 BluetoothGattCharacteristic::Permissions | 65 BluetoothGattCharacteristic::Permissions |
| 34 BluetoothRemoteGattDescriptorMac::GetPermissions() const { | 66 BluetoothRemoteGattDescriptorMac::GetPermissions() const { |
| 35 NOTIMPLEMENTED(); | 67 NOTIMPLEMENTED(); |
| 36 return BluetoothGattCharacteristic::PERMISSION_NONE; | 68 return BluetoothGattCharacteristic::PERMISSION_NONE; |
| 37 } | 69 } |
| 38 | 70 |
| 39 const std::vector<uint8_t>& BluetoothRemoteGattDescriptorMac::GetValue() const { | 71 const std::vector<uint8_t>& BluetoothRemoteGattDescriptorMac::GetValue() const { |
| 40 return value_; | 72 return value_; |
| 41 } | 73 } |
| 42 | 74 |
| 43 BluetoothRemoteGattDescriptorMac::~BluetoothRemoteGattDescriptorMac() {} | 75 BluetoothRemoteGattDescriptorMac::~BluetoothRemoteGattDescriptorMac() { |
| 76 if (!read_value_callbacks_.first.is_null()) { |
| 77 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 78 callbacks.swap(read_value_callbacks_); |
| 79 callbacks.second.Run(BluetoothGattService::GATT_ERROR_FAILED); |
| 80 } |
| 81 if (!write_value_callbacks_.first.is_null()) { |
| 82 std::pair<base::Closure, ErrorCallback> callbacks; |
| 83 callbacks.swap(write_value_callbacks_); |
| 84 callbacks.second.Run(BluetoothGattService::GATT_ERROR_FAILED); |
| 85 } |
| 86 } |
| 44 | 87 |
| 45 // Returns a pointer to the GATT characteristic that this characteristic | |
| 46 // descriptor belongs to. | |
| 47 BluetoothRemoteGattCharacteristic* | 88 BluetoothRemoteGattCharacteristic* |
| 48 BluetoothRemoteGattDescriptorMac::GetCharacteristic() const { | 89 BluetoothRemoteGattDescriptorMac::GetCharacteristic() const { |
| 49 return static_cast<BluetoothRemoteGattCharacteristic*>(gatt_characteristic_); | 90 return static_cast<BluetoothRemoteGattCharacteristic*>(gatt_characteristic_); |
| 50 } | 91 } |
| 51 | 92 |
| 52 // Sends a read request to a remote characteristic descriptor to read its | 93 // Sends a read request to a remote characteristic descriptor to read its |
| 53 // value. |callback| is called to return the read value on success and | 94 // value. |callback| is called to return the read value on success and |
| 54 // |error_callback| is called for failures. | 95 // |error_callback| is called for failures. |
| 55 void BluetoothRemoteGattDescriptorMac::ReadRemoteDescriptor( | 96 void BluetoothRemoteGattDescriptorMac::ReadRemoteDescriptor( |
| 56 const ValueCallback& callback, | 97 const ValueCallback& callback, |
| 57 const ErrorCallback& error_callback) { | 98 const ErrorCallback& error_callback) { |
| 58 NOTIMPLEMENTED(); | 99 if (value_read_or_write_in_progress_) { |
| 100 VLOG(1) << *this << ": Read failed, already in progress."; |
| 101 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 102 FROM_HERE, |
| 103 base::Bind(error_callback, |
| 104 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
| 105 return; |
| 106 } |
| 107 VLOG(1) << *this << ": Read value."; |
| 108 value_read_or_write_in_progress_ = true; |
| 109 read_value_callbacks_ = std::make_pair(callback, error_callback); |
| 110 [GetCBPeripheral() readValueForDescriptor:cb_descriptor_]; |
| 59 } | 111 } |
| 60 | 112 |
| 61 // Sends a write request to a remote characteristic descriptor, to modify the | |
| 62 // value of the descriptor with the new value |new_value|. |callback| is | |
| 63 // called to signal success and |error_callback| for failures. This method | |
| 64 // only applies to remote descriptors and will fail for those that are locally | |
| 65 // hosted. | |
| 66 void BluetoothRemoteGattDescriptorMac::WriteRemoteDescriptor( | 113 void BluetoothRemoteGattDescriptorMac::WriteRemoteDescriptor( |
| 67 const std::vector<uint8_t>& new_value, | 114 const std::vector<uint8_t>& value, |
| 68 const base::Closure& callback, | 115 const base::Closure& callback, |
| 69 const ErrorCallback& error_callback) { | 116 const ErrorCallback& error_callback) { |
| 70 NOTIMPLEMENTED(); | 117 if (value_read_or_write_in_progress_) { |
| 118 VLOG(1) << *this << ": Write failed, already in progress."; |
| 119 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 120 FROM_HERE, |
| 121 base::Bind(error_callback, |
| 122 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
| 123 return; |
| 124 } |
| 125 VLOG(1) << *this << ": Write value."; |
| 126 value_read_or_write_in_progress_ = true; |
| 127 write_value_callbacks_ = std::make_pair(callback, error_callback); |
| 128 base::scoped_nsobject<NSData> nsdata_value( |
| 129 [[NSData alloc] initWithBytes:value.data() length:value.size()]); |
| 130 [GetCBPeripheral() writeValue:nsdata_value forDescriptor:GetCBDescriptor()]; |
| 131 } |
| 132 |
| 133 void BluetoothRemoteGattDescriptorMac::DidUpdateValueForDescriptor( |
| 134 NSError* error) { |
| 135 if (!value_read_or_write_in_progress_) { |
| 136 VLOG(1) << *this << ": Value updated, no read in progress."; |
| 137 return; |
| 138 } |
| 139 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 140 callbacks.swap(read_value_callbacks_); |
| 141 value_read_or_write_in_progress_ = false; |
| 142 if (error) { |
| 143 BluetoothGattService::GattErrorCode error_code = |
| 144 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 145 VLOG(1) << *this << ": Read value failed with error: " |
| 146 << BluetoothAdapterMac::String(error) |
| 147 << ", converted to error code: " << error_code; |
| 148 callbacks.second.Run(error_code); |
| 149 return; |
| 150 } |
| 151 VLOG(1) << *this << ": Value read."; |
| 152 value_ = VectorValueFromObjC([cb_descriptor_ value]); |
| 153 callbacks.first.Run(value_); |
| 154 } |
| 155 |
| 156 void BluetoothRemoteGattDescriptorMac::DidWriteValueForDescriptor( |
| 157 NSError* error) { |
| 158 if (!value_read_or_write_in_progress_) { |
| 159 VLOG(1) << *this << ": Value written, no write in progress."; |
| 160 return; |
| 161 } |
| 162 std::pair<base::Closure, ErrorCallback> callbacks; |
| 163 callbacks.swap(write_value_callbacks_); |
| 164 value_read_or_write_in_progress_ = false; |
| 165 if (error) { |
| 166 BluetoothGattService::GattErrorCode error_code = |
| 167 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 168 VLOG(1) << *this << ": Write value failed with error: " |
| 169 << BluetoothAdapterMac::String(error) |
| 170 << ", converted to error code: " << error_code; |
| 171 callbacks.second.Run(error_code); |
| 172 return; |
| 173 } |
| 174 VLOG(1) << *this << ": Value written."; |
| 175 callbacks.first.Run(); |
| 176 } |
| 177 |
| 178 CBPeripheral* BluetoothRemoteGattDescriptorMac::GetCBPeripheral() const { |
| 179 return gatt_characteristic_->GetCBPeripheral(); |
| 71 } | 180 } |
| 72 | 181 |
| 73 CBDescriptor* BluetoothRemoteGattDescriptorMac::GetCBDescriptor() const { | 182 CBDescriptor* BluetoothRemoteGattDescriptorMac::GetCBDescriptor() const { |
| 74 return cb_descriptor_.get(); | 183 return cb_descriptor_; |
| 75 } | 184 } |
| 76 | 185 |
| 77 DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<( | 186 DEVICE_BLUETOOTH_EXPORT std::ostream& operator<<( |
| 78 std::ostream& out, | 187 std::ostream& out, |
| 79 const BluetoothRemoteGattDescriptorMac& descriptor) { | 188 const BluetoothRemoteGattDescriptorMac& descriptor) { |
| 80 const BluetoothRemoteGattCharacteristicMac* characteristic_mac = | 189 const BluetoothRemoteGattCharacteristicMac* characteristic_mac = |
| 81 static_cast<const BluetoothRemoteGattCharacteristicMac*>( | 190 static_cast<const BluetoothRemoteGattCharacteristicMac*>( |
| 82 descriptor.GetCharacteristic()); | 191 descriptor.GetCharacteristic()); |
| 83 return out << "<BluetoothRemoteGattServiceMac " | 192 return out << "<BluetoothRemoteGattServiceMac " |
| 84 << descriptor.GetUUID().canonical_value() << "/" << &descriptor | 193 << descriptor.GetUUID().canonical_value() << "/" << &descriptor |
| 85 << ", characteristic: " | 194 << ", characteristic: " |
| 86 << characteristic_mac->GetUUID().canonical_value() << "/" | 195 << characteristic_mac->GetUUID().canonical_value() << "/" |
| 87 << characteristic_mac << ">"; | 196 << characteristic_mac << ">"; |
| 88 } | 197 } |
| 89 | 198 |
| 90 } // namespace device. | 199 } // namespace device. |
| OLD | NEW |