ethernet: handle exit

This commit is contained in:
occheung 2020-09-02 17:23:03 +08:00
parent 3b83b6560f
commit 171ce942c3
1 changed files with 20 additions and 4 deletions

View File

@ -52,8 +52,8 @@ use core::mem::uninitialized;
// Exception: no phy::wait // Exception: no phy::wait
//use smoltcp::phy::wait as phy_wait; //use smoltcp::phy::wait as phy_wait;
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder}; use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder, Routes};
use smoltcp::socket::SocketSet; use smoltcp::socket::SocketSet;
use smoltcp::socket::{SocketHandle, TcpSocket, TcpSocketBuffer}; use smoltcp::socket::{SocketHandle, TcpSocket, TcpSocketBuffer};
use smoltcp::time::{Duration, Instant}; use smoltcp::time::{Duration, Instant};
@ -259,11 +259,18 @@ fn main() -> ! {
let mut neighbor_storage = [None; 16]; let mut neighbor_storage = [None; 16];
let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]); let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]);
// Routes
let default_v4_gw = Ipv4Address::new(192, 168, 1, 1);
let mut routes_storage = [None; 8];
let mut routes = Routes::new(&mut routes_storage[..]);
routes.add_default_ipv4_route(default_v4_gw).unwrap();
// Device? _eth_dma, as it implements phy::device // Device? _eth_dma, as it implements phy::device
let mut iface = EthernetInterfaceBuilder::new(_eth_dma) let mut iface = EthernetInterfaceBuilder::new(_eth_dma)
.ethernet_addr(mac_addr) .ethernet_addr(mac_addr)
.neighbor_cache(neighbor_cache) .neighbor_cache(neighbor_cache)
.ip_addrs(&mut ip_addrs[..]) .ip_addrs(&mut ip_addrs[..])
.routes(routes)
.finalize(); .finalize();
// SCPI configs // SCPI configs
@ -346,9 +353,18 @@ fn main() -> ! {
} }
if socket.can_recv() { if socket.can_recv() {
let result = context.run(socket.recv(|buffer| { // let result = context.run(socket.recv(|buffer| {
// (buffer.len(), buffer)
// }).unwrap(), &mut buf);
let mut data = socket.recv(|buffer| {
(buffer.len(), buffer) (buffer.len(), buffer)
}).unwrap(), &mut buf); }).unwrap();
if str::from_utf8(data).unwrap().trim() == "quit" {
socket.close();
socket.abort();
continue;
}
let result = context.run(data, &mut buf);
if let Err(err) = result { if let Err(err) = result {
writeln!(socket, "{}", str::from_utf8(err.get_message()).unwrap()); writeln!(socket, "{}", str::from_utf8(err.get_message()).unwrap());
} else { } else {