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 "device/bluetooth/bluetooth_adapter_mac.h" | 9 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 10 #include "device/bluetooth/bluetooth_device_mac.h" | |
| 10 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 11 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 11 | 12 |
| 12 namespace device { | 13 namespace device { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 static BluetoothGattCharacteristic::Properties ConvertProperties( | 17 static BluetoothGattCharacteristic::Properties ConvertProperties( |
| 17 CBCharacteristicProperties cb_property) { | 18 CBCharacteristicProperties cb_property) { |
| 18 BluetoothGattCharacteristic::Properties result = | 19 BluetoothGattCharacteristic::Properties result = |
| 19 BluetoothGattCharacteristic::PROPERTY_NONE; | 20 BluetoothGattCharacteristic::PROPERTY_NONE; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 result |= BluetoothGattCharacteristic::PROPERTY_INDICATE; | 59 result |= BluetoothGattCharacteristic::PROPERTY_INDICATE; |
| 59 } | 60 } |
| 60 return result; | 61 return result; |
| 61 } | 62 } |
| 62 } // namespace | 63 } // namespace |
| 63 | 64 |
| 64 BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac( | 65 BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac( |
| 65 BluetoothRemoteGattServiceMac* gatt_service, | 66 BluetoothRemoteGattServiceMac* gatt_service, |
| 66 CBCharacteristic* cb_characteristic) | 67 CBCharacteristic* cb_characteristic) |
| 67 : gatt_service_(gatt_service), | 68 : gatt_service_(gatt_service), |
| 68 cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN) { | 69 cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN), |
| 70 characteristic_value_read_in_progress_(false) { | |
| 69 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( | 71 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( |
| 70 [cb_characteristic_.get() UUID]); | 72 [cb_characteristic_.get() UUID]); |
| 71 identifier_ = | 73 identifier_ = |
| 72 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), | 74 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), |
| 73 (void*)cb_characteristic_] | 75 (void*)cb_characteristic_] |
| 74 .UTF8String; | 76 .UTF8String; |
| 75 } | 77 } |
| 76 | 78 |
| 77 BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() {} | 79 BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() {} |
| 78 | 80 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 | 128 |
| 127 void BluetoothRemoteGattCharacteristicMac::StartNotifySession( | 129 void BluetoothRemoteGattCharacteristicMac::StartNotifySession( |
| 128 const NotifySessionCallback& callback, | 130 const NotifySessionCallback& callback, |
| 129 const ErrorCallback& error_callback) { | 131 const ErrorCallback& error_callback) { |
| 130 NOTIMPLEMENTED(); | 132 NOTIMPLEMENTED(); |
| 131 } | 133 } |
| 132 | 134 |
| 133 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( | 135 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( |
| 134 const ValueCallback& callback, | 136 const ValueCallback& callback, |
| 135 const ErrorCallback& error_callback) { | 137 const ErrorCallback& error_callback) { |
| 136 NOTIMPLEMENTED(); | 138 if (!IsReadable()) { |
| 139 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_NOT_PERMITTED); | |
|
ortuno
2016/06/16 18:22:18
ReadRemoteCharacteristic should always be async. P
jlebel
2016/06/22 18:03:58
Can I denounce windows code?
https://cs.chromium.o
| |
| 140 return; | |
| 141 } | |
| 142 if (characteristic_value_read_in_progress_) { | |
| 143 error_callback.Run(BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS); | |
|
ortuno
2016/06/16 18:22:18
Same here, post a task.
jlebel
2016/06/22 18:03:58
Done.
| |
| 144 return; | |
| 145 } | |
| 146 characteristic_value_read_in_progress_ = true; | |
| 147 read_characteristic_value_callbacks_ = | |
| 148 std::make_pair(callback, error_callback); | |
| 149 [gatt_service_->GetCBPeripheral() | |
| 150 readValueForCharacteristic:cb_characteristic_]; | |
| 137 } | 151 } |
| 138 | 152 |
| 139 void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( | 153 void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( |
| 140 const std::vector<uint8_t>& new_value, | 154 const std::vector<uint8_t>& new_value, |
| 141 const base::Closure& callback, | 155 const base::Closure& callback, |
| 142 const ErrorCallback& error_callback) { | 156 const ErrorCallback& error_callback) { |
| 143 NOTIMPLEMENTED(); | 157 NOTIMPLEMENTED(); |
| 144 } | 158 } |
| 145 | 159 |
| 160 void BluetoothRemoteGattCharacteristicMac::DidReadValue(NSError* error) { | |
| 161 if (!characteristic_value_read_in_progress_) { | |
| 162 return; | |
| 163 } | |
| 164 characteristic_value_read_in_progress_ = false; | |
| 165 if (error) { | |
| 166 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " | |
| 167 << error.domain.UTF8String << ", error code: " << error.code; | |
| 168 BluetoothGattService::GattErrorCode error_code = | |
| 169 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | |
| 170 read_characteristic_value_callbacks_.second.Run(error_code); | |
| 171 return; | |
| 172 } | |
| 173 NSData* nsdata_value = cb_characteristic_.get().value; | |
| 174 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); | |
|
ortuno
2016/06/16 18:22:18
nit: Shouldn't you use [nsdata_value bytes] since
jlebel
2016/06/22 18:03:58
Calling [nsdata_value bytes] or doing |nsdata_valu
| |
| 175 value_.assign(buffer, buffer + nsdata_value.length); | |
| 176 read_characteristic_value_callbacks_.first.Run(value_); | |
| 177 } | |
| 178 | |
| 179 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { | |
| 180 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; | |
| 181 } | |
| 182 | |
| 146 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() | 183 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() |
| 147 const { | 184 const { |
| 148 return cb_characteristic_.get(); | 185 return cb_characteristic_.get(); |
| 149 } | 186 } |
| 150 | 187 |
| 151 } // namespace device. | 188 } // namespace device. |
| OLD | NEW |