GCS_MAVLink: send FTP replies directly from the FTP thread

this saves over 700 bytes of memory while also making ftp faster
This commit is contained in:
Andrew Tridgell 2023-01-02 07:49:26 +11:00 committed by Randy Mackay
parent 77cb12772d
commit bbee8312dd
3 changed files with 24 additions and 48 deletions

View File

@ -890,7 +890,6 @@ private:
struct ftp_state { struct ftp_state {
ObjectBuffer<pending_ftp> *requests; ObjectBuffer<pending_ftp> *requests;
ObjectBuffer<pending_ftp> *replies;
// session specific info, currently only support a single session over all links // session specific info, currently only support a single session over all links
int fd = -1; int fd = -1;
@ -907,7 +906,7 @@ private:
bool ftp_init(void); bool ftp_init(void);
void handle_file_transfer_protocol(const mavlink_message_t &msg); void handle_file_transfer_protocol(const mavlink_message_t &msg);
void send_ftp_replies(void); bool send_ftp_reply(const pending_ftp &reply);
void ftp_worker(void); void ftp_worker(void);
void ftp_push_replies(pending_ftp &reply); void ftp_push_replies(pending_ftp &reply);

View File

@ -1044,7 +1044,7 @@ uint16_t GCS_MAVLINK::get_reschedule_interval_ms(const deferred_message_bucket_t
// we are sending requests for waypoints, penalize streams: // we are sending requests for waypoints, penalize streams:
interval_ms *= 4; interval_ms *= 4;
} }
if (ftp.replies && AP_HAL::millis() - ftp.last_send_ms < 500) { if (AP_HAL::millis() - ftp.last_send_ms < 500) {
// we are sending ftp replies // we are sending ftp replies
interval_ms *= 4; interval_ms *= 4;
} }
@ -1221,7 +1221,6 @@ void GCS_MAVLINK::update_send()
AP::logger().handle_log_send(); AP::logger().handle_log_send();
} }
#endif #endif
send_ftp_replies();
if (!deferred_messages_initialised) { if (!deferred_messages_initialised) {
initialise_message_intervals_from_streamrates(); initialise_message_intervals_from_streamrates();

View File

@ -47,10 +47,6 @@ bool GCS_MAVLINK::ftp_init(void) {
if (ftp.requests == nullptr) { if (ftp.requests == nullptr) {
goto failed; goto failed;
} }
ftp.replies = new ObjectBuffer<pending_ftp>(30);
if (ftp.replies == nullptr) {
goto failed;
}
if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&GCS_MAVLINK::ftp_worker, void), if (!hal.scheduler->thread_create(FUNCTOR_BIND_MEMBER(&GCS_MAVLINK::ftp_worker, void),
"FTP", 2560, AP_HAL::Scheduler::PRIORITY_IO, 0)) { "FTP", 2560, AP_HAL::Scheduler::PRIORITY_IO, 0)) {
@ -62,8 +58,6 @@ bool GCS_MAVLINK::ftp_init(void) {
failed: failed:
delete ftp.requests; delete ftp.requests;
ftp.requests = nullptr; ftp.requests = nullptr;
delete ftp.replies;
ftp.replies = nullptr;
gcs().send_text(MAV_SEVERITY_WARNING, "failed to initialize MAVFTP"); gcs().send_text(MAV_SEVERITY_WARNING, "failed to initialize MAVFTP");
return false; return false;
@ -96,32 +90,20 @@ void GCS_MAVLINK::handle_file_transfer_protocol(const mavlink_message_t &msg) {
} }
} }
void GCS_MAVLINK::send_ftp_replies(void) bool GCS_MAVLINK::send_ftp_reply(const pending_ftp &reply)
{ {
/* /*
provide same banner we would give with old param download provide same banner we would give with old param download
*/ */
if (ftp.need_banner_send_mask & (1U<<chan)) { if (ftp.need_banner_send_mask & (1U<<reply.chan)) {
ftp.need_banner_send_mask &= ~(1U<<chan); ftp.need_banner_send_mask &= ~(1U<<reply.chan);
send_banner(); send_banner();
} }
WITH_SEMAPHORE(comm_chan_lock(reply.chan));
if (ftp.replies == nullptr || ftp.replies->is_empty()) {
return;
}
for (uint8_t i = 0; i < 20; i++) {
if (!HAVE_PAYLOAD_SPACE(chan, FILE_TRANSFER_PROTOCOL)) { if (!HAVE_PAYLOAD_SPACE(chan, FILE_TRANSFER_PROTOCOL)) {
return; return false;
} }
if ((i > 0) && comm_get_txspace(chan) < (2 * (packet_overhead() + MAVLINK_MSG_ID_FILE_TRANSFER_PROTOCOL_LEN))) {
// if this isn't the first packet we have to leave deadspace for the next message
return;
}
struct pending_ftp reply;
uint8_t payload[251] = {}; uint8_t payload[251] = {};
if (ftp.replies->peek(reply) && (reply.chan == chan)) {
put_le16_ptr(payload, reply.seq_number); put_le16_ptr(payload, reply.seq_number);
payload[2] = reply.session; payload[2] = reply.session;
payload[3] = static_cast<uint8_t>(reply.opcode); payload[3] = static_cast<uint8_t>(reply.opcode);
@ -134,12 +116,8 @@ void GCS_MAVLINK::send_ftp_replies(void)
reply.chan, reply.chan,
0, reply.sysid, reply.compid, 0, reply.sysid, reply.compid,
payload); payload);
ftp.replies->pop();
ftp.last_send_ms = AP_HAL::millis(); ftp.last_send_ms = AP_HAL::millis();
} else { return true;
return;
}
}
} }
void GCS_MAVLINK::ftp_error(struct pending_ftp &response, FTP_ERROR error) { void GCS_MAVLINK::ftp_error(struct pending_ftp &response, FTP_ERROR error) {
@ -168,7 +146,7 @@ void GCS_MAVLINK::ftp_error(struct pending_ftp &response, FTP_ERROR error) {
// send our response back out to the system // send our response back out to the system
void GCS_MAVLINK::ftp_push_replies(pending_ftp &reply) void GCS_MAVLINK::ftp_push_replies(pending_ftp &reply)
{ {
while (!ftp.replies->push(reply)) { // we must fit the response, keep shoving it in while (!send_ftp_reply(reply)) {
hal.scheduler->delay(2); hal.scheduler->delay(2);
} }
} }