Chromium Code Reviews| Index: device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm b/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| index 20f696ea2ea6819cdc3867700b57444c9d169935..e3c57e36b2ec4e99032326c3eba80d2848105f62 100644 |
| --- a/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| +++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm |
| @@ -162,7 +162,32 @@ void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( |
| const std::vector<uint8_t>& new_value, |
| const base::Closure& callback, |
| const ErrorCallback& error_callback) { |
| - NOTIMPLEMENTED(); |
| + if (!IsWritable()) { |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, |
| + base::Bind(error_callback, |
| + BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED)); |
| + return; |
| + } |
| + if (characteristic_value_read_or_write_in_progress_) { |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, |
| + base::Bind(error_callback, |
| + BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); |
| + return; |
| + } |
| + characteristic_value_read_or_write_in_progress_ = true; |
| + write_characteristic_value_callbacks_ = |
| + std::make_pair(callback, error_callback); |
| + base::scoped_nsobject<NSData> nsdata_value( |
| + [[NSData alloc] initWithBytes:new_value.data() length:new_value.size()]); |
| + CBCharacteristicWriteType write_type = GetCBWriteType(); |
| + [gatt_service_->GetCBPeripheral() writeValue:nsdata_value |
| + forCharacteristic:cb_characteristic_ |
| + type:write_type]; |
| + if (write_type == CBCharacteristicWriteWithoutResponse) { |
| + 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.
|
| + } |
| } |
| void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { |
| @@ -188,13 +213,48 @@ void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { |
| callbacks.first.Run(value_); |
| } |
| +void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { |
| + 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
|
| + return; |
| + } |
| + std::pair<base::Closure, ErrorCallback> callbacks; |
| + callbacks.swap(write_characteristic_value_callbacks_); |
| + characteristic_value_read_or_write_in_progress_ = false; |
| + if (error) { |
| + VLOG(1) << "Bluetooth error while reading for characteristic, domain: " |
| + << error.domain.UTF8String << ", error code: " << error.code; |
| + BluetoothGattService::GattErrorCode error_code = |
| + BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); |
| + callbacks.second.Run(error_code); |
| + return; |
| + } |
| + NSData* nsdata_value = cb_characteristic_.get().value; |
| + const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); |
| + std::vector<uint8_t> gatt_value(buffer, buffer + nsdata_value.length); |
| + gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, |
| + value_); |
| + callbacks.first.Run(); |
| +} |
| + |
| bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { |
| return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; |
| } |
| +bool BluetoothRemoteGattCharacteristicMac::IsWritable() const { |
| + BluetoothGattCharacteristic::Properties properties = GetProperties(); |
| + return (properties & BluetoothGattCharacteristic::PROPERTY_WRITE) || |
| + (properties & PROPERTY_WRITE_WITHOUT_RESPONSE); |
| +} |
| + |
| +CBCharacteristicWriteType BluetoothRemoteGattCharacteristicMac::GetCBWriteType() |
| + const { |
| + return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE) |
| + ? CBCharacteristicWriteWithResponse |
| + : CBCharacteristicWriteWithoutResponse; |
| +} |
| + |
| CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() |
| const { |
| return cb_characteristic_.get(); |
| } |
| - |
| } // namespace device. |