Skip to content

Commit

Permalink
Stats overhaul.
Browse files Browse the repository at this point in the history
  • Loading branch information
mutability committed Jan 22, 2015
1 parent 6672d92 commit a59077a
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 208 deletions.
44 changes: 15 additions & 29 deletions demod_2000.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
useModesMessage(&mm);

j += MODEAC_MSG_SAMPLES;
Modes.stats_current.ModeAC++;
Modes.stats_current.demod_modeac++;
continue;
}
}
Expand Down Expand Up @@ -398,7 +398,7 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
dumpRawMessage("Too high level in samples between 10 and 15", msg, m, j);
continue;
}
Modes.stats_current.valid_preamble++;
Modes.stats_current.demod_preambles++;
}

else {
Expand All @@ -407,7 +407,6 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
// Make a copy of the Payload, and phase correct the copy
memcpy(aux, &pPreamble[-1], sizeof(aux));
applyPhaseCorrection(&aux[1]);
Modes.stats_current.out_of_phase++;
pPayload = &aux[1 + MODES_PREAMBLE_SAMPLES];
// TODO ... apply other kind of corrections
}
Expand Down Expand Up @@ -475,7 +474,6 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
msglen = MODES_SHORT_MSG_BITS;
msg[0] ^= theErrs; errorsTy = 0;
errors = errors56; // revert to the number of errors prior to bit 56
Modes.stats_current.DF_Len_Corrected++;

} else if (i < MODES_LONG_MSG_BITS) {
msglen = MODES_SHORT_MSG_BITS;
Expand Down Expand Up @@ -517,7 +515,6 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
if (validDFbits & thisDFbit) {
// Yep, more likely, so update the main message
msg[0] = theByte;
Modes.stats_current.DF_Type_Corrected++;
errors--; // decrease the error count so we attempt to use the modified DF.
}
}
Expand All @@ -538,37 +535,26 @@ void demodulate2000(uint16_t *m, uint32_t mlen) {
if ( (msglen)
&& ((2 * snr) > (int) (MODES_MSG_SQUELCH_DB * 10))
&& (errors <= MODES_MSG_ENCODER_ERRS) ) {
int result;

// Set initial mm structure details
mm.timestampMsg = Modes.timestampBlk + (j*6);
mm.signalLevel = (365.0*60 + sigLevel + noiseLevel) * (365.0*60 + sigLevel + noiseLevel) / MAX_POWER / 60 / 60;
mm.phase_corrected = use_correction;

// Decode the received message
message_ok = (decodeModesMessage(&mm, msg) >= 0);
result = decodeModesMessage(&mm, msg);
if (result < 0) {
message_ok = 0;
if (result == -1)
Modes.stats_current.demod_rejected_unknown_icao++;
else
Modes.stats_current.demod_rejected_bad++;
} else {
message_ok = 1;
Modes.stats_current.demod_accepted[mm.correctedbits]++;
}

// Update statistics
if (Modes.stats) {
struct demod_stats *dstats = (use_correction ? &Modes.stats_current.demod_phasecorrected : &Modes.stats_current.demod);

switch (errors) {
case 0: dstats->demodulated0++; break;
case 1: dstats->demodulated1++; break;
case 2: dstats->demodulated2++; break;
default: dstats->demodulated3++; break;
}

if (!message_ok) {
dstats->badcrc++;
} else if (mm.correctedbits == 0) {
dstats->goodcrc++;
dstats->goodcrc_byphase[0]++;
} else {
dstats->badcrc++;
dstats->fixed++;
if (mm.correctedbits <= MODES_MAX_BITERRORS)
dstats->bit_fix[mm.correctedbits-1] += 1;
}
}

// Output debug mode info if needed
if (use_correction) {
Expand Down
36 changes: 17 additions & 19 deletions demod_2400.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ void demodulate2400(uint16_t *m, uint32_t mlen) {
preamble[16] >= high ||
preamble[17] >= high ||
preamble[18] >= high) {
++Modes.stats_current.preamble_not_quiet;
continue;
}

Expand All @@ -270,16 +269,14 @@ void demodulate2400(uint16_t *m, uint32_t mlen) {
// Crosscorrelate against the first few bits to find a likely phase offset
initial_phase = best_phase(&preamble[19]);
if (initial_phase < 0) {
++Modes.stats_current.preamble_no_correlation;
continue; // nothing satisfactory
}

Modes.stats_current.preamble_phase[initial_phase%MODES_MAX_PHASE_STATS]++;
first_phase = last_phase = initial_phase; // try only the phase we think it is
}

Modes.stats_current.valid_preamble++;
bestmsg = NULL; bestscore = -1; bestphase = -1;
Modes.stats_current.demod_preambles++;
bestmsg = NULL; bestscore = -2; bestphase = -1;
for (try_phase = first_phase; try_phase <= last_phase; ++try_phase) {
uint16_t *pPtr;
int phase, i, score, bytelen;
Expand Down Expand Up @@ -407,8 +404,11 @@ void demodulate2400(uint16_t *m, uint32_t mlen) {
}

// Do we have a candidate?
if (!bestmsg) {
Modes.stats_current.demod.badcrc++;
if (bestscore < 0) {
if (bestscore == -1)
Modes.stats_current.demod_rejected_unknown_icao++;
else
Modes.stats_current.demod_rejected_bad++;
continue; // nope.
}

Expand Down Expand Up @@ -442,22 +442,20 @@ void demodulate2400(uint16_t *m, uint32_t mlen) {
}

// Decode the received message
if (decodeModesMessage(&mm, bestmsg) < 0)
continue;

// Update statistics
if (Modes.stats) {
if (mm.correctedbits == 0) {
Modes.stats_current.demod.goodcrc++;
Modes.stats_current.demod.goodcrc_byphase[bestphase%MODES_MAX_PHASE_STATS]++;
{
int result = decodeModesMessage(&mm, bestmsg);
if (result < 0) {
if (result == -1)
Modes.stats_current.demod_rejected_unknown_icao++;
else
Modes.stats_current.demod_rejected_bad++;
continue;
} else {
Modes.stats_current.demod.badcrc++;
Modes.stats_current.demod.fixed++;
if (mm.correctedbits)
Modes.stats_current.demod.bit_fix[mm.correctedbits-1]++;
Modes.stats_current.demod_accepted[mm.correctedbits]++;
}
}


// Skip over the message:
// (we actually skip to 8 bits before the end of the message,
// because we can often decode two messages that *almost* collide,
Expand Down
6 changes: 2 additions & 4 deletions dump1090.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,11 @@ struct modesMessage {
int msgtype; // Downlink format #
uint32_t crc; // Message CRC
int correctedbits; // No. of bits corrected
char corrected[MODES_MAX_BITERRORS]; // corrected bit positions
uint32_t addr; // ICAO Address from bytes 1 2 and 3
int phase_corrected; // True if phase correction was applied
uint32_t addr; // Address Announced
uint64_t timestampMsg; // Timestamp of the message
int remote; // If set this message is from a remote station
double signalLevel; // RSSI, in the range [0..1], as a fraction of full-scale power
int score;
int score; // Scoring from scoreModesMessage, if used

// DF 11, DF 17
int ca; // Responder capabilities
Expand Down
34 changes: 16 additions & 18 deletions mode_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ static int correct_aa_field(uint32_t *addr, struct errorinfo *ei)
// 1000: DF20/21 with a CRC-derived address matching a known aircraft
// 500: DF20/21 with a CRC-derived address matching a known aircraft (bottom 16 bits only - overlay control in use)

// -1: bad message
// -1: message might be valid, but we couldn't validate the CRC against a known ICAO
// -2: bad message or unrepairable CRC error

int scoreModesMessage(unsigned char *msg, int validbits)
{
Expand All @@ -321,13 +322,13 @@ int scoreModesMessage(unsigned char *msg, int validbits)
struct errorinfo *ei;

if (validbits < 56)
return -1;
return -2;

msgtype = msg[0] >> 3; // Downlink Format
msgbits = modesMessageLenByType(msgtype);

if (validbits < msgbits)
return -1;
return -2;

crc = modesChecksum(msg, msgbits);

Expand All @@ -346,7 +347,7 @@ int scoreModesMessage(unsigned char *msg, int validbits)

ei = modesChecksumDiagnose(crc, msgbits);
if (!ei)
return -1; // can't correct errors
return -2; // can't correct errors

// fix any errors in the address field
correct_aa_field(&addr, ei);
Expand All @@ -368,7 +369,7 @@ int scoreModesMessage(unsigned char *msg, int validbits)
case 18: // Extended squitter/non-transponder
ei = modesChecksumDiagnose(crc, msgbits);
if (!ei)
return -1; // can't correct errors
return -2; // can't correct errors

// fix any errors in the address field
addr = (msg[1] << 16) | (msg[2] << 8) | (msg[3]);
Expand All @@ -391,11 +392,11 @@ int scoreModesMessage(unsigned char *msg, int validbits)
return 500; // Data/Parity
#endif

return -1;
return -2;

default:
// unknown message type
return -1;
return -2;
}
}

Expand All @@ -410,7 +411,10 @@ static void decodeExtendedSquitter(struct modesMessage *mm);
static void decodeCommB(struct modesMessage *mm);
static char *ais_charset = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_ !\"#$%&'()*+,-./0123456789:;<=>?";

// return 0 if all OK, -1 if the message was rejected
// return 0 if all OK
// -1: message might be valid, but we couldn't validate the CRC against a known ICAO
// -2: bad message or unrepairable CRC error

int decodeModesMessage(struct modesMessage *mm, unsigned char *msg)
{
// Work on our local copy
Expand All @@ -435,8 +439,7 @@ int decodeModesMessage(struct modesMessage *mm, unsigned char *msg)
// We can't tell if the CRC is correct or not as we don't know the correct address.
// Accept the message if it appears to be from a previously-seen aircraft
if (!icaoFilterTest(mm->crc)) {
//fprintf(stderr, "reject: AP doesn't match known ICAO\n");
return -1;
return -1;
}
mm->addr = mm->crc;
break;
Expand All @@ -453,8 +456,7 @@ int decodeModesMessage(struct modesMessage *mm, unsigned char *msg)
int addr;
struct errorinfo *ei = modesChecksumDiagnose(mm->crc & 0xffff80, mm->msgbits);
if (!ei) {
//fprintf(stderr, "reject: DF11 uncorrectable CRC error\n");
return -1; // couldn't fix it
return -2; // couldn't fix it
}
mm->correctedbits = ei->errors;
modesChecksumFix(msg, ei);
Expand All @@ -464,7 +466,6 @@ int decodeModesMessage(struct modesMessage *mm, unsigned char *msg)
// match an existing aircraft.
addr = (msg[1] << 16) | (msg[2] << 8) | (msg[3]);
if (!icaoFilterTest(addr)) {
//fprintf(stderr, "reject: DF11 CRC error, repaired address doesn't match known ICAO\n");
return -1;
}
}
Expand All @@ -482,8 +483,7 @@ int decodeModesMessage(struct modesMessage *mm, unsigned char *msg)

ei = modesChecksumDiagnose(mm->crc, mm->msgbits);
if (!ei) {
//fprintf(stderr, "reject: DF17/18 uncorrectable CRC error\n");
return -1; // couldn't fix it
return -2; // couldn't fix it
}

addr1 = (msg[1] << 16) | (msg[2] << 8) | (msg[3]);
Expand All @@ -494,7 +494,6 @@ int decodeModesMessage(struct modesMessage *mm, unsigned char *msg)
// we are conservative here: only accept corrected messages that
// match an existing aircraft.
if (addr1 != addr2 && !icaoFilterTest(addr2)) {
//fprintf(stderr, "reject: DF17/18 CRC corrected address, repaired address doesn't match known ICAO\n");
return -1;
}

Expand Down Expand Up @@ -528,12 +527,11 @@ int decodeModesMessage(struct modesMessage *mm, unsigned char *msg)
}
#endif

//fprintf(stderr, "reject: DF20/21 address doesn't match known ICAO\n");
return -1; // no good

default:
// All other message types, we don't know how to handle their CRCs, give up
return -1;
return -2;
}

// decode the bulk of the message
Expand Down
28 changes: 22 additions & 6 deletions net_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,21 @@ int decodeBinMessage(struct client *c, char *p) {
}

if (msgLen == MODEAC_MSG_BYTES) { // ModeA or ModeC
Modes.stats_current.remote_received_modeac++;
decodeModeAMessage(&mm, ((msg[0] << 8) | msg[1]));
} else {
if (decodeModesMessage(&mm, msg) < 0) {
Modes.stats_current.remote_rejected++;
int result;

Modes.stats_current.remote_received_modes++;
result = decodeModesMessage(&mm, msg);
if (result < 0) {
if (result == -1)
Modes.stats_current.remote_rejected_unknown_icao++;
else
Modes.stats_current.remote_rejected_bad++;
return 0;
} else {
Modes.stats_current.remote_accepted++;
Modes.stats_current.remote_accepted[mm.correctedbits]++;
}
}

Expand Down Expand Up @@ -684,13 +692,21 @@ int decodeHexMessage(struct client *c, char *hex) {
}

if (l == (MODEAC_MSG_BYTES * 2)) { // ModeA or ModeC
Modes.stats_current.remote_received_modeac++;
decodeModeAMessage(&mm, ((msg[0] << 8) | msg[1]));
} else { // Assume ModeS
if (decodeModesMessage(&mm, msg) < 0) {
Modes.stats_current.remote_rejected++;
int result;

Modes.stats_current.remote_received_modes++;
result = decodeModesMessage(&mm, msg);
if (result < 0) {
if (result == -1)
Modes.stats_current.remote_rejected_unknown_icao++;
else
Modes.stats_current.remote_rejected_bad++;
return 0;
} else {
Modes.stats_current.remote_accepted++;
Modes.stats_current.remote_accepted[mm.correctedbits]++;
}
}

Expand Down
Loading

0 comments on commit a59077a

Please sign in to comment.