Implement AccessPoint::last_seen, using procfs to get the boot time.

This commit is contained in:
Lucy 2022-02-02 15:07:20 -05:00
parent 8df68611fe
commit df6070b75c
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
4 changed files with 23 additions and 10 deletions

View file

@ -7,6 +7,8 @@ license = "MPL-2.0"
[dependencies]
bitflags = "1.3.2"
derive_builder = "0.10.2"
procfs = { version = "0.12.0", default-features = false }
time = "0.3.7"
zbus = "2.0.1"
[features]

View file

@ -1,23 +1,23 @@
// SPDX-License-Identifier: MPL-2.0
use crate::interface::{
access_point::AccessPointProxy,
enums::{ApFlags, ApSecurityFlags},
use crate::{
interface::{
access_point::AccessPointProxy,
enums::{ApFlags, ApSecurityFlags},
},
util::clock_boottime_to_time,
};
use std::ops::Deref;
use time::OffsetDateTime;
use zbus::Result;
#[derive(Debug)]
pub struct AccessPoint<'a>(AccessPointProxy<'a>);
impl<'a> AccessPoint<'a> {
/* TODO: figure out how to convert CLOCK_BOOTTIME to SystemTime, as CLOCK_BOOTTIME's starting point is arbritary and not guaranteed to match up with the UNIX Epoch
pub async fn last_seen(&self) -> Result<Option<SystemTime>> {
let last_seen = self.0.last_seen().await?;
if !last_seen.is_positive() {
return Ok(None);
}
}*/
pub async fn last_seen(&self) -> Result<Option<OffsetDateTime>> {
Ok(clock_boottime_to_time(self.0.last_seen().await?))
}
pub async fn flags(&self) -> Result<ApFlags> {
self.0.flags().await.map(ApFlags::from_bits_truncate)

View file

@ -7,3 +7,4 @@ pub mod device;
pub mod interface;
pub mod nm;
pub mod settings;
pub(crate) mod util;

View file

@ -0,0 +1,10 @@
// SPDX-License-Identifier: MPL-2.0
use time::OffsetDateTime;
pub fn clock_boottime_to_time(time: i32) -> Option<OffsetDateTime> {
let boot_time = procfs::boot_time_secs()
.ok()
.and_then(|boot_time| i64::try_from(boot_time).ok())?;
OffsetDateTime::from_unix_timestamp(boot_time + time as i64).ok()
}