Skip to content

Commit 178068a

Browse files
committed
Added first implementation of USB DFU probe
1 parent 81efcfd commit 178068a

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ module github.com/arduino/dfu-discovery
22

33
go 1.19
44

5-
require github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.1.1
5+
require (
6+
github.com/arduino/go-properties-orderedmap v1.7.1
7+
github.com/arduino/pluggable-discovery-protocol-handler/v2 v2.1.1
8+
)
69

710
require (
811
github.com/arduino/go-paths-helper v1.8.0 // indirect
9-
github.com/arduino/go-properties-orderedmap v1.7.1 // indirect
1012
github.com/pkg/errors v0.9.1 // indirect
1113
)

main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ const char *libusbOpen() {
4747
void libusbClose() {
4848
libusb_exit(ctx);
4949
ctx = NULL;
50-
}
50+
}
51+
52+
void dfuProbeDevices() {
53+
probe_devices(ctx);
54+
}

main.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ char *get_path(libusb_device *dev);
1313
// Defined in main.c
1414
const char *libusbOpen();
1515
void libusbClose();
16+
void dfuProbeDevices();
1617
*/
1718
import "C"
1819

1920
import (
2021
"fmt"
2122
"os"
2223

24+
"github.com/arduino/go-properties-orderedmap"
2325
discovery "github.com/arduino/pluggable-discovery-protocol-handler/v2"
2426
)
2527

@@ -56,9 +58,71 @@ func (d *DFUDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discov
5658
if cErr := C.libusbOpen(); cErr != nil {
5759
return fmt.Errorf("can't open libusb: %s", C.GoString(cErr))
5860
}
61+
go func() {
62+
C.dfuProbeDevices()
63+
for _, dfuIf := range d.getDFUInterfaces() {
64+
eventCB("add", dfuIf.AsDiscoveryPort())
65+
}
66+
}()
5967
return nil
6068
}
6169

6270
func getPath(dev *C.struct_libusb_device) string {
6371
return C.GoString(C.get_path(dev))
6472
}
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

Comments
 (0)