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
base: v1.x
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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.
src/unix/darwin-battery.c
Outdated
goto out; | ||
|
||
err = UV_EINVAL; | ||
info->charging = pIOPSGetTimeRemainingEstimate(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@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. |
643554f
to
c3f9e3f
Compare
I had to spin a Linux VM. I'm working on the Linux patch. This is WIP. |
2106a75
to
6b67cde
Compare
|
5b5cef5
to
9f62cfb
Compare
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 In the Linux implementation, if it fails to read any other "battery information" file, I will return 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) |
src/unix/battery.c
Outdated
FILE* fp; | ||
|
||
err = UV_EPERM; | ||
fp = uv__open_file("/sys/class/power_supply/BAT0"); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
#endif // defined(__APPLE__) || TARGET_OS_IPHONE | ||
|
||
// macOS won't need any of this | ||
#if defined(__LINUX__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and elsewhere:
#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
FILE* fp; | ||
|
||
err = UV_EPERM; | ||
fp = uv__open_file("/sys/class/power_supply/BAT0"); |
There was a problem hiding this comment.
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.
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.) |
There was a problem hiding this 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 :)
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. |
10391f6
to
511125e
Compare
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
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]>
511125e
to
cb01607
Compare
ping @bnoordhuis |
Is android/ios supported ? |
I think IOKit gives immediate support to iOS, not quite sure about android. |
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 thegetBattery
needs.Refs: #4172