Kuroneko.Gadget.FunctionFS behaviour (kuroneko_gadget v0.5.0)

Copy Markdown View Source

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

endpoint()

@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), :adaptive or :sync
  • :extra - class-specific descriptors that follow the endpoint's

event()

@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:

  • :bind and :unbind - the gadget puts the function on and off the bus
  • :enable and :disable - the host starts and stops using it; the endpoints carry data in between
  • :suspend and :resume - the bus sleeps and wakes
  • {:setup, request} - a control request, which waits for answer/2 or stall/1 for two seconds and is stalled after that
  • {:control_data, data} - the data of an OUT request that answer/2 took
  • {:control_error, reason} - an answer or stall that failed
  • {:data, endpoint, data} - what a read/3 got
  • {:written, endpoint, bytes} - a write/3 done
  • {:error, endpoint, reason} - a read or write that failed, such as with :eshutdown when the host disables the function

interface()

@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

request()

@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

handle_event(event, state)

@callback handle_event(event(), state :: term()) :: {:noreply, state :: term()}

Handles an event: by default, stalls control requests and ignores the rest.

handle_info(message, state)

(optional)
@callback handle_info(message :: term(), state :: term()) :: {:noreply, state :: term()}

Handles any other message to the process.

init(settings)

@callback init(settings :: term()) :: {:ok, state :: term()} | {:error, term()}

The process's state from the settings: by default, the settings.

interfaces(settings)

@callback interfaces(settings :: term()) :: [interface()]

The function's interfaces with these settings.

Functions

answer(server, data \\ <<>>)

@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.

read(server, endpoint, size)

@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.

stall(server)

@spec stall(GenServer.server()) :: :ok

Stalls the pending control request.

whereis(module, opts \\ [])

@spec whereis(module(), keyword()) :: pid() | nil

The process of a module's function: the default instance's, or that of the one :name names. Takes the :gadget option.

write(server, endpoint, data)

@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.