Debian Codegraph @

src/systeminfo/linux/qdeviceinfo_linux.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qdeviceinfo_linux_p.h"
41
42 #if !defined(QT_NO_OFONO)
43 #include "qofonowrapper_p.h"
44 #endif // QT_NO_OFONO
45
46 #if defined(QT_USE_SSU)
47 #include <ssudeviceinfo.h>
48 #endif // QT_USE_SSU
49
50 #include "qscreensaver_linux_p.h"
51 #include <QNetworkInfo>
52
53 #include <QtCore/qdir.h>
54 #include <QtCore/qmetaobject.h>
55 #include <QtCore/qprocess.h>
56 #include <QtCore/qregularexpression.h>
57 #include <QtCore/qtextstream.h>
58 #include <QtCore/qtimer.h>
59 #include <QtCore/qstandardpaths.h>
60
61 #include <fcntl.h>
62 #include <linux/videodev2.h>
63 #include <sys/ioctl.h>
64 #include <sys/types.h>
65 #include <sys/stat.h>
66 #include <QUuid>
67 #include <QCryptographicHash>
68
69 #if !defined(QT_NO_DBUS)
70 #include <QtDBus/QDBusInterface>
71 #include <QtDBus/QDBusReply>
72 #include <QtDBus/QDBusConnection>
73 #include <QtDBus/QDBusAbstractInterface>
74 #include <QtDBus/QDBusError>
75 #include <QtDBus/QDBusMessage>
76 #endif
77
78 QT_BEGIN_NAMESPACE
79
80 QDeviceInfoPrivate::QDeviceInfoPrivate(QDeviceInfo *parent)
81 : QObject(parent)
82 #if !defined(QT_SIMULATOR)
83 , q_ptr(parent)
84 #endif // QT_SIMULATOR
85 , watchThermalState(false)
86 , imeiBuffer(QStringList())
87 , uniqueDeviceIDBuffer(QString())
88 , timer(0)
89 , boardNameString(QString())
90 , osName(QString())
91 #if !defined(QT_NO_OFONO)
92 , ofonoWrapper(0)
93 #endif // QT_NO_OFONO
94 ,connectedBtPower(0)
95 ,btPowered(0)
96 {
97 }
98
99 bool QDeviceInfoPrivate::hasFeature(QDeviceInfo::Feature feature)
100 {
101 switch (feature) {
102 case QDeviceInfo::BluetoothFeature:
103 if (QDir(QStringLiteral("/sys/class/bluetooth/")).entryList(QDir::Dirs | QDir::NoDotAndDotDot).size() > 0)
104 return true;
105 return false;
106
107 case QDeviceInfo::CameraFeature: {
108 const QString devfsPath(QStringLiteral("/dev/"));
109 const QStringList dirs = QDir(devfsPath).entryList(QStringList() << QStringLiteral("video*"), QDir::System);
110 foreach (const QString &dir, dirs) {
111 QFile dev(devfsPath + dir);
112 if (!dev.open(QIODevice::ReadWrite))
113 continue;
114 struct v4l2_capability capability;
115 memset(&capability, 0, sizeof(struct v4l2_capability));
116 if (ioctl(dev.handle(), VIDIOC_QUERYCAP, &capability) != -1
117 && (capability.capabilities & V4L2_CAP_VIDEO_CAPTURE) == V4L2_CAP_VIDEO_CAPTURE) {
118 return true;
119 }
120 }
121 return false;
122 }
123
124 case QDeviceInfo::FmRadioFeature:
125 if (QDir(QStringLiteral("/sys/class/video4linux/")).entryList(QStringList() << QStringLiteral("radio*")).size() > 0)
126 return true;
127 return false;
128
129 case QDeviceInfo::FmTransmitterFeature: {
130 const QString devfsPath(QStringLiteral("/dev/"));
131 const QStringList dirs = QDir(devfsPath).entryList(QStringList() << QStringLiteral("radio*"), QDir::System);
132 foreach (const QString &dir, dirs) {
133 QFile dev(devfsPath + dir);
134 if (!dev.open(QIODevice::ReadWrite))
135 continue;
136 struct v4l2_capability capability;
137 memset(&capability, 0, sizeof(struct v4l2_capability));
138 if (ioctl(dev.handle(), VIDIOC_QUERYCAP, &capability) != -1
139 && (capability.capabilities & (V4L2_CAP_RADIO | V4L2_CAP_MODULATOR)) == (V4L2_CAP_RADIO | V4L2_CAP_MODULATOR)) {
140 return true;
141 }
142 }
143 return false;
144 }
145
146 case QDeviceInfo::InfraredFeature:
147 // TODO: find the kernel interface for this
148 return false;
149
150 case QDeviceInfo::LedFeature:
151 if (QDir(QStringLiteral("/sys/class/leds/")).entryList(QDir::Dirs | QDir::NoDotAndDotDot).size() > 0)
152 return true;
153 return false;
154
155 case QDeviceInfo::MemoryCardFeature:
156 if (QDir(QStringLiteral("/sys/class/mmc_host/")).entryList(QStringList() << QStringLiteral("mmc*")).size() > 0)
157 return true;
158 return false;
159
160 case QDeviceInfo::UsbFeature:
161 if (QDir(QStringLiteral("/sys/bus/usb/devices/")).entryList(QStringList() << QStringLiteral("usb*")).size() > 0)
162 return true;
163 return false;
164
165 case QDeviceInfo::VibrationFeature:
166 if (QDir(QStringLiteral("/sys/bus/platform/devices/")).entryList(QStringList() << QStringLiteral("*vib*")).size() > 0)
167 return true;
168 return false;
169
170 case QDeviceInfo::WlanFeature:
171 if ((QDir(QStringLiteral("/sys/class/ieee80211/")).entryList(QDir::Dirs | QDir::NoDotAndDotDot).size() > 0)
172 || (QDir(QStringLiteral("/sys/class/net/")).entryList(QStringList() << QStringLiteral("wlan*")).size() > 0))
173 return true;
174 return false;
175
176 case QDeviceInfo::SimFeature:
177 #if !defined(QT_NO_OFONO)
178 if (QOfonoWrapper::isOfonoAvailable()) {
179 if (!ofonoWrapper)
180 ofonoWrapper = new QOfonoWrapper(this);
181 return (ofonoWrapper->allModems().size() > 0);
182 }
183 #endif
184 return false;
185
186 case QDeviceInfo::PositioningFeature:
187 // TODO: find the kernel interface for this
188 if (QDir(QStringLiteral("/sys/class/misc/")).entryList(QStringList() << QStringLiteral("*nmea*")).size() > 0)
189 return true;
190 return false;
191
192 case QDeviceInfo::VideoOutFeature: {
193 const QString devfsPath(QStringLiteral("/dev/"));
194 const QStringList dirs = QDir(devfsPath).entryList(QStringList() << QStringLiteral("video*"), QDir::System);
195 foreach (const QString &dir, dirs) {
196 QFile dev(devfsPath + dir);
197 if (!dev.open(QIODevice::ReadWrite))
198 continue;
199 struct v4l2_capability capability;
200 memset(&capability, 0, sizeof(struct v4l2_capability));
201 if (ioctl(dev.handle(), VIDIOC_QUERYCAP, &capability) != -1
202 && (capability.capabilities & V4L2_CAP_VIDEO_OUTPUT) == V4L2_CAP_VIDEO_OUTPUT) {
203 return true;
204 }
205 }
206 if (QDir(QStringLiteral("/sys/class/video_output/")).entryList(QDir::Dirs | QDir::NoDotAndDotDot).size() > 0)
207 return true;
208 return false;
209 }
210
211 case QDeviceInfo::HapticsFeature:
212 if (QDir(QStringLiteral("/sys/class/haptic/")).entryList(QDir::Dirs | QDir::NoDotAndDotDot).size() > 0)
213 return true;
214 return false;
215
216 case QDeviceInfo::NfcFeature:
217 // As of now, it's the only supported NFC device in the kernel
218 return QFile::exists(QStringLiteral("/dev/pn544"));
219 }
220
221 return false;
222 }
223
224 QDeviceInfo::LockTypeFlags QDeviceInfoPrivate::activatedLocks()
225 {
226 return QDeviceInfo::NoLock;
227 }
228
229 QDeviceInfo::LockTypeFlags QDeviceInfoPrivate::enabledLocks()
230 {
231 QDeviceInfo::LockTypeFlags enabledLocks = QDeviceInfo::NoLock;
232
233 QScreenSaverPrivate screenSaver(0);
234 if (screenSaver.screenSaverEnabled())
235 enabledLocks = QDeviceInfo::TouchOrKeyboardLock;
236
237 return enabledLocks;
238 }
239
240 QDeviceInfo::ThermalState QDeviceInfoPrivate::thermalState()
241 {
242 if (watchThermalState)
243 return currentThermalState;
244 else
245 return getThermalState();
246 }
247
248 int QDeviceInfoPrivate::imeiCount()
249 {
250 if (imeiBuffer.size() == 0)
251 imei(0);
252
253 return imeiBuffer.size();
254 }
255
256 QString QDeviceInfoPrivate::imei(int interfaceNumber)
257 {
258 #if !defined(QT_NO_OFONO)
259 imeiBuffer.clear();
260 if (QOfonoWrapper::isOfonoAvailable()) {
261 if (!ofonoWrapper)
262 ofonoWrapper = new QOfonoWrapper(this);
263 QStringList modems = ofonoWrapper->allModems();
264 foreach (const QString &modem, modems) {
265 if (!modem.isEmpty())
266 imeiBuffer += ofonoWrapper->imei(modem);
267 }
268 }
269 #else
270 Q_UNUSED(interfaceNumber)
271 #endif
272 if (interfaceNumber >= 0 && interfaceNumber < imeiBuffer.size())
273 return imeiBuffer[interfaceNumber];
274 else
275 return QString();
276 }
277
278 QString QDeviceInfoPrivate::manufacturer()
279 {
280 #if defined(QT_USE_SSU)
281 if (manufacturerBuffer.isEmpty()) {
282 manufacturerBuffer = SsuDeviceInfo().displayName(Ssu::DeviceManufacturer);
283 }
284 #endif
285 if (manufacturerBuffer.isEmpty()) {
286 // for dmi enabled kernels
287 QFile file(QStringLiteral("/sys/devices/virtual/dmi/id/sys_vendor"));
288 if (file.open(QIODevice::ReadOnly))
289 manufacturerBuffer = QString::fromLocal8Bit(file.readAll().simplified().data());
290 }
291 if (manufacturerBuffer.isEmpty()) {
292 QStringList releaseFies = QDir(QStringLiteral("/etc/")).entryList(QStringList() << QStringLiteral("*-release"));
293 foreach (const QString &file, releaseFies) {
294 if (!manufacturerBuffer.isEmpty())
295 continue;
296 QFile release(QStringLiteral("/etc/") + file);
297 if (release.open(QIODevice::ReadOnly)) {
298 QTextStream stream(&release);
299 QString line;
300 do {
301 line = stream.readLine();
302 // this seems to be mer specific
303 if (line.startsWith(QStringLiteral("BUILD"))) {
304 manufacturerBuffer = line.split(QStringLiteral(":")).at(1).simplified().split(QStringLiteral("-")).at(0);
305 break;
306 }
307 } while (!line.isNull());
308 release.close();
309 }
310 }
311 }
312 if (manufacturerBuffer.isEmpty()) {
313 manufacturerBuffer = findInRelease(QStringLiteral("BUILD"));
314 }
315 return manufacturerBuffer;
316 }
317
318 QString QDeviceInfoPrivate::model()
319 {
320 #if defined(QT_USE_SSU)
321 if (modelBuffer.isEmpty()) {
322 modelBuffer = SsuDeviceInfo().displayName(Ssu::DeviceModel);
323 }
324 #endif
325 if (modelBuffer.isEmpty()) {
326 modelBuffer = findInRelease(QStringLiteral("NAME"),QStringLiteral("hw-release"));
327 }
328 // for dmi enabled kernels
329 if (modelBuffer.isEmpty()) {
330 QFile file(QStringLiteral("/sys/devices/virtual/dmi/id/product_name"));
331 if (file.open(QIODevice::ReadOnly))
332 modelBuffer = QString::fromLocal8Bit(file.readAll().simplified().data());
333 }
334 if (modelBuffer.isEmpty()) {
335 QStringList releaseFies = QDir(QStringLiteral("/etc/")).entryList(QStringList() << QStringLiteral("*-release"));
336 foreach (const QString &file, releaseFies) {
337 if (!modelBuffer.isEmpty())
338 continue;
339 QFile release(QStringLiteral("/etc/") + file);
340 if (release.open(QIODevice::ReadOnly)) {
341 QTextStream stream(&release);
342 QString line;
343 do {
344 line = stream.readLine();
345 // this seems to be mer specific
346 if (line.startsWith(QStringLiteral("BUILD"))) {
347 modelBuffer = line.split(QStringLiteral(":")).at(1).split(QStringLiteral("-")).at(3);
348 break;
349 }
350 } while (!line.isNull());
351 release.close();
352 }
353 }
354 }
355 return modelBuffer;
356 }
357
358 QString QDeviceInfoPrivate::productName()
359 {
360 #if defined(QT_USE_SSU)
361 if (productNameBuffer.isEmpty()) {
362 productNameBuffer = SsuDeviceInfo().displayName(Ssu::DeviceDesignation);
363 }
364 #endif
365
366 if (productNameBuffer.isEmpty()) {
367 productNameBuffer = findInRelease(QStringLiteral("PRETTY_NAME")).remove(QStringLiteral("\""));
368 }
369
370 if (productNameBuffer.isEmpty()) {
371 QProcess lsbRelease;
372 lsbRelease.start(QStringLiteral("/usr/bin/lsb_release"),
373 QStringList() << QStringLiteral("-c"));
374 if (lsbRelease.waitForFinished()) {
375 QString buffer(QString::fromLocal8Bit(lsbRelease.readAllStandardOutput().constData()));
376 productNameBuffer = buffer.section(QChar::fromLatin1('\t'), 1, 1).simplified();
377 }
378 }
379
380 return productNameBuffer;
381 }
382
383 QString QDeviceInfoPrivate::uniqueDeviceID()
384 {
385 if (uniqueDeviceIDBuffer.isEmpty()) {
386 // for dmi enabled kernels
387 QFile file(QStringLiteral("/sys/devices/virtual/dmi/id/product_uuid"));
388 if (file.open(QIODevice::ReadOnly)) {
389 QString id = QString::fromLocal8Bit(file.readAll().simplified().data());
390 if (id.length() == 36) {
391 if (isUuid(id)) {
392 uniqueDeviceIDBuffer = id;
393 }
394 }
395 }
396 }
397
398 if (uniqueDeviceIDBuffer.isEmpty()) {
399 QFile file(QStringLiteral("/etc/unique-id"));
400 if (file.open(QIODevice::ReadOnly)) {
401 QString id = QString::fromLocal8Bit(file.readAll().simplified().data());
402 if (id.length() == 32) {
403 id = id.insert(8,'-').insert(13,'-').insert(18,'-').insert(23,'-');
404 if (isUuid(id)) {
405 uniqueDeviceIDBuffer = id;
406 }
407 file.close();
408 }
409 }
410 }
411
412 if (uniqueDeviceIDBuffer.isEmpty()) { //try wifi mac address
413 QNetworkInfo netinfo;
414 QString macaddy;
415 macaddy = netinfo.macAddress(QNetworkInfo::WlanMode,0);
416 if (macaddy.isEmpty())
417 macaddy = netinfo.macAddress(QNetworkInfo::EthernetMode,0);
418 if (!macaddy.isEmpty()) {
419 QCryptographicHash hash2(QCryptographicHash::Sha1);
420 hash2.addData(macaddy.toLocal8Bit());
421
422 QUuid id = QUuid::fromRfc4122(hash2.result().left(16));
423 if (!id.isNull())
424 uniqueDeviceIDBuffer = id.toString();
425 }
426 }
427 if (uniqueDeviceIDBuffer.isEmpty()) {
428 QFile file(QStringLiteral("/etc/machine-id"));
429 if (file.open(QIODevice::ReadOnly)) {
430 QString id = QString::fromLocal8Bit(file.readAll().simplified().data());
431 if (id.length() == 32) {
432 id = id.insert(8,'-').insert(13,'-').insert(18,'-').insert(23,'-');
433 if (isUuid(id)) {
434 uniqueDeviceIDBuffer = id;
435 }
436 }
437 file.close();
438 }
439 }
440
441 //last ditch effort
442 if (uniqueDeviceIDBuffer.isEmpty()) {
443 QFile file(QStringLiteral("/var/lib/dbus/machine-id"));
444
445 if (file.open(QIODevice::ReadOnly)) {
446 QString id = QString::fromLocal8Bit(file.readAll().simplified().data());
447 if (id.length() == 32) {
448 id = id.insert(8,'-').insert(13,'-').insert(18,'-').insert(23,'-');
449 if (isUuid(id)) {
450 uniqueDeviceIDBuffer = id;
451 }
452 }
453 file.close();
454 }
455 }
456
457
458 return uniqueDeviceIDBuffer;
459 }
460
461 bool QDeviceInfoPrivate::isUuid(const QString &id)
462 {
463 QUuid uid(id); //make sure this can be made into a valid QUUid
464 return !uid.isNull();
465 }
466
467 QString QDeviceInfoPrivate::version(QDeviceInfo::Version type)
468 {
469 switch (type) {
470 case QDeviceInfo::Os:
471
472 if (versionBuffer[0].isEmpty()) {
473 versionBuffer[0] = findInRelease(QStringLiteral("VERSION_ID"),
474 QStringLiteral("os-release"));
475 }
476
477 if (versionBuffer[0].isEmpty()) {
478 versionBuffer[0] = findInRelease(QStringLiteral("VERSION_ID"));
479 }
480
481 if (versionBuffer[0].isEmpty() && QFile::exists(QStringLiteral("/usr/bin/lsb_release"))) {
482 QProcess lsbRelease;
483 lsbRelease.start(QStringLiteral("/usr/bin/lsb_release"),
484 QStringList() << QStringLiteral("-r"));
485 if (lsbRelease.waitForFinished()) {
486 QString buffer(QString::fromLocal8Bit(lsbRelease.readAllStandardOutput().constData()));
487 versionBuffer[0] = buffer.section(QChar::fromLatin1('\t'), 1, 1).simplified();
488 }
489 }
490
491 return versionBuffer[0];
492
493 case QDeviceInfo::Firmware:
494 // Try to read hardware adaptation version first.
495 if (versionBuffer[1].isEmpty()) {
496 versionBuffer[1] = findInRelease(QStringLiteral("VERSION_ID"),
497 QStringLiteral("hw-release"));
498 }
499
500 if (versionBuffer[1].isEmpty()) {
501 QFile file(QStringLiteral("/proc/sys/kernel/osrelease"));
502 if (file.open(QIODevice::ReadOnly)) {
503 versionBuffer[1] = QString::fromLocal8Bit(file.readAll().simplified().data());
504 file.close();
505 }
506 }
507 return versionBuffer[1];
508 }
509
510 return QString();
511 }
512
513 QString QDeviceInfoPrivate::operatingSystemName()
514 {
515 if (osName.isEmpty()) {
516 osName = findInRelease(QStringLiteral("NAME="), QStringLiteral("os-release"));
517 }
518 if (osName.isEmpty()) {
519 osName = findInRelease(QStringLiteral("NAME="));
520 }
521
522 return osName;
523 }
524
525 QString QDeviceInfoPrivate::boardName()
526 {
527 if (boardNameString.isEmpty()) {
528 QFile boardfile(QStringLiteral("/etc/boardname"));
529 if (boardfile.open(QIODevice::ReadOnly))
530 boardNameString = QString::fromLocal8Bit(boardfile.readAll().simplified().data());
531 }
532
533 if (boardNameString.isEmpty()) {
534 // for dmi enabled kernels
535 QFile file(QStringLiteral("/sys/devices/virtual/dmi/id/board_name"));
536 if (file.open(QIODevice::ReadOnly))
537 boardNameString = QString::fromLocal8Bit(file.readAll().simplified().data());
538 }
539 return boardNameString;
540 }
541
542 QString QDeviceInfoPrivate::findInRelease(const QString &searchTerm, const QString &file)
543 {
544 QString result;
545 QStringList releaseFiles;
546 if (file.isEmpty()) {
547 releaseFiles = QDir(QStringLiteral("/etc/")).entryList(QStringList() << QStringLiteral("*-release"));
548 } else {
549 releaseFiles.append(file);
550 }
551
552 foreach (const QString &file, releaseFiles) {
553 if (!result.isEmpty())
554 continue;
555 QFile release(QStringLiteral("/etc/") + file);
556 if (release.open(QIODevice::ReadOnly)) {
557 QTextStream stream(&release);
558 QString line;
559 do {
560 line = stream.readLine();
561 if (line.left(searchTerm.size()) == searchTerm) {
562 result = line.split(QStringLiteral("=")).at(1).simplified();
563 // Remove optional quotation marks.
564 result.remove(QRegularExpression(QStringLiteral("^\"|\"$")));
565 break;
566 }
567 } while (!line.isNull());
568 release.close();
569 }
570 }
571 return result;
572 }
573
574 #if !defined(QT_NO_DBUS)
575 void QDeviceInfoPrivate::bluezPropertyChanged(const QString &str, QDBusVariant v)
576 {
577
578 if (str == QStringLiteral("Powered")) {
579 if (btPowered != v.variant().toBool()) {
580 btPowered = !btPowered;
581 Q_EMIT bluetoothStateChanged(btPowered);
582 }
583 } else if (str == QStringLiteral("Adapters")) {
584 bool oldPoweredState = btPowered;
585 if (oldPoweredState != currentBluetoothPowerState())
586 Q_EMIT bluetoothStateChanged(btPowered);
587 }
588 }
589
590 void QDeviceInfoPrivate::connectBtPowered()
591 {
592 if (connectedBtPower) {
593 QDBusInterface *connectionInterface;
594 connectionInterface = new QDBusInterface(QStringLiteral("org.bluez"), QStringLiteral("/"),
595 QStringLiteral("org.bluez.Manager"),
596 QDBusConnection::systemBus(), this);
597 if (connectionInterface->isValid()) {
598 QDBusReply <QDBusObjectPath> reply = connectionInterface->call(QStringLiteral("DefaultAdapter"));
599 if (reply.isValid() && !reply.value().path().isEmpty()) {
600 if (!QDBusConnection::systemBus().connect(QStringLiteral("org.bluez"), reply.value().path(),
601 QStringLiteral("org.bluez.Adapter"),
602 QStringLiteral("PropertyChanged"),
603 this,
604 SLOT(bluezPropertyChanged(QString,QDBusVariant)))) {
605 }
606 }
607 }
608 connectedBtPower = true;
609 }
610 }
611 #endif
612
613 bool QDeviceInfoPrivate::currentBluetoothPowerState()
614 {
615 bool powered = false;
616 #ifndef QT_NO_DBUS
617 QDBusInterface *connectionInterface = new QDBusInterface(QStringLiteral("org.bluez"),
618 QStringLiteral("/"),
619 QStringLiteral("org.bluez.Manager"),
620 QDBusConnection::systemBus(), this);
621 if (connectionInterface->isValid()) {
622 QDBusReply<QDBusObjectPath> reply = connectionInterface->call(QStringLiteral("DefaultAdapter"));
623 if (reply.isValid() && !reply.value().path().isEmpty()) {
624 QDBusInterface *adapterInterface = new QDBusInterface(QStringLiteral("org.bluez"),
625 reply.value().path(),
626 QStringLiteral("org.bluez.Adapter"),
627 QDBusConnection::systemBus(), this);
628 if (adapterInterface->isValid()) {
629 QDBusReply<QVariantMap> reply = adapterInterface->call(QStringLiteral("GetProperties"));
630 QVariantMap map = reply.value();
631 QString property = QStringLiteral("Powered");
632 if (map.contains(property))
633 powered = map.value(property).toBool();
634 } else {
635 powered = false;
636 }
637 } else {
638 powered = false;
639 }
640 }
641 #endif
642 return btPowered = powered;
643 }
644
645 extern QMetaMethod proxyToSourceSignal(const QMetaMethod &, QObject *);
646
647 void QDeviceInfoPrivate::connectNotify(const QMetaMethod &signal)
648 {
649 if (timer == 0) {
650 timer = new QTimer(this);
651 timer->setInterval(2000);
652 connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
653 }
654
655 if (!timer->isActive())
656 timer->start();
657
658 static const QMetaMethod thermalStateChangedSignal = QMetaMethod::fromSignal(&QDeviceInfoPrivate::thermalStateChanged);
659 if (signal == thermalStateChangedSignal) {
660 watchThermalState = true;
661 currentThermalState = getThermalState();
662 }
663
664 static const QMetaMethod bluetoothStateChanged = QMetaMethod::fromSignal(&QDeviceInfoPrivate::bluetoothStateChanged);
665 if (signal == bluetoothStateChanged) {
666 #if !defined(QT_NO_DBUS)
667 connectBtPowered();
668 #endif
669 }
670 }
671
672 void QDeviceInfoPrivate::disconnectNotify(const QMetaMethod &signal)
673 {
674 static const QMetaMethod thermalStateChangedSignal = QMetaMethod::fromSignal(&QDeviceInfoPrivate::thermalStateChanged);
675 if (signal == thermalStateChangedSignal) {
676 watchThermalState = false;
677 currentThermalState = QDeviceInfo::UnknownThermal;
678 }
679
680 if (!watchThermalState)
681 timer->stop();
682 }
683
684 void QDeviceInfoPrivate::onTimeout()
685 {
686 if (watchThermalState) {
687 QDeviceInfo::ThermalState newState = getThermalState();
688 if (newState != currentThermalState) {
689 currentThermalState = newState;
690 emit thermalStateChanged(currentThermalState);
691 }
692 }
693 }
694
695 QDeviceInfo::ThermalState QDeviceInfoPrivate::getThermalState()
696 {
697 QDeviceInfo::ThermalState state = QDeviceInfo::UnknownThermal;
698
699 const QString hwmonRoot(QStringLiteral("/sys/class/hwmon/"));
700 const QStringList hwmonDirs(QDir(hwmonRoot).entryList(QStringList() << QStringLiteral("hwmon*")));
701 foreach (const QString &dir, hwmonDirs) {
702 int index = 1;
703 const QString input(hwmonRoot + dir + QDir::separator() + QStringLiteral("temp%1_input"));
704 const QString critical(hwmonRoot + dir + QDir::separator() + QStringLiteral("temp%1_crit"));
705 const QString emergency(hwmonRoot + dir + QDir::separator() + QStringLiteral("temp%1_emergency"));
706 while (true) {
707 QFile file(input.arg(index));
708 if (!file.open(QIODevice::ReadOnly))
709 break;
710 bool ok(false);
711 int currentTemp = file.readAll().simplified().toInt(&ok);
712 if (ok) {
713 if (state == QDeviceInfo::UnknownThermal)
714 state = QDeviceInfo::NormalThermal;
715
716 // Only check if we are below WarningThermal
717 if (state < QDeviceInfo::WarningThermal) {
718 file.close();
719 file.setFileName(critical.arg(index));
720 if (file.open(QIODevice::ReadOnly)) {
721 int criticalTemp = file.readAll().simplified().toInt(&ok);
722 if (ok && currentTemp > criticalTemp)
723 state = QDeviceInfo::WarningThermal;
724 }
725 }
726
727 // Only check if we are below AlertThermal
728 if (state < QDeviceInfo::AlertThermal) {
729 file.close();
730 file.setFileName(emergency.arg(index));
731 if (file.open(QIODevice::ReadOnly)) {
732 int emergencyTemp = file.readAll().simplified().toInt(&ok);
733 if (ok && currentTemp > emergencyTemp) {
734 state = QDeviceInfo::AlertThermal;
735 break; // No need for further checking, as we can't get the ErrorThermal state
736 }
737 }
738 }
739 } else {
740 break;
741 }
742
743 ++index;
744 }
745 }
746
747 return state;
748 }
749
750 QT_END_NAMESPACE