]>
Commit | Line | Data |
---|---|---|
4066e7d1 SW |
1 | // tattlekey: A one-key UDP keyboard |
2 | // Copyright (C) 2023 Scott Worley <scottworley@scottworley.com> | |
3 | // | |
4 | // This program is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // This program is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. | |
16 | ||
8325ffaa | 17 | use serde::{Deserialize, Serialize}; |
5b8bd049 | 18 | use std::collections::HashMap; |
dd98493c | 19 | use std::net::UdpSocket; |
d9c1d028 | 20 | use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
dd98493c SW |
21 | |
22 | const MESSAGE_SIZE: usize = 12; | |
4f2bb592 | 23 | const LOGFILENAME: &str = "log.csv"; |
dd98493c | 24 | |
6c8d15f4 | 25 | #[derive(Eq, Debug, Hash, PartialEq)] |
143d53ed SW |
26 | struct MessageKey { |
27 | epoch: u32, | |
28 | device: u16, | |
29 | seq: u16, | |
30 | } | |
31 | ||
8325ffaa | 32 | #[derive(Debug, Deserialize, Serialize)] |
143d53ed | 33 | struct Message { |
e8a97c55 SW |
34 | epoch: u32, |
35 | device: u16, | |
36 | seq: u16, | |
d9c1d028 | 37 | t: u64, |
143d53ed | 38 | } |
e8a97c55 SW |
39 | impl Message { |
40 | fn key(&self) -> MessageKey { | |
41 | MessageKey { | |
42 | epoch: self.epoch, | |
43 | device: self.device, | |
44 | seq: self.seq, | |
45 | } | |
6c8d15f4 SW |
46 | } |
47 | } | |
143d53ed SW |
48 | |
49 | impl From<&[u8; MESSAGE_SIZE]> for Message { | |
50 | fn from(value: &[u8; MESSAGE_SIZE]) -> Self { | |
f9625e91 | 51 | let ago = u32::from_be_bytes(value[8..=11].try_into().expect("I can't count")); |
d9c1d028 | 52 | let press_time = SystemTime::now() - Duration::new(ago.into(), 0); |
143d53ed | 53 | Self { |
e8a97c55 SW |
54 | epoch: u32::from_be_bytes(value[0..=3].try_into().expect("I can't count")), |
55 | device: u16::from_be_bytes(value[4..=5].try_into().expect("I can't count")), | |
56 | seq: u16::from_be_bytes(value[6..=7].try_into().expect("I can't count")), | |
d9c1d028 SW |
57 | t: press_time |
58 | .duration_since(UNIX_EPOCH) | |
59 | .expect("Bad time?") | |
60 | .as_secs(), | |
143d53ed SW |
61 | } |
62 | } | |
63 | } | |
64 | impl TryFrom<&[u8]> for Message { | |
65 | type Error = std::array::TryFromSliceError; | |
66 | fn try_from(value: &[u8]) -> Result<Self, Self::Error> { | |
67 | match <[u8; MESSAGE_SIZE]>::try_from(value) { | |
68 | Ok(correct_size) => Ok(Message::from(&correct_size)), | |
69 | Err(e) => Err(e), | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
5b8bd049 SW |
74 | #[derive(Debug)] |
75 | struct Range { | |
d9c1d028 SW |
76 | start: u64, |
77 | end: u64, | |
5b8bd049 SW |
78 | } |
79 | impl Range { | |
d9c1d028 | 80 | fn new(t: &u64) -> Self { |
5b8bd049 SW |
81 | Self { start: *t, end: *t } |
82 | } | |
d9c1d028 | 83 | fn contains(&self, t: &u64) -> bool { |
5b8bd049 SW |
84 | t > &self.start && t < &self.end |
85 | } | |
d9c1d028 | 86 | fn extend(&mut self, t: &u64) { |
5b8bd049 SW |
87 | if t < &self.start { |
88 | self.start = *t; | |
89 | } | |
90 | if t > &self.end { | |
91 | self.end = *t; | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
c54403e6 | 96 | fn merge_message(presses: &mut HashMap<MessageKey, Range>, message: Message) { |
e8a97c55 SW |
97 | let key = message.key(); |
98 | if let Some(r) = presses.get_mut(&key) { | |
c54403e6 SW |
99 | if !r.contains(&message.t) { |
100 | r.extend(&message.t); | |
101 | } | |
102 | } else { | |
e8a97c55 | 103 | presses.insert(key, Range::new(&message.t)); |
c54403e6 SW |
104 | } |
105 | } | |
106 | ||
8325ffaa SW |
107 | fn replay_log() -> HashMap<MessageKey, Range> { |
108 | let mut presses = HashMap::new(); | |
109 | if std::path::Path::new(LOGFILENAME).exists() { | |
110 | let mut log = csv::Reader::from_path(LOGFILENAME).expect("Couldn't open log for replay"); | |
111 | for message in log.deserialize() { | |
112 | merge_message( | |
113 | &mut presses, | |
114 | message.expect("Error reading log during replay"), | |
115 | ); | |
116 | } | |
117 | } | |
118 | presses | |
119 | } | |
120 | ||
0d4f2aa6 | 121 | fn open_log_for_writing() -> csv::Writer<std::fs::File> { |
7a3bc82f | 122 | let log_file_exists = std::path::Path::new(LOGFILENAME).exists(); |
3ad88929 | 123 | let logfile = std::fs::OpenOptions::new() |
7a3bc82f | 124 | .create_new(!log_file_exists) |
3ad88929 | 125 | .append(true) |
4f2bb592 | 126 | .open(LOGFILENAME) |
3ad88929 | 127 | .expect("Coudln't open log file"); |
0d4f2aa6 | 128 | csv::WriterBuilder::new() |
7a3bc82f | 129 | .has_headers(!log_file_exists) |
0d4f2aa6 SW |
130 | .from_writer(logfile) |
131 | } | |
132 | ||
133 | fn main() { | |
dd98493c | 134 | let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address"); |
8325ffaa | 135 | let mut presses = replay_log(); |
5f576831 | 136 | let mut log = open_log_for_writing(); |
dd98493c SW |
137 | loop { |
138 | let mut buf = [0; MESSAGE_SIZE]; | |
139 | match socket.recv_from(&mut buf) { | |
140 | Err(e) => eprintln!("Didn't receive data: {e}"), | |
141 | Ok((number_of_bytes, src_addr)) => { | |
143d53ed | 142 | let filled_buf = &buf[..number_of_bytes]; |
dd98493c SW |
143 | if number_of_bytes != MESSAGE_SIZE { |
144 | eprintln!("Ignoring short message ({number_of_bytes}) from {src_addr}"); | |
145 | continue; | |
146 | } | |
143d53ed | 147 | let message = Message::try_from(filled_buf).expect("I can't count"); |
6c8d15f4 | 148 | log.serialize(&message).expect("Couldn't write log"); |
79d78cde | 149 | log.flush().expect("Couldn't flush log"); |
c54403e6 | 150 | merge_message(&mut presses, message); |
dd98493c SW |
151 | } |
152 | } | |
153 | } | |
891f78b7 | 154 | } |