1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
// 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 "selection.h"
#include "core/settings/qPrefDiveComputer.h"
#include <QString> // for QString::number
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;
}
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;
}
bool device::operator<(const device &a) const
{
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;
}
extern "C" const struct device *get_device_for_dc(const struct device_table *table, const struct divecomputer *dc)
{
const std::vector<device> &dcs = table->devices;
device dev { dc->model ?: "", dc->deviceid, {}, {}, {} };
auto it = std::lower_bound(dcs.begin(), dcs.end(), dev);
return it != dcs.end() && same_device(*it, dev) ? &*it : NULL;
}
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);
}
/*
* 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)
{
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());
}
}
void device::showchanges(const std::string &n, const std::string &s, const std::string &f) const
{
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, {}, {}, {} };
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);
// 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;
} else {
dcs.insert(it, device{m, d, s, f, n});
}
}
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)
{
addDC(device_table->devices, model ?: "", deviceid, nickname ?: "", serial ?: "", firmware ?: "");
}
/* 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;
}
extern "C" int remove_device(struct device_table *device_table, const struct device *dev)
{
auto it = std::lower_bound(device_table->devices.begin(), device_table->devices.end(), *dev);
if (it != device_table->devices.end() && same_device(*it, *dev)) {
int idx = it - device_table->devices.begin();
device_table->devices.erase(it);
return idx;
} else {
return -1;
}
}
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);
}
extern "C" void clear_device_table(struct device_table *device_table)
{
device_table->devices.clear();
}
/* 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;
}
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);
if (existNode && !existNode->nickName.empty())
return existNode->nickName.c_str();
else
return dc->model;
}
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];
}
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];
}
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;
}
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;
}
|