Giter VIP home page Giter VIP logo

usb-asio's Introduction

usb-asio

A libusb wrapper for Asio. Header-only, supports both standalone and boost versions of Asio. Requires a C++20 compiler (only tested on gcc-10). This is an early version, some features might not work as expected.

Using with standalone Asio

By default, boost version of Asio is used. To use standalone asio:

  • When using as a conan package, add -o usb_asio:asio=standalone.
  • When using as a cmake subproject, add -DUSB_ASIO_USE_STANDALONE_ASIO=ON
  • Otherwise, define USB_ASIO_USE_STANDALONE_ASIO.

Example

Find a device with a given VID and PID, and read some data from the bulk endpoint 3 at interface 1 with alt setting 2.

#include <usb_asio/usb_asio.hpp>

auto main() -> int {
   auto ctx = asio::io_context{};
   
   auto future = asio::co_spawn(
       ctx.get_executor(),
       [&]() -> asio::awaitable<int> {
           auto dev = usb_asio::usb_device{ctx};
           
           for (auto const& dev_info : usb_asio::list_usb_devices(ctx))
           {
               auto const desc = dev_info.device_descriptor();
               if (desc.idVendor == 0xABCDu && desc.idProduct == 0x1234u)
               {
                   dev.open(dev_info);
               }
           }
           
           if (dev.is_open())
           {
               auto const interface_number = std::uint8_t{1};
               // Claim the interface
               auto interface = usb_asio::usb_interface{
                   dev, interface_number};

               auto const alt_setting_number = std::uint8_t{2};
               // Set interface alt setting
               co_await interface.async_set_alt_setting(
                   alt_setting_number, asio::use_awaitable);
   
               auto const endpoint_number = std::uint8_t{3};
               auto const timeout = std::chrono::seconds{1};
               auto transfer = usb_asio::usb_in_bulk_transfer{
                   dev, endpoint_number, timeout};
           
               auto buff = std::array<char, 512>{};
               // Read from the bulk endpoint 
               auto const n = co_await transfer.async_read_some(
                   asio::buffer(buff), asio::use_awaitable);
           
               std::cout << std::string_view{buff.data(), n} << "\n";
       
               // Unclaim the interface (otherwise the destructor would
               // do this synchronously, blocking the executor).
               // If you do not care about resetting the interface,
               // you can call interface.release() instead.
               co_await interface.async_unclaim(asio::use_awaitable);
   
               co_return EXIT_SUCCESS;
           }
   
           co_return EXIT_FAILURE;
       },
       asio::use_future);
   
   ctx.run();

   return future.get();
}

usb-asio's People

Contributors

diggit avatar miso1289 avatar xjankovic 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

usb-asio's Issues

can't use `boost::bind` as completion token

Hi,
I am trying to pass std::bind as completion token to async bulk read.
My inspiration is this boost example.
As I am total asio noob, I am quite lost. Do you know what could be the issue?

class Pump : public boost::enable_shared_from_this<Pump> {
public:
	std::array<std::byte, 512> buffer {};

	void start() {
		boost::asio::async_read(
			source,
			boost::asio::buffer(buffer),
			boost::bind(
				&Pump::handle_read, shared_from_this(),
				boost::asio::placeholders::error,
				boost::asio::placeholders::bytes_transferred
			)
		);
	}

  private:
	Pump(std::deque<std::byte> &sink, usb_asio::usb_in_bulk_transfer &source, const unsigned int id) :
		sink(sink),
		source(source),
		id(id)
		{}

	void handle_read(boost::system::error_code error_code, std::size_t bytes_read) {
		assert(error_code);
		if (bytes_read) {
			sink.insert(sink.end(), buffer.begin(), buffer.begin()+bytes_read);
		}
		start();
	}

	std::deque<std::byte> &sink;
	usb_asio::usb_in_bulk_transfer &source;
	unsigned int id {};
};
[error.txt](https://github.com/MiSo1289/usb-asio/files/10483346/error.txt)

Segfault (nullptr) access in interface claim

Hi,
first of all, thank you for all your work... díky :)

I am trying to use your asio wrapper of libusb and I am struggling a little bit. It's all blowing up in interface claiming. There is uninitialized data access resulting in null pointer de-reference.

My code is just slightly modified example from readme.

Constructing interface object calls claim.

claim itself calls unclaim at first.

unclaim calls ::libusb_release_interface which takes device handler as argument.

Device handle pointer is obtained by calling device_handle, which returns device_handle_, but this data member is not set yet. That happens at the end of claim fcn.

Unless I am using it wrong, there is several ways to fix this:

  • device_handle_ has to be set before calling unclaim
  • there has to be overload of interface unclaim which accepts device handle directly (available in claim fcn).
  • avoid calling unclaim when device_handle_ is not valid

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.