summaryrefslogtreecommitdiffstats
path: root/core/dive.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/dive.c')
-rw-r--r--core/dive.c215
1 files changed, 114 insertions, 101 deletions
diff --git a/core/dive.c b/core/dive.c
index 482ba585b..044c969ee 100644
--- a/core/dive.c
+++ b/core/dive.c
@@ -99,13 +99,13 @@ void add_sample_pressure(struct sample *sample, int sensor, int mbar)
* This function returns a negative number for "no legacy mode",
* or a non-negative number that indicates the o2 sensor index.
*/
-int legacy_format_o2pressures(struct dive *dive, struct divecomputer *dc)
+int legacy_format_o2pressures(const struct dive *dive, const struct divecomputer *dc)
{
int i, o2sensor;
o2sensor = (dc->divemode == CCR) ? get_cylinder_idx_by_use(dive, OXYGEN) : -1;
for (i = 0; i < dc->samples; i++) {
- struct sample *s = dc->sample + i;
+ const struct sample *s = dc->sample + i;
int seen_pressure = 0, idx;
for (idx = 0; idx < MAX_SENSORS; idx++) {
@@ -130,7 +130,7 @@ int legacy_format_o2pressures(struct dive *dive, struct divecomputer *dc)
return o2sensor < 0 ? 256 : o2sensor;
}
-int event_is_gaschange(struct event *ev)
+int event_is_gaschange(const struct event *ev)
{
return ev->type == SAMPLE_EVENT_GASCHANGE ||
ev->type == SAMPLE_EVENT_GASCHANGE2;
@@ -184,7 +184,7 @@ struct event *add_event(struct divecomputer *dc, unsigned int time, int type, in
return ev;
}
-static int same_event(struct event *a, struct event *b)
+static int same_event(const struct event *a, const struct event *b)
{
if (a->time.seconds != b->time.seconds)
return 0;
@@ -255,9 +255,9 @@ void add_extra_data(struct divecomputer *dc, const char *key, const char *value)
* saving the dive mode for each event. When the events occur AFTER 'time' seconds, the last stored divemode
* is returned. This function is self-tracking, relying on setting the event pointer 'evp' so that, in each iteration
* that calls this function, the search does not have to begin at the first event of the dive */
-enum divemode_t get_current_divemode(struct divecomputer *dc, int time, struct event **evp, enum divemode_t *divemode)
+enum divemode_t get_current_divemode(const struct divecomputer *dc, int time, const struct event **evp, enum divemode_t *divemode)
{
- struct event *ev = *evp;
+ const struct event *ev = *evp;
if (*divemode == UNDEF_COMP_TYPE) {
*divemode = dc->divemode;
ev = dc ? get_next_event(dc->events, "modechange") : NULL;
@@ -270,17 +270,16 @@ enum divemode_t get_current_divemode(struct divecomputer *dc, int time, struct e
return *divemode;
}
-/* this returns a pointer to static variable - so use it right away after calling */
-struct gasmix *get_gasmix_from_event(struct dive *dive, struct event *ev)
+struct gasmix get_gasmix_from_event(const struct dive *dive, const struct event *ev)
{
- static struct gasmix dummy;
+ struct gasmix dummy = { 0 };
if (ev && event_is_gaschange(ev)) {
int index = ev->gas.index;
if (index >= 0 && index < MAX_CYLINDERS)
- return &dive->cylinder[index].gasmix;
- return &ev->gas.mix;
+ return dive->cylinder[index].gasmix;
+ return ev->gas.mix;
}
- return &dummy;
+ return dummy;
}
int get_pressure_units(int mb, const char **units)
@@ -481,7 +480,7 @@ static void free_pic(struct picture *picture);
/* this is very different from the copy_divecomputer later in this file;
* this function actually makes full copies of the content */
-static void copy_dc(struct divecomputer *sdc, struct divecomputer *ddc)
+static void copy_dc(const struct divecomputer *sdc, struct divecomputer *ddc)
{
*ddc = *sdc;
ddc->model = copy_string(sdc->model);
@@ -557,7 +556,7 @@ void clear_dive(struct dive *d)
/* make a true copy that is independent of the source dive;
* all data structures are duplicated, so the copy can be modified without
* any impact on the source */
-void copy_dive(struct dive *s, struct dive *d)
+void copy_dive(const struct dive *s, struct dive *d)
{
clear_dive(d);
/* simply copy things over, but then make actual copies of the
@@ -598,7 +597,7 @@ struct dive *clone_dive(struct dive *s)
d->_component = copy_string(s->_component)
// copy elements, depending on bits in what that are set
-void selective_copy_dive(struct dive *s, struct dive *d, struct dive_components what, bool clear)
+void selective_copy_dive(const struct dive *s, struct dive *d, struct dive_components what, bool clear)
{
if (clear)
clear_dive(d);
@@ -642,9 +641,10 @@ struct event *clone_event(const struct event *src_ev)
}
/* copies all events in this dive computer */
-void copy_events(struct divecomputer *s, struct divecomputer *d)
+void copy_events(const struct divecomputer *s, struct divecomputer *d)
{
- struct event *ev, **pev;
+ const struct event *ev;
+ struct event **pev;
if (!s || !d)
return;
ev = s->events;
@@ -658,24 +658,24 @@ void copy_events(struct divecomputer *s, struct divecomputer *d)
*pev = NULL;
}
-int nr_cylinders(struct dive *dive)
+int nr_cylinders(const struct dive *dive)
{
int nr;
for (nr = MAX_CYLINDERS; nr; --nr) {
- cylinder_t *cylinder = dive->cylinder + nr - 1;
+ const cylinder_t *cylinder = dive->cylinder + nr - 1;
if (!cylinder_nodata(cylinder))
break;
}
return nr;
}
-int nr_weightsystems(struct dive *dive)
+int nr_weightsystems(const struct dive *dive)
{
int nr;
for (nr = MAX_WEIGHTSYSTEMS; nr; --nr) {
- weightsystem_t *ws = dive->weightsystem + nr - 1;
+ const weightsystem_t *ws = dive->weightsystem + nr - 1;
if (!weightsystem_none(ws))
break;
}
@@ -683,7 +683,7 @@ int nr_weightsystems(struct dive *dive)
}
/* copy the equipment data part of the cylinders */
-void copy_cylinders(struct dive *s, struct dive *d, bool used_only)
+void copy_cylinders(const struct dive *s, struct dive *d, bool used_only)
{
int i,j;
cylinder_t t[MAX_CYLINDERS];
@@ -729,7 +729,7 @@ int cylinderuse_from_text(const char *text)
return -1;
}
-void copy_samples(struct divecomputer *s, struct divecomputer *d)
+void copy_samples(const struct divecomputer *s, struct divecomputer *d)
{
/* instead of carefully copying them one by one and calling add_sample
* over and over again, let's just copy the whole blob */
@@ -876,13 +876,13 @@ void fixup_dc_duration(struct divecomputer *dc)
/* Which cylinders had gas used? */
#define SOME_GAS 5000
-static unsigned int get_cylinder_used(struct dive *dive)
+static unsigned int get_cylinder_used(const struct dive *dive)
{
int i;
unsigned int mask = 0;
for (i = 0; i < MAX_CYLINDERS; i++) {
- cylinder_t *cyl = dive->cylinder + i;
+ const cylinder_t *cyl = dive->cylinder + i;
int start_mbar, end_mbar;
if (cylinder_nodata(cyl))
@@ -899,10 +899,10 @@ static unsigned int get_cylinder_used(struct dive *dive)
}
/* Which cylinders do we know usage about? */
-static unsigned int get_cylinder_known(struct dive *dive, struct divecomputer *dc)
+static unsigned int get_cylinder_known(const struct dive *dive, const struct divecomputer *dc)
{
unsigned int mask = 0;
- struct event *ev;
+ const struct event *ev;
/* We know about using the O2 cylinder in a CCR dive */
if (dc->divemode == CCR) {
@@ -926,7 +926,7 @@ static unsigned int get_cylinder_known(struct dive *dive, struct divecomputer *d
return mask;
}
-void per_cylinder_mean_depth(struct dive *dive, struct divecomputer *dc, int *mean, int *duration)
+void per_cylinder_mean_depth(const struct dive *dive, struct divecomputer *dc, int *mean, int *duration)
{
int i;
int depthtime[MAX_CYLINDERS] = { 0, };
@@ -974,7 +974,7 @@ void per_cylinder_mean_depth(struct dive *dive, struct divecomputer *dc, int *me
}
if (!dc->samples)
fake_dc(dc);
- struct event *ev = get_next_event(dc->events, "gaschange");
+ const struct event *ev = get_next_event(dc->events, "gaschange");
for (i = 0; i < dc->samples; i++) {
struct sample *sample = dc->sample + i;
uint32_t time = sample->time.seconds;
@@ -1019,10 +1019,10 @@ static void update_min_max_temperatures(struct dive *dive, temperature_t tempera
}
}
-int gas_volume(cylinder_t *cyl, pressure_t p)
+int gas_volume(const cylinder_t *cyl, pressure_t p)
{
double bar = p.mbar / 1000.0;
- double z_factor = gas_compressibility_factor(&cyl->gasmix, bar);
+ double z_factor = gas_compressibility_factor(cyl->gasmix, bar);
return lrint(cyl->type.size.mliter * bar_to_atm(bar) / z_factor);
}
@@ -1040,10 +1040,10 @@ static int same_rounded_pressure(pressure_t a, pressure_t b)
* tell us what the first gas is with a gas change event in the first sample.
* Sneakily we'll use a return value of 0 (or FALSE) when there is no explicit
* first cylinder - in which case cylinder 0 is indeed the first cylinder */
-int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc)
+int explicit_first_cylinder(const struct dive *dive, const struct divecomputer *dc)
{
if (dc) {
- struct event *ev = get_next_event(dc->events, "gaschange");
+ const struct event *ev = get_next_event(dc->events, "gaschange");
if (ev && ((dc->sample && ev->time.seconds == dc->sample[0].time.seconds) || ev->time.seconds <= 1))
return get_cylinder_index(dive, ev);
else if (dc->divemode == CCR)
@@ -1055,7 +1055,7 @@ int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc)
/* this gets called when the dive mode has changed (so OC vs. CC)
* there are two places we might have setpoints... events or in the samples
*/
-void update_setpoint_events(struct dive *dive, struct divecomputer *dc)
+void update_setpoint_events(const struct dive *dive, struct divecomputer *dc)
{
struct event *ev;
int new_setpoint = 0;
@@ -1072,9 +1072,9 @@ void update_setpoint_events(struct dive *dive, struct divecomputer *dc)
// by mistake when it's actually CCR is _bad_
// So we make sure, this comes from a Predator or Petrel and we only remove
// pO2 values we would have computed anyway.
- struct event *ev = get_next_event(dc->events, "gaschange");
- struct gasmix *gasmix = get_gasmix_from_event(dive, ev);
- struct event *next = get_next_event(ev, "gaschange");
+ const struct event *ev = get_next_event(dc->events, "gaschange");
+ struct gasmix gasmix = get_gasmix_from_event(dive, ev);
+ const struct event *next = get_next_event(ev, "gaschange");
for (int i = 0; i < dc->samples; i++) {
struct gas_pressures pressures;
@@ -1092,7 +1092,7 @@ void update_setpoint_events(struct dive *dive, struct divecomputer *dc)
// an "SP change" event at t=0 is currently our marker for OC vs CCR
// this will need to change to a saner setup, but for now we can just
// check if such an event is there and adjust it, or add that event
- ev = get_next_event(dc->events, "SP change");
+ ev = get_next_event_mutable(dc->events, "SP change");
if (ev && ev->time.seconds == 0) {
ev->value = new_setpoint;
} else {
@@ -1113,7 +1113,7 @@ void sanitize_gasmix(struct gasmix *mix)
if (!o2)
return;
/* 20.8% to 21% O2 is just air */
- if (gasmix_is_air(mix)) {
+ if (gasmix_is_air(*mix)) {
mix->o2.permille = 0;
return;
}
@@ -1232,7 +1232,7 @@ static void sanitize_cylinder_info(struct dive *dive)
* Output: i) The icd_data stucture is filled with the delta_N2 and delta_He numbers (as permille).
* ii) Function returns a boolean indicating an exceeding of the rule-of-fifths. False = no icd problem.
*/
-bool isobaric_counterdiffusion(struct gasmix *oldgasmix, struct gasmix *newgasmix, struct icd_data *results)
+bool isobaric_counterdiffusion(struct gasmix oldgasmix, struct gasmix newgasmix, struct icd_data *results)
{
if (!prefs.show_icd)
return false;
@@ -1242,7 +1242,7 @@ bool isobaric_counterdiffusion(struct gasmix *oldgasmix, struct gasmix *newgasmi
}
/* some events should never be thrown away */
-static bool is_potentially_redundant(struct event *event)
+static bool is_potentially_redundant(const struct event *event)
{
if (!strcmp(event->name, "gaschange"))
return false;
@@ -1331,7 +1331,7 @@ static void fixup_duration(struct dive *dive)
* What do the dive computers say the water temperature is?
* (not in the samples, but as dc property for dcs that support that)
*/
-unsigned int dc_watertemp(struct divecomputer *dc)
+unsigned int dc_watertemp(const struct divecomputer *dc)
{
int sum = 0, nr = 0;
@@ -1355,7 +1355,7 @@ static void fixup_watertemp(struct dive *dive)
/*
* What do the dive computers say the air temperature is?
*/
-unsigned int dc_airtemp(struct divecomputer *dc)
+unsigned int dc_airtemp(const struct divecomputer *dc)
{
int sum = 0, nr = 0;
@@ -1609,7 +1609,7 @@ static void fixup_dive_pressures(struct dive *dive, struct divecomputer *dc)
simplify_dc_pressures(dc);
}
-int find_best_gasmix_match(struct gasmix *mix, cylinder_t array[], unsigned int used)
+int find_best_gasmix_match(struct gasmix mix, const cylinder_t array[], unsigned int used)
{
int i;
int best = -1, score = INT_MAX;
@@ -1623,7 +1623,7 @@ int find_best_gasmix_match(struct gasmix *mix, cylinder_t array[], unsigned int
match = array + i;
if (cylinder_nodata(match))
continue;
- distance = gasmix_distance(mix, &match->gasmix);
+ distance = gasmix_distance(mix, match->gasmix);
if (distance >= score)
continue;
best = i;
@@ -1641,14 +1641,14 @@ static bool validate_gaschange(struct dive *dive, struct event *event)
int o2, he, value;
/* We'll get rid of the per-event gasmix, but for now sanitize it */
- if (gasmix_is_air(&event->gas.mix))
+ if (gasmix_is_air(event->gas.mix))
event->gas.mix.o2.permille = 0;
/* Do we already have a cylinder index for this gasmix? */
if (event->gas.index >= 0)
return true;
- index = find_best_gasmix_match(&event->gas.mix, dive->cylinder, 0);
+ index = find_best_gasmix_match(event->gas.mix, dive->cylinder, 0);
if (index < 0)
return false;
@@ -1657,8 +1657,8 @@ static bool validate_gaschange(struct dive *dive, struct event *event)
event->gas.mix = dive->cylinder[index].gasmix;
/* Convert to odd libdivecomputer format */
- o2 = get_o2(&event->gas.mix);
- he = get_he(&event->gas.mix);
+ o2 = get_o2(event->gas.mix);
+ he = get_he(event->gas.mix);
o2 = (o2 + 5) / 10;
he = (he + 5) / 10;
@@ -1804,7 +1804,7 @@ struct dive *fixup_dive(struct dive *dive)
#define MERGE_TXT(res, a, b, n, sep) res->n = merge_text(a->n, b->n, sep)
#define MERGE_NONZERO(res, a, b, n) res->n = a->n ? a->n : b->n
-struct sample *add_sample(struct sample *sample, int time, struct divecomputer *dc)
+struct sample *add_sample(const struct sample *sample, int time, struct divecomputer *dc)
{
struct sample *p = prepare_sample(dc);
@@ -1969,7 +1969,7 @@ static char *merge_text(const char *a, const char *b, const char *sep)
if (a->field != b->field) \
return a->field < b->field ? -1 : 1
-static int sort_event(struct event *a, struct event *b)
+static int sort_event(const struct event *a, const struct event *b)
{
SORT(a, b, time.seconds);
SORT(a, b, type);
@@ -1978,10 +1978,10 @@ static int sort_event(struct event *a, struct event *b)
return strcmp(a->name, b->name);
}
-static int same_gas(struct event *a, struct event *b)
+static int same_gas(const struct event *a, const struct event *b)
{
if (a->type == b->type && a->flags == b->flags && a->value == b->value && !strcmp(a->name, b->name) &&
- same_gasmix(&a->gas.mix, &b->gas.mix)) {
+ same_gasmix(a->gas.mix, b->gas.mix)) {
return true;
}
return false;
@@ -2056,7 +2056,7 @@ static void merge_events(struct divecomputer *res, struct divecomputer *src1, st
}
}
-static void merge_weightsystem_info(weightsystem_t *res, weightsystem_t *a, weightsystem_t *b)
+static void merge_weightsystem_info(weightsystem_t *res, const weightsystem_t *a, const weightsystem_t *b)
{
if (!a->weight.grams)
a = b;
@@ -2069,7 +2069,7 @@ static void merge_weightsystem_info(weightsystem_t *res, weightsystem_t *a, weig
* A negative number returned indicates that a match could not be found.
* Call parameters: dive = the dive being processed
* cylinder_use_type = an enum, one of {oxygen, diluent, bailout} */
-extern int get_cylinder_idx_by_use(struct dive *dive, enum cylinderuse cylinder_use_type)
+extern int get_cylinder_idx_by_use(const struct dive *dive, enum cylinderuse cylinder_use_type)
{
int cylinder_index;
for (cylinder_index = 0; cylinder_index < MAX_CYLINDERS; cylinder_index++) {
@@ -2079,7 +2079,7 @@ extern int get_cylinder_idx_by_use(struct dive *dive, enum cylinderuse cylinder_
return -1; // negative number means cylinder_use_type not found in list of cylinders
}
-int gasmix_distance(const struct gasmix *a, const struct gasmix *b)
+int gasmix_distance(struct gasmix a, struct gasmix b)
{
int a_o2 = get_o2(a), b_o2 = get_o2(b);
int a_he = get_he(a), b_he = get_he(b);
@@ -2100,7 +2100,7 @@ int gasmix_distance(const struct gasmix *a, const struct gasmix *b)
* divemode = the dive mode pertaining to this point in the dive profile.
* This function called by: calculate_gas_information_new() in profile.c; add_segment() in deco.c.
*/
-extern void fill_pressures(struct gas_pressures *pressures, const double amb_pressure, const struct gasmix *mix, double po2, enum divemode_t divemode)
+extern void fill_pressures(struct gas_pressures *pressures, const double amb_pressure, struct gasmix mix, double po2, enum divemode_t divemode)
{
if ((divemode != OC) && po2) { // This is a rebreather dive where pressures->o2 is defined
if (po2 >= amb_pressure) {
@@ -2138,7 +2138,7 @@ extern void fill_pressures(struct gas_pressures *pressures, const double amb_pre
/* Force an initial gaschange event to the (old) gas #0 */
static void add_initial_gaschange(struct dive *dive, struct divecomputer *dc)
{
- struct event *ev = get_next_event(dc->events, "gaschange");
+ const struct event *ev = get_next_event(dc->events, "gaschange");
if (ev && ev->time.seconds < 30)
return;
@@ -2205,20 +2205,20 @@ void cylinder_renumber(struct dive *dive, int mapping[])
dc_cylinder_renumber(dive, dc, mapping);
}
-int same_gasmix(struct gasmix *a, struct gasmix *b)
+int same_gasmix(struct gasmix a, struct gasmix b)
{
if (gasmix_is_air(a) && gasmix_is_air(b))
return 1;
- return a->o2.permille == b->o2.permille && a->he.permille == b->he.permille;
+ return a.o2.permille == b.o2.permille && a.he.permille == b.he.permille;
}
int same_gasmix_cylinder(cylinder_t *cyl, int cylid, struct dive *dive, bool check_unused)
{
- struct gasmix *mygas = &cyl->gasmix;
+ struct gasmix mygas = cyl->gasmix;
for (int i = 0; i < MAX_CYLINDERS; i++) {
if (i == cylid || cylinder_none(&dive->cylinder[i]))
continue;
- struct gasmix *gas2 = &dive->cylinder[i].gasmix;
+ struct gasmix gas2 = dive->cylinder[i].gasmix;
if (gasmix_distance(mygas, gas2) == 0 && (is_cylinder_used(dive, i) || check_unused))
return i;
}
@@ -2230,7 +2230,7 @@ static int pdiff(pressure_t a, pressure_t b)
return a.mbar && b.mbar && a.mbar != b.mbar;
}
-static int different_manual_pressures(cylinder_t *a, cylinder_t *b)
+static int different_manual_pressures(const cylinder_t *a, const cylinder_t *b)
{
return pdiff(a->start, b->start) || pdiff(a->end, b->end);
}
@@ -2244,17 +2244,17 @@ static int different_manual_pressures(cylinder_t *a, cylinder_t *b)
* same cylinder use (ie OC/Diluent/Oxygen), and if pressures
* have been added manually they need to match.
*/
-static int match_cylinder(cylinder_t *cyl, struct dive *dive, unsigned int available)
+static int match_cylinder(const cylinder_t *cyl, const struct dive *dive, unsigned int available)
{
int i;
for (i = 0; i < MAX_CYLINDERS; i++) {
- cylinder_t *target;
+ const cylinder_t *target;
if (!(available & (1u << i)))
continue;
target = dive->cylinder + i;
- if (!same_gasmix(&cyl->gasmix, &target->gasmix))
+ if (!same_gasmix(cyl->gasmix, target->gasmix))
continue;
if (cyl->cylinder_use != target->cylinder_use)
continue;
@@ -2425,7 +2425,7 @@ static void merge_temperatures(struct dive *res, struct dive *a, struct dive *b)
* The 'next' dive is not involved in the dive merging, but is the dive
* that will be the next dive after the merged dive.
*/
-static void pick_trip(struct dive *res, struct dive *pick)
+static void pick_trip(struct dive *res, const struct dive *pick)
{
tripflag_t tripflag = pick->tripflag;
dive_trip_t *trip = pick->divetrip;
@@ -2690,7 +2690,7 @@ static int similar(unsigned long a, unsigned long b, unsigned long expected)
* positive for "same dive" and negative for "definitely
* not the same dive"
*/
-int match_one_dc(struct divecomputer *a, struct divecomputer *b)
+int match_one_dc(const struct divecomputer *a, const struct divecomputer *b)
{
/* Not same model? Don't know if matching.. */
if (!a->model || !b->model)
@@ -2722,10 +2722,10 @@ int match_one_dc(struct divecomputer *a, struct divecomputer *b)
* 0 for "don't know"
* 1 for "is definitely the same dive"
*/
-static int match_dc_dive(struct divecomputer *a, struct divecomputer *b)
+static int match_dc_dive(const struct divecomputer *a, const struct divecomputer *b)
{
do {
- struct divecomputer *tmp = b;
+ const struct divecomputer *tmp = b;
do {
int match = match_one_dc(a, tmp);
if (match)
@@ -2737,7 +2737,7 @@ static int match_dc_dive(struct divecomputer *a, struct divecomputer *b)
return 0;
}
-static bool new_without_trip(struct dive *a)
+static bool new_without_trip(const struct dive *a)
{
return a->downloaded && !a->divetrip;
}
@@ -2771,7 +2771,7 @@ static bool new_without_trip(struct dive *a)
* dives together manually. But this tries to handle the sane
* cases.
*/
-static int likely_same_dive(struct dive *a, struct dive *b)
+static int likely_same_dive(const struct dive *a, const struct dive *b)
{
int match, fuzz = 20 * 60;
@@ -2875,7 +2875,7 @@ static int same_sample(struct sample *a, struct sample *b)
static int same_dc(struct divecomputer *a, struct divecomputer *b)
{
int i;
- struct event *eva, *evb;
+ const struct event *eva, *evb;
i = match_one_dc(a, b);
if (i)
@@ -2899,7 +2899,7 @@ static int same_dc(struct divecomputer *a, struct divecomputer *b)
return eva == evb;
}
-static int might_be_same_device(struct divecomputer *a, struct divecomputer *b)
+static int might_be_same_device(const struct divecomputer *a, const struct divecomputer *b)
{
/* No dive computer model? That matches anything */
if (!a->model || !b->model)
@@ -2958,7 +2958,7 @@ static struct divecomputer *find_matching_computer(struct divecomputer *match, s
}
-static void copy_dive_computer(struct divecomputer *res, struct divecomputer *a)
+static void copy_dive_computer(struct divecomputer *res, const struct divecomputer *a)
{
*res = *a;
res->model = copy_string(a->model);
@@ -3859,7 +3859,7 @@ static bool new_picture_for_dive(struct dive *d, const char *filename)
// only add pictures that have timestamps between 30 minutes before the dive and
// 30 minutes after the dive ends
#define D30MIN (30 * 60)
-bool dive_check_picture_time(struct dive *d, int shift_time, timestamp_t timestamp)
+bool dive_check_picture_time(const struct dive *d, int shift_time, timestamp_t timestamp)
{
offset_t offset;
if (timestamp) {
@@ -4026,7 +4026,7 @@ void delete_current_divecomputer(void)
/* helper function to make it easier to work with our structures
* we don't interpolate here, just use the value from the last sample up to that time */
-int get_depth_at_time(struct divecomputer *dc, unsigned int time)
+int get_depth_at_time(const struct divecomputer *dc, unsigned int time)
{
int depth = 0;
if (dc && dc->sample)
@@ -4039,7 +4039,7 @@ int get_depth_at_time(struct divecomputer *dc, unsigned int time)
}
//Calculate O2 in best mix
-fraction_t best_o2(depth_t depth, struct dive *dive)
+fraction_t best_o2(depth_t depth, const struct dive *dive)
{
fraction_t fo2;
@@ -4051,7 +4051,7 @@ fraction_t best_o2(depth_t depth, struct dive *dive)
}
//Calculate He in best mix. O2 is considered narcopic
-fraction_t best_he(depth_t depth, struct dive *dive)
+fraction_t best_he(depth_t depth, const struct dive *dive)
{
fraction_t fhe;
int pnarcotic, ambient;
@@ -4063,10 +4063,10 @@ fraction_t best_he(depth_t depth, struct dive *dive)
return fhe;
}
-bool gasmix_is_air(const struct gasmix *gasmix)
+bool gasmix_is_air(struct gasmix gasmix)
{
- int o2 = gasmix->o2.permille;
- int he = gasmix->he.permille;
+ int o2 = gasmix.o2.permille;
+ int he = gasmix.he.permille;
return (he == 0) && (o2 == 0 || ((o2 >= O2_IN_AIR - 1) && (o2 <= O2_IN_AIR + 1)));
}
@@ -4108,17 +4108,17 @@ int calculate_depth_to_mbar(int depth, pressure_t surface_pressure, int salinity
return mbar;
}
-int depth_to_mbar(int depth, struct dive *dive)
+int depth_to_mbar(int depth, const struct dive *dive)
{
return calculate_depth_to_mbar(depth, dive->surface_pressure, dive->salinity);
}
-double depth_to_bar(int depth, struct dive *dive)
+double depth_to_bar(int depth, const struct dive *dive)
{
return depth_to_mbar(depth, dive) / 1000.0;
}
-double depth_to_atm(int depth, struct dive *dive)
+double depth_to_atm(int depth, const struct dive *dive)
{
return mbar_to_atm(depth_to_mbar(depth, dive));
}
@@ -4127,7 +4127,7 @@ double depth_to_atm(int depth, struct dive *dive)
* (that's the one that some dive computers like the Uemis Zurich
* provide - for the other models that do this libdivecomputer has to
* take care of this, but the Uemis we support natively */
-int rel_mbar_to_depth(int mbar, struct dive *dive)
+int rel_mbar_to_depth(int mbar, const struct dive *dive)
{
int cm;
double specific_weight = 1.03 * 0.981;
@@ -4138,7 +4138,7 @@ int rel_mbar_to_depth(int mbar, struct dive *dive)
return cm * 10;
}
-int mbar_to_depth(int mbar, struct dive *dive)
+int mbar_to_depth(int mbar, const struct dive *dive)
{
pressure_t surface_pressure;
if (dive->surface_pressure.mbar)
@@ -4149,7 +4149,7 @@ int mbar_to_depth(int mbar, struct dive *dive)
}
/* MOD rounded to multiples of roundto mm */
-depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, struct dive *dive, int roundto)
+depth_t gas_mod(struct gasmix mix, pressure_t po2_limit, const struct dive *dive, int roundto)
{
depth_t rounded_depth;
@@ -4159,7 +4159,7 @@ depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit, struct dive *dive, int
}
/* Maximum narcotic depth rounded to multiples of roundto mm */
-depth_t gas_mnd(struct gasmix *mix, depth_t end, struct dive *dive, int roundto)
+depth_t gas_mnd(struct gasmix mix, depth_t end, const struct dive *dive, int roundto)
{
depth_t rounded_depth;
pressure_t ppo2n2;
@@ -4184,14 +4184,14 @@ struct dive *get_dive_from_table(int nr, struct dive_table *dt)
return dt->dives[nr];
}
-struct dive_site *get_dive_site_for_dive(struct dive *dive)
+struct dive_site *get_dive_site_for_dive(const struct dive *dive)
{
if (dive)
return get_dive_site_by_uuid(dive->dive_site_uuid);
return NULL;
}
-const char *get_dive_country(struct dive *dive)
+const char *get_dive_country(const struct dive *dive)
{
struct dive_site *ds = get_dive_site_by_uuid(dive->dive_site_uuid);
if (ds) {
@@ -4210,10 +4210,10 @@ const char *get_dive_location(const struct dive *dive)
return NULL;
}
-unsigned int number_of_computers(struct dive *dive)
+unsigned int number_of_computers(const struct dive *dive)
{
unsigned int total_number = 0;
- struct divecomputer *dc = &dive->dc;
+ const struct divecomputer *dc = &dive->dc;
if (!dive)
return 1;
@@ -4276,31 +4276,44 @@ int get_idx_by_uniq_id(int id)
return i;
}
-bool dive_site_has_gps_location(struct dive_site *ds)
+bool dive_site_has_gps_location(const struct dive_site *ds)
{
return ds && (ds->latitude.udeg || ds->longitude.udeg);
}
-int dive_has_gps_location(struct dive *dive)
+int dive_has_gps_location(const struct dive *dive)
{
if (!dive)
return false;
return dive_site_has_gps_location(get_dive_site_by_uuid(dive->dive_site_uuid));
}
-struct gasmix *get_gasmix(struct dive *dive, struct divecomputer *dc, int time, struct event **evp, struct gasmix *gasmix)
+struct gasmix get_gasmix(const struct dive *dive, const struct divecomputer *dc, int time, const struct event **evp, struct gasmix gasmix)
{
- struct event *ev = *evp;
+ const struct event *ev = *evp;
+ struct gasmix res;
- if (!gasmix) {
+ if (!ev) {
+ /* on first invocation, get initial gas mix and first event (if any) */
int cyl = explicit_first_cylinder(dive, dc);
- gasmix = &dive->cylinder[cyl].gasmix;
+ res = dive->cylinder[cyl].gasmix;
ev = dc ? get_next_event(dc->events, "gaschange") : NULL;
+ } else {
+ res = gasmix;
}
+
while (ev && ev->time.seconds < time) {
- gasmix = get_gasmix_from_event(dive, ev);
+ res = get_gasmix_from_event(dive, ev);
ev = get_next_event(ev->next, "gaschange");
}
*evp = ev;
- return gasmix;
+ return res;
+}
+
+/* get the gas at a certain time during the dive */
+struct gasmix get_gasmix_at_time(const struct dive *d, const struct divecomputer *dc, duration_t time)
+{
+ const struct event *ev = NULL;
+ struct gasmix gasmix = { 0 };
+ return get_gasmix(d, dc, time.seconds, &ev, gasmix);
}