Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support '--ros-args -r' for __node and __ns #58

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions zenoh-bridge-ros2dds/src/bridge_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ pub struct BridgeArgs {
/// reports as error log any stalled status during the specified period [default: 1.0 second]
#[arg(short, long, value_name = "FLOAT", default_missing_value = "1.0")]
pub watchdog: Option<Option<f32>>,

/// ROS command line arguments as specified in https://design.ros2.org/articles/ros_command_line_arguments.html
/// Supported capabilities:
/// -r, --remap <from:=to> : remapping is supported only for '__ns' and '__node'
#[arg(
long,
value_name = " list of ROS args until '--' ",
verbatim_doc_comment
)]
pub ros_args: (),
}

impl From<BridgeArgs> for Config {
Expand Down Expand Up @@ -119,7 +129,7 @@ impl From<&BridgeArgs> for Config {
}
}

fn insert_json5<T>(config: &mut Config, key: &str, value: &T)
pub(crate) fn insert_json5<T>(config: &mut Config, key: &str, value: &T)
where
T: Sized + serde::Serialize,
{
Expand All @@ -128,7 +138,7 @@ where
.unwrap();
}

fn insert_json5_option<T>(config: &mut Config, key: &str, value: &Option<T>)
pub(crate) fn insert_json5_option<T>(config: &mut Config, key: &str, value: &Option<T>)
where
T: Sized + serde::Serialize,
{
Expand All @@ -139,7 +149,7 @@ where
}
}

fn insert_json5_list<T>(config: &mut Config, key: &str, values: &Vec<T>)
pub(crate) fn insert_json5_list<T>(config: &mut Config, key: &str, values: &Vec<T>)
where
T: Sized + serde::Serialize,
{
Expand Down
31 changes: 29 additions & 2 deletions zenoh-bridge-ros2dds/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,42 @@
use async_liveliness_monitor::LivelinessMonitor;
use bridge_args::BridgeArgs;
use clap::Parser;
use ros_args::RosArgs;
use std::time::{Duration, SystemTime};
use zenoh::config::Config;

mod bridge_args;
mod ros_args;
mod zenoh_args;

const ROS_ARG_START_FLAG: &str = "--ros-args";
const ROS_ARG_END_FLAG: &str = "--";

fn parse_args() -> (Option<f32>, Config) {
let args = BridgeArgs::parse();
(args.watchdog.flatten(), args.into())
// Split arguments between "ROS-defined" ones and the "user-defined" ones
// (as per https://design.ros2.org/articles/ros_command_line_arguments.html)
let mut ros_args = vec!["ros-args".to_string()];
let mut user_args = Vec::new();
let mut in_ros_args_section = false;
for arg in std::env::args() {
match arg.as_str() {
ROS_ARG_START_FLAG => in_ros_args_section = true,
ROS_ARG_END_FLAG => in_ros_args_section = false,
_ if in_ros_args_section => ros_args.push(arg),
_ => user_args.push(arg),
}
}

// Create config parsing user-defined args
let bridge_args = BridgeArgs::parse_from(user_args);
let watchdog_opt = bridge_args.watchdog.flatten();
let mut config = bridge_args.into();

// Amend config with "ROS-define" args
let ros_args = RosArgs::parse_from(ros_args);
ros_args.update_config(&mut config);

(watchdog_opt, config)
}

#[async_std::main]
Expand Down
80 changes: 80 additions & 0 deletions zenoh-bridge-ros2dds/src/ros_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

use zenoh::config::Config;

use crate::bridge_args::insert_json5;

#[derive(clap::Parser, Clone, PartialEq, Eq, Hash, Debug)]
pub struct RosArgs {
/// Name remapping
#[arg(short, long, value_name = "FROM:=TO", value_parser = parse_remap)]
pub remap: Vec<Remap>,
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
enum RemapFrom {
Namespace,
Node,
_Name(String),
}

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct Remap {
from: RemapFrom,
to: String,
}

fn parse_remap(s: &str) -> Result<Remap, String> {
match s.split_once(":=") {
Some(("__ns", to)) if !to.is_empty() => Ok(Remap {
from: RemapFrom::Namespace,
to: to.into(),
}),
Some(("__node", to)) if !to.is_empty() => Ok(Remap {
from: RemapFrom::Node,
to: to.into(),
}),
Some((from, to)) if !from.is_empty() && !to.is_empty() => {
Err("only remapping for '__ns' and '__node' are currently supported".into())
}
_ => Err("valid value must have format 'fromp:=to'".into()),
}
}

impl RosArgs {
pub fn update_config(&self, config: &mut Config) {
for r in &self.remap {
match r.from {
RemapFrom::Namespace => {
log::info!(
"Remapping namespace to '{}' as per ROS command line argument",
r.to
);
insert_json5(config, "plugins/ros2dds/namespace", &r.to);
}
RemapFrom::Node => {
log::info!(
"Remapping node name to '{}' as per ROS command line argument",
r.to
);
insert_json5(config, "plugins/ros2dds/nodename", &r.to);
}
RemapFrom::_Name(_) => {
panic!("Unsupported remapping... only '__node' and '__ns' are supported")
}
}
}
}
}