Skip to content

Commit b458870

Browse files
committed
Added libusb initialization and de-initialization
1 parent 36e905a commit b458870

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@ void *dfu_malloc(size_t size) { return malloc(size); }
2525
// This is the global DFU tree instance used in dfu_util.
2626
struct dfu_if *dfu_root = NULL;
2727

28+
libusb_context *ctx;
29+
30+
const char *libusbOpen() {
31+
int err = libusb_init(&ctx);
32+
if (err != 0) {
33+
return libusb_strerror(err);
34+
}
35+
return NULL;
36+
}
37+
38+
void libusbClose() {
39+
libusb_exit(ctx);
40+
ctx = NULL;
41+
}

main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ package main
77
88
#include <dfu.h>
99
#include <dfu_util.h>
10+
11+
// Defined in main.c
12+
const char *libusbOpen();
13+
void libusbClose();
1014
*/
1115
import "C"
1216

@@ -41,10 +45,14 @@ func (d *DFUDiscovery) Quit() {
4145

4246
// Stop is the handler for the pluggable-discovery STOP command
4347
func (d *DFUDiscovery) Stop() error {
48+
C.libusbClose()
4449
return nil
4550
}
4651

4752
// StartSync is the handler for the pluggable-discovery START_SYNC command
4853
func (d *DFUDiscovery) StartSync(eventCB discovery.EventCallback, errorCB discovery.ErrorCallback) error {
54+
if cErr := C.libusbOpen(); cErr != nil {
55+
return fmt.Errorf("can't open libusb: %s", C.GoString(cErr))
56+
}
4957
return nil
5058
}

0 commit comments

Comments
 (0)