Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zakorgy authored and dati91 committed May 3, 2016
1 parent b851045 commit 27ad143
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 76 deletions.
15 changes: 5 additions & 10 deletions components/script/dom/bluetoothremotegattcharacteristic.rs
Expand Up @@ -188,10 +188,7 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
} => return Err(Type(error)),
_ => unreachable!()
};
*self.value.borrow_mut() = value;
Expand All @@ -200,19 +197,17 @@ impl BluetoothRemoteGATTCharacteristicMethods for BluetoothRemoteGATTCharacteris
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattcharacteristic-writevalue
fn WriteValue(&self, value: Vec<u8>) {
fn WriteValue(&self, value: Vec<u8>) -> Fallible<()> {
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
let result = receiver.recv().unwrap();
match result {
BluetoothObjectMsg::BluetoothWriteValue => (),
BluetoothObjectMsg::BluetoothWriteValue => Ok(()),
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
},
} => Err(Type(error)),
_ => unreachable!()
};
}
}
}
17 changes: 6 additions & 11 deletions components/script/dom/bluetoothremotegattdescriptor.rs
Expand Up @@ -10,7 +10,7 @@ use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTDescriptorBinding::BluetoothRemoteGATTDescriptorMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::error::Error::Network;
use dom::bindings::error::Error::{Type, Network};
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
Expand Down Expand Up @@ -103,10 +103,7 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
} => return Err(Type(error)),
_ => unreachable!()
};
*self.value.borrow_mut() = value;
Expand All @@ -115,19 +112,17 @@ impl BluetoothRemoteGATTDescriptorMethods for BluetoothRemoteGATTDescriptor {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdescriptor-writevalue
fn WriteValue(&self, value: Vec<u8>) {
fn WriteValue(&self, value: Vec<u8>) -> Fallible<()> {
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
let result = receiver.recv().unwrap();
match result {
BluetoothObjectMsg::BluetoothWriteValue => (),
BluetoothObjectMsg::BluetoothWriteValue => Ok(()),
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
},
} => Err(Type(error)),
_ => unreachable!()
};
}
}
}
52 changes: 22 additions & 30 deletions components/script/dom/bluetoothremotegattserver.rs
Expand Up @@ -6,6 +6,8 @@ use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMet
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
Expand Down Expand Up @@ -60,29 +62,28 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-connect
fn Connect(&self) -> Root<BluetoothRemoteGATTServer> {
fn Connect(&self) -> Fallible<Root<BluetoothRemoteGATTServer>> {
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender)).unwrap();
self.get_bluetooth_thread()
.send(BluetoothMethodMsg::GATTServerConnect(String::from(self.Device().Id()), sender))
.unwrap();
let server = receiver.recv().unwrap();
match server {
BluetoothObjectMsg::BluetoothServer {
connected
} => {
self.connected.set(connected);
Ok(Root::from_ref(self))
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
},
} => Err(Type(error)),
_ => unreachable!()
}
Root::from_ref(self)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-disconnect
fn Disconnect(&self) {
fn Disconnect(&self) -> Fallible<()>{
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GATTServerDisconnect(String::from(self.Device().Id()), sender)).unwrap();
Expand All @@ -92,24 +93,20 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
connected
} => {
self.connected.set(connected);
Ok(())
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
},
} => Err(Type(error)),
_ => unreachable!()
}
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservice
fn GetPrimaryService(&self, service: StringOrUnsignedLong) -> Option<Root<BluetoothRemoteGATTService>> {
fn GetPrimaryService(&self, service: StringOrUnsignedLong) -> Fallible<Root<BluetoothRemoteGATTService>> {
let uuid: String = match BluetoothUUID::GetService(self.global().r(), service.clone()) {
Ok(domstring) => domstring.to_string(),
Err(_) => {
println!("No UUID provided!");
return None;
},
Err(error) => return Err(error),
};
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
Expand All @@ -121,36 +118,34 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
is_primary,
instance_id,
} => {
Some(BluetoothRemoteGATTService::new(self.global().r(),
Ok(BluetoothRemoteGATTService::new(self.global().r(),
&self.device.get(),
DOMString::from(uuid),
is_primary,
instance_id))
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
} => Err(Type(error)),
_ => unreachable!(),
}
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserver-getprimaryservices
fn GetPrimaryServices(&self, service: Option<StringOrUnsignedLong>)
-> Option<Vec<Root<BluetoothRemoteGATTService>>> {
-> Fallible<Vec<Root<BluetoothRemoteGATTService>>> {
let uuid: Option<String> = match service {
Some(s) => match BluetoothUUID::GetService(self.global().r(), s.clone()) {
Ok(domstring) => Some(domstring.to_string()),
Err(_) => None,
Err(error) => return Err(error),
},
None => None,
};
let mut services: Vec<Root<BluetoothRemoteGATTService>> = vec!();
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
self.get_bluetooth_thread()
.send(BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender))
.unwrap();
let services_vec = receiver.recv().unwrap();
match services_vec {
BluetoothObjectMsg::BluetoothServices {
Expand All @@ -172,14 +167,11 @@ impl BluetoothRemoteGATTServerMethods for BluetoothRemoteGATTServer {
_ => unreachable!(),
}
}
Some(services)
Ok(services)
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
} => Err(Type(error)),
_ => unreachable!(),
}
}
Expand Down
37 changes: 16 additions & 21 deletions components/script/dom/bluetoothremotegattservice.rs
Expand Up @@ -5,6 +5,8 @@
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding;
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
use dom::bindings::codegen::UnionTypes::StringOrUnsignedLong;
use dom::bindings::error::Error::Type;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutHeap, Root};
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
Expand Down Expand Up @@ -84,18 +86,16 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristic
fn GetCharacteristic(&self, characteristic: StringOrUnsignedLong)
-> Option<Root<BluetoothRemoteGATTCharacteristic>> {
-> Fallible<Root<BluetoothRemoteGATTCharacteristic>> {
let uuid: String = match BluetoothUUID::GetCharacteristic(self.global().r(),
characteristic.clone()) {
Ok(domstring) => domstring.to_string(),
Err(_) => {
println!("No UUID provided!");
return None;
},
Err(error) => return Err(error),
};
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap();
self.get_bluetooth_thread()
.send(BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender))
.unwrap();
let characteristic = receiver.recv().unwrap();
match characteristic {
BluetoothObjectMsg::BluetoothCharacteristic {
Expand All @@ -121,36 +121,34 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
authenticated_signed_writes,
reliable_write,
writable_auxiliaries);
Some(BluetoothRemoteGATTCharacteristic::new(self.global().r(),
Ok(BluetoothRemoteGATTCharacteristic::new(self.global().r(),
&self,
DOMString::from(uuid),
properties,
instance_id))
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
} => return Err(Type(error)),
_ => unreachable!(),
}
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattservice-getcharacteristics
fn GetCharacteristics(&self, characteristic: Option<StringOrUnsignedLong>)
-> Option<Vec<Root<BluetoothRemoteGATTCharacteristic>>> {
-> Fallible<Vec<Root<BluetoothRemoteGATTCharacteristic>>> {
let uuid: Option<String> = match characteristic {
Some(c) => match BluetoothUUID::GetCharacteristic(self.global().r(), c.clone()) {
Ok(domstring) => Some(domstring.to_string()),
Err(_) => None,
Err(error) => return Err(error),
},
None => None,
};
let mut characteristics: Vec<Root<BluetoothRemoteGATTCharacteristic>> = vec!();
let (sender, receiver) = ipc::channel().unwrap();
self.get_bluetooth_thread().send(
BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender)).unwrap();
self.get_bluetooth_thread()
.send(BluetoothMethodMsg::GetCharacteristics(self.get_instance_id(), uuid, sender))
.unwrap();
let characteristics_vec = receiver.recv().unwrap();
match characteristics_vec {
BluetoothObjectMsg::BluetoothCharacteristics {
Expand Down Expand Up @@ -192,14 +190,11 @@ impl BluetoothRemoteGATTServiceMethods for BluetoothRemoteGATTService {
_ => unreachable!(),
}
}
Some(characteristics)
Ok(characteristics)
},
BluetoothObjectMsg::Error {
error
} => {
println!("{}", error);
None
},
} => return Err(Type(error)),
_ => unreachable!(),
}
}
Expand Down
Expand Up @@ -20,6 +20,7 @@ interface BluetoothRemoteGATTCharacteristic {
[Throws]
ByteString readValue();
//Promise<DataView> readValue();
[Throws]
void writeValue(sequence<octet> value);
//Promise<void> writeValue(BufferSource value);
//Promise<void> startNotifications();
Expand Down
Expand Up @@ -12,6 +12,7 @@ interface BluetoothRemoteGATTDescriptor {
[Throws]
ByteString readValue();
//Promise<DataView> readValue();
[Throws]
void writeValue(sequence<octet> value);
//Promise<void> writeValue(BufferSource value);
};
Expand Up @@ -8,10 +8,14 @@
interface BluetoothRemoteGATTServer {
readonly attribute BluetoothDevice device;
readonly attribute boolean connected;
[Throws]
BluetoothRemoteGATTServer connect();
[Throws]
void disconnect();
BluetoothRemoteGATTService? getPrimaryService((DOMString or unsigned long) service);
sequence<BluetoothRemoteGATTService>? getPrimaryServices(optional (DOMString or unsigned long) service);
[Throws]
BluetoothRemoteGATTService getPrimaryService((DOMString or unsigned long) service);
[Throws]
sequence<BluetoothRemoteGATTService> getPrimaryServices(optional (DOMString or unsigned long) service);
//Promise<BluetoothRemoteGATTService> getPrimaryService(BluetoothServiceUUID service);
//Promise<sequence<BluetoothRemoteGATTService>>getPrimaryServices(optional BluetoothServiceUUID service);
//Promise<BluetoothRemoteGATTServer> connect();
Expand Down
Expand Up @@ -9,8 +9,10 @@ interface BluetoothRemoteGATTService {
readonly attribute BluetoothDevice device;
readonly attribute DOMString uuid;
readonly attribute boolean isPrimary;
BluetoothRemoteGATTCharacteristic? getCharacteristic((DOMString or unsigned long) characteristic);
sequence<BluetoothRemoteGATTCharacteristic>? getCharacteristics
[Throws]
BluetoothRemoteGATTCharacteristic getCharacteristic((DOMString or unsigned long) characteristic);
[Throws]
sequence<BluetoothRemoteGATTCharacteristic> getCharacteristics
(optional (DOMString or unsigned long) characteristic);
//Promise<BluetoothRemoteGATTCharacteristic>getCharacteristic(BluetoothCharacteristicUUID characteristic);
//Promise<sequence<BluetoothRemoteGATTCharacteristic>>
Expand Down

0 comments on commit 27ad143

Please sign in to comment.