Firmware
microRTPS_transport.h
1 /****************************************************************************
2  *
3  * Copyright 2017 Proyectos y Sistemas de Mantenimiento SL (eProsima).
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  ****************************************************************************/
32 
33 #pragma once
34 
35 #include <cstring>
36 #include <arpa/inet.h>
37 #include <poll.h>
38 #include <termios.h>
39 
41 {
42 public:
44  virtual ~Transport_node();
45 
46  virtual int init() {return 0;}
47  virtual uint8_t close() {return 0;}
48  ssize_t read(uint8_t *topic_ID, char out_buffer[], size_t buffer_len);
49 
62  ssize_t write(const uint8_t topic_ID, char buffer[], size_t length);
63 
65  ssize_t get_header_length();
66 
67 protected:
68  virtual ssize_t node_read(void *buffer, size_t len) = 0;
69  virtual ssize_t node_write(void *buffer, size_t len) = 0;
70  virtual bool fds_OK() = 0;
71  uint16_t crc16_byte(uint16_t crc, const uint8_t data);
72  uint16_t crc16(uint8_t const *buffer, size_t len);
73 
74 protected:
75  uint32_t rx_buff_pos;
76  char rx_buffer[1024] = {};
77 
78 private:
79  struct __attribute__((packed)) Header {
80  char marker[3];
81  uint8_t topic_ID;
82  uint8_t seq;
83  uint8_t payload_len_h;
84  uint8_t payload_len_l;
85  uint8_t crc_h;
86  uint8_t crc_l;
87  };
88 };
89 
91 {
92 public:
93  UART_node(const char *uart_name, uint32_t baudrate, uint32_t poll_ms);
94  virtual ~UART_node();
95 
96  int init();
97  uint8_t close();
98 
99 protected:
100  ssize_t node_read(void *buffer, size_t len);
101  ssize_t node_write(void *buffer, size_t len);
102  bool fds_OK();
103  bool baudrate_to_speed(uint32_t bauds, speed_t *speed);
104 
105  int uart_fd;
106  char uart_name[64] = {};
107  uint32_t baudrate;
108  uint32_t poll_ms;
109  struct pollfd poll_fd[1] = {};
110 };
111 
113 {
114 public:
115  UDP_node(uint16_t udp_port_recv, uint16_t udp_port_send);
116  virtual ~UDP_node();
117 
118  int init();
119  uint8_t close();
120 
121 protected:
122  int init_receiver(uint16_t udp_port);
123  int init_sender(uint16_t udp_port);
124  ssize_t node_read(void *buffer, size_t len);
125  ssize_t node_write(void *buffer, size_t len);
126  bool fds_OK();
127 
128  int sender_fd;
129  int receiver_fd;
130  uint16_t udp_port_recv;
131  uint16_t udp_port_send;
132  struct sockaddr_in sender_outaddr;
133  struct sockaddr_in receiver_inaddr;
134  struct sockaddr_in receiver_outaddr;
135 };
Definition: microRTPS_transport.h:40
ssize_t get_header_length()
Get the Length of struct Header to make headroom for the size of struct Header along with payload...
Definition: microRTPS_transport.cpp:199
ssize_t write(const uint8_t topic_ID, char buffer[], size_t length)
write a buffer
Definition: microRTPS_transport.cpp:204
Definition: crtp.h:57
Definition: microRTPS_transport.h:90
Definition: microRTPS_transport.h:112
Definition: video_device.h:50