Skip to content

Commit

Permalink
Add simple tool for TCP loopback testing
Browse files Browse the repository at this point in the history
Monitor your board's serial log for the IP address it's assigned. Then,
pass it into this command-line tool to make sure you can connect, send a
message, and receive the response.
  • Loading branch information
mciantyre committed Dec 9, 2023
1 parent 7428b13 commit 3710526
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ eh02-unproven = []
members = [
"board",
"logging",
"tools",
]

[workspace.dependencies]
Expand Down
11 changes: 11 additions & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "tools"
version = "0.1.0"
repository.workspace = true
keywords.workspace = true
categories.workspace = true
license.workspace = true
edition.workspace = true
publish = false

[dependencies]
24 changes: 24 additions & 0 deletions tools/src/bin/tcp_loopback.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::io::prelude::*;
use std::net::TcpStream;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let ip = std::env::args()
.skip(1)
.next()
.ok_or("Provide an IPv4 address")?;

let ip: std::net::Ipv4Addr = ip.parse()?;

let mut client = TcpStream::connect(std::net::SocketAddrV4::new(ip, 5000))?;

const MSG: &[u8] = b"Hello, world!";
client.write(MSG)?;
let mut resp = [0; MSG.len()];
client.read(&mut resp)?;

if resp == MSG {
Ok(())
} else {
Err(format!("Expected '{MSG:?}' but received '{resp:?}'").into())
}
}

0 comments on commit 3710526

Please sign in to comment.