-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.cpp
More file actions
339 lines (286 loc) · 12.6 KB
/
Copy pathMain.cpp
File metadata and controls
339 lines (286 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "Battery.hpp"
#include <batclass.h>
#include <initguid.h> // must be above Devpkey.h include
#include <Devpkey.h> // for DEVPKEY_xxx
#include <wrl/wrappers/corewrappers.h> // for FileHandle
#include <cassert>
#include <ShlObj.h>
#include "DeviceInstance.hpp"
#include "HidPowerDevice.hpp"
#include "DellBattery.hpp"
#include "../DevicePowerQuery/DeviceEnum.hpp"
/** RAII class for COM initialization. */
class ComInitialize {
public:
ComInitialize(COINIT apartment /*= COINIT_MULTITHREADED*/) : m_initialized(false) {
// REF: https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex
HRESULT hr = CoInitializeEx(NULL, apartment);
if (SUCCEEDED(hr))
m_initialized = true;
// enable admin calls if running as admin
CHECK(CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
));
}
~ComInitialize() {
if (m_initialized)
CoUninitialize();
}
private:
bool m_initialized; ///< must uninitialize in dtor
};
struct BatteryParameters {
BatteryParameters(HANDLE dev, bool verbose) {
DeviceName = GetBatteryInfoStr(dev, BatteryDeviceName);
GetBatteryInfoUlong(dev, BatteryEstimatedTime, EstimatedTime, verbose);
GranularityInformation_len = GetBatteryInfoGranularity(dev, GranularityInformation);
GetBatteryInfoDate(dev, ManufactureDate);
ManufactureName = GetBatteryInfoStr(dev, BatteryManufactureName);
SerialNumber = GetBatteryInfoStr(dev, BatterySerialNumber);
GetBatteryInfoUlong(dev, BatteryTemperature, Temperature, verbose);
UniqueID = GetBatteryInfoStr(dev, BatteryUniqueID);
}
void Print() const {
wprintf(L" BatteryDeviceName: %s\n", DeviceName.c_str());
if (EstimatedTime != BATTERY_UNKNOWN_TIME)
wprintf(L" BatteryEstimatedTime: %u s\n", EstimatedTime);
else
wprintf(L" BatteryEstimatedTime: <unknown>\n");
for (unsigned int idx = 0; idx < GranularityInformation_len; ++idx)
wprintf(L" BatteryGranularityInformation %u: Resolution=%u mWh for Capacity<=%u mWh\n", idx, GranularityInformation[idx].Granularity, GranularityInformation[idx].Capacity);
if (GranularityInformation_len == 0)
wprintf(L" BatteryGranularityInformation: <unknown>\n");
if (ManufactureDate.Year)
wprintf(L" BatteryManufactureDate: %u-%u-%u\n", ManufactureDate.Year, ManufactureDate.Month, ManufactureDate.Day);
else
wprintf(L" BatteryManufactureDate: <unknown>\n");
wprintf(L" BatteryManufactureName: %s\n", ManufactureName.c_str());
wprintf(L" BatterySerialNumber: %s\n", SerialNumber.c_str());
if (Temperature) {
float tempCelsius = ((int)Temperature - 2731)*0.1f; // convert to Celsius
wprintf(L" BatteryTemperature: %.1f Celsius\n", tempCelsius);
} else {
wprintf(L" BatteryTemperature: <unknown>\n");
}
wprintf(L" BatteryUniqueID: %s\n", UniqueID.c_str());
}
std::wstring DeviceName;
ULONG EstimatedTime = BATTERY_UNKNOWN_TIME;
BATTERY_REPORTING_SCALE GranularityInformation[4] = {};
UINT GranularityInformation_len = 0;
BATTERY_MANUFACTURE_DATE ManufactureDate = {};
std::wstring ManufactureName;
std::wstring SerialNumber;
ULONG Temperature = 0; // in 10ths of a degree Kelvin
std::wstring UniqueID;
};
int AccessBattery(const std::wstring& devInstPath, bool verbose, unsigned int newCharge = -1) {
wprintf(L"\n");
wprintf(L"Opening %s:\n", devInstPath.c_str());
DeviceInstance dev(devInstPath.c_str());
Microsoft::WRL::Wrappers::FileHandle battery(CreateFileW(dev.GetPDOPath().c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL));
if (!battery.IsValid()) {
DWORD err = GetLastError();
wprintf(L"ERROR: CreateFileW (err %u).\n", err);
return -1;
}
DellBattery dellBatt(devInstPath);
hid::HidPowerDevice hidpd(dev.GetPDOPath().c_str(), true);
wprintf(L"\n");
{
BatteryParameters params(battery.Get(), verbose);
dellBatt.Initialize(params.DeviceName);
wprintf(L"Misc. IOCTL_BATTERY_QUERY_INFORMATION parameters:\n");
if (!params.Temperature && hidpd.IsValid()) {
// fallback for HidBatt driver limitation
ULONG hidTemp = hidpd.GetTemperature();
if (hidTemp) {
if (verbose)
wprintf(L"WARNING: Retrieving Temperature directly from the HID device since it's not parsed by the HidBatt driver.\n");
params.Temperature = hidTemp;
}
}
if (!params.Temperature && dellBatt.IsValid()) {
// fallback to Dell WMI interface
ULONG dellTemp = dellBatt.GetTemperature();
if (dellTemp) {
if (verbose)
wprintf(L"WARNING: Retrieving Temperature directly from Dell WMI since it's not parsed by the CmBatt driver.\n");
params.Temperature = dellTemp;
}
}
if (!params.ManufactureDate.Year && dellBatt.IsValid()) {
// fallback to Dell WMI interface
BATTERY_MANUFACTURE_DATE date = dellBatt.GetManufactureDat();
if (date.Year) {
if (verbose)
wprintf(L"WARNING: Retrieving ManufactureDate directly from Dell WMI since it's not parsed by the CmBatt driver.\n");
params.ManufactureDate = date;
}
}
params.Print();
}
wprintf(L"\n");
BatteryInformationWrap info(battery.Get());
wprintf(L"BATTERY_INFORMATION parameters:\n");
if (!info.CycleCount && hidpd.IsValid()) {
// fallback for HidBatt driver limitation
auto hidCycleCount = hidpd.GetCycleCount();
if (hidCycleCount) {
if (verbose)
wprintf(L"WARNING: Retrieving CycleCount directly from the HID device since it's not parsed by the HidBatt driver.\n");
info.CycleCount = hidCycleCount;
}
}
if (!info.CycleCount && dellBatt.IsValid()) {
auto cycleCount = dellBatt.GetCycleCount();
if (cycleCount) {
if (verbose)
wprintf(L"WARNING: Retrieving CycleCount directly from Dell WMI since it's not parsed by the CmBatt driver.\n");
info.CycleCount = cycleCount;
}
}
info.Print();
wprintf(L"\n");
BatteryStausWrap status(battery.Get());
wprintf(L"BATTERY_STATUS parameters:\n");
status.Print();
wprintf(L"\n");
#if 0
// update battery information
info.SimbattSet(battery.Get());
#endif
// update battery charge level
if (newCharge != static_cast<unsigned int>(-1)) {
// toggle between charge and dischage
if (newCharge > status.Capacity)
status.PowerState = BATTERY_POWER_ON_LINE | BATTERY_CHARGING; // charging while on AC power
else if (newCharge < status.Capacity)
status.PowerState = BATTERY_DISCHARGING; // discharging
else
status.PowerState = 0; // same charge as before
// update charge level
status.Capacity = newCharge;
status.Rate = BATTERY_UNKNOWN_RATE; // was 0
status.Voltage = BATTERY_UNKNOWN_VOLTAGE; // was -1
status.Set(battery.Get());
wprintf(L"BATTERY_STATUS parameters (after update):\n");
status.Print();
}
return 0;
}
void PrintReport(const std::vector<BYTE>& report) {
wprintf(L" {");
for (BYTE elm : report)
wprintf(L" %02x,", elm);
wprintf(L"}\n");
}
bool AccessHidDevice(const std::wstring& pdoPath) {
hid::Device dev(pdoPath.c_str(), false);
if (!dev.IsValid())
return false; // not a HID device
wprintf(L"Accessing HID device %s:\n", dev.Name().c_str());
dev.PrintInfo();
wprintf(L"\n");
wprintf(L"Available INPUT reports:\n");
std::vector<HIDP_VALUE_CAPS> valCaps = dev.GetValueCaps(HidP_Input);
for (auto& elm : valCaps) {
wprintf(L" ReportID: %#04x\n", elm.ReportID);
wprintf(L" UsagePage=%#04x, Usage=%#04x\n", elm.UsagePage, elm.NotRange.Usage);
std::vector<BYTE> report = dev.GetReport(HidP_Input, elm.ReportID);
//PrintReport(report);
ULONG value = dev.GetUsageValue(HidP_Input, elm, report);
wprintf(L" Value=%u\n", value);
}
std::vector<HIDP_BUTTON_CAPS> butCaps = dev.GetButtonCaps(HidP_Input);
for (UCHAR reportId : hid::Device::UniqueReportIDs<HIDP_BUTTON_CAPS>(butCaps)) {
wprintf(L" ReportID: %#04x\n", reportId);
PrintReport(dev.GetReport(HidP_Input, reportId));
}
wprintf(L"Available OUTPUT reports:\n");
valCaps = dev.GetValueCaps(HidP_Output);
for (auto& elm : valCaps) {
wprintf(L" ReportID: %#04x\n", elm.ReportID);
wprintf(L" UsagePage=%#04x, Usage=%#04x\n", elm.UsagePage, elm.NotRange.Usage);
// cannot print output reports, since they're sent to the device
}
butCaps = dev.GetButtonCaps(HidP_Output);
for (UCHAR reportId : hid::Device::UniqueReportIDs<HIDP_BUTTON_CAPS>(butCaps)) {
wprintf(L" ReportID: %#04x\n", reportId);
// cannot print output reports, since they're sent to the device
}
wprintf(L"Available FEATURE reports:\n");
valCaps = dev.GetValueCaps(HidP_Feature);
for (auto& elm : valCaps) {
wprintf(L" ReportID: %#04x\n", elm.ReportID);
wprintf(L" UsagePage=%#04x, Usage=%#04x\n", elm.UsagePage, elm.NotRange.Usage);
std::vector<BYTE> report = dev.GetReport(HidP_Feature, elm.ReportID);
//PrintReport(report);
ULONG value = dev.GetUsageValue(HidP_Feature, elm, report);
wprintf(L" Value=%u\n", value);
}
butCaps = dev.GetButtonCaps(HidP_Feature);
for (UCHAR reportId : hid::Device::UniqueReportIDs<HIDP_BUTTON_CAPS>(butCaps)) {
wprintf(L" ReportID: %#04x\n", reportId);
PrintReport(dev.GetReport(HidP_Feature, reportId));
}
wprintf(L"\n");
return true;
}
void BatteryVisitor(int /*idx*/, HDEVINFO devInfo, SP_DEVINFO_DATA& devInfoData) {
std::wstring devInstPath = GetDevPropStr(devInfo, devInfoData, &DEVPKEY_Device_InstanceId);
try {
AccessBattery(devInstPath, /*verbose*/true);
#if 0
std::wstring PDOName = GetDevPropStr(devInfo, devInfoData, &DEVPKEY_Device_PDOName); // Physical Device Object
std::wstring PDOPrefix = L"\\\\?\\GLOBALROOT";
AccessHidDevice(PDOPrefix + PDOName); // check if it's also a HID device
#endif
} catch (const std::exception& err) {
printf("ERROR: Unable to access battery parameters (%s)\n", err.what());
printf("Continuing battery scan...\n");
}
}
int wmain(int argc, wchar_t* argv[]) {
// initialize COM to enable WMI calls
ComInitialize com(COINIT_APARTMENTTHREADED);
if (argc < 2) {
wprintf(L"Querying all batteries:\n");
EnumerateInterfaces(GUID_DEVICE_BATTERY, BatteryVisitor);
return 0;
}
const wchar_t* instanceId = argv[1]; // 1 is first battery
unsigned int newCharge = static_cast<unsigned int>(-1); // skip updating by default
if (argc >= 3)
newCharge = _wtoi(argv[2]); // in [0,100] range
std::wstring pdoPath;
try {
DeviceInstance dev(instanceId);
auto desc = dev.GetDriverDesc();
wprintf(L" Driver description: %s\n", desc.c_str());
auto prov = dev.GetDriverProvider();
wprintf(L" Driver provider: %s\n", prov.c_str());
auto time = dev.GetDriverDate();
wprintf(L" Driver date: %s\n", DeviceInstance::FileTimeToDateStr(time).c_str());
auto ver = dev.GetDriverVersion();
wprintf(L" Driver version: %s\n", ver.c_str()); // in "H.MIN.SEC.MS" format for automatic versioning
pdoPath = dev.GetPDOPath();
} catch (std::exception& e) {
wprintf(L"ERROR: Unable to locate battery %s\n", instanceId);
wprintf(L"ERROR: what: %hs\n", e.what());
return -1;
}
int res = AccessBattery(instanceId, /*verbose*/true, newCharge); // access battery APIs
#if 0
AccessHidDevice(pdoPath); // check if it's also a HID device
#endif
return res;
}