Lift code out to read_ostc3_settings

This lifts the reading 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:09 +01:00 committed by Dirk Hohndel
parent ec369141a3
commit 3655a726c2

View file

@ -332,85 +332,56 @@ static dc_status_t write_suunto_vyper_settings(dc_device_t *device, DeviceDetail
return rc; return rc;
} }
void ReadSettingsThread::run() static dc_status_t read_ostc3_settings(dc_device_t *device, DeviceDetails *m_deviceDetails)
{ {
bool supported = false;
dc_status_t rc; 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;
//Read gas mixes //Read gas mixes
gas gas1; gas gas1;
gas gas2; gas gas2;
gas gas3; gas gas3;
gas gas4; gas gas4;
gas gas5; gas gas5;
//Gas 1
unsigned char gasData[4] = { 0, 0, 0, 0 }; unsigned char gasData[4] = { 0, 0, 0, 0 };
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_GAS1, gasData, sizeof(gasData));
if (rc == DC_STATUS_SUCCESS) { rc = hw_ostc3_device_config_read(device, OSTC3_GAS1, gasData, sizeof(gasData));
//Gas data read successful if (rc != DC_STATUS_SUCCESS)
return rc;
gas1.oxygen = gasData[0]; gas1.oxygen = gasData[0];
gas1.helium = gasData[1]; gas1.helium = gasData[1];
gas1.type = gasData[2]; gas1.type = gasData[2];
gas1.depth = gasData[3]; gas1.depth = gasData[3];
}
//Gas 2 rc = hw_ostc3_device_config_read(device, OSTC3_GAS2, gasData, sizeof(gasData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_GAS2, gasData, sizeof(gasData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Gas data read successful
gas2.oxygen = gasData[0]; gas2.oxygen = gasData[0];
gas2.helium = gasData[1]; gas2.helium = gasData[1];
gas2.type = gasData[2]; gas2.type = gasData[2];
gas2.depth = gasData[3]; gas2.depth = gasData[3];
}
//Gas 3 rc = hw_ostc3_device_config_read(device, OSTC3_GAS3, gasData, sizeof(gasData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_GAS3, gasData, sizeof(gasData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Gas data read successful
gas3.oxygen = gasData[0]; gas3.oxygen = gasData[0];
gas3.helium = gasData[1]; gas3.helium = gasData[1];
gas3.type = gasData[2]; gas3.type = gasData[2];
gas3.depth = gasData[3]; gas3.depth = gasData[3];
}
//Gas 4 rc = hw_ostc3_device_config_read(device, OSTC3_GAS4, gasData, sizeof(gasData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_GAS4, gasData, sizeof(gasData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Gas data read successful
gas4.oxygen = gasData[0]; gas4.oxygen = gasData[0];
gas4.helium = gasData[1]; gas4.helium = gasData[1];
gas4.type = gasData[2]; gas4.type = gasData[2];
gas4.depth = gasData[3]; gas4.depth = gasData[3];
}
//Gas 5 rc = hw_ostc3_device_config_read(device, OSTC3_GAS5, gasData, sizeof(gasData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_GAS5, gasData, sizeof(gasData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Gas data read successful
gas5.oxygen = gasData[0]; gas5.oxygen = gasData[0];
gas5.helium = gasData[1]; gas5.helium = gasData[1];
gas5.type = gasData[2]; gas5.type = gasData[2];
gas5.depth = gasData[3]; gas5.depth = gasData[3];
}
m_deviceDetails->setGas1(gas1); m_deviceDetails->setGas1(gas1);
m_deviceDetails->setGas2(gas2); m_deviceDetails->setGas2(gas2);
@ -424,52 +395,47 @@ void ReadSettingsThread::run()
gas dil3; gas dil3;
gas dil4; gas dil4;
gas dil5; gas dil5;
//Dil 1
unsigned char dilData[4] = { 0, 0, 0, 0 }; unsigned char dilData[4] = { 0, 0, 0, 0 };
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_DIL1, dilData, sizeof(dilData));
if (rc == DC_STATUS_SUCCESS) { rc = hw_ostc3_device_config_read(device, OSTC3_DIL1, dilData, sizeof(dilData));
//Data read successful if (rc != DC_STATUS_SUCCESS)
return rc;
dil1.oxygen = dilData[0]; dil1.oxygen = dilData[0];
dil1.helium = dilData[1]; dil1.helium = dilData[1];
dil1.type = dilData[2]; dil1.type = dilData[2];
dil1.depth = dilData[3]; dil1.depth = dilData[3];
}
//Dil 2 rc = hw_ostc3_device_config_read(device, OSTC3_DIL2, dilData, sizeof(dilData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_DIL2, dilData, sizeof(dilData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
dil2.oxygen = dilData[0]; dil2.oxygen = dilData[0];
dil2.helium = dilData[1]; dil2.helium = dilData[1];
dil2.type = dilData[2]; dil2.type = dilData[2];
dil2.depth = dilData[3]; dil2.depth = dilData[3];
}
//Dil 3 rc = hw_ostc3_device_config_read(device, OSTC3_DIL3, dilData, sizeof(dilData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_DIL3, dilData, sizeof(dilData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
dil3.oxygen = dilData[0]; dil3.oxygen = dilData[0];
dil3.helium = dilData[1]; dil3.helium = dilData[1];
dil3.type = dilData[2]; dil3.type = dilData[2];
dil3.depth = dilData[3]; dil3.depth = dilData[3];
}
//Dil 4 rc = hw_ostc3_device_config_read(device, OSTC3_DIL4, dilData, sizeof(dilData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_DIL4, dilData, sizeof(dilData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
dil4.oxygen = dilData[0]; dil4.oxygen = dilData[0];
dil4.helium = dilData[1]; dil4.helium = dilData[1];
dil4.type = dilData[2]; dil4.type = dilData[2];
dil4.depth = dilData[3]; dil4.depth = dilData[3];
}
//Dil 5 rc = hw_ostc3_device_config_read(device, OSTC3_DIL5, dilData, sizeof(dilData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_DIL5, dilData, sizeof(dilData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
dil5.oxygen = dilData[0]; dil5.oxygen = dilData[0];
dil5.helium = dilData[1]; dil5.helium = dilData[1];
dil5.type = dilData[2]; dil5.type = dilData[2];
dil5.depth = dilData[3]; dil5.depth = dilData[3];
}
m_deviceDetails->setDil1(dil1); m_deviceDetails->setDil1(dil1);
m_deviceDetails->setDil2(dil2); m_deviceDetails->setDil2(dil2);
@ -483,44 +449,37 @@ void ReadSettingsThread::run()
setpoint sp3; setpoint sp3;
setpoint sp4; setpoint sp4;
setpoint sp5; setpoint sp5;
unsigned char spData[2] = { 0, 0 }; unsigned char spData[2] = { 0, 0 };
//Sp 1 rc = hw_ostc3_device_config_read(device, OSTC3_SP1, spData, sizeof(spData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_SP1, spData, sizeof(spData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
sp1.sp = spData[0]; sp1.sp = spData[0];
sp1.depth = spData[1]; sp1.depth = spData[1];
}
//Sp 2 rc = hw_ostc3_device_config_read(device, OSTC3_SP2, spData, sizeof(spData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_SP2, spData, sizeof(spData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
sp2.sp = spData[0]; sp2.sp = spData[0];
sp2.depth = spData[1]; sp2.depth = spData[1];
}
//Sp 3 rc = hw_ostc3_device_config_read(device, OSTC3_SP3, spData, sizeof(spData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_SP3, spData, sizeof(spData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
sp3.sp = spData[0]; sp3.sp = spData[0];
sp3.depth = spData[1]; sp3.depth = spData[1];
}
//Sp 4 rc = hw_ostc3_device_config_read(device, OSTC3_SP4, spData, sizeof(spData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_SP4, spData, sizeof(spData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
sp4.sp = spData[0]; sp4.sp = spData[0];
sp4.depth = spData[1]; sp4.depth = spData[1];
}
//Sp 5 rc = hw_ostc3_device_config_read(device, OSTC3_SP5, spData, sizeof(spData));
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_SP5, spData, sizeof(spData)); if (rc != DC_STATUS_SUCCESS)
if (rc == DC_STATUS_SUCCESS) { return rc;
//Data read successful
sp5.sp = spData[0]; sp5.sp = spData[0];
sp5.depth = spData[1]; sp5.depth = spData[1];
}
m_deviceDetails->setSp1(sp1); m_deviceDetails->setSp1(sp1);
m_deviceDetails->setSp2(sp2); m_deviceDetails->setSp2(sp2);
@ -533,8 +492,9 @@ void ReadSettingsThread::run()
#define READ_SETTING(_OSTC3_SETTING, _DEVICE_DETAIL) \ #define READ_SETTING(_OSTC3_SETTING, _DEVICE_DETAIL) \
do { \ do { \
rc = hw_ostc3_device_config_read(m_data->device, _OSTC3_SETTING, uData, sizeof(uData)); \ rc = hw_ostc3_device_config_read(device, _OSTC3_SETTING, uData, sizeof(uData)); \
if (rc == DC_STATUS_SUCCESS) \ if (rc != DC_STATUS_SUCCESS) \
return rc; \
m_deviceDetails->_DEVICE_DETAIL(uData[0]); \ m_deviceDetails->_DEVICE_DETAIL(uData[0]); \
} while (0) } while (0)
@ -567,26 +527,59 @@ void ReadSettingsThread::run()
#undef READ_SETTING #undef READ_SETTING
rc = hw_ostc3_device_config_read(m_data->device, OSTC3_PRESSURE_SENSOR_OFFSET, uData, sizeof(uData)); rc = hw_ostc3_device_config_read(device, OSTC3_PRESSURE_SENSOR_OFFSET, uData, sizeof(uData));
if (rc == DC_STATUS_SUCCESS) { if (rc != DC_STATUS_SUCCESS)
return rc;
// OSTC3 stores the pressureSensorOffset in two-complement // OSTC3 stores the pressureSensorOffset in two-complement
m_deviceDetails->setPressureSensorOffset((signed char)uData[0]); m_deviceDetails->setPressureSensorOffset((signed char)uData[0]);
}
//read firmware settings //read firmware settings
unsigned char fData[64] = { 0 }; unsigned char fData[64] = { 0 };
rc = hw_ostc3_device_version(m_data->device, fData, sizeof(fData)); rc = hw_ostc3_device_version(device, fData, sizeof (fData));
if (rc == DC_STATUS_SUCCESS) { if (rc != DC_STATUS_SUCCESS)
return rc;
int serial = fData[0] + (fData[1] << 8); int serial = fData[0] + (fData[1] << 8);
m_deviceDetails->setSerialNo(QString::number(serial)); m_deviceDetails->setSerialNo(QString::number(serial));
m_deviceDetails->setFirmwareVersion(QString::number(fData[2]) + "." + QString::number(fData[3])); m_deviceDetails->setFirmwareVersion(QString::number(fData[2]) + "." + QString::number(fData[3]));
QByteArray ar((char *)fData + 4, 60); QByteArray ar((char *)fData + 4, 60);
m_deviceDetails->setCustomText(ar.trimmed()); m_deviceDetails->setCustomText(ar.trimmed());
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); emit devicedetails(m_deviceDetails);
break; } 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 #endif // divecomputer 0.5.0
#ifdef DEBUG_OSTC #ifdef DEBUG_OSTC
case DC_FAMILY_NULL: case DC_FAMILY_NULL: