pub struct OcsdDeviceContext { /* private fields */ }Expand description
Context representing a single OCSD device
Implementations§
Source§impl OcsdDeviceContext
impl OcsdDeviceContext
Sourcepub fn read(&mut self) -> OcsdDevice
pub fn read(&mut self) -> OcsdDevice
Read and parse this device from the OCSD buffer.
Sourcepub fn write(&mut self, device: &OcsdDevice)
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§
impl !Send for OcsdDeviceContext
impl !Sync for OcsdDeviceContext
impl Freeze for OcsdDeviceContext
impl RefUnwindSafe for OcsdDeviceContext
impl Unpin for OcsdDeviceContext
impl UnsafeUnpin for OcsdDeviceContext
impl UnwindSafe for OcsdDeviceContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more