mavlink_comm.h
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 
37 #ifndef MAVROSFLIGHT_MAVLINK_COMM_H
38 #define MAVROSFLIGHT_MAVLINK_COMM_H
39 
42 
43 #include <boost/asio.hpp>
44 #include <boost/function.hpp>
45 #include <boost/thread.hpp>
46 
47 #include <cstdint>
48 #include <iostream>
49 #include <list>
50 #include <string>
51 #include <vector>
52 
53 #define MAVLINK_SERIAL_READ_BUF_SIZE 256
54 
55 namespace mavrosflight
56 {
58 {
59 public:
65  MavlinkComm();
66 
70  ~MavlinkComm();
71 
75  void open();
76 
80  void close();
81 
87 
93 
98  void send_message(const mavlink_message_t &msg);
99 
100 protected:
101  virtual bool is_open() = 0;
102  virtual void do_open() = 0;
103  virtual void do_close() = 0;
104  virtual void do_async_read(const boost::asio::mutable_buffers_1 &buffer,
105  boost::function<void(const boost::system::error_code &, size_t)> handler) = 0;
106  virtual void do_async_write(const boost::asio::const_buffers_1 &buffer,
107  boost::function<void(const boost::system::error_code &, size_t)> handler) = 0;
108 
109  boost::asio::io_service io_service_;
110 
111 private:
112  //===========================================================================
113  // definitions
114  //===========================================================================
115 
119  struct WriteBuffer
120  {
122  size_t len;
123  size_t pos;
124 
125  WriteBuffer() : len(0), pos(0) {}
126 
127  WriteBuffer(const uint8_t *buf, uint16_t len) : len(len), pos(0)
128  {
129  assert(len <= MAVLINK_MAX_PACKET_LEN);
130  memcpy(data, buf, len);
131  }
132 
133  const uint8_t *dpos() const { return data + pos; }
134 
135  size_t nbytes() const { return len - pos; }
136  };
137 
141  typedef boost::lock_guard<boost::recursive_mutex> mutex_lock;
142 
143  //===========================================================================
144  // methods
145  //===========================================================================
146 
150  void async_read();
151 
157  void async_read_end(const boost::system::error_code &error, size_t bytes_transferred);
158 
163  void async_write(bool check_write_state);
164 
170  void async_write_end(const boost::system::error_code &error, size_t bytes_transferred);
171 
172  //===========================================================================
173  // member variables
174  //===========================================================================
175 
176  std::vector<MavlinkListenerInterface *> listeners_;
177 
178  boost::thread io_thread_;
179  boost::recursive_mutex mutex_;
180 
181  uint8_t sysid_;
182  uint8_t compid_;
183 
185 
186  mavlink_message_t msg_in_;
188 
189  std::list<WriteBuffer *> write_queue_;
191 };
192 
193 } // namespace mavrosflight
194 
195 #endif // MAVROSFLIGHT_MAVLINK_COMM_H
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.
WriteBuffer(const uint8_t *buf, uint16_t len)
Definition: mavlink_comm.h:127
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