Skip to content

Commit 4a440ad

Browse files
committed
Added polling of dfu ports
1 parent 00fca38 commit 4a440ad

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

main.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import (
2727
)
2828

2929
func main() {
30-
dfuDisc := &DFUDiscovery{}
30+
dfuDisc := &DFUDiscovery{
31+
portsCache: map[string]*discovery.Port{},
32+
}
3133
disc := discovery.NewServer(dfuDisc)
3234
if err := disc.Run(os.Stdin, os.Stdout); err != nil {
3335
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
@@ -37,7 +39,8 @@ func main() {
3739

3840
// DFUDiscovery is the implementation of the DFU pluggable-discovery
3941
type DFUDiscovery struct {
40-
closeChan chan<- struct{}
42+
closeChan chan<- struct{}
43+
portsCache map[string]*discovery.Port
4144
}
4245

4346
// Hello is the handler for the pluggable-discovery HELLO command
@@ -82,9 +85,23 @@ func (d *DFUDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discov
8285

8386
func (d *DFUDiscovery) sendUpdates(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) {
8487
C.dfuProbeDevices()
88+
89+
newCache := map[string]*discovery.Port{}
8590
for _, dfuIf := range d.getDFUInterfaces() {
86-
eventCB("add", dfuIf.AsDiscoveryPort())
91+
newPort := dfuIf.AsDiscoveryPort()
92+
if _, exist := d.portsCache[newPort.Address]; !exist {
93+
eventCB("add", newPort)
94+
newCache[newPort.Address] = newPort
95+
}
8796
}
97+
98+
for _, oldPort := range d.portsCache {
99+
if _, exist := newCache[oldPort.Address]; !exist {
100+
eventCB("remove", oldPort)
101+
}
102+
}
103+
104+
d.portsCache = newCache
88105
}
89106

90107
func getPath(dev *C.struct_libusb_device) string {

0 commit comments

Comments
 (0)