aboutsummaryrefslogtreecommitdiff
path: root/transport/serial/usb.go
blob: b341321fe6f7647c209e1556bb0da16436b97ebe (plain)
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
package serial

import (
	"fmt"
	"github.com/charmbracelet/log"
	"go.bug.st/serial/enumerator"
)

type usbDevice struct {
	VID string
	PID string
}

var knownDevices = []usbDevice{
	{VID: "239A", PID: "8029"}, // rak4631_19003
}

func GetPorts() []string {
	ports, err := enumerator.GetDetailedPortsList()
	if err != nil {
		log.Fatal(err)
	}
	var foundDevices []string
	if len(ports) == 0 {
		fmt.Println("No serial ports found!")
		return nil
	}
	for _, port := range ports {
		//fmt.Printf("Found port: %s\n", port.SettingName)
		if port.IsUSB {
			for _, device := range knownDevices {
				if device.VID != port.VID {
					continue
				}
				if device.PID != port.PID {
					continue
				}
				foundDevices = append(foundDevices, port.Name)
			}
		}
	}
	return foundDevices
}