Skip to content

Commit dd7e1c4

Browse files
committed
use native C serial libraries to list ports
since we are already using CGO for most platform, make it compulsory https://releases.linaro.org/15.06/components/toolchain/binaries/4.8/arm-linux-gnueabihf/gcc-linaro-4.8-2015.06-x86_64_arm-linux-gnueabihf.tar.xz is used as default arm cross compilation toolchain
1 parent 3892176 commit dd7e1c4

File tree

5 files changed

+23
-298
lines changed

5 files changed

+23
-298
lines changed

compile_webidebridge.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ createZipEmbeddableFileArduino()
4545
bootstrapPlatforms()
4646
{
4747
echo 'In bootstrapPlatforms'
48-
#export PATH=$PATH:/opt/osxcross/target/bin
48+
#export PATH=$PATH:/opt/osxcross/target/bin:
4949
cd $GOROOT/src
5050
env CC_FOR_TARGET=o64-clang CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 ./make.bash --no-clean
5151
env CC_FOR_TARGET=gcc CGO_ENABLED=1 GOOS=linux GOARCH=amd64 ./make.bash --no-clean
5252
env CC_FOR_TARGET=gcc CGO_ENABLED=1 GOOS=linux GOARCH=386 ./make.bash --no-clean
53-
env CGO_ENABLED=0 GOOS=linux GOARCH=arm ./make.bash --no-clean
53+
env CC_FOR_TARGET=arm-linux-gnueabihf-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm ./make.bash --no-clean
5454
env CC_FOR_TARGET=i686-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=386 ./make.bash --no-clean
5555
}
5656

@@ -67,6 +67,7 @@ compilePlatform()
6767
NAME=$NAME".exe"
6868
EXTRAFLAGS="-ldflags -H=windowsgui"
6969
fi
70+
echo env GOOS=$GOOS GOARCH=$GOARCH CC=$CC CXX=$CC CGO_ENABLED=$CGO_ENABLED go build -o=$NAME $EXTRAFLAGS
7071
env GOOS=$GOOS GOARCH=$GOARCH CC=$CC CXX=$CC CGO_ENABLED=$CGO_ENABLED go build -o=$NAME $EXTRAFLAGS
7172
if [ $? != 0 ]
7273
then
@@ -83,7 +84,7 @@ extractVersionFromMain
8384
compilePlatform darwin amd64 o64-clang 1
8485
#compilePlatformLinux linux 386 gcc
8586
compilePlatform linux amd64 gcc 1
86-
compilePlatform linux arm 0
87+
compilePlatform linux arm arm-linux-gnueabihf-gcc 1
8788
compilePlatform windows 386 i686-w64-mingw32-gcc 1
8889

8990

seriallist.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
package main
44

55
import (
6+
"fmt"
67
log "github.com/Sirupsen/logrus"
7-
"github.com/facchinm/go-serial"
8+
"github.com/facchinm/go-serial-native"
89
"regexp"
910
)
1011

@@ -16,6 +17,7 @@ type OsSerialPort struct {
1617
Product string
1718
IdProduct string
1819
IdVendor string
20+
ISerial string
1921
NetworkPort bool
2022
}
2123

@@ -28,12 +30,18 @@ func GetList(network bool) ([]OsSerialPort, error) {
2830
return netportList, err
2931
} else {
3032

33+
arrPorts := []OsSerialPort{}
3134
// will timeout in 2 seconds
32-
ports, err := serial.GetPortsList()
35+
ports, err := serial.ListPorts()
36+
if err != nil {
37+
return arrPorts, err
38+
}
3339

34-
arrPorts := []OsSerialPort{}
3540
for _, element := range ports {
36-
arrPorts = append(arrPorts, OsSerialPort{Name: element})
41+
vid, pid, _ := element.USBVIDPID()
42+
vidString := fmt.Sprintf("0x%04X", vid)
43+
pidString := fmt.Sprintf("0x%04X", pid)
44+
arrPorts = append(arrPorts, OsSerialPort{Name: element.Name(), IdVendor: vidString, IdProduct: pidString, ISerial: element.USBSerialNumber()})
3745
}
3846

3947
// see if we should filter the list
@@ -54,7 +62,7 @@ func GetList(network bool) ([]OsSerialPort, error) {
5462
arrPorts = newarrPorts
5563
}
5664

57-
arrPorts = associateVidPidWithPort(arrPorts)
65+
arrPorts = extraFilterPorts(arrPorts)
5866
return arrPorts, err
5967
//log.Printf("Done doing GetList(). arrPorts:%v\n", arrPorts)
6068
}

utilities_darwin.go

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,12 @@ import (
1313
// search all board.txt files for map[Product ID]
1414
// assign it to name
1515

16-
func associateVidPidWithPort(ports []OsSerialPort) []OsSerialPort {
16+
func extraFilterPorts(ports []OsSerialPort) []OsSerialPort {
1717

1818
// prefilter ports
1919
ports = Filter(ports, func(port OsSerialPort) bool {
2020
return !strings.Contains(port.Name, "Blue") && !strings.Contains(port.Name, "/cu")
2121
})
22-
23-
for index, _ := range ports {
24-
port_hash := strings.Trim(ports[index].Name, "/dev/tty.usbmodem")
25-
port_hash = strings.Trim(port_hash, "/dev/tty.usbserial-")
26-
27-
port_hash = strings.ToLower(port_hash)
28-
29-
usbcmd := exec.Command("system_profiler", "SPUSBDataType")
30-
grepcmd := exec.Command("grep", "Location ID: 0x"+port_hash[:len(port_hash)-1], "-B6")
31-
cmdOutput, _ := pipe_commands(usbcmd, grepcmd)
32-
33-
if len(cmdOutput) == 0 {
34-
usbcmd = exec.Command("system_profiler", "SPUSBDataType")
35-
grepcmd = exec.Command("grep" /*"Serial Number: "+*/, strings.Trim(port_hash, "0"), "-B3", "-A3")
36-
cmdOutput, _ = pipe_commands(usbcmd, grepcmd)
37-
}
38-
39-
if len(cmdOutput) == 0 {
40-
//give up
41-
continue
42-
}
43-
44-
cmdOutSlice := strings.Split(string(cmdOutput), "\n")
45-
46-
cmdOutMap := make(map[string]string)
47-
48-
for _, element := range cmdOutSlice {
49-
if strings.Contains(element, "ID") || strings.Contains(element, "Manufacturer") {
50-
element = strings.TrimSpace(element)
51-
arr := strings.Split(element, ": ")
52-
if len(arr) > 1 {
53-
cmdOutMap[arr[0]] = arr[1]
54-
} else {
55-
cmdOutMap[arr[0]] = ""
56-
}
57-
}
58-
}
59-
ports[index].IdProduct = strings.Split(cmdOutMap["Product ID"], " ")[0]
60-
ports[index].IdVendor = strings.Split(cmdOutMap["Vendor ID"], " ")[0]
61-
ports[index].Manufacturer = cmdOutMap["Manufacturer"]
62-
}
6322
return ports
6423
}
6524

utilities_linux.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,15 @@
11
package main
22

33
import (
4-
"fmt"
54
"os/exec"
6-
"path/filepath"
7-
"strconv"
8-
"strings"
95
)
106

11-
func associateVidPidWithPort(ports []OsSerialPort) []OsSerialPort {
12-
13-
for index, _ := range ports {
14-
ueventcmd := exec.Command("cat", "/sys/class/tty/"+filepath.Base(ports[index].Name)+"/device/uevent")
15-
grep3cmd := exec.Command("grep", "PRODUCT=")
16-
17-
cmdOutput2, _ := pipe_commands(ueventcmd, grep3cmd)
18-
cmdOutput2S := string(cmdOutput2)
19-
20-
if len(cmdOutput2S) == 0 {
21-
continue
22-
}
23-
24-
infos := strings.Split(cmdOutput2S, "=")
25-
26-
vid_pid := strings.Split(infos[1], "/")
27-
28-
vid, _ := strconv.ParseInt(vid_pid[0], 16, 32)
29-
pid, _ := strconv.ParseInt(vid_pid[1], 16, 32)
30-
ports[index].IdVendor = fmt.Sprintf("0x%04x", vid)
31-
ports[index].IdProduct = fmt.Sprintf("0x%04x", pid)
32-
}
33-
return ports
34-
}
35-
367
func hideFile(path string) {
378
}
389

3910
func tellCommandNotToSpawnShell(_ *exec.Cmd) {
4011
}
12+
13+
func extraFilterPorts(ports []OsSerialPort) []OsSerialPort {
14+
return ports
15+
}

0 commit comments

Comments
 (0)