Skip to content

Commit

Permalink
fix chrono deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Mar 27, 2024
1 parent 753762c commit 1812f69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
15 changes: 7 additions & 8 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use defmt_rtt as _; // global logger
use hal::{
adc::{self, Adc},
aes::Aes,
chrono::{NaiveDate, NaiveDateTime},
chrono::{DateTime, NaiveDate, NaiveDateTime, Utc},
cortex_m::{delay::Delay, peripheral::syst::SystClkSource},
dma::{AllDma, Dma1Ch1, Dma1Ch2},
embedded_hal::digital::v2::ToggleableOutputPin,
Expand Down Expand Up @@ -112,9 +112,8 @@ fn locked_radio(
let timestamp_millis: i64 = i64::from(data[0]) << 32 | i64::from(data[1]);
let secs: i64 = timestamp_millis / 1000;
let nsec: u32 = unwrap!(u32::try_from(timestamp_millis % 1000).ok()) * 1_000_000;
let date_time: NaiveDateTime =
unwrap!(NaiveDateTime::from_timestamp_opt(secs, nsec));
rtc.set_date_time(date_time);
let date_time: DateTime<Utc> = unwrap!(DateTime::from_timestamp(secs, nsec));
rtc.set_date_time(date_time.naive_utc());
defmt::info!("set date time to {}", defmt::Display2Format(&date_time));

unwrap!(unsafe { sg.set_sleep(SLEEP_CFG) });
Expand Down Expand Up @@ -311,16 +310,16 @@ mod app {
// check RTC is setup and we have not exhausted the rate limit
ctx.shared.rtc.lock(|rtc| match rtc.date_time() {
Some(date_time) => {
let min_date: NaiveDate = NaiveDate::from_ymd(2021, 9, 11);
let min_date_time: NaiveDateTime = min_date.and_hms(0, 0, 0);
let min_date: NaiveDate = unwrap!(NaiveDate::from_ymd_opt(2021, 9, 11));
let min_date_time: NaiveDateTime = unwrap!(min_date.and_hms_opt(0, 0, 0));
if date_time < min_date_time {
defmt::warn!(
"ignoring PB3 IRQ, RTC date is in the past {}",
defmt::Display2Format(&date_time)
);
}

let timestamp: i64 = date_time.timestamp_millis();
let timestamp: i64 = date_time.and_utc().timestamp_millis();
if prev.saturating_add(300) > timestamp {
defmt::warn!("ignoring PB3 IRQ, rate limited to 300 ms");
} else {
Expand Down Expand Up @@ -433,7 +432,7 @@ mod app {

// fill IV
iv[0] = unwrap!(rng.try_u32());
let millis: i64 = unwrap!(rtc.date_time()).timestamp_millis();
let millis: i64 = unwrap!(rtc.date_time()).and_utc().timestamp_millis();
iv[1] = (millis >> 32) as u32;
iv[2] = millis as u32;

Expand Down
8 changes: 4 additions & 4 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ mod app {

// this is a security problem that needs to be fixed
// the server should have the time set by an up-to-date source.
let date: NaiveDate = NaiveDate::from_ymd(2021, 9, 11);
let date_time: NaiveDateTime = date.and_hms(10, 47, 25);
let date: NaiveDate = unwrap!(NaiveDate::from_ymd_opt(2021, 9, 11));
let date_time: NaiveDateTime = unwrap!(date.and_hms_opt(10, 47, 25));
rtc.set_date_time(date_time);

unwrap!(setup_radio(&mut sg));
Expand Down Expand Up @@ -171,7 +171,7 @@ mod app {
BASE_PACKET_PARAMS.set_payload_len(PKT_LEN);
unwrap!(sg.set_packet_params(&PACKET_PARAMS));

let millis: i64 = unwrap!(rtc.date_time()).timestamp_millis();
let millis: i64 = unwrap!(rtc.date_time()).and_utc().timestamp_millis();
buf[0] = *time_sync_cnt;
buf[1] = (millis >> 32) as u32;
buf[2] = millis as u32;
Expand Down Expand Up @@ -214,7 +214,7 @@ mod app {
}[..(len as usize) - IV_AND_TAG_LEN];

// verify message was sent recently
let now: i64 = unwrap!(rtc.date_time()).timestamp_millis();
let now: i64 = unwrap!(rtc.date_time()).and_utc().timestamp_millis();
let msg_millis: i64 = i64::from(iv[1]) << 32 | i64::from(iv[2]);
let elapsed: i64 = now - msg_millis;
defmt::info!("client-server time Δ: {} ms", elapsed);
Expand Down

0 comments on commit 1812f69

Please sign in to comment.