summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-10-24 02:37:58 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-10-24 02:37:58 +0200
commit359513452f4078cf6348602044567d7fcdeaa832 (patch)
tree4cd60b2d1d18f382e05796d827a020891ac05320
parent57bfc5207fb273b5c501d2803263497ec322e14b (diff)
Reply status
-rw-r--r--tda.erl46
1 files changed, 36 insertions, 10 deletions
diff --git a/tda.erl b/tda.erl
index b88dd32..f4e4f8b 100644
--- a/tda.erl
+++ b/tda.erl
@@ -9,6 +9,8 @@
-include("acse.hrl").
-include("rose.hrl").
+-define(TIMEOUT, 300000).
+
% A-ASSOCIATE Request
% 60 23 AARQ-apdu
% 80 02 07 80 protocol-version { version1 }
@@ -89,7 +91,6 @@ start_client() ->
client() ->
Host = "192.168.240.20",
- %Host = "localhost",
case gen_tcp:connect(Host, 33333, [binary, {active, true}, {packet, 2}]) of
{ok, Sock} ->
{ok, Hello} = associate_request(),
@@ -106,15 +107,40 @@ loop(Sock) ->
io:format("loop ~p~n", [Sock]),
receive
{tcp, Sock, Msg} ->
- decode(Msg),
+ decode(Sock, Msg),
loop(Sock);
- {tcp_closed, Sock} ->
- io:format(">>> closed", []),
- Sock
- after 5000 ->
+ {tcp_closed, _} ->
+ io:format(">>> closed", [])
+ after ?TIMEOUT ->
+ io:format("timeout~n"),
stop(Sock)
- end,
- io:format("loop end~n").
+ end.
+
+decode(Sock, Msg) ->
+ io:format(">>> ~p~n", [Msg]),
+ case rose:decode('ROS', Msg) of
+ {ok, Rose} ->
+ rose_handler(Sock, Rose),
+ io:format("ROSE> ~p~n", [Rose]);
+ {error, _} ->
+ case acse:decode('ACSE-apdu', Msg) of
+ {ok, Acse} ->
+ io:format("ACSE> ~p~n", [Acse])
+ end
+ end.
+
+rose_handler(Sock, {invoke, Rose}) ->
+ gen_tcp:send(Sock, invoke_handler(Rose)).
+
+invoke_handler(#'Invoke'{invokeId = {present, Id}, opcode = {local, Op}}) ->
+ case Op of
+ 211 ->
+ RR = #'ReturnResult_result'{opcode = {local, Op},
+ result = <<5,0>>},
+ R = #'ReturnResult'{invokeId = {present, Id}, result = RR},
+ case rose:encode('ROS', {returnResult, R}) of
+ {ok, Invoke} -> Invoke;
+ {error, Reason} -> Reason
+ end
+ end.
-decode(Msg) ->
- io:format(">>> ~p~n", [Msg]).