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