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

linux,darwin: introduce battery info API #4258

Open
wants to merge 1 commit into
base: v1.x
Choose a base branch
from

Conversation

juanarbol
Copy link
Contributor

This is WIP with the intention of doing corrections if needed and may actually define the uv_battery_info_t struct and refine the implementation to handle all that the getBattery needs.

Refs: #4172

Copy link
Member

@bnoordhuis bnoordhuis left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Left some comments.

include/uv.h Outdated Show resolved Hide resolved
include/uv.h Show resolved Hide resolved
src/unix/darwin-battery.c Outdated Show resolved Hide resolved
goto out;

err = UV_EINVAL;
info->charging = pIOPSGetTimeRemainingEstimate();
Copy link
Member

Choose a reason for hiding this comment

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

You should assign this to a local variable first because implementation details like the -1 and -2 status codes shouldn't leak out to callers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On it!

How are we going to handle -1 and -2 states? As long as the struct will store only positive numbers?

Copy link
Contributor

Choose a reason for hiding this comment

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

I've added further members to the structure, but I think the question remains: how to handle platform-specific values like -1, or BYTE(255) in Win32 case.

#4271

test/test-platform-output.c Outdated Show resolved Hide resolved
@bnoordhuis
Copy link
Member

@juanarbol is this ready for re-review? Some of the comments don't seem to be addressed.

@juanarbol
Copy link
Contributor Author

juanarbol commented Jan 15, 2024

@juanarbol is this ready for re-review? Some of the comments don't seem to be addressed.

Sorry, I will fix those today (or tomorrow)! Quite busy lately

I'm revamping this whole thing, I'm gonna need a bit of extra time.

@juanarbol juanarbol force-pushed the juan/battery-mac branch 3 times, most recently from 643554f to c3f9e3f Compare January 17, 2024 04:30
@juanarbol
Copy link
Contributor Author

I had to spin a Linux VM. I'm working on the Linux patch. This is WIP.

@juanarbol juanarbol force-pushed the juan/battery-mac branch 2 times, most recently from 2106a75 to 6b67cde Compare January 17, 2024 05:21
@juanarbol
Copy link
Contributor Author

juanarbol commented Jan 17, 2024

Some IOKit calls or "core foundation" are unavailable for the macOS machine, but I am not sure...
The build servers normally don't have batteries 😂

@juanarbol juanarbol force-pushed the juan/battery-mac branch 4 times, most recently from 5b5cef5 to 9f62cfb Compare January 21, 2024 01:29
@juanarbol
Copy link
Contributor Author

This is now ready for review, but I have a question about how this should behave.

For now, if there is no battery for both Linux and Darwin, it will return UV_EPERM, and nothing else will be done.

In the Linux implementation, if it fails to read any other "battery information" file, I will return UV_EPERM, but macOS will leave the struct with garbage in the struct members and will return 0.

What do reviewers think is a better behavior to adopt? I propose two. I'm open to modifying this to a better one or unifying this into a decided one (and make the CI pass, for sure)

@juanarbol juanarbol marked this pull request as ready for review January 21, 2024 01:36
@juanarbol juanarbol changed the title darwin: add battery status (WIP) linux,darwin: introduce battery info API Jan 21, 2024
include/uv.h Outdated Show resolved Hide resolved
docs/src/misc.rst Outdated Show resolved Hide resolved
test/test-platform-output.c Outdated Show resolved Hide resolved
src/unix/battery.c Outdated Show resolved Hide resolved
src/win/battery.c Outdated Show resolved Hide resolved
test/test-platform-output.c Outdated Show resolved Hide resolved
src/unix/battery.c Outdated Show resolved Hide resolved
FILE* fp;

err = UV_EPERM;
fp = uv__open_file("/sys/class/power_supply/BAT0");
Copy link
Contributor

Choose a reason for hiding this comment

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

Would uv__slurp() help with all of this file reading logic?

Copy link
Member

Choose a reason for hiding this comment

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

Another thing: there can be multiple batteries. Linux's ACPI smart battery driver supports up to 4 (BAT0 to BAT3.)

I suppose you should try them all and merge the results? Either that or the documentation should make explicit that it only queries the first one.

src/unix/battery.c Outdated Show resolved Hide resolved
docs/src/misc.rst Outdated Show resolved Hide resolved
src/unix/battery.c Outdated Show resolved Hide resolved
#endif // defined(__APPLE__) || TARGET_OS_IPHONE

// macOS won't need any of this
#if defined(__LINUX__)
Copy link
Member

Choose a reason for hiding this comment

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

Here and elsewhere:

Suggested change
#if defined(__LINUX__)
#if defined(__linux__)

That said, it's probably better to move the platform-specific code into linux.c, darwin.c, etc., and have a UV_ENOSYS stub for platforms where it's unimplemented.

src/unix/battery.c Outdated Show resolved Hide resolved
src/unix/battery.c Outdated Show resolved Hide resolved
src/unix/battery.c Outdated Show resolved Hide resolved
FILE* fp;

err = UV_EPERM;
fp = uv__open_file("/sys/class/power_supply/BAT0");
Copy link
Member

Choose a reason for hiding this comment

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

Another thing: there can be multiple batteries. Linux's ACPI smart battery driver supports up to 4 (BAT0 to BAT3.)

I suppose you should try them all and merge the results? Either that or the documentation should make explicit that it only queries the first one.

src/unix/battery.c Outdated Show resolved Hide resolved
@bnoordhuis
Copy link
Member

In the Linux implementation, if it fails to read any other "battery information" file, I will return UV_EPERM, but macOS will leave the struct with garbage in the struct members and will return 0.

If you return an error, it's okay to leave the struct fields untouched. If you return 0 (no error), they must be set to something sensible (like zero.)

Copy link
Contributor Author

@juanarbol juanarbol left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time reviewing this, I will address all your suggestions as soon as I can :)

@juanarbol
Copy link
Contributor Author

I am still working on this; this is not stale; I am just not having the best time w/out a Linux machine with a better attached to it.

@juanarbol juanarbol force-pushed the juan/battery-mac branch 4 times, most recently from 10391f6 to 511125e Compare February 22, 2024 21:05
@juanarbol
Copy link
Contributor Author

I suppose you should try them all and merge the results? Either that or the documentation should make explicit that it only queries the first one.

I have the loop counting and reading all batteries inside this code (validated with my coworker's machine), but I'm not sure what do you mean with "merge" results, what if one of the three batteries is charging but the rest are not? I was considering using a uv_cpu_info_t array, likes uv_cpu_info(), return an array of batteries instead. But I think windows, nor darwin support multiple batteries.

That said, it's probably better to move the platform-specific code into linux.c, darwin.c, etc., and have a UV_ENOSYS stub for platforms where it's unimplemented. (#4258 (comment))

I put the conditional inclusion due to, macOS way won't need stdlib, just link to IOKit. Does that worth move split all this into separate files? (I'm genuinely asking, I don't know)

Refs: libuv#4172
Signed-off-by: Juan José Arboleda <[email protected]>
@juanarbol
Copy link
Contributor Author

ping @bnoordhuis

@calvin2021y
Copy link

Is android/ios supported ?

@juanarbol
Copy link
Contributor Author

Is android/ios supported ?

I think IOKit gives immediate support to iOS, not quite sure about android.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants