-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (59 loc) · 1.88 KB
/
main.go
File metadata and controls
71 lines (59 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Measure battery current draw over time via the Health HAL.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/power_profiling ./examples/power_profiling/
// adb push build/power_profiling /data/local/tmp/ && adb shell /data/local/tmp/power_profiling
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/AndroidGoLab/binder/android/hardware/health"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
const samples = 5
const interval = 2 * time.Second
func main() {
ctx := context.Background()
drv, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer drv.Close(ctx)
transport, err := versionaware.NewTransport(ctx, drv, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
sm := servicemanager.New(transport)
svc, err := sm.GetService(ctx, servicemanager.ServiceName(health.DescriptorIHealth+"/default"))
if err != nil {
fmt.Fprintf(os.Stderr, "get health service: %v\n", err)
os.Exit(1)
}
h := health.NewHealthProxy(svc)
fmt.Printf("Sampling battery current %d times at %s intervals...\n", samples, interval)
for i := 0; i < samples; i++ {
now, err := h.GetCurrentNowMicroamps(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, " sample %d: GetCurrentNowMicroamps: %v\n", i, err)
} else {
fmt.Printf(" sample %d: %d uA (%.1f mA)\n", i, now, float64(now)/1000.0)
}
avg, err := h.GetCurrentAverageMicroamps(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, " sample %d: GetCurrentAverageMicroamps: %v\n", i, err)
} else {
fmt.Printf(" sample %d avg: %d uA (%.1f mA)\n", i, avg, float64(avg)/1000.0)
}
if i < samples-1 {
time.Sleep(interval)
}
}
}