/
rndssucli.cpp
465 lines (401 loc) · 14.1 KB
/
rndssucli.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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/**
* @file rndssucli.cpp
* @copyright 2012 Jolla Ltd.
* @author Bernd Wachter <bernd.wachter@jollamobile.com>
* @date 2012
*/
#include <QCoreApplication>
#include <termios.h>
#include <ssudeviceinfo.h>
#include <ssurepomanager.h>
#include <ssucoreconfig.h>
#include <QDebug>
#include "rndssucli.h"
RndSsuCli::RndSsuCli(): QObject(){
connect(this,SIGNAL(done()),
QCoreApplication::instance(),SLOT(quit()), Qt::DirectConnection);
connect(&ssu, SIGNAL(done()),
this, SLOT(handleResponse()));
state = Idle;
}
void RndSsuCli::handleResponse(){
QTextStream qout(stdout);
if (ssu.error()){
qout << "Last operation failed: \n" << ssu.lastError() << endl;
QCoreApplication::exit(1);
} else {
qout << "Operation successful" << endl;
QCoreApplication::exit(0);
}
}
void RndSsuCli::optFlavour(QStringList opt){
QTextStream qout(stdout);
if (opt.count() == 3){
qout << "Changing flavour from " << ssu.flavour()
<< " to " << opt.at(2) << endl;
ssu.setFlavour(opt.at(2));
state = Idle;
} else if (opt.count() == 2) {
qout << "Device flavour is currently: " << ssu.flavour() << endl;
state = Idle;
}
}
void RndSsuCli::optMode(QStringList opt){
QTextStream qout(stdout);
// TODO: allow setting meaningful names instead of numbers
if (opt.count() == 2){
QStringList modeList;
int deviceMode = ssu.deviceMode();
if ((deviceMode & Ssu::DisableRepoManager) == Ssu::DisableRepoManager)
modeList.append("DisableRepoManager");
if ((deviceMode & Ssu::RndMode) == Ssu::RndMode)
modeList.append("RndMode");
if ((deviceMode & Ssu::ReleaseMode) == Ssu::ReleaseMode)
modeList.append("ReleaseMode");
if ((deviceMode & Ssu::LenientMode) == Ssu::LenientMode)
modeList.append("LenientMode");
qout << "Device mode is: " << ssu.deviceMode()
<< " (" << modeList.join(" | ") << ")" << endl;
if ((deviceMode & Ssu::RndMode) == Ssu::RndMode &&
(deviceMode & Ssu::ReleaseMode) == Ssu::ReleaseMode)
qout << "Both Release and RnD mode set, device is in RnD mode" << endl;
state = Idle;
} else if (opt.count() == 3){
qout << "Setting device mode from " << ssu.deviceMode()
<< " to " << opt.at(2) << endl;
ssu.setDeviceMode(opt.at(2).toInt());
state = Idle;
}
}
void RndSsuCli::optModifyRepo(int action, QStringList opt){
SsuRepoManager repoManager;
QTextStream qout(stdout);
if (opt.count() == 3){
switch(action){
case Add:
repoManager.add(opt.at(2));
repoManager.update();
break;
case Remove:
repoManager.remove(opt.at(2));
repoManager.update();
break;
case Disable:
repoManager.disable(opt.at(2));
repoManager.update();
break;
case Enable:
repoManager.enable(opt.at(2));
repoManager.update();
break;
}
} else if (opt.count() == 4 && action == Add){
repoManager.add(opt.at(2), opt.at(3));
repoManager.update();
}
}
void RndSsuCli::optRegister(){
/*
* register a new device
*/
QString username, password;
QTextStream qin(stdin);
QTextStream qout(stdout);
struct termios termNew, termOld;
qout << "Username: " << flush;
username = qin.readLine();
tcgetattr(STDIN_FILENO, &termNew);
termOld = termNew;
termNew.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &termNew) == -1)
qout << "WARNING: Unable to disable echo on your terminal, password will echo!" << endl;
qout << "Password: " << flush;
password = qin.readLine();
tcsetattr(STDIN_FILENO, TCSANOW, &termOld);
ssu.sendRegistration(username, password);
state = Busy;
}
void RndSsuCli::optRelease(QStringList opt){
QTextStream qout(stdout);
if (opt.count() == 3){
if (opt.at(2) == "-r"){
qout << "Device release (RnD) is currently: " << ssu.release(true) << endl;
state = Idle;
} else {
qout << "Changing release from " << ssu.release()
<< " to " << opt.at(2) << endl;
qout << "Your device is now in release mode!" << endl;
ssu.setRelease(opt.at(2));
state = Idle;
}
} else if (opt.count() == 2) {
qout << "Device release is currently: " << ssu.release() << endl;
state = Idle;
} else if (opt.count() == 4 && opt.at(2) == "-r"){
qout << "Changing release (RnD) from " << ssu.release(true)
<< " to " << opt.at(3) << endl;
qout << "Your device is now in RnD mode!" << endl;
ssu.setRelease(opt.at(3), true);
state = Idle;
}
}
void RndSsuCli::optRepos(QStringList opt){
QTextStream qout(stdout);
SsuRepoManager repoManager;
SsuDeviceInfo deviceInfo;
QHash<QString, QString> repoParameters, repoOverride;
QString device="";
bool rndRepo = false;
int micMode=0, flagStart = 0;
if ((ssu.deviceMode() & Ssu::RndMode) == Ssu::RndMode)
rndRepo = true;
if (opt.count() >= 3 && opt.at(2) == "-m"){
micMode = 1;
// TODO: read the default mic override variables from some config
/*
repoOverride.insert("release", "@RELEASE@");
repoOverride.insert("rndRelease", "@RNDRELEASE@");
repoOverride.insert("flavour", "@FLAVOUR@");
repoOverride.insert("arch", "@ARCH@");
*/
}
if (opt.count() >= 3 + micMode){
// first argument is flag
if (opt.at(2 + micMode).contains("=")){
flagStart = 2 + micMode;
} else if (!opt.at(2 + micMode).contains("=") &&
opt.count() == 3 + micMode) {
// first (and only) argument is device)
device = opt.at(2 + micMode);
} else if(!opt.at(2 + micMode).contains("=") &&
opt.count() > 3 + micMode &&
opt.at(3 + micMode).contains("=")){
// first argument is device, second flag
device = opt.at(2 + micMode);
flagStart = 3 + micMode;
} else {
state = UserError;
return;
}
}
if (flagStart != 0){
for (int i=flagStart; i<opt.count(); i++){
if (opt.at(i).count("=") != 1){
qout << "Invalid flag: " << opt.at(i) << endl;
state = Idle;
return;
}
QStringList split = opt.at(i).split("=");
repoOverride.insert(split.at(0), split.at(1));
}
}
if (repoOverride.contains("rnd")){
if (repoOverride.value("rnd") == "true")
rndRepo = true;
else if (repoOverride.value("rnd") == "false")
rndRepo = false;
}
if (device != ""){
deviceInfo.setDeviceModel(device);
repoOverride.insert("model", device);
}
// TODO: rnd mode override needs to be implemented
QStringList repos;
// micMode? handle it and return, as it's a lot simpler than full mode
if (micMode){
repos = deviceInfo.repos(rndRepo, SsuDeviceInfo::BoardFilter);
foreach (const QString &repo, repos){
QString repoUrl = ssu.repoUrl(repo, rndRepo, repoParameters, repoOverride);
qout << "repo --name=" << repo << "-"
<< (rndRepo ? repoOverride.value("rndRelease")
: repoOverride.value("release"))
<< " --baseurl=" << repoUrl << endl;
}
state = Idle;
return;
}
if (device.isEmpty())
repos = deviceInfo.repos(rndRepo, SsuDeviceInfo::BoardFilterUserBlacklist);
else {
qout << "Printing repository configuration for '" << device << "'" << endl << endl;
repos = deviceInfo.repos(rndRepo, SsuDeviceInfo::BoardFilter);
}
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
qout << "Enabled repositories (global): " << endl;
for (int i=0; i<=3; i++){
// for each repository, print repo and resolve url
int longestField = 0;
foreach (const QString &repo, repos)
if (repo.length() > longestField)
longestField = repo.length();
qout.setFieldAlignment(QTextStream::AlignLeft);
foreach (const QString &repo, repos){
QString repoUrl = ssu.repoUrl(repo, rndRepo, repoParameters, repoOverride);
qout << " - " << qSetFieldWidth(longestField) << repo << qSetFieldWidth(0) << " ... " << repoUrl << endl;
}
if (i==0){
if (device != ""){
repos.clear();
continue;
}
repos = deviceInfo.repos(rndRepo, SsuDeviceInfo::UserFilter);
qout << endl << "Enabled repositories (user): " << endl;
} else if (i==1){
repos = deviceInfo.disabledRepos();
if (device.isEmpty())
qout << endl << "Disabled repositories (global, might be overridden by user config): " << endl;
else
qout << endl << "Disabled repositories (global): " << endl;
} else if (i==2) {
repos.clear();
if (device != "")
continue;
if (ssuSettings->contains("disabled-repos"))
repos.append(ssuSettings->value("disabled-repos").toStringList());
qout << endl << "Disabled repositories (user): " << endl;
}
}
state = Idle;
}
void RndSsuCli::optResolve(QStringList opt){
/*
* resolve URL and print
* TODO: arguments
*/
QString repo;
bool rndRepo=false;
/*
repo = arguments.at(2);
if (arguments.count() >= 3){
//qout << (arguments.at(3).compare("false")||arguments.at(3).compare("0"));
qout << (arguments.at(3).compare("false"));
}
qout << ssu.repoUrl(arguments.at(2));
QCoreApplication::exit(1);
*/
}
void RndSsuCli::optStatus(){
QTextStream qout(stdout);
SsuDeviceInfo deviceInfo;
/*
* print device information and registration status
*/
qout << "Device registration status: "
<< (ssu.isRegistered() ? "registered" : "not registered") << endl;
qout << "Device family: " << deviceInfo.deviceFamily() << endl;
qout << "Device model: " << deviceInfo.deviceModel() << endl;
qout << "Device variant: " << deviceInfo.deviceVariant() << endl;
qout << "Device UID: " << deviceInfo.deviceUid() << endl;
}
void RndSsuCli::optUpdateCredentials(QStringList opt){
QTextStream qout(stdout);
/*
* update the credentials
* optional argument: -f
*/
bool force=false;
if (opt.count() == 3 && opt.at(2) == "-f")
force=true;
if (!ssu.isRegistered()){
qout << "Device is not registered, can't update credentials" << endl;
state = Idle;
QCoreApplication::exit(1);
} else {
ssu.updateCredentials(force);
state = Busy;
}
}
void RndSsuCli::optUpdateRepos(){
SsuRepoManager repoManager;
repoManager.update();
}
void RndSsuCli::run(){
QTextStream qout(stdout);
QStringList arguments = QCoreApplication::arguments();
if (arguments.at(0).endsWith("rndssu"))
qout << "NOTE: this binary is now called ssu. The rndssu symlink will go away after some time" << endl;
// make sure there's a first argument to parse
if (arguments.count() < 2){
usage();
return;
}
// everything without additional arguments gets handled here
// functions with arguments need to take care of argument validation themselves
if (arguments.count() == 2){
if (arguments.at(1) == "register" || arguments.at(1) == "r")
optRegister();
else if (arguments.at(1) == "status" || arguments.at(1) == "s")
optStatus();
else if (arguments.at(1) == "updaterepos" || arguments.at(1) == "ur")
optUpdateRepos();
else
state = UserError;
} else if (arguments.count() >= 3){
if (arguments.at(1) == "addrepo" || arguments.at(1) == "ar")
optModifyRepo(Add, arguments);
else if (arguments.at(1) == "removerepo" || arguments.at(1) == "rr")
optModifyRepo(Remove, arguments);
else if (arguments.at(1) == "enablerepo" || arguments.at(1) == "er")
optModifyRepo(Enable, arguments);
else if (arguments.at(1) == "disablerepo" || arguments.at(1) == "dr")
optModifyRepo(Disable, arguments);
else if (arguments.at(1) == "resolve")
optResolve(arguments);
else
state = UserError;
} else
state = UserError;
// functions accepting 0 or more arguments; those need to set state to Idle
// on success
if (arguments.at(1) == "repos" || arguments.at(1) == "lr")
optRepos(arguments);
else if (arguments.at(1) == "flavour" || arguments.at(1) == "fl")
optFlavour(arguments);
else if (arguments.at(1) == "mode" || arguments.at(1) == "m")
optMode(arguments);
else if (arguments.at(1) == "release" || arguments.at(1) == "re")
optRelease(arguments);
else if (arguments.at(1) == "update" || arguments.at(1) == "up")
optUpdateCredentials(arguments);
// functions that need to wait for a response from ssu should set a flag so
// we can do default exit catchall here
if (state == Idle)
QCoreApplication::exit(0);
else if (state == UserError)
usage();
}
void RndSsuCli::usage(){
QTextStream qout(stdout);
qout << "\nUsage: ssu <command> [-command-options] [arguments]" << endl
<< endl
<< "Repository management:" << endl
<< "\tupdaterepos, ur \tupdate repository files" << endl
<< "\trepos, lr \tlist configured repositories" << endl
<< "\t [-m] \tformat output suitable for kickstart" << endl
<< "\t [device] \tuse repos for 'device'" << endl
<< "\t [flags] \tadditional flags" << endl
<< "\t rnd=<true|false> \tset rnd or release mode (default: take from host)" << endl
<< "\taddrepo, ar <repo> \tadd this repository" << endl
<< "\t [url] \tspecify URL, if not configured" << endl
<< "\tremoverepo, rr <repo> \tremove this repository from configuration" << endl
<< "\tenablerepo, er <repo> \tenable this repository" << endl
<< "\tdisablerepo, dr <repo> \tdisable this repository" << endl
<< endl
<< "Configuration management:" << endl
<< "\tflavour, fl \tdisplay flavour used (RnD only)" << endl
<< "\t [newflavour] \tset new flavour" << endl
<< "\trelease, re \tdisplay release used" << endl
<< "\t [-r] \tuse RnD release" << endl
<< "\t [newrelease] \tset new (RnD)release" << endl
<< "\tmode, m \tdisplay current device mode" << endl
<< "\t [newmode] \tset new device mode" << endl
<< endl
<< "Device management:" << endl
<< "\tstatus, s \tprint registration status and device information" << endl
<< "\tregister, r \tregister this device" << endl
<< "\tupdate, up \tupdate repository credentials" << endl
<< "\t [-f] \tforce update" << endl
<< endl;
qout.flush();
QCoreApplication::exit(1);
}