Skip to content

Latest commit

 

History

History
453 lines (370 loc) · 13.1 KB

ssudeviceinfo.cpp

File metadata and controls

453 lines (370 loc) · 13.1 KB
 
1
2
3
4
5
6
7
/**
* @file ssudeviceinfo.cpp
* @copyright 2013 2013 Jolla Ltd.
* @author Bernd Wachter <bwachter@lart.info>
* @date 2013
*/
Apr 20, 2013
Apr 20, 2013
8
9
10
11
#include <QTextStream>
#include <QDir>
Apr 26, 2013
Apr 26, 2013
12
13
#include <sys/utsname.h>
Apr 29, 2013
Apr 29, 2013
14
15
16
17
extern "C" {
#include <boardname.h>
}
Oct 14, 2013
Oct 14, 2013
18
#include "sandbox_p.h"
19
#include "ssudeviceinfo.h"
Mar 30, 2013
Mar 30, 2013
20
#include "ssucoreconfig.h"
Apr 1, 2013
Apr 1, 2013
21
#include "ssulog.h"
Jul 5, 2013
Jul 5, 2013
22
#include "ssuvariables.h"
Mar 19, 2013
Mar 19, 2013
23
24
25
#include "../constants.h"
Sep 2, 2013
Sep 2, 2013
26
#include <QDeviceInfo>
Apr 1, 2013
Apr 1, 2013
28
SsuDeviceInfo::SsuDeviceInfo(QString model): QObject(){
Mar 19, 2013
Mar 19, 2013
30
boardMappings = new SsuSettings(SSU_BOARD_MAPPING_CONFIGURATION, SSU_BOARD_MAPPING_CONFIGURATION_DIR);
Apr 1, 2013
Apr 1, 2013
31
32
if (!model.isEmpty())
cachedModel = model;
Mar 27, 2013
Mar 27, 2013
35
36
37
QStringList SsuDeviceInfo::adaptationRepos(){
QStringList result;
Mar 29, 2013
Mar 29, 2013
38
QString model = deviceVariant(true);
Mar 27, 2013
Mar 27, 2013
39
40
41
if (boardMappings->contains(model + "/adaptation-repos"))
result = boardMappings->value(model + "/adaptation-repos").toStringList();
Mar 27, 2013
Mar 27, 2013
43
44
45
return result;
}
Apr 1, 2013
Apr 1, 2013
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
QString SsuDeviceInfo::adaptationVariables(const QString &adaptationName, QHash<QString, QString> *storageHash){
SsuLog *ssuLog = SsuLog::instance();
QStringList adaptationRepoList = adaptationRepos();
// special handling for adaptation-repositories
// - check if repo is in right format (adaptation\d*)
// - check if the configuration has that many adaptation repos
// - export the entry in the adaptation list as %(adaptation)
// - look up variables for that adaptation, and export matching
// adaptation variable
QRegExp regex("adaptation\\\d*", Qt::CaseSensitive, QRegExp::RegExp2);
if (regex.exactMatch(adaptationName)){
regex.setPattern("\\\d*");
regex.lastIndexIn(adaptationName);
int n = regex.cap().toInt();
if (!adaptationRepoList.isEmpty()){
if (adaptationRepoList.size() <= n) {
ssuLog->print(LOG_INFO, "Note: repo index out of bounds, substituting 0" + adaptationName);
n = 0;
}
QString adaptationRepo = adaptationRepoList.at(n);
storageHash->insert("adaptation", adaptationRepo);
ssuLog->print(LOG_DEBUG, "Found first adaptation " + adaptationName);
Apr 5, 2013
Apr 5, 2013
71
QString model = deviceVariant(true);
Apr 5, 2013
Apr 5, 2013
72
QHash<QString, QString> h;
Apr 5, 2013
Apr 5, 2013
73
74
75
76
77
78
79
80
81
// add global variables for this model
if (boardMappings->contains(model + "/variables")){
QStringList sections = boardMappings->value(model + "/variables").toStringList();
foreach(const QString &section, sections)
variableSection(section, &h);
}
// override with variables specific to this repository
Apr 5, 2013
Apr 5, 2013
82
variableSection(adaptationRepo, &h);
Apr 1, 2013
Apr 1, 2013
83
84
85
86
87
88
89
90
91
92
93
94
95
96
QHash<QString, QString>::const_iterator i = h.constBegin();
while (i != h.constEnd()){
storageHash->insert(i.key(), i.value());
i++;
}
} else
ssuLog->print(LOG_INFO, "Note: adaptation repo for invalid repo requested " + adaptationName);
return "adaptation";
}
return adaptationName;
}
Jun 13, 2013
Jun 13, 2013
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
void SsuDeviceInfo::clearCache(){
cachedFamily = "";
cachedModel = "";
cachedVariant = "";
}
bool SsuDeviceInfo::contains(const QString &model){
QString oldModel = deviceModel();
bool found = false;
if (!model.isEmpty()){
clearCache();
setDeviceModel(model);
}
if (!deviceVariant(false).isEmpty())
found = true;
if (boardMappings->childGroups().contains(deviceModel()))
found = true;
if (!model.isEmpty()){
clearCache();
setDeviceModel(oldModel);
}
return found;
}
Mar 27, 2013
Mar 27, 2013
124
QString SsuDeviceInfo::deviceFamily(){
125
126
127
if (!cachedFamily.isEmpty())
return cachedFamily;
Mar 29, 2013
Mar 29, 2013
128
QString model = deviceVariant(true);
Mar 27, 2013
Mar 27, 2013
130
cachedFamily = "UNKNOWN";
131
132
133
134
135
136
137
if (boardMappings->contains(model + "/family"))
cachedFamily = boardMappings->value(model + "/family").toString();
return cachedFamily;
}
Mar 29, 2013
Mar 29, 2013
138
QString SsuDeviceInfo::deviceVariant(bool fallback){
Mar 19, 2013
Mar 19, 2013
139
140
141
142
143
144
145
146
147
if (!cachedVariant.isEmpty())
return cachedVariant;
cachedVariant = "";
if (boardMappings->contains("variants/" + deviceModel())) {
cachedVariant = boardMappings->value("variants/" + deviceModel()).toString();
}
Mar 29, 2013
Mar 29, 2013
148
149
150
if (cachedVariant == "" && fallback)
return deviceModel();
Mar 19, 2013
Mar 19, 2013
151
152
153
return cachedVariant;
}
154
155
156
157
QString SsuDeviceInfo::deviceModel(){
QDir dir;
QFile procCpuinfo;
QStringList keys;
Apr 29, 2013
Apr 29, 2013
158
QStringList sections;
159
160
161
162
163
164
165
166
167
168
if (!cachedModel.isEmpty())
return cachedModel;
boardMappings->beginGroup("file.exists");
keys = boardMappings->allKeys();
// check if the device can be identified by testing for a file
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
Oct 14, 2013
Oct 14, 2013
169
if (dir.exists(Sandbox::map(value))){
170
171
172
173
174
175
176
cachedModel = key;
break;
}
}
boardMappings->endGroup();
if (!cachedModel.isEmpty()) return cachedModel;
Apr 29, 2013
Apr 29, 2013
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
// check if boardname matches/contains
QString boardName(getboardname());
boardName = boardName.trimmed();
sections.clear();
sections << "boardname.equals" << "boardname.contains";
foreach (const QString &section, sections){
boardMappings->beginGroup(section);
keys = boardMappings->allKeys();
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (section.endsWith(".contains")){
if (boardName.contains(value)){
cachedModel = key;
break;
}
} else if (section.endsWith(".equals")){
if (boardName == value){
cachedModel = key;
break;
}
}
}
boardMappings->endGroup();
if (!cachedModel.isEmpty()) break;
}
if (!cachedModel.isEmpty()) return cachedModel;
204
// check if the QSystemInfo model is useful
Apr 20, 2013
Apr 20, 2013
205
206
207
208
209
210
211
212
//QSystemDeviceInfo devInfo;
// TODO Current Mer SystemDeviceInfo only returns cpuinfo stuff,
// which is what we can do with cpuinfo matching in a more
// flexible way, so there's not really any need to pull in the
// whole X11 stack just for this. Can be enabled once systeminfo
// is less insane
/*
QSystemDeviceInfoLinuxCommonPrivate devInfo;
213
214
215
216
217
218
219
220
221
222
223
224
QString model = devInfo.model();
boardMappings->beginGroup("systeminfo.equals");
keys = boardMappings->allKeys();
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (model == value){
cachedModel = key;
break;
}
}
boardMappings->endGroup();
if (!cachedModel.isEmpty()) return cachedModel;
Apr 20, 2013
Apr 20, 2013
225
*/
226
227
// check if the device can be identified by a string in /proc/cpuinfo
Oct 14, 2013
Oct 14, 2013
228
procCpuinfo.setFileName(Sandbox::map("/proc/cpuinfo"));
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
procCpuinfo.open(QIODevice::ReadOnly | QIODevice::Text);
if (procCpuinfo.isOpen()){
QTextStream in(&procCpuinfo);
QString cpuinfo = in.readAll();
boardMappings->beginGroup("cpuinfo.contains");
keys = boardMappings->allKeys();
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (cpuinfo.contains(value)){
cachedModel = key;
break;
}
}
boardMappings->endGroup();
}
if (!cachedModel.isEmpty()) return cachedModel;
Apr 26, 2013
Apr 26, 2013
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// check if the device can be identified by the kernel version string
struct utsname buf;
if (!uname(&buf)){
QString utsRelease(buf.release);
boardMappings->beginGroup("uname-release.contains");
keys = boardMappings->allKeys();
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (utsRelease.contains(value)){
cachedModel = key;
break;
}
}
boardMappings->endGroup();
}
if (!cachedModel.isEmpty()) return cachedModel;
Apr 26, 2013
Apr 26, 2013
265
// check if there's a match on arch of generic fallback. This probably
266
267
268
269
// only makes sense for x86
boardMappings->beginGroup("arch.equals");
keys = boardMappings->allKeys();
Mar 30, 2013
Mar 30, 2013
270
SsuCoreConfig *settings = SsuCoreConfig::instance();
271
272
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
Mar 30, 2013
Mar 30, 2013
273
if (settings->value("arch").toString() == value){
274
275
276
277
278
279
280
281
282
283
284
285
cachedModel = key;
break;
}
}
boardMappings->endGroup();
if (cachedModel.isEmpty()) cachedModel = "UNKNOWN";
return cachedModel;
}
QString SsuDeviceInfo::deviceUid(){
QString IMEI;
Sep 2, 2013
Sep 2, 2013
286
QDeviceInfo devInfo;
Sep 2, 2013
Sep 2, 2013
288
289
/// @todo properly check number of imeis, ...
IMEI = devInfo.imei(0);
290
291
292
293
// this might not be completely unique (or might change on reflash), but works for now
if (IMEI == ""){
IMEI = devInfo.uniqueDeviceID();
Sep 3, 2013
Sep 3, 2013
294
IMEI.replace("-", "");
295
296
297
298
}
return IMEI;
}
Mar 19, 2013
Mar 19, 2013
299
Mar 28, 2013
Mar 28, 2013
300
301
302
QStringList SsuDeviceInfo::disabledRepos(){
QStringList result;
Mar 29, 2013
Mar 29, 2013
303
QString model = deviceVariant(true);
Mar 28, 2013
Mar 28, 2013
304
305
306
307
308
309
310
if (boardMappings->contains(model + "/disabled-repos"))
result = boardMappings->value(model + "/disabled-repos").toStringList();
return result;
}
Oct 24, 2013
Oct 24, 2013
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
QString SsuDeviceInfo::displayName(const int type){
QString model = deviceModel();
QString variant = deviceVariant(false);
QString value, key;
switch (type){
case Ssu::DeviceManufacturer:
key = "/deviceManufacturer";
break;
case Ssu::DeviceModel:
key = "/prettyModel";
break;
case Ssu::DeviceDesignation:
key = "/deviceDesignation";
break;
default:
return "";
}
/*
* Go through different levels of fallbacks:
* 1. model specific setting
* 2. variant specific setting
* 3. global setting
* 4. return model name, or "UNKNOWN" in case query was for manufacturer
*/
if (boardMappings->contains(model + key))
value = boardMappings->value(model + key).toString();
else if (variant != "" && boardMappings->contains(variant + key))
value = boardMappings->value(variant + key).toString();
else if (boardMappings->contains(key))
value = boardMappings->value(key).toString();
else if (type != Ssu::DeviceManufacturer)
value = model;
else
value = "UNKNOWN";
return value;
}
Mar 31, 2013
Mar 31, 2013
353
354
355
356
// this half belongs into repo-manager, as it not only handles board-specific
// repositories. Right now this one looks like the better place due to the
// connection to board specific stuff, though
QStringList SsuDeviceInfo::repos(bool rnd, int filter){
Mar 28, 2013
Mar 28, 2013
357
358
359
int adaptationCount = adaptationRepos().size();
QStringList result;
Oct 6, 2013
Oct 6, 2013
360
361
362
if (filter == Ssu::NoFilter ||
filter == Ssu::BoardFilter ||
filter == Ssu::BoardFilterUserBlacklist){
Apr 10, 2013
Apr 10, 2013
363
364
365
366
// for repo names we have adaptation0, adaptation1, ..., adaptationN
for (int i=0; i<adaptationCount; i++)
result.append(QString("adaptation%1").arg(i));
Mar 31, 2013
Mar 31, 2013
367
// now read the release/rnd repos
May 23, 2013
May 23, 2013
368
SsuSettings repoSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat);
Mar 31, 2013
Mar 31, 2013
369
370
371
QString repoKey = (rnd ? "default-repos/rnd" : "default-repos/release");
if (repoSettings.contains(repoKey))
result.append(repoSettings.value(repoKey).toStringList());
Mar 28, 2013
Mar 28, 2013
372
Mar 31, 2013
Mar 31, 2013
373
// TODO: add specific repos (developer, sdk, ..)
Mar 28, 2013
Mar 28, 2013
374
Mar 31, 2013
Mar 31, 2013
375
376
377
378
// add device configured repos
if (boardMappings->contains(deviceVariant(true) + "/repos"))
result.append(boardMappings->value(deviceVariant(true) + "/repos").toStringList());
Oct 6, 2013
Oct 6, 2013
379
380
381
382
383
// add device configured repos only valid for rnd and/or release
repoKey = (rnd ? "/repos-rnd" : "/repos-release");
if (boardMappings->contains(deviceVariant(true) + repoKey))
result.append(boardMappings->value(deviceVariant(true) + repoKey).toStringList());
Mar 31, 2013
Mar 31, 2013
384
385
386
387
388
// read the disabled repositories for this device
// user can override repositories disabled here in the user configuration
foreach (const QString &key, disabledRepos())
result.removeAll(key);
}
Mar 29, 2013
Mar 29, 2013
389
Mar 28, 2013
Mar 28, 2013
390
391
// read user-defined repositories from ssu.ini
// TODO: in strict mode, filter the repository list from there
Mar 30, 2013
Mar 30, 2013
392
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
Mar 28, 2013
Mar 28, 2013
393
Oct 6, 2013
Oct 6, 2013
394
395
if (filter == Ssu::NoFilter ||
filter == Ssu::UserFilter){
Mar 31, 2013
Mar 31, 2013
396
397
398
ssuSettings->beginGroup("repository-urls");
result.append(ssuSettings->allKeys());
ssuSettings->endGroup();
Mar 28, 2013
Mar 28, 2013
399
Mar 31, 2013
Mar 31, 2013
400
401
402
403
// read user-enabled repositories from ssu.ini
if (ssuSettings->contains("enabled-repos"))
result.append(ssuSettings->value("enabled-repos").toStringList());
}
Mar 28, 2013
Mar 28, 2013
404
Oct 6, 2013
Oct 6, 2013
405
406
407
if (filter == Ssu::NoFilter ||
filter == Ssu::UserFilter ||
filter == Ssu::BoardFilterUserBlacklist){
Mar 31, 2013
Mar 31, 2013
408
409
410
411
412
// read the disabled repositories from user configuration
if (ssuSettings->contains("disabled-repos")){
foreach (const QString &key, ssuSettings->value("disabled-repos").toStringList())
result.removeAll(key);
}
Mar 28, 2013
Mar 28, 2013
413
414
}
Mar 31, 2013
Mar 31, 2013
415
result.removeDuplicates();
Mar 28, 2013
Mar 28, 2013
416
417
418
return result;
}
Jun 13, 2013
Jun 13, 2013
419
QVariant SsuDeviceInfo::variable(QString section, const QString &key){
Jul 6, 2013
Jul 6, 2013
420
421
422
423
424
/// @todo compat-setting as ssudeviceinfo guaranteed to prepend sections with var-;
/// SsuVariables does not have this guarantee. Remove from here as well.
if (!section.startsWith("var-"))
section = "var-" + section;
Jul 5, 2013
Jul 5, 2013
425
return SsuVariables::variable(boardMappings, section, key);
Jun 13, 2013
Jun 13, 2013
426
427
}
Apr 5, 2013
Apr 5, 2013
428
void SsuDeviceInfo::variableSection(QString section, QHash<QString, QString> *storageHash){
Jul 5, 2013
Jul 5, 2013
429
430
431
if (!section.startsWith("var-"))
section = "var-" + section;
Jul 5, 2013
Jul 5, 2013
432
SsuVariables::variableSection(boardMappings, section, storageHash);
Mar 27, 2013
Mar 27, 2013
433
434
435
436
437
438
439
440
441
442
443
}
void SsuDeviceInfo::setDeviceModel(QString model){
if (model == "")
cachedModel = "";
else
cachedModel = model;
cachedFamily = "";
cachedVariant = "";
}
Apr 1, 2013
Apr 1, 2013
444
445
446
447
448
449
450
451
452
453
QVariant SsuDeviceInfo::value(const QString &key, const QVariant &value){
if (boardMappings->contains(deviceVariant()+"/"+key)){
return boardMappings->value(deviceVariant()+"/"+key);
} else if (boardMappings->contains(deviceModel()+"/"+key)){
return boardMappings->value(deviceModel()+"/"+key);
}
return value;
}