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" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
9 #include "device/bluetooth/bluetooth_adapter_mac.h" | 11 #include "device/bluetooth/bluetooth_adapter_mac.h" |
12 #include "device/bluetooth/bluetooth_device_mac.h" | |
10 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 13 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
11 | 14 |
12 namespace device { | 15 namespace device { |
13 | 16 |
14 namespace { | 17 namespace { |
15 | 18 |
16 static BluetoothGattCharacteristic::Properties ConvertProperties( | 19 static BluetoothGattCharacteristic::Properties ConvertProperties( |
17 CBCharacteristicProperties cb_property) { | 20 CBCharacteristicProperties cb_property) { |
18 BluetoothGattCharacteristic::Properties result = | 21 BluetoothGattCharacteristic::Properties result = |
19 BluetoothGattCharacteristic::PROPERTY_NONE; | 22 BluetoothGattCharacteristic::PROPERTY_NONE; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 result |= BluetoothGattCharacteristic::PROPERTY_INDICATE; | 61 result |= BluetoothGattCharacteristic::PROPERTY_INDICATE; |
59 } | 62 } |
60 return result; | 63 return result; |
61 } | 64 } |
62 } // namespace | 65 } // namespace |
63 | 66 |
64 BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac( | 67 BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac( |
65 BluetoothRemoteGattServiceMac* gatt_service, | 68 BluetoothRemoteGattServiceMac* gatt_service, |
66 CBCharacteristic* cb_characteristic) | 69 CBCharacteristic* cb_characteristic) |
67 : gatt_service_(gatt_service), | 70 : gatt_service_(gatt_service), |
68 cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN) { | 71 cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN), |
72 characteristic_value_read_or_write_in_progress_(false) { | |
69 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( | 73 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( |
70 [cb_characteristic_.get() UUID]); | 74 [cb_characteristic_.get() UUID]); |
71 identifier_ = | 75 identifier_ = |
72 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), | 76 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), |
73 (void*)cb_characteristic_] | 77 (void*)cb_characteristic_] |
74 .UTF8String; | 78 .UTF8String; |
75 } | 79 } |
76 | 80 |
77 BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() {} | 81 BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() {} |
78 | 82 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 | 130 |
127 void BluetoothRemoteGattCharacteristicMac::StartNotifySession( | 131 void BluetoothRemoteGattCharacteristicMac::StartNotifySession( |
128 const NotifySessionCallback& callback, | 132 const NotifySessionCallback& callback, |
129 const ErrorCallback& error_callback) { | 133 const ErrorCallback& error_callback) { |
130 NOTIMPLEMENTED(); | 134 NOTIMPLEMENTED(); |
131 } | 135 } |
132 | 136 |
133 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( | 137 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( |
134 const ValueCallback& callback, | 138 const ValueCallback& callback, |
135 const ErrorCallback& error_callback) { | 139 const ErrorCallback& error_callback) { |
136 NOTIMPLEMENTED(); | 140 if (!IsReadable()) { |
141 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
142 FROM_HERE, | |
143 base::Bind(error_callback, | |
144 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | |
msarda
2016/06/27 11:52:47
I'm wondering if GATT_ERROR_IN_PROGRESS is the rig
jlebel
2016/06/27 12:09:20
Done.
| |
145 return; | |
146 } | |
147 if (characteristic_value_read_or_write_in_progress_) { | |
148 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
149 FROM_HERE, | |
150 base::Bind(error_callback, | |
151 BluetoothRemoteGattService::GATT_ERROR_IN_PROGRESS)); | |
152 return; | |
153 } | |
154 characteristic_value_read_or_write_in_progress_ = true; | |
155 read_characteristic_value_callbacks_ = | |
156 std::make_pair(callback, error_callback); | |
157 [gatt_service_->GetCBPeripheral() | |
158 readValueForCharacteristic:cb_characteristic_]; | |
137 } | 159 } |
138 | 160 |
139 void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( | 161 void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic( |
140 const std::vector<uint8_t>& new_value, | 162 const std::vector<uint8_t>& new_value, |
141 const base::Closure& callback, | 163 const base::Closure& callback, |
142 const ErrorCallback& error_callback) { | 164 const ErrorCallback& error_callback) { |
143 NOTIMPLEMENTED(); | 165 NOTIMPLEMENTED(); |
144 } | 166 } |
145 | 167 |
168 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { | |
169 if (!characteristic_value_read_or_write_in_progress_) { | |
170 return; | |
171 } | |
172 std::pair<ValueCallback, ErrorCallback> callbacks; | |
173 callbacks.swap(read_characteristic_value_callbacks_); | |
174 characteristic_value_read_or_write_in_progress_ = false; | |
175 if (error) { | |
176 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " | |
177 << error.domain.UTF8String << ", error code: " << error.code; | |
178 BluetoothGattService::GattErrorCode error_code = | |
179 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); | |
180 callbacks.second.Run(error_code); | |
181 return; | |
182 } | |
183 NSData* nsdata_value = cb_characteristic_.get().value; | |
184 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); | |
185 value_.assign(buffer, buffer + nsdata_value.length); | |
186 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, | |
187 value_); | |
188 callbacks.first.Run(value_); | |
189 } | |
190 | |
191 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { | |
192 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; | |
193 } | |
194 | |
146 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() | 195 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() |
147 const { | 196 const { |
148 return cb_characteristic_.get(); | 197 return cb_characteristic_.get(); |
149 } | 198 } |
150 | 199 |
151 } // namespace device. | 200 } // namespace device. |
OLD | NEW |