18
18
package main
19
19
20
20
/*
21
- #cgo CPPFLAGS: -DHAVE_UNISTD_H -DHAVE_NANOSLEEP -DHAVE_ERR -I. -Idfu-util-0.11/src -I/usr/local/include/libusb-1.0
22
- #cgo CFLAGS: -DHAVE_UNISTD_H -DHAVE_NANOSLEEP -DHAVE_ERR -I. -Idfu-util-0.11/src -I/usr/local/include/libusb-1.0
21
+ #cgo CPPFLAGS: -DHAVE_UNISTD_H -DHAVE_NANOSLEEP -DHAVE_ERR -I. -Idfu-util-0.11/src -I/usr/local/include/libusb-1.0 -Wall
22
+ #cgo CFLAGS: -DHAVE_UNISTD_H -DHAVE_NANOSLEEP -DHAVE_ERR -I. -Idfu-util-0.11/src -I/usr/local/include/libusb-1.0 -Wall
23
23
#cgo darwin LDFLAGS: -L/usr/local/lib -lusb-1.0 -framework IOKit -framework CoreFoundation -framework Security
24
24
#cgo !darwin LDFLAGS: -L/usr/local/lib -lusb-1.0
25
25
@@ -32,13 +32,16 @@ char *get_path(libusb_device *dev);
32
32
const char *libusbOpen();
33
33
void libusbClose();
34
34
void dfuProbeDevices();
35
+ const char *libusbHotplugRegisterCallback();
36
+ void libusbHotplugDeregisterCallback();
37
+ int libusbHandleEvents();
35
38
*/
36
39
import "C"
37
40
38
41
import (
42
+ "errors"
39
43
"fmt"
40
44
"os"
41
- "time"
42
45
43
46
"github.com/arduino/go-properties-orderedmap"
44
47
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
@@ -84,16 +87,24 @@ func (d *DFUDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discov
84
87
if cErr := C .libusbOpen (); cErr != nil {
85
88
return fmt .Errorf ("can't open libusb: %s" , C .GoString (cErr ))
86
89
}
90
+ if err := C .libusbHotplugRegisterCallback (); err != nil {
91
+ return errors .New (C .GoString (err ))
92
+ }
93
+
87
94
closeChan := make (chan struct {})
88
95
go func () {
89
96
d .portsCache = map [string ]* discovery.Port {}
97
+ d .sendUpdates (eventCB , errorCB )
90
98
for {
91
- d .sendUpdates (eventCB , errorCB )
99
+ if C .libusbHandleEvents () != 0 {
100
+ d .sendUpdates (eventCB , errorCB )
101
+ }
92
102
select {
93
- case <- time .After (5 * time .Second ):
94
103
case <- closeChan :
104
+ C .libusbHotplugDeregisterCallback ()
95
105
d .portsCache = nil
96
106
return
107
+ default :
97
108
}
98
109
}
99
110
}()
0 commit comments