Skip to content

Commit 42cb563

Browse files
committed
WIP: agent reports vid/pid instead of name lookup
1 parent 0d7d028 commit 42cb563

File tree

5 files changed

+56
-79
lines changed

5 files changed

+56
-79
lines changed

Diff for: serial.go

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ type SpPortItem struct {
101101
BufferAlgorithm string
102102
Ver string
103103
NetworkPort bool
104+
VendorID string
105+
ProductID string
104106
}
105107

106108
// SerialPorts contains the ports attached to the machine
@@ -526,6 +528,8 @@ func spListDual(network bool) {
526528
BufferAlgorithm: "",
527529
Ver: version,
528530
NetworkPort: item.NetworkPort,
531+
VendorID: item.IdVendor,
532+
ProductID: item.IdProduct,
529533
}
530534

531535
// figure out if port is open

Diff for: seriallist.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func GetList(network bool) ([]OsSerialPort, error) {
5959
arrPorts = newarrPorts
6060
}
6161

62-
arrPorts = removeNonArduinoBoards(arrPorts)
62+
arrPorts = associateVidPidWithPort(arrPorts)
6363
return arrPorts, err
6464
//log.Printf("Done doing GetList(). arrPorts:%v\n", arrPorts)
6565
}

Diff for: seriallist_darwin.go

+32-33
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
//"syscall"
1212
//"fmt"
1313
//"bufio"
14+
"fmt"
1415
"io/ioutil"
1516
"os/exec"
1617
)
@@ -23,52 +24,50 @@ import (
2324
// search all board.txt files for map[Product ID]
2425
// assign it to name
2526

26-
func removeNonArduinoBoards(ports []OsSerialPort) []OsSerialPort {
27-
usbcmd := exec.Command("system_profiler", "SPUSBDataType")
28-
grepcmd := exec.Command("grep", "0x2341", "-A5", "-B1")
27+
func associateVidPidWithPort(ports []OsSerialPort) []OsSerialPort {
2928

30-
cmdOutput, _ := pipe_commands(usbcmd, grepcmd)
29+
// prefilter ports
30+
ports = Filter(ports, func(port OsSerialPort) bool {
31+
return !strings.Contains(port.Name, "Blue") && !strings.Contains(port.Name, "/cu")
32+
})
3133

32-
//log.Println(string(cmdOutput))
33-
cmdOutSlice := strings.Split(string(cmdOutput), "\n")
34+
for index, _ := range ports {
35+
port_hash := strings.Trim(ports[index].Name, "/dev/tty.usbmodem")
3436

35-
// how many lines is the output? boards attached = lines/8
36-
for i := 0; i < len(cmdOutSlice)/8; i++ {
37+
usbcmd := exec.Command("system_profiler", "SPUSBDataType")
38+
grepcmd := exec.Command("grep", "Location ID: 0x"+port_hash[:len(port_hash)-1], "-B6")
39+
cmdOutput, _ := pipe_commands(usbcmd, grepcmd)
3740

38-
cmdOutSliceN := cmdOutSlice[i*8 : (i+1)*8]
41+
if len(cmdOutput) == 0 {
42+
usbcmd = exec.Command("system_profiler", "SPUSBDataType")
43+
grepcmd = exec.Command("grep" /*"Serial Number: "+*/, strings.Trim(port_hash, "0"), "-B3", "-A3")
44+
cmdOutput, _ = pipe_commands(usbcmd, grepcmd)
3945

40-
cmdOutMap := make(map[string]string)
46+
fmt.Println(string(cmdOutput))
47+
}
4148

42-
for _, element := range cmdOutSliceN {
43-
if strings.Contains(element, "ID") {
44-
element = strings.TrimSpace(element)
45-
arr := strings.Split(element, ": ")
46-
cmdOutMap[arr[0]] = arr[1]
47-
}
49+
if len(cmdOutput) == 0 {
50+
//give up
51+
continue
4852
}
4953

50-
archBoardName, boardName, _ := getBoardName(cmdOutMap["Product ID"])
54+
cmdOutSlice := strings.Split(string(cmdOutput), "\n")
5155

52-
// remove initial 0x and final zeros
53-
ttyHeader := strings.Trim((cmdOutMap["Location ID"]), "0x")
54-
ttyHeader = strings.Split(ttyHeader, " ")[0]
55-
ttyHeader = strings.Trim(ttyHeader, "0")
56+
fmt.Println(cmdOutSlice)
5657

57-
for _, port := range ports {
58-
if strings.Contains(port.Name, ttyHeader) {
59-
if !strings.Contains(port.Name, "/cu") {
60-
port.RelatedNames = append(port.RelatedNames, archBoardName)
61-
port.FriendlyName = strings.Trim(boardName, "\n")
62-
}
58+
cmdOutMap := make(map[string]string)
59+
60+
for _, element := range cmdOutSlice {
61+
if strings.Contains(element, "ID") || strings.Contains(element, "Manufacturer") {
62+
element = strings.TrimSpace(element)
63+
arr := strings.Split(element, ": ")
64+
cmdOutMap[arr[0]] = arr[1]
6365
}
6466
}
67+
ports[index].IdProduct = strings.Split(cmdOutMap["Product ID"], " ")[0]
68+
ports[index].IdVendor = strings.Split(cmdOutMap["Vendor ID"], " ")[0]
69+
ports[index].Manufacturer = cmdOutMap["Manufacturer"]
6570
}
66-
67-
// additional remove phase
68-
ports = Filter(ports, func(port OsSerialPort) bool {
69-
return !strings.Contains(port.Name, "Blue") && !strings.Contains(port.Name, "/cu")
70-
})
71-
7271
return ports
7372
}
7473

Diff for: seriallist_linux.go

+16-37
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package main
22

33
import (
4-
//"fmt"
4+
"fmt"
55
//"github.com/tarm/goserial"
66
//"log"
77
"os"
88
"os/exec"
99
"strings"
1010
//"encoding/binary"
11-
//"strconv"
11+
"strconv"
1212
//"syscall"
1313
//"fmt"
14-
//"io"
1514
"bytes"
1615
log "github.com/Sirupsen/logrus"
1716
"io/ioutil"
@@ -20,48 +19,28 @@ import (
2019
"sort"
2120
)
2221

23-
func removeNonArduinoBoards(ports []OsSerialPort) []OsSerialPort {
24-
usbcmd := exec.Command("lsusb", "-vvv")
25-
grepcmd := exec.Command("grep", "0x2341", "-A1")
26-
grep2cmd := exec.Command("grep", "idProduct")
27-
//awkcmd := exec.Command("awk", "\'{print $2}\'")
28-
//awkcmd := exec.Command("grep", "-E", "-o", "'0x[[:alnum:]]{4}'")
29-
30-
cmdOutput, _ := pipe_commands(usbcmd, grepcmd, grep2cmd)
31-
32-
cmdOutSliceT := strings.Split(string(cmdOutput), "\n")
22+
func associateVidPidWithPort(ports []OsSerialPort) []OsSerialPort {
3323

34-
re := regexp.MustCompile("0x[[:alnum:]]{4}")
35-
36-
var cmdOutSlice []string
37-
38-
for _, element := range cmdOutSliceT {
39-
cmdOutSlice = append(cmdOutSlice, re.FindString(element))
40-
}
24+
for index, _ := range ports {
25+
ueventcmd := exec.Command("cat", "/sys/class/tty/"+filepath.Base(ports[index].Name)+"/device/uevent")
26+
grep3cmd := exec.Command("grep", "PRODUCT=")
4127

42-
for _, element := range cmdOutSlice {
28+
cmdOutput2, _ := pipe_commands(ueventcmd, grep3cmd)
29+
cmdOutput2S := string(cmdOutput2)
4330

44-
if element == "" {
45-
break
31+
if len(cmdOutput2S) == 0 {
32+
continue
4633
}
4734

48-
archBoardName, boardName, _ := getBoardName(element)
49-
50-
for _, port := range ports {
51-
ueventcmd := exec.Command("cat", "/sys/class/tty/"+filepath.Base(port.Name)+"/device/uevent")
52-
grep3cmd := exec.Command("grep", "PRODUCT=")
53-
cutcmd := exec.Command("cut", "-f2", "-d/")
35+
infos := strings.Split(cmdOutput2S, "=")
5436

55-
cmdOutput2, _ := pipe_commands(ueventcmd, grep3cmd, cutcmd)
56-
cmdOutput2S := string(cmdOutput2)
37+
vid_pid := strings.Split(infos[1], "/")
5738

58-
if strings.Contains(element, strings.Trim(cmdOutput2S, "\n")) && cmdOutput2S != "" {
59-
port.RelatedNames = append(port.RelatedNames, archBoardName)
60-
port.FriendlyName = strings.Trim(boardName, "\n")
61-
}
62-
}
39+
vid, _ := strconv.ParseInt(vid_pid[0], 16, 32)
40+
pid, _ := strconv.ParseInt(vid_pid[1], 16, 32)
41+
ports[index].IdVendor = fmt.Sprintf("0x%04x", vid)
42+
ports[index].IdProduct = fmt.Sprintf("0x%04x", pid)
6343
}
64-
6544
return ports
6645
}
6746

Diff for: seriallist_windows.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
serialListWindowsWg sync.WaitGroup
2323
)
2424

25-
func removeNonArduinoBoards(ports []OsSerialPort) []OsSerialPort {
25+
func associateVidPidWithPort(ports []OsSerialPort) []OsSerialPort {
2626
ports, _ = getList()
2727
return ports
2828
}
@@ -206,13 +206,8 @@ func getListViaWmiPnpEntity() ([]OsSerialPort, os.SyscallError) {
206206
}
207207
}
208208

209-
if list[i].IdVendor != "2341" {
210-
list[i].FriendlyName = asString.ToString()
211-
} else {
212-
archBoardName, boardName, _ := getBoardName("0x" + list[i].IdProduct)
213-
list[i].RelatedNames = append(list[i].RelatedNames, archBoardName)
214-
list[i].FriendlyName = strings.Trim(boardName, "\n")
215-
}
209+
list[i].IdVendor = "0x" + list[i].IdVendor
210+
list[i].IdProduct = "0x" + list[i].IdProduct
216211

217212
manufStr, _ := oleutil.GetProperty(item, "Manufacturer")
218213
list[i].Manufacturer = manufStr.ToString()

0 commit comments

Comments
 (0)