Skip to content

Commit

Permalink
Don't use bitfields for >1 bit values.
Browse files Browse the repository at this point in the history
The semantics for unsigned bitfields are somewhat confusing -- they are
promoted to signed ints in many contexts. And we don't really care about
the small difference in storage size.

Keep single-bit bitfields since we're generally using those as booleans, not
integers.

Corresponding format-string changes.
  • Loading branch information
mutability committed Sep 20, 2023
1 parent add095a commit ad2171c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
15 changes: 7 additions & 8 deletions dump1090.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,21 +578,20 @@ struct modesMessage {
unsigned nic_c : 1; // if nic_c_valid
unsigned nic_baro : 1; // if nic_baro_valid

unsigned nac_p : 4; // if nac_p_valid
unsigned nac_v : 3; // if nac_v_valid
unsigned nac_p; // if nac_p_valid
unsigned nac_v; // if nac_v_valid

unsigned sil : 2; // if sil_type != SIL_INVALID
unsigned sil; // if sil_type != SIL_INVALID
sil_type_t sil_type;

unsigned gva : 2; // if gva_valid

unsigned sda : 2; // if sda_valid
unsigned gva; // if gva_valid
unsigned sda; // if sda_valid
} accuracy;

// Operational Status
struct {
unsigned valid : 1;
unsigned version : 3;
unsigned version;

unsigned om_acas_ra : 1;
unsigned om_ident : 1;
Expand All @@ -604,7 +603,7 @@ struct modesMessage {
unsigned cc_1090_in : 1;
unsigned cc_arv : 1;
unsigned cc_ts : 1;
unsigned cc_tc : 2;
unsigned cc_tc;
unsigned cc_uat_in : 1;
unsigned cc_poa : 1;
unsigned cc_b2_low : 1;
Expand Down
14 changes: 7 additions & 7 deletions mode_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -2146,13 +2146,13 @@ void displayModesMessage(struct modesMessage *mm) {
printf(" NIC-baro: %d\n", mm->accuracy.nic_baro);
}
if (mm->accuracy.nac_p_valid) {
printf(" NACp: %d\n", mm->accuracy.nac_p);
printf(" NACp: %u\n", mm->accuracy.nac_p);
}
if (mm->accuracy.nac_v_valid) {
printf(" NACv: %d\n", mm->accuracy.nac_v);
printf(" NACv: %u\n", mm->accuracy.nac_v);
}
if (mm->accuracy.gva_valid) {
printf(" GVA: %d\n", mm->accuracy.gva);
printf(" GVA: %u\n", mm->accuracy.gva);
}
if (mm->accuracy.sil_type != SIL_INVALID) {
const char *sil_description;
Expand All @@ -2170,26 +2170,26 @@ void displayModesMessage(struct modesMessage *mm) {
sil_description = "p > 0.1%";
break;
}
printf(" SIL: %d (%s, %s)\n",
printf(" SIL: %u (%s, %s)\n",
mm->accuracy.sil,
sil_description,
sil_type_to_string(mm->accuracy.sil_type));
}
if (mm->accuracy.sda_valid) {
printf(" SDA: %d\n", mm->accuracy.sda);
printf(" SDA: %u\n", mm->accuracy.sda);
}

if (mm->opstatus.valid) {
printf(" Aircraft Operational Status:\n");
printf(" Version: %d\n", mm->opstatus.version);
printf(" Version: %u\n", mm->opstatus.version);

printf(" Capability classes: ");
if (mm->opstatus.cc_acas) printf("ACAS ");
if (mm->opstatus.cc_cdti) printf("CDTI ");
if (mm->opstatus.cc_1090_in) printf("1090IN ");
if (mm->opstatus.cc_arv) printf("ARV ");
if (mm->opstatus.cc_ts) printf("TS ");
if (mm->opstatus.cc_tc) printf("TC=%d ", mm->opstatus.cc_tc);
if (mm->opstatus.cc_tc) printf("TC=%u ", mm->opstatus.cc_tc);
if (mm->opstatus.cc_uat_in) printf("UATIN ");
if (mm->opstatus.cc_poa) printf("POA ");
if (mm->opstatus.cc_b2_low) printf("B2-LOW ");
Expand Down
6 changes: 3 additions & 3 deletions net_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ static void modesSendStratuxOutput(struct modesMessage *mm, struct aircraft *a)

// navigation accuracy category - position
if (mm->accuracy.nac_p_valid) {
p = safe_snprintf(p, end, "\"NACp\":%d,", mm->accuracy.nac_p);
p = safe_snprintf(p, end, "\"NACp\":%u,", mm->accuracy.nac_p);
} else {
p = safe_snprintf(p, end, "\"NACp\":null,");
}
Expand Down Expand Up @@ -1819,7 +1819,7 @@ char *generateAircraftJson(const char *url_path, int *len) {
if (a->adsb_version >= 0)
p = safe_snprintf(p, end, ",\"version\":%d", a->adsb_version);
if (trackDataValid(&a->nic_baro_valid))
p = safe_snprintf(p, end, ",\"nic_baro\":%u", a->nic_baro);
p = safe_snprintf(p, end, ",\"nic_baro\":%u", (unsigned) a->nic_baro);
if (trackDataValid(&a->nac_p_valid))
p = safe_snprintf(p, end, ",\"nac_p\":%u", a->nac_p);
if (trackDataValid(&a->nac_v_valid))
Expand Down Expand Up @@ -2788,7 +2788,7 @@ static void writeFATSV()
p = appendFATSVMeta(p, end, "sil_type", a, &a->sil_valid, "%s", sil_type_enum_string(a->sil_type));
}
if (trackDataValid(&a->nic_baro_valid) && (forceEmit || a->nic_baro != a->fatsv_emitted_nic_baro)) {
p = appendFATSVMeta(p, end, "nic_baro", a, &a->nic_baro_valid, "%u", a->nic_baro);
p = appendFATSVMeta(p, end, "nic_baro", a, &a->nic_baro_valid, "%u", (unsigned) a->nic_baro);
}

// only emit alt, speed, latlon, track etc if they have been received since the last time
Expand Down
18 changes: 9 additions & 9 deletions track.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ struct aircraft {
data_validity gva_valid;
data_validity sda_valid;

unsigned nic_a : 1; // NIC supplement A from opstatus
unsigned nic_c : 1; // NIC supplement C from opstatus
unsigned nic_baro : 1; // NIC baro supplement from TSS or opstatus
unsigned nac_p : 4; // NACp from TSS or opstatus
unsigned nac_v : 3; // NACv from airborne velocity or opstatus
unsigned sil : 2; // SIL from TSS or opstatus
sil_type_t sil_type; // SIL supplement from TSS or opstatus
unsigned gva : 2; // GVA from opstatus
unsigned sda : 2; // SDA from opstatus
unsigned nic_a : 1; // NIC supplement A from opstatus
unsigned nic_c : 1; // NIC supplement C from opstatus
unsigned nic_baro : 1; // NIC baro supplement from TSS or opstatus
unsigned nac_p; // NACp from TSS or opstatus
unsigned nac_v; // NACv from airborne velocity or opstatus
unsigned sil; // SIL from TSS or opstatus
sil_type_t sil_type; // SIL supplement from TSS or opstatus
unsigned gva; // GVA from opstatus
unsigned sda; // SDA from opstatus

// data extracted from MRAR
data_validity mrar_source_valid;
Expand Down

0 comments on commit ad2171c

Please sign in to comment.