-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtelem_forwarder_client.cpp
192 lines (160 loc) · 5.56 KB
/
telem_forwarder_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "telem_forwarder_client.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "la-log.h"
#ifdef _WIN32
#include <ws2tcpip.h>
#endif
#define UNUSED __attribute__ ((unused))
void Telem_Forwarder_Client::pack_select_fds(fd_set &fds_read,
fd_set &fds_write UNUSED,
fd_set &fds_err,
uint8_t &nfds)
{
FD_SET(fd_telem_forwarder, &fds_read);
FD_SET(fd_telem_forwarder, &fds_err);
if (fd_telem_forwarder >= nfds) {
nfds = fd_telem_forwarder + 1;
}
}
void Telem_Forwarder_Client::handle_select_fds(fd_set &fds_read,
fd_set &fds_write UNUSED,
fd_set &fds_err,
uint8_t &nfds UNUSED)
{
/* check for packets from telem_forwarder */
if (FD_ISSET(fd_telem_forwarder, &fds_err)) {
FD_CLR(fd_telem_forwarder, &fds_err);
la_log(LOG_ERR, "select(fd_telem_forwarder): %s", strerror(errno));
}
if (FD_ISSET(fd_telem_forwarder, &fds_read)) {
FD_CLR(fd_telem_forwarder, &fds_read);
_recv_buflen_content = handle_recv();
// ::fprintf(stderr, "received %u bytes\n", _buflen_content);
}
}
/*
* create_and_bind - create a socket and bind it to a local UDP port
*
* Used to create the socket on the upstream side that receives from and sends
* to telem_forwarder
*
* Returns fd on success, -1 on error.
*/
void Telem_Forwarder_Client::create_and_bind()
{
int fd;
struct sockaddr_in sa;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
abort();
}
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = htonl(INADDR_ANY);
sa.sin_port = 0; // we don't care what our port is
if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
perror("bind");
abort();
}
fd_telem_forwarder = fd;
} /* create_and_bind */
void Telem_Forwarder_Client::pack_telem_forwarder_sockaddr(INIReader *config)
{
// uint16_t tf_port = config->GetInteger("solo", "telem_forward_port", 14560);
// std::string ip = config->Get("solo", "soloIp", "10.1.1.10");
uint16_t tf_port = config->GetInteger("solo", "telem_forward_port", 14560);
std::string ip = config->Get("solo", "soloIp", "127.0.0.1");
set_address(ip, tf_port);
}
void Telem_Forwarder_Client::set_address(std::string ip, uint16_t port)
{
la_log(LOG_INFO, "df-tfc: connecting to telem-forwarder at %s:%u", ip.c_str(), port);
memset(&sa_tf, 0, sizeof(sa_tf));
sa_tf.sin_family = AF_INET;
#ifdef _WIN32
sa_tf.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
#else
inet_aton(ip.c_str(), &sa_tf.sin_addr); // useful for debugging
#endif
sa_tf.sin_port = htons(port);
}
bool Telem_Forwarder_Client::sane_telem_forwarder_packet(uint8_t *pkt, uint16_t pktlen)
{
if (sa.sin_addr.s_addr != sa_tf.sin_addr.s_addr) {
la_log(LOG_ERR, "received packet not from solo (0x%08x)", sa.sin_addr.s_addr);
return false;
}
if (pktlen < 8) {
la_log(LOG_ERR, "received runt packet (%d bytes)", pktlen);
return false;
}
if (pkt[0] != 254 && pkt[0] != 253) {
la_log(LOG_ERR, "received bad magic (0x%02x)", pkt[0]);
return false;
}
// if (pkt[1] != (pktlen - 8)) {
// la_log(LOG_ERR, "inconsistent length (%u, %u)", pkt[1], pktlen);
// return false;
// }
return true;
}
uint32_t Telem_Forwarder_Client::handle_recv()
{
// ::printf("Receiving packet into position %u\n", _buflen_content);
/* packet from telem_forwarder */
socklen_t sa_len = sizeof(sa);
uint16_t res = recvfrom(fd_telem_forwarder, (char*)&_recv_buf[_recv_buflen_content], _recv_buflen-_recv_buflen_content, 0, (struct sockaddr*)&sa, &sa_len);
/* We get one mavlink packet per udp datagram. Sanity checks here
are: must be from solo's IP and have a valid mavlink header. */
// FIXME: we don't necessarily get just one packet/buffer!
// ::fprintf(stderr, "handle_recv\n");
if (!sane_telem_forwarder_packet(_recv_buf, res)) {
return 0;
}
return res;
}
bool Telem_Forwarder_Client::send_message(const mavlink_message_t &msg)
{
if (send_buffer_space_free() < 1) {
// dropped_packets++;
return false;
}
memcpy(&_send_buf[_send_buf_stop++], (char*)&msg, sizeof(msg));
if (_send_buf_stop >= send_buf_size()) {
_send_buf_stop = 0;
}
return true;
}
void Telem_Forwarder_Client::do_writer_sends()
{
char buf[1024]; // large enough...
while (_send_buf_start != _send_buf_stop) {
mavlink_message_t &msg = _send_buf[_send_buf_start];
uint16_t messageLen = mavlink_msg_to_send_buffer((uint8_t*)buf,&msg);
int32_t bytes_sent =
sendto(fd_telem_forwarder, buf, messageLen, 0,
(struct sockaddr *)&sa_tf, sizeof(struct sockaddr));
if (bytes_sent == -1) {
la_log(LOG_INFO, "Failed sendto: %s", strerror(errno));
// we drop the message anyway!
}
_send_buf_start++;
if (_send_buf_start >= send_buf_size()) {
_send_buf_start = 0;
}
}
return;
}
void Telem_Forwarder_Client::configure(INIReader *config)
{
/* prepare sockaddr used to contact telem_forwarder */
pack_telem_forwarder_sockaddr(config);
}
void Telem_Forwarder_Client::init() {
/* Prepare a port to receive and send data to/from telem_forwarder */
/* does not return on failure */
create_and_bind();
}