From b80dca38ff31980edf8702345e4a57c8cb32098f Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 6 Jan 2023 10:33:51 +1100 Subject: [PATCH] GCS_MAVLINK: added check_payload_size() method this saves flash by moving common code to cpp --- libraries/GCS_MAVLink/GCS.cpp | 12 ++++++++++++ libraries/GCS_MAVLink/GCS.h | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/libraries/GCS_MAVLink/GCS.cpp b/libraries/GCS_MAVLink/GCS.cpp index aa9575f7a3..7c338a4b9e 100644 --- a/libraries/GCS_MAVLink/GCS.cpp +++ b/libraries/GCS_MAVLink/GCS.cpp @@ -356,3 +356,15 @@ void gcs_out_of_space_to_send(mavlink_channel_t chan) { gcs().chan(chan)->out_of_space_to_send(); } + +/* + check there is enough space for a message + */ +bool GCS_MAVLINK::check_payload_size(uint16_t max_payload_len) +{ + if (txspace() < unsigned(packet_overhead()+max_payload_len)) { + gcs_out_of_space_to_send(chan); + return false; + } + return true; +} diff --git a/libraries/GCS_MAVLink/GCS.h b/libraries/GCS_MAVLink/GCS.h index 563bc77899..6ce0c2083d 100644 --- a/libraries/GCS_MAVLink/GCS.h +++ b/libraries/GCS_MAVLink/GCS.h @@ -38,6 +38,7 @@ // macros used to determine if a message will fit in the space available. void gcs_out_of_space_to_send(mavlink_channel_t chan); +bool check_payload_size(mavlink_channel_t chan, uint16_t max_payload_len); // important note: despite the names, these messages do NOT check to // see if the payload will fit in the buffer. They check to see if @@ -61,7 +62,7 @@ void gcs_out_of_space_to_send(mavlink_channel_t chan); // immediately return false from the current function if there is no // room to fit the mavlink message with id id on the current object's // output -#define CHECK_PAYLOAD_SIZE(id) if (txspace() < unsigned(packet_overhead()+MAVLINK_MSG_ID_ ## id ## _LEN)) { gcs_out_of_space_to_send(chan); return false; } +#define CHECK_PAYLOAD_SIZE(id) if (!check_payload_size(MAVLINK_MSG_ID_ ## id ## _LEN)) return false // CHECK_PAYLOAD_SIZE2 - macro which inserts code which will // immediately return false from the current function if there is no @@ -187,6 +188,8 @@ public: return MIN(_port->txspace(), 8192U); } + bool check_payload_size(uint16_t max_payload_len); + // this is called when we discover we'd like to send something but can't: void out_of_space_to_send() { out_of_space_to_send_count++; }