Fix battery related compilation errors on simulator
[qt:qtsystems.git] / src / systeminfo / simulator / qsysteminfoconnection_simulator.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc 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:LGPL$
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 Digia.  For licensing terms and
15 ** conditions see http://qt.digia.com/licensing.  For further information
16 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL included in the
22 ** packaging of this file.  Please review the following information to
23 ** ensure the GNU Lesser General Public License version 2.1 requirements
24 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** In addition, as a special exception, Digia gives you certain additional
27 ** rights.  These rights are described in the Digia Qt LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42
43 #include "qsysteminfoconnection_simulator_p.h"
44 #include "qsysteminfobackend_simulator_p.h"
45
46 #include <QtSimulator/connection.h>
47 #include <QtSimulator/version.h>
48 #include <QtSimulator/connectionworker.h>
49 #include <QtSimulator/QtSimulator>
50
51 #include <QTimer>
52 #include <QEventLoop>
53 #include <QMutex>
54
55 QT_BEGIN_NAMESPACE
56
57 using namespace Simulator;
58
59 const QString SystemInfoConnection::SERVERNAME(QStringLiteral("QtSimulator_Mobility_ServerName1.3.0.0"));
60 const int SystemInfoConnection::PORT(0xbeef+1);
61 const Simulator::Version SystemInfoConnection::VERSION(1,3,0,0);
62
63 SystemInfoConnection::SystemInfoConnection(QObject *parent)
64     : QObject(parent)
65     , mInitialDataSent(false)
66 {
67     qt_registerSystemInfoTypes();
68     mConnection = new Connection(Connection::Client, SERVERNAME, PORT, VERSION, this);
69     mWorker = mConnection->connectToServer(Connection::simulatorHostName(true), PORT);
70     if (mWorker) {
71         mWorker->addReceiver(this);
72         mWorker->call("setRequestsSystemInfo");
73
74         // wait until initial data is received
75         QTimer timer;
76         QEventLoop loop;
77         connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
78         connect(this, SIGNAL(initialDataReceived()), &loop, SLOT(quit()));
79         timer.start(1000);
80         loop.exec();
81         timer.stop();
82     } else
83         qWarning("Could not connect to server");
84 }
85
86 SystemInfoConnection::~SystemInfoConnection()
87 {
88     delete mWorker;
89 }
90
91 void SystemInfoConnection::ensureSimulatorConnection()
92 {
93     static QMutex mutex;
94
95     mutex.lock();
96     static SystemInfoConnection systemInfoConnection;
97     mutex.unlock();
98 }
99
100 void SystemInfoConnection::initialSystemInfoDataSent()
101 {
102     mInitialDataSent = true;
103     emit initialDataReceived();
104 }
105
106 void SystemInfoConnection::setBatteryInfoData(const QBatteryInfoData &data)
107 {
108     QBatteryInfoSimulatorBackend *batteryInfoBackend = QBatteryInfoSimulatorBackend::getSimulatorBackend();
109
110     batteryInfoBackend->setMaximumCapacity(data.maximumCapacity);
111     batteryInfoBackend->setRemainingCapacity(data.remainingCapacity);
112     batteryInfoBackend->setCurrentFlow(data.currentFlow);
113     batteryInfoBackend->setCycleCount(data.cycleCount);
114     batteryInfoBackend->setVoltage(data.voltage);
115     batteryInfoBackend->setRemainingChargingTime(data.remainingChargingTime);
116     batteryInfoBackend->setChargerType(data.chargerType);
117     batteryInfoBackend->setChargingState(data.chargingState);
118     batteryInfoBackend->setLevelStatus(data.levelStatus);
119     batteryInfoBackend->setHealth(data.health);
120     batteryInfoBackend->setTemperature(data.temperature);
121 }
122
123 void SystemInfoConnection::setDeviceInfoData(const QDeviceInfoData &data)
124 {
125     QDeviceInfoSimulatorBackend *deviceInfoBackend = QDeviceInfoSimulatorBackend::getSimulatorBackend();
126
127     deviceInfoBackend->setManufacturer(data.manufacturer);
128     deviceInfoBackend->setModel(data.model);
129     deviceInfoBackend->setProductName(data.productName);
130     deviceInfoBackend->setActivatedLocks(data.activatedLocks);
131     deviceInfoBackend->setEnabledLocks(data.enabledLocks);
132     deviceInfoBackend->setUniqueDeviceID(data.uniqueDeviceID);
133     deviceInfoBackend->setThermalState(data.currentThermalState);
134     deviceInfoBackend->setVersion(QDeviceInfo::Os, data.versionList.value(QDeviceInfo::Os));
135     deviceInfoBackend->setVersion(QDeviceInfo::Firmware, data.versionList.value(QDeviceInfo::Firmware));
136
137     int imeiCount = deviceInfoBackend->getImeiCount();
138     int receivedImeiCount = data.imeiList.count();
139     if (imeiCount > receivedImeiCount) {
140         int imeiIndex;
141         for (imeiIndex = 0; imeiIndex < imeiCount; imeiIndex++)
142             deviceInfoBackend->setImei(imeiIndex, QStringLiteral(""));
143         for (imeiIndex = 0; imeiIndex < receivedImeiCount; imeiIndex++)
144             deviceInfoBackend->setImei(imeiIndex, data.imeiList.value(imeiIndex));
145     } else {
146         for (int imeiIndex = 0; imeiIndex < imeiCount; imeiIndex++)
147             deviceInfoBackend->setImei(imeiIndex, data.imeiList.value(imeiIndex));
148     }
149
150     QList<QDeviceInfo::Feature> featureKeys = data.featureList.keys();
151     foreach (const QDeviceInfo::Feature &featureKey, featureKeys)
152         deviceInfoBackend->setFeature(featureKey, data.featureList.value(featureKey));
153 }
154
155 void SystemInfoConnection::setNetworkInfoData(const QNetworkInfoData &data)
156 {
157     QNetworkInfoSimulatorBackend *networkInfoBackend = QNetworkInfoSimulatorBackend::getSimulatorBackend();
158
159     int actualCount = networkInfoBackend->getNetworkInterfaceCount(QNetworkInfo::WlanMode);
160     int newCount = data.wLanInfo.count();
161
162     if (newCount < actualCount) {
163         for (int i = actualCount; i > newCount; i--)
164             networkInfoBackend->removeInterface(QNetworkInfo::WlanMode, i - 1);
165     } else if (newCount > actualCount) {
166         for (int i = actualCount; i < newCount; i++) {
167             QNetworkInfoData::WLanInfo info = {{QString(),
168                                                -1,
169                                                QNetworkInfo::WlanMode,
170                                                QNetworkInfo::UnknownStatus},
171                                                QString()};
172             networkInfoBackend->addWlanInterface(info);
173         }
174     }
175
176 #if defined(QT_NO_OFONO)
177     actualCount = networkInfoBackend->getNetworkInterfaceCount(QNetworkInfo::EthernetMode);
178     newCount = data.ethernetInfo.count();
179     if (newCount < actualCount) {
180         for (int i = actualCount; i > newCount; i--)
181             networkInfoBackend->removeInterface(QNetworkInfo::EthernetMode, i - 1);
182     } else if (newCount > actualCount) {
183         for (int i = actualCount; i < newCount; i++) {
184             QNetworkInfoData::EthernetInfo info = {{QString(),
185                                                    -1,
186                                                    QNetworkInfo::EthernetMode,
187                                                    QNetworkInfo::UnknownStatus},
188                                                    QString()};
189             networkInfoBackend->addEthernetInterface(info);
190         }
191     }
192
193     actualCount = networkInfoBackend->getNetworkInterfaceCount(QNetworkInfo::BluetoothMode);
194     newCount = data.bluetoothInfo.count();
195     if (newCount < actualCount) {
196         for (int i = actualCount; i > newCount; i--)
197             networkInfoBackend->removeInterface(QNetworkInfo::BluetoothMode, i - 1);
198     } else if (newCount > actualCount) {
199         for (int i = actualCount; i < newCount; i++) {
200             QNetworkInfoData::BluetoothInfo info = {{QString(),
201                                                     -1,
202                                                     QNetworkInfo::BluetoothMode,
203                                                     QNetworkInfo::UnknownStatus},
204                                                     QString()};
205             networkInfoBackend->addBluetoothInterface(info);
206         }
207     }
208
209     actualCount = networkInfoBackend->getNetworkInterfaceCount(QNetworkInfo::GsmMode);
210     newCount = data.cellularInfo.count();
211     if (newCount < actualCount) {
212         for (int i = actualCount; i > newCount; i--)
213             networkInfoBackend->removeInterface(QNetworkInfo::GsmMode, i - 1);
214     } else if (newCount > actualCount) {
215         for (int i = actualCount; i < newCount; i++) {
216             QNetworkInfoData::CellularInfo info = {{QString(),
217                                                    -1,
218                                                    QNetworkInfo::GsmMode,
219                                                    QNetworkInfo::UnknownStatus},
220                                                    QString(),
221                                                    QString(),
222                                                    QString(),
223                                                    QString(),
224                                                    QString(),
225                                                    QString(),
226                                                    QString(),
227                                                    QNetworkInfo::UnknownDataTechnology};
228             networkInfoBackend->addCellularInterface(info);
229         }
230     }
231 #endif
232
233     for (int i = 0; i < data.wLanInfo.count(); i++) {
234         networkInfoBackend->setNetworkName(QNetworkInfo::WlanMode, i, data.wLanInfo[i].basicNetworkInfo.name);
235         networkInfoBackend->setNetworkSignalStrength(QNetworkInfo::WlanMode, i, data.wLanInfo[i].basicNetworkInfo.signalStrength);
236         networkInfoBackend->setNetworkStatus(QNetworkInfo::WlanMode, i, data.wLanInfo[i].basicNetworkInfo.status);
237         networkInfoBackend->setNetworkMacAddress(QNetworkInfo::WlanMode, i, data.wLanInfo[i].macAddress);
238     }
239
240 #if defined(QT_NO_OFONO)
241     for (int i = 0; i < data.ethernetInfo.count(); i++) {
242         networkInfoBackend->setNetworkName(QNetworkInfo::EthernetMode, i, data.ethernetInfo[i].basicNetworkInfo.name);
243         networkInfoBackend->setNetworkSignalStrength(QNetworkInfo::EthernetMode, i, data.ethernetInfo[i].basicNetworkInfo.signalStrength);
244         networkInfoBackend->setNetworkStatus(QNetworkInfo::EthernetMode, i, data.ethernetInfo[i].basicNetworkInfo.status);
245         networkInfoBackend->setNetworkMacAddress(QNetworkInfo::EthernetMode, i, data.ethernetInfo[i].macAddress);
246     }
247
248     for (int i = 0; i < data.bluetoothInfo.count(); i++) {
249         networkInfoBackend->setNetworkName(QNetworkInfo::BluetoothMode, i, data.bluetoothInfo[i].basicNetworkInfo.name);
250         networkInfoBackend->setNetworkSignalStrength(QNetworkInfo::BluetoothMode, i, data.bluetoothInfo[i].basicNetworkInfo.signalStrength);
251         networkInfoBackend->setNetworkStatus(QNetworkInfo::BluetoothMode, i, data.bluetoothInfo[i].basicNetworkInfo.status);
252         networkInfoBackend->setNetworkMacAddress(QNetworkInfo::BluetoothMode, i, data.bluetoothInfo[i].btAddress);
253     }
254
255     for (int i = 0; i < data.cellularInfo.count(); i++) {
256         networkInfoBackend->setNetworkName(data.cellularInfo[i].basicNetworkInfo.mode, i, data.cellularInfo[i].basicNetworkInfo.name);
257         networkInfoBackend->setNetworkSignalStrength(data.cellularInfo[i].basicNetworkInfo.mode, i, data.cellularInfo[i].basicNetworkInfo.signalStrength);
258         networkInfoBackend->setMode(i, data.cellularInfo[i].basicNetworkInfo.mode);
259         networkInfoBackend->setNetworkStatus(data.cellularInfo[i].basicNetworkInfo.mode, i, data.cellularInfo[i].basicNetworkInfo.status);
260         networkInfoBackend->setImsi(i, data.cellularInfo[i].imsi);
261         networkInfoBackend->setCellId(i, data.cellularInfo[i].cellId);
262         networkInfoBackend->setLocationAreaCode(i, data.cellularInfo[i].locationAreaCode);
263         networkInfoBackend->setCurrentMobileCountryCode(i, data.cellularInfo[i].currentMobileCountryCode);
264         networkInfoBackend->setCurrentMobileNetworkCode(i, data.cellularInfo[i].currentMobileNetworkCode);
265         networkInfoBackend->setHomeMobileCountryCode(i, data.cellularInfo[i].homeMobileCountryCode);
266         networkInfoBackend->setHomeMobileNetworkCode(i, data.cellularInfo[i].homeMobileNetworkCode);
267         networkInfoBackend->setCellDataTechnology(i, data.cellularInfo[i].cellData);
268     }
269 #endif
270 }
271
272 QT_END_NAMESPACE