@@ -13,13 +13,15 @@ char *get_path(libusb_device *dev);
13
13
// Defined in main.c
14
14
const char *libusbOpen();
15
15
void libusbClose();
16
+ void dfuProbeDevices();
16
17
*/
17
18
import "C"
18
19
19
20
import (
20
21
"fmt"
21
22
"os"
22
23
24
+ "github.com/arduino/go-properties-orderedmap"
23
25
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
24
26
)
25
27
@@ -56,9 +58,71 @@ func (d *DFUDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discov
56
58
if cErr := C .libusbOpen (); cErr != nil {
57
59
return fmt .Errorf ("can't open libusb: %s" , C .GoString (cErr ))
58
60
}
61
+ go func () {
62
+ C .dfuProbeDevices ()
63
+ for _ , dfuIf := range d .getDFUInterfaces () {
64
+ eventCB ("add" , dfuIf .AsDiscoveryPort ())
65
+ }
66
+ }()
59
67
return nil
60
68
}
61
69
62
70
func getPath (dev * C.struct_libusb_device ) string {
63
71
return C .GoString (C .get_path (dev ))
64
72
}
73
+
74
+ type DFUInterface struct {
75
+ Path string
76
+ AltName string
77
+ VID , PID uint16
78
+ SerialNumber string
79
+ Flags uint32
80
+ }
81
+
82
+ func (i * DFUInterface ) AsDiscoveryPort () * discovery.Port {
83
+ props := properties .NewMap ()
84
+ props .Set ("vid" , fmt .Sprintf ("%04X" , i .VID ))
85
+ props .Set ("pid" , fmt .Sprintf ("%04X" , i .PID ))
86
+ if i .SerialNumber != "" {
87
+ props .Set ("serial" , i .SerialNumber )
88
+ }
89
+ // ProtocolLabel: pdfu.flags & DFU_IFF_DFU ? "DFU" : "Runtime"
90
+ return & discovery.Port {
91
+ Address : i .Path ,
92
+ AddressLabel : i .Path ,
93
+ Protocol : "dfu" ,
94
+ ProtocolLabel : "USB DFU" ,
95
+ Properties : props ,
96
+ HardwareID : props .Get ("serial" ),
97
+ }
98
+ }
99
+
100
+ func (d * DFUDiscovery ) getDFUInterfaces () []* DFUInterface {
101
+ res := []* DFUInterface {}
102
+ for pdfu := C .dfu_root ; pdfu != nil ; pdfu = pdfu .next {
103
+ fmt .Println (pdfu )
104
+ if (pdfu .flags & C .DFU_IFF_DFU ) == 0 {
105
+ // decide what to do with DFU runtime objects
106
+ // for the time being, ignore them
107
+ continue
108
+ }
109
+ ifPath := getPath (pdfu .dev )
110
+ // for i := 0; i < index; i++ {
111
+ // // allow only one object
112
+ // if j["ports"][i]["address"] == ifPath {
113
+ // index = i
114
+ // break
115
+ // }
116
+ // }
117
+
118
+ dfuIf := & DFUInterface {
119
+ Path : ifPath ,
120
+ AltName : C .GoString (pdfu .alt_name ),
121
+ VID : uint16 (pdfu .vendor ),
122
+ PID : uint16 (pdfu .product ),
123
+ SerialNumber : C .GoString (pdfu .serial_name ),
124
+ }
125
+ res = append (res , dfuIf )
126
+ }
127
+ return res
128
+ }
0 commit comments