From 52a5b964920b5c470b19e4b3a37443b8f5fea405 Mon Sep 17 00:00:00 2001 From: Thomas Schneider Date: Sat, 31 Oct 2026 19:03:43 +0100 Subject: Add and apply erlfmt --- rebar.config | 20 +- src/bcd_codec.erl | 88 ++++--- src/osmo_gsup_if.erl | 246 ++++++++++-------- src/osmo_smsc.app.src | 20 +- src/osmo_smsc_app.erl | 4 +- src/osmo_smsc_core.erl | 213 +++++++++------- src/sm_rp_trans.erl | 128 +++++----- src/sm_tp_codec.erl | 625 ++++++++++++++++++++++++---------------------- test/bcd_codec_test.erl | 93 +++---- test/sm_tp_codec_test.erl | 377 ++++++++++++++++++---------- 10 files changed, 1014 insertions(+), 800 deletions(-) diff --git a/rebar.config b/rebar.config index afe1328..6889d4f 100644 --- a/rebar.config +++ b/rebar.config @@ -1,15 +1,23 @@ {erl_opts, [debug_info, {parse_transform}]}. {deps, [ - {osmo_gsup, {git, "git://git.osmocom.org/erlang/osmo_gsup", {branch, "master"}}} + {osmo_gsup, {git, "git://git.osmocom.org/erlang/osmo_gsup", {branch, "master"}}} ]}. {minimum_otp_vsn, "20.3"}. -{xref_checks, [undefined_function_calls, undefined_functions, - deprecated_function_calls, deprecated_functions]}. +{xref_checks, [ + undefined_function_calls, + undefined_functions, + deprecated_function_calls, + deprecated_functions +]}. {dialyzer, [ - {plt_extra_apps, [kernel, stdlib, erts, tools, inets, compiler]}, - {warnings, [no_improper_lists]} - ]}. + {plt_extra_apps, [kernel, stdlib, erts, tools, inets, compiler]}, + {warnings, [no_improper_lists]} +]}. + +{project_plugins, [erlfmt]}. + +{erlfmt, [{exclude_files, ["src/ipa_proto.erl"]}]}. diff --git a/src/bcd_codec.erl b/src/bcd_codec.erl index 76738fb..d96230f 100644 --- a/src/bcd_codec.erl +++ b/src/bcd_codec.erl @@ -34,12 +34,13 @@ -module(bcd_codec). --export([encode/1, - encode_nopad/1, - decode/1, - encode_num/1, - decode_num/1 - ]). +-export([ + encode/1, + encode_nopad/1, + decode/1, + encode_num/1, + decode_num/1 +]). %% BCD-encode a number or a string of digits. %% The result is octet-aligned (with optional padding). @@ -47,39 +48,41 @@ %% 90170 -> << 16#09, 16#71, 16#f0 >>. -spec encode(list() | binary() | integer()) -> binary(). encode(Data) when is_list(Data); is_binary(Data); is_integer(Data) -> - encode_pad_nopad(Data, true). + encode_pad_nopad(Data, true). %% Same as encode/1, but without padding. -spec encode_nopad(list() | binary() | integer()) -> bitstring(). encode_nopad(Data) when is_list(Data); is_binary(Data); is_integer(Data) -> - encode_pad_nopad(Data, false). + encode_pad_nopad(Data, false). -spec encode_pad_nopad(list() | binary() | integer(), boolean()) -> bitstring(). encode_pad_nopad(Data, Pad) when is_integer(Data) -> - encode_pad_nopad(integer_to_list(Data), Pad); + encode_pad_nopad(integer_to_list(Data), Pad); encode_pad_nopad(Data, Pad) when is_binary(Data) -> - encode_pad_nopad(binary_to_list(Data), Pad); + encode_pad_nopad(binary_to_list(Data), Pad); encode_pad_nopad(Data, Pad) when is_list(Data) -> - case {encode(Data, << >>), Pad} of - {{odd, BCDNum, N1}, true} -> - %% Odd number of digits, with padding - << BCDNum/bytes, 02#1111:4, N1:4 >>; - {{odd, BCDNum, N1}, false} -> - %% Odd number of digits, without padding - << BCDNum/bytes, N1:4 >>; - {{even, BCDNum}, _} -> - %% Even number of digits - BCDNum - end. + case {encode(Data, <<>>), Pad} of + {{odd, BCDNum, N1}, true} -> + %% Odd number of digits, with padding + <>; + {{odd, BCDNum, N1}, false} -> + %% Odd number of digits, without padding + <>; + {{even, BCDNum}, _} -> + %% Even number of digits + BCDNum + end. encode([D1, D2 | Tail], BCDNum) -> - N1 = bcd_nibble(D1), N2 = bcd_nibble(D2), - N21 = N1 bor (N2 bsl 4), - encode(Tail, << BCDNum/bytes, N21 >>); + N1 = bcd_nibble(D1), + N2 = bcd_nibble(D2), + N21 = N1 bor (N2 bsl 4), + encode(Tail, <>); encode([D1], BCDNum) -> - N1 = bcd_nibble(D1), - {odd, << BCDNum/bytes >>, N1}; -encode([], BCDNum) -> {even, BCDNum}. + N1 = bcd_nibble(D1), + {odd, <>, N1}; +encode([], BCDNum) -> + {even, BCDNum}. %% Convert an ASCII character to a BCD digit (nibble). bcd_nibble(C) when C >= $0, C =< $9 -> C - $0; @@ -104,31 +107,34 @@ bcd_ascii(D) -> error({bad_bcd_nibble, D}). %% << 16#09, 16#71, 16#f0 >> -> "90170". -spec decode(binary() | list()) -> list(). decode(Data) when is_list(Data) -> - Binary = list_to_binary(Data), - decode(Binary, []); + Binary = list_to_binary(Data), + decode(Binary, []); decode(Data) when is_binary(Data) -> - decode(Data, []). + decode(Data, []). -decode(<< 02#1111:4, N1:4 >>, ACSII) -> - C1 = bcd_ascii(N1), - decode(<< >>, [C1 | ACSII]); -decode(<< N2:4, N1:4, Tail/bits >>, ACSII) -> - C1 = bcd_ascii(N1), C2 = bcd_ascii(N2), - decode(Tail, [C2, C1 | ACSII]); -decode(<< >>, ACSII) -> lists:reverse(ACSII). +decode(<<02#1111:4, N1:4>>, ACSII) -> + C1 = bcd_ascii(N1), + decode(<<>>, [C1 | ACSII]); +decode(<>, ACSII) -> + C1 = bcd_ascii(N1), + C2 = bcd_ascii(N2), + decode(Tail, [C2, C1 | ACSII]); +decode(<<>>, ACSII) -> + lists:reverse(ACSII). %% BCD-encode a number in range [0 .. 99]. %% Examples: 10#05 -> 16#50, %% 10#82 -> 16#28. -spec encode_num(integer()) -> integer(). encode_num(Num) when Num >= 0, Num =< 99 -> - N1 = Num div 10, N2 = Num rem 10, - N1 bor (N2 bsl 4). + N1 = Num div 10, + N2 = Num rem 10, + N1 bor (N2 bsl 4). %% Decode a BCD-coded number in range [0 .. 99]. %% Examples: 16#14 -> 10#41, %% 16#39 -> 10#93. -spec decode_num(integer()) -> integer(). decode_num(Num) when Num >= 0, Num =< 16#99, (Num rem 16) < 10 -> - << N2:4, N1:4 >> = << Num >>, - N1 * 10 + N2. + <> = <>, + N1 * 10 + N2. diff --git a/src/osmo_gsup_if.erl b/src/osmo_gsup_if.erl index 7b95376..943d70c 100644 --- a/src/osmo_gsup_if.erl +++ b/src/osmo_gsup_if.erl @@ -38,14 +38,14 @@ -include_lib("osmo_gsup/include/gsup_protocol.hrl"). --define(IPAC_PROTO_EXT_GSUP, {osmo, 5}). +-define(IPAC_PROTO_EXT_GSUP, {osmo, 5}). -record(smr_data, {imsi, msg_ref, orig_addr, dest_addr, tpdu}). -record(gsupc_state, { - core, - socket, - ipa_pid - }). + core, + socket, + ipa_pid +}). -export([start_link/2]). @@ -57,129 +57,171 @@ %% ------------------------------------------------------------------ start_link(Core, HLRAddr) -> - gen_server:start_link(?MODULE, [Core, HLRAddr], [{debug, [trace]}]). + gen_server:start_link(?MODULE, [Core, HLRAddr], [{debug, [trace]}]). %% ------------------------------------------------------------------ %% gen_server Function Definitions %% ------------------------------------------------------------------ init([Core, {Address, Port, Options}]) -> - ipa_proto:init(), - % register the GSUP codec with the IPA core; ignore result as we mgiht be doing this multiple times - ipa_proto:register_codec(?IPAC_PROTO_EXT_GSUP, fun gsup_protocol:encode/1, fun gsup_protocol:decode/1), - {ok, {Socket, IpaPid}} = ipa_proto:connect(Address, Port, Options), - true = ipa_proto:register_stream(Socket, ?IPAC_PROTO_EXT_GSUP, {process_id, self()}), - ipa_proto:unblock(Socket), - {ok, #gsupc_state{core = Core, socket = Socket, ipa_pid = IpaPid}}. + ipa_proto:init(), + % register the GSUP codec with the IPA core; ignore result as we mgiht be doing this multiple times + ipa_proto:register_codec( + ?IPAC_PROTO_EXT_GSUP, fun gsup_protocol:encode/1, fun gsup_protocol:decode/1 + ), + {ok, {Socket, IpaPid}} = ipa_proto:connect(Address, Port, Options), + true = ipa_proto:register_stream(Socket, ?IPAC_PROTO_EXT_GSUP, {process_id, self()}), + ipa_proto:unblock(Socket), + {ok, #gsupc_state{core = Core, socket = Socket, ipa_pid = IpaPid}}. -define(IPA_GSUP_STREAM_ID, {osmo, 16#05}). -define(GSUP_MSG_CLS_SM, 16#02). gen_sm_response(GSUPMsg, MsgType) -> - #{message_class => ?GSUP_MSG_CLS_SM, - message_type => MsgType, - imsi => maps:get(imsi, GSUPMsg), - sm_rp_mr => maps:get(sm_rp_mr, GSUPMsg), - source_name => << "EUSE-OsmoSMSC-00-00-00-00-00-00", 0 >>, - destination_name => maps:get(source_name, GSUPMsg)}. + #{ + message_class => ?GSUP_MSG_CLS_SM, + message_type => MsgType, + imsi => maps:get(imsi, GSUPMsg), + sm_rp_mr => maps:get(sm_rp_mr, GSUPMsg), + source_name => <<"EUSE-OsmoSMSC-00-00-00-00-00-00", 0>>, + destination_name => maps:get(source_name, GSUPMsg) + }. % IPA/GSUP messages from OsmoHLR handle_cast({ipa, ?IPA_GSUP_STREAM_ID, GSUPMsg}, State) -> - % Obtain mandatory IEs - #{imsi := IMSI, sm_rp_mr := MR} = GSUPMsg, - % Parse depending on a message type - case GSUPMsg of - % MO RP-DATA / RP-SMMA - #{message_type := mo_forward_sm_req, sm_rp_da := DA, sm_rp_oa := OA, sm_rp_ui := TPDU} -> - io:format("(IMSI:~p, MR:~p) Rx MT-forwardSM Request~n", [IMSI, MR]), - gen_server:cast(State#gsupc_state.core, - {smsc_ev_mo_fwd_sm_req, IMSI, MR, DA, OA, TPDU}); - #{message_type := ready_for_sm_req} -> - Response = gen_sm_response(GSUPMsg, ready_for_sm_res), - ipa_proto:send(State#gsupc_state.socket, ?IPA_GSUP_STREAM_ID, Response); - % MT RP-ACK - #{message_type := mt_forward_sm_res} -> - io:format("(IMSI:~p, MR:~p) Rx MT-forwardSM Result~n", [IMSI, MR]), - gen_server:cast(State#gsupc_state.core, - {smsc_ev_mt_fwd_sm_ack, IMSI, MR}); - % MT RP-ERROR - #{message_type := mt_forward_sm_err, sm_rp_cause := Cause} -> - io:format("(IMSI:~p, MR:~p) Rx MT-forwardSM Error: cause=~p~n", [IMSI, MR, Cause]), - gen_server:cast(State#gsupc_state.core, - {smsc_ev_mt_fwd_sm_err, IMSI, MR, Cause}); - _ -> - error_logger:error_report(["unknown GSUP message", - {module, ?MODULE}, - {message, GSUPMsg}, - {state, State}]) - end, - {noreply, State}; - + % Obtain mandatory IEs + #{imsi := IMSI, sm_rp_mr := MR} = GSUPMsg, + % Parse depending on a message type + case GSUPMsg of + % MO RP-DATA / RP-SMMA + #{message_type := mo_forward_sm_req, sm_rp_da := DA, sm_rp_oa := OA, sm_rp_ui := TPDU} -> + io:format("(IMSI:~p, MR:~p) Rx MT-forwardSM Request~n", [IMSI, MR]), + gen_server:cast( + State#gsupc_state.core, + {smsc_ev_mo_fwd_sm_req, IMSI, MR, DA, OA, TPDU} + ); + #{message_type := ready_for_sm_req} -> + Response = gen_sm_response(GSUPMsg, ready_for_sm_res), + ipa_proto:send(State#gsupc_state.socket, ?IPA_GSUP_STREAM_ID, Response); + % MT RP-ACK + #{message_type := mt_forward_sm_res} -> + io:format("(IMSI:~p, MR:~p) Rx MT-forwardSM Result~n", [IMSI, MR]), + gen_server:cast( + State#gsupc_state.core, + {smsc_ev_mt_fwd_sm_ack, IMSI, MR} + ); + % MT RP-ERROR + #{message_type := mt_forward_sm_err, sm_rp_cause := Cause} -> + io:format("(IMSI:~p, MR:~p) Rx MT-forwardSM Error: cause=~p~n", [IMSI, MR, Cause]), + gen_server:cast( + State#gsupc_state.core, + {smsc_ev_mt_fwd_sm_err, IMSI, MR, Cause} + ); + _ -> + error_logger:error_report([ + "unknown GSUP message", + {module, ?MODULE}, + {message, GSUPMsg}, + {state, State} + ]) + end, + {noreply, State}; % IPA/GSUP messages from the SMSC handle_cast({smsc_ev_mt_fwd_sm_req, Data}, State) -> - #smr_data{imsi = IMSI, msg_ref = MR, - orig_addr = OA, dest_addr = DA, - tpdu = TPDU} = Data, - ipa_proto:send(State#gsupc_state.socket, ?IPA_GSUP_STREAM_ID, - #{message_class => ?GSUP_MSG_CLS_SM, message_type => mt_forward_sm_req, - imsi => IMSI, sm_rp_mr => MR, - sm_rp_da => DA, sm_rp_oa => OA, - sm_rp_ui => TPDU, - % FIXME: this must be configurable - source_name => << "EUSE-OsmoSMSC-00-00-00-00-00-00", 0 >>, - destination_name => << "MSC-00-00-00-00-00-00", 0 >> }), - {noreply, State}; - + #smr_data{ + imsi = IMSI, + msg_ref = MR, + orig_addr = OA, + dest_addr = DA, + tpdu = TPDU + } = Data, + ipa_proto:send( + State#gsupc_state.socket, + ?IPA_GSUP_STREAM_ID, + #{ + message_class => ?GSUP_MSG_CLS_SM, + message_type => mt_forward_sm_req, + imsi => IMSI, + sm_rp_mr => MR, + sm_rp_da => DA, + sm_rp_oa => OA, + sm_rp_ui => TPDU, + % FIXME: this must be configurable + source_name => <<"EUSE-OsmoSMSC-00-00-00-00-00-00", 0>>, + destination_name => <<"MSC-00-00-00-00-00-00", 0>> + } + ), + {noreply, State}; handle_cast({smr_ev_fwd_mo_sm_ack, IMSI, MR}, State) -> - ipa_proto:send(State#gsupc_state.socket, ?IPA_GSUP_STREAM_ID, - #{message_class => ?GSUP_MSG_CLS_SM, message_type => mo_forward_sm_res, - imsi => IMSI, sm_rp_mr => MR, - % FIXME: this must be configurable - source_name => << "EUSE-OsmoSMSC-00-00-00-00-00-00", 0 >>, - destination_name => << "MSC-00-00-00-00-00-00", 0 >> }), - {noreply, State}; - + ipa_proto:send( + State#gsupc_state.socket, + ?IPA_GSUP_STREAM_ID, + #{ + message_class => ?GSUP_MSG_CLS_SM, + message_type => mo_forward_sm_res, + imsi => IMSI, + sm_rp_mr => MR, + % FIXME: this must be configurable + source_name => <<"EUSE-OsmoSMSC-00-00-00-00-00-00", 0>>, + destination_name => <<"MSC-00-00-00-00-00-00", 0>> + } + ), + {noreply, State}; handle_cast({smr_ev_fwd_mo_sm_err, IMSI, MR, Cause}, State) -> - ipa_proto:send(State#gsupc_state.socket, ?IPA_GSUP_STREAM_ID, - #{message_class => ?GSUP_MSG_CLS_SM, message_type => mo_forward_sm_err, - imsi => IMSI, sm_rp_mr => MR, sm_rp_cause => Cause, - % FIXME: this must be configurable - source_name => << "EUSE-OsmoSMSC-00-00-00-00-00-00", 0 >>, - destination_name => << "MSC-00-00-00-00-00-00", 0 >> }), - {noreply, State}; - + ipa_proto:send( + State#gsupc_state.socket, + ?IPA_GSUP_STREAM_ID, + #{ + message_class => ?GSUP_MSG_CLS_SM, + message_type => mo_forward_sm_err, + imsi => IMSI, + sm_rp_mr => MR, + sm_rp_cause => Cause, + % FIXME: this must be configurable + source_name => <<"EUSE-OsmoSMSC-00-00-00-00-00-00", 0>>, + destination_name => <<"MSC-00-00-00-00-00-00", 0>> + } + ), + {noreply, State}; % All unsupported IPA messages handle_cast({ipa, StreamID, GSUPMsg}, State) -> - error_logger:error_report(["unknown IPA message", - {module, ?MODULE}, - {stream_id, StreamID}, - {message, GSUPMsg}, - {state, State}]), - {noreply, State}; - + error_logger:error_report([ + "unknown IPA message", + {module, ?MODULE}, + {stream_id, StreamID}, + {message, GSUPMsg}, + {state, State} + ]), + {noreply, State}; handle_cast(Info, State) -> - error_logger:error_report(["unknown gen_server:cast()", - {module, ?MODULE}, - {info, Info}, - {state, State}]), - {noreply, State}. + error_logger:error_report([ + "unknown gen_server:cast()", + {module, ?MODULE}, + {info, Info}, + {state, State} + ]), + {noreply, State}. handle_call(Message, _From, State) -> - error_logger:error_report(["unknown gen_server:call()", - {module, ?MODULE}, - {message, Message}, - {state, State}]), - {reply, unknown_msg, State}. + error_logger:error_report([ + "unknown gen_server:call()", + {module, ?MODULE}, + {message, Message}, + {state, State} + ]), + {reply, unknown_msg, State}. handle_info(Info, State) -> - error_logger:error_report(["unknown handle_info", - {module, ?MODULE}, - {info, Info}, - {state, State}]), - {noreply, State}. + error_logger:error_report([ + "unknown handle_info", + {module, ?MODULE}, + {info, Info}, + {state, State} + ]), + {noreply, State}. terminate(Reason, _State) -> - io:format("Terminating ~p with reason ~p~n", [?MODULE, Reason]). + io:format("Terminating ~p with reason ~p~n", [?MODULE, Reason]). code_change(_OldVsn, State, _Extra) -> - {ok, State}. + {ok, State}. diff --git a/src/osmo_smsc.app.src b/src/osmo_smsc.app.src index 31ed6b7..2c5fcbc 100644 --- a/src/osmo_smsc.app.src +++ b/src/osmo_smsc.app.src @@ -1,12 +1,12 @@ {application, osmo_smsc, [ - {description, "Osmocom SMS Center (OsmoSMSC)"}, - {vsn, "1"}, - {registered, []}, - {applications, [ - kernel, - stdlib - ]}, - {modules, []}, - {mod, {osmo_smsc_app, []}}, - {env, []} + {description, "Osmocom SMS Center (OsmoSMSC)"}, + {vsn, "1"}, + {registered, []}, + {applications, [ + kernel, + stdlib + ]}, + {modules, []}, + {mod, {osmo_smsc_app, []}}, + {env, []} ]}. diff --git a/src/osmo_smsc_app.erl b/src/osmo_smsc_app.erl index 4b78fff..272c013 100644 --- a/src/osmo_smsc_app.erl +++ b/src/osmo_smsc_app.erl @@ -4,7 +4,7 @@ -export([start/2, stop/1]). start(_StartType, _StartArgs) -> - {ok, _} = osmo_smsc_core:start_link(). + {ok, _} = osmo_smsc_core:start_link(). stop(_State) -> - ok. + ok. diff --git a/src/osmo_smsc_core.erl b/src/osmo_smsc_core.erl index ecc47f0..200704b 100644 --- a/src/osmo_smsc_core.erl +++ b/src/osmo_smsc_core.erl @@ -45,132 +45,155 @@ -record(osmo_smsc_state, {interface, map}). sm_tpdu_submit2deliver(TPDUSubmit) -> - << Hdr:1/binary, MR, Tail/binary>> = TPDUSubmit, - << RP:1, UDHI:1, SRR:1, VPF:2, RD:1, MTI:2 >> = Hdr, - - % FIXME: make sure we've got SUBMIT - ?GSM411_TP_MTI_SUBMIT = MTI, - - << DANum, _:1, ODAToN:3, ODANPI:4, DATail/binary >> = Tail, - DALen = (DANum div 2) + (DANum rem 2), % Convert number of digits to number of bits - << DA:DALen/binary, PID, DCS, VPTail/binary >> = DATail, - - % VP is optional and may be either 1 octet or 7 octet long - case VPF of - 16#00 -> - << VP:0/binary, UDL, UD/binary >> = VPTail; - 16#02 -> - << VP:8, UDL, UD/binary >> = VPTail; - _ -> - << VP:56, UDL, UD/binary >> = VPTail - end, - - io:format("Rx SM-TPDU SUBMIT: RD=~p, VPF=~p, SRR=~p, UDHI=~p, RP=~p | " - "MR=~p, PID=~p, DCS=~p, DA len=~p~n", - [RD, VPF, SRR, UDHI, RP, MR, PID, DCS, DALen]), - - % SMS-DELIVER: no Return-Path, no Status-Report, no More-Messages - << 0:1, UDHI:1, 0:1, 0:1, 0:1, 1:1, 0:2, - % FIXME: hard-coded originating address (989108987294) - 16#0c, 16#91, 16#89, 16#19, 16#80, 16#89, 16#27, 16#49, - PID, DCS, - % FIXME: hard-coded Service-Centre-Timestamp - 16#71, 16#90, 16#71, 16#21, 16#70, 16#92, 16#41, - UDL, UD/binary >>. + <> = TPDUSubmit, + <> = Hdr, + + % FIXME: make sure we've got SUBMIT + ?GSM411_TP_MTI_SUBMIT = MTI, + + <> = Tail, + % Convert number of digits to number of bits + DALen = (DANum div 2) + (DANum rem 2), + <> = DATail, + + % VP is optional and may be either 1 octet or 7 octet long + case VPF of + 16#00 -> + <> = VPTail; + 16#02 -> + <> = VPTail; + _ -> + <> = VPTail + end, + + io:format( + "Rx SM-TPDU SUBMIT: RD=~p, VPF=~p, SRR=~p, UDHI=~p, RP=~p | " + "MR=~p, PID=~p, DCS=~p, DA len=~p~n", + [RD, VPF, SRR, UDHI, RP, MR, PID, DCS, DALen] + ), + + % SMS-DELIVER: no Return-Path, no Status-Report, no More-Messages + <<0:1, UDHI:1, 0:1, 0:1, 0:1, 1:1, 0:2, + % FIXME: hard-coded originating address (989108987294) + 16#0c, 16#91, 16#89, 16#19, 16#80, 16#89, 16#27, 16#49, PID, DCS, + % FIXME: hard-coded Service-Centre-Timestamp + 16#71, 16#90, 16#71, 16#21, 16#70, 16#92, 16#41, UDL, UD/binary>>. %% ------------------------------------------------------------------ %% our exported API %% ------------------------------------------------------------------ start() -> - gen_server:start(?MODULE, [], [{debug, [trace]}]). + gen_server:start(?MODULE, [], [{debug, [trace]}]). start_link() -> - gen_server:start_link(?MODULE, [], [{debug, [trace]}]). + gen_server:start_link(?MODULE, [], [{debug, [trace]}]). %% ------------------------------------------------------------------ %% gen_server Function Definitions %% ------------------------------------------------------------------ init([]) -> - % FIXME: IPA/GSUP is the only supported interface for now - {ok, Pid} = osmo_gsup_if:start_link(self(), {"127.0.0.1", 4222, []}), - {ok, #osmo_smsc_state{interface = Pid, map = #{}}}. + % FIXME: IPA/GSUP is the only supported interface for now + {ok, Pid} = osmo_gsup_if:start_link(self(), {"127.0.0.1", 4222, []}), + {ok, #osmo_smsc_state{interface = Pid, map = #{}}}. % Events coming from the external interfaces (IPA/GSUP, MAP, ...) handle_cast({smsc_ev_mo_fwd_sm_req, IMSI, MR, DA, OA, TPDU}, State) -> - {Interface, Core} = {State#osmo_smsc_state.interface, self()}, - {ok, MOFsm} = sm_rp_trans:start(Interface, Core), - {ok, MTFsm} = sm_rp_trans:start(Interface, Core), - - % FIXME: check if we already have Key = {IMSI, MR} - Mapping = maps:put({IMSI, MR}, {MOFsm, MTFsm}, - State#osmo_smsc_state.map), - - % HACK: we do not (yet) have the database, so here we forward the SMS immediately - ok = gen_statem:call(MOFsm, {smr_ev_fwd_mo_sm_req, #smr_data{ - imsi = IMSI, msg_ref = MR, - orig_addr = OA, dest_addr = DA, - tpdu = TPDU}}), - ok = gen_statem:call(MTFsm, {smr_ev_fwd_mt_sm_req, #smr_data{ - imsi = IMSI, msg_ref = 16#ff, orig_addr = DA, - dest_addr = OA, % FIXME: extract from TPDU! - tpdu = sm_tpdu_submit2deliver(TPDU)}}), - - {noreply, State#osmo_smsc_state{map = Mapping}}; - + {Interface, Core} = {State#osmo_smsc_state.interface, self()}, + {ok, MOFsm} = sm_rp_trans:start(Interface, Core), + {ok, MTFsm} = sm_rp_trans:start(Interface, Core), + + % FIXME: check if we already have Key = {IMSI, MR} + Mapping = maps:put( + {IMSI, MR}, + {MOFsm, MTFsm}, + State#osmo_smsc_state.map + ), + + % HACK: we do not (yet) have the database, so here we forward the SMS immediately + ok = gen_statem:call( + MOFsm, + {smr_ev_fwd_mo_sm_req, #smr_data{ + imsi = IMSI, + msg_ref = MR, + orig_addr = OA, + dest_addr = DA, + tpdu = TPDU + }} + ), + ok = gen_statem:call( + MTFsm, + {smr_ev_fwd_mt_sm_req, #smr_data{ + imsi = IMSI, + msg_ref = 16#ff, + orig_addr = DA, + % FIXME: extract from TPDU! + dest_addr = OA, + tpdu = sm_tpdu_submit2deliver(TPDU) + }} + ), + + {noreply, State#osmo_smsc_state{map = Mapping}}; handle_cast({smsc_ev_mt_fwd_sm_ack, IMSI, MR}, State) -> - {MOFsm, MTFsm} = maps:get({IMSI, MR}, State#osmo_smsc_state.map), - ok = gen_statem:call(MTFsm, smr_ev_fwd_mt_sm_ack), - ok = gen_statem:call(MOFsm, smr_ev_fwd_mo_sm_ack), - {noreply, State}; - + {MOFsm, MTFsm} = maps:get({IMSI, MR}, State#osmo_smsc_state.map), + ok = gen_statem:call(MTFsm, smr_ev_fwd_mt_sm_ack), + ok = gen_statem:call(MOFsm, smr_ev_fwd_mo_sm_ack), + {noreply, State}; handle_cast({smsc_ev_mt_fwd_sm_err, IMSI, MR, Cause}, State) -> - {MOFsm, MTFsm} = maps:get({IMSI, MR}, State#osmo_smsc_state.map), - ok = gen_statem:call(MTFsm, {smr_ev_fwd_mt_sm_err, Cause}), - ok = gen_statem:call(MOFsm, {smr_ev_fwd_mo_sm_err, Cause}), - {noreply, State}; - + {MOFsm, MTFsm} = maps:get({IMSI, MR}, State#osmo_smsc_state.map), + ok = gen_statem:call(MTFsm, {smr_ev_fwd_mt_sm_err, Cause}), + ok = gen_statem:call(MOFsm, {smr_ev_fwd_mo_sm_err, Cause}), + {noreply, State}; % Events coming from SMR state machines % TODO: remove mappings on termination % FIXME: we need IMSI! handle_cast({smr_ev_fwd_mo_sm_req, {IMSI, MR, OA, DA, TPDU}}, State) -> - {_MOFsm, MTFsm} = maps:get({IMSI, MR}, State#osmo_smsc_state.map), - ok = gen_statem:call(MTFsm, {smsc_ev_mt_fwd_sm_req, #smr_data{ - msg_ref = 16#ff, orig_addr = DA, - dest_addr = OA, % FIXME: extract from TPDU! - tpdu = sm_tpdu_submit2deliver(TPDU)}}), - {noreply, State}; - + {_MOFsm, MTFsm} = maps:get({IMSI, MR}, State#osmo_smsc_state.map), + ok = gen_statem:call( + MTFsm, + {smsc_ev_mt_fwd_sm_req, #smr_data{ + msg_ref = 16#ff, + orig_addr = DA, + % FIXME: extract from TPDU! + dest_addr = OA, + tpdu = sm_tpdu_submit2deliver(TPDU) + }} + ), + {noreply, State}; handle_cast({smr_ev_mt_fwd_sm_ack, IMSI, MR}, State) -> - {noreply, State}; - + {noreply, State}; handle_cast({smr_ev_mt_fwd_sm_err, IMSI, MR, Cause}, State) -> - {noreply, State}; - + {noreply, State}; handle_cast(Message, State) -> - error_logger:error_report(["unknown gen_server:cast()", - {module, ?MODULE}, - {message, Message}, - {state, State}]), - {noreply, State}. + error_logger:error_report([ + "unknown gen_server:cast()", + {module, ?MODULE}, + {message, Message}, + {state, State} + ]), + {noreply, State}. handle_call(Message, _From, State) -> - error_logger:error_report(["unknown gen_server:call()", - {module, ?MODULE}, - {message, Message}, - {state, State}]), - {reply, unknown_call, State}. + error_logger:error_report([ + "unknown gen_server:call()", + {module, ?MODULE}, + {message, Message}, + {state, State} + ]), + {reply, unknown_call, State}. handle_info(Info, State) -> - error_logger:error_report(["unknown handle_info", - {module, ?MODULE}, - {info, Info}, - {state, State}]), - {noreply, State}. + error_logger:error_report([ + "unknown handle_info", + {module, ?MODULE}, + {info, Info}, + {state, State} + ]), + {noreply, State}. terminate(Reason, _State) -> - io:format("Terminating ~p with reason ~p~n", [?MODULE, Reason]). + io:format("Terminating ~p with reason ~p~n", [?MODULE, Reason]). code_change(_OldVsn, State, _Extra) -> - {ok, State}. + {ok, State}. diff --git a/src/sm_rp_trans.erl b/src/sm_rp_trans.erl index 775f4d0..7f022ce 100644 --- a/src/sm_rp_trans.erl +++ b/src/sm_rp_trans.erl @@ -36,24 +36,24 @@ % gen_statem callbacks -export([ - init/1, - terminate/3, - code_change/4, - callback_mode/0 - ]). + init/1, + terminate/3, + code_change/4, + callback_mode/0 +]). % State functions (section 6.2.2) -export([ - smr_st_idle/3, - smr_st_wait_rx_ack/3, - smr_st_wait_tx_ack/3 - ]). + smr_st_idle/3, + smr_st_wait_rx_ack/3, + smr_st_wait_tx_ack/3 +]). % API functions -export([ - start_link/2, - start/2 - ]). + start_link/2, + start/2 +]). % 3GPP TS 24.011, chapter 10: Timers -define(GSM411_TMR_TR1N, 40 * 1000). @@ -66,90 +66,96 @@ %% API functions start_link(Interface, Core) -> - gen_statem:start_link(?MODULE, [Interface, Core], []). + gen_statem:start_link(?MODULE, [Interface, Core], []). start(Interface, Core) -> - gen_statem:start(?MODULE, [Interface, Core], []). + gen_statem:start(?MODULE, [Interface, Core], []). %% gen_statem callbacks init([Interface, Core]) -> - {ok, smr_st_idle, #smr_state{iface = Interface, core = Core, data = nope}}. + {ok, smr_st_idle, #smr_state{iface = Interface, core = Core, data = nope}}. % Define a separate handler function for each state callback_mode() -> - state_functions. + state_functions. % Rx MO RP-DATA from the MSC, forward to the SMSC smr_st_idle({call, From}, {smr_ev_fwd_mo_sm_req, Data}, State) -> - {next_state, smr_st_wait_tx_ack, - State#smr_state{data = Data}, - [{reply, From, ok}, {state_timeout, ?GSM411_TMR_TR2N, wait_tx_ack}]}; + {next_state, smr_st_wait_tx_ack, State#smr_state{data = Data}, [ + {reply, From, ok}, {state_timeout, ?GSM411_TMR_TR2N, wait_tx_ack} + ]}; % Rx MO RP-SMMA from the MSC, notify the SMSC smr_st_idle({call, From}, {smr_ev_fwd_mo_smma_req, Data}, State) -> - {next_state, smr_st_wait_tx_ack, - State#smr_state{data = Data}, - [{reply, From, ok}, {state_timeout, ?GSM411_TMR_TR2N, wait_tx_ack}]}; + {next_state, smr_st_wait_tx_ack, State#smr_state{data = Data}, [ + {reply, From, ok}, {state_timeout, ?GSM411_TMR_TR2N, wait_tx_ack} + ]}; % Rx MT RP-DATA from the SMSC, forward to the MSC smr_st_idle({call, From}, {smr_ev_fwd_mt_sm_req, Data}, State) -> - gen_server:cast(State#smr_state.iface, {smsc_ev_mt_fwd_sm_req, Data}), - {next_state, smr_st_wait_rx_ack, - State#smr_state{data = Data}, - [{reply, From, ok}, {state_timeout, ?GSM411_TMR_TR1N, wait_rx_ack}]}; + gen_server:cast(State#smr_state.iface, {smsc_ev_mt_fwd_sm_req, Data}), + {next_state, smr_st_wait_rx_ack, State#smr_state{data = Data}, [ + {reply, From, ok}, {state_timeout, ?GSM411_TMR_TR1N, wait_rx_ack} + ]}; % Trap for unhandled events smr_st_idle(EventType, EventContent, State) -> - io:format("Event ~p (~p) unhandled in state IDLE~n", - [EventContent, EventType]), - {keep_state, State}. + io:format( + "Event ~p (~p) unhandled in state IDLE~n", + [EventContent, EventType] + ), + {keep_state, State}. % Rx MO RP-ACK from the SMSC, forward to the MSC smr_st_wait_tx_ack({call, From}, smr_ev_fwd_mo_sm_ack, State) -> - SMRData = State#smr_state.data, MR = SMRData#smr_data.msg_ref, - gen_server:cast(State#smr_state.iface, - {smsc_ev_mo_fwd_sm_ack, - SMRData#smr_data.imsi, - SMRData#smr_data.msg_ref}), - {stop, normal, State}; + SMRData = State#smr_state.data, + MR = SMRData#smr_data.msg_ref, + gen_server:cast( + State#smr_state.iface, + {smsc_ev_mo_fwd_sm_ack, SMRData#smr_data.imsi, SMRData#smr_data.msg_ref} + ), + {stop, normal, State}; % Rx MO RP-ERROR from the SMSC, forward to the MSC smr_st_wait_tx_ack({call, From}, {smr_ev_fwd_mo_sm_err, Cause}, State) -> - SMRData = State#smr_state.data, - gen_server:cast(State#smr_state.iface, - {smsc_ev_mo_fwd_sm_err, - SMRData#smr_data.imsi, - SMRData#smr_data.msg_ref, - Cause}), - {stop, normal, State}; + SMRData = State#smr_state.data, + gen_server:cast( + State#smr_state.iface, + {smsc_ev_mo_fwd_sm_err, SMRData#smr_data.imsi, SMRData#smr_data.msg_ref, Cause} + ), + {stop, normal, State}; % Timeout waiting for RP-ACK / RP-ERROR smr_st_wait_tx_ack(state_timeout, wait_tx_ack, S) -> - % TODO: we probably want to try again here - % TODO: notify the MSC/MS about that - io:format("Timeout waiting for RP-ACK / RP-ERROR for MO SMS~n"), - {stop, timeout, S}; + % TODO: we probably want to try again here + % TODO: notify the MSC/MS about that + io:format("Timeout waiting for RP-ACK / RP-ERROR for MO SMS~n"), + {stop, timeout, S}; smr_st_wait_tx_ack(EventType, EventContent, State) -> - io:format("Event ~p (~p) unhandled in state WAIT_TX_ACK~n", - [EventContent, EventType]), - {keep_state, State}. + io:format( + "Event ~p (~p) unhandled in state WAIT_TX_ACK~n", + [EventContent, EventType] + ), + {keep_state, State}. % Rx MT RP-ACK from the MSC, forward to the SMSC smr_st_wait_rx_ack({call, From}, smr_ev_fwd_mt_sm_ack, S) -> - {stop, normal, S}; + {stop, normal, S}; % Rx MT RP-ERROR from the MSC, forward to the SMSC smr_st_wait_rx_ack({call, From}, {smr_ev_fwd_mt_sm_err, _Cause}, S) -> - {stop, normal, S}; + {stop, normal, S}; % Timeout waiting for RP-ACK / RP-ERROR smr_st_wait_rx_ack(state_timeout, wait_rx_ack, S) -> - % TODO: we probably want to try again here - % TODO: notify the SMSC about that - io:format("Timeout waiting for RP-ACK / RP-ERROR for MT SMS~n"), - {stop, timeout, S}; + % TODO: we probably want to try again here + % TODO: notify the SMSC about that + io:format("Timeout waiting for RP-ACK / RP-ERROR for MT SMS~n"), + {stop, timeout, S}; smr_st_wait_rx_ack(EventType, EventContent, State) -> - io:format("Event ~p (~p) unhandled in state WAIT_RX_ACK~n", - [EventContent, EventType]), - {keep_state, State}. + io:format( + "Event ~p (~p) unhandled in state WAIT_RX_ACK~n", + [EventContent, EventType] + ), + {keep_state, State}. terminate(Reason, StateName, _State) -> - io:format("Terminating in state ~p (reason ~p)~n", [StateName, Reason]), - ok. + io:format("Terminating in state ~p (reason ~p)~n", [StateName, Reason]), + ok. code_change(_OldVsn, StateName, State, _Extra) -> - {ok, StateName, State}. + {ok, StateName, State}. diff --git a/src/sm_tp_codec.erl b/src/sm_tp_codec.erl index 0a0e52a..56812b8 100644 --- a/src/sm_tp_codec.erl +++ b/src/sm_tp_codec.erl @@ -36,378 +36,403 @@ -export([encode/2, decode/2]). --ifdef (TEST). --export ([encode_tp_addr/2, decode_tp_addr/1]). --export ([encode_tp_scts/2, decode_tp_scts/1]). --export ([decode_tp_vp/2]). +-ifdef(TEST). +-export([encode_tp_addr/2, decode_tp_addr/1]). +-export([encode_tp_scts/2, decode_tp_scts/1]). +-export([decode_tp_vp/2]). -endif. %% 9.2.3.1 "TP-Message-Type-Indicator (TP-MTI)" --define(TP_MTI_SUBMIT, 02#01). --define(TP_MTI_COMMAND, 02#10). --define(TP_MTI_DELIVER, 02#00). +-define(TP_MTI_SUBMIT, 02#01). +-define(TP_MTI_COMMAND, 02#10). +-define(TP_MTI_DELIVER, 02#00). --define(TP_MTI_SUBMIT_REPORT, 02#01). --define(TP_MTI_STATUS_REPORT, 02#10). --define(TP_MTI_DELIVER_REPORT, 02#00). +-define(TP_MTI_SUBMIT_REPORT, 02#01). +-define(TP_MTI_STATUS_REPORT, 02#10). +-define(TP_MTI_DELIVER_REPORT, 02#00). %% 9.2.3.3 "TP-Validity-Period-Format (TP-VPF)" --define(TP_VP_VPF_ABSENT, 02#00). --define(TP_VP_VPF_RELATIVE, 02#10). --define(TP_VP_VPF_ENHANCED, 02#01). --define(TP_VP_VPF_ABSOLUTE, 02#11). +-define(TP_VP_VPF_ABSENT, 02#00). +-define(TP_VP_VPF_RELATIVE, 02#10). +-define(TP_VP_VPF_ENHANCED, 02#01). +-define(TP_VP_VPF_ABSOLUTE, 02#11). %% 9.2.3.12.3 "TP-Validity-Period (Enhanced format)" sub-types --define(TP_VP_EVPF_ABSENT, 02#000). --define(TP_VP_EVPF_RELATIVE, 02#001). --define(TP_VP_EVPF_RELATIVE_SEC, 02#010). --define(TP_VP_EVPF_RELATIVE_BCD, 02#011). +-define(TP_VP_EVPF_ABSENT, 02#000). +-define(TP_VP_EVPF_RELATIVE, 02#001). +-define(TP_VP_EVPF_RELATIVE_SEC, 02#010). +-define(TP_VP_EVPF_RELATIVE_BCD, 02#011). %% 9.2.2 TP-User-Data may contain up to 140 octets %% (together with optional TP-User-Data-Header) --define(TP_USER_DATA_MAX_LEN, 140). --define(TP_USER_DATA_PADDING, 16#ff). +-define(TP_USER_DATA_MAX_LEN, 140). +-define(TP_USER_DATA_PADDING, 16#ff). %% Encode TP-{Originating,Destination}-Address (see 9.2.3.7 and 9.2.3.8). %% NOTE: The Addr is expected to be octet-aligned and encoded by the caller. %% TODO: The maximum length of the full address field is 12 octets (see 9.1.2.5). encode_tp_addr({NPI, ToN}, {AddrBitLen, Addr}) when is_binary(Addr) -> - %% QBitNum indicates number of useful nibbles (octet halves) in the Address - %% regardless of the indicated ToN (e.g. BCD number or alphanumeric), - %% excluding any nibbles containing only fill bits. - case {AddrBitLen div 4, AddrBitLen rem 4} of - {QBitEven, QBitRem} when QBitRem > 0 -> - AddrQBitLen = QBitEven + 1; - {QBitEven, 0} -> - AddrQBitLen = QBitEven - end, - - %% As per 9.1.2.5, the 1st MSB of the 2nd octet shall be set high. - << AddrQBitLen, 1:1, ToN:3, NPI:4, Addr/bytes >>. + %% QBitNum indicates number of useful nibbles (octet halves) in the Address + %% regardless of the indicated ToN (e.g. BCD number or alphanumeric), + %% excluding any nibbles containing only fill bits. + case {AddrBitLen div 4, AddrBitLen rem 4} of + {QBitEven, QBitRem} when QBitRem > 0 -> + AddrQBitLen = QBitEven + 1; + {QBitEven, 0} -> + AddrQBitLen = QBitEven + end, + + %% As per 9.1.2.5, the 1st MSB of the 2nd octet shall be set high. + <>. %% Decode TP-{Originating,Destination}-Address (see 9.2.3.7 and 9.2.3.8). %% NOTE: The returned Addr is octet-aligned and to be decoded by the caller. decode_tp_addr(TPDU) when is_binary(TPDU) -> - %% QBitLen indicates number of useful nibbles (octet halves) in the Address - %% regardless of the indicated ToN (e.g. BCD number or alphanumeric), - %% excluding any nibbles containing only fill bits. - %% As per 9.1.2.5, the 1st MSB of the 2nd octet shall be set high. - << QBitLen, 1:1, ToN:3, NPI:4, AddrRest/bytes >> = TPDU, + %% QBitLen indicates number of useful nibbles (octet halves) in the Address + %% regardless of the indicated ToN (e.g. BCD number or alphanumeric), + %% excluding any nibbles containing only fill bits. + %% As per 9.1.2.5, the 1st MSB of the 2nd octet shall be set high. + <> = TPDU, - %% Full address field shall be octet-aligned, so there may be optional padding. - if (QBitLen rem 2) =/= 0 -> - AddrByteLen = QBitLen div 2 + 1; - true -> - AddrByteLen = QBitLen div 2 - end, + %% Full address field shall be octet-aligned, so there may be optional padding. + if + (QBitLen rem 2) =/= 0 -> + AddrByteLen = QBitLen div 2 + 1; + true -> + AddrByteLen = QBitLen div 2 + end, - << Addr:AddrByteLen/bytes, Rest/bytes >> = AddrRest, + <> = AddrRest, - {ok, {NPI, ToN}, {QBitLen * 4, Addr}, Rest}. + {ok, {NPI, ToN}, {QBitLen * 4, Addr}, Rest}. %% Encode TP-Service-Centre-Time-Stamp as per 9.2.3.11. encode_tp_scts(current, TimeZone) -> - LocalTime = erlang:localtime(), - encode_tp_scts(LocalTime, TimeZone); + LocalTime = erlang:localtime(), + encode_tp_scts(LocalTime, TimeZone); encode_tp_scts(LocalTime, {TZHour, TZMin}) when (TZMin rem 15) == 0 -> - %% Encoded timezone is in quarters of an hour (signed) - if TZHour < 0 -> - AbsOffset = (TZHour * 60 * -1 + TZMin) div 15, - encode_tp_scts(LocalTime, 1, AbsOffset); - true -> - AbsOffset = (TZHour * 60 + TZMin) div 15, - encode_tp_scts(LocalTime, 0, AbsOffset) - end. + %% Encoded timezone is in quarters of an hour (signed) + if + TZHour < 0 -> + AbsOffset = (TZHour * 60 * -1 + TZMin) div 15, + encode_tp_scts(LocalTime, 1, AbsOffset); + true -> + AbsOffset = (TZHour * 60 + TZMin) div 15, + encode_tp_scts(LocalTime, 0, AbsOffset) + end. encode_tp_scts({{Year, Month, Day}, {Hour, Min, Sec}}, Minus, AbsOffset) -> - %% BCD-encode date and time as YY:MM:DD:hh:mm:ss. - DateTimeList = lists:map(fun bcd_codec:encode_num/1, - [Year rem 100, Month, Day, Hour, Min, Sec]), - DateTime = list_to_binary(DateTimeList), + %% BCD-encode date and time as YY:MM:DD:hh:mm:ss. + DateTimeList = lists:map( + fun bcd_codec:encode_num/1, + [Year rem 100, Month, Day, Hour, Min, Sec] + ), + DateTime = list_to_binary(DateTimeList), - %% BCD-encode the timezone offset. - N1 = AbsOffset div 10, - N2 = AbsOffset rem 10, - TZ = << N2:4, Minus:1, N1:3 >>, + %% BCD-encode the timezone offset. + N1 = AbsOffset div 10, + N2 = AbsOffset rem 10, + TZ = <>, - << DateTime/bytes, TZ/bytes >>. + <>. %% Decode TP-Service-Centre-Time-Stamp as per 9.2.3.11. -decode_tp_scts(<< DateTime:6/bytes, N2:4, Minus:1, N1:3 >>) -> - %% Decode BCD-coded date and time as YY:MM:DD:hh:mm:ss. - DateTimeList = lists:map(fun bcd_codec:decode_num/1, - binary_to_list(DateTime)), - [Year, Month, Day, Hour, Min, Sec] = DateTimeList, - - %% Decode BCD-encoded (in quarters of an hour) timezone offset. - AbsOffset = (N1 * 10) + N2, - if Minus == 1 -> - TZHour = -AbsOffset div 4; - true -> - TZHour = AbsOffset div 4 - end, - TZMin = AbsOffset * 15 rem 60, - - {ok, {{Year, Month, Day}, {Hour, Min, Sec}}, {TZHour, TZMin}}. +decode_tp_scts(<>) -> + %% Decode BCD-coded date and time as YY:MM:DD:hh:mm:ss. + DateTimeList = lists:map( + fun bcd_codec:decode_num/1, + binary_to_list(DateTime) + ), + [Year, Month, Day, Hour, Min, Sec] = DateTimeList, + + %% Decode BCD-encoded (in quarters of an hour) timezone offset. + AbsOffset = (N1 * 10) + N2, + if + Minus == 1 -> + TZHour = -AbsOffset div 4; + true -> + TZHour = AbsOffset div 4 + end, + TZMin = AbsOffset * 15 rem 60, + + {ok, {{Year, Month, Day}, {Hour, Min, Sec}}, {TZHour, TZMin}}. %% TP-Validity-Period is not present. decode_tp_vp(?TP_VP_VPF_ABSENT, Rest) -> - {ok, omit, Rest}; - + {ok, omit, Rest}; %% 9.2.3.12.1 TP-Validity-Period (Relative format). %% NOTE: Returned value is always in seconds. -decode_tp_vp(?TP_VP_VPF_RELATIVE, << VP:8, Rest/bytes >>) -> - if VP >= 0, VP =< 143 -> - Minutes = (VP + 1) * 5; - VP >= 144, VP =< 167 -> - %% 12 hours + (VP - 143) * 30 minutes - Minutes = 12 * 60 + (VP - 143) * 30; - VP >= 168, VP =< 196 -> - %% (VP - 166) days - Minutes = (VP - 166) * 24 * 60; - VP >= 197, VP =< 255 -> - %% (VP - 192) weeks - Minutes = (VP - 192) * 7 * 24 * 60 - end, - - {ok, {relative, Minutes * 60}, Rest}; - +decode_tp_vp(?TP_VP_VPF_RELATIVE, <>) -> + if + VP >= 0, VP =< 143 -> + Minutes = (VP + 1) * 5; + VP >= 144, VP =< 167 -> + %% 12 hours + (VP - 143) * 30 minutes + Minutes = 12 * 60 + (VP - 143) * 30; + VP >= 168, VP =< 196 -> + %% (VP - 166) days + Minutes = (VP - 166) * 24 * 60; + VP >= 197, VP =< 255 -> + %% (VP - 192) weeks + Minutes = (VP - 192) * 7 * 24 * 60 + end, + + {ok, {relative, Minutes * 60}, Rest}; %% 9.2.3.12.2 TP-Validity-Period (Absolute format). %% The representation of time is identical to TP-Service-Centre-Time-Stamp. -decode_tp_vp(?TP_VP_VPF_ABSOLUTE, << VP:7/bytes, Rest/bytes >>) -> - {ok, DateTime, TimeZone} = decode_tp_scts(VP), - {ok, {absolute, DateTime, TimeZone}, Rest}; - +decode_tp_vp(?TP_VP_VPF_ABSOLUTE, <>) -> + {ok, DateTime, TimeZone} = decode_tp_scts(VP), + {ok, {absolute, DateTime, TimeZone}, Rest}; %% 9.2.3.12.3 TP-Validity-Period (Enhanced format, no extensions). -decode_tp_vp(?TP_VP_VPF_ENHANCED, << 0:1, SingleShot:1, _:3, EVPF:3, - VP:6/bytes, Rest/bytes >>) -> - case EVPF of - ?TP_VP_EVPF_ABSENT -> - Result = omit; - ?TP_VP_EVPF_RELATIVE -> - {ok, Result, _} = decode_tp_vp(?TP_VP_VPF_RELATIVE, VP); - ?TP_VP_EVPF_RELATIVE_SEC -> - << Seconds, _VPRest/bytes >> = VP, - Result = {relative, Seconds}; - ?TP_VP_EVPF_RELATIVE_BCD -> - << Hh, Mm, Ss, _VPRest/bytes >> = VP, - Seconds = bcd_codec:decode_num(Hh) * 60 * 60 - + bcd_codec:decode_num(Mm) * 60 - + bcd_codec:decode_num(Ss), - Result = {relative, Seconds}; - _ -> - Result = reserved - end, - - {ok, {enhanced, SingleShot, Result}, Rest}; - -decode_tp_vp(02#01, << 1:1, _/bits >>) -> error({vpf_ext_not_supported}); - -decode_tp_vp(VPF, _) -> error({unknown_vpf, VPF}). +decode_tp_vp(?TP_VP_VPF_ENHANCED, <<0:1, SingleShot:1, _:3, EVPF:3, VP:6/bytes, Rest/bytes>>) -> + case EVPF of + ?TP_VP_EVPF_ABSENT -> + Result = omit; + ?TP_VP_EVPF_RELATIVE -> + {ok, Result, _} = decode_tp_vp(?TP_VP_VPF_RELATIVE, VP); + ?TP_VP_EVPF_RELATIVE_SEC -> + <> = VP, + Result = {relative, Seconds}; + ?TP_VP_EVPF_RELATIVE_BCD -> + <> = VP, + Seconds = + bcd_codec:decode_num(Hh) * 60 * 60 + + bcd_codec:decode_num(Mm) * 60 + + bcd_codec:decode_num(Ss), + Result = {relative, Seconds}; + _ -> + Result = reserved + end, + + {ok, {enhanced, SingleShot, Result}, Rest}; +decode_tp_vp(02#01, <<1:1, _/bits>>) -> + error({vpf_ext_not_supported}); +decode_tp_vp(VPF, _) -> + error({unknown_vpf, VPF}). %% Decode TP-User-Data and TP-User-Data-Header (if present). %% Depending on DCS, TP-User-Data-Length can be in septets or octets. decode_tp_ud(_UDHI, _DCS, TPDU) -> - %% TODO: actually decode UD-Header if present - %% NOTE: UDLen may indicate either the number of octets or septets - %% NOTE: UDHLen always in octets - << _UDLen, UserData/bytes >> = TPDU, - {ok, {[], UserData}}. + %% TODO: actually decode UD-Header if present + %% NOTE: UDLen may indicate either the number of octets or septets + %% NOTE: UDHLen always in octets + <<_UDLen, UserData/bytes>> = TPDU, + {ok, {[], UserData}}. %% 23.040 9.2.2.2 decode_submit(TPDU) -> - %% Header and TP-Message-Reference - << RP:1, UDHI:1, SRR:1, VPF:2, RD:1, ?TP_MTI_SUBMIT:2, - MR, Rest1/binary >> = TPDU, - - %% TP-Destination-Address (variable size, odd units, padding) - {ok, DAType, DA, Rest2} = decode_tp_addr(Rest1), - - %% TP-Protocol-Identifier and TP-Data-Coding-Scheme - << PID, DCS, Rest3/binary >> = Rest2, - %% followed by optional TP-Validity-Period (depends on VPF) - {ok, VP, Rest4} = decode_tp_vp(VPF, Rest3), - - %% Finally, TP-User-Data (and optional UD-Header) - {ok, UD} = decode_tp_ud(UDHI, DCS, Rest4), - - {ok, {sm_tp_submit, #{reply_path_req => RP, - status_report_req => SRR, - reject_duplicates => RD, - tp_msg_ref => MR, - dest_addr => {DAType, DA}, - pid => PID, dcs => DCS, - valid_period => VP, - user_data => UD}}}. + %% Header and TP-Message-Reference + <> = TPDU, + + %% TP-Destination-Address (variable size, odd units, padding) + {ok, DAType, DA, Rest2} = decode_tp_addr(Rest1), + + %% TP-Protocol-Identifier and TP-Data-Coding-Scheme + <> = Rest2, + %% followed by optional TP-Validity-Period (depends on VPF) + {ok, VP, Rest4} = decode_tp_vp(VPF, Rest3), + + %% Finally, TP-User-Data (and optional UD-Header) + {ok, UD} = decode_tp_ud(UDHI, DCS, Rest4), + + {ok, + {sm_tp_submit, #{ + reply_path_req => RP, + status_report_req => SRR, + reject_duplicates => RD, + tp_msg_ref => MR, + dest_addr => {DAType, DA}, + pid => PID, + dcs => DCS, + valid_period => VP, + user_data => UD + }}}. %% 23.040 9.2.2.4 decode_command(TPDU) -> - << _:1, _UDHI:1, SRR:1, _:3, ?TP_MTI_COMMAND:2, - MR, PID, CT, MN, Rest1/binary >> = TPDU, + <<_:1, _UDHI:1, SRR:1, _:3, ?TP_MTI_COMMAND:2, MR, PID, CT, MN, Rest1/binary>> = TPDU, - %% TP-Destination-Address (variable size, odd units, padding) - {ok, DAType, DA, Rest2} = decode_tp_addr(Rest1), + %% TP-Destination-Address (variable size, odd units, padding) + {ok, DAType, DA, Rest2} = decode_tp_addr(Rest1), - %% TP-Command-Data and its length - << CDLen, CommandData:CDLen/bytes >> = Rest2, + %% TP-Command-Data and its length + <> = Rest2, - {ok, {sm_tp_command, #{status_report_req => SRR, - tp_msg_ref => MR, - pid => PID, - cmd_type => CT, - msg_number => MN, - dest_addr => {DAType, DA}, - cmd_data => CommandData}}}. + {ok, + {sm_tp_command, #{ + status_report_req => SRR, + tp_msg_ref => MR, + pid => PID, + cmd_type => CT, + msg_number => MN, + dest_addr => {DAType, DA}, + cmd_data => CommandData + }}}. %% 23.040 9.2.2.2a (ii) decode_submit_report_ack(TPDU) -> - %% NOTE: Extension bit (for PID) != 0 is not supported - << _:1, UDHI:1, _:4, ?TP_MTI_SUBMIT_REPORT:2, - 0:1, _:4, UDPres:1, DCSPres:1, PIDPres:1, - SCTS:7/bytes, Rest1/binary >> = TPDU, - - %% PID, DCS fields are optional (presence defined by flags) - << PID:PIDPres/unit:8, DCS:DCSPres/unit:8, Rest2/bytes >> = Rest1, - - if %% TP-User-Data is also optional - UDPres == 1, size(Rest2) > 0 -> - {ok, UD} = decode_tp_ud(UDHI, DCS, Rest2); - true -> - UD = omit - end, - - %% TODO: further decode TP-Service-Centre-Time-Stamp - {ok, {sm_tp_submit_report, #{sc_time_stamp => SCTS, - pid => PID, dcs => DCS, - user_data => UD}}}. + %% NOTE: Extension bit (for PID) != 0 is not supported + <<_:1, UDHI:1, _:4, ?TP_MTI_SUBMIT_REPORT:2, 0:1, _:4, UDPres:1, DCSPres:1, PIDPres:1, + SCTS:7/bytes, Rest1/binary>> = TPDU, + + %% PID, DCS fields are optional (presence defined by flags) + <> = Rest1, + + %% TP-User-Data is also optional + if + UDPres == 1, size(Rest2) > 0 -> + {ok, UD} = decode_tp_ud(UDHI, DCS, Rest2); + true -> + UD = omit + end, + + %% TODO: further decode TP-Service-Centre-Time-Stamp + {ok, + {sm_tp_submit_report, #{ + sc_time_stamp => SCTS, + pid => PID, + dcs => DCS, + user_data => UD + }}}. %% 23.040 9.2.2.2a (i) decode_submit_report_error(TPDU) -> - %% NOTE: Extension bit (for PID) != 0 is not supported - << _:1, UDHI:1, _:4, ?TP_MTI_SUBMIT_REPORT:2, - Cause, 0:1, _:4, UDPres:1, DCSPres:1, PIDPres:1, - SCTS:7/bytes, Rest1/binary >> = TPDU, - - %% PID, DCS fields are optional (presence defined by flags) - << PID:PIDPres/unit:8, DCS:DCSPres/unit:8, Rest2/bytes >> = Rest1, - - if %% TP-User-Data is also optional - UDPres == 1, size(Rest2) > 0 -> - {ok, UD} = decode_tp_ud(UDHI, DCS, Rest2); - true -> - UD = omit - end, - - %% TODO: further decode TP-Service-Centre-Time-Stamp - {ok, {sm_tp_submit_report, #{sc_time_stamp => SCTS, - pid => PID, dcs => DCS, - cause => Cause, - user_data => UD}}}. - -decode(RPMsgType, << _:6, MTI:2, _/bits >> = TPDU) -> - case {RPMsgType, MTI} of - %% Mobile originated message submission - {sm_rp_mo_data, ?TP_MTI_SUBMIT} -> - %% MS -> MSC/SGSN: RP-DATA - decode_submit(TPDU); - {sm_rp_mo_data, ?TP_MTI_COMMAND} -> - %% MS -> MSC/SGSN: RP-DATA - decode_command(TPDU); - {sm_rp_mo_ack, ?TP_MTI_SUBMIT_REPORT} -> - %% MSC/SGSN -> MS: RP-ACK - decode_submit_report_ack(TPDU); - {sm_rp_mo_error, ?TP_MTI_SUBMIT_REPORT} -> - %% MSC/SGSN -> MS: RP-ERROR - decode_submit_report_error(TPDU); - _ -> - {error, unknown_msg} - end. + %% NOTE: Extension bit (for PID) != 0 is not supported + <<_:1, UDHI:1, _:4, ?TP_MTI_SUBMIT_REPORT:2, Cause, 0:1, _:4, UDPres:1, DCSPres:1, PIDPres:1, + SCTS:7/bytes, Rest1/binary>> = TPDU, + + %% PID, DCS fields are optional (presence defined by flags) + <> = Rest1, + + %% TP-User-Data is also optional + if + UDPres == 1, size(Rest2) > 0 -> + {ok, UD} = decode_tp_ud(UDHI, DCS, Rest2); + true -> + UD = omit + end, + + %% TODO: further decode TP-Service-Centre-Time-Stamp + {ok, + {sm_tp_submit_report, #{ + sc_time_stamp => SCTS, + pid => PID, + dcs => DCS, + cause => Cause, + user_data => UD + }}}. + +decode(RPMsgType, <<_:6, MTI:2, _/bits>> = TPDU) -> + case {RPMsgType, MTI} of + %% Mobile originated message submission + {sm_rp_mo_data, ?TP_MTI_SUBMIT} -> + %% MS -> MSC/SGSN: RP-DATA + decode_submit(TPDU); + {sm_rp_mo_data, ?TP_MTI_COMMAND} -> + %% MS -> MSC/SGSN: RP-DATA + decode_command(TPDU); + {sm_rp_mo_ack, ?TP_MTI_SUBMIT_REPORT} -> + %% MSC/SGSN -> MS: RP-ACK + decode_submit_report_ack(TPDU); + {sm_rp_mo_error, ?TP_MTI_SUBMIT_REPORT} -> + %% MSC/SGSN -> MS: RP-ERROR + decode_submit_report_error(TPDU); + _ -> + {error, unknown_msg} + end. %% Encode TP-User-Data and TP-User-Data-Header(s) as per 9.2.3.24. %% NOTE: UserData must be encoded by the caller itself. encode_tp_ud(HdrList, UserData) -> - Hdrs = encode_tp_ud_hdrs(HdrList, << >>), - HdrsLen = byte_size(Hdrs), - - %% The maximum length of TP-User-Data (and optional headers) is 140 octets. - %% NOTE: byte_size() will round the size to octet boundary. - TotalLen = HdrsLen + byte_size(UserData), - if TotalLen > ?TP_USER_DATA_MAX_LEN -> - error({sm_tp_ud, overflow, TotalLen}); - true -> - %% TPDU must be octet-aligned - %% TODO: is there a more elegant way? - PadLen = byte_size(UserData) * 8 - bit_size(UserData), - UDEnc = << Hdrs/bytes, UserData/bits, ?TP_USER_DATA_PADDING:PadLen >>, - {HdrsLen, UDEnc} - end. + Hdrs = encode_tp_ud_hdrs(HdrList, <<>>), + HdrsLen = byte_size(Hdrs), + + %% The maximum length of TP-User-Data (and optional headers) is 140 octets. + %% NOTE: byte_size() will round the size to octet boundary. + TotalLen = HdrsLen + byte_size(UserData), + if + TotalLen > ?TP_USER_DATA_MAX_LEN -> + error({sm_tp_ud, overflow, TotalLen}); + true -> + %% TPDU must be octet-aligned + %% TODO: is there a more elegant way? + PadLen = byte_size(UserData) * 8 - bit_size(UserData), + UDEnc = <>, + {HdrsLen, UDEnc} + end. encode_tp_ud_hdrs([{Type, Value} | Tail], Hdrs) -> - Length = byte_size(Value), - HdrTLV = << Type:8, Length:8, Value/bytes >>, - encode_tp_ud_hdrs(Tail, << Hdrs/bytes, HdrTLV/bytes >> ); -encode_tp_ud_hdrs([], Hdrs) -> Hdrs. + Length = byte_size(Value), + HdrTLV = <>, + encode_tp_ud_hdrs(Tail, <>); +encode_tp_ud_hdrs([], Hdrs) -> + Hdrs. %% Calculate the number of septets or octets (depending on DCS) %% in both TP-User-Data and TP-User-Data-Header(s). calc_tp_udl(_DCS, HdrsLen, UserDataBitLen) -> - %% HACK! TODO: parse DCS as per 3GPP TS 23.038, section 4 - if (UserDataBitLen rem 8) > 0 -> - (HdrsLen * 8 div 7) + (UserDataBitLen div 7); - true -> - HdrsLen + UserDataBitLen div 8 - end. + %% HACK! TODO: parse DCS as per 3GPP TS 23.038, section 4 + if + (UserDataBitLen rem 8) > 0 -> + (HdrsLen * 8 div 7) + (UserDataBitLen div 7); + true -> + HdrsLen + UserDataBitLen div 8 + end. %% 23.040 9.2.2.1 "SMS-DELIVER type" encode_deliver(Params) -> - #{reply_path := RP, - status_report_ind := SRI, - loop_prevention := LP, - more_messages_to_send := MMS, - orig_addr := {OAType, OA}, - pid := PID, dcs := DCS, - date_time := DateTime, - timezone := TimeZone, - user_data := {HdrList, UserData}} = Params, - - %% TP-Originating-Address - OAEnc = encode_tp_addr(OAType, OA), - - %% TP-Service-Centre-Time-Stamp - SCTS = encode_tp_scts(DateTime, TimeZone), - - %% TP-User-Data and TP-User-Data-Header(s) - {HdrsLen, UDEnc} = encode_tp_ud(HdrList, UserData), - if HdrsLen > 0 -> - UDHI = 1; - true -> - UDHI = 0 - end, - - %% TP-User-Data-Length (depends on DCS) - UDLen = calc_tp_udl(DCS, HdrsLen, bit_size(UserData)), - - << RP:1, UDHI:1, SRI:1, 0:1, LP:1, MMS:1, ?TP_MTI_DELIVER:2, - OAEnc/bytes, PID, DCS, SCTS/bytes, UDLen, UDEnc/bytes >>. + #{ + reply_path := RP, + status_report_ind := SRI, + loop_prevention := LP, + more_messages_to_send := MMS, + orig_addr := {OAType, OA}, + pid := PID, + dcs := DCS, + date_time := DateTime, + timezone := TimeZone, + user_data := {HdrList, UserData} + } = Params, + + %% TP-Originating-Address + OAEnc = encode_tp_addr(OAType, OA), + + %% TP-Service-Centre-Time-Stamp + SCTS = encode_tp_scts(DateTime, TimeZone), + + %% TP-User-Data and TP-User-Data-Header(s) + {HdrsLen, UDEnc} = encode_tp_ud(HdrList, UserData), + if + HdrsLen > 0 -> + UDHI = 1; + true -> + UDHI = 0 + end, + + %% TP-User-Data-Length (depends on DCS) + UDLen = calc_tp_udl(DCS, HdrsLen, bit_size(UserData)), + + <>. encode(RPMsgType, {TPMsgType, Params}) -> - case {RPMsgType, TPMsgType} of - %% Mobile terminated message delivery - {sm_rp_mt_data, sm_tp_deliver} -> - %% MSC/SGSN -> MS: RP-DATA - encode_deliver(Params); - {sm_rp_mt_data, sm_tp_status_report} -> - %% MSC/SGSN -> MS: RP-DATA - %% encode_status_report(Params); - ok; - {sm_rp_mt_ack, sm_tp_deliver_report} -> - %% MS -> MSC/SGSN: RP-ACK - %% encode_deliver_report_ack(Params); - ok; - {sm_rp_mt_error, sm_tp_deliver_report} -> - %% MS -> MSC/SGSN: RP-ERROR - %% encode_deliver_report_error(Params); - ok; - _ -> - {error, unknown_msg} - end. + case {RPMsgType, TPMsgType} of + %% Mobile terminated message delivery + {sm_rp_mt_data, sm_tp_deliver} -> + %% MSC/SGSN -> MS: RP-DATA + encode_deliver(Params); + {sm_rp_mt_data, sm_tp_status_report} -> + %% MSC/SGSN -> MS: RP-DATA + %% encode_status_report(Params); + ok; + {sm_rp_mt_ack, sm_tp_deliver_report} -> + %% MS -> MSC/SGSN: RP-ACK + %% encode_deliver_report_ack(Params); + ok; + {sm_rp_mt_error, sm_tp_deliver_report} -> + %% MS -> MSC/SGSN: RP-ERROR + %% encode_deliver_report_error(Params); + ok; + _ -> + {error, unknown_msg} + end. diff --git a/test/bcd_codec_test.erl b/test/bcd_codec_test.erl index be71ed1..58f5885 100644 --- a/test/bcd_codec_test.erl +++ b/test/bcd_codec_test.erl @@ -38,67 +38,68 @@ -define(TEST_IMSI_STR, "901701234567890"). -define(TEST_IMSI_NUM, 901701234567890). --define(TEST_IMSI_ENC, << 16#09, 16#71, 16#10, 16#32, 16#54, 16#76, 16#98, 16#f0 >>). --define(TEST_IMSI_ENC_NOPAD, << 16#09, 16#71, 16#10, 16#32, 16#54, 16#76, 16#98, 16#0:4 >>). +-define(TEST_IMSI_ENC, <<16#09, 16#71, 16#10, 16#32, 16#54, 16#76, 16#98, 16#f0>>). +-define(TEST_IMSI_ENC_NOPAD, <<16#09, 16#71, 16#10, 16#32, 16#54, 16#76, 16#98, 16#0:4>>). -define(TEST_USSD_STR, "*#100#"). --define(TEST_USSD_ENC, << 16#ed, 16#01, 16#e0 >>). +-define(TEST_USSD_ENC, <<16#ed, 16#01, 16#e0>>). encode_test() -> - ?assertEqual(?TEST_IMSI_ENC, bcd_codec:encode(<< ?TEST_IMSI_STR >>)), - ?assertEqual(?TEST_IMSI_ENC, bcd_codec:encode(?TEST_IMSI_STR)), - ?assertEqual(?TEST_IMSI_ENC, bcd_codec:encode(?TEST_IMSI_NUM)), + ?assertEqual(?TEST_IMSI_ENC, bcd_codec:encode(<>)), + ?assertEqual(?TEST_IMSI_ENC, bcd_codec:encode(?TEST_IMSI_STR)), + ?assertEqual(?TEST_IMSI_ENC, bcd_codec:encode(?TEST_IMSI_NUM)), - ?assertEqual(?TEST_USSD_ENC, bcd_codec:encode(<< ?TEST_USSD_STR >>)), - ?assertEqual(?TEST_USSD_ENC, bcd_codec:encode(?TEST_USSD_STR)), + ?assertEqual(?TEST_USSD_ENC, bcd_codec:encode(<>)), + ?assertEqual(?TEST_USSD_ENC, bcd_codec:encode(?TEST_USSD_STR)), - ?assertEqual(<< 16#ba, 16#fc >>, bcd_codec:encode(<< "abc" >>)), - ?assertEqual(<< >>, bcd_codec:encode(<< >>)), - ?assertEqual(<< >>, bcd_codec:encode("")), + ?assertEqual(<<16#ba, 16#fc>>, bcd_codec:encode(<<"abc">>)), + ?assertEqual(<<>>, bcd_codec:encode(<<>>)), + ?assertEqual(<<>>, bcd_codec:encode("")), - ?assertError({bad_bcd_character, $M}, bcd_codec:encode(<< "Mahlzeit!" >>)), - ?assertError({bad_bcd_character, $f}, bcd_codec:encode("880043556f")), - ?assertError(_, bcd_codec:encode(nope)). % TODO: 'no matching function clause' + ?assertError({bad_bcd_character, $M}, bcd_codec:encode(<<"Mahlzeit!">>)), + ?assertError({bad_bcd_character, $f}, bcd_codec:encode("880043556f")), + % TODO: 'no matching function clause' + ?assertError(_, bcd_codec:encode(nope)). encode_nopad_test() -> - ?assertEqual(?TEST_IMSI_ENC_NOPAD, bcd_codec:encode_nopad(<< ?TEST_IMSI_STR >>)), - ?assertEqual(?TEST_USSD_ENC, bcd_codec:encode_nopad(<< ?TEST_USSD_STR >>)), - ?assertEqual(<< 16#ba, 16#c:4 >>, bcd_codec:encode_nopad(<< "abc" >>)). + ?assertEqual(?TEST_IMSI_ENC_NOPAD, bcd_codec:encode_nopad(<>)), + ?assertEqual(?TEST_USSD_ENC, bcd_codec:encode_nopad(<>)), + ?assertEqual(<<16#ba, 16#c:4>>, bcd_codec:encode_nopad(<<"abc">>)). decode_test() -> - ?assertEqual(?TEST_IMSI_STR, bcd_codec:decode(?TEST_IMSI_ENC)), - ?assertEqual(?TEST_USSD_STR, bcd_codec:decode(?TEST_USSD_ENC)), + ?assertEqual(?TEST_IMSI_STR, bcd_codec:decode(?TEST_IMSI_ENC)), + ?assertEqual(?TEST_USSD_STR, bcd_codec:decode(?TEST_USSD_ENC)), - ?assertEqual("abc", bcd_codec:decode(<< 16#ba, 16#fc >>)), - ?assertEqual("abc", bcd_codec:decode([16#ba, 16#fc])), - ?assertEqual("", bcd_codec:decode(<< >>)), + ?assertEqual("abc", bcd_codec:decode(<<16#ba, 16#fc>>)), + ?assertEqual("abc", bcd_codec:decode([16#ba, 16#fc])), + ?assertEqual("", bcd_codec:decode(<<>>)), - %% TODO: expect 'no matching function clause' here - ?assertError(_, bcd_codec:decode_num(<< ?TEST_IMSI_ENC/binary, 16#ff:3 >>)), - ?assertError(_, bcd_codec:decode_num(<< 16#ff:1, ?TEST_USSD_ENC/binary >>)), - ?assertError(_, bcd_codec:encode_num(nope)). + %% TODO: expect 'no matching function clause' here + ?assertError(_, bcd_codec:decode_num(<>)), + ?assertError(_, bcd_codec:decode_num(<<16#ff:1, ?TEST_USSD_ENC/binary>>)), + ?assertError(_, bcd_codec:encode_num(nope)). encode_num_test() -> - ?assertEqual(16#99, bcd_codec:encode_num(99)), - ?assertEqual(16#05, bcd_codec:encode_num(50)), - ?assertEqual(16#14, bcd_codec:encode_num(41)), - ?assertEqual(16#10, bcd_codec:encode_num(1)), - ?assertEqual(16#00, bcd_codec:encode_num(0)), + ?assertEqual(16#99, bcd_codec:encode_num(99)), + ?assertEqual(16#05, bcd_codec:encode_num(50)), + ?assertEqual(16#14, bcd_codec:encode_num(41)), + ?assertEqual(16#10, bcd_codec:encode_num(1)), + ?assertEqual(16#00, bcd_codec:encode_num(0)), - %% TODO: expect 'no matching function clause' here - ?assertError(_, bcd_codec:encode_num(999)), - ?assertError(_, bcd_codec:encode_num(100)), - ?assertError(_, bcd_codec:encode_num(-1)). + %% TODO: expect 'no matching function clause' here + ?assertError(_, bcd_codec:encode_num(999)), + ?assertError(_, bcd_codec:encode_num(100)), + ?assertError(_, bcd_codec:encode_num(-1)). decode_num_test() -> - ?assertEqual(99, bcd_codec:decode_num(16#99)), - ?assertEqual(50, bcd_codec:decode_num(16#05)), - ?assertEqual(41, bcd_codec:decode_num(16#14)), - ?assertEqual(1, bcd_codec:decode_num(16#10)), - ?assertEqual(0, bcd_codec:decode_num(16#00)), - - %% TODO: expect 'no matching function clause' here - ?assertError(_, bcd_codec:decode_num(16#666)), - ?assertError(_, bcd_codec:decode_num(16#a0)), - ?assertError(_, bcd_codec:decode_num(16#0a)), - ?assertError(_, bcd_codec:decode_num(-1)). + ?assertEqual(99, bcd_codec:decode_num(16#99)), + ?assertEqual(50, bcd_codec:decode_num(16#05)), + ?assertEqual(41, bcd_codec:decode_num(16#14)), + ?assertEqual(1, bcd_codec:decode_num(16#10)), + ?assertEqual(0, bcd_codec:decode_num(16#00)), + + %% TODO: expect 'no matching function clause' here + ?assertError(_, bcd_codec:decode_num(16#666)), + ?assertError(_, bcd_codec:decode_num(16#a0)), + ?assertError(_, bcd_codec:decode_num(16#0a)), + ?assertError(_, bcd_codec:decode_num(-1)). diff --git a/test/sm_tp_codec_test.erl b/test/sm_tp_codec_test.erl index 0bc54d2..d33bc3d 100644 --- a/test/sm_tp_codec_test.erl +++ b/test/sm_tp_codec_test.erl @@ -36,176 +36,279 @@ -include_lib("eunit/include/eunit.hrl"). %% Rest octets following TP-Address field --define(TEST_TP_DUMMY_REST, << 16#de, 16#ad, 16#be, 16#ef >>). +-define(TEST_TP_DUMMY_REST, <<16#de, 16#ad, 16#be, 16#ef>>). %% Alphanumeric address "MTC" (3 * 7 useful bits, 3 * 8 bits with padding) -define(TEST_TP_ADDR_ANUM_T, {02#0000, 02#101}). --define(TEST_TP_ADDR_ANUM_V, << 16#4d, 16#ea, 16#10 >>). +-define(TEST_TP_ADDR_ANUM_V, <<16#4d, 16#ea, 16#10>>). -define(TEST_TP_ADDR_ANUM_LV, {3 * 7, ?TEST_TP_ADDR_ANUM_V}). -define(TEST_TP_ADDR_ANUM_TLV, ?TEST_TP_ADDR_ANUM_T, ?TEST_TP_ADDR_ANUM_LV). --define(TEST_TP_ADDR_ANUM_ENC, << 16#06, 16#d0, ?TEST_TP_ADDR_ANUM_V/bytes >>). +-define(TEST_TP_ADDR_ANUM_ENC, <<16#06, 16#d0, ?TEST_TP_ADDR_ANUM_V/bytes>>). -define(TEST_TP_ADDR_ANUM_LV_DEC, {3 * 8, ?TEST_TP_ADDR_ANUM_V}). -define(TEST_TP_ADDR_ANUM_TLV_DEC, ?TEST_TP_ADDR_ANUM_T, ?TEST_TP_ADDR_ANUM_LV_DEC). %% BCD-encoded odd address "+79137081000" (11 * 4 useful bits, 12 * 4 bits with padding) -define(TEST_TP_ADDR_ONUM_T, {02#0001, 02#001}). --define(TEST_TP_ADDR_ONUM_V, << 16#97, 16#31, 16#07, 16#18, 16#00, 16#f0 >>). +-define(TEST_TP_ADDR_ONUM_V, <<16#97, 16#31, 16#07, 16#18, 16#00, 16#f0>>). -define(TEST_TP_ADDR_ONUM_LV, {11 * 4, ?TEST_TP_ADDR_ONUM_V}). -define(TEST_TP_ADDR_ONUM_TLV, ?TEST_TP_ADDR_ONUM_T, ?TEST_TP_ADDR_ONUM_LV). --define(TEST_TP_ADDR_ONUM_ENC, << 16#0b, 16#91, ?TEST_TP_ADDR_ONUM_V/bytes >>). +-define(TEST_TP_ADDR_ONUM_ENC, <<16#0b, 16#91, ?TEST_TP_ADDR_ONUM_V/bytes>>). %% BCD-encoded even address "123456" (6 * 4 useful bits, no padding) -define(TEST_TP_ADDR_ENUM_T, {02#1000, 02#000}). --define(TEST_TP_ADDR_ENUM_V, << 16#21, 16#43, 16#65 >>). +-define(TEST_TP_ADDR_ENUM_V, <<16#21, 16#43, 16#65>>). -define(TEST_TP_ADDR_ENUM_LV, {6 * 4, ?TEST_TP_ADDR_ENUM_V}). -define(TEST_TP_ADDR_ENUM_TLV, ?TEST_TP_ADDR_ENUM_T, ?TEST_TP_ADDR_ENUM_LV). --define(TEST_TP_ADDR_ENUM_ENC, << 16#06, 16#88, ?TEST_TP_ADDR_ENUM_V/bytes >>). +-define(TEST_TP_ADDR_ENUM_ENC, <<16#06, 16#88, ?TEST_TP_ADDR_ENUM_V/bytes>>). encode_tp_addr_test() -> - ?assertEqual(?TEST_TP_ADDR_ANUM_ENC, sm_tp_codec:encode_tp_addr(?TEST_TP_ADDR_ANUM_TLV)), - ?assertEqual(?TEST_TP_ADDR_ONUM_ENC, sm_tp_codec:encode_tp_addr(?TEST_TP_ADDR_ONUM_TLV)), - ?assertEqual(?TEST_TP_ADDR_ENUM_ENC, sm_tp_codec:encode_tp_addr(?TEST_TP_ADDR_ENUM_TLV)). + ?assertEqual(?TEST_TP_ADDR_ANUM_ENC, sm_tp_codec:encode_tp_addr(?TEST_TP_ADDR_ANUM_TLV)), + ?assertEqual(?TEST_TP_ADDR_ONUM_ENC, sm_tp_codec:encode_tp_addr(?TEST_TP_ADDR_ONUM_TLV)), + ?assertEqual(?TEST_TP_ADDR_ENUM_ENC, sm_tp_codec:encode_tp_addr(?TEST_TP_ADDR_ENUM_TLV)). decode_tp_addr_test() -> - %% Alphanumeric (GSM 7 bit encoding) address "MTC" - ?assertEqual({ok, ?TEST_TP_ADDR_ANUM_TLV_DEC, << >>}, - sm_tp_codec:decode_tp_addr(?TEST_TP_ADDR_ANUM_ENC)), - ?assertEqual({ok, ?TEST_TP_ADDR_ANUM_TLV_DEC, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_addr(<< ?TEST_TP_ADDR_ANUM_ENC/bytes, - ?TEST_TP_DUMMY_REST/bytes >>)), - - %% BCD-encoded odd address "+79137081000" - ?assertEqual({ok, ?TEST_TP_ADDR_ONUM_TLV, << >>}, - sm_tp_codec:decode_tp_addr(?TEST_TP_ADDR_ONUM_ENC)), - ?assertEqual({ok, ?TEST_TP_ADDR_ONUM_TLV, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_addr(<< ?TEST_TP_ADDR_ONUM_ENC/bytes, - ?TEST_TP_DUMMY_REST/bytes >>)), - - %% BCD-encoded even address "123456" - ?assertEqual({ok, ?TEST_TP_ADDR_ENUM_TLV, << >>}, - sm_tp_codec:decode_tp_addr(?TEST_TP_ADDR_ENUM_ENC)), - ?assertEqual({ok, ?TEST_TP_ADDR_ENUM_TLV, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_addr(<< ?TEST_TP_ADDR_ENUM_ENC/bytes, - ?TEST_TP_DUMMY_REST/bytes >>)). - --define(TEST_TP_SCTS_TIME0, {{00, 00, 00}, {00, 00, 00}}). % Intentionally incorrect --define(TEST_TP_SCTS_TIME1, {{70, 04, 22}, {11, 10, 01}}). % Lenin's birthday! --define(TEST_TP_SCTS_TIME2, {{69, 12, 01}, {22, 45, 33}}). % Just a random date - --define(TEST_TP_SCTS_TIME0_ENC, << 16#00, 16#00, 16#00, 16#00, 16#00, 16#00 >>). --define(TEST_TP_SCTS_TIME1_ENC, << 16#07, 16#40, 16#22, 16#11, 16#01, 16#10 >>). --define(TEST_TP_SCTS_TIME2_ENC, << 16#96, 16#21, 16#10, 16#22, 16#54, 16#33 >>). - --define(TEST_TP_SCTS_TZ_00_ENC, 16#00). % GMT+0 --define(TEST_TP_SCTS_TZ_40_ENC, 16#61). % GMT+4 --define(TEST_TP_SCTS_TZ_F715_ENC, 16#9a). % GMT-7,15 --define(TEST_TP_SCTS_TZ_F30_ENC, 16#29). % GMT-3 --define(TEST_TP_SCTS_TZ_1230_ENC, 16#05). % GMT+12,30 + %% Alphanumeric (GSM 7 bit encoding) address "MTC" + ?assertEqual( + {ok, ?TEST_TP_ADDR_ANUM_TLV_DEC, <<>>}, + sm_tp_codec:decode_tp_addr(?TEST_TP_ADDR_ANUM_ENC) + ), + ?assertEqual( + {ok, ?TEST_TP_ADDR_ANUM_TLV_DEC, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_addr(<>) + ), + + %% BCD-encoded odd address "+79137081000" + ?assertEqual( + {ok, ?TEST_TP_ADDR_ONUM_TLV, <<>>}, + sm_tp_codec:decode_tp_addr(?TEST_TP_ADDR_ONUM_ENC) + ), + ?assertEqual( + {ok, ?TEST_TP_ADDR_ONUM_TLV, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_addr(<>) + ), + + %% BCD-encoded even address "123456" + ?assertEqual( + {ok, ?TEST_TP_ADDR_ENUM_TLV, <<>>}, + sm_tp_codec:decode_tp_addr(?TEST_TP_ADDR_ENUM_ENC) + ), + ?assertEqual( + {ok, ?TEST_TP_ADDR_ENUM_TLV, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_addr(<>) + ). + +% Intentionally incorrect +-define(TEST_TP_SCTS_TIME0, {{00, 00, 00}, {00, 00, 00}}). +% Lenin's birthday! +-define(TEST_TP_SCTS_TIME1, {{70, 04, 22}, {11, 10, 01}}). +% Just a random date +-define(TEST_TP_SCTS_TIME2, {{69, 12, 01}, {22, 45, 33}}). + +-define(TEST_TP_SCTS_TIME0_ENC, <<16#00, 16#00, 16#00, 16#00, 16#00, 16#00>>). +-define(TEST_TP_SCTS_TIME1_ENC, <<16#07, 16#40, 16#22, 16#11, 16#01, 16#10>>). +-define(TEST_TP_SCTS_TIME2_ENC, <<16#96, 16#21, 16#10, 16#22, 16#54, 16#33>>). + +% GMT+0 +-define(TEST_TP_SCTS_TZ_00_ENC, 16#00). +% GMT+4 +-define(TEST_TP_SCTS_TZ_40_ENC, 16#61). +% GMT-7,15 +-define(TEST_TP_SCTS_TZ_F715_ENC, 16#9a). +% GMT-3 +-define(TEST_TP_SCTS_TZ_F30_ENC, 16#29). +% GMT+12,30 +-define(TEST_TP_SCTS_TZ_1230_ENC, 16#05). encode_tp_scts_test() -> - ?assertEqual(<< ?TEST_TP_SCTS_TIME0_ENC:6/bytes, ?TEST_TP_SCTS_TZ_00_ENC >>, - sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME0, {0, 0})), - ?assertEqual(<< ?TEST_TP_SCTS_TIME1_ENC:6/bytes, ?TEST_TP_SCTS_TZ_40_ENC >>, - sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME1, {4, 0})), - ?assertEqual(<< ?TEST_TP_SCTS_TIME2_ENC:6/bytes, ?TEST_TP_SCTS_TZ_F715_ENC >>, - sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME2, {-7, 15})), - ?assertEqual(<< ?TEST_TP_SCTS_TIME1_ENC:6/bytes, ?TEST_TP_SCTS_TZ_F30_ENC >>, - sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME1, {-3, 0})), - ?assertEqual(<< ?TEST_TP_SCTS_TIME2_ENC:6/bytes, ?TEST_TP_SCTS_TZ_1230_ENC >>, - sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME2, {12, 30})), - - ?assertError(_, % TODO: {error, function_clause, ...} due to YY > 99 and HH > 99 - sm_tp_codec:encode_tp_scts({{1999, 01, 01}, {120, 00, 00}}, {0, 0})), - ?assertError(_, % TODO: {error, function_clause, ...} due to HH < 0 - sm_tp_codec:encode_tp_scts({{89, 02, 04}, {-5, 00, 00}}, {7, 0})), - ?assertError(_, % TODO: {error, function_clause, ...} due to (TZMin rem 15) =/= 0 - sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME1, {5, 5})). + ?assertEqual( + <>, + sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME0, {0, 0}) + ), + ?assertEqual( + <>, + sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME1, {4, 0}) + ), + ?assertEqual( + <>, + sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME2, {-7, 15}) + ), + ?assertEqual( + <>, + sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME1, {-3, 0}) + ), + ?assertEqual( + <>, + sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME2, {12, 30}) + ), + + % TODO: {error, function_clause, ...} due to YY > 99 and HH > 99 + ?assertError( + _, + sm_tp_codec:encode_tp_scts({{1999, 01, 01}, {120, 00, 00}}, {0, 0}) + ), + % TODO: {error, function_clause, ...} due to HH < 0 + ?assertError( + _, + sm_tp_codec:encode_tp_scts({{89, 02, 04}, {-5, 00, 00}}, {7, 0}) + ), + % TODO: {error, function_clause, ...} due to (TZMin rem 15) =/= 0 + ?assertError( + _, + sm_tp_codec:encode_tp_scts(?TEST_TP_SCTS_TIME1, {5, 5}) + ). decode_tp_scts_test() -> - ?assertEqual({ok, ?TEST_TP_SCTS_TIME0, {0, 0}}, - sm_tp_codec:decode_tp_scts(<< ?TEST_TP_SCTS_TIME0_ENC:6/bytes, - ?TEST_TP_SCTS_TZ_00_ENC >>)), - ?assertEqual({ok, ?TEST_TP_SCTS_TIME1, {4, 0}}, - sm_tp_codec:decode_tp_scts(<< ?TEST_TP_SCTS_TIME1_ENC:6/bytes, - ?TEST_TP_SCTS_TZ_40_ENC >>)), - ?assertEqual({ok, ?TEST_TP_SCTS_TIME2, {-7, 15}}, - sm_tp_codec:decode_tp_scts(<< ?TEST_TP_SCTS_TIME2_ENC:6/bytes, - ?TEST_TP_SCTS_TZ_F715_ENC >>)), - ?assertEqual({ok, ?TEST_TP_SCTS_TIME1, {-3, 0}}, - sm_tp_codec:decode_tp_scts(<< ?TEST_TP_SCTS_TIME1_ENC:6/bytes, - ?TEST_TP_SCTS_TZ_F30_ENC >>)), - ?assertEqual({ok, ?TEST_TP_SCTS_TIME2, {12, 30}}, - sm_tp_codec:decode_tp_scts(<< ?TEST_TP_SCTS_TIME2_ENC:6/bytes, - ?TEST_TP_SCTS_TZ_1230_ENC >>)). + ?assertEqual( + {ok, ?TEST_TP_SCTS_TIME0, {0, 0}}, + sm_tp_codec:decode_tp_scts(<>) + ), + ?assertEqual( + {ok, ?TEST_TP_SCTS_TIME1, {4, 0}}, + sm_tp_codec:decode_tp_scts(<>) + ), + ?assertEqual( + {ok, ?TEST_TP_SCTS_TIME2, {-7, 15}}, + sm_tp_codec:decode_tp_scts(<>) + ), + ?assertEqual( + {ok, ?TEST_TP_SCTS_TIME1, {-3, 0}}, + sm_tp_codec:decode_tp_scts(<>) + ), + ?assertEqual( + {ok, ?TEST_TP_SCTS_TIME2, {12, 30}}, + sm_tp_codec:decode_tp_scts(<>) + ). %% TP-Validity-Period is absent. --define(TEST_TP_VP_OMIT, 02#00, ?TEST_TP_DUMMY_REST). +-define(TEST_TP_VP_OMIT, 02#00, ?TEST_TP_DUMMY_REST). %% 9.2.3.12.1 TP-Validity-Period (Relative format). --define(TEST_TP_VP_RELATIVE0, 02#10, << 16#00, ?TEST_TP_DUMMY_REST/bytes >>). % 5 minutes --define(TEST_TP_VP_RELATIVE1, 02#10, << 16#8f, ?TEST_TP_DUMMY_REST/bytes >>). % 12 hours --define(TEST_TP_VP_RELATIVE2, 02#10, << 16#a7, ?TEST_TP_DUMMY_REST/bytes >>). % 24 hours --define(TEST_TP_VP_RELATIVE3, 02#10, << 16#a8, ?TEST_TP_DUMMY_REST/bytes >>). % 2 days --define(TEST_TP_VP_RELATIVE4, 02#10, << 16#ff, ?TEST_TP_DUMMY_REST/bytes >>). % 63 weeks + +% 5 minutes +-define(TEST_TP_VP_RELATIVE0, 02#10, <<16#00, ?TEST_TP_DUMMY_REST/bytes>>). +% 12 hours +-define(TEST_TP_VP_RELATIVE1, 02#10, <<16#8f, ?TEST_TP_DUMMY_REST/bytes>>). +% 24 hours +-define(TEST_TP_VP_RELATIVE2, 02#10, <<16#a7, ?TEST_TP_DUMMY_REST/bytes>>). +% 2 days +-define(TEST_TP_VP_RELATIVE3, 02#10, <<16#a8, ?TEST_TP_DUMMY_REST/bytes>>). +% 63 weeks +-define(TEST_TP_VP_RELATIVE4, 02#10, <<16#ff, ?TEST_TP_DUMMY_REST/bytes>>). %% 9.2.3.12.2 TP-Validity-Period (Absolute format). --define(TEST_TP_VP_ABSOLUTE1, 02#11, << ?TEST_TP_SCTS_TIME1_ENC:6/bytes, - ?TEST_TP_SCTS_TZ_40_ENC >>). --define(TEST_TP_VP_ABSOLUTE2, 02#11, << ?TEST_TP_SCTS_TIME2_ENC:6/bytes, - ?TEST_TP_SCTS_TZ_F30_ENC >>). +-define(TEST_TP_VP_ABSOLUTE1, 02#11, <>). +-define(TEST_TP_VP_ABSOLUTE2, 02#11, <>). %% 9.2.3.12.3 TP-Validity-Period (Enhanced format). --define(TEST_TP_VP_ENHANCED0, 02#01, << 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, - ?TEST_TP_DUMMY_REST/bytes >>). % not present --define(TEST_TP_VP_ENHANCED1, 02#01, << 16#41, 16#01, 16#00, 16#00, 16#00, 16#00, 16#00, - ?TEST_TP_DUMMY_REST/bytes >>). % relative, 10 minutes --define(TEST_TP_VP_ENHANCED2, 02#01, << 16#42, 16#1e, 16#00, 16#00, 16#00, 16#00, 16#00, - ?TEST_TP_DUMMY_REST/bytes >>). % relative, 30 seconds --define(TEST_TP_VP_ENHANCED3, 02#01, << 16#03, 16#00, 16#22, 16#71, 16#00, 16#00, 16#00, - ?TEST_TP_DUMMY_REST/bytes >>). % semi-octet, 00:22:17 - --define(TEST_TP_VP_ENHANCED4, 02#01, << 16#47, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, - ?TEST_TP_DUMMY_REST/bytes >>). % reserved EVPF --define(TEST_TP_VP_ENHANCED5, 02#01, << 16#80, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, - ?TEST_TP_DUMMY_REST/bytes >>). % extension bit=1 - --define(TEST_TP_VPF_UNKNOWN, 16#ff, << ?TEST_TP_DUMMY_REST/bytes >>). +-define(TEST_TP_VP_ENHANCED0, + 02#01, + <<16#00, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, + % not present + ?TEST_TP_DUMMY_REST/bytes>> +). +-define(TEST_TP_VP_ENHANCED1, + 02#01, + <<16#41, 16#01, 16#00, 16#00, 16#00, 16#00, 16#00, + % relative, 10 minutes + ?TEST_TP_DUMMY_REST/bytes>> +). +-define(TEST_TP_VP_ENHANCED2, + 02#01, + <<16#42, 16#1e, 16#00, 16#00, 16#00, 16#00, 16#00, + % relative, 30 seconds + ?TEST_TP_DUMMY_REST/bytes>> +). +-define(TEST_TP_VP_ENHANCED3, + 02#01, + <<16#03, 16#00, 16#22, 16#71, 16#00, 16#00, 16#00, + % semi-octet, 00:22:17 + ?TEST_TP_DUMMY_REST/bytes>> +). + +-define(TEST_TP_VP_ENHANCED4, + 02#01, + <<16#47, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, + % reserved EVPF + ?TEST_TP_DUMMY_REST/bytes>> +). +-define(TEST_TP_VP_ENHANCED5, + 02#01, + <<16#80, 16#00, 16#00, 16#00, 16#00, 16#00, 16#00, + % extension bit=1 + ?TEST_TP_DUMMY_REST/bytes>> +). + +-define(TEST_TP_VPF_UNKNOWN, 16#ff, <>). decode_tp_vp_test() -> - ?assertEqual({ok, omit, << >>}, - sm_tp_codec:decode_tp_vp(02#00, << >>)), - ?assertEqual({ok, omit, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_OMIT)), - - ?assertEqual({ok, {relative, 5 * 60}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE0)), - ?assertEqual({ok, {relative, 12 * 3600}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE1)), - ?assertEqual({ok, {relative, 24 * 3600}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE2)), - ?assertEqual({ok, {relative, 2 * 24 * 3600}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE3)), - ?assertEqual({ok, {relative, 63 * 7 * 24 * 3600}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE4)), - - ?assertEqual({ok, {absolute, ?TEST_TP_SCTS_TIME1, {4, 0}}, << >>}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ABSOLUTE1)), - ?assertEqual({ok, {absolute, ?TEST_TP_SCTS_TIME2, {-3, 0}}, << >>}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ABSOLUTE2)), - - ?assertEqual({ok, {enhanced, 0, omit}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED0)), - ?assertEqual({ok, {enhanced, 1, {relative, 600}}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED1)), - ?assertEqual({ok, {enhanced, 1, {relative, 30}}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED2)), - ?assertEqual({ok, {enhanced, 0, {relative, 1337}}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED3)), - - ?assertEqual({ok, {enhanced, 1, reserved}, ?TEST_TP_DUMMY_REST}, - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED4)), - ?assertError({vpf_ext_not_supported}, % extension bit=1 not supported - sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED5)), - ?assertError({unknown_vpf, 16#ff}, % unknown TP-Validity-Period-Format - sm_tp_codec:decode_tp_vp(?TEST_TP_VPF_UNKNOWN)). + ?assertEqual( + {ok, omit, <<>>}, + sm_tp_codec:decode_tp_vp(02#00, <<>>) + ), + ?assertEqual( + {ok, omit, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_OMIT) + ), + + ?assertEqual( + {ok, {relative, 5 * 60}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE0) + ), + ?assertEqual( + {ok, {relative, 12 * 3600}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE1) + ), + ?assertEqual( + {ok, {relative, 24 * 3600}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE2) + ), + ?assertEqual( + {ok, {relative, 2 * 24 * 3600}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE3) + ), + ?assertEqual( + {ok, {relative, 63 * 7 * 24 * 3600}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_RELATIVE4) + ), + + ?assertEqual( + {ok, {absolute, ?TEST_TP_SCTS_TIME1, {4, 0}}, <<>>}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ABSOLUTE1) + ), + ?assertEqual( + {ok, {absolute, ?TEST_TP_SCTS_TIME2, {-3, 0}}, <<>>}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ABSOLUTE2) + ), + + ?assertEqual( + {ok, {enhanced, 0, omit}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED0) + ), + ?assertEqual( + {ok, {enhanced, 1, {relative, 600}}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED1) + ), + ?assertEqual( + {ok, {enhanced, 1, {relative, 30}}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED2) + ), + ?assertEqual( + {ok, {enhanced, 0, {relative, 1337}}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED3) + ), + + ?assertEqual( + {ok, {enhanced, 1, reserved}, ?TEST_TP_DUMMY_REST}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED4) + ), + % extension bit=1 not supported + ?assertError( + {vpf_ext_not_supported}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VP_ENHANCED5) + ), + % unknown TP-Validity-Period-Format + ?assertError( + {unknown_vpf, 16#ff}, + sm_tp_codec:decode_tp_vp(?TEST_TP_VPF_UNKNOWN) + ). -- cgit v1.2.3