From 6419e189a4bfa108b7bb0e5e92ca69d547af25b1 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Thu, 3 Jan 2013 10:24:39 +0100 Subject: [PATCH] Add a single isinstance function per object type. Currently, each backend has it's own function to verify whether the object vtable pointer is the expected one. All these functions can be removed in favor of a single isintance function in the base class, which takes the expected vtable pointer as a parameter. Functions which are called through the vtable, don't need to verify the vtable pointer, and those checks are removed. --- src/atomics_cobalt.c | 25 ++++++----------------- src/atomics_cobalt_parser.c | 20 +++--------------- src/cressi_edy.c | 20 ++---------------- src/cressi_edy_parser.c | 18 ++-------------- src/cressi_leonardo.c | 13 +++--------- src/cressi_leonardo_parser.c | 18 ++-------------- src/device-private.h | 3 +++ src/device.c | 11 ++++++++++ src/hw_frog.c | 20 ++++++------------ src/hw_ostc.c | 32 ++++++++--------------------- src/hw_ostc_parser.c | 18 ++-------------- src/mares_darwin.c | 12 +++-------- src/mares_darwin_parser.c | 15 ++------------ src/mares_iconhd.c | 16 +++------------ src/mares_iconhd_parser.c | 15 ++------------ src/mares_nemo.c | 16 +++------------ src/mares_nemo_parser.c | 15 ++------------ src/mares_puck.c | 13 +++--------- src/oceanic_atom2.c | 25 ++++------------------- src/oceanic_atom2_parser.c | 21 ++----------------- src/oceanic_veo250.c | 22 ++++---------------- src/oceanic_veo250_parser.c | 21 ++----------------- src/oceanic_vtpro.c | 21 ++++--------------- src/oceanic_vtpro_parser.c | 21 ++----------------- src/parser-private.h | 3 +++ src/parser.c | 10 +++++++++ src/reefnet_sensus.c | 27 ++++-------------------- src/reefnet_sensus_parser.c | 23 +++------------------ src/reefnet_sensuspro.c | 29 +++++--------------------- src/reefnet_sensuspro_parser.c | 23 +++------------------ src/reefnet_sensusultra.c | 35 ++++++++------------------------ src/reefnet_sensusultra_parser.c | 23 +++------------------ src/shearwater_predator.c | 14 +++---------- src/shearwater_predator_parser.c | 18 ++-------------- src/suunto_d9.c | 18 ++++------------ src/suunto_d9_parser.c | 21 ++----------------- src/suunto_eon.c | 24 +++++----------------- src/suunto_eon_parser.c | 21 ++----------------- src/suunto_solution.c | 22 +++----------------- src/suunto_solution_parser.c | 21 ++----------------- src/suunto_vyper.c | 31 ++++------------------------ src/suunto_vyper2.c | 18 ++++------------ src/suunto_vyper_parser.c | 21 ++----------------- src/uwatec_aladin.c | 25 +++-------------------- src/uwatec_memomouse.c | 25 +++-------------------- src/uwatec_memomouse_parser.c | 21 ++----------------- src/uwatec_smart.c | 25 +++-------------------- src/uwatec_smart_parser.c | 21 ++----------------- src/zeagle_n2ition3.c | 20 ++---------------- 49 files changed, 172 insertions(+), 798 deletions(-) diff --git a/src/atomics_cobalt.c b/src/atomics_cobalt.c index c23bf5f..1ee9018 100644 --- a/src/atomics_cobalt.c +++ b/src/atomics_cobalt.c @@ -40,6 +40,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &atomics_cobalt_device_vtable) + #define EXITCODE(rc) (rc == LIBUSB_ERROR_TIMEOUT ? DC_STATUS_TIMEOUT : DC_STATUS_IO) #define VID 0x0471 @@ -76,15 +78,6 @@ static const dc_device_vtable_t atomics_cobalt_device_vtable = { atomics_cobalt_device_close /* close */ }; -static int -device_is_atomics_cobalt (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &atomics_cobalt_device_vtable; -} - dc_status_t atomics_cobalt_device_open (dc_device_t **out, dc_context_t *context) @@ -156,9 +149,6 @@ atomics_cobalt_device_close (dc_device_t *abstract) { atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract; - if (! device_is_atomics_cobalt (abstract)) - return DC_STATUS_INVALIDARGS; - #ifdef HAVE_LIBUSB libusb_release_interface(device->handle, 0); libusb_close (device->handle); @@ -177,9 +167,6 @@ atomics_cobalt_device_set_fingerprint (dc_device_t *abstract, const unsigned cha { atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract; - if (! device_is_atomics_cobalt (abstract)) - return DC_STATUS_INVALIDARGS; - if (size && size != sizeof (device->fingerprint)) return DC_STATUS_INVALIDARGS; @@ -197,7 +184,7 @@ atomics_cobalt_device_set_simulation (dc_device_t *abstract, unsigned int simula { atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract; - if (! device_is_atomics_cobalt (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; device->simulation = simulation; @@ -211,6 +198,9 @@ atomics_cobalt_device_version (dc_device_t *abstract, unsigned char data[], unsi { atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract; + if (!ISINSTANCE (abstract)) + return DC_STATUS_INVALIDARGS; + if (size < SZ_VERSION) return DC_STATUS_INVALIDARGS; @@ -350,9 +340,6 @@ atomics_cobalt_device_foreach (dc_device_t *abstract, dc_dive_callback_t callbac { atomics_cobalt_device_t *device = (atomics_cobalt_device_t *) abstract; - if (! device_is_atomics_cobalt (abstract)) - return DC_STATUS_INVALIDARGS; - // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = SZ_MEMORY + 2; diff --git a/src/atomics_cobalt_parser.c b/src/atomics_cobalt_parser.c index 94cf020..108e381 100644 --- a/src/atomics_cobalt_parser.c +++ b/src/atomics_cobalt_parser.c @@ -28,6 +28,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &atomics_cobalt_parser_vtable) + #define SZ_HEADER 228 #define SZ_GASMIX 18 #define SZ_GASSWITCH 6 @@ -58,16 +60,6 @@ static const dc_parser_vtable_t atomics_cobalt_parser_vtable = { }; -static int -parser_is_atomics_cobalt (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &atomics_cobalt_parser_vtable; -} - - dc_status_t atomics_cobalt_parser_create (dc_parser_t **out, dc_context_t *context) { @@ -97,9 +89,6 @@ atomics_cobalt_parser_create (dc_parser_t **out, dc_context_t *context) static dc_status_t atomics_cobalt_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_atomics_cobalt (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -110,9 +99,6 @@ atomics_cobalt_parser_destroy (dc_parser_t *abstract) static dc_status_t atomics_cobalt_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) { - if (! parser_is_atomics_cobalt (abstract)) - return DC_STATUS_INVALIDARGS; - return DC_STATUS_SUCCESS; } @@ -122,7 +108,7 @@ atomics_cobalt_parser_set_calibration (dc_parser_t *abstract, double atmospheric { atomics_cobalt_parser_t *parser = (atomics_cobalt_parser_t*) abstract; - if (! parser_is_atomics_cobalt (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; parser->atmospheric = atmospheric; diff --git a/src/cressi_edy.c b/src/cressi_edy.c index 844bc29..0de977a 100644 --- a/src/cressi_edy.c +++ b/src/cressi_edy.c @@ -32,6 +32,8 @@ #include "array.h" #include "ringbuffer.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &cressi_edy_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -73,15 +75,6 @@ static const dc_device_vtable_t cressi_edy_device_vtable = { cressi_edy_device_close /* close */ }; -static int -device_is_cressi_edy (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &cressi_edy_device_vtable; -} - static dc_status_t cressi_edy_transfer (cressi_edy_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, int trailer) @@ -252,9 +245,6 @@ cressi_edy_device_close (dc_device_t *abstract) { cressi_edy_device_t *device = (cressi_edy_device_t*) abstract; - if (! device_is_cressi_edy (abstract)) - return DC_STATUS_INVALIDARGS; - // Send the quit command. cressi_edy_quit (device); @@ -276,9 +266,6 @@ cressi_edy_device_read (dc_device_t *abstract, unsigned int address, unsigned ch { cressi_edy_device_t *device = (cressi_edy_device_t*) abstract; - if (! device_is_cressi_edy (abstract)) - return DC_STATUS_INVALIDARGS; - if ((address % SZ_PAGE != 0) || (size % SZ_PACKET != 0)) return DC_STATUS_INVALIDARGS; @@ -326,9 +313,6 @@ cressi_edy_device_set_fingerprint (dc_device_t *abstract, const unsigned char da static dc_status_t cressi_edy_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { - if (! device_is_cressi_edy (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { diff --git a/src/cressi_edy_parser.c b/src/cressi_edy_parser.c index 3642dc4..3886c8e 100644 --- a/src/cressi_edy_parser.c +++ b/src/cressi_edy_parser.c @@ -27,6 +27,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &cressi_edy_parser_vtable) + typedef struct cressi_edy_parser_t cressi_edy_parser_t; struct cressi_edy_parser_t { @@ -50,16 +52,6 @@ static const dc_parser_vtable_t cressi_edy_parser_vtable = { }; -static int -parser_is_cressi_edy (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &cressi_edy_parser_vtable; -} - - dc_status_t cressi_edy_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model) { @@ -88,9 +80,6 @@ cressi_edy_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int static dc_status_t cressi_edy_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_cressi_edy (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -101,9 +90,6 @@ cressi_edy_parser_destroy (dc_parser_t *abstract) static dc_status_t cressi_edy_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) { - if (! parser_is_cressi_edy (abstract)) - return DC_STATUS_INVALIDARGS; - return DC_STATUS_SUCCESS; } diff --git a/src/cressi_leonardo.c b/src/cressi_leonardo.c index a1f5066..fa29310 100644 --- a/src/cressi_leonardo.c +++ b/src/cressi_leonardo.c @@ -32,6 +32,8 @@ #include "array.h" #include "ringbuffer.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &cressi_leonardo_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -69,15 +71,6 @@ static const dc_device_vtable_t cressi_leonardo_device_vtable = { cressi_leonardo_device_close /* close */ }; -static int -device_is_cressi_leonardo (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &cressi_leonardo_device_vtable; -} - dc_status_t cressi_leonardo_device_open (dc_device_t **out, dc_context_t *context, const char *name) { @@ -289,7 +282,7 @@ cressi_leonardo_extract_dives (dc_device_t *abstract, const unsigned char data[] cressi_leonardo_device_t *device = (cressi_leonardo_device_t *) abstract; dc_context_t *context = (abstract ? abstract->context : NULL); - if (abstract && !device_is_cressi_leonardo (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_MEMORY) diff --git a/src/cressi_leonardo_parser.c b/src/cressi_leonardo_parser.c index 858d1e5..0291366 100644 --- a/src/cressi_leonardo_parser.c +++ b/src/cressi_leonardo_parser.c @@ -27,6 +27,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_device_isinstance((parser), &cressi_leonardo_parser_vtable) + #define SZ_HEADER 82 typedef struct cressi_leonardo_parser_t cressi_leonardo_parser_t; @@ -51,16 +53,6 @@ static const dc_parser_vtable_t cressi_leonardo_parser_vtable = { }; -static int -parser_is_cressi_leonardo (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &cressi_leonardo_parser_vtable; -} - - dc_status_t cressi_leonardo_parser_create (dc_parser_t **out, dc_context_t *context) { @@ -86,9 +78,6 @@ cressi_leonardo_parser_create (dc_parser_t **out, dc_context_t *context) static dc_status_t cressi_leonardo_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_cressi_leonardo (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -99,9 +88,6 @@ cressi_leonardo_parser_destroy (dc_parser_t *abstract) static dc_status_t cressi_leonardo_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) { - if (! parser_is_cressi_leonardo (abstract)) - return DC_STATUS_INVALIDARGS; - return DC_STATUS_SUCCESS; } diff --git a/src/device-private.h b/src/device-private.h index 497c319..7ac530e 100644 --- a/src/device-private.h +++ b/src/device-private.h @@ -70,6 +70,9 @@ struct dc_device_vtable_t { dc_status_t (*close) (dc_device_t *device); }; +int +dc_device_isinstance (dc_device_t *device, const dc_device_vtable_t *vtable); + void device_init (dc_device_t *device, dc_context_t *context, const dc_device_vtable_t *vtable); diff --git a/src/device.c b/src/device.c index e2b73eb..4b76a3c 100644 --- a/src/device.c +++ b/src/device.c @@ -150,6 +150,17 @@ dc_device_open (dc_device_t **out, dc_context_t *context, dc_descriptor_t *descr return rc; } + +int +dc_device_isinstance (dc_device_t *device, const dc_device_vtable_t *vtable) +{ + if (device == NULL) + return 0; + + return device->vtable == vtable; +} + + dc_family_t dc_device_get_type (dc_device_t *device) { diff --git a/src/hw_frog.c b/src/hw_frog.c index 5bef907..0ae1dc0 100644 --- a/src/hw_frog.c +++ b/src/hw_frog.c @@ -31,6 +31,8 @@ #include "ringbuffer.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &hw_frog_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -78,16 +80,6 @@ static const dc_device_vtable_t hw_frog_device_vtable = { }; -static int -device_is_hw_frog (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &hw_frog_device_vtable; -} - - static dc_status_t hw_frog_transfer (hw_frog_device_t *device, dc_event_progress_t *progress, @@ -297,7 +289,7 @@ hw_frog_device_version (dc_device_t *abstract, unsigned char data[], unsigned in { hw_frog_device_t *device = (hw_frog_device_t *) abstract; - if (!device_is_hw_frog (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size != SZ_VERSION) @@ -478,7 +470,7 @@ hw_frog_device_clock (dc_device_t *abstract, const dc_datetime_t *datetime) { hw_frog_device_t *device = (hw_frog_device_t *) abstract; - if (!device_is_hw_frog (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (datetime == NULL) { @@ -503,7 +495,7 @@ hw_frog_device_display (dc_device_t *abstract, const char *text) { hw_frog_device_t *device = (hw_frog_device_t *) abstract; - if (!device_is_hw_frog (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Check the maximum length. @@ -533,7 +525,7 @@ hw_frog_device_customtext (dc_device_t *abstract, const char *text) { hw_frog_device_t *device = (hw_frog_device_t *) abstract; - if (!device_is_hw_frog (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Check the maximum length. diff --git a/src/hw_ostc.c b/src/hw_ostc.c index f3dc98f..f95601d 100644 --- a/src/hw_ostc.c +++ b/src/hw_ostc.c @@ -30,6 +30,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &hw_ostc_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -70,16 +72,6 @@ static const dc_device_vtable_t hw_ostc_device_vtable = { }; -static int -device_is_hw_ostc (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &hw_ostc_device_vtable; -} - - static dc_status_t hw_ostc_send (hw_ostc_device_t *device, unsigned char cmd, unsigned int echo) { @@ -173,9 +165,6 @@ hw_ostc_device_close (dc_device_t *abstract) { hw_ostc_device_t *device = (hw_ostc_device_t*) abstract; - if (! device_is_hw_ostc (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -211,9 +200,6 @@ hw_ostc_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { hw_ostc_device_t *device = (hw_ostc_device_t*) abstract; - if (! device_is_hw_ostc (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); @@ -346,7 +332,7 @@ hw_ostc_device_md2hash (dc_device_t *abstract, unsigned char data[], unsigned in { hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; - if (! device_is_hw_ostc (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_MD2HASH) { @@ -375,7 +361,7 @@ hw_ostc_device_clock (dc_device_t *abstract, const dc_datetime_t *datetime) { hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; - if (! device_is_hw_ostc (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (datetime == NULL) { @@ -407,7 +393,7 @@ hw_ostc_device_eeprom_read (dc_device_t *abstract, unsigned int bank, unsigned c { hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; - if (! device_is_hw_ostc (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (bank > 2) { @@ -442,7 +428,7 @@ hw_ostc_device_eeprom_write (dc_device_t *abstract, unsigned int bank, const uns { hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; - if (! device_is_hw_ostc (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (bank > 2) { @@ -477,7 +463,7 @@ hw_ostc_device_reset (dc_device_t *abstract) { hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; - if (! device_is_hw_ostc (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Send the command. @@ -494,7 +480,7 @@ hw_ostc_device_screenshot (dc_device_t *abstract, dc_buffer_t *buffer, hw_ostc_f { hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; - if (! device_is_hw_ostc (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Erase the current contents of the buffer. @@ -629,7 +615,7 @@ hw_ostc_extract_dives (dc_device_t *abstract, const unsigned char data[], unsign { hw_ostc_device_t *device = (hw_ostc_device_t *) abstract; - if (abstract && !device_is_hw_ostc (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; const unsigned char header[2] = {0xFA, 0xFA}; diff --git a/src/hw_ostc_parser.c b/src/hw_ostc_parser.c index 47e0886..34c43e8 100644 --- a/src/hw_ostc_parser.c +++ b/src/hw_ostc_parser.c @@ -28,6 +28,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &hw_ostc_parser_vtable) + #define NINFO 6 typedef struct hw_ostc_parser_t hw_ostc_parser_t; @@ -58,16 +60,6 @@ static const dc_parser_vtable_t hw_ostc_parser_vtable = { }; -static int -parser_is_hw_ostc (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &hw_ostc_parser_vtable; -} - - dc_status_t hw_ostc_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int frog) { @@ -95,9 +87,6 @@ hw_ostc_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int fr static dc_status_t hw_ostc_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_hw_ostc (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -108,9 +97,6 @@ hw_ostc_parser_destroy (dc_parser_t *abstract) static dc_status_t hw_ostc_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) { - if (! parser_is_hw_ostc (abstract)) - return DC_STATUS_INVALIDARGS; - return DC_STATUS_SUCCESS; } diff --git a/src/mares_darwin.c b/src/mares_darwin.c index 93c37bd..5c2513b 100644 --- a/src/mares_darwin.c +++ b/src/mares_darwin.c @@ -31,6 +31,8 @@ #include "mares_common.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &mares_darwin_device_vtable) + #define DARWIN 0 #define DARWINAIR 1 @@ -90,14 +92,6 @@ static const mares_darwin_layout_t mares_darwinair_layout = { 3 /* samplesize */ }; -static int -device_is_mares_darwin (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &mares_darwin_device_vtable; -} dc_status_t mares_darwin_device_open (dc_device_t **out, dc_context_t *context, const char *name, unsigned int model) @@ -263,7 +257,7 @@ mares_darwin_extract_dives (dc_device_t *abstract, const unsigned char data[], u { mares_darwin_device_t *device = (mares_darwin_device_t *) abstract; - if (!device_is_mares_darwin (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; assert (device->layout != NULL); diff --git a/src/mares_darwin_parser.c b/src/mares_darwin_parser.c index 6b40fe8..4235f40 100644 --- a/src/mares_darwin_parser.c +++ b/src/mares_darwin_parser.c @@ -29,6 +29,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &mares_darwin_parser_vtable) + #define DARWIN 0 #define DARWINAIR 1 @@ -56,16 +58,6 @@ static const dc_parser_vtable_t mares_darwin_parser_vtable = { }; -static int -parser_is_mares_darwin (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &mares_darwin_parser_vtable; -} - - dc_status_t mares_darwin_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model) { @@ -99,9 +91,6 @@ mares_darwin_parser_create (dc_parser_t **out, dc_context_t *context, unsigned i static dc_status_t mares_darwin_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_mares_darwin (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); diff --git a/src/mares_iconhd.c b/src/mares_iconhd.c index a1042ac..5105e26 100644 --- a/src/mares_iconhd.c +++ b/src/mares_iconhd.c @@ -30,6 +30,8 @@ #include "serial.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &mares_iconhd_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -80,15 +82,6 @@ static const dc_device_vtable_t mares_iconhd_device_vtable = { mares_iconhd_device_close /* close */ }; -static int -device_is_mares_iconhd (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &mares_iconhd_device_vtable; -} - static unsigned int mares_iconhd_get_model (mares_iconhd_device_t *device, unsigned int model) @@ -323,9 +316,6 @@ mares_iconhd_device_close (dc_device_t *abstract) { mares_iconhd_device_t *device = (mares_iconhd_device_t*) abstract; - if (! device_is_mares_iconhd (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -431,7 +421,7 @@ 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 && !device_is_mares_iconhd (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_MEMORY) diff --git a/src/mares_iconhd_parser.c b/src/mares_iconhd_parser.c index 5dded02..9d7bb75 100644 --- a/src/mares_iconhd_parser.c +++ b/src/mares_iconhd_parser.c @@ -27,6 +27,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &mares_iconhd_parser_vtable) + #define ICONHD 0x14 #define ICONHDNET 0x15 @@ -53,16 +55,6 @@ static const dc_parser_vtable_t mares_iconhd_parser_vtable = { }; -static int -parser_is_mares_iconhd (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &mares_iconhd_parser_vtable; -} - - dc_status_t mares_iconhd_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model) { @@ -91,9 +83,6 @@ mares_iconhd_parser_create (dc_parser_t **out, dc_context_t *context, unsigned i static dc_status_t mares_iconhd_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_mares_iconhd (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); diff --git a/src/mares_nemo.c b/src/mares_nemo.c index 9bf1837..c2052ea 100644 --- a/src/mares_nemo.c +++ b/src/mares_nemo.c @@ -31,6 +31,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &mares_nemo_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -84,15 +86,6 @@ static const mares_common_layout_t mares_nemo_apneist_layout = { 0x4000 /* rb_freedives_end */ }; -static int -device_is_mares_nemo (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &mares_nemo_device_vtable; -} - dc_status_t mares_nemo_device_open (dc_device_t **out, dc_context_t *context, const char *name) @@ -162,9 +155,6 @@ mares_nemo_device_close (dc_device_t *abstract) { mares_nemo_device_t *device = (mares_nemo_device_t*) abstract; - if (! device_is_mares_nemo (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -320,7 +310,7 @@ mares_nemo_extract_dives (dc_device_t *abstract, const unsigned char data[], uns { mares_nemo_device_t *device = (mares_nemo_device_t*) abstract; - if (abstract && !device_is_mares_nemo (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < PACKETSIZE) diff --git a/src/mares_nemo_parser.c b/src/mares_nemo_parser.c index 769072f..5eee1f3 100644 --- a/src/mares_nemo_parser.c +++ b/src/mares_nemo_parser.c @@ -29,6 +29,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &mares_nemo_parser_vtable) + #define NEMO 0 #define NEMOWIDE 1 #define NEMOAIR 4 @@ -68,16 +70,6 @@ static const dc_parser_vtable_t mares_nemo_parser_vtable = { }; -static int -parser_is_mares_nemo (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &mares_nemo_parser_vtable; -} - - dc_status_t mares_nemo_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model) { @@ -118,9 +110,6 @@ mares_nemo_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int static dc_status_t mares_nemo_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_mares_nemo (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); diff --git a/src/mares_puck.c b/src/mares_puck.c index 6cd25a4..b989b9a 100644 --- a/src/mares_puck.c +++ b/src/mares_puck.c @@ -32,6 +32,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &mares_puck_device_vtable) + #define NEMOWIDE 1 #define NEMOAIR 4 #define PUCK 7 @@ -82,15 +84,6 @@ static const mares_common_layout_t mares_nemowide_layout = { 0x4000 /* rb_freedives_end */ }; -static int -device_is_mares_puck (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &mares_puck_device_vtable; -} - dc_status_t mares_puck_device_open (dc_device_t **out, dc_context_t *context, const char *name) @@ -273,7 +266,7 @@ mares_puck_extract_dives (dc_device_t *abstract, const unsigned char data[], uns { mares_puck_device_t *device = (mares_puck_device_t*) abstract; - if (abstract && !device_is_mares_puck (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < PACKETSIZE) diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index bc2530f..137a34b 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -32,6 +32,8 @@ #include "ringbuffer.h" #include "checksum.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &oceanic_atom2_device_vtable) + #define MAXRETRIES 2 #define EXITCODE(rc) \ @@ -240,16 +242,6 @@ static const oceanic_common_layout_t oceanic_veo1_layout = { }; -static int -device_is_oceanic_atom2 (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &oceanic_atom2_device_vtable; -} - - static dc_status_t oceanic_atom2_send (oceanic_atom2_device_t *device, const unsigned char command[], unsigned int csize, unsigned char ack) { @@ -441,9 +433,6 @@ oceanic_atom2_device_close (dc_device_t *abstract) { oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract; - if (! device_is_oceanic_atom2 (abstract)) - return DC_STATUS_INVALIDARGS; - // Send the quit command. oceanic_atom2_quit (device); @@ -465,7 +454,7 @@ oceanic_atom2_device_keepalive (dc_device_t *abstract) { oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract; - if (! device_is_oceanic_atom2 (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Send the command to the dive computer. @@ -483,7 +472,7 @@ oceanic_atom2_device_version (dc_device_t *abstract, unsigned char data[], unsig { oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract; - if (! device_is_oceanic_atom2 (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < PAGESIZE) @@ -506,9 +495,6 @@ oceanic_atom2_device_read (dc_device_t *abstract, unsigned int address, unsigned { oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract; - if (! device_is_oceanic_atom2 (abstract)) - return DC_STATUS_INVALIDARGS; - if ((address % PAGESIZE != 0) || (size % PAGESIZE != 0)) return DC_STATUS_INVALIDARGS; @@ -542,9 +528,6 @@ oceanic_atom2_device_write (dc_device_t *abstract, unsigned int address, const u { oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract; - if (! device_is_oceanic_atom2 (abstract)) - return DC_STATUS_INVALIDARGS; - if ((address % PAGESIZE != 0) || (size % PAGESIZE != 0)) return DC_STATUS_INVALIDARGS; diff --git a/src/oceanic_atom2_parser.c b/src/oceanic_atom2_parser.c index 73e8d2c..1694d48 100644 --- a/src/oceanic_atom2_parser.c +++ b/src/oceanic_atom2_parser.c @@ -29,6 +29,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &oceanic_atom2_parser_vtable) + #define ATOM1 0x4250 #define EPICA 0x4257 #define VT3 0x4258 @@ -81,16 +83,6 @@ static const dc_parser_vtable_t oceanic_atom2_parser_vtable = { }; -static int -parser_is_oceanic_atom2 (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &oceanic_atom2_parser_vtable; -} - - dc_status_t oceanic_atom2_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model) { @@ -122,9 +114,6 @@ oceanic_atom2_parser_create (dc_parser_t **out, dc_context_t *context, unsigned static dc_status_t oceanic_atom2_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_oceanic_atom2 (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -137,9 +126,6 @@ oceanic_atom2_parser_set_data (dc_parser_t *abstract, const unsigned char *data, { oceanic_atom2_parser_t *parser = (oceanic_atom2_parser_t *) abstract; - if (! parser_is_oceanic_atom2 (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -358,9 +344,6 @@ oceanic_atom2_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_ { oceanic_atom2_parser_t *parser = (oceanic_atom2_parser_t *) abstract; - if (! parser_is_oceanic_atom2 (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/oceanic_veo250.c b/src/oceanic_veo250.c index 2e73cd4..1e9f91d 100644 --- a/src/oceanic_veo250.c +++ b/src/oceanic_veo250.c @@ -31,6 +31,8 @@ #include "ringbuffer.h" #include "checksum.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &oceanic_veo250_device_vtable) + #define MAXRETRIES 2 #define MULTIPAGE 4 @@ -85,16 +87,6 @@ static const oceanic_common_layout_t oceanic_veo250_layout = { }; -static int -device_is_oceanic_veo250 (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &oceanic_veo250_device_vtable; -} - - static dc_status_t oceanic_veo250_send (oceanic_veo250_device_t *device, const unsigned char command[], unsigned int csize) { @@ -322,9 +314,6 @@ oceanic_veo250_device_close (dc_device_t *abstract) { oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; - if (! device_is_oceanic_veo250 (abstract)) - return DC_STATUS_INVALIDARGS; - // Switch the device back to surface mode. oceanic_veo250_quit (device); @@ -346,7 +335,7 @@ oceanic_veo250_device_keepalive (dc_device_t *abstract) { oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; - if (! device_is_oceanic_veo250 (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; unsigned char answer[2] = {0}; @@ -373,7 +362,7 @@ oceanic_veo250_device_version (dc_device_t *abstract, unsigned char data[], unsi { oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; - if (! device_is_oceanic_veo250 (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < PAGESIZE) @@ -404,9 +393,6 @@ oceanic_veo250_device_read (dc_device_t *abstract, unsigned int address, unsigne { oceanic_veo250_device_t *device = (oceanic_veo250_device_t*) abstract; - if (! device_is_oceanic_veo250 (abstract)) - return DC_STATUS_INVALIDARGS; - if ((address % PAGESIZE != 0) || (size % PAGESIZE != 0)) return DC_STATUS_INVALIDARGS; diff --git a/src/oceanic_veo250_parser.c b/src/oceanic_veo250_parser.c index 6044d39..baffc4c 100644 --- a/src/oceanic_veo250_parser.c +++ b/src/oceanic_veo250_parser.c @@ -29,6 +29,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &oceanic_veo250_parser_vtable) + typedef struct oceanic_veo250_parser_t oceanic_veo250_parser_t; struct oceanic_veo250_parser_t { @@ -56,16 +58,6 @@ static const dc_parser_vtable_t oceanic_veo250_parser_vtable = { }; -static int -parser_is_oceanic_veo250 (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &oceanic_veo250_parser_vtable; -} - - dc_status_t oceanic_veo250_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model) { @@ -97,9 +89,6 @@ oceanic_veo250_parser_create (dc_parser_t **out, dc_context_t *context, unsigned static dc_status_t oceanic_veo250_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_oceanic_veo250 (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -112,9 +101,6 @@ oceanic_veo250_parser_set_data (dc_parser_t *abstract, const unsigned char *data { oceanic_veo250_parser_t *parser = (oceanic_veo250_parser_t *) abstract; - if (! parser_is_oceanic_veo250 (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -208,9 +194,6 @@ oceanic_veo250_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, un static dc_status_t oceanic_veo250_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) { - if (! parser_is_oceanic_veo250 (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/oceanic_vtpro.c b/src/oceanic_vtpro.c index e98314a..44a925a 100644 --- a/src/oceanic_vtpro.c +++ b/src/oceanic_vtpro.c @@ -31,6 +31,8 @@ #include "ringbuffer.h" #include "checksum.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &oceanic_vtpro_device_vtable) + #define MAXRETRIES 2 #define MULTIPAGE 4 @@ -98,15 +100,6 @@ static const oceanic_common_layout_t oceanic_wisdom_layout = { 0 /* pt_mode_logbook */ }; -static int -device_is_oceanic_vtpro (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &oceanic_vtpro_device_vtable; -} - static dc_status_t oceanic_vtpro_send (oceanic_vtpro_device_t *device, const unsigned char command[], unsigned int csize) @@ -364,9 +357,6 @@ oceanic_vtpro_device_close (dc_device_t *abstract) { oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract; - if (! device_is_oceanic_vtpro (abstract)) - return DC_STATUS_INVALIDARGS; - // Switch the device back to surface mode. oceanic_vtpro_quit (device); @@ -388,7 +378,7 @@ oceanic_vtpro_device_keepalive (dc_device_t *abstract) { oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract; - if (! device_is_oceanic_vtpro (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Send the command to the dive computer. @@ -413,7 +403,7 @@ oceanic_vtpro_device_version (dc_device_t *abstract, unsigned char data[], unsig { oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract; - if (! device_is_oceanic_vtpro (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < PAGESIZE) @@ -474,9 +464,6 @@ oceanic_vtpro_device_read (dc_device_t *abstract, unsigned int address, unsigned { oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t*) abstract; - if (! device_is_oceanic_vtpro (abstract)) - return DC_STATUS_INVALIDARGS; - if ((address % PAGESIZE != 0) || (size % PAGESIZE != 0)) return DC_STATUS_INVALIDARGS; diff --git a/src/oceanic_vtpro_parser.c b/src/oceanic_vtpro_parser.c index 93d7809..c67483c 100644 --- a/src/oceanic_vtpro_parser.c +++ b/src/oceanic_vtpro_parser.c @@ -29,6 +29,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &oceanic_vtpro_parser_vtable) + typedef struct oceanic_vtpro_parser_t oceanic_vtpro_parser_t; struct oceanic_vtpro_parser_t { @@ -55,16 +57,6 @@ static const dc_parser_vtable_t oceanic_vtpro_parser_vtable = { }; -static int -parser_is_oceanic_vtpro (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &oceanic_vtpro_parser_vtable; -} - - dc_status_t oceanic_vtpro_parser_create (dc_parser_t **out, dc_context_t *context) { @@ -95,9 +87,6 @@ oceanic_vtpro_parser_create (dc_parser_t **out, dc_context_t *context) static dc_status_t oceanic_vtpro_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_oceanic_vtpro (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -110,9 +99,6 @@ oceanic_vtpro_parser_set_data (dc_parser_t *abstract, const unsigned char *data, { oceanic_vtpro_parser_t *parser = (oceanic_vtpro_parser_t *) abstract; - if (! parser_is_oceanic_vtpro (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -211,9 +197,6 @@ oceanic_vtpro_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, uns static dc_status_t oceanic_vtpro_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) { - if (! parser_is_oceanic_vtpro (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/parser-private.h b/src/parser-private.h index 1081f02..b6e83db 100644 --- a/src/parser-private.h +++ b/src/parser-private.h @@ -58,6 +58,9 @@ struct dc_parser_vtable_t { void parser_init (dc_parser_t *parser, dc_context_t *context, const dc_parser_vtable_t *vtable); +int +dc_parser_isinstance (dc_parser_t *parser, const dc_parser_vtable_t *vtable); + typedef struct sample_statistics_t { unsigned int divetime; double maxdepth; diff --git a/src/parser.c b/src/parser.c index ff5f3a8..c6276d8 100644 --- a/src/parser.c +++ b/src/parser.c @@ -137,6 +137,16 @@ parser_init (dc_parser_t *parser, dc_context_t *context, const dc_parser_vtable_ } +int +dc_parser_isinstance (dc_parser_t *parser, const dc_parser_vtable_t *vtable) +{ + if (parser == NULL) + return 0; + + return parser->vtable == vtable; +} + + dc_family_t dc_parser_get_type (dc_parser_t *parser) { diff --git a/src/reefnet_sensus.c b/src/reefnet_sensus.c index 4435766..c2e68a4 100644 --- a/src/reefnet_sensus.c +++ b/src/reefnet_sensus.c @@ -30,6 +30,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &reefnet_sensus_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -63,15 +65,6 @@ static const dc_device_vtable_t reefnet_sensus_device_vtable = { reefnet_sensus_device_close /* close */ }; -static int -device_is_reefnet_sensus (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &reefnet_sensus_device_vtable; -} - static dc_status_t reefnet_sensus_cancel (reefnet_sensus_device_t *device) @@ -156,9 +149,6 @@ reefnet_sensus_device_close (dc_device_t *abstract) { reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; - if (! device_is_reefnet_sensus (abstract)) - return DC_STATUS_INVALIDARGS; - // Safely close the connection if the last handshake was // successful, but no data transfer was ever initiated. if (device->waiting) @@ -182,7 +172,7 @@ reefnet_sensus_device_get_handshake (dc_device_t *abstract, unsigned char data[] { reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; - if (! device_is_reefnet_sensus (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_HANDSHAKE) { @@ -201,9 +191,6 @@ reefnet_sensus_device_set_fingerprint (dc_device_t *abstract, const unsigned cha { reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; - if (! device_is_reefnet_sensus (abstract)) - return DC_STATUS_INVALIDARGS; - if (size && size != 4) return DC_STATUS_INVALIDARGS; @@ -286,9 +273,6 @@ reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; - if (! device_is_reefnet_sensus (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // pre-allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { @@ -362,9 +346,6 @@ reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) static dc_status_t reefnet_sensus_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { - if (! device_is_reefnet_sensus (abstract)) - return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -390,7 +371,7 @@ reefnet_sensus_extract_dives (dc_device_t *abstract, const unsigned char data[], reefnet_sensus_device_t *device = (reefnet_sensus_device_t*) abstract; dc_context_t *context = (abstract ? abstract->context : NULL); - if (abstract && !device_is_reefnet_sensus (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Search the entire data stream for start markers. diff --git a/src/reefnet_sensus_parser.c b/src/reefnet_sensus_parser.c index ed0cf0a..74e55b3 100644 --- a/src/reefnet_sensus_parser.c +++ b/src/reefnet_sensus_parser.c @@ -28,6 +28,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &reefnet_sensus_parser_vtable) + #define SAMPLE_DEPTH_ADJUST 13 typedef struct reefnet_sensus_parser_t reefnet_sensus_parser_t; @@ -62,16 +64,6 @@ static const dc_parser_vtable_t reefnet_sensus_parser_vtable = { }; -static int -parser_is_reefnet_sensus (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &reefnet_sensus_parser_vtable; -} - - dc_status_t reefnet_sensus_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int devtime, dc_ticks_t systime) { @@ -106,9 +98,6 @@ reefnet_sensus_parser_create (dc_parser_t **out, dc_context_t *context, unsigned static dc_status_t reefnet_sensus_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_reefnet_sensus (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -121,9 +110,6 @@ reefnet_sensus_parser_set_data (dc_parser_t *abstract, const unsigned char *data { reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t*) abstract; - if (! parser_is_reefnet_sensus (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -138,7 +124,7 @@ reefnet_sensus_parser_set_calibration (dc_parser_t *abstract, double atmospheric { reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t*) abstract; - if (! parser_is_reefnet_sensus (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; parser->atmospheric = atmospheric; @@ -239,9 +225,6 @@ reefnet_sensus_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback { reefnet_sensus_parser_t *parser = (reefnet_sensus_parser_t*) abstract; - if (! parser_is_reefnet_sensus (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c index 7a45bed..df6b717 100644 --- a/src/reefnet_sensuspro.c +++ b/src/reefnet_sensuspro.c @@ -30,6 +30,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &reefnet_sensuspro_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -62,15 +64,6 @@ static const dc_device_vtable_t reefnet_sensuspro_device_vtable = { reefnet_sensuspro_device_close /* close */ }; -static int -device_is_reefnet_sensuspro (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &reefnet_sensuspro_device_vtable; -} - dc_status_t reefnet_sensuspro_device_open (dc_device_t **out, dc_context_t *context, const char *name) @@ -134,9 +127,6 @@ reefnet_sensuspro_device_close (dc_device_t *abstract) { reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; - if (! device_is_reefnet_sensuspro (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -155,7 +145,7 @@ reefnet_sensuspro_device_get_handshake (dc_device_t *abstract, unsigned char dat { reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; - if (! device_is_reefnet_sensuspro (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_HANDSHAKE) { @@ -174,9 +164,6 @@ reefnet_sensuspro_device_set_fingerprint (dc_device_t *abstract, const unsigned { reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; - if (! device_is_reefnet_sensuspro (abstract)) - return DC_STATUS_INVALIDARGS; - if (size && size != 4) return DC_STATUS_INVALIDARGS; @@ -274,9 +261,6 @@ reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; - if (! device_is_reefnet_sensuspro (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // pre-allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { @@ -330,9 +314,6 @@ reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) static dc_status_t reefnet_sensuspro_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { - if (! device_is_reefnet_sensuspro (abstract)) - return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -357,7 +338,7 @@ reefnet_sensuspro_device_write_interval (dc_device_t *abstract, unsigned char in { reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; - if (! device_is_reefnet_sensuspro (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (interval < 1 || interval > 127) @@ -385,7 +366,7 @@ reefnet_sensuspro_extract_dives (dc_device_t *abstract, const unsigned char data { reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract; - if (abstract && !device_is_reefnet_sensuspro (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; diff --git a/src/reefnet_sensuspro_parser.c b/src/reefnet_sensuspro_parser.c index 02a98d9..781d60a 100644 --- a/src/reefnet_sensuspro_parser.c +++ b/src/reefnet_sensuspro_parser.c @@ -29,6 +29,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &reefnet_sensuspro_parser_vtable) + typedef struct reefnet_sensuspro_parser_t reefnet_sensuspro_parser_t; struct reefnet_sensuspro_parser_t { @@ -61,16 +63,6 @@ static const dc_parser_vtable_t reefnet_sensuspro_parser_vtable = { }; -static int -parser_is_reefnet_sensuspro (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &reefnet_sensuspro_parser_vtable; -} - - dc_status_t reefnet_sensuspro_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int devtime, dc_ticks_t systime) { @@ -105,9 +97,6 @@ reefnet_sensuspro_parser_create (dc_parser_t **out, dc_context_t *context, unsig static dc_status_t reefnet_sensuspro_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_reefnet_sensuspro (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -120,9 +109,6 @@ reefnet_sensuspro_parser_set_data (dc_parser_t *abstract, const unsigned char *d { reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t*) abstract; - if (! parser_is_reefnet_sensuspro (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -137,7 +123,7 @@ reefnet_sensuspro_parser_set_calibration (dc_parser_t *abstract, double atmosphe { reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t*) abstract; - if (! parser_is_reefnet_sensuspro (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; parser->atmospheric = atmospheric; @@ -228,9 +214,6 @@ reefnet_sensuspro_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callb { reefnet_sensuspro_parser_t *parser = (reefnet_sensuspro_parser_t*) abstract; - if (! parser_is_reefnet_sensuspro (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; const unsigned char footer[2] = {0xFF, 0xFF}; diff --git a/src/reefnet_sensusultra.c b/src/reefnet_sensusultra.c index f6254cc..3e0f848 100644 --- a/src/reefnet_sensusultra.c +++ b/src/reefnet_sensusultra.c @@ -31,6 +31,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &reefnet_sensusultra_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -71,15 +73,6 @@ static const dc_device_vtable_t reefnet_sensusultra_device_vtable = { reefnet_sensusultra_device_close /* close */ }; -static int -device_is_reefnet_sensusultra (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &reefnet_sensusultra_device_vtable; -} - dc_status_t reefnet_sensusultra_device_open (dc_device_t **out, dc_context_t *context, const char *name) @@ -143,9 +136,6 @@ reefnet_sensusultra_device_close (dc_device_t *abstract) { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -164,7 +154,7 @@ reefnet_sensusultra_device_get_handshake (dc_device_t *abstract, unsigned char d { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_HANDSHAKE) { @@ -183,9 +173,6 @@ reefnet_sensusultra_device_set_fingerprint (dc_device_t *abstract, const unsigne { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) - return DC_STATUS_INVALIDARGS; - if (size && size != 4) return DC_STATUS_INVALIDARGS; @@ -393,9 +380,6 @@ reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // pre-allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { @@ -450,7 +434,7 @@ reefnet_sensusultra_device_read_user (dc_device_t *abstract, unsigned char *data { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_USER) { @@ -493,7 +477,7 @@ reefnet_sensusultra_device_write_user (dc_device_t *abstract, const unsigned cha { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_USER) { @@ -541,7 +525,7 @@ reefnet_sensusultra_device_write_parameter (dc_device_t *abstract, reefnet_sensu { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Set the instruction code and validate the new value. @@ -590,7 +574,7 @@ reefnet_sensusultra_device_sense (dc_device_t *abstract, unsigned char *data, un { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_SENSE) { @@ -690,9 +674,6 @@ reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t ca { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract; - if (! device_is_reefnet_sensusultra (abstract)) - return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) { ERROR (abstract->context, "Failed to allocate memory."); @@ -777,7 +758,7 @@ reefnet_sensusultra_extract_dives (dc_device_t *abstract, const unsigned char da { reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t *) abstract; - if (abstract && !device_is_reefnet_sensusultra (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; unsigned int remaining = size; diff --git a/src/reefnet_sensusultra_parser.c b/src/reefnet_sensusultra_parser.c index 5a7e7a3..6329143 100644 --- a/src/reefnet_sensusultra_parser.c +++ b/src/reefnet_sensusultra_parser.c @@ -29,6 +29,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &reefnet_sensusultra_parser_vtable) + typedef struct reefnet_sensusultra_parser_t reefnet_sensusultra_parser_t; struct reefnet_sensusultra_parser_t { @@ -61,16 +63,6 @@ static const dc_parser_vtable_t reefnet_sensusultra_parser_vtable = { }; -static int -parser_is_reefnet_sensusultra (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &reefnet_sensusultra_parser_vtable; -} - - dc_status_t reefnet_sensusultra_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int devtime, dc_ticks_t systime) { @@ -105,9 +97,6 @@ reefnet_sensusultra_parser_create (dc_parser_t **out, dc_context_t *context, uns static dc_status_t reefnet_sensusultra_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_reefnet_sensusultra (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -120,9 +109,6 @@ reefnet_sensusultra_parser_set_data (dc_parser_t *abstract, const unsigned char { reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t*) abstract; - if (! parser_is_reefnet_sensusultra (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -137,7 +123,7 @@ reefnet_sensusultra_parser_set_calibration (dc_parser_t *abstract, double atmosp { reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t*) abstract; - if (! parser_is_reefnet_sensusultra (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; parser->atmospheric = atmospheric; @@ -229,9 +215,6 @@ reefnet_sensusultra_parser_samples_foreach (dc_parser_t *abstract, dc_sample_cal { reefnet_sensusultra_parser_t *parser = (reefnet_sensusultra_parser_t*) abstract; - if (! parser_is_reefnet_sensusultra (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char header[4] = {0x00, 0x00, 0x00, 0x00}; const unsigned char footer[4] = {0xFF, 0xFF, 0xFF, 0xFF}; diff --git a/src/shearwater_predator.c b/src/shearwater_predator.c index a38e5cc..50a1fd9 100644 --- a/src/shearwater_predator.c +++ b/src/shearwater_predator.c @@ -29,6 +29,8 @@ #include "serial.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &shearwater_predator_device_vtable) + #define PREDATOR 2 #define PETREL 3 @@ -69,16 +71,6 @@ static const dc_device_vtable_t shearwater_predator_device_vtable = { }; -static int -device_is_shearwater_predator (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &shearwater_predator_device_vtable; -} - - static int shearwater_predator_slip_write (shearwater_predator_device_t *device, const unsigned char data[], unsigned int size) { @@ -623,7 +615,7 @@ shearwater_predator_extract_petrel (dc_device_t *abstract, const unsigned char d dc_status_t shearwater_predator_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) { - if (abstract && !device_is_shearwater_predator (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_MEMORY) diff --git a/src/shearwater_predator_parser.c b/src/shearwater_predator_parser.c index cce7a68..9816569 100644 --- a/src/shearwater_predator_parser.c +++ b/src/shearwater_predator_parser.c @@ -28,6 +28,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &shearwater_predator_parser_vtable) + #define SZ_BLOCK 0x80 #define SZ_SAMPLE 0x10 @@ -56,16 +58,6 @@ static const dc_parser_vtable_t shearwater_predator_parser_vtable = { }; -static int -parser_is_shearwater_predator (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &shearwater_predator_parser_vtable; -} - - dc_status_t shearwater_predator_parser_create (dc_parser_t **out, dc_context_t *context) { @@ -91,9 +83,6 @@ shearwater_predator_parser_create (dc_parser_t **out, dc_context_t *context) static dc_status_t shearwater_predator_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_shearwater_predator (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -104,9 +93,6 @@ shearwater_predator_parser_destroy (dc_parser_t *abstract) static dc_status_t shearwater_predator_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) { - if (! parser_is_shearwater_predator (abstract)) - return DC_STATUS_INVALIDARGS; - return DC_STATUS_SUCCESS; } diff --git a/src/suunto_d9.c b/src/suunto_d9.c index 793d433..7069ca9 100644 --- a/src/suunto_d9.c +++ b/src/suunto_d9.c @@ -31,6 +31,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), (const dc_device_vtable_t *) &suunto_d9_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -79,15 +81,6 @@ static const suunto_common2_layout_t suunto_d9tx_layout = { 0xEBF0 /* rb_profile_end */ }; -static int -device_is_suunto_d9 (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == (const dc_device_vtable_t *) &suunto_d9_device_vtable; -} - static dc_status_t suunto_d9_device_autodetect (suunto_d9_device_t *device, unsigned int model) @@ -209,9 +202,6 @@ suunto_d9_device_close (dc_device_t *abstract) { suunto_d9_device_t *device = (suunto_d9_device_t*) abstract; - if (! device_is_suunto_d9 (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -301,7 +291,7 @@ suunto_d9_device_packet (dc_device_t *abstract, const unsigned char command[], u dc_status_t suunto_d9_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size) { - if (! device_is_suunto_d9 (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; return suunto_common2_device_version (abstract, data, size); @@ -311,7 +301,7 @@ suunto_d9_device_version (dc_device_t *abstract, unsigned char data[], unsigned dc_status_t suunto_d9_device_reset_maxdepth (dc_device_t *abstract) { - if (! device_is_suunto_d9 (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; return suunto_common2_device_reset_maxdepth (abstract); diff --git a/src/suunto_d9_parser.c b/src/suunto_d9_parser.c index faff739..19f681b 100644 --- a/src/suunto_d9_parser.c +++ b/src/suunto_d9_parser.c @@ -28,6 +28,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &suunto_d9_parser_vtable) + #define MAXPARAMS 3 #define D9 0x0E @@ -82,16 +84,6 @@ static const dc_parser_vtable_t suunto_d9_parser_vtable = { }; -static int -parser_is_suunto_d9 (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &suunto_d9_parser_vtable; -} - - dc_status_t suunto_d9_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model) { @@ -120,9 +112,6 @@ suunto_d9_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int static dc_status_t suunto_d9_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_suunto_d9 (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -133,9 +122,6 @@ suunto_d9_parser_destroy (dc_parser_t *abstract) static dc_status_t suunto_d9_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) { - if (! parser_is_suunto_d9 (abstract)) - return DC_STATUS_INVALIDARGS; - return DC_STATUS_SUCCESS; } @@ -276,9 +262,6 @@ suunto_d9_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t ca { suunto_d9_parser_t *parser = (suunto_d9_parser_t*) abstract; - if (! parser_is_suunto_d9 (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/suunto_eon.c b/src/suunto_eon.c index 3243bde..31a1eda 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -31,6 +31,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &suunto_eon_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -66,16 +68,6 @@ static const suunto_common_layout_t suunto_eon_layout = { }; -static int -device_is_suunto_eon (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &suunto_eon_device_vtable; -} - - dc_status_t suunto_eon_device_open (dc_device_t **out, dc_context_t *context, const char *name) { @@ -139,9 +131,6 @@ suunto_eon_device_close (dc_device_t *abstract) { suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; - if (! device_is_suunto_eon (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -160,9 +149,6 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; - if (! device_is_suunto_eon (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // pre-allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { @@ -248,7 +234,7 @@ suunto_eon_device_write_name (dc_device_t *abstract, unsigned char data[], unsig { suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; - if (! device_is_suunto_eon (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size > 20) @@ -272,7 +258,7 @@ suunto_eon_device_write_interval (dc_device_t *abstract, unsigned char interval) { suunto_eon_device_t *device = (suunto_eon_device_t*) abstract; - if (! device_is_suunto_eon (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Send the command. @@ -292,7 +278,7 @@ suunto_eon_extract_dives (dc_device_t *abstract, const unsigned char data[], uns { suunto_common_device_t *device = (suunto_common_device_t*) abstract; - if (abstract && !device_is_suunto_eon (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_MEMORY) diff --git a/src/suunto_eon_parser.c b/src/suunto_eon_parser.c index 58f7452..b287710 100644 --- a/src/suunto_eon_parser.c +++ b/src/suunto_eon_parser.c @@ -28,6 +28,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &suunto_eon_parser_vtable) + typedef struct suunto_eon_parser_t suunto_eon_parser_t; struct suunto_eon_parser_t { @@ -55,16 +57,6 @@ static const dc_parser_vtable_t suunto_eon_parser_vtable = { }; -static int -parser_is_suunto_eon (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &suunto_eon_parser_vtable; -} - - dc_status_t suunto_eon_parser_create (dc_parser_t **out, dc_context_t *context, int spyder) { @@ -96,9 +88,6 @@ suunto_eon_parser_create (dc_parser_t **out, dc_context_t *context, int spyder) static dc_status_t suunto_eon_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_suunto_eon (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -111,9 +100,6 @@ suunto_eon_parser_set_data (dc_parser_t *abstract, const unsigned char *data, un { suunto_eon_parser_t *parser = (suunto_eon_parser_t *) abstract; - if (! parser_is_suunto_eon (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -225,9 +211,6 @@ suunto_eon_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t c { suunto_eon_parser_t *parser = (suunto_eon_parser_t *) abstract; - if (! parser_is_suunto_eon (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/suunto_solution.c b/src/suunto_solution.c index a35c208..4647ac5 100644 --- a/src/suunto_solution.c +++ b/src/suunto_solution.c @@ -30,6 +30,8 @@ #include "serial.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &suunto_solution_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -59,15 +61,6 @@ static const dc_device_vtable_t suunto_solution_device_vtable = { suunto_solution_device_close /* close */ }; -static int -device_is_suunto_solution (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &suunto_solution_device_vtable; -} - dc_status_t suunto_solution_device_open (dc_device_t **out, dc_context_t *context, const char *name) @@ -132,9 +125,6 @@ suunto_solution_device_close (dc_device_t *abstract) { suunto_solution_device_t *device = (suunto_solution_device_t*) abstract; - if (! device_is_suunto_solution (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -153,9 +143,6 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { suunto_solution_device_t *device = (suunto_solution_device_t*) abstract; - if (! device_is_suunto_solution (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { @@ -261,9 +248,6 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) static dc_status_t suunto_solution_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { - if (! device_is_suunto_solution (abstract)) - return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -298,7 +282,7 @@ suunto_solution_device_foreach (dc_device_t *abstract, dc_dive_callback_t callba dc_status_t suunto_solution_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) { - if (abstract && !device_is_suunto_solution (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_MEMORY) diff --git a/src/suunto_solution_parser.c b/src/suunto_solution_parser.c index a0a1a66..2da6a7b 100644 --- a/src/suunto_solution_parser.c +++ b/src/suunto_solution_parser.c @@ -27,6 +27,8 @@ #include "context-private.h" #include "parser-private.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &suunto_solution_parser_vtable) + typedef struct suunto_solution_parser_t suunto_solution_parser_t; struct suunto_solution_parser_t { @@ -52,16 +54,6 @@ static const dc_parser_vtable_t suunto_solution_parser_vtable = { }; -static int -parser_is_suunto_solution (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &suunto_solution_parser_vtable; -} - - dc_status_t suunto_solution_parser_create (dc_parser_t **out, dc_context_t *context) { @@ -92,9 +84,6 @@ suunto_solution_parser_create (dc_parser_t **out, dc_context_t *context) static dc_status_t suunto_solution_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_suunto_solution (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -107,9 +96,6 @@ suunto_solution_parser_set_data (dc_parser_t *abstract, const unsigned char *dat { suunto_solution_parser_t *parser = (suunto_solution_parser_t *) abstract; - if (! parser_is_suunto_solution (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -188,9 +174,6 @@ suunto_solution_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, u static dc_status_t suunto_solution_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) { - if (! parser_is_suunto_solution (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c index 1d01cc8..0e959d0 100644 --- a/src/suunto_vyper.c +++ b/src/suunto_vyper.c @@ -32,6 +32,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &suunto_vyper_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -86,16 +88,6 @@ static const suunto_common_layout_t suunto_spyder_layout = { }; -static int -device_is_suunto_vyper (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &suunto_vyper_device_vtable; -} - - dc_status_t suunto_vyper_device_open (dc_device_t **out, dc_context_t *context, const char *name) { @@ -165,9 +157,6 @@ suunto_vyper_device_close (dc_device_t *abstract) { suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract; - if (! device_is_suunto_vyper (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -266,9 +255,6 @@ suunto_vyper_device_read (dc_device_t *abstract, unsigned int address, unsigned { suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract; - if (! device_is_suunto_vyper (abstract)) - return DC_STATUS_INVALIDARGS; - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the package size. @@ -302,9 +288,6 @@ suunto_vyper_device_write (dc_device_t *abstract, unsigned int address, const un { suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract; - if (! device_is_suunto_vyper (abstract)) - return DC_STATUS_INVALIDARGS; - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the package size. @@ -460,7 +443,7 @@ suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc dc_status_t suunto_vyper_device_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init) { - if (! device_is_suunto_vyper (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; return suunto_vyper_read_dive (abstract, buffer, init, NULL); @@ -470,9 +453,6 @@ suunto_vyper_device_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int i static dc_status_t suunto_vyper_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { - if (! device_is_suunto_vyper (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { @@ -490,9 +470,6 @@ suunto_vyper_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, { suunto_common_device_t *device = (suunto_common_device_t*) abstract; - if (! device_is_suunto_vyper (abstract)) - return DC_STATUS_INVALIDARGS; - // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; progress.maximum = SZ_MEMORY; @@ -581,7 +558,7 @@ suunto_vyper_extract_dives (dc_device_t *abstract, const unsigned char data[], u { suunto_common_device_t *device = (suunto_common_device_t*) abstract; - if (abstract && !device_is_suunto_vyper (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_MEMORY) diff --git a/src/suunto_vyper2.c b/src/suunto_vyper2.c index 0c79d08..7289302 100644 --- a/src/suunto_vyper2.c +++ b/src/suunto_vyper2.c @@ -30,6 +30,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), (const dc_device_vtable_t *) &suunto_vyper2_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -74,15 +76,6 @@ static const suunto_common2_layout_t suunto_helo2_layout = { 0x7FFE /* rb_profile_end */ }; -static int -device_is_suunto_vyper2 (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == (const dc_device_vtable_t *) &suunto_vyper2_device_vtable; -} - dc_status_t suunto_vyper2_device_open (dc_device_t **out, dc_context_t *context, const char *name) @@ -172,9 +165,6 @@ suunto_vyper2_device_close (dc_device_t *abstract) { suunto_vyper2_device_t *device = (suunto_vyper2_device_t*) abstract; - if (! device_is_suunto_vyper2 (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -251,7 +241,7 @@ suunto_vyper2_device_packet (dc_device_t *abstract, const unsigned char command[ dc_status_t suunto_vyper2_device_version (dc_device_t *abstract, unsigned char data[], unsigned int size) { - if (! device_is_suunto_vyper2 (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; return suunto_common2_device_version (abstract, data, size); @@ -261,7 +251,7 @@ suunto_vyper2_device_version (dc_device_t *abstract, unsigned char data[], unsig dc_status_t suunto_vyper2_device_reset_maxdepth (dc_device_t *abstract) { - if (! device_is_suunto_vyper2 (abstract)) + if (!ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; return suunto_common2_device_reset_maxdepth (abstract); diff --git a/src/suunto_vyper_parser.c b/src/suunto_vyper_parser.c index ee59056..9a33bab 100644 --- a/src/suunto_vyper_parser.c +++ b/src/suunto_vyper_parser.c @@ -27,6 +27,8 @@ #include "context-private.h" #include "parser-private.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &suunto_vyper_parser_vtable) + typedef struct suunto_vyper_parser_t suunto_vyper_parser_t; struct suunto_vyper_parser_t { @@ -53,16 +55,6 @@ static const dc_parser_vtable_t suunto_vyper_parser_vtable = { }; -static int -parser_is_suunto_vyper (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &suunto_vyper_parser_vtable; -} - - dc_status_t suunto_vyper_parser_create (dc_parser_t **out, dc_context_t *context) { @@ -93,9 +85,6 @@ suunto_vyper_parser_create (dc_parser_t **out, dc_context_t *context) static dc_status_t suunto_vyper_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_suunto_vyper (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -108,9 +97,6 @@ suunto_vyper_parser_set_data (dc_parser_t *abstract, const unsigned char *data, { suunto_vyper_parser_t *parser = (suunto_vyper_parser_t *) abstract; - if (! parser_is_suunto_vyper (abstract)) - return DC_STATUS_INVALIDARGS; - // Reset the cache. parser->cached = 0; parser->divetime = 0; @@ -209,9 +195,6 @@ suunto_vyper_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, unsi static dc_status_t suunto_vyper_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) { - if (! parser_is_suunto_vyper (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index fa422ff..a1d157a 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -31,6 +31,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &uwatec_aladin_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -68,15 +70,6 @@ static const dc_device_vtable_t uwatec_aladin_device_vtable = { uwatec_aladin_device_close /* close */ }; -static int -device_is_uwatec_aladin (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &uwatec_aladin_device_vtable; -} - dc_status_t uwatec_aladin_device_open (dc_device_t **out, dc_context_t *context, const char *name) @@ -145,9 +138,6 @@ uwatec_aladin_device_close (dc_device_t *abstract) { uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract; - if (! device_is_uwatec_aladin (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -166,9 +156,6 @@ uwatec_aladin_device_set_fingerprint (dc_device_t *abstract, const unsigned char { uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract; - if (! device_is_uwatec_aladin (abstract)) - return DC_STATUS_INVALIDARGS; - if (size && size != 4) return DC_STATUS_INVALIDARGS; @@ -186,9 +173,6 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract; - if (! device_is_uwatec_aladin (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // pre-allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { @@ -269,9 +253,6 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) static dc_status_t uwatec_aladin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { - if (! device_is_uwatec_aladin (abstract)) - return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -304,7 +285,7 @@ uwatec_aladin_extract_dives (dc_device_t *abstract, const unsigned char* data, u { uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract; - if (abstract && !device_is_uwatec_aladin (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; if (size < SZ_MEMORY) diff --git a/src/uwatec_memomouse.c b/src/uwatec_memomouse.c index cc7e32d..95b29b3 100644 --- a/src/uwatec_memomouse.c +++ b/src/uwatec_memomouse.c @@ -31,6 +31,8 @@ #include "checksum.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &uwatec_memomouse_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -64,15 +66,6 @@ static const dc_device_vtable_t uwatec_memomouse_device_vtable = { uwatec_memomouse_device_close /* close */ }; -static int -device_is_uwatec_memomouse (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &uwatec_memomouse_device_vtable; -} - dc_status_t uwatec_memomouse_device_open (dc_device_t **out, dc_context_t *context, const char *name) @@ -144,9 +137,6 @@ uwatec_memomouse_device_close (dc_device_t *abstract) { uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract; - if (! device_is_uwatec_memomouse (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -165,9 +155,6 @@ uwatec_memomouse_device_set_fingerprint (dc_device_t *abstract, const unsigned c { uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract; - if (! device_is_uwatec_memomouse (abstract)) - return DC_STATUS_INVALIDARGS; - if (size && size != 4) return DC_STATUS_INVALIDARGS; @@ -452,9 +439,6 @@ uwatec_memomouse_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract; - if (! device_is_uwatec_memomouse (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); @@ -487,9 +471,6 @@ uwatec_memomouse_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) static dc_status_t uwatec_memomouse_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { - if (! device_is_uwatec_memomouse (abstract)) - return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (0); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -512,7 +493,7 @@ uwatec_memomouse_device_foreach (dc_device_t *abstract, dc_dive_callback_t callb dc_status_t uwatec_memomouse_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) { - if (abstract && !device_is_uwatec_memomouse (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; // Parse the data stream to find the total number of dives. diff --git a/src/uwatec_memomouse_parser.c b/src/uwatec_memomouse_parser.c index de49981..3609ef9 100644 --- a/src/uwatec_memomouse_parser.c +++ b/src/uwatec_memomouse_parser.c @@ -27,6 +27,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &uwatec_memomouse_parser_vtable) + typedef struct uwatec_memomouse_parser_t uwatec_memomouse_parser_t; struct uwatec_memomouse_parser_t { @@ -51,16 +53,6 @@ static const dc_parser_vtable_t uwatec_memomouse_parser_vtable = { }; -static int -parser_is_uwatec_memomouse (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &uwatec_memomouse_parser_vtable; -} - - dc_status_t uwatec_memomouse_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int devtime, dc_ticks_t systime) { @@ -90,9 +82,6 @@ uwatec_memomouse_parser_create (dc_parser_t **out, dc_context_t *context, unsign static dc_status_t uwatec_memomouse_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_uwatec_memomouse (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -103,9 +92,6 @@ uwatec_memomouse_parser_destroy (dc_parser_t *abstract) static dc_status_t uwatec_memomouse_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) { - if (! parser_is_uwatec_memomouse (abstract)) - return DC_STATUS_INVALIDARGS; - return DC_STATUS_SUCCESS; } @@ -193,9 +179,6 @@ uwatec_memomouse_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, static dc_status_t uwatec_memomouse_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t callback, void *userdata) { - if (! parser_is_uwatec_memomouse (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index 913d9c8..bd6a0ae 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -29,6 +29,8 @@ #include "irda.h" #include "array.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &uwatec_smart_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -58,15 +60,6 @@ static const dc_device_vtable_t uwatec_smart_device_vtable = { uwatec_smart_device_close /* close */ }; -static int -device_is_uwatec_smart (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &uwatec_smart_device_vtable; -} - static void uwatec_smart_discovery (unsigned int address, const char *name, unsigned int charset, unsigned int hints, void *userdata) @@ -219,9 +212,6 @@ uwatec_smart_device_close (dc_device_t *abstract) { uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract; - if (! device_is_uwatec_smart (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (irda_socket_close (device->socket) == -1) { free (device); @@ -240,9 +230,6 @@ uwatec_smart_device_set_fingerprint (dc_device_t *abstract, const unsigned char { uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract; - if (! device_is_uwatec_smart (abstract)) - return DC_STATUS_INVALIDARGS; - if (size && size != 4) return DC_STATUS_INVALIDARGS; @@ -261,9 +248,6 @@ uwatec_smart_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract; dc_status_t rc = DC_STATUS_SUCCESS; - if (! device_is_uwatec_smart (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer. if (!dc_buffer_clear (buffer)) { ERROR (abstract->context, "Insufficient buffer space available."); @@ -403,9 +387,6 @@ uwatec_smart_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) static dc_status_t uwatec_smart_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { - if (! device_is_uwatec_smart (abstract)) - return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (0); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -428,7 +409,7 @@ uwatec_smart_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, dc_status_t uwatec_smart_extract_dives (dc_device_t *abstract, const unsigned char data[], unsigned int size, dc_dive_callback_t callback, void *userdata) { - if (abstract && !device_is_uwatec_smart (abstract)) + if (abstract && !ISINSTANCE (abstract)) return DC_STATUS_INVALIDARGS; const unsigned char header[4] = {0xa5, 0xa5, 0x5a, 0x5a}; diff --git a/src/uwatec_smart_parser.c b/src/uwatec_smart_parser.c index f11b86c..6c53730 100644 --- a/src/uwatec_smart_parser.c +++ b/src/uwatec_smart_parser.c @@ -29,6 +29,8 @@ #include "parser-private.h" #include "array.h" +#define ISINSTANCE(parser) dc_parser_isinstance((parser), &uwatec_smart_parser_vtable) + #define NBITS 8 #define NELEMENTS(x) ( sizeof(x) / sizeof((x)[0]) ) @@ -66,16 +68,6 @@ static const dc_parser_vtable_t uwatec_smart_parser_vtable = { }; -static int -parser_is_uwatec_smart (dc_parser_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &uwatec_smart_parser_vtable; -} - - dc_status_t uwatec_smart_parser_create (dc_parser_t **out, dc_context_t *context, unsigned int model, unsigned int devtime, dc_ticks_t systime) { @@ -106,9 +98,6 @@ uwatec_smart_parser_create (dc_parser_t **out, dc_context_t *context, unsigned i static dc_status_t uwatec_smart_parser_destroy (dc_parser_t *abstract) { - if (! parser_is_uwatec_smart (abstract)) - return DC_STATUS_INVALIDARGS; - // Free memory. free (abstract); @@ -119,9 +108,6 @@ uwatec_smart_parser_destroy (dc_parser_t *abstract) static dc_status_t uwatec_smart_parser_set_data (dc_parser_t *abstract, const unsigned char *data, unsigned int size) { - if (! parser_is_uwatec_smart (abstract)) - return DC_STATUS_INVALIDARGS; - return DC_STATUS_SUCCESS; } @@ -451,9 +437,6 @@ uwatec_smart_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t { uwatec_smart_parser_t *parser = (uwatec_smart_parser_t*) abstract; - if (! parser_is_uwatec_smart (abstract)) - return DC_STATUS_INVALIDARGS; - const unsigned char *data = abstract->data; unsigned int size = abstract->size; diff --git a/src/zeagle_n2ition3.c b/src/zeagle_n2ition3.c index f9c5bc0..4d6db49 100644 --- a/src/zeagle_n2ition3.c +++ b/src/zeagle_n2ition3.c @@ -32,6 +32,8 @@ #include "array.h" #include "ringbuffer.h" +#define ISINSTANCE(device) dc_device_isinstance((device), &zeagle_n2ition3_device_vtable) + #define EXITCODE(rc) \ ( \ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ @@ -69,15 +71,6 @@ static const dc_device_vtable_t zeagle_n2ition3_device_vtable = { zeagle_n2ition3_device_close /* close */ }; -static int -device_is_zeagle_n2ition3 (dc_device_t *abstract) -{ - if (abstract == NULL) - return 0; - - return abstract->vtable == &zeagle_n2ition3_device_vtable; -} - static dc_status_t zeagle_n2ition3_packet (zeagle_n2ition3_device_t *device, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize) @@ -199,9 +192,6 @@ zeagle_n2ition3_device_close (dc_device_t *abstract) { zeagle_n2ition3_device_t *device = (zeagle_n2ition3_device_t*) abstract; - if (! device_is_zeagle_n2ition3 (abstract)) - return DC_STATUS_INVALIDARGS; - // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -237,9 +227,6 @@ zeagle_n2ition3_device_read (dc_device_t *abstract, unsigned int address, unsign { zeagle_n2ition3_device_t *device = (zeagle_n2ition3_device_t*) abstract; - if (! device_is_zeagle_n2ition3 (abstract)) - return DC_STATUS_INVALIDARGS; - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the package size. @@ -273,9 +260,6 @@ zeagle_n2ition3_device_read (dc_device_t *abstract, unsigned int address, unsign static dc_status_t zeagle_n2ition3_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) { - if (! device_is_zeagle_n2ition3 (abstract)) - return DC_STATUS_INVALIDARGS; - // Erase the current contents of the buffer and // allocate the required amount of memory. if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) {