Add random Device ID generation

This commit is contained in:
fokinv 2016-05-31 16:18:28 +02:00 committed by Attila Dusnoki
parent b11648903b
commit 69dc199528
5 changed files with 30 additions and 3 deletions

View file

@ -28,6 +28,7 @@ openssl = "0.7.6"
openssl-verify = "0.1" openssl-verify = "0.1"
plugins = {path = "../plugins"} plugins = {path = "../plugins"}
profile_traits = {path = "../profile_traits"} profile_traits = {path = "../profile_traits"}
rand = "0.3"
rustc-serialize = "0.3" rustc-serialize = "0.3"
threadpool = "1.0" threadpool = "1.0"
time = "0.1.17" time = "0.1.17"

View file

@ -14,6 +14,7 @@ use net_traits::bluetooth_thread::{BluetoothCharacteristicMsg, BluetoothCharacte
use net_traits::bluetooth_thread::{BluetoothDescriptorMsg, BluetoothDescriptorsMsg}; use net_traits::bluetooth_thread::{BluetoothDescriptorMsg, BluetoothDescriptorsMsg};
use net_traits::bluetooth_thread::{BluetoothDeviceMsg, BluetoothMethodMsg}; use net_traits::bluetooth_thread::{BluetoothDeviceMsg, BluetoothMethodMsg};
use net_traits::bluetooth_thread::{BluetoothResult, BluetoothServiceMsg, BluetoothServicesMsg}; use net_traits::bluetooth_thread::{BluetoothResult, BluetoothServiceMsg, BluetoothServicesMsg};
use rand::{self, Rng};
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::collections::HashMap; use std::collections::HashMap;
use std::string::String; use std::string::String;
@ -121,6 +122,7 @@ fn matches_filters(device: &BluetoothDevice, filters: &BluetoothScanfilterSequen
pub struct BluetoothManager { pub struct BluetoothManager {
receiver: IpcReceiver<BluetoothMethodMsg>, receiver: IpcReceiver<BluetoothMethodMsg>,
adapter: Option<BluetoothAdapter>, adapter: Option<BluetoothAdapter>,
address_to_id: HashMap<String, String>,
service_to_device: HashMap<String, String>, service_to_device: HashMap<String, String>,
characteristic_to_service: HashMap<String, String>, characteristic_to_service: HashMap<String, String>,
descriptor_to_characteristic: HashMap<String, String>, descriptor_to_characteristic: HashMap<String, String>,
@ -135,6 +137,7 @@ impl BluetoothManager {
BluetoothManager { BluetoothManager {
receiver: receiver, receiver: receiver,
adapter: adapter, adapter: adapter,
address_to_id: HashMap::new(),
service_to_device: HashMap::new(), service_to_device: HashMap::new(),
characteristic_to_service: HashMap::new(), characteristic_to_service: HashMap::new(),
descriptor_to_characteristic: HashMap::new(), descriptor_to_characteristic: HashMap::new(),
@ -210,7 +213,11 @@ impl BluetoothManager {
let devices = adapter.get_devices().unwrap_or(vec!()); let devices = adapter.get_devices().unwrap_or(vec!());
for device in &devices { for device in &devices {
if let Ok(address) = device.get_address() { if let Ok(address) = device.get_address() {
self.cached_devices.insert(address, device.clone()); if !self.address_to_id.contains_key(&address) {
let generated_id = self.generate_device_id();
self.address_to_id.insert(address, generated_id.clone());
self.cached_devices.insert(generated_id, device.clone());
}
} }
} }
self.cached_devices.iter().map(|(_, d)| d.clone()).collect() self.cached_devices.iter().map(|(_, d)| d.clone()).collect()
@ -254,6 +261,18 @@ impl BluetoothManager {
None None
} }
fn generate_device_id(&mut self) -> String {
let mut device_id;
let mut rng = rand::thread_rng();
loop {
device_id = rng.gen::<u32>().to_string();
if !self.cached_devices.contains_key(&device_id) {
break;
}
}
device_id
}
// Service // Service
fn get_and_cache_gatt_services(&mut self, fn get_and_cache_gatt_services(&mut self,
@ -417,9 +436,13 @@ impl BluetoothManager {
.filter(|d| matches_filters(d, options.get_filters())) .filter(|d| matches_filters(d, options.get_filters()))
.collect(); .collect();
if let Some(address) = self.select_device(matched_devices) { if let Some(address) = self.select_device(matched_devices) {
if let Some(device) = self.get_device(&mut adapter, address.as_str()) { let device_id = match self.address_to_id.get(&address) {
Some(id) => id.clone(),
None => return drop(sender.send(Err(String::from(DEVICE_MATCH_ERROR)))),
};
if let Some(device) = self.get_device(&mut adapter, &device_id) {
let message = Ok(BluetoothDeviceMsg { let message = Ok(BluetoothDeviceMsg {
id: address, id: device_id,
name: device.get_name().ok(), name: device.get_name().ok(),
appearance: device.get_appearance().ok(), appearance: device.get_appearance().ok(),
tx_power: device.get_tx_power().ok().map(|p| p as i8), tx_power: device.get_tx_power().ok().map(|p| p as i8),

View file

@ -33,6 +33,7 @@ extern crate net_traits;
extern crate openssl; extern crate openssl;
extern crate openssl_verify; extern crate openssl_verify;
extern crate profile_traits; extern crate profile_traits;
extern crate rand;
extern crate rustc_serialize; extern crate rustc_serialize;
extern crate threadpool; extern crate threadpool;
extern crate time; extern crate time;

View file

@ -1374,6 +1374,7 @@ dependencies = [
"openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1", "plugins 0.0.1",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"threadpool 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",

1
ports/cef/Cargo.lock generated
View file

@ -1279,6 +1279,7 @@ dependencies = [
"openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "openssl-verify 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"plugins 0.0.1", "plugins 0.0.1",
"profile_traits 0.0.1", "profile_traits 0.0.1",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"threadpool 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",