command_parser, main: implement ShowCommand::Ipv4

Fixes Gitea issue #30
pull/36/head
Astro 2020-12-20 20:44:10 +01:00
parent 22b0c9fcad
commit 9852b32646
2 changed files with 37 additions and 19 deletions

View File

@ -100,6 +100,7 @@ pub enum ShowCommand {
Pid, Pid,
SteinhartHart, SteinhartHart,
PostFilter, PostFilter,
Ipv4,
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -494,27 +495,32 @@ fn ipv4_addr(input: &[u8]) -> IResult<&[u8], Result<[u8; 4], Error>> {
fn ipv4(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> { fn ipv4(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, _) = tag("ipv4")(input)?; let (input, _) = tag("ipv4")(input)?;
let (input, _) = whitespace(input)?; alt((
let (input, address) = ipv4_addr(input)?;
let (input, _) = tag("/")(input)?;
let (input, mask_len) = unsigned(input)?;
let (input, gateway) = alt((
|input| { |input| {
let (input, _) = whitespace(input)?; let (input, _) = whitespace(input)?;
let (input, gateway) = ipv4_addr(input)?; let (input, address) = ipv4_addr(input)?;
Ok((input, gateway.map(Some))) let (input, _) = tag("/")(input)?;
}, let (input, mask_len) = unsigned(input)?;
value(Ok(None), end), let (input, gateway) = alt((
))(input)?; |input| {
let (input, _) = whitespace(input)?;
let (input, gateway) = ipv4_addr(input)?;
Ok((input, gateway.map(Some)))
},
value(Ok(None), end),
))(input)?;
let result = move || { let result = move || {
Ok(Command::Ipv4(Ipv4Config { Ok(Command::Ipv4(Ipv4Config {
address: address?, address: address?,
mask_len: mask_len? as u8, mask_len: mask_len? as u8,
gateway: gateway?, gateway: gateway?,
})) }))
}; };
Ok((input, result())) Ok((input, result()))
},
value(Ok(Command::Show(ShowCommand::Ipv4)), end),
))(input)
} }
fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> { fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
@ -579,6 +585,12 @@ mod test {
assert_eq!(command, Ok(Command::Save { channel: Some(0) })); assert_eq!(command, Ok(Command::Save { channel: Some(0) }));
} }
#[test]
fn parse_show_ipv4() {
let command = Command::parse(b"ipv4");
assert_eq!(command, Ok(Command::Show(ShowCommand::Ipv4)));
}
#[test] #[test]
fn parse_ipv4() { fn parse_ipv4() {
let command = Command::parse(b"ipv4 192.168.1.26/24"); let command = Command::parse(b"ipv4 192.168.1.26/24");

View File

@ -183,7 +183,7 @@ fn main() -> ! {
let hwaddr = EthernetAddress(eui48); let hwaddr = EthernetAddress(eui48);
info!("EEPROM MAC address: {}", hwaddr); info!("EEPROM MAC address: {}", hwaddr);
net::run(clocks, dp.ETHERNET_MAC, dp.ETHERNET_DMA, eth_pins, hwaddr, ipv4_config, |iface| { net::run(clocks, dp.ETHERNET_MAC, dp.ETHERNET_DMA, eth_pins, hwaddr, ipv4_config.clone(), |iface| {
Server::<Session>::run(iface, |server| { Server::<Session>::run(iface, |server| {
leds.r1.off(); leds.r1.off();
@ -280,6 +280,12 @@ fn main() -> ! {
} }
} }
} }
Command::Show(ShowCommand::Ipv4) => {
let (cidr, gateway) = net::split_ipv4_config(ipv4_config.clone());
let _ = write!(socket, "{{\"addr\":\"{}\"", cidr);
gateway.map(|gateway| write!(socket, ",\"gateway\":\"{}\"", gateway));
let _ = writeln!(socket, "}}");
}
Command::PwmPid { channel } => { Command::PwmPid { channel } => {
channels.channel_state(channel).pid_engaged = true; channels.channel_state(channel).pid_engaged = true;
leds.g3.on(); leds.g3.on();