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/thread.hpp>
45 #include <boost/function.hpp>
46 
47 #include <list>
48 #include <string>
49 #include <vector>
50 #include <iostream>
51 
52 #include <stdint.h>
53 
54 #define MAVLINK_SERIAL_READ_BUF_SIZE 256
55 
56 namespace mavrosflight
57 {
58 
60 {
61 public:
62 
68  MavlinkComm();
69 
73  ~MavlinkComm();
74 
78  void open();
79 
83  void close();
84 
90 
96 
101  void send_message(const mavlink_message_t &msg);
102 
103 protected:
104  virtual bool is_open() = 0;
105  virtual void do_open() = 0;
106  virtual void do_close() = 0;
107  virtual void do_async_read(const boost::asio::mutable_buffers_1 &buffer, boost::function<void(const boost::system::error_code&, size_t)> handler) = 0;
108  virtual void do_async_write(const boost::asio::const_buffers_1 &buffer, boost::function<void(const boost::system::error_code&, size_t)> handler) = 0;
109 
110  boost::asio::io_service io_service_;
111 
112 private:
113 
114  //===========================================================================
115  // definitions
116  //===========================================================================
117 
121  struct WriteBuffer
122  {
124  size_t len;
125  size_t pos;
126 
127  WriteBuffer() : len(0), pos(0) {}
128 
129  WriteBuffer(const uint8_t * buf, uint16_t len) : len(len), pos(0)
130  {
131  assert(len <= MAVLINK_MAX_PACKET_LEN);
132  memcpy(data, buf, len);
133  }
134 
135  const uint8_t * dpos() const { return data + pos; }
136 
137  size_t nbytes() const { return len - pos; }
138  };
139 
143  typedef boost::lock_guard<boost::recursive_mutex> mutex_lock;
144 
145  //===========================================================================
146  // methods
147  //===========================================================================
148 
152  void async_read();
153 
159  void async_read_end(const boost::system::error_code& error, size_t bytes_transferred);
160 
165  void async_write(bool check_write_state);
166 
172  void async_write_end(const boost::system::error_code& error, size_t bytes_transferred);
173 
174  //===========================================================================
175  // member variables
176  //===========================================================================
177 
178  std::vector<MavlinkListenerInterface*> listeners_;
179 
180  boost::thread io_thread_;
181  boost::recursive_mutex mutex_;
182 
183  uint8_t sysid_;
184  uint8_t compid_;
185 
187 
188  mavlink_message_t msg_in_;
190 
191  std::list<WriteBuffer*> write_queue_;
193 };
194 
195 } // namespace mavrosflight
196 
197 #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: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.
WriteBuffer(const uint8_t *buf, uint16_t len)
Definition: mavlink_comm.h:129
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