Commit 2e3bfabc authored by Hannah von Reth's avatar Hannah von Reth

Added missing methods in winbattery and some fixes

REVIEW: 118935
parent ca96df82
......@@ -36,11 +36,6 @@ WinBattery::WinBattery(WinDevice *device) :
connect(SolidWinEventFilter::instance(), SIGNAL(powerChanged()), this, SLOT(powerChanged()));
}
bool WinBattery::isPlugged() const
{
return m_pluggedIn;
}
Solid::Battery::BatteryType WinBattery::type() const
{
return m_type;
......@@ -75,10 +70,10 @@ QSet<QString> WinBattery::getUdis()
{
QSet<QString> udis;
HDEVINFO hdev =
SetupDiGetClassDevs(&GUID_DEVCLASS_BATTERY,
0,
0,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
SetupDiGetClassDevs(&GUID_DEVCLASS_BATTERY,
0,
0,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (INVALID_HANDLE_VALUE != hdev) {
// Limit search to 100 batteries max
......@@ -139,10 +134,10 @@ void WinBattery::powerChanged()
const int old_charge = m_charge;
const int old_capacity = m_capacity;
const Solid::Battery::ChargeState old_state = m_state;
const bool old_pluggedIn = m_pluggedIn;
const bool old_isPowerSupply = m_isPowerSupply;
const double old_energy = m_energy;
const double old_energyRate = m_energyRate;
const double old_voltage = m_voltage;
BATTERY_WAIT_STATUS batteryStatusQuery;
ZeroMemory(&batteryStatusQuery, sizeof(batteryStatusQuery));
......@@ -156,9 +151,11 @@ void WinBattery::powerChanged()
batteryInformationQuery.InformationLevel = BatteryInformation;
BATTERY_INFORMATION info = WinDeviceManager::getDeviceInfo<BATTERY_INFORMATION, BATTERY_QUERY_INFORMATION>(b.first, IOCTL_BATTERY_QUERY_INFORMATION, &batteryInformationQuery);
m_pluggedIn = status.PowerState & BATTERY_POWER_ON_LINE;
initSerial(b);
updateBatteryTemp(b);
updateTimeToEmpty(b);
m_isPowerSupply = true;//TODO: is there a wy to implement this
m_isPowerSupply = !(status.PowerState & BATTERY_POWER_ON_LINE);
QString tech = QString::fromUtf8((const char *)info.Chemistry, 4);
......@@ -171,13 +168,12 @@ void WinBattery::powerChanged()
} else if (tech == "NIMH") {
m_technology = Solid::Battery::NickelMetalHydride;
} else {
qDebug() << tech << tr("Unknown", "battery technology");
m_technology = Solid::Battery::UnknownTechnology;
}
m_energy = status.Capacity;
m_energyRate = status.Rate;
m_voltage = status.Voltage;
m_energy = status.Capacity / 1000.0;//provided in mWh
m_energyRate = status.Rate / 1000.0;//provided in mW
m_voltage = status.Voltage / 1000.0;//provided in mV
if (info.FullChargedCapacity != 0) {
m_charge = m_energy / info.FullChargedCapacity * 100.0;
......@@ -219,10 +215,6 @@ void WinBattery::powerChanged()
emit chargeStateChanged(m_state, m_device->udi());
}
if (old_pluggedIn != m_pluggedIn) {
emit plugStateChanged(m_pluggedIn, m_device->udi());
}
if (old_isPowerSupply != m_isPowerSupply) {
emit powerSupplyStateChanged(m_isPowerSupply, m_device->udi());
}
......@@ -234,6 +226,60 @@ void WinBattery::powerChanged()
if (old_energyRate != m_energyRate) {
emit energyRateChanged(m_energyRate, m_device->udi());
}
if(old_voltage != m_voltage)
{
emit voltageChanged(m_voltage, m_device->udi());
}
}
void WinBattery::initSerial(const Battery &b)
{
wchar_t buffer[1024];
BATTERY_QUERY_INFORMATION batteryInformationQuery;
ZeroMemory(&batteryInformationQuery, sizeof(batteryInformationQuery));
batteryInformationQuery.BatteryTag = b.second;
batteryInformationQuery.InformationLevel = BatterySerialNumber;
WinDeviceManager::getDeviceInfo<wchar_t, BATTERY_QUERY_INFORMATION>(b.first, IOCTL_BATTERY_QUERY_INFORMATION, buffer, 1024, &batteryInformationQuery);
m_serial = QString::fromWCharArray(buffer);
}
void WinBattery::updateTimeToEmpty(const WinBattery::Battery &b)
{
BATTERY_QUERY_INFORMATION batteryInformationQuery;
ZeroMemory(&batteryInformationQuery, sizeof(batteryInformationQuery));
batteryInformationQuery.BatteryTag = b.second;
batteryInformationQuery.InformationLevel = BatteryEstimatedTime;
ulong time = WinDeviceManager::getDeviceInfo<ulong, BATTERY_QUERY_INFORMATION>(b.first, IOCTL_BATTERY_QUERY_INFORMATION, &batteryInformationQuery);
if(time == BATTERY_UNKNOWN_TIME)
{
time = 0;
}
if(time != m_timeUntilEmpty)
{
m_timeUntilEmpty = time;
emit timeToEmptyChanged(time, m_device->udi());
}
}
void WinBattery::updateBatteryTemp(const WinBattery::Battery &b)
{
BATTERY_QUERY_INFORMATION batteryInformationQuery;
ZeroMemory(&batteryInformationQuery, sizeof(batteryInformationQuery));
batteryInformationQuery.BatteryTag = b.second;
batteryInformationQuery.InformationLevel = BatteryTemperature;
ulong batteryTemp = WinDeviceManager::getDeviceInfo<ulong, BATTERY_QUERY_INFORMATION>(b.first, IOCTL_BATTERY_QUERY_INFORMATION, &batteryInformationQuery);
if(batteryTemp != m_temperature)
{
m_temperature = batteryTemp;
emit temperatureChanged(batteryTemp, m_device->udi());
}
}
Solid::Battery::Technology WinBattery::technology() const
......@@ -255,3 +301,43 @@ double WinBattery::voltage() const
{
return m_voltage;
}
bool WinBattery::isPresent() const
{
return true;
}
qlonglong WinBattery::timeToEmpty() const
{
return m_timeUntilEmpty;
}
qlonglong WinBattery::timeToFull() const
{
return 0;
}
double WinBattery::temperature() const
{
return m_temperature;
}
bool WinBattery::isRecalled() const
{
return false;
}
QString WinBattery::recallVendor() const
{
return QString();
}
QString WinBattery::recallUrl() const
{
return QString();
}
QString WinBattery::serial() const
{
return m_serial;
}
......@@ -41,8 +41,6 @@ public:
WinBattery(WinDevice *device);
virtual bool isPlugged() const;
virtual Solid::Battery::BatteryType type() const;
virtual int chargePercent() const;
......@@ -55,10 +53,6 @@ public:
virtual Solid::Battery::ChargeState chargeState() const;
static QSet<QString> getUdis();
static const Battery batteryInfoFromUdi(const QString &udi);
Solid::Battery::Technology technology() const;
double energy() const;
......@@ -67,22 +61,57 @@ public:
double voltage() const;
virtual qlonglong timeToEmpty() const;
virtual qlonglong timeToFull() const;
virtual double temperature() const;
virtual QString serial() const;
// not yet implemented
// ------------
virtual bool isPresent() const;
virtual bool isRecalled() const;
virtual QString recallVendor() const;
virtual QString recallUrl() const;
// ------------
static QSet<QString> getUdis();
static const Battery batteryInfoFromUdi(const QString &udi);
Q_SIGNALS:
void chargePercentChanged(int value, const QString &udi);
void capacityChanged(int value, const QString &udi);
void chargeStateChanged(int newState, const QString &udi);
void plugStateChanged(bool newState, const QString &udi);
void powerSupplyStateChanged(bool newState, const QString &udi);
void energyChanged(double energy, const QString &udi);
void energyRateChanged(double energyRate, const QString &udi);
void timeToEmptyChanged(qlonglong time, const QString &udi);
void temperatureChanged(double temperature, const QString &udi);
void voltageChanged(double voltage, const QString &udi);
// not yet implemented
// ------------
void presentStateChanged(bool newState, const QString &udi);
void timeToFullChanged(qlonglong time, const QString &udi);
// ------------
private Q_SLOTS:
void powerChanged();
private:
void initSerial(const Battery &b);
void updateTimeToEmpty(const Battery &b);
void updateBatteryTemp(const Battery &b);
static QMap<QString, Battery> m_udiToGDI;
bool m_pluggedIn;
Solid::Battery::BatteryType m_type;
int m_charge;
int m_capacity;
......@@ -94,6 +123,11 @@ private:
double m_energyRate;
double m_voltage;
QString m_serial;
ulong m_temperature;
ulong m_timeUntilEmpty;
};
}
}
......
......@@ -156,19 +156,18 @@ private:
}
err = GetLastError();
if (err == ERROR_NOT_READY) {
switch(err)
{
case ERROR_NOT_READY:
//the drive is a cd drive with no disk
::CloseHandle(handle);
return;
break;
case ERROR_INVALID_FUNCTION:
//in most cases this means that the device doesn't support this method, like temperature for some batteries
break;
default:
qWarning() << "Failed to query" << dev << "reason:" << qGetLastError(err);
//DebugBreak();
}
#if 0
::CloseHandle(handle);
qFatal("Failed to query %s reason: %s", qPrintable(dev), qPrintable(qGetLastError(err)));
#else
qWarning() << "Failed to query" << dev << "reason:" << qGetLastError(err);
#endif
::CloseHandle(handle);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment