Split the bluetooth code out from the net crates.

This commit is contained in:
Ms2ger 2016-11-03 14:31:14 +01:00
parent e3493cdd26
commit 4fbe415e80
28 changed files with 143 additions and 34 deletions

View file

@ -1,134 +0,0 @@
/* 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 std::collections::HashSet;
use std::slice::Iter;
// A device name can never be longer than 29 bytes. An adv packet is at most
// 31 bytes long. The length and identifier of the length field take 2 bytes.
// That leaves 29 bytes for the name.
const MAX_NAME_LENGTH: usize = 29;
#[derive(Deserialize, Serialize)]
pub struct ServiceUUIDSequence(Vec<String>);
impl ServiceUUIDSequence {
pub fn new(vec: Vec<String>) -> ServiceUUIDSequence {
ServiceUUIDSequence(vec)
}
fn get_services_set(&self) -> HashSet<String> {
self.0.iter().map(String::clone).collect()
}
}
#[derive(Deserialize, Serialize)]
pub struct BluetoothScanfilter {
name: String,
name_prefix: String,
services: ServiceUUIDSequence,
manufacturer_id: Option<u16>,
service_data_uuid: String,
}
impl BluetoothScanfilter {
pub fn new(name: String,
name_prefix: String,
services: Vec<String>,
manufacturer_id: Option<u16>,
service_data_uuid: String)
-> BluetoothScanfilter {
BluetoothScanfilter {
name: name,
name_prefix: name_prefix,
services: ServiceUUIDSequence::new(services),
manufacturer_id: manufacturer_id,
service_data_uuid: service_data_uuid,
}
}
pub fn get_name(&self) -> &str {
&self.name
}
pub fn get_name_prefix(&self) -> &str {
&self.name_prefix
}
pub fn get_services(&self) -> &[String] {
&self.services.0
}
pub fn get_manufacturer_id(&self) -> Option<u16> {
self.manufacturer_id
}
pub fn get_service_data_uuid(&self) -> &str {
&self.service_data_uuid
}
pub fn is_empty_or_invalid(&self) -> bool {
(self.name.is_empty() &&
self.name_prefix.is_empty() &&
self.get_services().is_empty() &&
self.manufacturer_id.is_none() &&
self.service_data_uuid.is_empty()) ||
self.name.len() > MAX_NAME_LENGTH ||
self.name_prefix.len() > MAX_NAME_LENGTH
}
}
#[derive(Deserialize, Serialize)]
pub struct BluetoothScanfilterSequence(Vec<BluetoothScanfilter>);
impl BluetoothScanfilterSequence {
pub fn new(vec: Vec<BluetoothScanfilter>) -> BluetoothScanfilterSequence {
BluetoothScanfilterSequence(vec)
}
pub fn has_empty_or_invalid_filter(&self) -> bool {
self.0.iter().any(BluetoothScanfilter::is_empty_or_invalid)
}
pub fn iter(&self) -> Iter<BluetoothScanfilter> {
self.0.iter()
}
fn get_services_set(&self) -> HashSet<String> {
self.iter().flat_map(|filter| filter.services.get_services_set()).collect()
}
fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
#[derive(Deserialize, Serialize)]
pub struct RequestDeviceoptions {
filters: BluetoothScanfilterSequence,
optional_services: ServiceUUIDSequence,
}
impl RequestDeviceoptions {
pub fn new(filters: BluetoothScanfilterSequence,
services: ServiceUUIDSequence)
-> RequestDeviceoptions {
RequestDeviceoptions {
filters: filters,
optional_services: services,
}
}
pub fn get_filters(&self) -> &BluetoothScanfilterSequence {
&self.filters
}
pub fn get_services_set(&self) -> HashSet<String> {
&self.filters.get_services_set() | &self.optional_services.get_services_set()
}
pub fn is_accepting_all_devices(&self) -> bool {
self.filters.is_empty()
}
}

View file

@ -1,82 +0,0 @@
/* 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 bluetooth_scanfilter::RequestDeviceoptions;
use ipc_channel::ipc::IpcSender;
#[derive(Deserialize, Serialize)]
pub enum BluetoothError {
Type(String),
Network,
NotFound,
NotSupported,
Security,
}
#[derive(Deserialize, Serialize)]
pub struct BluetoothDeviceMsg {
// Bluetooth Device properties
pub id: String,
pub name: Option<String>,
// Advertisiong Data properties
pub appearance: Option<u16>,
pub tx_power: Option<i8>,
pub rssi: Option<i8>,
}
#[derive(Deserialize, Serialize)]
pub struct BluetoothServiceMsg {
pub uuid: String,
pub is_primary: bool,
pub instance_id: String,
}
#[derive(Deserialize, Serialize)]
pub struct BluetoothCharacteristicMsg {
// Characteristic
pub uuid: String,
pub instance_id: String,
// Characteristic properties
pub broadcast: bool,
pub read: bool,
pub write_without_response: bool,
pub write: bool,
pub notify: bool,
pub indicate: bool,
pub authenticated_signed_writes: bool,
pub reliable_write: bool,
pub writable_auxiliaries: bool,
}
#[derive(Deserialize, Serialize)]
pub struct BluetoothDescriptorMsg {
pub uuid: String,
pub instance_id: String,
}
pub type BluetoothServicesMsg = Vec<BluetoothServiceMsg>;
pub type BluetoothCharacteristicsMsg = Vec<BluetoothCharacteristicMsg>;
pub type BluetoothDescriptorsMsg = Vec<BluetoothDescriptorMsg>;
pub type BluetoothResult<T> = Result<T, BluetoothError>;
#[derive(Deserialize, Serialize)]
pub enum BluetoothMethodMsg {
RequestDevice(RequestDeviceoptions, IpcSender<BluetoothResult<BluetoothDeviceMsg>>),
GATTServerConnect(String, IpcSender<BluetoothResult<bool>>),
GATTServerDisconnect(String, IpcSender<BluetoothResult<bool>>),
GetPrimaryService(String, String, IpcSender<BluetoothResult<BluetoothServiceMsg>>),
GetPrimaryServices(String, Option<String>, IpcSender<BluetoothResult<BluetoothServicesMsg>>),
GetIncludedService(String, String, IpcSender<BluetoothResult<BluetoothServiceMsg>>),
GetIncludedServices(String, Option<String>, IpcSender<BluetoothResult<BluetoothServicesMsg>>),
GetCharacteristic(String, String, IpcSender<BluetoothResult<BluetoothCharacteristicMsg>>),
GetCharacteristics(String, Option<String>, IpcSender<BluetoothResult<BluetoothCharacteristicsMsg>>),
GetDescriptor(String, String, IpcSender<BluetoothResult<BluetoothDescriptorMsg>>),
GetDescriptors(String, Option<String>, IpcSender<BluetoothResult<BluetoothDescriptorsMsg>>),
ReadValue(String, IpcSender<BluetoothResult<Vec<u8>>>),
WriteValue(String, Vec<u8>, IpcSender<BluetoothResult<bool>>),
Exit,
}

View file

@ -51,8 +51,6 @@ use url::Url;
use websocket::header;
pub mod blob_url_store;
pub mod bluetooth_scanfilter;
pub mod bluetooth_thread;
pub mod filemanager_thread;
pub mod hosts;
pub mod image_cache_thread;