usb-safe

Type-safe communication with USB devices.

https://github.com/basvandijk/usb-safe/

Latest on Hackage:0.14

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

BSD-3-Clause licensed and maintained by Bas van Dijk

The usb package provides a standard Haskell abstraction layer over bindings-libusb providing: abstract types instead of Ptrs, automatic marshalling and unmarshalling, automatic garbage collection, exceptions instead of integer return codes, etc..

While all that is very nice there are still some things that you can do wrong. For example doing I/O with a closed device or reading from or writing to an endpoint which doesn't belong to the claimed interface. Or reading from an Out endpoint or writing to an In endpoint.

usb-safe provides the following guarantees:

  • You can't reference handles to devices that are closed. In other words: no I/O with closed handles is possible.

  • The programmer specifies the region in which devices should remain open. On exit from the region the opened devices will be closed automatically.

  • You can't reference handles to configurations that have not been set.

  • You can't reference handles to interfaces that have not been claimed.

  • Just like with devices, the programmer can specify the region in which interfaces should remain claimed. On exit from the region the claimed interfaces will be released automatically.

  • You can't reference handles to alternates that have not been set.

  • You can't reference endpoints that don't belong to a setted alternate.

  • You can't read from an endpoint with an Out transfer direction.

  • You can't write to an endpoint with an In transfer direction.

The primary technique used in usb-safe is called "Lightweight monadic regions" which was invented by Oleg Kiselyov and Chung-chieh Shan. See:

http://okmij.org/ftp/Haskell/regions.html#light-weight

This technique is implemented in the regions package which is re-exported from usb-safe.

See the usb-safe-examples package for examples how to use this library:

git clone https://github.com/basvandijk/usb-safe-examples