diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/osmo_smsc_core.erl | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/src/osmo_smsc_core.erl b/src/osmo_smsc_core.erl new file mode 100644 index 0000000..ecc47f0 --- /dev/null +++ b/src/osmo_smsc_core.erl @@ -0,0 +1,176 @@ +% Core of the SMSC. +% +% (C) 2019 by Vadim Yanitskiy <axilirator@gmail.com> +% +% All Rights Reserved +% +% This program is free software; you can redistribute it and/or modify +% it under the terms of the GNU Affero General Public License as +% published by the Free Software Foundation; either version 3 of the +% License, or (at your option) any later version. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU Affero General Public License +% along with this program. If not, see <http://www.gnu.org/licenses/>. +% +% Additional Permission under GNU AGPL version 3 section 7: +% +% If you modify this Program, or any covered work, by linking or +% combining it with runtime libraries of Erlang/OTP as released by +% Ericsson on http://www.erlang.org (or a modified version of these +% libraries), containing parts covered by the terms of the Erlang Public +% License (http://www.erlang.org/EPLICENSE), the licensors of this +% Program grant you additional permission to convey the resulting work +% without the need to license the runtime libraries of Erlang/OTP under +% the GNU Affero General Public License. Corresponding Source for a +% non-source form of such a combination shall include the source code +% for the parts of the runtime libraries of Erlang/OTP used as well as +% that of the covered work. + +-module(osmo_smsc_core). +-behaviour(gen_server). + +-export([start/0, start_link/0]). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2]). +-export([code_change/3, terminate/2]). + +-define(GSM411_TP_MTI_DELIVER, 16#00). +-define(GSM411_TP_MTI_SUBMIT, 16#01). + +-record(smr_data, {imsi, msg_ref, orig_addr, dest_addr, tpdu}). +-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 >>. + +%% ------------------------------------------------------------------ +%% our exported API +%% ------------------------------------------------------------------ + +start() -> + gen_server:start(?MODULE, [], [{debug, [trace]}]). + +start_link() -> + 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 = #{}}}. + +% 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}}; + +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}; + +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}; + +% 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}; + +handle_cast({smr_ev_mt_fwd_sm_ack, IMSI, MR}, State) -> + {noreply, State}; + +handle_cast({smr_ev_mt_fwd_sm_err, IMSI, MR, Cause}, State) -> + {noreply, State}; + +handle_cast(Message, 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}. + +handle_info(Info, 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]). + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. |
