-
Notifications
You must be signed in to change notification settings - Fork 6
fix(pkg/board): handle adb connection error #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lucarin91
merged 8 commits into
arduino:main
from
lucarin91:handler-adb-connection-issues
Nov 25, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7cb1de5
fix(pkg/board): handle adb connection error
lucarin91 02af799
add a warning
lucarin91 8fd8e18
don't expose the function
lucarin91 4a77c68
rename errors
lucarin91 e3aa26f
fixup! rename errors
lucarin91 368e3a8
fixup! don't expose the function
lucarin91 ac49ee9
implement code review suggestions
lucarin91 eaf16c1
nit
lucarin91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,14 +46,50 @@ type ADBConnection struct { | |
| // Ensures ADBConnection implements the RemoteConn interface at compile time. | ||
| var _ remote.RemoteConn = (*ADBConnection)(nil) | ||
|
|
||
| var ( | ||
| // ErrNotFound is returned when the ADB device is not found. | ||
| ErrNotFound = fmt.Errorf("ADB device not found") | ||
| // ErrDeviceOffline is returned when the ADB device is not reachable. | ||
| // This usually requires a restart of the adbd server daemon on the device. | ||
| ErrDeviceOffline = fmt.Errorf("ADB device is offline") | ||
| ) | ||
|
|
||
| // FromSerial creates an ADBConnection from a device serial number. | ||
| // returns an error NotFoundErr if the device is not found, and DeviceOfflineErr if the device is offline. | ||
| func FromSerial(serial string, adbPath string) (*ADBConnection, error) { | ||
| if adbPath == "" { | ||
| adbPath = FindAdbPath() | ||
| } | ||
|
|
||
| isConnected := func(serial, adbPath string) (bool, error) { | ||
| cmd, err := paths.NewProcess(nil, adbPath, "-s", serial, "get-state") | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to create ADB command: %w", err) | ||
| } | ||
|
|
||
| output, err := cmd.RunAndCaptureCombinedOutput(context.TODO()) | ||
mirkoCrobu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| slog.Error("unable to connect to ADB device", "error", err, "output", string(output), "serial", serial) | ||
| if bytes.Contains(output, []byte("device offline")) { | ||
| return false, ErrDeviceOffline | ||
| } else if bytes.Contains(output, []byte("not found")) { | ||
| return false, ErrNotFound | ||
lucarin91 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return false, fmt.Errorf("failed to get ADB device state: %w: %s", err, output) | ||
| } | ||
|
|
||
| return string(bytes.TrimSpace(output)) == "device", nil | ||
| } | ||
|
|
||
| if connected, err := isConnected(serial, adbPath); err != nil { | ||
| return nil, err | ||
| } else if !connected { | ||
| return nil, fmt.Errorf("device %s is not connected", serial) | ||
| } | ||
|
|
||
| return &ADBConnection{ | ||
| host: serial, | ||
| adbPath: adbPath, | ||
| host: serial, | ||
| }, nil | ||
| } | ||
|
|
||
|
|
@@ -65,8 +101,8 @@ func FromHost(host string, adbPath string) (*ADBConnection, error) { | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := cmd.Run(); err != nil { | ||
| return nil, fmt.Errorf("failed to connect to ADB host %s: %w", host, err) | ||
| if out, err := cmd.RunAndCaptureCombinedOutput(context.TODO()); err != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not context.Background()?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's exactly the same, but TODO, remember to add it in the future. |
||
| return nil, fmt.Errorf("failed to connect to ADB host %s: %w: %s", host, err, out) | ||
| } | ||
| return FromSerial(host, adbPath) | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.