From 7415824a8c6a650bba185686f389b34bcd3ea854 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 5 Oct 2020 21:24:31 +0200 Subject: [PATCH] core: use C accessors in core/libdivecomputer.c instead of callback Searching the proper device for the divecomputer was done via a callback. Very hard to follow code. Since we can now access "struct device" from C, obtain it directly via get_device_for_dc(). Signed-off-by: Berthold Stoeger --- core/libdivecomputer.c | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c index 12b9dabf9..071350c8b 100644 --- a/core/libdivecomputer.c +++ b/core/libdivecomputer.c @@ -551,25 +551,6 @@ static uint32_t calculate_string_hash(const char *str) return calculate_diveid((const unsigned char *)str, strlen(str)); } -/* - * Find an existing device ID for this device model and serial number - */ -static void dc_match_serial(void *_dc, const char *model, uint32_t deviceid, const char *nickname, const char *serial, const char *firmware) -{ - UNUSED(nickname); - UNUSED(firmware); - - struct divecomputer *dc = _dc; - - if (!deviceid) - return; - if (!dc->model || !model || strcasecmp(dc->model, model)) - return; - if (!dc->serial || !serial || strcasecmp(dc->serial, serial)) - return; - dc->deviceid = deviceid; -} - /* * Set the serial number. * @@ -581,8 +562,12 @@ static void dc_match_serial(void *_dc, const char *model, uint32_t deviceid, con */ static void set_dc_serial(struct divecomputer *dc, const char *serial) { + const struct device *device; + dc->serial = serial; - call_for_each_dc(dc, dc_match_serial, false); + if ((device = get_device_for_dc(&device_table, dc)) != NULL) + dc->deviceid = device_get_id(device); + if (!dc->deviceid) dc->deviceid = calculate_string_hash(serial); }