svartalf / rust-battery

Rust crate providing cross-platform information about the notebook batteries.
https://crates.io/crates/battery
Apache License 2.0
358 stars 40 forks source link

Fix division by zero if charging or discharging with no energy_rate #5

Closed kerhong closed 5 years ago

kerhong commented 5 years ago

When using battery-cli and unplugging the charger a division by zero happens because the battery is now in State::Discharging, but energy_rate is still 0.

svartalf commented 5 years ago

Unfortunately, I was not able to reproduce this case with my battery driver, guess that's why that bug appeared.

Do you mind to add a comment with short description and link to this PR? It seems to be a good idea to collect some knowledge base about possible edge cases.

kerhong commented 5 years ago

Added a comment in code. More info about reproduction:

$ uname -a
Linux <redacted> 4.19.23-1-MANJARO #1 SMP PREEMPT Fri Feb 15 21:27:33 UTC 2019 x86_64 GNU/Linux
$ lshw | grep -A6 battery
  *-battery:0
       product: 45N1743
       vendor: SONY
       physical id: 1
       slot: Front
       capacity: 44460mWh
       configuration: voltage=11.4V
  *-battery:1
       product: 45N1127
       vendor: LGC
       physical id: 2
       slot: Rear
       capacity: 23480mWh
       configuration: voltage=11.4V
$ lshw | grep -A4 Motherboard
       description: Motherboard
       product: 20FH001TUS
       vendor: LENOVO
       physical id: 0
       version: SDK0J40705 WIN
$ lshw | grep -A4 Notebook
    description: Notebook
    product: 20FH001TUS (LENOVO_MT_20FH_BU_Think_FM_ThinkPad T560)
    vendor: LENOVO
    version: ThinkPad T560

The battery that starts discharging (and triggers the divsion by zero) is battery:1 from LGC

svartalf commented 5 years ago

Amazing, thank you!

I'll upload fix to the crates.io soon, depending if any other changes would appear today