Skip to content

Commit afa72d1

Browse files
authored
support for encoding and decoding messages (#111)
* support for encoding and decoding messages * moved encode and decode releated functions to smpp_msg * moved encode and decode releated functions to smpp_msg * added test cases for smpp_msg * removed debug logs
1 parent 6ebf96a commit afa72d1

2 files changed

Lines changed: 486 additions & 66 deletions

File tree

src/smpp.erl

Lines changed: 5 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
internal2json/1, encode/1, decode/1, decode_bin/1, info/0]).
66

77
-export([err/1, cmd/1, cmdstr/1, to_enum/1, from_enum/1, encode_msg/1,
8-
decode_msg/1]).
8+
decode_msg/1, enc/1]).
99

1010
-safe([unpack_map/1]).
1111

@@ -272,78 +272,17 @@ decode_bin(Bin) when is_binary(Bin) ->
272272
end.
273273

274274
encode_msg(#{<<"data_coding">> := EncodingScheme, <<"short_message">> := Msg} = Pdu) ->
275-
Pdu#{<<"short_message">> => encode_msg(EncodingScheme, Msg)};
275+
Pdu#{<<"short_message">> => smpp_msg:encode_msg(EncodingScheme, Msg)};
276276
encode_msg(#{data_coding := EncodingScheme, short_message := Msg} = Pdu) ->
277-
Pdu#{short_message => encode_msg(EncodingScheme, Msg)};
277+
Pdu#{short_message => smpp_msg:encode_msg(EncodingScheme, Msg)};
278278
encode_msg(Pdu) -> Pdu.
279279

280-
encode_msg(EncodingScheme, Msg) when is_binary(EncodingScheme) ->
281-
encode_msg(enc(EncodingScheme), Msg);
282-
encode_msg(?ENCODING_SCHEME_LATIN_1, Msg) -> Msg;
283-
encode_msg(?ENCODING_SCHEME_IA5_ASCII, Msg) -> Msg;
284-
encode_msg(?ENCODING_SCHEME_UCS2, Msg) -> ucs2_encoding(Msg);
285-
encode_msg(EncodingScheme, Msg) when is_binary(Msg) ->
286-
encode_msg(EncodingScheme, binary_to_list(Msg));
287-
encode_msg(_, Msg) when is_list(Msg) ->
288-
case io_lib:printable_list(Msg) of
289-
true -> list_to_binary(Msg);
290-
false -> base64:decode(Msg)
291-
end.
292-
293280
decode_msg(#{<<"data_coding">> := EncodingScheme, <<"short_message">> := Msg} = Pdu) ->
294-
Pdu#{<<"short_message">> => decode_msg(EncodingScheme, Msg)};
281+
Pdu#{<<"short_message">> => smpp_msg:decode_msg(EncodingScheme, Msg)};
295282
decode_msg(#{data_coding := EncodingScheme, short_message := Msg} = Pdu) ->
296-
Pdu#{short_message => decode_msg(EncodingScheme, Msg)};
283+
Pdu#{short_message => smpp_msg:decode_msg(EncodingScheme, Msg)};
297284
decode_msg(Pdu) -> Pdu.
298285

299-
decode_msg(EncodingScheme, Msg) when is_binary(EncodingScheme) ->
300-
decode_msg(enc(EncodingScheme), Msg);
301-
decode_msg(?ENCODING_SCHEME_UCS2, Msg) -> decode_ucs2(Msg);
302-
decode_msg(?ENCODING_SCHEME_LATIN_1, Msg) -> Msg;
303-
decode_msg(?ENCODING_SCHEME_IA5_ASCII, Msg) -> Msg;
304-
decode_msg(EncodingScheme, Msg) when is_binary(Msg) ->
305-
decode_msg(EncodingScheme, binary_to_list(Msg));
306-
decode_msg(_, Msg) when is_list(Msg) ->
307-
case io_lib:printable_list(Msg) of
308-
true -> list_to_binary(Msg);
309-
false -> base64:encode(Msg)
310-
end.
311-
312-
ucs2_encoding(Msg) when is_binary(Msg) ->
313-
ucs2_encoding(unicode:characters_to_list(Msg, unicode));
314-
ucs2_encoding(Msg) when is_list(Msg) ->
315-
SM1 = unicode:characters_to_list(Msg, unicode),
316-
SM2 = unicode:characters_to_binary(SM1, unicode, utf16),
317-
re:replace(SM2, "\"", "\\\\\"", [global, {return, binary}]).
318-
319-
decode_ucs2(Msg) when is_binary(Msg) ->
320-
decode_ucs2(binary_to_list(Msg));
321-
decode_ucs2(Msg) when is_list(Msg) ->
322-
re:replace(ucs2_to_utf16(Msg), "\"", "\\\\\"", [global, {return, binary}]).
323-
324-
ucs2_to_utf16({cp, CPList}) ->
325-
case unicode:characters_to_binary(CPList, utf16, utf8) of
326-
{error, ConvertedBin, [[Failed]|Rest]} ->
327-
<<ConvertedBin/binary, (Failed bsr 8), (Failed band 16#00FF),
328-
(ucs2_to_utf16({cp, Rest}))/binary>>;
329-
ConvertedBin when is_binary(ConvertedBin) -> ConvertedBin
330-
end;
331-
ucs2_to_utf16(Msg) when is_list(Msg) ->
332-
CPList = ucs2_to_utf16_cp(Msg, []),
333-
ucs2_to_utf16({cp, CPList}).
334-
335-
ucs2_to_utf16_cp([], Result) -> lists:reverse(Result);
336-
% Erlang DOC: An integer in the range 16#D800 to 16#DFFF (invalid range
337-
% reserved for UTF-16 surrogate pairs)
338-
ucs2_to_utf16_cp([A,B|Rest], Result) when A >= 16#D8, A =< 16#DF ->
339-
ucs2_to_utf16_cp(
340-
Rest, lists:reverse(
341-
lists:flatten(
342-
[io_lib:format("\\u~2.16.0B~2.16.0B", [A,B])]
343-
)) ++ Result);
344-
ucs2_to_utf16_cp([A,B|Rest], Result) ->
345-
ucs2_to_utf16_cp(Rest, [(A bsl 8) + B | Result]).
346-
347286
to_enum(SMPP) when is_map(SMPP) ->
348287
maps:fold(
349288
fun(K, V, M) ->

0 commit comments

Comments
 (0)