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::{LinearMap, 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);
|
|
|
|
|
2020-06-25 09:51:16 +00:00
|
|
|
/* # Build a dictionary */
|
2021-06-04 11:41:31 +00:00
|
|
|
let dict = LinearMap::<u8, u8, 128>::new();
|
2020-06-25 09:51:16 +00:00
|
|
|
|
2020-06-22 13:23:59 +00:00
|
|
|
let mut packet = Packet::new();
|
2020-06-25 09:51:16 +00:00
|
|
|
for source in 0..=127 {
|
2020-06-22 13:23:59 +00:00
|
|
|
packet.copy_from_slice(&[source]);
|
|
|
|
|
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_ok() {
|
|
|
|
// response should be one byte large
|
|
|
|
if packet.len() == 1 {
|
2020-06-22 17:11:42 +00:00
|
|
|
let _destination = packet[0];
|
2020-06-22 13:23:59 +00:00
|
|
|
|
|
|
|
// TODO insert the key-value pair
|
|
|
|
// dict.insert(/* ? */, /* ? */).expect("dictionary full");
|
|
|
|
} else {
|
2021-04-12 09:51:44 +00:00
|
|
|
defmt::error!("response packet was not a single byte");
|
2020-06-22 13:23:59 +00:00
|
|
|
dk::exit()
|
|
|
|
}
|
|
|
|
} else {
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* # 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()
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:24:21 +00:00
|
|
|
defmt::println!(
|
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 buffer = Vec::<u8, 128>::new();
|
2020-06-22 13:23:59 +00:00
|
|
|
|
|
|
|
// iterate over the bytes
|
|
|
|
for byte in packet.iter() {
|
|
|
|
// NOTE this should map from the encrypted letter to the plaintext letter
|
|
|
|
let key = byte;
|
|
|
|
let value = dict[key];
|
|
|
|
buffer.push(value).expect("buffer full");
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:24:21 +00:00
|
|
|
defmt::println!(
|
2020-06-22 13:23:59 +00:00
|
|
|
"plaintext: {}",
|
|
|
|
str::from_utf8(&buffer).expect("buffer contains non-UTF-8 data")
|
|
|
|
);
|
|
|
|
|
2020-06-25 09:51:16 +00:00
|
|
|
/* # (NEW) Verify decrypted text */
|
2020-06-22 13:23:59 +00:00
|
|
|
packet.copy_from_slice(&buffer);
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:24:21 +00:00
|
|
|
defmt::println!(
|
2020-06-22 13:23:59 +00:00
|
|
|
"Dongle response: {}",
|
|
|
|
str::from_utf8(&packet).expect("response was not UTF-8")
|
|
|
|
);
|
|
|
|
|
|
|
|
dk::exit()
|
|
|
|
}
|