@ -118,7 +118,7 @@ ssize_t Transport_node::read(uint8_t *topic_ID, char out_buffer[], size_t buffer
@@ -118,7 +118,7 @@ ssize_t Transport_node::read(uint8_t *topic_ID, char out_buffer[], size_t buffer
if ( errsv & & EAGAIN ! = errsv & & ETIMEDOUT ! = errsv ) {
# ifndef PX4_ERR
printf ( " Read fail %d \n " , errsv ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t Read fail %d \033 [0m \n " , errsv ) ;
# else
PX4_ERR ( " Read fail %d " , errsv ) ;
# endif /* PX4_ERR */
@ -147,11 +147,11 @@ ssize_t Transport_node::read(uint8_t *topic_ID, char out_buffer[], size_t buffer
@@ -147,11 +147,11 @@ ssize_t Transport_node::read(uint8_t *topic_ID, char out_buffer[], size_t buffer
// Start not found
if ( msg_start_pos > rx_buff_pos - header_size ) {
# ifndef PX4_INFO
printf ( " (↓↓ %u)\n " , msg_start_pos ) ;
# ifndef PX4_WARN
printf ( " \033 [1;33m[ micrortps_transport ] \t (↓↓ %u) \033 [0m \n " , msg_start_pos ) ;
# else
PX4_INFO ( " (↓↓ %u)" , msg_start_pos ) ;
# endif /* PX4_INFO */
PX4_WARN ( " (↓↓ %u) " , msg_start_pos ) ;
# endif /* PX4_WARN */
// All we've checked so far is garbage, drop it - but save unchecked bytes
memmove ( rx_buffer , rx_buffer + msg_start_pos , rx_buff_pos - msg_start_pos ) ;
@ -176,11 +176,11 @@ ssize_t Transport_node::read(uint8_t *topic_ID, char out_buffer[], size_t buffer
@@ -176,11 +176,11 @@ ssize_t Transport_node::read(uint8_t *topic_ID, char out_buffer[], size_t buffer
if ( msg_start_pos + header_size + payload_len > rx_buff_pos ) {
// If there's garbage at the beginning, drop it
if ( msg_start_pos > 0 ) {
# ifndef PX4_INFO
printf ( " (↓ %u)\n " , msg_start_pos ) ;
# ifndef PX4_WARN
printf ( " \033 [1;33m[ micrortps_transport ] \t (↓ %u) \033 [0m \n " , msg_start_pos ) ;
# else
PX4_INFO ( " (↓ %u)" , msg_start_pos ) ;
# endif /* PX4_INFO */
PX4_WARN ( " (↓ %u) " , msg_start_pos ) ;
# endif /* PX4_WARN */
memmove ( rx_buffer , rx_buffer + msg_start_pos , rx_buff_pos - msg_start_pos ) ;
rx_buff_pos - = msg_start_pos ;
}
@ -193,11 +193,9 @@ ssize_t Transport_node::read(uint8_t *topic_ID, char out_buffer[], size_t buffer
@@ -193,11 +193,9 @@ ssize_t Transport_node::read(uint8_t *topic_ID, char out_buffer[], size_t buffer
if ( read_crc ! = calc_crc ) {
# ifndef PX4_ERR
printf ( " Bad CRC %u != %u \n " , read_crc , calc_crc ) ;
printf ( " (↓ %lu) \n " , ( unsigned long ) ( header_size + payload_len ) ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t Bad CRC %u != %u \t \t (↓ %lu) \033 [0m \n " , read_crc , calc_crc , ( unsigned long ) ( header_size + payload_len ) ) ;
# else
PX4_ERR ( " Bad CRC %u != %u " , read_crc , calc_crc ) ;
PX4_ERR ( " (↓ %lu) " , ( unsigned long ) ( header_size + payload_len ) ) ;
PX4_ERR ( " Bad CRC %u != %u \t \t (↓ %lu) " , read_crc , calc_crc , ( unsigned long ) ( header_size + payload_len ) ) ;
# endif /* PX4_ERR */
len = - 1 ;
@ -249,10 +247,12 @@ ssize_t Transport_node::write(const uint8_t topic_ID, char buffer[], size_t leng
@@ -249,10 +247,12 @@ ssize_t Transport_node::write(const uint8_t topic_ID, char buffer[], size_t leng
return len + sizeof ( header ) ;
}
UART_node : : UART_node ( const char * _uart_name , uint32_t _baudrate , uint32_t _poll_ms ) :
UART_node : : UART_node ( const char * _uart_name , const uint32_t _baudrate , const uint32_t _poll_ms , const bool _hw_flow_control , const bool _sw_flow_control ) :
uart_fd ( - 1 ) ,
baudrate ( _baudrate ) ,
poll_ms ( _poll_ms )
poll_ms ( _poll_ms ) ,
hw_flow_control ( _hw_flow_control ) ,
sw_flow_control ( _sw_flow_control )
{
if ( nullptr ! = _uart_name ) {
@ -272,9 +272,9 @@ int UART_node::init()
@@ -272,9 +272,9 @@ int UART_node::init()
if ( uart_fd < 0 ) {
# ifndef PX4_ERR
printf ( " Failed to open device: %s (%d) \n " , uart_name , errno ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t UART transport: Failed to open device: %s (%d)\033 [0m \n " , uart_name , errno ) ;
# else
PX4_ERR ( " Failed to open device: %s (%d) " , uart_name , errno ) ;
PX4_ERR ( " UART transport: Failed to open device: %s (%d)" , uart_name , errno ) ;
# endif /* PX4_ERR */
return - errno ;
}
@ -292,61 +292,67 @@ int UART_node::init()
@@ -292,61 +292,67 @@ int UART_node::init()
if ( ( termios_state = tcgetattr ( uart_fd , & uart_config ) ) < 0 ) {
int errno_bkp = errno ;
# ifndef PX4_ERR
printf ( " ERR GET CONF %s: %d (%d) \n " , uart_name , termios_state , errno ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t UART transport: ERR GET CONF %s: %d (%d)\n \033 [0m " , uart_name , termios_state , errno ) ;
# else
PX4_ERR ( " ERR GET CONF %s: %d (%d) " , uart_name , termios_state , errno ) ;
PX4_ERR ( " UART transport: ERR GET CONF %s: %d (%d)" , uart_name , termios_state , errno ) ;
# endif /* PX4_ERR */
close ( ) ;
return - errno_bkp ;
}
//Set up the UART for non-canonical binary communication: 8 bits, 1 stop bit, no parity,
//no flow control, no modem control
uart_config . c_iflag & = ! ( INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF ) ;
uart_config . c_iflag | = IGNBRK | IGNPAR ;
uart_config . c_oflag & = ! ( OPOST | ONLCR | OCRNL | ONOCR | ONLRET | OFILL | NLDLY | VTDLY ) ;
uart_config . c_oflag | = NL0 | VT0 ;
// Set up the UART for non-canonical binary communication: 8 bits, 1 stop bit, no parity.
uart_config . c_iflag & = ! ( INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF ) ;
uart_config . c_iflag | = IGNBRK | IGNPAR ;
uart_config . c_oflag & = ! ( OPOST | ONLCR | OCRNL | ONOCR | ONLRET | OFILL | NLDLY | VTDLY ) ;
uart_config . c_oflag | = NL0 | VT0 ;
uart_config . c_cflag & = ! ( CSIZE | CSTOPB | PARENB ) ;
uart_config . c_cflag | = CS8 | CREAD | CLOCAL ;
uart_config . c_cflag & = ! ( CSIZE | CSTOPB | PARENB ) ;
uart_config . c_cflag | = CS8 | CREAD | CLOCAL ;
uart_config . c_lflag & = ! ( ISIG | ICANON | ECHO | TOSTOP | IEXTEN ) ;
uart_config . c_lflag & = ! ( ISIG | ICANON | ECHO | TOSTOP | IEXTEN ) ;
// USB serial is indicated by /dev/ttyACM0
if ( strcmp ( uart_name , " /dev/ttyACM0 " ) ! = 0 & & strcmp ( uart_name , " /dev/ttyACM1 " ) ! = 0 ) {
// Set baud rate
speed_t speed ;
// Flow control
if ( hw_flow_control ) {
// HW flow control
uart_config . c_lflag | = CRTSCTS ;
} else if ( sw_flow_control ) {
// SW flow control
uart_config . c_lflag | = ( IXON | IXOFF | IXANY ) ;
}
// Set baud rate
speed_t speed ;
if ( ! baudrate_to_speed ( baudrate , & speed ) ) {
if ( ! baudrate_to_speed ( baudrate , & speed ) ) {
# ifndef PX4_ERR
printf ( " ERR SET BAUD %s: Unsupported baudrate: %d \n \t supported examples: \n \t 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 921600, 1000000 \n " ,
uart_name , baudrate ) ;
printf ( " \033 [0;31mUART transport: ERR SET BAUD %s: Unsupported baudrate: %d\n \t supported examples: \n \t 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 921600, 1000000 \033 [0m \n " ,
uart_name , baudrate ) ;
# else
PX4_ERR ( " ERR SET BAUD %s: Unsupported baudrate: %d \n \t supported examples: \n \t 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 921600, 1000000 \n " ,
uart_name , baudrate ) ;
PX4_ERR ( " UART transport: ERR SET BAUD %s: Unsupported baudrate: %d\n \t supported examples: \n \t 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 921600, 1000000 \n " ,
uart_name , baudrate ) ;
# endif /* PX4_ERR */
close ( ) ;
return - EINVAL ;
}
close ( ) ;
return - EINVAL ;
}
if ( cfsetispeed ( & uart_config , speed ) < 0 | | cfsetospeed ( & uart_config , speed ) < 0 ) {
int errno_bkp = errno ;
if ( cfsetispeed ( & uart_config , speed ) < 0 | | cfsetospeed ( & uart_config , speed ) < 0 ) {
int errno_bkp = errno ;
# ifndef PX4_ERR
printf ( " ERR SET BAUD %s: %d (%d) \n " , uart_name , termios_state , errno ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t UART transport: ERR SET BAUD %s: %d (%d)\033 [0m \n " , uart_name , termios_state , errno ) ;
# else
PX4_ERR ( " ERR SET BAUD %s: %d (%d) " , uart_name , termios_state , errno ) ;
PX4_ERR ( " ERR SET BAUD %s: %d (%d) " , uart_name , termios_state , errno ) ;
# endif /* PX4_ERR */
close ( ) ;
return - errno_bkp ;
}
close ( ) ;
return - errno_bkp ;
}
if ( ( termios_state = tcsetattr ( uart_fd , TCSANOW , & uart_config ) ) < 0 ) {
int errno_bkp = errno ;
# ifndef PX4_ERR
printf ( " ERR SET CONF %s (%d) \n " , uart_name , errno ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t UART transport: ERR SET CONF %s (%d)\033 [0m \n " , uart_name , errno ) ;
# else
PX4_ERR ( " ERR SET CONF %s (%d) " , uart_name , errno ) ;
PX4_ERR ( " UART transport: ERR SET CONF %s (%d)" , uart_name , errno ) ;
# endif /* PX4_ERR */
close ( ) ;
return - errno_bkp ;
@ -370,15 +376,15 @@ int UART_node::init()
@@ -370,15 +376,15 @@ int UART_node::init()
if ( flush ) {
# ifndef PX4_INFO
printf ( " Flush \n " ) ;
printf ( " [ micrortps_transport ] \t UART transport: Flush\n " ) ;
# else
PX4_INFO ( " Flush " ) ;
PX4_INFO ( " UART transport: Flush" ) ;
# endif /* PX4_INFO */
} else {
# ifndef PX4_INFO
printf ( " No flush \n " ) ;
printf ( " [ micrortps_transport ] \t UART transport: No flush\n " ) ;
# else
PX4_INFO ( " No flush " ) ;
PX4_INFO ( " UART transport: No flush" ) ;
# endif /* PX4_INFO */
}
@ -397,9 +403,9 @@ uint8_t UART_node::close()
@@ -397,9 +403,9 @@ uint8_t UART_node::close()
{
if ( - 1 ! = uart_fd ) {
# ifndef PX4_WARN
printf ( " Closed UART... \n " ) ;
printf ( " \033 [1;33m[ micrortps_transport ] \t Closed UART. \n \033 [0m " ) ;
# else
PX4_WARN ( " Closed UART... " ) ;
PX4_WARN ( " Closed UART. " ) ;
# endif /* PX4_WARN */
: : close ( uart_fd ) ;
uart_fd = - 1 ;
@ -557,30 +563,30 @@ int UDP_node::init_receiver(uint16_t udp_port)
@@ -557,30 +563,30 @@ int UDP_node::init_receiver(uint16_t udp_port)
if ( ( receiver_fd = socket ( AF_INET , SOCK_DGRAM , 0 ) ) < 0 ) {
# ifndef PX4_ERR
printf ( " Create socket failed \n " ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t UDP transport: Create socket failed\033 [0m \n " ) ;
# else
PX4_ERR ( " Create socket failed " ) ;
PX4_ERR ( " UDP transport: Create socket failed" ) ;
# endif /* PX4_ERR */
return - 1 ;
}
# ifndef PX4_INFO
printf ( " - Trying to connect..." ) ;
printf ( " [ micrortps_transport ] \t UDP transport: Trying to connect..." ) ;
# else
PX4_INFO ( " Trying to connect... " ) ;
PX4_INFO ( " UDP transport: Trying to connect..." ) ;
# endif /* PX4_INFO */
if ( bind ( receiver_fd , ( struct sockaddr * ) & receiver_inaddr , sizeof ( receiver_inaddr ) ) < 0 ) {
# ifndef PX4_ERR
printf ( " Bind failed \n " ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t UDP transport: Bind failed\033 [0m \n " ) ;
# else
PX4_ERR ( " Bind failed " ) ;
PX4_ERR ( " UDP transport: Bind failed" ) ;
# endif /* PX4_ERR */
return - 1 ;
}
# ifndef PX4_INFO
printf ( " Connected to server! \n \n " ) ;
printf ( " [ micrortps_transport ] \t UDP transport: Connected to server!\n \n " ) ;
# else
PX4_INFO ( " Connected to server! " ) ;
PX4_INFO ( " UDP transport: Connected to server!" ) ;
# endif /* PX4_INFO */
# endif /* __PX4_NUTTX */
return 0 ;
@ -592,9 +598,9 @@ int UDP_node::init_sender(uint16_t udp_port)
@@ -592,9 +598,9 @@ int UDP_node::init_sender(uint16_t udp_port)
if ( ( sender_fd = socket ( AF_INET , SOCK_DGRAM , 0 ) ) < 0 ) {
# ifndef PX4_ERR
printf ( " Create socket failed \n " ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t UDP transport: Create socket failed\033 [0m \n " ) ;
# else
PX4_ERR ( " Create socket failed " ) ;
PX4_ERR ( " UDP transport: Create socket failed" ) ;
# endif /* PX4_ERR */
return - 1 ;
}
@ -605,9 +611,9 @@ int UDP_node::init_sender(uint16_t udp_port)
@@ -605,9 +611,9 @@ int UDP_node::init_sender(uint16_t udp_port)
if ( inet_aton ( udp_ip , & sender_outaddr . sin_addr ) = = 0 ) {
# ifndef PX4_ERR
printf ( " inet_aton() failed \n " ) ;
printf ( " \033 [0;31m[ micrortps_transport ] \t UDP transport: inet_aton() failed\033 [0m \n " ) ;
# else
PX4_ERR ( " inet_aton() failed " ) ;
PX4_ERR ( " UDP transport: inet_aton() failed" ) ;
# endif /* PX4_ERR */
return - 1 ;
}
@ -623,9 +629,9 @@ uint8_t UDP_node::close()
@@ -623,9 +629,9 @@ uint8_t UDP_node::close()
if ( sender_fd ! = - 1 ) {
# ifndef PX4_WARN
printf ( " Closed sender socket! \n " ) ;
printf ( " \033 [1;33m[ micrortps_transport ] \t UDP transport: Closed sender socket!\033 [0m \n " ) ;
# else
PX4_WARN ( " Closed sender socket! " ) ;
PX4_WARN ( " UDP transport: Closed sender socket!" ) ;
# endif /* PX4_WARN */
shutdown ( sender_fd , SHUT_RDWR ) ;
: : close ( sender_fd ) ;
@ -634,9 +640,9 @@ uint8_t UDP_node::close()
@@ -634,9 +640,9 @@ uint8_t UDP_node::close()
if ( receiver_fd ! = - 1 ) {
# ifndef PX4_WARN
printf ( " Closed receiver socket! \n " ) ;
printf ( " \033 [1;33m[ micrortps_transport ] \t UDP transport: Closed receiver socket!\033 [0m \n " ) ;
# else
PX4_WARN ( " Closed receiver socket! " ) ;
PX4_WARN ( " UDP transport: Closed receiver socket!" ) ;
# endif /* PX4_WARN */
shutdown ( receiver_fd , SHUT_RDWR ) ;
: : close ( receiver_fd ) ;