-
Notifications
You must be signed in to change notification settings - Fork 40
/
client.go
219 lines (180 loc) · 4.51 KB
/
client.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package gadb
import (
"fmt"
"strconv"
"strings"
)
const AdbServerPort = 5037
const AdbDaemonPort = 5555
type Client struct {
host string
port int
}
func NewClient() (Client, error) {
return NewClientWith("localhost")
}
func NewClientWith(host string, port ...int) (adbClient Client, err error) {
if len(port) == 0 {
port = []int{AdbServerPort}
}
adbClient.host = host
adbClient.port = port[0]
var tp transport
if tp, err = adbClient.createTransport(); err != nil {
return Client{}, err
}
defer func() { _ = tp.Close() }()
return
}
func (c Client) ServerVersion() (version int, err error) {
var resp string
if resp, err = c.executeCommand("host:version"); err != nil {
return 0, err
}
var v int64
if v, err = strconv.ParseInt(resp, 16, 64); err != nil {
return 0, err
}
version = int(v)
return
}
func (c Client) DeviceSerialList() (serials []string, err error) {
var resp string
if resp, err = c.executeCommand("host:devices"); err != nil {
return
}
lines := strings.Split(resp, "\n")
serials = make([]string, 0, len(lines))
for i := range lines {
fields := strings.Fields(lines[i])
if len(fields) < 2 {
continue
}
serials = append(serials, fields[0])
}
return
}
func (c Client) DeviceList() (devices []Device, err error) {
var resp string
if resp, err = c.executeCommand("host:devices-l"); err != nil {
return
}
lines := strings.Split(resp, "\n")
devices = make([]Device, 0, len(lines))
for i := range lines {
line := strings.TrimSpace(lines[i])
if line == "" {
continue
}
fields := strings.Fields(line)
if len(fields) < 4 || len(fields[0]) == 0 {
debugLog(fmt.Sprintf("can't parse: %s", line))
continue
}
sliceAttrs := fields[2:]
mapAttrs := map[string]string{}
for _, field := range sliceAttrs {
split := strings.Split(field, ":")
if len(split) == 1 {
continue
}
key, val := split[0], split[1]
mapAttrs[key] = val
}
devices = append(devices, Device{adbClient: c, serial: fields[0], attrs: mapAttrs})
}
return
}
func (c Client) ForwardList() (deviceForward []DeviceForward, err error) {
var resp string
if resp, err = c.executeCommand("host:list-forward"); err != nil {
return nil, err
}
lines := strings.Split(resp, "\n")
deviceForward = make([]DeviceForward, 0, len(lines))
for i := range lines {
line := strings.TrimSpace(lines[i])
if line == "" {
continue
}
fields := strings.Fields(line)
deviceForward = append(deviceForward, DeviceForward{Serial: fields[0], Local: fields[1], Remote: fields[2]})
}
return
}
func (c Client) ForwardKillAll() (err error) {
_, err = c.executeCommand("host:killforward-all", true)
return
}
func (c Client) Connect(ip string, port ...int) (err error) {
if len(port) == 0 {
port = []int{AdbDaemonPort}
}
var resp string
if resp, err = c.executeCommand(fmt.Sprintf("host:connect:%s:%d", ip, port[0])); err != nil {
return err
}
if !strings.HasPrefix(resp, "connected to") && !strings.HasPrefix(resp, "already connected to") {
return fmt.Errorf("adb connect: %s", resp)
}
return
}
func (c Client) Disconnect(ip string, port ...int) (err error) {
cmd := fmt.Sprintf("host:disconnect:%s", ip)
if len(port) != 0 {
cmd = fmt.Sprintf("host:disconnect:%s:%d", ip, port[0])
}
var resp string
if resp, err = c.executeCommand(cmd); err != nil {
return err
}
if !strings.HasPrefix(resp, "disconnected") {
return fmt.Errorf("adb disconnect: %s", resp)
}
return
}
func (c Client) DisconnectAll() (err error) {
var resp string
if resp, err = c.executeCommand("host:disconnect:"); err != nil {
return err
}
if !strings.HasPrefix(resp, "disconnected everything") {
return fmt.Errorf("adb disconnect all: %s", resp)
}
return
}
func (c Client) KillServer() (err error) {
var tp transport
if tp, err = c.createTransport(); err != nil {
return err
}
defer func() { _ = tp.Close() }()
err = tp.Send("host:kill")
return
}
func (c Client) createTransport() (tp transport, err error) {
return newTransport(fmt.Sprintf("%s:%d", c.host, c.port))
}
func (c Client) executeCommand(command string, onlyVerifyResponse ...bool) (resp string, err error) {
if len(onlyVerifyResponse) == 0 {
onlyVerifyResponse = []bool{false}
}
var tp transport
if tp, err = c.createTransport(); err != nil {
return "", err
}
defer func() { _ = tp.Close() }()
if err = tp.Send(command); err != nil {
return "", err
}
if err = tp.VerifyResponse(); err != nil {
return "", err
}
if onlyVerifyResponse[0] {
return
}
if resp, err = tp.UnpackString(); err != nil {
return "", err
}
return
}