Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_characteristic_mac.h" | 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| 6 | 6 |
| 7 #import <CoreBluetooth/CoreBluetooth.h> | 7 #import <CoreBluetooth/CoreBluetooth.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 read_characteristic_value_callbacks_ = | 155 read_characteristic_value_callbacks_ = |
| 156 std::make_pair(callback, error_callback); | 156 std::make_pair(callback, error_callback); |
| 157 [gatt_service_->GetCBPeripheral() | 157 [gatt_service_->GetCBPeripheral() |
| 158 readValueForCharacteristic:cb_characteristic_]; | 158 readValueForCharacteristic:cb_characteristic_]; |
| 159 } | 159 } |
| 160 | 160 |
| 161 void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( | 161 void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( |
| 162 const std::vector<uint8_t>& new_value, | 162 const std::vector<uint8_t>& new_value, |
| 163 const base::Closure& callback, | 163 const base::Closure& callback, |
| 164 const ErrorCallback& error_callback) { | 164 const ErrorCallback& error_callback) { |
| 165 NOTIMPLEMENTED(); | 165 if (!IsWritable()) { |
| 166 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 167 FROM_HERE, | |
| 168 base::Bind(error_callback, | |
| 169 BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED)); | |
| 170 return; | |
| 171 } | |
| 172 if (characteristic_value_read_or_write_in_progress_) { | |
| 173 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 174 FROM_HERE, | |
| 175 base::Bind(error_callback, | |
| 176 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | |
| 177 return; | |
| 178 } | |
| 179 characteristic_value_read_or_write_in_progress_ = true; | |
| 180 write_characteristic_value_callbacks_ = | |
| 181 std::make_pair(callback, error_callback); | |
| 182 base::scoped_nsobject<NSData> nsdata_value( | |
| 183 [[NSData alloc] initWithBytes:new_value.data() length:new_value.size()]); | |
| 184 CBCharacteristicWriteType write_type = GetCBWriteType(); | |
| 185 [gatt_service_->GetCBPeripheral() writeValue:nsdata_value | |
| 186 forCharacteristic:cb_characteristic_ | |
| 187 type:write_type]; | |
| 188 if (write_type == CBCharacteristicWriteWithoutResponse) { | |
| 189 DidWriteValue(nil); | |
|
ortuno
2016/06/22 21:37:45
We still want this function to be async so you nee
jlebel
2016/06/23 18:33:01
Done.
| |
| 190 } | |
| 166 } | 191 } |
| 167 | 192 |
| 168 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { | 193 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { |
| 169 if (!characteristic_value_read_or_write_in_progress_) { | 194 if (!characteristic_value_read_or_write_in_progress_) { |
| 170 return; | 195 return; |
| 171 } | 196 } |
| 172 std::pair<ValueCallback, ErrorCallback> callbacks; | 197 std::pair<ValueCallback, ErrorCallback> callbacks; |
| 173 callbacks.swap(read_characteristic_value_callbacks_); | 198 callbacks.swap(read_characteristic_value_callbacks_); |
| 174 characteristic_value_read_or_write_in_progress_ = false; | 199 characteristic_value_read_or_write_in_progress_ = false; |
| 175 if (error) { | 200 if (error) { |
| 176 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " | 201 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " |
| 177 << error.domain.UTF8String << ", error code: " << error.code; | 202 << error.domain.UTF8String << ", error code: " << error.code; |
| 178 BluetoothGattService::GattErrorCode error_code = | 203 BluetoothGattService::GattErrorCode error_code = |
| 179 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | 204 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| 180 callbacks.second.Run(error_code); | 205 callbacks.second.Run(error_code); |
| 181 return; | 206 return; |
| 182 } | 207 } |
| 183 NSData* nsdata_value = cb_characteristic_.get().value; | 208 NSData* nsdata_value = cb_characteristic_.get().value; |
| 184 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); | 209 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); |
| 185 value_.assign(buffer, buffer + nsdata_value.length); | 210 value_.assign(buffer, buffer + nsdata_value.length); |
| 186 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, | 211 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, |
| 187 value_); | 212 value_); |
| 188 callbacks.first.Run(value_); | 213 callbacks.first.Run(value_); |
| 189 } | 214 } |
| 190 | 215 |
| 216 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { | |
| 217 if (!characteristic_value_read_or_write_in_progress_) { | |
|
ortuno
2016/06/22 21:37:45
Please add a comment explaining when would this fu
jlebel
2016/06/23 18:33:01
I'm not sure when it would be the case. For me it
ortuno
2016/06/23 19:21:16
Now that I think about it we have the same line in
jlebel
2016/06/23 19:28:29
I'm not an expert in the BLE protocol, I don't kno
| |
| 218 return; | |
| 219 } | |
| 220 std::pair<base::Closure, ErrorCallback> callbacks; | |
| 221 callbacks.swap(write_characteristic_value_callbacks_); | |
| 222 characteristic_value_read_or_write_in_progress_ = false; | |
| 223 if (error) { | |
| 224 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " | |
| 225 << error.domain.UTF8String << ", error code: " << error.code; | |
| 226 BluetoothGattService::GattErrorCode error_code = | |
| 227 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | |
| 228 callbacks.second.Run(error_code); | |
| 229 return; | |
| 230 } | |
| 231 NSData* nsdata_value = cb_characteristic_.get().value; | |
| 232 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); | |
| 233 std::vector<uint8_t> gatt_value(buffer, buffer + nsdata_value.length); | |
| 234 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, | |
| 235 value_); | |
| 236 callbacks.first.Run(); | |
| 237 } | |
| 238 | |
| 191 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { | 239 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { |
| 192 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; | 240 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; |
| 193 } | 241 } |
| 194 | 242 |
| 243 bool BluetoothRemoteGattCharacteristicMac::IsWritable() const { | |
| 244 BluetoothGattCharacteristic::Properties properties = GetProperties(); | |
| 245 return (properties & BluetoothGattCharacteristic::PROPERTY_WRITE) || | |
| 246 (properties & PROPERTY_WRITE_WITHOUT_RESPONSE); | |
| 247 } | |
| 248 | |
| 249 CBCharacteristicWriteType BluetoothRemoteGattCharacteristicMac::GetCBWriteType() | |
| 250 const { | |
| 251 return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE) | |
| 252 ? CBCharacteristicWriteWithResponse | |
| 253 : CBCharacteristicWriteWithoutResponse; | |
| 254 } | |
| 255 | |
| 195 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() | 256 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() |
| 196 const { | 257 const { |
| 197 return cb_characteristic_.get(); | 258 return cb_characteristic_.get(); |
| 198 } | 259 } |
| 199 | |
| 200 } // namespace device. | 260 } // namespace device. |
| OLD | NEW |