From 640809b73bb67d76b1a3fb56f0de7fa3e62996ae Mon Sep 17 00:00:00 2001
From: Linus Torvalds <torvalds@linux-foundation.org>
Date: Mon, 27 Oct 2014 17:19:48 -0700
Subject: [PATCH 3/3] Suunto EON Steel: populate dive surface pressure, and
 sw/hw version info

The EON Steel saves the dive computer firmware versions etc per dive,
which is really nice for upgrades: old dives done with older firmware
still show the firmware at the time of the dive.  That, in turn, is nice
because we can use it for a reliable dive ID - dive time with serial
number etc.

This uses the new DC_FIELD_DEVINFO model to feed the hw/sw information
to the application, since we need the parser to access it.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
---
 src/suunto_eon_steel_parser.c | 132 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 131 insertions(+), 1 deletion(-)

diff --git a/src/suunto_eon_steel_parser.c b/src/suunto_eon_steel_parser.c
index ef77fec3b947..3f32ef162adf 100644
--- a/src/suunto_eon_steel_parser.c
+++ b/src/suunto_eon_steel_parser.c
@@ -55,6 +55,7 @@ struct eon_parser {
 		dc_gasmix_t gasmix[MAXGASES];
 		dc_salinity_t salinity;
 		double surface_pressure;
+		struct dc_field_devinfo_t devinfo;
 	} cache;
 };
 
@@ -76,6 +77,17 @@ static unsigned char get_u8(const void *src)
 	return *(const unsigned char *)src;
 }
 
+static float get_le32_float(const void *src)
+{
+	union {
+		unsigned int val;
+		float result;
+	} u;
+
+	u.val = array_uint32_le(src);
+	return u.result;
+}
+
 static void debug(const char *name, const char *buf, int len)
 {
 	int i;
@@ -363,7 +375,6 @@ static dc_status_t eon_steel_parser_samples_foreach(dc_parser_t *abstract, dc_sa
 {
 	struct eon_parser *eon = (void *)abstract;
 	struct sample_data data = { eon, callback, userdata, 0 };
-
 	traverse_data(eon, traverse_samples, &data);
 	return DC_STATUS_SUCCESS;
 }
@@ -404,6 +415,9 @@ static dc_status_t eon_steel_parser_get_field(dc_parser_t *parser, dc_field_type
 	case DC_FIELD_ATMOSPHERIC:
 		field_value(value, eon->cache.surface_pressure);
 		break;
+	case DC_FIELD_DEVINFO:
+		field_value(value, eon->cache.devinfo);
+		break;
 	}
 	return DC_STATUS_SUCCESS;
 }
@@ -467,6 +481,115 @@ static void add_gas_he(struct eon_parser *eon, unsigned char he)
 	eon->cache.initialized |= 1 << DC_FIELD_GASMIX;
 }
 
+// "Device" fields are all utf8:
+//   Info.BatteryAtEnd
+//   Info.BatteryAtStart
+//   Info.BSL
+//   Info.HW
+//   Info.SW
+//   Name
+//   SerialNumber
+static int traverse_device_fields(struct eon_parser *eon, const char *name, const void *data, int len)
+{
+	const char **entry;
+
+	if (!strcmp(name, "SerialNumber"))
+		entry = &eon->cache.devinfo.serial;
+	else if (!strcmp(name, "Info.HW"))
+		entry = &eon->cache.devinfo.hw_version;
+	else if (!strcmp(name, "Info.SW"))
+		entry = &eon->cache.devinfo.fw_version;
+	else if (!strcmp(name, "Name"))
+		entry = &eon->cache.devinfo.model;
+	else
+		return 0;
+
+	if (*entry)
+		free((void *)*entry);
+	*entry = strdup(data);
+	eon->cache.initialized |= 1 << DC_FIELD_DEVINFO;
+	return 0;
+}
+
+// "Header" fields are:
+//   Activity (utf8)
+//   DateTime (utf8)
+//   Depth.Avg (float32,precision=2)
+//   Depth.Max (float32,precision=2)
+//   Diving.AlgorithmAscentTime (uint32)
+//   Diving.AlgorithmBottomMixture.Helium (uint8,precision=2) (0.01*x,100*x)
+//   Diving.AlgorithmBottomMixture.Oxygen (uint8,precision=2) (0.01*x,100*x)
+//   Diving.AlgorithmBottomTime (uint32)
+//   Diving.AlgorithmTransitionDepth (uint8)
+//   Diving.Algorithm (utf8)
+//   Diving.Altitude (uint16)
+//   Diving.Conservatism (int8)
+//   Diving.DaysInSeries (uint32)
+//   Diving.DesaturationTime (uint32)
+//   Diving.DiveMode (utf8)
+//   Diving.EndTissue.CNS (float32,precision=3)
+//   Diving.EndTissue.Helium+Pressure (uint32)
+//   Diving.EndTissue.Nitrogen+Pressure (uint32)
+//   Diving.EndTissue.OLF (float32,precision=3)
+//   Diving.EndTissue.OTU (float32)
+//   Diving.EndTissue.RgbmHelium (float32,precision=3)
+//   Diving.EndTissue.RgbmNitrogen (float32,precision=3)
+//   Diving.NumberInSeries (uint32)
+//   Diving.PreviousDiveDepth (float32,precision=2)
+//   Diving.StartTissue.CNS (float32,precision=3)
+//   Diving.StartTissue.Helium+Pressure (uint32)
+//   Diving.StartTissue.Nitrogen+Pressure (uint32)
+//   Diving.StartTissue.OLF (float32,precision=3)
+//   Diving.StartTissue.OTU (float32)
+//   Diving.StartTissue.RgbmHelium (float32,precision=3)
+//   Diving.StartTissue.RgbmNitrogen (float32,precision=3)
+//   Diving.SurfacePressure (uint32)
+//   Diving.SurfaceTime (uint32)
+//   Duration (uint32)
+//   PauseDuration (uint32)
+//   SampleInterval (uint8)
+static int traverse_header_fields(struct eon_parser *eon, const char *name, const void *data, int len)
+{
+	if (!strcmp(name, "Depth.Max")) {
+		double d = get_le32_float(data);
+		if (d > eon->cache.maxdepth)
+			eon->cache.maxdepth = d;
+		return 0;
+	}
+	if (!strcmp(name, "Diving.SurfacePressure")) {
+		unsigned int pressure = array_uint32_le(data); // in SI units - Pascal
+		eon->cache.surface_pressure = pressure / 100000.0; // bar
+		eon->cache.initialized |= 1 << DC_FIELD_ATMOSPHERIC;
+		return 0;
+	}
+	if (!strcmp(name, "DateTime")) {
+		if (eon->cache.devinfo.dive_id)
+			free((void *)eon->cache.devinfo.dive_id);
+		eon->cache.devinfo.dive_id = strdup(data);
+		eon->cache.initialized |= 1 << DC_FIELD_DEVINFO;
+		return 0;
+	}
+	return 0;
+}
+
+static int traverse_dynamic_fields(struct eon_parser *eon, const struct type_desc *desc, const void *data, int len)
+{
+	const char *name = desc->desc;
+
+	if (!strncmp(name, "sml.", 4)) {
+		name += 4;
+		if (!strncmp(name, "DeviceLog.", 10)) {
+			name += 10;
+			if (!strncmp(name, "Device.", 7))
+				return traverse_device_fields(eon, name+7, data, len);
+			if (!strncmp(name, "Header.", 7)) {
+				return traverse_header_fields(eon, name+7, data, len);
+			}
+		}
+	}
+	return 0;
+}
+
 static int traverse_fields(unsigned short type, const struct type_desc *desc, const void *data, int len, void *user)
 {
 	struct eon_parser *eon = user;
@@ -491,6 +614,13 @@ static int traverse_fields(unsigned short type, const struct type_desc *desc, co
 	case 0x000f: // Helium percentage in first byte
 		add_gas_he(eon, get_u8(data));
 		break;
+	default:
+	// The types with the high byte set seem to be dynamic
+	// although not all of them seem to change. But let's
+	// just check the descriptor name for them.
+		if (type > 255)
+			traverse_dynamic_fields(eon, desc, data, len);
+		break;
 	}
 	return 0;
 }
-- 
2.1.2.452.gd5ca7ae

