From 0974e138b0b7d1be8fa98a2f02b2ea5c1d064cc3 Mon Sep 17 00:00:00 2001 From: Connor Rigby Date: Thu, 21 Sep 2023 08:53:17 -0600 Subject: [PATCH] Fix tests and mix format Signed-off-by: Connor Rigby --- lib/blue_heron/acl.ex | 5 +- lib/blue_heron/acl_buffer.ex | 2 +- lib/blue_heron/address.ex | 2 +- .../att/responses/read_by_type_response.ex | 4 +- lib/blue_heron/gatt/server.ex | 5 +- .../set_controller_to_host_flow_control.ex | 2 +- .../hci/commands/link_control/disconnect.ex | 2 +- .../le_meta/connection_update_complete.ex | 6 - .../enhanced_connection_complete_v1.ex | 4 +- lib/blue_heron/hci/transport.ex | 4 +- lib/blue_heron/peripheral.ex | 2 +- test/blue_heron/acl_test.exs | 2 +- test/blue_heron/gatt/server_test.exs | 8 + .../regression/btstack_govee_btled_test.exs | 233 +++++++++--------- 14 files changed, 150 insertions(+), 131 deletions(-) diff --git a/lib/blue_heron/acl.ex b/lib/blue_heron/acl.ex index 2a584f2..77613b9 100644 --- a/lib/blue_heron/acl.ex +++ b/lib/blue_heron/acl.ex @@ -4,8 +4,11 @@ defmodule BlueHeron.ACL do defstruct [:handle, :flags, :data] - def deserialize(<>) do + def deserialize( + <> + ) do data = BlueHeron.L2Cap.deserialize(acl_data) + %ACL{ handle: handle, flags: %{pb: pb, bc: bc}, diff --git a/lib/blue_heron/acl_buffer.ex b/lib/blue_heron/acl_buffer.ex index d676eec..3bd5615 100644 --- a/lib/blue_heron/acl_buffer.ex +++ b/lib/blue_heron/acl_buffer.ex @@ -57,7 +57,7 @@ defmodule BlueHeron.ACLBuffer do {:noreply, state} end - def handle_info({:HCI_EVENT_PACKET, %NumberOfCompletedPackets{} = event}, state) do + def handle_info({:HCI_EVENT_PACKET, %NumberOfCompletedPackets{} = _event}, state) do send(self(), :out) {:noreply, state} end diff --git a/lib/blue_heron/address.ex b/lib/blue_heron/address.ex index 2874e70..b3352a9 100644 --- a/lib/blue_heron/address.ex +++ b/lib/blue_heron/address.ex @@ -84,6 +84,6 @@ defmodule BlueHeron.Address do end defp format_iodata(iodata) do - to_string(:io_lib.format('~2.16.0B:~2.16.0B:~2.16.0B:~2.16.0B:~2.16.0B:~2.16.0B', iodata)) + to_string(:io_lib.format(~c"~2.16.0B:~2.16.0B:~2.16.0B:~2.16.0B:~2.16.0B:~2.16.0B", iodata)) end end diff --git a/lib/blue_heron/att/responses/read_by_type_response.ex b/lib/blue_heron/att/responses/read_by_type_response.ex index f7c40f6..f1632d0 100644 --- a/lib/blue_heron/att/responses/read_by_type_response.ex +++ b/lib/blue_heron/att/responses/read_by_type_response.ex @@ -42,7 +42,7 @@ defmodule BlueHeron.ATT.ReadByTypeResponse do when uuid > 0xFFFF do <> - end + end def serialize(%{ handle: handle, @@ -51,7 +51,7 @@ defmodule BlueHeron.ATT.ReadByTypeResponse do uuid: _uuid, value: value }) do - <> + <> end end diff --git a/lib/blue_heron/gatt/server.ex b/lib/blue_heron/gatt/server.ex index 1ef1fb2..66a6d35 100644 --- a/lib/blue_heron/gatt/server.ex +++ b/lib/blue_heron/gatt/server.ex @@ -460,11 +460,14 @@ defmodule BlueHeron.GATT.Server do [characteristic] -> # TODO: Handle exceptions and long values value = state.mod.read(characteristic.id) + attr = %ReadByTypeResponse.AttributeData{ handle: characteristic.handle, uuid: characteristic.type, - value: value + value: value, + characteristic_properties: characteristic.properties, + characteristic_value_handle: characteristic.value_handle } {state, %ReadByTypeResponse{attribute_data: [attr]}} diff --git a/lib/blue_heron/hci/commands/controller_and_baseband/set_controller_to_host_flow_control.ex b/lib/blue_heron/hci/commands/controller_and_baseband/set_controller_to_host_flow_control.ex index a198b92..ab85a4a 100644 --- a/lib/blue_heron/hci/commands/controller_and_baseband/set_controller_to_host_flow_control.ex +++ b/lib/blue_heron/hci/commands/controller_and_baseband/set_controller_to_host_flow_control.ex @@ -1,7 +1,7 @@ defmodule BlueHeron.HCI.Command.ControllerAndBaseband.SetControllerToHostFlowControl do use BlueHeron.HCI.Command.ControllerAndBaseband, ocf: 0x0031 - defparameters [flow_control_enable: 0] + defparameters flow_control_enable: 0 defimpl BlueHeron.HCI.Serializable do def serialize(%{opcode: opcode, flow_control_enable: flow_control_enable}) do diff --git a/lib/blue_heron/hci/commands/link_control/disconnect.ex b/lib/blue_heron/hci/commands/link_control/disconnect.ex index 4a3e23b..85b1a0c 100644 --- a/lib/blue_heron/hci/commands/link_control/disconnect.ex +++ b/lib/blue_heron/hci/commands/link_control/disconnect.ex @@ -1,7 +1,7 @@ defmodule BlueHeron.HCI.Command.LinkControl.Disconnect do use BlueHeron.HCI.Command.LinkControl, ocf: 0x0006 - defparameters [reason: 0x16, connection_handle: 0] + defparameters reason: 0x16, connection_handle: 0 defimpl BlueHeron.HCI.Serializable do def serialize(%{opcode: opcode, connection_handle: handle, reason: reason}) do diff --git a/lib/blue_heron/hci/events/le_meta/connection_update_complete.ex b/lib/blue_heron/hci/events/le_meta/connection_update_complete.ex index eb52c3e..1e3fdb6 100644 --- a/lib/blue_heron/hci/events/le_meta/connection_update_complete.ex +++ b/lib/blue_heron/hci/events/le_meta/connection_update_complete.ex @@ -10,12 +10,6 @@ defmodule BlueHeron.HCI.Event.LEMeta.ConnectionUpdateComplete do :status ] - defimpl BlueHeron.HCI.Serializable do - def serialize(cc) do - raise(RuntimeError, "fixme") - end - end - @impl BlueHeron.HCI.Event def deserialize(<<@code, _size, @subevent_code, bin::binary>>) do << diff --git a/lib/blue_heron/hci/events/le_meta/enhanced_connection_complete_v1.ex b/lib/blue_heron/hci/events/le_meta/enhanced_connection_complete_v1.ex index 533cee2..5738e13 100644 --- a/lib/blue_heron/hci/events/le_meta/enhanced_connection_complete_v1.ex +++ b/lib/blue_heron/hci/events/le_meta/enhanced_connection_complete_v1.ex @@ -13,7 +13,7 @@ defmodule BlueHeron.HCI.Event.LEMeta.EnhancedConnectionCompleteV1 do :connection_interval, :peripheral_latency, :supervision_timeout, - :central_clock_accuracy, + :central_clock_accuracy ] defimpl BlueHeron.HCI.Serializable do @@ -72,7 +72,7 @@ defmodule BlueHeron.HCI.Event.LEMeta.EnhancedConnectionCompleteV1 do supervision_timeout: supervision_timeout, central_clock_accuracy: central_clock_accuracy, local_resolvable_private_address: local_resolvable_private_address, - peer_resolvable_private_address: peer_resolvable_private_address, + peer_resolvable_private_address: peer_resolvable_private_address } end diff --git a/lib/blue_heron/hci/transport.ex b/lib/blue_heron/hci/transport.ex index 290b711..0a86314 100644 --- a/lib/blue_heron/hci/transport.ex +++ b/lib/blue_heron/hci/transport.ex @@ -296,7 +296,7 @@ defmodule BlueHeron.HCI.Transport do :keep_state_and_data end - def ready(:info, {:transport_data, <<0x01, unknown::binary>>}, data) do + def ready(:info, {:transport_data, <<0x01, unknown::binary>>}, _data) do Logger.warning(%{unexpected_data: inspect(unknown, base: :hex)}) :keep_state_and_data end @@ -326,7 +326,7 @@ defmodule BlueHeron.HCI.Transport do for pid <- data.handlers, do: send(pid, {:HCI_EVENT_PACKET, reply}) {:ok, reply, data} - %{opcode: opcode} = reply -> + %{opcode: _opcode} = reply -> {:error, reply, data} %{} = reply -> diff --git a/lib/blue_heron/peripheral.ex b/lib/blue_heron/peripheral.ex index d545315..e7cca90 100644 --- a/lib/blue_heron/peripheral.ex +++ b/lib/blue_heron/peripheral.ex @@ -47,7 +47,7 @@ defmodule BlueHeron.Peripheral do :gatt_server, :smp_server, :smp_handler, - :acl_buffer, + :acl_buffer ] @doc "Start a non-secured peripheral" diff --git a/test/blue_heron/acl_test.exs b/test/blue_heron/acl_test.exs index f946d39..b87fd18 100644 --- a/test/blue_heron/acl_test.exs +++ b/test/blue_heron/acl_test.exs @@ -15,7 +15,7 @@ defmodule BlueHeron.ACLTest do } |> ACL.serialize() - assert <<64, 32, 7, 0, 3, 0, 4, 0, 2, 185, 0>> == serialized + assert <<64, 2, 7, 0, 3, 0, 4, 0, 2, 185, 0>> == serialized end test "serde is symmetric" do diff --git a/test/blue_heron/gatt/server_test.exs b/test/blue_heron/gatt/server_test.exs index 85754dc..b7e1548 100644 --- a/test/blue_heron/gatt/server_test.exs +++ b/test/blue_heron/gatt/server_test.exs @@ -109,10 +109,18 @@ defmodule BlueHeron.GATT.ServerTest do "a-value-longer-than-22-bytes" end + def read({:custom_service_1, :long_uuid}) do + "a-value-longer-than-22-bytes" + end + def read({:gap, :service_changed}) do "" end + def read({:gap, :appearance}) do + <<0x008D::little-16>> + end + @impl Server def write({:custom_service_1, :short_uuid}, value) do send(self(), value) diff --git a/test/blue_heron/regression/btstack_govee_btled_test.exs b/test/blue_heron/regression/btstack_govee_btled_test.exs index 3b6221d..a01f005 100644 --- a/test/blue_heron/regression/btstack_govee_btled_test.exs +++ b/test/blue_heron/regression/btstack_govee_btled_test.exs @@ -945,11 +945,11 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, - data: %BlueHeron.ATT.ExchangeMTUResponse{opcode: 3, server_rx_mtu: 23} + data: %BlueHeron.ATT.ExchangeMTUResponse{opcode: 3, server_rx_mtu: 23}, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -986,30 +986,30 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByGroupTypeResponse{ + opcode: 17, attribute_data: [ %BlueHeron.ATT.ReadByGroupTypeResponse.AttributeData{ - end_group_handle: 7, handle: 1, + end_group_handle: 7, uuid: 6144 }, %BlueHeron.ATT.ReadByGroupTypeResponse.AttributeData{ - end_group_handle: 11, handle: 8, + end_group_handle: 11, uuid: 6145 }, %BlueHeron.ATT.ReadByGroupTypeResponse.AttributeData{ - end_group_handle: 14, handle: 12, + end_group_handle: 14, uuid: 6154 } - ], - opcode: 17 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1046,20 +1046,20 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByGroupTypeResponse{ + opcode: 17, attribute_data: [ %BlueHeron.ATT.ReadByGroupTypeResponse.AttributeData{ - end_group_handle: 22, handle: 15, + end_group_handle: 22, uuid: 5_233_100_606_242_806_050_955_395_731_364_112 } - ], - opcode: 17 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1096,20 +1096,20 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByGroupTypeResponse{ + opcode: 17, attribute_data: [ %BlueHeron.ATT.ReadByGroupTypeResponse.AttributeData{ - end_group_handle: 26, handle: 23, + end_group_handle: 26, uuid: 5_233_100_606_242_806_050_955_395_731_364_114 } - ], - opcode: 17 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1144,16 +1144,16 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ErrorResponse{ - error: :attribute_not_found, - handle: 27, opcode: 1, - request_opcode: 16 - } + request_opcode: 16, + handle: 27, + error: :attribute_not_found + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1190,33 +1190,36 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByTypeResponse{ + opcode: 9, attribute_data: [ %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 2, characteristic_properties: 18, characteristic_value_handle: 3, - handle: 2, + value: nil, uuid: 10752 }, %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 4, characteristic_properties: 2, characteristic_value_handle: 5, - handle: 4, + value: nil, uuid: 10753 }, %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 6, characteristic_properties: 2, characteristic_value_handle: 7, - handle: 6, + value: nil, uuid: 10756 } - ], - opcode: 9 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1253,33 +1256,36 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByTypeResponse{ + opcode: 9, attribute_data: [ %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 2, characteristic_properties: 18, characteristic_value_handle: 3, - handle: 2, + value: nil, uuid: 10752 }, %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 4, characteristic_properties: 2, characteristic_value_handle: 5, - handle: 4, + value: nil, uuid: 10753 }, %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 6, characteristic_properties: 2, characteristic_value_handle: 7, - handle: 6, + value: nil, uuid: 10756 } - ], - opcode: 9 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1292,13 +1298,13 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByTypeRequest{ - ending_handle: 11, opcode: 8, starting_handle: 8, + ending_handle: 11, uuid: 10243 - } + }, + cid: 4 }, flags: %{bc: 0, pb: 0}, handle: 16 @@ -1316,21 +1322,22 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByTypeResponse{ + opcode: 9, attribute_data: [ %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 9, characteristic_properties: 32, characteristic_value_handle: 10, - handle: 9, + value: nil, uuid: 10757 } - ], - opcode: 9 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1365,16 +1372,16 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ErrorResponse{ - error: :attribute_not_found, - handle: 11, opcode: 1, - request_opcode: 8 - } + request_opcode: 8, + handle: 11, + error: :attribute_not_found + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1411,21 +1418,22 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByTypeResponse{ + opcode: 9, attribute_data: [ %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 13, characteristic_properties: 2, characteristic_value_handle: 14, - handle: 13, + value: nil, uuid: 10832 } - ], - opcode: 9 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1462,21 +1470,22 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByTypeResponse{ + opcode: 9, attribute_data: [ %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 16, characteristic_properties: 18, characteristic_value_handle: 17, - handle: 16, + value: nil, uuid: 5_233_100_606_242_806_050_955_395_731_368_720 } - ], - opcode: 9 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1513,21 +1522,22 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByTypeResponse{ + opcode: 9, attribute_data: [ %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 20, characteristic_properties: 6, characteristic_value_handle: 21, - handle: 20, + value: nil, uuid: 5_233_100_606_242_806_050_955_395_731_368_721 } - ], - opcode: 9 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1562,16 +1572,16 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ErrorResponse{ - error: :attribute_not_found, - handle: 22, opcode: 1, - request_opcode: 8 - } + request_opcode: 8, + handle: 22, + error: :attribute_not_found + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1631,21 +1641,22 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ReadByTypeResponse{ + opcode: 9, attribute_data: [ %BlueHeron.ATT.ReadByTypeResponse.AttributeData{ + handle: 24, characteristic_properties: 6, characteristic_value_handle: 25, - handle: 24, + value: nil, uuid: 5_233_100_606_242_806_050_955_395_731_368_722 } - ], - opcode: 9 - } + ] + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary) @@ -1680,16 +1691,16 @@ defmodule BlueHeronRegressionTest do expected = %BlueHeron.ACL{ data: %BlueHeron.L2Cap{ - cid: 4, data: %BlueHeron.ATT.ErrorResponse{ - error: :attribute_not_found, - handle: 26, opcode: 1, - request_opcode: 8 - } + request_opcode: 8, + handle: 26, + error: :attribute_not_found + }, + cid: 4 }, - flags: %{bc: 2, pb: 0}, - handle: 16 + flags: %{bc: 0, pb: 0}, + handle: 528 } actual = BlueHeron.ACL.deserialize(binary)