Add a common base class.
Introducing a common base class allows to share more code between the backends. Sharing the fingerprint data eliminates the need to pass it with a function parameter.
This commit is contained in:
parent
f642049fe2
commit
f0b3253296
@ -43,16 +43,12 @@
|
||||
rc == -1 ? DEVICE_STATUS_IO : DEVICE_STATUS_TIMEOUT \
|
||||
)
|
||||
|
||||
#define FP_OFFSET 0
|
||||
#define FP_SIZE 8
|
||||
|
||||
#define ACK 0x5A
|
||||
#define NAK 0xA5
|
||||
|
||||
typedef struct oceanic_atom2_device_t {
|
||||
device_t base;
|
||||
oceanic_common_device_t base;
|
||||
struct serial *port;
|
||||
unsigned char fingerprint[FP_SIZE];
|
||||
} oceanic_atom2_device_t;
|
||||
|
||||
static device_status_t oceanic_atom2_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size);
|
||||
@ -244,11 +240,10 @@ oceanic_atom2_device_open (device_t **out, const char* name)
|
||||
}
|
||||
|
||||
// Initialize the base class.
|
||||
device_init (&device->base, &oceanic_atom2_device_backend);
|
||||
oceanic_common_device_init (&device->base, &oceanic_atom2_device_backend);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
memset (device->fingerprint, 0, FP_SIZE);
|
||||
|
||||
// Open the device.
|
||||
int rc = serial_open (&device->port, name);
|
||||
@ -293,20 +288,12 @@ oceanic_atom2_device_open (device_t **out, const char* name)
|
||||
static device_status_t
|
||||
oceanic_atom2_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size)
|
||||
{
|
||||
oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract;
|
||||
oceanic_common_device_t *device = (oceanic_common_device_t*) abstract;
|
||||
|
||||
if (! device_is_oceanic_atom2 (abstract))
|
||||
return DEVICE_STATUS_TYPE_MISMATCH;
|
||||
|
||||
if (size && size != FP_SIZE)
|
||||
return DEVICE_STATUS_ERROR;
|
||||
|
||||
if (size)
|
||||
memcpy (device->fingerprint, data, FP_SIZE);
|
||||
else
|
||||
memset (device->fingerprint, 0, FP_SIZE);
|
||||
|
||||
return DEVICE_STATUS_SUCCESS;
|
||||
return oceanic_common_device_set_fingerprint (device, data, size);
|
||||
}
|
||||
|
||||
|
||||
@ -506,10 +493,10 @@ oceanic_atom2_device_dump (device_t *abstract, unsigned char data[], unsigned in
|
||||
static device_status_t
|
||||
oceanic_atom2_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
|
||||
{
|
||||
oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract;
|
||||
oceanic_common_device_t *device = (oceanic_common_device_t*) abstract;
|
||||
|
||||
if (! device_is_oceanic_atom2 (abstract))
|
||||
return DEVICE_STATUS_TYPE_MISMATCH;
|
||||
|
||||
return oceanic_common_device_foreach (abstract, &oceanic_atom2_layout, device->fingerprint, callback, userdata);
|
||||
return oceanic_common_device_foreach (device, &oceanic_atom2_layout, callback, userdata);
|
||||
}
|
||||
|
||||
@ -99,12 +99,43 @@ get_profile_last (const unsigned char data[], const oceanic_common_layout_t *lay
|
||||
}
|
||||
|
||||
|
||||
device_status_t
|
||||
oceanic_common_device_foreach (device_t *abstract, const oceanic_common_layout_t *layout, const unsigned char fingerprint[], dive_callback_t callback, void *userdata)
|
||||
void
|
||||
oceanic_common_device_init (oceanic_common_device_t *device, const device_backend_t *backend)
|
||||
{
|
||||
assert (device != NULL);
|
||||
|
||||
// Initialize the base class.
|
||||
device_init (&device->base, backend);
|
||||
|
||||
// Set the default values.
|
||||
memset (device->fingerprint, 0, sizeof (device->fingerprint));
|
||||
}
|
||||
|
||||
|
||||
device_status_t
|
||||
oceanic_common_device_set_fingerprint (oceanic_common_device_t *device, const unsigned char data[], unsigned int size)
|
||||
{
|
||||
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
|
||||
oceanic_common_device_foreach (oceanic_common_device_t *device, const oceanic_common_layout_t *layout, dive_callback_t callback, void *userdata)
|
||||
{
|
||||
device_t *abstract = (device_t *) device;
|
||||
|
||||
assert (abstract != NULL);
|
||||
assert (layout != NULL);
|
||||
assert (fingerprint != NULL);
|
||||
|
||||
// Enable progress notifications.
|
||||
device_progress_t progress = DEVICE_PROGRESS_INITIALIZER;
|
||||
@ -276,7 +307,7 @@ oceanic_common_device_foreach (device_t *abstract, const oceanic_common_layout_t
|
||||
current -= PAGESIZE / 2;
|
||||
|
||||
// Compare the fingerprint to identify previously downloaded entries.
|
||||
if (memcmp (logbooks + current, fingerprint, PAGESIZE / 2) == 0) {
|
||||
if (memcmp (logbooks + current, device->fingerprint, PAGESIZE / 2) == 0) {
|
||||
begin = current + PAGESIZE / 2;
|
||||
abort = 1;
|
||||
break;
|
||||
|
||||
@ -22,12 +22,17 @@
|
||||
#ifndef OCEANIC_COMMON_H
|
||||
#define OCEANIC_COMMON_H
|
||||
|
||||
#include "device.h"
|
||||
#include "device-private.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
typedef struct oceanic_common_device_t {
|
||||
device_t base;
|
||||
unsigned char fingerprint[8];
|
||||
} oceanic_common_device_t;
|
||||
|
||||
typedef struct oceanic_common_layout_t {
|
||||
// Device info.
|
||||
unsigned int cf_devinfo;
|
||||
@ -48,8 +53,14 @@ typedef struct oceanic_common_layout_t {
|
||||
unsigned int mode;
|
||||
} oceanic_common_layout_t;
|
||||
|
||||
void
|
||||
oceanic_common_device_init (oceanic_common_device_t *device, const device_backend_t *backend);
|
||||
|
||||
device_status_t
|
||||
oceanic_common_device_foreach (device_t *abstract, const oceanic_common_layout_t *layout, const unsigned char fingerprint[], dive_callback_t callback, void *userdata);
|
||||
oceanic_common_device_set_fingerprint (oceanic_common_device_t *device, const unsigned char data[], unsigned int size);
|
||||
|
||||
device_status_t
|
||||
oceanic_common_device_foreach (oceanic_common_device_t *device, const oceanic_common_layout_t *layout, dive_callback_t callback, void *userdata);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@ -43,17 +43,13 @@
|
||||
rc == -1 ? DEVICE_STATUS_IO : DEVICE_STATUS_TIMEOUT \
|
||||
)
|
||||
|
||||
#define FP_OFFSET 0
|
||||
#define FP_SIZE 8
|
||||
|
||||
#define ACK 0x5A
|
||||
#define NAK 0xA5
|
||||
|
||||
typedef struct oceanic_veo250_device_t {
|
||||
device_t base;
|
||||
oceanic_common_device_t base;
|
||||
struct serial *port;
|
||||
unsigned int last;
|
||||
unsigned char fingerprint[FP_SIZE];
|
||||
} oceanic_veo250_device_t;
|
||||
|
||||
static device_status_t oceanic_veo250_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size);
|
||||
@ -242,12 +238,11 @@ oceanic_veo250_device_open (device_t **out, const char* name)
|
||||
}
|
||||
|
||||
// Initialize the base class.
|
||||
device_init (&device->base, &oceanic_veo250_device_backend);
|
||||
oceanic_common_device_init (&device->base, &oceanic_veo250_device_backend);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
device->last = 0;
|
||||
memset (device->fingerprint, 0, FP_SIZE);
|
||||
|
||||
// Open the device.
|
||||
int rc = serial_open (&device->port, name);
|
||||
@ -302,20 +297,12 @@ oceanic_veo250_device_open (device_t **out, const char* name)
|
||||
static device_status_t
|
||||
oceanic_veo250_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size)
|
||||
{
|
||||
oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract;
|
||||
oceanic_common_device_t *device = (oceanic_common_device_t*) abstract;
|
||||
|
||||
if (! device_is_oceanic_veo250 (abstract))
|
||||
return DEVICE_STATUS_TYPE_MISMATCH;
|
||||
|
||||
if (size && size != FP_SIZE)
|
||||
return DEVICE_STATUS_ERROR;
|
||||
|
||||
if (size)
|
||||
memcpy (device->fingerprint, data, FP_SIZE);
|
||||
else
|
||||
memset (device->fingerprint, 0, FP_SIZE);
|
||||
|
||||
return DEVICE_STATUS_SUCCESS;
|
||||
return oceanic_common_device_set_fingerprint (device, data, size);
|
||||
}
|
||||
|
||||
|
||||
@ -483,10 +470,10 @@ oceanic_veo250_device_dump (device_t *abstract, unsigned char data[], unsigned i
|
||||
static device_status_t
|
||||
oceanic_veo250_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
|
||||
{
|
||||
oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract;
|
||||
oceanic_common_device_t *device = (oceanic_common_device_t*) abstract;
|
||||
|
||||
if (! device_is_oceanic_veo250 (abstract))
|
||||
return DEVICE_STATUS_TYPE_MISMATCH;
|
||||
|
||||
return oceanic_common_device_foreach (abstract, &oceanic_veo250_layout, device->fingerprint, callback, userdata);
|
||||
return oceanic_common_device_foreach (device, &oceanic_veo250_layout, callback, userdata);
|
||||
}
|
||||
|
||||
@ -43,16 +43,12 @@
|
||||
rc == -1 ? DEVICE_STATUS_IO : DEVICE_STATUS_TIMEOUT \
|
||||
)
|
||||
|
||||
#define FP_OFFSET 0
|
||||
#define FP_SIZE 8
|
||||
|
||||
#define ACK 0x5A
|
||||
#define NAK 0xA5
|
||||
|
||||
typedef struct oceanic_vtpro_device_t {
|
||||
device_t base;
|
||||
oceanic_common_device_t base;
|
||||
struct serial *port;
|
||||
unsigned char fingerprint[FP_SIZE];
|
||||
} oceanic_vtpro_device_t;
|
||||
|
||||
static device_status_t oceanic_vtpro_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size);
|
||||
@ -236,11 +232,10 @@ oceanic_vtpro_device_open (device_t **out, const char* name)
|
||||
}
|
||||
|
||||
// Initialize the base class.
|
||||
device_init (&device->base, &oceanic_vtpro_device_backend);
|
||||
oceanic_common_device_init (&device->base, &oceanic_vtpro_device_backend);
|
||||
|
||||
// Set the default values.
|
||||
device->port = NULL;
|
||||
memset (device->fingerprint, 0, FP_SIZE);
|
||||
|
||||
// Open the device.
|
||||
int rc = serial_open (&device->port, name);
|
||||
@ -319,20 +314,12 @@ oceanic_vtpro_device_close (device_t *abstract)
|
||||
static device_status_t
|
||||
oceanic_vtpro_device_set_fingerprint (device_t *abstract, const unsigned char data[], unsigned int size)
|
||||
{
|
||||
oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
|
||||
oceanic_common_device_t *device = (oceanic_common_device_t*) abstract;
|
||||
|
||||
if (! device_is_oceanic_vtpro (abstract))
|
||||
return DEVICE_STATUS_TYPE_MISMATCH;
|
||||
|
||||
if (size && size != FP_SIZE)
|
||||
return DEVICE_STATUS_ERROR;
|
||||
|
||||
if (size)
|
||||
memcpy (device->fingerprint, data, FP_SIZE);
|
||||
else
|
||||
memset (device->fingerprint, 0, FP_SIZE);
|
||||
|
||||
return DEVICE_STATUS_SUCCESS;
|
||||
return oceanic_common_device_set_fingerprint (device, data, size);
|
||||
}
|
||||
|
||||
device_status_t
|
||||
@ -564,10 +551,10 @@ oceanic_vtpro_device_dump (device_t *abstract, unsigned char data[], unsigned in
|
||||
static device_status_t
|
||||
oceanic_vtpro_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
|
||||
{
|
||||
oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract;
|
||||
oceanic_common_device_t *device = (oceanic_common_device_t*) abstract;
|
||||
|
||||
if (! device_is_oceanic_vtpro (abstract))
|
||||
return DEVICE_STATUS_TYPE_MISMATCH;
|
||||
|
||||
return oceanic_common_device_foreach (abstract, &oceanic_vtpro_layout, device->fingerprint, callback, userdata);
|
||||
return oceanic_common_device_foreach (device, &oceanic_vtpro_layout, callback, userdata);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user