[PATCH] Limited support for Suunto DM4 import

Miika Turkia miika.turkia at gmail.com
Sun Mar 3 05:57:29 PST 2013


Basic functionality is implemented but at least support for multiple
cylinders and events is currently missing.

Signed-off-by: Miika Turkia <miika.turkia at gmail.com>
---
 Makefile    |   10 ++++-
 dive.h      |    2 +
 file.c      |   20 +++++++++
 gtk-gui.c   |    5 +++
 parse-xml.c |  130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 165 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile
index 71e19f0..65932b5 100644
--- a/Makefile
+++ b/Makefile
@@ -6,6 +6,7 @@ INSTALL=install
 PKGCONFIG=pkg-config
 XML2CONFIG=xml2-config
 XSLCONFIG=xslt-config
+SQLITECONFIG=pkg-config
 
 # these locations seem to work for SuSE and Fedora
 # prefix = $(HOME)
@@ -120,6 +121,11 @@ ifneq ($(strip $(LIBZIP)),)
 	ZIP = -DLIBZIP $(shell $(PKGCONFIG) --cflags libzip)
 endif
 
+LIBSQLITE3 = $(shell $(PKGCONFIG) --libs sqlite3 2> /dev/null)
+ifneq ($(strip $(LIBSQLITE3)),)
+	SQLITE3 = -DSQLITE3 $(shell $(PKGCONFIG) --cflags sqlite3)
+endif
+
 ifeq ($(UNAME), linux)
 	LIBGCONF2 = $(shell $(PKGCONFIG) --libs gconf-2.0)
 	GCONF2CFLAGS =  $(shell $(PKGCONFIG) --cflags gconf-2.0)
@@ -152,7 +158,7 @@ ifneq ($(strip $(LIBXSLT)),)
 	XSLT=-DXSLT='"$(XSLTDIR)"'
 endif
 
-LIBS = $(LIBXML2) $(LIBXSLT) $(LIBGTK) $(LIBGCONF2) $(LIBDIVECOMPUTER) $(EXTRALIBS) $(LIBZIP) -lpthread -lm $(LIBOSMGPSMAP) $(LIBSOUP) $(LIBWINSOCK)
+LIBS = $(LIBXML2) $(LIBXSLT) $(LIBSQLITE3) $(LIBGTK) $(LIBGCONF2) $(LIBDIVECOMPUTER) $(EXTRALIBS) $(LIBZIP) -lpthread -lm $(LIBOSMGPSMAP) $(LIBSOUP) $(LIBWINSOCK)
 
 MSGLANGS=$(notdir $(wildcard po/*po))
 MSGOBJS=$(addprefix share/locale/,$(MSGLANGS:.po=.UTF-8/LC_MESSAGES/subsurface.mo))
@@ -266,7 +272,7 @@ update-po-files:
 	tx pull -af
 
 EXTRA_FLAGS =	$(GTKCFLAGS) $(GLIB2CFLAGS) $(XML2CFLAGS) \
-		$(XSLT) $(ZIP) $(LIBDIVECOMPUTERCFLAGS) \
+		$(XSLT) $(ZIP) $(SQLITE3) $(LIBDIVECOMPUTERCFLAGS) \
 		$(LIBSOUPCFLAGS) $(OSMGPSMAPFLAGS) $(GCONF2CFLAGS)
 
 %.o: %.c
diff --git a/dive.h b/dive.h
index 7e00a6c..bfcf0dc 100644
--- a/dive.h
+++ b/dive.h
@@ -525,6 +525,8 @@ extern void parse_xml_buffer(const char *url, const char *buf, int size, struct
 extern void parse_xml_exit(void);
 extern void set_filename(const char *filename, gboolean force);
 
+extern int parse_sde_buffer(const char *url, const char *buf, int size, struct dive_table *table, GError **error);
+
 extern void parse_file(const char *filename, GError **error, gboolean possible_default_filename);
 
 extern void show_dive_info(struct dive *);
diff --git a/file.c b/file.c
index 8f8fb90..0fb59c2 100644
--- a/file.c
+++ b/file.c
@@ -100,6 +100,13 @@ static int try_to_open_zip(const char *filename, struct memblock *mem, GError **
 	return success;
 }
 
+#ifdef SQLITE3
+static int try_to_open_db(const char *filename, struct memblock *mem, GError **error)
+{
+	return parse_sde_buffer(filename, mem->buffer, mem->size, &dive_table, error);
+}
+#endif
+
 static timestamp_t parse_date(const char *date)
 {
 	int hour, min, sec;
@@ -258,6 +265,9 @@ static void parse_file_buffer(const char *filename, struct memblock *mem, GError
 void parse_file(const char *filename, GError **error, gboolean possible_default_filename)
 {
 	struct memblock mem;
+#ifdef SQLITE3
+	char *fmt;
+#endif
 
 	if (readfile(filename, &mem) < 0) {
 		/* we don't want to display an error if this was the default file */
@@ -285,6 +295,16 @@ void parse_file(const char *filename, GError **error, gboolean possible_default_
 	if (possible_default_filename)
 		set_filename(filename, TRUE);
 
+#ifdef SQLITE3
+	fmt = strrchr(filename, '.');
+	if (fmt && !strcasecmp(fmt + 1, "DB")) {
+		if (!try_to_open_db(filename, &mem, error)) {
+			free(mem.buffer);
+			return;
+		}
+	}
+#endif
+
 	parse_file_buffer(filename, &mem, error);
 	free(mem.buffer);
 }
diff --git a/gtk-gui.c b/gtk-gui.c
index 5397903..61286ec 100644
--- a/gtk-gui.c
+++ b/gtk-gui.c
@@ -135,6 +135,11 @@ static GtkFileFilter *setup_filter(void)
 	gtk_file_filter_add_pattern(filter, "*.dld");
 	gtk_file_filter_add_pattern(filter, "*.DLD");
 #endif
+#ifdef SQLITE3
+	gtk_file_filter_add_pattern(filter, "*.DB");
+	gtk_file_filter_add_pattern(filter, "*.db");
+#endif
+
 	gtk_file_filter_add_mime_type(filter, "text/xml");
 	gtk_file_filter_set_name(filter, _("XML file"));
 
diff --git a/parse-xml.c b/parse-xml.c
index b1bb2ef..0ebbce5 100644
--- a/parse-xml.c
+++ b/parse-xml.c
@@ -14,6 +14,10 @@
 #endif
 #include <glib/gi18n.h>
 
+#ifdef SQLITE3
+#include<sqlite3.h>
+#endif
+
 #include "dive.h"
 #include "device.h"
 
@@ -1532,6 +1536,132 @@ void parse_xml_buffer(const char *url, const char *buffer, int size,
 	xmlFreeDoc(doc);
 }
 
+#ifdef SQLITE3
+int sde_dive(void *a_param, int argc, char **argv, char **column)
+{
+	int i;
+	float *profileBlob;
+	unsigned char *tempBlob;
+	int *pressureBlob;
+	time_t when;
+	struct tm *tm;
+	int interval;
+
+	dive_start();
+	cur_dive->number = atoi(argv[0]);
+
+	/* Suunto saves time in 100 nano seconds, we'll need the time in
+	 * seconds.
+	 */
+	when = (time_t)(atol(argv[1]) / 10000000);
+	tm = localtime(&when);
+
+	/* Suunto starts counting time in year 1, we need epoch */
+	tm->tm_year -= 1969;
+	cur_dive->when = mktime(tm);
+	if (argv[2])
+		utf8_string(argv[2], &cur_dive->notes);
+
+	/*
+	 * DM4 stores Duration and DiveTime. It looks like DiveTime is
+	 * 10 to 60 seconds shorter than Duration. However, I have no
+	 * idea what is the difference and which one should be used.
+	 */
+	if (argv[3])
+		cur_dive->duration.seconds = atoi(argv[3]);
+
+	/*
+	 * TODO: the deviceid hash should be calculated here.
+	 */
+	settings_start();
+	dc_settings_start();
+	if (argv[4])
+		utf8_string(argv[4], &cur_settings.dc.serial_nr);
+	if (argv[5])
+		utf8_string(argv[5], &cur_settings.dc.model);
+
+	cur_settings.dc.deviceid = 0xffffffff;
+	dc_settings_end();
+	settings_end();
+
+	if (argv[6])
+		cur_dive->maxdepth.mm = atof(argv[6]) * 1000;
+	if (argv[8])
+		cur_dive->airtemp.mkelvin = (atoi(argv[8]) + 273.15) * 1000;
+	if (argv[9])
+		cur_dive->watertemp.mkelvin  = (atoi(argv[9]) + 273.15) * 1000;
+
+	/*
+	 * TODO: handle multiple cylinders
+	 */
+	cylinder_start();
+	if (argv[10])
+		cur_dive->cylinder[cur_cylinder_index].start.mbar = (atoi(argv[10]));
+	if (argv[11])
+		cur_dive->cylinder[cur_cylinder_index].end.mbar = (atoi(argv[11]));
+	if (argv[12])
+		cur_dive->cylinder[cur_cylinder_index].type.size.mliter = (atof(argv[12])) * 1000;
+	if (argv[13])
+		cur_dive->cylinder[cur_cylinder_index].type.workingpressure.mbar = (atoi(argv[13]));
+	if (argv[20])
+		cur_dive->cylinder[cur_cylinder_index].gasmix.o2.permille = atoi(argv[20]) * 10;
+	if (argv[21])
+		cur_dive->cylinder[cur_cylinder_index].gasmix.he.permille = atoi(argv[21]) * 10;
+	cylinder_end();
+
+	if (argv[14])
+		cur_dive->surface_pressure.mbar = (atoi(argv[14]) * 1000);
+
+	interval = argv[16] ? atoi(argv[16]) : 0;
+	profileBlob = (float *)argv[17];
+	tempBlob = (unsigned char *)argv[18];
+	pressureBlob = (int *)argv[19];
+	for (i=0; interval && i * interval < cur_dive->duration.seconds; i++) {
+		sample_start();
+		cur_sample->time.seconds = i * interval;
+		cur_sample->depth.mm = profileBlob[i] * 1000;
+		if (tempBlob[i])
+			cur_sample->temperature.mkelvin = (tempBlob[i] + 273.15) * 1000;
+		cur_sample->cylinderpressure.mbar = pressureBlob[i] ;
+		sample_end();
+	}
+
+	dive_end();
+	return SQLITE_OK;
+}
+#endif
+
+int parse_sde_buffer(const char *url, const char *buffer, int size,
+			struct dive_table *table, GError **error)
+{
+#ifdef SQLITE3
+	int retval;
+	char *err = NULL;
+	sqlite3 *handle;
+	target_table = table;
+
+	char get_dives[] = "select D.DiveId,StartTime,Note,Duration,SourceSerialNumber,Source,MaxDepth,SampleInterval,StartTemperature,BottomTemperature,D.StartPressure,D.EndPressure,CylinderVolume,CylinderWorkPressure,SurfacePressure,DiveTime,SampleInterval,ProfileBlob,TemperatureBlob,PressureBlob,Oxygen,Helium FROM Dive AS D JOIN DiveMixture AS MIX ON D.DiveId=MIX.DiveId";
+
+	retval = sqlite3_open(url,&handle);
+
+	if(retval) {
+		fprintf(stderr, _("Database connection failed '%s'.\n"), url);
+		return 1;
+	}
+
+	retval = sqlite3_exec(handle, get_dives, &sde_dive, 0, &err);
+
+	if (retval != SQLITE_OK) {
+		fprintf(stderr, _("Database query failed '%s'.\n"), url);
+		perror("asdf");
+		return 1;
+	}
+
+	sqlite3_close(handle);
+#endif
+	return 0;
+}
+
 void parse_xml_init(void)
 {
 	LIBXML_TEST_VERSION
-- 
1.7.9.5



More information about the subsurface mailing list