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
|
use anyhow::{anyhow, Context};
use ham_cats::{
buffer::Buffer,
whisker::{Arbitrary, Identification, Gps},
};
const MAX_PACKET_LEN : usize = 8191;
fn build_example_packet(comment: &str) -> anyhow::Result<Vec<u8>> {
let callsign = "EX4MPLE";
let ssid = 0;
let icon = 0;
let mut buf = [0; MAX_PACKET_LEN];
let mut pkt = ham_cats::packet::Packet::new(&mut buf);
pkt.add_identification(
Identification::new(&callsign, ssid, icon)
.context("Invalid identification")?,
)
.map_err(|e| anyhow!("Could not add identification to packet: {e}"))?;
pkt.add_comment(comment)
.map_err(|e| anyhow!("Could not add comment to packet: {e}"))?;
let latitude = 46.5;
let longitude = -6.2;
let altitude = 200u8.into();
let max_error = 1;
let heading = 120.0;
let speed = 1u8.into();
pkt.add_gps(Gps::new(
latitude,
longitude,
altitude,
max_error,
heading,
speed)
)
.map_err(|e| anyhow!("Could not add GPS to packet: {e}"))?;
pkt.add_arbitrary(Arbitrary::new(&[0xA5; 8]).unwrap())
.map_err(|e| anyhow!("Could not add arbitrary to packet: {e}"))?;
let mut buf2 = [0; MAX_PACKET_LEN];
let mut data = Buffer::new_empty(&mut buf2);
pkt.fully_encode(&mut data)
.map_err(|e| anyhow!("Could not encode packet: {e}"))?;
Ok(data.to_vec())
}
fn main() -> std::io::Result<()> {
std::thread::spawn(receive_loop);
eprintln!("Receiving messages. Write a comment and press ENTER to send. Ctrl-C to stop");
let mut stdin_lines = std::io::stdin().lines();
let sock = std::net::UdpSocket::bind("127.0.0.1:9075").unwrap();
while let Some(Ok(line)) = stdin_lines.next() {
eprintln!("Sending with comment = {}", line);
let packet = build_example_packet(&line).unwrap();
sock.send_to(&packet, "127.0.0.1:9073").unwrap();
}
Ok(())
}
fn receive_loop() {
let sock = std::net::UdpSocket::bind("127.0.0.1:9074").unwrap();
let mut data = [0; MAX_PACKET_LEN];
while let Ok((len, _addr)) = sock.recv_from(&mut data) {
eprintln!("Packet of length {}", len);
let mut buf = [0; MAX_PACKET_LEN];
match ham_cats::packet::Packet::fully_decode(&data[2..len], &mut buf) {
Ok(packet) => {
if let Some(ident) = packet.identification() {
eprintln!(" Ident {}-{}", ident.callsign, ident.ssid);
}
if let Some(gps) = packet.gps() {
eprintln!(" GPS {} {}", gps.latitude(), gps.longitude());
}
let mut comment = [0; 1024];
if let Ok(c) = packet.comment(&mut comment) {
eprintln!(" Comment {}", c);
}
eprintln!(" With {} Arbitrary whiskers", packet.arbitrary_iter().count());
},
Err(e) => {
eprintln!(" Cannot decode {:?}", e);
}
}
}
}
|