mavros_plugin.h
Go to the documentation of this file.
1 
10 /*
11  * Copyright 2013,2014,2015 Vladimir Ermakov.
12  *
13  * This file is part of the mavros package and subject to the license terms
14  * in the top-level LICENSE file of the mavros repository.
15  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
16  */
17 
18 #pragma once
19 
20 #include <tuple>
21 #include <vector>
22 #include <functional>
24 #include <mavconn/interface.h>
25 #include <mavros/mavros_uas.h>
26 
27 namespace mavros {
28 namespace plugin {
29 using mavros::UAS;
30 typedef std::lock_guard<std::recursive_mutex> lock_guard;
31 typedef std::unique_lock<std::recursive_mutex> unique_lock;
32 
37 {
38 private:
39  PluginBase(const PluginBase&) = delete;
40 
41 public:
45  using HandlerInfo = std::tuple<mavlink::msgid_t, const char*, size_t, HandlerCb>;
47  using Subscriptions = std::vector<HandlerInfo>;
48 
49  // pluginlib return boost::shared_ptr
52 
53  virtual ~PluginBase() {};
54 
60  virtual void initialize(UAS &uas) {
61  m_uas = &uas;
62  }
63 
67  virtual Subscriptions get_subscriptions() = 0;
68 
69 protected:
74  PluginBase() : m_uas(nullptr) {};
75 
76  UAS *m_uas;
77 
78  // TODO: filtered handlers
79 
86  template<class _C>
87  HandlerInfo make_handler(const mavlink::msgid_t id, void (_C::*fn)(const mavlink::mavlink_message_t *msg, const mavconn::Framing framing)) {
88  auto bfn = std::bind(fn, static_cast<_C*>(this), std::placeholders::_1, std::placeholders::_2);
89  const auto type_hash_ = typeid(mavlink::mavlink_message_t).hash_code();
90 
91  return HandlerInfo{ id, nullptr, type_hash_, bfn };
92  }
93 
99  template<class _C, class _T>
100  HandlerInfo make_handler(void (_C::*fn)(const mavlink::mavlink_message_t*, _T &)) {
101  auto bfn = std::bind(fn, static_cast<_C*>(this), std::placeholders::_1, std::placeholders::_2);
102  const auto id = _T::MSG_ID;
103  const auto name = _T::NAME;
104  const auto type_hash_ = typeid(_T).hash_code();
105 
106  return HandlerInfo{
107  id, name, type_hash_,
108  [bfn](const mavlink::mavlink_message_t *msg, const mavconn::Framing framing) {
109  if (framing != mavconn::Framing::ok)
110  return;
111 
112  mavlink::MsgMap map(msg);
113  _T obj;
114  obj.deserialize(map);
115 
116  bfn(msg, obj);
117  }
118  };
119  }
120 
124  virtual void connection_cb(bool connected) {
125  ROS_BREAK();
126  }
127 
131  inline void enable_connection_cb() {
132  m_uas->add_connection_change_handler(std::bind(&PluginBase::connection_cb, this, std::placeholders::_1));
133  }
134 
138  virtual void capabilities_cb(UAS::MAV_CAP capabilities) {
139  ROS_BREAK();
140  }
141 
146  m_uas->add_capabilities_change_handler(std::bind(&PluginBase::capabilities_cb, this, std::placeholders::_1));
147  }
148 };
149 } // namespace plugin
150 } // namespace mavros
std::lock_guard< std::recursive_mutex > lock_guard
Definition: mavros_plugin.h:30
msg
MAVROS Plugin base class.
Definition: mavros_plugin.h:36
std::function< void(const mavlink::mavlink_message_t *message, const Framing framing)> ReceivedCb
void add_capabilities_change_handler(CapabilitiesCb cb)
Adds a function to the capabilities callback queue.
Definition: uas_data.cpp:133
std::unique_lock< std::recursive_mutex > unique_lock
Definition: mavros_plugin.h:31
std::tuple< mavlink::msgid_t, const char *, size_t, HandlerCb > HandlerInfo
Tuple: MSG ID, MSG NAME, message type into hash_code, message handler callback.
Definition: mavros_plugin.h:45
HandlerInfo make_handler(const mavlink::msgid_t id, void(_C::*fn)(const mavlink::mavlink_message_t *msg, const mavconn::Framing framing))
Definition: mavros_plugin.h:87
PluginBase()
Plugin constructor Should not do anything before initialize()
Definition: mavros_plugin.h:74
UAS for plugins.
Definition: mavros_uas.h:67
void add_connection_change_handler(ConnectionCb cb)
Add connection change handler callback.
Definition: uas_data.cpp:83
mavlink::common::MAV_PROTOCOL_CAPABILITY MAV_CAP
Definition: mavros_uas.h:74
virtual void capabilities_cb(UAS::MAV_CAP capabilities)
virtual Subscriptions get_subscriptions()=0
Return vector of MAVLink message subscriptions (handlers)
MAVROS Plugin context.
virtual void connection_cb(bool connected)
std::vector< HandlerInfo > Subscriptions
Subscriptions vector.
Definition: mavros_plugin.h:47
HandlerInfo make_handler(void(_C::*fn)(const mavlink::mavlink_message_t *, _T &))
virtual void initialize(UAS &uas)
Plugin initializer.
Definition: mavros_plugin.h:60
#define ROS_BREAK()
mavconn::MAVConnInterface::ReceivedCb HandlerCb
generic message handler callback
Definition: mavros_plugin.h:43


mavros
Author(s): Vladimir Ermakov
autogenerated on Tue Jun 1 2021 02:36:26