Skip to content

Commit

Permalink
convert all GTP flag fields from lists to maps
Browse files Browse the repository at this point in the history
  • Loading branch information
RoadRunnr committed Dec 1, 2022
1 parent b714ac8 commit bbe98aa
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 99 deletions.
28 changes: 14 additions & 14 deletions include/gtp_packet.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@

-record(common_flags, {
instance = 0,
flags = []
flags = #{}
}).

-record(apn_restriction, {
Expand Down Expand Up @@ -693,7 +693,7 @@

-record(extended_common_flags, {
instance = 0,
flags = []
flags = #{}
}).

-record(user_csg_information, {
Expand Down Expand Up @@ -959,7 +959,7 @@

-record(v2_indication, {
instance = 0,
flags = []
flags = #{}
}).

-record(v2_protocol_configuration_options, {
Expand Down Expand Up @@ -1073,7 +1073,7 @@

-record(v2_bearer_flags, {
instance = 0,
flags = []
flags = #{}
}).

-record(v2_pdn_type, {
Expand Down Expand Up @@ -1305,7 +1305,7 @@

-record(v2_csg_information_reporting_action, {
instance = 0,
actions = []
actions = #{}
}).

-record(v2_csg_id, {
Expand Down Expand Up @@ -1335,7 +1335,7 @@

-record(v2_node_features, {
instance = 0,
features = []
features = #{}
}).

-record(v2_mbms_time_to_data_transfer, {
Expand Down Expand Up @@ -1364,7 +1364,7 @@

-record(v2_signalling_priority_indication, {
instance = 0,
indication = []
indication = #{}
}).

-record(v2_temporary_mobile_group_identity, {
Expand All @@ -1380,7 +1380,7 @@

-record(v2_additional_flags_for_srvcc, {
instance = 0,
flags = []
flags = #{}
}).

-record(v2_mdt_configuration, {
Expand All @@ -1398,7 +1398,7 @@

-record(v2_henb_information_reporting_, {
instance = 0,
flags = []
flags = #{}
}).

-record(v2_ipv4_configuration_parameters, {
Expand All @@ -1409,7 +1409,7 @@

-record(v2_change_to_report_flags_, {
instance = 0,
flags = []
flags = #{}
}).

-record(v2_action_indication, {
Expand Down Expand Up @@ -1441,7 +1441,7 @@

-record(v2_trusted_wlan_mode_indication, {
instance = 0,
indication = []
indication = #{}
}).

-record(v2_node_number, {
Expand Down Expand Up @@ -1496,7 +1496,7 @@

-record(v2_wlan_offloadability_indication, {
instance = 0,
indication = []
indication = #{}
}).


Expand Down Expand Up @@ -1528,7 +1528,7 @@

-record(v2_ciot_optimizations_support_indication, {
instance = 0,
indication = []
indication = #{}
}).

-record(v2_scef_pdn_connection, {
Expand Down Expand Up @@ -1578,7 +1578,7 @@

-record(v2_up_function_selection_indication_flags, {
instance = 0,
indication = []
indication = #{}
}).


Expand Down
43 changes: 25 additions & 18 deletions priv/ie_gen_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ ies() ->
"MBMS Counting Information",
"RAN Procedures Ready",
"MBMS Service Type",
"Prohibit Payload Compression"]}},
{'_', 0}]},
"Prohibit Payload Compression"]}}]},
{149, "APN Restriction", '_',
[{"Restriction Type Value", 8, integer}]},
{150, "Radio Priority LCS", '_',
Expand Down Expand Up @@ -366,15 +365,14 @@ ies() ->
{192, "Evolved Allocation/Retention Priority II", '_',
[]},
{193, "Extended Common Flags", '_',
[{"Flags", {flags, ["Unauthenticated IMSI",
"CCRSI",
"CPSR",
"RetLoc",
"VB",
"PCRI",
[{"Flags", {flags, ["UASI",
"BDWI",
"UASI"]}},
{'_', 0}]},
"PCRI",
"VB",
"RetLoc",
"CPSR",
"CCRSI",
"Unauthenticated IMSI"]}}]},
{194, "User CSG Information", '_',
[]},
{195, "CSG Information Reporting Action", '_',
Expand Down Expand Up @@ -510,7 +508,7 @@ msgs() ->
gen_record_def({Value, _}) when is_integer(Value); is_atom(Value) ->
[];
gen_record_def({Name, {flags, _}}) ->
[io_lib:format("~s = []", [s2a(Name)])];
[io_lib:format("~s = #{}", [s2a(Name)])];
gen_record_def({Name, _, {enum, [{_,H}|_]}}) ->
[io_lib:format("~s = ~s", [s2a(Name), s2a(H)])];
gen_record_def({Name, _, {enum, [H|_]}}) ->
Expand Down Expand Up @@ -541,8 +539,8 @@ gen_decoder_header_match({'1', Size}) ->
[io_lib:format("_:~w", [Size])];
gen_decoder_header_match({Value, Size}) when is_integer(Value); is_atom(Value) ->
[io_lib:format("~w:~w", [Value, Size])];
gen_decoder_header_match({Name, {flags, Flags}}) ->
[io_lib:format("M_~s_~s:1", [s2a(Name), s2a(Flag)]) || Flag <- Flags];
gen_decoder_header_match({Name, {flags, _}}) ->
[io_lib:format("M_~s/binary", [s2a(Name)])];
gen_decoder_header_match({Name, boolean}) ->
[io_lib:format("M_~s:1", [s2a(Name)])];
gen_decoder_header_match({Name, Size, {enum, _Enum}}) ->
Expand All @@ -566,9 +564,9 @@ gen_decoder_header_match({Name, Size, Type}) ->

gen_decoder_record_assign({Value, _}) when is_integer(Value); is_atom(Value) ->
[];
gen_decoder_record_assign({Name, {flags, Flags}}) ->
F = [io_lib:format("[ '~s' || M_~s_~s =/= 0 ]", [X, s2a(Name), s2a(X)]) || X <- Flags],
[io_lib:format("~s = ~s", [s2a(Name), string:join(F, " ++ ")])];
gen_decoder_record_assign({Name, {flags, Flags0}}) ->
Flags = [s2f(X) || X <- Flags0],
[io_lib:format("~s = decode_flags(M_~s, ~p)", [s2a(Name), s2a(Name), Flags])];
gen_decoder_record_assign({Name, _Size, {enum, _Enum}}) ->
[io_lib:format("~s = enum_~s(M_~s)", [s2a(Name), s2a(Name), s2a(Name)])];
gen_decoder_record_assign({Name, boolean}) ->
Expand Down Expand Up @@ -598,8 +596,10 @@ gen_encoder_bin({'1', Size}) ->
[io_lib:format("~w:~w", [(1 bsl Size) - 1, Size])];
gen_encoder_bin({Value, Size}) when is_integer(Value); is_atom(Value) ->
[io_lib:format("~w:~w", [Value, Size])];
gen_encoder_bin({Name, {flags, Flags}}) ->
[io_lib:format("(encode_flag('~s', M_~s)):1", [Flag, s2a(Name)]) || Flag <- Flags];
gen_encoder_bin({Name, {flags, Flags0}}) ->
Flags = [s2f(Flag) || Flag <- reorder_flags(Flags0)],
[io_lib:format("(encode_min_int(8, encode_flags(M_~s, ~p), little))/binary",
[s2a(Name), Flags])];
gen_encoder_bin({Name, Size, {enum, _Enum}}) ->
[io_lib:format("(enum_~s(M_~s)):~w/integer", [s2a(Name), s2a(Name), Size])];
gen_encoder_bin({Name, boolean}) ->
Expand Down Expand Up @@ -643,6 +643,9 @@ s2a(Name) ->
end,
string:to_lower(Name)).

s2f(Flags) when is_list(Flags) ->
list_to_atom(Flags).

append([], Acc) ->
Acc;
append([H|T], Acc) ->
Expand Down Expand Up @@ -716,6 +719,10 @@ collect_late_assign(Fields = [H | T], Acc) ->
collect_late_assign(T, [Match|Acc])
end.

reorder_flags([]) -> [];
reorder_flags(Flags) ->
{Head, Tail} = lists:split(8, Flags),
lists:reverse(Head) ++ reorder_flags(Tail).

collect_enum({Name, _, {enum, Enum}}, Acc) ->
{FwdFuns, RevFuns} = gen_enum(Name, Enum, 0, {[], []}),
Expand Down
2 changes: 1 addition & 1 deletion priv/ie_gen_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ gen_record_def(#field{spec = mccmnc}) ->
gen_record_def(#field{name = Name, optional = true}) ->
[to_string(Name)];
gen_record_def(#field{name = Name, type = flags}) ->
[io_lib:format("~s = []", [Name])];
[io_lib:format("~s = #{}", [Name])];
gen_record_def(#field{name = Name, type = enum, spec = [{_,H}|_]}) ->
[io_lib:format("~s = ~s", [Name, s2a(H)])];
gen_record_def(#field{name = Name, type = enum, spec = [H|_]}) ->
Expand Down
101 changes: 44 additions & 57 deletions src/gtp_packet.erl
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,6 @@ bool2int(true) -> 1.
int2bool(0) -> false;
int2bool(_) -> true.

encode_flag(Flag, Flags) ->
bool2int(proplists:get_bool(Flag, Flags)).

is_bin(Bin) -> bool2int(is_binary(Bin)).

%% decoder funs for optional fields
Expand Down Expand Up @@ -474,24 +471,30 @@ v1_instance(CurrId, PrevId, PrevInst)
v1_instance(_CurrId, _PrevId, _PrevInst) ->
0.

decode_flags(<<>>, _) ->
[];
decode_flags(<<_:1, Next/bits>>, ['_' | Flags]) ->
decode_flags(Next, Flags);
decode_flags(<<1:1, Next/bits>>, [F | Flags]) ->
[F | decode_flags(Next, Flags)];
decode_flags(<<_:1, Next/bits>>, [_ | Flags]) ->
decode_flags(Next, Flags);
decode_flags(Bin, []) ->
[binary:decode_unsigned(Bin, little)].

encode_flags([I|_], []) when is_integer(I) -> I;
encode_flags([_|N], []) -> encode_flags(N, []);
encode_flags([], _) -> 0;
encode_flags(Flags, ['_' | N]) -> encode_flags(Flags, N) * 2;
encode_flags(Flags, [F | N]) ->
bool2int(lists:member(F, Flags)) +
encode_flags(Flags -- [F], N) * 2.
decode_flags(<<>>, _, Acc) ->
Acc;
decode_flags(<<_:1, Next/bits>>, ['_' | Flags], Acc) ->
decode_flags(Next, Flags, Acc);
decode_flags(<<1:1, Next/bits>>, [F | Flags], Acc) ->
decode_flags(Next, Flags, [{F, []} | Acc]);
decode_flags(<<_:1, Next/bits>>, [_ | Flags], Acc) ->
decode_flags(Next, Flags, Acc);
decode_flags(Bin, [], Acc) ->
case binary:decode_unsigned(Bin, little) of
0 -> Acc;
Value -> [{undecoded, Value}|Acc]
end.

decode_flags(Bin, Flags) ->
maps:from_list(decode_flags(Bin, Flags, [])).

encode_flags(List, Flags)
when is_list(List) ->
encode_flags(maps:from_list([{K, []} || K <- List]), Flags);
encode_flags(Set, []) ->
maps:get(undecoded, Set, 0);
encode_flags(Set, [F | N]) ->
bool2int(is_map_key(F, Set)) + encode_flags(Set, N) * 2.

decode_v2_cgi(<<MCCMNC:3/bytes, LAC:16, CI:16>>) ->
#cgi{plmn_id = {decode_mcc(MCCMNC), decode_mnc(MCCMNC)}, lac = LAC, ci = CI}.
Expand Down Expand Up @@ -1663,17 +1666,14 @@ decode_v1_element(<<>>, 146, Instance) ->
decode_v1_element(<<>>, 147, Instance) ->
#sgsn_number{instance = Instance};

decode_v1_element(<<M_flags_dual_address_bearer_flag:1,
M_flags_upgrade_qos_supported:1,
M_flags_nrsn:1,
M_flags_no_qos_negotiation:1,
M_flags_mbms_counting_information:1,
M_flags_ran_procedures_ready:1,
M_flags_mbms_service_type:1,
M_flags_prohibit_payload_compression:1,
_/binary>>, 148, Instance) ->
decode_v1_element(<<M_flags/binary>>, 148, Instance) ->
#common_flags{instance = Instance,
flags = [ 'Dual Address Bearer Flag' || M_flags_dual_address_bearer_flag =/= 0 ] ++ [ 'Upgrade QoS Supported' || M_flags_upgrade_qos_supported =/= 0 ] ++ [ 'NRSN' || M_flags_nrsn =/= 0 ] ++ [ 'No QoS negotiation' || M_flags_no_qos_negotiation =/= 0 ] ++ [ 'MBMS Counting Information' || M_flags_mbms_counting_information =/= 0 ] ++ [ 'RAN Procedures Ready' || M_flags_ran_procedures_ready =/= 0 ] ++ [ 'MBMS Service Type' || M_flags_mbms_service_type =/= 0 ] ++ [ 'Prohibit Payload Compression' || M_flags_prohibit_payload_compression =/= 0 ]};
flags = decode_flags(M_flags, ['Dual Address Bearer Flag',
'Upgrade QoS Supported','NRSN',
'No QoS negotiation',
'MBMS Counting Information',
'RAN Procedures Ready','MBMS Service Type',
'Prohibit Payload Compression'])};

decode_v1_element(<<M_restriction_type_value:8/integer>>, 149, Instance) ->
#apn_restriction{instance = Instance,
Expand Down Expand Up @@ -1823,17 +1823,10 @@ decode_v1_element(<<_:1,
decode_v1_element(<<>>, 192, Instance) ->
#evolved_allocation_retention_priority_ii{instance = Instance};

decode_v1_element(<<M_flags_unauthenticated_imsi:1,
M_flags_ccrsi:1,
M_flags_cpsr:1,
M_flags_retloc:1,
M_flags_vb:1,
M_flags_pcri:1,
M_flags_bdwi:1,
M_flags_uasi:1,
_/binary>>, 193, Instance) ->
decode_v1_element(<<M_flags/binary>>, 193, Instance) ->
#extended_common_flags{instance = Instance,
flags = [ 'Unauthenticated IMSI' || M_flags_unauthenticated_imsi =/= 0 ] ++ [ 'CCRSI' || M_flags_ccrsi =/= 0 ] ++ [ 'CPSR' || M_flags_cpsr =/= 0 ] ++ [ 'RetLoc' || M_flags_retloc =/= 0 ] ++ [ 'VB' || M_flags_vb =/= 0 ] ++ [ 'PCRI' || M_flags_pcri =/= 0 ] ++ [ 'BDWI' || M_flags_bdwi =/= 0 ] ++ [ 'UASI' || M_flags_uasi =/= 0 ]};
flags = decode_flags(M_flags, ['UASI','BDWI','PCRI','VB','RetLoc','CPSR',
'CCRSI','Unauthenticated IMSI'])};

decode_v1_element(<<>>, 194, Instance) ->
#user_csg_information{instance = Instance};
Expand Down Expand Up @@ -2428,14 +2421,13 @@ encode_v1_element(#sgsn_number{
encode_v1_element(#common_flags{
instance = Instance,
flags = M_flags}) ->
encode_v1_element(148, Instance, <<(encode_flag('Dual Address Bearer Flag', M_flags)):1,
(encode_flag('Upgrade QoS Supported', M_flags)):1,
(encode_flag('NRSN', M_flags)):1,
(encode_flag('No QoS negotiation', M_flags)):1,
(encode_flag('MBMS Counting Information', M_flags)):1,
(encode_flag('RAN Procedures Ready', M_flags)):1,
(encode_flag('MBMS Service Type', M_flags)):1,
(encode_flag('Prohibit Payload Compression', M_flags)):1>>);
encode_v1_element(148, Instance, <<(encode_min_int(8, encode_flags(M_flags, ['Prohibit Payload Compression',
'MBMS Service Type',
'RAN Procedures Ready',
'MBMS Counting Information',
'No QoS negotiation','NRSN',
'Upgrade QoS Supported',
'Dual Address Bearer Flag']), little))/binary>>);

encode_v1_element(#apn_restriction{
instance = Instance,
Expand Down Expand Up @@ -2627,14 +2619,9 @@ encode_v1_element(#evolved_allocation_retention_priority_ii{
encode_v1_element(#extended_common_flags{
instance = Instance,
flags = M_flags}) ->
encode_v1_element(193, Instance, <<(encode_flag('Unauthenticated IMSI', M_flags)):1,
(encode_flag('CCRSI', M_flags)):1,
(encode_flag('CPSR', M_flags)):1,
(encode_flag('RetLoc', M_flags)):1,
(encode_flag('VB', M_flags)):1,
(encode_flag('PCRI', M_flags)):1,
(encode_flag('BDWI', M_flags)):1,
(encode_flag('UASI', M_flags)):1>>);
encode_v1_element(193, Instance, <<(encode_min_int(8, encode_flags(M_flags, ['Unauthenticated IMSI','CCRSI',
'CPSR','RetLoc','VB','PCRI','BDWI',
'UASI']), little))/binary>>);

encode_v1_element(#user_csg_information{
instance = Instance}) ->
Expand Down
Loading

0 comments on commit bbe98aa

Please sign in to comment.