From d4c2945a48cd42a40fd91555704660b03079d84c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 26 Nov 2018 22:18:46 +1100 Subject: [PATCH] AP_RCProtocol: increased robustness of SBUS parsing take full advantage of the frame gap to reduce CPU and the chance of a bad decode --- libraries/AP_RCProtocol/AP_RCProtocol_SBUS.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libraries/AP_RCProtocol/AP_RCProtocol_SBUS.cpp b/libraries/AP_RCProtocol/AP_RCProtocol_SBUS.cpp index f699c32fab..357e002036 100644 --- a/libraries/AP_RCProtocol/AP_RCProtocol_SBUS.cpp +++ b/libraries/AP_RCProtocol/AP_RCProtocol_SBUS.cpp @@ -226,11 +226,22 @@ void AP_RCProtocol_SBUS::process_pulse(uint32_t width_s0, uint32_t width_s1) // support byte input void AP_RCProtocol_SBUS::_process_byte(uint32_t timestamp_us, uint8_t b) { - if (timestamp_us - byte_input.last_byte_us > 2000U || + const bool have_frame_gap = (timestamp_us - byte_input.last_byte_us > 2000U); + if (have_frame_gap && byte_input.ofs == sizeof(byte_input.buf)) { byte_input.ofs = 0; } byte_input.last_byte_us = timestamp_us; + + if (b != 0x0F && byte_input.ofs == 0) { + // definately not SBUS, missing header byte + return; + } + if (byte_input.ofs == 0 && !have_frame_gap) { + // must have a frame gap before the start of a new SBUS frame + return; + } + byte_input.buf[byte_input.ofs++] = b; if (byte_input.ofs == sizeof(byte_input.buf)) {