From b77bae1d1a29d557417f75017f95a6166d9f763a Mon Sep 17 00:00:00 2001 From: Fyor Klein Gunnewiek Date: Wed, 5 Jul 2023 11:18:29 +0200 Subject: [PATCH] Change ip for last two octets (#83) Co-authored-by: Gijs de Jong Co-authored-by: Gijs de Jong <14833076+oxkitsune@users.noreply.github.com> Co-authored-by: Mark Honkoop <112875887+MarkHonkoop@users.noreply.github.com> --- crates/nao/src/lib.rs | 14 +++++++++++++ tools/pepsi/src/change_ip.rs | 39 ++++++++++++++++++++++++++++++++++++ tools/pepsi/src/main.rs | 7 +++++++ 3 files changed, 60 insertions(+) create mode 100644 tools/pepsi/src/change_ip.rs diff --git a/crates/nao/src/lib.rs b/crates/nao/src/lib.rs index fc342d04..7eaa12bb 100644 --- a/crates/nao/src/lib.rs +++ b/crates/nao/src/lib.rs @@ -304,6 +304,19 @@ impl Nao { String::from_utf8(output.stdout).wrap_err("failed to decode UTF-8") } + pub async fn set_last_ip_octets(&self, first_octet: u8, second_octet: u8) -> Result<()> { + self.ssh_to_nao() + .arg("sudo") + .arg("sh") + .arg("~/configure_network") + .arg(first_octet.to_string()) + .arg(second_octet.to_string()) + .spawn() + .wrap_err("failed to execute configure_network ssh command")?; + + Ok(()) + } + pub async fn set_network(&self, network: Network) -> Result<()> { let command_string = [ Network::SplA, @@ -312,6 +325,7 @@ impl Nao { Network::SplD, Network::SplE, Network::SplF, + Network::Dnt5G, ] .into_iter() .map(|possible_network| { diff --git a/tools/pepsi/src/change_ip.rs b/tools/pepsi/src/change_ip.rs new file mode 100644 index 00000000..3756ad3e --- /dev/null +++ b/tools/pepsi/src/change_ip.rs @@ -0,0 +1,39 @@ +use clap::Args; +use color_eyre::Result; + +use nao::Nao; + +use crate::parsers::NaoAddress; + +#[derive(Args)] +pub struct Arguments { + /// The NAO to update the IP address e.g. 10.0.8.42 + #[arg(required = true)] + pub nao: NaoAddress, + + /// The third octet of the new ip address for the robot. + pub first_octet: u8, + + /// The last octet of the new ip address for the robot. + pub second_octet: u8, +} + +pub async fn change_ip(arguments: Arguments) -> Result<()> { + let nao = Nao::new(arguments.nao.ip); + + let result = nao + .set_last_ip_octets(arguments.first_octet, arguments.second_octet) + .await; + + if result.is_ok() { + println!( + "The new IP address is: {}.{}.{}.{}", + arguments.nao.ip.octets()[0], + arguments.nao.ip.octets()[1], + arguments.first_octet, + arguments.second_octet + ); + } + + result +} diff --git a/tools/pepsi/src/main.rs b/tools/pepsi/src/main.rs index 78a88400..fa20e404 100644 --- a/tools/pepsi/src/main.rs +++ b/tools/pepsi/src/main.rs @@ -6,6 +6,7 @@ use color_eyre::{config::HookBuilder, eyre::WrapErr, Result}; use crate::aliveness::{aliveness, Arguments as AlivenessArguments}; use analyze::{analyze, Arguments as AnalyzeArguments}; use cargo::{cargo, Arguments as CargoArguments, Command as CargoCommand}; +use change_ip::{change_ip, Arguments as ChangeIpArguments}; use communication::{communication, Arguments as CommunicationArguments}; use completions::{completions, Arguments as CompletionArguments}; use gammaray::{gammaray, Arguments as GammarayArguments}; @@ -26,6 +27,7 @@ use wireless::{wireless, Arguments as WirelessArguments}; mod aliveness; mod analyze; mod cargo; +mod change_ip; mod communication; mod completions; mod gammaray; @@ -58,6 +60,9 @@ async fn main() -> Result<()> { let repository = repository_root.map(Repository::new); match arguments.command { + Command::ChangeIp(arguments) => change_ip(arguments) + .await + .wrap_err("failed to execute change_ip command")?, Command::Analyze(arguments) => analyze(arguments, &repository?) .await .wrap_err("failed to execute analyze command")?, @@ -138,6 +143,8 @@ struct Arguments { #[derive(Subcommand)] enum Command { + /// Change ip address of NAOs + ChangeIp(ChangeIpArguments), /// Analyze source code #[clap(subcommand)] Analyze(AnalyzeArguments),