Update the serial number and deviceid in sync when loading

When we save the divecomputer data, we never actually save the serial
value as a field.  We used to rely on saving the very dodgy 'deviceid',
and then look up the serial number from there.  And that never really
worked reliably, but we didn't really notice, because we never really
_used_ the serial number anywhere.

The only place the serial number is actually reliably displayed is in
the "Extra data" tab, which contains the key value pairs, and that's
where the original dive download code got the serial number from.

So just parse that at load time too, the same way we parsed it at dive
download time.

In fact, do the firmware version the same way, and remove the code from
the downloader, since it too can rely on 'add_extra_data()' just picking
up the information directly.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
Linus Torvalds 2021-08-16 20:52:00 -10:00 committed by Dirk Hohndel
parent 35adf2d729
commit d141bbf38f
2 changed files with 11 additions and 24 deletions

View file

@ -474,16 +474,22 @@ void remove_event_from_dc(struct divecomputer *dc, struct event *event)
void add_extra_data(struct divecomputer *dc, const char *key, const char *value)
{
struct extra_data **ed = &dc->extra_data;
const char *newval = strdup(value);
if (!strcasecmp(key, "Serial"))
if (!strcasecmp(key, "Serial")) {
dc->deviceid = calculate_string_hash(value);
dc->serial = newval;
}
if (!strcmp(key, "FW Version")) {
dc->fw_version = newval;
}
while (*ed)
ed = &(*ed)->next;
*ed = malloc(sizeof(struct extra_data));
if (*ed) {
(*ed)->key = strdup(key);
(*ed)->value = strdup(value);
(*ed)->value = newval;
(*ed)->next = NULL;
}
}

View file

@ -553,20 +553,6 @@ uint32_t calculate_string_hash(const char *str)
return calculate_diveid((const unsigned char *)str, strlen(str));
}
/*
* Set the serial number.
*
* This also sets the device ID by hashing the serial
* number string.
*/
static void set_dc_serial(struct divecomputer *dc, const char *serial, const device_data_t *devdata)
{
const struct device *device;
dc->serial = strdup(serial);
dc->deviceid = calculate_string_hash(serial);
}
static void parse_string_field(device_data_t *devdata, struct dive *dive, dc_field_string_t *str)
{
// Our dive ID is the string hash of the "Dive ID" string
@ -575,15 +561,10 @@ static void parse_string_field(device_data_t *devdata, struct dive *dive, dc_fie
dive->dc.diveid = calculate_string_hash(str->value);
return;
}
// This will pick up serial number and firmware data
add_extra_data(&dive->dc, str->desc, str->value);
if (!strcmp(str->desc, "Serial")) {
set_dc_serial(&dive->dc, str->value, devdata);
return;
}
if (!strcmp(str->desc, "FW Version")) {
dive->dc.fw_version = strdup(str->value);
return;
}
/* GPS data? */
if (!strncmp(str->desc, "GPS", 3)) {
char *line = (char *) str->value;