| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Copyright (C) 2018 BlackBerry Limited. All rights reserved. |
| 5 | ** Contact: http://www.qt-project.org/legal |
| 6 | ** |
| 7 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 8 | ** |
| 9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 10 | ** Commercial License Usage |
| 11 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 12 | ** accordance with the commercial license agreement provided with the |
| 13 | ** Software or, alternatively, in accordance with the terms contained in |
| 14 | ** a written agreement between you and The Qt Company. For licensing terms |
| 15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 16 | ** information use the contact form at https://www.qt.io/contact-us. |
| 17 | ** |
| 18 | ** GNU General Public License Usage |
| 19 | ** Alternatively, this file may be used under the terms of the GNU |
| 20 | ** General Public License version 3 as published by the Free Software |
| 21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 22 | ** included in the packaging of this file. Please review the following |
| 23 | ** information to ensure the GNU General Public License requirements will |
| 24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 25 | ** |
| 26 | ** $QT_END_LICENSE$ |
| 27 | ** |
| 28 | ****************************************************************************/ |
| 29 | |
| 30 | #include <QCoreApplication> |
| 31 | #include <QDebug> |
| 32 | #include <QStringList> |
| 33 | |
| 34 | |
| 35 | #include "qbatteryinfo.h" |
| 36 | #include "qdeviceinfo.h" |
| 37 | #include "qnetworkinfo.h" |
| 38 | #include "qinputinfo.h" |
| 39 | |
| 40 | |
| 41 | #define X(expr) qDebug() << #expr << "->" << (expr); |
| 42 | |
| 43 | struct symbol_t |
| 44 | { |
| 45 | const char *key; |
| 46 | int val; |
| 47 | }; |
| 48 | |
| 49 | int lookup(const symbol_t *stab, const char *key, int def) |
| 50 | { |
| 51 | for (;stab->key;++stab) { |
| 52 | if (!strcmp(stab->key,key)) return stab->val; |
| 53 | } |
| 54 | return def; |
| 55 | } |
| 56 | |
| 57 | const char *rlookup(const symbol_t *stab, int val, const char *def) |
| 58 | { |
| 59 | for (;stab->key; ++stab) { |
| 60 | if (stab->val == val) return stab->key; |
| 61 | } |
| 62 | return def; |
| 63 | } |
| 64 | |
| 65 | #define SYM(x) { #x, x } |
| 66 | |
| 67 | static const symbol_t Version_lut[] = |
| 68 | { |
| 69 | SYM(QDeviceInfo::Os), |
| 70 | SYM(QDeviceInfo::Firmware), |
| 71 | {0,0} |
| 72 | }; |
| 73 | |
| 74 | static const symbol_t Feature_lut[] = |
| 75 | { |
| 76 | SYM(QDeviceInfo::BluetoothFeature), |
| 77 | SYM(QDeviceInfo::CameraFeature), |
| 78 | SYM(QDeviceInfo::FmRadioFeature), |
| 79 | SYM(QDeviceInfo::FmTransmitterFeature), |
| 80 | SYM(QDeviceInfo::InfraredFeature), |
| 81 | SYM(QDeviceInfo::LedFeature), |
| 82 | SYM(QDeviceInfo::MemoryCardFeature), |
| 83 | SYM(QDeviceInfo::UsbFeature), |
| 84 | SYM(QDeviceInfo::VibrationFeature), |
| 85 | SYM(QDeviceInfo::WlanFeature), |
| 86 | SYM(QDeviceInfo::SimFeature), |
| 87 | SYM(QDeviceInfo::PositioningFeature), |
| 88 | SYM(QDeviceInfo::VideoOutFeature), |
| 89 | SYM(QDeviceInfo::HapticsFeature), |
| 90 | SYM(QDeviceInfo::NfcFeature), |
| 91 | {0,0} |
| 92 | }; |
| 93 | |
| 94 | static const symbol_t Thermal_lut[] = |
| 95 | { |
| 96 | SYM(QDeviceInfo::UnknownThermal), |
| 97 | SYM(QDeviceInfo::UnknownThermal), |
| 98 | SYM(QDeviceInfo::NormalThermal), |
| 99 | SYM(QDeviceInfo::WarningThermal), |
| 100 | SYM(QDeviceInfo::AlertThermal), |
| 101 | SYM(QDeviceInfo::ErrorThermal), |
| 102 | {0,0} |
| 103 | }; |
| 104 | static const symbol_t NetworkStatus_lut[] = |
| 105 | { |
| 106 | SYM(QNetworkInfo::UnknownStatus), |
| 107 | SYM(QNetworkInfo::NoNetworkAvailable), |
| 108 | SYM(QNetworkInfo::EmergencyOnly), |
| 109 | SYM(QNetworkInfo::Searching), |
| 110 | SYM(QNetworkInfo::Busy), |
| 111 | // SYM(QNetworkInfo::Connected), |
| 112 | SYM(QNetworkInfo::HomeNetwork), |
| 113 | SYM(QNetworkInfo::Denied), |
| 114 | SYM(QNetworkInfo::Roaming), |
| 115 | {0,0} |
| 116 | }; |
| 117 | |
| 118 | static const symbol_t NetworkMode_lut[] = |
| 119 | { |
| 120 | SYM(QNetworkInfo::UnknownMode), |
| 121 | SYM(QNetworkInfo::GsmMode), |
| 122 | SYM(QNetworkInfo::CdmaMode), |
| 123 | SYM(QNetworkInfo::WcdmaMode), |
| 124 | SYM(QNetworkInfo::WlanMode), |
| 125 | SYM(QNetworkInfo::EthernetMode), |
| 126 | SYM(QNetworkInfo::BluetoothMode), |
| 127 | SYM(QNetworkInfo::WimaxMode), |
| 128 | SYM(QNetworkInfo::LteMode), |
| 129 | SYM(QNetworkInfo::TdscdmaMode), |
| 130 | {0,0} |
| 131 | }; |
| 132 | |
| 133 | static const symbol_t Health_lut[] = |
| 134 | { |
| 135 | SYM(QBatteryInfo::HealthUnknown), |
| 136 | SYM(QBatteryInfo::HealthOk), |
| 137 | SYM(QBatteryInfo::HealthBad), |
| 138 | }; |
| 139 | |
| 140 | static const symbol_t LevelStatus_lut[] = |
| 141 | { |
| 142 | SYM(QBatteryInfo::LevelUnknown), |
| 143 | SYM(QBatteryInfo::LevelEmpty), |
| 144 | SYM(QBatteryInfo::LevelLow), |
| 145 | SYM(QBatteryInfo::LevelOk), |
| 146 | SYM(QBatteryInfo::LevelFull), |
| 147 | }; |
| 148 | |
| 149 | static const symbol_t ChargingState_lut[] = |
| 150 | { |
| 151 | SYM(QBatteryInfo::UnknownChargingState), |
| 152 | SYM(QBatteryInfo::Charging), |
| 153 | SYM(QBatteryInfo::IdleChargingState), |
| 154 | SYM(QBatteryInfo::Discharging) |
| 155 | }; |
| 156 | |
| 157 | static const symbol_t ChargerType_lut[] = |
| 158 | { |
| 159 | SYM(QBatteryInfo::UnknownCharger), |
| 160 | SYM(QBatteryInfo::WallCharger), |
| 161 | SYM(QBatteryInfo::USBCharger), |
| 162 | SYM(QBatteryInfo::VariableCurrentCharger), |
| 163 | }; |
| 164 | |
| 165 | |
| 166 | /* ------------------------------------------------------------------------- * |
| 167 | * test_systemdeviceinfo |
| 168 | * ------------------------------------------------------------------------- */ |
| 169 | |
| 170 | static void test_deviceinfo(void) |
| 171 | { |
| 172 | QDeviceInfo deviceinfo; |
| 173 | |
| 174 | X(deviceinfo.hasFeature(QDeviceInfo::BluetoothFeature)); |
| 175 | X(deviceinfo.hasFeature(QDeviceInfo::CameraFeature)); |
| 176 | X(deviceinfo.hasFeature(QDeviceInfo::FmRadioFeature)); |
| 177 | X(deviceinfo.hasFeature(QDeviceInfo::FmTransmitterFeature)); |
| 178 | X(deviceinfo.hasFeature(QDeviceInfo::InfraredFeature)); |
| 179 | X(deviceinfo.hasFeature(QDeviceInfo::LedFeature)); |
| 180 | X(deviceinfo.hasFeature(QDeviceInfo::MemoryCardFeature)); |
| 181 | X(deviceinfo.hasFeature(QDeviceInfo::UsbFeature)); |
| 182 | X(deviceinfo.hasFeature(QDeviceInfo::VibrationFeature)); |
| 183 | X(deviceinfo.hasFeature(QDeviceInfo::WlanFeature)); |
| 184 | X(deviceinfo.hasFeature(QDeviceInfo::SimFeature)); |
| 185 | X(deviceinfo.hasFeature(QDeviceInfo::PositioningFeature)); |
| 186 | X(deviceinfo.hasFeature(QDeviceInfo::VideoOutFeature)); |
| 187 | X(deviceinfo.hasFeature(QDeviceInfo::HapticsFeature)); |
| 188 | X(deviceinfo.hasFeature(QDeviceInfo::NfcFeature)); |
| 189 | |
| 190 | X(deviceinfo.imei(0)); |
| 191 | X(deviceinfo.manufacturer()); |
| 192 | X(deviceinfo.model()); |
| 193 | X(deviceinfo.productName()); |
| 194 | X(deviceinfo.uniqueDeviceID()); |
| 195 | |
| 196 | X(deviceinfo.version(QDeviceInfo::Os)); |
| 197 | X(deviceinfo.version(QDeviceInfo::Firmware)); |
| 198 | |
| 199 | X(deviceinfo.boardName()); |
| 200 | X(deviceinfo.operatingSystemName()); |
| 201 | |
| 202 | X(deviceinfo.thermalState()); |
| 203 | } |
| 204 | |
| 205 | /* ------------------------------------------------------------------------- * |
| 206 | * test_systemnetworkinfo |
| 207 | * ------------------------------------------------------------------------- */ |
| 208 | |
| 209 | static void test_networkinfo(void) |
| 210 | { |
| 211 | QNetworkInfo networkinfo; |
| 212 | |
| 213 | X(networkinfo.cellId(0)); |
| 214 | X(networkinfo.currentMobileCountryCode(0)); |
| 215 | X(networkinfo.currentMobileNetworkCode(0)); |
| 216 | X(networkinfo.homeMobileCountryCode(0)); |
| 217 | X(networkinfo.homeMobileNetworkCode(0)); |
| 218 | X(networkinfo.locationAreaCode(0)); |
| 219 | X(networkinfo.currentCellDataTechnology(0)); |
| 220 | |
| 221 | |
| 222 | for (const symbol_t *sym = NetworkMode_lut; sym->key; ++sym) { |
| 223 | QNetworkInfo::NetworkMode mode = |
| 224 | (QNetworkInfo::NetworkMode) sym->val; |
| 225 | |
| 226 | if (QCoreApplication::arguments().count() > 2) |
| 227 | if (!QString(sym->key).contains(QCoreApplication::arguments().at(2),Qt::CaseInsensitive)) |
| 228 | continue; |
| 229 | |
| 230 | qDebug() << ""; |
| 231 | qDebug() << "NetworkMode:" << sym->key; |
| 232 | |
| 233 | QNetworkInfo::NetworkMode netmode = networkinfo.currentNetworkMode(); |
| 234 | qDebug() << " networkinfo.currentNetworkMode() ->" << netmode; |
| 235 | |
| 236 | int intCount = networkinfo.networkInterfaceCount(mode); |
| 237 | qDebug() << " networkinfo.networkInterfaceCount() ->" << intCount; |
| 238 | |
| 239 | for (int j=0; j < networkinfo.networkInterfaceCount(mode);j++) { |
| 240 | QNetworkInterface iface = networkinfo.interfaceForMode(mode,j); |
| 241 | qDebug() << " networkinfo.interfaceForMode() ->" << iface; |
| 242 | |
| 243 | QString macaddr = networkinfo.macAddress(mode,j); |
| 244 | qDebug() << " networkinfo.macAddress() ->" << macaddr; |
| 245 | |
| 246 | QNetworkInfo::NetworkStatus status = networkinfo.networkStatus(mode,j); |
| 247 | qDebug() << " networkinfo.networkStatus() ->" << status; |
| 248 | |
| 249 | QString network = networkinfo.networkName(mode,j); |
| 250 | qDebug() << " networkinfo.networkName() ->" << network; |
| 251 | |
| 252 | int sigstr = networkinfo.networkSignalStrength(mode,j); |
| 253 | qDebug() << " networkinfo.networkSignalStrength() ->" << sigstr; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* ------------------------------------------------------------------------- * |
| 259 | * test_systemscreensaver |
| 260 | * ------------------------------------------------------------------------- */ |
| 261 | //static void test_systemscreensaver(void) |
| 262 | //{ |
| 263 | // QSystemScreenSaver screensaver; |
| 264 | |
| 265 | // X(screensaver.screenSaverInhibited()); |
| 266 | // X(screensaver.setScreenSaverInhibit()); |
| 267 | //}*/ |
| 268 | |
| 269 | /* ------------------------------------------------------------------------- * |
| 270 | * test_systembatteryinfo |
| 271 | * ------------------------------------------------------------------------- */ |
| 272 | |
| 273 | static void test_batteryinfo(void) |
| 274 | { |
| 275 | QBatteryInfo batInfo; |
| 276 | X(batInfo.batteryIndex()); |
| 277 | X(batInfo.chargerType()); |
| 278 | X(batInfo.chargingState() ); |
| 279 | X(batInfo.maximumCapacity()); |
| 280 | X(batInfo.remainingCapacity()); |
| 281 | X(batInfo.voltage()); |
| 282 | X(batInfo.remainingChargingTime()); |
| 283 | X(batInfo.currentFlow()); |
| 284 | X(batInfo.levelStatus()); |
| 285 | X(batInfo.batteryCount()); |
| 286 | X(batInfo.health()); |
| 287 | } |
| 288 | |
| 289 | /* ------------------------------------------------------------------------- * |
| 290 | * test_systeminputdeviceinfo |
| 291 | * ------------------------------------------------------------------------- */ |
| 292 | |
| 293 | static void test_inputinfo(void) |
| 294 | { |
| 295 | QInputInfoManager manager; |
| 296 | QMap <QString, QInputDevice *> map = manager.deviceMap(); |
| 297 | X(map.count()); |
| 298 | QMapIterator<QString, QInputDevice *> i(map); |
| 299 | while (i.hasNext()) { |
| 300 | i.next(); |
| 301 | X(i.value()->name()); |
| 302 | X(i.value()->types()); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | struct dummy_t |
| 307 | { |
| 308 | const char *name; |
| 309 | void (*func)(void); |
| 310 | } lut[] = { |
| 311 | #define ADD(x) {#x, test_##x } |
| 312 | // ADD(systeminfo), |
| 313 | ADD(deviceinfo), |
| 314 | ADD(networkinfo), |
| 315 | // ADD(screensaver), |
| 316 | ADD(batteryinfo), |
| 317 | ADD(inputinfo), |
| 318 | #undef ADD |
| 319 | {0,0} |
| 320 | }; |
| 321 | |
| 322 | static bool endswith(const char *str, const char *pat) |
| 323 | { |
| 324 | int slen = strlen(str); |
| 325 | int plen = strlen(pat); |
| 326 | return (slen >= plen) && !strcmp(str+slen-plen, pat); |
| 327 | } |
| 328 | |
| 329 | int lookup_test(const char *name) |
| 330 | { |
| 331 | for (int i = 0; lut[i].name; ++i) { |
| 332 | if (!strcmp(lut[i].name, name)) return i; |
| 333 | } |
| 334 | for (int i = 0; lut[i].name; ++i) { |
| 335 | if (endswith(lut[i].name, name)) return i; |
| 336 | } |
| 337 | for (int i = 0; lut[i].name; ++i) { |
| 338 | if (strstr(lut[i].name, name)) return i; |
| 339 | } |
| 340 | return -1; |
| 341 | } |
| 342 | |
| 343 | int main(int ac, char **av) |
| 344 | { |
| 345 | #if !defined(Q_OS_WIN) |
| 346 | if (!getenv("DISPLAY")) { |
| 347 | qDebug() << "$DISPLAY not set, assuming :0"; |
| 348 | setenv("DISPLAY", ":0", 1); |
| 349 | } |
| 350 | if (!getenv("DBUS_SESSION_BUS_ADDRESS")) { |
| 351 | qDebug() << "session bus not configured"; |
| 352 | } |
| 353 | #endif |
| 354 | QCoreApplication app(ac, av, true); |
| 355 | |
| 356 | if (ac < 2) { |
| 357 | qDebug() << "available tests:"; |
| 358 | for (int k = 0; lut[k].name; ++k) { |
| 359 | qDebug() << *av << lut[k].name; |
| 360 | } |
| 361 | exit(0); |
| 362 | } |
| 363 | |
| 364 | for (int i = 1; i < ac; ++i) { |
| 365 | const char *name = av[i]; |
| 366 | |
| 367 | int k = lookup_test(name); |
| 368 | |
| 369 | if (k != -1) { |
| 370 | qDebug() << ""; |
| 371 | qDebug() << "----(" << lut[k].name << ")----"; |
| 372 | qDebug() << ""; |
| 373 | lut[k].func(); |
| 374 | } else if ( !strcmp(name, "all")) { |
| 375 | for (int k = 0; lut[k].name; ++k) { |
| 376 | qDebug() << ""; |
| 377 | qDebug() << "----(" << lut[k].name << ")----"; |
| 378 | qDebug() << ""; |
| 379 | lut[k].func(); |
| 380 | } |
| 381 | } else { |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // EOF |