From a01ab817139be16906c30f89e16a0a3fbbe026cd Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 13 Sep 2020 19:08:41 +0200 Subject: cleanup: fold core/divecomputer.cpp into core/device.c core/device.h was declaring a number of functions that were related to divecomputers (dcs): creating a fake dc for manually entered dives and registering / accessing dc nicknames. On could argue whether these should be lumped together, but it is what it is. However, part of that was implemented in C++/Qt code in a separate core/divecomputer.cpp file. Some function therein where only accessible to C++ and declared in core/divecomputer.h. All in all, a big mess. Let's simply combine the files and conditionally compile the C++-only functions depending on the __cplusplus define. Yes, that means turning device.c into device.cpp. A brave soul might turn the C++/Qt code into C code if they whish later on. Signed-off-by: Berthold Stoeger --- core/CMakeLists.txt | 4 +- core/device.c | 221 --------------------- core/device.cpp | 377 ++++++++++++++++++++++++++++++++++++ core/device.h | 33 ++++ core/divecomputer.cpp | 164 ---------------- core/divecomputer.h | 35 ---- packaging/ios/Subsurface-mobile.pro | 5 +- profile-widget/profilewidget2.cpp | 1 - qt-models/divecomputermodel.h | 2 +- 9 files changed, 413 insertions(+), 429 deletions(-) delete mode 100644 core/device.c create mode 100644 core/device.cpp delete mode 100644 core/divecomputer.cpp delete mode 100644 core/divecomputer.h diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index fedba3614..b16f546e7 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -57,15 +57,13 @@ set(SUBSURFACE_CORE_LIB_SRCS datatrak.h deco.c deco.h - device.c + device.cpp device.h devicedetails.cpp devicedetails.h display.h dive.c dive.h - divecomputer.cpp - divecomputer.h divefilter.cpp divefilter.h divelist.c diff --git a/core/device.c b/core/device.c deleted file mode 100644 index 22fb7f49a..000000000 --- a/core/device.c +++ /dev/null @@ -1,221 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include "ssrf.h" -#include -#include "dive.h" -#include "subsurface-string.h" -#include "device.h" - -/* - * Good fake dive profiles are hard. - * - * "depthtime" is the integral of the dive depth over - * time ("area" of the dive profile). We want that - * area to match the average depth (avg_d*max_t). - * - * To do that, we generate a 6-point profile: - * - * (0, 0) - * (t1, max_d) - * (t2, max_d) - * (t3, d) - * (t4, d) - * (max_t, 0) - * - * with the same ascent/descent rates between the - * different depths. - * - * NOTE: avg_d, max_d and max_t are given constants. - * The rest we can/should play around with to get a - * good-looking profile. - * - * That six-point profile gives a total area of: - * - * (max_d*max_t) - (max_d*t1) - (max_d-d)*(t4-t3) - * - * And the "same ascent/descent rates" requirement - * gives us (time per depth must be same): - * - * t1 / max_d = (t3-t2) / (max_d-d) - * t1 / max_d = (max_t-t4) / d - * - * We also obviously require: - * - * 0 <= t1 <= t2 <= t3 <= t4 <= max_t - * - * Let us call 'd_frac = d / max_d', and we get: - * - * Total area must match average depth-time: - * - * (max_d*max_t) - (max_d*t1) - (max_d-d)*(t4-t3) = avg_d*max_t - * max_d*(max_t-t1-(1-d_frac)*(t4-t3)) = avg_d*max_t - * max_t-t1-(1-d_frac)*(t4-t3) = avg_d*max_t/max_d - * t1+(1-d_frac)*(t4-t3) = max_t*(1-avg_d/max_d) - * - * and descent slope must match ascent slopes: - * - * t1 / max_d = (t3-t2) / (max_d*(1-d_frac)) - * t1 = (t3-t2)/(1-d_frac) - * - * and - * - * t1 / max_d = (max_t-t4) / (max_d*d_frac) - * t1 = (max_t-t4)/d_frac - * - * In general, we have more free variables than we have constraints, - * but we can aim for certain basics, like a good ascent slope. - */ -static int fill_samples(struct sample *s, int max_d, int avg_d, int max_t, double slope, double d_frac) -{ - double t_frac = max_t * (1 - avg_d / (double)max_d); - int t1 = lrint(max_d / slope); - int t4 = lrint(max_t - t1 * d_frac); - int t3 = lrint(t4 - (t_frac - t1) / (1 - d_frac)); - int t2 = lrint(t3 - t1 * (1 - d_frac)); - - if (t1 < 0 || t1 > t2 || t2 > t3 || t3 > t4 || t4 > max_t) - return 0; - - s[1].time.seconds = t1; - s[1].depth.mm = max_d; - s[2].time.seconds = t2; - s[2].depth.mm = max_d; - s[3].time.seconds = t3; - s[3].depth.mm = lrint(max_d * d_frac); - s[4].time.seconds = t4; - s[4].depth.mm = lrint(max_d * d_frac); - - return 1; -} - -/* we have no average depth; instead of making up a random average depth - * we should assume either a PADI rectangular profile (for short and/or - * shallow dives) or more reasonably a six point profile with a 3 minute - * safety stop at 5m */ -static void fill_samples_no_avg(struct sample *s, int max_d, int max_t, double slope) -{ - // shallow or short dives are just trapecoids based on the given slope - if (max_d < 10000 || max_t < 600) { - s[1].time.seconds = lrint(max_d / slope); - s[1].depth.mm = max_d; - s[2].time.seconds = max_t - lrint(max_d / slope); - s[2].depth.mm = max_d; - } else { - s[1].time.seconds = lrint(max_d / slope); - s[1].depth.mm = max_d; - s[2].time.seconds = max_t - lrint(max_d / slope) - 180; - s[2].depth.mm = max_d; - s[3].time.seconds = max_t - lrint(5000 / slope) - 180; - s[3].depth.mm = 5000; - s[4].time.seconds = max_t - lrint(5000 / slope); - s[4].depth.mm = 5000; - } -} - -void fake_dc(struct divecomputer *dc) -{ - alloc_samples(dc, 6); - struct sample *fake = dc->sample; - int i; - - dc->samples = 6; - - /* The dive has no samples, so create a few fake ones */ - int max_t = dc->duration.seconds; - int max_d = dc->maxdepth.mm; - int avg_d = dc->meandepth.mm; - - memset(fake, 0, 6 * sizeof(struct sample)); - fake[5].time.seconds = max_t; - for (i = 0; i < 6; i++) { - fake[i].bearing.degrees = -1; - fake[i].ndl.seconds = -1; - } - if (!max_t || !max_d) { - dc->samples = 0; - return; - } - - /* Set last manually entered time to the total dive length */ - dc->last_manual_time = dc->duration; - - /* - * We want to fake the profile so that the average - * depth ends up correct. However, in the absence of - * a reasonable average, let's just make something - * up. Note that 'avg_d == max_d' is _not_ a reasonable - * average. - * We explicitly treat avg_d == 0 differently */ - if (avg_d == 0) { - /* we try for a sane slope, but bow to the insanity of - * the user supplied data */ - fill_samples_no_avg(fake, max_d, max_t, MAX(2.0 * max_d / max_t, (double)prefs.ascratelast6m)); - if (fake[3].time.seconds == 0) { // just a 4 point profile - dc->samples = 4; - fake[3].time.seconds = max_t; - } - return; - } - if (avg_d < max_d / 10 || avg_d >= max_d) { - avg_d = (max_d + 10000) / 3; - if (avg_d > max_d) - avg_d = max_d * 2 / 3; - } - if (!avg_d) - avg_d = 1; - - /* - * Ok, first we try a basic profile with a specific ascent - * rate (5 meters per minute) and d_frac (1/3). - */ - if (fill_samples(fake, max_d, avg_d, max_t, (double)prefs.ascratelast6m, 0.33)) - return; - - /* - * Ok, assume that didn't work because we cannot make the - * average come out right because it was a quick deep dive - * followed by a much shallower region - */ - if (fill_samples(fake, max_d, avg_d, max_t, 10000.0 / 60, 0.10)) - return; - - /* - * Uhhuh. That didn't work. We'd need to find a good combination that - * satisfies our constraints. Currently, we don't, we just give insane - * slopes. - */ - if (fill_samples(fake, max_d, avg_d, max_t, 10000.0, 0.01)) - return; - - /* 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 *nickname, const char *serial, const char *firmware) -{ - // here nickname is unused - UNUSED(nickname); - - struct divecomputer *dc = _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 - */ -void set_dc_deviceid(struct divecomputer *dc, unsigned int deviceid) -{ - if (deviceid) { - dc->deviceid = deviceid; - call_for_each_dc(dc, match_id, false); - } -} diff --git a/core/device.cpp b/core/device.cpp new file mode 100644 index 000000000..d72f4d481 --- /dev/null +++ b/core/device.cpp @@ -0,0 +1,377 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "ssrf.h" +#include "dive.h" +#include "subsurface-string.h" +#include "device.h" +#include "errorhelper.h" // for verbose flag +#include "core/settings/qPrefDiveComputer.h" + +/* + * Good fake dive profiles are hard. + * + * "depthtime" is the integral of the dive depth over + * time ("area" of the dive profile). We want that + * area to match the average depth (avg_d*max_t). + * + * To do that, we generate a 6-point profile: + * + * (0, 0) + * (t1, max_d) + * (t2, max_d) + * (t3, d) + * (t4, d) + * (max_t, 0) + * + * with the same ascent/descent rates between the + * different depths. + * + * NOTE: avg_d, max_d and max_t are given constants. + * The rest we can/should play around with to get a + * good-looking profile. + * + * That six-point profile gives a total area of: + * + * (max_d*max_t) - (max_d*t1) - (max_d-d)*(t4-t3) + * + * And the "same ascent/descent rates" requirement + * gives us (time per depth must be same): + * + * t1 / max_d = (t3-t2) / (max_d-d) + * t1 / max_d = (max_t-t4) / d + * + * We also obviously require: + * + * 0 <= t1 <= t2 <= t3 <= t4 <= max_t + * + * Let us call 'd_frac = d / max_d', and we get: + * + * Total area must match average depth-time: + * + * (max_d*max_t) - (max_d*t1) - (max_d-d)*(t4-t3) = avg_d*max_t + * max_d*(max_t-t1-(1-d_frac)*(t4-t3)) = avg_d*max_t + * max_t-t1-(1-d_frac)*(t4-t3) = avg_d*max_t/max_d + * t1+(1-d_frac)*(t4-t3) = max_t*(1-avg_d/max_d) + * + * and descent slope must match ascent slopes: + * + * t1 / max_d = (t3-t2) / (max_d*(1-d_frac)) + * t1 = (t3-t2)/(1-d_frac) + * + * and + * + * t1 / max_d = (max_t-t4) / (max_d*d_frac) + * t1 = (max_t-t4)/d_frac + * + * In general, we have more free variables than we have constraints, + * but we can aim for certain basics, like a good ascent slope. + */ +static int fill_samples(struct sample *s, int max_d, int avg_d, int max_t, double slope, double d_frac) +{ + double t_frac = max_t * (1 - avg_d / (double)max_d); + int t1 = lrint(max_d / slope); + int t4 = lrint(max_t - t1 * d_frac); + int t3 = lrint(t4 - (t_frac - t1) / (1 - d_frac)); + int t2 = lrint(t3 - t1 * (1 - d_frac)); + + if (t1 < 0 || t1 > t2 || t2 > t3 || t3 > t4 || t4 > max_t) + return 0; + + s[1].time.seconds = t1; + s[1].depth.mm = max_d; + s[2].time.seconds = t2; + s[2].depth.mm = max_d; + s[3].time.seconds = t3; + s[3].depth.mm = lrint(max_d * d_frac); + s[4].time.seconds = t4; + s[4].depth.mm = lrint(max_d * d_frac); + + return 1; +} + +/* we have no average depth; instead of making up a random average depth + * we should assume either a PADI rectangular profile (for short and/or + * shallow dives) or more reasonably a six point profile with a 3 minute + * safety stop at 5m */ +static void fill_samples_no_avg(struct sample *s, int max_d, int max_t, double slope) +{ + // shallow or short dives are just trapecoids based on the given slope + if (max_d < 10000 || max_t < 600) { + s[1].time.seconds = lrint(max_d / slope); + s[1].depth.mm = max_d; + s[2].time.seconds = max_t - lrint(max_d / slope); + s[2].depth.mm = max_d; + } else { + s[1].time.seconds = lrint(max_d / slope); + s[1].depth.mm = max_d; + s[2].time.seconds = max_t - lrint(max_d / slope) - 180; + s[2].depth.mm = max_d; + s[3].time.seconds = max_t - lrint(5000 / slope) - 180; + s[3].depth.mm = 5000; + s[4].time.seconds = max_t - lrint(5000 / slope); + s[4].depth.mm = 5000; + } +} + +extern "C" void fake_dc(struct divecomputer *dc) +{ + alloc_samples(dc, 6); + struct sample *fake = dc->sample; + int i; + + dc->samples = 6; + + /* The dive has no samples, so create a few fake ones */ + int max_t = dc->duration.seconds; + int max_d = dc->maxdepth.mm; + int avg_d = dc->meandepth.mm; + + memset(fake, 0, 6 * sizeof(struct sample)); + fake[5].time.seconds = max_t; + for (i = 0; i < 6; i++) { + fake[i].bearing.degrees = -1; + fake[i].ndl.seconds = -1; + } + if (!max_t || !max_d) { + dc->samples = 0; + return; + } + + /* Set last manually entered time to the total dive length */ + dc->last_manual_time = dc->duration; + + /* + * We want to fake the profile so that the average + * depth ends up correct. However, in the absence of + * a reasonable average, let's just make something + * up. Note that 'avg_d == max_d' is _not_ a reasonable + * average. + * We explicitly treat avg_d == 0 differently */ + if (avg_d == 0) { + /* we try for a sane slope, but bow to the insanity of + * the user supplied data */ + fill_samples_no_avg(fake, max_d, max_t, MAX(2.0 * max_d / max_t, (double)prefs.ascratelast6m)); + if (fake[3].time.seconds == 0) { // just a 4 point profile + dc->samples = 4; + fake[3].time.seconds = max_t; + } + return; + } + if (avg_d < max_d / 10 || avg_d >= max_d) { + avg_d = (max_d + 10000) / 3; + if (avg_d > max_d) + avg_d = max_d * 2 / 3; + } + if (!avg_d) + avg_d = 1; + + /* + * Ok, first we try a basic profile with a specific ascent + * rate (5 meters per minute) and d_frac (1/3). + */ + if (fill_samples(fake, max_d, avg_d, max_t, (double)prefs.ascratelast6m, 0.33)) + return; + + /* + * Ok, assume that didn't work because we cannot make the + * average come out right because it was a quick deep dive + * followed by a much shallower region + */ + if (fill_samples(fake, max_d, avg_d, max_t, 10000.0 / 60, 0.10)) + return; + + /* + * Uhhuh. That didn't work. We'd need to find a good combination that + * satisfies our constraints. Currently, we don't, we just give insane + * slopes. + */ + if (fill_samples(fake, max_d, avg_d, max_t, 10000.0, 0.01)) + return; + + /* 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 +{ + return model == a.model && + deviceId == a.deviceId && + firmware == a.firmware && + serialNumber == a.serialNumber && + nickName == a.nickName; +} + +bool DiveComputerNode::operator!=(const DiveComputerNode &a) const +{ + return !(*this == a); +} + +bool DiveComputerNode::operator<(const DiveComputerNode &a) const +{ + return std::tie(model, deviceId) < std::tie(a.model, a.deviceId); +} + +const DiveComputerNode *DiveComputerList::getExact(const QString &m, uint32_t d) +{ + auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{m, d, {}, {}, {}}); + return it != dcs.end() && it->model == m && it->deviceId == d ? &*it : NULL; +} + +const DiveComputerNode *DiveComputerList::get(const QString &m) +{ + auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{m, 0, {}, {}, {}}); + return it != dcs.end() && it->model == m ? &*it : NULL; +} + +void DiveComputerNode::showchanges(const QString &n, const QString &s, const QString &f) const +{ + if (nickName != n && !n.isEmpty()) + qDebug("new nickname %s for DC model %s deviceId 0x%x", qPrintable(n), qPrintable(model), deviceId); + if (serialNumber != s && !s.isEmpty()) + qDebug("new serial number %s for DC model %s deviceId 0x%x", qPrintable(s), qPrintable(model), deviceId); + if (firmware != f && !f.isEmpty()) + qDebug("new firmware version %s for DC model %s deviceId 0x%x", qPrintable(f), qPrintable(model), deviceId); +} + +void DiveComputerList::addDC(QString m, uint32_t d, QString n, QString s, QString f) +{ + if (m.isEmpty() || d == 0) + return; + auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{m, d, {}, {}, {}}); + if (it != dcs.end() && it->model == m && it->deviceId == d) { + // debugging: show changes + if (verbose) + it->showchanges(n, s, f); + // Update any non-existent fields from the old entry + if (!n.isEmpty()) + it->nickName = n; + if (!s.isEmpty()) + it->serialNumber = s; + if (!f.isEmpty()) + it->firmware = f; + } else { + dcs.insert(it, DiveComputerNode{m, d, s, f, n}); + } +} + +extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname) +{ + dcList.addDC(model, deviceid, nickname, serial, firmware); +} + +extern "C" void clear_device_nodes() +{ + dcList.dcs.clear(); +} + +static bool compareDCById(const DiveComputerNode &a, const DiveComputerNode &b) +{ + return a.deviceId < b.deviceId; +} + +extern "C" void call_for_each_dc (void *f, void (*callback)(void *, const char *, uint32_t, const char *, const char *, const char *), + bool select_only) +{ + QVector values = dcList.dcs; + std::sort(values.begin(), values.end(), compareDCById); + for (const DiveComputerNode &node : values) { + bool found = false; + if (select_only) { + int j; + struct dive *d; + for_each_dive (j, d) { + struct divecomputer *dc; + if (!d->selected) + continue; + for_each_dc (d, dc) { + if (dc->deviceid == node.deviceId) { + found = true; + break; + } + } + if (found) + break; + } + } else { + found = true; + } + if (found) + callback(f, qPrintable(node.model), node.deviceId, qPrintable(node.nickName), + qPrintable(node.serialNumber), qPrintable(node.firmware)); + } +} + +extern "C" int is_default_dive_computer(const char *vendor, const char *product) +{ + return qPrefDiveComputer::vendor() == vendor && qPrefDiveComputer::product() == product; +} + +extern "C" int is_default_dive_computer_device(const char *name) +{ + return qPrefDiveComputer::device() == name; +} + +extern "C" void set_dc_nickname(struct dive *dive) +{ + if (!dive) + return; + + struct divecomputer *dc; + + for_each_dc (dive, dc) { + if (!empty_string(dc->model) && dc->deviceid && + !dcList.getExact(dc->model, dc->deviceid)) { + // we don't have this one, yet + const DiveComputerNode *existNode = dcList.get(dc->model); + if (existNode) { + // we already have this model but a different deviceid + QString simpleNick(dc->model); + if (dc->deviceid == 0) + simpleNick.append(" (unknown deviceid)"); + else + simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")"); + dcList.addDC(dc->model, dc->deviceid, simpleNick); + } else { + dcList.addDC(dc->model, dc->deviceid); + } + } + } +} + +QString get_dc_nickname(const struct divecomputer *dc) +{ + const DiveComputerNode *existNode = dcList.getExact(dc->model, dc->deviceid); + + if (existNode && !existNode->nickName.isEmpty()) + return existNode->nickName; + else + return dc->model; +} diff --git a/core/device.h b/core/device.h index 4c202bee5..4906a2a49 100644 --- a/core/device.h +++ b/core/device.h @@ -21,4 +21,37 @@ extern void clear_device_nodes(); } #endif +// Functions and global variables that are only available to C++ code +#ifdef __cplusplus + +#include +#include +class DiveComputerNode { +public: + bool operator==(const DiveComputerNode &a) const; + bool operator!=(const DiveComputerNode &a) const; + bool operator<(const DiveComputerNode &a) const; + void showchanges(const QString &n, const QString &s, const QString &f) const; + QString model; + uint32_t deviceId; + QString serialNumber; + QString firmware; + QString nickName; +}; + +class DiveComputerList { +public: + const DiveComputerNode *getExact(const QString &m, uint32_t d); + const DiveComputerNode *get(const QString &m); + void addDC(QString m, uint32_t d, QString n = QString(), QString s = QString(), QString f = QString()); + + // Keep the dive computers in a vector sorted by (model, deviceId) + QVector dcs; +}; + +QString get_dc_nickname(const struct divecomputer *dc); +extern DiveComputerList dcList; + +#endif + #endif // DEVICE_H diff --git a/core/divecomputer.cpp b/core/divecomputer.cpp deleted file mode 100644 index 5b4067b82..000000000 --- a/core/divecomputer.cpp +++ /dev/null @@ -1,164 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include "divecomputer.h" -#include "dive.h" -#include "errorhelper.h" -#include "core/settings/qPrefDiveComputer.h" -#include "subsurface-string.h" - -DiveComputerList dcList; - -bool DiveComputerNode::operator==(const DiveComputerNode &a) const -{ - return model == a.model && - deviceId == a.deviceId && - firmware == a.firmware && - serialNumber == a.serialNumber && - nickName == a.nickName; -} - -bool DiveComputerNode::operator!=(const DiveComputerNode &a) const -{ - return !(*this == a); -} - -bool DiveComputerNode::operator<(const DiveComputerNode &a) const -{ - return std::tie(model, deviceId) < std::tie(a.model, a.deviceId); -} - -const DiveComputerNode *DiveComputerList::getExact(const QString &m, uint32_t d) -{ - auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{m, d, {}, {}, {}}); - return it != dcs.end() && it->model == m && it->deviceId == d ? &*it : NULL; -} - -const DiveComputerNode *DiveComputerList::get(const QString &m) -{ - auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{m, 0, {}, {}, {}}); - return it != dcs.end() && it->model == m ? &*it : NULL; -} - -void DiveComputerNode::showchanges(const QString &n, const QString &s, const QString &f) const -{ - if (nickName != n && !n.isEmpty()) - qDebug("new nickname %s for DC model %s deviceId 0x%x", qPrintable(n), qPrintable(model), deviceId); - if (serialNumber != s && !s.isEmpty()) - qDebug("new serial number %s for DC model %s deviceId 0x%x", qPrintable(s), qPrintable(model), deviceId); - if (firmware != f && !f.isEmpty()) - qDebug("new firmware version %s for DC model %s deviceId 0x%x", qPrintable(f), qPrintable(model), deviceId); -} - -void DiveComputerList::addDC(QString m, uint32_t d, QString n, QString s, QString f) -{ - if (m.isEmpty() || d == 0) - return; - auto it = std::lower_bound(dcs.begin(), dcs.end(), DiveComputerNode{m, d, {}, {}, {}}); - if (it != dcs.end() && it->model == m && it->deviceId == d) { - // debugging: show changes - if (verbose) - it->showchanges(n, s, f); - // Update any non-existent fields from the old entry - if (!n.isEmpty()) - it->nickName = n; - if (!s.isEmpty()) - it->serialNumber = s; - if (!f.isEmpty()) - it->firmware = f; - } else { - dcs.insert(it, DiveComputerNode{m, d, s, f, n}); - } -} - -extern "C" void create_device_node(const char *model, uint32_t deviceid, const char *serial, const char *firmware, const char *nickname) -{ - dcList.addDC(model, deviceid, nickname, serial, firmware); -} - -extern "C" void clear_device_nodes() -{ - dcList.dcs.clear(); -} - -static bool compareDCById(const DiveComputerNode &a, const DiveComputerNode &b) -{ - return a.deviceId < b.deviceId; -} - -extern "C" void call_for_each_dc (void *f, void (*callback)(void *, const char *, uint32_t, const char *, const char *, const char *), - bool select_only) -{ - QVector values = dcList.dcs; - std::sort(values.begin(), values.end(), compareDCById); - for (const DiveComputerNode &node : values) { - bool found = false; - if (select_only) { - int j; - struct dive *d; - for_each_dive (j, d) { - struct divecomputer *dc; - if (!d->selected) - continue; - for_each_dc (d, dc) { - if (dc->deviceid == node.deviceId) { - found = true; - break; - } - } - if (found) - break; - } - } else { - found = true; - } - if (found) - callback(f, qPrintable(node.model), node.deviceId, qPrintable(node.nickName), - qPrintable(node.serialNumber), qPrintable(node.firmware)); - } -} - -extern "C" int is_default_dive_computer(const char *vendor, const char *product) -{ - return qPrefDiveComputer::vendor() == vendor && qPrefDiveComputer::product() == product; -} - -extern "C" int is_default_dive_computer_device(const char *name) -{ - return qPrefDiveComputer::device() == name; -} - -extern "C" void set_dc_nickname(struct dive *dive) -{ - if (!dive) - return; - - struct divecomputer *dc; - - for_each_dc (dive, dc) { - if (!empty_string(dc->model) && dc->deviceid && - !dcList.getExact(dc->model, dc->deviceid)) { - // we don't have this one, yet - const DiveComputerNode *existNode = dcList.get(dc->model); - if (existNode) { - // we already have this model but a different deviceid - QString simpleNick(dc->model); - if (dc->deviceid == 0) - simpleNick.append(" (unknown deviceid)"); - else - simpleNick.append(" (").append(QString::number(dc->deviceid, 16)).append(")"); - dcList.addDC(dc->model, dc->deviceid, simpleNick); - } else { - dcList.addDC(dc->model, dc->deviceid); - } - } - } -} - -QString get_dc_nickname(const struct divecomputer *dc) -{ - const DiveComputerNode *existNode = dcList.getExact(dc->model, dc->deviceid); - - if (existNode && !existNode->nickName.isEmpty()) - return existNode->nickName; - else - return dc->model; -} diff --git a/core/divecomputer.h b/core/divecomputer.h deleted file mode 100644 index c92d56558..000000000 --- a/core/divecomputer.h +++ /dev/null @@ -1,35 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#ifndef DIVECOMPUTER_H -#define DIVECOMPUTER_H - -#include -#include -#include - -class DiveComputerNode { -public: - bool operator==(const DiveComputerNode &a) const; - bool operator!=(const DiveComputerNode &a) const; - bool operator<(const DiveComputerNode &a) const; - void showchanges(const QString &n, const QString &s, const QString &f) const; - QString model; - uint32_t deviceId; - QString serialNumber; - QString firmware; - QString nickName; -}; - -class DiveComputerList { -public: - const DiveComputerNode *getExact(const QString &m, uint32_t d); - const DiveComputerNode *get(const QString &m); - void addDC(QString m, uint32_t d, QString n = QString(), QString s = QString(), QString f = QString()); - - // Keep the dive computers in a vector sorted by (model, deviceId) - QVector dcs; -}; - -QString get_dc_nickname(const struct divecomputer *dc); -extern DiveComputerList dcList; - -#endif diff --git a/packaging/ios/Subsurface-mobile.pro b/packaging/ios/Subsurface-mobile.pro index 1281d0377..322e2b9a2 100644 --- a/packaging/ios/Subsurface-mobile.pro +++ b/packaging/ios/Subsurface-mobile.pro @@ -36,7 +36,6 @@ SOURCES += ../../subsurface-mobile-main.cpp \ ../../core/checkcloudconnection.cpp \ ../../core/color.cpp \ ../../core/configuredivecomputer.cpp \ - ../../core/divecomputer.cpp \ ../../core/divelogexportlogic.cpp \ ../../core/divesitehelpers.cpp \ ../../core/errorhelper.c \ @@ -52,7 +51,7 @@ SOURCES += ../../subsurface-mobile-main.cpp \ ../../core/subsurfacestartup.c \ ../../core/ios.cpp \ ../../core/profile.c \ - ../../core/device.c \ + ../../core/device.cpp \ ../../core/dive.c \ ../../core/divefilter.cpp \ ../../core/divelist.c \ @@ -217,7 +216,6 @@ HEADERS += \ ../../core/datatrak.h \ ../../core/deco.h \ ../../core/display.h \ - ../../core/divecomputer.h \ ../../core/divefilter.h \ ../../core/divelist.h \ ../../core/divelogexportlogic.h \ @@ -232,7 +230,6 @@ HEADERS += \ ../../core/metrics.h \ ../../core/qt-gui.h \ ../../core/selection.h \ - ../../core/divecomputer.h \ ../../core/sha1.h \ ../../core/strndup.h \ ../../core/subsurfacestartup.h \ diff --git a/profile-widget/profilewidget2.cpp b/profile-widget/profilewidget2.cpp index 3373b96cd..f3a636fe8 100644 --- a/profile-widget/profilewidget2.cpp +++ b/profile-widget/profilewidget2.cpp @@ -1,7 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include "profile-widget/profilewidget2.h" #include "qt-models/diveplotdatamodel.h" -#include "core/divecomputer.h" #include "core/subsurface-string.h" #include "core/qthelper.h" #include "core/picture.h" diff --git a/qt-models/divecomputermodel.h b/qt-models/divecomputermodel.h index f1e3a0a38..8c2373695 100644 --- a/qt-models/divecomputermodel.h +++ b/qt-models/divecomputermodel.h @@ -3,7 +3,7 @@ #define DIVECOMPUTERMODEL_H #include "qt-models/cleanertablemodel.h" -#include "core/divecomputer.h" +#include "core/device.h" class DiveComputerModel : public CleanerTableModel { Q_OBJECT -- cgit v1.2.3-70-g09d2