/
ssudeviceinfo.cpp
317 lines (251 loc) · 9.39 KB
/
ssudeviceinfo.cpp
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* @file ssudeviceinfo.cpp
* @copyright 2013 2013 Jolla Ltd.
* @author Bernd Wachter <bwachter@lart.info>
* @date 2013
*/
#include <QSystemDeviceInfo>
#include <QTextStream>
#include <QDir>
#include "ssudeviceinfo.h"
#include "ssucoreconfig.h"
#include "ssulog.h"
#include "../constants.h"
QTM_USE_NAMESPACE
SsuDeviceInfo::SsuDeviceInfo(QString model): QObject(){
boardMappings = new SsuSettings(SSU_BOARD_MAPPING_CONFIGURATION, SSU_BOARD_MAPPING_CONFIGURATION_DIR);
if (!model.isEmpty())
cachedModel = model;
}
QStringList SsuDeviceInfo::adaptationRepos(){
QStringList result;
QString model = deviceVariant(true);
if (boardMappings->contains(model + "/adaptation-repos"))
result = boardMappings->value(model + "/adaptation-repos").toStringList();
return result;
}
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);
QString model = deviceVariant(true);
QHash<QString, QString> h;
// add global variables for this model
if (boardMappings->contains(model + "/variables")){
QStringList sections = boardMappings->value(model + "/variables").toStringList();
foreach(const QString §ion, sections)
variableSection(section, &h);
}
// override with variables specific to this repository
variableSection(adaptationRepo, &h);
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;
}
QString SsuDeviceInfo::deviceFamily(){
if (!cachedFamily.isEmpty())
return cachedFamily;
QString model = deviceVariant(true);
cachedFamily = "UNKNOWN";
if (boardMappings->contains(model + "/family"))
cachedFamily = boardMappings->value(model + "/family").toString();
return cachedFamily;
}
QString SsuDeviceInfo::deviceVariant(bool fallback){
if (!cachedVariant.isEmpty())
return cachedVariant;
cachedVariant = "";
if (boardMappings->contains("variants/" + deviceModel())) {
cachedVariant = boardMappings->value("variants/" + deviceModel()).toString();
}
if (cachedVariant == "" && fallback)
return deviceModel();
return cachedVariant;
}
QString SsuDeviceInfo::deviceModel(){
QDir dir;
QFile procCpuinfo;
QStringList keys;
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();
if (dir.exists(value)){
cachedModel = key;
break;
}
}
boardMappings->endGroup();
if (!cachedModel.isEmpty()) return cachedModel;
// check if the QSystemInfo model is useful
QSystemDeviceInfo devInfo;
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;
// check if the device can be identified by a string in /proc/cpuinfo
procCpuinfo.setFileName("/proc/cpuinfo");
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;
// check if there's a match on arch ofr generic fallback. This probably
// only makes sense for x86
boardMappings->beginGroup("arch.equals");
keys = boardMappings->allKeys();
SsuCoreConfig *settings = SsuCoreConfig::instance();
foreach (const QString &key, keys){
QString value = boardMappings->value(key).toString();
if (settings->value("arch").toString() == value){
cachedModel = key;
break;
}
}
boardMappings->endGroup();
if (cachedModel.isEmpty()) cachedModel = "UNKNOWN";
return cachedModel;
}
QString SsuDeviceInfo::deviceUid(){
QString IMEI;
QSystemDeviceInfo devInfo;
IMEI = devInfo.imei();
// this might not be completely unique (or might change on reflash), but works for now
if (IMEI == ""){
IMEI = devInfo.uniqueDeviceID();
}
return IMEI;
}
QStringList SsuDeviceInfo::disabledRepos(){
QStringList result;
QString model = deviceVariant(true);
if (boardMappings->contains(model + "/disabled-repos"))
result = boardMappings->value(model + "/disabled-repos").toStringList();
return result;
}
// 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){
int adaptationCount = adaptationRepos().size();
QStringList result;
// for repo names we have adaptation0, adaptation1, ..., adaptationN
for (int i=0; i<adaptationCount; i++)
result.append(QString("adaptation%1").arg(i));
if (filter == NoFilter || filter == BoardFilter || filter == BoardFilterUserBlacklist){
// now read the release/rnd repos
QSettings repoSettings(SSU_REPO_CONFIGURATION, QSettings::IniFormat);
QString repoKey = (rnd ? "default-repos/rnd" : "default-repos/release");
if (repoSettings.contains(repoKey))
result.append(repoSettings.value(repoKey).toStringList());
// TODO: add specific repos (developer, sdk, ..)
// add device configured repos
if (boardMappings->contains(deviceVariant(true) + "/repos"))
result.append(boardMappings->value(deviceVariant(true) + "/repos").toStringList());
// 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);
}
// read user-defined repositories from ssu.ini
// TODO: in strict mode, filter the repository list from there
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
if (filter == NoFilter || filter == UserFilter){
ssuSettings->beginGroup("repository-urls");
result.append(ssuSettings->allKeys());
ssuSettings->endGroup();
// read user-enabled repositories from ssu.ini
if (ssuSettings->contains("enabled-repos"))
result.append(ssuSettings->value("enabled-repos").toStringList());
}
if (filter == NoFilter || filter == UserFilter || filter == BoardFilterUserBlacklist){
// read the disabled repositories from user configuration
if (ssuSettings->contains("disabled-repos")){
foreach (const QString &key, ssuSettings->value("disabled-repos").toStringList())
result.removeAll(key);
}
}
result.removeDuplicates();
return result;
}
void SsuDeviceInfo::variableSection(QString section, QHash<QString, QString> *storageHash){
if (!section.startsWith("var-"))
section = "var-" + section;
if (boardMappings->contains(section + "/variables")){
QStringList sections = boardMappings->value(section + "/variables").toStringList();
foreach(const QString §ion, sections)
variableSection(section, storageHash);
return;
}
boardMappings->beginGroup(section);
if (boardMappings->group() != section)
return;
QStringList keys = boardMappings->allKeys();
foreach (const QString &key, keys){
storageHash->insert(key, boardMappings->value(key).toString());
}
boardMappings->endGroup();
}
void SsuDeviceInfo::setDeviceModel(QString model){
if (model == "")
cachedModel = "";
else
cachedModel = model;
cachedFamily = "";
cachedVariant = "";
}
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;
}