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 
42 using boost::asio::serial_port_base;
43 
45  io_service_(),
46  write_in_progress_(false)
47 {
48 }
49 
51 {
52 }
53 
55 {
56  // open the port
57  do_open();
58 
59  // start reading from the port
60  async_read();
61  io_thread_ = boost::thread(boost::bind(&boost::asio::io_service::run, &this->io_service_));
62 }
63 
65 {
66  mutex_lock lock(mutex_);
67 
68  io_service_.stop();
69  do_close();
70 
71  if (io_thread_.joinable())
72  {
73  io_thread_.join();
74  }
75 }
76 
78 {
79  if (listener == NULL)
80  return;
81 
82  bool already_registered = false;
83  for (int i = 0; i < listeners_.size(); i++)
84  {
85  if (listener == listeners_[i])
86  {
87  already_registered = true;
88  break;
89  }
90  }
91 
92  if (!already_registered)
93  listeners_.push_back(listener);
94 }
95 
97 {
98  if (listener == NULL)
99  return;
100 
101  for (int i = 0; i < listeners_.size(); i++)
102  {
103  if (listener == listeners_[i])
104  {
105  listeners_.erase(listeners_.begin() + i);
106  i--;
107  }
108  }
109 }
110 
112 {
113  if (!is_open()) return;
114 
116  boost::asio::buffer(read_buf_raw_, MAVLINK_SERIAL_READ_BUF_SIZE),
117  boost::bind(
119  this,
120  boost::asio::placeholders::error,
121  boost::asio::placeholders::bytes_transferred));
122 }
123 
124 void MavlinkComm::async_read_end(const boost::system::error_code &error, size_t bytes_transferred)
125 {
126  if (!is_open()) return;
127 
128  if (error)
129  {
130  close();
131  return;
132  }
133 
134  for (int i = 0; i < bytes_transferred; i++)
135  {
137  {
138  for (int i = 0; i < listeners_.size(); i++)
139  {
140  listeners_[i]->handle_mavlink_message(msg_in_);
141  }
142  }
143  }
144 
145  async_read();
146 }
147 
148 void MavlinkComm::send_message(const mavlink_message_t &msg)
149 {
150  WriteBuffer *buffer = new WriteBuffer();
151  buffer->len = mavlink_msg_to_send_buffer(buffer->data, &msg);
152  assert(buffer->len <= MAVLINK_MAX_PACKET_LEN);
153 
154  {
155  mutex_lock lock(mutex_);
156  write_queue_.push_back(buffer);
157  }
158 
159  async_write(true);
160 }
161 
162 void MavlinkComm::async_write(bool check_write_state)
163 {
164  if (check_write_state && write_in_progress_)
165  return;
166 
167  mutex_lock lock(mutex_);
168  if (write_queue_.empty())
169  return;
170 
171  write_in_progress_ = true;
172  WriteBuffer *buffer = write_queue_.front();
174  boost::asio::buffer(buffer->dpos(), buffer->nbytes()),
175  boost::bind(
177  this,
178  boost::asio::placeholders::error,
179  boost::asio::placeholders::bytes_transferred));
180 
181 }
182 
183 void MavlinkComm::async_write_end(const boost::system::error_code &error, std::size_t bytes_transferred)
184 {
185  if (error)
186  {
187  std::cerr << error.message() << std::endl;
188  close();
189  return;
190  }
191 
192  mutex_lock lock(mutex_);
193  if (write_queue_.empty())
194  {
195  write_in_progress_ = false;
196  return;
197  }
198 
199  WriteBuffer *buffer = write_queue_.front();
200  buffer->pos += bytes_transferred;
201  if (buffer->nbytes() == 0)
202  {
203  write_queue_.pop_front();
204  delete buffer;
205  }
206 
207  if (write_queue_.empty())
208  write_in_progress_ = false;
209  else
210  async_write(false);
211 }
212 
213 } // namespace mavrosflight
void async_read()
Initiate an asynchronous read operation.
boost::asio::io_service io_service_
boost io service provider
Definition: mavlink_comm.h:110
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:186
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:192
void close()
Stops communication and closes the port.
mavlink_message_t msg_in_
Definition: mavlink_comm.h:188
boost::lock_guard< boost::recursive_mutex > mutex_lock
Convenience typedef for mutex lock.
Definition: mavlink_comm.h:143
virtual bool is_open()=0
boost::thread io_thread_
thread on which the io service runs
Definition: mavlink_comm.h:180
void register_mavlink_listener(MavlinkListenerInterface *const listener)
Register a listener for mavlink messages.
mavlink_status_t status_in_
Definition: mavlink_comm.h:189
uint8_t data[MAVLINK_MAX_PACKET_LEN]
Definition: mavlink_comm.h:123
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
std::vector< MavlinkListenerInterface * > listeners_
listeners for mavlink messages
Definition: mavlink_comm.h:178
virtual void do_async_read(const boost::asio::mutable_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler)=0
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:181
Struct for buffering the contents of a mavlink message.
Definition: mavlink_comm.h:121
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.
std::list< WriteBuffer * > write_queue_
queue of buffers to be written to the serial port
Definition: mavlink_comm.h:191


rosflight
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 20:00:12