forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
adb.go
144 lines (119 loc) · 3.26 KB
/
adb.go
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
package adb
import (
"strconv"
"github.com/jt6562/go-adb/internal/errors"
"github.com/jt6562/go-adb/wire"
)
/*
Adb communicates with host services on the adb server.
Eg.
client := adb.New()
client.ListDevices()
See list of services at https://android.googlesource.com/platform/system/core/+/master/adb/SERVICES.TXT.
*/
// TODO(z): Finish implementing host services.
type Adb struct {
server server
}
// New creates a new Adb client that uses the default ServerConfig.
func New() (*Adb, error) {
return NewWithConfig(ServerConfig{})
}
func NewWithConfig(config ServerConfig) (*Adb, error) {
server, err := newServer(config)
if err != nil {
return nil, err
}
return &Adb{server}, nil
}
// Dial establishes a connection with the adb server.
func (c *Adb) Dial() (*wire.Conn, error) {
return c.server.Dial()
}
// Starts the adb server if it’s not running.
func (c *Adb) StartServer() error {
return c.server.Start()
}
func (c *Adb) Device(descriptor DeviceDescriptor) *Device {
return &Device{
server: c.server,
descriptor: descriptor,
deviceListFunc: c.ListDevices,
}
}
func (c *Adb) NewDeviceWatcher() *DeviceWatcher {
return newDeviceWatcher(c.server)
}
// ServerVersion asks the ADB server for its internal version number.
func (c *Adb) ServerVersion() (int, error) {
resp, err := roundTripSingleResponse(c.server, "host:version")
if err != nil {
return 0, wrapClientError(err, c, "GetServerVersion")
}
version, err := c.parseServerVersion(resp)
if err != nil {
return 0, wrapClientError(err, c, "GetServerVersion")
}
return version, nil
}
/*
KillServer tells the server to quit immediately.
Corresponds to the command:
adb kill-server
*/
func (c *Adb) KillServer() error {
conn, err := c.server.Dial()
if err != nil {
return wrapClientError(err, c, "KillServer")
}
defer conn.Close()
if err = wire.SendMessageString(conn, "host:kill"); err != nil {
return wrapClientError(err, c, "KillServer")
}
return nil
}
/*
ListDeviceSerials returns the serial numbers of all attached devices.
Corresponds to the command:
adb devices
*/
func (c *Adb) ListDeviceSerials() ([]string, error) {
resp, err := roundTripSingleResponse(c.server, "host:devices")
if err != nil {
return nil, wrapClientError(err, c, "ListDeviceSerials")
}
devices, err := parseDeviceList(string(resp), parseDeviceShort)
if err != nil {
return nil, wrapClientError(err, c, "ListDeviceSerials")
}
serials := make([]string, len(devices))
for i, dev := range devices {
serials[i] = dev.Serial
}
return serials, nil
}
/*
ListDevices returns the list of connected devices.
Corresponds to the command:
adb devices -l
*/
func (c *Adb) ListDevices() ([]*DeviceInfo, error) {
resp, err := roundTripSingleResponse(c.server, "host:devices-l")
if err != nil {
return nil, wrapClientError(err, c, "ListDevices")
}
devices, err := parseDeviceList(string(resp), parseDeviceLong)
if err != nil {
return nil, wrapClientError(err, c, "ListDevices")
}
return devices, nil
}
func (c *Adb) parseServerVersion(versionRaw []byte) (int, error) {
versionStr := string(versionRaw)
version, err := strconv.ParseInt(versionStr, 16, 32)
if err != nil {
return 0, errors.WrapErrorf(err, errors.ParseError,
"error parsing server version: %s", versionStr)
}
return int(version), nil
}