Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve tests #6

Merged
merged 6 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,047 changes: 1,044 additions & 3 deletions include/gtp_packet.hrl

Large diffs are not rendered by default.

551 changes: 0 additions & 551 deletions include/gtp_packet_v1_gen.hrl

This file was deleted.

483 changes: 0 additions & 483 deletions include/gtp_packet_v2_gen.hrl

This file was deleted.

76 changes: 54 additions & 22 deletions priv/ie_gen_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

-mode(compile).

-define(V1_TAG, <<"%% -include(\"gtp_packet_v1_gen.hrl\").">>).
-define(V2_TAG, <<"%% -include(\"gtp_packet_v2_gen.hrl\").">>).

ies() ->
[{1, "Cause", 1,
[{"Value", 8, {enum, [{0, "Request IMSI"},
Expand Down Expand Up @@ -61,7 +64,7 @@ ies() ->
{231, "Target access restricted for the subscriber"}]}}
]},
{2, "International Mobile Subscriber Identity", 8,
[{"IMSI", 64, {type, tbcd}}]},
[{"IMSI", 64, {type, imsi}}]},
{3, "Routeing Area Identity", 6, v1_rai},
{4, "Temporary Logical Link Identity", 4,
[{"TLLI", 4, bytes}]},
Expand Down Expand Up @@ -467,9 +470,9 @@ gen_record_def({Value, _}) when is_integer(Value); is_atom(Value) ->
gen_record_def({Name, {flags, _}}) ->
[io_lib:format("~s = []", [s2a(Name)])];
gen_record_def({Name, _, {enum, [{_,H}|_]}}) ->
[io_lib:format("~s = ~w", [s2a(Name), H])];
[io_lib:format("~s = ~s", [s2a(Name), s2a(H)])];
gen_record_def({Name, _, {enum, [H|_]}}) ->
[io_lib:format("~s = ~w", [s2a(Name), H])];
[io_lib:format("~s = ~s", [s2a(Name), s2a(H)])];
gen_record_def({Name, _, integer}) ->
[io_lib:format("~s = 0", [s2a(Name)])];
gen_record_def({Name, boolean}) ->
Expand Down Expand Up @@ -582,7 +585,9 @@ indent(Atom, Extra) when is_atom(Atom) ->
indent(atom_to_list(Atom), Extra);
indent(List, Extra) ->
Indent = length(lists:flatten(List)) + Extra,
lists:duplicate(Indent, " ").
Spaces = Indent rem 8,
Tabs = Indent div 8,
[lists:duplicate(Tabs, "\t"), lists:duplicate(Spaces, " ")].

s2a(Name) when is_atom(Name) ->
Name;
Expand Down Expand Up @@ -689,11 +694,24 @@ write_enums(IEs) ->
{_, Str} = lists:unzip(E),
string:join(Str, "\n").

write_record(Name, FieldDefs) ->
Indent = "\t ",
RecordDef = string:join(FieldDefs, [",\n", Indent]),
io_lib:format("-record(~s, {~n~s~s~n}).~n", [s2a(Name), Indent, RecordDef]).

write_record({_Id, Name, Length, []})
when is_integer(Length) ->
FieldDefs =
collect(
fun gen_record_def/1, [{"Instance", 0, integer},
{"Content", Length, bytes}], []),
write_record(Name, FieldDefs);
write_record({_Id, Name, _Length, Fields})
when is_list(Fields) ->
Indent = " ",
RecordDef = string:join(collect(fun gen_record_def/1, [{"Instance", 0, integer} | Fields], []), [",\n", Indent]),
io_lib:format("-record(~s, {~n~s~s~n}).~n", [s2a(Name), Indent, RecordDef]);
FieldDefs =
collect(
fun gen_record_def/1, [{"Instance", 0, integer} | Fields], []),
write_record(Name, FieldDefs);
write_record(_) ->
[].

Expand All @@ -716,16 +734,26 @@ write_decoder(FunName, {Id, _Name, Length, Helper})
io_lib:format("~s(<<Data/binary>>, ~w, Instance) ->~n decode_~s(Data, Instance)",
[FunName, Id, Helper]).

write_encoder(FunName, {Id, Name, _Length, Fields})
when is_list(Fields) ->
RecIdent = indent("encode_v1_element(#", 4),
RecAssign = string:join(["instance = Instance" |
collect(fun gen_encoder_record_assign/1, Fields)], [",\n", RecIdent]),
FunHead = io_lib:format("encode_v1_element(#~s{~n~s~s}) ->~n", [s2a(Name), RecIdent, RecAssign]),
write_rec_encoder(FunName, Id, Name, RecordAssigns, FieldAssigns) ->
RecIdent = indent("encode_v1_element(#", 2),
RecAssign = string:join(["instance = Instance" | RecordAssigns], [",\n", RecIdent]),
FunHead = io_lib:format("encode_v1_element(#~s{~n~s~s}) ->~n",
[s2a(Name), RecIdent, RecAssign]),
DecHead = io_lib:format(" ~s(~w, Instance, ", [FunName, Id]),
BinIndent = indent(DecHead, 2),
BinAssign = string:join(collect(fun(X) -> gen_encoder_bin(X) end, Fields), [",\n", BinIndent]),
io_lib:format("~s~s<<~s>>)", [FunHead, DecHead, BinAssign]);
BinAssign = string:join(FieldAssigns, [",\n", BinIndent]),
io_lib:format("~s~s<<~s>>)", [FunHead, DecHead, BinAssign]).

write_encoder(FunName, {Id, Name, Length, []})
when is_integer(Length) ->
RecordAssigns = ["content = Content"],
FieldAssigns = [io_lib:format("Content:~w/bytes", [Length])],
write_rec_encoder(FunName, Id, Name, RecordAssigns, FieldAssigns);
write_encoder(FunName, {Id, Name, _Length, Fields})
when is_list(Fields) ->
RecordAssigns = collect(fun gen_encoder_record_assign/1, Fields),
FieldAssigns = collect(fun(X) -> gen_encoder_bin(X) end, Fields),
write_rec_encoder(FunName, Id, Name, RecordAssigns, FieldAssigns);
write_encoder(FunName, {Id, Name, Helper})
when is_atom(Helper) ->
io_lib:format("encode_v1_element(#~s{instance = Instance} = IE) ->~n ~s(~w, Instance, encode_~s(IE))",
Expand All @@ -747,10 +775,10 @@ main(_) ->
MTypes = string:join(FwdFuns ++ RevFuns ++ ErrorFun, ";\n") ++ ".\n",

Records = string:join([write_record(X) || X <- ies()], "\n"),
HrlRecs = io_lib:format("%% This file is auto-generated. DO NOT EDIT~n~n~s~n", [Records]),
HrlRecs = io_lib:format("~n~n~s~n", [Records]),
Enums = write_enums(ies()),

CatchAnyDecoder = "decode_v1_element(Value, Tag, Instance) ->\n {Tag, Instance, Value}",
CatchAnyDecoder = "decode_v1_element(Value, Tag, Instance) ->\n {Tag, Instance, Value}",

Funs = string:join([write_decoder("decode_v1_element", X) || X <- ies()] ++ [CatchAnyDecoder], ";\n\n"),

Expand All @@ -777,10 +805,14 @@ main(_) ->
RecPrettyDefs = string:join([write_pretty_print("pretty_print_v1", X) || X <- ies()]
++ [CatchAnyPretty] , ";\n"),

ErlDecls = io_lib:format("%% This file is auto-generated. DO NOT EDIT~n~n~s~n~s~n~s~n~s.~n~n~s~n~n~s.~n~n~s.~n",
[MsgDescription, MTypes, Enums, Funs, MainDecodeSwitch,
EncFuns, RecPrettyDefs]),
ErlDecls = io_lib:format("~n~n~s~n~s~n~s~n~s.~n~n~s~n~n~s.~n~n~s.~n",
[MsgDescription, MTypes, Enums, Funs,
MainDecodeSwitch, EncFuns, RecPrettyDefs]),

{ok, HrlF0} = file:read_file("include/gtp_packet.hrl"),
[HrlHead, _, HrlV2] = binary:split(HrlF0, [?V1_TAG, ?V2_TAG], [global]),
file:write_file("include/gtp_packet.hrl", [HrlHead, ?V1_TAG, HrlRecs, ?V2_TAG, HrlV2]),

file:write_file("include/gtp_packet_v1_gen.hrl", HrlRecs),
file:write_file("src/gtp_packet_v1_gen.hrl", ErlDecls).
{ok, ErlF0} = file:read_file("src/gtp_packet.erl"),
[ErlHead, _, ErlV2] = binary:split(ErlF0, [?V1_TAG, ?V2_TAG], [global]),
file:write_file("src/gtp_packet.erl", [ErlHead, ?V1_TAG, ErlDecls, ?V2_TAG, ErlV2]).
34 changes: 23 additions & 11 deletions priv/ie_gen_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

-mode(compile).

-define(V1_TAG, <<"%% -include(\"gtp_packet_v1_gen.hrl\").">>).
-define(V2_TAG, <<"%% -include(\"gtp_packet_v2_gen.hrl\").">>).

ies() ->
[
{1, "v2 International Mobile Subscriber Identity",
Expand Down Expand Up @@ -334,9 +337,9 @@ gen_record_def({Value, _}) when is_integer(Value); is_atom(Value) ->
gen_record_def({Name, {flags, _}}) ->
[io_lib:format("~s = []", [s2a(Name)])];
gen_record_def({Name, _, {enum, [{_,H}|_]}}) ->
[io_lib:format("~s = ~w", [s2a(Name), H])];
[io_lib:format("~s = ~s", [s2a(Name), s2a(H)])];
gen_record_def({Name, _, {enum, [H|_]}}) ->
[io_lib:format("~s = ~w", [s2a(Name), H])];
[io_lib:format("~s = ~s", [s2a(Name), s2a(H)])];
gen_record_def({Name, _, integer}) ->
[io_lib:format("~s = 0", [s2a(Name)])];
gen_record_def({Name, Size, bits}) ->
Expand Down Expand Up @@ -437,7 +440,9 @@ indent(Atom, Extra) when is_atom(Atom) ->
indent(atom_to_list(Atom), Extra);
indent(List, Extra) ->
Indent = length(lists:flatten(List)) + Extra,
lists:duplicate(Indent, " ").
Spaces = Indent rem 8,
Tabs = Indent div 8,
[lists:duplicate(Tabs, "\t"), lists:duplicate(Spaces, " ")].

s2a(Name) when is_atom(Name) ->
Name;
Expand Down Expand Up @@ -546,7 +551,7 @@ write_enums(IEs) ->

write_record({_Id, Name, Fields})
when is_list(Fields) ->
Indent = " ",
Indent = "\t ",
RecordDef = string:join(collect(fun gen_record_def/1, [{"Instance", 0, integer} | Fields], []), [",\n", Indent]),
io_lib:format("-record(~s, {~n~s~s~n}).~n", [s2a(Name), Indent, RecordDef]);
write_record(_) ->
Expand All @@ -570,7 +575,7 @@ write_decoder(FunName, {Id, _Name, Helper})

write_encoder(FunName, {Id, Name, Fields})
when is_list(Fields) ->
RecIdent = indent("encode_v2_element(#", 4),
RecIdent = indent("encode_v2_element(#", 2),
RecAssign = string:join(["instance = Instance" |
collect(fun gen_encoder_record_assign/1, Fields)], [",\n", RecIdent]),
FunHead = io_lib:format("encode_v2_element(#~s{~n~s~s}) ->~n", [s2a(Name), RecIdent, RecAssign]),
Expand All @@ -595,10 +600,10 @@ main(_) ->
MTypes = string:join(FwdFuns ++ RevFuns ++ ErrorFun, ";\n") ++ ".\n",

Records = string:join([write_record(X) || X <- ies()], "\n"),
HrlRecs = io_lib:format("%% This file is auto-generated. DO NOT EDIT~n~n~s~n", [Records]),
HrlRecs = io_lib:format("~n~n~s", [Records]),
Enums = write_enums(ies()),

CatchAnyDecoder = "decode_v2_element(Value, Tag, Instance) ->\n {Tag, Instance, Value}",
CatchAnyDecoder = "decode_v2_element(Value, Tag, Instance) ->\n {Tag, Instance, Value}",

Funs = string:join([write_decoder("decode_v2_element", X) || X <- ies()] ++ [CatchAnyDecoder], ";\n\n"),

Expand All @@ -611,7 +616,14 @@ main(_) ->
RecPrettyDefs = string:join([write_pretty_print("pretty_print_v2", X) || X <- ies()]
++ [CatchAnyPretty] , ";\n"),

ErlDecls = io_lib:format("%% This file is auto-generated. DO NOT EDIT~n~n~s~n~s~n~s~n~s.~n~n~s.~n~n~s.~n",
[MsgDescription, MTypes, Enums, Funs, EncFuns, RecPrettyDefs]),
file:write_file("include/gtp_packet_v2_gen.hrl", HrlRecs),
file:write_file("src/gtp_packet_v2_gen.hrl", ErlDecls).
ErlDecls = io_lib:format("~n~n~s~n~s~n~s~n~s.~n~n~s.~n~n~s.~n",
[MsgDescription, MTypes, Enums, Funs,
EncFuns, RecPrettyDefs]),

{ok, HrlF0} = file:read_file("include/gtp_packet.hrl"),
[HrlHead, HrlV1, _] = binary:split(HrlF0, [?V1_TAG, ?V2_TAG], [global]),
file:write_file("include/gtp_packet.hrl", [HrlHead, ?V1_TAG, HrlV1, ?V2_TAG, HrlRecs]),

{ok, ErlF0} = file:read_file("src/gtp_packet.erl"),
[ErlHead, ErlV1, _] = binary:split(ErlF0, [?V1_TAG, ?V2_TAG], [global]),
file:write_file("src/gtp_packet.erl", [ErlHead, ?V1_TAG, ErlV1, ?V2_TAG, ErlDecls]).
Loading