Skip to content

Commit

Permalink
Merged main.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirVer committed Dec 11, 2023
2 parents 56fb992 + 8eb4a3a commit e314abe
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 154 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ uses in production in most of our charging stations.

## Getting started

Start by looking in [`status.rs`](zvt/examples/status.rs) for a typical way of
Start by looking in [`status`](zvt/src/bin/status/main.rs) for a typical way of
interfacing with a terminal. A useful standalone tool is
[`feig_update.rs`](zvt/examples/feig_update.rs) which we use in production to
[`feig_update`](zvt/src/bin/feig_update/main.rs) which we use in production to
update the Firmware of our cVEND plug terminals.
26 changes: 12 additions & 14 deletions zvt/src/feig/sequences.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::sequences::{read_packet_async, write_packet_async, write_with_ack_async, Sequence};
use crate::{packets, ZvtEnum, ZvtParser};
use crate::io::PacketTransport;
use crate::sequences::Sequence;
use crate::{packets, ZvtEnum};
use anyhow::Result;
use async_stream::try_stream;
use std::boxed::Box;
Expand Down Expand Up @@ -94,10 +95,10 @@ impl WriteFile {
path: PathBuf,
password: usize,
adpu_size: u32,
src: &mut Source,
src: &mut PacketTransport<Source>,
) -> Pin<Box<impl Stream<Item = Result<WriteFileResponse>> + '_>>
where
Source: AsyncReadExt + AsyncWriteExt + Unpin,
Source: AsyncReadExt + AsyncWriteExt + Unpin + Send,
{
// Protocol from the handbook (the numbering is not part of the handbook)
// 1.1 ECR->PT: Send over the list of all files with their sizes.
Expand All @@ -108,8 +109,6 @@ impl WriteFile {
// 3.0 PT->ERC replies with Completion.

let s = try_stream! {
tokio::pin!(src);

use super::packets::tlv::File as TlvFile;
let files = convert_dir(&path)?;
let mut packets = Vec::with_capacity(files.len());
Expand All @@ -133,25 +132,24 @@ impl WriteFile {
};

// 1.1. and 1.2
write_with_ack_async(&packet, &mut src).await?;
src.write_packet_with_ack(&packet).await?;
let mut buf = vec![0; adpu_size as usize];
println!("the length is {}", buf.len());

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

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

match response {
WriteFileResponse::CompletionData(_) => {
write_packet_async(&mut src, &packets::Ack {}).await?;
src.write_packet(&packets::Ack {}).await?;

yield response;
break;
}
WriteFileResponse::Abort(_) => {
write_packet_async(&mut src, &packets::Ack {}).await?;
src.write_packet(&packets::Ack {}).await?;

yield response;
break;
}
Expand Down Expand Up @@ -193,7 +191,7 @@ impl WriteFile {
}),
}),
};
write_packet_async(&mut src, &packet).await?;
src.write_packet(&packet).await?;

yield response;
}
Expand Down
96 changes: 96 additions & 0 deletions zvt/src/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use crate::packets;
use crate::ZvtEnum;
use crate::ZvtParser;
use anyhow::Result;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use zvt_builder::encoding;
use zvt_builder::ZvtSerializer;

#[derive(ZvtEnum)]
pub enum Ack {
Ack(packets::Ack),
}

pub struct PacketTransport<Source> {
pub source: Source,
}

impl<S> PacketTransport<S>
where
S: AsyncReadExt + Unpin + Send,
{
/// 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?;

// Get the len.
let len = if buf[2] == 0xff {
buf.resize(5, 0);
self.source.read_exact(&mut buf[3..5]).await?;
u16::from_le_bytes(buf[3..5].try_into().unwrap()) as usize
} else {
buf[2] as usize
};

let start = buf.len();
buf.resize(start + len, 0);
self.source.read_exact(&mut buf[start..]).await?;

// NOCOM(#sirver): add pretty hex here
log::debug!("Read {:?}", buf);

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

impl<S> PacketTransport<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,
encoding::Default: encoding::Encoding<T>,
{
let bytes = msg.zvt_serialize();
log::debug!("Write {:?}", bytes);
self.source
.write_all(&bytes)
.await
.map_err(|e| anyhow::anyhow!("Failed to write {:?}", e))
}
}

impl<S> PacketTransport<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 _ = self.read_packet::<Ack>().await?;

Ok(())
}
}
1 change: 1 addition & 0 deletions zvt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod constants;
pub mod feig;
pub mod io;
pub mod packets;
pub mod sequences;

Expand Down
Loading

0 comments on commit e314abe

Please sign in to comment.