]> git.scottworley.com Git - tattlekey/blob - server/src/main.rs
a186abd58ec1b0e8045b0e29881fbcd7ecd76229
[tattlekey] / server / src / main.rs
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
17 use serde::Serialize;
18 use std::collections::HashMap;
19 use std::net::UdpSocket;
20 use std::time::{Duration, SystemTime};
21
22 const MESSAGE_SIZE: usize = 12;
23
24 #[derive(Eq, Debug, Hash, PartialEq, Serialize)]
25 struct MessageKey {
26 epoch: u32,
27 device: u16,
28 seq: u16,
29 }
30
31 #[derive(Debug)]
32 struct Message {
33 key: MessageKey,
34 t: SystemTime,
35 }
36
37 impl From<&[u8; MESSAGE_SIZE]> for Message {
38 fn from(value: &[u8; MESSAGE_SIZE]) -> Self {
39 let ago = u32::from_be_bytes(value[8..=11].try_into().expect("I can't count"));
40 Self {
41 key: MessageKey {
42 epoch: u32::from_be_bytes(value[0..=3].try_into().expect("I can't count")),
43 device: u16::from_be_bytes(value[4..=5].try_into().expect("I can't count")),
44 seq: u16::from_be_bytes(value[6..=7].try_into().expect("I can't count")),
45 },
46 t: SystemTime::now() - Duration::new(ago.into(), 0),
47 }
48 }
49 }
50 impl TryFrom<&[u8]> for Message {
51 type Error = std::array::TryFromSliceError;
52 fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
53 match <[u8; MESSAGE_SIZE]>::try_from(value) {
54 Ok(correct_size) => Ok(Message::from(&correct_size)),
55 Err(e) => Err(e),
56 }
57 }
58 }
59
60 #[derive(Debug)]
61 struct Range {
62 start: SystemTime,
63 end: SystemTime,
64 }
65 impl Range {
66 fn new(t: &SystemTime) -> Self {
67 Self { start: *t, end: *t }
68 }
69 fn contains(&self, t: &SystemTime) -> bool {
70 t > &self.start && t < &self.end
71 }
72 fn extend(&mut self, t: &SystemTime) {
73 if t < &self.start {
74 self.start = *t;
75 }
76 if t > &self.end {
77 self.end = *t;
78 }
79 }
80 }
81
82 fn main() {
83 let logfile = std::fs::OpenOptions::new()
84 .create(true)
85 .append(true)
86 .open("log.csv")
87 .expect("Coudln't open log file");
88 let mut log = csv::Writer::from_writer(logfile);
89 let mut presses = HashMap::<MessageKey, Range>::new();
90 let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address");
91 loop {
92 let mut buf = [0; MESSAGE_SIZE];
93 match socket.recv_from(&mut buf) {
94 Err(e) => eprintln!("Didn't receive data: {e}"),
95 Ok((number_of_bytes, src_addr)) => {
96 let filled_buf = &buf[..number_of_bytes];
97 if number_of_bytes != MESSAGE_SIZE {
98 eprintln!("Ignoring short message ({number_of_bytes}) from {src_addr}");
99 continue;
100 }
101 let message = Message::try_from(filled_buf).expect("I can't count");
102 log.serialize((&message.key, message.t))
103 .expect("Couldn't write log");
104 log.flush().expect("Couldn't flush log");
105 if let Some(r) = presses.get_mut(&message.key) {
106 if !r.contains(&message.t) {
107 r.extend(&message.t);
108 }
109 } else {
110 presses.insert(message.key, Range::new(&message.t));
111 }
112 }
113 }
114 }
115 }