Check that the model is a supported one

This code adds some crude checks to verify that the device is a
supported one before we actually read/write from it. This is to
white/black-list devices so we don't write or read memory that we don't
know anything about.

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-10-13 17:14:12 +02:00 committed by Dirk Hohndel
parent c8eb2dccc5
commit d7cae093be

View file

@ -78,8 +78,51 @@ void ReadSettingsThread::run()
DeviceDetails *m_deviceDetails = new DeviceDetails(0); DeviceDetails *m_deviceDetails = new DeviceDetails(0);
switch (dc_device_get_type(m_data->device)) { switch (dc_device_get_type(m_data->device)) {
case DC_FAMILY_SUUNTO_VYPER: case DC_FAMILY_SUUNTO_VYPER:
supported = true;
unsigned char data[SUUNTO_VYPER_CUSTOM_TEXT_LENGHT + 1]; unsigned char data[SUUNTO_VYPER_CUSTOM_TEXT_LENGHT + 1];
rc = dc_device_read(m_data->device, SUUNTO_VYPER_COMPUTER_TYPE, data, 1);
if (rc == DC_STATUS_SUCCESS) {
const char *model;
// FIXME: grab this info from libdivecomputer descriptor
// instead of hard coded here
switch(data[0]) {
case 0x03:
model = "Stinger";
break;
case 0x04:
model = "Mosquito";
break;
case 0x05:
model = "D3";
break;
case 0x0A:
model = "Vyper";
break;
case 0x0B:
model = "Vytec";
break;
case 0x0C:
model = "Cobra";
break;
case 0x0D:
model = "Gekko";
break;
case 0x16:
model = "Zoop";
break;
case 20:
case 30:
case 60:
// Suunto Spyder have there sample interval at this position
// Fallthrough
default:
supported = false;
goto unsupported_dc_error;
}
// We found a supported device
// we can safely proceed with reading/writing to this device.
supported = true;
m_deviceDetails->setModel(model);
}
rc = dc_device_read(m_data->device, SUUNTO_VYPER_MAXDEPTH, data, 2); rc = dc_device_read(m_data->device, SUUNTO_VYPER_MAXDEPTH, data, 2);
if (rc == DC_STATUS_SUCCESS) { if (rc == DC_STATUS_SUCCESS) {
// in ft * 128.0 // in ft * 128.0
@ -96,33 +139,6 @@ void ReadSettingsThread::run()
int number_of_dives = data[0] << 8 ^ data[1]; int number_of_dives = data[0] << 8 ^ data[1];
m_deviceDetails->setNumberOfDives(number_of_dives); m_deviceDetails->setNumberOfDives(number_of_dives);
} }
rc = dc_device_read(m_data->device, SUUNTO_VYPER_COMPUTER_TYPE, data, 1);
if (rc == DC_STATUS_SUCCESS) {
const char *model;
switch(data[0]) {
case 0x03:
model = "Stinger";
break;
case 0x04:
model = "Mosquito";
break;
case 0x0A:
model = "new Vyper";
break;
case 0x0C:
model = "Vyper or Cobra";
break;
case 0x0B:
model = "Vytec";
break;
case 0x0D:
model = "Gekko";
break;
default:
model = "UNKNOWN";
}
m_deviceDetails->setModel(model);
}
rc = dc_device_read(m_data->device, SUUNTO_VYPER_FIRMWARE, data, 1); rc = dc_device_read(m_data->device, SUUNTO_VYPER_FIRMWARE, data, 1);
if (rc == DC_STATUS_SUCCESS) { if (rc == DC_STATUS_SUCCESS) {
m_deviceDetails->setFirmwareVersion(QString::number(data[0]) + ".0.0"); m_deviceDetails->setFirmwareVersion(QString::number(data[0]) + ".0.0");
@ -429,6 +445,7 @@ void ReadSettingsThread::run()
supported = false; supported = false;
break; break;
} }
unsupported_dc_error:
dc_device_close(m_data->device); dc_device_close(m_data->device);
if (!supported) { if (!supported) {
@ -460,9 +477,15 @@ void WriteSettingsThread::run()
if (rc == DC_STATUS_SUCCESS) { if (rc == DC_STATUS_SUCCESS) {
switch (dc_device_get_type(m_data->device)) { switch (dc_device_get_type(m_data->device)) {
case DC_FAMILY_SUUNTO_VYPER: case DC_FAMILY_SUUNTO_VYPER:
supported = true;
unsigned char data; unsigned char data;
unsigned char data2[2]; unsigned char data2[2];
// Maybee we should read the model from the device to sanity check it here too..
// For now we just check that we actually read a device before writing to one.
if (m_deviceDetails->model() == "")
break;
else
supported = true;
dc_device_write(m_data->device, SUUNTO_VYPER_CUSTOM_TEXT, dc_device_write(m_data->device, SUUNTO_VYPER_CUSTOM_TEXT,
// Convert the customText to a 30 char wide padded with " " // Convert the customText to a 30 char wide padded with " "
(const unsigned char *) QString("%1").arg(m_deviceDetails->customText(), -30, QChar(' ')).toUtf8().data(), (const unsigned char *) QString("%1").arg(m_deviceDetails->customText(), -30, QChar(' ')).toUtf8().data(),