Skip to content

Commit

Permalink
Merge pull request #152 from cmaglie/enumerator_improvements
Browse files Browse the repository at this point in the history
Retry port enumeration 5 times before giving up (macosx)
  • Loading branch information
cmaglie authored Jan 2, 2023
2 parents bc6a7f7 + 376ad6b commit f85edb4
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions enumerator/usb_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "C"
import (
"errors"
"fmt"
"time"
"unsafe"
)

Expand All @@ -37,12 +38,20 @@ func nativeGetDetailedPortsList() ([]*PortDetails, error) {
}

func extractPortInfo(service C.io_registry_entry_t) (*PortDetails, error) {
name, err := service.GetStringProperty("IOCalloutDevice")
if err != nil {
return nil, fmt.Errorf("Error extracting port info from device: %s", err.Error())
}
port := &PortDetails{}
port.Name = name
// If called too early the port may still not be ready or fully enumerated
// so we retry 5 times before returning error.
for retries := 5; retries > 0; retries-- {
name, err := service.GetStringProperty("IOCalloutDevice")
if err == nil {
port.Name = name
break
}
if retries == 0 {
return nil, fmt.Errorf("error extracting port info from device: %w", err)
}
time.Sleep(50 * time.Millisecond)
}
port.IsUSB = false

validUSBDeviceClass := map[string]bool{
Expand Down

0 comments on commit f85edb4

Please sign in to comment.