Uemis patches

Guido Lerch guido.lerch at gmail.com
Mon Sep 14 13:09:23 PDT 2015


One bug fix,
2 code simplifications that done have real algorithm changes.

-- 
Best regards,
Guido
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.subsurface-divelog.org/pipermail/subsurface/attachments/20150914/e4d55f59/attachment.html>
-------------- next part --------------
From 75eedcfa1b3f3e57cf667661ca08bf6e52349c1b Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Mon, 14 Sep 2015 10:31:29 +0200
Subject: [PATCH 3/5] Uemis bugfix

Fixed a bug in do_uemis_download when cleaning up delted dives. My
test for valid dives was wrong. now counting the nr of dives in the
download table.

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

diff --git a/uemis-downloader.c b/uemis-downloader.c
index 9db2f33..7e7c4fc 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -10,7 +10,7 @@
  * Zurich) but did not actually use any of his copyrighted code, therefore the license under which
  * he released his code does not apply to this new implementation in C
  *
- * Modified by Guido Lerch guido.lerch at gmx.ch in August 2015
+ * Modified by Guido Lerch guido.lerch at gmail.com in August 2015
  */
 #include <fcntl.h>
 #include <dirent.h>
@@ -1301,7 +1301,7 @@ const char *do_uemis_import(device_data_t *data)
 	/* Regardless on where we are with the memory situation, it's time now
 	 * to see if we have to clean some dead bodies from our download table */
 	next_table_index = 0;
-	while (data->download_table->dives[next_table_index]) {
+	while (next_table_index < data->download_table->nr) {
 		if (!data->download_table->dives[next_table_index]->downloaded)
 			uemis_delete_dive(data, data->download_table->dives[next_table_index]->dc.diveid);
 		else
-- 
1.9.5 (Apple Git-50.3)

-------------- next part --------------
From 0f18babe5874f4d9252439c8f109bc3b82e00a4a Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Mon, 14 Sep 2015 10:42:00 +0200
Subject: [PATCH 4/5] Uemis code simplification 1

Reducing the identination within the do_uemis_import.

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

diff --git a/uemis-downloader.c b/uemis-downloader.c
index 7e7c4fc..063f4f6 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -1037,6 +1037,36 @@ static bool load_uemis_divespot(const char *mountpath, int divespot_id)
 	return false;
 }
 
+static void get_uemis_divespot(const char *mountpath, int divespot_id, struct dive *dive)
+{
+	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);
+	}
+}
+
 const char *do_uemis_import(device_data_t *data)
 {
 	const char *mountpath = data->devname;
@@ -1188,32 +1218,8 @@ const char *do_uemis_import(device_data_t *data)
 #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)) {
-										/* 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);
-									}
+									(void)get_uemis_divespot(mountpath, divespot_id, dive);
+
 								} 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 111e06a15a41b121e4def2c2e0bd9a4883682d35 Mon Sep 17 00:00:00 2001
From: glerch <guido.lerch at gmail.com>
Date: Mon, 14 Sep 2015 22:04:10 +0200
Subject: [PATCH 5/5] Uemis - code simplification 2

Reducing code depth, not done yet as I'd like to
simplyfy the new helper further.

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

diff --git a/uemis-downloader.c b/uemis-downloader.c
index 063f4f6..2f88c97 100644
--- a/uemis-downloader.c
+++ b/uemis-downloader.c
@@ -1067,6 +1067,87 @@ static void get_uemis_divespot(const char *mountpath, int divespot_id, struct di
 	}
 }
 
+static bool get_matching_dive(int idx, int *dive_to_read, int *last_found_log_file_nr, int *deleted_files, char *newmax, int *uemis_mem_status, struct device_data_t *data, const char* mountpath, const char deviceidnr)
+{
+	struct dive *dive = data->download_table->dives[idx];
+	const char *dTime = get_dive_date_c_string(dive->when);
+	char log_file_no_to_find[20];
+	char dive_to_read_buf[10];
+	bool found = false;
+
+	snprintf(log_file_no_to_find, sizeof(log_file_no_to_find), "logfilenr{int{%d", dive->dc.diveid);
+	while (!found) {
+		snprintf(dive_to_read_buf, sizeof(dive_to_read_buf), "%d", *dive_to_read);
+		param_buff[2] = dive_to_read_buf;
+		(void)uemis_get_answer(mountpath, "getDive", 3, 0, NULL);
+#if UEMIS_DEBUG & 16
+		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) {
+			/* if the memory isn's completely full we can try to read more divelog vs. dive details
+			 * UEMIS_MEM_CRITICAL means not enough space for a full round but the dive details
+			 * and the divespots should fit into the UEMIS memory
+			 * The match we do here is to map the object_id to the logfilenr, we do this
+			 * by iterating through the last set of loaded divelogs and then find the corresponding
+			 * dive with the matching logfilenr */
+			if (mbuf) {
+				if (strstr(mbuf, log_file_no_to_find)) {
+					/* we found the logfilenr that matches our object_id from the divelog we were looking for
+					 * we mark the search sucessfull even if the dive has been deleted. */
+					found = true;
+					if (strstr(mbuf, "deleted{bool{true") == NULL) {
+						process_raw_buffer(data, deviceidnr, mbuf, &newmax, false, NULL);
+						/* remember the last log file number as it is very likely that subsequent dives
+						 * have the same or higher logfile number.
+						 * UEMIS unfortunately deletes dives by deleting the dive details and not the logs. */
+#if UEMIS_DEBUG & 2
+						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);
+						(void)get_uemis_divespot(mountpath, divespot_id, dive);
+
+					} else {
+						/* in this case we found a deleted file, so let's increment */
+#if UEMIS_DEBUG & 2
+						fprintf(debugfile, "TRY matching divelog id %d from %s with dive details %d but details are deleted\n", dive->dc.diveid, dTime, iDiveToRead);
+#endif
+						deleted_files++;
+						/* mark this log entry as deleted and cleanup later, otherwise we mess up our array */
+						dive->downloaded = false;
+#if UEMIS_DEBUG & 2
+						fprintf(debugfile, "Deleted dive from %s, with id %d from table\n", dTime, dive->dc.diveid);
+#endif
+					}
+					return true;
+				} else {
+					/* Ugly, need something better than this
+					 * essentially, if we start reading divelogs not from the start
+					 * we have no idea on how many log entries are there that have no
+					 * valid dive details */
+					if (*dive_to_read >= dive->dc.diveid)
+						*dive_to_read = (*dive_to_read - 2 >= 0 ? *dive_to_read - 2 : 0);
+				}
+			}
+			*dive_to_read = *dive_to_read + 1;
+		} else {
+			/* At this point the memory of the UEMIS is full, let's cleanup all divelog files were
+			 * we could not match the details to. */
+			do_delete_dives(data->download_table, idx);
+			return false;
+		}
+	}
+	/* decrement iDiveToRead by the amount of deleted entries found to assure
+	 * we are not missing any valid matches when processing subsequent logs */
+	*dive_to_read = (dive_to_read - deleted_files > 0 ? dive_to_read - deleted_files : 0);
+	deleted_files = 0;
+	if (*uemis_mem_status == UEMIS_MEM_FULL)
+		/* game over, not enough memory left */
+		return false;
+	return true;
+}
+
 const char *do_uemis_import(device_data_t *data)
 {
 	const char *mountpath = data->devname;
@@ -1074,23 +1155,16 @@ const char *do_uemis_import(device_data_t *data)
 	char *newmax = NULL;
 	int first, start, end = -2;
 	uint32_t deviceidnr;
-	//char objectid[10];
 	char *deviceid = NULL;
 	const char *result = NULL;
 	char *endptr;
 	bool success, keep_number = false, once = true;
-	char dive_to_read_buf[10];
-	char log_file_no_to_find[20];
 	int deleted_files = 0;
 	int last_found_log_file_nr = 0;
 	int match_dive_and_log = 0;
 	int start_cleanup = 0;
-	struct dive_table *td = NULL;
-	struct dive *dive = NULL;
 	int uemis_mem_status = UEMIS_MEM_OK;
 
-	const char *dTime;
-
 	if (dive_table.nr == 0)
 		keep_number = true;
 
@@ -1180,81 +1254,9 @@ const char *do_uemis_import(device_data_t *data)
 			 * dive_to_read = the dive deatils entry that need to be read using the object_id
 			 * logFileNoToFind = map the logfilenr of the dive details with the object_id = diveid from the get dive logs */
 			int dive_to_read = (last_found_log_file_nr > 0 ? last_found_log_file_nr + 1 : start);
-			td = data->download_table;
-
-			for (int i = match_dive_and_log; i < td->nr; i++) {
-				dive = td->dives[i];
-				dTime = get_dive_date_c_string(dive->when);
-				snprintf(log_file_no_to_find, sizeof(log_file_no_to_find), "logfilenr{int{%d", dive->dc.diveid);
-
-				bool found = false;
-				while (!found) {
-					snprintf(dive_to_read_buf, sizeof(dive_to_read_buf), "%d", dive_to_read);
-					param_buff[2] = dive_to_read_buf;
-					success = uemis_get_answer(mountpath, "getDive", 3, 0, &result);
-#if UEMIS_DEBUG & 16
-					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) {
-						/* if the memory isn's completely full we can try to read more divelog vs. dive details
-						 * UEMIS_MEM_CRITICAL means not enough space for a full round but the dive details
-						 * and the divespots should fit into the UEMIS memory
-						 * The match we do here is to map the object_id to the logfilenr, we do this
-						 * by iterating through the last set of loaded divelogs and then find the corresponding
-						 * dive with the matching logfilenr */
-						if (mbuf) {
-							if (strstr(mbuf, log_file_no_to_find)) {
-								/* we found the logfilenr that matches our object_id from the divelog we were looking for
-								 * we mark the search sucessfull even if the dive has been deleted. */
-								found = true;
-								if (strstr(mbuf, "deleted{bool{true") == NULL) {
-									process_raw_buffer(data, deviceidnr, mbuf, &newmax, false, NULL);
-									/* remember the last log file number as it is very likely that subsequent dives
-									 * have the same or higher logfile number.
-									 * UEMIS unfortunately deletes dives by deleting the dive details and not the logs. */
-#if UEMIS_DEBUG & 2
-									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);
-									(void)get_uemis_divespot(mountpath, divespot_id, dive);
-
-								} else {
-									/* in this case we found a deleted file, so let's increment */
-#if UEMIS_DEBUG & 2
-									fprintf(debugfile, "TRY matching divelog id %d from %s with dive details %d but details are deleted\n", dive->dc.diveid, dTime, iDiveToRead);
-#endif
-									deleted_files++;
-									/* mark this log entry as deleted and cleanup later, otherwise we mess up our array */
-									dive->downloaded = false;
-#if UEMIS_DEBUG & 2
-									fprintf(debugfile, "Deleted dive from %s, with id %d from table\n", dTime, dive->dc.diveid);
-#endif
-								}
-							} else {
-								/* Ugly, need something better than this
-								 * essentially, if we start reading divelogs not from the start
-								 * we have no idea on how many log entries are there that have no
-								 * valid dive details */
-								if (dive_to_read >= dive->dc.diveid)
-									dive_to_read = (dive_to_read - 2 >= 0 ? dive_to_read - 2 : 0);
-							}
-						}
-						dive_to_read++;
-					} else {
-						/* At this point the memory of the UEMIS is full, let's cleanup all divelog files were
-						 * we could not match the details to. */
-						do_delete_dives(td, i);
-						break;
-					}
-				}
-				/* decrement iDiveToRead by the amount of deleted entries found to assure
-				 * we are not missing any valid matches when processing subsequent logs */
-				dive_to_read = (dive_to_read - deleted_files > 0 ? dive_to_read - deleted_files : 0);
-				deleted_files = 0;
-				if (uemis_mem_status == UEMIS_MEM_FULL)
-					/* game over, not enough memory left */
+			for (int i = match_dive_and_log; i < data->download_table->nr; i++) {
+				bool success  = get_matching_dive(i, &dive_to_read, &last_found_log_file_nr, &deleted_files, newmax, &uemis_mem_status, data, mountpath, deviceidnr);
+				if (!success)
 					break;
 			}
 
-- 
1.9.5 (Apple Git-50.3)



More information about the subsurface mailing list