Skip to content

Commit 90f48dc

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
fix: improve HAL error messages in battery_health and softap_wifi_hal examples
Add contextual hints when HAL calls fail: the health HAL may not be available on the device or may have died, and WiFi HAL access may be restricted by SELinux. This helps users diagnose why examples fail on devices where HAL services are dead or inaccessible.
1 parent c98a491 commit 90f48dc

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

examples/battery_health/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,23 @@ func main() {
3838

3939
svc, err := sm.GetService(ctx, servicemanager.ServiceName("android.hardware.health.IHealth/default"))
4040
if err != nil {
41-
fmt.Fprintf(os.Stderr, "get health HAL: %v (may be blocked by SELinux)\n", err)
41+
fmt.Fprintf(os.Stderr, "get health HAL: %v\n", err)
42+
fmt.Fprintf(os.Stderr, "(health HAL may not be available on this device, or access may be blocked by SELinux)\n")
4243
os.Exit(1)
4344
}
4445

4546
h := health.NewHealthProxy(svc)
4647

4748
capacity, err := h.GetCapacity(ctx)
4849
if err != nil {
49-
fmt.Fprintf(os.Stderr, "GetCapacity: %v\n", err)
50+
fmt.Fprintf(os.Stderr, "GetCapacity: %v (health HAL may have died or returned an error)\n", err)
5051
} else {
5152
fmt.Printf("Battery level: %d%%\n", capacity)
5253
}
5354

5455
status, err := h.GetChargeStatus(ctx)
5556
if err != nil {
56-
fmt.Fprintf(os.Stderr, "GetChargeStatus: %v\n", err)
57+
fmt.Fprintf(os.Stderr, "GetChargeStatus: %v (health HAL may have died or returned an error)\n", err)
5758
} else {
5859
statusName := "unknown"
5960
switch status {
@@ -73,14 +74,14 @@ func main() {
7374

7475
current, err := h.GetCurrentNowMicroamps(ctx)
7576
if err != nil {
76-
fmt.Fprintf(os.Stderr, "GetCurrentNowMicroamps: %v\n", err)
77+
fmt.Fprintf(os.Stderr, "GetCurrentNowMicroamps: %v (health HAL may have died or returned an error)\n", err)
7778
} else {
7879
fmt.Printf("Current draw: %d µA\n", current)
7980
}
8081

8182
counter, err := h.GetChargeCounterUah(ctx)
8283
if err != nil {
83-
fmt.Fprintf(os.Stderr, "GetChargeCounterUah: %v\n", err)
84+
fmt.Fprintf(os.Stderr, "GetChargeCounterUah: %v (health HAL may have died or returned an error)\n", err)
8485
} else {
8586
fmt.Printf("Charge counter: %d µAh\n", counter)
8687
}

examples/softap_wifi_hal/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ func main() {
6262
started, err := wifiHal.IsStarted(ctx)
6363
if err != nil {
6464
fmt.Fprintf(os.Stderr, "IsStarted: %v\n", err)
65+
fmt.Fprintf(os.Stderr, "(WiFi HAL may have died or access may be restricted by SELinux)\n")
6566
} else {
6667
fmt.Printf("WiFi HAL started: %v\n", started)
6768
}
6869

6970
// List available WiFi chips.
7071
chipIds, err := wifiHal.GetChipIds(ctx)
7172
if err != nil {
72-
fmt.Fprintf(os.Stderr, "GetChipIds: %v\n", err)
73+
fmt.Fprintf(os.Stderr, "GetChipIds: %v (WiFi HAL may have died or access may be restricted by SELinux)\n", err)
7374
return
7475
}
7576

0 commit comments

Comments
 (0)