diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-09-19 13:30:56 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-10-03 10:53:26 -0700 |
commit | 90ca6353167071af57028975dc650cd027605372 (patch) | |
tree | c66500fceb7431c0390d23ae94b27f31b8523e29 | |
parent | ce7e74f62f378eae9cc473db76b10a748f5607b4 (diff) | |
download | subsurface-90ca6353167071af57028975dc650cd027605372.tar.gz |
cleanup: use getDCExact() instead of callback in set_dc_deviceid()
core/device.c used to be a C file, which couldn't access the C++
divecomputer list directly. Therefore, instead of a simple loop,
searching for a matching DC was implemented via a callback with
void * user data parameter. Wild. Since the file is now C++, let's
just use direct access to the C++ data structures to make this
readable by mere humans.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
-rw-r--r-- | core/device.cpp | 54 |
1 files changed, 26 insertions, 28 deletions
diff --git a/core/device.cpp b/core/device.cpp index b6af22074..23b477c67 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -2,6 +2,7 @@ #include "ssrf.h" #include "dive.h" #include "subsurface-string.h" +#include "qthelper.h" // for copy_qstring #include "device.h" #include "errorhelper.h" // for verbose flag #include "selection.h" @@ -191,34 +192,6 @@ extern "C" void fake_dc(struct divecomputer *dc) /* Even that didn't work? Give up, there's something wrong */ } -static void match_id(void *_dc, const char *model, uint32_t deviceid, - const char *, const char *serial, const char *firmware) -{ - struct divecomputer *dc = (divecomputer *)_dc; - - if (dc->deviceid != deviceid) - return; - if (!model || !dc->model || strcmp(dc->model, model)) - return; - - if (serial && !dc->serial) - dc->serial = strdup(serial); - if (firmware && !dc->fw_version) - dc->fw_version = strdup(firmware); -} - -/* - * 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) -{ - if (deviceid) { - dc->deviceid = deviceid; - call_for_each_dc(dc, match_id, false); - } -} - DiveComputerList dcList; bool DiveComputerNode::operator==(const DiveComputerNode &a) const @@ -252,6 +225,31 @@ static const DiveComputerNode *getDC(const QVector<DiveComputerNode> &dcs, const return it != dcs.end() && it->model == dc->model ? &*it : NULL; } +/* + * 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) +{ + if (!deviceid) + return; + + dc->deviceid = deviceid; + + // Serial and firmware can only be deduced if we know the model + if (!dc->model) + return; + + const DiveComputerNode *node = getDCExact(dcList.dcs, dc); + if (!node) + return; + + if (!node->serialNumber.isEmpty() && empty_string(dc->serial)) + dc->serial = copy_qstring(node->serialNumber); + if (!node->firmware.isEmpty() && empty_string(dc->fw_version)) + dc->fw_version = copy_qstring(node->firmware); +} + void DiveComputerNode::showchanges(const QString &n, const QString &s, const QString &f) const { if (nickName != n && !n.isEmpty()) |