@@ -11,6 +11,8 @@ import (
1111 "strconv"
1212 "strings"
1313 "syscall"
14+
15+ shellquote "github.com/kballard/go-shellquote"
1416)
1517
1618const (
@@ -65,11 +67,13 @@ type AdbConnection struct {
6567 net.Conn
6668}
6769
68- // SendPacket data is like "000chost:version"
69- func (conn * AdbConnection ) SendPacket (data string ) error {
70+ func (conn * AdbConnection ) WritePacket (data string ) error {
7071 pktData := fmt .Sprintf ("%04x%s" , len (data ), data )
7172 _ , err := conn .Write ([]byte (pktData ))
72- return err
73+ if err != nil {
74+ return err
75+ }
76+ return conn .respCheck ()
7377}
7478
7579func (conn * AdbConnection ) readN (n int ) (v string , err error ) {
@@ -84,6 +88,9 @@ func (conn *AdbConnection) readN(n int) (v string, err error) {
8488// respCheck check OKAY, or FAIL
8589func (conn * AdbConnection ) respCheck () error {
8690 status , err := conn .readN (4 )
91+ if err != nil {
92+ return err
93+ }
8794 switch status {
8895 case _OKAY :
8996 return nil
@@ -94,7 +101,7 @@ func (conn *AdbConnection) respCheck() error {
94101 }
95102 return errors .New (data )
96103 default :
97- return fmt .Errorf ("Unknown status : %s, should be OKAY or FAIL" , strconv .Quote (stat ))
104+ return fmt .Errorf ("Unexpected response : %s, should be OKAY or FAIL" , strconv .Quote (status ))
98105 }
99106}
100107
@@ -111,27 +118,6 @@ func (conn *AdbConnection) readString() (string, error) {
111118 return conn .readN (length )
112119}
113120
114- // RecvPacket receive data like "OKAY00040028"
115- func (conn * AdbConnection ) RecvPacket () (data string , err error ) {
116- stat , err := conn .readN (4 )
117- if err != nil {
118- return "" , err
119- }
120- switch stat {
121- case _OKAY :
122- return conn .readString ()
123- case _FAIL :
124- data , err = conn .readString ()
125- if err != nil {
126- return
127- }
128- err = errors .New (data )
129- return
130- default :
131- return "" , fmt .Errorf ("Unknown stat: %s" , strconv .Quote (stat ))
132- }
133- }
134-
135121type AdbClient struct {
136122 Addr string
137123}
@@ -154,51 +140,7 @@ func (c *AdbClient) newConnection() (conn *AdbConnection, err error) {
154140 return & AdbConnection {netConn }, nil
155141}
156142
157- func (c * AdbClient ) sendTwoWay (data string ) (string , error ) {
158- if _ , err := c .Version (); err != nil {
159- return "" , err
160- }
161- conn , err := c .newConnection ()
162- if err != nil {
163- return "" , err
164- }
165- defer conn .Close ()
166- if err := conn .SendPacket (data ); err != nil {
167- return "" , err
168- }
169- return conn .RecvPacket ()
170- }
171-
172- func (c * AdbClient ) sendOneWay (data string ) error {
173- if _ , err := c .Version (); err != nil {
174- return err
175- }
176- conn , err := c .newConnection ()
177- if err != nil {
178- return err
179- }
180- defer conn .Close ()
181- if err := conn .SendPacket (data ); err != nil {
182- return err
183- }
184- status , err := conn .readN (4 )
185- if err != nil {
186- return err
187- }
188- switch status {
189- case _OKAY :
190- return nil
191- case _FAIL :
192- message , err := conn .readString ()
193- if err != nil {
194- return err
195- }
196- return errors .New (message )
197- default :
198- return errors .New ("Invalid status: " + status )
199- }
200- }
201-
143+ // Version return 4 size string
202144func (c * AdbClient ) Version () (string , error ) {
203145 ver , err := c .rawVersion ()
204146 if err == nil {
@@ -215,10 +157,10 @@ func (c *AdbClient) rawVersion() (string, error) {
215157 return "" , err
216158 }
217159 defer conn .Close ()
218- if err := conn .SendPacket ("host:version" ); err != nil {
160+ if err := conn .WritePacket ("host:version" ); err != nil {
219161 return "" , err
220162 }
221- return conn .RecvPacket ()
163+ return conn .readString ()
222164}
223165
224166type AdbDevice struct {
@@ -233,60 +175,29 @@ func (c *AdbClient) DeviceWithSerial(serial string) *AdbDevice {
233175 }
234176}
235177
236- func (d * AdbDevice ) deviceSelector () string {
237- if d .Serial == "" {
238- return "host:transport-any"
239- }
240- return "host-serial:" + d .Serial
241- }
242-
243- func (d * AdbDevice ) transportSelector () string {
244- if d .Serial == "" {
245- return "host:transport-any"
246- }
247- return "host:transport:" + d .Serial
248- }
249-
250- func (d * AdbDevice ) check () error {
251- _ , err := d .sendTwoWay (d .deviceSelector () + ":features" )
252- return err
253- }
254-
255- type TCPCmd struct {
256- Cmd string
257- NeedResponse bool
258- }
259-
260- func (c * AdbClient ) Shell (args ... string ) (output string , exitCode int , err error ) {
178+ func (c * AdbDevice ) openCommand (cmd string ) (reader io.ReadCloser , err error ) {
261179 conn , err := c .newConnection ()
262180 if err != nil {
263181 return
264182 }
265- defer conn .Close ()
266- // if err = conn.SendPacket("host:features"); err != nil {
267- // return
268- // }
269- // _, err = conn.RecvPacket()
270- // if err != nil {
271- // return
272- // }
273- if err = conn .SendPacket ("host:transport-any" ); err != nil {
183+ err = conn .WritePacket ("host:transport:" + c .Serial )
184+ if err != nil {
274185 return
275186 }
276- ok , _ := conn .Okay ()
277- if ! ok {
278- err = fmt .Errorf ("shell connection broken" )
279- }
280- err = conn .SendPacket ("shell:" + shellquote .Join (args ... ) + " ; echo :$?" )
187+ err = conn .WritePacket ("shell:" + cmd ) //shellquote.Join(args...)) // + " ; echo :$?")
281188 if err != nil {
282189 return
283190 }
284- ok , _ = conn .Okay ()
285- if ! ok {
286- err = fmt .Errorf ("shell connection broken" )
287- }
288- buf := bytes .NewBuffer (nil )
289- io .Copy (buf , conn )
290- output = string (buf .Bytes ())
291- return
191+ return conn , nil
192+ }
193+
194+ func (c * AdbDevice ) OpenShell (args ... string ) (reader io.ReadCloser , err error ) {
195+ return c .openCommand (shellquote .Join (args ... ))
292196}
197+
198+ // func (c *AdbDevice) RunShell(args ...string) (exitCode int, err error) {
199+ // reader, err := c.OpenShell(args...)
200+ // if err != nil {
201+ // return
202+ // }
203+ // }
0 commit comments