aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/bluetoothdevice.rs
blob: a941f89fa7e6d95a32fc3c8b01342d7c6d3c3c5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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()
    }
}