Skip to content

Commit

Permalink
Merge pull request #7 from valiot/alde103/INRPD340-Valgrind_testing
Browse files Browse the repository at this point in the history
Alde103/inrpd340 valgrind testing
  • Loading branch information
alde103 authored Jun 18, 2020
2 parents 5bbba4c + 02eeb40 commit ea2499a
Show file tree
Hide file tree
Showing 16 changed files with 680 additions and 397 deletions.
51 changes: 21 additions & 30 deletions lib/opc_ua/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ defmodule OpcUA.Client do
quote location: :keep, bind_quoted: [opts: opts] do
use GenServer, Keyword.drop(opts, [:configuration])
@behaviour OpcUA.Client
@mix_env Mix.env()

alias __MODULE__

Expand All @@ -149,7 +150,7 @@ defmodule OpcUA.Client do
configuration = apply(__MODULE__, :configuration, [user_initial_params])
monitored_items = apply(__MODULE__, :monitored_items, [user_initial_params])

OpcUA.Client.set_config(c_pid)
#OpcUA.Client.set_config(c_pid)

# configutation = [config: list(), connection: list()]
set_client_config(c_pid, configuration, :config)
Expand Down Expand Up @@ -253,7 +254,12 @@ defmodule OpcUA.Client do
config_params = Keyword.get(configuration, type, [])

Enum.each(config_params, fn config_param ->
GenServer.call(c_pid, {type, config_param})
if(@mix_env != :test) do
GenServer.call(c_pid, {type, config_param})
else
# Valgrind
GenServer.call(c_pid, {type, config_param}, :infinity)
end
end)
end

Expand Down Expand Up @@ -340,7 +346,12 @@ defmodule OpcUA.Client do
"""
@spec connect_by_url(GenServer.server(), list()) :: :ok | {:error, term} | {:error, :einval}
def connect_by_url(pid, args) when is_list(args) do
GenServer.call(pid, {:conn, {:by_url, args}})
if(@mix_env != :test) do
GenServer.call(pid, {:conn, {:by_url, args}})
else
# Valgrind
GenServer.call(pid, {:conn, {:by_url, args}}, :infinity)
end
end

@doc """
Expand All @@ -353,7 +364,12 @@ defmodule OpcUA.Client do
@spec connect_by_username(GenServer.server(), list()) ::
:ok | {:error, term} | {:error, :einval}
def connect_by_username(pid, args) when is_list(args) do
GenServer.call(pid, {:conn, {:by_username, args}})
if(@mix_env != :test) do
GenServer.call(pid, {:conn, {:by_username, args}})
else
# Valgrind
GenServer.call(pid, {:conn, {:by_username, args}}, :infinity)
end
end

@doc """
Expand Down Expand Up @@ -575,32 +591,7 @@ defmodule OpcUA.Client do

executable = lib_dir <> "/opc_ua_client"

port =
Port.open({:spawn_executable, to_charlist(executable)}, [
{:args, []},
{:packet, 2},
:use_stdio,
:binary,
:exit_status
])

# # Valgrind
# port =
# Port.open({:spawn_executable, to_charlist("/usr/bin/valgrind.bin")}, [
# {:args,
# [
# "-q",
# "--undef-value-errors=no",
# "--leak-check=full",
# "--show-leak-kinds=all",
# # "--track-origins=yes",
# executable
# ]},
# {:packet, 2},
# :use_stdio,
# :binary,
# :exit_status
# ])
port = open_port(executable, use_valgrind?())

state = %State{port: port, controlling_process: controlling_process}
{:ok, state}
Expand Down
74 changes: 73 additions & 1 deletion lib/opc_ua/common.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule OpcUA.Common do

@c_timeout 5000

@mix_env Mix.env()

defmodule State do
@moduledoc false

Expand Down Expand Up @@ -364,7 +366,27 @@ defmodule OpcUA.Common do
@spec read_node_value(GenServer.server(), %NodeId{}, integer()) ::
{:ok, term()} | {:error, binary()} | {:error, :einval}
def read_node_value(pid, node_id, index \\ 0) do
GenServer.call(pid, {:read, {:value, {node_id, index}}})
if(@mix_env != :test) do
GenServer.call(pid, {:read, {:value, {node_id, index}}})
else
# Valgrind
GenServer.call(pid, {:read, {:value, {node_id, index}}}, :infinity)
end
end

@doc """
Reads 'value' attribute of a node in the server.
Note: If the value is an array you can search a scalar using `index` parameter.
"""
@spec read_node_value_by_index(GenServer.server(), %NodeId{}, integer()) ::
{:ok, term()} | {:error, binary()} | {:error, :einval}
def read_node_value_by_index(pid, node_id, index \\ 0) do
if(@mix_env != :test) do
GenServer.call(pid, {:read, {:value_by_index, {node_id, index}}})
else
# Valgrind
GenServer.call(pid, {:read, {:value_by_index, {node_id, index}}}, :infinity)
end
end

@doc """
Expand Down Expand Up @@ -632,6 +654,12 @@ defmodule OpcUA.Common do
{:noreply, state}
end

def handle_call({:read, {:value_by_index, {node_id, index}}}, caller_info, state) do
c_args = {to_c(node_id), index}
call_port(state, :read_node_value_by_index, caller_info, c_args)
{:noreply, state}
end

def handle_call({:read, {:value_by_data_type, {node_id, data_type}}}, caller_info, state) do
c_args = {to_c(node_id), data_type}
call_port(state, :read_node_value_by_data_type, caller_info, c_args)
Expand Down Expand Up @@ -839,6 +867,12 @@ defmodule OpcUA.Common do
state
end

defp handle_c_response({:read_node_value_by_index, caller_metadata, value_response}, state) do
response = parse_value(value_response)
GenServer.reply(caller_metadata, response)
state
end

defp handle_c_response(
{:read_node_value_by_data_type, caller_metadata, value_response},
state
Expand All @@ -848,6 +882,38 @@ defmodule OpcUA.Common do
state
end

defp use_valgrind?(), do: System.get_env("USE_VALGRIND", "false")

defp open_port(executable, "false") do
Port.open({:spawn_executable, to_charlist(executable)}, [
{:args, []},
{:packet, 2},
:use_stdio,
:binary,
:exit_status
])
end

defp open_port(executable, valgrind_env) do
Logger.warn("(#{__MODULE__}) Valgrind Activated: #{inspect valgrind_env}")
Port.open({:spawn_executable, to_charlist("/usr/bin/valgrind.bin")}, [
{:args,
[
"-q",
"--undef-value-errors=no",
"--leak-check=full",
"--show-leak-kinds=all",
#"--verbose",
#"--track-origins=yes",
executable
]},
{:packet, 2},
:use_stdio,
:binary,
:exit_status
])
end

defp call_port(state, command, caller, arguments) do
msg = {command, caller, arguments}
send(state.port, {self(), {:command, :erlang.term_to_binary(msg)}})
Expand Down Expand Up @@ -909,6 +975,9 @@ defmodule OpcUA.Common do
defp parse_value({:ok, {ns_index, name}}) when is_integer(ns_index),
do: {:ok, QualifiedName.new(ns_index: ns_index, name: name)}

defp parse_value({:ok, array}) when is_list(array),
do: {:ok, Enum.map(array, fn(data) -> parse_c_value(data) end)}

defp parse_value(response), do: response

defp parse_c_value({ns_index, type, name, name_space_uri, server_index}),
Expand All @@ -931,6 +1000,9 @@ defmodule OpcUA.Common do
defp parse_c_value({ns_index, name}) when is_integer(ns_index),
do: QualifiedName.new(ns_index: ns_index, name: name)

defp parse_c_value(array) when is_list(array),
do: Enum.map(array, fn(data) -> parse_c_value(data) end)

defp parse_c_value(response), do: response

@doc false
Expand Down
43 changes: 21 additions & 22 deletions lib/opc_ua/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ defmodule OpcUA.Server do
quote location: :keep, bind_quoted: [opts: opts] do
use GenServer, Keyword.drop(opts, [:configuration])
@behaviour OpcUA.Server
@mix_env Mix.env()

alias __MODULE__

Expand Down Expand Up @@ -247,7 +248,12 @@ defmodule OpcUA.Server do
"""
@spec set_default_config(GenServer.server()) :: :ok | {:error, binary()} | {:error, :einval}
def set_default_config(pid) do
GenServer.call(pid, {:config, {:set_default_server_config, nil}})
if(@mix_env != :test) do
GenServer.call(pid, {:config, {:set_default_server_config, nil}})
else
# Valgrind
GenServer.call(pid, {:config, {:set_default_server_config, nil}}, :infinity)
end
end

@doc """
Expand All @@ -263,7 +269,12 @@ defmodule OpcUA.Server do
"""
@spec set_port(GenServer.server(), integer()) :: :ok | {:error, binary()} | {:error, :einval}
def set_port(pid, port) when is_integer(port) do
GenServer.call(pid, {:config, {:port, port}})
if(@mix_env != :test) do
GenServer.call(pid, {:config, {:port, port}})
else
# Valgrind
GenServer.call(pid, {:config, {:port, port}}, :infinity)
end
end

@doc """
Expand Down Expand Up @@ -322,7 +333,12 @@ defmodule OpcUA.Server do
"""
@spec discovery_register(GenServer.server(), list()) :: :ok | {:error, binary()} | {:error, :einval}
def discovery_register(pid, args) when is_list(args) do
GenServer.call(pid, {:discovery, {:discovery_register, args}})
if(@mix_env != :test) do
GenServer.call(pid, {:discovery, {:discovery_register, args}})
else
# Valgrinnd
GenServer.call(pid, {:discovery, {:discovery_register, args}}, :infinity)
end
end

@doc """
Expand Down Expand Up @@ -509,7 +525,7 @@ defmodule OpcUA.Server do

@doc false
def test(pid) do
GenServer.call(pid, {:test, nil})
GenServer.call(pid, {:test, nil}, :infinity)
end

# Handlers
Expand All @@ -523,24 +539,7 @@ defmodule OpcUA.Server do

executable = lib_dir <> "/opc_ua_server"

port =
Port.open({:spawn_executable, to_charlist(executable)}, [
{:args, []},
{:packet, 2},
:use_stdio,
:binary,
:exit_status
])

#Valgrind
# port =
# Port.open({:spawn_executable, to_charlist("/usr/bin/valgrind.bin")}, [
# {:args, ["-q", "--leak-check=full", "--show-leak-kinds=all", "--track-origins=yes", "--show-reachable=no", executable]},
# {:packet, 2},
# :use_stdio,
# :binary,
# :exit_status
# ])
port = open_port(executable, use_valgrind?())

state = %State{port: port, controlling_process: controlling_process}
{:ok, state}
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ if(NOT MANUAL_BUILD)
set(BASE_FLAGS "${BASE_RELEASE_FLAGS}")
endif()

set(BASE_C_FLAGS "${BASE_FLAGS} -std=c99 -lpthread")
set(BASE_C_FLAGS "-g -Wall -Wextra ${BASE_FLAGS} -std=c99 -lpthread")
set(BASE_CXX_FLAGS "${BASE_FLAGS}")

set (opex62541_PROGRAMS opc_ua_server opc_ua_client client_example server_example)
Expand Down
Loading

0 comments on commit ea2499a

Please sign in to comment.