USB functions written in Elixir, on the kernel's FunctionFS.
A module that uses this one describes its interfaces as data and handles the function's events in a process of its own:
defmodule Loopback do
use Kuroneko.Gadget.FunctionFS
alias Kuroneko.Gadget.FunctionFS
@impl FunctionFS
def interfaces(_settings) do
[[name: "Loopback", endpoints: [{:in, :bulk}, {:out, :bulk}]]]
end
@impl FunctionFS
def handle_event(:enable, state) do
FunctionFS.read(self(), 2, 512)
{:noreply, state}
end
def handle_event({:data, 2, data}, state) do
FunctionFS.write(self(), 1, data)
FunctionFS.read(self(), 2, 512)
{:noreply, state}
end
def handle_event(event, state), do: super(event, state)
end
Kuroneko.Gadget.configure([:keyboard, :mouse, Loopback])Such a module is a Kuroneko.Gadget.Function of the type "ffs". It goes
in the functions as Loopback, or as {Loopback, name: "loop2", ...},
whose options besides :name are the settings its callbacks get. The
endpoints are numbered from 1 in the order of interfaces/1.
The process starts when the gadget readies the function and stops when
the function is removed. write/3, read/3, answer/2 and stall/1
ask it for transfers, from a callback with self() or from elsewhere
with whereis/2, and their results come back as events. An exception in
a callback is logged and the function goes on.
Summary
Types
An endpoint: its direction and transfer type, with options
What happens to the function
An interface, with options
A control request to the function.
Callbacks
Handles an event: by default, stalls control requests and ignores the rest.
Handles any other message to the process.
The process's state from the settings: by default, the settings.
The function's interfaces with these settings.
Functions
Answers the pending control request: an IN request with data, an OUT
one by taking the data it carries, which {:control_data, data} brings.
Reads up to size bytes, best a multiple of the packet size, from an
OUT endpoint; {:data, endpoint, data} follows when the host sends them.
Stalls the pending control request.
The process of a module's function: the default instance's, or that of
the one :name names. Takes the :gadget option.
Writes data to an IN endpoint for the host to read; {:written, endpoint, bytes} follows. An empty binary sends a packet of zero length.
Types
@type endpoint() :: {:in | :out, :bulk | :interrupt | :isochronous} | {:in | :out, :bulk | :interrupt | :isochronous, keyword()}
An endpoint: its direction and transfer type, with options:
:max_packet- the largest packet in bytes. For bulk endpoints it applies at full speed: 8, 16, 32 or 64 (the default), high speed always having 512. Interrupt endpoints take up to 64 (the default), isochronous ones up to 1023 (the default).:interval- how often an interrupt or isochronous endpoint is served, in milliseconds: 1 (the default), 2, 4 and so on up to 128:sync- an isochronous endpoint's synchronization::none,:async(the default),:adaptiveor:sync:extra- class-specific descriptors that follow the endpoint's
@type event() :: :bind | :unbind | :enable | :disable | :suspend | :resume | {:setup, request()} | {:control_data, binary()} | {:control_error, term()} | {:data, pos_integer(), binary()} | {:written, pos_integer(), non_neg_integer()} | {:error, pos_integer(), term()}
What happens to the function:
:bindand:unbind- the gadget puts the function on and off the bus:enableand:disable- the host starts and stops using it; the endpoints carry data in between:suspendand:resume- the bus sleeps and wakes{:setup, request}- a control request, which waits foranswer/2orstall/1for two seconds and is stalled after that{:control_data, data}- the data of an OUT request thatanswer/2took{:control_error, reason}- an answer or stall that failed{:data, endpoint, data}- what aread/3got{:written, endpoint, bytes}- awrite/3done{:error, endpoint, reason}- a read or write that failed, such as with:eshutdownwhen the host disables the function
@type interface() :: keyword()
An interface, with options:
:class,:subclass,:protocol- its codes, 0xFF, 0 and 0 by default: vendor-specific:name- a name the host may show:extra- class-specific descriptors that follow the interface's:endpoints- its endpoints
@type request() :: %{ direction: :in | :out, type: :standard | :class | :vendor | :reserved, recipient: :device | :interface | :endpoint | :other, request: byte(), value: char(), index: char(), length: char() }
A control request to the function.
Callbacks
Handles an event: by default, stalls control requests and ignores the rest.
Handles any other message to the process.
The process's state from the settings: by default, the settings.
The function's interfaces with these settings.
Functions
@spec answer(GenServer.server(), binary()) :: :ok
Answers the pending control request: an IN request with data, an OUT
one by taking the data it carries, which {:control_data, data} brings.
@spec read(GenServer.server(), pos_integer(), non_neg_integer()) :: :ok
Reads up to size bytes, best a multiple of the packet size, from an
OUT endpoint; {:data, endpoint, data} follows when the host sends them.
@spec stall(GenServer.server()) :: :ok
Stalls the pending control request.
The process of a module's function: the default instance's, or that of
the one :name names. Takes the :gadget option.
@spec write(GenServer.server(), pos_integer(), binary()) :: :ok
Writes data to an IN endpoint for the host to read; {:written, endpoint, bytes} follows. An empty binary sends a packet of zero length.