4b9891533a
Milestone: the demo app prints /proc/loadavg from the device.
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
package wire
|
|
|
|
const (
|
|
// The official implementation of adb imposes an undocumented 255-byte limit
|
|
// on messages.
|
|
MaxMessageLength = 255
|
|
)
|
|
|
|
/*
|
|
Conn is a normal connection to an adb server.
|
|
|
|
For most cases, usage looks something like:
|
|
conn := wire.Dial()
|
|
conn.SendMessage(data)
|
|
conn.ReadStatus() == StatusSuccess || StatusFailure
|
|
conn.ReadMessage()
|
|
conn.Close()
|
|
|
|
For some messages, the server will return more than one message (but still a single
|
|
status). Generally, after calling ReadStatus once, you should call ReadMessage until
|
|
it returns an io.EOF error.
|
|
|
|
For most commands, the server will close the connection after sending the response.
|
|
You should still always call Close() when you're done with the connection.
|
|
*/
|
|
type Conn struct {
|
|
Scanner
|
|
Sender
|
|
closer func() error
|
|
}
|
|
|
|
func NewConn(scanner Scanner, sender Sender, closer func() error) *Conn {
|
|
return &Conn{scanner, sender, closer}
|
|
}
|
|
|
|
// Close closes the underlying connection.
|
|
func (c *Conn) Close() error {
|
|
if c.closer != nil {
|
|
return c.closer()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// NewSyncConn returns connection that can operate in sync mode.
|
|
// The connection must already have been switched (by sending the sync command
|
|
// to a specific device), or the return connection will return an error.
|
|
func (c *Conn) NewSyncConn() *SyncConn {
|
|
return &SyncConn{
|
|
SyncScanner: c.Scanner.NewSyncScanner(),
|
|
SyncSender: c.Sender.NewSyncSender(),
|
|
}
|
|
}
|
|
|
|
// RoundTripSingleResponse sends a message to the server, and reads a single
|
|
// message response. If the reponse has a failure status code, returns it as an error.
|
|
func (conn *Conn) RoundTripSingleResponse(req []byte) (resp []byte, err error) {
|
|
if err = conn.SendMessage(req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err = ReadStatusFailureAsError(conn, req); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return conn.ReadMessage()
|
|
}
|