File indexing completed on 2024-05-26 05:54:04

0001 /*
0002  * Copyright 2019 Patrick José Pereira <patrickjp@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #pragma once
0022 
0023 #include "abstractconnection.h"
0024 
0025 #include "mavlinkpluginconfig.h"
0026 
0027 #include <QByteArray>
0028 #include <QObject>
0029 #include <QTimer>
0030 
0031 #if defined(__GNUC__) || defined(__clang__)
0032 #if __GNUC__ > 8 || defined(__clang__)
0033 #pragma GCC diagnostic push
0034 #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
0035 #pragma GCC diagnostic ignored "-Wpedantic"
0036 #pragma GCC diagnostic ignored "-Wold-style-cast"
0037 #pragma GCC diagnostic ignored "-Wcast-align"
0038 #else
0039 #pragma GCC diagnostic push
0040 #pragma GCC diagnostic ignored "-Wall"
0041 #endif
0042 #else
0043 #pragma warning(push, 0)
0044 #endif
0045 
0046 #include "mavlink.h"
0047 
0048 #if defined(__GNUC__) || defined(__clang__)
0049 #pragma GCC diagnostic pop
0050 #else
0051 #pragma warning(pop)
0052 #endif
0053 
0054 /**
0055  * A base class of UDP, TCP and Serial type connection of mavlink plugin.
0056  *
0057  * This class defines common functionalities of all types of mavlink connections.
0058  */
0059 class MAVLinkConnection : public Kirogi::AbstractConnection
0060 {
0061     Q_OBJECT
0062 
0063 public:
0064     MAVLinkConnection(uint8_t channel, QObject *parent = nullptr);
0065     ~MAVLinkConnection();
0066 
0067     template<typename T>
0068     constexpr void sendMessage(const T &message)
0069     {
0070         if constexpr (std::is_same<T, mavlink_message_t>::value) {
0071             const int length = mavlink_msg_to_send_buffer(m_buffer, &message);
0072             sendBytes({reinterpret_cast<const char *>(m_buffer), length});
0073         } else if constexpr (std::is_same<T, mavlink_command_long_t>::value) {
0074             mavlink_message_t mavlink_message = {};
0075             mavlink_msg_command_long_encode(MAVLinkPluginConfig::instance().sysid(), MAV_COMP_ID_MISSIONPLANNER, &mavlink_message, &message);
0076             sendMessage(mavlink_message);
0077         } else if constexpr (std::is_same<T, mavlink_manual_control_t>::value) {
0078             mavlink_message_t mavlink_message = {};
0079             mavlink_msg_manual_control_encode(MAVLinkPluginConfig::instance().sysid(), MAV_COMP_ID_MISSIONPLANNER, &mavlink_message, &message);
0080             sendMessage(mavlink_message);
0081         } else if constexpr (std::is_same<T, mavlink_param_request_list_t>::value) {
0082             mavlink_message_t mavlink_message = {};
0083             mavlink_msg_param_request_list_encode(MAVLinkPluginConfig::instance().sysid(), MAV_COMP_ID_MISSIONPLANNER, &mavlink_message, &message);
0084             sendMessage(mavlink_message);
0085         }
0086     }
0087 
0088 public Q_SLOTS:
0089     void parseData(const QByteArray &bytes);
0090     void sendHeartbeat();
0091 
0092 Q_SIGNALS:
0093     void messageReceived(MAVLinkConnection *connection, const mavlink_message_t &message);
0094 
0095 protected:
0096     uint8_t m_channel; ///< A channel that mavlink_parse_char() uses to manage internal buffers.
0097 
0098 private:
0099     QTimer m_gcsHeartbeatTimer;
0100     mavlink_message_t m_message; ///< Used for parsing incoming mavlink messages.
0101     mutable uint8_t m_buffer[1024]; ///< Buffer that sendMessage() uses.
0102 };