-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathsearch_linux.go
62 lines (56 loc) · 1.49 KB
/
search_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//go:build linux
// +build linux
package serial
import (
"os"
"path/filepath"
"strings"
"go.viam.com/utils"
"go.viam.com/utils/usb"
)
func searchUSB(filter SearchFilter) []Description {
usbDevices := usb.Search(
usb.SearchFilter{},
func(vendorID, productID int) bool {
return checkProductDeviceIDs(vendorID, productID) != TypeUnknown
})
serialDeviceDescs := make([]Description, 0, len(usbDevices))
for _, dev := range usbDevices {
devType := checkProductDeviceIDs(dev.ID.Vendor, dev.ID.Product)
if filter.Type != "" && filter.Type != devType {
continue
}
serialDeviceDescs = append(serialDeviceDescs, Description{
Type: devType,
Path: dev.Path,
})
}
return serialDeviceDescs
}
var devPath = "/dev"
// Search uses linux device APIs to find all applicable serial devices.
// It's a variable in case you need to override it during tests.
var Search = func(filter SearchFilter) []Description {
serialDeviceDescs := searchUSB(filter)
if filter.Type != "" && filter.Type != TypeJetson {
return serialDeviceDescs
}
devicesDir, err := os.Open(devPath)
if err != nil {
return serialDeviceDescs
}
defer utils.UncheckedErrorFunc(devicesDir.Close)
devices, err := devicesDir.Readdir(0)
if err != nil {
return serialDeviceDescs
}
for _, dev := range devices {
if strings.HasPrefix(dev.Name(), "ttyTHS") {
serialDeviceDescs = append(serialDeviceDescs, Description{
Type: TypeJetson,
Path: filepath.Join(devPath, dev.Name()),
})
}
}
return serialDeviceDescs
}