зеркало из https://github.com/mozilla/gecko-dev.git
servo: Merge #13918 - WebBluetooth fixes for the wpt tests (from szeged:wpt-error-fixes); r=jdm
<!-- Please describe your changes on the following line: --> Note: depends on #13612 WebBluetooth fixes for the failing wpt tests, excluding the `disconnect-during` tests cases, due to the lack of the event handling in the current implementation. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] There are tests for these changes OR <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: eb2484f86dd57c95957b417ac0dd249ea86df06b --HG-- rename : servo/components/script/bluetooth_blacklist.rs => servo/components/bluetooth_traits/blacklist.rs
This commit is contained in:
Родитель
fa4f7b60bd
Коммит
154504b9f2
|
@ -19,6 +19,7 @@ use bluetooth_traits::{BluetoothCharacteristicMsg, BluetoothCharacteristicsMsg};
|
||||||
use bluetooth_traits::{BluetoothDescriptorMsg, BluetoothDescriptorsMsg};
|
use bluetooth_traits::{BluetoothDescriptorMsg, BluetoothDescriptorsMsg};
|
||||||
use bluetooth_traits::{BluetoothDeviceMsg, BluetoothError, BluetoothMethodMsg};
|
use bluetooth_traits::{BluetoothDeviceMsg, BluetoothError, BluetoothMethodMsg};
|
||||||
use bluetooth_traits::{BluetoothResult, BluetoothServiceMsg, BluetoothServicesMsg};
|
use bluetooth_traits::{BluetoothResult, BluetoothServiceMsg, BluetoothServicesMsg};
|
||||||
|
use bluetooth_traits::blacklist::{uuid_is_blacklisted, Blacklist};
|
||||||
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence, RequestDeviceoptions};
|
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence, RequestDeviceoptions};
|
||||||
use device::bluetooth::{BluetoothAdapter, BluetoothDevice, BluetoothGATTCharacteristic};
|
use device::bluetooth::{BluetoothAdapter, BluetoothDevice, BluetoothGATTCharacteristic};
|
||||||
use device::bluetooth::{BluetoothGATTDescriptor, BluetoothGATTService};
|
use device::bluetooth::{BluetoothGATTDescriptor, BluetoothGATTService};
|
||||||
|
@ -31,10 +32,6 @@ use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use util::thread::spawn_named;
|
use util::thread::spawn_named;
|
||||||
|
|
||||||
const ADAPTER_ERROR: &'static str = "No adapter found";
|
|
||||||
|
|
||||||
const ADAPTER_NOT_POWERED_ERROR: &'static str = "Bluetooth adapter not powered";
|
|
||||||
|
|
||||||
// A transaction not completed within 30 seconds shall time out. Such a transaction shall be considered to have failed.
|
// A transaction not completed within 30 seconds shall time out. Such a transaction shall be considered to have failed.
|
||||||
// https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439 (Vol. 3, page 480)
|
// https://www.bluetooth.org/DocMan/handlers/DownloadDoc.ashx?doc_id=286439 (Vol. 3, page 480)
|
||||||
const MAXIMUM_TRANSACTION_TIME: u8 = 30;
|
const MAXIMUM_TRANSACTION_TIME: u8 = 30;
|
||||||
|
@ -75,11 +72,11 @@ macro_rules! get_adapter_or_return_error(
|
||||||
match $bl_manager.get_or_create_adapter() {
|
match $bl_manager.get_or_create_adapter() {
|
||||||
Some(adapter) => {
|
Some(adapter) => {
|
||||||
if !adapter.is_powered().unwrap_or(false) {
|
if !adapter.is_powered().unwrap_or(false) {
|
||||||
return drop($sender.send(Err(BluetoothError::Type(ADAPTER_NOT_POWERED_ERROR.to_string()))))
|
return drop($sender.send(Err(BluetoothError::NotFound)))
|
||||||
}
|
}
|
||||||
adapter
|
adapter
|
||||||
},
|
},
|
||||||
None => return drop($sender.send(Err(BluetoothError::Type(ADAPTER_ERROR.to_string())))),
|
None => return drop($sender.send(Err(BluetoothError::NotFound))),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
);
|
);
|
||||||
|
@ -106,8 +103,8 @@ fn matches_filter(device: &BluetoothDevice, filter: &BluetoothScanfilter) -> boo
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 1.
|
// Step 1.
|
||||||
if !filter.get_name().is_empty() {
|
if let Some(name) = filter.get_name() {
|
||||||
if device.get_name().ok() != Some(filter.get_name().to_string()) {
|
if device.get_name().ok() != Some(name.to_string()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -628,6 +625,9 @@ impl BluetoothManager {
|
||||||
device_id: String,
|
device_id: String,
|
||||||
uuid: String,
|
uuid: String,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
|
||||||
|
if !self.cached_devices.contains_key(&device_id) {
|
||||||
|
return drop(sender.send(Err(BluetoothError::InvalidState)));
|
||||||
|
}
|
||||||
let mut adapter = get_adapter_or_return_error!(self, sender);
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
if !self.allowed_services.get(&device_id).map_or(false, |s| s.contains(&uuid)) {
|
if !self.allowed_services.get(&device_id).map_or(false, |s| s.contains(&uuid)) {
|
||||||
return drop(sender.send(Err(BluetoothError::Security)));
|
return drop(sender.send(Err(BluetoothError::Security)));
|
||||||
|
@ -654,6 +654,9 @@ impl BluetoothManager {
|
||||||
device_id: String,
|
device_id: String,
|
||||||
uuid: Option<String>,
|
uuid: Option<String>,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
|
||||||
|
if !self.cached_devices.contains_key(&device_id) {
|
||||||
|
return drop(sender.send(Err(BluetoothError::InvalidState)));
|
||||||
|
}
|
||||||
let mut adapter = get_adapter_or_return_error!(self, sender);
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
let services = match uuid {
|
let services = match uuid {
|
||||||
Some(ref id) => {
|
Some(ref id) => {
|
||||||
|
@ -679,6 +682,10 @@ impl BluetoothManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
services_vec.retain(|s| !uuid_is_blacklisted(&s.uuid, Blacklist::All) &&
|
||||||
|
self.allowed_services
|
||||||
|
.get(&device_id)
|
||||||
|
.map_or(false, |uuids| uuids.contains(&s.uuid)));
|
||||||
if services_vec.is_empty() {
|
if services_vec.is_empty() {
|
||||||
return drop(sender.send(Err(BluetoothError::NotFound)));
|
return drop(sender.send(Err(BluetoothError::NotFound)));
|
||||||
}
|
}
|
||||||
|
@ -690,9 +697,12 @@ impl BluetoothManager {
|
||||||
service_id: String,
|
service_id: String,
|
||||||
uuid: String,
|
uuid: String,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothServiceMsg>>) {
|
||||||
|
if !self.cached_services.contains_key(&service_id) {
|
||||||
|
return drop(sender.send(Err(BluetoothError::InvalidState)));
|
||||||
|
}
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = match self.get_or_create_adapter() {
|
||||||
Some(a) => a,
|
Some(a) => a,
|
||||||
None => return drop(sender.send(Err(BluetoothError::Type(ADAPTER_ERROR.to_string())))),
|
None => return drop(sender.send(Err(BluetoothError::NotFound))),
|
||||||
};
|
};
|
||||||
let device = match self.device_from_service_id(&service_id) {
|
let device = match self.device_from_service_id(&service_id) {
|
||||||
Some(device) => device,
|
Some(device) => device,
|
||||||
|
@ -721,9 +731,12 @@ impl BluetoothManager {
|
||||||
service_id: String,
|
service_id: String,
|
||||||
uuid: Option<String>,
|
uuid: Option<String>,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothServicesMsg>>) {
|
||||||
|
if !self.cached_services.contains_key(&service_id) {
|
||||||
|
return drop(sender.send(Err(BluetoothError::InvalidState)));
|
||||||
|
}
|
||||||
let mut adapter = match self.get_or_create_adapter() {
|
let mut adapter = match self.get_or_create_adapter() {
|
||||||
Some(a) => a,
|
Some(a) => a,
|
||||||
None => return drop(sender.send(Err(BluetoothError::Type(ADAPTER_ERROR.to_string())))),
|
None => return drop(sender.send(Err(BluetoothError::NotFound))),
|
||||||
};
|
};
|
||||||
let device = match self.device_from_service_id(&service_id) {
|
let device = match self.device_from_service_id(&service_id) {
|
||||||
Some(device) => device,
|
Some(device) => device,
|
||||||
|
@ -747,6 +760,7 @@ impl BluetoothManager {
|
||||||
if let Some(uuid) = uuid {
|
if let Some(uuid) = uuid {
|
||||||
services_vec.retain(|ref s| s.uuid == uuid);
|
services_vec.retain(|ref s| s.uuid == uuid);
|
||||||
}
|
}
|
||||||
|
services_vec.retain(|s| !uuid_is_blacklisted(&s.uuid, Blacklist::All));
|
||||||
if services_vec.is_empty() {
|
if services_vec.is_empty() {
|
||||||
return drop(sender.send(Err(BluetoothError::NotFound)));
|
return drop(sender.send(Err(BluetoothError::NotFound)));
|
||||||
}
|
}
|
||||||
|
@ -758,6 +772,9 @@ impl BluetoothManager {
|
||||||
service_id: String,
|
service_id: String,
|
||||||
uuid: String,
|
uuid: String,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>) {
|
||||||
|
if !self.cached_services.contains_key(&service_id) {
|
||||||
|
return drop(sender.send(Err(BluetoothError::InvalidState)));
|
||||||
|
}
|
||||||
let mut adapter = get_adapter_or_return_error!(self, sender);
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
|
let characteristics = self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &uuid);
|
||||||
if characteristics.is_empty() {
|
if characteristics.is_empty() {
|
||||||
|
@ -789,6 +806,9 @@ impl BluetoothManager {
|
||||||
service_id: String,
|
service_id: String,
|
||||||
uuid: Option<String>,
|
uuid: Option<String>,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>) {
|
||||||
|
if !self.cached_services.contains_key(&service_id) {
|
||||||
|
return drop(sender.send(Err(BluetoothError::InvalidState)));
|
||||||
|
}
|
||||||
let mut adapter = get_adapter_or_return_error!(self, sender);
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
let characteristics = match uuid {
|
let characteristics = match uuid {
|
||||||
Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id),
|
Some(id) => self.get_gatt_characteristics_by_uuid(&mut adapter, &service_id, &id),
|
||||||
|
@ -817,6 +837,7 @@ impl BluetoothManager {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
characteristics_vec.retain(|c| !uuid_is_blacklisted(&c.uuid, Blacklist::All));
|
||||||
if characteristics_vec.is_empty() {
|
if characteristics_vec.is_empty() {
|
||||||
return drop(sender.send(Err(BluetoothError::NotFound)));
|
return drop(sender.send(Err(BluetoothError::NotFound)));
|
||||||
}
|
}
|
||||||
|
@ -828,6 +849,9 @@ impl BluetoothManager {
|
||||||
characteristic_id: String,
|
characteristic_id: String,
|
||||||
uuid: String,
|
uuid: String,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothDescriptorMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothDescriptorMsg>>) {
|
||||||
|
if !self.cached_characteristics.contains_key(&characteristic_id) {
|
||||||
|
return drop(sender.send(Err(BluetoothError::InvalidState)));
|
||||||
|
}
|
||||||
let mut adapter = get_adapter_or_return_error!(self, sender);
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
|
let descriptors = self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &uuid);
|
||||||
if descriptors.is_empty() {
|
if descriptors.is_empty() {
|
||||||
|
@ -848,6 +872,9 @@ impl BluetoothManager {
|
||||||
characteristic_id: String,
|
characteristic_id: String,
|
||||||
uuid: Option<String>,
|
uuid: Option<String>,
|
||||||
sender: IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>) {
|
sender: IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>) {
|
||||||
|
if !self.cached_characteristics.contains_key(&characteristic_id) {
|
||||||
|
return drop(sender.send(Err(BluetoothError::InvalidState)));
|
||||||
|
}
|
||||||
let mut adapter = get_adapter_or_return_error!(self, sender);
|
let mut adapter = get_adapter_or_return_error!(self, sender);
|
||||||
let descriptors = match uuid {
|
let descriptors = match uuid {
|
||||||
Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id),
|
Some(id) => self.get_gatt_descriptors_by_uuid(&mut adapter, &characteristic_id, &id),
|
||||||
|
@ -865,6 +892,7 @@ impl BluetoothManager {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
descriptors_vec.retain(|d| !uuid_is_blacklisted(&d.uuid, Blacklist::All));
|
||||||
if descriptors_vec.is_empty() {
|
if descriptors_vec.is_empty() {
|
||||||
return drop(sender.send(Err(BluetoothError::NotFound)));
|
return drop(sender.send(Err(BluetoothError::NotFound)));
|
||||||
}
|
}
|
||||||
|
@ -879,7 +907,7 @@ impl BluetoothManager {
|
||||||
value = self.get_gatt_descriptor(&mut adapter, &id)
|
value = self.get_gatt_descriptor(&mut adapter, &id)
|
||||||
.map(|d| d.read_value().unwrap_or(vec![]));
|
.map(|d| d.read_value().unwrap_or(vec![]));
|
||||||
}
|
}
|
||||||
let _ = sender.send(value.ok_or(BluetoothError::NotSupported));
|
let _ = sender.send(value.ok_or(BluetoothError::InvalidState));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothResult<bool>>) {
|
fn write_value(&mut self, id: String, value: Vec<u8>, sender: IpcSender<BluetoothResult<bool>>) {
|
||||||
|
@ -895,7 +923,7 @@ impl BluetoothManager {
|
||||||
Ok(_) => Ok(true),
|
Ok(_) => Ok(true),
|
||||||
Err(_) => return drop(sender.send(Err(BluetoothError::NotSupported))),
|
Err(_) => return drop(sender.send(Err(BluetoothError::NotSupported))),
|
||||||
},
|
},
|
||||||
None => return drop(sender.send(Err(BluetoothError::NotSupported))),
|
None => return drop(sender.send(Err(BluetoothError::InvalidState))),
|
||||||
};
|
};
|
||||||
let _ = sender.send(message);
|
let _ = sender.send(message);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,5 +11,7 @@ path = "lib.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ipc-channel = "0.5"
|
ipc-channel = "0.5"
|
||||||
|
regex = "0.1.43"
|
||||||
serde = "0.8"
|
serde = "0.8"
|
||||||
serde_derive = "0.8"
|
serde_derive = "0.8"
|
||||||
|
util = {path = "../util"}
|
||||||
|
|
|
@ -5,9 +5,12 @@
|
||||||
#![feature(proc_macro)]
|
#![feature(proc_macro)]
|
||||||
|
|
||||||
extern crate ipc_channel;
|
extern crate ipc_channel;
|
||||||
|
extern crate regex;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
extern crate util;
|
||||||
|
|
||||||
|
pub mod blacklist;
|
||||||
pub mod scanfilter;
|
pub mod scanfilter;
|
||||||
|
|
||||||
use ipc_channel::ipc::IpcSender;
|
use ipc_channel::ipc::IpcSender;
|
||||||
|
@ -20,6 +23,7 @@ pub enum BluetoothError {
|
||||||
NotFound,
|
NotFound,
|
||||||
NotSupported,
|
NotSupported,
|
||||||
Security,
|
Security,
|
||||||
|
InvalidState,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
|
|
|
@ -25,7 +25,7 @@ impl ServiceUUIDSequence {
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct BluetoothScanfilter {
|
pub struct BluetoothScanfilter {
|
||||||
name: String,
|
name: Option<String>,
|
||||||
name_prefix: String,
|
name_prefix: String,
|
||||||
services: ServiceUUIDSequence,
|
services: ServiceUUIDSequence,
|
||||||
manufacturer_id: Option<u16>,
|
manufacturer_id: Option<u16>,
|
||||||
|
@ -33,7 +33,7 @@ pub struct BluetoothScanfilter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BluetoothScanfilter {
|
impl BluetoothScanfilter {
|
||||||
pub fn new(name: String,
|
pub fn new(name: Option<String>,
|
||||||
name_prefix: String,
|
name_prefix: String,
|
||||||
services: Vec<String>,
|
services: Vec<String>,
|
||||||
manufacturer_id: Option<u16>,
|
manufacturer_id: Option<u16>,
|
||||||
|
@ -48,8 +48,8 @@ impl BluetoothScanfilter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_name(&self) -> &str {
|
pub fn get_name(&self) -> Option<&str> {
|
||||||
&self.name
|
self.name.as_ref().map(|s| s.as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_name_prefix(&self) -> &str {
|
pub fn get_name_prefix(&self) -> &str {
|
||||||
|
@ -69,12 +69,12 @@ impl BluetoothScanfilter {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_empty_or_invalid(&self) -> bool {
|
pub fn is_empty_or_invalid(&self) -> bool {
|
||||||
(self.name.is_empty() &&
|
(self.name.is_none() &&
|
||||||
self.name_prefix.is_empty() &&
|
self.name_prefix.is_empty() &&
|
||||||
self.get_services().is_empty() &&
|
self.get_services().is_empty() &&
|
||||||
self.manufacturer_id.is_none() &&
|
self.manufacturer_id.is_none() &&
|
||||||
self.service_data_uuid.is_empty()) ||
|
self.service_data_uuid.is_empty()) ||
|
||||||
self.name.len() > MAX_NAME_LENGTH ||
|
self.get_name().unwrap_or("").len() > MAX_NAME_LENGTH ||
|
||||||
self.name_prefix.len() > MAX_NAME_LENGTH
|
self.name_prefix.len() > MAX_NAME_LENGTH
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,30 +2,34 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted};
|
|
||||||
use bluetooth_traits::{BluetoothError, BluetoothMethodMsg};
|
use bluetooth_traits::{BluetoothError, BluetoothMethodMsg};
|
||||||
|
use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted};
|
||||||
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
|
use bluetooth_traits::scanfilter::{BluetoothScanfilter, BluetoothScanfilterSequence};
|
||||||
use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
|
use bluetooth_traits::scanfilter::{RequestDeviceoptions, ServiceUUIDSequence};
|
||||||
use core::clone::Clone;
|
use core::clone::Clone;
|
||||||
|
use dom::bindings::cell::DOMRefCell;
|
||||||
use dom::bindings::codegen::Bindings::BluetoothBinding::{self, BluetoothMethods, BluetoothRequestDeviceFilter};
|
use dom::bindings::codegen::Bindings::BluetoothBinding::{self, BluetoothMethods, BluetoothRequestDeviceFilter};
|
||||||
use dom::bindings::codegen::Bindings::BluetoothBinding::RequestDeviceOptions;
|
use dom::bindings::codegen::Bindings::BluetoothBinding::RequestDeviceOptions;
|
||||||
use dom::bindings::error::Error::{self, Security, Type};
|
use dom::bindings::error::Error::{self, NotFound, Security, Type};
|
||||||
use dom::bindings::error::Fallible;
|
use dom::bindings::error::Fallible;
|
||||||
use dom::bindings::js::Root;
|
use dom::bindings::js::{JS, MutHeap, Root};
|
||||||
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
||||||
use dom::bindings::str::DOMString;
|
use dom::bindings::str::DOMString;
|
||||||
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
|
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
|
||||||
use dom::bluetoothdevice::BluetoothDevice;
|
use dom::bluetoothdevice::BluetoothDevice;
|
||||||
|
use dom::bluetoothremotegattcharacteristic::BluetoothRemoteGATTCharacteristic;
|
||||||
|
use dom::bluetoothremotegattdescriptor::BluetoothRemoteGATTDescriptor;
|
||||||
|
use dom::bluetoothremotegattservice::BluetoothRemoteGATTService;
|
||||||
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
|
use dom::bluetoothuuid::{BluetoothServiceUUID, BluetoothUUID};
|
||||||
use dom::globalscope::GlobalScope;
|
use dom::globalscope::GlobalScope;
|
||||||
use dom::promise::Promise;
|
use dom::promise::Promise;
|
||||||
use ipc_channel::ipc::{self, IpcSender};
|
use ipc_channel::ipc::{self, IpcSender};
|
||||||
use js::conversions::ToJSValConvertible;
|
use js::conversions::ToJSValConvertible;
|
||||||
|
use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
const FILTER_EMPTY_ERROR: &'static str = "'filters' member, if present, must be nonempty to find any devices.";
|
const FILTER_EMPTY_ERROR: &'static str = "'filters' member, if present, must be nonempty to find any devices.";
|
||||||
const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way.";
|
const FILTER_ERROR: &'static str = "A filter must restrict the devices in some way.";
|
||||||
const FILTER_NAME_TOO_LONG_ERROR: &'static str = "A 'name' or 'namePrefix' can't be longer then 29 bytes.";
|
|
||||||
// 248 is the maximum number of UTF-8 code units in a Bluetooth Device Name.
|
// 248 is the maximum number of UTF-8 code units in a Bluetooth Device Name.
|
||||||
const MAX_DEVICE_NAME_LENGTH: usize = 248;
|
const MAX_DEVICE_NAME_LENGTH: usize = 248;
|
||||||
// A device name can never be longer than 29 bytes.
|
// A device name can never be longer than 29 bytes.
|
||||||
|
@ -43,12 +47,20 @@ const OPTIONS_ERROR: &'static str = "Fields of 'options' conflict with each othe
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct Bluetooth {
|
pub struct Bluetooth {
|
||||||
reflector_: Reflector,
|
reflector_: Reflector,
|
||||||
|
device_instance_map: DOMRefCell<HashMap<String, MutHeap<JS<BluetoothDevice>>>>,
|
||||||
|
service_instance_map: DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTService>>>>,
|
||||||
|
characteristic_instance_map: DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTCharacteristic>>>>,
|
||||||
|
descriptor_instance_map: DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTDescriptor>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bluetooth {
|
impl Bluetooth {
|
||||||
pub fn new_inherited() -> Bluetooth {
|
pub fn new_inherited() -> Bluetooth {
|
||||||
Bluetooth {
|
Bluetooth {
|
||||||
reflector_: Reflector::new(),
|
reflector_: Reflector::new(),
|
||||||
|
device_instance_map: DOMRefCell::new(HashMap::new()),
|
||||||
|
service_instance_map: DOMRefCell::new(HashMap::new()),
|
||||||
|
characteristic_instance_map: DOMRefCell::new(HashMap::new()),
|
||||||
|
descriptor_instance_map: DOMRefCell::new(HashMap::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,6 +70,19 @@ impl Bluetooth {
|
||||||
BluetoothBinding::Wrap)
|
BluetoothBinding::Wrap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_service_map(&self) -> &DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTService>>>> {
|
||||||
|
&self.service_instance_map
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_characteristic_map(&self)
|
||||||
|
-> &DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTCharacteristic>>>> {
|
||||||
|
&self.characteristic_instance_map
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_descriptor_map(&self) -> &DOMRefCell<HashMap<String, MutHeap<JS<BluetoothRemoteGATTDescriptor>>>> {
|
||||||
|
&self.descriptor_instance_map
|
||||||
|
}
|
||||||
|
|
||||||
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
|
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
|
||||||
self.global().as_window().bluetooth_thread()
|
self.global().as_window().bluetooth_thread()
|
||||||
}
|
}
|
||||||
|
@ -103,15 +128,21 @@ impl Bluetooth {
|
||||||
// Step 12-13.
|
// Step 12-13.
|
||||||
match device {
|
match device {
|
||||||
Ok(device) => {
|
Ok(device) => {
|
||||||
let global = self.global();
|
let mut device_instance_map = self.device_instance_map.borrow_mut();
|
||||||
let ad_data = BluetoothAdvertisingData::new(&global,
|
if let Some(existing_device) = device_instance_map.get(&device.id.clone()) {
|
||||||
|
return Ok(existing_device.get());
|
||||||
|
}
|
||||||
|
let ad_data = BluetoothAdvertisingData::new(&self.global(),
|
||||||
device.appearance,
|
device.appearance,
|
||||||
device.tx_power,
|
device.tx_power,
|
||||||
device.rssi);
|
device.rssi);
|
||||||
Ok(BluetoothDevice::new(&global,
|
let bt_device = BluetoothDevice::new(&self.global(),
|
||||||
DOMString::from(device.id),
|
DOMString::from(device.id.clone()),
|
||||||
device.name.map(DOMString::from),
|
device.name.map(DOMString::from),
|
||||||
&ad_data))
|
&ad_data,
|
||||||
|
&self);
|
||||||
|
device_instance_map.insert(device.id, MutHeap::new(&bt_device));
|
||||||
|
Ok(bt_device)
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
|
@ -213,13 +244,13 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter) -> Fallible<Blueto
|
||||||
return Err(Type(NAME_TOO_LONG_ERROR.to_owned()));
|
return Err(Type(NAME_TOO_LONG_ERROR.to_owned()));
|
||||||
}
|
}
|
||||||
if name.len() > MAX_FILTER_NAME_LENGTH {
|
if name.len() > MAX_FILTER_NAME_LENGTH {
|
||||||
return Err(Type(FILTER_NAME_TOO_LONG_ERROR.to_owned()));
|
return Err(NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2.4.4.2.
|
// Step 2.4.4.2.
|
||||||
name.to_string()
|
Some(name.to_string())
|
||||||
},
|
},
|
||||||
None => String::new(),
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Step 2.4.5.
|
// Step 2.4.5.
|
||||||
|
@ -233,7 +264,7 @@ fn canonicalize_filter(filter: &BluetoothRequestDeviceFilter) -> Fallible<Blueto
|
||||||
return Err(Type(NAME_TOO_LONG_ERROR.to_owned()));
|
return Err(Type(NAME_TOO_LONG_ERROR.to_owned()));
|
||||||
}
|
}
|
||||||
if name_prefix.len() > MAX_FILTER_NAME_LENGTH {
|
if name_prefix.len() > MAX_FILTER_NAME_LENGTH {
|
||||||
return Err(Type(FILTER_NAME_TOO_LONG_ERROR.to_owned()));
|
return Err(NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2.4.5.2.
|
// Step 2.4.5.2.
|
||||||
|
@ -289,6 +320,7 @@ impl From<BluetoothError> for Error {
|
||||||
BluetoothError::NotFound => Error::NotFound,
|
BluetoothError::NotFound => Error::NotFound,
|
||||||
BluetoothError::NotSupported => Error::NotSupported,
|
BluetoothError::NotSupported => Error::NotSupported,
|
||||||
BluetoothError::Security => Error::Security,
|
BluetoothError::Security => Error::Security,
|
||||||
|
BluetoothError::InvalidState => Error::InvalidState,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMet
|
||||||
use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap};
|
use dom::bindings::js::{JS, Root, MutHeap, MutNullableHeap};
|
||||||
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
||||||
use dom::bindings::str::DOMString;
|
use dom::bindings::str::DOMString;
|
||||||
|
use dom::bluetooth::Bluetooth;
|
||||||
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
|
use dom::bluetoothadvertisingdata::BluetoothAdvertisingData;
|
||||||
use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer;
|
use dom::bluetoothremotegattserver::BluetoothRemoteGATTServer;
|
||||||
use dom::globalscope::GlobalScope;
|
use dom::globalscope::GlobalScope;
|
||||||
|
@ -19,12 +20,14 @@ pub struct BluetoothDevice {
|
||||||
name: Option<DOMString>,
|
name: Option<DOMString>,
|
||||||
ad_data: MutHeap<JS<BluetoothAdvertisingData>>,
|
ad_data: MutHeap<JS<BluetoothAdvertisingData>>,
|
||||||
gatt: MutNullableHeap<JS<BluetoothRemoteGATTServer>>,
|
gatt: MutNullableHeap<JS<BluetoothRemoteGATTServer>>,
|
||||||
|
context: MutHeap<JS<Bluetooth>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BluetoothDevice {
|
impl BluetoothDevice {
|
||||||
pub fn new_inherited(id: DOMString,
|
pub fn new_inherited(id: DOMString,
|
||||||
name: Option<DOMString>,
|
name: Option<DOMString>,
|
||||||
ad_data: &BluetoothAdvertisingData)
|
ad_data: &BluetoothAdvertisingData,
|
||||||
|
context: &Bluetooth)
|
||||||
-> BluetoothDevice {
|
-> BluetoothDevice {
|
||||||
BluetoothDevice {
|
BluetoothDevice {
|
||||||
reflector_: Reflector::new(),
|
reflector_: Reflector::new(),
|
||||||
|
@ -32,20 +35,27 @@ impl BluetoothDevice {
|
||||||
name: name,
|
name: name,
|
||||||
ad_data: MutHeap::new(ad_data),
|
ad_data: MutHeap::new(ad_data),
|
||||||
gatt: Default::default(),
|
gatt: Default::default(),
|
||||||
|
context: MutHeap::new(context),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(global: &GlobalScope,
|
pub fn new(global: &GlobalScope,
|
||||||
id: DOMString,
|
id: DOMString,
|
||||||
name: Option<DOMString>,
|
name: Option<DOMString>,
|
||||||
adData: &BluetoothAdvertisingData)
|
adData: &BluetoothAdvertisingData,
|
||||||
|
context: &Bluetooth)
|
||||||
-> Root<BluetoothDevice> {
|
-> Root<BluetoothDevice> {
|
||||||
reflect_dom_object(box BluetoothDevice::new_inherited(id,
|
reflect_dom_object(box BluetoothDevice::new_inherited(id,
|
||||||
name,
|
name,
|
||||||
adData),
|
adData,
|
||||||
|
context),
|
||||||
global,
|
global,
|
||||||
BluetoothDeviceBinding::Wrap)
|
BluetoothDeviceBinding::Wrap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_context(&self) -> Root<Bluetooth> {
|
||||||
|
self.context.get()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BluetoothDeviceMethods for BluetoothDevice {
|
impl BluetoothDeviceMethods for BluetoothDevice {
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted};
|
|
||||||
use bluetooth_traits::BluetoothMethodMsg;
|
use bluetooth_traits::BluetoothMethodMsg;
|
||||||
|
use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted};
|
||||||
use dom::bindings::cell::DOMRefCell;
|
use dom::bindings::cell::DOMRefCell;
|
||||||
use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding::
|
use dom::bindings::codegen::Bindings::BluetoothCharacteristicPropertiesBinding::
|
||||||
BluetoothCharacteristicPropertiesMethods;
|
BluetoothCharacteristicPropertiesMethods;
|
||||||
|
@ -87,16 +87,26 @@ impl BluetoothRemoteGATTCharacteristic {
|
||||||
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||||
return Err(Security)
|
return Err(Security)
|
||||||
}
|
}
|
||||||
|
if !self.Service().Device().Gatt().Connected() {
|
||||||
|
return Err(Network)
|
||||||
|
}
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::GetDescriptor(self.get_instance_id(), uuid, sender)).unwrap();
|
BluetoothMethodMsg::GetDescriptor(self.get_instance_id(), uuid, sender)).unwrap();
|
||||||
let descriptor = receiver.recv().unwrap();
|
let descriptor = receiver.recv().unwrap();
|
||||||
match descriptor {
|
match descriptor {
|
||||||
Ok(descriptor) => {
|
Ok(descriptor) => {
|
||||||
Ok(BluetoothRemoteGATTDescriptor::new(&self.global(),
|
let context = self.service.get().get_device().get_context();
|
||||||
|
let mut descriptor_map = context.get_descriptor_map().borrow_mut();
|
||||||
|
if let Some(existing_descriptor) = descriptor_map.get(&descriptor.instance_id) {
|
||||||
|
return Ok(existing_descriptor.get());
|
||||||
|
}
|
||||||
|
let bt_descriptor = BluetoothRemoteGATTDescriptor::new(&self.global(),
|
||||||
self,
|
self,
|
||||||
DOMString::from(descriptor.uuid),
|
DOMString::from(descriptor.uuid),
|
||||||
descriptor.instance_id))
|
descriptor.instance_id.clone());
|
||||||
|
descriptor_map.insert(descriptor.instance_id, MutHeap::new(&bt_descriptor));
|
||||||
|
Ok(bt_descriptor)
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
|
@ -117,18 +127,34 @@ impl BluetoothRemoteGATTCharacteristic {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
if !self.Service().Device().Gatt().Connected() {
|
||||||
|
return Err(Network)
|
||||||
|
}
|
||||||
|
let mut descriptors = vec!();
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap();
|
BluetoothMethodMsg::GetDescriptors(self.get_instance_id(), uuid, sender)).unwrap();
|
||||||
let descriptors_vec = receiver.recv().unwrap();
|
let descriptors_vec = receiver.recv().unwrap();
|
||||||
match descriptors_vec {
|
match descriptors_vec {
|
||||||
Ok(descriptor_vec) => {
|
Ok(descriptor_vec) => {
|
||||||
Ok(descriptor_vec.into_iter()
|
let context = self.service.get().get_device().get_context();
|
||||||
.map(|desc| BluetoothRemoteGATTDescriptor::new(&self.global(),
|
let mut descriptor_map = context.get_descriptor_map().borrow_mut();
|
||||||
|
for descriptor in descriptor_vec {
|
||||||
|
let bt_descriptor = match descriptor_map.get(&descriptor.instance_id) {
|
||||||
|
Some(existing_descriptor) => existing_descriptor.get(),
|
||||||
|
None => {
|
||||||
|
BluetoothRemoteGATTDescriptor::new(&self.global(),
|
||||||
self,
|
self,
|
||||||
DOMString::from(desc.uuid),
|
DOMString::from(descriptor.uuid),
|
||||||
desc.instance_id))
|
descriptor.instance_id.clone())
|
||||||
.collect())
|
},
|
||||||
|
};
|
||||||
|
if !descriptor_map.contains_key(&descriptor.instance_id) {
|
||||||
|
descriptor_map.insert(descriptor.instance_id, MutHeap::new(&bt_descriptor));
|
||||||
|
}
|
||||||
|
descriptors.push(bt_descriptor);
|
||||||
|
}
|
||||||
|
Ok(descriptors)
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
|
@ -182,10 +208,10 @@ impl BluetoothRemoteGATTCharacteristic {
|
||||||
}
|
}
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
|
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value.clone(), sender)).unwrap();
|
||||||
let result = receiver.recv().unwrap();
|
let result = receiver.recv().unwrap();
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(*self.value.borrow_mut() = Some(ByteString::new(value))),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
},
|
},
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted};
|
|
||||||
use bluetooth_traits::BluetoothMethodMsg;
|
use bluetooth_traits::BluetoothMethodMsg;
|
||||||
|
use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted};
|
||||||
use dom::bindings::cell::DOMRefCell;
|
use dom::bindings::cell::DOMRefCell;
|
||||||
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
|
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
|
||||||
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::
|
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTCharacteristicBinding::
|
||||||
|
@ -105,10 +105,10 @@ impl BluetoothRemoteGATTDescriptor {
|
||||||
}
|
}
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value, sender)).unwrap();
|
BluetoothMethodMsg::WriteValue(self.get_instance_id(), value.clone(), sender)).unwrap();
|
||||||
let result = receiver.recv().unwrap();
|
let result = receiver.recv().unwrap();
|
||||||
match result {
|
match result {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(*self.value.borrow_mut() = Some(ByteString::new(value))),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
},
|
},
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted};
|
|
||||||
use bluetooth_traits::BluetoothMethodMsg;
|
use bluetooth_traits::BluetoothMethodMsg;
|
||||||
|
use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted};
|
||||||
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
|
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
|
||||||
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
|
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding;
|
||||||
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
|
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
|
||||||
use dom::bindings::error::{ErrorResult, Fallible};
|
use dom::bindings::error::{ErrorResult, Fallible};
|
||||||
use dom::bindings::error::Error::{self, Security};
|
use dom::bindings::error::Error::{self, Network, Security};
|
||||||
use dom::bindings::js::{JS, MutHeap, Root};
|
use dom::bindings::js::{JS, MutHeap, Root};
|
||||||
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
||||||
use dom::bindings::str::DOMString;
|
use dom::bindings::str::DOMString;
|
||||||
|
@ -72,17 +72,27 @@ impl BluetoothRemoteGATTServer {
|
||||||
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||||
return Err(Security)
|
return Err(Security)
|
||||||
}
|
}
|
||||||
|
if !self.Device().Gatt().Connected() {
|
||||||
|
return Err(Network)
|
||||||
|
}
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::GetPrimaryService(String::from(self.Device().Id()), uuid, sender)).unwrap();
|
BluetoothMethodMsg::GetPrimaryService(String::from(self.Device().Id()), uuid, sender)).unwrap();
|
||||||
let service = receiver.recv().unwrap();
|
let service = receiver.recv().unwrap();
|
||||||
match service {
|
match service {
|
||||||
Ok(service) => {
|
Ok(service) => {
|
||||||
Ok(BluetoothRemoteGATTService::new(&self.global(),
|
let context = self.device.get().get_context();
|
||||||
|
let mut service_map = context.get_service_map().borrow_mut();
|
||||||
|
if let Some(existing_service) = service_map.get(&service.instance_id) {
|
||||||
|
return Ok(existing_service.get());
|
||||||
|
}
|
||||||
|
let bt_service = BluetoothRemoteGATTService::new(&self.global(),
|
||||||
&self.device.get(),
|
&self.device.get(),
|
||||||
DOMString::from(service.uuid),
|
DOMString::from(service.uuid),
|
||||||
service.is_primary,
|
service.is_primary,
|
||||||
service.instance_id))
|
service.instance_id.clone());
|
||||||
|
service_map.insert(service.instance_id, MutHeap::new(&bt_service));
|
||||||
|
Ok(bt_service)
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
|
@ -103,19 +113,35 @@ impl BluetoothRemoteGATTServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
if !self.Device().Gatt().Connected() {
|
||||||
|
return Err(Network)
|
||||||
|
}
|
||||||
|
let mut services = vec!();
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
|
BluetoothMethodMsg::GetPrimaryServices(String::from(self.Device().Id()), uuid, sender)).unwrap();
|
||||||
let services_vec = receiver.recv().unwrap();
|
let services_vec = receiver.recv().unwrap();
|
||||||
match services_vec {
|
match services_vec {
|
||||||
Ok(service_vec) => {
|
Ok(service_vec) => {
|
||||||
Ok(service_vec.into_iter()
|
let context = self.device.get().get_context();
|
||||||
.map(|service| BluetoothRemoteGATTService::new(&self.global(),
|
let mut service_map = context.get_service_map().borrow_mut();
|
||||||
|
for service in service_vec {
|
||||||
|
let bt_service = match service_map.get(&service.instance_id) {
|
||||||
|
Some(existing_service) => existing_service.get(),
|
||||||
|
None => {
|
||||||
|
BluetoothRemoteGATTService::new(&self.global(),
|
||||||
&self.device.get(),
|
&self.device.get(),
|
||||||
DOMString::from(service.uuid),
|
DOMString::from(service.uuid),
|
||||||
service.is_primary,
|
service.is_primary,
|
||||||
service.instance_id))
|
service.instance_id.clone())
|
||||||
.collect())
|
},
|
||||||
|
};
|
||||||
|
if !service_map.contains_key(&service.instance_id) {
|
||||||
|
service_map.insert(service.instance_id, MutHeap::new(&bt_service));
|
||||||
|
}
|
||||||
|
services.push(bt_service);
|
||||||
|
}
|
||||||
|
Ok(services)
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
|
|
|
@ -2,11 +2,13 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
use bluetooth_blacklist::{Blacklist, uuid_is_blacklisted};
|
|
||||||
use bluetooth_traits::BluetoothMethodMsg;
|
use bluetooth_traits::BluetoothMethodMsg;
|
||||||
|
use bluetooth_traits::blacklist::{Blacklist, uuid_is_blacklisted};
|
||||||
|
use dom::bindings::codegen::Bindings::BluetoothDeviceBinding::BluetoothDeviceMethods;
|
||||||
|
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServerBinding::BluetoothRemoteGATTServerMethods;
|
||||||
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding;
|
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding;
|
||||||
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
|
use dom::bindings::codegen::Bindings::BluetoothRemoteGATTServiceBinding::BluetoothRemoteGATTServiceMethods;
|
||||||
use dom::bindings::error::Error::{self, Security};
|
use dom::bindings::error::Error::{self, Network, Security};
|
||||||
use dom::bindings::error::Fallible;
|
use dom::bindings::error::Fallible;
|
||||||
use dom::bindings::js::{JS, MutHeap, Root};
|
use dom::bindings::js::{JS, MutHeap, Root};
|
||||||
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
use dom::bindings::reflector::{Reflectable, Reflector, reflect_dom_object};
|
||||||
|
@ -60,6 +62,10 @@ impl BluetoothRemoteGATTService {
|
||||||
BluetoothRemoteGATTServiceBinding::Wrap)
|
BluetoothRemoteGATTServiceBinding::Wrap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_device(&self) -> Root<BluetoothDevice> {
|
||||||
|
self.device.get()
|
||||||
|
}
|
||||||
|
|
||||||
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
|
fn get_bluetooth_thread(&self) -> IpcSender<BluetoothMethodMsg> {
|
||||||
self.global().as_window().bluetooth_thread()
|
self.global().as_window().bluetooth_thread()
|
||||||
}
|
}
|
||||||
|
@ -76,12 +82,20 @@ impl BluetoothRemoteGATTService {
|
||||||
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||||
return Err(Security)
|
return Err(Security)
|
||||||
}
|
}
|
||||||
|
if !self.Device().Gatt().Connected() {
|
||||||
|
return Err(Network)
|
||||||
|
}
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap();
|
BluetoothMethodMsg::GetCharacteristic(self.get_instance_id(), uuid, sender)).unwrap();
|
||||||
let characteristic = receiver.recv().unwrap();
|
let characteristic = receiver.recv().unwrap();
|
||||||
match characteristic {
|
match characteristic {
|
||||||
Ok(characteristic) => {
|
Ok(characteristic) => {
|
||||||
|
let context = self.device.get().get_context();
|
||||||
|
let mut characteristic_map = context.get_characteristic_map().borrow_mut();
|
||||||
|
if let Some(existing_characteristic) = characteristic_map.get(&characteristic.instance_id) {
|
||||||
|
return Ok(existing_characteristic.get());
|
||||||
|
}
|
||||||
let global = self.global();
|
let global = self.global();
|
||||||
let properties = BluetoothCharacteristicProperties::new(&global,
|
let properties = BluetoothCharacteristicProperties::new(&global,
|
||||||
characteristic.broadcast,
|
characteristic.broadcast,
|
||||||
|
@ -93,11 +107,13 @@ impl BluetoothRemoteGATTService {
|
||||||
characteristic.authenticated_signed_writes,
|
characteristic.authenticated_signed_writes,
|
||||||
characteristic.reliable_write,
|
characteristic.reliable_write,
|
||||||
characteristic.writable_auxiliaries);
|
characteristic.writable_auxiliaries);
|
||||||
Ok(BluetoothRemoteGATTCharacteristic::new(&global,
|
let bt_characteristic = BluetoothRemoteGATTCharacteristic::new(&global,
|
||||||
self,
|
self,
|
||||||
DOMString::from(characteristic.uuid),
|
DOMString::from(characteristic.uuid),
|
||||||
&properties,
|
&properties,
|
||||||
characteristic.instance_id))
|
characteristic.instance_id.clone());
|
||||||
|
characteristic_map.insert(characteristic.instance_id, MutHeap::new(&bt_characteristic));
|
||||||
|
Ok(bt_characteristic)
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
|
@ -118,6 +134,9 @@ impl BluetoothRemoteGATTService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
if !self.Device().Gatt().Connected() {
|
||||||
|
return Err(Network)
|
||||||
|
}
|
||||||
let mut characteristics = vec!();
|
let mut characteristics = vec!();
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
|
@ -125,9 +144,14 @@ impl BluetoothRemoteGATTService {
|
||||||
let characteristics_vec = receiver.recv().unwrap();
|
let characteristics_vec = receiver.recv().unwrap();
|
||||||
match characteristics_vec {
|
match characteristics_vec {
|
||||||
Ok(characteristic_vec) => {
|
Ok(characteristic_vec) => {
|
||||||
|
let context = self.device.get().get_context();
|
||||||
|
let mut characteristic_map = context.get_characteristic_map().borrow_mut();
|
||||||
for characteristic in characteristic_vec {
|
for characteristic in characteristic_vec {
|
||||||
let global = self.global();
|
let bt_characteristic = match characteristic_map.get(&characteristic.instance_id) {
|
||||||
let properties = BluetoothCharacteristicProperties::new(&global,
|
Some(existing_characteristic) => existing_characteristic.get(),
|
||||||
|
None => {
|
||||||
|
let properties =
|
||||||
|
BluetoothCharacteristicProperties::new(&self.global(),
|
||||||
characteristic.broadcast,
|
characteristic.broadcast,
|
||||||
characteristic.read,
|
characteristic.read,
|
||||||
characteristic.write_without_response,
|
characteristic.write_without_response,
|
||||||
|
@ -137,11 +161,18 @@ impl BluetoothRemoteGATTService {
|
||||||
characteristic.authenticated_signed_writes,
|
characteristic.authenticated_signed_writes,
|
||||||
characteristic.reliable_write,
|
characteristic.reliable_write,
|
||||||
characteristic.writable_auxiliaries);
|
characteristic.writable_auxiliaries);
|
||||||
characteristics.push(BluetoothRemoteGATTCharacteristic::new(&global,
|
|
||||||
|
BluetoothRemoteGATTCharacteristic::new(&self.global(),
|
||||||
self,
|
self,
|
||||||
DOMString::from(characteristic.uuid),
|
DOMString::from(characteristic.uuid),
|
||||||
&properties,
|
&properties,
|
||||||
characteristic.instance_id));
|
characteristic.instance_id.clone())
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if !characteristic_map.contains_key(&characteristic.instance_id) {
|
||||||
|
characteristic_map.insert(characteristic.instance_id, MutHeap::new(&bt_characteristic));
|
||||||
|
}
|
||||||
|
characteristics.push(bt_characteristic);
|
||||||
}
|
}
|
||||||
Ok(characteristics)
|
Ok(characteristics)
|
||||||
},
|
},
|
||||||
|
@ -159,6 +190,9 @@ impl BluetoothRemoteGATTService {
|
||||||
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
if uuid_is_blacklisted(uuid.as_ref(), Blacklist::All) {
|
||||||
return Err(Security)
|
return Err(Security)
|
||||||
}
|
}
|
||||||
|
if !self.Device().Gatt().Connected() {
|
||||||
|
return Err(Network)
|
||||||
|
}
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::GetIncludedService(self.get_instance_id(),
|
BluetoothMethodMsg::GetIncludedService(self.get_instance_id(),
|
||||||
|
@ -167,11 +201,18 @@ impl BluetoothRemoteGATTService {
|
||||||
let service = receiver.recv().unwrap();
|
let service = receiver.recv().unwrap();
|
||||||
match service {
|
match service {
|
||||||
Ok(service) => {
|
Ok(service) => {
|
||||||
Ok(BluetoothRemoteGATTService::new(&self.global(),
|
let context = self.device.get().get_context();
|
||||||
|
let mut service_map = context.get_service_map().borrow_mut();
|
||||||
|
if let Some(existing_service) = service_map.get(&service.instance_id) {
|
||||||
|
return Ok(existing_service.get());
|
||||||
|
}
|
||||||
|
let bt_service = BluetoothRemoteGATTService::new(&self.global(),
|
||||||
&self.device.get(),
|
&self.device.get(),
|
||||||
DOMString::from(service.uuid),
|
DOMString::from(service.uuid),
|
||||||
service.is_primary,
|
service.is_primary,
|
||||||
service.instance_id))
|
service.instance_id.clone());
|
||||||
|
service_map.insert(service.instance_id, MutHeap::new(&bt_service));
|
||||||
|
Ok(bt_service)
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
|
@ -192,21 +233,37 @@ impl BluetoothRemoteGATTService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
if !self.Device().Gatt().Connected() {
|
||||||
|
return Err(Network)
|
||||||
|
}
|
||||||
let (sender, receiver) = ipc::channel().unwrap();
|
let (sender, receiver) = ipc::channel().unwrap();
|
||||||
self.get_bluetooth_thread().send(
|
self.get_bluetooth_thread().send(
|
||||||
BluetoothMethodMsg::GetIncludedServices(self.get_instance_id(),
|
BluetoothMethodMsg::GetIncludedServices(self.get_instance_id(),
|
||||||
uuid,
|
uuid,
|
||||||
sender)).unwrap();
|
sender)).unwrap();
|
||||||
let services_vec = receiver.recv().unwrap();
|
let services_vec = receiver.recv().unwrap();
|
||||||
|
let mut services = vec!();
|
||||||
match services_vec {
|
match services_vec {
|
||||||
Ok(service_vec) => {
|
Ok(service_vec) => {
|
||||||
Ok(service_vec.into_iter()
|
let context = self.device.get().get_context();
|
||||||
.map(|service| BluetoothRemoteGATTService::new(&self.global(),
|
let mut service_map = context.get_service_map().borrow_mut();
|
||||||
|
for service in service_vec {
|
||||||
|
let bt_service = match service_map.get(&service.instance_id) {
|
||||||
|
Some(existing_service) => existing_service.get(),
|
||||||
|
None => {
|
||||||
|
BluetoothRemoteGATTService::new(&self.global(),
|
||||||
&self.device.get(),
|
&self.device.get(),
|
||||||
DOMString::from(service.uuid),
|
DOMString::from(service.uuid),
|
||||||
service.is_primary,
|
service.is_primary,
|
||||||
service.instance_id))
|
service.instance_id.clone())
|
||||||
.collect())
|
},
|
||||||
|
};
|
||||||
|
if !service_map.contains_key(&service.instance_id) {
|
||||||
|
service_map.insert(service.instance_id, MutHeap::new(&bt_service));
|
||||||
|
}
|
||||||
|
services.push(bt_service);
|
||||||
|
}
|
||||||
|
Ok(services)
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
Err(Error::from(error))
|
Err(Error::from(error))
|
||||||
|
|
|
@ -8,9 +8,13 @@
|
||||||
interface BluetoothDevice {
|
interface BluetoothDevice {
|
||||||
readonly attribute DOMString id;
|
readonly attribute DOMString id;
|
||||||
readonly attribute DOMString? name;
|
readonly attribute DOMString? name;
|
||||||
|
// TODO: remove this after BluetoothAdvertisingEvent implemented.
|
||||||
readonly attribute BluetoothAdvertisingData adData;
|
readonly attribute BluetoothAdvertisingData adData;
|
||||||
readonly attribute BluetoothRemoteGATTServer gatt;
|
readonly attribute BluetoothRemoteGATTServer gatt;
|
||||||
// readonly attribute FrozenArray[] uuids;
|
|
||||||
|
// Promise<void> watchAdvertisements();
|
||||||
|
// void unwatchAdvertisements();
|
||||||
|
// readonly attribute boolean watchingAdvertisements;
|
||||||
};
|
};
|
||||||
|
|
||||||
// BluetoothDevice implements EventTarget;
|
// BluetoothDevice implements EventTarget;
|
||||||
|
|
|
@ -96,7 +96,6 @@ extern crate webrender_traits;
|
||||||
extern crate websocket;
|
extern crate websocket;
|
||||||
extern crate xml5ever;
|
extern crate xml5ever;
|
||||||
|
|
||||||
pub mod bluetooth_blacklist;
|
|
||||||
mod body;
|
mod body;
|
||||||
pub mod clipboard_provider;
|
pub mod clipboard_provider;
|
||||||
mod devtools;
|
mod devtools;
|
||||||
|
|
|
@ -207,8 +207,10 @@ name = "bluetooth_traits"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ipc-channel 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"ipc-channel 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"regex 0.1.76 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde_derive 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde_derive 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"util 0.0.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
|
@ -178,8 +178,10 @@ name = "bluetooth_traits"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ipc-channel 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"ipc-channel 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"regex 0.1.76 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"serde_derive 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
"serde_derive 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"util 0.0.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
Загрузка…
Ссылка в новой задаче