Remove the common device structure for the Mares Nemo.

The common device structure was used only for sharing the fingerprint
and layout descriptor, but the nemo backend doesn't even store a layout
descriptor, and the fingerprint can equally well be passed around as a
function argument.
This commit is contained in:
Jef Driesen 2011-10-29 11:30:56 +02:00
parent 22686d6fe9
commit 5410378849
4 changed files with 65 additions and 54 deletions

View File

@ -39,32 +39,11 @@ mares_common_device_init (mares_common_device_t *device, const device_backend_t
device_init (&device->base, backend);
// Set the default values.
memset (device->fingerprint, 0, sizeof (device->fingerprint));
device->layout = NULL;
}
device_status_t
mares_common_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size)
{
mares_common_device_t *device = (mares_common_device_t *) abstract;
assert (device != NULL);
if (size && size != sizeof (device->fingerprint))
return DEVICE_STATUS_ERROR;
if (size)
memcpy (device->fingerprint, data, sizeof (device->fingerprint));
else
memset (device->fingerprint, 0, sizeof (device->fingerprint));
return DEVICE_STATUS_SUCCESS;
}
device_status_t
mares_common_extract_dives (mares_common_device_t *device, const mares_common_layout_t *layout, const unsigned char data[], dive_callback_t callback, void *userdata)
mares_common_extract_dives (const mares_common_layout_t *layout, const unsigned char fingerprint[], const unsigned char data[], dive_callback_t callback, void *userdata)
{
assert (layout != NULL);
@ -205,12 +184,12 @@ mares_common_extract_dives (mares_common_device_t *device, const mares_common_la
}
unsigned int fp_offset = offset + length - extra - FP_OFFSET;
if (device && memcmp (buffer + fp_offset, device->fingerprint, sizeof (device->fingerprint)) == 0) {
if (fingerprint && memcmp (buffer + fp_offset, fingerprint, FP_SIZE) == 0) {
free (buffer);
return DEVICE_STATUS_SUCCESS;
}
if (callback && !callback (buffer + offset, nbytes, buffer + fp_offset, sizeof (device->fingerprint), userdata)) {
if (callback && !callback (buffer + offset, nbytes, buffer + fp_offset, FP_SIZE, userdata)) {
free (buffer);
return DEVICE_STATUS_SUCCESS;
}

View File

@ -38,18 +38,13 @@ typedef struct mares_common_layout_t {
typedef struct mares_common_device_t {
device_t base;
unsigned char fingerprint[5];
const mares_common_layout_t *layout;
} mares_common_device_t;
void
mares_common_device_init (mares_common_device_t *device, const device_backend_t *backend);
device_status_t
mares_common_device_set_fingerprint (device_t *device, const unsigned char data[], unsigned int size);
device_status_t
mares_common_extract_dives (mares_common_device_t *device, const mares_common_layout_t *layout, const unsigned char data[], dive_callback_t callback, void *userdata);
mares_common_extract_dives (const mares_common_layout_t *layout, const unsigned char fingerprint[], const unsigned char data[], dive_callback_t callback, void *userdata);
#ifdef __cplusplus
}

View File

@ -21,7 +21,6 @@
#include <string.h> // memcpy, memcmp
#include <stdlib.h> // malloc, free
#include <assert.h> // assert
#include "device-private.h"
#include "mares_common.h"
@ -44,17 +43,19 @@
#define NEMOAPNEIST 18
typedef struct mares_nemo_device_t {
mares_common_device_t base;
device_t base;
serial_t *port;
unsigned char fingerprint[5];
} mares_nemo_device_t;
static device_status_t mares_nemo_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size);
static device_status_t mares_nemo_device_dump (device_t *abstract, dc_buffer_t *buffer);
static device_status_t mares_nemo_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata);
static device_status_t mares_nemo_device_close (device_t *abstract);
static const device_backend_t mares_nemo_device_backend = {
DEVICE_TYPE_MARES_NEMO,
mares_common_device_set_fingerprint, /* set_fingerprint */
mares_nemo_device_set_fingerprint, /* set_fingerprint */
NULL, /* version */
NULL, /* read */
NULL, /* write */
@ -103,10 +104,11 @@ mares_nemo_device_open (device_t **out, const char* name)
}
// Initialize the base class.
mares_common_device_init (&device->base, &mares_nemo_device_backend);
device_init (&device->base, &mares_nemo_device_backend);
// Set the default values.
device->port = NULL;
memset (device->fingerprint, 0, sizeof (device->fingerprint));
// Open the device.
int rc = serial_open (&device->port, name);
@ -173,12 +175,26 @@ mares_nemo_device_close (device_t *abstract)
static device_status_t
mares_nemo_device_dump (device_t *abstract, dc_buffer_t *buffer)
mares_nemo_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size)
{
mares_nemo_device_t *device = (mares_nemo_device_t *) abstract;
assert (device != NULL);
assert (device->base.layout == NULL);
if (size && size != sizeof (device->fingerprint))
return DEVICE_STATUS_ERROR;
if (size)
memcpy (device->fingerprint, data, sizeof (device->fingerprint));
else
memset (device->fingerprint, 0, sizeof (device->fingerprint));
return DEVICE_STATUS_SUCCESS;
}
static device_status_t
mares_nemo_device_dump (device_t *abstract, dc_buffer_t *buffer)
{
mares_nemo_device_t *device = (mares_nemo_device_t *) abstract;
// Erase the current contents of the buffer and
// pre-allocate the required amount of memory.
@ -269,11 +285,6 @@ mares_nemo_device_dump (device_t *abstract, dc_buffer_t *buffer)
static device_status_t
mares_nemo_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
{
mares_common_device_t *device = (mares_common_device_t *) abstract;
assert (device != NULL);
assert (device->layout == NULL);
dc_buffer_t *buffer = dc_buffer_new (MEMORYSIZE);
if (buffer == NULL)
return DEVICE_STATUS_MEMORY;
@ -303,7 +314,7 @@ mares_nemo_device_foreach (device_t *abstract, dive_callback_t callback, void *u
device_status_t
mares_nemo_extract_dives (device_t *abstract, const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata)
{
mares_common_device_t *device = (mares_common_device_t*) abstract;
mares_nemo_device_t *device = (mares_nemo_device_t*) abstract;
if (abstract && !device_is_mares_nemo (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
@ -311,6 +322,8 @@ mares_nemo_extract_dives (device_t *abstract, const unsigned char data[], unsign
if (size < PACKETSIZE)
return DEVICE_STATUS_ERROR;
unsigned char *fingerprint = (device ? device->fingerprint : NULL);
const mares_common_layout_t *layout = NULL;
switch (data[1]) {
case NEMO:
@ -328,5 +341,5 @@ mares_nemo_extract_dives (device_t *abstract, const unsigned char data[], unsign
if (size < layout->memsize)
return DEVICE_STATUS_ERROR;
return mares_common_extract_dives (device, layout, data, callback, userdata);
return mares_common_extract_dives (layout, fingerprint, data, callback, userdata);
}

View File

@ -47,8 +47,11 @@
typedef struct mares_puck_device_t {
mares_common_device_t base;
serial_t *port;
const mares_common_layout_t *layout;
unsigned char fingerprint[5];
} mares_puck_device_t;
static device_status_t mares_puck_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size);
static device_status_t mares_puck_device_read (device_t *abstract, unsigned int address, unsigned char data[], unsigned int size);
static device_status_t mares_puck_device_dump (device_t *abstract, dc_buffer_t *buffer);
static device_status_t mares_puck_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata);
@ -56,7 +59,7 @@ static device_status_t mares_puck_device_close (device_t *abstract);
static const device_backend_t mares_puck_device_backend = {
DEVICE_TYPE_MARES_PUCK,
mares_common_device_set_fingerprint, /* set_fingerprint */
mares_puck_device_set_fingerprint, /* set_fingerprint */
NULL, /* version */
mares_puck_device_read, /* read */
NULL, /* write */
@ -117,6 +120,8 @@ mares_puck_device_open (device_t **out, const char* name)
// Set the default values.
device->port = NULL;
device->layout = NULL;
memset (device->fingerprint, 0, sizeof (device->fingerprint));
// Open the device.
int rc = serial_open (&device->port, name);
@ -167,17 +172,17 @@ mares_puck_device_open (device_t **out, const char* name)
// Override the base class values.
switch (header[1]) {
case NEMOWIDE:
device->base.layout = &mares_nemowide_layout;
device->layout = &mares_nemowide_layout;
break;
case NEMOAIR:
case PUCKAIR:
device->base.layout = &mares_nemoair_layout;
device->layout = &mares_nemoair_layout;
break;
case PUCK:
device->base.layout = &mares_puck_layout;
device->layout = &mares_puck_layout;
break;
default: // Unknown, try puck
device->base.layout = &mares_puck_layout;
device->layout = &mares_puck_layout;
break;
}
@ -208,6 +213,23 @@ mares_puck_device_close (device_t *abstract)
}
static device_status_t
mares_puck_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size)
{
mares_puck_device_t *device = (mares_puck_device_t *) abstract;
if (size && size != sizeof (device->fingerprint))
return DEVICE_STATUS_ERROR;
if (size)
memcpy (device->fingerprint, data, sizeof (device->fingerprint));
else
memset (device->fingerprint, 0, sizeof (device->fingerprint));
return DEVICE_STATUS_SUCCESS;
}
static void
mares_puck_convert_binary_to_ascii (const unsigned char input[], unsigned int isize, unsigned char output[], unsigned int osize)
{
@ -386,7 +408,7 @@ mares_puck_device_read (device_t *abstract, unsigned int address, unsigned char
static device_status_t
mares_puck_device_dump (device_t *abstract, dc_buffer_t *buffer)
{
mares_common_device_t *device = (mares_common_device_t *) abstract;
mares_puck_device_t *device = (mares_puck_device_t *) abstract;
assert (device != NULL);
assert (device->layout != NULL);
@ -406,7 +428,7 @@ mares_puck_device_dump (device_t *abstract, dc_buffer_t *buffer)
static device_status_t
mares_puck_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
{
mares_common_device_t *device = (mares_common_device_t *) abstract;
mares_puck_device_t *device = (mares_puck_device_t *) abstract;
assert (device != NULL);
assert (device->layout != NULL);
@ -429,7 +451,7 @@ mares_puck_device_foreach (device_t *abstract, dive_callback_t callback, void *u
devinfo.serial = array_uint16_be (data + 8);
device_event_emit (abstract, DEVICE_EVENT_DEVINFO, &devinfo);
rc = mares_common_extract_dives (device, device->layout, data, callback, userdata);
rc = mares_common_extract_dives (device->layout, device->fingerprint, data, callback, userdata);
dc_buffer_free (buffer);
@ -440,7 +462,7 @@ mares_puck_device_foreach (device_t *abstract, dive_callback_t callback, void *u
device_status_t
mares_puck_extract_dives (device_t *abstract, const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata)
{
mares_common_device_t *device = (mares_common_device_t*) abstract;
mares_puck_device_t *device = (mares_puck_device_t*) abstract;
if (abstract && !device_is_mares_puck (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
@ -448,6 +470,8 @@ mares_puck_extract_dives (device_t *abstract, const unsigned char data[], unsign
if (size < PACKETSIZE)
return DEVICE_STATUS_ERROR;
unsigned char *fingerprint = (device ? device->fingerprint : NULL);
const mares_common_layout_t *layout = NULL;
switch (data[1]) {
case NEMOWIDE:
@ -468,5 +492,5 @@ mares_puck_extract_dives (device_t *abstract, const unsigned char data[], unsign
if (size < layout->memsize)
return DEVICE_STATUS_ERROR;
return mares_common_extract_dives (device, layout, data, callback, userdata);
return mares_common_extract_dives (layout, fingerprint, data, callback, userdata);
}