Skip to content

Commit

Permalink
WebBluetooth API classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dati91 committed Mar 16, 2016
1 parent 0062870 commit e7d70cf
Show file tree
Hide file tree
Showing 28 changed files with 999 additions and 28 deletions.
1 change: 1 addition & 0 deletions components/script/Cargo.toml
Expand Up @@ -82,6 +82,7 @@ rand = "0.3"
phf = "0.7.13"
phf_macros = "0.7.13"
ref_slice = "0.1.0"
regex = "0.1.43"
rustc-serialize = "0.3"
selectors = {version = "0.5", features = ["heap_size"]}
serde = "0.6"
Expand Down
39 changes: 39 additions & 0 deletions components/script/dom/bluetooth.rs
@@ -0,0 +1,39 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::BluetoothBinding;
use dom::bindings::codegen::Bindings::BluetoothBinding::BluetoothMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bluetoothdevice::BluetoothDevice;

// https://webbluetoothcg.github.io/web-bluetooth/#bluetooth
#[dom_struct]
pub struct Bluetooth {
reflector_: Reflector,
}

impl Bluetooth {
pub fn new_inherited() -> Bluetooth {
Bluetooth {
reflector_: Reflector::new(),
}
}

pub fn new(global: GlobalRef) -> Root<Bluetooth> {
reflect_dom_object(box Bluetooth::new_inherited(),
global,
BluetoothBinding::Wrap)
}
}

impl BluetoothMethods for Bluetooth {

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice
fn RequestDevice(&self) -> Option<Root<BluetoothDevice>> {
//UNIMPLEMENTED
None
}
}
54 changes: 54 additions & 0 deletions components/script/dom/bluetoothadvertisingdata.rs
@@ -0,0 +1,54 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::BluetoothAdvertisingDataBinding;
use dom::bindings::codegen::Bindings::BluetoothAdvertisingDataBinding::BluetoothAdvertisingDataMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflector, reflect_dom_object};

// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothadvertisingdata
#[dom_struct]
pub struct BluetoothAdvertisingData {
reflector_: Reflector,
appearance: u16,
txPower: i8,
rssi: i8,
}

impl BluetoothAdvertisingData {
pub fn new_inherited(appearance: u16, txPower: i8, rssi: i8) -> BluetoothAdvertisingData {
BluetoothAdvertisingData {
reflector_: Reflector::new(),
appearance: appearance,
txPower: txPower,
rssi: rssi,
}
}

pub fn new(global: GlobalRef, appearance: u16, txPower: i8, rssi: i8) -> Root<BluetoothAdvertisingData> {
reflect_dom_object(box BluetoothAdvertisingData::new_inherited(appearance,
txPower,
rssi),
global,
BluetoothAdvertisingDataBinding::Wrap)
}
}

impl BluetoothAdvertisingDataMethods for BluetoothAdvertisingData {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingdata-appearance
fn GetAppearance(&self) -> Option<u16> {
Some(self.appearance)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingdata-txpower
fn GetTxPower(&self) -> Option<i8> {
Some(self.txPower)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothadvertisingdata-rssi
fn GetRssi(&self) -> Option<i8> {
Some(self.rssi)
}
}
122 changes: 122 additions & 0 deletions components/script/dom/bluetoothcharacteristicproperties.rs
@@ -0,0 +1,122 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding;
use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding::
BluetoothCharacteristicPropertiesMethods;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::Root;
use dom::bindings::reflector::{Reflector, reflect_dom_object};

// https://webbluetoothcg.github.io/web-bluetooth/#characteristicproperties
#[dom_struct]
pub struct BluetoothCharacteristicProperties {
reflector_: Reflector,
broadcast: bool,
read: bool,
writeWithoutResponse: bool,
write: bool,
notify: bool,
indicate: bool,
authenticatedSignedWrites: bool,
reliableWrite: bool,
writableAuxiliaries: bool,
}

impl BluetoothCharacteristicProperties {
pub fn new_inherited(broadcast: bool,
read: bool,
writeWithoutResponse: bool,
write: bool,
notify: bool,
indicate: bool,
authenticatedSignedWrites: bool,
reliableWrite: bool,
writableAuxiliaries: bool)
-> BluetoothCharacteristicProperties {
BluetoothCharacteristicProperties {
reflector_: Reflector::new(),
broadcast: broadcast,
read: read,
writeWithoutResponse: writeWithoutResponse,
write: write,
notify: notify,
indicate: indicate,
authenticatedSignedWrites: authenticatedSignedWrites,
reliableWrite: reliableWrite,
writableAuxiliaries: writableAuxiliaries,
}
}

pub fn new(global: GlobalRef,
broadcast: bool,
read: bool,
writeWithoutResponse: bool,
write: bool,
notify: bool,
indicate: bool,
authenticatedSignedWrites: bool,
reliableWrite: bool,
writableAuxiliaries: bool)
-> Root<BluetoothCharacteristicProperties> {
reflect_dom_object(box BluetoothCharacteristicProperties::new_inherited(broadcast,
read,
writeWithoutResponse,
write,
notify,
indicate,
authenticatedSignedWrites,
reliableWrite,
writableAuxiliaries),
global,
BluetoothCharacteristicPropertiesBinding::Wrap)
}
}

impl BluetoothCharacteristicPropertiesMethods for BluetoothCharacteristicProperties {
// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-broadcast
fn Broadcast(&self) -> bool {
self.broadcast
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-read
fn Read(&self) -> bool {
self.read
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-writewithoutresponse
fn WriteWithoutResponse(&self) -> bool {
self.writeWithoutResponse
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-write
fn Write(&self) -> bool {
self.write
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-notify
fn Notify(&self) -> bool {
self.notify
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-indicate
fn Indicate(&self) -> bool {
self.indicate
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-authenticatedsignedwrites
fn AuthenticatedSignedWrites(&self) -> bool {
self.authenticatedSignedWrites
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-reliablewrite
fn ReliableWrite(&self) -> bool {
self.reliableWrite
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothcharacteristicproperties-writableauxiliaries
fn WritableAuxiliaries(&self) -> bool {
self.writableAuxiliaries
}
}
125 changes: 125 additions & 0 deletions components/script/dom/bluetoothdevice.rs
@@ -0,0 +1,125 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::bindings::codegen::Bindings::BluetoothDeviceBinding;
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::{BluetoothDeviceMethods, VendorIDSource};
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, Root, MutHeap};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer;
use util::str::DOMString;

// https://webbluetoothcg.github.io/web-bluetooth/#bluetoothdevice
#[dom_struct]
pub struct BluetoothDevice {
reflector_: Reflector,
id: DOMString,
name: DOMString,
adData: MutHeap<JS<BluetoothAdvertisingData>>,
deviceClass: u32,
vendorIDSource: VendorIDSource,
vendorID: u32,
productID: u32,
productVersion: u32,
gatt: MutHeap<JS<BluetoothRemoteGATTServer>>,
}

impl BluetoothDevice {
pub fn new_inherited(id: DOMString,
name: DOMString,
adData: &BluetoothAdvertisingData,
deviceClass: u32,
vendorIDSource: VendorIDSource,
vendorID: u32,
productID: u32,
productVersion: u32,
gatt: &BluetoothRemoteGATTServer)
-> BluetoothDevice {
BluetoothDevice {
reflector_: Reflector::new(),
id: id,
name: name,
adData: MutHeap::new(adData),
deviceClass: deviceClass,
vendorIDSource: vendorIDSource,
vendorID: vendorID,
productID: productID,
productVersion: productVersion,
gatt: MutHeap::new(gatt),
}
}

pub fn new(global: GlobalRef,
id: DOMString,
name: DOMString,
adData: &BluetoothAdvertisingData,
deviceClass: u32,
vendorIDSource: VendorIDSource,
vendorID: u32,
productID: u32,
productVersion: u32,
gatt: &BluetoothRemoteGATTServer)
-> Root<BluetoothDevice> {
reflect_dom_object(box BluetoothDevice::new_inherited(id,
name,
adData,
deviceClass,
vendorIDSource,
vendorID,
productID,
productVersion,
gatt),
global,
BluetoothDeviceBinding::Wrap)
}
}

impl BluetoothDeviceMethods for BluetoothDevice {

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-id
fn Id(&self) -> DOMString {
self.id.clone()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-name
fn GetName(&self) -> Option<DOMString> {
Some(self.name.clone())
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-addata
fn AdData(&self) -> Root<BluetoothAdvertisingData> {
self.adData.get()
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-deviceclass
fn GetDeviceClass(&self) -> Option<u32> {
Some(self.deviceClass)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-vendoridsource
fn GetVendorIDSource(&self) -> Option<VendorIDSource> {
Some(self.vendorIDSource)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-vendorid
fn GetVendorID(&self) -> Option<u32> {
Some(self.vendorID)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-productid
fn GetProductID(&self) -> Option<u32> {
Some(self.productID)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-productversion
fn GetProductVersion(&self) -> Option<u32> {
Some(self.productVersion)
}

// https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothdevice-gatt
fn Gatt(&self) -> Root<BluetoothRemoteGATTServer> {
self.gatt.get()
}
}

0 comments on commit e7d70cf

Please sign in to comment.