-
Notifications
You must be signed in to change notification settings - Fork 30
/
dataflash_logger_program.cpp
238 lines (198 loc) · 5.92 KB
/
dataflash_logger_program.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "dataflash_logger_program.h"
#include "dataflash_logger.h"
#include "heart.h"
#include "mavlink_reader.h"
#include "telem_forwarder_client.h"
#include "telem_serial.h"
#include "telem_udp.h"
#include "la-log.h"
#include <signal.h>
#include <unistd.h>
const char *DataFlash_Logger_Program::program_name()
{
if (_argv == NULL) {
return "[Unknown]";
}
return _argv[0];
}
void DataFlash_Logger_Program::usage()
{
::printf("Usage:\n");
::printf("%s [OPTION] [FILE]\n", program_name());
::printf(" -c filepath use config file filepath\n");
::printf(" -h display usage information\n");
::printf(" -d debug mode\n");
::printf(" -s use serial port\n");
::printf(" -u use UDP port\n");
::printf(" -v display version information\n");
::printf("\n");
::printf("Example: %s\n", program_name());
exit(0);
}
void DataFlash_Logger_Program::version()
{
::printf(GIT_VERSION "\n");
exit(0);
}
DataFlash_Logger_Program logger;
void sighup_handler(int signal)
{
logger.sighup_handler(signal);
}
void DLP_Client::do_idle_callbacks()
{
reader->do_idle_callbacks();
}
void DLP_Client::sighup_handler()
{
reader->sighup_handler();
}
void DLP_Client::pack_select_fds(fd_set &fds_read, fd_set &fds_write, fd_set &fds_err, uint8_t &nfds)
{
telem_client->pack_select_fds(fds_read, fds_write, fds_err, nfds);
}
void DLP_Client::do_writer_sends()
{
telem_client->do_writer_sends();
}
void DLP_Client::handle_select_fds(fd_set &fds_read, fd_set &fds_write, fd_set &fds_err, uint8_t &nfds) {
telem_client->handle_select_fds(fds_read, fds_write, fds_err, nfds);
// FIXME: find a more interesting way of doing this... we should
// probably rejig things so that the client is a mavlink_reader
// and simply produces mavlink_message_t's itself, rather than us
// handing off the a dedicated parser object here.
reader->feed(telem_client->_recv_buf, telem_client->_recv_buflen_content);
telem_client->_recv_buflen_content = 0;
}
void DataFlash_Logger_Program::do_idle_callbacks()
{
client->do_idle_callbacks();
}
void DataFlash_Logger_Program::sighup_received_tophalf()
{
client->sighup_handler();
}
uint32_t DataFlash_Logger_Program::select_timeout_us() {
if (_writer->any_data_to_send()) {
return 0;
}
return Common_Tool::select_timeout_us();
}
void DataFlash_Logger_Program::pack_select_fds(fd_set &fds_read, fd_set &fds_write, fd_set &fds_err, uint8_t &nfds)
{
client->pack_select_fds(fds_read, fds_write, fds_err, nfds);
}
void DataFlash_Logger_Program::handle_select_fds(fd_set &fds_read, fd_set &fds_write, fd_set &fds_err, uint8_t &nfds)
{
client->handle_select_fds(fds_read, fds_write, fds_err, nfds);
// handle data *to* e.g. telem_forwarder
client->do_writer_sends();
}
void DataFlash_Logger_Program::create_serial_client()
{
client = new DLP_Client();
client->reader = new MAVLink_Reader(config());
if (client->reader == NULL) {
la_log(LOG_ERR, "Failed to create reader from (%s)\n", config_filename);
exit(1);
}
client->telem_client = new Telem_Serial();
client->telem_client->configure(config());
client->telem_client->init();
}
void DataFlash_Logger_Program::create_telem_forwarder_client()
{
client = new DLP_Client();
client->reader = new MAVLink_Reader(config());
if (client->reader == NULL) {
la_log(LOG_ERR, "Failed to create reader from (%s)\n", config_filename);
exit(1);
}
client->telem_client = new Telem_Forwarder_Client();
client->telem_client->configure(config());
client->telem_client->init();
}
void DataFlash_Logger_Program::create_udp_client()
{
client = new DLP_Client();
client->reader = new MAVLink_Reader(config());
if (client->reader == NULL) {
la_log(LOG_ERR, "Failed to create reader from (%s)\n", config_filename);
exit(1);
}
client->telem_client = new Telem_UDP();
client->telem_client->configure(config());
client->telem_client->init();
}
void DataFlash_Logger_Program::run()
{
init_config();
if (! debug_mode) {
la_log_syslog_open();
}
la_log(LOG_INFO, "dataflash_logger starting: built " __DATE__ " " __TIME__);
signal(SIGHUP, ::sighup_handler);
if (serial_port) {
create_serial_client();
} else if (udp_port) {
create_udp_client();
} else {
create_telem_forwarder_client();
}
_writer = new MAVLink_Writer(config());
if (_writer == NULL) {
la_log(LOG_ERR, "Failed to create writer from (%s)\n", config_filename);
exit(1);
}
_writer->add_client(client->telem_client);
// instantiate message handlers:
DataFlash_Logger *dataflash_logger = new DataFlash_Logger(_writer);
if (dataflash_logger != NULL) {
client->reader->add_message_handler(dataflash_logger, "DataFlash_Logger");
} else {
la_log(LOG_INFO, "Failed to create dataflash logger");
}
Heart *heart= new Heart(_writer);
if (heart != NULL) {
client->reader->add_message_handler(heart, "Heart");
} else {
la_log(LOG_INFO, "Failed to create heart");
}
return select_loop();
}
void DataFlash_Logger_Program::parse_arguments(int argc, char *argv[])
{
int opt;
_argc = argc;
_argv = argv;
while ((opt = getopt(argc, argv, "hvc:dsu")) != -1) {
switch(opt) {
case 'h':
usage();
break;
case 'v':
version();
break;
case 'c':
config_filename = optarg;
break;
case 'd':
debug_mode = true;
break;
case 's':
serial_port = true;
break;
case 'u':
udp_port = true;
break;
}
}
}
/*
* main - entry point
*/
int main(int argc, char* argv[])
{
logger.parse_arguments(argc, argv);
logger.run();
}