Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
Auto merge of #23 - servo:fork-blurmac, r=nox
Browse files Browse the repository at this point in the history
Fork blurmac

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/devices/23)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Feb 19, 2018
2 parents c3b012b + eea7884 commit 1069d67
Show file tree
Hide file tree
Showing 15 changed files with 1,846 additions and 5 deletions.
19 changes: 15 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
language: rust
sudo: required

rust:
- stable
- beta
- nightly

os:
- linux
- osx

dist: trusty
notifications:
webhooks: http://build.servo.org:54856/travis
addons:
apt:
packages: libdbus-1-dev

script:
- sudo apt-get install libdbus-1-dev
- cargo build --features bluetooth-test
- cargo test

notifications:
webhooks: http://build.servo.org:54856/travis
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ blurz = { version = "0.2.1", optional = true }
blurdroid = { version = "0.1.2", optional = true }

[target.'cfg(target_os = "macos")'.dependencies]
blurmac = { version = "0.0.1", optional = true }
blurmac = { path = "blurmac", optional = true }

[dependencies]
blurmock = { version = "0.1.2", optional = true }
18 changes: 18 additions & 0 deletions blurmac/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "blurmac"
description = "Bluetooth Rust lib using macOS CoreBluetooth"
version = "0.1.0"
readme = "README.md"
keywords = ["bluetooth", "ble", "macOS", "CoreBluetooth"]
repository = "https://github.com/akosthekiss/blurmac"
authors = ["Akos Kiss <[email protected]>"]
license = "BSD-3-Clause"

[lib]
name = "blurmac"
path = "src/lib.rs"
crate-type = ["rlib"]

[dependencies]
log = "0.4"
objc = "0.2"
27 changes: 27 additions & 0 deletions blurmac/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2017 Akos Kiss.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 changes: 55 additions & 0 deletions blurmac/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Bluetooth Rust lib using macOS CoreBluetooth

[![Build Status](https://travis-ci.org/akosthekiss/blurmac.svg?branch=master)](https://travis-ci.org/akosthekiss/blurmac)
[![Crates.io](https://img.shields.io/crates/v/blurmac.svg)](https://crates.io/crates/blurmac)

The main aim of BlurMac is to enable [WebBluetooth](https://webbluetoothcg.github.io)
in [Servo](https://github.com/servo/servo) on macOS. Thus, API and implementation
decisions are affected by the encapsulating [Devices](https://github.com/servo/devices),
and the sibling [BlurZ](https://github.com/szeged/blurz) and [BlurDroid](https://github.com/szeged/blurdroid)
crates.


## Run Servo with WebBluetooth Enabled

Usually, you don't want to work with BlurMac on its own but use it within Servo.
So, most probably you'll want to run Servo with WebBluetooth enabled:

```
RUST_LOG=blurmac \
./mach run \
--dev \
--pref=dom.bluetooth.enabled \
--pref=dom.permissions.testing.allowed_in_nonsecure_contexts \
URL
```

Notes:
* The above command is actually not really BlurMac-specific (except for the `RUST_LOG`
part). It runs Servo with WBT enabled on any platform where WBT is supported.
* You don't need the `RUST_LOG=blurmac` part if you don't want to see BlurMac debug
messages on the console.
* You don't need the `--dev` part if you want to run a release build.
* You don't need the `--pref=dom.permissions.testing.allowed_in_nonsecure_contexts`
part if your `URL` is https (but you do need it if you test a local file).


## Known Issues

* Device RSSI can not be retrieved yet.
* Support for included services is incomplete.
* Descriptors are not supported yet.
* Notifications on characteristics are not supported yet (the limitation comes from
Devices).


## Compatibility

Tested on:

* macOS Sierra 10.12.


## Copyright and Licensing

Licensed under the BSD 3-Clause [License](LICENSE.md).
209 changes: 209 additions & 0 deletions blurmac/src/adapter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// Copyright (c) 2017 Akos Kiss.
//
// Licensed under the BSD 3-Clause License
// <LICENSE.md or https://opensource.org/licenses/BSD-3-Clause>.
// This file may not be copied, modified, or distributed except
// according to those terms.

use std::error::Error;
use std::os::raw::c_int;

use objc::runtime::{Object, YES};

use delegate::bm;
use framework::{cb, io, ns};
use utils::{NOT_SUPPORTED_ERROR, nsx};


#[derive(Clone, Debug)]
pub struct BluetoothAdapter {
pub(crate) manager: *mut Object,
pub(crate) delegate: *mut Object,
}
// TODO: implement std::fmt::Debug and/or std::fmt::Display instead of derive?

unsafe impl Send for BluetoothAdapter {}
unsafe impl Sync for BluetoothAdapter {}

impl BluetoothAdapter {
pub fn init() -> Result<BluetoothAdapter, Box<Error>> {
trace!("BluetoothAdapter::init");
let delegate = bm::delegate();
let manager = cb::centralmanager(delegate);
let adapter = BluetoothAdapter { manager: manager, delegate: delegate };

// NOTE: start discovery at once, servo leaves close to no time to do a proper discovery
// in a BluetoothDiscoverySession
adapter.start_discovery().unwrap();

Ok(adapter)
}

pub fn get_id(&self) -> String {
trace!("BluetoothAdapter::get_id");
// NOTE: not aware of any better native ID than the address string
self.get_address().unwrap()
}

pub fn get_name(&self) -> Result<String, Box<Error>> {
trace!("BluetoothAdapter::get_name");
let controller = io::bluetoothhostcontroller_defaultcontroller();
let name = io::bluetoothhostcontroller_nameasstring(controller);
Ok(nsx::string_to_string(name))
}

pub fn get_address(&self) -> Result<String, Box<Error>> {
trace!("BluetoothAdapter::get_address");
let controller = io::bluetoothhostcontroller_defaultcontroller();
let address = io::bluetoothhostcontroller_addressasstring(controller);
Ok(nsx::string_to_string(address))
}

pub fn get_class(&self) -> Result<u32, Box<Error>> {
trace!("BluetoothAdapter::get_class");
let controller = io::bluetoothhostcontroller_defaultcontroller();
let device_class = io::bluetoothhostcontroller_classofdevice(controller);
Ok(device_class)
}

pub fn is_powered(&self) -> Result<bool, Box<Error>> {
trace!("BluetoothAdapter::is_powered");
// NOTE: might be also available through
// [[IOBluetoothHostController defaultController] powerState], but that's readonly, so keep
// it in sync with set_powered
Ok(io::bluetoothpreferencegetcontrollerpowerstate() == 1)
}

pub fn set_powered(&self, value: bool) -> Result<(), Box<Error>> {
trace!("BluetoothAdapter::set_powered");
io::bluetoothpreferencesetcontrollerpowerstate(value as c_int);
// TODO: wait for change to happen? whether it really happened?
Ok(())
}

pub fn is_discoverable(&self) -> Result<bool, Box<Error>> {
trace!("BluetoothAdapter::is_discoverable");
Ok(io::bluetoothpreferencegetdiscoverablestate() == 1)
}

pub fn set_discoverable(&self, value: bool) -> Result<(), Box<Error>> {
trace!("BluetoothAdapter::set_discoverable");
io::bluetoothpreferencesetdiscoverablestate(value as c_int);
// TODO: wait for change to happen? whether it really happened?
Ok(())
}

pub fn get_device_list(&self) -> Result<Vec<String>, Box<Error>> {
trace!("BluetoothAdapter::get_device_list");
let mut v = vec!();
let peripherals = bm::delegate_peripherals(self.delegate);
let keys = ns::dictionary_allkeys(peripherals);
for i in 0..ns::array_count(keys) {
v.push(nsx::string_to_string(ns::array_objectatindex(keys, i)));
}
Ok(v)
}

// Was in BluetoothDiscoverySession

fn start_discovery(&self) -> Result<(), Box<Error>> {
trace!("BluetoothAdapter::start_discovery");
let options = ns::mutabledictionary();
// NOTE: If duplicates are not allowed then a peripheral will not show up again once
// connected and then disconnected.
ns::mutabledictionary_setobject_forkey(options, ns::number_withbool(YES), unsafe { cb::CENTRALMANAGERSCANOPTIONALLOWDUPLICATESKEY });
cb::centralmanager_scanforperipherals_options(self.manager, options);
Ok(())
}

fn stop_discovery(&self) -> Result<(), Box<Error>> {
trace!("BluetoothAdapter::stop_discovery");
cb::centralmanager_stopscan(self.manager);
Ok(())
}

// Not supported

pub fn get_alias(&self) -> Result<String, Box<Error>> {
warn!("BluetoothAdapter::get_alias not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn set_alias(&self, _value: String) -> Result<(), Box<Error>> {
warn!("BluetoothAdapter::set_alias not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn is_pairable(&self) -> Result<bool, Box<Error>> {
warn!("BluetoothAdapter::is_pairable not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn set_pairable(&self, _value: bool) -> Result<(), Box<Error>> {
warn!("BluetoothAdapter::set_pairable not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn get_pairable_timeout(&self) -> Result<u32, Box<Error>> {
warn!("BluetoothAdapter::get_pairable_timeout not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn set_pairable_timeout(&self, _value: u32) -> Result<(), Box<Error>> {
warn!("BluetoothAdapter::set_pairable_timeout not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn get_discoverable_timeout(&self) -> Result<u32, Box<Error>> {
warn!("BluetoothAdapter::get_discoverable_timeout not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn set_discoverable_timeout(&self, _value: u32) -> Result<(), Box<Error>> {
warn!("BluetoothAdapter::set_discoverable_timeout not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn is_discovering(&self) -> Result<bool, Box<Error>> {
warn!("BluetoothAdapter::is_discovering not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn get_uuids(&self) -> Result<Vec<String>, Box<Error>> {
warn!("BluetoothAdapter::get_uuids not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn get_vendor_id_source(&self) -> Result<String, Box<Error>> {
warn!("BluetoothAdapter::get_vendor_id_source not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn get_vendor_id(&self) -> Result<u32, Box<Error>> {
warn!("BluetoothAdapter::get_vendor_id not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn get_product_id(&self) -> Result<u32, Box<Error>> {
warn!("BluetoothAdapter::get_product_id not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn get_device_id(&self) -> Result<u32, Box<Error>> {
warn!("BluetoothAdapter::get_device_id not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}

pub fn get_modalias(&self) -> Result<(String, u32, u32, u32), Box<Error>> {
warn!("BluetoothAdapter::get_modalias not supported by BlurMac");
Err(Box::from(NOT_SUPPORTED_ERROR))
}
}

impl Drop for BluetoothAdapter {
fn drop(&mut self) {
trace!("BluetoothAdapter::drop");
// NOTE: stop discovery only here instead of in BluetoothDiscoverySession
self.stop_discovery().unwrap();
}
}
Loading

0 comments on commit 1069d67

Please sign in to comment.