Switches characteristic to use BufferSource

This commit is contained in:
Anthony Urena 2018-03-24 16:10:28 -04:00
parent 40235da2d3
commit 5bf2e71407
6 changed files with 17 additions and 12 deletions

View file

@ -12,6 +12,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::
BluetoothRemoteGATTCharacteristicMethods; BluetoothRemoteGATTCharacteristicMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods; use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer;
use dom::bindings::error::Error::{self, InvalidModification, Network, NotSupported, Security}; use dom::bindings::error::Error::{self, InvalidModification, Network, NotSupported, Security};
use dom::bindings::inheritance::Castable; use dom::bindings::inheritance::Castable;
use dom::bindings::reflector::{DomObject, reflect_dom_object}; use dom::bindings::reflector::{DomObject, reflect_dom_object};
@ -155,7 +156,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
#[allow(unrooted_must_root)] #[allow(unrooted_must_root)]
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue
fn WriteValue(&self, value: Vec<u8>) -> Rc<Promise> { fn WriteValue(&self, value: ArrayBufferViewOrArrayBuffer) -> Rc<Promise> {
let p = Promise::new(&self.global()); let p = Promise::new(&self.global());
// Step 1. // Step 1.
@ -165,7 +166,12 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
} }
// Step 2 - 3. // Step 2 - 3.
if value.len() > MAXIMUM_ATTRIBUTE_LENGTH { let vec = match value {
ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut avb) => avb.to_vec(),
ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut ab) => ab.to_vec(),
};
if vec.len() > MAXIMUM_ATTRIBUTE_LENGTH {
p.reject_error(InvalidModification); p.reject_error(InvalidModification);
return p; return p;
} }
@ -190,7 +196,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
// in writeValue function and in handle_response function. // in writeValue function and in handle_response function.
let sender = response_async(&p, self); let sender = response_async(&p, self);
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothRequest::WriteValue(self.get_instance_id(), value, sender)).unwrap(); BluetoothRequest::WriteValue(self.get_instance_id(), vec, sender)).unwrap();
return p; return p;
} }

View file

@ -125,11 +125,11 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
} }
// Step 2 - 3. // Step 2 - 3.
let v = match value { let vec = match value {
ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut avb) => avb.to_vec(), ArrayBufferViewOrArrayBuffer::ArrayBufferView(mut avb) => avb.to_vec(),
ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut ab) => ab.to_vec(), ArrayBufferViewOrArrayBuffer::ArrayBuffer(mut ab) => ab.to_vec(),
}; };
if v.len() > MAXIMUM_ATTRIBUTE_LENGTH { if vec.len() > MAXIMUM_ATTRIBUTE_LENGTH {
p.reject_error(InvalidModification); p.reject_error(InvalidModification);
return p; return p;
} }
@ -145,7 +145,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
// in writeValue function and in handle_response function. // in writeValue function and in handle_response function.
let sender = response_async(&p, self); let sender = response_async(&p, self);
self.get_bluetooth_thread().send( self.get_bluetooth_thread().send(
BluetoothRequest::WriteValue(self.get_instance_id(), v, sender)).unwrap(); BluetoothRequest::WriteValue(self.get_instance_id(), vec, sender)).unwrap();
return p; return p;
} }
} }

View file

@ -16,8 +16,7 @@ interface BluetoothRemoteGATTCharacteristic : EventTarget {
getDescriptors(optional BluetoothDescriptorUUID descriptor); getDescriptors(optional BluetoothDescriptorUUID descriptor);
Promise<ByteString> readValue(); Promise<ByteString> readValue();
//Promise<DataView> readValue(); //Promise<DataView> readValue();
Promise<void> writeValue(sequence<octet> value); Promise<void> writeValue(BufferSource value);
//Promise<void> writeValue(BufferSource value);
Promise<BluetoothRemoteGATTCharacteristic> startNotifications(); Promise<BluetoothRemoteGATTCharacteristic> startNotifications();
Promise<BluetoothRemoteGATTCharacteristic> stopNotifications(); Promise<BluetoothRemoteGATTCharacteristic> stopNotifications();
}; };

View file

@ -11,6 +11,6 @@ interface BluetoothRemoteGATTDescriptor {
readonly attribute DOMString uuid; readonly attribute DOMString uuid;
readonly attribute ByteString? value; readonly attribute ByteString? value;
Promise<ByteString> readValue(); Promise<ByteString> readValue();
// Promise<DataView> readValue(); //Promise<DataView> readValue();
Promise<void> writeValue(BufferSource value); Promise<void> writeValue(BufferSource value);
}; };

View file

@ -15,8 +15,8 @@ promise_test(() => {
.then(service => service.getCharacteristic(device_name.name)) .then(service => service.getCharacteristic(device_name.name))
.then(characteristic => { .then(characteristic => {
assert_equals(characteristic.value, null); assert_equals(characteristic.value, null);
return characteristic.writeValue(asciiToDecimal('foo')) return characteristic.writeValue(Uint8Array.of(1, 2))
.then(() => assert_equals(characteristic.value, 'foo')); .then(() => assert_equals(characteristic.value, '\x01\x02'));
}); });
}, 'A regular write request to a writable characteristic should update value.'); }, 'A regular write request to a writable characteristic should update value.');
</script> </script>

View file

@ -17,7 +17,7 @@ promise_test(() => {
.then(descriptor => { .then(descriptor => {
assert_equals(descriptor.value, null); assert_equals(descriptor.value, null);
return descriptor.writeValue(Uint8Array.of(1, 2)) return descriptor.writeValue(Uint8Array.of(1, 2))
.then(() => assert_equals(descriptor.value, "\x01\x02")); .then(() => assert_equals(descriptor.value, '\x01\x02'));
}); });
}, 'A regular write request to a writable descriptor should update value.'); }, 'A regular write request to a writable descriptor should update value.');
</script> </script>