Read telemetry

This commit is contained in:
hodasemi 2023-01-12 22:09:35 +01:00
parent 7e334b81ac
commit dc729b3f10

View file

@ -1,36 +1,36 @@
mod additional_rfactor;
#[allow(warnings)] #[allow(warnings)]
mod rfactor_structs; mod rfactor_structs;
use std::{fs::File, path::Path}; use std::{fs::File, path::Path};
use additional_rfactor::*;
use anyhow::Result; use anyhow::Result;
use memmap2::Mmap; use memmap2::Mmap;
use rfactor_structs::TelemInfoV01;
const MM_TELEMETRY_FILE_NAME: &str = "$rFactor2SMMP_Telemetry$";
const MM_SCORING_FILE_NAME: &str = "$rFactor2SMMP_Scoring$";
const MM_RULES_FILE_NAME: &str = "$rFactor2SMMP_Rules$";
const MM_FORCE_FEEDBACK_FILE_NAME: &str = "$rFactor2SMMP_ForceFeedback$";
const MM_GRAPHICS_FILE_NAME: &str = "$rFactor2SMMP_Graphics$";
const MM_PITINFO_FILE_NAME: &str = "$rFactor2SMMP_PitInfo$";
const MM_WEATHER_FILE_NAME: &str = "$rFactor2SMMP_Weather$";
const MM_EXTENDED_FILE_NAME: &str = "$rFactor2SMMP_Extended$";
const MM_HWCONTROL_FILE_NAME: &str = "$rFactor2SMMP_HWControl$";
const MM_HWCONTROL_LAYOUT_VERSION: u32 = 1;
const MM_WEATHER_CONTROL_FILE_NAME: &str = "$rFactor2SMMP_WeatherControl$";
const MM_WEATHER_CONTROL_LAYOUT_VERSION: u32 = 1;
const MM_RULES_CONTROL_FILE_NAME: &str = "$rFactor2SMMP_RulesControl$";
const MM_RULES_CONTROL_LAYOUT_VERSION: u32 = 1;
const MM_PLUGIN_CONTROL_FILE_NAME: &str = "$rFactor2SMMP_PluginControl$";
const RFACTOR_SHM_FILE: &str = "/dev/shm"; const RFACTOR_SHM_FILE: &str = "/dev/shm";
pub fn test_read() -> Result<()> { pub struct TelemetryReader {
_telemetry_file: File,
shm: Mmap,
}
impl TelemetryReader {
pub fn new() -> Result<Self> {
let file = File::open(Path::new(RFACTOR_SHM_FILE).join(MM_TELEMETRY_FILE_NAME))?; let file = File::open(Path::new(RFACTOR_SHM_FILE).join(MM_TELEMETRY_FILE_NAME))?;
let mmap = unsafe { Mmap::map(&file)? }; let mmap = unsafe { Mmap::map(&file)? };
Ok(()) Ok(Self {
_telemetry_file: file,
shm: mmap,
})
}
pub fn query_telemetry(&self) -> Result<Vec<TelemInfoV01>> {
let telemetry = rF2Telemetry::try_from(&self.shm[0..rF2Telemetry::SIZE])?;
let vehicles = telemetry.vehicles();
Ok(vehicles.to_vec())
}
} }