Skip to content

Commit

Permalink
When battery level is critical, try to put macOS into sleep/hibernate…
Browse files Browse the repository at this point in the history
… mode only once per minute.
  • Loading branch information
lvs1974 committed Oct 25, 2021
1 parent 0924390 commit 8c0e6dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
HibernationFixup Changelog
============================
#### v1.4.5
- When battery level is critical, try to put macOS into sleep/hibernate mode only once per minute.

#### v1.4.4
- Automatically puts macOS into sleep/hibernate mode when WhenBatteryIsAtWarnLevel or WhenBatteryAtCriticalLevel bit is set in hbfx-ahbm. If battery kext does not provide these levels, additional bits for remaining capacity can be specified (RemainCapacityBit1, RemainCapacityBit2, RemainCapacityBit3, RemainCapacityBit4).

Expand Down
22 changes: 20 additions & 2 deletions HibernationFixup/kern_hbfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ IOReturn HBFX::IOHibernateSystemWake(void)

if (callbackHBFX->nextSleepTimer)
callbackHBFX->nextSleepTimer->cancelTimeout();

if (callbackHBFX->checkCapacityTimer) {
callbackHBFX->checkCapacityTimer->cancelTimeout();
callbackHBFX->checkCapacityTimer->setTimeoutMS(60000);
}

if (result == KERN_SUCCESS && wakeType && wakeReason)
{
Expand All @@ -185,8 +190,6 @@ IOReturn HBFX::IOHibernateSystemWake(void)
void HBFX::IOPMrootDomain_evaluatePolicy(IOPMrootDomain* that, int stimulus, uint32_t arg)
{
DBGLOG("HBFX", "evaluatePolicy called, stimulus = 0x%x", stimulus);

callbackHBFX->checkCapacity();

auto autoHibernateMode = ADDPR(hbfx_config).autoHibernateMode;
if (autoHibernateMode & Configuration::DisableStimulusDarkWakeActivityTickle) {
Expand Down Expand Up @@ -218,6 +221,11 @@ void HBFX::IOPMrootDomain_willEnterFullWake(IOPMrootDomain* that)

if (callbackHBFX->nextSleepTimer)
callbackHBFX->nextSleepTimer->cancelTimeout();

if (callbackHBFX->checkCapacityTimer) {
callbackHBFX->checkCapacityTimer->cancelTimeout();
callbackHBFX->checkCapacityTimer->setTimeoutMS(60000);
}
}

//==============================================================================
Expand Down Expand Up @@ -505,6 +513,14 @@ IOReturn HBFX::IOPCIBridge_restoreMachineState(IOService *that, IOOptionBits opt
if (kMachineRestoreDehibernate & options)
callbackHBFX->correct_pci_config_command = false;

if (callbackHBFX->nextSleepTimer)
callbackHBFX->nextSleepTimer->cancelTimeout();

if (callbackHBFX->checkCapacityTimer) {
callbackHBFX->checkCapacityTimer->cancelTimeout();
callbackHBFX->checkCapacityTimer->setTimeoutMS(60000);
}

return result;
}

Expand Down Expand Up @@ -959,6 +975,8 @@ void HBFX::readConfigFromNVRAM()
}
else
{
DBGLOG("HBFX", "readConfigFromNVRAM: use EfiRuntimeServices");

auto rt = EfiRuntimeServices::get(true);
if (rt) {
constexpr const size_t buf_size = 64;
Expand Down

0 comments on commit 8c0e6dc

Please sign in to comment.