aboutsummaryrefslogtreecommitdiffstats
path: root/core/dive.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-04-24 23:59:59 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-05-11 12:35:11 -0700
commitbab7033ccba8840eb661feed0750e60404d06fc0 (patch)
treecbabc793980bc844e4d36a88234eb46034d1648f /core/dive.c
parentc529cfd361365d3440823efe289a68af6310a983 (diff)
downloadsubsurface-bab7033ccba8840eb661feed0750e60404d06fc0.tar.gz
Dive site: create new dive site at location from GPS data
Some dive computers save GPS data. Currently, this is stored by libdivecomputer in an "extra field". When generating a new dive site for a dive try to use this data to place the dive site. To do so, create a "dive_get_gps_location()" function. This function can be extended later to use e.g. event. When creating a dive site, use the result of this function over a potential pre-existing dive site. Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/dive.c')
-rw-r--r--core/dive.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/core/dive.c b/core/dive.c
index 82a318089..8b676c060 100644
--- a/core/dive.c
+++ b/core/dive.c
@@ -4500,6 +4500,53 @@ int dive_has_gps_location(const struct dive *dive)
return dive_site_has_gps_location(dive->dive_site);
}
+/* Extract GPS location of a dive computer stored in the GPS1
+ * or GPS2 extra data fields */
+static location_t dc_get_gps_location(const struct divecomputer *dc)
+{
+ location_t res = { };
+
+ for (struct extra_data *data = dc->extra_data; data; data = data->next) {
+ if (!strcmp(data->key, "GPS1")) {
+ parse_location(data->value, &res);
+ /* If we found a valid GPS1 field exit early since
+ * it has priority over GPS2 */
+ if (has_location(&res))
+ break;
+ } else if (!strcmp(data->key, "GPS2")) {
+ /* For GPS2 fields continue searching, as we might
+ * still find a GPS1 field */
+ parse_location(data->value, &res);
+ }
+ }
+ return res;
+}
+
+/* Get GPS location for a dive. Highest priority is given to the GPS1
+ * extra data written by libdivecomputer, as this comes from a real GPS
+ * device. If that doesn't exits, use the currently set dive site.
+ * This function is potentially slow, therefore only call sparingly
+ * and remember the result.
+ */
+location_t dive_get_gps_location(const struct dive *d)
+{
+ location_t res = { };
+
+ for (const struct divecomputer *dc = &d->dc; dc; dc = dc->next) {
+ res = dc_get_gps_location(dc);
+ if (has_location(&res))
+ return res;
+ }
+
+ /* No libdivecomputer generated GPS data found.
+ * Let's use the location of the current dive site.
+ */
+ if (d->dive_site)
+ res = d->dive_site->location;
+
+ return res;
+}
+
/* When evaluated at the time of a gasswitch, this returns the new gas */
struct gasmix get_gasmix(const struct dive *dive, const struct divecomputer *dc, int time, const struct event **evp, struct gasmix gasmix)
{