Skip to content

Commit 9d57865

Browse files
committed
Initial (partially hardcoded) implementation of pluggable discoverers
1 parent 477900c commit 9d57865

File tree

4 files changed

+38
-75
lines changed

4 files changed

+38
-75
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func loop() {
297297
}
298298

299299
// list serial ports
300-
portList, _ := GetList(false)
300+
portList, _ := GetList()
301301
log.Println("Your serial ports:")
302302
if len(portList) == 0 {
303303
log.Println("\tThere are no serial ports to list.")

serial.go

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import (
2323
"strings"
2424
"sync"
2525
"time"
26-
27-
"github.com/arduino/arduino-create-agent/upload"
2826
)
2927

3028
type writeRequest struct {
@@ -129,15 +127,9 @@ func write(wr writeRequest) {
129127
func spList(network bool) {
130128
var ls []byte
131129
var err error
132-
if network {
133-
NetworkPorts.Mu.Lock()
134-
ls, err = json.MarshalIndent(&NetworkPorts, "", "\t")
135-
NetworkPorts.Mu.Unlock()
136-
} else {
137-
SerialPorts.Mu.Lock()
138-
ls, err = json.MarshalIndent(&SerialPorts, "", "\t")
139-
SerialPorts.Mu.Unlock()
140-
}
130+
SerialPorts.Mu.Lock()
131+
ls, err = json.MarshalIndent(&SerialPorts, "", "\t")
132+
SerialPorts.Mu.Unlock()
141133
if err != nil {
142134
//log.Println(err)
143135
h.broadcastSys <- []byte("Error creating json on port list " +
@@ -153,19 +145,7 @@ func discoverLoop() {
153145
SerialPorts.Network = false
154146
SerialPorts.Ports = make([]SpPortItem, 0)
155147
SerialPorts.Mu.Unlock()
156-
NetworkPorts.Mu.Lock()
157-
NetworkPorts.Network = true
158-
NetworkPorts.Ports = make([]SpPortItem, 0)
159-
NetworkPorts.Mu.Unlock()
160148

161-
go func() {
162-
for {
163-
if !upload.Busy {
164-
spListDual(false)
165-
}
166-
time.Sleep(2 * time.Second)
167-
}
168-
}()
169149
go func() {
170150
for {
171151
spListDual(true)
@@ -177,7 +157,7 @@ func discoverLoop() {
177157
func spListDual(network bool) {
178158

179159
// call our os specific implementation of getting the serial list
180-
list, err := GetList(network)
160+
list, err := GetList()
181161

182162
//log.Println(list)
183163
//log.Println(err)
@@ -249,15 +229,9 @@ func spListDual(network bool) {
249229
ctr++
250230
}
251231

252-
if network {
253-
NetworkPorts.Mu.Lock()
254-
NetworkPorts.Ports = spl
255-
NetworkPorts.Mu.Unlock()
256-
} else {
257-
SerialPorts.Mu.Lock()
258-
SerialPorts.Ports = spl
259-
SerialPorts.Mu.Unlock()
260-
}
232+
SerialPorts.Mu.Lock()
233+
SerialPorts.Ports = spl
234+
SerialPorts.Mu.Unlock()
261235
}
262236

263237
func spErr(err string) {

seriallist.go

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package main
1919

2020
import (
2121
"fmt"
22-
"regexp"
2322
"strings"
2423

25-
log "github.com/sirupsen/logrus"
26-
"go.bug.st/serial/enumerator"
24+
"github.com/arduino/arduino-cli/arduino/discovery"
25+
"github.com/arduino/arduino-cli/arduino/discovery/discoverymanager"
2726
)
2827

28+
var discoveryManager *discoverymanager.DiscoveryManager
29+
2930
// OsSerialPort is the Os serial port
3031
type OsSerialPort struct {
3132
Name string
@@ -39,51 +40,39 @@ type OsSerialPort struct {
3940
}
4041

4142
// GetList will return the OS serial port
42-
func GetList(network bool) ([]OsSerialPort, error) {
43-
44-
if network {
45-
netportList, err := GetNetworkList()
46-
return netportList, err
47-
}
43+
func GetList() ([]OsSerialPort, error) {
4844

49-
// will timeout in 2 seconds
50-
arrPorts := []OsSerialPort{}
51-
ports, err := enumerator.GetDetailedPortsList()
52-
if err != nil {
53-
return arrPorts, err
54-
}
55-
56-
for _, element := range ports {
57-
if element.IsUSB {
58-
vid := element.VID
59-
pid := element.PID
60-
vidString := fmt.Sprintf("0x%s", vid)
61-
pidString := fmt.Sprintf("0x%s", pid)
62-
if vid != "0000" && pid != "0000" {
63-
arrPorts = append(arrPorts, OsSerialPort{Name: element.Name, IDVendor: vidString, IDProduct: pidString, ISerial: element.SerialNumber})
64-
}
45+
if discoveryManager == nil {
46+
discoveryManager = discoverymanager.New()
47+
Tools.Download("builtin", "serial-discovery", "latest", "keep")
48+
Tools.Download("builtin", "mdns-discovery", "latest", "keep")
49+
sd, err := Tools.GetLocation("serial-discovery")
50+
if err == nil {
51+
d := discovery.New("serial", sd+"/serial-discovery")
52+
discoveryManager.Add(d)
53+
}
54+
md, err := Tools.GetLocation("mdns-discovery")
55+
if err == nil {
56+
d := discovery.New("mdns", md+"/mdns-discovery")
57+
discoveryManager.Add(d)
6558
}
59+
discoveryManager.Start()
6660
}
6761

68-
// see if we should filter the list
69-
if len(*regExpFilter) > 0 {
70-
// yes, user asked for a filter
71-
reFilter := regexp.MustCompile("(?i)" + *regExpFilter)
62+
fmt.Println("calling getList")
63+
ports := discoveryManager.List()
7264

73-
newarrPorts := []OsSerialPort{}
74-
for _, element := range arrPorts {
75-
// if matches regex, include
76-
if reFilter.MatchString(element.Name) {
77-
newarrPorts = append(newarrPorts, element)
78-
} else {
79-
log.Debugf("serial port did not match. port: %v\n", element)
80-
}
65+
fmt.Println(ports)
8166

82-
}
83-
arrPorts = newarrPorts
67+
arrPorts := []OsSerialPort{}
68+
for _, port := range ports {
69+
vid := port.Properties.AsMap()["vid"]
70+
pid := port.Properties.AsMap()["pid"]
71+
arrPorts = append(arrPorts, OsSerialPort{Name: port.Address, IDVendor: vid, IDProduct: pid, ISerial: port.HardwareID})
8472
}
73+
fmt.Println(arrPorts)
8574

86-
return arrPorts, err
75+
return arrPorts, nil
8776
}
8877

8978
func findPortByName(portname string) (*serport, bool) {

upload/upload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func Kill() {
165165
// sometimes) and an error (usually because the port listing failed)
166166
func reset(port string, wait bool, l Logger) (string, error) {
167167
info(l, "Restarting in bootloader mode")
168-
newPort, err := serialutils.Reset(port, wait, nil) // TODO use callbacks to print as the cli does
168+
newPort, err := serialutils.Reset(port, wait, nil, false) // TODO use callbacks to print as the cli does
169169
if err != nil {
170170
info(l, err)
171171
return "", err

0 commit comments

Comments
 (0)