Skip to content

Commit

Permalink
sntpc: example: Update smoltcp example to use the latest crate version
Browse files Browse the repository at this point in the history
  • Loading branch information
vpetrigo committed May 9, 2023
1 parent d11efb1 commit 566a5b5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ no-std-net = "~0.6"

[dev-dependencies]
simple_logger = { version = "~1.13" }
smoltcp = { version = "~0.8", default-features = false, features = ["phy-tuntap_interface", "socket-udp", "proto-ipv4"] }
smoltcp = { version = "~0.9", default-features = false, features = ["phy-tuntap_interface", "socket-udp", "proto-ipv4"] }
clap = { version = "2.33", default-features = false }

[badges]
Expand Down
165 changes: 79 additions & 86 deletions examples/smoltcp_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ use core::default::Default;
use core::fmt::Debug;
use core::str::FromStr;

use std::collections::BTreeMap;
use std::fmt::Formatter;
use std::net::{IpAddr, SocketAddr, ToSocketAddrs};
use std::os::unix::prelude::AsRawFd;

use smoltcp::iface::{EthernetInterfaceBuilder, NeighborCache, Routes};
use smoltcp::phy::wait;
use smoltcp::phy::TapInterface;
use smoltcp::socket::{SocketRef, SocketSet, UdpSocket, UdpSocketBuffer};
use smoltcp::iface::{Config, Interface, SocketSet};
use smoltcp::phy::TunTapInterface;
use smoltcp::phy::{wait, Medium};
// use smoltcp::socket::{SocketRef, SocketSet, UdpSocket, UdpSocketBuffer};
use smoltcp::socket::udp;
use smoltcp::storage::PacketMetadata;
use smoltcp::time::Instant;
use smoltcp::wire::{
EthernetAddress, IpAddress, IpCidr, IpEndpoint, Ipv4Address,
};

use clap::{crate_version, App, Arg};
use clap::{crate_version, App, Arg, ArgMatches};

use sntpc::{self, Error, NtpContext, NtpTimestampGenerator, NtpUdpSocket};

Expand Down Expand Up @@ -125,18 +125,18 @@ impl Default for Buffers {
}

struct UdpSocketBuffers<'a> {
rx: UdpSocketBuffer<'a>,
tx: UdpSocketBuffer<'a>,
rx: udp::PacketBuffer<'a>,
tx: udp::PacketBuffer<'a>,
}

impl<'a> UdpSocketBuffers<'a> {
fn new(buffers: &'a mut Buffers) -> Self {
UdpSocketBuffers {
rx: UdpSocketBuffer::new(
rx: udp::PacketBuffer::new(
buffers.rx_meta.as_mut(),
buffers.rx_buffer.as_mut(),
),
tx: UdpSocketBuffer::new(
tx: udp::PacketBuffer::new(
buffers.tx_meta.as_mut(),
buffers.tx_buffer.as_mut(),
),
Expand Down Expand Up @@ -166,7 +166,7 @@ impl NtpTimestampGenerator for StdTimestampGen {
}

struct SmoltcpUdpSocketWrapper<'a, 'b> {
socket: RefCell<SocketRef<'b, UdpSocket<'a>>>,
socket: RefCell<&'b mut udp::Socket<'a>>,
}

impl<'a, 'b> Debug for SmoltcpUdpSocketWrapper<'a, 'b> {
Expand Down Expand Up @@ -195,9 +195,7 @@ impl<'a, 'b> NtpUdpSocket for SmoltcpUdpSocketWrapper<'a, 'b> {
SocketAddr::V6(_) => return Err(Error::Network),
};

if let Ok(_) =
self.socket.borrow_mut().send_slice(&buf[..], endpoint)
{
if self.socket.borrow_mut().send_slice(buf, endpoint).is_ok() {
return Ok(buf.len());
}
}
Expand All @@ -211,12 +209,11 @@ impl<'a, 'b> NtpUdpSocket for SmoltcpUdpSocketWrapper<'a, 'b> {
if let Ok((size, address)) = result {
let sockaddr = match address.addr {
IpAddress::Ipv4(v4) => SocketAddr::new(
std::net::IpAddr::V4(std::net::Ipv4Addr::new(
IpAddr::V4(std::net::Ipv4Addr::new(
v4.0[0], v4.0[1], v4.0[2], v4.0[3],
)),
address.port,
),
_ => return Err(Error::Network),
};

return Ok((size, sockaddr));
Expand All @@ -226,16 +223,11 @@ impl<'a, 'b> NtpUdpSocket for SmoltcpUdpSocketWrapper<'a, 'b> {
}
}

fn main() {
#[cfg(feature = "log")]
if cfg!(feature = "log") {
simple_logger::init_with_level(log::Level::Trace).unwrap();
}

const GOOGLE_NTP_ADDR: &str = "time.google.com";
fn create_app_cli<'a>() -> ArgMatches<'a> {
const GOOGLE_NTP_ADDR: &str = "pool.ntp.org";
const APP_PORT: &str = "6666";

let app = App::new("smoltcp_request")
App::new("smoltcp_request")
.version(crate_version!())
.arg(
Arg::with_name("server")
Expand Down Expand Up @@ -290,11 +282,19 @@ fn main() {
.default_value(APP_PORT)
.help("Device port to bind UDP socket to"),
)
.get_matches();
.get_matches()
}

fn main() {
#[cfg(feature = "log")]
if cfg!(feature = "log") {
simple_logger::init_with_level(log::Level::Trace).unwrap();
}

let app = create_app_cli();
let interface_name = app.value_of("interface").unwrap();
let tuntap =
TapInterface::new(interface_name).expect("Cannot create TAP interface");
let mut tuntap = TunTapInterface::new(interface_name, Medium::Ethernet)
.expect("Cannot create TAP interface");

let server_ip = app.value_of("server").unwrap();
let server_port = u16::from_str(app.value_of("port").unwrap())
Expand All @@ -313,87 +313,80 @@ fn main() {
let mut buffer = Buffers::default();
let udp_buffer = UdpSocketBuffers::new(&mut buffer);

let mut socket = UdpSocket::new(udp_buffer.rx, udp_buffer.tx);
let mut socket = udp::Socket::new(udp_buffer.rx, udp_buffer.tx);
socket.bind(sock_port).unwrap();
let ip_addrs = [ip_addr];
let mut routes_storage = [None; 3];
let mut routes = Routes::new(&mut routes_storage[..]);
routes.add_default_ipv4_route(default_gw.into()).unwrap();
let neighbor_cache = NeighborCache::new(BTreeMap::new());

let mut iface = EthernetInterfaceBuilder::new(tuntap)
.ethernet_addr(eth_address)
.neighbor_cache(neighbor_cache)
.ip_addrs(ip_addrs)
.routes(routes)
.finalize();

let mut socket_items = [None; 1];
let mut sockets = SocketSet::new(socket_items.as_mut());
let mut config = Config::new();

config.random_seed = 0;
config.hardware_addr = Some(eth_address.into());

let mut iface = Interface::new(config, &mut tuntap);
iface.update_ip_addrs(|ip_addrs| ip_addrs.push(ip_addr).unwrap());
iface
.routes_mut()
.add_default_ipv4_route(default_gw)
.unwrap();

// let mut socket_items = [None; 1];
let mut sockets = SocketSet::new(vec![]);
let udp_handle = sockets.add(socket);
let mut once_tx = true;
let mut once_rx = true;
let mut send_result = Option::None;
let mut send_result = None;

while once_rx {
let timestamp = Instant::now();

match iface.poll(&mut sockets, timestamp) {
Ok(_) => {
#[cfg(feature = "log")]
log::trace!("Poll ok!");
}
Err(_e) => {
#[cfg(feature = "log")]
log::trace!("Poll error: {}!", _e);
}
if iface.poll(timestamp, &mut tuntap, &mut sockets) {
#[cfg(feature = "log")]
log::trace!("Poll ok!");
}

{
if once_tx && sockets.get::<UdpSocket>(udp_handle).can_send() {
once_tx = false;
let sock_wrapper = SmoltcpUdpSocketWrapper {
socket: RefCell::new(sockets.get::<UdpSocket>(udp_handle)),
};
let context = NtpContext::new(StdTimestampGen::default());
let result = sntpc::sntp_send_request(
server_sock_addr,
&sock_wrapper,
context,
);

match result {
Ok(result) => {
send_result = Some(result);
}
Err(_e) => {
#[cfg(feature = "log")]
log::error!("send error: {:?}", _e);
once_tx = true;
}
if once_tx && sockets.get::<udp::Socket>(udp_handle).can_send() {
once_tx = false;
let sock_wrapper = SmoltcpUdpSocketWrapper {
socket: RefCell::new(
sockets.get_mut::<udp::Socket>(udp_handle),
),
};
let context = NtpContext::new(StdTimestampGen::default());
let result = sntpc::sntp_send_request(
server_sock_addr,
&sock_wrapper,
context,
);

match result {
Ok(result) => {
send_result = Some(result);
}
Err(_e) => {
#[cfg(feature = "log")]
log::error!("send error: {:?}", _e);
once_tx = true;
}

#[cfg(feature = "log")]
log::trace!("{:?}", &result);
}

if once_rx
&& sockets.get::<UdpSocket>(udp_handle).can_recv()
&& send_result.is_some()
{
#[cfg(feature = "log")]
log::trace!("{:?}", &result);
}

if let Some(tx_result) = send_result {
if once_rx && sockets.get::<udp::Socket>(udp_handle).can_recv() {
once_rx = false;

let sock_wrapper = SmoltcpUdpSocketWrapper {
socket: RefCell::new(sockets.get::<UdpSocket>(udp_handle)),
socket: RefCell::new(
sockets.get_mut::<udp::Socket>(udp_handle),
),
};
let context = NtpContext::new(StdTimestampGen::default());

let _result = sntpc::sntp_process_response(
server_sock_addr,
&sock_wrapper,
context,
send_result.unwrap(),
tx_result,
);

#[cfg(feature = "log")]
Expand All @@ -402,8 +395,8 @@ fn main() {
}

wait(
iface.device().as_raw_fd(),
iface.poll_delay(&sockets, Instant::from_secs(1)),
tuntap.as_raw_fd(),
iface.poll_delay(Instant::from_secs(1), &sockets),
)
.unwrap();
}
Expand Down

0 comments on commit 566a5b5

Please sign in to comment.