-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
283 lines (251 loc) · 9.98 KB
/
lib.rs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// © 2018 Sebastian Reichel
// SPDX-License-Identifier: ISC
#![crate_type = "lib"]
#![crate_name = "cff3000"]
//! The `cff3000` crate provides a highlevel API for a GPIO connected
//! CFF3000 remote control.
//!
//! # Example
//! ```
//! extern crate cff3000;
//! use cff3000::CFF3000;
//! use std::io::{Error,ErrorKind};
//!
//! fn execute(cmd: &str) -> std::io::Result<()> {
//! let cff3000 = try!(CFF3000::new("/dev/gpiochip2", [2,3,4,5]));
//! let duration: u8;
//!
//! match cmd {
//! "lock" => {try!(cff3000.lock()); duration = 10;},
//! "unlock" => {try!(cff3000.unlock()); duration = 10;},
//! "check" => {try!(cff3000.check()); duration = 8;},
//! _ => return Err(Error::new(ErrorKind::Other, "unsupported command")),
//! }
//!
//! try!(cff3000.show_leds(duration));
//! Ok(())
//! }
//!
//! fn main() {
//! let args: Vec<String> = std::env::args().collect();
//!
//! if args.len() < 2 {
//! println!("missing parameter: lock, unlock, check");
//! std::process::exit(1)
//! }
//!
//! match execute(args[1].as_str()) {
//! Err(err) => println!("{}", err.to_string()),
//! Ok(()) => {},
//! }
//! }
//! ```
extern crate gpiochip as gpio;
use std::io::Write;
pub struct CFF3000 {
red: gpio::GpioEventHandle,
green: gpio::GpioEventHandle,
unlock: gpio::GpioHandle,
lock: gpio::GpioHandle,
}
#[derive(Debug, Copy, Clone)]
struct Event {
/// led (0 = red, 1 = green)
mask: u8,
/// timestamp (in ms)
timestamp: u64,
/// enabled = HIGH, otherwise LOW
state: u8,
}
#[derive(Debug)]
#[derive(PartialEq)]
pub enum CFF3000State {
/// The door is locked (green LED on)
Locked,
/// The door is unlocked (red LED on)
Unlocked,
/// The door state has been changed manually (both LEDs blink synchronously)
Manual,
/// The CFA3000 is out of range (both LEDs blink alternating)
OutOfRange,
}
impl CFF3000 {
/// Create new CFF3000 device.
///
/// This functions requires permissions to open the gpiochip
/// device. `chipdev` should be something like "/dev/gpiochip0"
/// and `gpios` should be an array containing the line offsets
/// for LED red, LED green, button unlock and button lock (in
/// this order)
pub fn new(chipdev: &str, gpios: [u32; 4]) -> std::io::Result<CFF3000> {
let chip = try!(gpio::GpioChip::new(chipdev));
let led_red = try!(chip.request_event("led-red", gpios[0], gpio::RequestFlags::INPUT, gpio::EventRequestFlags::BOTH_EDGES));
let led_green = try!(chip.request_event("led-green", gpios[1], gpio::RequestFlags::INPUT, gpio::EventRequestFlags::BOTH_EDGES));
let button_unlock = try!(chip.request("button-unlock", gpio::RequestFlags::OUTPUT, gpios[2], 0));
let button_lock = try!(chip.request("button-lock", gpio::RequestFlags::OUTPUT, gpios[3], 0));
Ok(CFF3000 {red: led_red, green: led_green, unlock: button_unlock, lock: button_lock})
}
fn print_leds(red: bool, green: bool) -> std::io::Result<()> {
if red && green {
print!("\r\x1b[31m ● \x1b[32m● \x1b[0m ")
} else if red {
print!("\r\x1b[31m ● \x1b[32m◯ \x1b[0m ")
} else if green {
print!("\r\x1b[31m ◯ \x1b[32m● \x1b[0m ")
} else {
print!("\r\x1b[31m ◯ \x1b[32m◯ \x1b[0m ")
}
try!(std::io::stdout().flush());
Ok(())
}
/// Print LED state to stdout for `duration` seconds.
///
/// This will print the current LED state of the CFF3000 using colored
/// UTF-8 symbols. The output will be refreshed for `duration` seconds
/// using the rollback character.
pub fn show_leds(&self, duration: u8) -> std::io::Result<()> {
let mut r = false;
let mut g = false;
let start = std::time::Instant::now();
try!(CFF3000::print_leds(r, g));
while start.elapsed().as_secs() < duration as u64 {
let events = try!(gpio::wait_for_event(&[&self.red, &self.green], 1000));
if events == 0 {
continue;
}
if events & 0x1 != 0 {
r = try!(self.red.read()).id == gpio::EventId::RISING_EDGE;
}
if events & 0x2 != 0 {
g = try!(self.green.read()).id == gpio::EventId::RISING_EDGE;
}
try!(CFF3000::print_leds(r, g));
}
println!("");
Ok(())
}
/// Press and release lock button.
pub fn lock(&self) -> std::io::Result<()> {
try!(self.lock.set(1));
std::thread::sleep(std::time::Duration::from_millis(500));
try!(self.lock.set(0));
Ok(())
}
/// Press and release unlock button.
pub fn unlock(&self) -> std::io::Result<()> {
try!(self.unlock.set(1));
std::thread::sleep(std::time::Duration::from_millis(500));
try!(self.unlock.set(0));
Ok(())
}
/// Press and release both buttons to query state.
pub fn check(&self) -> std::io::Result<()> {
try!(self.lock.set(1));
try!(self.unlock.set(1));
std::thread::sleep(std::time::Duration::from_millis(500));
try!(self.unlock.set(0));
try!(self.lock.set(0));
Ok(())
}
/// Flush LED events
pub fn flush_led_events(&self) -> std::io::Result<()> {
try!(self.green.flush());
try!(self.red.flush());
Ok(())
}
/// Query CFA3000 state and interpret the following
/// LED pattern. This function blocks for 8 seconds
/// to capture the LED blink pattern.
pub fn state(&self) -> std::io::Result<CFF3000State> {
try!(self.flush_led_events());
try!(self.check());
let mut eventlog: std::vec::Vec<Event> = std::vec::Vec::new();
let start = std::time::Instant::now();
print!("waiting for led events... ");
try!(std::io::stdout().flush());
while start.elapsed().as_secs() < 8 {
let events = try!(gpio::wait_for_event(&[&self.red, &self.green], 1000));
if events == 0 {
continue;
}
if events & 0x1 != 0 {
let event = try!(self.red.read());
let state: u8 = if event.id == gpio::EventId::RISING_EDGE {1} else {0};
eventlog.push(Event {mask: 0b01, timestamp: event.timestamp/1000/1000, state: state << 0});
}
if events & 0x2 != 0 {
let event = try!(self.green.read());
let state: u8 = if event.id == gpio::EventId::RISING_EDGE {1} else {0};
eventlog.push(Event {mask: 0b10, timestamp: event.timestamp/1000/1000, state: state << 1});
}
}
/* combine events within 50ms */
let mut simple_eventlog: std::vec::Vec<Event> = std::vec::Vec::new();
simple_eventlog.push(eventlog[0]);
for i in 1..eventlog.len() {
if eventlog[i-1].timestamp > eventlog[i].timestamp - 50 {
let pos = simple_eventlog.len()-1;
simple_eventlog[pos].mask |= eventlog[i].mask;
simple_eventlog[pos].state |= eventlog[i].state & eventlog[i].mask;
simple_eventlog[pos].state &= eventlog[i].state | !eventlog[i].mask;
} else {
simple_eventlog.push(eventlog[i]);
}
}
/* fill up event data for unchanged leds with previous information and use relative timestamps */
let mut state = 0u8;
let offset = simple_eventlog[0].timestamp;
for e in &mut simple_eventlog {
state &= !e.mask;
state |= e.state & e.mask;
if e.mask != 0b11 {
e.state &= e.mask;
e.state |= state;
}
e.mask = 0b11;
e.timestamp -= offset;
}
/* check for obvious problems */
if simple_eventlog.len() <= 2 {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "did not receive enough LED change events"));
}
if simple_eventlog.first().unwrap().state != 0b11 {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "first LED changes is invalid"));
}
if simple_eventlog.last().unwrap().state != 0b00 {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "last LED change is invalid"));
}
let result: CFF3000State;
if simple_eventlog.len() == 3 {
if simple_eventlog[1].state == 0b10 {
result = CFF3000State::Locked;
} else if simple_eventlog[1].state == 0b01 {
result = CFF3000State::Unlocked;
} else {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid LED state"));
}
} else {
result = match simple_eventlog[1].state {
0b00 => CFF3000State::Manual,
0b01 => CFF3000State::OutOfRange,
0b10 => CFF3000State::OutOfRange,
_ => {return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid LED state"))},
};
for i in 2..simple_eventlog.len()-1 {
if result == CFF3000State::Manual {
if simple_eventlog[i-1].state & 0b11 != !simple_eventlog[i].state & 0b11 {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid manual error LED substate"));
}
} else {
if simple_eventlog[i].state == 0b00 || simple_eventlog[i].state == 0b11 {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid out of range error LED substate"));
}
if simple_eventlog[i-1].state & 0b11 == !simple_eventlog[i-1].state & 0b11 {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid out of range error LED substate"));
}
}
}
}
Ok(result)
}
}