summaryrefslogtreecommitdiffstats
path: root/src/bssmap_codec.erl
blob: 530c62e1d342720311f1d934c970212e741a44c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
% GSM TS 08.08 / 3GPP TS 48.008 BSSMAP

% (C) 2010 by Harald Welte <laforge@gnumonks.org>
%
% 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/>.

-module(bssmap_codec).
-author('Harald Welte <laforge@gnumonks.org>').
-include("bssmap.hrl").

-export([parse_bssmap_msg/1, encode_bssmap_msg/1]).

parse_bssmap_msg(<<MsgType:8, Remain/binary>>) ->
	parse_bssmap_msgt(MsgType, Remain).

parse_bssmap_msgt(MsgType, Msg) when is_integer(MsgType), is_binary(Msg) ->
	IeList = parse_ies(Msg, []),
	{bssmap_msg, MsgType, IeList}.

parse_ies(<<>>, ParsedIeList) ->
	ParsedIeList;
parse_ies(Msg, ParsedIeList) when is_binary(Msg) ->
	CurIe = binary:first(Msg),
	% Parse current IE and append it to list of Parsed IEs
	case is_tv_ie(CurIe) of
		true ->
			Res = parse_ie_tv(CurIe, Msg);
		false ->
			Res = parse_ie(CurIe, Msg)
	end,
	{ok, BytesConsumed, ParsedIe} = Res,
	{CurIe, Payload} = ParsedIe,
	DecodedIe = decode_ie(CurIe, Payload),
	ParsedIeList1 = ParsedIeList ++ [DecodedIe],
	%ParsedIeList1 = ParsedIeList ++ [ParsedIe],
	RemainMsg = binary:part(Msg, BytesConsumed, byte_size(Msg)-BytesConsumed),
	parse_ies(RemainMsg, ParsedIeList1).

% check if this element is of TV type
is_tv_ie(T) when 
	T == ?BSSMAP_IE_NUMBER_OF_MSS;
	T == ?BSSMAP_IE_PERIODICITY;
	T == ?BSSMAP_IE_EXTD_RES_IND;
	T == ?BSSMAP_IE_INTERF_BAND_TO_USE;
	T == ?BSSMAP_IE_RR_CAUSE;
	T == ?BSSMAP_IE_DLCI;
	T == ?BSSMAP_IE_DOWNLINK_DTX_FLAG;
	T == ?BSSMAP_IE_RESPONSE_RQST;
	T == ?BSSMAP_IE_RES_IND_METHOD;
	T == ?BSSMAP_IE_CM_INFO_T1;
	T == ?BSSMAP_IE_CHOSEN_CHANNEL;
	T == ?BSSMAP_IE_CIPH_RESP_MODE;
	T == ?BSSMAP_IE_TRACE_TYPE;
	T == ?BSSMAP_IE_TRACE_REFERENCE;
	T == ?BSSMAP_IE_FORWARD_INDICATOR;
	T == ?BSSMAP_IE_CHOSEN_ENCR_ALG;
	T == ?BSSMAP_IE_CIRCUIT_POOL;
	T == ?BSSMAP_IE_TIME_INDICATION;
	T == ?BSSMAP_IE_CUR_CHAN_TYPE_1;
	T == ?BSSMAP_IE_QUEUEING_IND;
	T == ?BSSMAP_IE_SPEECH_VERSION;
	T == ?BSSMAP_IE_ASS_REQUIREMENT;
	T == ?BSSMAP_IE_EMLPP_PRIORITY;
	T == ?BSSMAP_IE_CONFIG_EVO_INDI;
	T == ?BSSMAP_IE_LSA_ACCESS_CTRL_SUPPR ->
		true;
is_tv_ie(_T) ->
		false.

% Parser for any non-TLV and non-TV IEs
parse_ie(?BSSMAP_IE_CIRC_ID_CODE, Msg) ->
	<<?BSSMAP_IE_CIRC_ID_CODE:8, Cic:16/big>> = Msg,
	{ok, 3, {?BSSMAP_IE_CIRC_ID_CODE, Cic}};
parse_ie(?BSSMAP_IE_CONN_REL_RQSTED, Msg) ->
	<<?BSSMAP_IE_CONN_REL_RQSTED:8>> = Msg,
	{ok, 1, {?BSSMAP_IE_CONN_REL_RQSTED, 1}};
parse_ie(?BSSMAP_IE_RES_AVAIL, Msg) ->
	<<?BSSMAP_IE_RES_AVAIL:8, ResAvail:8/binary>> = Msg,
	{ok, 9, {?BSSMAP_IE_RES_AVAIL, ResAvail}};
parse_ie(?BSSMAP_IE_TOT_RES_ACCESS, Msg) ->
	<<?BSSMAP_IE_TOT_RES_ACCESS:8, ResAvail:4/binary>> = Msg,
	{ok, 5, {?BSSMAP_IE_TOT_RES_ACCESS, ResAvail}};
parse_ie(?BSSMAP_IE_TALKER_FLAG, Msg) ->
	<<?BSSMAP_IE_TALKER_FLAG:8>> = Msg,
	{ok, 1, {?BSSMAP_IE_TALKER_FLAG, 1}};
% Default: Parser for TLV IE
parse_ie(MsgType, Msg) ->
	<<MsgType:8, Length:8, Value:Length/binary, _/binary>> = Msg,
	{ok, 2+Length, {MsgType, Value}}.

% Parser for simple Tag-Value IE
parse_ie_tv(IeType, Msg) ->
	<<IeType:8, Par:8>> = Msg,
	{ok, 2, {IeType, Par}}.


% FIXME
encode_bssmap_msg(_) ->
	ok.





decode_ie(?BSSMAP_IE_CIRC_ID_CODE, <<Pcm:11, Ts:5>>) ->
	{circuit_id, Pcm, Ts};
decode_ie(?BSSMAP_IE_IMSI, Remain) ->
	{imsi, bin_bcd2str(Remain)};
decode_ie(?BSSMAP_IE_TMSI, <<Tmsi:32>>) ->
	{tmsi, Tmsi};
decode_ie(?BSSMAP_IE_L3_HDR_INFO, <<Pdisc:8, Tid:8>>) ->
	{l3_hdr_info, Pdisc, Tid};
decode_ie(?BSSMAP_IE_ENCR_INFO, <<Algos:8, Key/binary>>) ->
	{encr_info, Algos, Key};
decode_ie(?BSSMAP_IE_CHANNEL_TYPE, <<_:4, Spdi:4, RateType:8, Remain/binary>>) ->
	{chan_type, Spdi, RateType, Remain};
decode_ie(?BSSMAP_IE_EXTD_RES_IND, Ri) ->
	<<_:6, Sm:1, Tarr:1>> = <<Ri>>,
	{extended_ri, Sm, Tarr};
decode_ie(?BSSMAP_IE_TOT_RES_ACCESS, <<NumFr:16/big, NumHr:16/big>>) ->
	{tot_res_access, NumFr, NumHr};
decode_ie(?BSSMAP_IE_CELL_ID, <<_Spare:4, Discr:4, Remain/binary>>) ->
	{cell_id, decode_cid_ie(Discr, Remain)};
decode_ie(?BSSMAP_IE_PRIORITY, <<_:1, Pci:1, Prio:4, Qa:1, Pvi:1>>) ->
	{priority, Pci, Prio, Qa, Pvi};
decode_ie(?BSSMAP_IE_CELL_ID_LIST, <<_Spare:4, Discr:4, Remain/binary>>) ->
	{cell_id_list, decode_cid_list(Discr, Remain, [])};
decode_ie(?BSSMAP_IE_DIAGNOSTIC, <<ErrPtr:8, _:4, BitPtr:4, MsgRecv/binary>>) ->
	{diagnostic, ErrPtr, BitPtr, MsgRecv};
decode_ie(?BSSMAP_IE_CHOSEN_CHANNEL, Int) ->
	<<Mode:4, Chan:4>> = <<Int:8>>,
	{chosen_channel, Mode, Chan};
decode_ie(?BSSMAP_IE_MOBILE_IDENTITY, Data) ->
	% FIXME
	fixme;
% Default: don't decode
decode_ie(IeI, Data) ->
	{IeI, Data}.

decode_cid_ie(?CELL_ID_WHOLE_GLOBAL, Remain) ->
	<<Mcc2:4, Mcc1:4, Mnc3:4, Mcc3:4, Mnc2:4, Mnc1:4, Lac:16/big, Ci:16/big>> = Remain,
	[{mcc, [Mcc1, Mcc2, Mcc3]}, {mnc, [Mnc1, Mnc2, Mnc3]}, {lac, Lac}, {cid, Ci}];
decode_cid_ie(?CELL_ID_LAC_AND_CI, Remain) ->
	<<Lac:16/big, Ci:16/big>> = Remain,
	[{lac, Lac}, {cid, Ci}];
decode_cid_ie(?CELL_ID_CI, Remain) ->
	<<Ci:16/big>> = Remain,
	[{cid, Ci}];
decode_cid_ie(?CELL_ID_NO_CELL, _Remain) ->
	[];
decode_cid_ie(?CELL_ID_UTRAN_PLMN_LAC_RNC, Remain) ->
	<<Mcc2:4, Mcc1:4, Mnc3:4, Mcc3:4, Mnc2:4, Mnc1:4, Lac:16/big, Rnc:16/big>> = Remain,
	[{mcc, [Mcc1, Mcc2, Mcc3]}, {mnc, [Mnc1, Mnc2, Mnc3]}, {lac, Lac}, {rnc_id, Rnc}];
decode_cid_ie(?CELL_ID_UTRAN_RNC, Remain) ->
	<<Rnc:16/big>> = Remain,
	[{rnc_id, Rnc}];
decode_cid_ie(?CELL_ID_UTRAN_LAC_RNC, Remain) ->
	<<Lac:16/big, Rnc:16/big>> = Remain,
	[{lac, Lac}, {rnc_id, Rnc}].

decode_cid_list(Discr, Data, List) ->
	case Discr of
		?CELL_ID_WHOLE_GLOBAL -> Len = 7;
		?CELL_ID_LAC_AND_CI ->	 Len = 4;
		?CELL_ID_CI ->		 Len = 2;
		?CELL_ID_NO_CELL ->	 Len = 0;
		?CELL_ID_UTRAN_PLMN_LAC_RNC -> Len = 7;
		?CELL_ID_UTRAN_RNC ->	Len = 2;
		?CELL_ID_UTRAN_LAC_RNC -> Len = 4
	end,
	<<Subset:Len/binary, Remain/binary>> = Data,
	Elem = {cell_id, decode_cid_ie(Discr, Subset)},
	decode_cid_list(Discr, Remain, List ++ [Elem]).



bin_bcd2str(BcdBin) when is_binary(BcdBin) ->
	bin_bcd2str(BcdBin, []).
bin_bcd2str(<<>>, List) ->
	List;
bin_bcd2str(BcdBin, List) ->
	<<Nibble:4, Remain/bitstring>> = BcdBin,
	Char = $0 + Nibble,
	bin_bcd2str(Remain, List ++ [Char]).