Skip to content

Commit

Permalink
update serial-pc's client
Browse files Browse the repository at this point in the history
  • Loading branch information
ktpss95112 committed Aug 23, 2024
1 parent d445c37 commit 08f4bfc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
3 changes: 3 additions & 0 deletions fw/Core/Hitcon/Secret/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.log
cache
secret-out*.h
14 changes: 14 additions & 0 deletions sw/serial-pc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
```
62 changes: 47 additions & 15 deletions sw/serial-pc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>,
#[arg(long)]
col: Option<u8>,
#[arg(long)]
data_int: Option<u64>,
}

fn main() {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<u8> = 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<u8> {
// preamble, id, len, type, checksum
fn make_pkt(type_: u8, data: &[u8]) -> Vec<u8> {
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
}

0 comments on commit 08f4bfc

Please sign in to comment.