Go to the documentation of this file.00001 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
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 
00042 class MAVConnUDP : public MAVConnInterface {
00043 public:
00050         MAVConnUDP(uint8_t system_id = 1, uint8_t component_id = MAV_COMP_ID_UDP_BRIDGE,
00051                         std::string bind_host = "localhost", unsigned short bind_port = 14555,
00052                         std::string remote_host = "", unsigned short remote_port = 14550);
00053         ~MAVConnUDP();
00054 
00055         void close();
00056 
00057         using MAVConnInterface::send_message;
00058         void send_message(const mavlink_message_t *message, uint8_t sysid, uint8_t compid);
00059         void send_bytes(const uint8_t *bytes, size_t length);
00060 
00061         inline mavlink_status_t get_status() { return *mavlink_get_channel_status(channel); };
00062         inline bool is_open() { return socket.is_open(); };
00063 
00064 private:
00065         boost::asio::io_service io_service;
00066         std::unique_ptr<boost::asio::io_service::work> io_work;
00067         std::thread io_thread;
00068 
00069         std::atomic<bool> remote_exists;
00070         boost::asio::ip::udp::socket socket;
00071         boost::asio::ip::udp::endpoint remote_ep;
00072         boost::asio::ip::udp::endpoint last_remote_ep;
00073         boost::asio::ip::udp::endpoint bind_ep;
00074 
00075         std::atomic<bool> tx_in_progress;
00076         std::list<MsgBuffer*> tx_q;
00077         uint8_t rx_buf[MsgBuffer::MAX_SIZE];
00078         std::recursive_mutex mutex;
00079 
00080         void do_recvfrom();
00081         void async_receive_end(boost::system::error_code, size_t bytes_transferred);
00082         void do_sendto(bool check_tx_state);
00083         void async_sendto_end(boost::system::error_code, size_t bytes_transferred);
00084 };
00085 
00086 }; 
00087