diff --git a/examples/reefnet_sensusultra_test.c b/examples/reefnet_sensusultra_test.c index 1ab35ef..96b5586 100644 --- a/examples/reefnet_sensusultra_test.c +++ b/examples/reefnet_sensusultra_test.c @@ -136,7 +136,7 @@ test_dump_memory_user (const char* name, const char* filename) { dc_context_t *context = NULL; dc_device_t *device = NULL; - unsigned char data[REEFNET_SENSUSULTRA_MEMORY_USER_SIZE] = {0}; + unsigned char data[REEFNET_SENSUSULTRA_USER_SIZE] = {0}; dc_context_new (&context); dc_context_set_loglevel (context, DC_LOGLEVEL_ALL); diff --git a/include/libdivecomputer/cressi_edy.h b/include/libdivecomputer/cressi_edy.h index 18e22db..852aa05 100644 --- a/include/libdivecomputer/cressi_edy.h +++ b/include/libdivecomputer/cressi_edy.h @@ -30,9 +30,6 @@ extern "C" { #endif /* __cplusplus */ -#define CRESSI_EDY_MEMORY_SIZE 0x8000 -#define CRESSI_EDY_PACKET_SIZE 128 - dc_status_t cressi_edy_device_open (dc_device_t **device, dc_context_t *context, const char *name); diff --git a/include/libdivecomputer/mares_iconhd.h b/include/libdivecomputer/mares_iconhd.h index 9fd8581..00c1224 100644 --- a/include/libdivecomputer/mares_iconhd.h +++ b/include/libdivecomputer/mares_iconhd.h @@ -30,8 +30,6 @@ extern "C" { #endif /* __cplusplus */ -#define MARES_ICONHD_MEMORY_SIZE 0x100000 - dc_status_t mares_iconhd_device_open (dc_device_t **device, dc_context_t *context, const char *name); diff --git a/include/libdivecomputer/reefnet_sensus.h b/include/libdivecomputer/reefnet_sensus.h index 9e9bf7e..57f5274 100644 --- a/include/libdivecomputer/reefnet_sensus.h +++ b/include/libdivecomputer/reefnet_sensus.h @@ -30,7 +30,6 @@ extern "C" { #endif /* __cplusplus */ -#define REEFNET_SENSUS_MEMORY_SIZE 32768 #define REEFNET_SENSUS_HANDSHAKE_SIZE 10 dc_status_t diff --git a/include/libdivecomputer/reefnet_sensuspro.h b/include/libdivecomputer/reefnet_sensuspro.h index d346747..f718dd4 100644 --- a/include/libdivecomputer/reefnet_sensuspro.h +++ b/include/libdivecomputer/reefnet_sensuspro.h @@ -30,7 +30,6 @@ extern "C" { #endif /* __cplusplus */ -#define REEFNET_SENSUSPRO_MEMORY_SIZE 56320 #define REEFNET_SENSUSPRO_HANDSHAKE_SIZE 10 dc_status_t diff --git a/include/libdivecomputer/reefnet_sensusultra.h b/include/libdivecomputer/reefnet_sensusultra.h index e4d1851..7d39f7e 100644 --- a/include/libdivecomputer/reefnet_sensusultra.h +++ b/include/libdivecomputer/reefnet_sensusultra.h @@ -30,10 +30,7 @@ extern "C" { #endif /* __cplusplus */ -#define REEFNET_SENSUSULTRA_PACKET_SIZE 512 -#define REEFNET_SENSUSULTRA_MEMORY_USER_SIZE 16384 /* 32 PAGES */ -#define REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE 2080768 /* 4064 PAGES */ -#define REEFNET_SENSUSULTRA_MEMORY_SIZE 2097152 /* USER + DATA */ +#define REEFNET_SENSUSULTRA_USER_SIZE 16384 #define REEFNET_SENSUSULTRA_HANDSHAKE_SIZE 24 #define REEFNET_SENSUSULTRA_SENSE_SIZE 6 diff --git a/include/libdivecomputer/suunto_d9.h b/include/libdivecomputer/suunto_d9.h index dc080f7..f2f9977 100644 --- a/include/libdivecomputer/suunto_d9.h +++ b/include/libdivecomputer/suunto_d9.h @@ -30,8 +30,6 @@ extern "C" { #endif /* __cplusplus */ -#define SUUNTO_D9_MEMORY_SIZE 0x8000 -#define SUUNTO_D9_PACKET_SIZE 0x78 #define SUUNTO_D9_VERSION_SIZE 0x04 dc_status_t diff --git a/include/libdivecomputer/suunto_eon.h b/include/libdivecomputer/suunto_eon.h index f56314f..8bde22d 100644 --- a/include/libdivecomputer/suunto_eon.h +++ b/include/libdivecomputer/suunto_eon.h @@ -30,8 +30,6 @@ extern "C" { #endif /* __cplusplus */ -#define SUUNTO_EON_MEMORY_SIZE 0x900 - dc_status_t suunto_eon_device_open (dc_device_t **device, dc_context_t *context, const char *name); diff --git a/include/libdivecomputer/suunto_solution.h b/include/libdivecomputer/suunto_solution.h index 5312c90..6f5b210 100644 --- a/include/libdivecomputer/suunto_solution.h +++ b/include/libdivecomputer/suunto_solution.h @@ -30,8 +30,6 @@ extern "C" { #endif /* __cplusplus */ -#define SUUNTO_SOLUTION_MEMORY_SIZE 256 - dc_status_t suunto_solution_device_open (dc_device_t **device, dc_context_t *context, const char *name); diff --git a/include/libdivecomputer/suunto_vyper.h b/include/libdivecomputer/suunto_vyper.h index 7a256a2..727a125 100644 --- a/include/libdivecomputer/suunto_vyper.h +++ b/include/libdivecomputer/suunto_vyper.h @@ -30,9 +30,6 @@ extern "C" { #endif /* __cplusplus */ -#define SUUNTO_VYPER_MEMORY_SIZE 0x2000 -#define SUUNTO_VYPER_PACKET_SIZE 32 - dc_status_t suunto_vyper_device_open (dc_device_t **device, dc_context_t *context, const char *name); diff --git a/include/libdivecomputer/suunto_vyper2.h b/include/libdivecomputer/suunto_vyper2.h index 95e8534..dbefdd1 100644 --- a/include/libdivecomputer/suunto_vyper2.h +++ b/include/libdivecomputer/suunto_vyper2.h @@ -29,8 +29,6 @@ extern "C" { #include "context.h" #include "device.h" -#define SUUNTO_VYPER2_MEMORY_SIZE 0x8000 -#define SUUNTO_VYPER2_PACKET_SIZE 0x78 #define SUUNTO_VYPER2_VERSION_SIZE 0x04 dc_status_t diff --git a/include/libdivecomputer/uwatec_aladin.h b/include/libdivecomputer/uwatec_aladin.h index 688513b..311f01b 100644 --- a/include/libdivecomputer/uwatec_aladin.h +++ b/include/libdivecomputer/uwatec_aladin.h @@ -29,8 +29,6 @@ extern "C" { #include "context.h" #include "device.h" -#define UWATEC_ALADIN_MEMORY_SIZE 2048 - dc_status_t uwatec_aladin_device_open (dc_device_t **device, dc_context_t *context, const char *name); diff --git a/include/libdivecomputer/zeagle_n2ition3.h b/include/libdivecomputer/zeagle_n2ition3.h index 4c69d89..a9509cd 100644 --- a/include/libdivecomputer/zeagle_n2ition3.h +++ b/include/libdivecomputer/zeagle_n2ition3.h @@ -29,9 +29,6 @@ extern "C" { #endif /* __cplusplus */ -#define ZEAGLE_N2ITION3_MEMORY_SIZE 0x8000 -#define ZEAGLE_N2ITION3_PACKET_SIZE 64 - dc_status_t zeagle_n2ition3_device_open (dc_device_t **device, dc_context_t *context, const char *name); diff --git a/src/cressi_edy.c b/src/cressi_edy.c index 4ee3946..92a4053 100644 --- a/src/cressi_edy.c +++ b/src/cressi_edy.c @@ -37,7 +37,9 @@ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ ) -#define PAGESIZE (CRESSI_EDY_PACKET_SIZE / 4) +#define SZ_MEMORY 0x8000 +#define SZ_PACKET 0x80 +#define SZ_PAGE (SZ_PACKET / 4) #define BASE 0x4000 @@ -51,7 +53,7 @@ typedef struct cressi_edy_device_t { dc_device_t base; serial_t *port; - unsigned char fingerprint[PAGESIZE / 2]; + unsigned char fingerprint[SZ_PAGE / 2]; unsigned int model; } cressi_edy_device_t; @@ -278,18 +280,15 @@ cressi_edy_device_read (dc_device_t *abstract, unsigned int address, unsigned ch if (! device_is_cressi_edy (abstract)) return DC_STATUS_INVALIDARGS; - if ((address % (CRESSI_EDY_PACKET_SIZE / 4) != 0) || - (size % CRESSI_EDY_PACKET_SIZE != 0)) + if ((address % SZ_PAGE != 0) || + (size % SZ_PACKET != 0)) return DC_STATUS_INVALIDARGS; - // The data transmission is split in packages - // of maximum $CRESSI_EDY_PACKET_SIZE bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Read the package. - unsigned int number = address / (CRESSI_EDY_PACKET_SIZE / 4); - unsigned char answer[3 + CRESSI_EDY_PACKET_SIZE + 1] = {0}; + unsigned int number = address / SZ_PAGE; + unsigned char answer[3 + SZ_PACKET + 1] = {0}; unsigned char command[3] = {0x52, (number >> 8) & 0xFF, // high (number ) & 0xFF}; // low @@ -297,11 +296,11 @@ cressi_edy_device_read (dc_device_t *abstract, unsigned int address, unsigned ch if (rc != DC_STATUS_SUCCESS) return rc; - memcpy (data, answer + 3, CRESSI_EDY_PACKET_SIZE); + memcpy (data, answer + 3, SZ_PACKET); - nbytes += CRESSI_EDY_PACKET_SIZE; - address += CRESSI_EDY_PACKET_SIZE; - data += CRESSI_EDY_PACKET_SIZE; + nbytes += SZ_PACKET; + address += SZ_PACKET; + data += SZ_PACKET; } return DC_STATUS_SUCCESS; @@ -333,13 +332,13 @@ cressi_edy_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, CRESSI_EDY_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } return device_dump_read (abstract, dc_buffer_get_data (buffer), - dc_buffer_get_size (buffer), CRESSI_EDY_PACKET_SIZE); + dc_buffer_get_size (buffer), SZ_PACKET); } @@ -350,7 +349,7 @@ cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, v // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = CRESSI_EDY_PACKET_SIZE + + progress.maximum = SZ_PACKET + (RB_PROFILE_END - RB_PROFILE_BEGIN); device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); @@ -362,7 +361,7 @@ cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, v device_event_emit (abstract, DC_EVENT_DEVINFO, &devinfo); // Read the configuration data. - unsigned char config[CRESSI_EDY_PACKET_SIZE] = {0}; + unsigned char config[SZ_PACKET] = {0}; dc_status_t rc = cressi_edy_device_read (abstract, 0x7F80, config, sizeof (config)); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the configuration data."); @@ -370,7 +369,7 @@ cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, v } // Update and emit a progress event. - progress.current += CRESSI_EDY_PACKET_SIZE; + progress.current += SZ_PACKET; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Get the logbook pointers. @@ -388,7 +387,7 @@ cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, v unsigned int count = ringbuffer_distance (first, last, 0, RB_LOGBOOK_BEGIN, RB_LOGBOOK_END) + 1; // Get the profile pointer. - unsigned int eop = array_uint16_le (config + 0x7E) * PAGESIZE + BASE; + unsigned int eop = array_uint16_le (config + 0x7E) * SZ_PAGE + BASE; if (eop < RB_PROFILE_BEGIN || eop >= RB_PROFILE_END) { ERROR (abstract->context, "Invalid ringbuffer pointer detected."); return DC_STATUS_DATAFORMAT; @@ -406,7 +405,7 @@ cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, v unsigned int idx = last; for (unsigned int i = 0; i < count; ++i) { // Get the pointer to the profile data. - unsigned int current = array_uint16_le (config + 2 * idx) * PAGESIZE + BASE; + unsigned int current = array_uint16_le (config + 2 * idx) * SZ_PAGE + BASE; if (current < RB_PROFILE_BEGIN || current >= RB_PROFILE_END) { ERROR (abstract->context, "Invalid ringbuffer pointer detected."); return DC_STATUS_DATAFORMAT; @@ -415,7 +414,7 @@ cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, v // Position the pointer at the start of the header. if (current == RB_PROFILE_BEGIN) current = RB_PROFILE_END; - current -= PAGESIZE; + current -= SZ_PAGE; // Get the profile length. unsigned int length = ringbuffer_distance (current, previous, 1, RB_PROFILE_BEGIN, RB_PROFILE_END); @@ -424,21 +423,21 @@ cressi_edy_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, v while (nbytes < length) { if (address == RB_PROFILE_BEGIN) address = RB_PROFILE_END; - address -= CRESSI_EDY_PACKET_SIZE; - offset -= CRESSI_EDY_PACKET_SIZE; + address -= SZ_PACKET; + offset -= SZ_PACKET; // Read the memory page. - rc = cressi_edy_device_read (abstract, address, buffer + offset, CRESSI_EDY_PACKET_SIZE); + rc = cressi_edy_device_read (abstract, address, buffer + offset, SZ_PACKET); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the memory page."); return rc; } // Update and emit a progress event. - progress.current += CRESSI_EDY_PACKET_SIZE; + progress.current += SZ_PACKET; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); - nbytes += CRESSI_EDY_PACKET_SIZE; + nbytes += SZ_PACKET; } available = nbytes - length; diff --git a/src/mares_common.c b/src/mares_common.c index 419b09a..6cda828 100644 --- a/src/mares_common.c +++ b/src/mares_common.c @@ -213,9 +213,6 @@ mares_common_device_read (dc_device_t *abstract, unsigned int address, unsigned { mares_common_device_t *device = (mares_common_device_t*) abstract; - // The data transmission is split in packages - // of maximum $PACKETSIZE bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the packet size. diff --git a/src/mares_iconhd.c b/src/mares_iconhd.c index 05b2ef7..7d5e551 100644 --- a/src/mares_iconhd.c +++ b/src/mares_iconhd.c @@ -46,8 +46,10 @@ #define ACK 0xAA #define EOF 0xEA +#define SZ_MEMORY 0x100000 + #define RB_PROFILE_BEGIN 0xA000 -#define RB_PROFILE_END MARES_ICONHD_MEMORY_SIZE +#define RB_PROFILE_END SZ_MEMORY typedef struct mares_iconhd_device_t { dc_device_t base; @@ -334,7 +336,7 @@ mares_iconhd_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // pre-allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, MARES_ICONHD_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } @@ -349,7 +351,7 @@ mares_iconhd_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, { mares_iconhd_device_t *device = (mares_iconhd_device_t *) abstract; - dc_buffer_t *buffer = dc_buffer_new (MARES_ICONHD_MEMORY_SIZE); + dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -385,7 +387,7 @@ mares_iconhd_extract_dives (dc_device_t *abstract, const unsigned char data[], u if (abstract && !device_is_mares_iconhd (abstract)) return DC_STATUS_INVALIDARGS; - if (size < MARES_ICONHD_MEMORY_SIZE) + if (size < SZ_MEMORY) return DC_STATUS_DATAFORMAT; // Get the model code. diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index 5e68fe7..4c71a10 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -489,9 +489,6 @@ oceanic_atom2_device_read (dc_device_t *abstract, unsigned int address, unsigned if ((address % PAGESIZE != 0) || (size % PAGESIZE != 0)) return DC_STATUS_INVALIDARGS; - - // The data transmission is split in packages - // of maximum $PAGESIZE bytes. unsigned int nbytes = 0; while (nbytes < size) { @@ -529,9 +526,6 @@ oceanic_atom2_device_write (dc_device_t *abstract, unsigned int address, const u (size % PAGESIZE != 0)) return DC_STATUS_INVALIDARGS; - // The data transmission is split in packages - // of maximum $PAGESIZE bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Prepare to write the package. diff --git a/src/oceanic_veo250.c b/src/oceanic_veo250.c index 1ee539e..4003f4d 100644 --- a/src/oceanic_veo250.c +++ b/src/oceanic_veo250.c @@ -405,9 +405,6 @@ oceanic_veo250_device_read (dc_device_t *abstract, unsigned int address, unsigne (size % PAGESIZE != 0)) return DC_STATUS_INVALIDARGS; - // The data transmission is split in packages - // of maximum $PAGESIZE bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the number of packages. diff --git a/src/oceanic_vtpro.c b/src/oceanic_vtpro.c index d811c2d..21f9e97 100644 --- a/src/oceanic_vtpro.c +++ b/src/oceanic_vtpro.c @@ -476,9 +476,6 @@ oceanic_vtpro_device_read (dc_device_t *abstract, unsigned int address, unsigned (size % PAGESIZE != 0)) return DC_STATUS_INVALIDARGS; - // The data transmission is split in packages - // of maximum $PAGESIZE bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the number of packages. diff --git a/src/reefnet_sensus.c b/src/reefnet_sensus.c index b907e86..da22ee5 100644 --- a/src/reefnet_sensus.c +++ b/src/reefnet_sensus.c @@ -35,10 +35,13 @@ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ ) +#define SZ_MEMORY 32768 +#define SZ_HANDSHAKE 10 + typedef struct reefnet_sensus_device_t { dc_device_t base; serial_t *port; - unsigned char handshake[REEFNET_SENSUS_HANDSHAKE_SIZE]; + unsigned char handshake[SZ_HANDSHAKE]; unsigned int waiting; unsigned int timestamp; unsigned int devtime; @@ -183,12 +186,12 @@ reefnet_sensus_device_get_handshake (dc_device_t *abstract, unsigned char data[] if (! device_is_reefnet_sensus (abstract)) return DC_STATUS_INVALIDARGS; - if (size < REEFNET_SENSUS_HANDSHAKE_SIZE) { + if (size < SZ_HANDSHAKE) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_INVALIDARGS; } - memcpy (data, device->handshake, REEFNET_SENSUS_HANDSHAKE_SIZE); + memcpy (data, device->handshake, SZ_HANDSHAKE); return DC_STATUS_SUCCESS; } @@ -242,7 +245,7 @@ reefnet_sensus_handshake (reefnet_sensus_device_t *device) } // Receive the answer from the device. - unsigned char handshake[REEFNET_SENSUS_HANDSHAKE_SIZE + 2] = {0}; + unsigned char handshake[SZ_HANDSHAKE + 2] = {0}; n = serial_read (device->port, handshake, sizeof (handshake)); if (n != sizeof (handshake)) { ERROR (abstract->context, "Failed to receive the handshake."); @@ -263,7 +266,7 @@ reefnet_sensus_handshake (reefnet_sensus_device_t *device) device->devtime = array_uint32_le (handshake + 8); // Store the handshake packet. - memcpy (device->handshake, handshake + 2, REEFNET_SENSUS_HANDSHAKE_SIZE); + memcpy (device->handshake, handshake + 2, SZ_HANDSHAKE); // Emit a clock event. dc_event_clock_t clock; @@ -297,14 +300,14 @@ reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // pre-allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, REEFNET_SENSUS_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = 4 + REEFNET_SENSUS_MEMORY_SIZE + 2 + 3; + progress.maximum = 4 + SZ_MEMORY + 2 + 3; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Wake-up the device. @@ -325,7 +328,7 @@ reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Receive the answer from the device. unsigned int nbytes = 0; - unsigned char answer[4 + REEFNET_SENSUS_MEMORY_SIZE + 2 + 3] = {0}; + unsigned char answer[4 + SZ_MEMORY + 2 + 3] = {0}; while (nbytes < sizeof (answer)) { unsigned int len = sizeof (answer) - nbytes; if (len > 128) @@ -352,14 +355,14 @@ reefnet_sensus_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) } // Verify the checksum of the package. - unsigned short crc = array_uint16_le (answer + 4 + REEFNET_SENSUS_MEMORY_SIZE); - unsigned short ccrc = checksum_add_uint16 (answer + 4, REEFNET_SENSUS_MEMORY_SIZE, 0x00); + unsigned short crc = array_uint16_le (answer + 4 + SZ_MEMORY); + unsigned short ccrc = checksum_add_uint16 (answer + 4, SZ_MEMORY, 0x00); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; } - dc_buffer_append (buffer, answer + 4, REEFNET_SENSUS_MEMORY_SIZE); + dc_buffer_append (buffer, answer + 4, SZ_MEMORY); return DC_STATUS_SUCCESS; } @@ -371,7 +374,7 @@ reefnet_sensus_device_foreach (dc_device_t *abstract, dc_dive_callback_t callbac if (! device_is_reefnet_sensus (abstract)) return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (REEFNET_SENSUS_MEMORY_SIZE); + dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c index a688ef9..770fead 100644 --- a/src/reefnet_sensuspro.c +++ b/src/reefnet_sensuspro.c @@ -35,10 +35,13 @@ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ ) +#define SZ_MEMORY 56320 +#define SZ_HANDSHAKE 10 + typedef struct reefnet_sensuspro_device_t { dc_device_t base; serial_t *port; - unsigned char handshake[REEFNET_SENSUSPRO_HANDSHAKE_SIZE]; + unsigned char handshake[SZ_HANDSHAKE]; unsigned int timestamp; unsigned int devtime; dc_ticks_t systime; @@ -156,12 +159,12 @@ reefnet_sensuspro_device_get_handshake (dc_device_t *abstract, unsigned char dat if (! device_is_reefnet_sensuspro (abstract)) return DC_STATUS_INVALIDARGS; - if (size < REEFNET_SENSUSPRO_HANDSHAKE_SIZE) { + if (size < SZ_HANDSHAKE) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_INVALIDARGS; } - memcpy (data, device->handshake, REEFNET_SENSUSPRO_HANDSHAKE_SIZE); + memcpy (data, device->handshake, SZ_HANDSHAKE); return DC_STATUS_SUCCESS; } @@ -210,7 +213,7 @@ reefnet_sensuspro_handshake (reefnet_sensuspro_device_t *device) serial_set_break (device->port, 1); // Receive the handshake from the dive computer. - unsigned char handshake[REEFNET_SENSUSPRO_HANDSHAKE_SIZE + 2] = {0}; + unsigned char handshake[SZ_HANDSHAKE + 2] = {0}; int rc = serial_read (device->port, handshake, sizeof (handshake)); if (rc != sizeof (handshake)) { ERROR (abstract->context, "Failed to receive the handshake."); @@ -221,8 +224,8 @@ reefnet_sensuspro_handshake (reefnet_sensuspro_device_t *device) serial_set_break (device->port, 0); // Verify the checksum of the handshake packet. - unsigned short crc = array_uint16_le (handshake + REEFNET_SENSUSPRO_HANDSHAKE_SIZE); - unsigned short ccrc = checksum_crc_ccitt_uint16 (handshake, REEFNET_SENSUSPRO_HANDSHAKE_SIZE); + unsigned short crc = array_uint16_le (handshake + SZ_HANDSHAKE); + unsigned short ccrc = checksum_crc_ccitt_uint16 (handshake, SZ_HANDSHAKE); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; @@ -233,7 +236,7 @@ reefnet_sensuspro_handshake (reefnet_sensuspro_device_t *device) device->devtime = array_uint32_le (handshake + 6); // Store the handshake packet. - memcpy (device->handshake, handshake, REEFNET_SENSUSPRO_HANDSHAKE_SIZE); + memcpy (device->handshake, handshake, SZ_HANDSHAKE); // Emit a clock event. dc_event_clock_t clock; @@ -285,14 +288,14 @@ reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // pre-allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, REEFNET_SENSUSPRO_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = REEFNET_SENSUSPRO_MEMORY_SIZE + 2; + progress.maximum = SZ_MEMORY + 2; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Wake-up the device and send the instruction code. @@ -301,7 +304,7 @@ reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) return rc; unsigned int nbytes = 0; - unsigned char answer[REEFNET_SENSUSPRO_MEMORY_SIZE + 2] = {0}; + unsigned char answer[SZ_MEMORY + 2] = {0}; while (nbytes < sizeof (answer)) { unsigned int len = sizeof (answer) - nbytes; if (len > 256) @@ -320,14 +323,14 @@ reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) nbytes += len; } - unsigned short crc = array_uint16_le (answer + REEFNET_SENSUSPRO_MEMORY_SIZE); - unsigned short ccrc = checksum_crc_ccitt_uint16 (answer, REEFNET_SENSUSPRO_MEMORY_SIZE); + unsigned short crc = array_uint16_le (answer + SZ_MEMORY); + unsigned short ccrc = checksum_crc_ccitt_uint16 (answer, SZ_MEMORY); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; } - dc_buffer_append (buffer, answer, REEFNET_SENSUSPRO_MEMORY_SIZE); + dc_buffer_append (buffer, answer, SZ_MEMORY); return DC_STATUS_SUCCESS; } @@ -339,7 +342,7 @@ reefnet_sensuspro_device_foreach (dc_device_t *abstract, dc_dive_callback_t call if (! device_is_reefnet_sensuspro (abstract)) return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (REEFNET_SENSUSPRO_MEMORY_SIZE); + dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; diff --git a/src/reefnet_sensusultra.c b/src/reefnet_sensusultra.c index da73b6c..3bcc30f 100644 --- a/src/reefnet_sensusultra.c +++ b/src/reefnet_sensusultra.c @@ -36,6 +36,12 @@ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ ) +#define SZ_PACKET 512 +#define SZ_MEMORY 2080768 +#define SZ_USER 16384 +#define SZ_HANDSHAKE 24 +#define SZ_SENSE 6 + #define PROMPT 0xA5 #define ACCEPT PROMPT #define REJECT 0x00 @@ -43,7 +49,7 @@ typedef struct reefnet_sensusultra_device_t { dc_device_t base; serial_t *port; - unsigned char handshake[REEFNET_SENSUSULTRA_HANDSHAKE_SIZE]; + unsigned char handshake[SZ_HANDSHAKE]; unsigned int maxretries; unsigned int timestamp; unsigned int devtime; @@ -163,12 +169,12 @@ reefnet_sensusultra_device_get_handshake (dc_device_t *abstract, unsigned char d if (! device_is_reefnet_sensusultra (abstract)) return DC_STATUS_INVALIDARGS; - if (size < REEFNET_SENSUSULTRA_HANDSHAKE_SIZE) { + if (size < SZ_HANDSHAKE) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_INVALIDARGS; } - memcpy (data, device->handshake, REEFNET_SENSUSULTRA_HANDSHAKE_SIZE); + memcpy (data, device->handshake, SZ_HANDSHAKE); return DC_STATUS_SUCCESS; } @@ -304,7 +310,7 @@ static dc_status_t reefnet_sensusultra_handshake (reefnet_sensusultra_device_t *device, unsigned short value) { // Wake-up the device. - unsigned char handshake[REEFNET_SENSUSULTRA_HANDSHAKE_SIZE + 2] = {0}; + unsigned char handshake[SZ_HANDSHAKE + 2] = {0}; dc_status_t rc = reefnet_sensusultra_packet (device, handshake, sizeof (handshake), 0); if (rc != DC_STATUS_SUCCESS) return rc; @@ -314,7 +320,7 @@ reefnet_sensusultra_handshake (reefnet_sensusultra_device_t *device, unsigned sh device->devtime = array_uint32_le (handshake + 4); // Store the handshake packet. - memcpy (device->handshake, handshake, REEFNET_SENSUSULTRA_HANDSHAKE_SIZE); + memcpy (device->handshake, handshake, SZ_HANDSHAKE); // Emit a clock event. dc_event_clock_t clock; @@ -343,7 +349,7 @@ reefnet_sensusultra_page (reefnet_sensusultra_device_t *device, unsigned char *d { dc_device_t *abstract = (dc_device_t *) device; - assert (size >= REEFNET_SENSUSULTRA_PACKET_SIZE + 4); + assert (size >= SZ_PACKET + 4); unsigned int nretries = 0; dc_status_t rc = DC_STATUS_SUCCESS; @@ -416,14 +422,14 @@ reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // pre-allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE; + progress.maximum = SZ_MEMORY; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Wake-up the device and send the instruction code. @@ -433,19 +439,19 @@ reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) unsigned int nbytes = 0; unsigned int npages = 0; - while (nbytes < REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE) { + while (nbytes < SZ_MEMORY) { // Receive the packet. - unsigned char packet[REEFNET_SENSUSULTRA_PACKET_SIZE + 4] = {0}; + unsigned char packet[SZ_PACKET + 4] = {0}; rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages); if (rc != DC_STATUS_SUCCESS) return rc; // Update and emit a progress event. - progress.current += REEFNET_SENSUSULTRA_PACKET_SIZE; + progress.current += SZ_PACKET; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Prepend the packet to the buffer. - if (!dc_buffer_prepend (buffer, packet + 2, REEFNET_SENSUSULTRA_PACKET_SIZE)) { + if (!dc_buffer_prepend (buffer, packet + 2, SZ_PACKET)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } @@ -455,7 +461,7 @@ reefnet_sensusultra_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) if (rc != DC_STATUS_SUCCESS) return rc; - nbytes += REEFNET_SENSUSULTRA_PACKET_SIZE; + nbytes += SZ_PACKET; npages++; } @@ -471,7 +477,7 @@ reefnet_sensusultra_device_read_user (dc_device_t *abstract, unsigned char *data if (! device_is_reefnet_sensusultra (abstract)) return DC_STATUS_INVALIDARGS; - if (size < REEFNET_SENSUSULTRA_MEMORY_USER_SIZE) { + if (size < SZ_USER) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_INVALIDARGS; } @@ -483,22 +489,22 @@ reefnet_sensusultra_device_read_user (dc_device_t *abstract, unsigned char *data unsigned int nbytes = 0; unsigned int npages = 0; - while (nbytes < REEFNET_SENSUSULTRA_MEMORY_USER_SIZE) { + while (nbytes < SZ_USER) { // Receive the packet. - unsigned char packet[REEFNET_SENSUSULTRA_PACKET_SIZE + 4] = {0}; + unsigned char packet[SZ_PACKET + 4] = {0}; rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages); if (rc != DC_STATUS_SUCCESS) return rc; // Append the packet to the buffer. - memcpy (data + nbytes, packet + 2, REEFNET_SENSUSULTRA_PACKET_SIZE); + memcpy (data + nbytes, packet + 2, SZ_PACKET); // Accept the packet. rc = reefnet_sensusultra_send_uchar (device, ACCEPT); if (rc != DC_STATUS_SUCCESS) return rc; - nbytes += REEFNET_SENSUSULTRA_PACKET_SIZE; + nbytes += SZ_PACKET; npages++; } @@ -514,14 +520,14 @@ reefnet_sensusultra_device_write_user (dc_device_t *abstract, const unsigned cha if (! device_is_reefnet_sensusultra (abstract)) return DC_STATUS_INVALIDARGS; - if (size < REEFNET_SENSUSULTRA_MEMORY_USER_SIZE) { + if (size < SZ_USER) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_INVALIDARGS; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = REEFNET_SENSUSULTRA_MEMORY_USER_SIZE + 2; + progress.maximum = SZ_USER + 2; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Wake-up the device and send the instruction code. @@ -530,7 +536,7 @@ reefnet_sensusultra_device_write_user (dc_device_t *abstract, const unsigned cha return rc; // Send the data to the device. - for (unsigned int i = 0; i < REEFNET_SENSUSULTRA_MEMORY_USER_SIZE; ++i) { + for (unsigned int i = 0; i < SZ_USER; ++i) { rc = reefnet_sensusultra_send_uchar (device, data[i]); if (rc != DC_STATUS_SUCCESS) return rc; @@ -541,7 +547,7 @@ reefnet_sensusultra_device_write_user (dc_device_t *abstract, const unsigned cha } // Send the checksum to the device. - unsigned short crc = checksum_crc_ccitt_uint16 (data, REEFNET_SENSUSULTRA_MEMORY_USER_SIZE); + unsigned short crc = checksum_crc_ccitt_uint16 (data, SZ_USER); rc = reefnet_sensusultra_send_ushort (device, crc); if (rc != DC_STATUS_SUCCESS) return rc; @@ -611,7 +617,7 @@ reefnet_sensusultra_device_sense (dc_device_t *abstract, unsigned char *data, un if (! device_is_reefnet_sensusultra (abstract)) return DC_STATUS_INVALIDARGS; - if (size < REEFNET_SENSUSULTRA_SENSE_SIZE) { + if (size < SZ_SENSE) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_INVALIDARGS; } @@ -622,12 +628,12 @@ reefnet_sensusultra_device_sense (dc_device_t *abstract, unsigned char *data, un return rc; // Receive the packet. - unsigned char package[REEFNET_SENSUSULTRA_SENSE_SIZE + 2] = {0}; + unsigned char package[SZ_SENSE + 2] = {0}; rc = reefnet_sensusultra_packet (device, package, sizeof (package), 0); if (rc != DC_STATUS_SUCCESS) return rc; - memcpy (data, package, REEFNET_SENSUSULTRA_SENSE_SIZE); + memcpy (data, package, SZ_SENSE); return DC_STATUS_SUCCESS; } @@ -711,7 +717,7 @@ reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t ca if (! device_is_reefnet_sensusultra (abstract)) return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE); + dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) { ERROR (abstract->context, "Failed to allocate memory."); return DC_STATUS_NOMEMORY; @@ -719,7 +725,7 @@ reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t ca // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE; + progress.maximum = SZ_MEMORY; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Wake-up the device and send the instruction code. @@ -735,9 +741,9 @@ reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t ca unsigned int nbytes = 0; unsigned int npages = 0; - while (nbytes < REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE) { + while (nbytes < SZ_MEMORY) { // Receive the packet. - unsigned char packet[REEFNET_SENSUSULTRA_PACKET_SIZE + 4] = {0}; + unsigned char packet[SZ_PACKET + 4] = {0}; rc = reefnet_sensusultra_page (device, packet, sizeof (packet), npages); if (rc != DC_STATUS_SUCCESS) { dc_buffer_free (buffer); @@ -745,22 +751,22 @@ reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t ca } // Update and emit a progress event. - progress.current += REEFNET_SENSUSULTRA_PACKET_SIZE; + progress.current += SZ_PACKET; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Abort the transfer if the page contains no useful data. - if (array_isequal (packet + 2, REEFNET_SENSUSULTRA_PACKET_SIZE, 0xFF) && nbytes != 0) + if (array_isequal (packet + 2, SZ_PACKET, 0xFF) && nbytes != 0) break; // Prepend the packet to the buffer. - if (!dc_buffer_prepend (buffer, packet + 2, REEFNET_SENSUSULTRA_PACKET_SIZE)) { + if (!dc_buffer_prepend (buffer, packet + 2, SZ_PACKET)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Update the parser state. - remaining += REEFNET_SENSUSULTRA_PACKET_SIZE; - previous += REEFNET_SENSUSULTRA_PACKET_SIZE; + remaining += SZ_PACKET; + previous += SZ_PACKET; // Parse the page data. int aborted = 0; @@ -780,7 +786,7 @@ reefnet_sensusultra_device_foreach (dc_device_t *abstract, dc_dive_callback_t ca return rc; } - nbytes += REEFNET_SENSUSULTRA_PACKET_SIZE; + nbytes += SZ_PACKET; npages++; } diff --git a/src/suunto_common2.c b/src/suunto_common2.c index 33bb654..35d652e 100644 --- a/src/suunto_common2.c +++ b/src/suunto_common2.c @@ -138,9 +138,6 @@ suunto_common2_device_reset_maxdepth (dc_device_t *abstract) dc_status_t suunto_common2_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size) { - // The data transmission is split in packages - // of maximum $SZ_PACKET bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the package size. @@ -174,9 +171,6 @@ suunto_common2_device_read (dc_device_t *abstract, unsigned int address, unsigne dc_status_t suunto_common2_device_write (dc_device_t *abstract, unsigned int address, const unsigned char data[], unsigned int size) { - // The data transmission is split in packages - // of maximum $SZ_PACKET bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the package size. diff --git a/src/suunto_eon.c b/src/suunto_eon.c index 2275ff7..9f06e20 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -36,6 +36,8 @@ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ ) +#define SZ_MEMORY 0x900 + typedef struct suunto_eon_device_t { suunto_common_device_t base; serial_t *port; @@ -59,7 +61,7 @@ static const device_backend_t suunto_eon_device_backend = { static const suunto_common_layout_t suunto_eon_layout = { 0, /* eop */ 0x100, /* rb_profile_begin */ - SUUNTO_EON_MEMORY_SIZE, /* rb_profile_end */ + SZ_MEMORY, /* rb_profile_end */ 6, /* fp_offset */ 3 /* peek */ }; @@ -164,14 +166,14 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // pre-allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SUUNTO_EON_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = SUUNTO_EON_MEMORY_SIZE + 1; + progress.maximum = SZ_MEMORY + 1; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Send the command. @@ -183,7 +185,7 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) } // Receive the answer. - unsigned char answer[SUUNTO_EON_MEMORY_SIZE + 1] = {0}; + unsigned char answer[SZ_MEMORY + 1] = {0}; rc = serial_read (device->port, answer, sizeof (answer)); if (rc != sizeof (answer)) { ERROR (abstract->context, "Failed to receive the answer."); @@ -202,7 +204,7 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) return DC_STATUS_PROTOCOL; } - dc_buffer_append (buffer, answer, SUUNTO_EON_MEMORY_SIZE); + dc_buffer_append (buffer, answer, SZ_MEMORY); return DC_STATUS_SUCCESS; } @@ -211,7 +213,7 @@ suunto_eon_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) static dc_status_t suunto_eon_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, void *userdata) { - dc_buffer_t *buffer = dc_buffer_new (SUUNTO_EON_MEMORY_SIZE); + dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -290,7 +292,7 @@ suunto_eon_extract_dives (dc_device_t *abstract, const unsigned char data[], uns if (abstract && !device_is_suunto_eon (abstract)) return DC_STATUS_INVALIDARGS; - if (size < SUUNTO_EON_MEMORY_SIZE) + if (size < SZ_MEMORY) return DC_STATUS_DATAFORMAT; return suunto_common_extract_dives (device, &suunto_eon_layout, data, callback, userdata); diff --git a/src/suunto_solution.c b/src/suunto_solution.c index a66c64f..b4c7054 100644 --- a/src/suunto_solution.c +++ b/src/suunto_solution.c @@ -35,6 +35,8 @@ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ ) +#define SZ_MEMORY 256 + #define RB_PROFILE_BEGIN 0x020 #define RB_PROFILE_END 0x100 @@ -157,7 +159,7 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SUUNTO_SOLUTION_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } @@ -166,7 +168,7 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = SUUNTO_SOLUTION_MEMORY_SIZE - 1 + 2; + progress.maximum = SZ_MEMORY - 1 + 2; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); int n = 0; @@ -197,7 +199,7 @@ suunto_solution_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); data[0] = 0x00; - for (unsigned int i = 1; i < SUUNTO_SOLUTION_MEMORY_SIZE; ++i) { + for (unsigned int i = 1; i < SZ_MEMORY; ++i) { // Receive: 0x01, i, data[i] n = serial_read (device->port, answer, 3); if (n != 3) return EXITCODE (n); @@ -263,7 +265,7 @@ suunto_solution_device_foreach (dc_device_t *abstract, dc_dive_callback_t callba if (! device_is_suunto_solution (abstract)) return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (SUUNTO_SOLUTION_MEMORY_SIZE); + dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -296,7 +298,7 @@ suunto_solution_extract_dives (dc_device_t *abstract, const unsigned char data[] if (abstract && !device_is_suunto_solution (abstract)) return DC_STATUS_INVALIDARGS; - if (size < SUUNTO_SOLUTION_MEMORY_SIZE) + if (size < SZ_MEMORY) return DC_STATUS_DATAFORMAT; unsigned char buffer[RB_PROFILE_END - RB_PROFILE_BEGIN] = {0}; diff --git a/src/suunto_vyper.c b/src/suunto_vyper.c index 12956e3..10715ae 100644 --- a/src/suunto_vyper.c +++ b/src/suunto_vyper.c @@ -40,6 +40,9 @@ #define MIN(a,b) (((a) < (b)) ? (a) : (b)) #define MAX(a,b) (((a) > (b)) ? (a) : (b)) +#define SZ_MEMORY 0x2000 +#define SZ_PACKET 32 + #define HDR_DEVINFO_VYPER 0x24 #define HDR_DEVINFO_SPYDER 0x16 #define HDR_DEVINFO_BEGIN (HDR_DEVINFO_SPYDER) @@ -71,7 +74,7 @@ static const device_backend_t suunto_vyper_device_backend = { static const suunto_common_layout_t suunto_vyper_layout = { 0x51, /* eop */ 0x71, /* rb_profile_begin */ - SUUNTO_VYPER_MEMORY_SIZE, /* rb_profile_end */ + SZ_MEMORY, /* rb_profile_end */ 9, /* fp_offset */ 5 /* peek */ }; @@ -79,7 +82,7 @@ static const suunto_common_layout_t suunto_vyper_layout = { static const suunto_common_layout_t suunto_spyder_layout = { 0x1C, /* eop */ 0x4C, /* rb_profile_begin */ - SUUNTO_VYPER_MEMORY_SIZE, /* rb_profile_end */ + SZ_MEMORY, /* rb_profile_end */ 6, /* fp_offset */ 3 /* peek */ }; @@ -283,16 +286,13 @@ suunto_vyper_device_read (dc_device_t *abstract, unsigned int address, unsigned if (! device_is_suunto_vyper (abstract)) return DC_STATUS_INVALIDARGS; - // The data transmission is split in packages - // of maximum $SUUNTO_VYPER_PACKET_SIZE bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the package size. - unsigned int len = MIN (size - nbytes, SUUNTO_VYPER_PACKET_SIZE); + unsigned int len = MIN (size - nbytes, SZ_PACKET); // Read the package. - unsigned char answer[SUUNTO_VYPER_PACKET_SIZE + 5] = {0}; + unsigned char answer[SZ_PACKET + 5] = {0}; unsigned char command[5] = {0x05, (address >> 8) & 0xFF, // high (address ) & 0xFF, // low @@ -322,13 +322,10 @@ suunto_vyper_device_write (dc_device_t *abstract, unsigned int address, const un if (! device_is_suunto_vyper (abstract)) return DC_STATUS_INVALIDARGS; - // The data transmission is split in packages - // of maximum $SUUNTO_VYPER_PACKET_SIZE bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the package size. - unsigned int len = MIN (size - nbytes, SUUNTO_VYPER_PACKET_SIZE); + unsigned int len = MIN (size - nbytes, SZ_PACKET); // Prepare to write the package. unsigned char panswer[3] = {0}; @@ -339,7 +336,7 @@ suunto_vyper_device_write (dc_device_t *abstract, unsigned int address, const un // Write the package. unsigned char wanswer[5] = {0}; - unsigned char wcommand[SUUNTO_VYPER_PACKET_SIZE + 5] = {0x06, + unsigned char wcommand[SZ_PACKET + 5] = {0x06, (address >> 8) & 0xFF, // high (address ) & 0xFF, // low len, // count @@ -382,13 +379,10 @@ suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc return rc; } - // The data transmission is split in packages - // of maximum $SUUNTO_VYPER_PACKET_SIZE bytes. - unsigned int nbytes = 0; for (unsigned int npackages = 0;; ++npackages) { // Receive the header of the package. - unsigned char answer[SUUNTO_VYPER_PACKET_SIZE + 3] = {0}; + unsigned char answer[SZ_PACKET + 3] = {0}; int n = serial_read (device->port, answer, 2); if (n != 2) { // If no data is received because a timeout occured, we assume @@ -407,7 +401,7 @@ suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc // Verify the header of the package. if (answer[0] != command[0] || - answer[1] > SUUNTO_VYPER_PACKET_SIZE) { + answer[1] > SZ_PACKET) { ERROR (abstract->context, "Unexpected answer start byte(s)."); return DC_STATUS_PROTOCOL; } @@ -454,12 +448,12 @@ suunto_vyper_read_dive (dc_device_t *abstract, dc_buffer_t *buffer, int init, dc nbytes += len; - // If a package is smaller than $SUUNTO_VYPER_PACKET_SIZE bytes, + // If a package is smaller than $SZ_PACKET bytes, // we assume it's the last packet and the transmission can be // finished early. However, this approach does not work if the - // last packet is exactly $SUUNTO_VYPER_PACKET_SIZE bytes long! + // last packet is exactly $SZ_PACKET bytes long! #if 0 - if (len != SUUNTO_VYPER_PACKET_SIZE) + if (len != SZ_PACKET) break; #endif } @@ -498,13 +492,13 @@ suunto_vyper_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SUUNTO_VYPER_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } return device_dump_read (abstract, dc_buffer_get_data (buffer), - dc_buffer_get_size (buffer), SUUNTO_VYPER_PACKET_SIZE); + dc_buffer_get_size (buffer), SZ_PACKET); } @@ -518,7 +512,7 @@ suunto_vyper_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = SUUNTO_VYPER_MEMORY_SIZE; + progress.maximum = SZ_MEMORY; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); // Read the device info. The Vyper and the Spyder store this data @@ -603,7 +597,7 @@ suunto_vyper_extract_dives (dc_device_t *abstract, const unsigned char data[], u if (abstract && !device_is_suunto_vyper (abstract)) return DC_STATUS_INVALIDARGS; - if (size < SUUNTO_VYPER_MEMORY_SIZE) + if (size < SZ_MEMORY) return DC_STATUS_DATAFORMAT; const suunto_common_layout_t *layout = &suunto_vyper_layout; diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index 9c14213..a6d337a 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -36,6 +36,8 @@ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ ) +#define SZ_MEMORY 2048 + #define RB_PROFILE_BEGIN 0x000 #define RB_PROFILE_END 0x600 #define RB_PROFILE_NEXT(a) ringbuffer_increment (a, 1, RB_PROFILE_BEGIN, RB_PROFILE_END) @@ -204,17 +206,17 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // pre-allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, UWATEC_ALADIN_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_reserve (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } // Enable progress notifications. dc_event_progress_t progress = EVENT_PROGRESS_INITIALIZER; - progress.maximum = UWATEC_ALADIN_MEMORY_SIZE + 2; + progress.maximum = SZ_MEMORY + 2; device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); - unsigned char answer[UWATEC_ALADIN_MEMORY_SIZE + 2] = {0}; + unsigned char answer[SZ_MEMORY + 2] = {0}; // Receive the header of the package. for (unsigned int i = 0; i < 4;) { @@ -256,8 +258,8 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) array_reverse_bits (answer, sizeof (answer)); // Verify the checksum of the package. - unsigned short crc = array_uint16_le (answer + UWATEC_ALADIN_MEMORY_SIZE); - unsigned short ccrc = checksum_add_uint16 (answer, UWATEC_ALADIN_MEMORY_SIZE, 0x0000); + unsigned short crc = array_uint16_le (answer + SZ_MEMORY); + unsigned short ccrc = checksum_add_uint16 (answer, SZ_MEMORY, 0x0000); if (ccrc != crc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; @@ -273,7 +275,7 @@ uwatec_aladin_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) clock.devtime = device->devtime; device_event_emit (abstract, DC_EVENT_CLOCK, &clock); - dc_buffer_append (buffer, answer, UWATEC_ALADIN_MEMORY_SIZE); + dc_buffer_append (buffer, answer, SZ_MEMORY); return DC_STATUS_SUCCESS; } @@ -285,7 +287,7 @@ uwatec_aladin_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback if (! device_is_uwatec_aladin (abstract)) return DC_STATUS_INVALIDARGS; - dc_buffer_t *buffer = dc_buffer_new (UWATEC_ALADIN_MEMORY_SIZE); + dc_buffer_t *buffer = dc_buffer_new (SZ_MEMORY); if (buffer == NULL) return DC_STATUS_NOMEMORY; @@ -320,7 +322,7 @@ uwatec_aladin_extract_dives (dc_device_t *abstract, const unsigned char* data, u if (abstract && !device_is_uwatec_aladin (abstract)) return DC_STATUS_INVALIDARGS; - if (size < UWATEC_ALADIN_MEMORY_SIZE) + if (size < SZ_MEMORY) return DC_STATUS_DATAFORMAT; // The logbook ring buffer can store up to 37 dives. But diff --git a/src/zeagle_n2ition3.c b/src/zeagle_n2ition3.c index b90037f..a7a6721 100644 --- a/src/zeagle_n2ition3.c +++ b/src/zeagle_n2ition3.c @@ -37,6 +37,9 @@ rc == -1 ? DC_STATUS_IO : DC_STATUS_TIMEOUT \ ) +#define SZ_MEMORY 0x8000 +#define SZ_PACKET 64 + #define RB_PROFILE_BEGIN 0x3FA0 #define RB_PROFILE_END 0x7EC0 @@ -238,18 +241,15 @@ zeagle_n2ition3_device_read (dc_device_t *abstract, unsigned int address, unsign if (! device_is_zeagle_n2ition3 (abstract)) return DC_STATUS_INVALIDARGS; - // The data transmission is split in packages - // of maximum $ZEAGLE_N2ITION3_PACKET_SIZE bytes. - unsigned int nbytes = 0; while (nbytes < size) { // Calculate the package size. unsigned int len = size - nbytes; - if (len > ZEAGLE_N2ITION3_PACKET_SIZE) - len = ZEAGLE_N2ITION3_PACKET_SIZE; + if (len > SZ_PACKET) + len = SZ_PACKET; // Read the package. - unsigned char answer[13 + ZEAGLE_N2ITION3_PACKET_SIZE + 6] = {0}; + unsigned char answer[13 + SZ_PACKET + 6] = {0}; unsigned char command[13] = {0x02, 0x08, 0x00, 0x4D, (address ) & 0xFF, // low (address >> 8) & 0xFF, // high @@ -279,13 +279,13 @@ zeagle_n2ition3_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Erase the current contents of the buffer and // allocate the required amount of memory. - if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, ZEAGLE_N2ITION3_MEMORY_SIZE)) { + if (!dc_buffer_clear (buffer) || !dc_buffer_resize (buffer, SZ_MEMORY)) { ERROR (abstract->context, "Insufficient buffer space available."); return DC_STATUS_NOMEMORY; } return device_dump_read (abstract, dc_buffer_get_data (buffer), - dc_buffer_get_size (buffer), ZEAGLE_N2ITION3_PACKET_SIZE); + dc_buffer_get_size (buffer), SZ_PACKET); } @@ -389,7 +389,7 @@ zeagle_n2ition3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callba if (address == RB_PROFILE_BEGIN) address = RB_PROFILE_END; - unsigned int len = ZEAGLE_N2ITION3_PACKET_SIZE; + unsigned int len = SZ_PACKET; if (RB_PROFILE_BEGIN + len > address) len = address - RB_PROFILE_BEGIN; // End of ringbuffer. if (nbytes + len > remaining)