From 2495ee7e58c5ac716089912bf393cdd7f0b1887b Mon Sep 17 00:00:00 2001 From: Ben Doherty Date: Sun, 8 Nov 2026 18:30:45 +1100 Subject: [PATCH 1/2] shearwater: link air integration tanks to their gas mixes The logs record the gas mix in use and up to four transmitter pressures, but not which gas mix each transmitter is on, so every tank was reported with DC_GASMIX_UNKNOWN and applications had to guess, usually by position. Work out the link from the dive itself: each tank gets the gas mix it was clearly used most on, measured as the pressure drop while that mix was in use. Tanks flagged as a sidemount pair by the GTR mode are counted together and get the same mix. A tank only gets a mix if it used at least 10 bar, and at least twice as much as on any other mix; otherwise it stays DC_GASMIX_UNKNOWN. This is only done for open circuit dives. Report the highest reading as the begin pressure instead of the first one. A stage whose valve stays closed until the gas switch first reads the gas left in its regulator as it bleeds down, sometimes only a few bar. The same readings are ignored when measuring use. The pressure samples themselves are reported unchanged. Add a unit test for the matching and a Perdix 2 fixture with four tanks (sidemount pair, 50% and oxygen stages). Co-Authored-By: Claude Opus 5.5 Signed-off-by: Ben Doherty --- Makefile.am | 1 + src/Makefile.am | 2 +- src/shearwater_predator_parser.c | 59 ++++++++++- src/shearwater_tank_gas.h | 115 ++++++++++++++++++++++ test/Makefile.am | 7 +- test/fixtures/manifest.txt | 1 + test/fixtures/shearwater_perdix2-0001.bin | Bin 0 -> 32384 bytes test/test_shearwater_tank_gas.c | 114 +++++++++++++++++++++ 8 files changed, 295 insertions(+), 4 deletions(-) create mode 100644 src/shearwater_tank_gas.h create mode 100644 test/fixtures/shearwater_perdix2-0001.bin create mode 100644 test/test_shearwater_tank_gas.c diff --git a/Makefile.am b/Makefile.am index 88688d98..7fb7c61a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -26,6 +26,7 @@ EXTRA_DIST = \ test/run-parse.sh \ test/fixtures/manifest.txt \ test/fixtures/shearwater_petrel2-0001.bin \ + test/fixtures/shearwater_perdix2-0001.bin \ test/fixtures/hw_ostc5-0001.bin \ test/fixtures/garmin_descent_mk1-0001.bin \ test/test_descriptor_product_name.c diff --git a/src/Makefile.am b/src/Makefile.am index 67230fff..99a9b39c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -59,7 +59,7 @@ libdivecomputer_la_SOURCES = \ zeagle_n2ition3.h zeagle_n2ition3.c \ atomics_cobalt.h atomics_cobalt.c atomics_cobalt_parser.c \ shearwater_common.h shearwater_common.c \ - shearwater_predator.h shearwater_predator.c shearwater_predator_parser.c \ + shearwater_predator.h shearwater_predator.c shearwater_predator_parser.c shearwater_tank_gas.h \ shearwater_petrel.h shearwater_petrel.c \ diverite_nitekq.h diverite_nitekq.c diverite_nitekq_parser.c \ citizen_aqualand.h citizen_aqualand.c citizen_aqualand_parser.c \ diff --git a/src/shearwater_predator_parser.c b/src/shearwater_predator_parser.c index ff1174d8..6679cbd4 100644 --- a/src/shearwater_predator_parser.c +++ b/src/shearwater_predator_parser.c @@ -32,6 +32,7 @@ #include "platform.h" #include "array.h" #include "field-cache.h" +#include "shearwater_tank_gas.h" #define ISINSTANCE(parser) ( \ dc_parser_isinstance((parser), &shearwater_predator_parser_vtable) || \ @@ -147,6 +148,7 @@ typedef struct shearwater_predator_tank_t { char name[2]; unsigned int battery; dc_tank_usage_t usage; + unsigned int gasmix; } shearwater_predator_tank_t; struct shearwater_predator_parser_t { @@ -320,6 +322,7 @@ shearwater_common_parser_create (dc_parser_t **out, dc_context_t *context, const memset (parser->tank[i].name, 0, sizeof (parser->tank[i].name)); parser->tank[i].battery = 0; parser->tank[i].usage = DC_TANK_USAGE_NONE; + parser->tank[i].gasmix = UNDEFINED; parser->tankidx[i] = i; } parser->aimode = AI_OFF; @@ -570,6 +573,21 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) unsigned int sub_mode = SM_OC_REC; unsigned int stack_time_total_s = 0; unsigned int stack_time_remaining_s; + + // AI-generated (Claude) + // The gas mix in use and the tank pressures of every sample, used to work + // out which gas mix each tank is on once all samples have been seen. + unsigned int maxsamples = length > offset ? (length - offset) / parser->samplesize : 0; + unsigned int *trace_gas = (unsigned int *) malloc ((maxsamples + 1) * sizeof (unsigned int)); + unsigned int *trace_pressure = (unsigned int *) calloc ((maxsamples + 1) * NTANKS, sizeof (unsigned int)); + if (trace_gas == NULL || trace_pressure == NULL) { + ERROR (abstract->context, "Failed to allocate memory."); + free (trace_gas); + free (trace_pressure); + return DC_STATUS_NOMEMORY; + } + unsigned int gas_current = SHEARWATER_TANK_GAS_NONE; + while (offset + parser->samplesize <= length) { // Ignore empty samples. if (array_isequal (data + offset, parser->samplesize, 0x00)) { @@ -613,6 +631,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) if (idx >= ngasmixes) { if (idx >= NGASMIXES) { ERROR (abstract->context, "Maximum number of gas mixes reached."); + free (trace_gas); + free (trace_pressure); return DC_STATUS_NOMEMORY; } gasmix[idx].oxygen = o2; @@ -622,11 +642,13 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) } gasmix[idx].active = 1; + gas_current = idx; o2_previous = o2; he_previous = he; dil_previous = ccr; } + trace_gas[nsamples - 1] = gas_current; // Tank pressure if (logversion >= 7) { @@ -654,6 +676,7 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) } tank[id].endpressure = pressure; tank[id].battery |= battery; + trace_pressure[(nsamples - 1) * NTANKS + id] = pressure; } } } @@ -673,6 +696,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) tank[id].endpressure = pressure; } tank[id].endpressure = pressure; + if (nsamples) + trace_pressure[(nsamples - 1) * NTANKS + id] = pressure; } } } @@ -838,6 +863,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) for (unsigned int i = 0; i <= 4; ++i) { if (parser->opening[i] == UNDEFINED || parser->closing[i] == UNDEFINED) { ERROR (abstract->context, "Opening or closing record %u not found.", i); + free (trace_gas); + free (trace_pressure); return DC_STATUS_DATAFORMAT; } } @@ -916,6 +943,9 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) parser->headersize = headersize; parser->footersize = footersize; parser->ngasmixes = 0; + unsigned int gasmap[NGASMIXES]; + for (unsigned int i = 0; i < NGASMIXES; ++i) + gasmap[i] = UNDEFINED; if (divemode != M_FREEDIVE) { for (unsigned int i = 0; i < ngasmixes; ++i) { if (gasmix[i].oxygen == 0 && gasmix[i].helium == 0) @@ -924,10 +954,37 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) continue; if (gasmix[i].diluent && !shearwater_predator_is_ccr (divemode)) continue; + gasmap[i] = parser->ngasmixes; parser->gasmix[parser->ngasmixes] = gasmix[i]; parser->ngasmixes++; } } + + // AI-generated (Claude) + // The begin pressure is the highest reading, not the first one: a stage + // with its valve closed until the gas switch first reads the pressure left + // in its regulator. For open circuit dives, link each tank to the gas mix + // it was used on, if that is clear from the pressure drops. + unsigned int assigned[NTANKS]; + unsigned int sidemount = 0; + for (unsigned int i = 0; i < NTANKS; ++i) { + assigned[i] = SHEARWATER_TANK_GAS_NONE; + if (tank[i].usage == DC_TANK_USAGE_SIDEMOUNT) + sidemount |= 1u << i; + for (unsigned int n = 0; n < nsamples; ++n) { + if (trace_pressure[n * NTANKS + i] > tank[i].beginpressure) + tank[i].beginpressure = trace_pressure[n * NTANKS + i]; + } + } + if (divemode != M_FREEDIVE && !shearwater_predator_is_ccr (divemode)) { + shearwater_tank_gas_assign (trace_gas, trace_pressure, nsamples, NTANKS, ngasmixes, + sidemount, (int) (10.0 * BAR / PSI / 2), assigned); + } + for (unsigned int i = 0; i < NTANKS; ++i) + tank[i].gasmix = assigned[i] < NGASMIXES ? gasmap[assigned[i]] : UNDEFINED; + free (trace_gas); + free (trace_pressure); + parser->ntanks = 0; for (unsigned int i = 0; i < NTANKS; ++i) { if (tank[i].active) { @@ -1090,7 +1147,7 @@ shearwater_predator_parser_get_field (dc_parser_t *abstract, dc_field_type_t typ tank->workpressure = 0.0; tank->beginpressure = parser->tank[flags].beginpressure * 2 * PSI / BAR; tank->endpressure = parser->tank[flags].endpressure * 2 * PSI / BAR; - tank->gasmix = DC_GASMIX_UNKNOWN; + tank->gasmix = parser->tank[flags].gasmix == UNDEFINED ? DC_GASMIX_UNKNOWN : parser->tank[flags].gasmix; tank->usage = parser->tank[flags].usage; break; case DC_FIELD_SALINITY: diff --git a/src/shearwater_tank_gas.h b/src/shearwater_tank_gas.h new file mode 100644 index 00000000..589b28ac --- /dev/null +++ b/src/shearwater_tank_gas.h @@ -0,0 +1,115 @@ +/* + * libdivecomputer + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +// AI-generated (Claude) + +/* Internal matching of Shearwater air integration transmitters to gas mixes. */ +#ifndef SHEARWATER_TANK_GAS_H +#define SHEARWATER_TANK_GAS_H + +#define SHEARWATER_TANK_GAS_NONE 0xFFFFFFFF +#define SHEARWATER_TANK_GAS_MAXTANKS 8 +#define SHEARWATER_TANK_GAS_MAXGASES 20 + +/* + * The logs record the gas mix in use and the transmitter pressures, but not + * which gas mix each transmitter is on. Assign each transmitter the gas mix + * it was clearly used most on: the pressure drop while that mix was in use. + * + * gas[n] is the gas mix in use at sample n (or SHEARWATER_TANK_GAS_NONE), and + * pressure[n * ntanks + i] is the reading of transmitter i at sample n (0 if + * there is none). Readings before a transmitter's highest reading are ignored, + * so the regulator of a closed stage bleeding down doesn't count as use. The + * transmitters in the sidemount mask are counted together and get the same + * gas mix. A transmitter gets a gas mix only if it used at least minuse, and + * at least twice as much as on any other mix. Otherwise result[i] is + * SHEARWATER_TANK_GAS_NONE. + */ +static void +shearwater_tank_gas_assign (const unsigned int gas[], const unsigned int pressure[], + unsigned int nsamples, unsigned int ntanks, unsigned int ngases, + unsigned int sidemount, int minuse, unsigned int result[]) +{ + int use[SHEARWATER_TANK_GAS_MAXTANKS][SHEARWATER_TANK_GAS_MAXGASES] = {{0}}; + + if (ntanks > SHEARWATER_TANK_GAS_MAXTANKS) + ntanks = SHEARWATER_TANK_GAS_MAXTANKS; + if (ngases > SHEARWATER_TANK_GAS_MAXGASES) + ngases = SHEARWATER_TANK_GAS_MAXGASES; + + for (unsigned int i = 0; i < ntanks; ++i) { + unsigned int highest = 0, start = 0; + for (unsigned int n = 0; n < nsamples; ++n) { + if (pressure[n * ntanks + i] > highest) { + highest = pressure[n * ntanks + i]; + start = n; + } + } + + unsigned int previous = highest, previous_gas = gas[start]; + for (unsigned int n = start + 1; n < nsamples && highest; ++n) { + unsigned int p = pressure[n * ntanks + i]; + if (p == 0) + continue; + if (gas[n] < ngases && gas[n] == previous_gas) + use[i][gas[n]] += (int) previous - (int) p; + previous = p; + previous_gas = gas[n]; + } + } + + // Sidemount tanks are breathed alternately on one gas mix. + unsigned int count = 0; + int pooled[SHEARWATER_TANK_GAS_MAXGASES] = {0}; + for (unsigned int i = 0; i < ntanks; ++i) { + if (sidemount & (1u << i)) { + count++; + for (unsigned int g = 0; g < ngases; ++g) + pooled[g] += use[i][g]; + } + } + if (count >= 2) { + for (unsigned int i = 0; i < ntanks; ++i) { + if (sidemount & (1u << i)) { + for (unsigned int g = 0; g < ngases; ++g) + use[i][g] = pooled[g]; + } + } + } + + for (unsigned int i = 0; i < ntanks; ++i) { + unsigned int best = 0; + int second = 0; + for (unsigned int g = 1; g < ngases; ++g) { + if (use[i][g] > use[i][best]) { + if (use[i][best] > second) + second = use[i][best]; + best = g; + } else if (use[i][g] > second) { + second = use[i][g]; + } + } + if (ngases && use[i][best] >= minuse && use[i][best] >= 2 * second) + result[i] = best; + else + result[i] = SHEARWATER_TANK_GAS_NONE; + } +} + +#endif diff --git a/test/Makefile.am b/test/Makefile.am index 6d07e758..3c80cecd 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -3,17 +3,20 @@ AUTOMAKE_OPTIONS = subdir-objects AM_CPPFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include LDADD = $(top_builddir)/src/libdivecomputer.la -check_PROGRAMS = test_descriptor_product_name test_mares_iconhd_freedive +check_PROGRAMS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas test_descriptor_product_name_SOURCES = test_descriptor_product_name.c test_mares_iconhd_freedive_SOURCES = test_mares_iconhd_freedive.c $(top_srcdir)/src/array.c test_mares_iconhd_freedive_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src +test_shearwater_tank_gas_SOURCES = test_shearwater_tank_gas.c +test_shearwater_tank_gas_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src -TESTS = test_descriptor_product_name test_mares_iconhd_freedive +TESTS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas EXTRA_DIST = \ run-parse.sh \ fixtures/manifest.txt \ fixtures/shearwater_petrel2-0001.bin \ + fixtures/shearwater_perdix2-0001.bin \ fixtures/hw_ostc5-0001.bin \ fixtures/garmin_descent_mk1-0001.bin diff --git a/test/fixtures/manifest.txt b/test/fixtures/manifest.txt index 0ba8efd9..6696aedb 100644 --- a/test/fixtures/manifest.txt +++ b/test/fixtures/manifest.txt @@ -4,5 +4,6 @@ # All dives are the owner's own recordings, cleared for redistribution under # the library licence. No GPS or personal location data is present. shearwater_petrel2-0001.bin|Shearwater|Petrel 2 +shearwater_perdix2-0001.bin|Shearwater|Perdix 2 hw_ostc5-0001.bin|Heinrichs Weikamp|OSTC 5 garmin_descent_mk1-0001.bin|Garmin|Descentâ„¢ Mk1 diff --git a/test/fixtures/shearwater_perdix2-0001.bin b/test/fixtures/shearwater_perdix2-0001.bin new file mode 100644 index 0000000000000000000000000000000000000000..9d17940129ec11e286d64676f3a5c459cdbb68d4 GIT binary patch literal 32384 zcmb`Q2VfM{*2nLin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w=KQMoYJlwQ^_+1x%56AwTKS%}qL{faKI8rd;{G~^O ze}BBLpG|?36#Dz)I5!xt9izNPnSX!0UWDHt&xH$c1zAek3xq5pk1D@EJ|9krfB5_3 zdEtJ4++p+o{VS_~g#G>T{0P54UJ&8;#|z!&-yescP!BBr*!=yKnrqtcx&OLe ONQj#K_fPaU$^Qda8_~G{ literal 0 HcmV?d00001 diff --git a/test/test_shearwater_tank_gas.c b/test/test_shearwater_tank_gas.c new file mode 100644 index 00000000..86cde01c --- /dev/null +++ b/test/test_shearwater_tank_gas.c @@ -0,0 +1,114 @@ +// AI-generated (Claude) +#include + +#include "shearwater_tank_gas.h" + +#define NONE SHEARWATER_TANK_GAS_NONE +#define NTANKS 4 +#define NSAMPLES 60 +#define MINUSE 70 + +typedef struct { + unsigned int gas[NSAMPLES]; + unsigned int pressure[NSAMPLES * NTANKS]; +} trace_t; + +// Breathe gas mix 'gas' from sample 'from' up to (not including) sample 'to'. +static void +breathe (trace_t *t, unsigned int from, unsigned int to, unsigned int gas) +{ + for (unsigned int n = from; n < to; ++n) + t->gas[n] = gas; +} + +// Tank 'tank' reads 'start' at sample 'from', falling by 'step' per sample. +static void +reading (trace_t *t, unsigned int tank, unsigned int from, unsigned int to, unsigned int start, int step) +{ + for (unsigned int n = from; n < to; ++n) + t->pressure[n * NTANKS + tank] = start - step * (int) (n - from); +} + +static int +check (const char *name, const trace_t *t, unsigned int sidemount, const unsigned int expected[NTANKS]) +{ + unsigned int result[NTANKS]; + shearwater_tank_gas_assign (t->gas, t->pressure, NSAMPLES, NTANKS, 3, sidemount, MINUSE, result); + for (unsigned int i = 0; i < NTANKS; ++i) { + if (result[i] != expected[i]) { + fprintf (stderr, "FAIL: %s: tank %u got %d, expected %d\n", name, i, (int) result[i], (int) expected[i]); + return 1; + } + } + return 0; +} + +int +main (void) +{ + int failures = 0; + + // Sidemount pair on bottom gas (2), stages on 50% (1) and oxygen (0) + // with their valves closed until the switch: before that, the stage + // transmitters read the pressure in their regulators bleeding down. + trace_t a = {0}; + breathe (&a, 0, 30, 2); + breathe (&a, 30, 45, 1); + breathe (&a, 45, 60, 0); + reading (&a, 0, 0, 30, 1500, 15); + reading (&a, 0, 30, 60, 1065, 0); + reading (&a, 1, 0, 30, 1550, 15); + reading (&a, 1, 30, 60, 1115, 0); + reading (&a, 2, 0, 30, 1000, 20); + reading (&a, 2, 30, 45, 1400, 15); + reading (&a, 2, 45, 60, 1190, 0); + reading (&a, 3, 0, 45, 900, 15); + reading (&a, 3, 45, 60, 1100, 15); + const unsigned int expected_a[NTANKS] = {2, 2, 1, 0}; + failures += check ("sidemount with closed stages", &a, 0x3, expected_a); + + // Without the sidemount flag, each tank still gets the gas it was used on. + failures += check ("doubles without sidemount flag", &a, 0, expected_a); + + // Cave staging: sidemount pair and two staged tanks, all on air (0). + trace_t b = {0}; + breathe (&b, 0, 60, 0); + reading (&b, 0, 0, 20, 1500, 10); + reading (&b, 0, 20, 60, 1310, 0); + reading (&b, 1, 0, 20, 1500, 0); + reading (&b, 1, 20, 60, 1500, 10); + reading (&b, 2, 0, 40, 1400, 0); + reading (&b, 2, 40, 60, 1400, 10); + reading (&b, 3, 0, 60, 1400, 3); + const unsigned int expected_b[NTANKS] = {0, 0, 0, 0}; + failures += check ("all tanks on one gas", &b, 0x3, expected_b); + + // A stage that was carried but never breathed, and a tank without data. + trace_t c = {0}; + breathe (&c, 0, 40, 2); + breathe (&c, 40, 60, 1); + reading (&c, 0, 0, 60, 1500, 10); + reading (&c, 1, 0, 60, 1400, 0); + const unsigned int expected_c[NTANKS] = {2, NONE, NONE, NONE}; + failures += check ("unused stage", &c, 0, expected_c); + + // Used about as much on two gas mixes (a switch logged late): no guess. + trace_t d = {0}; + breathe (&d, 0, 30, 1); + breathe (&d, 30, 60, 0); + reading (&d, 0, 0, 60, 1200, 5); + const unsigned int expected_d[NTANKS] = {NONE, NONE, NONE, NONE}; + failures += check ("ambiguous", &d, 0, expected_d); + + // One side of a sidemount pair barely breathed: it goes with its pair. + trace_t e = {0}; + breathe (&e, 0, 60, 2); + reading (&e, 0, 0, 60, 1500, 15); + reading (&e, 1, 0, 60, 1500, 0); + reading (&e, 1, 50, 60, 1500, 1); + const unsigned int expected_e[NTANKS] = {2, 2, NONE, NONE}; + failures += check ("one-sided sidemount", &e, 0x3, expected_e); + failures += check ("one-sided without sidemount flag", &e, 0, (const unsigned int[NTANKS]) {2, NONE, NONE, NONE}); + + return failures ? 1 : 0; +} From 480658dc3a9f0e0e8d9b01cffaeb30de867d2f67 Mon Sep 17 00:00:00 2001 From: Ben Doherty Date: Sun, 8 Nov 2026 22:25:20 +1100 Subject: [PATCH 2/2] shearwater: address review comments on the tank gas mix links - Only use the highest reading as the begin pressure on open circuit dives, as the gas mix links already were, and limit both to the open circuit modes (OC technical, OC recreational and Avelo). - Don't count a pressure drop between two readings if the gas mix changed in between, also when it changed back during missing readings. - Skip tanks without readings before looking at their samples, and keep the original stride when the number of tanks is capped. - Round the 10 bar minimum up to 73 units of 2 psi. - Allocate the gas mix trace with calloc. - Test the inclusive limits and a gas change during missing readings, and add a test that parses the Perdix 2 fixture and checks the tanks' gas mixes and begin pressures. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Ben Doherty --- src/shearwater_predator_parser.c | 18 +++--- src/shearwater_tank_gas.h | 20 +++++-- test/Makefile.am | 5 +- test/test_shearwater_perdix2_tanks.c | 87 ++++++++++++++++++++++++++++ test/test_shearwater_tank_gas.c | 28 +++++++++ 5 files changed, 142 insertions(+), 16 deletions(-) create mode 100644 test/test_shearwater_perdix2_tanks.c diff --git a/src/shearwater_predator_parser.c b/src/shearwater_predator_parser.c index 6679cbd4..9e52926e 100644 --- a/src/shearwater_predator_parser.c +++ b/src/shearwater_predator_parser.c @@ -578,7 +578,7 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) // The gas mix in use and the tank pressures of every sample, used to work // out which gas mix each tank is on once all samples have been seen. unsigned int maxsamples = length > offset ? (length - offset) / parser->samplesize : 0; - unsigned int *trace_gas = (unsigned int *) malloc ((maxsamples + 1) * sizeof (unsigned int)); + unsigned int *trace_gas = (unsigned int *) calloc (maxsamples + 1, sizeof (unsigned int)); unsigned int *trace_pressure = (unsigned int *) calloc ((maxsamples + 1) * NTANKS, sizeof (unsigned int)); if (trace_gas == NULL || trace_pressure == NULL) { ERROR (abstract->context, "Failed to allocate memory."); @@ -961,24 +961,26 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser) } // AI-generated (Claude) - // The begin pressure is the highest reading, not the first one: a stage - // with its valve closed until the gas switch first reads the pressure left - // in its regulator. For open circuit dives, link each tank to the gas mix - // it was used on, if that is clear from the pressure drops. + // For open circuit dives, the begin pressure is the highest reading, not + // the first one: a stage with its valve closed until the gas switch first + // reads the pressure left in its regulator. And link each tank to the gas + // mix it was used on, if that is clear from the pressure drops. unsigned int assigned[NTANKS]; unsigned int sidemount = 0; + unsigned int oc = divemode == M_OC_TEC || divemode == M_OC_REC || divemode == M_AVELO; for (unsigned int i = 0; i < NTANKS; ++i) { assigned[i] = SHEARWATER_TANK_GAS_NONE; if (tank[i].usage == DC_TANK_USAGE_SIDEMOUNT) sidemount |= 1u << i; - for (unsigned int n = 0; n < nsamples; ++n) { + for (unsigned int n = 0; n < nsamples && oc; ++n) { if (trace_pressure[n * NTANKS + i] > tank[i].beginpressure) tank[i].beginpressure = trace_pressure[n * NTANKS + i]; } } - if (divemode != M_FREEDIVE && !shearwater_predator_is_ccr (divemode)) { + if (oc) { + // At least 10 bar, in units of 2 psi, rounded up. shearwater_tank_gas_assign (trace_gas, trace_pressure, nsamples, NTANKS, ngasmixes, - sidemount, (int) (10.0 * BAR / PSI / 2), assigned); + sidemount, 73, assigned); } for (unsigned int i = 0; i < NTANKS; ++i) tank[i].gasmix = assigned[i] < NGASMIXES ? gasmap[assigned[i]] : UNDEFINED; diff --git a/src/shearwater_tank_gas.h b/src/shearwater_tank_gas.h index 589b28ac..b3fd5cb8 100644 --- a/src/shearwater_tank_gas.h +++ b/src/shearwater_tank_gas.h @@ -47,6 +47,7 @@ shearwater_tank_gas_assign (const unsigned int gas[], const unsigned int pressur unsigned int sidemount, int minuse, unsigned int result[]) { int use[SHEARWATER_TANK_GAS_MAXTANKS][SHEARWATER_TANK_GAS_MAXGASES] = {{0}}; + const unsigned int stride = ntanks; if (ntanks > SHEARWATER_TANK_GAS_MAXTANKS) ntanks = SHEARWATER_TANK_GAS_MAXTANKS; @@ -56,21 +57,28 @@ shearwater_tank_gas_assign (const unsigned int gas[], const unsigned int pressur for (unsigned int i = 0; i < ntanks; ++i) { unsigned int highest = 0, start = 0; for (unsigned int n = 0; n < nsamples; ++n) { - if (pressure[n * ntanks + i] > highest) { - highest = pressure[n * ntanks + i]; + if (pressure[n * stride + i] > highest) { + highest = pressure[n * stride + i]; start = n; } } + if (!highest) + continue; - unsigned int previous = highest, previous_gas = gas[start]; - for (unsigned int n = start + 1; n < nsamples && highest; ++n) { - unsigned int p = pressure[n * ntanks + i]; + // A drop between two readings only counts if the same gas mix was + // in use throughout, including the samples without a reading. + unsigned int previous = highest, previous_gas = gas[start], changed = 0; + for (unsigned int n = start + 1; n < nsamples; ++n) { + if (gas[n] != previous_gas) + changed = 1; + unsigned int p = pressure[n * stride + i]; if (p == 0) continue; - if (gas[n] < ngases && gas[n] == previous_gas) + if (!changed && gas[n] < ngases) use[i][gas[n]] += (int) previous - (int) p; previous = p; previous_gas = gas[n]; + changed = 0; } } diff --git a/test/Makefile.am b/test/Makefile.am index 3c80cecd..3a1e35e4 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -3,15 +3,16 @@ AUTOMAKE_OPTIONS = subdir-objects AM_CPPFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include LDADD = $(top_builddir)/src/libdivecomputer.la -check_PROGRAMS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas +check_PROGRAMS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas test_shearwater_perdix2_tanks test_descriptor_product_name_SOURCES = test_descriptor_product_name.c test_mares_iconhd_freedive_SOURCES = test_mares_iconhd_freedive.c $(top_srcdir)/src/array.c test_mares_iconhd_freedive_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src test_shearwater_tank_gas_SOURCES = test_shearwater_tank_gas.c test_shearwater_tank_gas_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src +test_shearwater_perdix2_tanks_SOURCES = test_shearwater_perdix2_tanks.c -TESTS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas +TESTS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas test_shearwater_perdix2_tanks EXTRA_DIST = \ run-parse.sh \ diff --git a/test/test_shearwater_perdix2_tanks.c b/test/test_shearwater_perdix2_tanks.c new file mode 100644 index 00000000..6503ac2a --- /dev/null +++ b/test/test_shearwater_perdix2_tanks.c @@ -0,0 +1,87 @@ +// AI-generated (Claude) +// Parses the Perdix 2 fixture, a dive with a sidemount pair on 21% (T1, T2) +// and stages on 50% (T3) and oxygen (T4), with the stage valves closed until +// the gas switch, and checks the tanks the parser reports. +#include +#include +#include + +#include +#include +#include + +#define NTANKS 4 + +int +main (void) +{ + // The gas mixes are 99%, 50% and 21%; diluent slot 5 is filtered out. + const unsigned int expected_gasmix[NTANKS] = {2, 2, 1, 0}; + // The highest readings, in bar. T3 and T4 first read 179 and 115 bar. + const double expected_begin[NTANKS] = {220.0, 232.0, 207.0, 162.0}; + int failures = 0; + + const char *srcdir = getenv ("srcdir"); + char path[1024]; + snprintf (path, sizeof (path), "%s/fixtures/shearwater_perdix2-0001.bin", srcdir ? srcdir : "."); + FILE *fp = fopen (path, "rb"); + if (fp == NULL) { + fprintf (stderr, "FAIL: cannot open %s\n", path); + return 1; + } + static unsigned char data[64 * 1024]; + size_t size = fread (data, 1, sizeof (data), fp); + fclose (fp); + + dc_context_t *context = NULL; + dc_context_new (&context); + dc_iterator_t *iterator = NULL; + dc_descriptor_t *descriptor = NULL, *d = NULL; + dc_descriptor_iterator_new (&iterator, context); + while (dc_iterator_next (iterator, &d) == DC_STATUS_SUCCESS) { + if (strcmp (dc_descriptor_get_vendor (d), "Shearwater") == 0 && + strcmp (dc_descriptor_get_product (d), "Perdix 2") == 0) { + descriptor = d; + break; + } + dc_descriptor_free (d); + } + dc_iterator_free (iterator); + if (descriptor == NULL) { + fprintf (stderr, "FAIL: no Perdix 2 descriptor\n"); + return 1; + } + + dc_parser_t *parser = NULL; + if (dc_parser_new2 (&parser, context, descriptor, data, size) != DC_STATUS_SUCCESS) { + fprintf (stderr, "FAIL: cannot create the parser\n"); + return 1; + } + + unsigned int ngasmixes = 0, ntanks = 0; + dc_parser_get_field (parser, DC_FIELD_GASMIX_COUNT, 0, &ngasmixes); + dc_parser_get_field (parser, DC_FIELD_TANK_COUNT, 0, &ntanks); + if (ngasmixes != 3 || ntanks != NTANKS) { + fprintf (stderr, "FAIL: %u gas mixes and %u tanks, expected 3 and %u\n", ngasmixes, ntanks, NTANKS); + failures++; + } else { + for (unsigned int i = 0; i < NTANKS; ++i) { + dc_tank_t tank = {0}; + dc_parser_get_field (parser, DC_FIELD_TANK, i, &tank); + if (tank.gasmix != expected_gasmix[i]) { + fprintf (stderr, "FAIL: tank %u on gas mix %d, expected %u\n", i, (int) tank.gasmix, expected_gasmix[i]); + failures++; + } + if (tank.beginpressure < expected_begin[i] - 1.0 || tank.beginpressure > expected_begin[i] + 1.0) { + fprintf (stderr, "FAIL: tank %u begin pressure %.1f bar, expected %.0f bar\n", i, tank.beginpressure, expected_begin[i]); + failures++; + } + } + } + + dc_parser_destroy (parser); + dc_descriptor_free (descriptor); + dc_context_free (context); + + return failures ? 1 : 0; +} diff --git a/test/test_shearwater_tank_gas.c b/test/test_shearwater_tank_gas.c index 86cde01c..78fd0a02 100644 --- a/test/test_shearwater_tank_gas.c +++ b/test/test_shearwater_tank_gas.c @@ -110,5 +110,33 @@ main (void) failures += check ("one-sided sidemount", &e, 0x3, expected_e); failures += check ("one-sided without sidemount flag", &e, 0, (const unsigned int[NTANKS]) {2, NONE, NONE, NONE}); + // The limits are inclusive: exactly the minimum use, and exactly twice + // the use on the next mix, still get a link; one less doesn't. + trace_t f = {0}; + breathe (&f, 0, 30, 1); + breathe (&f, 30, 60, 0); + reading (&f, 0, 0, 30, 1000, 0); + reading (&f, 0, 30, 31, 1000, 0); + reading (&f, 0, 31, 60, 930, 0); + reading (&f, 1, 0, 2, 1000, 35); + reading (&f, 1, 2, 30, 965, 0); + reading (&f, 1, 30, 31, 965, 0); + reading (&f, 1, 31, 60, 895, 0); + reading (&f, 2, 0, 30, 1000, 0); + reading (&f, 2, 30, 31, 1000, 0); + reading (&f, 2, 31, 60, 931, 0); + const unsigned int expected_f[NTANKS] = {0, 0, NONE, NONE}; + failures += check ("inclusive limits", &f, 0, expected_f); + + // A drop across missing readings, during which the gas mix changed and + // changed back, can't be attributed to either mix. + trace_t g = {0}; + breathe (&g, 0, 60, 0); + breathe (&g, 20, 40, 1); + reading (&g, 0, 0, 10, 1500, 0); + reading (&g, 0, 50, 60, 1200, 0); + const unsigned int expected_g[NTANKS] = {NONE, NONE, NONE, NONE}; + failures += check ("gas change during missing readings", &g, 0, expected_g); + return failures ? 1 : 0; }