Skip to main content

OcsdDeviceContext

Struct OcsdDeviceContext 

Source
pub struct OcsdDeviceContext { /* private fields */ }
Expand description

Context representing a single OCSD device

Implementations§

Source§

impl OcsdDeviceContext

Source

pub fn read(&mut self) -> OcsdDevice

Read and parse this device from the OCSD buffer.

Source

pub fn write(&mut self, device: &OcsdDevice)

Replace the device data in the OCSD buffer with that provided.

Examples found in repository?
examples/report_device/main.rs (line 131)
76fn main() {
77    match OcsdContext::new(base_address::ML350_GEN9) {
78        Ok(mut context) => {
79            let mut header = context.read_header();
80            println!("Header data before write:");
81            print_struct_bytes(&header.to_bytes());
82
83            // enable readings for device #2
84            header.buffers_in_use = 3;
85
86            println!("Ready to write:");
87            print_struct_bytes(&header.to_bytes());
88
89            context.write_header(&header);
90
91            let app_state: AppState = match OpenOptions::new().read(true).open("state.json") {
92                Ok(reader) => match serde_json::from_reader(reader) {
93                    Ok(app_state) => app_state,
94                    Err(err) => {
95                        println!("Couldn't load state: {:?}", err);
96                        println!("Using default.");
97                        AppState { count: 0 }
98                    }
99                },
100                Err(err) => {
101                    println!("Couldn't open state file: {:?}", err);
102                    println!("Using default.");
103                    AppState { count: 0 }
104                }
105            };
106            let count = Arc::new(AtomicU16::new(app_state.count));
107
108            let should_exit = Arc::new(AtomicBool::new(false));
109            let mut file = OpenOptions::new()
110                .create(true)
111                .truncate(true) // If the file already exists we want to overwrite the old data
112                .write(true)
113                .open("state.json")
114                .unwrap();
115
116            let should_exit_clone = should_exit.clone();
117            let count_clone = count.clone();
118            let _ = ctrlc::set_handler(move || {
119                serde_json::to_writer(
120                    &mut file,
121                    &AppState {
122                        count: (*count_clone).load(atomic::Ordering::Relaxed),
123                    },
124                )
125                .unwrap();
126                should_exit_clone.store(true, atomic::Ordering::Relaxed);
127            });
128
129            loop {
130                let device = make_device((*count).load(atomic::Ordering::Relaxed));
131                context.device_mappings[2].write(&device);
132
133                std::thread::sleep(Duration::from_millis(1000));
134                (*count).fetch_add(1, atomic::Ordering::Relaxed);
135
136                if should_exit.load(atomic::Ordering::Relaxed) {
137                    break;
138                };
139            }
140        }
141        Err(_) => {
142            println!(
143                "Unable to open OCSD header context in memory. Do you have access to /dev/mem?"
144            )
145        }
146    }
147}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.