From 8116bfd84fddfde7b550ec382cd4c120958d18f7 Mon Sep 17 00:00:00 2001 From: Atri Sarkar Date: Thu, 1 Aug 2024 15:46:37 +0530 Subject: [PATCH] Fetch local ip and netmask automatically in VSomeIpServiceConfig --- Cargo.lock | 2 +- crates/smip_core/Cargo.toml | 1 - crates/smip_core/src/runtime.rs | 22 +------------- crates/vsomeip_compat/Cargo.toml | 1 + crates/vsomeip_compat/src/lib.rs | 50 +++++++++++++++++++++++--------- examples/dashboard_client.rs | 13 +-------- examples/simple_client.rs | 5 +--- 7 files changed, 41 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2869ed1..74ccf78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1186,7 +1186,6 @@ version = "0.1.0" dependencies = [ "anyhow", "bincode", - "netdev", "parking_lot", "serde", "someip_types", @@ -1466,6 +1465,7 @@ dependencies = [ name = "vsomeip_compat" version = "0.1.0" dependencies = [ + "netdev", "serde_json", "tempfile", "vsomeip-rs", diff --git a/crates/smip_core/Cargo.toml b/crates/smip_core/Cargo.toml index d9a35c0..613b814 100644 --- a/crates/smip_core/Cargo.toml +++ b/crates/smip_core/Cargo.toml @@ -13,7 +13,6 @@ vsomeip_compat = {path = "../vsomeip_compat"} serde = "1" bincode = "1" anyhow = "1" -netdev = "0.25" thiserror = "1" [features] diff --git a/crates/smip_core/src/runtime.rs b/crates/smip_core/src/runtime.rs index 6a1f34d..7e09ab2 100644 --- a/crates/smip_core/src/runtime.rs +++ b/crates/smip_core/src/runtime.rs @@ -130,27 +130,7 @@ impl Runtime { self } - fn set_addr(&mut self) -> Result<(), VSomeIpError> { - - if self.config.addr.is_some() && self.config.netmask.is_some() { - return Ok(()); - } - - let default_iface = netdev::get_default_interface().expect("No default network interface found, please provide IP and netmask"); - - if self.config.addr.is_none() { - self.vsomeip_config.addr = default_iface.ipv4[0].addr.into(); - } - - if self.config.netmask.is_none() { - self.vsomeip_config.netmask = default_iface.ipv4[0].netmask().into(); - } - - Ok(()) - } - pub fn run(mut self) -> Result<(), VSomeIpError> { - self.set_addr()?; - + pub fn run(self) -> Result<(), VSomeIpError> { let config_str = self.vsomeip_config.build(); let app = vsomeip_rs::Runtime::get().create_application_with(self.config.name, |_app| { diff --git a/crates/vsomeip_compat/Cargo.toml b/crates/vsomeip_compat/Cargo.toml index 80c91ed..61f918c 100644 --- a/crates/vsomeip_compat/Cargo.toml +++ b/crates/vsomeip_compat/Cargo.toml @@ -10,5 +10,6 @@ edition = "2021" tempfile = "3" vsomeip-rs = {path = "../vsomeip-rs"} serde_json = "1" +netdev = "0.25" [dev-dependencies] \ No newline at end of file diff --git a/crates/vsomeip_compat/src/lib.rs b/crates/vsomeip_compat/src/lib.rs index ff6012e..2e3cb6f 100644 --- a/crates/vsomeip_compat/src/lib.rs +++ b/crates/vsomeip_compat/src/lib.rs @@ -1,5 +1,5 @@ -use std::{io::Write, net::{IpAddr, Ipv4Addr}}; +use std::{io::Write, net::IpAddr}; use serde_json::json; use vsomeip_rs::{InstanceId, MajorVersion, MinorVersion, ServiceId}; @@ -9,8 +9,8 @@ use tempfile::NamedTempFile; pub struct VsomeIpConfig { pub app_id: (String, u16), pub services: Vec, - pub addr: IpAddr, - pub netmask: IpAddr, + pub addr: Option, + pub netmask: Option, pub addr_mode: AddressingMode, pub service_discovery: bool, pub instance_id: InstanceId, @@ -24,8 +24,8 @@ impl VsomeIpConfig { services: vec![], service_discovery: false, addr_mode: AddressingMode::Unicast, - netmask: IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0)), - addr: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), + netmask: None, + addr: None, instance_id: 0, routing: None } @@ -47,12 +47,12 @@ impl VsomeIpConfig { } pub fn addr(mut self, addr: IpAddr) -> Self { - self.addr = addr; + self.addr = Some(addr); self } pub fn netmask(mut self, netmask: IpAddr) -> Self { - self.netmask = netmask; + self.netmask = Some(netmask); self } @@ -67,12 +67,32 @@ impl VsomeIpConfig { AddressingMode::Multicast => "multicast".into(), } } - pub fn build(self) -> String { + fn set_addr(&mut self) { + if self.addr.is_some() && self.netmask.is_some() { + return; + } + + let default_iface = netdev::get_default_interface().expect("No default network interface found, please provide IP and netmask"); + + if self.addr.is_none() { + self.addr = Some(default_iface.ipv4[0].addr.into()); + } + + if self.netmask.is_none() { + self.netmask = Some(default_iface.ipv4[0].netmask().into()); + } + } + pub fn build(mut self) -> String { let addr_mode = self.build_addr_mode(); let addr_mode = addr_mode.as_str(); + + self.set_addr(); + let addr = self.addr.unwrap(); + let netmask = self.netmask.unwrap(); + let mut json = json!({ - addr_mode: self.addr, - "netmask": self.netmask, + addr_mode: addr, + "netmask": netmask, "logging": { "level": "debug", "console": true, @@ -163,6 +183,8 @@ pub fn set_vsomeip_config(config: &str) { #[cfg(test)] mod tests { + use std::net::Ipv4Addr; + use super::*; #[test] @@ -178,8 +200,8 @@ mod tests { ], service_discovery: false, addr_mode: AddressingMode::Unicast, - netmask: IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0)), - addr: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 23)), + netmask: Some(IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0))), + addr: Some(IpAddr::V4(Ipv4Addr::new(192, 168, 0, 23))), instance_id: 3, routing: None }; @@ -232,8 +254,8 @@ mod tests { ], service_discovery: true, addr_mode: AddressingMode::Unicast, - netmask: IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0)), - addr: IpAddr::V4(Ipv4Addr::new(192, 168, 0, 23)), + netmask: Some(IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0))), + addr: Some(IpAddr::V4(Ipv4Addr::new(192, 168,0 ,23))), instance_id: 3, routing: None }; diff --git a/examples/dashboard_client.rs b/examples/dashboard_client.rs index 7266aa4..4e55200 100644 --- a/examples/dashboard_client.rs +++ b/examples/dashboard_client.rs @@ -1,4 +1,3 @@ -use std::net::{IpAddr, Ipv4Addr}; use std::error::Error; use smip::Client; @@ -17,17 +16,7 @@ fn main() -> Result<(), Box>{ id: service_id, conn_type: smip::ConnectionType::Udp(30509), ..Default::default() - }) - // .service( - // smip::VSomeIpServiceConfig { - // id: service_id, - // conn_type: smip::ConnectionType::Udp(30509), - - // } - // ) - .netmask(IpAddr::V4(Ipv4Addr::new(255, 255, 240, 0))) - .addr(IpAddr::V4(Ipv4Addr::new(172, 31, 43, 55))); - + }); let client = Client::new(&vsomeip_config)?; diff --git a/examples/simple_client.rs b/examples/simple_client.rs index 0fd2bf3..2643070 100644 --- a/examples/simple_client.rs +++ b/examples/simple_client.rs @@ -1,4 +1,3 @@ -use std::net::{IpAddr, Ipv4Addr}; use std::error::Error; use smip::Client; @@ -13,9 +12,7 @@ fn main() -> Result<(), Box>{ conn_type: smip::ConnectionType::Udp(30509), major_version: 1, minor_version: 0 - }) - .netmask(IpAddr::V4(Ipv4Addr::new(255, 255, 240, 0))) - .addr(IpAddr::V4(Ipv4Addr::new(172, 31, 43, 55))); + }); let client = Client::new(&vsomeip_config)?;