@@ -36,14 +36,6 @@ import (
3636 "github.com/arduino/arduino-app-cli/pkg/x/ports"
3737)
3838
39- var (
40- // NotFoundErr is returned when the ADB device is not found.
41- NotFoundErr = fmt .Errorf ("ADB device not found" )
42- // DeviceOfflineErr is returned when the ADB device is not reachable.
43- // This usually requires a restart of the adbd server daemon on the device.
44- DeviceOfflineErr = fmt .Errorf ("ADB device is offline" )
45- )
46-
4739type ADBConnection struct {
4840 adbPath string
4941 host string
@@ -54,19 +46,49 @@ var _ remote.RemoteConn = (*ADBConnection)(nil)
5446
5547const username = "arduino"
5648
49+ var (
50+ // NotFoundErr is returned when the ADB device is not found.
51+ NotFoundErr = fmt .Errorf ("ADB device not found" )
52+ // DeviceOfflineErr is returned when the ADB device is not reachable.
53+ // This usually requires a restart of the adbd server daemon on the device.
54+ DeviceOfflineErr = fmt .Errorf ("ADB device is offline" )
55+ )
56+
57+ // FromSerial creates an ADBConnection from a device serial number.
58+ // returns an error NotFoundErr if the device is not found, and DeviceOfflineErr if the device is offline.
5759func FromSerial (serial string , adbPath string ) (* ADBConnection , error ) {
5860 if adbPath == "" {
5961 adbPath = FindAdbPath ()
6062 }
6163
62- conn := ADBConnection {host : serial , adbPath : adbPath }
63- if connected , err := conn .IsConnected (); err != nil {
64+ isConnected := func (serial , adbPath string ) (bool , error ) {
65+ cmd , err := paths .NewProcess (nil , adbPath , "-s" , serial , "get-state" )
66+ if err != nil {
67+ return false , fmt .Errorf ("failed to create ADB command: %w" , err )
68+ }
69+
70+ output , err := cmd .RunAndCaptureCombinedOutput (context .TODO ())
71+ if err != nil {
72+ if bytes .Contains (output , []byte ("device offline" )) {
73+ return false , DeviceOfflineErr
74+ } else if bytes .Contains (output , []byte ("not found" )) {
75+ return false , NotFoundErr
76+ }
77+ return false , fmt .Errorf ("failed to get ADB device state: %w: %s" , err , output )
78+ }
79+
80+ return string (bytes .TrimSpace (output )) == "device" , nil
81+ }
82+ if connected , err := isConnected (adbPath , serial ); err != nil {
6483 return nil , err
6584 } else if ! connected {
6685 return nil , fmt .Errorf ("device %s is not connected" , serial )
6786 }
6887
69- return & conn , nil
88+ return & ADBConnection {
89+ adbPath : adbPath ,
90+ host : serial ,
91+ }, nil
7092}
7193
7294func FromHost (host string , adbPath string ) (* ADBConnection , error ) {
@@ -77,32 +99,12 @@ func FromHost(host string, adbPath string) (*ADBConnection, error) {
7799 if err != nil {
78100 return nil , err
79101 }
80- if err := cmd .Run ( ); err != nil {
81- return nil , fmt .Errorf ("failed to connect to ADB host %s: %w" , host , err )
102+ if out , err := cmd .RunAndCaptureCombinedOutput ( context . TODO () ); err != nil {
103+ return nil , fmt .Errorf ("failed to connect to ADB host %s: %w: %s " , host , err , out )
82104 }
83105 return FromSerial (host , adbPath )
84106}
85107
86- // IsConnected checks if the ADB device is connected and online.
87- func (a * ADBConnection ) IsConnected () (bool , error ) {
88- cmd , err := paths .NewProcess (nil , a .adbPath , "-s" , a .host , "get-state" )
89- if err != nil {
90- return false , fmt .Errorf ("failed to create ADB command: %w" , err )
91- }
92-
93- output , err := cmd .RunAndCaptureCombinedOutput (context .TODO ())
94- if err != nil {
95- if bytes .Contains (output , []byte ("device offline" )) {
96- return false , DeviceOfflineErr
97- } else if bytes .Contains (output , []byte ("not found" )) {
98- return false , NotFoundErr
99- }
100- return false , fmt .Errorf ("failed to get ADB device state: %w: %s" , err , output )
101- }
102-
103- return string (bytes .TrimSpace (output )) == "device" , nil
104- }
105-
106108func (a * ADBConnection ) Forward (ctx context.Context , localPort int , remotePort int ) error {
107109 if ! ports .IsAvailable (localPort ) {
108110 return remote .ErrPortAvailable
0 commit comments