--- src/cochran_commander.h | 2 -- 1 file changed, 2 deletions(-)
diff --git a/src/cochran_commander.h b/src/cochran_commander.h index 8fa6228..95d4da4 100644 --- a/src/cochran_commander.h +++ b/src/cochran_commander.h @@ -89,8 +89,6 @@ typedef struct cochran_data_t {
unsigned int sample_data_offset; unsigned int sample_size; - unsigned int last_interdive_offset; - unsigned int last_entry_offset;
cochran_config_t conf; } cochran_data_t;
--- src/cochran_commander.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/src/cochran_commander.c b/src/cochran_commander.c index fdcac67..fdb1390 100644 --- a/src/cochran_commander.c +++ b/src/cochran_commander.c @@ -61,6 +61,7 @@ cochran_packet (cochran_device_t *device, const unsigned char command[], dc_device_t *abstract = (dc_device_t *) device; unsigned int bytes_read = 0, n, read_size; unsigned int ptr; + int rc;
if (device_is_cancelled (abstract)) return DC_STATUS_CANCELLED; @@ -80,10 +81,21 @@ cochran_packet (cochran_device_t *device, const unsigned char command[],
// Weird but I only get the right result when I do it twice // Rates are odd, like 825600 for the EMC, 115200 for commander - serial_configure(device->port, device->data.conf.high_baud, 8, + rc = serial_configure(device->port, device->data.conf.high_baud, 8, SERIAL_PARITY_NONE, 2, SERIAL_FLOWCONTROL_NONE); - serial_configure(device->port, device->data.conf.high_baud, 8, + if (rc == -1) { + ERROR (abstract->context, "Failed to set the high baud rate."); + free (device); + return DC_STATUS_IO; + } + + rc = serial_configure(device->port, device->data.conf.high_baud, 8, SERIAL_PARITY_NONE, 2, SERIAL_FLOWCONTROL_NONE); + if (rc == -1) { + ERROR (abstract->context, "Failed to set the high baud rate."); + free (device); + return DC_STATUS_IO; + } }
// Receive the answer from the device.
The progress bar now shows a single progress for all reads. --- src/cochran_commander.c | 69 ++++++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 32 deletions(-)
diff --git a/src/cochran_commander.c b/src/cochran_commander.c index fdb1390..838f423 100644 --- a/src/cochran_commander.c +++ b/src/cochran_commander.c @@ -116,7 +116,7 @@ cochran_packet (cochran_device_t *device, const unsigned char command[], bytes_read += n;
if (device->progress) { - device->progress->current = bytes_read; + device->progress->current += n; device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); } } @@ -517,17 +517,6 @@ cochran_read_logbook (dc_device_t *abstract) return DC_STATUS_NOMEMORY; }
- // Enable progress notifications. - device->progress = malloc(sizeof(dc_event_progress_t)); - if (device->progress == NULL) { - ERROR (abstract->context, "Failed to allocate memory."); - return DC_STATUS_NOMEMORY; - } - - device->progress->current = 0; - device->progress->maximum = d->logbook_size; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); - serial_sleep(device->port, 800);
// set back to 9600 baud @@ -537,10 +526,6 @@ cochran_read_logbook (dc_device_t *abstract) rc = cochran_commander_device_read(abstract, 0, d->logbook, d->logbook_size);
- device->progress->current = d->logbook_size; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); - free (device->progress); - return rc; }
@@ -632,17 +617,6 @@ cochran_read_samples(dc_device_t *abstract) return DC_STATUS_NOMEMORY; }
- // Enable progress notifications. - device->progress = malloc(sizeof(dc_event_progress_t)); - if (device->progress == NULL) { - ERROR (abstract->context, "Failed to allocate memory."); - return DC_STATUS_NOMEMORY; - } - - device->progress->current = 0; - device->progress->maximum = d->sample_size; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); - serial_sleep(device->port, 800);
// set back to 9600 baud @@ -652,14 +626,9 @@ cochran_read_samples(dc_device_t *abstract) rc = cochran_commander_device_read (abstract, d->sample_data_offset, d->sample, d->sample_size); if (rc != DC_STATUS_SUCCESS) { - free (device->progress); ERROR (abstract->context, "Failed to read the sample data."); return rc; } - - device->progress->current = d->sample_size; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); - free (device->progress); }
return DC_STATUS_SUCCESS; @@ -674,12 +643,40 @@ cochran_commander_device_read_all (dc_device_t *abstract) cochran_config_t *conf = &d->conf;
dc_status_t rc; + int sample_size; + int max_config, max_misc, max_logbook, max_sample; + + if ((conf->model & 0xFFFF0000) == COCHRAN_MODEL_COMMANDER_FAMILY) + sample_size = 2; + else + sample_size = 3; + + max_config = 512 * 4; // max config page size + max_misc = 1500; // misc size + max_logbook = 1024 * conf->log_size; // max logbook size + max_sample = 1450 * 3600 * sample_size; // max sample size + + // Enable progress notifications. + device->progress = malloc(sizeof(dc_event_progress_t)); + if (device->progress == NULL) { + ERROR (abstract->context, "Failed to allocate memory."); + return DC_STATUS_NOMEMORY; + } + + device->progress->current = 0; + device->progress->maximum = max_config + max_misc + max_logbook + + max_sample; + device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress);
// Read config rc = cochran_read_config(abstract); if (rc != DC_STATUS_SUCCESS) return rc;
+ // Update max based on what's read + device->progress->maximum -= max_config - device->progress->current; + device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); + rc = cochran_read_misc(abstract); if (rc != DC_STATUS_SUCCESS) return rc; @@ -693,6 +690,9 @@ cochran_commander_device_read_all (dc_device_t *abstract) d->logbook_size = ((d->dive_count * conf->log_size) & 0xFFFFC000) + 0x4000;
+ device->progress->maximum -= max_logbook - d->logbook_size; + device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); + rc = cochran_read_logbook(abstract); if (rc != DC_STATUS_SUCCESS) return rc; @@ -701,10 +701,15 @@ cochran_commander_device_read_all (dc_device_t *abstract) cochran_find_fingerprint(abstract); cochran_get_sample_parms(abstract);
+ device->progress->maximum -= max_sample - d->sample_size; + device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); + rc = cochran_read_samples(abstract); if (rc != DC_STATUS_SUCCESS) return rc;
+ free (device->progress); + return DC_STATUS_SUCCESS; }
The progress variable was moved out of the device structure and into a local function variable. --- src/cochran_commander.c | 85 +++++++++++++++++++++++++------------------------ src/cochran_commander.h | 4 --- 2 files changed, 43 insertions(+), 46 deletions(-)
diff --git a/src/cochran_commander.c b/src/cochran_commander.c index 838f423..5f7b8f3 100644 --- a/src/cochran_commander.c +++ b/src/cochran_commander.c @@ -51,12 +51,15 @@ static const dc_device_vtable_t cochran_commander_device_vtable = {
static dc_status_t cochran_read_id(dc_device_t *device); +static dc_status_t +cochran_commander_read (dc_device_t *abstract, dc_event_progress_t *progress, + unsigned int address, unsigned char data[], unsigned int size);;
-dc_status_t -cochran_packet (cochran_device_t *device, const unsigned char command[], - unsigned int csize, unsigned char answer[], unsigned int asize, - int high_speed) +static dc_status_t +cochran_packet (cochran_device_t *device, dc_event_progress_t *progress, + const unsigned char command[], unsigned int csize, + unsigned char answer[], unsigned int asize, int high_speed) { dc_device_t *abstract = (dc_device_t *) device; unsigned int bytes_read = 0, n, read_size; @@ -115,9 +118,9 @@ cochran_packet (cochran_device_t *device, const unsigned char command[],
bytes_read += n;
- if (device->progress) { - device->progress->current += n; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); + if (progress) { + progress->current += n; + device_event_emit (abstract, DC_EVENT_PROGRESS, progress); } }
@@ -215,7 +218,6 @@ cochran_commander_device_open (dc_device_t **out, dc_context_t *context, // Set the default values. device->port = NULL; device->name = name; - device->progress = NULL; device->data.logbook = NULL; device->data.sample = NULL; cochran_commander_device_set_fingerprint((dc_device_t *) device, @@ -297,6 +299,14 @@ dc_status_t cochran_commander_device_read (dc_device_t *abstract, unsigned int address, unsigned char data[], unsigned int size) { + return cochran_commander_read(abstract, NULL, address, data, size); +} + + +static dc_status_t +cochran_commander_read (dc_device_t *abstract, dc_event_progress_t *progress, + unsigned int address, unsigned char data[], unsigned int size) +{ cochran_device_t *device = (cochran_device_t*) abstract;
// Build the command @@ -335,7 +345,7 @@ cochran_commander_device_read (dc_device_t *abstract, unsigned int address, }
// Read data at high speed - dc_status_t rc = cochran_packet (device, command, command_size, data, + dc_status_t rc = cochran_packet (device, progress, command, command_size, data, size, 1); if (rc != DC_STATUS_SUCCESS) return rc; @@ -416,7 +426,7 @@ cochran_read_id (dc_device_t *abstract) dc_status_t rc; unsigned char command[6] = {0x05, 0x9D, 0xFF, 0x00, 0x43, 0x00};
- rc = cochran_packet(device, command, 6, device->data.id, 67, 0); + rc = cochran_packet(device, NULL, command, 6, device->data.id, 67, 0); if (rc != DC_STATUS_SUCCESS) return rc;
@@ -427,7 +437,7 @@ cochran_read_id (dc_device_t *abstract) command[1] = 0xBD; command[2] = 0x7F;
- rc = cochran_packet(device, command, 6, device->data.id, 67, 0); + rc = cochran_packet(device, NULL, command, 6, device->data.id, 67, 0); if (rc != DC_STATUS_SUCCESS) return rc; } @@ -440,7 +450,7 @@ cochran_read_id (dc_device_t *abstract)
static dc_status_t -cochran_read_config (dc_device_t *abstract) +cochran_read_config (dc_device_t *abstract, dc_event_progress_t *progress) { cochran_device_t *device = (cochran_device_t *) abstract; cochran_data_t *data = &device->data; @@ -456,7 +466,7 @@ cochran_read_config (dc_device_t *abstract) int n; for (n = 0; n < data->config_count; n++) { command[1] = n; - rc = cochran_packet(device, command, 2, data->config[n], 512, 0); + rc = cochran_packet(device, progress, command, 2, data->config[n], 512, 0); if (rc != DC_STATUS_SUCCESS) return rc; } @@ -466,7 +476,7 @@ cochran_read_config (dc_device_t *abstract)
static dc_status_t -cochran_read_misc (dc_device_t *abstract) +cochran_read_misc (dc_device_t *abstract, dc_event_progress_t *progress) { cochran_device_t *device = (cochran_device_t *) abstract;
@@ -496,12 +506,12 @@ cochran_read_misc (dc_device_t *abstract) return EXITCODE (n); }
- return cochran_packet(device, command + 1, 6, device->data.misc, 1500, 0); + return cochran_packet(device, progress, command + 1, 6, device->data.misc, 1500, 0); }
static dc_status_t -cochran_read_logbook (dc_device_t *abstract) +cochran_read_logbook (dc_device_t *abstract, dc_event_progress_t *progress) { cochran_device_t *device = (cochran_device_t *) abstract; cochran_data_t *d = &device->data; @@ -523,7 +533,7 @@ cochran_read_logbook (dc_device_t *abstract) cochran_commander_serial_setup(device, abstract->context);
// Request log book - rc = cochran_commander_device_read(abstract, 0, d->logbook, + rc = cochran_commander_read(abstract, progress, 0, d->logbook, d->logbook_size);
return rc; @@ -600,7 +610,7 @@ cochran_get_sample_parms(dc_device_t *abstract)
static dc_status_t -cochran_read_samples(dc_device_t *abstract) +cochran_read_samples(dc_device_t *abstract, dc_event_progress_t *progress) { cochran_device_t *device = (cochran_device_t *) abstract; cochran_data_t *d = (cochran_data_t *) &device->data; @@ -623,7 +633,7 @@ cochran_read_samples(dc_device_t *abstract) cochran_commander_serial_setup(device, abstract->context);
// Read the sample data - rc = cochran_commander_device_read (abstract, d->sample_data_offset, + rc = cochran_commander_read (abstract, progress, d->sample_data_offset, d->sample, d->sample_size); if (rc != DC_STATUS_SUCCESS) { ERROR (abstract->context, "Failed to read the sample data."); @@ -641,6 +651,7 @@ cochran_commander_device_read_all (dc_device_t *abstract) cochran_device_t *device = (cochran_device_t *) abstract; cochran_data_t *d = (cochran_data_t *) &device->data; cochran_config_t *conf = &d->conf; + dc_event_progress_t progress = {0};
dc_status_t rc; int sample_size; @@ -656,28 +667,20 @@ cochran_commander_device_read_all (dc_device_t *abstract) max_logbook = 1024 * conf->log_size; // max logbook size max_sample = 1450 * 3600 * sample_size; // max sample size
- // Enable progress notifications. - device->progress = malloc(sizeof(dc_event_progress_t)); - if (device->progress == NULL) { - ERROR (abstract->context, "Failed to allocate memory."); - return DC_STATUS_NOMEMORY; - } - - device->progress->current = 0; - device->progress->maximum = max_config + max_misc + max_logbook - + max_sample; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); + progress.current = 0; + progress.maximum = max_config + max_misc + max_logbook + max_sample; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
// Read config - rc = cochran_read_config(abstract); + rc = cochran_read_config(abstract, &progress); if (rc != DC_STATUS_SUCCESS) return rc;
// Update max based on what's read - device->progress->maximum -= max_config - device->progress->current; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); + progress.maximum -= max_config - progress.current; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
- rc = cochran_read_misc(abstract); + rc = cochran_read_misc(abstract, &progress); if (rc != DC_STATUS_SUCCESS) return rc;
@@ -690,10 +693,10 @@ cochran_commander_device_read_all (dc_device_t *abstract) d->logbook_size = ((d->dive_count * conf->log_size) & 0xFFFFC000) + 0x4000;
- device->progress->maximum -= max_logbook - d->logbook_size; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); + progress.maximum -= max_logbook - d->logbook_size; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
- rc = cochran_read_logbook(abstract); + rc = cochran_read_logbook(abstract, &progress); if (rc != DC_STATUS_SUCCESS) return rc;
@@ -701,15 +704,13 @@ cochran_commander_device_read_all (dc_device_t *abstract) cochran_find_fingerprint(abstract); cochran_get_sample_parms(abstract);
- device->progress->maximum -= max_sample - d->sample_size; - device_event_emit (abstract, DC_EVENT_PROGRESS, device->progress); + progress.maximum -= max_sample - d->sample_size; + device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
- rc = cochran_read_samples(abstract); + rc = cochran_read_samples(abstract, &progress); if (rc != DC_STATUS_SUCCESS) return rc;
- free (device->progress); - return DC_STATUS_SUCCESS; }
diff --git a/src/cochran_commander.h b/src/cochran_commander.h index 95d4da4..05b49ed 100644 --- a/src/cochran_commander.h +++ b/src/cochran_commander.h @@ -98,7 +98,6 @@ typedef struct cochran_device_t { const char *name; // serial port name serial_t *port; cochran_data_t data; // dive data used in parsing - dc_event_progress_t *progress; // for progress in the _read function } cochran_device_t;
@@ -146,9 +145,6 @@ typedef struct cochran_device_t { #define EMC_MAX_TEMP 407 // 1 byte, /2+20=F
-dc_status_t cochran_packet (cochran_device_t *device, - const unsigned char command[], unsigned int csize, - unsigned char answer[], unsigned int asize, int high_speed); dc_status_t cochran_commander_device_open (dc_device_t **out, dc_context_t *context, const char *name); dc_status_t cochran_commander_device_close (dc_device_t *abstract);