Giter Club home page Giter Club logo

libusb's Introduction

libusb

Go bindings for the libusb C library.

GoDoc Go Report Card Build Status License Badge

Installation

$ go get github.com/gotmc/libusb/v2

Installing C libusb library

To use libusb package, you'll need to install the libusb C library first.

OS X

$ brew install libusb

Windows

Download and install the latest Windows libusb binaries from libusb.info.

Linux

$ sudo apt-get install -y libusb-dev libusb-1.0-0-dev

Documentation

Documentation can be found at either:

Contributing

Contributions are welcome! To contribute please:

  1. Fork the repository
  2. Create a feature branch
  3. Code
  4. Submit a pull request

Testing

Prior to submitting a pull request, please run:

$ make check
$ make lint

To update and view the test coverage report:

$ make cover

Alternatives

There are other USB Go libraries besides libusb. Below are a few alternatives:

License

libusb is released under the MIT license. Please see the LICENSE.txt file for more information.

libusb's People

Contributors

daviyang35 avatar duskwuff avatar jpoirier avatar matthewrankin avatar netforces avatar realpy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libusb's Issues

incorrect class name

Hi guys! I want to get a list of usb devices, Then catch my usb printer by it class
So i did this

package main

import (
	"fmt"

	"github.com/gotmc/libusb/v2"
)

func main(){
 
 	c, err := libusb.NewContext();
 	if err != nil {
 		panic(err)
 	}
 	
 	list, _:= c.DeviceList();

 	for _, device := range list {
 		d,_ := device.DeviceDescriptor()
 		fmt.Println(d.DeviceClass)
 	}
    
}

output:

Hub class.
Hub class.
Wireless class.
Each interface specifies its own class information and all interfaces operate independently.

its gives me Each interface specifies its own class information and all interfaces operate independently. instead of "printer" class!

in terminal i get:

❯ lsusb --verbose | grep "Printer"
  iManufacturer           1 Printer-58
      bInterfaceClass         7 Printer
      bInterfaceSubClass      1 Printer

Why? and Thanks!

Compilation error under CentOS7 (libusb-1.0)

# github.com/gotmc/libusb/v2
/home/builder/go/pkg/mod/github.com/gotmc/libusb/[email protected]/context.go: In function 'set_debug':
/home/builder/go/pkg/mod/github.com/gotmc/libusb/[email protected]/context.go:11:34: error: 'LIBUSB_OPTION_LOG_LEVEL' undeclared (first use in this function)
 //   return libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level);

The culprit code is:

// #cgo pkg-config: libusb-1.0
// #include <libusb.h>
// int set_debug(libusb_context * ctx, int level) {
//   return libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level);
// }

In order to have better backward compatibility, it should be changed to:

// #cgo pkg-config: libusb-1.0
// #include <libusb.h>
// int set_debug(libusb_context * ctx, int level) {
// #if HAVE_LIBUSB_SET_OPTION
//    return libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level);
// #else
//    libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_INFO);
// #endif
// }

github.com/gotmc/libusb/[email protected]/context.go:11:34: error: 'LIBUSB_OPTION_LOG_LEVEL' undeclared (first use in this function)

my os:
uname -a:
Linux y1cloud-Z790-UD 6.5.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
dpkg -l | grep libusb:
ii libgusb2:amd64 0.3.10-1 amd64 GLib wrapper around libusb1
ii libusb-0.1-4:amd64 2:0.1.12-32build3 amd64 userspace USB programming library
ii libusb-1.0-0:amd64 2:1.0.25-1ubuntu2 amd64 userspace USB programming library
ii libusb-1.0-0:i386 2:1.0.25-1ubuntu2 i386 userspace USB programming library
ii libusb-1.0-0-dev:amd64 2:1.0.25-1ubuntu2 amd64 userspace USB programming library development files
ii libusb-1.0-doc 2:1.0.25-1ubuntu2 all documentation for userspace USB programming
ii libusb-dev 2:0.1.12-32build3 amd64 userspace USB programming library development files
ii libusbmuxd6:amd64 2.0.2-3build2 amd64 USB multiplexor daemon for iPhone and iPod Touch devices - library
go version:
go version go1.22.1 linux/amd64

when I run example/get_sn/main.go, it show the error msg "github.com/gotmc/libusb/[email protected]/context.go:11:34: error: 'LIBUSB_OPTION_LOG_LEVEL' undeclared (first use in this function)
11 | // return libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level);"
so, how to make it work?

go get fails for v2 with libusb v1.0.24

Getting this error when trying to go get v2:

> go get github.com/gotmc/libusb/v2                                                                                                                                                                                                          174ms  Do 29 Apr 2021 09:04:49 CEST
go: downloading github.com/gotmc/libusb/v2 v2.1.0
# github.com/gotmc/libusb/v2
../../.go/pkg/mod/github.com/gotmc/libusb/[email protected]/hotplug.go:87:26: cannot use event (type _Ctype_libusb_hotplug_event) as type _Ctype_int in assignment
../../.go/pkg/mod/github.com/gotmc/libusb/[email protected]/miscellaneous.go:34:43: cannot use int32(err) (type int32) as type _Ctype_int in argument to _Cfunc_libusb_strerror

This is on Gentoo Linux/amd64 (kernel v5.10.x), with Go v1.16.3, libusb v1.0.24.

Issue using this lib with Travis-CI

Hey there, we use this library in our project but we have some troubles integrating it in Travis-CI, this is the script

language: go

go:
  - 1.8.x
  - master

os:
  - linux
dist: trusty
addons:
        apt:
                packages:
                        libusb-1.0-0-dev
                        libusb-1.0-0
install:
  - go get -v github.com/gotmc/libusb
  - go build

script:
  - go tool vet -v -all ./

The error is
could not determine kind of name for C.LIBUSB_TRANSFER_TYPE_BULK_STREAM

Cannot pass variables to `ControlTransfer` because type is not exported

The libusb.DeviceHandle::ControlTransfer methods arg[0] has a non-exported type bmRequestType. As a result it seems like this method can only be called with hard-coded values and not passed in with a variable of type byte.

Example of what I have to write:

bytes_sent, err := handle.ControlTransfer(
	(0x01<<5)+0x01,
	0x09,
	reportId+(0x03<<8),
	0,
	msg,
	len(msg),
	5000)

Example of what I would like to write:

requestClass := byte(0)
interfaceIndex := byte(32)
endpointOutIndex := byte(1)
bytes_sent, err := handle.ControlTransfer(
	requestClass | interfaceIndex | endpointOutIndex,
	0x09,
	reportId+(0x03<<8),
	0,
	msg,
	len(msg),
	5000)

Can't build/install on Windows 7 32bit

I am not sure if this is related to go or rather to the libusb C version.

I can install the example via go install github.com/gotmc/libusb/example under Windows 7 64bit without problems and running the executable shows the expected output. However, building/installing under Windows 7 32bit doesn't work and I get a lot of linking errors.

C:\git\go_work>go install github.com/gotmc/libusb/example
# github.com/gotmc/libusb
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\context.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_exit':
c:/git/go_work/src/github.com/gotmc/libusb/context.go:42: undefined reference to
 `libusb_exit@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\context.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_free_device_list':
c:/git/go_work/src/github.com/gotmc/libusb/context.go:52: undefined reference to
 `libusb_free_device_list@8'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\context.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_get_device':
c:/git/go_work/src/github.com/gotmc/libusb/context.go:63: undefined reference to
 `libusb_get_device@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\context.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_get_device_list':
c:/git/go_work/src/github.com/gotmc/libusb/context.go:77: undefined reference to
 `libusb_get_device_list@8'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\context.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_init':
c:/git/go_work/src/github.com/gotmc/libusb/context.go:90: undefined reference to
 `libusb_init@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\context.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_open_device_with_vi
d_pid':
c:/git/go_work/src/github.com/gotmc/libusb/context.go:105: undefined reference t
o `libusb_open_device_with_vid_pid@12'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\context.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_set_debug':
c:/git/go_work/src/github.com/gotmc/libusb/context.go:117: undefined reference t
o `libusb_set_debug@8'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_C2func_libusb_get_bus_number':
c:/git/go_work/src/github.com/gotmc/libusb/device.go:46: undefined reference to
`libusb_get_bus_number@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_C2func_libusb_get_device_address'
:
c:/git/go_work/src/github.com/gotmc/libusb/device.go:62: undefined reference to
`libusb_get_device_address@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_C2func_libusb_get_device_speed':
c:/git/go_work/src/github.com/gotmc/libusb/device.go:77: undefined reference to
`libusb_get_device_speed@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_C2func_libusb_get_max_packet_size
':
c:/git/go_work/src/github.com/gotmc/libusb/device.go:94: undefined reference to
`libusb_get_max_packet_size@8'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_C2func_libusb_get_port_number':
c:/git/go_work/src/github.com/gotmc/libusb/device.go:110: undefined reference to
 `libusb_get_port_number@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_free_config_descript
or':
c:/git/go_work/src/github.com/gotmc/libusb/device.go:122: undefined reference to
 `libusb_free_config_descriptor@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_get_active_config_de
scriptor':
c:/git/go_work/src/github.com/gotmc/libusb/device.go:134: undefined reference to
 `libusb_get_active_config_descriptor@8'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_get_bus_number':
c:/git/go_work/src/github.com/gotmc/libusb/device.go:148: undefined reference to
 `libusb_get_bus_number@4'
C:\Users\bjoern\AppData\Local\Temp\go-build325367382\github.com\gotmc\libusb\_ob
j\device.cgo2.o: In function `cgo_d160d8f02340_Cfunc_libusb_get_config_descripto
r':
c:/git/go_work/src/github.com/gotmc/libusb/device.go:164: undefined reference to
 `libusb_get_config_descriptor@12'

This is what I did in order to be able to build on Win 7 32bit:

  1. Installing Go 1.6.2 32bit
  2. Cloning github.com/gotmc/libusb
  3. Installing tdm-gcc-5.1.0-3.exe (32bit) from http://tdm-gcc.tdragon.net/download
  4. Downloading and extracting libusb 1.0.20 binaries for Windows from libusb.info. Adjusting libusb-1.0.pc to point to 32bit dlls and include folder
  5. Setting PKG_CONFIG_PATH environment variable to point to folder where libusb-1.0.pc resides
  6. Following the instructions from http://stackoverflow.com/questions/1710922/how-to-install-pkg-config-in-windows to install pkg-config.exe and dependencies to tdm-gcc
  7. Running go install github.com/gotmc/libusb/example and getting the above errors

Issue with getting the package

Ran: go get github.com/gotmc/libusb
Expected: No issues
Got: # github.com/gotmc/libusb ../../workspace/src/github.com/gotmc/libusb/context.go: In function 'set_debug': ../../workspace/src/github.com/gotmc/libusb/context.go:11:34: error: 'LIBUSB_OPTION_LOG_LEVEL' undeclared (first use in this function); did you mean 'LIBUSB_ERROR_NO_MEM'? // return libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level); ^~~~~~~~~~~~~~~~~~~~~~~ LIBUSB_ERROR_NO_MEM ../../workspace/src/github.com/gotmc/libusb/context.go:11:34: note: each undeclared identifier is reported only once for each function it appears in

KernelDriverActive method ignores the return value 1, "kernel driver active"

return values for libusb_kernel_driver_active

0 if no kernel driver is active 
1 if a kernel driver is active 
LIBUSB_ERROR_NO_DEVICE if the device has been disconnected 
LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality is not available 
another LIBUSB_ERROR code on other failure

Not sure how you plan on implementing it but for the fork I'm using I did:

func (dh *DeviceHandle) KernelDriverActive(interfaceNum int) (bool, error) {
        err := C.libusb_kernel_driver_active(
                dh.libusbDeviceHandle, C.int(interfaceNum))
        if err == 1 {
                return true, nil
        } else if err != 0 {
                return false, ErrorCode(err)
        }
        return false, nil
}

Again: go get fails for v2 with libusb v1.0.24

Debian 11
go version go1.17.5 linux/amd64

root@localhost:~# apt-get install -y libusb-dev libusb-1.0-0-dev && go get github.com/gotmc/libusb/v2
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
libusb-dev is already the newest version (2:0.1.12-32).
libusb-1.0-0-dev is already the newest version (2:1.0.24-3).
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.
# github.com/gotmc/libusb/v2
go/pkg/mod/github.com/gotmc/libusb/[email protected]/hotplug.go:87:26: cannot use event (type _Ctype_libusb_hotplug_event) as type _Ctype_int in assignment
go/pkg/mod/github.com/gotmc/libusb/[email protected]/miscellaneous.go:34:43: cannot use int32(err) (type int32) as type _Ctype_int in argument to _Cfunc_libusb_strerror

libusb with go and msys compilation error

Hello,

Im trying to work with libusb and go. I have msys mingw64 and I've installed mingw-w64-x86_64-libusb-1.0.26-1 there. Pkg-Config finds it but when I compile the example:

// Copyright (c) 2016 The libusb developers. All rights reserved.
// Project site: https://github.com/gotmc/libusb
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.

package main

import (
	"log"

	"github.com/gotmc/libusb"
)

func main() {
	ctx, _ := libusb.NewContext()
	defer ctx.Close()
	devices, _ := ctx.GetDeviceList()
	for _, device := range devices {
		usbDeviceDescriptor, _ := device.GetDeviceDescriptor()
		handle, _ := device.Open()
		defer handle.Close()
		serialNumber, _ := handle.GetStringDescriptorASCII(usbDeviceDescriptor.SerialNumberIndex)
		log.Printf("Found S/N: %s", serialNumber)
	}

}

Then i get this:

panic: runtime error: invalid memory address or nil pointer dereference
        panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x7ff76d865a54]

goroutine 1 [running]:
github.com/gotmc/libusb.(*DeviceHandle).Close.func1(0x7ff76d8089f6?)
        C:/Users/elias/go/pkg/mod/github.com/gotmc/[email protected]/handle.go:65 +0x14
github.com/gotmc/libusb.(*DeviceHandle).Close(0x0?)
        C:/Users/elias/go/pkg/mod/github.com/gotmc/[email protected]/handle.go:65 +0x19
panic({0x7ff76d891ca0, 0x7ff76d87b700})
        C:/Program Files/Go/src/runtime/panic.go:890 +0x262
github.com/gotmc/libusb.(*DeviceHandle).GetStringDescriptorASCII.func1(0x7ff76d93b4e0?, 0x80?, {0xc000006600?, 0xc00011de98?, 0xc00011de98?}, 0x7ff76d7f531f?)
        C:/Users/elias/go/pkg/mod/github.com/gotmc/[email protected]/handle.go:51 +0x1d
github.com/gotmc/libusb.(*DeviceHandle).GetStringDescriptorASCII(0xc00006a480?, 0xf8?)
        C:/Users/elias/go/pkg/mod/github.com/gotmc/[email protected]/handle.go:56 +0x57
main.main()
        D:/Dokumente/Go Lang/project/cmd/main.go:22 +0xbd
exit status 2

Is the msys version incomatible? And I had to change some function names like device.DeviceDescriptor() -> device.GetDeviceDescriptor(). Has the API changed but the examples not?

datarace in hotplug callback map

access to hotplugCallbackStorage is not protected by mutex.

==================
WARNING: DATA RACE
Read at 0x00c00009a330 by goroutine 7:
  runtime.mapaccess2_fast32()
      runtime/map_fast32.go:53 +0x0
  github.com/gotmc/libusb/v2.libusbHotplugCallback()
      github.com/gotmc/libusb/[email protected]/hotplug.go:203 +0xf0
  _cgoexp_0b0a3dd1d46c_libusbHotplugCallback()
      _cgo_gotypes.go:998 +0x99
  runtime.cgocallbackg1()
      runtime/cgocall.go:315 +0x2c2
  github.com/gotmc/libusb/v2.(*HotplugCallbackStorage).handleEvents.func1()
      github.com/gotmc/libusb/[email protected]/hotplug.go:176 +0x5b
  github.com/gotmc/libusb/v2.(*HotplugCallbackStorage).handleEvents()
      github.com/gotmc/libusb/[email protected]/hotplug.go:176 +0x5c
  github.com/gotmc/libusb/v2.(*Context).newHotPlugHandler.func1()
      github.com/gotmc/libusb/[email protected]/hotplug.go:65 +0x47

Previous write at 0x00c00009a330 by main goroutine:
  runtime.mapassign_fast32()
      runtime/map_fast32.go:93 +0x0
  github.com/gotmc/libusb/v2.(*Context).HotplugRegisterCallbackEvent()
      github.com/gotmc/libusb/[email protected]/hotplug.go:115 +0x147
  main.main()
      github.com/bokunodev/hid/main.go:36 +0xed

Goroutine 7 (running) created at:
  github.com/gotmc/libusb/v2.(*Context).newHotPlugHandler()
      github.com/gotmc/libusb/[email protected]/hotplug.go:65 +0x14b
  github.com/gotmc/libusb/v2.(*Context).HotplugRegisterCallbackEvent()
      github.com/gotmc/libusb/[email protected]/hotplug.go:75 +0x6b
  main.main()
      github.com/bokunodev/hid/main.go:32 +0xca
==================

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.