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
mod history;
pub mod packet;
mod route;
use crate::id::*;
use crate::link::Link;
use crate::protocol;
use core::sync::atomic::{AtomicU8, Ordering};
use heapless::Vec;
use log::{info, warn};
use packet::{Field, Packet};
use route::Routes;
use spin::RwLock;
lazy_static! {
static ref SN: AtomicU8 = AtomicU8::new({
let mut data: [u8; 1] = [0; 1];
getrandom::getrandom(&mut data).unwrap();
data[0]
});
static ref CFG: RwLock<Config> = RwLock::new(Config {
id: Id::default(),
relay: false,
});
static ref ROUTES: RwLock<Routes> = RwLock::new(Routes::new());
}
struct Config {
id: Id,
relay: bool,
}
pub fn set_id(id: Id) {
CFG.write().id = id;
}
pub fn get_id() -> Id {
return CFG.read().id.clone();
}
pub fn set_relay(relay: bool) {
CFG.write().relay = relay;
}
fn send_common(
packet: &mut Packet,
dst: Option<&Id>,
dst_link: Option<Link>,
origin: Option<Link>,
) {
let id: &Id = if let Some(id) = dst {
let buf: Vec<_, _> = id.into();
packet.set_var(Field::Dst, &buf);
id
} else {
&packet.dst
};
if let None = dst_link {
history::add(&packet);
let buf = packet.to_buf();
if let Ok(_) = ROUTES.write().sendto(id, &buf, origin) {
return;
}
if let Err(_) = ROUTES.write().sendto(&ID_PARENT, &buf, origin) {
warn!("unmp can't find router.");
}
} else {
let link = dst_link.unwrap();
if let Some(origin) = origin {
if origin == link {
return;
}
}
history::add(&packet);
let err = link.send(&packet.to_buf());
if let Err(_) = err {
ROUTES.write().remove(id, link);
}
}
}
pub fn send(protocol: u8, data: &[u8], id: Option<&Id>, link: Option<Link>) {
let mut packet = Packet::new();
packet.sn = SN.fetch_add(1, Ordering::Relaxed);
packet.set_var(Field::Protocol, &[protocol]);
packet.set_var(Field::Data, data);
let id_buf: Vec<_, _> = (&CFG.read().id).into();
packet.set_var(Field::Src, &id_buf);
packet.set_var(Field::Ttl, &[8]);
send_common(&mut packet, id, link, None);
}
pub fn broadcast(protocol: u8, data: &[u8]) {
let mut packet = Packet::new();
packet.sn = SN.fetch_add(1, Ordering::Relaxed);
packet.set_var(Field::Protocol, &[protocol]);
packet.set_var(Field::Data, data);
let id_buf: Vec<_, _> = (&CFG.read().id).into();
packet.set_var(Field::Src, &id_buf);
let id_buf: Vec<_, _> = (&ID_CHILD).into();
packet.set_var(Field::Dst, &id_buf);
packet.set_var(Field::Ttl, &[0]);
let buf = packet.to_buf();
Link::broadcast(&buf);
}
pub fn when_recv(link: Link, buf: &[u8]) {
info!("{} recv:{:02X?}.", link, buf);
let packet = Packet::try_from_buf(buf);
let mut packet = if let Ok(packet) = packet {
packet
} else {
return;
};
if history::has(&packet) {
return;
}
if packet.src != ID_PARENT && packet.src != ID_CHILD {
connect(&packet.src, link);
}
if packet.dst == CFG.read().id || packet.dst == ID_PARENT || packet.dst == ID_CHILD {
protocol::distribute(packet.protocol, &packet.src, &packet.data);
} else if CFG.read().relay {
if packet.ttl > 0 {
packet.set_var(Field::Ttl, &[packet.ttl - 1]);
send_common(&mut packet, None, None, Some(link));
} else {
info!("unmp TTL is 0.");
}
}
}
pub fn connect(id: &Id, link: Link) {
ROUTES.write().add(id, link);
}