Test the usage of the module DS3231

main
kirbylife 2023-03-30 22:10:13 -06:00
parent e34838c985
commit 432a770887
1 changed files with 44 additions and 27 deletions

View File

@ -2,16 +2,17 @@
#![no_main]
mod crypto;
mod datetime;
use arduino_hal::hal::usart::BaudrateArduinoExt;
use arduino_hal::prelude::*;
use arduino_hal::{delay_ms, Usart};
use arduino_hal::default_serial;
use arduino_hal::delay_ms;
use crypto::hmac_sha1;
use ds323x::Rtcc;
use panic_halt as _;
const INTERVAL: u64 = 30;
const SECRET_KEY_MAX_LEN: u8 = 32;
const SECRET_KEY_MAX_LEN: usize = 32;
fn process_secret<'a>(secret: &str, output: &mut [u8]) -> usize {
// RFC 6238 Doesn't specify a max length for the secret keys, So I chose 32
@ -40,7 +41,7 @@ fn process_secret<'a>(secret: &str, output: &mut [u8]) -> usize {
}
fn generate_otp_token(private_key: &str, actual_time: u64) -> u32 {
let mut output = [0u8; 50];
let mut output = [0u8; 32];
let private_key_len = process_secret(private_key, &mut output);
let interval = actual_time / INTERVAL;
@ -56,38 +57,54 @@ fn generate_otp_token(private_key: &str, actual_time: u64) -> u32 {
((raw_token & 0x7F_FF_FF_FF) % 1_000_000) as u32
}
fn token_to_bytes(mut token: u32, output: &mut [u8]) {
let mut inc = 0;
while token > 0 {
output[inc] = (token % 10) as u8 + b'0';
token /= 10;
inc += 1;
}
output.reverse()
}
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut led = pins.d13.into_output();
let mut serial = Usart::new(
dp.USART0,
pins.d0,
pins.d1.into_output(),
BaudrateArduinoExt::into_baudrate(9600),
let mut serial = default_serial!(dp, pins, 9600);
let i2c_clock = arduino_hal::I2c::new(
dp.TWI,
pins.a4.into_pull_up_input(),
pins.a5.into_pull_up_input(),
50000,
);
let mut rtc = ds323x::Ds323x::new_ds3231(i2c_clock);
let year = rtc.year().unwrap();
let month = rtc.month().unwrap();
let day = rtc.day().unwrap();
let hour = match rtc.hours().unwrap() {
ds323x::Hours::AM(val) => val,
ds323x::Hours::PM(val) => val + 12,
ds323x::Hours::H24(val) => val,
};
let minute = rtc.minutes().unwrap();
let second = rtc.seconds().unwrap();
let datetime = datetime::Datetime {
year: year,
month: month,
day: day,
hours: hour,
minutes: minute,
seconds: second,
};
let timestamp = datetime.unix_epoch();
ufmt::uwriteln!(&mut serial, "Timestamp: {}", timestamp);
// let token = generate_otp_token("holaaaaamundo", 1680155315);
let token = generate_otp_token("FS7J22EHLLSOGKUVJKV2XE7FTIX24JAJ", 1680155315);
let mut output = [0u8; 6];
token_to_bytes(token, &mut output);
ufmt::uwriteln!(&mut serial, "token: {}", token).unwrap();
for b in output {
serial.write_byte(b);
}
serial.write_byte('\n' as u8);
delay_ms(5000);
let token = generate_otp_token("FS7J22EHLLSOGKUVJKV2XE7FTIX24JAJ", 1680155315);
ufmt::uwriteln!(&mut serial, "token: {}", token).unwrap();
delay_ms(5000);
let token = generate_otp_token("FS7J22EHLLSOGKUVJKV2XE7FTIX24JAJ", 1680155315);
ufmt::uwriteln!(&mut serial, "token: {}", token).unwrap();
loop {
led.toggle();