From cfb78dd92a3a6f8a459eba24601159bb5ffe2a87 Mon Sep 17 00:00:00 2001 From: Tommy Vestermark Date: Sat, 29 Aug 2015 16:03:13 +0200 Subject: [PATCH] FSK: Fix pulse overflow and integer overflow Avoids crash when detecting too many FSK pulses. Frequncy estimaters could overflow when F1 and F2 are too far apart. --- src/pulse_detect.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/pulse_detect.c b/src/pulse_detect.c index 82d251f4..b3fe5f5b 100644 --- a/src/pulse_detect.c +++ b/src/pulse_detect.c @@ -50,8 +50,8 @@ typedef struct { PD_STATE_FSK_F2 = 1 // Low pulse (gap) } state_fsk; - int16_t fm_f1_est; // Estimate for the F1 frequency for FSK - int16_t fm_delta_est; // Estimate for the delta |F2-F1| frequency for FSK + int fm_f1_est; // Estimate for the F1 frequency for FSK + int fm_delta_est; // Estimate for the delta |F2-F1| frequency for FSK } pulse_state_t; static pulse_state_t pulse_state; @@ -106,9 +106,9 @@ int detect_pulse_package(const int16_t *envelope_data, const int16_t *fm_data, u } } else { // FSK Demodulation - int16_t fm_n = fm_data[s->data_counter]; // Get current FM sample - int16_t fm_delta = abs(fm_n - s->fm_f1_est); // Get delta from base frequency estimate - int16_t fm_hyst = (s->fm_delta_est/2) / 8; // ±12% hysteresis on threshold + int fm_n = fm_data[s->data_counter]; // Get current FM sample + int fm_delta = abs(fm_n - s->fm_f1_est); // Get delta from base frequency estimate + int fm_hyst = (s->fm_delta_est/2) / 8; // ±12% hysteresis on threshold switch(s->state_fsk) { case PD_STATE_FSK_F1: // Pulse high at initial frequency // Initial samples in OOK pulse? @@ -131,6 +131,11 @@ int detect_pulse_package(const int16_t *envelope_data, const int16_t *fm_data, u fsk_pulses->num_pulses++; s->fsk_pulse_length = 0; s->state_fsk = PD_STATE_FSK_F1; + // EOP if too many pulses + if (fsk_pulses->num_pulses >= PD_MAX_PULSES) { + s->state = PD_STATE_IDLE; + return 2; // FSK: End Of Package!! + } // Still above threshold } else { s->fm_delta_est = s->fm_delta_est - s->fm_delta_est/FSK_EST_RATIO + fm_delta/FSK_EST_RATIO; // Slow estimator