Skip to content

Commit a1b42a3

Browse files
committed
wip
1 parent cd29de0 commit a1b42a3

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

pkg/board/board_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package board
1717

1818
import (
19+
"fmt"
1920
"os"
2021
"testing"
2122

@@ -31,3 +32,28 @@ func TestEnsurePlatformInstalled(t *testing.T) {
3132
err := EnsurePlatformInstalled(t.Context(), "arduino:zephyr:unoq")
3233
require.NoError(t, err)
3334
}
35+
36+
func TestAdbKillIssue(t *testing.T) {
37+
var (
38+
boards []Board
39+
err error
40+
)
41+
for range 5 {
42+
boards, err = FromFQBN(t.Context(), "arduino:zephyr:unoq")
43+
require.NoError(t, err)
44+
}
45+
46+
for _, b := range boards {
47+
fmt.Printf("Board: %s %s %s %s\n", b.BoardName, b.Protocol, b.Address, b.Serial)
48+
}
49+
selectedBoard := boards[0]
50+
51+
conn, err := selectedBoard.GetConnection()
52+
require.NoError(t, err)
53+
54+
files, err := conn.List("/home/arduino")
55+
require.NoError(t, err)
56+
for _, file := range files {
57+
fmt.Printf("File: %s\n", file.Name)
58+
}
59+
}

pkg/board/remote/adb/adb.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,18 @@ func FromSerial(serial string, adbPath string) (*ADBConnection, error) {
5151
adbPath = FindAdbPath()
5252
}
5353

54-
return &ADBConnection{
54+
conn := &ADBConnection{
5555
host: serial,
5656
adbPath: adbPath,
57-
}, nil
57+
}
58+
59+
if connected, err := conn.IsConnected(); err != nil {
60+
return nil, err
61+
} else if !connected {
62+
return nil, fmt.Errorf("device %s is not connected", serial)
63+
}
64+
65+
return conn, nil
5866
}
5967

6068
func FromHost(host string, adbPath string) (*ADBConnection, error) {
@@ -71,6 +79,23 @@ func FromHost(host string, adbPath string) (*ADBConnection, error) {
7179
return FromSerial(host, adbPath)
7280
}
7381

82+
func (a *ADBConnection) IsConnected() (bool, error) {
83+
cmd, err := paths.NewProcess(nil, a.adbPath, "-s", a.host, "get-state")
84+
if err != nil {
85+
return false, fmt.Errorf("failed to create ADB command: %w", err)
86+
}
87+
88+
output, err := cmd.RunAndCaptureCombinedOutput(context.TODO())
89+
if err != nil {
90+
if strings.Contains(string(output), "device offline") {
91+
return false, nil
92+
}
93+
return false, fmt.Errorf("failed to get ADB device state: %w: %s", err, output)
94+
}
95+
96+
return string(bytes.TrimSpace(output)) == "device", nil
97+
}
98+
7499
func (a *ADBConnection) Forward(ctx context.Context, localPort int, remotePort int) error {
75100
if !ports.IsAvailable(localPort) {
76101
return remote.ErrPortAvailable

0 commit comments

Comments
 (0)