diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bcd_codec.erl | 88 | ||||
| -rw-r--r-- | src/osmo_gsup_if.erl | 246 | ||||
| -rw-r--r-- | src/osmo_smsc.app.src | 20 | ||||
| -rw-r--r-- | src/osmo_smsc_app.erl | 4 | ||||
| -rw-r--r-- | src/osmo_smsc_core.erl | 213 | ||||
| -rw-r--r-- | src/sm_rp_trans.erl | 128 | ||||
| -rw-r--r-- | src/sm_tp_codec.erl | 625 |
7 files changed, 713 insertions, 611 deletions
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 + <<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. 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, <<BCDNum/bytes, N21>>); encode([D1], BCDNum) -> - N1 = bcd_nibble(D1), - {odd, << BCDNum/bytes >>, N1}; -encode([], BCDNum) -> {even, BCDNum}. + N1 = bcd_nibble(D1), + {odd, <<BCDNum/bytes>>, 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(<<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). %% 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. + <<N2:4, N1:4>> = <<Num>>, + 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 >>. + <<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, + % Convert number of digits to number of bits + DALen = (DANum div 2) + (DANum rem 2), + <<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>>. %% ------------------------------------------------------------------ %% 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. + <<AddrQBitLen, 1:1, ToN:3, NPI:4, Addr/bytes>>. %% 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. + <<QBitLen, 1:1, ToN:3, NPI:4, AddrRest/bytes>> = 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, + <<Addr:AddrByteLen/bytes, Rest/bytes>> = 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 = <<N2:4, Minus:1, N1:3>>, - << DateTime/bytes, TZ/bytes >>. + <<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(<<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}}. %% 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, <<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}; %% 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, <<VP:7/bytes, Rest/bytes>>) -> + {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 -> + <<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-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 + <<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 + }}}. %% 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 + <<CDLen, CommandData:CDLen/bytes>> = 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) + <<PID:PIDPres/unit:8, DCS:DCSPres/unit:8, Rest2/bytes>> = 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) + <<PID:PIDPres/unit:8, DCS:DCSPres/unit:8, Rest2/bytes>> = 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 = <<Hdrs/bytes, UserData/bits, ?TP_USER_DATA_PADDING:PadLen>>, + {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 = <<Type:8, Length:8, Value/bytes>>, + encode_tp_ud_hdrs(Tail, <<Hdrs/bytes, HdrTLV/bytes>>); +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)), + + <<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>>. 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. |
