[PATCH 4/4] Cochran: narrowed the scope of the progress variable

John Van Ostrand john at vanostrand.com
Wed Nov 19 12:11:41 PST 2014


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);
-- 
1.8.3.1



More information about the devel mailing list