Skip to content

Commit

Permalink
Flush transport on timeout during setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorRigby committed Jan 13, 2025
1 parent 396b223 commit 3c30240
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/blue_heron/hci/transport.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ defmodule BlueHeron.HCI.Transport do
@type command_complete :: %BlueHeron.HCI.Event.CommandComplete{}
@type command_status :: %BlueHeron.HCI.Event.CommandStatus{}

@doc "Checks if setup is complete on the transport"
@spec setup_complete?() :: boolean()
def setup_complete? do
GenServer.call(__MODULE__, :setup_complete?)
end

@doc "Send an HCI frame"
@spec send_hci(map()) ::
{:ok, command_complete() | command_status()} | {:error, :setup_incomplete | :timeout}
Expand Down Expand Up @@ -182,6 +188,7 @@ defmodule BlueHeron.HCI.Transport do
nil ->
Logger.warning("Setup command timeout: #{inspect(state.current)}")
hci_bin = serialize(state.current)
:ok = BlueHeron.HCI.Transport.UART.flush(state.transport)
:ok = BlueHeron.HCI.Transport.UART.send_command(state.transport, hci_bin)
timer = Process.send_after(self(), :current_timeout, 5000)
{:noreply, %{new_state | current_timer: timer}}
Expand Down Expand Up @@ -309,6 +316,10 @@ defmodule BlueHeron.HCI.Transport do
{:reply, :ok, state}
end

def handle_call(:setup_complete, _from, %{setup_complete: setup_complete} = state) do
{:reply, setup_complete, state}
end

def handle_call(_call, _from, %{setup_complete: false} = state) do
{:reply, {:error, :setup_incomplete}, state}
end
Expand Down
10 changes: 10 additions & 0 deletions lib/blue_heron/hci/transport/uart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ defmodule BlueHeron.HCI.Transport.UART do
GenServer.call(pid, {:send, [<<@hci_acl_packet::8>>, acl]})
end

@doc "Flush buffers"
@spec flush(GenServer.server()) :: :ok
def flush(pid) do
GenServer.call(pid, :flush)
end

## Server Callbacks

@impl GenServer
Expand All @@ -48,6 +54,10 @@ defmodule BlueHeron.HCI.Transport.UART do
{:reply, UART.write(uart_pid, command), state}
end

def handle_call(:flush, _from, %{uart_pid: uart_pid} = state) do
{:reply, UART.flush(uart_pid), state}
end

@impl GenServer
def handle_info({:open, device, opts}, state) when is_binary(device) and is_list(opts) do
case UART.open(state.uart_pid, device, opts) do
Expand Down

0 comments on commit 3c30240

Please sign in to comment.