Skip to content

Commit

Permalink
Merge branch 'preventsleep'
Browse files Browse the repository at this point in the history
  • Loading branch information
benma committed Sep 19, 2024
2 parents 55d1fc2 + c91e327 commit 88d5934
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
8 changes: 8 additions & 0 deletions api/firmware/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/BitBoxSwiss/bitbox02-api-go/api/firmware/messages"
"github.com/BitBoxSwiss/bitbox02-api-go/util/errp"
"github.com/BitBoxSwiss/bitbox02-api-go/util/semver"
"github.com/BitBoxSwiss/bitbox02-api-go/util/sleep"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -75,6 +76,8 @@ func (device *Device) rawQueryV7(msg []byte) ([]byte, error) {
break
}

isLongRunning := false

for {
switch status {
case hwwRspAck:
Expand All @@ -100,6 +103,11 @@ func (device *Device) rawQueryV7(msg []byte) ([]byte, error) {
}
return nil, errp.New("unexpected NACK response")
case hwwRspNotready:
if !isLongRunning {
isLongRunning = true
sleep.Prevent()
defer sleep.Allow()
}
time.Sleep(200 * time.Millisecond)
lastQueryTimes.Value = time.Now()
lastQueryTimes = lastQueryTimes.Next()
Expand Down
25 changes: 25 additions & 0 deletions util/sleep/sleep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !darwin || nosleep

package sleep

// Prevent is a no-op on non macOS platforms or when nosleep is configured.
func Prevent() {
}

// Allow is a no-op on non macOS platforms or when nosleep is configured.
func Allow() {
}
55 changes: 55 additions & 0 deletions util/sleep/sleep_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Shift Crypto AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build darwin && !nosleep

package sleep

/*
#cgo LDFLAGS: -framework IOKit
#include <IOKit/pwr_mgt/IOPMLib.h>
#include <IOKit/IOReturn.h>
#include <stdbool.h>
static IOPMAssertionID _assertionID;
static bool _assertionCreated = false;
void preventSleep() {
if (_assertionCreated) {
return;
}
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, CFSTR("Prevent Sleep"), &_assertionID);
if (success == kIOReturnSuccess) {
// Successfully disabled sleep.
_assertionCreated = true;
}
}
void allowSleep() {
if (_assertionCreated) {
IOPMAssertionRelease(_assertionID);
_assertionCreated = false;
}
}
*/
import "C"

// Prevent prevents macOS from going to sleep. Must be paired with `Allow()`.
func Prevent() {
C.preventSleep()
}

// Allow allows macOS from going to sleep.
func Allow() {
C.allowSleep()
}

0 comments on commit 88d5934

Please sign in to comment.