Cache the parser data internally.
This commit is contained in:
parent
6f89e445e3
commit
95aa477129
@ -79,6 +79,11 @@
|
|||||||
#define GAUGE 1
|
#define GAUGE 1
|
||||||
#define FREEDIVE 2
|
#define FREEDIVE 2
|
||||||
|
|
||||||
|
#define NGASMIXES 6
|
||||||
|
|
||||||
|
#define HEADER 1
|
||||||
|
#define PROFILE 2
|
||||||
|
|
||||||
typedef struct oceanic_atom2_parser_t oceanic_atom2_parser_t;
|
typedef struct oceanic_atom2_parser_t oceanic_atom2_parser_t;
|
||||||
|
|
||||||
struct oceanic_atom2_parser_t {
|
struct oceanic_atom2_parser_t {
|
||||||
@ -88,6 +93,12 @@ struct oceanic_atom2_parser_t {
|
|||||||
unsigned int footersize;
|
unsigned int footersize;
|
||||||
// Cached fields.
|
// Cached fields.
|
||||||
unsigned int cached;
|
unsigned int cached;
|
||||||
|
unsigned int header;
|
||||||
|
unsigned int footer;
|
||||||
|
unsigned int mode;
|
||||||
|
unsigned int ngasmixes;
|
||||||
|
unsigned int oxygen[NGASMIXES];
|
||||||
|
unsigned int helium[NGASMIXES];
|
||||||
unsigned int divetime;
|
unsigned int divetime;
|
||||||
double maxdepth;
|
double maxdepth;
|
||||||
};
|
};
|
||||||
@ -152,6 +163,14 @@ oceanic_atom2_parser_create (dc_parser_t **out, dc_context_t *context, unsigned
|
|||||||
}
|
}
|
||||||
|
|
||||||
parser->cached = 0;
|
parser->cached = 0;
|
||||||
|
parser->header = 0;
|
||||||
|
parser->footer = 0;
|
||||||
|
parser->mode = NORMAL;
|
||||||
|
parser->ngasmixes = 0;
|
||||||
|
for (unsigned int i = 0; i < NGASMIXES; ++i) {
|
||||||
|
parser->oxygen[i] = 0;
|
||||||
|
parser->helium[i] = 0;
|
||||||
|
}
|
||||||
parser->divetime = 0;
|
parser->divetime = 0;
|
||||||
parser->maxdepth = 0.0;
|
parser->maxdepth = 0.0;
|
||||||
|
|
||||||
@ -178,6 +197,14 @@ oceanic_atom2_parser_set_data (dc_parser_t *abstract, const unsigned char *data,
|
|||||||
|
|
||||||
// Reset the cache.
|
// Reset the cache.
|
||||||
parser->cached = 0;
|
parser->cached = 0;
|
||||||
|
parser->header = 0;
|
||||||
|
parser->footer = 0;
|
||||||
|
parser->mode = NORMAL;
|
||||||
|
parser->ngasmixes = 0;
|
||||||
|
for (unsigned int i = 0; i < NGASMIXES; ++i) {
|
||||||
|
parser->oxygen[i] = 0;
|
||||||
|
parser->helium[i] = 0;
|
||||||
|
}
|
||||||
parser->divetime = 0;
|
parser->divetime = 0;
|
||||||
parser->maxdepth = 0.0;
|
parser->maxdepth = 0.0;
|
||||||
|
|
||||||
@ -320,12 +347,14 @@ oceanic_atom2_parser_get_datetime (dc_parser_t *abstract, dc_datetime_t *datetim
|
|||||||
|
|
||||||
|
|
||||||
static dc_status_t
|
static dc_status_t
|
||||||
oceanic_atom2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
|
oceanic_atom2_parser_cache (oceanic_atom2_parser_t *parser)
|
||||||
{
|
{
|
||||||
oceanic_atom2_parser_t *parser = (oceanic_atom2_parser_t *) abstract;
|
const unsigned char *data = parser->base.data;
|
||||||
|
unsigned int size = parser->base.size;
|
||||||
|
|
||||||
const unsigned char *data = abstract->data;
|
if (parser->cached) {
|
||||||
unsigned int size = abstract->size;
|
return DC_STATUS_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
// Get the total amount of bytes before and after the profile data.
|
// Get the total amount of bytes before and after the profile data.
|
||||||
unsigned int headersize = parser->headersize;
|
unsigned int headersize = parser->headersize;
|
||||||
@ -352,14 +381,88 @@ oceanic_atom2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, uns
|
|||||||
mode = (data[1] & 0x60) >> 5;
|
mode = (data[1] & 0x60) >> 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parser->cached) {
|
// Get the gas mixes.
|
||||||
sample_statistics_t statistics = SAMPLE_STATISTICS_INITIALIZER;
|
unsigned int ngasmixes = 0;
|
||||||
dc_status_t rc = oceanic_atom2_parser_samples_foreach (
|
unsigned int o2_offset = 0;
|
||||||
abstract, sample_statistics_cb, &statistics);
|
unsigned int he_offset = 0;
|
||||||
if (rc != DC_STATUS_SUCCESS)
|
if (mode == FREEDIVE) {
|
||||||
return rc;
|
ngasmixes = 0;
|
||||||
|
} else if (parser->model == DATAMASK || parser->model == COMPUMASK) {
|
||||||
|
ngasmixes = 1;
|
||||||
|
o2_offset = header + 3;
|
||||||
|
} else if (parser->model == VT4 || parser->model == VT41 ||
|
||||||
|
parser->model == A300AI) {
|
||||||
|
o2_offset = header + 4;
|
||||||
|
ngasmixes = 4;
|
||||||
|
} else if (parser->model == OCI) {
|
||||||
|
o2_offset = 0x28;
|
||||||
|
ngasmixes = 4;
|
||||||
|
} else if (parser->model == TX1) {
|
||||||
|
o2_offset = 0x3E;
|
||||||
|
he_offset = 0x48;
|
||||||
|
ngasmixes = 6;
|
||||||
|
} else if (parser->model == A300CS || parser->model == VTX) {
|
||||||
|
o2_offset = 0x2A;
|
||||||
|
if (data[0x39] & 0x04) {
|
||||||
|
ngasmixes = 1;
|
||||||
|
} else if (data[0x39] & 0x08) {
|
||||||
|
ngasmixes = 2;
|
||||||
|
} else if (data[0x39] & 0x10) {
|
||||||
|
ngasmixes = 3;
|
||||||
|
} else {
|
||||||
|
ngasmixes = 4;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
o2_offset = header + 4;
|
||||||
|
ngasmixes = 3;
|
||||||
|
}
|
||||||
|
|
||||||
parser->cached = 1;
|
// Cache the data for later use.
|
||||||
|
parser->header = header;
|
||||||
|
parser->footer = footer;
|
||||||
|
parser->mode = mode;
|
||||||
|
parser->ngasmixes = ngasmixes;
|
||||||
|
for (unsigned int i = 0; i < ngasmixes; ++i) {
|
||||||
|
if (data[o2_offset + i]) {
|
||||||
|
parser->oxygen[i] = data[o2_offset + i];
|
||||||
|
} else {
|
||||||
|
parser->oxygen[i] = 21;
|
||||||
|
}
|
||||||
|
if (he_offset) {
|
||||||
|
parser->helium[i] = data[he_offset + i];
|
||||||
|
} else {
|
||||||
|
parser->helium[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parser->cached = HEADER;
|
||||||
|
|
||||||
|
return DC_STATUS_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static dc_status_t
|
||||||
|
oceanic_atom2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsigned int flags, void *value)
|
||||||
|
{
|
||||||
|
dc_status_t status = DC_STATUS_SUCCESS;
|
||||||
|
oceanic_atom2_parser_t *parser = (oceanic_atom2_parser_t *) abstract;
|
||||||
|
|
||||||
|
const unsigned char *data = abstract->data;
|
||||||
|
unsigned int size = abstract->size;
|
||||||
|
|
||||||
|
// Cache the header data.
|
||||||
|
status = oceanic_atom2_parser_cache (parser);
|
||||||
|
if (status != DC_STATUS_SUCCESS)
|
||||||
|
return status;
|
||||||
|
|
||||||
|
// Cache the profile data.
|
||||||
|
if (parser->cached < PROFILE) {
|
||||||
|
sample_statistics_t statistics = SAMPLE_STATISTICS_INITIALIZER;
|
||||||
|
status = oceanic_atom2_parser_samples_foreach (
|
||||||
|
abstract, sample_statistics_cb, &statistics);
|
||||||
|
if (status != DC_STATUS_SUCCESS)
|
||||||
|
return status;
|
||||||
|
|
||||||
|
parser->cached = PROFILE;
|
||||||
parser->divetime = statistics.divetime;
|
parser->divetime = statistics.divetime;
|
||||||
parser->maxdepth = statistics.maxdepth;
|
parser->maxdepth = statistics.maxdepth;
|
||||||
}
|
}
|
||||||
@ -367,9 +470,6 @@ oceanic_atom2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, uns
|
|||||||
dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
|
dc_gasmix_t *gasmix = (dc_gasmix_t *) value;
|
||||||
dc_salinity_t *water = (dc_salinity_t *) value;
|
dc_salinity_t *water = (dc_salinity_t *) value;
|
||||||
|
|
||||||
unsigned int oxygen = 0;
|
|
||||||
unsigned int helium = 0;
|
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case DC_FIELD_DIVETIME:
|
case DC_FIELD_DIVETIME:
|
||||||
@ -382,47 +482,14 @@ oceanic_atom2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, uns
|
|||||||
if (parser->model == F10 || parser->model == F11)
|
if (parser->model == F10 || parser->model == F11)
|
||||||
*((double *) value) = array_uint16_le (data + 4) / 16.0 * FEET;
|
*((double *) value) = array_uint16_le (data + 4) / 16.0 * FEET;
|
||||||
else
|
else
|
||||||
*((double *) value) = array_uint16_le (data + footer + 4) / 16.0 * FEET;
|
*((double *) value) = array_uint16_le (data + parser->footer + 4) / 16.0 * FEET;
|
||||||
break;
|
break;
|
||||||
case DC_FIELD_GASMIX_COUNT:
|
case DC_FIELD_GASMIX_COUNT:
|
||||||
if (mode == FREEDIVE) {
|
*((unsigned int *) value) = parser->ngasmixes;
|
||||||
*((unsigned int *) value) = 0;
|
|
||||||
} else if (parser->model == DATAMASK || parser->model == COMPUMASK) {
|
|
||||||
*((unsigned int *) value) = 1;
|
|
||||||
} else if (parser->model == VT4 || parser->model == VT41 ||
|
|
||||||
parser->model == OCI || parser->model == A300AI) {
|
|
||||||
*((unsigned int *) value) = 4;
|
|
||||||
} else if (parser->model == TX1) {
|
|
||||||
*((unsigned int *) value) = 6;
|
|
||||||
} else if (parser->model == A300CS || parser->model == VTX) {
|
|
||||||
if (data[0x39] & 0x04) {
|
|
||||||
*((unsigned int *) value) = 1;
|
|
||||||
} else if (data[0x39] & 0x08) {
|
|
||||||
*((unsigned int *) value) = 2;
|
|
||||||
} else if (data[0x39] & 0x10) {
|
|
||||||
*((unsigned int *) value) = 3;
|
|
||||||
} else {
|
|
||||||
*((unsigned int *) value) = 4;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
*((unsigned int *) value) = 3;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case DC_FIELD_GASMIX:
|
case DC_FIELD_GASMIX:
|
||||||
if (parser->model == DATAMASK || parser->model == COMPUMASK) {
|
gasmix->oxygen = parser->oxygen[flags] / 100.0;
|
||||||
oxygen = data[header + 3];
|
gasmix->helium = parser->helium[flags] / 100.0;
|
||||||
} else if (parser->model == OCI) {
|
|
||||||
oxygen = data[0x28 + flags];
|
|
||||||
} else if (parser->model == A300CS || parser->model == VTX) {
|
|
||||||
oxygen = data[0x2A + flags];
|
|
||||||
} else if (parser->model == TX1) {
|
|
||||||
oxygen = data[0x3E + flags];
|
|
||||||
helium = data[0x48 + flags];
|
|
||||||
} else {
|
|
||||||
oxygen = data[header + 4 + flags];
|
|
||||||
}
|
|
||||||
gasmix->helium = helium / 100.0;
|
|
||||||
gasmix->oxygen = (oxygen ? oxygen / 100.0 : 0.21);
|
|
||||||
gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
|
gasmix->nitrogen = 1.0 - gasmix->oxygen - gasmix->helium;
|
||||||
break;
|
break;
|
||||||
case DC_FIELD_SALINITY:
|
case DC_FIELD_SALINITY:
|
||||||
@ -438,7 +505,7 @@ oceanic_atom2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, uns
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case DC_FIELD_DIVEMODE:
|
case DC_FIELD_DIVEMODE:
|
||||||
switch (mode) {
|
switch (parser->mode) {
|
||||||
case NORMAL:
|
case NORMAL:
|
||||||
*((unsigned int *) value) = DC_DIVEMODE_OC;
|
*((unsigned int *) value) = DC_DIVEMODE_OC;
|
||||||
break;
|
break;
|
||||||
@ -464,38 +531,20 @@ oceanic_atom2_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, uns
|
|||||||
static dc_status_t
|
static dc_status_t
|
||||||
oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
|
oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata)
|
||||||
{
|
{
|
||||||
|
dc_status_t status = DC_STATUS_SUCCESS;
|
||||||
oceanic_atom2_parser_t *parser = (oceanic_atom2_parser_t *) abstract;
|
oceanic_atom2_parser_t *parser = (oceanic_atom2_parser_t *) abstract;
|
||||||
|
|
||||||
const unsigned char *data = abstract->data;
|
const unsigned char *data = abstract->data;
|
||||||
unsigned int size = abstract->size;
|
unsigned int size = abstract->size;
|
||||||
|
|
||||||
// Get the total amount of bytes before and after the profile data.
|
// Cache the header data.
|
||||||
unsigned int headersize = parser->headersize;
|
status = oceanic_atom2_parser_cache (parser);
|
||||||
unsigned int footersize = parser->footersize;
|
if (status != DC_STATUS_SUCCESS)
|
||||||
if (size < headersize + footersize)
|
return status;
|
||||||
return DC_STATUS_DATAFORMAT;
|
|
||||||
|
|
||||||
// Get the offset to the header sample.
|
|
||||||
unsigned int header = headersize - PAGESIZE / 2;
|
|
||||||
if (parser->model == VT4 || parser->model == VT41 ||
|
|
||||||
parser->model == A300AI) {
|
|
||||||
header = 3 * PAGESIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the dive mode.
|
|
||||||
unsigned int mode = NORMAL;
|
|
||||||
if (parser->model == F10 || parser->model == F11) {
|
|
||||||
mode = FREEDIVE;
|
|
||||||
} else if (parser->model == T3B || parser->model == VT3 ||
|
|
||||||
parser->model == DG03) {
|
|
||||||
mode = (data[2] & 0xC0) >> 6;
|
|
||||||
} else if (parser->model == VEO20 || parser->model == VEO30) {
|
|
||||||
mode = (data[1] & 0x60) >> 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int time = 0;
|
unsigned int time = 0;
|
||||||
unsigned int interval = 1;
|
unsigned int interval = 1;
|
||||||
if (mode != FREEDIVE) {
|
if (parser->mode != FREEDIVE) {
|
||||||
unsigned int idx = 0x17;
|
unsigned int idx = 0x17;
|
||||||
if (parser->model == A300CS || parser->model == VTX)
|
if (parser->model == A300CS || parser->model == VTX)
|
||||||
idx = 0x1f;
|
idx = 0x1f;
|
||||||
@ -516,7 +565,7 @@ oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int samplesize = PAGESIZE / 2;
|
unsigned int samplesize = PAGESIZE / 2;
|
||||||
if (mode == FREEDIVE) {
|
if (parser->mode == FREEDIVE) {
|
||||||
if (parser->model == F10 || parser->model == F11) {
|
if (parser->model == F10 || parser->model == F11) {
|
||||||
samplesize = 2;
|
samplesize = 2;
|
||||||
} else {
|
} else {
|
||||||
@ -530,7 +579,7 @@ oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_
|
|||||||
}
|
}
|
||||||
|
|
||||||
unsigned int have_temperature = 1, have_pressure = 1;
|
unsigned int have_temperature = 1, have_pressure = 1;
|
||||||
if (mode == FREEDIVE) {
|
if (parser->mode == FREEDIVE) {
|
||||||
have_temperature = 0;
|
have_temperature = 0;
|
||||||
have_pressure = 0;
|
have_pressure = 0;
|
||||||
} else if (parser->model == VEO30 || parser->model == OCS ||
|
} else if (parser->model == VEO30 || parser->model == OCS ||
|
||||||
@ -544,7 +593,7 @@ oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_
|
|||||||
// Initial temperature.
|
// Initial temperature.
|
||||||
unsigned int temperature = 0;
|
unsigned int temperature = 0;
|
||||||
if (have_temperature) {
|
if (have_temperature) {
|
||||||
temperature = data[header + 7];
|
temperature = data[parser->header + 7];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial tank pressure.
|
// Initial tank pressure.
|
||||||
@ -554,14 +603,14 @@ oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_
|
|||||||
unsigned int idx = 2;
|
unsigned int idx = 2;
|
||||||
if (parser->model == A300CS || parser->model == VTX)
|
if (parser->model == A300CS || parser->model == VTX)
|
||||||
idx = 16;
|
idx = 16;
|
||||||
pressure = data[header + idx] + (data[header + idx + 1] << 8);
|
pressure = array_uint16_le(data + parser->header + idx);
|
||||||
if (pressure == 10000)
|
if (pressure == 10000)
|
||||||
have_pressure = 0;
|
have_pressure = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int complete = 1;
|
unsigned int complete = 1;
|
||||||
unsigned int offset = headersize;
|
unsigned int offset = parser->headersize;
|
||||||
while (offset + samplesize <= size - footersize) {
|
while (offset + samplesize <= size - parser->footersize) {
|
||||||
dc_sample_value_t sample = {0};
|
dc_sample_value_t sample = {0};
|
||||||
|
|
||||||
// Ignore empty samples.
|
// Ignore empty samples.
|
||||||
@ -582,7 +631,7 @@ oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_
|
|||||||
|
|
||||||
// Get the sample type.
|
// Get the sample type.
|
||||||
unsigned int sampletype = data[offset + 0];
|
unsigned int sampletype = data[offset + 0];
|
||||||
if (mode == FREEDIVE)
|
if (parser->mode == FREEDIVE)
|
||||||
sampletype = 0;
|
sampletype = 0;
|
||||||
|
|
||||||
// The sample size is usually fixed, but some sample types have a
|
// The sample size is usually fixed, but some sample types have a
|
||||||
@ -698,7 +747,7 @@ oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_
|
|||||||
|
|
||||||
// Depth (1/16 ft)
|
// Depth (1/16 ft)
|
||||||
unsigned int depth;
|
unsigned int depth;
|
||||||
if (mode == FREEDIVE)
|
if (parser->mode == FREEDIVE)
|
||||||
depth = array_uint16_le (data + offset);
|
depth = array_uint16_le (data + offset);
|
||||||
else if (parser->model == GEO20 || parser->model == VEO20 ||
|
else if (parser->model == GEO20 || parser->model == VEO20 ||
|
||||||
parser->model == VEO30 || parser->model == OC1A ||
|
parser->model == VEO30 || parser->model == OC1A ||
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user