-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
99 lines (89 loc) · 2.49 KB
/
main.go
File metadata and controls
99 lines (89 loc) · 2.49 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
// Query storage and USB state: last fstrim, USB functions, USB speed.
//
// Build:
//
// GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o build/storage_info ./examples/storage_info/
// adb push storage_info /data/local/tmp/ && adb shell /data/local/tmp/storage_info
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/android/hardware/usb"
"github.com/AndroidGoLab/binder/android/os/storage"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
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)
// Storage Manager
mountSvc, err := sm.GetService(ctx, servicemanager.MountService)
if err != nil {
fmt.Fprintf(os.Stderr, "get mount service: %v\n", err)
} else {
store := storage.NewStorageManagerProxy(mountSvc)
lastMaint, err := store.LastMaintenance(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "LastMaintenance: %v\n", err)
} else {
t := time.UnixMilli(lastMaint)
fmt.Printf("Last fstrim: %s (%s ago)\n", t.Format(time.RFC3339), time.Since(t).Round(time.Second))
}
}
// USB Manager
usbMgr, err := usb.GetUsbManager(ctx, sm)
if err != nil {
fmt.Fprintf(os.Stderr, "get usb service: %v\n", err)
} else {
funcs, err := usbMgr.GetCurrentFunctions(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetCurrentFunctions: %v\n", err)
} else {
fmt.Printf("USB functions: 0x%x", funcs)
var names []string
if funcs&1 != 0 {
names = append(names, "MTP")
}
if funcs&4 != 0 {
names = append(names, "PTP")
}
if funcs&8 != 0 {
names = append(names, "MIDI")
}
if funcs&32 != 0 {
names = append(names, "ACCESSORY")
}
if funcs&64 != 0 {
names = append(names, "AUDIO_SOURCE")
}
if funcs&512 != 0 {
names = append(names, "NCM")
}
if len(names) > 0 {
fmt.Printf(" (%v)", names)
}
fmt.Println()
}
speed, err := usbMgr.GetCurrentUsbSpeed(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetCurrentUsbSpeed: %v\n", err)
} else {
fmt.Printf("USB speed: %d\n", speed)
}
}
}