@@ -27,7 +27,9 @@ import (
27
27
)
28
28
29
29
func main () {
30
- dfuDisc := & DFUDiscovery {}
30
+ dfuDisc := & DFUDiscovery {
31
+ portsCache : map [string ]* discovery.Port {},
32
+ }
31
33
disc := discovery .NewServer (dfuDisc )
32
34
if err := disc .Run (os .Stdin , os .Stdout ); err != nil {
33
35
fmt .Fprintf (os .Stderr , "Error: %s\n " , err .Error ())
@@ -37,7 +39,8 @@ func main() {
37
39
38
40
// DFUDiscovery is the implementation of the DFU pluggable-discovery
39
41
type DFUDiscovery struct {
40
- closeChan chan <- struct {}
42
+ closeChan chan <- struct {}
43
+ portsCache map [string ]* discovery.Port
41
44
}
42
45
43
46
// Hello is the handler for the pluggable-discovery HELLO command
@@ -82,9 +85,23 @@ func (d *DFUDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discov
82
85
83
86
func (d * DFUDiscovery ) sendUpdates (eventCB discovery.EventCallback , errorCB discovery.ErrorCallback ) {
84
87
C .dfuProbeDevices ()
88
+
89
+ newCache := map [string ]* discovery.Port {}
85
90
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
+ }
87
96
}
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
88
105
}
89
106
90
107
func getPath (dev * C.struct_libusb_device ) string {
0 commit comments