[PATCH] Fill in divecomputer serial number and firmware version on loading

Linus Torvalds torvalds at linux-foundation.org
Sun Aug 14 09:49:19 PDT 2016


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Mon, 20 Jun 2016 21:07:36 -0700
Subject: [PATCH] Fill in divecomputer serial number and firmware version on loading

We have the serial number and firmware version fields in "struct
divecomputer", but we don't actually fill them in when loading the data
from git or xml, because we save all that information in the separate
device table instead.

But in order to always have the serial number associated with a device,
let's make sure to fill those fields in.  It won't hurt, and this way we
have the information available whether we just loaded the dive from a
file, or imported it from the dive computer.  One less semantic
difference to worry about.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

I've had this patch in my tree for a while - it doesn't actually matter 
the way things are now, but it fixes an internal inconsistency that 
confused me some time ago.

Apply or not, I'm just sending this because I just sent a _real_ patch to 
fix an actual real bug, and I had this hanging around.

 core/device.c    | 28 ++++++++++++++++++++++++++++
 core/device.h    |  1 +
 core/load-git.c  |  2 +-
 core/parse-xml.c |  6 +++++-
 4 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/core/device.c b/core/device.c
index 6c4452f7813b..afd52b8ef19b 100644
--- a/core/device.c
+++ b/core/device.c
@@ -182,3 +182,31 @@ struct divecomputer *fake_dc(struct divecomputer *dc, bool alloc)
 	/* Even that didn't work? Give up, there's something wrong */
 	return &fakedc;
 }
+
+static void match_id(void *_dc, const char *model, uint32_t deviceid,
+		     const char *nickname, const char *serial, const char *firmware)
+{
+	struct divecomputer *dc = _dc;
+
+	if (dc->deviceid != deviceid)
+		return;
+	if (strcmp(dc->model, model))
+		return;
+
+	if (serial && !dc->serial)
+		dc->serial = strdup(serial);
+	if (firmware && !dc->fw_version)
+		dc->fw_version = strdup(firmware);
+}
+
+/*
+ * When setting the device ID, we also fill in the
+ * serial number and firmware version data
+ */
+void set_dc_deviceid(struct divecomputer *dc, unsigned int deviceid)
+{
+	if (deviceid) {
+		dc->deviceid = deviceid;
+		call_for_each_dc(dc, match_id, false);
+	}
+}
diff --git a/core/device.h b/core/device.h
index 8a00b96d31cb..264ea8db5dfc 100644
--- a/core/device.h
+++ b/core/device.h
@@ -7,6 +7,7 @@ extern "C" {
 #endif
 
 extern struct divecomputer *fake_dc(struct divecomputer *dc, bool alloc);
+extern void set_dc_deviceid(struct divecomputer *dc, unsigned int deviceid);
 extern void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname);
 extern void call_for_each_dc(void *f, void (*callback)(void *, const char *, uint32_t,
 						       const char *, const char *, const char *), bool select_only);
diff --git a/core/load-git.c b/core/load-git.c
index 0fe2c6353a87..670658d1b50b 100644
--- a/core/load-git.c
+++ b/core/load-git.c
@@ -643,7 +643,7 @@ static void parse_dc_date(char *line, struct membuffer *str, void *_dc)
 { (void) str; struct divecomputer *dc = _dc; update_date(&dc->when, line); }
 
 static void parse_dc_deviceid(char *line, struct membuffer *str, void *_dc)
-{ (void) str; struct divecomputer *dc = _dc; dc->deviceid = get_hex(line); }
+{ (void) str; struct divecomputer *dc = _dc; set_dc_deviceid(dc, get_hex(line)); }
 
 static void parse_dc_diveid(char *line, struct membuffer *str, void *_dc)
 { (void) str; struct divecomputer *dc = _dc; dc->diveid = get_hex(line); }
diff --git a/core/parse-xml.c b/core/parse-xml.c
index 3b5a2d79476e..052cb124c36b 100644
--- a/core/parse-xml.c
+++ b/core/parse-xml.c
@@ -892,6 +892,8 @@ static int match_dc_data_fields(struct divecomputer *dc, const char *name, char
 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
 static void try_to_fill_dc(struct divecomputer *dc, const char *name, char *buf)
 {
+	unsigned int deviceid;
+
 	start_match("divecomputer", name, buf);
 
 	if (MATCH("date", divedate, &dc->when))
@@ -900,8 +902,10 @@ static void try_to_fill_dc(struct divecomputer *dc, const char *name, char *buf)
 		return;
 	if (MATCH("model", utf8_string, &dc->model))
 		return;
-	if (MATCH("deviceid", hex_value, &dc->deviceid))
+	if (MATCH("deviceid", hex_value, &deviceid)) {
+		set_dc_deviceid(dc, deviceid);
 		return;
+	}
 	if (MATCH("diveid", hex_value, &dc->diveid))
 		return;
 	if (MATCH("dctype", get_dc_type, &dc->divemode))
-- 
2.9.2.729.ga42d7b6



More information about the subsurface mailing list