Skip to content

Latest commit

 

History

History
715 lines (608 loc) · 21.9 KB

ssucli.cpp

File metadata and controls

715 lines (608 loc) · 21.9 KB
 
Oct 8, 2012
Oct 8, 2012
1
/**
Aug 20, 2014
Aug 20, 2014
2
* @file ssucli.cpp
Oct 8, 2012
Oct 8, 2012
3
4
5
6
7
8
* @copyright 2012 Jolla Ltd.
* @author Bernd Wachter <bernd.wachter@jollamobile.com>
* @date 2012
*/
#include <QCoreApplication>
Mar 20, 2013
Mar 20, 2013
9
10
#include <termios.h>
Apr 6, 2013
Apr 6, 2013
11
#include <unistd.h>
Mar 20, 2013
Mar 20, 2013
12
Apr 4, 2013
Apr 4, 2013
13
14
#include "libssu/ssudeviceinfo.h"
#include "libssu/ssurepomanager.h"
Aug 21, 2015
Aug 21, 2015
15
16
#include "libssu/ssucoreconfig_p.h"
#include "libssu/ssuvariables_p.h"
Mar 30, 2013
Mar 30, 2013
17
18
#include <QDebug>
Mar 27, 2013
Mar 27, 2013
19
Aug 20, 2014
Aug 20, 2014
20
#include "ssucli.h"
Oct 8, 2012
Oct 8, 2012
21
Aug 20, 2014
Aug 20, 2014
22
SsuCli::SsuCli(): QObject(){
Oct 8, 2012
Oct 8, 2012
23
24
25
26
connect(this,SIGNAL(done()),
QCoreApplication::instance(),SLOT(quit()), Qt::DirectConnection);
connect(&ssu, SIGNAL(done()),
this, SLOT(handleResponse()));
Sep 3, 2013
Sep 3, 2013
27
28
29
30
31
32
ssuProxy = new SsuProxy("org.nemo.ssu", "/org/nemo/ssu", QDBusConnection::systemBus(), 0);
connect(ssuProxy, SIGNAL(done()),
this, SLOT(handleDBusResponse()));
Mar 30, 2013
Mar 30, 2013
33
state = Idle;
Oct 8, 2012
Oct 8, 2012
34
35
}
Aug 20, 2014
Aug 20, 2014
36
SsuCli::~SsuCli(){
Sep 4, 2013
Sep 4, 2013
37
38
39
ssuProxy->quit();
}
Aug 20, 2014
Aug 20, 2014
40
void SsuCli::handleResponse(){
Oct 8, 2012
Oct 8, 2012
41
42
43
44
45
QTextStream qout(stdout);
if (ssu.error()){
qout << "Last operation failed: \n" << ssu.lastError() << endl;
QCoreApplication::exit(1);
Sep 3, 2013
Sep 3, 2013
46
47
48
49
50
51
} else {
qout << "Operation successful (direct)" << endl;
QCoreApplication::exit(0);
}
}
Aug 20, 2014
Aug 20, 2014
52
void SsuCli::handleDBusResponse(){
Sep 3, 2013
Sep 3, 2013
53
54
55
56
57
QTextStream qout(stdout);
if (ssuProxy->error()){
qout << "Last operation failed: \n" << ssuProxy->lastError() << endl;
QCoreApplication::exit(1);
Oct 8, 2012
Oct 8, 2012
58
59
60
61
62
63
} else {
qout << "Operation successful" << endl;
QCoreApplication::exit(0);
}
}
Aug 20, 2014
Aug 20, 2014
64
void SsuCli::optDomain(QStringList opt){
Jul 4, 2013
Jul 4, 2013
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
QTextStream qout(stdout);
if (opt.count() == 3 && opt.at(2) == "-s"){
qout << ssu.domain();
state = Idle;
} else if (opt.count() == 3){
qout << "Changing domain from " << ssu.domain()
<< " to " << opt.at(2) << endl;
ssu.setDomain(opt.at(2));
state = Idle;
} else if (opt.count() == 2) {
qout << "Device domain is currently: " << ssu.domain() << endl;
state = Idle;
}
}
Oct 8, 2012
Oct 8, 2012
81
Aug 20, 2014
Aug 20, 2014
82
void SsuCli::optFlavour(QStringList opt){
Mar 19, 2013
Mar 19, 2013
83
QTextStream qout(stdout);
Sep 4, 2013
Sep 4, 2013
84
QTextStream qerr(stderr);
Mar 19, 2013
Mar 19, 2013
85
Apr 10, 2013
Apr 10, 2013
86
87
88
89
if (opt.count() == 3 && opt.at(2) == "-s"){
qout << ssu.flavour();
state = Idle;
} else if (opt.count() == 3){
Mar 19, 2013
Mar 19, 2013
90
qout << "Changing flavour from " << ssu.flavour()
Mar 30, 2013
Mar 30, 2013
91
<< " to " << opt.at(2) << endl;
Apr 6, 2013
Apr 6, 2013
92
Sep 4, 2013
Sep 4, 2013
93
94
95
96
97
98
99
100
101
102
QDBusPendingReply<> reply = ssuProxy->setFlavour(opt.at(2));
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus call failed, falling back to libssu" << endl;
ssu.setFlavour(opt.at(2));
SsuRepoManager repoManager;
repoManager.update();
uidWarning();
}
Apr 6, 2013
Apr 6, 2013
103
Mar 30, 2013
Mar 30, 2013
104
105
state = Idle;
} else if (opt.count() == 2) {
Mar 22, 2013
Mar 22, 2013
106
qout << "Device flavour is currently: " << ssu.flavour() << endl;
Mar 30, 2013
Mar 30, 2013
107
108
109
state = Idle;
}
}
Mar 19, 2013
Mar 19, 2013
110
Aug 20, 2014
Aug 20, 2014
111
void SsuCli::optMode(QStringList opt){
Mar 30, 2013
Mar 30, 2013
112
QTextStream qout(stdout);
Sep 4, 2013
Sep 4, 2013
113
QTextStream qerr(stderr);
Mar 30, 2013
Mar 30, 2013
114
115
116
117
// TODO: allow setting meaningful names instead of numbers
if (opt.count() == 2){
Mar 31, 2013
Mar 31, 2013
118
119
120
121
122
123
124
125
126
127
128
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");
Sep 10, 2014
Sep 10, 2014
129
130
if ((deviceMode & Ssu::UpdateMode) == Ssu::UpdateMode)
modeList.append("UpdateMode");
Mar 31, 2013
Mar 31, 2013
131
132
133
134
135
136
137
138
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;
Apr 10, 2013
Apr 10, 2013
139
140
141
state = Idle;
} else if (opt.count() == 3 && opt.at(2) == "-s"){
qout << ssu.deviceMode();
Mar 30, 2013
Mar 30, 2013
142
143
144
145
state = Idle;
} else if (opt.count() == 3){
qout << "Setting device mode from " << ssu.deviceMode()
<< " to " << opt.at(2) << endl;
Apr 6, 2013
Apr 6, 2013
146
Sep 4, 2013
Sep 4, 2013
147
148
149
150
QDBusPendingReply<> reply = ssuProxy->setDeviceMode(opt.at(2).toInt());
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus call failed, falling back to libssu" << endl;
Feb 2, 2015
Feb 2, 2015
151
ssu.setDeviceMode(Ssu::DeviceModeFlags(opt.at(2).toInt()));
Sep 4, 2013
Sep 4, 2013
152
153
154
155
156
SsuRepoManager repoManager;
repoManager.update();
uidWarning();
}
Apr 6, 2013
Apr 6, 2013
157
Mar 30, 2013
Mar 30, 2013
158
159
state = Idle;
}
Mar 30, 2013
Mar 30, 2013
160
161
}
Aug 20, 2014
Aug 20, 2014
162
void SsuCli::optModel(QStringList opt){
Apr 10, 2013
Apr 10, 2013
163
QTextStream qout(stdout);
Sep 3, 2013
Sep 3, 2013
164
QTextStream qerr(stderr);
Apr 10, 2013
Apr 10, 2013
165
166
167
168
169
170
171
172
173
174
175
SsuDeviceInfo deviceInfo;
if (opt.count() == 3 && opt.at(2) == "-s"){
qout << deviceInfo.deviceModel();
state = Idle;
} else if (opt.count() == 2){
qout << "Device model is: " << deviceInfo.deviceModel() << endl;
state = Idle;
}
}
Aug 20, 2014
Aug 20, 2014
176
void SsuCli::optModifyRepo(enum Actions action, QStringList opt){
Mar 30, 2013
Mar 30, 2013
177
178
SsuRepoManager repoManager;
QTextStream qout(stdout);
Apr 10, 2013
Apr 10, 2013
179
QTextStream qerr(stderr);
Mar 30, 2013
Mar 30, 2013
180
181
if (opt.count() == 3){
Sep 4, 2013
Sep 4, 2013
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
QDBusPendingReply<> reply = ssuProxy->modifyRepo(action, opt.at(2));
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus call failed, falling back to libssu" << endl;
switch(action){
case Add:
repoManager.add(opt.at(2));
repoManager.update();
uidWarning();
break;
case Remove:
repoManager.remove(opt.at(2));
repoManager.update();
uidWarning();
break;
case Disable:
repoManager.disable(opt.at(2));
repoManager.update();
uidWarning();
break;
case Enable:
repoManager.enable(opt.at(2));
repoManager.update();
uidWarning();
break;
}
Mar 30, 2013
Mar 30, 2013
209
210
}
} else if (opt.count() == 4 && action == Add){
Apr 10, 2013
Apr 10, 2013
211
212
213
214
215
216
217
218
219
220
221
222
223
QString url, repo;
if (opt.at(2).indexOf(QRegExp("[a-z]*://", Qt::CaseInsensitive)) == 0){
url = opt.at(2);
repo = opt.at(3);
} else if (opt.at(3).indexOf(QRegExp("[a-z]*://", Qt::CaseInsensitive)) == 0){
url = opt.at(3);
repo = opt.at(2);
} else {
qerr << "Invalid parameters for 'ssu ar': URL required." << endl;
return;
}
Sep 4, 2013
Sep 4, 2013
224
225
226
227
228
229
230
231
QDBusPendingReply<> reply = ssuProxy->addRepo(repo, url);
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus call failed, falling back to libssu" << endl;
repoManager.add(repo, url);
repoManager.update();
uidWarning();
}
Mar 30, 2013
Mar 30, 2013
232
}
Mar 19, 2013
Mar 19, 2013
233
234
}
Aug 20, 2014
Aug 20, 2014
235
void SsuCli::optRegister(QStringList opt){
Mar 19, 2013
Mar 19, 2013
236
237
238
239
240
241
242
/*
* register a new device
*/
QString username, password;
QTextStream qin(stdin);
QTextStream qout(stdout);
Sep 3, 2013
Sep 3, 2013
243
QTextStream qerr(stderr);
Aug 27, 2013
Aug 27, 2013
244
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
Mar 19, 2013
Mar 19, 2013
245
Mar 20, 2013
Mar 20, 2013
246
247
struct termios termNew, termOld;
Mar 19, 2013
Mar 19, 2013
248
249
qout << "Username: " << flush;
username = qin.readLine();
Mar 20, 2013
Mar 20, 2013
250
251
252
253
254
tcgetattr(STDIN_FILENO, &termNew);
termOld = termNew;
termNew.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &termNew) == -1)
Mar 22, 2013
Mar 22, 2013
255
qout << "WARNING: Unable to disable echo on your terminal, password will echo!" << endl;
Mar 20, 2013
Mar 20, 2013
256
Mar 19, 2013
Mar 19, 2013
257
258
qout << "Password: " << flush;
password = qin.readLine();
Sep 3, 2013
Sep 3, 2013
259
qout << endl;
Mar 19, 2013
Mar 19, 2013
260
Mar 20, 2013
Mar 20, 2013
261
262
tcsetattr(STDIN_FILENO, TCSANOW, &termOld);
Aug 27, 2013
Aug 27, 2013
263
264
265
if (opt.count() == 3 && opt.at(2) == "-h")
ssuSettings->setValue("repository-url-variables/user", username);
Sep 3, 2013
Sep 3, 2013
266
267
268
269
270
271
272
273
QDBusPendingReply<> reply = ssuProxy->registerDevice(username, password);
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus call failed, falling back to libssu" << endl;
qerr << reply.error().message() << endl;
ssu.sendRegistration(username, password);
}
Mar 30, 2013
Mar 30, 2013
274
275
276
state = Busy;
}
Aug 20, 2014
Aug 20, 2014
277
void SsuCli::optRelease(QStringList opt){
Mar 30, 2013
Mar 30, 2013
278
QTextStream qout(stdout);
Sep 4, 2013
Sep 4, 2013
279
QTextStream qerr(stderr);
Mar 30, 2013
Mar 30, 2013
280
281
282
283
284
285
286
287
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;
Mar 31, 2013
Mar 31, 2013
288
qout << "Your device is now in release mode!" << endl;
Apr 6, 2013
Apr 6, 2013
289
Sep 4, 2013
Sep 4, 2013
290
291
292
293
294
295
296
297
298
299
QDBusPendingReply<> reply = ssuProxy->setRelease(opt.at(2), false);
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus call failed, falling back to libssu" << endl;
ssu.setRelease(opt.at(2));
SsuRepoManager repoManager;
repoManager.update();
uidWarning();
}
Apr 6, 2013
Apr 6, 2013
300
Mar 30, 2013
Mar 30, 2013
301
302
303
304
305
306
307
308
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;
Mar 31, 2013
Mar 31, 2013
309
qout << "Your device is now in RnD mode!" << endl;
Apr 6, 2013
Apr 6, 2013
310
Sep 4, 2013
Sep 4, 2013
311
312
313
314
315
316
317
318
319
320
QDBusPendingReply<> reply = ssuProxy->setRelease(opt.at(3), true);
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus call failed, falling back to libssu" << endl;
ssu.setRelease(opt.at(3), true);
SsuRepoManager repoManager;
repoManager.update();
uidWarning();
}
Apr 6, 2013
Apr 6, 2013
321
Mar 30, 2013
Mar 30, 2013
322
323
324
325
state = Idle;
}
}
Aug 20, 2014
Aug 20, 2014
326
void SsuCli::optRepos(QStringList opt){
Mar 31, 2013
Mar 31, 2013
327
QTextStream qout(stdout);
Mar 30, 2013
Mar 30, 2013
328
329
SsuRepoManager repoManager;
SsuDeviceInfo deviceInfo;
Mar 31, 2013
Mar 31, 2013
330
331
QHash<QString, QString> repoParameters, repoOverride;
QString device="";
Mar 30, 2013
Mar 30, 2013
332
bool rndRepo = false;
Mar 31, 2013
Mar 31, 2013
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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@");
*/
}
Mar 30, 2013
Mar 30, 2013
348
Mar 31, 2013
Mar 31, 2013
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
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;
}
}
Mar 30, 2013
Mar 30, 2013
368
Mar 31, 2013
Mar 31, 2013
369
370
371
372
373
374
375
376
377
378
379
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));
}
}
Mar 30, 2013
Mar 30, 2013
380
Mar 31, 2013
Mar 31, 2013
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
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){
Oct 6, 2013
Oct 6, 2013
398
repos = repoManager.repos(rndRepo, deviceInfo, Ssu::BoardFilter);
Mar 31, 2013
Mar 31, 2013
399
foreach (const QString &repo, repos){
Apr 12, 2013
Apr 12, 2013
400
401
402
403
404
405
406
407
QString repoName = repo;
if (repo.endsWith("-debuginfo")){
repoName = repo.left(repo.size() - 10);
repoParameters.insert("debugSplit", "debug");
} else if (repoParameters.value("debugSplit") == "debug")
repoParameters.remove("debugSplit");
QString repoUrl = ssu.repoUrl(repoName, rndRepo, repoParameters, repoOverride);
Mar 31, 2013
Mar 31, 2013
408
409
410
411
412
413
414
415
416
417
qout << "repo --name=" << repo << "-"
<< (rndRepo ? repoOverride.value("rndRelease")
: repoOverride.value("release"))
<< " --baseurl=" << repoUrl << endl;
}
state = Idle;
return;
}
if (device.isEmpty())
Oct 6, 2013
Oct 6, 2013
418
repos = repoManager.repos(rndRepo, deviceInfo, Ssu::BoardFilterUserBlacklist);
Mar 31, 2013
Mar 31, 2013
419
420
else {
qout << "Printing repository configuration for '" << device << "'" << endl << endl;
Oct 6, 2013
Oct 6, 2013
421
repos = repoManager.repos(rndRepo, deviceInfo, Ssu::BoardFilter);
Mar 31, 2013
Mar 31, 2013
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
}
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){
Apr 12, 2013
Apr 12, 2013
437
438
439
440
441
442
443
444
QString repoName = repo;
if (repo.endsWith("-debuginfo")){
repoName = repo.left(repo.size() - 10);
repoParameters.insert("debugSplit", "debug");
} else if (repoParameters.value("debugSplit") == "debug")
repoParameters.remove("debugSplit");
QString repoUrl = ssu.repoUrl(repoName, rndRepo, repoParameters, repoOverride);
Mar 31, 2013
Mar 31, 2013
445
446
447
448
449
450
451
452
qout << " - " << qSetFieldWidth(longestField) << repo << qSetFieldWidth(0) << " ... " << repoUrl << endl;
}
if (i==0){
if (device != ""){
repos.clear();
continue;
}
Oct 6, 2013
Oct 6, 2013
453
repos = repoManager.repos(rndRepo, deviceInfo, Ssu::UserFilter);
Mar 31, 2013
Mar 31, 2013
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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;
}
Mar 30, 2013
Mar 30, 2013
469
470
471
}
state = Idle;
Mar 19, 2013
Mar 19, 2013
472
473
}
Jan 4, 2015
Jan 4, 2015
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
void SsuCli::optSet(QStringList opt){
QTextStream qout(stdout);
SsuVariables var;
SsuCoreConfig *settings = SsuCoreConfig::instance();
QHash<QString, QString> storageHash;
// set repository specific variable
if (opt.count() == 5 && opt.at(2) == "-r"){
settings->setValue("repository-url-variables/" + opt.at(3), opt.at(4));
// clear repo specific variable
} else if (opt.count() == 4 && opt.at(2) == "-r"){
settings->remove("repository-url-variables/" + opt.at(3));
// list repo specific variables
} else if (opt.count() == 3 && opt.at(2) == "-r"){
qout << "Repository specific variables:" << endl << endl;
var.variableSection(settings, "repository-url-variables", &storageHash);
// set global variable
} else if (opt.count() == 4){
settings->setValue("global-variables/" + opt.at(2), opt.at(3));
// clear global variable
} else if (opt.count() == 3){
settings->remove("global-variables/" + opt.at(2));
// list global variables
} else if (opt.count() == 2){
qout << "Global variables:" << endl << endl;
var.variableSection(settings, "global-variables", &storageHash);
}
settings->sync();
if (!storageHash.isEmpty()){
QHash<QString, QString>::const_iterator i = storageHash.constBegin();
while (i != storageHash.constEnd()){
qout << i.key() << "=" << i.value() << endl;
i++;
}
}
state = Idle;
}
Aug 20, 2014
Aug 20, 2014
515
void SsuCli::optStatus(QStringList opt){
Mar 19, 2013
Mar 19, 2013
516
QTextStream qout(stdout);
Sep 3, 2013
Sep 3, 2013
517
QTextStream qerr(stderr);
Mar 27, 2013
Mar 27, 2013
518
SsuDeviceInfo deviceInfo;
Mar 19, 2013
Mar 19, 2013
519
520
521
522
/*
* print device information and registration status
*/
Sep 3, 2013
Sep 3, 2013
523
524
525
526
527
528
529
530
531
532
533
QString deviceUid;
QDBusPendingReply<QString> reply = ssuProxy->deviceUid();
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus unavailable, UUID not necessarily connected to reality." << endl;
deviceUid = deviceInfo.deviceUid();
} else
deviceUid = reply.value();
Mar 19, 2013
Mar 19, 2013
534
535
qout << "Device registration status: "
<< (ssu.isRegistered() ? "registered" : "not registered") << endl;
Oct 24, 2013
Oct 24, 2013
536
537
538
qout << "Device model: " << deviceInfo.displayName(Ssu::DeviceModel) << " ("
<< deviceInfo.deviceModel() << " / "
<< deviceInfo.displayName(Ssu::DeviceDesignation) << ")" << endl;
Apr 10, 2013
Apr 10, 2013
539
540
if (deviceInfo.deviceVariant() != "")
qout << "Device variant: " << deviceInfo.deviceVariant() << endl;
Sep 3, 2013
Sep 3, 2013
541
qout << "Device UID: " << deviceUid << endl;
Apr 10, 2013
Apr 10, 2013
542
543
544
545
if ((ssu.deviceMode() & Ssu::RndMode) == Ssu::RndMode)
qout << "Release (rnd): " << ssu.release(true) << " (" << ssu.flavour() << ")" << endl;
else
qout << "Release: " << ssu.release() << endl;
Mar 10, 2015
Mar 10, 2015
546
qout << "Domain: " << ssu.domain() << endl;
Mar 19, 2013
Mar 19, 2013
547
548
}
Aug 20, 2014
Aug 20, 2014
549
void SsuCli::optUpdateCredentials(QStringList opt){
Mar 19, 2013
Mar 19, 2013
550
551
552
553
554
QTextStream qout(stdout);
/*
* update the credentials
* optional argument: -f
*/
Mar 30, 2013
Mar 30, 2013
555
556
557
558
bool force=false;
if (opt.count() == 3 && opt.at(2) == "-f")
force=true;
Mar 19, 2013
Mar 19, 2013
559
560
if (!ssu.isRegistered()){
qout << "Device is not registered, can't update credentials" << endl;
Mar 30, 2013
Mar 30, 2013
561
state = Idle;
Mar 19, 2013
Mar 19, 2013
562
QCoreApplication::exit(1);
Mar 30, 2013
Mar 30, 2013
563
} else {
Mar 19, 2013
Mar 19, 2013
564
ssu.updateCredentials(force);
Mar 30, 2013
Mar 30, 2013
565
566
567
state = Busy;
}
}
Mar 19, 2013
Mar 19, 2013
568
Aug 20, 2014
Aug 20, 2014
569
void SsuCli::optUpdateRepos(QStringList opt){
Sep 4, 2013
Sep 4, 2013
570
571
572
573
574
575
576
577
578
579
QTextStream qerr(stdout);
QDBusPendingReply<> reply = ssuProxy->updateRepos();
reply.waitForFinished();
if (reply.isError()){
qerr << "DBus call failed, falling back to libssu" << endl;
SsuRepoManager repoManager;
repoManager.update();
uidWarning();
}
Mar 19, 2013
Mar 19, 2013
580
581
}
Aug 20, 2014
Aug 20, 2014
582
void SsuCli::run(){
Oct 8, 2012
Oct 8, 2012
583
QTextStream qout(stdout);
Sep 4, 2013
Sep 4, 2013
584
QTextStream qerr(stderr);
Oct 8, 2012
Oct 8, 2012
585
586
587
QStringList arguments = QCoreApplication::arguments();
Sep 4, 2013
Sep 4, 2013
588
589
590
591
SsuCoreConfig *ssuSettings = SsuCoreConfig::instance();
if (!ssuSettings->isWritable())
qerr << "WARNING: ssu.ini does not seem to be writable. Setting values might not work." << endl;
Mar 19, 2013
Mar 19, 2013
592
593
594
595
596
// make sure there's a first argument to parse
if (arguments.count() < 2){
usage();
return;
}
Oct 8, 2012
Oct 8, 2012
597
Aug 20, 2014
Aug 20, 2014
598
599
600
601
602
struct {
const char *longopt; // long option name
const char *shortopt; // option shortcut name
int minargs; // minimum number of required args
int maxargs; // -1 for "function will handle max args"
Aug 20, 2014
Aug 20, 2014
603
void (SsuCli::*handler)(QStringList opt); // handler function
Aug 20, 2014
Aug 20, 2014
604
605
} handlers[] = {
// functions accepting no additional arguments
Aug 20, 2014
Aug 20, 2014
606
607
"status", "s", 0, 0, &SsuCli::optStatus,
"updaterepos", "ur", 0, 0, &SsuCli::optUpdateRepos,
Aug 20, 2014
Aug 20, 2014
608
609
// functions requiring at least one argument
Aug 20, 2014
Aug 20, 2014
610
611
612
613
"addrepo", "ar", 1, 2, &SsuCli::optAddRepo,
"removerepo", "rr", 1, 1, &SsuCli::optRemoveRepo,
"enablerepo", "er", 1, 1, &SsuCli::optEnableRepo,
"disablerepo", "dr", 1, 1, &SsuCli::optDisableRepo,
Aug 20, 2014
Aug 20, 2014
614
615
616
// functions accepting 0 or more arguments
// those need to set state to Idle on success
Aug 20, 2014
Aug 20, 2014
617
618
619
620
621
622
623
624
"register", "r", 0, -1, &SsuCli::optRegister,
"repos", "lr", 0, -1, &SsuCli::optRepos,
"flavour", "fl", 0, -1, &SsuCli::optFlavour,
"mode", "m", 0, -1, &SsuCli::optMode,
"model", "mo", 0, -1, &SsuCli::optModel,
"release", "re", 0, -1, &SsuCli::optRelease,
"update", "up", 0, -1, &SsuCli::optUpdateCredentials,
"domain", "do", 0, -1, &SsuCli::optDomain,
Jan 4, 2015
Jan 4, 2015
625
"set", "set", 0, -1, &SsuCli::optSet,
Aug 20, 2014
Aug 20, 2014
626
627
628
};
bool found = false;
Aug 22, 2014
Aug 22, 2014
629
int argc = arguments.count() - 2;
Aug 20, 2014
Aug 20, 2014
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
for (int i=0; i<sizeof(handlers)/sizeof(handlers[0]); i++) {
if ((arguments.at(1) != handlers[i].longopt) &&
(arguments.at(1) != handlers[i].shortopt)) {
continue;
}
if (argc < handlers[i].minargs) {
usage(QString("%1: Too few arguments").arg(handlers[i].longopt));
return;
}
if (handlers[i].maxargs != -1 && argc > handlers[i].maxargs) {
usage(QString("%1: Too many arguments").arg(handlers[i].longopt));
return;
}
// Call option handler
(this->*(handlers[i].handler))(arguments);
found = true;
break;
}
if (!found)
Mar 30, 2013
Mar 30, 2013
655
state = UserError;
Aug 20, 2014
Aug 20, 2014
656
Mar 30, 2013
Mar 30, 2013
657
658
659
660
661
// 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)
Oct 8, 2012
Oct 8, 2012
662
663
664
usage();
}
Aug 20, 2014
Aug 20, 2014
665
void SsuCli::uidWarning(QString message){
Apr 6, 2013
Apr 6, 2013
666
667
668
669
670
671
672
673
674
if (message.isEmpty())
message = "Run 'ssu ur' as root to recreate repository files";
if (geteuid() != 0){
QTextStream qout(stderr);
qout << "You're not root. " << message << endl;
}
}
Aug 20, 2014
Aug 20, 2014
675
void SsuCli::usage(QString message){
Apr 6, 2013
Apr 6, 2013
676
QTextStream qout(stderr);
Mar 30, 2013
Mar 30, 2013
677
678
679
680
681
682
683
684
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
Mar 31, 2013
Mar 31, 2013
685
<< "\t rnd=<true|false> \tset rnd or release mode (default: take from host)" << endl
Mar 30, 2013
Mar 30, 2013
686
687
688
689
690
691
692
<< "\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
Jan 4, 2015
Jan 4, 2015
693
694
695
696
697
698
699
700
701
<< "\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
<< "\tset \tdisplay global variables" << endl
<< "\t [-r] \toperate on repository only variables" << endl
<< "\t <variable> \tdisplay value of <variable>" << endl
<< "\t <variable> <value> \tset value of <variable> to <value>" << endl
Mar 30, 2013
Mar 30, 2013
702
703
704
705
<< endl
<< "Device management:" << endl
<< "\tstatus, s \tprint registration status and device information" << endl
<< "\tregister, r \tregister this device" << endl
Aug 27, 2013
Aug 27, 2013
706
<< "\t [-h] \tconfigure user for OBS home" << endl
Mar 30, 2013
Mar 30, 2013
707
708
<< "\tupdate, up \tupdate repository credentials" << endl
<< "\t [-f] \tforce update" << endl
Apr 10, 2013
Apr 10, 2013
709
<< "\tmodel, mo \tprint name of device model (like N9)" << endl
Mar 30, 2013
Mar 30, 2013
710
<< endl;
Aug 20, 2014
Aug 20, 2014
711
712
if (!message.isEmpty())
qout << message << endl;
Mar 30, 2013
Mar 30, 2013
713
qout.flush();
Oct 8, 2012
Oct 8, 2012
714
715
QCoreApplication::exit(1);
}