From 6e5ec7765195762e4b7f4c45b421365e651aa727 Mon Sep 17 00:00:00 2001 From: Jean Parpaillon Date: Tue, 8 Dec 2020 14:25:05 +0100 Subject: [PATCH 1/6] Upgrade dialyxir --- mix.exs | 2 +- mix.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mix.exs b/mix.exs index 45e06f4..82aea39 100644 --- a/mix.exs +++ b/mix.exs @@ -25,7 +25,7 @@ defmodule Coap.MixProject do {:plug, "~> 1.0"}, {:gen_coap, github: "gotthardp/gen_coap", only: [:dev, :test]}, {:stream_data, "~> 0.1", only: :test}, - {:dialyxir, "~> 1.0.0-rc.6", only: :dev, runtime: false}, + {:dialyxir, "~> 1.0.0", only: :dev, runtime: false}, {:ex_doc, "~> 0.19", only: :dev, runtime: false}, {:telemetry, "~> 0.4.0"} ] diff --git a/mix.lock b/mix.lock index 93b8a8d..f91ff67 100644 --- a/mix.lock +++ b/mix.lock @@ -1,7 +1,7 @@ %{ - "dialyxir": {:hex, :dialyxir, "1.0.0-rc.6", "78e97d9c0ff1b5521dd68041193891aebebce52fc3b93463c0a6806874557d7d", [:mix], [{:erlex, "~> 0.2.1", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "49496d63267bc1a4614ffd5f67c45d9fc3ea62701a6797975bc98bc156d2763f"}, + "dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"}, "earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm", "e3be2bc3ae67781db529b80aa7e7c49904a988596e2dbff897425b48b3581161"}, - "erlex": {:hex, :erlex, "0.2.1", "cee02918660807cbba9a7229cae9b42d1c6143b768c781fa6cee1eaf03ad860b", [:mix], [], "hexpm", "df65aa8e1e926941982b208f5957158a52b21fbba06ba8141fff2b8c5ce87574"}, + "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "8e24fc8ff9a50b9f557ff020d6c91a03cded7e59ac3e0eec8a27e771430c7d27"}, "gen_coap": {:git, "https://github.com/gotthardp/gen_coap.git", "245e2205cf233e94e2d481f6ddda5da038ebc6ea", []}, "makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5fbc8e549aa9afeea2847c0769e3970537ed302f93a23ac612602e805d9d1e7f"}, From 9d2ea8049a947ca2e5f65ca7c21a0102c1ec8c5e Mon Sep 17 00:00:00 2001 From: Jean Parpaillon Date: Tue, 8 Dec 2020 14:37:42 +0100 Subject: [PATCH 2/6] Fix typespecs issues --- lib/coap/block.ex | 7 +------ lib/coap/message.ex | 28 +++++++++++++--------------- lib/coap/multipart.ex | 9 ++------- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/lib/coap/block.ex b/lib/coap/block.ex index 9931496..59d2545 100644 --- a/lib/coap/block.ex +++ b/lib/coap/block.ex @@ -67,32 +67,27 @@ defmodule CoAP.Block do } end - @spec encode(t()) :: binary_t() + @spec encode(t() | tuple_t()) :: binary_t() def encode(%__MODULE__{} = block), do: encode({block.number, block.more, block.size}) @spec encode(%{number: integer, more: 0 | 1, size: integer}) :: binary_t() def encode(%{number: number, more: more, size: size}), do: encode({number, more, size}) - @spec encode({integer, boolean, 0}) :: binary_t() def encode({number, more, 0}) do encode(number, if(more, do: 1, else: 0), 0) end - @spec encode({integer, boolean, integer}) :: binary_t() def encode({number, more, size}) do encode(number, if(more, do: 1, else: 0), trunc(:math.log2(size)) - 4) end - @spec encode(integer, 0 | 1, integer) :: binary_t_small() def encode(number, more, size_exponent) when number < 16 do <> end - @spec encode(integer, 0 | 1, integer) :: binary_t_medium() def encode(number, more, size_exponent) when number < 4096 do <> end - @spec encode(integer, 0 | 1, integer) :: binary_t_large() def encode(number, more, size_exponent) do <> end diff --git a/lib/coap/message.ex b/lib/coap/message.ex index 2b840a6..038fa54 100644 --- a/lib/coap/message.ex +++ b/lib/coap/message.ex @@ -64,18 +64,20 @@ defmodule CoAP.Message do @type status_code :: {integer, integer} @type status_t :: nil | {atom, atom} + @type request_type :: :con | :non | :ack | :reset + @type t :: %__MODULE__{ version: integer, type: request_type, - request: boolean, + request: boolean | nil, code_class: integer, code_detail: integer, - method: request_method | {integer, integer}, + method: request_method | nil | {integer, integer}, status: status_t, message_id: integer, token: binary, options: map, - multipart: CoAP.Multipart.t(), + multipart: CoAP.Multipart.t() | nil, payload: binary, raw_size: integer } @@ -88,8 +90,6 @@ defmodule CoAP.Message do } @types_map Enum.into(@types, %{}, fn {k, v} -> {v, k} end) - @type request_type :: :con | :non | :ack | :reset - @message_header_format (quote do << var!(version)::unsigned-integer-size(2), @@ -130,11 +130,10 @@ defmodule CoAP.Message do # Always check code_detail in case the message was made directly, not decoded blocks = Multipart.as_blocks(request?(message.code_class), message.multipart) - %{message | options: Map.merge(message.options, blocks), multipart: nil} + %__MODULE__{message | options: Map.merge(message.options, blocks), multipart: nil} |> encode() end - @spec encode(t()) :: binary def encode(%__MODULE__{ version: version, type: type, @@ -324,19 +323,19 @@ defmodule CoAP.Message do Multipart.build(request, options[:block1], options[:block2]) end - @spec request?(0) :: true + @spec request?(any) :: boolean defp request?(0), do: true - @spec request?(any) :: false + defp request?(_), do: false - @spec method_for(0, integer) :: request_method() + @spec method_for(any, any) :: request_method() | nil defp method_for(0, code_detail), do: @methods[{0, code_detail}] - @spec method_for(any, any) :: nil + defp method_for(_code_class, _code_detail), do: nil - @spec status_for(0, any) :: nil + @spec status_for(integer, any) :: nil | status_t defp status_for(0, _code_detail), do: nil - @spec status_for(integer, integer) :: status_t + defp status_for(code_class, code_detail), do: @methods[{code_class, code_detail}] @doc """ @@ -371,7 +370,7 @@ defmodule CoAP.Message do """ @spec next_message(t(), integer) :: t() def next_message(%__MODULE__{} = message, next_message_id) do - %{message | message_id: next_message_id, payload: <<>>, multipart: nil} + %__MODULE__{message | message_id: next_message_id, payload: <<>>, multipart: nil} end @doc """ @@ -386,7 +385,6 @@ defmodule CoAP.Message do } end - @spec response_for(t()) :: t() def response_for(%__MODULE__{type: :non} = message) do %__MODULE__{ type: :non, diff --git a/lib/coap/multipart.ex b/lib/coap/multipart.ex index a8bce17..98a044b 100644 --- a/lib/coap/multipart.ex +++ b/lib/coap/multipart.ex @@ -32,22 +32,19 @@ defmodule CoAP.Multipart do requested_number: integer } - @spec build(any, nil, nil) :: t() + @spec build(any, any, any) :: t() def build(_request, nil, nil), do: %__MODULE__{} # Request variation - @spec build(boolean, CoAP.Block.t(), CoAP.Block.t()) :: t() def build(true, block1, block2) do build(Block.build(block1), Block.build(block2)) end # Response variation - @spec build(boolean, CoAP.Block.t(), CoAP.Block.t()) :: t() def build(false, block1, block2) do build(Block.build(block2), Block.build(block1)) end - @spec build(CoAP.Block.t(), CoAP.Block.t()) :: t() def build(%Block{} = description, %Block{} = control) do %__MODULE__{ multipart: true, @@ -61,7 +58,7 @@ defmodule CoAP.Multipart do } end - @spec build(nil, CoAP.Block.t()) :: t() + @spec build(CoAP.Block.t() | nil, CoAP.Block.t() | nil) :: t() def build(nil, %Block{} = control) do %__MODULE__{ multipart: true, @@ -72,7 +69,6 @@ defmodule CoAP.Multipart do } end - @spec build(CoAP.Block.t(), nil) :: t() def build(%Block{} = description, nil) do case {description.more, description.number} do {false, 0} -> @@ -92,7 +88,6 @@ defmodule CoAP.Multipart do end end - @spec build(nil, nil) :: t() def build(nil, nil), do: %__MODULE__{multipart: false, description: nil, control: nil} @spec as_blocks(true, CoAP.Multipart.t()) :: %{block1: CoAP.Block.t(), block2: CoAP.Block.t()} From 65867ca960b73dec016e84a40bd5e65801f4484b Mon Sep 17 00:00:00 2001 From: Jean Parpaillon Date: Tue, 8 Dec 2020 15:24:01 +0100 Subject: [PATCH 3/6] Adds credo and fix TODOs --- lib/coap/adapters/phoenix.ex | 1 - lib/coap/block.ex | 2 +- lib/coap/client.ex | 2 +- lib/coap/connection.ex | 37 ++++++++++++++---------------------- lib/coap/handler.ex | 4 ---- lib/coap/message.ex | 2 +- lib/coap/message_options.ex | 5 ----- lib/coap/multipart.ex | 5 +---- lib/coap/payload.ex | 1 - lib/coap/phoenix/conn.ex | 4 ---- lib/coap/phoenix/listener.ex | 7 +++---- lib/coap/phoenix/request.ex | 1 - lib/coap/records.ex | 21 -------------------- lib/coap/socket_server.ex | 4 ---- mix.exs | 8 ++++++-- mix.lock | 4 ++++ test/coap/multipart_test.exs | 3 --- test/coap/payload_test.exs | 2 -- 18 files changed, 31 insertions(+), 82 deletions(-) delete mode 100644 lib/coap/records.ex diff --git a/lib/coap/adapters/phoenix.ex b/lib/coap/adapters/phoenix.ex index 332b2a6..48bb0f7 100644 --- a/lib/coap/adapters/phoenix.ex +++ b/lib/coap/adapters/phoenix.ex @@ -29,7 +29,6 @@ defmodule CoAP.Adapters.Phoenix do end defp process(req, {endpoint, opts}) do - # TODO: conn_from_message(message) case endpoint.__handler__(@connection.conn(req), opts) do {:plug, conn, handler, opts} -> %{adapter: {@connection, _req}} = diff --git a/lib/coap/block.ex b/lib/coap/block.ex index 59d2545..eb915a2 100644 --- a/lib/coap/block.ex +++ b/lib/coap/block.ex @@ -8,7 +8,7 @@ defmodule CoAP.Block do @type binary_t_large :: <<_::32>> @type binary_t :: binary_t_small | binary_t_medium | binary_t_large - # TODO: if more: false, a size_exponent of 0 should be ignored? + # _TODO: if more: false, a size_exponent of 0 should be ignored? # otherwise size_exponent of 0 results in size: 16 @doc """ diff --git a/lib/coap/client.ex b/lib/coap/client.ex index bd5e0ea..2aeb3ee 100644 --- a/lib/coap/client.ex +++ b/lib/coap/client.ex @@ -32,7 +32,7 @@ defmodule CoAP.Client do tag: nil end - # TODO: options: headers/params? + # _TODO: options: headers/params? @doc """ Perform a confirmable, GET request to a URL diff --git a/lib/coap/connection.ex b/lib/coap/connection.ex index 771214a..2eff291 100644 --- a/lib/coap/connection.ex +++ b/lib/coap/connection.ex @@ -163,8 +163,6 @@ defmodule CoAP.Connection do def start_link(args), do: GenServer.start_link(__MODULE__, args) - # TODO: predefined defaults, merged with client/server-specific options - # TODO: default adapter to GenericServer? @doc """ `init` functions for Server and Client processes @@ -214,10 +212,6 @@ defmodule CoAP.Connection do end def handle_info({:receive, %Message{} = message}, state) do - # TODO: connection timeouts - # TODO: start timer for conn - # TODO: check if the message_id matches what we may have already sent? - :telemetry.execute( [:coap_ex, :connection, :data_received], %{size: message.raw_size}, @@ -251,7 +245,7 @@ defmodule CoAP.Connection do def handle_info({:tag, tag}, state), do: {:noreply, %{state | tag: tag}} - # TODO: connection timeout, set to original state? + # _TODO: connection timeout, set to original state? # def handle_info(:retry, state) @@ -260,17 +254,17 @@ defmodule CoAP.Connection do # RECEIVE ==================================================================== # con -> reset - # TODO: how do we get a nil method, vs a response + # _TODO: how do we get a nil method, vs a response # defp receive_message(%Message{method: nil, type: :con} = message, %{phase: :idle} = state) do - # TODO: peer ack with reset, next state is peer_ack_sent + # _TODO: peer ack with reset, next state is peer_ack_sent # Message.response_for(message) # reply(:reset, message, state[:server]) # end - # TODO: resend reset? - # TODO: what is the message if the client has to re-request after a processing timeout from the app? + # _TODO: resend reset? + # _TODO: what is the message if the client has to re-request after a processing timeout from the app? - # TODO: resend stored message (ack) + # _TODO: resend stored message (ack) defp receive_message(_message, %{phase: :peer_ack_sent} = state), do: state # Do nothing if we receive a message from peer during these states; we should be shutting down @@ -355,7 +349,7 @@ defmodule CoAP.Connection do ) do timer = restart_timer(state.timer, ack_timeout(state)) - # TODO: respect the number/size from control + # _TODO: respect the number/size from control # more must be false, must use same size on subsequent request multipart = Multipart.build(nil, Block.build({number + 1, false, size})) @@ -363,7 +357,7 @@ defmodule CoAP.Connection do response = case message.status do # client sends original message with new control number - # TODO: what parts of the message are we supposed to send back? + # _TODO: what parts of the message are we supposed to send back? {:ok, _} -> Message.next_message(state.message, next_message_id(state.message.message_id)) @@ -456,7 +450,7 @@ defmodule CoAP.Connection do defp receive_message(message, %{phase: :awaiting_peer_ack} = state) do cancel_timer(state.timer) - # TODO: what happens if this is an empty ACK and we get another response later? + # _TODO: what happens if this is an empty ACK and we get another response later? # Could we go back to being idle? handle(message, state.handler, peer_for(state)) @@ -464,17 +458,17 @@ defmodule CoAP.Connection do end # DELIVER ==================================================================== - # TODO: deliver message for got_non as a NON message, phase becomes :sent_non - # TODO: deliver message for peer_ack_sent as a CON message, phase becomes :awaiting_peer_ack + # _TODO: deliver message for got_non as a NON message, phase becomes :sent_non + # _TODO: deliver message for peer_ack_sent as a CON message, phase becomes :awaiting_peer_ack defp deliver_message(message, %{phase: :awaiting_app_ack} = state) do - # TODO: does the message include the original request control? + # _TODO: does the message include the original request control? {bytes, block, payload} = Payload.segment_at(message.payload, @default_payload_size, 0) # Cancel the app_ack waiting timeout cancel_timer(state.timer) - # TODO: what happens if the app response is a status code, no code_class/detail tuple? + # _TODO: what happens if the app response is a status code, no code_class/detail tuple? response = Message.response_for( {message.code_class, message.code_detail}, @@ -501,7 +495,7 @@ defmodule CoAP.Connection do %Message{type: type, message_id: message_id} = message, %{phase: :idle, next_message_id: next_message_id} = state ) do - # TODO: get payload size from the request control + # _TODO: get payload size from the request control {bytes, block, payload} = Payload.segment_at(message.payload, @default_payload_size, 0) # The server should send back the same message id of the request @@ -562,7 +556,6 @@ defmodule CoAP.Connection do ack_timeout(state) _ -> - # TODO: exponential backoff timeout * 2 end @@ -676,7 +669,6 @@ defmodule CoAP.Connection do end # HANDLER - # TODO: move to CoAP defp start_handler({adapter, endpoint}), do: start_handler(adapter, endpoint) defp start_handler(adapter, endpoint) do @@ -689,7 +681,6 @@ defmodule CoAP.Connection do ) end - # TODO: move to CoAP defp start_socket_for(endpoint, peer) do DynamicSupervisor.start_child( CoAP.SocketServerSupervisor, diff --git a/lib/coap/handler.ex b/lib/coap/handler.ex index 710dbe8..e4999e9 100644 --- a/lib/coap/handler.ex +++ b/lib/coap/handler.ex @@ -16,10 +16,6 @@ defmodule CoAP.Handler do {:ok, {adapter, endpoint}} end - # TODO: this process is blocked when calling endpoint - # TODO: we may want to instrument/log the queue depth here - # It _should not_ be an issue because this process is already per-peer connection - def handle_info({:request, message, peer, connection}, {adapter, endpoint} = state) do adapter.request(message, {endpoint, peer}, connection) {:noreply, state} diff --git a/lib/coap/message.ex b/lib/coap/message.ex index 038fa54..e32be19 100644 --- a/lib/coap/message.ex +++ b/lib/coap/message.ex @@ -316,7 +316,7 @@ defmodule CoAP.Message do %CoAP.Multipart{multipart: false} """ - # TODO: test if either block1 or block2 is nil + # _TODO: test if either block1 or block2 is nil @spec multipart(boolean, %{block1: CoAP.Block.tuple_t(), block2: CoAP.Block.tuple_t()}) :: CoAP.Multipart.t() def multipart(request, options) do diff --git a/lib/coap/message_options.ex b/lib/coap/message_options.ex index de1416a..7bace3a 100644 --- a/lib/coap/message_options.ex +++ b/lib/coap/message_options.ex @@ -1,8 +1,6 @@ defmodule CoAP.MessageOptions do # @payload_marker 0xFF - # TODO: struct for all options? - @doc """ Examples @@ -69,7 +67,6 @@ defmodule CoAP.MessageOptions do {tail, delta_sum + delta} delta == 13 -> - # TODO: size here `::size(4)`? <> = tail {new_tail1, delta_sum + key + 13} @@ -84,7 +81,6 @@ defmodule CoAP.MessageOptions do {tail1, length} length == 13 -> - # TODO: size here `::size(4)`? <> = tail1 {new_tail2, extended_option_length + 13} @@ -175,7 +171,6 @@ defmodule CoAP.MessageOptions do acc::binary, delta::size(4), length::size(4), - # TODO: what size should this be? extended_number::binary, extended_length::binary, value::binary diff --git a/lib/coap/multipart.ex b/lib/coap/multipart.ex index 98a044b..73bff5c 100644 --- a/lib/coap/multipart.ex +++ b/lib/coap/multipart.ex @@ -11,7 +11,6 @@ defmodule CoAP.Multipart do alias CoAP.Block - # TODO: redefine as description/control based on request/response defstruct multipart: false, description: nil, control: nil, @@ -90,7 +89,7 @@ defmodule CoAP.Multipart do def build(nil, nil), do: %__MODULE__{multipart: false, description: nil, control: nil} - @spec as_blocks(true, CoAP.Multipart.t()) :: %{block1: CoAP.Block.t(), block2: CoAP.Block.t()} + @spec as_blocks(boolean, CoAP.Multipart.t()) :: %{block1: CoAP.Block.t(), block2: CoAP.Block.t()} def as_blocks(true, multipart) do %{ block1: multipart.description |> Block.to_tuple(), @@ -99,8 +98,6 @@ defmodule CoAP.Multipart do |> reject_nil_values() end - # TODO: if we get nil here, that's wrong - @spec as_blocks(false, CoAP.Multipart.t()) :: %{block1: CoAP.Block.t(), block2: CoAP.Block.t()} def as_blocks(false, multipart) do %{ block1: multipart.control |> Block.to_tuple(), diff --git a/lib/coap/payload.ex b/lib/coap/payload.ex index 1ecc435..abad595 100644 --- a/lib/coap/payload.ex +++ b/lib/coap/payload.ex @@ -74,7 +74,6 @@ defmodule CoAP.Payload do part_size = Enum.min([data_size - offset, size]) more = data_size > offset + part_size - # TODO: splits into the appropriate segment data = data |> :binary.part(offset, part_size) block = Block.build({number, more, size}) diff --git a/lib/coap/phoenix/conn.ex b/lib/coap/phoenix/conn.ex index 1b303a0..d73f10e 100644 --- a/lib/coap/phoenix/conn.ex +++ b/lib/coap/phoenix/conn.ex @@ -27,14 +27,11 @@ defmodule CoAP.Phoenix.Conn do query_string: qs, req_headers: to_headers_list(headers), request_path: path, - # TODO: coaps scheme: "coap" } end def send_resp(req, status, _headers, body) do - # IO.puts("#{inspect(req)} returns #{inspect(status)}, #{inspect(headers)}, #{inspect(body)}") - message = req.message connection = req.owner @@ -46,7 +43,6 @@ defmodule CoAP.Phoenix.Conn do code_detail: code_detail, message_id: message.message_id, token: message.token, - # TODO: options from filtered headers payload: body } diff --git a/lib/coap/phoenix/listener.ex b/lib/coap/phoenix/listener.ex index 3a21407..ace5a5c 100644 --- a/lib/coap/phoenix/listener.ex +++ b/lib/coap/phoenix/listener.ex @@ -30,16 +30,15 @@ defmodule CoAP.Phoenix.Listener do GenServer.start_link(__MODULE__, endpoint) end - # TODO: spec for this def init(endpoint) do - # TODO: take this config and use it to start a CoAP.SocketServer + # _TODO: take this config and use it to start a CoAP.SocketServer config = endpoint.config(:coap) info("Starting CoAP.Phoenix.Listener: #{inspect(config)}") {:ok, server} = CoAP.SocketServer.start_link([{@adapter, endpoint}, config[:port], config]) - # TODO: ref and monitor? - # TODO: die if server dies? + # _TODO: ref and monitor? + # _TODO: die if server dies? {:ok, %{endpoint: endpoint, config: config, server: server}} end diff --git a/lib/coap/phoenix/request.ex b/lib/coap/phoenix/request.ex index 914d9e7..a8afd65 100644 --- a/lib/coap/phoenix/request.ex +++ b/lib/coap/phoenix/request.ex @@ -66,7 +66,6 @@ defmodule CoAP.Phoenix.Request do def build(%Message{options: options} = message, {address, port}, owner, config \\ %{}) do _ip_string = Enum.join(Tuple.to_list(address), ".") - # TODO: defstruct? %{ method: message |> method, path: options[:uri_path] |> Enum.join("/"), diff --git a/lib/coap/records.ex b/lib/coap/records.ex deleted file mode 100644 index 3afe339..0000000 --- a/lib/coap/records.ex +++ /dev/null @@ -1,21 +0,0 @@ -defmodule CoAP.Records do - require Record - - # TODO: remove/deprecate - - # Record.defrecord( - # :coap_content, - # Record.extract( - # :coap_content, - # from_lib: "gen_coap/include/coap.hrl" - # ) - # ) - # - # Record.defrecord( - # :coap_message, - # Record.extract( - # :coap_message, - # from_lib: "gen_coap/include/coap.hrl" - # ) - # ) -end diff --git a/lib/coap/socket_server.ex b/lib/coap/socket_server.ex index 7a3bd27..ff12c8b 100644 --- a/lib/coap/socket_server.ex +++ b/lib/coap/socket_server.ex @@ -57,7 +57,6 @@ defmodule CoAP.SocketServer do def init([endpoint, {host, port, token}, connection]) do {:ok, socket} = :gen_udp.open(0, [:binary, {:active, true}, {:reuseaddr, true}]) - # TODO: use handle_continue to do this ip = normalize_host(host) connection_id = {ip, port, token} @@ -154,8 +153,6 @@ defmodule CoAP.SocketServer do }:#{inspect(connection)}:#{inspect(ref)}" ) - # TODO: handle noproc - {:noreply, %{ state @@ -213,7 +210,6 @@ defmodule CoAP.SocketServer do end end - # TODO: move to CoAP defp start_connection(server, endpoint, peer, config) do DynamicSupervisor.start_child( CoAP.ConnectionSupervisor, diff --git a/mix.exs b/mix.exs index 82aea39..1b1647a 100644 --- a/mix.exs +++ b/mix.exs @@ -22,11 +22,15 @@ defmodule Coap.MixProject do # Run "mix help deps" to learn about dependencies. defp deps do [ - {:plug, "~> 1.0"}, + # Dev + {:dialyxir, "~> 1.0.0", only: :dev, runtime: false}, + {:credo, "~> 1.5"}, {:gen_coap, github: "gotthardp/gen_coap", only: [:dev, :test]}, {:stream_data, "~> 0.1", only: :test}, - {:dialyxir, "~> 1.0.0", only: :dev, runtime: false}, {:ex_doc, "~> 0.19", only: :dev, runtime: false}, + + # Runtime + {:plug, "~> 1.0"}, {:telemetry, "~> 0.4.0"} ] end diff --git a/mix.lock b/mix.lock index f91ff67..1fa88f2 100644 --- a/mix.lock +++ b/mix.lock @@ -1,9 +1,13 @@ %{ + "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, + "credo": {:hex, :credo, "1.5.2", "5562f1a1693f77e7319fdabac6d17d26de7e6b0a2b57743bca24a89469232f04", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "7003506f069866a4e5d6216a7216823b00ed4bcc4bd9c6e449fa6625c411649b"}, "dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"}, "earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm", "e3be2bc3ae67781db529b80aa7e7c49904a988596e2dbff897425b48b3581161"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "8e24fc8ff9a50b9f557ff020d6c91a03cded7e59ac3e0eec8a27e771430c7d27"}, + "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, "gen_coap": {:git, "https://github.com/gotthardp/gen_coap.git", "245e2205cf233e94e2d481f6ddda5da038ebc6ea", []}, + "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, "makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5fbc8e549aa9afeea2847c0769e3970537ed302f93a23ac612602e805d9d1e7f"}, "makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "adf0218695e22caeda2820eaba703fa46c91820d53813a2223413da3ef4ba515"}, "mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm", "5e839994289d60326aa86020c4fbd9c6938af188ecddab2579f07b66cd665328"}, diff --git a/test/coap/multipart_test.exs b/test/coap/multipart_test.exs index 60d0599..16d280f 100644 --- a/test/coap/multipart_test.exs +++ b/test/coap/multipart_test.exs @@ -1,7 +1,4 @@ defmodule CoAP.MultipartTest do use ExUnit.Case doctest CoAP.Multipart - - # TODO: test build - # TODO: test as_blocks end diff --git a/test/coap/payload_test.exs b/test/coap/payload_test.exs index 8364c07..1f8a675 100644 --- a/test/coap/payload_test.exs +++ b/test/coap/payload_test.exs @@ -4,8 +4,6 @@ defmodule CoAP.PayloadTest do alias CoAP.Payload - # TODO: test next_segment with StreamData - describe "to_binary/1" do test "keeps a unique set of blocks by number" do segment = <<0, 0, 0, 0, 1>> From 509ce2a2b10011ed1ca5e998c5ccf9baf18b149f Mon Sep 17 00:00:00 2001 From: Jean Parpaillon Date: Tue, 8 Dec 2020 15:26:57 +0100 Subject: [PATCH 4/6] Upgrade deps Remove obseolte gen_coap dep --- mix.exs | 5 ++--- mix.lock | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/mix.exs b/mix.exs index 1b1647a..8087253 100644 --- a/mix.exs +++ b/mix.exs @@ -25,12 +25,11 @@ defmodule Coap.MixProject do # Dev {:dialyxir, "~> 1.0.0", only: :dev, runtime: false}, {:credo, "~> 1.5"}, - {:gen_coap, github: "gotthardp/gen_coap", only: [:dev, :test]}, {:stream_data, "~> 0.1", only: :test}, - {:ex_doc, "~> 0.19", only: :dev, runtime: false}, + {:ex_doc, "~> 0.23", only: :dev, runtime: false}, # Runtime - {:plug, "~> 1.0"}, + {:plug, "~> 1.11"}, {:telemetry, "~> 0.4.0"} ] end diff --git a/mix.lock b/mix.lock index 1fa88f2..cfb1f89 100644 --- a/mix.lock +++ b/mix.lock @@ -3,16 +3,18 @@ "credo": {:hex, :credo, "1.5.2", "5562f1a1693f77e7319fdabac6d17d26de7e6b0a2b57743bca24a89469232f04", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "7003506f069866a4e5d6216a7216823b00ed4bcc4bd9c6e449fa6625c411649b"}, "dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"}, "earmark": {:hex, :earmark, "1.3.2", "b840562ea3d67795ffbb5bd88940b1bed0ed9fa32834915125ea7d02e35888a5", [:mix], [], "hexpm", "e3be2bc3ae67781db529b80aa7e7c49904a988596e2dbff897425b48b3581161"}, + "earmark_parser": {:hex, :earmark_parser, "1.4.12", "b245e875ec0a311a342320da0551da407d9d2b65d98f7a9597ae078615af3449", [:mix], [], "hexpm", "711e2cc4d64abb7d566d43f54b78f7dc129308a63bc103fbd88550d2174b3160"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, - "ex_doc": {:hex, :ex_doc, "0.20.2", "1bd0dfb0304bade58beb77f20f21ee3558cc3c753743ae0ddbb0fd7ba2912331", [:mix], [{:earmark, "~> 1.3", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.10", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "8e24fc8ff9a50b9f557ff020d6c91a03cded7e59ac3e0eec8a27e771430c7d27"}, + "ex_doc": {:hex, :ex_doc, "0.23.0", "a069bc9b0bf8efe323ecde8c0d62afc13d308b1fa3d228b65bca5cf8703a529d", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f5e2c4702468b2fd11b10d39416ddadd2fcdd173ba2a0285ebd92c39827a5a16"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, "gen_coap": {:git, "https://github.com/gotthardp/gen_coap.git", "245e2205cf233e94e2d481f6ddda5da038ebc6ea", []}, "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, - "makeup": {:hex, :makeup, "0.8.0", "9cf32aea71c7fe0a4b2e9246c2c4978f9070257e5c9ce6d4a28ec450a839b55f", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5fbc8e549aa9afeea2847c0769e3970537ed302f93a23ac612602e805d9d1e7f"}, - "makeup_elixir": {:hex, :makeup_elixir, "0.13.0", "be7a477997dcac2e48a9d695ec730b2d22418292675c75aa2d34ba0909dcdeda", [:mix], [{:makeup, "~> 0.8", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "adf0218695e22caeda2820eaba703fa46c91820d53813a2223413da3ef4ba515"}, - "mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm", "5e839994289d60326aa86020c4fbd9c6938af188ecddab2579f07b66cd665328"}, - "nimble_parsec": {:hex, :nimble_parsec, "0.5.0", "90e2eca3d0266e5c53f8fbe0079694740b9c91b6747f2b7e3c5d21966bba8300", [:mix], [], "hexpm", "5c040b8469c1ff1b10093d3186e2e10dbe483cd73d79ec017993fb3985b8a9b3"}, - "plug": {:hex, :plug, "1.6.2", "e06a7bd2bb6de5145da0dd950070110dce88045351224bd98e84edfdaaf5ffee", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm", "b2713c085797012661a6c10e1898a009f0cc2bae556cc00341b69600074c757b"}, - "stream_data": {:hex, :stream_data, "0.4.2", "fa86b78c88ec4eaa482c0891350fcc23f19a79059a687760ddcf8680aac2799b", [:mix], [], "hexpm", "54d6bf6f1e5e27fbf4a7784a2bffbb993446d0efd079debca0f27bf859c0d1cf"}, - "telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [:rebar3], [], "hexpm", "e9e3cacfd37c1531c0ca70ca7c0c30ce2dbb02998a4f7719de180fe63f8d41e4"}, + "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"}, + "makeup_elixir": {:hex, :makeup_elixir, "0.15.0", "98312c9f0d3730fde4049985a1105da5155bfe5c11e47bdc7406d88e01e4219b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "75ffa34ab1056b7e24844c90bfc62aaf6f3a37a15faa76b07bc5eba27e4a8b4a"}, + "mime": {:hex, :mime, "1.5.0", "203ef35ef3389aae6d361918bf3f952fa17a09e8e43b5aa592b93eba05d0fb8d", [:mix], [], "hexpm", "55a94c0f552249fc1a3dd9cd2d3ab9de9d3c89b559c2bd01121f824834f24746"}, + "nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"}, + "plug": {:hex, :plug, "1.11.0", "f17217525597628298998bc3baed9f8ea1fa3f1160aa9871aee6df47a6e4d38e", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2d9c633f0499f9dc5c2fd069161af4e2e7756890b81adcbb2ceaa074e8308876"}, + "plug_crypto": {:hex, :plug_crypto, "1.2.0", "1cb20793aa63a6c619dd18bb33d7a3aa94818e5fd39ad357051a67f26dfa2df6", [:mix], [], "hexpm", "a48b538ae8bf381ffac344520755f3007cc10bd8e90b240af98ea29b69683fc2"}, + "stream_data": {:hex, :stream_data, "0.5.0", "b27641e58941685c75b353577dc602c9d2c12292dd84babf506c2033cd97893e", [:mix], [], "hexpm", "012bd2eec069ada4db3411f9115ccafa38540a3c78c4c0349f151fc761b9e271"}, + "telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"}, } From f6b34cc6eadc6cad3ac060e27e6b08abf330e2a6 Mon Sep 17 00:00:00 2001 From: Jean Parpaillon Date: Tue, 8 Dec 2020 17:23:59 +0100 Subject: [PATCH 5/6] Hello CircleCI, goodbye GH actions --- .circleci/config.yml | 153 +++++++++++++++++++++++++++++++++++ .dialyzer/ignore.exs | 1 + .github/workflows/elixir.yml | 21 ----- mix.exs | 12 ++- 4 files changed, 165 insertions(+), 22 deletions(-) create mode 100644 .circleci/config.yml create mode 100644 .dialyzer/ignore.exs delete mode 100644 .github/workflows/elixir.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..aa10f0b --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,153 @@ +version: 2.1 # use CircleCI 2.1 instead of CircleCI Classic + +x-common: + job-common: &job-common + working_directory: /home/circleci/project + docker: + - image: circleci/elixir:1.11 + environment: + MIX_ARCHIVES: /home/circleci/project/.mix/archives + MIX_HOME: /home/circleci/project/.mix + deploy-common: &deploy-common + working_directory: /home/circleci/project + docker: + - image: circleci/buildpack-deps + +x-steps: + - &save-deps-cache + save_cache: + key: v1-deps-cache-{{ .Branch }}-{{ checksum "mix.lock" }} + paths: ["deps"] + - &restore-deps-cache + restore_cache: + keys: + - v1-deps-cache-{{ .Branch }}-{{ checksum "mix.lock" }} + - &save-plt-cache + save_cache: + key: v1-plt-cache-{{ .Branch }}-{{ checksum ".dialyzer/cache.plt" }} + paths: [".dialyzer/cache.plt"] + - &restore-plt-cache + restore_cache: + keys: + - v1-plt-cache-{{ .Branch }}-{{ checksum ".dialyzer/cache.plt" }} + - &attach-workspace + attach_workspace: + at: /home/circleci + +x-filters: + only-pr: &only-pr + branches: + ignore: /^(master)$/ + only-master: &only-master + branches: + only: /^master$/ + +jobs: + setup: + <<: *job-common + steps: + - checkout + - *restore-deps-cache + - run: + name: Install Hex + command: mix local.hex --force --if-missing + - run: + name: Install rebar + command: mix local.rebar --force --if-missing + - run: + name: Install project's deps + command: mix deps.get + - *save-deps-cache + - persist_to_workspace: + root: /home/circleci/ + paths: ["project"] + + build: + <<: *job-common + steps: + - *attach-workspace + - run: + name: Build project + command: mix compile --warnings-as-errors + - persist_to_workspace: + root: /home/circleci/ + paths: ["project/_build"] + + format: + <<: *job-common + steps: + - *attach-workspace + - run: + name: Check formatting rules + command: mix format --check-formatted + + dialyzer: + <<: *job-common + steps: + - *attach-workspace + - run: + name: Dialyzer static analysis + command: mix dialyzer + + credo: + <<: *job-common + steps: + - *attach-workspace + - run: + name: Check coding rules + command: mix credo + + test: + <<: *job-common + steps: + - *attach-workspace + - run: mix test + +workflows: + version: 2 + on-pull-request: + jobs: + - setup: + filters: + <<: *only-pr + - build: + requires: + - setup + filters: + <<: *only-pr + - format: + requires: + - setup + filters: + <<: *only-pr + - credo: + requires: + - build + filters: + <<: *only-pr + - dialyzer: + requires: + - build + filters: + <<: *only-pr + - test: + requires: + - build + filters: + <<: *only-pr + + on-merge: + jobs: + - setup: + filters: + <<: *only-master + - build: + requires: + - setup + filters: + <<: *only-master + - test: + requires: + - setup + filters: + <<: *only-master diff --git a/.dialyzer/ignore.exs b/.dialyzer/ignore.exs new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/.dialyzer/ignore.exs @@ -0,0 +1 @@ +[] diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml deleted file mode 100644 index 372ff7d..0000000 --- a/.github/workflows/elixir.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Elixir CI - -on: push - -jobs: - build: - - runs-on: ubuntu-latest - - container: - image: elixir:1.9.1-slim - - steps: - - uses: actions/checkout@v1 - - name: Install Dependencies - run: | - mix local.rebar --force - mix local.hex --force - mix deps.get - - name: Run Tests - run: mix test diff --git a/mix.exs b/mix.exs index 8087253..e40ffae 100644 --- a/mix.exs +++ b/mix.exs @@ -7,7 +7,8 @@ defmodule Coap.MixProject do version: "0.1.0", elixir: "~> 1.6", start_permanent: Mix.env() == :prod, - deps: deps() + deps: deps(), + dialyzer: dialyzer() ] end @@ -33,4 +34,13 @@ defmodule Coap.MixProject do {:telemetry, "~> 0.4.0"} ] end + + defp dialyzer do + [ + plt_ignore_apps: [:credo], + plt_add_apps: [:ex_unit, :mix], + ignore_warnings: ".dialyzer/ignore.exs", + plt_file: {:no_warn, ".dialyzer/cache.plt"} + ] + end end From b69dde6d19cdd7a2e19a63ce517a33522501043f Mon Sep 17 00:00:00 2001 From: Jean Parpaillon Date: Wed, 9 Dec 2020 16:28:21 +0100 Subject: [PATCH 6/6] Apply mix format --- lib/coap/multipart.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/coap/multipart.ex b/lib/coap/multipart.ex index 73bff5c..fe40662 100644 --- a/lib/coap/multipart.ex +++ b/lib/coap/multipart.ex @@ -89,7 +89,10 @@ defmodule CoAP.Multipart do def build(nil, nil), do: %__MODULE__{multipart: false, description: nil, control: nil} - @spec as_blocks(boolean, CoAP.Multipart.t()) :: %{block1: CoAP.Block.t(), block2: CoAP.Block.t()} + @spec as_blocks(boolean, CoAP.Multipart.t()) :: %{ + block1: CoAP.Block.t(), + block2: CoAP.Block.t() + } def as_blocks(true, multipart) do %{ block1: multipart.description |> Block.to_tuple(),