crazyflie_lib/subsystems/memory/raw.rs
1use crate::{subsystems::memory::{memory_types, MemoryBackend}, Result};
2
3use memory_types::{FromMemoryBackend};
4
5/// This is used to get raw memory access to any memory device.
6pub struct RawMemory {
7 memory: MemoryBackend,
8}
9
10impl FromMemoryBackend for RawMemory {
11 async fn from_memory_backend(memory: MemoryBackend) -> Result<Self> {
12 Ok(Self { memory })
13 }
14
15 async fn initialize_memory_backend(memory: MemoryBackend) -> Result<Self> {
16 Ok(Self { memory })
17 }
18
19 fn close_memory(self) -> MemoryBackend {
20 self.memory
21 }
22}
23
24impl RawMemory {
25 /// Read raw data from the memory device at the specified address and length.
26 ///
27 /// # Arguments
28 /// * `address` - The starting address to read from
29 /// * `length` - The number of bytes to read
30 /// # Returns
31 /// A `Result` containing a vector of bytes read from the memory or an `Error
32 /// if the operation fails
33 pub async fn read(&self, address: usize, length: usize) -> Result<Vec<u8>> {
34 self.memory.read::<fn(usize, usize)>(address, length, None).await
35 }
36
37 /// Read raw data from the memory device at the specified address and length and
38 /// report progress via the provided callback.
39 ///
40 /// # Arguments
41 /// * `address` - The starting address to read from
42 /// * `length` - The number of bytes to read
43 /// * `progress_callback` - A callback function that takes two usize arguments:
44 /// the number of bytes read so far and the total number of bytes to read.
45 /// # Returns
46 /// A `Result` containing a vector of bytes read from the memory or an `Error
47 /// if the operation fails
48 pub async fn read_with_progress<F>(&self, address: usize, length: usize, progress_callback: F) -> Result<Vec<u8>>
49 where
50 F: FnMut(usize, usize),
51 {
52 self.memory.read(address, length, Some(progress_callback)).await
53 }
54
55 /// Write raw data to the memory device at the specified address.
56 ///
57 /// # Arguments
58 /// * `address` - The starting address to write to
59 /// * `data` - A slice of bytes to write to the memory
60 /// # Returns
61 /// A `Result` indicating success or failure of the write operation
62 pub async fn write(&self, address: usize, data: &[u8]) -> Result<()> {
63 self.memory.write::<fn(usize, usize)>(address, data, None).await
64 }
65
66 /// Write raw data to the memory device at the specified address and report progress
67 /// via the provided callback.
68 ///
69 /// # Arguments
70 /// * `address` - The starting address to write to
71 /// * `data` - A slice of bytes to write to the memory
72 /// * `progress_callback` - A callback function that takes two usize arguments:
73 /// the number of bytes written so far and the total number of bytes to write.
74 /// # Returns
75 /// A `Result` indicating success or failure of the write operation
76 pub async fn write_with_progress<F>(&self, address: usize, data: &[u8], progress_callback: F) -> Result<()>
77 where
78 F: FnMut(usize, usize),
79 {
80 self.memory.write(address, data, Some(progress_callback)).await
81 }
82}