Code Monkey home page Code Monkey logo

virtualusbdevice's Introduction

Overview

VirtualUSBDevice is a C++ class that creates a virtual USB device on Linux. The virtual device is visible to the rest of the system as any real USB device is.

The implementation relies on the usbip subsystem, but is self-contained and doesn't require the usbip utility to be manually invoked.

Creation

To create a virtual USB device, instantiate a VirtualUSBDevice (supplying standard USB descriptors to the constructor), and call start().

Handling Transfers

To handle USB transfers, call read(). Before read() returns, VirtualUSBDevice will automatically handle standard USB requests (such as GET_STATUS, GET_DESCRIPTOR, SET_CONFIGURATION requests, and all IN transfers), and will only return from read() when there's an OUT transfer that it can't handle itself. The returned Xfer object represents the USB OUT transfer to be performed, and contains these fields:

  • ep: the transfer's endpoint
  • setupReq: if ep==0, the Setup packet
  • data: the payload data
  • len: the length of data

Sending Data

To write data to an IN endpoint, call write() with the endpoint, data, and length.

Stopping

To tear down the virtual USB device, call stop().

Error Handling

Using the throwOnErr member of VirtualUSBDevice::Info, VirtualUSBDevice can be configured to throw an exception when encountering an error, or alternatively store the error to be queried by the caller. If you choose to disable exceptions, each call to VirtualUSBDevice must be followed by checking err() to see whether an error occurred.

Example

The included example (main.cpp) implements a simple ACM/CDC USB serial device that can be accessed at /dev/ttyACM0 (or ttyACM1, etc). The example outputs periodic messages while consuming input sent to the device.

Thanks

Thanks to the USB-Emulation and USBIP-Virtual-USB-Device projects for trailblazing and illustrating the techniques used by this project.

virtualusbdevice's People

Contributors

davekeck avatar

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.