-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_get_device_info.go
More file actions
161 lines (131 loc) · 4.31 KB
/
tool_get_device_info.go
File metadata and controls
161 lines (131 loc) · 4.31 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
//go:build linux
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/facebookincubator/go-belt/tool/logger"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"github.com/AndroidGoLab/binder/binder"
"github.com/AndroidGoLab/binder/parcel"
"github.com/AndroidGoLab/binder/servicemanager"
)
// DeviceInfoResult aggregates high-level device state from multiple services.
type DeviceInfoResult struct {
Power PowerInfo `json:"power"`
Display DisplayInfo `json:"display"`
}
// PowerInfo holds power manager state.
type PowerInfo struct {
Interactive bool `json:"interactive"`
Error string `json:"error,omitempty"`
}
// DisplayInfo holds basic display state.
type DisplayInfo struct {
Brightness float32 `json:"brightness"`
Error string `json:"error,omitempty"`
}
func (ts *ToolSet) registerGetDeviceInfo(s *server.MCPServer) {
tool := mcp.NewTool("get_device_info",
mcp.WithDescription(
"Get high-level device information by querying multiple binder "+
"services (power, display). Returns a JSON object with each "+
"subsystem's state. Errors for individual subsystems are "+
"reported inline rather than failing the whole call.",
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, ts.handleGetDeviceInfo)
}
func (ts *ToolSet) handleGetDeviceInfo(
ctx context.Context,
_ mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleGetDeviceInfo")
defer func() { logger.Tracef(ctx, "/handleGetDeviceInfo") }()
result := DeviceInfoResult{
Power: ts.queryPowerInfo(ctx),
Display: ts.queryDisplayInfo(ctx),
}
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("marshaling device info: %w", err)
}
return mcp.NewToolResultText(string(data)), nil
}
const (
powerManagerDescriptor = "android.os.IPowerManager"
displayManagerDescriptor = "android.hardware.display.IDisplayManager"
)
func (ts *ToolSet) queryPowerInfo(ctx context.Context) PowerInfo {
svc, err := ts.sm.CheckService(ctx, servicemanager.ServiceName("power"))
if err != nil || svc == nil {
return PowerInfo{Error: "power service unavailable"}
}
code, err := svc.ResolveCode(ctx, powerManagerDescriptor, "isInteractive")
if err != nil {
return PowerInfo{Error: fmt.Sprintf("resolving isInteractive: %v", err)}
}
interactive, err := transactBool(ctx, svc, powerManagerDescriptor, code)
if err != nil {
return PowerInfo{Error: fmt.Sprintf("isInteractive: %v", err)}
}
return PowerInfo{Interactive: interactive}
}
func (ts *ToolSet) queryDisplayInfo(ctx context.Context) DisplayInfo {
svc, err := ts.sm.CheckService(ctx, servicemanager.ServiceName("display"))
if err != nil || svc == nil {
return DisplayInfo{Error: "display service unavailable"}
}
code, err := svc.ResolveCode(ctx, displayManagerDescriptor, "getBrightness")
if err != nil {
// getBrightness may not be available on all API levels.
return DisplayInfo{Error: fmt.Sprintf("resolving getBrightness: %v", err)}
}
data := parcel.New()
defer data.Recycle()
data.WriteInterfaceToken(displayManagerDescriptor)
// getBrightness(int displayId) -- use display 0 (default).
data.WriteInt32(0)
reply, err := svc.Transact(ctx, code, 0, data)
if err != nil {
return DisplayInfo{Error: fmt.Sprintf("getBrightness: %v", err)}
}
defer reply.Recycle()
if err := binder.ReadStatus(reply); err != nil {
return DisplayInfo{Error: fmt.Sprintf("getBrightness status: %v", err)}
}
brightness, err := reply.ReadFloat32()
if err != nil {
return DisplayInfo{Error: fmt.Sprintf("reading brightness: %v", err)}
}
return DisplayInfo{Brightness: brightness}
}
// transactBool sends a simple transaction that returns a boolean (int32 0/1)
// after the status field. It writes the interface token automatically.
func transactBool(
ctx context.Context,
svc binder.IBinder,
descriptor string,
code binder.TransactionCode,
) (bool, error) {
data := parcel.New()
defer data.Recycle()
data.WriteInterfaceToken(descriptor)
reply, err := svc.Transact(ctx, code, 0, data)
if err != nil {
return false, err
}
defer reply.Recycle()
if err := binder.ReadStatus(reply); err != nil {
return false, err
}
val, err := reply.ReadInt32()
if err != nil {
return false, err
}
return val != 0, nil
}