From 08b32779c5e7b826ed413bdbecfb5ba173fceaf2 Mon Sep 17 00:00:00 2001 From: bugobliterator Date: Tue, 17 Oct 2023 16:33:02 +1100 Subject: [PATCH] AP_DroneCAN: add support for detecting downed link --- libraries/AP_DroneCAN/AP_Canard_iface.cpp | 30 +++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/libraries/AP_DroneCAN/AP_Canard_iface.cpp b/libraries/AP_DroneCAN/AP_Canard_iface.cpp index 6b76690f59ff7f..5fc326ecfb1787 100644 --- a/libraries/AP_DroneCAN/AP_Canard_iface.cpp +++ b/libraries/AP_DroneCAN/AP_Canard_iface.cpp @@ -101,7 +101,8 @@ bool CanardInterface::broadcast(const Canard::Transfer &bcast_transfer) { #endif }; // do canard broadcast - int16_t ret = canardBroadcastObj(&canard, &tx_transfer); + volatile int16_t ret; + ret = canardBroadcastObj(&canard, &tx_transfer); #if AP_TEST_DRONECAN_DRIVERS if (this == &test_iface) { test_iface_sem.give(); @@ -219,6 +220,18 @@ void CanardInterface::processTx(bool raw_commands_only = false) { if (txq == nullptr) { return; } + const auto *stats = ifaces[iface]->get_statistics(); + bool iface_down = true; + if (stats == nullptr || (AP_HAL::micros64() - stats->last_transmit_us) < 200000UL) { + /* + We were not able to queue the frame for + sending. Only mark the send as failing if the + interface is active. We consider an interface as + active if it has had a successful transmit in the + last 500 milliseconds + */ + iface_down = false; + } // scan through list of pending transfers while (true) { auto txf = &txq->frame; @@ -245,15 +258,22 @@ void CanardInterface::processTx(bool raw_commands_only = false) { if (!write) { // if there is no space then we need to start from the // top of the queue, so wait for the next loop - break; - } - if ((txf->iface_mask & (1U<deadline_usec)) { + if (!iface_down) { + break; + } else { + txf->iface_mask &= ~(1U<iface_mask & (1U<deadline_usec)) { // try sending to interfaces, clearing the mask if we succeed if (ifaces[iface]->send(txmsg, txf->deadline_usec, 0) > 0) { txf->iface_mask &= ~(1U<iface_mask &= ~(1U<