Uemis Patches

Guido Lerch guido.lerch at gmail.com
Thu Sep 10 10:54:29 PDT 2015


I am done with my first round of Uemis patches.
I am new to this so please forgive me if I still don't get this 100%
correct.

Dirk advised me to commit as much as possible but I still struggle to make
a commit
if the commit would not end up in something that is executable. Advise
appreciated.

The patches are ALL required to get a running Uemis download, again happy
to get feedback
on how to structure patches better.

I've tested the code with 2 Uemis computers that are very different in dive
sites and length
of dives as well as the additional information provided by the uemis and
found no problems,
all dives loaded nicely.

There might still be memory leaks and I have to learn more about patching
and GIT in general,
so I would appreciate if you send me your comments and let me fix issues
myself as this helps
me in my learning process. I come from C# and I know that C and C++ guys
might laugh at us
as we have not a lot experience when it comes down to memory handling. I
want to learn that
too, so please give me a chance :-)

Enough bla, bla ...

-- 
Best regards,
Guido
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.subsurface-divelog.org/pipermail/subsurface/attachments/20150910/9d2dc1b9/attachment-0001.html>
-------------- next part --------------
From 5235d3e58f17de60662876dc0b579994a26964de Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Mon, 7 Sep 2015 23:34:52 +0200
Subject: [PATCH 1/8] fixing bug when creating dive site

- using the dive->when when creating a dive site instead of time(NULL) as Dirk suggested

Signed-off-by: glerch <guido.lerch at gmail.com>
---
 uemis-downloader.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/uemis-downloader.c b/uemis-downloader.c
index 8500708..058dbc2 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -892,8 +892,7 @@ static bool process_raw_buffer(device_data_t *devdata, uint32_t deviceid, char *
 			if (for_dive)
 				*for_dive = atoi(val);
 		} else if (!is_log && dive && !strcmp(tag, "divespot_id")) {
-			timestamp_t t;
-			dive->dive_site_uuid = create_dive_site("from Uemis", (int)time(NULL));
+			dive->dive_site_uuid = create_dive_site("from Uemis", dive->when);
 			track_divespot(val, dive->dc.diveid, dive->dive_site_uuid);
 #if UEMIS_DEBUG & 2
 			fprintf(debugfile, "Created divesite %d for diveid : %d\n", dive->dive_site_uuid, dive->dc.diveid);
-- 
1.9.5 (Apple Git-50.3)

-------------- next part --------------
From a10c9b8877a708157f52e335d4009bb9a22d33c5 Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Thu, 10 Sep 2015 16:05:16 +0200
Subject: [PATCH 2/8] Uemis - New Find divespot by diveid

New routine to support finding a dive spot based on the dive id,
this is needed due to the weird way Uemis is providing data.
This function will be used in the uemis-downloader.c within
do_uemis_import.

Signed-off-by: glerch <guido.lerch at gmail.com>
---
 uemis.c | 14 ++++++++++++++
 uemis.h |  2 ++
 2 files changed, 16 insertions(+)

diff --git a/uemis.c b/uemis.c
index e60e94a..0e55a8e 100644
--- a/uemis.c
+++ b/uemis.c
@@ -155,6 +155,20 @@ void uemis_mark_divelocation(int diveid, int divespot, uint32_t dive_site_uuid)
 	hp->dive_site_uuid = dive_site_uuid;
 }
 
+/* glerch Sep 2015
+ * support finding a dive spot based on the diveid
+ */
+int uemis_get_divespot_id_by_diveid(uint32_t diveid)
+{
+	struct uemis_helper *hp = uemis_helper;
+	while(hp) {
+		if (hp->diveid == diveid)
+			return hp->divespot;
+		hp = hp->next;
+	}
+	return 0;
+}
+
 void uemis_set_divelocation(int divespot, char *text, double longitude, double latitude)
 {
 	struct uemis_helper *hp = uemis_helper;
diff --git a/uemis.h b/uemis.h
index d19f293..a836b21 100644
--- a/uemis.h
+++ b/uemis.h
@@ -17,6 +17,8 @@ int uemis_get_weight_unit(int diveid);
 void uemis_mark_divelocation(int diveid, int divespot, uint32_t dive_site_uuid);
 void uemis_set_divelocation(int divespot, char *text, double longitude, double latitude);
 void uemis_set_max_diveid_from_dialog(int diveid);
+// glerch Sep 2015
+int uemis_get_divespot_id_by_diveid(uint32_t diveid);
 
 typedef struct
 {
-- 
1.9.5 (Apple Git-50.3)

-------------- next part --------------
From 74cd9601b28083f3a7d40252e9714296c6c15c06 Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Thu, 10 Sep 2015 16:12:57 +0200
Subject: [PATCH 3/8] Uemis - new routine to load divespots

Added load_uemis_divespot.
This will be used later in do_uemis_import to improve
the amount of divespots that must be loaded actually.

Signed-off-by: glerch <guido.lerch at gmail.com>
---
 uemis-downloader.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/uemis-downloader.c b/uemis-downloader.c
index 058dbc2..a32cc62 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -1032,6 +1032,24 @@ static void do_delete_dives(struct dive_table *td, int idx)
 		td->dives[x]->downloaded = false;
 }
 
+// glerch Sep. 2015
+static bool load_uemis_divespot(const char *mountpath, int divespot_id) {
+	char divespotnr[10];
+	snprintf(divespotnr, sizeof(divespotnr), "%d", divespot_id);
+	param_buff[2] = divespotnr;
+#if UEMIS_DEBUG & 2
+	fprintf(debugfile, "getDivespot %d\n", divespot_id);
+#endif
+	bool success = uemis_get_answer(mountpath, "getDivespot", 3, 0, NULL);
+	if (mbuf && success) {
+#if UEMIS_DEBUG & 2
+		do_dump_buffer_to_file(mbuf, strdup("Spot"), round);
+#endif
+		return parse_divespot(mbuf);
+	}
+	return false;
+}
+
 const char *do_uemis_import(device_data_t *data)
 {
 	const char *mountpath = data->devname;
-- 
1.9.5 (Apple Git-50.3)

-------------- next part --------------
From c24a66457779920f8e2134ebb8135e11a24ea93f Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Thu, 10 Sep 2015 16:14:47 +0200
Subject: [PATCH 4/8] Uemis - deleting code because of design change

Deleting unnecessary code to support future design
change coming with the next patch

Signed-off-by: glerch <guido.lerch at gmail.com>
---
 uemis-downloader.c | 27 ---------------------------
 1 file changed, 27 deletions(-)

diff --git a/uemis-downloader.c b/uemis-downloader.c
index a32cc62..62f9ce4 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -1295,33 +1295,6 @@ const char *do_uemis_import(device_data_t *data)
 		else
 			next_table_index++;
 	}
-
-	switch (uemis_mem_status) {
-	case UEMIS_MEM_CRITICAL:
-	case UEMIS_MEM_OK:
-		for (i = 0; i <= nr_divespots; i++) {
-			char divespotnr[10];
-			snprintf(divespotnr, sizeof(divespotnr), "%d", i);
-			param_buff[2] = divespotnr;
-#if UEMIS_DEBUG & 2
-			fprintf(debugfile, "getDivespot %d of %d, started at %d\n", i, nr_divespots, 0);
-#endif
-			success = uemis_get_answer(mountpath, "getDivespot", 3, 0, &result);
-			if (mbuf && success) {
-#if UEMIS_DEBUG & 2
-				do_dump_buffer_to_file(mbuf, strdup("Spot"), round);
-#endif
-				parse_divespot(mbuf);
-			}
-		}
-		if (uemis_mem_status == UEMIS_MEM_CRITICAL)
-			result = translate("gettextFromC", ERR_FS_ALMOST_FULL);
-		break;
-	case UEMIS_MEM_FULL:
-		result = translate("gettextFromC", ERR_FS_FULL);
-		break;
-	}
-
 bail:
 	(void)uemis_get_answer(mountpath, "terminateSync", 0, 3, &result);
 	if (!strcmp(param_buff[0], "error")) {
-- 
1.9.5 (Apple Git-50.3)

-------------- next part --------------
From 0e4059b0fbebbb2cb63a839fe8eed110c15ada8f Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Thu, 10 Sep 2015 17:29:19 +0200
Subject: [PATCH 5/8] Uemis - new logic for divespots

changed the do_uemis_import to load divespots right after matching
the dive details.

logic implemented to verify that we are not duplicating divespots by
comparing the uuid from get_dive_site_by_uuid(dive->dive_site_uuid)
with the one from get_dive_site_uuid_by_name

Signed-off-by: glerch <guido.lerch at gmail.com>
---
 uemis-downloader.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/uemis-downloader.c b/uemis-downloader.c
index 62f9ce4..58d9bb6 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -892,8 +892,11 @@ static bool process_raw_buffer(device_data_t *devdata, uint32_t deviceid, char *
 			if (for_dive)
 				*for_dive = atoi(val);
 		} else if (!is_log && dive && !strcmp(tag, "divespot_id")) {
-			dive->dive_site_uuid = create_dive_site("from Uemis", dive->when);
-			track_divespot(val, dive->dc.diveid, dive->dive_site_uuid);
+			int divespot_id = atoi(val);
+			if (divespot_id != -1) {
+				dive->dive_site_uuid = create_dive_site("from Uemis", dive->when);
+				uemis_mark_divelocation(dive->dc.diveid, divespot_id, dive->dive_site_uuid);
+			}
 #if UEMIS_DEBUG & 2
 			fprintf(debugfile, "Created divesite %d for diveid : %d\n", dive->dive_site_uuid, dive->dc.diveid);
 #endif
@@ -1202,6 +1205,20 @@ const char *do_uemis_import(device_data_t *data)
 									fprintf(debugfile, "Matching divelog id %d from %s with dive details %d\n", dive->dc.diveid, dTime, iDiveToRead);
 #endif
 									last_found_log_file_nr = dive_to_read;
+									int divespot_id = uemis_get_divespot_id_by_diveid(dive->dc.diveid);
+									if (load_uemis_divespot(mountpath, divespot_id)) {
+										struct dive_site *nds = get_dive_site_by_uuid(dive->dive_site_uuid);
+										struct dive_site *ods = NULL;
+										if (nds) {
+											(void)get_dive_site_uuid_by_name(nds->name, &ods);
+											if (ods) {
+												if (nds->uuid != ods->uuid) {
+													delete_dive_site(nds->uuid);
+													dive->dive_site_uuid = ods->uuid;
+												}
+											}
+										}
+									}
 								} else {
 									/* in this case we found a deleted file, so let's increment */
 #if UEMIS_DEBUG & 2
-- 
1.9.5 (Apple Git-50.3)

-------------- next part --------------
From 9d0a21389cca115a88ff1003eb6d95c08c29bc5f Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Thu, 10 Sep 2015 17:59:03 +0200
Subject: [PATCH 6/8] Uemis - removal of code not needed anymore

Cleaning up track_uemis_divespot because this function is not needed
anymore.

As I am loading the divespots now within do_uemis_import, I also adjusted
the memory calculation - this is not completed yet.

Signed-off-by: glerch <guido.lerch at gmail.com>
---
 uemis-downloader.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)

diff --git a/uemis-downloader.c b/uemis-downloader.c
index 58d9bb6..cb1b3c2 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -59,7 +59,6 @@ static int filenr;
 static int number_of_files;
 static char *mbuf = NULL;
 static int mbuf_size = 0;
-static int nr_divespots = -1;
 
 static int max_mem_used = -1;
 static int next_table_index = 0;
@@ -685,15 +684,6 @@ static bool parse_divespot(char *buf)
 	return true;
 }
 
-static void track_divespot(char *val, int diveid, uint32_t dive_site_uuid)
-{
-	int id = atoi(val);
-	if (id >= 0 && id > nr_divespots)
-		nr_divespots = id;
-	uemis_mark_divelocation(diveid, id, dive_site_uuid);
-	return;
-}
-
 static char *suit[] = {"", QT_TRANSLATE_NOOP("gettextFromC", "wetsuit"), QT_TRANSLATE_NOOP("gettextFromC", "semidry"), QT_TRANSLATE_NOOP("gettextFromC", "drysuit")};
 static char *suit_type[] = {"", QT_TRANSLATE_NOOP("gettextFromC", "shorty"), QT_TRANSLATE_NOOP("gettextFromC", "vest"), QT_TRANSLATE_NOOP("gettextFromC", "long john"), QT_TRANSLATE_NOOP("gettextFromC", "jacket"), QT_TRANSLATE_NOOP("gettextFromC", "full suit"), QT_TRANSLATE_NOOP("gettextFromC", "2 pcs full suit")};
 static char *suit_thickness[] = {"", "0.5-2mm", "2-3mm", "3-5mm", "5-7mm", "8mm+", QT_TRANSLATE_NOOP("gettextFromC", "membrane")};
@@ -1014,13 +1004,7 @@ static int get_memory(struct dive_table *td)
 	/* predict based on the max_mem_used value if the set of next 11 divelogs plus details
 	 * fit into the memory before we have to disconnect the UEMIS and continuem. To be on
 	 * the safe side we calculate using 12 dives. */
-	if (max_mem_used * 11 > UEMIS_MAX_FILES - filenr) {
-		/* the next set of divelogs will most likely not fit into the memory */
-		if (nr_divespots * 2 > UEMIS_MAX_FILES - filenr) {
-			/* if we get here we have a severe issue as the divespots will not fit into
-			 * this run either. */
-			return UEMIS_MEM_FULL;
-		}
+	if (max_mem_used * 10 > UEMIS_MAX_FILES - filenr) {
 		/* we continue reading the divespots */
 		return UEMIS_MEM_CRITICAL;
 	}
-- 
1.9.5 (Apple Git-50.3)

-------------- next part --------------
From 59c92d6700bee580a1c5c105c7df094440f9805d Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Thu, 10 Sep 2015 19:05:29 +0200
Subject: [PATCH 7/8] Uemis - assure empty divespots are not added

If the load_uemis_divespot returns false we must assure
we delete the divespot that was created during process_raw_buffer

Also added some comments

Signed-off-by: glerch <guido.lerch at gmail.com>
---
 uemis-downloader.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/uemis-downloader.c b/uemis-downloader.c
index cb1b3c2..4c29237 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -1191,17 +1191,30 @@ const char *do_uemis_import(device_data_t *data)
 									last_found_log_file_nr = dive_to_read;
 									int divespot_id = uemis_get_divespot_id_by_diveid(dive->dc.diveid);
 									if (load_uemis_divespot(mountpath, divespot_id)) {
+										/* get the divesite based on the diveid, this should give us
+										 * the newly created site
+										 */
 										struct dive_site *nds = get_dive_site_by_uuid(dive->dive_site_uuid);
 										struct dive_site *ods = NULL;
 										if (nds) {
+											/* with the divesite name we got from parse_dive, that is called on load_uemis_divespot
+											 * we search all existing divesites if we have one with the same name already. The function
+											 * returns the first found which is luckily not the newly created.
+											 */
 											(void)get_dive_site_uuid_by_name(nds->name, &ods);
 											if (ods) {
+												/* if the uuid's are the same, the new site is a duplicat and can be deleted */
 												if (nds->uuid != ods->uuid) {
 													delete_dive_site(nds->uuid);
 													dive->dive_site_uuid = ods->uuid;
 												}
 											}
 										}
+									} else {
+										/* if we cant load the dive site details, delete the site we
+										 * created in process_raw_buffer
+										 */
+										delete_dive_site(dive->dive_site_uuid);
 									}
 								} else {
 									/* in this case we found a deleted file, so let's increment */
-- 
1.9.5 (Apple Git-50.3)

-------------- next part --------------
From fb054c86f30b93004e54d991fcbc0462dbd36c6e Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Thu, 10 Sep 2015 19:51:05 +0200
Subject: [PATCH 8/8] Uemis - memory leaks removal

found more memory leaks

Signed-off-by: glerch <guido.lerch at gmail.com>
---
 uemis-downloader.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/uemis-downloader.c b/uemis-downloader.c
index 4c29237..6b51ad2 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -1030,7 +1030,7 @@ static bool load_uemis_divespot(const char *mountpath, int divespot_id) {
 	bool success = uemis_get_answer(mountpath, "getDivespot", 3, 0, NULL);
 	if (mbuf && success) {
 #if UEMIS_DEBUG & 2
-		do_dump_buffer_to_file(mbuf, strdup("Spot"), round);
+		do_dump_buffer_to_file(mbuf, "Spot", round);
 #endif
 		return parse_divespot(mbuf);
 	}
@@ -1096,7 +1096,6 @@ const char *do_uemis_import(device_data_t *data)
 	else
 		newmax = strdup("0");
 
-	// newmax = strdup("240");
 	first = start = atoi(newmax);
 
 #if UEMIS_DEBUG & 2
@@ -1119,7 +1118,7 @@ const char *do_uemis_import(device_data_t *data)
 		uemis_mem_status = get_memory(data->download_table);
 		if (success && mbuf && uemis_mem_status != UEMIS_MEM_FULL) {
 #if UEMIS_DEBUG % 2
-			do_dump_buffer_to_file(mbuf, strdup("Divelogs"), round);
+			do_dump_buffer_to_file(mbuf, "Divelogs", round);
 #endif
 			/* process the buffer we have assembled */
 
@@ -1165,7 +1164,7 @@ const char *do_uemis_import(device_data_t *data)
 					param_buff[2] = dive_to_read_buf;
 					success = uemis_get_answer(mountpath, "getDive", 3, 0, &result);
 #if UEMIS_DEBUG % 2
-					do_dump_buffer_to_file(mbuf, strdup("Dive"), round);
+					do_dump_buffer_to_file(mbuf, "Dive", round);
 #endif
 					uemis_mem_status = get_memory(data->download_table);
 					if (uemis_mem_status == UEMIS_MEM_OK || uemis_mem_status == UEMIS_MEM_CRITICAL) {
-- 
1.9.5 (Apple Git-50.3)



More information about the subsurface mailing list