2017-04-27 18:18:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2018-05-22 07:07:42 +00:00
|
|
|
#include "ssrf.h"
|
2013-01-09 20:07:09 +00:00
|
|
|
#include "dive.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "subsurface-string.h"
|
2013-01-09 20:07:09 +00:00
|
|
|
#include "device.h"
|
2020-09-13 17:08:41 +00:00
|
|
|
#include "errorhelper.h" // for verbose flag
|
2020-09-29 22:01:24 +00:00
|
|
|
#include "selection.h"
|
2020-09-13 17:08:41 +00:00
|
|
|
#include "core/settings/qPrefDiveComputer.h"
|
2020-10-05 07:56:21 +00:00
|
|
|
#include <QString> // for QString::number
|
2013-01-09 20:07:09 +00:00
|
|
|
|
2020-10-03 09:18:42 +00:00
|
|
|
struct device_table device_table;
|
2020-09-13 17:08:41 +00:00
|
|
|
|
2020-10-03 09:18:42 +00:00
|
|
|
bool device::operator==(const device &a) const
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
|
|
|
return model == a.model &&
|
|
|
|
deviceId == a.deviceId &&
|
|
|
|
firmware == a.firmware &&
|
|
|
|
serialNumber == a.serialNumber &&
|
|
|
|
nickName == a.nickName;
|
|
|
|
}
|
|
|
|
|
2020-10-17 11:54:29 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-03 09:18:42 +00:00
|
|
|
bool device::operator<(const device &a) const
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
2020-10-14 20:15:32 +00:00
|
|
|
if (deviceId != a.deviceId)
|
|
|
|
return deviceId < a.deviceId;
|
|
|
|
|
|
|
|
// 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;
|
2020-09-13 17:08:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 12:01:52 +00:00
|
|
|
extern "C" const struct device *get_device_for_dc(const struct device_table *table, const struct divecomputer *dc)
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
2020-10-05 19:12:49 +00:00
|
|
|
const std::vector<device> &dcs = table->devices;
|
2020-11-04 22:04:17 +00:00
|
|
|
device dev { dc->model ?: "", dc->deviceid, {}, {}, {} };
|
2020-10-17 11:54:29 +00:00
|
|
|
auto it = std::lower_bound(dcs.begin(), dcs.end(), dev);
|
|
|
|
return it != dcs.end() && same_device(*it, dev) ? &*it : NULL;
|
2020-09-13 17:08:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 12:01:52 +00:00
|
|
|
extern "C" bool device_exists(const struct device_table *device_table, const struct device *dev)
|
|
|
|
{
|
|
|
|
auto it = std::lower_bound(device_table->devices.begin(), device_table->devices.end(), *dev);
|
|
|
|
return it != device_table->devices.end() && same_device(*it, *dev);
|
|
|
|
}
|
|
|
|
|
2020-09-19 11:30:56 +00:00
|
|
|
/*
|
|
|
|
* When setting the device ID, we also fill in the
|
|
|
|
* serial number and firmware version data
|
|
|
|
*/
|
2020-10-10 19:41:36 +00:00
|
|
|
extern "C" void set_dc_deviceid(struct divecomputer *dc, unsigned int deviceid, const struct device_table *device_table)
|
2020-09-19 11:30:56 +00:00
|
|
|
{
|
|
|
|
if (!deviceid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dc->deviceid = deviceid;
|
|
|
|
|
|
|
|
// Serial and firmware can only be deduced if we know the model
|
2020-11-04 22:04:17 +00:00
|
|
|
if (empty_string(dc->model))
|
2020-09-19 11:30:56 +00:00
|
|
|
return;
|
|
|
|
|
2020-10-10 19:41:36 +00:00
|
|
|
const device *node = get_device_for_dc(device_table, dc);
|
2020-09-19 11:30:56 +00:00
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
2020-10-05 07:56:21 +00:00
|
|
|
if (!node->serialNumber.empty() && empty_string(dc->serial)) {
|
2020-10-05 07:45:56 +00:00
|
|
|
free((void *)dc->serial);
|
2020-10-05 07:56:21 +00:00
|
|
|
dc->serial = strdup(node->serialNumber.c_str());
|
2020-10-05 07:45:56 +00:00
|
|
|
}
|
2020-10-05 07:56:21 +00:00
|
|
|
if (!node->firmware.empty() && empty_string(dc->fw_version)) {
|
2020-10-05 07:45:56 +00:00
|
|
|
free((void *)dc->fw_version);
|
2020-10-05 07:56:21 +00:00
|
|
|
dc->fw_version = strdup(node->firmware.c_str());
|
2020-10-05 07:45:56 +00:00
|
|
|
}
|
2020-09-19 11:30:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 07:56:21 +00:00
|
|
|
void device::showchanges(const std::string &n, const std::string &s, const std::string &f) const
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
2020-10-05 07:56:21 +00:00
|
|
|
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);
|
2020-09-13 17:08:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 08:12:12 +00:00
|
|
|
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)
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
2020-10-05 07:56:21 +00:00
|
|
|
if (m.empty() || d == 0)
|
2020-09-13 17:08:41 +00:00
|
|
|
return;
|
2020-10-17 11:54:29 +00:00
|
|
|
device dev { m, d, {}, {}, {} };
|
|
|
|
auto it = std::lower_bound(dcs.begin(), dcs.end(), dev);
|
|
|
|
if (it != dcs.end() && same_device(*it, dev)) {
|
2020-09-13 17:08:41 +00:00
|
|
|
// debugging: show changes
|
|
|
|
if (verbose)
|
|
|
|
it->showchanges(n, s, f);
|
|
|
|
// Update any non-existent fields from the old entry
|
2020-10-05 07:56:21 +00:00
|
|
|
if (!n.empty())
|
2020-09-13 17:08:41 +00:00
|
|
|
it->nickName = n;
|
2020-10-05 07:56:21 +00:00
|
|
|
if (!s.empty())
|
2020-09-13 17:08:41 +00:00
|
|
|
it->serialNumber = s;
|
2020-10-05 07:56:21 +00:00
|
|
|
if (!f.empty())
|
2020-09-13 17:08:41 +00:00
|
|
|
it->firmware = f;
|
|
|
|
} else {
|
2020-10-03 09:18:42 +00:00
|
|
|
dcs.insert(it, device{m, d, s, f, n});
|
2020-09-13 17:08:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 19:41:36 +00:00
|
|
|
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)
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
2020-10-10 19:41:36 +00:00
|
|
|
addDC(device_table->devices, model ?: "", deviceid, nickname ?: "", serial ?: "", firmware ?: "");
|
2020-09-13 17:08:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 12:01:52 +00:00
|
|
|
/* Does not check for duplicates! */
|
2020-10-21 13:25:18 +00:00
|
|
|
extern "C" int add_to_device_table(struct device_table *device_table, const struct device *dev)
|
2020-10-17 12:01:52 +00:00
|
|
|
{
|
|
|
|
auto it = std::lower_bound(device_table->devices.begin(), device_table->devices.end(), *dev);
|
2020-10-21 13:25:18 +00:00
|
|
|
int idx = it - device_table->devices.begin();
|
2020-10-17 12:01:52 +00:00
|
|
|
device_table->devices.insert(it, *dev);
|
2020-10-21 13:25:18 +00:00
|
|
|
return idx;
|
2020-10-17 12:01:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 13:25:18 +00:00
|
|
|
extern "C" int remove_device(struct device_table *device_table, const struct device *dev)
|
2020-10-17 12:01:52 +00:00
|
|
|
{
|
|
|
|
auto it = std::lower_bound(device_table->devices.begin(), device_table->devices.end(), *dev);
|
2020-10-21 13:25:18 +00:00
|
|
|
if (it != device_table->devices.end() && same_device(*it, *dev)) {
|
|
|
|
int idx = it - device_table->devices.begin();
|
2020-10-17 12:01:52 +00:00
|
|
|
device_table->devices.erase(it);
|
2020-10-21 13:25:18 +00:00
|
|
|
return idx;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-10-17 12:01:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 20:46:36 +00:00
|
|
|
extern "C" void remove_from_device_table(struct device_table *device_table, int idx)
|
|
|
|
{
|
|
|
|
if (idx < 0 || idx >= (int)device_table->devices.size())
|
|
|
|
return;
|
|
|
|
device_table->devices.erase(device_table->devices.begin() + idx);
|
|
|
|
}
|
|
|
|
|
2020-10-17 11:28:05 +00:00
|
|
|
extern "C" void clear_device_table(struct device_table *device_table)
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
2020-10-10 19:41:36 +00:00
|
|
|
device_table->devices.clear();
|
2020-09-13 17:08:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 19:36:51 +00:00
|
|
|
/* Returns whether the given device is used by a selected dive. */
|
|
|
|
extern "C" bool device_used_by_selected_dive(const struct device *dev)
|
|
|
|
{
|
|
|
|
for (dive *d: getDiveSelection()) {
|
|
|
|
struct divecomputer *dc;
|
|
|
|
for_each_dc (d, dc) {
|
|
|
|
if (dc->deviceid == dev->deviceId)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-13 17:08:41 +00:00
|
|
|
extern "C" int is_default_dive_computer_device(const char *name)
|
|
|
|
{
|
|
|
|
return qPrefDiveComputer::device() == name;
|
|
|
|
}
|
|
|
|
|
2020-10-18 19:45:50 +00:00
|
|
|
extern "C" void add_devices_of_dive(const struct dive *dive, struct device_table *device_table)
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
|
|
|
if (!dive)
|
|
|
|
return;
|
|
|
|
|
2020-10-18 19:45:50 +00:00
|
|
|
const struct divecomputer *dc;
|
2020-09-13 17:08:41 +00:00
|
|
|
|
|
|
|
for_each_dc (dive, dc) {
|
|
|
|
if (!empty_string(dc->model) && dc->deviceid &&
|
2020-10-10 19:41:36 +00:00
|
|
|
!get_device_for_dc(device_table, dc)) {
|
2020-09-13 17:08:41 +00:00
|
|
|
// we don't have this one, yet
|
2020-10-14 20:06:08 +00:00
|
|
|
if (std::any_of(device_table->devices.begin(), device_table->devices.end(),
|
|
|
|
[dc] (const device &dev)
|
|
|
|
{ return !strcasecmp(dev.model.c_str(), dc->model); })) {
|
2020-09-13 17:08:41 +00:00
|
|
|
// we already have this model but a different deviceid
|
2020-10-05 07:56:21 +00:00
|
|
|
std::string simpleNick(dc->model);
|
2020-09-13 17:08:41 +00:00
|
|
|
if (dc->deviceid == 0)
|
2020-10-05 07:56:21 +00:00
|
|
|
simpleNick += " (unknown deviceid)";
|
2020-09-13 17:08:41 +00:00
|
|
|
else
|
2020-10-05 07:56:21 +00:00
|
|
|
simpleNick += " (" + QString::number(dc->deviceid, 16).toStdString() + ")";
|
2020-10-10 19:41:36 +00:00
|
|
|
addDC(device_table->devices, dc->model, dc->deviceid, simpleNick, {}, {});
|
2020-09-13 17:08:41 +00:00
|
|
|
} else {
|
2020-10-10 19:41:36 +00:00
|
|
|
addDC(device_table->devices, dc->model, dc->deviceid, {}, {}, {});
|
2020-09-13 17:08:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:56:21 +00:00
|
|
|
const char *get_dc_nickname(const struct divecomputer *dc)
|
2020-09-13 17:08:41 +00:00
|
|
|
{
|
2020-10-05 19:12:49 +00:00
|
|
|
const device *existNode = get_device_for_dc(&device_table, dc);
|
2020-09-13 17:08:41 +00:00
|
|
|
|
2020-10-05 07:56:21 +00:00
|
|
|
if (existNode && !existNode->nickName.empty())
|
|
|
|
return existNode->nickName.c_str();
|
2020-09-13 17:08:41 +00:00
|
|
|
else
|
|
|
|
return dc->model;
|
|
|
|
}
|
2020-10-05 18:55:57 +00:00
|
|
|
|
|
|
|
extern "C" int nr_devices(const struct device_table *table)
|
|
|
|
{
|
|
|
|
return (int)table->devices.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" const struct device *get_device(const struct device_table *table, int i)
|
|
|
|
{
|
|
|
|
if (i < 0 || i > nr_devices(table))
|
|
|
|
return NULL;
|
|
|
|
return &table->devices[i];
|
|
|
|
}
|
|
|
|
|
2020-10-25 06:53:40 +00:00
|
|
|
extern "C" struct device *get_device_mutable(struct device_table *table, int i)
|
|
|
|
{
|
|
|
|
if (i < 0 || i > nr_devices(table))
|
|
|
|
return NULL;
|
|
|
|
return &table->devices[i];
|
|
|
|
}
|
|
|
|
|
2020-10-05 18:55:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-10-17 14:07:39 +00:00
|
|
|
|
|
|
|
extern "C" struct device_table *alloc_device_table()
|
|
|
|
{
|
|
|
|
return new struct device_table;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void free_device_table(struct device_table *devices)
|
|
|
|
{
|
|
|
|
delete devices;
|
|
|
|
}
|