# Gateways

> Utilise gateway scripts to interact with Infuse-IoT Powered devices.

The python gateway tools act as a bridge between local Infuse-IoT devices and other tools running on the computer.
Currently the gateways support interaction with devices connected directly via a serial port,
or directly over Bluetooth.

Bluetooth connectivity can be provided:

- Via an Infuse-IoT powered embedded device with the `infuse gateway` tool. (Preferred) or
- Directly via your machines native PC Bluetooth libraries with the `infuse native_bt` tool
<note>

Using `infuse native_bt` requires compatible hardware with Bluetooth 5.0 and support advertising extension capabilities.
See [below](#native-bluetooth) for more details.

</note>

<mermaid :config="config" :toolbar="{"title":"How Python tools use gateway connections to connect to Infuse-IoT devices"}" code="---%0Atoolbar%3A%0A%20%20title%3A%20How%20Python%20tools%20use%20gateway%20connections%20to%20connect%20to%20Infuse-IoT%20devices%0A---%0Aflowchart%20LR%0A%20%20%20%20Tool%5B%22Python%20tool%3Cbr%2F%3E(RPC%20runner%2C%20OTA%20upgrade%2C%20etc.)%22%5D%0A%20%20%20%20Gateway%5B%22infuse%20gateway%22%5D%0A%20%20%20%20DeviceGateway%5B%22Infuse-IoT%20device%3Cbr%2F%3Erunning%20gateway_usb%22%5D%0A%20%20%20%20NativeBT%5B%22infuse%20native_bt%22%5D%0A%20%20%20%20DeviceBT%5B%22Infuse-IoT%20device%22%5D%0A%0A%20%20%20%20Tool%20%3C--%3E%7CUDP%7C%20Gateway%0A%20%20%20%20Gateway%20%3C--%3E%7CSerial%7C%20DeviceGateway%0A%20%20%20%20Gateway%20%3C--%3E%7CRTT%7C%20DeviceGateway%0A%20%20%20%20Gateway%20%3C--%3E%7CPyOCD%7C%20DeviceGateway%0A%20%20%20%20DeviceGateway%20%3C--%3E%7CBluetooth%7C%20DeviceBT%0A%20%20%20%20Tool%20%3C--%3E%7CUDP%7C%20NativeBT%0A%20%20%20%20NativeBT%20%3C--%3E%7CBluetooth%7C%20DeviceBT">



</mermaid>

Both tools are designed to run persistently in the background, managing connections to remote devices,
forwarding packets out to Infuse-IoT hardware and broadcasting received data on localhost multicast UDP sockets.

<note>

Gateway tools rely on an Infuse-IoT API Key and any [Custom Network Keys](credentials#network-keys)
and [definitions](credentials#custom-content) being provided through the
[Credentials](credentials) command.

</note>

## Common Functionality

Both gateway tools have common configuration options

### `--server-port`: Server Port

The `--server-port` argument is used to run more than one instance of
`infuse gateway` concurrently on the same machine.

<tip>

When specifying `--server-port`, the port must be an odd number.

</tip>

<accordion sync="server-port">
<accordion-item label="More Details">

The Gateway tools act as a bridge facilitating multiple other tools to access Infuse-IoT devices.
It does so by utilising socket communications (multicast UDP). As a result of multicast UDP behaviours,
by default only one instance of `infuse gateway` can run at a time.

<mermaid :config="config" :toolbar="{"title":"How Python tools use gateway connections to connect to Infuse-IoT devices"}" code="---%0A%20%20%20%20toolbar%3A%0A%20%20%20%20%20%20title%3A%20How%20Python%20tools%20use%20gateway%20connections%20to%20connect%20to%20Infuse-IoT%20devices%0A%20%20%20%20---%0A%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20RpcTool%5B%22Tool%3A%20RPC%20runner%22%5D%0A%20%20%20%20%20%20%20%20OtaTool%5B%22Tool%3A%20OTA%20upgrade%22%5D%0A%20%20%20%20%20%20%20%20SyncTool%5B%22Tool%3A%20Data%20Logger%20Sync%22%5D%0A%20%20%20%20%20%20%20%20UdpPort%5B%22Multicast%20%3Cbr%3EUDP%20Port%3A%208751%3Cbr%3E(and%208752)%22%5D%0A%20%20%20%20%20%20%20%20Gateway1%5B%22infuse%20gateway%22%5D%0A%20%20%20%20%20%20%20%20Gateway2%5B%22infuse%20gateway%22%5D%0A%20%20%20%20%20%20%20%20DeviceGateway1%5B%22Device1%22%5D%0A%20%20%20%20%20%20%20%20DeviceGateway2%5B%22Device2%22%5D%0A%0A%20%20%20%20%20%20%20%20RpcTool%20%3C--%3E%20UdpPort%0A%20%20%20%20%20%20%20%20OtaTool%20%3C--%3E%20UdpPort%0A%20%20%20%20%20%20%20%20SyncTool%20%3C--%3E%20UdpPort%0A%20%20%20%20%20%20%20%20UdpPort%20%3C--%3E%20%7C%20Bind%20%7C%20Gateway1%0A%20%20%20%20%20%20%20%20UdpPort%20x--%20%22%60**Bind%3Cbr%3EFail**%60%22%20--x%20Gateway2%0A%20%20%20%20%20%20%20%20Gateway1%20%3C--%3E%20DeviceGateway1%0A%20%20%20%20%20%20%20%20Gateway2%20%3C--%3E%20DeviceGateway2">



</mermaid>

The `--server-port` argument can be added to enable the operation multiple gateways concurrently on the same machine.
By providing a different server port, the gateway can run independently of other gateway scripts at the same time.
Similarly, additional tools can be supplied with a `--server-port` argument to use a different gateway instance
(instead of the default).

<mermaid :config="config" :toolbar="{"title":"How Python tools use gateway connections to connect to Infuse-IoT devices"}" code="---%0A%20%20%20%20toolbar%3A%0A%20%20%20%20%20%20title%3A%20How%20Python%20tools%20use%20gateway%20connections%20to%20connect%20to%20Infuse-IoT%20devices%0A%20%20%20%20---%0A%20%20%20%20flowchart%20LR%0A%20%20%20%20%20%20%20%20RpcTool%5B%22Tool%3A%20RPC%20runner%22%5D%0A%20%20%20%20%20%20%20%20RpcTool2%5B%22Tool%3A%20RPC%20runner%20(2)%22%5D%0A%20%20%20%20%20%20%20%20OtaTool%5B%22Tool%3A%20OTA%20upgrade%22%5D%0A%20%20%20%20%20%20%20%20SyncTool%5B%22Tool%3A%20Data%20Logger%20Sync%22%5D%0A%20%20%20%20%20%20%20%20UdpPort%5B%22Multicast%20%3Cbr%3EUDP%20Port%3A%208751%3Cbr%3E(and%208752)%22%5D%0A%20%20%20%20%20%20%20%20UdpPort2%5B%22Multicast%20%3Cbr%3EUDP%20Port%3A%208753%3Cbr%3E(and%208754)%22%5D%0A%20%20%20%20%20%20%20%20Gateway1%5B%22infuse%20gateway%22%5D%0A%20%20%20%20%20%20%20%20Gateway2%5B%22infuse%20gateway%22%5D%0A%20%20%20%20%20%20%20%20DeviceGateway1%5B%22Device1%22%5D%0A%20%20%20%20%20%20%20%20DeviceGateway2%5B%22Device2%22%5D%0A%0A%20%20%20%20%20%20%20%20RpcTool%20%3C--%3E%20UdpPort%0A%20%20%20%20%20%20%20%20RpcTool2%20%3C--%3E%20UdpPort2%0A%20%20%20%20%20%20%20%20OtaTool%20%3C--%3E%20UdpPort%0A%20%20%20%20%20%20%20%20SyncTool%20%3C--%3E%20UdpPort%0A%20%20%20%20%20%20%20%20UdpPort%20%3C--%3E%20%7C%20Bind%20%7C%20Gateway1%0A%20%20%20%20%20%20%20%20UdpPort2%20%3C--%3E%20%7C%20Bind%20%7C%20Gateway2%0A%20%20%20%20%20%20%20%20Gateway1%20%3C--%3E%20DeviceGateway1%0A%20%20%20%20%20%20%20%20Gateway2%20%3C--%3E%20DeviceGateway2">



</mermaid>
</accordion-item>
</accordion>

### `--root`: Key File

In some instances, Tags utilise a Root Identity that is different from that provided by the Infuse-IoT Cloud.
In such instances, utilising the default Cloud for authentication will cause cryptography to fail.

As a result, a root identity certificate can be provided to override the default Cloud identity with
the `--root` argument.

## Native Bluetooth

Native Bluetooth support is supported through the cross platform [bleak](https://github.com/hbldh/bleak) package.

```bash
> infuse native_bt -h
usage: infuse native_bt [-h] [--root ROOT] [--server-port SERVER_SOCK]

Use the local Bluetooth adapter for Bluetooth interaction

options:
  -h, --help            show this help message and exit
  --root ROOT           Root identity certificate to use instead of cloud
  --server-port SERVER_SOCK
                        Alternate port to use for Gateway connections (default
                        8751)
```

<caution>

Your machine must support the required Bluetooth Hardware Requirements to use `infuse native_bt`.

</caution>

<warning>

`native_bt` is convenient, however uses the Operating System's managed Bluetooth APIs.
As a result, OS power saving policies can and will interfer with the tool's functionality.

</warning>

Follow the instructions below to check compatibility.

<tabs sync="OS">
<tabs-item icon="i-simple-icons-linux" label="Ubuntu">

Ensure your Bluetooth Adaptor supports Bluetooth 5.0 and advertising extensions.

<tip>

Some older Bluetooth hardware might be compatible, but might not work out of the box.<br />


Updating Bluetooth Drivers may help.

</tip>

<caution>

Native Bluetooth is not available when using WSL.
Utilise the Infuse-IoT powered hardware options instead.

</caution>
</tabs-item>

<tabs-item icon="i-simple-icons:apple" label="macOS">

All Arm Silicon/aarch64 macOS devices support the Bluetooth requirements.

Support on Intel Mac devices will depend on hardware compatibility.
As a result this has not been tested or supported.

<note>

On MacOS, `native_bt` currently relies on an
[undocumented IOBluetooth API](https://bleak.readthedocs.io/en/latest/backends/macos.html#bleak.backends.corebluetooth.scanner.CBScannerArgs.use_bdaddr)

</note>
</tabs-item>

<tabs-item icon="i-simple-icons-windows" label="Windows">

Ensure your Bluetooth Adaptor supports Bluetooth 5.0 and advertising extensions.

<tip>

Some older Bluetooth hardware might be compatible, but might not work out of the box.<br />


Updating Bluetooth Drivers may help.

</tip>
</tabs-item>
</tabs>

## Embedded Devices

To create a bridge to a physically connected device, use the `infuse gateway` command.
This command can be used both for firmrware applications that forward data over Bluetooth (such as `infuse-sdk/apps/gateway_usb`)
or devices that only interact over the physical link, or even just to view device debug logs.

### Supported backends

An appropriate physical connection interface (backend) must be provided to the tool depending on the physical connection:

```bash
> infuse gateway --help
usage: infuse gateway [-h] (--serial SERIAL | --rtt RTT | --pyocd PYOCD) [--display-only] [--log [filename]]

Connect to a gateway device over serial and route commands to Bluetooth devices

options:
  -h, --help            show this help message and exit
  --serial SERIAL       Gateway serial port
  --rtt RTT             RTT serial port
  --pyocd PYOCD         RTT via PyOCD
  --display-only, -d    No networking, only display serial
  --log [filename], -l [filename]
                        Save serial output to file
  --baud BAUD           Baudrate for serial port
  --root ROOT           Root identity certificate to use instead of cloud
  --server-port SERVER_SOCK
                        Alternate port to use for Gateway connections (default 8751)
```

- `--serial` is used for standard serial ports (e.g. `COMx`, `/dev/ttyACMx`).
- `--rtt` is used for RTT through [pylink](https://github.com/square/pylink).
The argument is the device name (e.g. `nRF52840_xxAA`)
- `--pyocd` is used for RTT through [pyocd](https://github.com/pyocd/pyOCD).
The argument is the device name (e.g. `stm32l451ceux`)

### `--display-only`: Only display serial output

As the tool is also useful to display basic text logging message displays,
the networking aspect of the tool can be disabled with `--display-only` or `-d`.

### `--log`: Record serial logs to a file

Recording of the serial logs to a dedicated file can be enabled with the `--log` parameter.
This can be useful for long lived monitoring that would otherwise be lost due to terminal scrollback limits.

### `--baud`: Specify the baud rate for serial connections

When using `--serial` the baud rate can be specified with `--baud`.
