-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
125 lines (108 loc) · 3.71 KB
/
main.go
File metadata and controls
125 lines (108 loc) · 3.71 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
// Scan available WiFi networks via the wificond system service.
//
// Uses IWificond via the "wifinl80211" service to get client interfaces,
// then uses the IWifiScannerImpl from a client interface to retrieve
// scan results. Also queries available channels and PHY capabilities.
//
// Note: Requires root or appropriate SELinux permissions to interact
// with wificond. The scan results structure is empty in generated code
// (NativeScanResult fields not yet mapped), so only the count is shown.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/wifi_scanner ./examples/wifi_scanner/
// adb push build/wifi_scanner /data/local/tmp/ && adb shell /data/local/tmp/wifi_scanner
package main
import (
"context"
"fmt"
"os"
"github.com/AndroidGoLab/binder/android/net/wifi/nl80211"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"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)
svc, err := sm.GetService(ctx, servicemanager.WifiNl80211Service)
if err != nil {
fmt.Fprintf(os.Stderr, "get wifinl80211 service: %v\n", err)
fmt.Fprintf(os.Stderr, "(wificond not available or access denied)\n")
os.Exit(1)
}
wificond := nl80211.NewWificondProxy(svc)
// List client (STA) interfaces and try to scan from each.
clientIfaces, err := wificond.GetClientInterfaces(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "GetClientInterfaces: %v\n", err)
fmt.Fprintf(os.Stderr, "Hint: SELinux denies shell→wificond binder calls.\n")
fmt.Fprintf(os.Stderr, "Requires: adb root + setenforce 0 (permissive mode).\n")
return
}
fmt.Printf("Client interfaces found: %d\n", len(clientIfaces))
for i, ifaceBinder := range clientIfaces {
client := nl80211.NewClientInterfaceProxy(ifaceBinder)
ifName, err := client.GetInterfaceName(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, " [%d] GetInterfaceName: %v\n", i, err)
continue
}
fmt.Printf("\n Interface %d: %s\n", i, ifName)
mac, err := client.GetMacAddress(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, " GetMacAddress: %v\n", err)
} else {
fmt.Printf(" MAC: %x\n", mac)
}
scanner, err := client.GetWifiScannerImpl(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, " GetWifiScannerImpl: %v\n", err)
continue
}
maxSSIDs, err := scanner.GetMaxSsidsPerScan(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, " GetMaxSsidsPerScan: %v\n", err)
} else {
fmt.Printf(" Max SSIDs per scan: %d\n", maxSSIDs)
}
results, err := scanner.GetScanResults(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, " GetScanResults: %v\n", err)
continue
}
fmt.Printf(" Cached scan results: %d networks\n", len(results))
}
// Show available channels per band.
fmt.Println("\nAvailable channels by band:")
bands := []struct {
name string
fn func(context.Context) ([]int32, error)
}{
{"2.4 GHz", wificond.GetAvailable2gChannels},
{"5 GHz (non-DFS)", wificond.GetAvailable5gNonDFSChannels},
{"DFS", wificond.GetAvailableDFSChannels},
{"6 GHz", wificond.GetAvailable6gChannels},
{"60 GHz", wificond.GetAvailable60gChannels},
}
for _, band := range bands {
channels, err := band.fn(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, " %s: %v\n", band.name, err)
continue
}
fmt.Printf(" %-20s %v\n", band.name+":", channels)
}
}