Lift code out to write_ostc_settings

This lifts the writing of settings out of the run() method and
introduces better error handling when libdivecomputer returns a
error.

Signed-off-by: Anton Lundin <glance@acc.umu.se>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Anton Lundin 2014-12-29 08:52:12 +01:00 committed by Dirk Hohndel
parent 79327d7181
commit 8e33c0ce38

View file

@ -1083,119 +1083,17 @@ static dc_status_t read_ostc_settings(dc_device_t *device, DeviceDetails *m_devi
return rc;
}
void ReadSettingsThread::run()
static dc_status_t write_ostc_settings(dc_device_t *device, DeviceDetails *m_deviceDetails)
{
bool supported = false;
dc_status_t rc;
#ifdef DEBUG_OSTC
if (strcmp(m_data->vendor, "Heinrichs Weikamp") == 0 && strcmp(m_data->product, "OSTC 2N") == 0)
rc = DC_STATUS_SUCCESS;
else
#endif
rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
if (rc == DC_STATUS_SUCCESS) {
DeviceDetails *m_deviceDetails = new DeviceDetails(0);
switch (dc_device_get_type(m_data->device)) {
case DC_FAMILY_SUUNTO_VYPER:
rc = read_suunto_vyper_settings(m_data->device, m_deviceDetails);
if (rc == DC_STATUS_SUCCESS) {
supported = true;
emit devicedetails(m_deviceDetails);
} else if (rc == DC_STATUS_UNSUPPORTED) {
supported = false;
} else {
emit error("Failed!");
}
break;
#if DC_VERSION_CHECK(0, 5, 0)
case DC_FAMILY_HW_OSTC3:
supported = true;
rc = read_ostc3_settings(m_data->device, m_deviceDetails);
if (rc == DC_STATUS_SUCCESS)
emit devicedetails(m_deviceDetails);
else
emit error("Failed!");
break;
#endif // divecomputer 0.5.0
#ifdef DEBUG_OSTC
case DC_FAMILY_NULL:
#endif
case DC_FAMILY_HW_OSTC:
supported = true;
rc = read_ostc_settings(m_data->device, m_deviceDetails);
if (rc == DC_STATUS_SUCCESS)
emit devicedetails(m_deviceDetails);
else
emit error("Failed!");
break;
default:
supported = false;
break;
}
} else {
lastError = tr("Could not a establish connection to the dive computer.");
emit error(lastError);
// So we don't trigger the "unsupported" clause later..
supported = true;
}
dc_device_close(m_data->device);
if (!supported) {
lastError = tr("This feature is not yet available for the selected dive computer.");
emit error(lastError);
}
}
WriteSettingsThread::WriteSettingsThread(QObject *parent, device_data_t *data) : QThread(parent), m_data(data)
{
}
void WriteSettingsThread::setDeviceDetails(DeviceDetails *details)
{
m_deviceDetails = details;
}
void WriteSettingsThread::run()
{
bool supported = false;
dc_status_t rc;
#ifdef DEBUG_OSTC
rc = DC_STATUS_SUCCESS;
#else
rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
#endif
if (rc == DC_STATUS_SUCCESS) {
switch (dc_device_get_type(m_data->device)) {
case DC_FAMILY_SUUNTO_VYPER:
rc = write_suunto_vyper_settings(m_data->device, m_deviceDetails);
if (rc == DC_STATUS_SUCCESS) {
supported = true;
} else if (rc == DC_STATUS_UNSUPPORTED) {
supported = false;
} else {
emit error(tr("Failed!"));
}
break;
#if DC_VERSION_CHECK(0,5,0)
case DC_FAMILY_HW_OSTC3:
supported = true;
rc = write_ostc3_settings(m_data->device, m_deviceDetails);
if (rc != DC_STATUS_SUCCESS)
emit error(tr("Failed!"));
break;
#endif // divecomputer 0.5.0
#ifdef DEBUG_OSTC
case DC_FAMILY_NULL:
#endif
case DC_FAMILY_HW_OSTC: {
supported = true;
unsigned char data[256] = {};
unsigned char max_CF = 0;
// Because we write whole memory blocks, we read all the current
// values out and then change then ones we should change.
rc = hw_ostc_device_eeprom_read(m_data->device, 0, data, sizeof(data));
if (rc == DC_STATUS_SUCCESS) {
rc = hw_ostc_device_eeprom_read(device, 0, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS)
return rc;
//Byte5-6:
//Gas 1 default (%O2=21, %He=0)
gas gas1 = m_deviceDetails->gas1();
@ -1307,7 +1205,7 @@ void WriteSettingsThread::run()
// Custom Text active (=1), Custom Text Disabled (<>1)
// Byte66-90:
// (25Bytes): Custom Text for Surfacemode (Real text must end with "}")
// Example: OSTC Dive Computer} (19 Characters incl. "}") Bytes 85-90 will be ignored.
// Example: "OSTC Dive Computer}" (19 Characters incl. "}") Bytes 85-90 will be ignored.
if (m_deviceDetails->customText() == "") {
data[64] = 0;
} else {
@ -1410,13 +1308,13 @@ void WriteSettingsThread::run()
for (int cf = 0; cf <= 31 && cf <= max_CF; cf++)
printf("CF %d: %d\n", cf, read_ostc_cf(data, cf));
#endif
rc = hw_ostc_device_eeprom_write(m_data->device, 0, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS) {
//FIXME: ERROR!
}
}
rc = hw_ostc_device_eeprom_read(m_data->device, 1, data, sizeof(data));
if (rc == DC_STATUS_SUCCESS) {
rc = hw_ostc_device_eeprom_write(device, 0, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS)
return rc;
rc = hw_ostc_device_eeprom_read(device, 1, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS)
return rc;
// Byte1:
// Logbook version indicator (Not writable!)
// Byte2-3:
@ -1441,13 +1339,13 @@ void WriteSettingsThread::run()
for (int cf = 32; cf <= 63 && cf <= max_CF; cf++)
printf("CF %d: %d\n", cf, read_ostc_cf(data, cf));
#endif
rc = hw_ostc_device_eeprom_write(m_data->device, 1, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS) {
//FIXME: ERROR!
}
}
rc = hw_ostc_device_eeprom_read(m_data->device, 2, data, sizeof(data));
if (rc == DC_STATUS_SUCCESS) {
rc = hw_ostc_device_eeprom_write(device, 1, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS)
return rc;
rc = hw_ostc_device_eeprom_read(device, 2, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS)
return rc;
// Byte1-4:
// not used/reserved (Not writable!)
// Byte5-128:
@ -1468,11 +1366,10 @@ void WriteSettingsThread::run()
for (int cf = 64; cf <= 95 && cf <= max_CF; cf++)
printf("CF %d: %d\n", cf, read_ostc_cf(data, cf));
#endif
rc = hw_ostc_device_eeprom_write(m_data->device, 2, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS) {
//FIXME: ERROR!
}
}
rc = hw_ostc_device_eeprom_write(device, 2, data, sizeof(data));
if (rc != DC_STATUS_SUCCESS)
return rc;
//sync date and time
if (m_deviceDetails->syncTime()) {
QDateTime timeToSet = QDateTime::currentDateTime();
@ -1483,13 +1380,121 @@ void WriteSettingsThread::run()
time.hour = timeToSet.time().hour();
time.minute = timeToSet.time().minute();
time.second = timeToSet.time().second();
rc = hw_ostc_device_clock(m_data->device, &time);
if (rc != DC_STATUS_SUCCESS) {
//FIXME: ERROR!
rc = hw_ostc_device_clock(device, &time);
}
return rc;
}
void ReadSettingsThread::run()
{
bool supported = false;
dc_status_t rc;
#ifdef DEBUG_OSTC
if (strcmp(m_data->vendor, "Heinrichs Weikamp") == 0 && strcmp(m_data->product, "OSTC 2N") == 0)
rc = DC_STATUS_SUCCESS;
else
#endif
rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
if (rc == DC_STATUS_SUCCESS) {
DeviceDetails *m_deviceDetails = new DeviceDetails(0);
switch (dc_device_get_type(m_data->device)) {
case DC_FAMILY_SUUNTO_VYPER:
rc = read_suunto_vyper_settings(m_data->device, m_deviceDetails);
if (rc == DC_STATUS_SUCCESS) {
supported = true;
emit devicedetails(m_deviceDetails);
} else if (rc == DC_STATUS_UNSUPPORTED) {
supported = false;
} else {
emit error("Failed!");
}
break;
#if DC_VERSION_CHECK(0, 5, 0)
case DC_FAMILY_HW_OSTC3:
supported = true;
rc = read_ostc3_settings(m_data->device, m_deviceDetails);
if (rc == DC_STATUS_SUCCESS)
emit devicedetails(m_deviceDetails);
else
emit error("Failed!");
break;
#endif // divecomputer 0.5.0
#ifdef DEBUG_OSTC
case DC_FAMILY_NULL:
#endif
case DC_FAMILY_HW_OSTC:
supported = true;
rc = read_ostc_settings(m_data->device, m_deviceDetails);
if (rc == DC_STATUS_SUCCESS)
emit devicedetails(m_deviceDetails);
else
emit error("Failed!");
break;
default:
supported = false;
break;
}
} else {
lastError = tr("Could not a establish connection to the dive computer.");
emit error(lastError);
// So we don't trigger the "unsupported" clause later..
supported = true;
}
dc_device_close(m_data->device);
if (!supported) {
lastError = tr("This feature is not yet available for the selected dive computer.");
emit error(lastError);
}
}
WriteSettingsThread::WriteSettingsThread(QObject *parent, device_data_t *data) : QThread(parent), m_data(data)
{
}
void WriteSettingsThread::setDeviceDetails(DeviceDetails *details)
{
m_deviceDetails = details;
}
void WriteSettingsThread::run()
{
bool supported = false;
dc_status_t rc;
#ifdef DEBUG_OSTC
rc = DC_STATUS_SUCCESS;
#else
rc = dc_device_open(&m_data->device, m_data->context, m_data->descriptor, m_data->devname);
#endif
if (rc == DC_STATUS_SUCCESS) {
switch (dc_device_get_type(m_data->device)) {
case DC_FAMILY_SUUNTO_VYPER:
rc = write_suunto_vyper_settings(m_data->device, m_deviceDetails);
if (rc == DC_STATUS_SUCCESS) {
supported = true;
} else if (rc == DC_STATUS_UNSUPPORTED) {
supported = false;
} else {
emit error(tr("Failed!"));
}
break;
#if DC_VERSION_CHECK(0,5,0)
case DC_FAMILY_HW_OSTC3:
supported = true;
rc = write_ostc3_settings(m_data->device, m_deviceDetails);
if (rc != DC_STATUS_SUCCESS)
emit error(tr("Failed!"));
break;
#endif // divecomputer 0.5.0
#ifdef DEBUG_OSTC
case DC_FAMILY_NULL:
#endif
case DC_FAMILY_HW_OSTC:
supported = true;
rc = write_ostc_settings(m_data->device, m_deviceDetails);
if (rc != DC_STATUS_SUCCESS)
emit error(tr("Failed!"));
break;
default:
supported = false;
break;