From 7a58e3cdc5cbfa731d112d5867240aa89688e84a Mon Sep 17 00:00:00 2001 From: Oliver Jowett Date: Sun, 20 Mar 2016 19:47:27 +0000 Subject: [PATCH] Move Mode C altitude calc into mode A/C message decoding. If SPI is set, it cannot be mode C. --- mode_ac.c | 43 ++++++++++++++++++++++++++----------------- track.c | 6 +----- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/mode_ac.c b/mode_ac.c index 7c376e0..37c20c8 100644 --- a/mode_ac.c +++ b/mode_ac.c @@ -75,28 +75,37 @@ int ModeAToModeC(unsigned int ModeA) //========================================================================= // void decodeModeAMessage(struct modesMessage *mm, int ModeA) - { - mm->msgtype = 32; // Valid Mode S DF's are DF-00 to DF-31. - // so use 32 to indicate Mode A/C +{ + mm->msgtype = 32; // Valid Mode S DF's are DF-00 to DF-31. + // so use 32 to indicate Mode A/C - mm->msgbits = 16; // Fudge up a Mode S style data stream - mm->msg[0] = (ModeA >> 8); - mm->msg[1] = (ModeA); + mm->msgbits = 16; // Fudge up a Mode S style data stream + mm->msg[0] = (ModeA >> 8); + mm->msg[1] = (ModeA); - // Fudge an address based on Mode A (remove the Ident bit) - mm->addr = (ModeA & 0x0000FF7F) | MODES_NON_ICAO_ADDRESS; + // Fudge an address based on Mode A (remove the Ident bit) + mm->addr = (ModeA & 0x0000FF7F) | MODES_NON_ICAO_ADDRESS; - // Set the Identity field to ModeA - mm->modeA = ModeA & 0x7777; - mm->bFlags |= MODES_ACFLAGS_SQUAWK_VALID; + // Set the Identity field to ModeA + mm->modeA = ModeA & 0x7777; + mm->bFlags |= MODES_ACFLAGS_SQUAWK_VALID; - // Flag ident in flight status - mm->fs = ModeA & 0x0080; + // Flag ident in flight status + mm->fs = ModeA & 0x0080; - // Not much else we can tell from a Mode A/C reply. - // Just fudge up a few bits to keep other code happy - mm->correctedbits = 0; - } + // Decode an altitude if this looks like a possible mode C + if (!mm->fs) { + int modeC = ModeAToModeC(ModeA); + if (modeC >= -12) { + mm->altitude = modeC * 100; + mm->bFlags |= MODES_ACFLAGS_ALTITUDE_VALID; + } + } + + // Not much else we can tell from a Mode A/C reply. + // Just fudge up a few bits to keep other code happy + mm->correctedbits = 0; +} // // ===================== Mode A/C detection and decoding =================== // diff --git a/track.c b/track.c index 4fc7763..ca51267 100644 --- a/track.c +++ b/track.c @@ -73,13 +73,9 @@ struct aircraft *trackCreateAircraft(struct modesMessage *mm) { // set them once here during initialisation, and don't bother to set them every // time this ModeA/C is received again in the future if (mm->msgtype == 32) { - int modeC = ModeAToModeC(mm->modeA | mm->fs); a->modeACflags = MODEAC_MSG_FLAG; - if (modeC < -12) { + if (!(mm->bFlags & MODES_ACFLAGS_ALTITUDE_VALID)) { a->modeACflags |= MODEAC_MSG_MODEA_ONLY; - } else { - mm->altitude = modeC * 100; - mm->bFlags |= MODES_ACFLAGS_ALTITUDE_VALID; } }