From 08f4bfc01e5771c08bd38c98e330a3c557990f78 Mon Sep 17 00:00:00 2001 From: Chi-Feng Tsai Date: Fri, 23 Aug 2024 21:57:23 +0800 Subject: [PATCH] update serial-pc's client --- fw/Core/Hitcon/Secret/.gitignore | 3 ++ sw/serial-pc/README.md | 14 ++++++++ sw/serial-pc/src/main.rs | 62 ++++++++++++++++++++++++-------- 3 files changed, 64 insertions(+), 15 deletions(-) create mode 100644 fw/Core/Hitcon/Secret/.gitignore diff --git a/fw/Core/Hitcon/Secret/.gitignore b/fw/Core/Hitcon/Secret/.gitignore new file mode 100644 index 0000000..77b1560 --- /dev/null +++ b/fw/Core/Hitcon/Secret/.gitignore @@ -0,0 +1,3 @@ +*.log +cache +secret-out*.h diff --git a/sw/serial-pc/README.md b/sw/serial-pc/README.md index 453cf00..dce9340 100644 --- a/sw/serial-pc/README.md +++ b/sw/serial-pc/README.md @@ -4,6 +4,9 @@ To verify the score on PCB badge, connect badge to PC by serial and run the comm ``` cargo run -- --dump + +# help +cargo run -- --help ``` To connect to a specific serial device, use `--dev` command line argument. @@ -13,3 +16,14 @@ To install `cargo` and relevant rust tools, follow the [Rust website](https://ww ## Hardware Setup ![badge-serial](https://github.com/user-attachments/assets/3e51b8a7-13aa-42c7-8a3f-4fe058f338c0) + +## client + +If you run `cargo run` twice, the second instance will become client mode, and you can send data to the badge. + +``` +cargo run -- --send-game-data --col 0 --data-int 1 + +# help +cargo run -- --help +``` diff --git a/sw/serial-pc/src/main.rs b/sw/serial-pc/src/main.rs index b45ee51..783ac51 100644 --- a/sw/serial-pc/src/main.rs +++ b/sw/serial-pc/src/main.rs @@ -31,8 +31,14 @@ struct ServerArgs { #[command(version, about, long_about = None)] struct ClientArgs { /// Send packet type - #[arg(short, long)] - pkt_type: u8, + #[arg(long)] + send_game_data: bool, + #[arg(long)] + pkt_type: Option, + #[arg(long)] + col: Option, + #[arg(long)] + data_int: Option, } fn main() { @@ -96,7 +102,7 @@ fn server(listener: TcpListener) { packet_parser.extend(&serial_buf[..t]); let pkts = packet_parser.parse(); if !pkts.is_empty() && dump { - port.write_all(&make_pkt(5)).unwrap(); + port.write_all(&make_pkt(5, &[])).unwrap(); dump = false; } let pkts = pkts @@ -177,22 +183,48 @@ fn client() { let args = ClientArgs::parse(); let mut stream = TcpStream::connect("127.0.0.1:7878").expect("Couldn't connect to the server..."); - let pkt = make_pkt(args.pkt_type); - println!("send `{:02X?}`", pkt); - stream.write_all(&pkt).unwrap(); + + if args.send_game_data { + let col = args.col.unwrap(); + // python's equalivalent code: `data = bytes([args.col, *args.data_int.to_bytes(8, 'big')])` + let mut data: Vec = vec![]; + data.push(col); + data.extend(args.data_int.unwrap().to_be_bytes().iter()); + let pkt = make_pkt(3, &data); + println!("send `{:02X?}`", pkt); + stream.write_all(&pkt).unwrap(); + } else { + let pkt = make_pkt(5, &[]); + println!("send `{:02X?}`", pkt); + stream.write_all(&pkt).unwrap(); + } } -fn make_pkt(pkt_type: u8) -> Vec { - // preamble, id, len, type, checksum +fn make_pkt(type_: u8, data: &[u8]) -> Vec { let mut pkt: Vec<_> = b"\x55\x55\x55\x55\x55\x55\x55\xd5".to_vec(); - pkt.extend(b"\0\0"); - pkt.extend(b"\0"); - // pkt.extend(b"\x05"); - pkt.extend([pkt_type]); - pkt.extend(b"\0\0\0\0"); - let c = Crc::default(); - let checksum = c.hash_bytes(&pkt); + pkt.extend(b"\0\0"); // id + pkt.extend([data.len() as u8]); // len + pkt.extend([type_]); // type + pkt.extend(b"\0\0\0\0"); // placeholder of checksum + // copy a pkt and calculate checksum + let mut pkt2 = pkt.clone(); + pkt2.extend(data); + match pkt2.len() % 4 { + 1 => { + pkt2.extend([0, 0, 0]); + } + 2 => { + pkt2.extend([0, 0]); + } + 3 => { + pkt2.extend([0]); + } + _ => (), + } + let checksum = Crc::default().hash_bytes(&pkt2); + // finalize pkt pkt.drain(pkt.len() - 4..); pkt.extend(checksum); + pkt.extend(data); pkt }