Skip to content

Commit

Permalink
make better interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Dorezyuk committed Dec 8, 2023
1 parent 15199d7 commit b34c99a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
7 changes: 2 additions & 5 deletions zvt/src/feig/sequences.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::logging::PacketWriter;
use crate::sequences::Sequence;
use crate::{packets, ZvtEnum, ZvtParser};
use crate::{packets, ZvtEnum};
use anyhow::Result;
use async_stream::try_stream;
use std::boxed::Box;
Expand Down Expand Up @@ -138,10 +138,7 @@ impl WriteFile {

loop {
// Get the data.
let bytes = src.read_packet().await?;
println!("The packet is {:?}", bytes);

let response = WriteFileResponse::zvt_parse(&bytes)?;
let response = src.read_packet().await?;

match response {
WriteFileResponse::CompletionData(_) => {
Expand Down
24 changes: 20 additions & 4 deletions zvt/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ impl<S> PacketWriter<S>
where
S: AsyncReadExt + Unpin + Send,
{
pub async fn read_packet(&mut self) -> Result<Vec<u8>> {
/// Reads an ADPU packet from the PT.
pub async fn read_packet<T>(&mut self) -> Result<T>
where
T: ZvtParser + Send,
{
let mut buf = vec![0; 3];
self.source.read_exact(&mut buf).await?;

Expand All @@ -38,14 +42,15 @@ where

log::debug!("Read {:?}", buf);

Ok(buf.to_vec())
Ok(T::zvt_parse(&buf)?)
}
}

impl<S> PacketWriter<S>
where
S: AsyncWriteExt + Unpin + Send,
{
/// Writes an ADPU packet to the PT.
pub async fn write_packet<'a, T>(&mut self, msg: &T) -> Result<()>
where
T: ZvtSerializer + Sync + Send,
Expand All @@ -64,15 +69,26 @@ impl<S> PacketWriter<S>
where
S: AsyncWriteExt + AsyncReadExt + Unpin + Send,
{
/// Reads an ADPU packet from the PT and send an [packets::Ack].
pub async fn read_packet_with_ack<'a, T>(&mut self) -> Result<T>
where
T: ZvtParser + Send,
{
let packet = self.read_packet::<T>().await?;
self.write_packet(&packets::Ack {}).await?;

Ok(packet)
}

/// Writes an ADPU packet to the PT and awaits its [packets::Ack].
pub async fn write_packet_with_ack<'a, T>(&mut self, msg: &T) -> Result<()>
where
T: ZvtSerializer + Sync + Send,
encoding::Default: encoding::Encoding<T>,
{
self.write_packet(msg).await?;

let bytes = self.read_packet().await?;
let _ = Ack::zvt_parse(&bytes)?;
let _ = self.read_packet::<Ack>().await?;

Ok(())
}
Expand Down
30 changes: 10 additions & 20 deletions zvt/src/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ where
// This pin has nothing to do with the fact that we return a Stream
// but is needed to access methods like `write_packet`.
src.write_packet_with_ack(input).await?;
let bytes = src.read_packet().await?;
let packet = Self::Output::zvt_parse(&bytes)?;
let packet = src.read_packet().await?;
// Write the response.
src.write_packet::<packets::Ack>(&packets::Ack {}).await?;
yield packet;
Expand Down Expand Up @@ -99,8 +98,7 @@ impl Sequence for ReadCard {
let s = try_stream! {
src.write_packet_with_ack(input).await?;
loop {
let bytes = src.read_packet().await?;
let packet = ReadCardResponse::zvt_parse(&bytes)?;
let packet = src.read_packet().await?;
// Write the response.
src.write_packet(&packets::Ack {}).await?;

Expand Down Expand Up @@ -154,8 +152,7 @@ impl Sequence for Initialization {
// 2.18.1
src.write_packet_with_ack(input).await?;
loop {
let bytes = src.read_packet().await?;
let response = InitializationResponse::zvt_parse(&bytes)?;
let response = src.read_packet().await?;

// Every message requires an Ack.
src.write_packet(&packets::Ack {}).await?;
Expand Down Expand Up @@ -260,8 +257,7 @@ impl Sequence for Diagnosis {
// 2.18.1
src.write_packet_with_ack(input).await?;
loop {
let bytes = src.read_packet().await?;
let response = DiagnosisResponse::zvt_parse(&bytes)?;
let response = src.read_packet().await?;

// Every message requires an Ack.
src.write_packet(&packets::Ack {}).await?;
Expand Down Expand Up @@ -326,8 +322,7 @@ impl Sequence for EndOfDay {
src.write_packet_with_ack(input).await?;

loop {
let bytes = src.read_packet().await?;
let packet = EndOfDayResponse::zvt_parse(&bytes)?;
let packet = src.read_packet().await?;

// Write the response.
src.write_packet(&packets::Ack {}).await?;
Expand Down Expand Up @@ -392,8 +387,7 @@ impl Sequence for Reservation {
src.write_packet_with_ack(input).await?;

loop {
let bytes = src.read_packet().await?;
let packet = AuthorizationResponse::zvt_parse(&bytes)?;
let packet = src.read_packet().await?;
src.write_packet(&packets::Ack {}).await?;
match packet {
AuthorizationResponse::CompletionData(_) | AuthorizationResponse::Abort(_) => {
Expand Down Expand Up @@ -461,8 +455,7 @@ impl Sequence for PartialReversal {
src.write_packet_with_ack(input).await?;

loop {
let bytes = src.read_packet().await?;
let packet = PartialReversalResponse::zvt_parse(&bytes)?;
let packet = src.read_packet().await?;
src.write_packet(&packets::Ack {}).await?;
match packet {
PartialReversalResponse::CompletionData(_)
Expand Down Expand Up @@ -500,8 +493,7 @@ impl Sequence for PreAuthReversal {
src.write_packet_with_ack(input).await?;

loop {
let bytes = src.read_packet().await?;
let packet = PartialReversalResponse::zvt_parse(&bytes)?;
let packet = src.read_packet().await?;
src.write_packet(&packets::Ack {}).await?;
match packet {
PartialReversalResponse::CompletionData(_)
Expand Down Expand Up @@ -550,8 +542,7 @@ impl Sequence for PrintSystemConfiguration {
src.write_packet_with_ack(input).await?;

loop {
let bytes = src.read_packet().await?;
let packet = PrintSystemConfigurationResponse::zvt_parse(&bytes)?;
let packet = src.read_packet().await?;
src.write_packet(&packets::Ack {}).await?;
match packet {
PrintSystemConfigurationResponse::CompletionData(_) => {
Expand Down Expand Up @@ -620,8 +611,7 @@ impl Sequence for StatusEnquiry {
src.write_packet_with_ack(input).await?;

loop {
let bytes = src.read_packet().await?;
let packet = StatusEnquiryResponse::zvt_parse(&bytes)?;
let packet = src.read_packet().await?;
src.write_packet(&packets::Ack {}).await?;
match packet {
StatusEnquiryResponse::CompletionData(_) => {
Expand Down

0 comments on commit b34c99a

Please sign in to comment.