mavlink_comm.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Daniel Koch and James Jackson, BYU MAGICC Lab.
3  * 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 are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * * 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  * * Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without 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 
38 
39 namespace mavrosflight
40 {
41 using boost::asio::serial_port_base;
42 
43 MavlinkComm::MavlinkComm() : io_service_(), write_in_progress_(false) {}
44 
46 
48 {
49  // open the port
50  do_open();
51 
52  // start reading from the port
53  async_read();
54  io_thread_ = boost::thread(boost::bind(&boost::asio::io_service::run, &this->io_service_));
55 }
56 
58 {
59  mutex_lock lock(mutex_);
60 
61  io_service_.stop();
62  do_close();
63 
64  if (io_thread_.joinable())
65  {
66  io_thread_.join();
67  }
68 }
69 
71 {
72  if (listener == NULL)
73  return;
74 
75  bool already_registered = false;
76  for (int i = 0; i < listeners_.size(); i++)
77  {
78  if (listener == listeners_[i])
79  {
80  already_registered = true;
81  break;
82  }
83  }
84 
85  if (!already_registered)
86  listeners_.push_back(listener);
87 }
88 
90 {
91  if (listener == NULL)
92  return;
93 
94  for (int i = 0; i < listeners_.size(); i++)
95  {
96  if (listener == listeners_[i])
97  {
98  listeners_.erase(listeners_.begin() + i);
99  i--;
100  }
101  }
102 }
103 
105 {
106  if (!is_open())
107  return;
108 
110  boost::bind(&MavlinkComm::async_read_end, this, boost::asio::placeholders::error,
111  boost::asio::placeholders::bytes_transferred));
112 }
113 
114 void MavlinkComm::async_read_end(const boost::system::error_code &error, size_t bytes_transferred)
115 {
116  if (!is_open())
117  return;
118 
119  if (error)
120  {
121  close();
122  return;
123  }
124 
125  for (int i = 0; i < bytes_transferred; i++)
126  {
128  {
129  for (int i = 0; i < listeners_.size(); i++)
130  {
131  listeners_[i]->handle_mavlink_message(msg_in_);
132  }
133  }
134  }
135 
136  async_read();
137 }
138 
139 void MavlinkComm::send_message(const mavlink_message_t &msg)
140 {
141  WriteBuffer *buffer = new WriteBuffer();
142  buffer->len = mavlink_msg_to_send_buffer(buffer->data, &msg);
143  assert(buffer->len <= MAVLINK_MAX_PACKET_LEN);
144 
145  {
146  mutex_lock lock(mutex_);
147  write_queue_.push_back(buffer);
148  }
149 
150  async_write(true);
151 }
152 
153 void MavlinkComm::async_write(bool check_write_state)
154 {
155  if (check_write_state && write_in_progress_)
156  return;
157 
158  mutex_lock lock(mutex_);
159  if (write_queue_.empty())
160  return;
161 
162  write_in_progress_ = true;
163  WriteBuffer *buffer = write_queue_.front();
164  do_async_write(boost::asio::buffer(buffer->dpos(), buffer->nbytes()),
165  boost::bind(&MavlinkComm::async_write_end, this, boost::asio::placeholders::error,
166  boost::asio::placeholders::bytes_transferred));
167 }
168 
169 void MavlinkComm::async_write_end(const boost::system::error_code &error, std::size_t bytes_transferred)
170 {
171  if (error)
172  {
173  std::cerr << error.message() << std::endl;
174  close();
175  return;
176  }
177 
178  mutex_lock lock(mutex_);
179  if (write_queue_.empty())
180  {
181  write_in_progress_ = false;
182  return;
183  }
184 
185  WriteBuffer *buffer = write_queue_.front();
186  buffer->pos += bytes_transferred;
187  if (buffer->nbytes() == 0)
188  {
189  write_queue_.pop_front();
190  delete buffer;
191  }
192 
193  if (write_queue_.empty())
194  write_in_progress_ = false;
195  else
196  async_write(false);
197 }
198 
199 } // namespace mavrosflight
void async_read()
Initiate an asynchronous read operation.
boost::asio::io_service io_service_
boost io service provider
Definition: mavlink_comm.h:109
void async_read_end(const boost::system::error_code &error, size_t bytes_transferred)
Handler for end of asynchronous read operation.
uint8_t read_buf_raw_[MAVLINK_SERIAL_READ_BUF_SIZE]
Definition: mavlink_comm.h:184
Describes an interface classes can implement to receive and handle mavlink messages.
bool write_in_progress_
flag for whether async_write is already running
Definition: mavlink_comm.h:190
std::vector< MavlinkListenerInterface * > listeners_
listeners for mavlink messages
Definition: mavlink_comm.h:176
void close()
Stops communication and closes the port.
mavlink_message_t msg_in_
Definition: mavlink_comm.h:186
boost::lock_guard< boost::recursive_mutex > mutex_lock
Convenience typedef for mutex lock.
Definition: mavlink_comm.h:141
virtual bool is_open()=0
boost::thread io_thread_
thread on which the io service runs
Definition: mavlink_comm.h:178
void register_mavlink_listener(MavlinkListenerInterface *const listener)
Register a listener for mavlink messages.
mavlink_status_t status_in_
Definition: mavlink_comm.h:187
uint8_t data[MAVLINK_MAX_PACKET_LEN]
Definition: mavlink_comm.h:121
void open()
Opens the port and begins communication.
void unregister_mavlink_listener(MavlinkListenerInterface *const listener)
Unregister a listener for mavlink messages.
virtual void do_open()=0
virtual void do_async_read(const boost::asio::mutable_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler)=0
std::list< WriteBuffer * > write_queue_
queue of buffers to be written to the serial port
Definition: mavlink_comm.h:189
MavlinkComm()
Instantiates the class and begins communication on the specified serial port.
void async_write_end(const boost::system::error_code &error, size_t bytes_transferred)
Handler for end of asynchronous write operation.
~MavlinkComm()
Stops communication and closes the serial port before the object is destroyed.
virtual void do_close()=0
boost::recursive_mutex mutex_
mutex for threadsafe operation
Definition: mavlink_comm.h:179
Struct for buffering the contents of a mavlink message.
Definition: mavlink_comm.h:119
void async_write(bool check_write_state)
Initialize an asynchronous write operation.
virtual void do_async_write(const boost::asio::const_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler)=0
void send_message(const mavlink_message_t &msg)
Send a mavlink message.


rosflight
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:09:25