00001 00009 /* 00010 * Copyright 2013,2014 Vladimir Ermakov. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU General Public License as published by 00014 * the Free Software Foundation; either version 3 of the License, or 00015 * (at your option) any later version. 00016 * 00017 * This program is distributed in the hope that it will be useful, but 00018 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00019 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00020 * for more details. 00021 * 00022 * You should have received a copy of the GNU General Public License along 00023 * with this program; if not, write to the Free Software Foundation, Inc., 00024 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00025 */ 00026 00027 #pragma once 00028 00029 #include <list> 00030 #include <atomic> 00031 #include <boost/asio.hpp> 00032 #include <mavconn/interface.h> 00033 #include <mavconn/msgbuffer.h> 00034 00035 namespace mavconn { 00036 00040 class MAVConnSerial : public MAVConnInterface { 00041 public: 00048 MAVConnSerial(uint8_t system_id = 1, uint8_t component_id = MAV_COMP_ID_UDP_BRIDGE, 00049 std::string device = "/dev/ttyACM0", unsigned baudrate = 57600); 00050 ~MAVConnSerial(); 00051 00052 void close(); 00053 00054 using MAVConnInterface::send_message; 00055 void send_message(const mavlink_message_t *message, uint8_t sysid, uint8_t compid); 00056 void send_bytes(const uint8_t *bytes, size_t length); 00057 00058 inline mavlink_status_t get_status() { return *mavlink_get_channel_status(channel); }; 00059 inline bool is_open() { return serial_dev.is_open(); }; 00060 00061 private: 00062 boost::asio::io_service io_service; 00063 std::thread io_thread; 00064 boost::asio::serial_port serial_dev; 00065 00066 std::atomic<bool> tx_in_progress; 00067 std::list<MsgBuffer*> tx_q; 00068 uint8_t rx_buf[MsgBuffer::MAX_SIZE]; 00069 std::recursive_mutex mutex; 00070 00071 void do_read(); 00072 void async_read_end(boost::system::error_code, size_t bytes_transferred); 00073 void do_write(bool check_tx_state); 00074 void async_write_end(boost::system::error_code, size_t bytes_transferred); 00075 }; 00076 00077 }; // namespace mavconn 00078