Simplify state switching and checking for OSTC3

This simplifies the code to check and handle state switching for the
OSTC3.

Suggested-by: Jef Driesen <jef@libdivecomputer.org>
Signed-off-by: Anton Lundin <glance@acc.umu.se>
This commit is contained in:
Anton Lundin 2014-12-20 11:56:03 +01:00 committed by Jef Driesen
parent 7c447c5c16
commit 2d4f76ab51

View File

@ -353,18 +353,33 @@ hw_ostc3_device_init_service (hw_ostc3_device_t *device)
static dc_status_t static dc_status_t
hw_ostc3_check_state_or_init (hw_ostc3_device_t *device) hw_ostc3_device_init (hw_ostc3_device_t *device, hw_ostc3_state_t state)
{ {
dc_status_t rc = DC_STATUS_SUCCESS; dc_status_t rc = DC_STATUS_SUCCESS;
if (device->state == OPEN) { if (device->state == state) {
rc = hw_ostc3_device_init_download (device); // No change.
if (rc != DC_STATUS_SUCCESS) rc = DC_STATUS_SUCCESS;
return rc; } else if (device->state == OPEN) {
} else if (device->state != DOWNLOAD && device->state != SERVICE) { // Change to download or service mode.
return DC_STATUS_INVALIDARGS; if (state == DOWNLOAD) {
rc = hw_ostc3_device_init_download(device);
} else if (state == SERVICE) {
rc = hw_ostc3_device_init_service(device);
} else {
rc = DC_STATUS_INVALIDARGS;
}
} else if (device->state == SERVICE && state == DOWNLOAD) {
// Switching between service and download mode is not possible.
// But in service mode, all download commands are supported too,
// so there is no need to change the state.
rc = DC_STATUS_SUCCESS;
} else {
// Not supported.
rc = DC_STATUS_INVALIDARGS;
} }
return DC_STATUS_SUCCESS;
return rc;
} }
@ -426,7 +441,7 @@ hw_ostc3_device_version (dc_device_t *abstract, unsigned char data[], unsigned i
if (size != SZ_VERSION) if (size != SZ_VERSION)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
dc_status_t rc = hw_ostc3_check_state_or_init (device); dc_status_t rc = hw_ostc3_device_init (device, DOWNLOAD);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;
@ -449,7 +464,7 @@ hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, voi
progress.maximum = (RB_LOGBOOK_SIZE * RB_LOGBOOK_COUNT) + SZ_MEMORY; progress.maximum = (RB_LOGBOOK_SIZE * RB_LOGBOOK_COUNT) + SZ_MEMORY;
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
dc_status_t rc = hw_ostc3_check_state_or_init (device); dc_status_t rc = hw_ostc3_device_init (device, DOWNLOAD);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;
@ -620,7 +635,7 @@ hw_ostc3_device_clock (dc_device_t *abstract, const dc_datetime_t *datetime)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
} }
dc_status_t rc = hw_ostc3_check_state_or_init (device); dc_status_t rc = hw_ostc3_device_init (device, DOWNLOAD);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;
@ -651,7 +666,7 @@ hw_ostc3_device_display (dc_device_t *abstract, const char *text)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
} }
dc_status_t rc = hw_ostc3_check_state_or_init (device); dc_status_t rc = hw_ostc3_device_init (device, DOWNLOAD);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;
@ -679,7 +694,7 @@ hw_ostc3_device_customtext (dc_device_t *abstract, const char *text)
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
} }
dc_status_t rc = hw_ostc3_check_state_or_init (device); dc_status_t rc = hw_ostc3_device_init (device, DOWNLOAD);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;
@ -704,7 +719,7 @@ hw_ostc3_device_config_read (dc_device_t *abstract, unsigned int config, unsigne
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
} }
dc_status_t rc = hw_ostc3_check_state_or_init (device); dc_status_t rc = hw_ostc3_device_init (device, DOWNLOAD);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;
@ -730,7 +745,7 @@ hw_ostc3_device_config_write (dc_device_t *abstract, unsigned int config, const
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
} }
dc_status_t rc = hw_ostc3_check_state_or_init (device); dc_status_t rc = hw_ostc3_device_init (device, DOWNLOAD);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;
@ -752,7 +767,7 @@ hw_ostc3_device_config_reset (dc_device_t *abstract)
if (!ISINSTANCE (abstract)) if (!ISINSTANCE (abstract))
return DC_STATUS_INVALIDARGS; return DC_STATUS_INVALIDARGS;
dc_status_t rc = hw_ostc3_check_state_or_init (device); dc_status_t rc = hw_ostc3_device_init (device, DOWNLOAD);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;
@ -984,15 +999,10 @@ hw_ostc3_device_fwupdate (dc_device_t *abstract, const char *filename)
} }
// Make sure the device is in service mode // Make sure the device is in service mode
if (device->state == OPEN) { rc = hw_ostc3_device_init (device, SERVICE);
rc = hw_ostc3_device_init_service (device); if (rc != DC_STATUS_SUCCESS) {
if (rc != DC_STATUS_SUCCESS) {
free (firmware);
return rc;
}
} else if (device->state != SERVICE) {
free (firmware); free (firmware);
return DC_STATUS_INVALIDARGS; return rc;
} }
// Device open and firmware loaded // Device open and firmware loaded