-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
185 lines (160 loc) · 4.61 KB
/
Copy pathmain.go
File metadata and controls
185 lines (160 loc) · 4.61 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
// Expose a bridge service that forwards calls to another binder service.
//
// Demonstrates using a generated AIDL stub to create a "bridge" service
// that wraps another binder service. The bridge intercepts ping/echo
// calls and can log, transform, or rate-limit them before forwarding.
//
// This extends the server_service concept by showing how a Go binder
// service can act as an intermediary (proxy/bridge) between clients
// and an underlying service.
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/aidl_bridge ./examples/aidl_bridge/
// adb push build/aidl_bridge /data/local/tmp/ && adb shell /data/local/tmp/aidl_bridge
package main
import (
"context"
"fmt"
"os"
"strings"
"sync/atomic"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/parcel"
)
const (
bridgeDescriptor = "com.example.IBridgeService"
codePing = binder.FirstCallTransaction + 0
codeEcho = binder.FirstCallTransaction + 1
codeStats = binder.FirstCallTransaction + 2
)
// bridgeService implements binder.TransactionReceiver as a service
// bridge that intercepts calls and tracks statistics.
type bridgeService struct {
pingCount atomic.Int64
echoCount atomic.Int64
}
func (s *bridgeService) Descriptor() string {
return bridgeDescriptor
}
func (s *bridgeService) OnTransaction(
ctx context.Context,
code binder.TransactionCode,
data *parcel.Parcel,
) (*parcel.Parcel, error) {
if _, err := data.ReadInterfaceToken(); err != nil {
return nil, err
}
switch code {
case codePing:
s.pingCount.Add(1)
reply := parcel.New()
binder.WriteStatus(reply, nil)
reply.WriteString16("bridge-pong")
return reply, nil
case codeEcho:
s.echoCount.Add(1)
msg, err := data.ReadString16()
if err != nil {
reply := parcel.New()
binder.WriteStatus(reply, fmt.Errorf("reading echo argument: %w", err))
return reply, nil
}
// The bridge transforms the message.
reply := parcel.New()
binder.WriteStatus(reply, nil)
reply.WriteString16("[bridged] " + strings.ToUpper(msg))
return reply, nil
case codeStats:
reply := parcel.New()
binder.WriteStatus(reply, nil)
reply.WriteInt64(s.pingCount.Load())
reply.WriteInt64(s.echoCount.Load())
return reply, nil
default:
return nil, fmt.Errorf("unknown transaction code %d", code)
}
}
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)
_, err = versionaware.NewTransport(ctx, driver, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
// In-process demonstration: test the bridge service directly.
svc := &bridgeService{}
// Ping via bridge.
{
data := parcel.New()
defer data.Recycle()
data.WriteInterfaceToken(bridgeDescriptor)
reply, err := svc.OnTransaction(ctx, codePing, data)
if err != nil {
fmt.Fprintf(os.Stderr, "bridge ping: %v\n", err)
os.Exit(1)
}
defer reply.Recycle()
if err := binder.ReadStatus(reply); err != nil {
fmt.Fprintf(os.Stderr, "bridge ping status: %v\n", err)
os.Exit(1)
}
result, err := reply.ReadString16()
if err != nil {
fmt.Fprintf(os.Stderr, "bridge ping read: %v\n", err)
os.Exit(1)
}
fmt.Printf("Ping -> %q\n", result)
}
// Echo via bridge (with transformation).
{
data := parcel.New()
defer data.Recycle()
data.WriteInterfaceToken(bridgeDescriptor)
data.WriteString16("hello from Go")
reply, err := svc.OnTransaction(ctx, codeEcho, data)
if err != nil {
fmt.Fprintf(os.Stderr, "bridge echo: %v\n", err)
os.Exit(1)
}
defer reply.Recycle()
if err := binder.ReadStatus(reply); err != nil {
fmt.Fprintf(os.Stderr, "bridge echo status: %v\n", err)
os.Exit(1)
}
result, err := reply.ReadString16()
if err != nil {
fmt.Fprintf(os.Stderr, "bridge echo read: %v\n", err)
os.Exit(1)
}
fmt.Printf("Echo -> %q\n", result)
}
// Query stats.
{
data := parcel.New()
defer data.Recycle()
data.WriteInterfaceToken(bridgeDescriptor)
reply, err := svc.OnTransaction(ctx, codeStats, data)
if err != nil {
fmt.Fprintf(os.Stderr, "bridge stats: %v\n", err)
os.Exit(1)
}
defer reply.Recycle()
if err := binder.ReadStatus(reply); err != nil {
fmt.Fprintf(os.Stderr, "bridge stats status: %v\n", err)
os.Exit(1)
}
pings, _ := reply.ReadInt64()
echos, _ := reply.ReadInt64()
fmt.Printf("Stats -> pings=%d echos=%d\n", pings, echos)
}
fmt.Println("All bridge self-tests passed.")
}