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
use btleplug::api::{Characteristic, Peripheral as _};
use btleplug::platform::Peripheral;
use btleplug::api::bleuuid::uuid_from_u16;
use uuid::Uuid;
use std::fmt;
use crate::device::Device;
#[derive(Debug)]
pub struct LedDevice {
pub name: String,
alias: String,
peripheral: Option<Peripheral>,
write_char: Option<Characteristic>,
read_char: Option<Characteristic>,
write_char_uuid: Uuid,
}
pub const DEFAULT_WRITE_CHARACTERISTIC_UUID: Uuid = uuid_from_u16(0xFFD9);
impl Device for LedDevice {
fn new(
name: &str,
alias: &str,
peripheral: Option<Peripheral>,
write_char: Option<Characteristic>,
read_char: Option<Characteristic>,
write_char_uuid: Option<Uuid>,
) -> Self {
Self {
name: name.to_string(),
alias: alias.to_string(),
peripheral,
write_char: write_char.clone(),
read_char: read_char.clone(),
write_char_uuid: write_char_uuid.unwrap_or(DEFAULT_WRITE_CHARACTERISTIC_UUID),
}
}
fn alias(&self) -> &str {
&self.alias
}
fn name(&self) -> &str {
&self.name
}
fn address(&self) -> Option<String> {
if let Some(peripheral) = self.peripheral.as_ref() {
return Some(peripheral.address().to_string());
}
None
}
fn peripheral(&self) -> Option<&Peripheral> {
self.peripheral.as_ref()
}
fn write_char_uuid(&self) -> &Uuid {
&self.write_char_uuid
}
fn write_char(&self) -> Option<&Characteristic> {
self.write_char.as_ref()
}
fn read_char(&self) -> Option<&Characteristic> {
self.read_char.as_ref()
}
fn default_write_characteristic_uuid(&self) -> Uuid {
DEFAULT_WRITE_CHARACTERISTIC_UUID.clone()
}
fn set_alias(&mut self, alias: &str) {
self.alias = alias.to_string();
}
fn set_name(&mut self, name: &str) {
self.name = name.to_string();
}
fn set_peripheral(&mut self, peripheral: Peripheral) {
self.peripheral = Some(peripheral);
}
fn set_write_char_uuid(&mut self, char_uuid: Uuid) {
self.write_char_uuid = char_uuid;
}
fn set_write_char(&mut self, characteristic: &Characteristic) {
self.write_char = Some(characteristic.clone());
}
}
impl fmt::Display for LedDevice {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.name(), self.address().unwrap_or(String::from("-")))
}
}