Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement battery table for Windows #8267

Merged
merged 8 commits into from
Mar 1, 2024
Merged

Conversation

zwass
Copy link
Member

@zwass zwass commented Feb 9, 2024

Almost all of the columns are able to line up with those already available on macOS.

This has been tested on a single Windows 11 Pro laptop. All columns look correct except for cycle_count in which this device reports 0. We seem to be using the correct API though and will hopefully get values on other devices.

Testing also performed on a macOS laptop with Windows 10/11 in VMs - the table successfully reports the VMWare virtual battery when that is configured and no results (along with appropriate logging) when there is no virtual battery.

Copy link
Contributor

@getvictor getvictor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation makes sense. I added a couple questions.

Does osquery have integration tests that run on Windows?

Comment on lines 250 to 251
// Once we find one battery, no need to do anything
// else
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we return all the batteries? There are some laptops with 2 batteries. Also, for some laptops you can get an external battery, like https://www.amazon.com/593553-001-593554-001-Extended-Pavilion-CQ42/dp/B07SGHM9RR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point. I've never encountered such a setup (and don't have one to test on) but it's pretty easy to allow this code to support it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to support this but I'm not able to test it. Code still works for 0/1 batteries.

if (!SetupDiEnumDeviceInterfaces(
hdev, 0, &GUID_DEVCLASS_BATTERY, idev, &did)) {
LOG(ERROR) << "Failed to set up enumeration for batteries: "
<< GetLastError();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens on a Windows desktop without a battery? Also, what happens in a Windows VM?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this testing as well and updated the top level description

Testing also performed on a macOS laptop with Windows 10/11 in VMs - the table successfully reports the VMWare virtual battery when that is configured and no results (along with appropriate logging) when there is no virtual battery.

@directionless directionless added this to the 5.12.0 milestone Feb 27, 2024
@zwass zwass requested review from getvictor and directionless and removed request for getvictor February 29, 2024 02:18
@zwass
Copy link
Member Author

zwass commented Feb 29, 2024

@directionless I updated the PR to support multiple batteries as suggested by @getvictor and also performed additional testing.

@getvictor yes there are integration tests for Windows. I turned on the test for this table in the CMakeLists.txt. There is no diff for the .cpp file.

Copy link
Member

@directionless directionless left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a couple questions, but I think we can merge if you feel okay with the answers

Comment on lines +86 to +88
if (!SetupDiEnumDeviceInterfaces(
hdev, 0, &GUID_DEVCLASS_BATTERY, idev, &did)) {
if (GetLastError() != ERROR_NO_MORE_ITEMS) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure what this does. Does it cause an early return if the OS says we're done with batteries, or does it always try 100 times? (Because if it's the latter, I'm wondering if there's a speed/overhead impact)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does an early return no matter what the error is, but only logs if the error is something besides the OS saying there are no more batteries to enumerate (which is an expected case).

Comment on lines +185 to +189
// Assume that 12 volts is the intended voltage for the
// battery in order to convert from the mWh units that
// Microsoft provides to match the mAh units that the battery
// table already uses for macOS.
const int designedVoltage = 12;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How confident are you here? And why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not able to find any API that provides the designed voltage, so I'm not sure what to do here to make the values on Windows match the values on macOS besides trying this assumption. I also only have a single Windows device for testing, so my inclination is to see how these values do in the real world with bigger Windows deployments.

Copy link
Contributor

@getvictor getvictor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zwass zwass merged commit fdabe5a into osquery:master Mar 1, 2024
16 checks passed
@zwass zwass deleted the battery-table branch March 1, 2024 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants