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.
This commit is contained in:
parent
0753f10661
commit
7e3bf7eeb8
12
src/array.c
12
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)
|
||||
{
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -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 +
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user