Make fields of Command private.

This commit is contained in:
Heiko Schaefer 2021-08-25 17:29:21 +02:00
parent 833a22f8f0
commit 786515a7f4
2 changed files with 32 additions and 12 deletions

View file

@ -8,17 +8,17 @@ use anyhow::Result;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) struct Command { pub(crate) struct Command {
// Class byte (CLA) // Class byte (CLA)
pub cla: u8, cla: u8,
// Instruction byte (INS) // Instruction byte (INS)
pub ins: u8, ins: u8,
// Parameter bytes (P1/P2) // Parameter bytes (P1/P2)
pub p1: u8, p1: u8,
pub p2: u8, p2: u8,
// NOTE: data must be smaller than 64 kbyte // NOTE: data must be smaller than 64 kbyte
pub data: Vec<u8>, data: Vec<u8>,
} }
impl Command { impl Command {
@ -34,6 +34,22 @@ impl Command {
} }
} }
pub(crate) fn get_ins(&self) -> u8 {
self.ins
}
pub(crate) fn get_p1(&self) -> u8 {
self.p1
}
pub(crate) fn get_p2(&self) -> u8 {
self.p2
}
pub(crate) fn get_data(&self) -> &[u8] {
&self.data
}
fn encode_len(len: u16, ext: Le) -> Vec<u8> { fn encode_len(len: u16, ext: Le) -> Vec<u8> {
if len > 0xff || ext == Le::Long { if len > 0xff || ext == Le::Long {
vec![0, (len as u16 >> 8) as u8, (len as u16 & 255) as u8] vec![0, (len as u16 >> 8) as u8, (len as u16 & 255) as u8]

View file

@ -130,21 +130,25 @@ fn send_command_low_level(
log::trace!("buf_size {}", buf_size); log::trace!("buf_size {}", buf_size);
if chaining_support && !cmd.data.is_empty() { if chaining_support && !cmd.get_data().is_empty() {
// Send command in chained mode // Send command in chained mode
log::debug!("chained command mode"); log::debug!("chained command mode");
// Break up payload into chunks that fit into one command, each // Break up payload into chunks that fit into one command, each
let chunks: Vec<_> = cmd.data.chunks(max_cmd_bytes).collect(); let chunks: Vec<_> = cmd.get_data().chunks(max_cmd_bytes).collect();
for (i, d) in chunks.iter().enumerate() { for (i, d) in chunks.iter().enumerate() {
let last = i == chunks.len() - 1; let last = i == chunks.len() - 1;
let partial = Command {
cla: if last { 0x00 } else { 0x10 }, let cla = if last { 0x00 } else { 0x10 };
data: d.to_vec(), let partial = Command::new(
..cmd cla,
}; cmd.get_ins(),
cmd.get_p1(),
cmd.get_p2(),
d.to_vec(),
);
let serialized = partial let serialized = partial
.serialize(ext) .serialize(ext)