Skip to content

Commit

Permalink
Merge #730
Browse files Browse the repository at this point in the history
730: Error refactor, part 2: Remove Error enum. r=Dirbaio a=Dirbaio

Finishes work started on #617, see there for motivation.

Depends on #726 

Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
Co-authored-by: Thibaut Vandervelden <thvdveld@vub.be>
  • Loading branch information
3 people committed Jan 14, 2023
2 parents 8f52dab + 84cea13 commit 1f7e1fa
Show file tree
Hide file tree
Showing 41 changed files with 1,489 additions and 2,047 deletions.
8 changes: 1 addition & 7 deletions examples/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

mod utils;

use log::debug;
use std::cmp;
use std::io::{Read, Write};
use std::net::TcpStream;
Expand Down Expand Up @@ -114,12 +113,7 @@ fn main() {
let mut processed = 0;
while !CLIENT_DONE.load(Ordering::SeqCst) {
let timestamp = Instant::now();
match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(timestamp, &mut device, &mut sockets);

// tcp:1234: emit data
let socket = sockets.get_mut::<tcp::Socket>(tcp1_handle);
Expand Down
40 changes: 10 additions & 30 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
mod utils;

use log::debug;
use std::collections::BTreeMap;
use std::os::unix::io::AsRawFd;
use std::str::{self, FromStr};

#[cfg(any(
feature = "proto-sixlowpan-fragmentation",
feature = "proto-ipv4-fragmentation"
))]
use smoltcp::iface::ReassemblyBuffer;

use smoltcp::iface::{InterfaceBuilder, NeighborCache, Routes, SocketSet};
use smoltcp::phy::{wait as phy_wait, Device, Medium};
use smoltcp::socket::tcp;
Expand Down Expand Up @@ -51,33 +44,25 @@ fn main() {
routes.add_default_ipv4_route(default_v4_gw).unwrap();

let medium = device.capabilities().medium;
let mut builder = InterfaceBuilder::new().ip_addrs(ip_addrs).routes(routes);
let builder = InterfaceBuilder::new().ip_addrs(ip_addrs).routes(routes);

#[cfg(feature = "proto-ipv4-fragmentation")]
let mut ipv4_out_packet_cache = [0u8; 1280];
#[cfg(feature = "proto-ipv4-fragmentation")]
{
let ipv4_frag_cache = ReassemblyBuffer::new(vec![], BTreeMap::new());
builder = builder
.ipv4_reassembly_buffer(ipv4_frag_cache)
.ipv4_fragmentation_buffer(&mut ipv4_out_packet_cache[..]);
}
let builder = builder.ipv4_fragmentation_buffer(&mut ipv4_out_packet_cache[..]);

#[cfg(feature = "proto-sixlowpan-fragmentation")]
let mut sixlowpan_out_packet_cache = [0u8; 1280];
#[cfg(feature = "proto-sixlowpan-fragmentation")]
{
let sixlowpan_frag_cache = ReassemblyBuffer::new(vec![], BTreeMap::new());
builder = builder
.sixlowpan_reassembly_buffer(sixlowpan_frag_cache)
.sixlowpan_fragmentation_buffer(&mut sixlowpan_out_packet_cache[..]);
}
let builder = builder.sixlowpan_fragmentation_buffer(&mut sixlowpan_out_packet_cache[..]);

if medium == Medium::Ethernet {
builder = builder
let builder = if medium == Medium::Ethernet {
builder
.hardware_addr(ethernet_addr.into())
.neighbor_cache(neighbor_cache);
}
.neighbor_cache(neighbor_cache)
} else {
builder
};
let mut iface = builder.finalize(&mut device);

let mut sockets = SocketSet::new(vec![]);
Expand All @@ -91,12 +76,7 @@ fn main() {
let mut tcp_active = false;
loop {
let timestamp = Instant::now();
match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(timestamp, &mut device, &mut sockets);

let socket = sockets.get_mut::<tcp::Socket>(tcp_handle);
if socket.is_active() && !tcp_active {
Expand Down
4 changes: 1 addition & 3 deletions examples/dhcp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ fn main() {

loop {
let timestamp = Instant::now();
if let Err(e) = iface.poll(timestamp, &mut device, &mut sockets) {
debug!("poll error: {}", e);
}
iface.poll(timestamp, &mut device, &mut sockets);

let event = sockets.get_mut::<dhcpv4::Socket>(dhcp_handle).poll();
match event {
Expand Down
7 changes: 1 addition & 6 deletions examples/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ fn main() {
let timestamp = Instant::now();
debug!("timestamp {:?}", timestamp);

match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(timestamp, &mut device, &mut sockets);

match sockets
.get_mut::<dns::Socket>(dns_handle)
Expand Down
7 changes: 1 addition & 6 deletions examples/httpclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ fn main() {

loop {
let timestamp = Instant::now();
match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(timestamp, &mut device, &mut sockets);

let socket = sockets.get_mut::<tcp::Socket>(tcp_handle);
let cx = iface.context();
Expand Down
7 changes: 1 addition & 6 deletions examples/loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,7 @@ fn main() {
let mut did_connect = false;
let mut done = false;
while !done && clock.elapsed() < Instant::from_millis(10_000) {
match iface.poll(clock.elapsed(), &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(clock.elapsed(), &mut device, &mut sockets);

let mut socket = sockets.get_mut::<tcp::Socket>(server_handle);
if !socket.is_active() && !socket.is_listening() {
Expand Down
8 changes: 1 addition & 7 deletions examples/multicast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
mod utils;

use log::debug;
use std::os::unix::io::AsRawFd;

use smoltcp::iface::{InterfaceBuilder, NeighborCache, SocketSet};
Expand Down Expand Up @@ -70,12 +69,7 @@ fn main() {

loop {
let timestamp = Instant::now();
match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(timestamp, &mut device, &mut sockets);

let socket = sockets.get_mut::<raw::Socket>(raw_handle);

Expand Down
8 changes: 1 addition & 7 deletions examples/ping.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod utils;

use byteorder::{ByteOrder, NetworkEndian};
use log::debug;
use smoltcp::iface::SocketSet;
use std::cmp;
use std::collections::HashMap;
Expand Down Expand Up @@ -150,12 +149,7 @@ fn main() {

loop {
let timestamp = Instant::now();
match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(timestamp, &mut device, &mut sockets);

let timestamp = Instant::now();
let socket = sockets.get_mut::<icmp::Socket>(icmp_handle);
Expand Down
27 changes: 3 additions & 24 deletions examples/server.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
mod utils;

use log::debug;
use std::collections::BTreeMap;
use std::fmt::Write;
use std::os::unix::io::AsRawFd;

#[cfg(any(
feature = "proto-sixlowpan-fragmentation",
feature = "proto-ipv4-fragmentation"
))]
use smoltcp::iface::ReassemblyBuffer;
use smoltcp::iface::{InterfaceBuilder, NeighborCache, SocketSet};
use smoltcp::phy::{wait as phy_wait, Device, Medium};
use smoltcp::socket::{tcp, udp};
Expand Down Expand Up @@ -82,22 +76,12 @@ fn main() {
#[cfg(feature = "proto-ipv4-fragmentation")]
let mut ipv4_out_packet_cache = [0u8; 10_000];
#[cfg(feature = "proto-ipv4-fragmentation")]
{
let ipv4_frag_cache = ReassemblyBuffer::new(vec![], BTreeMap::new());
builder = builder
.ipv4_reassembly_buffer(ipv4_frag_cache)
.ipv4_fragmentation_buffer(&mut ipv4_out_packet_cache[..]);
}
let builder = builder.ipv4_fragmentation_buffer(&mut ipv4_out_packet_cache[..]);

#[cfg(feature = "proto-sixlowpan-fragmentation")]
let mut sixlowpan_out_packet_cache = [0u8; 1280];
#[cfg(feature = "proto-sixlowpan-fragmentation")]
{
let sixlowpan_frag_cache = ReassemblyBuffer::new(vec![], BTreeMap::new());
builder = builder
.sixlowpan_reassembly_buffer(sixlowpan_frag_cache)
.sixlowpan_fragmentation_buffer(&mut sixlowpan_out_packet_cache[..]);
}
let mut builder = builder.sixlowpan_fragmentation_buffer(&mut sixlowpan_out_packet_cache[..]);

if medium == Medium::Ethernet {
builder = builder
Expand All @@ -116,12 +100,7 @@ fn main() {
let mut tcp_6970_active = false;
loop {
let timestamp = Instant::now();
match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(timestamp, &mut device, &mut sockets);

// udp:6969: respond "hello"
let socket = sockets.get_mut::<udp::Socket>(udp_handle);
Expand Down
26 changes: 3 additions & 23 deletions examples/sixlowpan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@
mod utils;

use log::debug;
use std::collections::BTreeMap;
use std::os::unix::io::AsRawFd;
use std::str;

use smoltcp::iface::{InterfaceBuilder, NeighborCache, ReassemblyBuffer, SocketSet};
use smoltcp::iface::{InterfaceBuilder, NeighborCache, SocketSet};
use smoltcp::phy::{wait as phy_wait, Medium, RawSocket};
use smoltcp::socket::tcp;
use smoltcp::socket::udp;
Expand Down Expand Up @@ -96,20 +95,11 @@ fn main() {
.hardware_addr(ieee802154_addr.into())
.neighbor_cache(neighbor_cache);

#[cfg(feature = "proto-ipv4-fragmentation")]
{
let ipv4_frag_cache = ReassemblyBuffer::new(vec![], BTreeMap::new());
builder = builder.ipv4_reassembly_buffer(ipv4_frag_cache);
}

#[cfg(feature = "proto-sixlowpan-fragmentation")]
let mut out_packet_buffer = [0u8; 1280];
#[cfg(feature = "proto-sixlowpan-fragmentation")]
{
let sixlowpan_frag_cache = ReassemblyBuffer::new(vec![], BTreeMap::new());
builder = builder
.sixlowpan_reassembly_buffer(sixlowpan_frag_cache)
.sixlowpan_fragmentation_buffer(&mut out_packet_buffer[..]);
builder = builder.sixlowpan_fragmentation_buffer(&mut out_packet_buffer[..]);
}

let mut iface = builder.finalize(&mut device);
Expand All @@ -125,17 +115,7 @@ fn main() {

loop {
let timestamp = Instant::now();

let mut poll = true;
while poll {
match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(r) => poll = r,
Err(e) => {
debug!("poll error: {}", e);
break;
}
}
}
iface.poll(timestamp, &mut device, &mut sockets);

// udp:6969: respond "hello"
let socket = sockets.get_mut::<udp::Socket>(udp_handle);
Expand Down
14 changes: 2 additions & 12 deletions examples/sixlowpan_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@

mod utils;

use log::debug;
use std::collections::BTreeMap;
use std::os::unix::io::AsRawFd;
use std::str;

use smoltcp::iface::{InterfaceBuilder, NeighborCache, ReassemblyBuffer, SocketSet};
use smoltcp::iface::{InterfaceBuilder, NeighborCache, SocketSet};
use smoltcp::phy::{wait as phy_wait, Medium, RawSocket};
use smoltcp::socket::tcp;
use smoltcp::wire::{Ieee802154Pan, IpAddress, IpCidr};
Expand Down Expand Up @@ -169,15 +167,12 @@ fn main() {
))
.unwrap();

let cache = ReassemblyBuffer::new(vec![], BTreeMap::new());

let mut builder = InterfaceBuilder::new()
.ip_addrs(ip_addrs)
.pan_id(Ieee802154Pan(0xbeef));
builder = builder
.hardware_addr(ieee802154_addr.into())
.neighbor_cache(neighbor_cache)
.sixlowpan_reassembly_buffer(cache)
.sixlowpan_fragmentation_buffer(vec![]);
let mut iface = builder.finalize(&mut device);

Expand All @@ -192,12 +187,7 @@ fn main() {

while !CLIENT_DONE.load(Ordering::SeqCst) {
let timestamp = Instant::now();
match iface.poll(timestamp, &mut device, &mut sockets) {
Ok(_) => {}
Err(e) => {
debug!("poll error: {}", e);
}
}
iface.poll(timestamp, &mut device, &mut sockets);

// tcp:1234: emit data
let socket = sockets.get_mut::<tcp::Socket>(tcp1_handle);
Expand Down
17 changes: 7 additions & 10 deletions examples/tcpdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ fn main() {
let mut socket = RawSocket::new(ifname.as_ref(), smoltcp::phy::Medium::Ethernet).unwrap();
loop {
phy_wait(socket.as_raw_fd(), None).unwrap();
let (rx_token, _) = socket.receive().unwrap();
rx_token
.consume(Instant::now(), |buffer| {
println!(
"{}",
PrettyPrinter::<EthernetFrame<&[u8]>>::new("", &buffer)
);
Ok(())
})
.unwrap();
let (rx_token, _) = socket.receive(Instant::now()).unwrap();
rx_token.consume(|buffer| {
println!(
"{}",
PrettyPrinter::<EthernetFrame<&[u8]>>::new("", &buffer)
);
})
}
}

0 comments on commit 1f7e1fa

Please sign in to comment.