Fix the dc sensor index fixup

fixup_dc_sample_sensors() would make sure that any pressure sensor
indexes were in range of the cylinders by just clearing the pressure
data if the sensor index was larger than the number of cylinders in the
dive.

That certainly makes the sensor index data consistent, but at the cost
of just dropping the sensor data entirely.

Dirk had some cases of odd sensor data (probably because of an older
version of subsurface, but possibly due to removing cylinders manually
or because of oddities with the downloader for the Atomic Aquatics
Cobalt dive computer he used), and when re-saving the dive, the pressure
data would magically just get removed due to this.

So rewrite the sensor data fixup to strive very hard to avoid throwing
pressure sensor data away.  The simplest way to do that is to just add
the required number of cylinders, and then people can fix up their dives
manually by remapping the sensor data.

This whole "we clear the pressure data" was at least partly hidden by
two things:

 (1) in the git save format, we don't rewrite dives unless you've
     changed the dive some way, so old dives stay around with old data
     in the save until explicitly changed.

 (2) if you had multiple dive computers, and one dive computer does not
     have any pressure data but another one does, our profile will use
     that "other" dive computer pressure data (because often times you
     might have only one dive computer that is air integrated, but you
     still want to see the tank pressure when you look at other dive
     computers - or you have one dive computer give pressure data for
     your deco bottle, and another for your travel gas etc).

So those two facts hid the reality that we had actually cleared the tank
sensor data for Dirk's dive with the Atomic Aquatics dive computer,
because we'd still see pressure data in the profile, and the git data
would still be the old one.

Until Dirk renumbered his dives, and the data was rewritten.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Linus Torvalds 2022-09-12 16:38:22 -04:00
parent aa50be9cd5
commit 02f158b0c2

View file

@ -1210,17 +1210,41 @@ static void fixup_no_o2sensors(struct divecomputer *dc)
}
}
static void fixup_dc_sample_sensors(struct divecomputer *dc, int nr_cylinders)
static void fixup_dc_sample_sensors(struct dive *dive, struct divecomputer *dc)
{
unsigned long sensor_mask = 0;
for (int i = 0; i < dc->samples; i++) {
struct sample *s = dc->sample + i;
for (int j = 0; j < MAX_SENSORS; j++) {
if (s->sensor[j] < 0 || s->sensor[j] >= nr_cylinders) {
int sensor = s->sensor[j];
// No invalid sensor ID's, please
if (sensor < 0 || sensor > MAX_SENSORS) {
s->sensor[j] = NO_SENSOR;
s->pressure[j].mbar = 0;
continue;
}
// Don't bother tracking sensors with no data
if (!s->pressure[j].mbar) {
s->sensor[j] = NO_SENSOR;
continue;
}
// Remember the set of sensors we had
sensor_mask |= 1ul << sensor;
}
}
// Ignore the sensors we have cylinders for
sensor_mask >>= dive->cylinders.nr;
// Do we need to add empty cylinders?
while (sensor_mask) {
add_empty_cylinder(&dive->cylinders);
sensor_mask >>= 1;
}
}
static void fixup_dive_dc(struct dive *dive, struct divecomputer *dc)
@ -1241,7 +1265,7 @@ static void fixup_dive_dc(struct dive *dive, struct divecomputer *dc)
fixup_dc_gasswitch(dive, dc);
/* Fix up cylinder ids in pressure sensors */
fixup_dc_sample_sensors(dc, dive->cylinders.nr);
fixup_dc_sample_sensors(dive, dc);
/* Fix up cylinder pressures based on DC info */
fixup_dive_pressures(dive, dc);