Skip to main content

device_tests/
device.rs

1use std::fs;
2use std::time::Duration;
3
4use nix::unistd;
5use tokio::sync::{Mutex, MutexGuard};
6
7use crate::model::{PROJECT_VERSION, Revision};
8use crate::print::print_skip;
9
10/// Timeout of one QMI request.
11pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
12
13/// Serializes the tests. They share the modem and the kernel state.
14static MODEM: Mutex<()> = Mutex::const_new(());
15
16/// An AG35 that the test has to itself.
17#[derive(Debug)]
18pub struct Device {
19    revision: Option<Revision>,
20    _modem: MutexGuard<'static, ()>,
21}
22
23impl Device {
24    /// Returns the revision from the project version file.
25    #[must_use]
26    pub const fn revision(&self) -> Option<&Revision> {
27        self.revision.as_ref()
28    }
29}
30
31/// Checks that the test runs as root on an AG35 and waits until no other
32/// test uses the device.
33///
34/// Returns `None` after it printed why the test can't run.
35pub async fn device() -> Option<Device> {
36    let content = match fs::read_to_string(PROJECT_VERSION) {
37        Ok(content) => content,
38        Err(err) => {
39            print_skip(format_args!(
40                "this is not an AG35, {PROJECT_VERSION} can't be read: {err}"
41            ));
42            return None;
43        }
44    };
45    if !unistd::geteuid().is_root() {
46        print_skip(format_args!("the tests must run as root"));
47        return None;
48    }
49    let modem = MODEM.lock().await;
50    Some(Device {
51        revision: Revision::parse(&content),
52        _modem: modem,
53    })
54}