|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+use core::convert::TryInto;
|
|
|
2
|
+
|
|
|
3
|
+use embedded_hal::blocking::delay::DelayMs;
|
|
|
4
|
+use embedded_hal::digital::v2::OutputPin;
|
|
|
5
|
+use embedded_nrf24l01::{Configuration, CrcMode, DataRate, RxMode, StandbyMode, NRF24L01};
|
|
|
6
|
+use mkl25z4_hal::time::CopyableMonoTimer;
|
|
|
7
|
+use mkl25z4_hal::NoError;
|
|
|
8
|
+use protocol::{Packet, Report, Value};
|
|
|
9
|
+
|
|
|
10
|
+use super::pins::{RadioCe, RadioCs, RadioIrq, RadioPins, RadioPwr, RadioSpi};
|
|
|
11
|
+use super::sensors::BME280Data;
|
|
|
12
|
+
|
|
|
13
|
+const DEVICE_ID: u8 = 0x20;
|
|
|
14
|
+const KEY: [u8; 16] = include!("../../../common/display_key.txt");
|
|
|
15
|
+
|
|
|
16
|
+pub struct Radio {
|
|
|
17
|
+ nrf24: Option<NRF24L01<NoError, RadioCe, RadioCs, RadioSpi>>,
|
|
|
18
|
+ pwr: RadioPwr,
|
|
|
19
|
+ irq: RadioIrq,
|
|
|
20
|
+ salt: Option<u64>,
|
|
|
21
|
+ time: CopyableMonoTimer,
|
|
|
22
|
+}
|
|
|
23
|
+
|
|
|
24
|
+impl Radio {
|
|
|
25
|
+ pub fn init(mut pins: RadioPins, time: CopyableMonoTimer) -> Radio {
|
|
|
26
|
+ pins.pwr.set_low().ok();
|
|
|
27
|
+ let nrf24 = NRF24L01::new(pins.ce, pins.cs, pins.spi).unwrap();
|
|
|
28
|
+ let nrf24 = nrf24.power_down().unwrap();
|
|
|
29
|
+ pins.pwr.set_high().ok();
|
|
|
30
|
+ Radio {
|
|
|
31
|
+ nrf24: Some(nrf24),
|
|
|
32
|
+ pwr: pins.pwr,
|
|
|
33
|
+ irq: pins.irq,
|
|
|
34
|
+ salt: None,
|
|
|
35
|
+ time,
|
|
|
36
|
+ }
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ pub fn enable<'a>(&'a mut self) -> EnabledRadio<'a> {
|
|
|
40
|
+ // Enable supply voltage.
|
|
|
41
|
+ self.pwr.set_low().ok();
|
|
|
42
|
+ self.time.delay_ms(10u8);
|
|
|
43
|
+
|
|
|
44
|
+ // Configure the device.
|
|
|
45
|
+ let nrf24 = self.nrf24.take().unwrap();
|
|
|
46
|
+ let mut nrf24 = StandbyMode::power_up(nrf24).unwrap();
|
|
|
47
|
+ self.time.delay_ms(2u8);
|
|
|
48
|
+ nrf24.set_frequency(0x32).unwrap();
|
|
|
49
|
+ nrf24.set_rf(DataRate::R2Mbps, 3).unwrap();
|
|
|
50
|
+ nrf24.set_crc(Some(CrcMode::OneByte)).unwrap();
|
|
|
51
|
+ nrf24.set_auto_retransmit(250, 1).unwrap();
|
|
|
52
|
+ nrf24.set_auto_ack(&[true; 6]).unwrap();
|
|
|
53
|
+ nrf24
|
|
|
54
|
+ .set_pipes_rx_enable(&[true, true, false, false, false, false])
|
|
|
55
|
+ .unwrap();
|
|
|
56
|
+ nrf24.set_tx_addr(&[0xB3, 0xB3, 0xB3, 0xB3, 0x00]).unwrap();
|
|
|
57
|
+ nrf24
|
|
|
58
|
+ .set_rx_addr(0, &[0xB3, 0xB3, 0xB3, 0xB3, 0x00])
|
|
|
59
|
+ .unwrap();
|
|
|
60
|
+ nrf24
|
|
|
61
|
+ .set_rx_addr(1, &[0xB3, 0xB3, 0xB3, 0xB3, 0x20])
|
|
|
62
|
+ .unwrap();
|
|
|
63
|
+ nrf24.flush_rx().unwrap();
|
|
|
64
|
+ nrf24.flush_tx().unwrap();
|
|
|
65
|
+ nrf24.set_pipes_rx_lengths(&[Some(32); 6]).unwrap();
|
|
|
66
|
+
|
|
|
67
|
+ EnabledRadio {
|
|
|
68
|
+ radio: self,
|
|
|
69
|
+ nrf24: Some(nrf24),
|
|
|
70
|
+ }
|
|
|
71
|
+ }
|
|
|
72
|
+}
|
|
|
73
|
+
|
|
|
74
|
+pub struct EnabledRadio<'a> {
|
|
|
75
|
+ radio: &'a mut Radio,
|
|
|
76
|
+ nrf24: Option<StandbyMode<NRF24L01<NoError, RadioCe, RadioCs, RadioSpi>>>,
|
|
|
77
|
+}
|
|
|
78
|
+
|
|
|
79
|
+impl<'a> EnabledRadio<'a> {
|
|
|
80
|
+ // TODO: Use generic "SensorValues" instead of BME280Data, which includes ambient light
|
|
|
81
|
+ // readings
|
|
|
82
|
+ pub fn send_sensor_values(&mut self, values: BME280Data) -> bool {
|
|
|
83
|
+ if self.radio.salt.is_none() {
|
|
|
84
|
+ if !self.fetch_salt() {
|
|
|
85
|
+ return false;
|
|
|
86
|
+ }
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ let mut tx = self.nrf24.take().unwrap().tx().unwrap();
|
|
|
90
|
+ while !tx.can_send().unwrap() {}
|
|
|
91
|
+ let mut payload = [0u8; 32];
|
|
|
92
|
+ let mut report = Report {
|
|
|
93
|
+ count: 3,
|
|
|
94
|
+ values: [Value::Invalid; 8],
|
|
|
95
|
+ };
|
|
|
96
|
+ report.values[0] = Value::Temperature(values.temperature as i16);
|
|
|
97
|
+ report.values[1] = Value::Pressure(values.pressure);
|
|
|
98
|
+ report.values[2] = Value::Humidity(values.humidity as u16);
|
|
|
99
|
+ let packet = Packet::Report(report);
|
|
|
100
|
+ packet.encode_and_encrypt(&KEY, self.radio.salt.unwrap(), &mut payload);
|
|
|
101
|
+ tx.send(&payload).unwrap();
|
|
|
102
|
+ // TODO: Check whether the packet arrived.
|
|
|
103
|
+
|
|
|
104
|
+ self.nrf24 = Some(tx.standby().unwrap());
|
|
|
105
|
+ true
|
|
|
106
|
+ }
|
|
|
107
|
+
|
|
|
108
|
+ fn fetch_salt(&mut self) -> bool {
|
|
|
109
|
+ // Request a device ID.
|
|
|
110
|
+ let mut tx = self.nrf24.take().unwrap().tx().unwrap();
|
|
|
111
|
+ while !tx.can_send().unwrap() {}
|
|
|
112
|
+ let mut payload = [0u8; 32];
|
|
|
113
|
+ let packet = Packet::GetSalt;
|
|
|
114
|
+ packet.encode_and_encrypt(&KEY, DEVICE_ID as u64, &mut payload);
|
|
|
115
|
+ tx.send(&payload).unwrap();
|
|
|
116
|
+ // TODO: Check whether the packet arrived.
|
|
|
117
|
+
|
|
|
118
|
+ // Wait for the initial salt.
|
|
|
119
|
+ let mut salt_received = false;
|
|
|
120
|
+ let mut rx = tx.standby().unwrap().rx().unwrap();
|
|
|
121
|
+ loop {
|
|
|
122
|
+ let packet = match self.receive_packet(&mut rx) {
|
|
|
123
|
+ None => break,
|
|
|
124
|
+ Some(p) => p,
|
|
|
125
|
+ };
|
|
|
126
|
+
|
|
|
127
|
+ match packet {
|
|
|
128
|
+ Packet::Salt(salt) => {
|
|
|
129
|
+ self.radio.salt = Some((salt & 0x7fffffffffffff00) | DEVICE_ID as u64);
|
|
|
130
|
+ salt_received = true;
|
|
|
131
|
+ break;
|
|
|
132
|
+ }
|
|
|
133
|
+ _ => continue,
|
|
|
134
|
+ }
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ self.nrf24 = Some(rx.standby());
|
|
|
138
|
+ salt_received
|
|
|
139
|
+ }
|
|
|
140
|
+
|
|
|
141
|
+ fn receive_packet(
|
|
|
142
|
+ &mut self,
|
|
|
143
|
+ rx: &mut RxMode<NRF24L01<NoError, RadioCe, RadioCs, RadioSpi>>,
|
|
|
144
|
+ ) -> Option<Packet> {
|
|
|
145
|
+ // TODO: Timeout.
|
|
|
146
|
+ loop {
|
|
|
147
|
+ if let Some(_pipe) = rx.can_read().unwrap() {
|
|
|
148
|
+ // Receive a packet.
|
|
|
149
|
+ // TODO: Check pipe?
|
|
|
150
|
+ let payload = rx.read().unwrap();
|
|
|
151
|
+ if payload.len() != 32 {
|
|
|
152
|
+ continue;
|
|
|
153
|
+ }
|
|
|
154
|
+ let mut payload: [u8; 32] = payload[0..32].try_into().unwrap();
|
|
|
155
|
+
|
|
|
156
|
+ let packet = match Packet::decrypt_and_decode(&KEY, &mut payload) {
|
|
|
157
|
+ Ok(p) => p,
|
|
|
158
|
+ Err(_) => continue,
|
|
|
159
|
+ };
|
|
|
160
|
+
|
|
|
161
|
+ return Some(packet);
|
|
|
162
|
+ }
|
|
|
163
|
+ }
|
|
|
164
|
+ }
|
|
|
165
|
+}
|
|
|
166
|
+
|
|
|
167
|
+impl<'a> Drop for EnabledRadio<'a> {
|
|
|
168
|
+ fn drop(&mut self) {
|
|
|
169
|
+ let nrf24 = self.nrf24.take().unwrap();
|
|
|
170
|
+ // Disable the chip and remove the supply voltage.
|
|
|
171
|
+ let nrf24 = nrf24.power_down().unwrap();
|
|
|
172
|
+ self.radio.pwr.set_high().ok();
|
|
|
173
|
+ self.radio.nrf24 = Some(nrf24);
|
|
|
174
|
+ }
|
|
|
175
|
+}
|