Skip to content

Commit

Permalink
Extract current time from HTTP responses and keep track of it
Browse files Browse the repository at this point in the history
  • Loading branch information
zargony committed Oct 29, 2024
1 parent a48ccc3 commit 5f0eb33
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions firmware/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions firmware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ overflow-checks = false
git2 = { version = "0.19", default-features = false }

[dependencies]
chrono = { version = "0.4", default-features = false, features = ["alloc"] }
display-interface = "0.5"
embassy-embedded-hal = "0.2"
embassy-executor = "0.6"
Expand Down
16 changes: 15 additions & 1 deletion firmware/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::json::{self, FromJson, ToJson};
use crate::time;
use crate::wifi::{DnsSocket, TcpClient, TcpConnection, Wifi};
use alloc::vec::Vec;
use chrono::{DateTime, Utc};
use core::convert::Infallible;
use core::fmt;
use core::{fmt, str};
use embedded_io_async::{BufRead, Read};
use log::debug;
use reqwless::client::{HttpClient, HttpResource, HttpResourceRequestBuilder};
Expand Down Expand Up @@ -212,6 +214,18 @@ impl<'a> Connection<'a> {
let response = request.send(rx_buf).await?;
debug!("HTTP: Status {}", response.status.0);

// Extract current date and time from response
let time = response
.headers()
.find_map(|(k, v)| (k == "Date").then_some(v))
.and_then(|v| str::from_utf8(v).ok())
.and_then(|s| DateTime::parse_from_rfc2822(s).ok())
.map(|d| d.with_timezone(&Utc));
if let Some(time) = time {
time::set(time);
}

// Check HTTP response status
if response.status.0 == 401 {
return Err(Error::Unauthorized);
} else if response.status.is_server_error() {
Expand Down
1 change: 1 addition & 0 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ mod keypad;
mod nfc;
mod pn532;
mod screen;
mod time;
mod ui;
mod user;
mod vereinsflieger;
Expand Down
44 changes: 44 additions & 0 deletions firmware/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use chrono::{DateTime, TimeDelta, Utc};
use core::cell::RefCell;
use embassy_sync::blocking_mutex::CriticalSectionMutex;
use log::debug;

/// Calculated time of system start
static SYSTEM_START_TIME: CriticalSectionMutex<RefCell<Option<DateTime<Utc>>>> =
CriticalSectionMutex::new(RefCell::new(None));

/// Time of system start
fn start_time() -> Option<DateTime<Utc>> {
SYSTEM_START_TIME.lock(|sst| *sst.borrow())
}

/// Set time of system start
fn set_start_time(time: DateTime<Utc>) {
SYSTEM_START_TIME.lock(|sst| {
*sst.borrow_mut() = Some(time);
});
}

/// Duration of system run time
pub fn uptime() -> Option<TimeDelta> {
let millis = esp_hal::time::now().duration_since_epoch().to_millis();
TimeDelta::try_milliseconds(i64::try_from(millis).ok()?)
}

/// Current time
#[allow(dead_code)]
pub fn now() -> Option<DateTime<Utc>> {
if let (Some(start_time), Some(uptime)) = (start_time(), uptime()) {
Some(start_time + uptime)
} else {
None
}
}

/// Set current time by using the given current time to calculate the time of system start
pub fn set(now: DateTime<Utc>) {
if let Some(uptime) = uptime() {
set_start_time(now - uptime);
debug!("Time: Current time set to {}", now);
}
}

0 comments on commit 5f0eb33

Please sign in to comment.