Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm

Issue 2074563002: bluetooth: mac: write characteristic implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@read_characteristicscan_servicescan_cleanup
Patch Set: Updating comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 base::ThreadTaskRunnerHandle::Get()->PostTask(
190 FROM_HERE,
191 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue,
192 base::Unretained(this), nil));
193 }
166 } 194 }
167 195
168 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { 196 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) {
169 if (!characteristic_value_read_or_write_in_progress_) { 197 if (!characteristic_value_read_or_write_in_progress_) {
198 // In case of buggy device, nothing should be done if receiving extra
199 // read confirmation.
170 return; 200 return;
171 } 201 }
172 std::pair<ValueCallback, ErrorCallback> callbacks; 202 std::pair<ValueCallback, ErrorCallback> callbacks;
173 callbacks.swap(read_characteristic_value_callbacks_); 203 callbacks.swap(read_characteristic_value_callbacks_);
174 characteristic_value_read_or_write_in_progress_ = false; 204 characteristic_value_read_or_write_in_progress_ = false;
175 if (error) { 205 if (error) {
176 VLOG(1) << "Bluetooth error while reading for characteristic, domain: " 206 VLOG(1) << "Bluetooth error while reading for characteristic, domain: "
177 << error.domain.UTF8String << ", error code: " << error.code; 207 << error.domain.UTF8String << ", error code: " << error.code;
178 BluetoothGattService::GattErrorCode error_code = 208 BluetoothGattService::GattErrorCode error_code =
179 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); 209 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
180 callbacks.second.Run(error_code); 210 callbacks.second.Run(error_code);
181 return; 211 return;
182 } 212 }
183 NSData* nsdata_value = cb_characteristic_.get().value; 213 NSData* nsdata_value = cb_characteristic_.get().value;
184 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); 214 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes);
185 value_.assign(buffer, buffer + nsdata_value.length); 215 value_.assign(buffer, buffer + nsdata_value.length);
186 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, 216 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this,
187 value_); 217 value_);
188 callbacks.first.Run(value_); 218 callbacks.first.Run(value_);
189 } 219 }
190 220
221 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) {
222 if (!characteristic_value_read_or_write_in_progress_) {
223 // In case of buggy device, nothing should be done if receiving extra
224 // write confirmation.
225 return;
226 }
227 std::pair<base::Closure, ErrorCallback> callbacks;
228 callbacks.swap(write_characteristic_value_callbacks_);
229 characteristic_value_read_or_write_in_progress_ = false;
230 if (error) {
231 VLOG(1) << "Bluetooth error while reading for characteristic, domain: "
232 << error.domain.UTF8String << ", error code: " << error.code;
233 BluetoothGattService::GattErrorCode error_code =
234 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
235 callbacks.second.Run(error_code);
236 return;
237 }
238 NSData* nsdata_value = cb_characteristic_.get().value;
239 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes);
240 std::vector<uint8_t> gatt_value(buffer, buffer + nsdata_value.length);
241 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this,
242 value_);
243 callbacks.first.Run();
244 }
245
191 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { 246 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const {
192 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; 247 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ;
193 } 248 }
194 249
250 bool BluetoothRemoteGattCharacteristicMac::IsWritable() const {
251 BluetoothGattCharacteristic::Properties properties = GetProperties();
252 return (properties & BluetoothGattCharacteristic::PROPERTY_WRITE) ||
253 (properties & PROPERTY_WRITE_WITHOUT_RESPONSE);
254 }
255
256 CBCharacteristicWriteType BluetoothRemoteGattCharacteristicMac::GetCBWriteType()
257 const {
258 return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE)
259 ? CBCharacteristicWriteWithResponse
260 : CBCharacteristicWriteWithoutResponse;
261 }
262
195 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() 263 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic()
196 const { 264 const {
197 return cb_characteristic_.get(); 265 return cb_characteristic_.get();
198 } 266 }
199
200 } // namespace device. 267 } // namespace device.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698