5 #include <condition_variable> 19 typedef asio::buffers_iterator<asio::streambuf::const_buffers_type>
iterator;
62 bool setVersion(
const int& ver);
68 int getVersion()
const;
86 bool start(
const std::string& device,
const size_t baudrate = 115200);
98 bool isConnected()
const;
111 bool sendMessage(
msp::Message& message,
const double& timeout = 0);
128 template <
typename T,
typename C,
129 class =
typename std::enable_if<
130 std::is_base_of<msp::Message, T>::value>::type>
131 std::shared_ptr<SubscriptionBase>
subscribe(
void (C::*callback)(
const T&),
132 C* context,
const double& tp) {
133 return subscribe<T>(std::bind(callback, context, std::placeholders::_1),
146 template <
typename T,
class =
typename std::enable_if<
147 std::is_base_of<msp::Message, T>::value>::type>
149 const std::function<
void(
const T&)>& recv_callback,
const double& tp) {
151 if(!(tp >= 0.0))
throw std::runtime_error(
"Period must be positive!");
154 const msp::ID id = T(fw_variant).id();
155 if(log_level_ >=
INFO)
156 std::cout <<
"SUBSCRIBING TO " <<
id << std::endl;
159 std::function<bool(const Message&)> send_callback =
163 auto subscription = std::make_shared<Subscription<T>>(
164 recv_callback, send_callback, std::make_unique<T>(fw_variant), tp);
167 std::lock_guard<std::mutex> lock(mutex_subscriptions);
170 if(subscriptions.count(
id)) subscriptions.erase(
id);
173 subscriptions.emplace(
id, std::move(subscription));
174 return subscriptions[
id];
183 return (subscriptions.count(
id) == 1);
192 return subscriptions.at(
id);
202 void processOneMessage(
const asio::error_code& ec,
203 const std::size_t& bytes_transferred);
222 if(!data)
return sendData(
id);
223 return sendData(
id, *data);
231 bool connectPort(
const std::string& device,
const size_t baudrate = 115200);
237 bool disconnectPort();
243 bool startReadThread();
249 bool stopReadThread();
255 bool startSubscriptions();
261 bool stopSubscriptions();
267 uint8_t extractChar();
276 std::pair<iterator, bool> messageReady(iterator begin, iterator end)
const;
310 uint8_t crcV1(
const uint8_t
id,
const ByteVector& data)
const;
328 uint8_t crcV2(uint8_t crc,
const ByteVector& data)
const;
336 uint8_t crcV2(uint8_t crc,
const uint8_t& b)
const;
345 std::atomic_flag running_ = ATOMIC_FLAG_INIT;
std::condition_variable cv_response
bool sendMessageNoWait(const msp::Message &message)
Send a message, but do not wait for any response.
std::mutex mutex_response
bool sendData(const msp::ID id, const ByteVectorUptr &&data)
Send an ID and payload to the flight controller.
std::mutex mutex_subscriptions
asio::io_service io
! io service
std::unique_ptr< ReceivedMessage > request_received
FirmwareVariant
Enum of firmware variants.
std::shared_ptr< SubscriptionBase > getSubscription(const msp::ID &id)
Get pointer to subscription.
std::unique_ptr< ByteVector > ByteVectorUptr
std::map< msp::ID, std::shared_ptr< SubscriptionBase > > subscriptions
asio::buffers_iterator< asio::streambuf::const_buffers_type > iterator
std::mutex cv_response_mtx
std::shared_ptr< SubscriptionBase > subscribe(void(C::*callback)(const T &), C *context, const double &tp)
Register callback function that is called when a message of matching ID is received.
asio::serial_port port
! port for serial device
bool hasSubscription(const msp::ID &id) const
Check if message ID already has a subscription.
std::shared_ptr< SubscriptionBase > subscribe(const std::function< void(const T &)> &recv_callback, const double &tp)
Register callback function that is called when a message of matching ID is received. Replaces the previous callback of the same ID.
FirmwareVariant fw_variant