Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up log and docs #13

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions lib/modbuzz/pdu.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ defmodule Modbuzz.PDU do
def encode_request(struct), do: {:ok, Modbuzz.PDU.Protocol.encode(struct)}
def encode_response(struct), do: {:ok, Modbuzz.PDU.Protocol.encode(struct)}

for {modbus_function_code, modbus_function} <- [
{0x01, ReadCoils},
{0x02, ReadDiscreteInputs},
{0x03, ReadHoldingRegisters},
{0x04, ReadInputRegisters},
{0x05, WriteSingleCoil},
{0x06, WriteSingleRegister},
{0x0F, WriteMultipleCoils},
{0x10, WriteMultipleRegisters}
] do
for {modbus_function_code, modbus_function} <- Modbuzz.MixProject.pdu_seed() do
req_module = Module.concat([Modbuzz.PDU, modbus_function, Req])
res_module = Module.concat([Modbuzz.PDU, modbus_function, Res])
err_module = Module.concat([Modbuzz.PDU, modbus_function, Err])
Expand Down
110 changes: 110 additions & 0 deletions lib/modbuzz/pdu/diagnostics.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
defmodule Modbuzz.PDU.Diagnostics do
@moduledoc false

defmodule Req do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)

@type t :: %__MODULE__{
sub_function: 0x0000..0xFFFF,
data: 0x0000..0xFFFF
}

defstruct [:sub_function, :data]

defimpl Modbuzz.PDU.Protocol do
@function_code 0x08

@doc """
iex> req = %Modbuzz.PDU.Diagnostics.Req{
...> sub_function: 0x0000,
...> data: 0xA537
...> }
iex> Modbuzz.PDU.Protocol.encode(req)
<<#{@function_code}, 0x0000::16, 0xA537::16>>
"""
def encode(struct) do
<<@function_code, struct.sub_function::16, struct.data::16>>
end

@doc """
iex> req = %Modbuzz.PDU.Diagnostics.Req{}
iex> Modbuzz.PDU.Protocol.decode(req, <<#{@function_code}, 0x0000::16, 0xA537::16>>)
%Modbuzz.PDU.Diagnostics.Req{sub_function: 0x0000, data: 0xA537}
"""
def decode(struct, <<@function_code, sub_function::16, data::16>>) do
%{struct | sub_function: sub_function, data: data}
end
end
end

defmodule Res do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)

@type t :: %__MODULE__{
sub_function: 0x0000..0xFFFF,
data: 0x0000..0xFFFF
}

defstruct [:sub_function, :data]

defimpl Modbuzz.PDU.Protocol do
@function_code 0x08

@doc """
iex> res = %Modbuzz.PDU.Diagnostics.Res{
...> sub_function: 0x0000,
...> data: 0xA537
...> }
iex> Modbuzz.PDU.Protocol.encode(res)
<<#{@function_code}, 0x0000::16, 0xA537::16>>
"""
def encode(struct) do
<<@function_code, struct.sub_function::16, struct.data::16>>
end

@doc """
iex> res = %Modbuzz.PDU.Diagnostics.Res{}
iex> Modbuzz.PDU.Protocol.decode(res, <<#{@function_code}, 0x0000::16, 0xA537::16>>)
%Modbuzz.PDU.Diagnostics.Res{
sub_function: 0x0000,
data: 0xA537
}
"""
def decode(struct, <<@function_code, sub_function::16, data::16>>) do
%{struct | sub_function: sub_function, data: data}
end
end
end

defmodule Err do
@moduledoc Modbuzz.PDU.Helper.module_one_line_doc(__MODULE__)

@type t :: %__MODULE__{
exception_code: 0x01 | 0x03 | 0x04
}

defstruct [:exception_code]

defimpl Modbuzz.PDU.Protocol do
@error_code 0x08 + 0x80

@doc """
iex> err = %Modbuzz.PDU.Diagnostics.Err{exception_code: 0x01}
iex> Modbuzz.PDU.Protocol.encode(err)
<<#{@error_code}, 0x01>>
"""
def encode(struct) do
<<@error_code, struct.exception_code>>
end

@doc """
iex> err = %Modbuzz.PDU.Diagnostics.Err{}
iex> Modbuzz.PDU.Protocol.decode(err, <<#{@error_code}, 0x01>>)
%Modbuzz.PDU.Diagnostics.Err{exception_code: 0x01}
"""
def decode(struct, <<@error_code, exception_code>>) do
%{struct | exception_code: exception_code}
end
end
end
end
34 changes: 14 additions & 20 deletions lib/modbuzz/tcp/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Modbuzz.TCP.Client do

use GenServer

require Logger
alias Modbuzz.TCP.Log

defmodule Transaction do
@moduledoc false
Expand Down Expand Up @@ -125,11 +125,11 @@ defmodule Modbuzz.TCP.Client do
def handle_continue(:connect, %{socket: nil} = state) do
case gen_tcp_connect(state) do
{:ok, socket} ->
Logger.debug("#{__MODULE__}: :connect succeeded.")
Log.debug(":connect succeeded", state)
{:noreply, %{state | socket: socket}}

{:error, reason} ->
Logger.error("#{__MODULE__}: :connect failed, the reason is #{inspect(reason)}.")
Log.error(":connect failed", reason, state)
{:noreply, state, {:continue, :connect}}
end
end
Expand All @@ -154,17 +154,17 @@ defmodule Modbuzz.TCP.Client do
{:noreply, %{state | socket: socket}}
else
{:connect, {:error, reason} = error} ->
Logger.error("#{__MODULE__}: :recall connect failed, the reason is #{inspect(reason)}.")
Log.error(":recall connect failed", reason, state)
GenServer.reply(from, error)
{:noreply, state, {:continue, :connect}}

{:send, {:error, reason} = error} ->
Logger.error("#{__MODULE__}: :recall send failed, the reason is #{inspect(reason)}.")
Log.error(":recall send failed", reason, state)
GenServer.reply(from, error)
{:noreply, state, {:continue, :connect}}

{:recv, {:error, reason} = error} ->
Logger.error("#{__MODULE__}: :recall recv failed, the reason is #{inspect(reason)}.")
Log.error(":recall recv failed", reason, state)
GenServer.reply(from, error)
{:noreply, state, {:continue, :connect}}
end
Expand Down Expand Up @@ -195,11 +195,11 @@ defmodule Modbuzz.TCP.Client do
{:noreply, %{state | socket: socket, transactions: transactions}}
else
{:connect, {:error, reason}} ->
Logger.error("#{__MODULE__}: :recast connect failed, the reason is #{inspect(reason)}.")
Log.error(":recast connect failed", reason, state)
{:noreply, state, {:continue, :connect}}

{:send, {:error, reason}} ->
Logger.error("#{__MODULE__}: :recast send failed, the reason is #{inspect(reason)}.")
Log.error(":recast send failed", reason, state)
{:noreply, state, {:continue, :connect}}
end
end
Expand All @@ -223,18 +223,14 @@ defmodule Modbuzz.TCP.Client do
{:reply, res_tuple, state}
else
{:send, {:error, reason}} ->
Logger.warning(
"#{__MODULE__}: :call send failed, the reason is #{inspect(reason)}, :recall."
)
Log.warning(":call send failed, :recall", reason, state)

:ok = transport.close(socket)
state = %{state | socket: nil}
{:noreply, state, {:continue, {:recall, unit_id, request, timeout, from}}}

{:recv, {:error, reason}} ->
Logger.warning(
"#{__MODULE__}: :call recv failed, the reason is #{inspect(reason)}, :recall."
)
Log.warning(":call recv failed, :recall", reason, state)

:ok = transport.close(socket)
state = %{state | socket: nil}
Expand Down Expand Up @@ -273,9 +269,7 @@ defmodule Modbuzz.TCP.Client do
{:noreply, %{state | transactions: transactions}}

{:error, reason} ->
Logger.warning(
"#{__MODULE__}: :cast send failed, the reason is #{inspect(reason)}, :recast."
)
Log.warning(":cast send failed, :recast", reason, state)

:ok = transport.close(socket)
state = %{state | socket: nil}
Expand Down Expand Up @@ -316,7 +310,7 @@ defmodule Modbuzz.TCP.Client do
def handle_info({:tcp_closed, socket}, %{socket: socket, active: true} = state) do
%{transport: transport, transactions: transactions} = state

Logger.warning("#{__MODULE__}: transport closed.")
Log.warning("transport closed.", nil, state)
:ok = transport.close(socket)
state = %{state | socket: nil}

Expand All @@ -329,7 +323,7 @@ defmodule Modbuzz.TCP.Client do
{:noreply, state, {:continue, :connect}}

[{_transaction_id, transaction}] ->
Logger.debug("#{__MODULE__}: sent successfully but RST ACK received, :recast.")
Log.debug("sent successfully but RST ACK received, :recast", state)

{:noreply, state,
{:continue, {:recast, transaction.unit_id, transaction.request, transaction.from_pid}}}
Expand All @@ -338,7 +332,7 @@ defmodule Modbuzz.TCP.Client do

def handle_info({:tcp_error, socket, reason}, %{socket: socket, active: true} = state) do
%{transport: transport} = state
Logger.error("#{__MODULE__}: transport error, the reason is #{inspect(reason)}.")
Log.error("transport error", reason, state)
:ok = transport.close(socket)
{:noreply, %{state | socket: nil}, {:continue, :connect}}
end
Expand Down
33 changes: 33 additions & 0 deletions lib/modbuzz/tcp/log.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule Modbuzz.TCP.Log do
@moduledoc false

require Logger

def debug(what_happend) do
Logger.debug(what_happend)
end

def debug(what_happend, state) do
Logger.debug(what_happend <> where(state))
end

def error(what_happend) do
Logger.error(what_happend)
end

def error(what_happend, reason, state) do
Logger.error(what_happend <> why(reason) <> where(state))
end

def warning(what_happend, reason, state) do
Logger.warning(what_happend <> why(reason) <> where(state))
end

defp why(nil), do: ""
defp why(reason), do: ", the reason is #{inspect(reason)}"

defp where(state) do
%{address: address, port: port} = state
" (address: #{inspect(address)}, port: #{inspect(port)})"
end
end
6 changes: 3 additions & 3 deletions lib/modbuzz/tcp/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Modbuzz.TCP.Server do

use GenServer

require Logger
alias Modbuzz.TCP.Log

@doc false
@spec start_link(keyword()) :: GenServer.on_start()
Expand Down Expand Up @@ -40,7 +40,7 @@ defmodule Modbuzz.TCP.Server do
{:noreply, %{state | listen_socket: socket}, {:continue, :accept}}

{:error, reason} ->
Logger.error("#{__MODULE__}: :listen failed, the reason is #{inspect(reason)}.")
Log.error(":listen failed", reason, state)
{:noreply, state, {:continue, :listen}}
end
end
Expand All @@ -65,7 +65,7 @@ defmodule Modbuzz.TCP.Server do
)

{:error, reason} ->
Logger.error("#{__MODULE__}: #{inspect(reason)}")
Log.error(":accept failed", reason, state)
end

{:noreply, state, {:continue, :accept}}
Expand Down
10 changes: 7 additions & 3 deletions lib/modbuzz/tcp/server/socket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ defmodule Modbuzz.TCP.Server.SocketHandler do

use GenServer, restart: :temporary

require Logger
alias Modbuzz.TCP.Log

def start_link(args) do
GenServer.start_link(__MODULE__, args)
end

def init(args) do
transport = Keyword.fetch!(args, :transport)
address = Keyword.fetch!(args, :address)
port = Keyword.fetch!(args, :port)
socket = Keyword.fetch!(args, :socket)
data_source = Keyword.fetch!(args, :data_source)
timeout = Keyword.get(args, :timeout, 5000)

{:ok,
%{
transport: transport,
address: address,
port: port,
socket: socket,
data_source: data_source,
timeout: timeout
Expand Down Expand Up @@ -52,7 +56,7 @@ defmodule Modbuzz.TCP.Server.SocketHandler do
{:stop, reason, state}

{:error, reason} ->
Logger.error("#{__MODULE__}: #{inspect(reason)}")
Log.error(":recv failed", reason, state)
:ok = transport.close(socket)
{:stop, reason, state}
end
Expand All @@ -66,7 +70,7 @@ defmodule Modbuzz.TCP.Server.SocketHandler do
end
catch
:exit, {:noproc, mfa} ->
Logger.error("#{__MODULE__}: `#{data_source}` not found. (mfa is #{inspect(mfa)})")
Log.error("`#{data_source}` not found. (mfa is #{inspect(mfa)})")
Modbuzz.PDU.to_error(request)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/modbuzz/tcp/server/socket_handler_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ defmodule Modbuzz.TCP.Server.SocketHandlerSupervisor do
{Modbuzz.TCP.Server.SocketHandler,
[
transport: transport,
socket: socket,
address: address,
port: port,
socket: socket,
data_source: data_source
]}
)
Expand Down
20 changes: 17 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ defmodule Modbuzz.MixProject do
]
end

def pdu_seed() do
[
{0x01, ReadCoils},
{0x02, ReadDiscreteInputs},
{0x03, ReadHoldingRegisters},
{0x04, ReadInputRegisters},
{0x05, WriteSingleCoil},
{0x06, WriteSingleRegister},
{0x0F, WriteMultipleCoils},
{0x10, WriteMultipleRegisters}
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
Expand Down Expand Up @@ -75,9 +88,10 @@ defmodule Modbuzz.MixProject do
docs: [
main: "readme",
extras: ["README.md"],
nest_modules_by_prefix: [
Modbuzz.PDU
]
nest_modules_by_prefix:
for {_modbus_function_code, modbus_function} <- pdu_seed() do
Module.concat([Modbuzz.PDU, modbus_function])
end
]
]
end
Expand Down
Loading