File indexing completed on 2024-06-09 05:46:34

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 #include "mavlinkconnection.h"
0022 
0023 MAVLinkConnection::MAVLinkConnection(uint8_t channel, QObject *parent)
0024     : Kirogi::AbstractConnection(parent)
0025     , m_channel(channel)
0026 {
0027     m_gcsHeartbeatTimer.setInterval(1000);
0028     QObject::connect(&m_gcsHeartbeatTimer, &QTimer::timeout, this, &MAVLinkConnection::sendHeartbeat);
0029     m_gcsHeartbeatTimer.start();
0030 }
0031 
0032 MAVLinkConnection::~MAVLinkConnection()
0033 {
0034 }
0035 
0036 void MAVLinkConnection::parseData(const QByteArray &bytes)
0037 {
0038     mavlink_status_t *status = mavlink_get_channel_status(m_channel);
0039 
0040     for (const auto &byte : bytes) {
0041         if (!mavlink_parse_char(m_channel, byte, &m_message, status)) {
0042             continue;
0043         }
0044         if (m_message.sysid == MAVLinkPluginConfig::instance().sysid()) {
0045             continue;
0046         }
0047         if (state() < State::Connected) {
0048             setState(State::Connected);
0049         }
0050         emit messageReceived(this, m_message);
0051     }
0052 }
0053 
0054 void MAVLinkConnection::sendHeartbeat()
0055 {
0056     mavlink_message_t message;
0057     mavlink_msg_heartbeat_pack_chan(MAVLinkPluginConfig::instance().sysid(),
0058                                     MAVLinkPluginConfig::instance().compid(),
0059                                     m_channel,
0060                                     &message,
0061                                     MAV_TYPE_GCS,
0062                                     MAV_AUTOPILOT_INVALID,
0063                                     MAV_MODE_MANUAL_ARMED,
0064                                     0,
0065                                     MAV_STATE_ACTIVE);
0066     sendMessage(message);
0067 }