1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::command::{rx, xmit, Command, CommandResponseStatus, Commander, Error};
use scroll::{ctx, ctx::TryIntoCtx, Pread, Pwrite, LE};
pub struct ReadWords {
target_address: u32,
num_words: u32,
}
impl<'a> ctx::TryIntoCtx<::scroll::Endian> for &'a ReadWords {
type Error = ::scroll::Error;
fn try_into_ctx(
self,
dst: &mut [u8],
ctx: ::scroll::Endian,
) -> ::scroll::export::result::Result<usize, Self::Error> {
let mut offset = 0;
dst.gwrite_with(self.target_address, &mut offset, ctx)?;
dst.gwrite_with(self.num_words, &mut offset, ctx)?;
Ok(offset)
}
}
impl<'a> Commander<'a, ReadWordsResponse> for ReadWords {
const ID: u32 = 0x0008;
fn send(&self, d: &hidapi::HidDevice) -> Result<ReadWordsResponse, Error> {
let mut data = vec![0_u8; 8];
let _ = self.try_into_ctx(&mut data, LE)?;
let command = Command::new(Self::ID, 0, data);
xmit(command, d)?;
let rsp = rx(d)?;
if rsp.status != CommandResponseStatus::Success {
return Err(Error::CommandNotRecognized);
}
let res: ReadWordsResponse =
(rsp.data.as_slice()).pread_with::<ReadWordsResponse>(0, LE)?;
Ok(res)
}
}
#[derive(Debug, PartialEq)]
pub struct ReadWordsResponse {
pub words: Vec<u32>,
}
impl<'a> ctx::TryFromCtx<'a, scroll::Endian> for ReadWordsResponse {
type Error = Error;
fn try_from_ctx(this: &'a [u8], le: scroll::Endian) -> Result<(Self, usize), Self::Error> {
if this.len() < 4 {
return Err(Error::Parse);
}
let mut words: Vec<u32> = vec![0; this.len() / 4];
let mut offset = 0;
this.gread_inout_with(&mut offset, &mut words, le)?;
Ok((ReadWordsResponse { words }, offset))
}
}