-module(tda). -export([associate_request/0, associate_result/1]). -export([release_request/0, release_result/1]). -export([associate_abort/1]). -export([system_status/0, system_status/1]). -export([client/0, stop/1, loop/1, start_client/0]). -include("acse.hrl"). -include("rose.hrl"). % A-ASSOCIATE Request % 60 23 AARQ-apdu % 80 02 07 80 protocol-version { version1 } % A1 07 application-context-name % 06 05 2B 0C 00 81 5A { 1 3 12 0 218 } % BE 14 user-information % 28 12 % 06 07 2B 0C 00 82 1D 81 48 direct-reference { 1 3 12 0 285 200 } % A0 07 single-ASN1-type % (ACSEUserInfomrationForCSTA) % A0 05 newDefinition % 03 03 00 08 00 cSTAVersion { versionFive } associate_request() -> Ver = #'NewACSEUserInformationForCSTA'{cSTAVersion = [versionFive]}, {ok, Enc} = acse:encode('ACSEUserInformationForCSTA', {newDefinition, Ver}), Pdu = #'AARQ-apdu'{ 'protocol-version' = [version1], 'application-context-name' = {1, 3, 12, 0, 218}, 'user-information' = [#'EXTERNAL'{ 'direct-reference' = {1, 3, 12, 0, 285, 200}, encoding = {'single-ASN1-type', Enc}}]}, io:format("~p~n", [Pdu]), acse:encode('AARQ-apdu', Pdu). % Request % 60,23,80,02,07,80,a1,07,06,05,2b,0c,00,81,5a,be,14,28,12,06,07,2b,0c,00,82,1d,81,48,a0,07,a0,05,03,03,00,08,00 % <<96,35,128,2,7,128,161,7,6,5,43,12,0,129,90,190,20,40,18,6,7,43,12,0,130,29,129,72,160,7,160,5,3,3,0,8,0>> % <<96,30,161,7,6,5,43,12,0,129,90,190,19,40,17,6,7,43,12,0, 130,29,129,72,160,6,160,4,3,...>> % Accept % <<97,47,128,2,7,128,161,7,6,5,43,12,0,129,90,162,3,2,1,0,163,5,161,3,2,1,1,190,20,40,18,6,7,43,12,0,130,29,129,72,160,7,160,5,3,3,0,8,0>> % Reject % <<97,25,128,2,7,128,161,7,6,5,43,12,0,129,90,162,3,2,1,1,163,5,161,3,2,1,1>> associate_result(Bin) -> case acse:decode('AARE-apdu', Bin) of {ok, Pdu} -> Pdu#'AARE-apdu'.result; {error, Reason} -> Reason end. release_request() -> acse:encode('RLRQ-apdu', #'RLRQ-apdu'{}). release_result(Bin) -> acse:decode('RLRE-apdu', Bin). % <<100,3,128,1,0>> associate_abort(Bin) -> {ok, Pdu} = acse:decode('ABRT-apdu', Bin), {ok, Pdu#'ABRT-apdu'.'abort-source'}. % <<161,12,2,1,1,2,2,0,211,48,3,10,1,2>> % <<162,11,2,1,1,48,6,2,2,0,211,5,0>> system_status(Bin) -> case rose:decode('ROS', Bin) of {ok, {invoke, Invoke}} -> {id(Invoke), opcode(Invoke), arg(Invoke)}; {error, Reason} -> Reason end. opcode(#'Invoke'{opcode = {local, Opcode}}) -> Opcode; opcode(Any) -> {local, Any}. id(#'Invoke'{invokeId = {present, Id}}) -> Id; id(Any) -> {present, Any}. arg(#'Invoke'{argument = Arg}) -> Arg. system_status() -> RR = #'ReturnResult_result'{opcode = {local, 211}, result = <<5,0>>}, R = #'ReturnResult'{invokeId = {present, 1}, result = RR}, io:format("~p~n", [R]), case rose:encode('ROS', {returnResult, R}) of {ok, Invoke} -> Invoke; {error, Reason} -> Reason end. start_client() -> register(cl, spawn(?MODULE, 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(), io:format("Hello: ~p~n", [Hello]), gen_tcp:send(Sock, Hello), loop(Sock); {error, Reason} -> {error, Reason} end. stop(Sock) -> gen_tcp:close(Sock). loop(Sock) -> %inet:setopts(Sock, [{active, once}]), io:format("loop ~p~n", [Sock]), receive {tcp, Sock, Msg} -> decode(Msg), loop(Sock); {tcp_closed, Sock} -> io:format(">>> closed", []), Sock after 5000 -> stop(Sock) end, io:format("loop end~n"). decode(Msg) -> io:format(">>> ~p~n", [Msg]).