-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtool_get_bluetooth_state.go
More file actions
123 lines (102 loc) · 3.39 KB
/
tool_get_bluetooth_state.go
File metadata and controls
123 lines (102 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
//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"
)
const bluetoothManagerDescriptor = "android.bluetooth.IBluetoothManager"
// BluetoothState maps the int32 returned by IBluetoothManager.getState().
type BluetoothState int32
const (
BluetoothStateOff BluetoothState = 10
BluetoothStateTurningOn BluetoothState = 11
BluetoothStateOn BluetoothState = 12
BluetoothStateTurningOff BluetoothState = 13
BluetoothStateBLETurningOn BluetoothState = 14
BluetoothStateBLEOn BluetoothState = 15
BluetoothStateBLETurnOff BluetoothState = 16
)
// BluetoothStateResult holds the get_bluetooth_state response.
type BluetoothStateResult struct {
StateCode int32 `json:"state_code"`
State string `json:"state"`
Error string `json:"error,omitempty"`
}
func (ts *ToolSet) registerGetBluetoothState(s *server.MCPServer) {
tool := mcp.NewTool("get_bluetooth_state",
mcp.WithDescription(
"Get the Bluetooth adapter state using IBluetoothManager.getState(). "+
"Returns state_code and human-readable state (off/turning_on/on/turning_off).",
),
mcp.WithReadOnlyHintAnnotation(true),
mcp.WithDestructiveHintAnnotation(false),
mcp.WithIdempotentHintAnnotation(true),
)
s.AddTool(tool, ts.handleGetBluetoothState)
}
func (ts *ToolSet) handleGetBluetoothState(
ctx context.Context,
_ mcp.CallToolRequest,
) (*mcp.CallToolResult, error) {
logger.Tracef(ctx, "handleGetBluetoothState")
defer func() { logger.Tracef(ctx, "/handleGetBluetoothState") }()
svc, err := ts.sm.CheckService(ctx, servicemanager.ServiceName("bluetooth_manager"))
if err != nil || svc == nil {
return mcp.NewToolResultError("bluetooth_manager service unavailable"), nil
}
code, err := svc.ResolveCode(ctx, bluetoothManagerDescriptor, "getState")
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("resolving getState: %v", err)), nil
}
data := parcel.New()
defer data.Recycle()
data.WriteInterfaceToken(bluetoothManagerDescriptor)
reply, err := svc.Transact(ctx, code, 0, data)
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("getState: %v", err)), nil
}
defer reply.Recycle()
if err := binder.ReadStatus(reply); err != nil {
return mcp.NewToolResultError(fmt.Sprintf("getState status: %v", err)), nil
}
stateCode, err := reply.ReadInt32()
if err != nil {
return mcp.NewToolResultError(fmt.Sprintf("reading state: %v", err)), nil
}
result := BluetoothStateResult{
StateCode: stateCode,
State: bluetoothStateString(BluetoothState(stateCode)),
}
out, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("marshaling bluetooth state: %w", err)
}
return mcp.NewToolResultText(string(out)), nil
}
func bluetoothStateString(state BluetoothState) string {
switch state {
case BluetoothStateOff:
return "off"
case BluetoothStateTurningOn:
return "turning_on"
case BluetoothStateOn:
return "on"
case BluetoothStateTurningOff:
return "turning_off"
case BluetoothStateBLETurningOn:
return "ble_turning_on"
case BluetoothStateBLEOn:
return "ble_on"
case BluetoothStateBLETurnOff:
return "ble_turning_off"
default:
return fmt.Sprintf("unknown(%d)", state)
}
}