forked from arduino/arduino-create-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseriallist_darwin.go
70 lines (56 loc) · 1.92 KB
/
seriallist_darwin.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
63
64
65
66
67
68
69
70
package main
import (
"os/exec"
"strings"
)
// execute system_profiler SPUSBDataType | grep "Vendor ID: 0x2341" -A5 -B2
// maybe -B2 is not necessary
// trim whitespace and eol
// map everything with :
// get [map][Location ID] first 5 chars
// search all board.txt files for map[Product ID]
// assign it to name
func associateVidPidWithPort(ports []OsSerialPort) []OsSerialPort {
// prefilter ports
ports = Filter(ports, func(port OsSerialPort) bool {
return !strings.Contains(port.Name, "Blue") && !strings.Contains(port.Name, "/cu")
})
for index, _ := range ports {
port_hash := strings.Trim(ports[index].Name, "/dev/tty.usbmodem")
port_hash = strings.Trim(port_hash, "/dev/tty.usbserial-")
port_hash = strings.ToLower(port_hash)
usbcmd := exec.Command("system_profiler", "SPUSBDataType")
grepcmd := exec.Command("grep", "Location ID: 0x"+port_hash[:len(port_hash)-1], "-B6")
cmdOutput, _ := pipe_commands(usbcmd, grepcmd)
if len(cmdOutput) == 0 {
usbcmd = exec.Command("system_profiler", "SPUSBDataType")
grepcmd = exec.Command("grep" /*"Serial Number: "+*/, strings.Trim(port_hash, "0"), "-B3", "-A3")
cmdOutput, _ = pipe_commands(usbcmd, grepcmd)
}
if len(cmdOutput) == 0 {
//give up
continue
}
cmdOutSlice := strings.Split(string(cmdOutput), "\n")
cmdOutMap := make(map[string]string)
for _, element := range cmdOutSlice {
if strings.Contains(element, "ID") || strings.Contains(element, "Manufacturer") {
element = strings.TrimSpace(element)
arr := strings.Split(element, ": ")
if len(arr) > 1 {
cmdOutMap[arr[0]] = arr[1]
} else {
cmdOutMap[arr[0]] = ""
}
}
}
ports[index].IdProduct = strings.Split(cmdOutMap["Product ID"], " ")[0]
ports[index].IdVendor = strings.Split(cmdOutMap["Vendor ID"], " ")[0]
ports[index].Manufacturer = cmdOutMap["Manufacturer"]
}
return ports
}
func hideFile(path string) {
}
func tellCommandNotToSpawnShell(_ *exec.Cmd) {
}