-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
127 lines (107 loc) · 3.39 KB
/
main.go
File metadata and controls
127 lines (107 loc) · 3.39 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
// Test camera ConnectDevice with a local callback stub.
//
// This example demonstrates the server-side binder integration:
// a local ICameraDeviceCallback stub is created, passed to
// ConnectDevice, and the binder driver handles it as a local
// binder object (BINDER_TYPE_BINDER).
//
// Build:
//
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o build/camera_connect ./examples/camera_connect/
// adb push build/camera_connect /data/local/tmp/ && adb shell /data/local/tmp/camera_connect
package main
import (
"context"
"fmt"
"os"
"github.com/AndroidGoLab/binder/android/frameworks/cameraservice/device"
"github.com/AndroidGoLab/binder/android/frameworks/cameraservice/service"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/binder/versionaware"
"github.com/AndroidGoLab/binder/kernelbinder"
"github.com/AndroidGoLab/binder/servicemanager"
)
// noopCallback is a no-op ICameraDeviceCallbackServer implementation.
type noopCallback struct{}
func (c *noopCallback) OnCaptureStarted(
_ context.Context,
_ device.CaptureResultExtras,
_ int64,
) error {
return nil
}
func (c *noopCallback) OnDeviceError(
_ context.Context,
_ device.ErrorCode,
_ device.CaptureResultExtras,
) error {
return nil
}
func (c *noopCallback) OnDeviceIdle(_ context.Context) error {
return nil
}
func (c *noopCallback) OnPrepared(_ context.Context, _ int32) error {
return nil
}
func (c *noopCallback) OnRepeatingRequestError(
_ context.Context,
_ int64,
_ int32,
) error {
return nil
}
func (c *noopCallback) OnResultReceived(
_ context.Context,
_ device.CaptureMetadataInfo,
_ device.CaptureResultExtras,
_ []device.PhysicalCaptureResultInfo,
) error {
return nil
}
func (c *noopCallback) OnClientSharedAccessPriorityChanged(
_ context.Context,
_ bool,
) error {
return nil
}
func main() {
ctx := context.Background()
drv, err := kernelbinder.Open(ctx, binder.WithMapSize(128*1024))
if err != nil {
fmt.Fprintf(os.Stderr, "open binder: %v\n", err)
os.Exit(1)
}
defer drv.Close(ctx)
transport, err := versionaware.NewTransport(ctx, drv, 0)
if err != nil {
fmt.Fprintf(os.Stderr, "version-aware transport: %v\n", err)
os.Exit(1)
}
sm := servicemanager.New(transport)
// Look up the camera service.
cameraSvc, err := sm.GetService(ctx, "android.frameworks.cameraservice.service.ICameraService/default")
if err != nil {
fmt.Fprintf(os.Stderr, "get camera service: %v\n", err)
os.Exit(1)
}
cameraProxy := service.NewCameraServiceProxy(cameraSvc)
// Create a local callback stub.
callback := device.NewCameraDeviceCallbackStub(&noopCallback{})
fmt.Println("Created callback stub, calling ConnectDevice...")
// Try to connect. This will likely fail with a permission error,
// but the important thing is that the binder plumbing works
// (the stub is written as BINDER_TYPE_BINDER, not BINDER_TYPE_HANDLE).
deviceUser, err := cameraProxy.ConnectDevice(ctx, callback, "0")
if err != nil {
fmt.Fprintf(os.Stderr, "ConnectDevice error (expected): %v\n", err)
// Let deferred drv.Close run for clean shutdown.
return
}
fmt.Printf("ConnectDevice succeeded: %v\n", deviceUser)
// Disconnect the camera device before closing the binder driver.
// This tells the camera service to release the callback, so the
// binder driver's read loop is not stuck waiting for transactions.
if err := deviceUser.Disconnect(ctx); err != nil {
fmt.Fprintf(os.Stderr, "Disconnect: %v\n", err)
}
}