From c0735d136c4ecbd1f09f6a37be14d45a601079da Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Mon, 19 Aug 2024 17:33:58 +1000 Subject: [PATCH] GCS_MAVLink: factor out a check_receiving_cancel method allow reuse of this logic --- libraries/GCS_MAVLink/MissionItemProtocol.cpp | 39 ++++++++++++------- libraries/GCS_MAVLink/MissionItemProtocol.h | 4 ++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/libraries/GCS_MAVLink/MissionItemProtocol.cpp b/libraries/GCS_MAVLink/MissionItemProtocol.cpp index 3d4017fcef..5f2bb6d4cc 100644 --- a/libraries/GCS_MAVLink/MissionItemProtocol.cpp +++ b/libraries/GCS_MAVLink/MissionItemProtocol.cpp @@ -52,6 +52,29 @@ bool MissionItemProtocol::mavlink2_requirement_met(const GCS_MAVLINK &_link, con return false; } +// returns true if we are either not receiving, or we successfully +// cancelled an existing upload: +bool MissionItemProtocol::cancel_upload(const GCS_MAVLINK &_link, const mavlink_message_t &msg) +{ + if (receiving) { + // someone is already uploading a mission. If we are + // receiving from someone then we will allow them to restart - + // otherwise we deny. + if (msg.sysid != dest_sysid || msg.compid != dest_compid) { + // reject another upload until + send_mission_ack(_link, msg, MAV_MISSION_DENIED); + return false; + } + // the upload count may have changed; free resources and + // allocate them again: + free_upload_resources(); + receiving = false; + link = nullptr; + } + + return true; +} + void MissionItemProtocol::handle_mission_count( GCS_MAVLINK &_link, const mavlink_mission_count_t &packet, @@ -61,20 +84,8 @@ void MissionItemProtocol::handle_mission_count( return; } - if (receiving) { - // someone is already uploading a mission. If we are - // receiving from someone then we will allow them to restart - - // otherwise we deny. - if (msg.sysid != dest_sysid || msg.compid != dest_compid) { - // reject another upload until - send_mission_ack(_link, msg, MAV_MISSION_DENIED); - return; - } - // the upload count may have changed; free resources and - // allocate them again: - free_upload_resources(); - receiving = false; - link = nullptr; + if (!cancel_upload(_link, msg)) { + return; } if (packet.count > max_items()) { diff --git a/libraries/GCS_MAVLink/MissionItemProtocol.h b/libraries/GCS_MAVLink/MissionItemProtocol.h index a7df605a42..37f37fd559 100644 --- a/libraries/GCS_MAVLink/MissionItemProtocol.h +++ b/libraries/GCS_MAVLink/MissionItemProtocol.h @@ -79,6 +79,10 @@ protected: private: + // returns true if we are either not receiving, or we successfully + // cancelled an existing upload: + bool cancel_upload(const GCS_MAVLINK &_link, const mavlink_message_t &msg); + virtual void truncate(const mavlink_mission_count_t &packet) = 0; uint16_t request_i; // request index