Skip to content

Commit

Permalink
Fetch local ip and netmask automatically in VSomeIpServiceConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Ax9DTW committed Aug 1, 2024
1 parent 2771fe2 commit 8116bfd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/smip_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ vsomeip_compat = {path = "../vsomeip_compat"}
serde = "1"
bincode = "1"
anyhow = "1"
netdev = "0.25"
thiserror = "1"

[features]
Expand Down
22 changes: 1 addition & 21 deletions crates/smip_core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
1 change: 1 addition & 0 deletions crates/vsomeip_compat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ edition = "2021"
tempfile = "3"
vsomeip-rs = {path = "../vsomeip-rs"}
serde_json = "1"
netdev = "0.25"

[dev-dependencies]
50 changes: 36 additions & 14 deletions crates/vsomeip_compat/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -9,8 +9,8 @@ use tempfile::NamedTempFile;
pub struct VsomeIpConfig {
pub app_id: (String, u16),
pub services: Vec<VSomeIpServiceConfig>,
pub addr: IpAddr,
pub netmask: IpAddr,
pub addr: Option<IpAddr>,
pub netmask: Option<IpAddr>,
pub addr_mode: AddressingMode,
pub service_discovery: bool,
pub instance_id: InstanceId,
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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,
Expand Down Expand Up @@ -163,6 +183,8 @@ pub fn set_vsomeip_config(config: &str) {

#[cfg(test)]
mod tests {
use std::net::Ipv4Addr;

use super::*;

#[test]
Expand All @@ -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
};
Expand Down Expand Up @@ -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
};
Expand Down
13 changes: 1 addition & 12 deletions examples/dashboard_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::net::{IpAddr, Ipv4Addr};
use std::error::Error;

use smip::Client;
Expand All @@ -17,17 +16,7 @@ fn main() -> Result<(), Box<dyn Error>>{
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)?;

Expand Down
5 changes: 1 addition & 4 deletions examples/simple_client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::net::{IpAddr, Ipv4Addr};
use std::error::Error;

use smip::Client;
Expand All @@ -13,9 +12,7 @@ fn main() -> Result<(), Box<dyn Error>>{
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)?;
Expand Down

0 comments on commit 8116bfd

Please sign in to comment.