Remove duplicate varbles

Several variables within structures contain the same information in
different forms.

The aircraft structure contains addr and hexaddr. Hexaddr is a printable
string version of addr. However, hexaddr is only ever used in printf
statments, so we can use printf(%06x) to print addr. This saves a printf
for every received command.

The modesMessage structure contains addr, and aa1,aa2,aa3 as separate
bytes. aa1,aa2 and aa3 are only ever used to construct addr, and to
print out. Therefore, we can use addr instead of them..
This commit is contained in:
Malcolm Robb 2013-04-25 21:17:00 +01:00
parent 3766492067
commit f9c1dc1da5

View file

@ -56,7 +56,7 @@
// MinorVer changes when additional features are added, but not for bug fixes (range 00-99) // MinorVer changes when additional features are added, but not for bug fixes (range 00-99)
// DayDate & Year changes for all changes, including for bug fixes. It represent the release date of the update // DayDate & Year changes for all changes, including for bug fixes. It represent the release date of the update
// //
#define MODES_DUMP1090_VERSION "1.02.2404.13" #define MODES_DUMP1090_VERSION "1.02.2504.13"
#define MODES_DEFAULT_RATE 2000000 #define MODES_DEFAULT_RATE 2000000
#define MODES_DEFAULT_FREQ 1090000000 #define MODES_DEFAULT_FREQ 1090000000
@ -136,7 +136,6 @@ struct client {
/* Structure used to describe an aircraft in iteractive mode. */ /* Structure used to describe an aircraft in iteractive mode. */
struct aircraft { struct aircraft {
uint32_t addr; /* ICAO address */ uint32_t addr; /* ICAO address */
char hexaddr[7]; /* Printable ICAO address */
char flight[9]; /* Flight number */ char flight[9]; /* Flight number */
int altitude; /* Altitude */ int altitude; /* Altitude */
int speed; /* Velocity computed from EW and NS components. */ int speed; /* Velocity computed from EW and NS components. */
@ -252,7 +251,7 @@ struct modesMessage {
int crcok; /* True if CRC was valid */ int crcok; /* True if CRC was valid */
uint32_t crc; /* Message CRC */ uint32_t crc; /* Message CRC */
int errorbit; /* Bit corrected. -1 if no bit corrected. */ int errorbit; /* Bit corrected. -1 if no bit corrected. */
int aa1, aa2, aa3; /* ICAO Address bytes 1 2 and 3 */ uint32_t addr; /* ICAO Address from bytes 1 2 and 3 */
int phase_corrected; /* True if phase correction was applied. */ int phase_corrected; /* True if phase correction was applied. */
uint64_t timestampMsg; /* Timestamp of the message. */ uint64_t timestampMsg; /* Timestamp of the message. */
unsigned char signalLevel; /* Signal Amplitude */ unsigned char signalLevel; /* Signal Amplitude */
@ -1053,10 +1052,9 @@ void decodeModeAMessage(struct modesMessage *mm, int ModeA)
mm->msg[0] = (ModeA >> 8); mm->msg[0] = (ModeA >> 8);
mm->msg[1] = (ModeA); mm->msg[1] = (ModeA);
// Fudge an ICAO address based on Mode A // Fudge an ICAO address based on Mode A (remove the Ident bit)
mm->aa1 = 0xFF; // Use an upper address byte of FF, since this is ICAO unallocated // Use an upper address byte of FF, since this is ICAO unallocated
mm->aa2 = (ModeA >> 8); mm->addr = 0x00FF0000 | (ModeA & 0x0000FF7F);
mm->aa3 = (ModeA & 0x7F); // remove the Ident bit
// Set the Identity field to decimal ModeA // Set the Identity field to decimal ModeA
mm->identity = (ModeA & 7) mm->identity = (ModeA & 7)
@ -1278,7 +1276,7 @@ int ICAOAddressWasRecentlySeen(uint32_t addr) {
* populated by the caller. * populated by the caller.
* *
* On success the correct ICAO address is stored in the modesMessage * On success the correct ICAO address is stored in the modesMessage
* structure in the aa3, aa2, and aa1 fiedls. * structure in the addr field.
* *
* If the function successfully recovers a message with a correct checksum * If the function successfully recovers a message with a correct checksum
* it returns 1. Otherwise 0 is returned. */ * it returns 1. Otherwise 0 is returned. */
@ -1307,21 +1305,19 @@ int bruteForceAP(unsigned char *msg, struct modesMessage *mm) {
* *
* (ADDR xor CRC) xor CRC = ADDR. */ * (ADDR xor CRC) xor CRC = ADDR. */
crc = modesChecksum(aux,msgbits); crc = modesChecksum(aux,msgbits);
aux[lastbyte] ^= crc & 0xff; aux[lastbyte] ^= crc & 0xff;
aux[lastbyte-1] ^= (crc >> 8) & 0xff; aux[lastbyte-1] ^= (crc >> 8) & 0xff;
aux[lastbyte-2] ^= (crc >> 16) & 0xff; aux[lastbyte-2] ^= (crc >> 16) & 0xff;
/* If the obtained address exists in our cache we consider /* If the obtained address exists in our cache we consider
* the message valid. */ * the message valid. */
addr = aux[lastbyte] | (aux[lastbyte-1] << 8) | (aux[lastbyte-2] << 16); addr = aux[lastbyte] | (aux[lastbyte-1] << 8) | (aux[lastbyte-2] << 16);
if (ICAOAddressWasRecentlySeen(addr)) { if (ICAOAddressWasRecentlySeen(addr)) {
mm->aa1 = aux[lastbyte-2]; mm->addr = addr;
mm->aa2 = aux[lastbyte-1]; return (1);
mm->aa3 = aux[lastbyte];
return 1;
} }
} }
return 0; return (0);
} }
/* Decode the 13 bit AC altitude field (in DF 20 and others). /* Decode the 13 bit AC altitude field (in DF 20 and others).
@ -1476,10 +1472,8 @@ void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) {
* fields again. */ * fields again. */
mm->ca = msg[0] & 7; /* Responder capabilities. */ mm->ca = msg[0] & 7; /* Responder capabilities. */
/* ICAO address */ // ICAO address
mm->aa1 = msg[1]; mm->addr = (msg[1] << 16) | (msg[2] << 8) | (msg[3]);
mm->aa2 = msg[2];
mm->aa3 = msg[3];
/* DF 17 type (assuming this is a DF17, otherwise not used) */ /* DF 17 type (assuming this is a DF17, otherwise not used) */
mm->metype = msg[4] >> 3; /* Extended squitter message type. */ mm->metype = msg[4] >> 3; /* Extended squitter message type. */
@ -1544,8 +1538,7 @@ void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) {
* we can add this address to the list of recently seen * we can add this address to the list of recently seen
* addresses. */ * addresses. */
if (mm->crcok && mm->errorbit == -1) { if (mm->crcok && mm->errorbit == -1) {
uint32_t addr = (mm->aa1 << 16) | (mm->aa2 << 8) | mm->aa3; addRecentlySeenICAOAddr(mm->addr);
addRecentlySeenICAOAddr(addr);
} }
} }
@ -1632,7 +1625,7 @@ void displayModesMessage(struct modesMessage *mm) {
/* Handle only addresses mode first. */ /* Handle only addresses mode first. */
if (Modes.onlyaddr) { if (Modes.onlyaddr) {
printf("%02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3); printf("%06x\n", mm->addr);
return; return;
} }
@ -1665,7 +1658,7 @@ void displayModesMessage(struct modesMessage *mm) {
printf("DF 0: Short Air-Air Surveillance.\n"); printf("DF 0: Short Air-Air Surveillance.\n");
printf(" Altitude : %d %s\n", mm->altitude, printf(" Altitude : %d %s\n", mm->altitude,
(mm->unit == MODES_UNIT_METERS) ? "meters" : "feet"); (mm->unit == MODES_UNIT_METERS) ? "meters" : "feet");
printf(" ICAO Address : %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3); printf(" ICAO Address : %06x\n", mm->addr);
} else if (mm->msgtype == 4 || mm->msgtype == 20) { } else if (mm->msgtype == 4 || mm->msgtype == 20) {
printf("DF %d: %s, Altitude Reply.\n", mm->msgtype, printf("DF %d: %s, Altitude Reply.\n", mm->msgtype,
(mm->msgtype == 4) ? "Surveillance" : "Comm-B"); (mm->msgtype == 4) ? "Surveillance" : "Comm-B");
@ -1674,7 +1667,7 @@ void displayModesMessage(struct modesMessage *mm) {
printf(" UM : %d\n", mm->um); printf(" UM : %d\n", mm->um);
printf(" Altitude : %d %s\n", mm->altitude, printf(" Altitude : %d %s\n", mm->altitude,
(mm->unit == MODES_UNIT_METERS) ? "meters" : "feet"); (mm->unit == MODES_UNIT_METERS) ? "meters" : "feet");
printf(" ICAO Address : %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3); printf(" ICAO Address : %06x\n", mm->addr);
if (mm->msgtype == 20) { if (mm->msgtype == 20) {
/* TODO: 56 bits DF20 MB additional field. */ /* TODO: 56 bits DF20 MB additional field. */
@ -1686,7 +1679,7 @@ void displayModesMessage(struct modesMessage *mm) {
printf(" DR : %d\n", mm->dr); printf(" DR : %d\n", mm->dr);
printf(" UM : %d\n", mm->um); printf(" UM : %d\n", mm->um);
printf(" Squawk : %d\n", mm->identity); printf(" Squawk : %d\n", mm->identity);
printf(" ICAO Address : %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3); printf(" ICAO Address : %06x\n", mm->addr);
if (mm->msgtype == 21) { if (mm->msgtype == 21) {
/* TODO: 56 bits DF21 MB additional field. */ /* TODO: 56 bits DF21 MB additional field. */
@ -1695,7 +1688,7 @@ void displayModesMessage(struct modesMessage *mm) {
/* DF 11 */ /* DF 11 */
printf("DF 11: All Call Reply.\n"); printf("DF 11: All Call Reply.\n");
printf(" Capability : %s\n", ca_str[mm->ca]); printf(" Capability : %s\n", ca_str[mm->ca]);
printf(" ICAO Address: %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3); printf(" ICAO Address %06x\n", mm->addr);
if (mm->iid > 16) if (mm->iid > 16)
{printf(" IID : SI-%02d\n", mm->iid-16);} {printf(" IID : SI-%02d\n", mm->iid-16);}
else else
@ -1704,7 +1697,7 @@ void displayModesMessage(struct modesMessage *mm) {
/* DF 17 */ /* DF 17 */
printf("DF 17: ADS-B message.\n"); printf("DF 17: ADS-B message.\n");
printf(" Capability : %d (%s)\n", mm->ca, ca_str[mm->ca]); printf(" Capability : %d (%s)\n", mm->ca, ca_str[mm->ca]);
printf(" ICAO Address : %02x%02x%02x\n", mm->aa1, mm->aa2, mm->aa3); printf(" ICAO Address : %06x\n", mm->addr);
printf(" Extended Squitter Type: %d\n", mm->metype); printf(" Extended Squitter Type: %d\n", mm->metype);
printf(" Extended Squitter Sub : %d\n", mm->mesub); printf(" Extended Squitter Sub : %d\n", mm->mesub);
printf(" Extended Squitter Name: %s\n", printf(" Extended Squitter Name: %s\n",
@ -2158,57 +2151,58 @@ void useModesMessage(struct modesMessage *mm) {
} }
/* ========================= Interactive mode =============================== */ /* ========================= Interactive mode =============================== */
//
/* Return a new aircraft structure for the interactive mode linked list // Return a new aircraft structure for the interactive mode linked list
* of aircrafts. */ // of aircraft
struct aircraft *interactiveCreateAircraft(uint32_t addr) { //
struct aircraft *interactiveCreateAircraft(struct modesMessage *mm) {
struct aircraft *a = (struct aircraft *) malloc(sizeof(*a)); struct aircraft *a = (struct aircraft *) malloc(sizeof(*a));
a->addr = addr; a->addr = mm->addr;
snprintf(a->hexaddr,sizeof(a->hexaddr),"%06x",(int)addr); a->flight[0] = '\0';
a->flight[0] = '\0'; a->altitude = 0;
a->altitude = 0; a->speed = 0;
a->speed = 0; a->track = 0;
a->track = 0; a->odd_cprlat = 0;
a->odd_cprlat = 0; a->odd_cprlon = 0;
a->odd_cprlon = 0; a->odd_cprtime = 0;
a->odd_cprtime = 0; a->even_cprlat = 0;
a->even_cprlat = 0; a->even_cprlon = 0;
a->even_cprlon = 0;
a->even_cprtime = 0; a->even_cprtime = 0;
a->lat = 0; a->lat = 0;
a->lon = 0; a->lon = 0;
a->sbsflags = 0; a->sbsflags = 0;
a->seen = time(NULL); a->seen = time(NULL);
a->messages = 0; a->messages = 0;
a->squawk = 0; a->squawk = 0;
a->modeACflags = 0; a->modeACflags = 0;
a->modeAcount = 0; a->modeAcount = 0;
a->modeCcount = 0; a->modeCcount = 0;
a->modeC = 0; a->modeC = 0;
a->next = NULL; a->next = NULL;
return a; return (a);
} }
//
/* Return the aircraft with the specified address, or NULL if no aircraft // Return the aircraft with the specified address, or NULL if no aircraft
* exists with this address. */ // exists with this address.
//
struct aircraft *interactiveFindAircraft(uint32_t addr) { struct aircraft *interactiveFindAircraft(uint32_t addr) {
struct aircraft *a = Modes.aircrafts; struct aircraft *a = Modes.aircrafts;
while(a) { while(a) {
if (a->addr == addr) return a; if (a->addr == addr) return (a);
a = a->next; a = a->next;
} }
return NULL; return (NULL);
} }
// //
// We have received a Mode A or C response. // We have received a Mode A or C response.
// //
// Search through the list of known aircraft and tag them if this Mode A/C matches any // Search through the list of known Mode-S aircraft and tag them if this Mode A/C
// known Mode S Squawks or Altitudes(+/- 50feet). // matches their known Mode S Squawks or Altitudes(+/- 50feet).
// //
// A Mode S equipped aircraft may also respond to Mode A and Mode C SSR interrogations. // A Mode S equipped aircraft may also respond to Mode A and Mode C SSR interrogations.
// We can't tell if this is a mode A or C, so scan through the entire aircraft list // We can't tell if this is a Mode A or C, so scan through the entire aircraft list
// looking for matches on Mode A (squawk) and Mode C (altitude). Flag in the Mode S // looking for matches on Mode A (squawk) and Mode C (altitude). Flag in the Mode S
// records that we have had a potential Mode A or Mode C response from this aircraft. // records that we have had a potential Mode A or Mode C response from this aircraft.
// //
@ -2221,23 +2215,29 @@ struct aircraft *interactiveFindAircraft(uint32_t addr) {
// cruising at FL370, but it's less likely (though not impossible) that there are two // cruising at FL370, but it's less likely (though not impossible) that there are two
// aircraft on the same squawk. Therefore, give precidence to Mode A record matches // aircraft on the same squawk. Therefore, give precidence to Mode A record matches
// //
// Note : It's theoretically possible for an aircraft to have the same value for Mode A
// and Mode C. Therefore we have to check BOTH A AND C for EVERY S.
//
void interactiveUpdateAircraftModeA(struct aircraft *a) { void interactiveUpdateAircraftModeA(struct aircraft *a) {
struct aircraft *b = Modes.aircrafts; struct aircraft *b = Modes.aircrafts;
while(b) { while(b) {
if ((b->modeACflags & MODEAC_MSG_FLAG) == 0) {// skip any fudged ICAO records if ((b->modeACflags & MODEAC_MSG_FLAG) == 0) {// skip any fudged ICAO records
if (a->squawk == b->squawk) { // If a 'real' Mode S ICAO exists using this Squawk // First check for Mode-A <=> Mode-S Squawk matches
if (a->squawk == b->squawk) { // If a 'real' Mode-S ICAO exists using this Mode-A Squawk
b->modeAcount++; b->modeAcount++;
if ( (b->modeAcount > 0) && if ( (b->modeAcount > 0) &&
((b->modeCcount > 1) || (a->modeC < -12)) ) ((b->modeCcount > 1) || (a->modeC < -12)) )
{a->modeACflags |= MODEAC_MSG_MODES_HIT;} // flag that this ModeA/C probably belongs to a known Mode S {a->modeACflags |= MODEAC_MSG_MODES_HIT;} // flag this ModeA/C probably belongs to a known Mode S
}
} else if (a->modeC == b->modeC) // If a 'real' Mode S ICAO exists at this Altitude // Next check for Mode-C <=> Mode-S Altitude matches
if (a->modeC == b->modeC) // If a 'real' Mode S ICAO exists at this Mode-C Altitude
{ {
b->modeCcount++; b->modeCcount++;
if ((b->modeAcount > 0) && (b->modeCcount > 1)) if ((b->modeAcount > 0) && (b->modeCcount > 1))
{a->modeACflags |= MODEAC_MSG_MODES_HIT;} // flag that this ModeA/C probably belongs to a known Mode S {a->modeACflags |= MODEAC_MSG_MODES_HIT;} // flag this ModeA/C probably belongs to a known Mode S
} }
} }
b = b->next; b = b->next;
@ -2248,9 +2248,13 @@ void interactiveUpdateAircraftModeS() {
struct aircraft *a = Modes.aircrafts; struct aircraft *a = Modes.aircrafts;
while(a) { while(a) {
if (a->modeACflags & MODEAC_MSG_FLAG) { // find any fudged ICAO records int flags = a->modeACflags;
a->modeACflags &= ~MODEAC_MSG_MODES_HIT; // clear the hit bit if (flags & MODEAC_MSG_FLAG) { // find any fudged ICAO records
interactiveUpdateAircraftModeA(a); // and attempt to match them with Mode-S
// clear the hit bit
a->modeACflags = flags & ~MODEAC_MSG_MODES_HIT;
interactiveUpdateAircraftModeA(a); // and attempt to match them with Mode-S
} }
a = a->next; a = a->next;
} }
@ -2454,17 +2458,15 @@ int decodeCPRrelative(struct aircraft *a, int fflag, int surface, double latr, d
/* Receive new messages and populate the interactive mode with more info. */ /* Receive new messages and populate the interactive mode with more info. */
struct aircraft *interactiveReceiveData(struct modesMessage *mm) { struct aircraft *interactiveReceiveData(struct modesMessage *mm) {
uint32_t addr;
struct aircraft *a, *aux; struct aircraft *a, *aux;
if (Modes.check_crc && mm->crcok == 0) return NULL; if (Modes.check_crc && mm->crcok == 0) return NULL;
addr = (mm->aa1 << 16) | (mm->aa2 << 8) | mm->aa3;
// Loookup our aircraft or create a new one // Loookup our aircraft or create a new one
a = interactiveFindAircraft(addr); a = interactiveFindAircraft(mm->addr);
if (!a) { // If it's a currently unknown aircraft.... if (!a) { // If it's a currently unknown aircraft....
a = interactiveCreateAircraft(addr); // ., create a new record for it, a = interactiveCreateAircraft(mm); // ., create a new record for it,
a->next = Modes.aircrafts; // .. and put it at the head of the list a->next = Modes.aircrafts; // .. and put it at the head of the list
Modes.aircrafts = a; Modes.aircrafts = a;
} else { } else {
/* If it is an already known aircraft, move it on head /* If it is an already known aircraft, move it on head
@ -2557,7 +2559,7 @@ void interactiveShowData(void) {
printf("\x1b[H\x1b[2J"); /* Clear the screen */ printf("\x1b[H\x1b[2J"); /* Clear the screen */
if (Modes.interactive_rtl1090 ==0) { if (Modes.interactive_rtl1090 == 0) {
printf ( printf (
"Hex ModeA Flight Alt Speed Lat Lon Track Msgs Seen %c\n", progress); "Hex ModeA Flight Alt Speed Lat Lon Track Msgs Seen %c\n", progress);
} else { } else {
@ -2614,11 +2616,11 @@ void interactiveShowData(void) {
if (a->track > 0) { if (a->track > 0) {
sprintf (tt,"%03d",a->track); sprintf (tt,"%03d",a->track);
} }
printf("%-6s %-8s %-4s %-3s %-3s %4s %-6d %d %c \n", printf("%06x %-8s %-4s %-3s %-3s %4s %-6d %d %c \n",
a->hexaddr, a->flight, fl, gs, tt, squawk, msgs, (int)(now - a->seen), spacer); a->addr, a->flight, fl, gs, tt, squawk, msgs, (int)(now - a->seen), spacer);
} else { } else {
printf("%-6s %-4s %-8s %-7d %-7d %-7.03f %-7.03f %-3d %-6d %d%c sec\n", printf("%06x %-4s %-8s %-7d %-6d %-7.03f %-7.03f %-3d %-6d %d%c sec\n",
a->hexaddr, squawk, a->flight, altitude, speed, a->addr, squawk, a->flight, altitude, speed,
a->lat, a->lon, a->track, msgs, (int)(now - a->seen), spacer); a->lat, a->lon, a->track, msgs, (int)(now - a->seen), spacer);
} }
count++; count++;
@ -2883,7 +2885,7 @@ void modesSendSBSOutput(struct modesMessage *mm, struct aircraft *a) {
} }
// ICAO address of the aircraft // ICAO address of the aircraft
pCommon += sprintf(pCommon, "111,11111,%02X%02X%02X,111111,", mm->aa1, mm->aa2, mm->aa3); pCommon += sprintf(pCommon, "111,11111,%06X,111111,", mm->addr);
// Make sure the records' timestamp is valid before outputing it // Make sure the records' timestamp is valid before outputing it
if (mm->timestampMsg != (uint64_t)(-1)) { if (mm->timestampMsg != (uint64_t)(-1)) {
@ -3053,10 +3055,10 @@ char *aircraftsToJson(int *len) {
if (a->lat != 0 && a->lon != 0) { if (a->lat != 0 && a->lon != 0) {
l = snprintf(p,buflen, l = snprintf(p,buflen,
"{\"hex\":\"%s\", \"flight\":\"%s\", \"lat\":%f, " "{\"hex\":\"%06x\", \"flight\":\"%s\", \"lat\":%f, "
"\"lon\":%f, \"altitude\":%d, \"track\":%d, " "\"lon\":%f, \"altitude\":%d, \"track\":%d, "
"\"speed\":%d},\n", "\"speed\":%d},\n",
a->hexaddr, a->flight, a->lat, a->lon, a->altitude, a->track, a->addr, a->flight, a->lat, a->lon, a->altitude, a->track,
a->speed); a->speed);
p += l; buflen -= l; p += l; buflen -= l;
/* Resize if needed. */ /* Resize if needed. */