-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
196 lines (174 loc) · 5.72 KB
/
main.go
File metadata and controls
196 lines (174 loc) · 5.72 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Poll thermal service for CPU/GPU temperatures, throttling status, and cooling devices.
//
// Uses the generated IThermalService proxy via the "thermalservice" binder
// service and IHardwarePropertiesManager via "hardware_properties" for
// CPU usage and device temperatures.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/thermal_monitor ./examples/thermal_monitor/
// adb push build/thermal_monitor /data/local/tmp/ && adb shell /data/local/tmp/thermal_monitor
package main
import (
"context"
"fmt"
"os"
genOs "github.com/AndroidGoLab/binder/android/os"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
// Android temperature type constants (from android.os.Temperature).
const (
temperatureTypeCPU int32 = 0
temperatureTypeGPU int32 = 1
temperatureTypeBattery int32 = 2
temperatureTypeSkin int32 = 3
temperatureTypeUSBPort int32 = 4
temperatureTypePowerAmplif int32 = 5
temperatureTypeBCL int32 = 6
temperatureTypeNPU int32 = 7
)
// Android HardwarePropertiesManager temperature type constants.
const (
deviceTempCPU int32 = 0
deviceTempGPU int32 = 1
deviceTempBattery int32 = 2
deviceTempSkin int32 = 3
)
// Android HardwarePropertiesManager temperature source constants.
const (
tempSourceCurrent int32 = 0
tempSourceThrottle int32 = 1
tempSourceShutdown int32 = 2
)
var temperatureTypeNames = map[int32]string{
temperatureTypeCPU: "CPU",
temperatureTypeGPU: "GPU",
temperatureTypeBattery: "Battery",
temperatureTypeSkin: "Skin",
temperatureTypeUSBPort: "USB Port",
temperatureTypePowerAmplif: "Power Amp",
temperatureTypeBCL: "BCL",
temperatureTypeNPU: "NPU",
}
var thermalStatusNames = []string{
"none", "light", "moderate", "severe", "critical", "emergency", "shutdown",
}
func main() {
ctx := context.Background()
driver, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer driver.Close(ctx)
transport, err := versionaware.NewTransport(ctx, driver, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
sm := servicemanager.New(transport)
// --- Thermal Service ---
thermalSvc, err := sm.GetService(ctx, servicemanager.ThermalService)
if err != nil {
fmt.Fprintf(os.Stderr, "get thermal service: %v\n", err)
} else {
thermal := genOs.NewThermalServiceProxy(thermalSvc)
// Current thermal status
status, err := thermal.GetCurrentThermalStatus(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetCurrentThermalStatus: %v\n", err)
} else {
name := "unknown"
if int(status) < len(thermalStatusNames) {
name = thermalStatusNames[status]
}
fmt.Printf("Thermal status: %s (%d)\n", name, status)
}
// Thermal headroom forecast
headroom, err := thermal.GetThermalHeadroom(ctx, 10)
if err != nil {
fmt.Fprintf(os.Stderr, "GetThermalHeadroom: %v\n", err)
} else {
fmt.Printf("Thermal headroom: %.2f (10s forecast)\n", headroom)
}
// All current temperatures
temps, err := thermal.GetCurrentTemperatures(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetCurrentTemperatures: %v\n", err)
} else {
fmt.Printf("\nTemperature sensors: %d\n", len(temps))
for _, t := range temps {
typeName := temperatureTypeNames[t.Type]
if typeName == "" {
typeName = fmt.Sprintf("type(%d)", t.Type)
}
statusName := "ok"
if int(t.Status) < len(thermalStatusNames) {
statusName = thermalStatusNames[t.Status]
}
fmt.Printf(" %-20s %-10s %.1f C status=%s\n", t.Name, typeName, t.Value, statusName)
}
}
// Cooling devices
coolers, err := thermal.GetCurrentCoolingDevices(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetCurrentCoolingDevices: %v\n", err)
} else {
fmt.Printf("\nCooling devices: %d\n", len(coolers))
for _, c := range coolers {
fmt.Printf(" %-20s type=%d value=%d\n", c.Name, c.Type, c.Value)
}
}
}
// --- Hardware Properties Manager ---
hwProps, err := genOs.GetHardwarePropertiesManager(ctx, sm)
if err != nil {
fmt.Fprintf(os.Stderr, "get hardware_properties service: %v\n", err)
return
}
// CPU temperatures (current)
cpuTemps, err := hwProps.GetDeviceTemperatures(ctx, deviceTempCPU, tempSourceCurrent)
if err != nil {
fmt.Fprintf(os.Stderr, "GetDeviceTemperatures(CPU): %v\n", err)
} else {
fmt.Printf("\nCPU temperatures (current): %v C\n", cpuTemps)
}
// CPU throttling thresholds
cpuThrottle, err := hwProps.GetDeviceTemperatures(ctx, deviceTempCPU, tempSourceThrottle)
if err != nil {
fmt.Fprintf(os.Stderr, "GetDeviceTemperatures(CPU,throttle): %v\n", err)
} else {
fmt.Printf("CPU throttle thresholds: %v C\n", cpuThrottle)
}
// GPU temperatures
gpuTemps, err := hwProps.GetDeviceTemperatures(ctx, deviceTempGPU, tempSourceCurrent)
if err != nil {
fmt.Fprintf(os.Stderr, "GetDeviceTemperatures(GPU): %v\n", err)
} else {
fmt.Printf("GPU temperatures (current): %v C\n", gpuTemps)
}
// CPU usage
cpuUsages, err := hwProps.GetCpuUsages(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetCpuUsages: %v\n", err)
} else {
fmt.Printf("\nCPU usage: %d cores\n", len(cpuUsages))
for i, u := range cpuUsages {
pct := float64(0)
if u.Total > 0 {
pct = float64(u.Active) / float64(u.Total) * 100
}
fmt.Printf(" Core %d: %.1f%% (active=%d total=%d)\n", i, pct, u.Active, u.Total)
}
}
// Fan speeds
fans, err := hwProps.GetFanSpeeds(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetFanSpeeds: %v\n", err)
} else if len(fans) > 0 {
fmt.Printf("\nFan speeds: %v RPM\n", fans)
}
}