From 7e3bf7eeb8e1ed63178af885dd6d5589e82d4354 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 29 Apr 2022 17:00:43 +0200 Subject: [PATCH] Use helper functions to decode multibyte values Add and use some helper functions for decoding multibyte BCD and binary encoded values, to remove some code duplication. --- src/array.c | 12 ++++++++++++ src/array.h | 3 +++ src/oceanic_common.c | 4 ++-- src/suunto_common2.c | 6 +----- src/suunto_eon.c | 6 +----- src/suunto_solution.c | 6 +----- src/suunto_vyper.c | 6 +----- 7 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/array.c b/src/array.c index 1777a01..6d9550e 100644 --- a/src/array.c +++ b/src/array.c @@ -160,6 +160,18 @@ array_convert_str2num (const unsigned char data[], unsigned int size) return value; } +unsigned int +array_convert_bin2dec (const unsigned char data[], unsigned int size) +{ + unsigned int value = 0; + for (unsigned int i = 0; i < size; ++i) { + value *= 100; + value += data[i]; + } + + return value; +} + unsigned int array_convert_bcd2dec (const unsigned char data[], unsigned int size) { diff --git a/src/array.h b/src/array.h index d69fa32..3c45785 100644 --- a/src/array.h +++ b/src/array.h @@ -52,6 +52,9 @@ array_convert_hex2bin (const unsigned char input[], unsigned int isize, unsigned unsigned int array_convert_str2num (const unsigned char data[], unsigned int size); +unsigned int +array_convert_bin2dec (const unsigned char data[], unsigned int size); + unsigned int array_convert_bcd2dec (const unsigned char data[], unsigned int size); diff --git a/src/oceanic_common.c b/src/oceanic_common.c index 51c234f..77f4dc7 100644 --- a/src/oceanic_common.c +++ b/src/oceanic_common.c @@ -612,9 +612,9 @@ oceanic_common_device_foreach (dc_device_t *abstract, dc_dive_callback_t callbac devinfo.model = array_uint16_be (id + 8); devinfo.firmware = device->firmware; if (layout->pt_mode_serial == 0) - devinfo.serial = bcd2dec (id[10]) * 10000 + bcd2dec (id[11]) * 100 + bcd2dec (id[12]); + devinfo.serial = array_convert_bcd2dec (id + 10, 3); else if (layout->pt_mode_serial == 1) - devinfo.serial = id[11] * 10000 + id[12] * 100 + id[13]; + devinfo.serial = array_convert_bin2dec (id + 11, 3); else devinfo.serial = (id[11] & 0x0F) * 100000 + ((id[11] & 0xF0) >> 4) * 10000 + diff --git a/src/suunto_common2.c b/src/suunto_common2.c index fd56562..e8f7c51 100644 --- a/src/suunto_common2.c +++ b/src/suunto_common2.c @@ -264,11 +264,7 @@ suunto_common2_device_foreach (dc_device_t *abstract, dc_dive_callback_t callbac dc_event_devinfo_t devinfo; devinfo.model = device->version[0]; devinfo.firmware = array_uint24_be (device->version + 1); - devinfo.serial = 0; - for (unsigned int i = 0; i < 4; ++i) { - devinfo.serial *= 100; - devinfo.serial += serial[i]; - } + devinfo.serial = array_convert_bin2dec (serial, 4); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); // Read the header bytes. diff --git a/src/suunto_eon.c b/src/suunto_eon.c index 84fc367..cc0ea5d 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -205,11 +205,7 @@ suunto_eon_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, v dc_event_devinfo_t devinfo; devinfo.model = 0; devinfo.firmware = 0; - devinfo.serial = 0; - for (unsigned int i = 0; i < 3; ++i) { - devinfo.serial *= 100; - devinfo.serial += bcd2dec (data[244 + i]); - } + devinfo.serial = array_convert_bcd2dec (data + 244, 3); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); rc = suunto_common_extract_dives (device, &suunto_eon_layout, data, callback, userdata); diff --git a/src/suunto_solution.c b/src/suunto_solution.c index a09c6d5..99ac331 100644 --- a/src/suunto_solution.c +++ b/src/suunto_solution.c @@ -243,11 +243,7 @@ suunto_solution_device_foreach (dc_device_t *abstract, dc_dive_callback_t callba dc_event_devinfo_t devinfo; devinfo.model = 0; devinfo.firmware = 0; - devinfo.serial = 0; - for (unsigned int i = 0; i < 3; ++i) { - devinfo.serial *= 100; - devinfo.serial += bcd2dec (data[0x1D + i]); - } + devinfo.serial = array_convert_bcd2dec (data + 0x1D, 3); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); rc = suunto_solution_extract_dives (abstract, diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c index fe3b405..8465b9c 100644 --- a/src/suunto_vyper.c +++ b/src/suunto_vyper.c @@ -476,11 +476,7 @@ suunto_vyper_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, dc_event_devinfo_t devinfo; devinfo.model = header[hoffset + 0]; devinfo.firmware = header[hoffset + 1]; - devinfo.serial = 0; - for (unsigned int i = 0; i < 4; ++i) { - devinfo.serial *= 100; - devinfo.serial += header[hoffset + 2 + i]; - } + devinfo.serial = array_convert_bin2dec (header + hoffset + 2, 4); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); // Allocate a memory buffer.