# `MAVLink2.Router`
[🔗](https://github.com/isaiahdw/mavlink2/blob/v0.1.0/lib/mavlink2/router.ex#L1)

Forwards frames between `MAVLink2.Link`s, by MAVLink's
[routing rules](https://mavlink.io/en/guide/routing.html):

- Each frame received teaches the router that its sender, `{sysid,
  compid}`, is reachable through the link it arrived on.
- A broadcast goes out on every other link: a frame with no target, or a
  target system of 0. A frame whose id the dialect lacks (a link with
  `unknown: :keep`) is a broadcast.
- A frame for another system goes out on the other links where that
  system has been seen, any component of it.
- A frame for the router's own system (`:sysid`) and a component other
  than 0 goes out on the other links where that component has been seen;
  for component 0, where any component of the system has been seen.
- A target not seen on any other link goes nowhere, and nothing goes back
  out on the link it came in on.
- `SETUP_SIGNING` is never forwarded: it carries a signing key.
- A frame a link's signing refuses is forwarded by the same rules, but
  teaches the router nothing, so a forged sender cannot take a route.
- A `SYSTEM_TIME` whose `time_boot_ms` goes back means its sender has
  rebooted: that system's routes are forgotten.

The target is the header's `target_sysid` when present, else the
payload's `target_system` and `target_component`, found through the
dialect's `target_offsets/1` (`MAVLink2.Dialect`).

Frames go out with `MAVLink2.Link.forward/2`, unchanged. The router is a
subscriber like any other: an application on the same links sees every
frame too, and decides for itself what is addressed to it.

Options:

| | |
| --- | --- |
| `:links` | the links to route between, as pids or registered names (required) |
| `:sysid` | the system the router is part of, if any |
| `:name` | registered name |

The router stops when one of its links does, so a supervisor restarting
both resubscribes it.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `routes`

```elixir
@spec routes(GenServer.server()) :: %{
  required({non_neg_integer(), 0..255}) =&gt; [GenServer.server()]
}
```

Where each sender has been seen: `%{{sysid, compid} => [link]}`.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

# `target`

```elixir
@spec target(MAVLink2.Frame.t(), module()) :: :broadcast | {pos_integer(), 0..255}
```

The target of `frame`: `:broadcast`, or `{system, component}`, where
component 0 is any component of the system.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
