Replace the hardcoded constants with a layout descriptor.

A side effect is that the mares_iconhd_extract_dives function now
requires a valid device handle. This shouldn't cause any real problems
because this function will likely become private some day.
This commit is contained in:
Jef Driesen 2013-05-09 22:20:33 +02:00
parent 51d6074bfe
commit ad0e187c0c

View File

@ -52,15 +52,16 @@
#define ACK 0xAA
#define EOF 0xEA
#define SZ_MEMORY 0x100000
#define SZ_PACKET 0x000100
#define RB_PROFILE_BEGIN 0xA000
#define RB_PROFILE_END SZ_MEMORY
typedef struct mares_iconhd_layout_t {
unsigned int memsize;
unsigned int rb_profile_begin;
unsigned int rb_profile_end;
} mares_iconhd_layout_t;
typedef struct mares_iconhd_device_t {
dc_device_t base;
serial_t *port;
const mares_iconhd_layout_t *layout;
unsigned char fingerprint[10];
unsigned char version[140];
unsigned int packetsize;
@ -82,6 +83,11 @@ static const dc_device_vtable_t mares_iconhd_device_vtable = {
mares_iconhd_device_close /* close */
};
static const mares_iconhd_layout_t mares_iconhd_layout = {
0x100000, /* memsize */
0x00A000, /* rb_profile_begin */
0x100000, /* rb_profile_end */
};
static unsigned int
mares_iconhd_get_model (mares_iconhd_device_t *device, unsigned int model)
@ -247,10 +253,11 @@ mares_iconhd_device_open (dc_device_t **out, dc_context_t *context, const char *
// Set the default values.
device->port = NULL;
device->layout = &mares_iconhd_layout;
memset (device->fingerprint, 0, sizeof (device->fingerprint));
memset (device->version, 0, sizeof (device->version));
if (model == NEMOWIDE2 || model == MATRIX || model == PUCKPRO) {
device->packetsize = SZ_PACKET;
device->packetsize = 64;
} else {
device->packetsize = 0;
}
@ -362,14 +369,14 @@ mares_iconhd_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
// Erase the current contents of the buffer and
// pre-allocate the required amount of memory.
if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) {
if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, device->layout->memsize)) {
ERROR (abstract->context, "Insufficient buffer space available.");
return DC_STATUS_NOMEMORY;
}
// Enable progress notifications.
dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER;
progress.maximum = SZ_MEMORY;
progress.maximum = device->layout->memsize;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
// Emit a vendor event.
@ -388,7 +395,7 @@ mares_iconhd_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback,
{
mares_iconhd_device_t *device = (mares_iconhd_device_t *) abstract;
dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY);
dc_buffer_t *buffer = dc_buffer_new (device->layout->memsize);
if (buffer == NULL)
return DC_STATUS_NOMEMORY;
@ -421,10 +428,12 @@ mares_iconhd_extract_dives (dc_device_t *abstract, const unsigned char data[], u
mares_iconhd_device_t *device = (mares_iconhd_device_t *) abstract;
dc_context_t *context = (abstract ? abstract->context : NULL);
if (abstract && !ISINSTANCE (abstract))
if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS;
if (size < SZ_MEMORY)
const mares_iconhd_layout_t *layout = device->layout;
if (size < layout->memsize)
return DC_STATUS_DATAFORMAT;
// Get the model code.
@ -443,22 +452,22 @@ mares_iconhd_extract_dives (dc_device_t *abstract, const unsigned char data[], u
if (eop != 0xFFFFFFFF)
break;
}
if (eop < RB_PROFILE_BEGIN || eop >= RB_PROFILE_END) {
if (eop < layout->rb_profile_begin || eop >= layout->rb_profile_end) {
ERROR (context, "Ringbuffer pointer out of range.");
return DC_STATUS_DATAFORMAT;
}
// Make the ringbuffer linear, to avoid having to deal with the wrap point.
unsigned char *buffer = (unsigned char *) malloc (RB_PROFILE_END - RB_PROFILE_BEGIN);
unsigned char *buffer = (unsigned char *) malloc (layout->rb_profile_end - layout->rb_profile_begin);
if (buffer == NULL) {
ERROR (context, "Failed to allocate memory.");
return DC_STATUS_NOMEMORY;
}
memcpy (buffer + 0, data + eop, RB_PROFILE_END - eop);
memcpy (buffer + RB_PROFILE_END - eop, data + RB_PROFILE_BEGIN, eop - RB_PROFILE_BEGIN);
memcpy (buffer + 0, data + eop, layout->rb_profile_end - eop);
memcpy (buffer + layout->rb_profile_end - eop, data + layout->rb_profile_begin, eop - layout->rb_profile_begin);
unsigned int offset = RB_PROFILE_END - RB_PROFILE_BEGIN;
unsigned int offset = layout->rb_profile_end - layout->rb_profile_begin;
while (offset >= header + 4) {
// Get the number of samples in the profile data.
unsigned int nsamples = array_uint16_le (buffer + offset - header + 2);