Use macros to detect the device type

The macros make the code a bit more compact, and adding support for new
models becomes easier too.
This commit is contained in:
Jef Driesen 2024-01-11 21:20:22 +01:00
parent 89a28427d6
commit 9a603fa8cf
2 changed files with 39 additions and 21 deletions

View File

@ -49,6 +49,15 @@
#define QUAD 0x29 #define QUAD 0x29
#define HORIZON 0x2C #define HORIZON 0x2C
#define ISSMART(model) ( \
(model) == SMART || \
(model) == SMARTAPNEA || \
(model) == SMARTAIR)
#define ISGENIUS(model) ( \
(model) == GENIUS || \
(model) == HORIZON)
#define MAXRETRIES 4 #define MAXRETRIES 4
#define ACK 0xAA #define ACK 0xAA
@ -723,7 +732,7 @@ mares_iconhd_device_foreach_raw (dc_device_t *abstract, dc_dive_callback_t callb
// Get the number of samples in the profile data. // Get the number of samples in the profile data.
unsigned int type = 0, nsamples = 0; unsigned int type = 0, nsamples = 0;
if (model == SMART || model == SMARTAPNEA || model == SMARTAIR) { if (ISSMART(model)) {
type = array_uint16_le (buffer + offset - header + 2); type = array_uint16_le (buffer + offset - header + 2);
nsamples = array_uint16_le (buffer + offset - header + 0); nsamples = array_uint16_le (buffer + offset - header + 0);
} else { } else {
@ -938,7 +947,7 @@ mares_iconhd_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback,
devinfo.serial = array_uint32_le (serial); devinfo.serial = array_uint32_le (serial);
device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo);
if (device->model == GENIUS || device->model == HORIZON) { if (ISGENIUS(device->model)) {
return mares_iconhd_device_foreach_object (abstract, callback, userdata); return mares_iconhd_device_foreach_object (abstract, callback, userdata);
} else { } else {
return mares_iconhd_device_foreach_raw (abstract, callback, userdata); return mares_iconhd_device_foreach_raw (abstract, callback, userdata);

View File

@ -46,6 +46,15 @@
#define SMARTAIR 0x24 #define SMARTAIR 0x24
#define HORIZON 0x2C #define HORIZON 0x2C
#define ISSMART(model) ( \
(model) == SMART || \
(model) == SMARTAPNEA || \
(model) == SMARTAIR)
#define ISGENIUS(model) ( \
(model) == GENIUS || \
(model) == HORIZON)
#define NGASMIXES_ICONHD 3 #define NGASMIXES_ICONHD 3
#define NGASMIXES_GENIUS 5 #define NGASMIXES_GENIUS 5
#define NGASMIXES NGASMIXES_GENIUS #define NGASMIXES NGASMIXES_GENIUS
@ -332,7 +341,7 @@ mares_iconhd_cache (mares_iconhd_parser_t *parser)
// Get the number of samples in the profile data. // Get the number of samples in the profile data.
unsigned int type = 0, nsamples = 0; unsigned int type = 0, nsamples = 0;
if (parser->model == SMART || parser->model == SMARTAPNEA || parser->model == SMARTAIR) { if (ISSMART (parser->model)) {
type = array_uint16_le (data + length - header + 2); type = array_uint16_le (data + length - header + 2);
nsamples = array_uint16_le (data + length - header + 0); nsamples = array_uint16_le (data + length - header + 0);
} else { } else {
@ -387,7 +396,7 @@ mares_iconhd_cache (mares_iconhd_parser_t *parser)
} }
const unsigned char *p = data + length - headersize; const unsigned char *p = data + length - headersize;
if (parser->model != SMART && parser->model != SMARTAPNEA && parser->model != SMARTAIR) { if (!ISSMART(parser->model)) {
p += 4; p += 4;
} }
@ -646,7 +655,7 @@ mares_iconhd_parser_cache (mares_iconhd_parser_t *parser)
return DC_STATUS_SUCCESS; return DC_STATUS_SUCCESS;
} }
if (parser->model == GENIUS || parser->model == HORIZON) { if (ISGENIUS(parser->model)) {
return mares_genius_cache (parser); return mares_genius_cache (parser);
} else { } else {
return mares_iconhd_cache (parser); return mares_iconhd_cache (parser);
@ -672,7 +681,7 @@ mares_iconhd_parser_create (dc_parser_t **out, dc_context_t *context, const unsi
parser->model = model; parser->model = model;
parser->cached = 0; parser->cached = 0;
parser->logformat = 0; parser->logformat = 0;
parser->mode = (model == GENIUS || model == HORIZON) ? GENIUS_AIR : ICONHD_AIR; parser->mode = ISGENIUS(model) ? GENIUS_AIR : ICONHD_AIR;
parser->nsamples = 0; parser->nsamples = 0;
parser->samplesize = 0; parser->samplesize = 0;
parser->headersize = 0; parser->headersize = 0;
@ -711,9 +720,9 @@ mares_iconhd_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime
// Pointer to the header data. // Pointer to the header data.
const unsigned char *p = abstract->data; const unsigned char *p = abstract->data;
if (parser->model != GENIUS && parser->model != HORIZON) { if (!ISGENIUS(parser->model)) {
p += abstract->size - parser->headersize; p += abstract->size - parser->headersize;
if (parser->model != SMART && parser->model != SMARTAPNEA && parser->model != SMARTAIR) { if (!ISSMART(parser->model)) {
p += 4; p += 4;
} }
} }
@ -722,7 +731,7 @@ mares_iconhd_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetime
p += parser->layout->datetime; p += parser->layout->datetime;
if (datetime) { if (datetime) {
if (parser->model == GENIUS || parser->model == HORIZON) { if (ISGENIUS(parser->model)) {
unsigned int timestamp = array_uint32_le (p); unsigned int timestamp = array_uint32_le (p);
datetime->hour = (timestamp ) & 0x1F; datetime->hour = (timestamp ) & 0x1F;
datetime->minute = (timestamp >> 5) & 0x3F; datetime->minute = (timestamp >> 5) & 0x3F;
@ -757,9 +766,9 @@ mares_iconhd_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsi
// Pointer to the header data. // Pointer to the header data.
const unsigned char *p = abstract->data; const unsigned char *p = abstract->data;
if (parser->model != GENIUS && parser->model != HORIZON) { if (!ISGENIUS(parser->model)) {
p += abstract->size - parser->headersize; p += abstract->size - parser->headersize;
if (parser->model != SMART && parser->model != SMARTAPNEA && parser->model != SMARTAIR) { if (!ISSMART(parser->model)) {
p += 4; p += 4;
} }
} }
@ -770,7 +779,7 @@ mares_iconhd_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsi
extra = 8; extra = 8;
} }
unsigned int metric = (parser->model == GENIUS || parser->model == HORIZON) ? unsigned int metric = ISGENIUS(parser->model) ?
p[0x34 + extra] : parser->settings & 0x0100; p[0x34 + extra] : parser->settings & 0x0100;
dc_gasmix_t *gasmix = (dc_gasmix_t *) value; dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
@ -827,7 +836,7 @@ mares_iconhd_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsi
*((double *) value) = array_uint16_le (p + parser->layout->atmospheric) / (1000.0 * parser->layout->atmospheric_divisor); *((double *) value) = array_uint16_le (p + parser->layout->atmospheric) / (1000.0 * parser->layout->atmospheric_divisor);
break; break;
case DC_FIELD_SALINITY: case DC_FIELD_SALINITY:
if (parser->model == GENIUS || parser->model == HORIZON) { if (ISGENIUS(parser->model)) {
unsigned int salinity = (parser->settings >> 5) & 0x03; unsigned int salinity = (parser->settings >> 5) & 0x03;
switch (salinity) { switch (salinity) {
case WATER_FRESH: case WATER_FRESH:
@ -869,7 +878,7 @@ mares_iconhd_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsi
*((double *) value) = (signed short) array_uint16_le (p + parser->layout->temperature_max) / 10.0; *((double *) value) = (signed short) array_uint16_le (p + parser->layout->temperature_max) / 10.0;
break; break;
case DC_FIELD_DIVEMODE: case DC_FIELD_DIVEMODE:
if (parser->model == GENIUS || parser->model == HORIZON) { if (ISGENIUS(parser->model)) {
switch (parser->mode) { switch (parser->mode) {
case GENIUS_AIR: case GENIUS_AIR:
case GENIUS_NITROX_SINGLE: case GENIUS_NITROX_SINGLE:
@ -933,7 +942,7 @@ mares_iconhd_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t
unsigned int offset = 4; unsigned int offset = 4;
unsigned int marker = 0; unsigned int marker = 0;
if (parser->model == GENIUS || parser->model == HORIZON) { if (ISGENIUS(parser->model)) {
// Skip the dive header. // Skip the dive header.
data += parser->headersize; data += parser->headersize;
@ -1003,7 +1012,7 @@ mares_iconhd_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t
offset += 2; offset += 2;
} }
} else if (parser->model != GENIUS && parser->model != HORIZON && parser->mode == ICONHD_FREEDIVE) { } else if (!ISGENIUS(parser->model) && parser->mode == ICONHD_FREEDIVE) {
unsigned int maxdepth = array_uint16_le (data + offset + 0); unsigned int maxdepth = array_uint16_le (data + offset + 0);
unsigned int divetime = array_uint16_le (data + offset + 2); unsigned int divetime = array_uint16_le (data + offset + 2);
unsigned int surftime = array_uint16_le (data + offset + 4); unsigned int surftime = array_uint16_le (data + offset + 4);
@ -1033,7 +1042,7 @@ mares_iconhd_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t
unsigned int gasmix = 0, alarms = 0; unsigned int gasmix = 0, alarms = 0;
unsigned int decostop = 0, decodepth = 0, decotime = 0, tts = 0; unsigned int decostop = 0, decodepth = 0, decotime = 0, tts = 0;
unsigned int bookmark = 0; unsigned int bookmark = 0;
if (parser->model == GENIUS || parser->model == HORIZON) { if (ISGENIUS(parser->model)) {
if (parser->logformat == 1) { if (parser->logformat == 1) {
if (!mares_genius_isvalid (data + offset, SDPT_SIZE, SDPT_TYPE)) { if (!mares_genius_isvalid (data + offset, SDPT_SIZE, SDPT_TYPE)) {
ERROR (abstract->context, "Invalid SDPT record."); ERROR (abstract->context, "Invalid SDPT record.");
@ -1114,7 +1123,7 @@ mares_iconhd_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t
if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata); if (callback) callback (DC_SAMPLE_EVENT, &sample, userdata);
} }
if (parser->model == GENIUS || parser->model == HORIZON) { if (ISGENIUS(parser->model)) {
// Deco stop / NDL. // Deco stop / NDL.
if (decostop) { if (decostop) {
sample.deco.type = DC_DECO_DECOSTOP; sample.deco.type = DC_DECO_DECOSTOP;
@ -1161,7 +1170,7 @@ mares_iconhd_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t
// Some extra data. // Some extra data.
if (parser->layout->tanks != UNSUPPORTED && (nsamples % 4) == 0) { if (parser->layout->tanks != UNSUPPORTED && (nsamples % 4) == 0) {
if ((parser->model == GENIUS || parser->model == HORIZON) && if (ISGENIUS(parser->model) &&
!mares_genius_isvalid (data + offset, AIRS_SIZE, AIRS_TYPE)) { !mares_genius_isvalid (data + offset, AIRS_SIZE, AIRS_TYPE)) {
ERROR (abstract->context, "Invalid AIRS record."); ERROR (abstract->context, "Invalid AIRS record.");
return DC_STATUS_DATAFORMAT; return DC_STATUS_DATAFORMAT;
@ -1177,12 +1186,12 @@ mares_iconhd_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t
WARNING (abstract->context, "Invalid tank with non-zero pressure."); WARNING (abstract->context, "Invalid tank with non-zero pressure.");
} }
offset += (parser->model == GENIUS || parser->model == HORIZON) ? AIRS_SIZE : 8; offset += ISGENIUS(parser->model) ? AIRS_SIZE : 8;
} }
} }
} }
if (parser->model == GENIUS || parser->model == HORIZON) { if (ISGENIUS(parser->model)) {
// Skip the DEND record. // Skip the DEND record.
if (!mares_genius_isvalid (data + offset, DEND_SIZE, DEND_TYPE)) { if (!mares_genius_isvalid (data + offset, DEND_SIZE, DEND_TYPE)) {
ERROR (abstract->context, "Invalid DEND record."); ERROR (abstract->context, "Invalid DEND record.");