Skip to content

Commit f377b7a

Browse files
BorjaOutereloJulián Bermúdez Ortega
andcommitted
Dashing migration. (#15)
* Addapt to Dashing Diademata rosidl generation. * Use new Micro XRCE-DDS agent library API. * main.cpp update (#22) * Update main.cpp according to the new version of Micro-XRCE-DDS-Agent. * Apply suggestions from code review Co-Authored-By: Borja Outerelo <borjaouterelo@gmail.com> Co-authored-by: Borja Outerelo <borjaouterelo@gmail.com> Co-authored-by: Julián Bermúdez Ortega <julianbermudez@eprosima.com>
1 parent b47de61 commit f377b7a

File tree

3 files changed

+58
-208
lines changed

3 files changed

+58
-208
lines changed

micro_ros_agent/bin/Xml_interface_gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
sys.path.append(os.path.join(os.path.dirname(os.path.realpath(__file__)),'..'))
88

99
from rosidl_cmake import read_generator_arguments
10-
from rosidl_parser import UnknownMessageType
10+
from rosidl_adapter.parser import UnknownMessageType
1111
from micro_ros_agent import generate_XML
1212

1313

micro_ros_agent/micro_ros_agent/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
from rosidl_cmake import convert_camel_case_to_lower_case_underscore
2020
from rosidl_cmake import expand_template
21-
from rosidl_cmake import extract_message_types
2221
from rosidl_cmake import get_newest_modification_time
23-
from rosidl_parser import parse_message_file
24-
from rosidl_parser import parse_service_file
25-
from rosidl_parser import validate_field_types
22+
from rosidl_adapter.parser import parse_message_file
23+
from rosidl_adapter.parser import parse_service_file
24+
from rosidl_adapter.parser import validate_field_types
2625

26+
from rosidl_cmake import generate_files
2727

2828
def GetPackage(Dir):
2929

@@ -214,4 +214,4 @@ def generate_XML(args):
214214
#f = open(os.path.join(args['output_dir'], 'demofile.txt'), 'w+')
215215
#f.write("%s\n" % spec)
216216
#f.close()
217-
return 0
217+
return 0

micro_ros_agent/src/main.cpp

Lines changed: 52 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
1+
// Copyright 2017-present Proyectos y Sistemas de Mantenimiento SL (eProsima).
32
//
43
// Licensed under the Apache License, Version 2.0 (the "License");
54
// you may not use this file except in compliance with the License.
@@ -13,225 +12,76 @@
1312
// See the License for the specific language governing permissions and
1413
// limitations under the License.
1514

16-
#ifdef _WIN32
17-
#include <uxr/agent/transport/udp/UDPServerWindows.hpp>
18-
#include <uxr/agent/transport/tcp/TCPServerWindows.hpp>
19-
20-
#elif __unix__
21-
#include <unistd.h>
22-
#include <libgen.h>
23-
24-
#include <uxr/agent/transport/serial/SerialServerLinux.hpp>
25-
#include <uxr/agent/transport/udp/UDPServerLinux.hpp>
26-
#include <uxr/agent/transport/tcp/TCPServerLinux.hpp>
27-
#include <termios.h>
28-
#include <fcntl.h>
29-
#endif
30-
31-
#include <iterator>
32-
#include <iostream>
33-
#include <string>
34-
#include <limits>
35-
#include "rclcpp/rclcpp.hpp"
36-
37-
void showHelp()
38-
{
39-
std::cout << "Usage: program <command>" << std::endl;
40-
std::cout << "List of commands:" << std::endl;
41-
#ifdef _WIN32
42-
std::cout << " udp <local_port>" << std::endl;
43-
std::cout << " tcp <local_port>" << std::endl;
44-
#else
45-
std::cout << " serial <device_name>" << std::endl;
46-
std::cout << " pseudo-serial" << std::endl;
47-
std::cout << " udp <local_port> [<discovery_port>]" << std::endl;
48-
std::cout << " tcp <local_port> [<discovery_port>]" << std::endl;
49-
#endif
50-
}
51-
52-
void initializationError()
53-
{
54-
std::cout << "Error: Invalid arguments." << std::endl;
55-
showHelp();
56-
std::exit(EXIT_FAILURE);
57-
}
58-
59-
uint16_t parsePort(const std::string& str_port)
60-
{
61-
uint16_t valid_port = 0;
62-
try
63-
{
64-
int port = std::stoi(str_port);
65-
if(port > (std::numeric_limits<uint16_t>::max)())
66-
{
67-
std::cout << "Error: port number '" << port << "out of range." << std::endl;
68-
initializationError();
69-
}
70-
valid_port = uint16_t(port);
71-
}
72-
catch (const std::invalid_argument& )
73-
{
74-
initializationError();
75-
}
76-
return valid_port;
77-
}
15+
#include <uxr/agent/utils/CLI.hpp>
16+
#include <csignal>
7817

7918
int main(int argc, char** argv)
8019
{
81-
eprosima::uxr::Server* server = nullptr;
82-
std::vector<std::string> cl(0);
83-
84-
if (1 == argc)
85-
{
86-
showHelp();
87-
std::cout << std::endl;
88-
std::cout << "Enter command: ";
89-
90-
std::string raw_cl;
91-
std::getline(std::cin, raw_cl);
92-
std::istringstream iss(raw_cl);
93-
cl.insert(cl.begin(), std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>());
94-
std::cout << raw_cl << std::endl;
95-
}
96-
else
97-
{
98-
for (int i = 1; i < argc; ++i)
99-
{
100-
cl.push_back(argv[i]);
101-
}
102-
}
103-
104-
if((1 == cl.size()) && (("-h" == cl[0]) || ("--help" == cl[0])))
105-
{
106-
showHelp();
107-
}
108-
else if((2 <= cl.size()) && ("udp" == cl[0]))
20+
#ifndef _WIN32
21+
sigset_t signals;
22+
sigemptyset(&signals);
23+
if(sigaddset(&signals, SIGINT) && sigaddset(&signals, SIGTERM))
10924
{
110-
std::cout << "UDP agent initialization... ";
111-
uint16_t port = parsePort(cl[1]);
112-
#ifdef _WIN32
113-
server = new eprosima::uxr::UDPServer(port);
114-
#else
115-
server = (3 == cl.size()) //discovery port
116-
? new eprosima::uxr::UDPServer(port, parsePort(cl[2]))
117-
: new eprosima::uxr::UDPServer(port);
118-
#endif
25+
std::cerr << "Wrong signalset" << std::endl;
26+
std::exit(EXIT_FAILURE);
11927
}
120-
else if((2 <= cl.size()) && ("tcp" == cl[0]))
121-
{
122-
std::cout << "TCP agent initialization... ";
123-
uint16_t port = parsePort(cl[1]);
124-
#ifdef _WIN32
125-
server = new eprosima::uxr::TCPServer(port);
126-
#else
127-
server = (3 == cl.size()) //discovery port
128-
? new eprosima::uxr::TCPServer(port, parsePort(cl[2]))
129-
: new eprosima::uxr::TCPServer(port);
28+
sigprocmask( SIG_BLOCK, &signals, nullptr );
13029
#endif
131-
}
132-
#ifndef _WIN32
133-
else if((2 == cl.size()) && ("serial" == cl[0]))
134-
{
135-
std::cout << "Serial agent initialization... ";
136-
137-
/* Open serial device. */
138-
int fd = open(cl[1].c_str(), O_RDWR | O_NOCTTY);
139-
if (0 < fd)
140-
{
141-
struct termios tty_config;
142-
memset(&tty_config, 0, sizeof(tty_config));
143-
if (0 == tcgetattr(fd, &tty_config))
144-
{
145-
/* Setting CONTROL OPTIONS. */
146-
tty_config.c_cflag |= CREAD; // Enable read.
147-
tty_config.c_cflag |= CLOCAL; // Set local mode.
148-
tty_config.c_cflag &= ~PARENB; // Disable parity.
149-
tty_config.c_cflag &= ~CSTOPB; // Set one stop bit.
150-
tty_config.c_cflag &= ~CSIZE; // Mask the character size bits.
151-
tty_config.c_cflag |= CS8; // Set 8 data bits.
152-
tty_config.c_cflag &= ~CRTSCTS; // Disable hardware flow control.
153-
154-
/* Setting LOCAL OPTIONS. */
155-
tty_config.c_lflag &= ~ICANON; // Set non-canonical input.
156-
tty_config.c_lflag &= ~ECHO; // Disable echoing of input characters.
157-
tty_config.c_lflag &= ~ECHOE; // Disable echoing the erase character.
158-
tty_config.c_lflag &= ~ISIG; // Disable SIGINTR, SIGSUSP, SIGDSUSP and SIGQUIT signals.
159-
160-
/* Setting INPUT OPTIONS. */
161-
tty_config.c_iflag &= ~IXON; // Disable output software flow control.
162-
tty_config.c_iflag &= ~IXOFF; // Disable input software flow control.
163-
tty_config.c_iflag &= ~INPCK; // Disable parity check.
164-
tty_config.c_iflag &= ~ISTRIP; // Disable strip parity bits.
165-
tty_config.c_iflag &= ~IGNBRK; // No ignore break condition.
166-
tty_config.c_iflag &= ~IGNCR; // No ignore carrier return.
167-
tty_config.c_iflag &= ~INLCR; // No map NL to CR.
168-
tty_config.c_iflag &= ~ICRNL; // No map CR to NL.
169-
170-
/* Setting OUTPUT OPTIONS. */
171-
tty_config.c_oflag &= ~OPOST; // Set raw output.
17230

173-
/* Setting OUTPUT CHARACTERS. */
174-
tty_config.c_cc[VMIN] = 10;
175-
tty_config.c_cc[VTIME] = 1;
17631

177-
/* Setting BAUD RATE. */
178-
cfsetispeed(&tty_config, B115200);
179-
cfsetospeed(&tty_config, B115200);
32+
/* CLI application. */
33+
CLI::App app("micro-ROS Agent");
34+
app.require_subcommand(1, 1);
35+
app.get_formatter()->column_width(42);
18036

181-
if (0 == tcsetattr(fd, TCSANOW, &tty_config))
182-
{
183-
server = new eprosima::uxr::SerialServer(fd, 0);
184-
}
185-
}
186-
}
187-
}
188-
else if ((1 == cl.size()) && ("pseudo-serial" == cl[0]))
189-
{
190-
std::cout << "Pseudo-Serial initialization... ";
191-
192-
/* Open pseudo-terminal. */
193-
char* dev = NULL;
194-
int fd = posix_openpt(O_RDWR | O_NOCTTY);
195-
if (-1 != fd)
196-
{
197-
if (grantpt(fd) == 0 && unlockpt(fd) == 0 && (dev = ptsname(fd)))
198-
{
199-
struct termios attr;
200-
tcgetattr(fd, &attr);
201-
cfmakeraw(&attr);
202-
tcflush(fd, TCIOFLUSH);
203-
tcsetattr(fd, TCSANOW, &attr);
204-
std::cout << "Device: " << dev << std::endl;
205-
}
206-
}
207-
server = new eprosima::uxr::SerialServer(fd, 0x00);
208-
}
37+
/* CLI subcommands. */
38+
eprosima::uxr::cli::UDPv4Subcommand udpv4_subcommand(app);
39+
eprosima::uxr::cli::UDPv6Subcommand udpv6_subcommand(app);
40+
eprosima::uxr::cli::TCPv4Subcommand tcpv4_subcommand(app);
41+
eprosima::uxr::cli::TCPv6Subcommand tcpv6_subcommand(app);
42+
#ifndef _WIN32
43+
eprosima::uxr::cli::SerialSubcommand serial_subcommand(app);
44+
eprosima::uxr::cli::PseudoSerialSubcommand pseudo_serial_subcommand(app);
20945
#endif
210-
else
46+
eprosima::uxr::cli::ExitSubcommand exit_subcommand(app);
47+
48+
/* CLI parse. */
49+
std::string cli_input{};
50+
for (int i = 1; i < argc; ++i)
21151
{
212-
initializationError();
52+
cli_input.append(argv[i]);
53+
cli_input.append(" ");
21354
}
21455

215-
if (nullptr != server)
56+
while (true)
21657
{
217-
/* Launch server. */
218-
if (server->run())
58+
try
21959
{
220-
std::cout << "OK" << std::endl;
221-
std::cin.clear();
222-
char exit_flag = 0;
223-
while ('q' != exit_flag)
224-
{
225-
std::cout << "Enter 'q' for exit" << std::endl;
226-
std::cin >> exit_flag;
227-
}
228-
server->stop();
60+
app.parse(cli_input);
61+
break;
22962
}
230-
else
63+
catch (const CLI::ParseError& e)
23164
{
232-
std::cout << "ERROR" << std::endl;
65+
app.exit(e);
66+
std::cin.clear();
67+
std::cout << std::endl;
68+
std::cout << "Enter command: ";
69+
std::getline(std::cin, cli_input);
23370
}
23471
}
23572

73+
#ifdef _WIN32
74+
/* Waiting until exit. */
75+
std::cin.clear();
76+
char exit_flag = 0;
77+
while ('q' != exit_flag)
78+
{
79+
std::cin >> exit_flag;
80+
}
81+
#else
82+
/* Wait for SIGTERM/SIGINT instead, as reading from stdin may be redirected to /dev/null. */
83+
int n_signal = 0;
84+
sigwait(&signals, &n_signal);
85+
#endif
23686
return 0;
23787
}

0 commit comments

Comments
 (0)