mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Clean up divecomputer 'device' handling
We have this odd legacy notion of a divecomputer 'device', that was originally just basically the libdivecomputer 'EVENT_DEVINFO' report that was associated with each dive. So it had firmware version, deviceid, and serial number. It had also gotten extended to do 'nickname' handling, and it was all confusing, ugly and bad. It was particularly bad because it wasn't actually a 'per device' thing at all: due to the firmware field, a dive computer that got a firmware update forced a new 'device'. To make matters worse, the 'deviceid' was also almost random, because we've calculated it a couple of different ways, and libdivecomputer itself has changed how the legacy 32-bit 'serial number' is expressed. Finally, because of all these issues, we didn't even try to make the thing unique, so it really ended up being a random snapshot of the state of the dive computer at the time of a dive, and sometimes we'd pick one, and sometimes another, since they weren't really well-defined. So get rid of all this confusion. The new rules: - the actual random dive computer state at the time of a dive is kept in the dive data. So if you want to know the firmware version, it should be in the 'extra data' - the only serial number that matters is the string one in the extra data, because that's the one that actually matches what the dive computer reports, and isn't some random 32-bit integer with ambiguous formatting. - the 'device id' - the thing we match with (together with the model name, eg "Suunto EON Steel") is purely a hash of the real serial number. The device ID that libdivecomputer reports in EVENT_DEVINFO is ignored, as is the device ID we've saved in the XML or git files. If we have a serial number, the device ID will be uniquely associated with that serial number, and if we don't have one, the device ID will be zero (for 'match anything'). So now 'deviceid' is literally just a shorthand for the serial number string, and the two are joined at the hip. - the 'device' managament is _only_ used to track devices that have serial numbers _and_ nicknames. So no more different device structures just because one had a nickname and the other didn't etc. Without a serial number, the device is 'anonymous' and fundamentally cannot be distinguished from other devices of the same model, so a nickname is meaningless. And without a nickname, there is no point in creating a device data structure, since all the data is in the dive itself and the device structure wouldn't add any value.. These rules mean that we no longer have ambiguous 'device' structures, and we can never have duplicates that can confuse us. This does mean that you can't give a nickname to a device that cannot be uniquely identified with a serial number, but those are happily fairly rare (and mostly older ones). Dirk said he'd look at what it takes to give more dive computers proper serial numbers, and I already did it for the Garmin Descent family yesterday. (Honesty in advertizing: right now you can't add a nickname to a dive computer that doesn't already have one, because such a dive computer will not have a device structure. But that's a UI issue, and I'll sort that out separately) Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
61524f9b2d
commit
6c4e890960
20 changed files with 93 additions and 206 deletions
142
core/device.cpp
142
core/device.cpp
|
@ -13,31 +13,33 @@ struct device_table device_table;
|
|||
bool device::operator==(const device &a) const
|
||||
{
|
||||
return model == a.model &&
|
||||
deviceId == a.deviceId &&
|
||||
firmware == a.firmware &&
|
||||
serialNumber == a.serialNumber &&
|
||||
nickName == a.nickName;
|
||||
serialNumber == a.serialNumber;
|
||||
}
|
||||
|
||||
static bool same_device(const device &dev1, const device &dev2)
|
||||
{
|
||||
return dev1.deviceId == dev2.deviceId && strcoll(dev1.model.c_str(), dev2.model.c_str()) == 0;
|
||||
return strcmp(dev1.model.c_str(), dev2.model.c_str()) == 0 &&
|
||||
strcmp(dev1.serialNumber.c_str(), dev2.serialNumber.c_str()) == 0;
|
||||
}
|
||||
|
||||
bool device::operator<(const device &a) const
|
||||
{
|
||||
if (deviceId != a.deviceId)
|
||||
return deviceId < a.deviceId;
|
||||
int diff;
|
||||
|
||||
// Use strcoll to compare model-strings, since these might be unicode
|
||||
// and therefore locale dependent? Let's hope that not, but who knows?
|
||||
return strcoll(model.c_str(), a.model.c_str()) < 0;
|
||||
diff = strcmp(model.c_str(), a.model.c_str());
|
||||
if (diff)
|
||||
return diff < 0;
|
||||
|
||||
return strcmp(serialNumber.c_str(), a.serialNumber.c_str()) < 0;
|
||||
}
|
||||
|
||||
extern "C" const struct device *get_device_for_dc(const struct device_table *table, const struct divecomputer *dc)
|
||||
{
|
||||
if (!dc->model || !dc->serial)
|
||||
return NULL;
|
||||
|
||||
const std::vector<device> &dcs = table->devices;
|
||||
device dev { dc->model ?: "", dc->deviceid, {}, {}, {} };
|
||||
device dev { dc->model, dc->serial };
|
||||
auto it = std::lower_bound(dcs.begin(), dcs.end(), dev);
|
||||
return it != dcs.end() && same_device(*it, dev) ? &*it : NULL;
|
||||
}
|
||||
|
@ -48,79 +50,51 @@ extern "C" bool device_exists(const struct device_table *device_table, const str
|
|||
return it != device_table->devices.end() && same_device(*it, *dev);
|
||||
}
|
||||
|
||||
/*
|
||||
* When setting the device ID, we also fill in the
|
||||
* serial number and firmware version data
|
||||
*/
|
||||
extern "C" void set_dc_deviceid(struct divecomputer *dc, unsigned int deviceid, const struct device_table *device_table)
|
||||
void device::showchanges(const std::string &n) const
|
||||
{
|
||||
if (!deviceid)
|
||||
return;
|
||||
|
||||
dc->deviceid = deviceid;
|
||||
|
||||
// Serial and firmware can only be deduced if we know the model
|
||||
if (empty_string(dc->model))
|
||||
return;
|
||||
|
||||
const device *node = get_device_for_dc(device_table, dc);
|
||||
if (!node)
|
||||
return;
|
||||
|
||||
if (!node->serialNumber.empty() && empty_string(dc->serial)) {
|
||||
free((void *)dc->serial);
|
||||
dc->serial = strdup(node->serialNumber.c_str());
|
||||
}
|
||||
if (!node->firmware.empty() && empty_string(dc->fw_version)) {
|
||||
free((void *)dc->fw_version);
|
||||
dc->fw_version = strdup(node->firmware.c_str());
|
||||
if (nickName != n) {
|
||||
if (!n.empty())
|
||||
qDebug("new nickname %s for DC model %s serial %s", n.c_str(), model.c_str(), serialNumber.c_str());
|
||||
else
|
||||
qDebug("deleted nickname %s for DC model %s serial %s", nickName.c_str(), model.c_str(), serialNumber.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void device::showchanges(const std::string &n, const std::string &s, const std::string &f) const
|
||||
static int addDC(std::vector<device> &dcs, const std::string &m, const std::string &s, const std::string &n)
|
||||
{
|
||||
if (nickName != n && !n.empty())
|
||||
qDebug("new nickname %s for DC model %s deviceId 0x%x", n.c_str(), model.c_str(), deviceId);
|
||||
if (serialNumber != s && !s.empty())
|
||||
qDebug("new serial number %s for DC model %s deviceId 0x%x", s.c_str(), model.c_str(), deviceId);
|
||||
if (firmware != f && !f.empty())
|
||||
qDebug("new firmware version %s for DC model %s deviceId 0x%x", f.c_str(), model.c_str(), deviceId);
|
||||
}
|
||||
|
||||
static void addDC(std::vector<device> &dcs, const std::string &m, uint32_t d, const std::string &n, const std::string &s, const std::string &f)
|
||||
{
|
||||
if (m.empty() || d == 0)
|
||||
return;
|
||||
device dev { m, d, {}, {}, {} };
|
||||
if (m.empty() || s.empty())
|
||||
return -1;
|
||||
device dev { m, s, n };
|
||||
auto it = std::lower_bound(dcs.begin(), dcs.end(), dev);
|
||||
if (it != dcs.end() && same_device(*it, dev)) {
|
||||
// debugging: show changes
|
||||
if (verbose)
|
||||
it->showchanges(n, s, f);
|
||||
it->showchanges(n);
|
||||
// Update any non-existent fields from the old entry
|
||||
if (!n.empty())
|
||||
it->nickName = n;
|
||||
if (!s.empty())
|
||||
it->serialNumber = s;
|
||||
if (!f.empty())
|
||||
it->firmware = f;
|
||||
if (n.empty()) {
|
||||
dcs.erase(it);
|
||||
return -1;
|
||||
}
|
||||
it->nickName = n;
|
||||
return it - dcs.begin();
|
||||
} else {
|
||||
dcs.insert(it, device{m, d, s, f, n});
|
||||
if (n.empty())
|
||||
return -1;
|
||||
|
||||
dev.deviceId = calculate_string_hash(s.c_str());
|
||||
dcs.insert(it, dev);
|
||||
return it - dcs.begin();
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void create_device_node(struct device_table *device_table, const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname)
|
||||
extern "C" int create_device_node(struct device_table *device_table, const char *model, const char *serial, const char *nickname)
|
||||
{
|
||||
addDC(device_table->devices, model ?: "", deviceid, nickname ?: "", serial ?: "", firmware ?: "");
|
||||
return addDC(device_table->devices, model ?: "", serial ?: "", nickname ?: "");
|
||||
}
|
||||
|
||||
/* Does not check for duplicates! */
|
||||
extern "C" int add_to_device_table(struct device_table *device_table, const struct device *dev)
|
||||
{
|
||||
auto it = std::lower_bound(device_table->devices.begin(), device_table->devices.end(), *dev);
|
||||
int idx = it - device_table->devices.begin();
|
||||
device_table->devices.insert(it, *dev);
|
||||
return idx;
|
||||
return create_device_node(device_table, dev->model.c_str(), dev->serialNumber.c_str(), dev->nickName.c_str());
|
||||
}
|
||||
|
||||
extern "C" int remove_device(struct device_table *device_table, const struct device *dev)
|
||||
|
@ -165,34 +139,6 @@ extern "C" int is_default_dive_computer_device(const char *name)
|
|||
return qPrefDiveComputer::device() == name;
|
||||
}
|
||||
|
||||
extern "C" void add_devices_of_dive(const struct dive *dive, struct device_table *device_table)
|
||||
{
|
||||
if (!dive)
|
||||
return;
|
||||
|
||||
const struct divecomputer *dc;
|
||||
|
||||
for_each_dc (dive, dc) {
|
||||
if (!empty_string(dc->model) && dc->deviceid &&
|
||||
!get_device_for_dc(device_table, dc)) {
|
||||
// we don't have this one, yet
|
||||
if (std::any_of(device_table->devices.begin(), device_table->devices.end(),
|
||||
[dc] (const device &dev)
|
||||
{ return !strcasecmp(dev.model.c_str(), dc->model); })) {
|
||||
// we already have this model but a different deviceid
|
||||
std::string simpleNick(dc->model);
|
||||
if (dc->deviceid == 0)
|
||||
simpleNick += " (unknown deviceid)";
|
||||
else
|
||||
simpleNick += " (" + QString::number(dc->deviceid, 16).toStdString() + ")";
|
||||
addDC(device_table->devices, dc->model, dc->deviceid, simpleNick, {}, {});
|
||||
} else {
|
||||
addDC(device_table->devices, dc->model, dc->deviceid, {}, {}, {});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *get_dc_nickname(const struct divecomputer *dc)
|
||||
{
|
||||
const device *existNode = get_device_for_dc(&device_table, dc);
|
||||
|
@ -227,21 +173,11 @@ extern "C" const char *device_get_model(const struct device *dev)
|
|||
return dev ? dev->model.c_str() : NULL;
|
||||
}
|
||||
|
||||
extern "C" const uint32_t device_get_id(const struct device *dev)
|
||||
{
|
||||
return dev ? dev->deviceId : -1;
|
||||
}
|
||||
|
||||
extern "C" const char *device_get_serial(const struct device *dev)
|
||||
{
|
||||
return dev ? dev->serialNumber.c_str() : NULL;
|
||||
}
|
||||
|
||||
extern "C" const char *device_get_firmware(const struct device *dev)
|
||||
{
|
||||
return dev ? dev->firmware.c_str() : NULL;
|
||||
}
|
||||
|
||||
extern "C" const char *device_get_nickname(const struct device *dev)
|
||||
{
|
||||
return dev ? dev->nickName.c_str() : NULL;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue