PX4 Firmware
PX4 Autopilot Software http://px4.io
log_writer_mavlink.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #include "log_writer_mavlink.h"
35 #include "messages.h"
36 
37 #include <drivers/drv_hrt.h>
38 #include <mathlib/mathlib.h>
39 #include <px4_platform_common/log.h>
40 #include <px4_platform_common/posix.h>
41 #include <cstring>
42 
43 namespace px4
44 {
45 namespace logger
46 {
47 
49 {
51 }
52 
54 {
55  return true;
56 }
57 
59 {
60  if (_ulog_stream_ack_sub >= 0) {
62  }
63 }
64 
66 {
67  if (_ulog_stream_ack_sub == -1) {
68  _ulog_stream_ack_sub = orb_subscribe(ORB_ID(ulog_stream_ack));
69  }
70 
71  // make sure we don't get any stale ack's by doing an orb_copy
73  orb_copy(ORB_ID(ulog_stream_ack), _ulog_stream_ack_sub, &ack);
74 
78 
79  _is_started = true;
80 }
81 
83 {
85  _is_started = false;
86 }
87 
88 int LogWriterMavlink::write_message(void *ptr, size_t size)
89 {
90  if (!is_started()) {
91  return 0;
92  }
93 
94  const uint8_t data_len = (uint8_t)sizeof(_ulog_stream_data.data);
95  uint8_t *ptr_data = (uint8_t *)ptr;
96 
99  }
100 
101  while (size > 0) {
102  size_t send_len = math::min((size_t)data_len - _ulog_stream_data.length, size);
103  memcpy(_ulog_stream_data.data + _ulog_stream_data.length, ptr_data, send_len);
104  _ulog_stream_data.length += send_len;
105  ptr_data += send_len;
106  size -= send_len;
107 
108  if (_ulog_stream_data.length >= data_len) {
109  if (publish_message()) {
110  return -2;
111  }
112  }
113  }
114 
115  return 0;
116 }
117 
119 {
120  if (!need_reliable && _need_reliable_transfer) {
121  if (_ulog_stream_data.length > 0) {
122  // make sure to send previous data using reliable transfer
123  publish_message();
124  }
125  }
126 
127  _need_reliable_transfer = need_reliable;
128 }
129 
131 {
134 
136  _ulog_stream_data.flags = _ulog_stream_data.FLAGS_NEED_ACK;
137  }
138 
140 
142  // we need to wait for an ack. Note that this blocks the main logger thread, so if a file logging
143  // is already running, it will miss samples.
144  px4_pollfd_struct_t fds[1];
145  fds[0].fd = _ulog_stream_ack_sub;
146  fds[0].events = POLLIN;
147  bool got_ack = false;
148  const int timeout_ms = ulog_stream_ack_s::ACK_TIMEOUT * ulog_stream_ack_s::ACK_MAX_TRIES;
149 
150  hrt_abstime started = hrt_absolute_time();
151 
152  do {
153  int ret = px4_poll(fds, sizeof(fds) / sizeof(fds[0]), timeout_ms);
154 
155  if (ret <= 0) {
156  break;
157  }
158 
159  if (fds[0].revents & POLLIN) {
160  ulog_stream_ack_s ack;
161  orb_copy(ORB_ID(ulog_stream_ack), _ulog_stream_ack_sub, &ack);
162 
164  got_ack = true;
165  }
166 
167  } else {
168  break;
169  }
170  } while (!got_ack && hrt_elapsed_time(&started) / 1000 < timeout_ms);
171 
172  if (!got_ack) {
173  PX4_ERR("Ack timeout. Stopping mavlink log");
174  stop_log();
175  return -2;
176  }
177 
178  PX4_DEBUG("got ack in %i ms", (int)(hrt_elapsed_time(&started) / 1000));
179  }
180 
184  return 0;
185 }
186 
187 }
188 }
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Definition: uORB.cpp:90
static int timeout_ms
uint8_t flags
Definition: ulog_stream.h:59
bool publish(const T &data)
Publish the struct.
int px4_poll(px4_pollfd_struct_t *fds, nfds_t nfds, int timeout)
uint8_t data[249]
Definition: ulog_stream.h:60
High-resolution timer with callouts and timekeeping.
int orb_subscribe(const struct orb_metadata *meta)
Definition: uORB.cpp:75
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
uint8_t length
Definition: ulog_stream.h:57
int orb_unsubscribe(int handle)
Definition: uORB.cpp:85
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
Definition: drv_hrt.h:102
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
constexpr _Tp min(_Tp a, _Tp b)
Definition: Limits.hpp:54
uint64_t timestamp
Definition: ulog_stream.h:55
Definition: bst.cpp:62
uint8_t first_message_offset
Definition: ulog_stream.h:58
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
uint16_t msg_sequence
Definition: ulog_stream.h:56