embedded-trainings-2020/beginner/apps/src/bin/radio-puzzle-5.rs

58 lines
1.4 KiB
Rust
Raw Normal View History

2020-06-22 13:23:59 +00:00
#![deny(unused_must_use)]
#![no_main]
#![no_std]
use core::str;
use cortex_m_rt::entry;
use dk::ieee802154::{Channel, Packet};
2021-06-04 11:41:31 +00:00
use heapless::Vec;
2021-04-12 09:51:44 +00:00
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
2020-06-22 13:23:59 +00:00
const TEN_MS: u32 = 10_000;
#[entry]
fn main() -> ! {
let board = dk::init().unwrap();
let mut radio = board.radio;
let mut timer = board.timer;
// puzzle.hex uses channel 25
radio.set_channel(Channel::_25);
let mut packet = Packet::new();
/* # Retrieve the secret string */
packet.copy_from_slice(&[]); // empty packet
2021-03-16 12:26:51 +00:00
radio.send(&mut packet);
2020-06-22 13:23:59 +00:00
if radio.recv_timeout(&mut packet, &mut timer, TEN_MS).is_err() {
2021-04-12 09:51:44 +00:00
defmt::error!("no response or response packet was corrupted");
2020-06-22 13:23:59 +00:00
dk::exit()
}
2021-04-12 09:51:44 +00:00
defmt::info!(
2020-06-22 13:23:59 +00:00
"ciphertext: {}",
str::from_utf8(&packet).expect("packet was not valid UTF-8")
);
/* # Decrypt the string */
2021-06-04 11:41:31 +00:00
let mut buf = Vec::<u8, 128>::new();
2020-06-22 13:23:59 +00:00
// iterate over the bytes
for input in packet.iter() {
// process each byte
// here we should do the reverse mapping; instead we'll do a shift for illustrative purposes
let output = input + 1;
buf.push(output).expect("buffer full");
}
2021-04-12 09:51:44 +00:00
defmt::info!(
2020-06-22 13:23:59 +00:00
"plaintext: {}",
str::from_utf8(&buf).expect("buffer contains non-UTF-8 data")
);
dk::exit()
}