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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2005-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
%%----------------------------------------------------------------------
%% Purpose : Handle ASN.1 BER encoding of Megaco/H.248
%%----------------------------------------------------------------------
-module(megaco_binary_encoder_lib).
%% API
-export([
version_of/4,
decode_message/5, decode_message_dynamic/4,
decode_mini_message/4, decode_mini_message_dynamic/4,
encode_message/5,
encode_transaction/5,
encode_action_requests/5,
encode_action_request/5,
encode_action_reply/5
]).
-include_lib("megaco/src/engine/megaco_message_internal.hrl").
%%----------------------------------------------------------------------
%% Detect (check) which version a message is
%% Return {ok, Version} | {error, Reason}
%%----------------------------------------------------------------------
version_of(_EC, Binary, dynamic, [AsnModV1|_AsnMods])
when is_binary(Binary) andalso is_atom(AsnModV1) ->
case (catch AsnModV1:decode_message_version(Binary)) of
{ok, PartialMsg} ->
V = (PartialMsg#'MegacoMessage'.mess)#'Message'.version,
{ok, V};
Error ->
Error
end;
version_of(_EC, Binary, 1, AsnMods)
when is_binary(Binary) andalso is_list(AsnMods) ->
version_of(AsnMods, Binary, []);
version_of(_EC, Binary, 2, [AsnModV1, AsnModV2, AsnModV3])
when is_binary(Binary) ->
version_of([AsnModV2, AsnModV1, AsnModV3], Binary, []);
version_of(_EC, Binary, 3, [AsnModV1, AsnModV2, AsnModV3])
when is_binary(Binary) ->
version_of([AsnModV3, AsnModV1, AsnModV2], Binary, []).
version_of([], _Binary, Err) ->
{error, {decode_failed, lists:reverse(Err)}};
version_of([AsnMod|AsnMods], Binary, Errs) when is_atom(AsnMod) ->
case (catch AsnMod:decode('MegacoMessage', Binary)) of
{ok, M} ->
V = (M#'MegacoMessage'.mess)#'Message'.version,
{ok, V};
Err ->
version_of(AsnMods, Binary, [Err|Errs])
end.
%%----------------------------------------------------------------------
%% Convert a 'MegacoMessage' record into a binary
%% Return {ok, Binary} | {error, Reason}
%%----------------------------------------------------------------------
encode_message([native], MegaMsg, AsnMod, _TransMod, binary)
when is_record(MegaMsg, 'MegacoMessage') ->
AsnMod:encode('MegacoMessage', MegaMsg);
encode_message(EC, MegaMsg, AsnMod, TransMod, binary)
when is_list(EC) andalso is_record(MegaMsg, 'MegacoMessage') ->
case (catch TransMod:tr_message(MegaMsg, encode, EC)) of
{'EXIT', Reason} ->
{error, Reason};
MegaMsg2 ->
AsnMod:encode('MegacoMessage', MegaMsg2)
end;
encode_message(EC, MegaMsg, AsnMod, TransMod, io_list) ->
case encode_message(EC, MegaMsg, AsnMod, TransMod, binary) of
{ok, Bin} when is_binary(Bin) ->
{ok, Bin};
{ok, DeepIoList} ->
Bin = erlang:list_to_binary(DeepIoList),
{ok, Bin};
{error, Reason} ->
{error, Reason}
end;
encode_message(EC, MegaMsg, _AsnMod, _TransMod, _Type)
when is_record(MegaMsg, 'MegacoMessage') ->
{error, {bad_encoding_config, EC}};
encode_message(_EC, MegaMsg, _AsnMod, _TransMod, _Type) ->
{error, {no_megaco_message, MegaMsg}}.
%%----------------------------------------------------------------------
%% Convert a transaction (or transactions in the case of ack) record(s)
%% into a binary
%% Return {ok, Binary} | {error, Reason}
%%----------------------------------------------------------------------
%% Should handle encoding of all types of transactions:
%% TransactionAck, TransactionPending, TransactionRequest
%% and TransactionReply
encode_transaction(EC, {Tag, _} = Trans, AsnMod, TransMod, Type)
when (Tag == transactionResponseAck) ->
do_encode_transaction(EC, Trans, AsnMod, TransMod, Type);
encode_transaction(EC, {Tag, _} = Trans, AsnMod, TransMod, Type)
when (Tag == transactionPending) ->
do_encode_transaction(EC, Trans, AsnMod, TransMod, Type);
encode_transaction(EC, {Tag, _} = Trans, AsnMod, TransMod, Type)
when (Tag == transactionRequest) ->
do_encode_transaction(EC, Trans, AsnMod, TransMod, Type);
%% TransactionReply has been changed as of v3 so we cannot use
%% the record definition in this common module.
encode_transaction(EC, {Tag, _} = Trans, AsnMod, TransMod, Type)
when (Tag == transactionReply) ->
do_encode_transaction(EC, Trans, AsnMod, TransMod, Type);
encode_transaction(_EC, T, _AsnMod, _TransMod, _Type) ->
{error, {no_megaco_transaction, T}}.
-spec do_encode_transaction(EC :: list(),
Trans :: tuple(),
AnsMod :: atom(),
TransMod :: atom(),
Type :: atom()) ->
{'ok', binary()} | {'error', any()}.
do_encode_transaction([native], _Trans, _AsnMod, _TransMod, binary) ->
%% asn1rt:encode(AsnMod, element(1, T), T);
{error, not_implemented};
do_encode_transaction(EC, _Trans, _AsnMod, _TransMod, binary)
when is_list(EC) ->
%% T2 = TransMod:tr_transaction(Trans, encode, EC),
%% asn1rt:encode(AsnMod, element(1, T), T2);
{error, not_implemented};
do_encode_transaction(EC, Trans, AsnMod, TransMod, io_list) ->
case do_encode_transaction(EC, Trans, AsnMod, TransMod, binary) of
{ok, Bin} when is_binary(Bin) ->
{ok, Bin};
{ok, DeepIoList} ->
Bin = erlang:list_to_binary(DeepIoList),
{ok, Bin};
{error, Reason} ->
{error, Reason}
end;
do_encode_transaction(EC, _Trans, _AsnMod, _TransMod, _Type) ->
{error, {bad_encoding_config, EC}}.
%%----------------------------------------------------------------------
%% Convert a list of ActionRequest record's into a binary
%% Return {ok, DeepIoList} | {error, Reason}
%%----------------------------------------------------------------------
-spec encode_action_requests(EC :: list(),
ARs :: list(),
AnsMod :: atom(),
TransMod :: atom(),
Type :: atom()) ->
{'ok', binary()} | {'error', any()}.
encode_action_requests([native], _ARs, _AsnMod, _TransMod, binary) ->
%% asn1rt:encode(AsnMod, element(1, T), T);
{error, not_implemented};
encode_action_requests(_EC, _ARs0, _AsnMod, _TransMod, binary) ->
{error, not_implemented};
encode_action_requests(EC, ARs, AsnMod, TransMod, io_list) ->
case encode_action_requests(EC, ARs, AsnMod, TransMod, binary) of
{ok, Bin} when is_binary(Bin) ->
{ok, Bin};
{ok, DeepIoList} ->
Bin = erlang:list_to_binary(DeepIoList),
{ok, Bin};
{error, Reason} ->
{error, Reason}
end;
encode_action_requests(EC, _ARs, _AsnMod, _TransMod, _Type) ->
{error, {bad_encoding_config, EC}}.
%%----------------------------------------------------------------------
%% Convert a ActionRequest record into a binary
%% Return {ok, DeepIoList} | {error, Reason}
%%----------------------------------------------------------------------
-spec encode_action_request(EC :: list(),
AR :: tuple(),
AnsMod :: atom(),
TransMod :: atom(),
Type :: atom()) ->
{'ok', binary()} | {'error', any()}.
encode_action_request([native], _AR, _AsnMod, _TransMod, binary) ->
%% asn1rt:encode(AsnMod, element(1, T), T);
{error, not_implemented};
encode_action_request(_EC, _AR, _AsnMod, _TransMod, binary) ->
{error, not_implemented};
encode_action_request(EC, AR, AsnMod, TransMod, io_list) ->
case encode_action_request(EC, AR, AsnMod, TransMod, binary) of
{ok, Bin} when is_binary(Bin) ->
{ok, Bin};
{ok, DeepIoList} ->
Bin = erlang:list_to_binary(DeepIoList),
{ok, Bin};
{error, Reason} ->
{error, Reason}
end;
encode_action_request(EC, _AR, _AsnMod, _TransMod, _Type) ->
{error, {bad_encoding_config, EC}}.
%%----------------------------------------------------------------------
%% Convert a ActionReply record into a binary
%% Return {ok, DeepIoList} | {error, Reason}
%%----------------------------------------------------------------------
encode_action_reply([native], _ARs, _AsnMod, _TransMod, binary) ->
%% asn1rt:encode(AsnMod, element(1, T), T);
{error, not_implemented};
encode_action_reply(_EC, _ARs0, _AsnMod, _TransMod, binary) ->
{error, not_implemented};
encode_action_reply(EC, ARs, AsnMod, TransMod, io_list) ->
case encode_action_reply(EC, ARs, AsnMod, TransMod, binary) of
{ok, Bin} when is_binary(Bin) ->
{ok, Bin};
{ok, DeepIoList} ->
Bin = erlang:list_to_binary(DeepIoList),
{ok, Bin};
{error, Reason} ->
{error, Reason}
end;
encode_action_reply(EC, _ARs, _AsnMod, _TransMod, _Type) ->
{error, {bad_encoding_config, EC}}.
%%----------------------------------------------------------------------
%% Convert a binary into a 'MegacoMessage' record
%% Return {ok, MegacoMessageRecord} | {error, Reason}
%%----------------------------------------------------------------------
decode_message_dynamic(EC, Bin,
[{AsnModV1, TransModV1},
{AsnModV2, TransModV2},
{AsnModV3, TransModV3}], Form)
when is_list(EC) andalso is_binary(Bin) ->
case AsnModV1:decode_message_version(Bin) of
{ok, PartialMsg} ->
V = (PartialMsg#'MegacoMessage'.mess)#'Message'.version,
case V of
1 ->
decode_message(EC, Bin, AsnModV1, TransModV1, Form);
2 ->
decode_message(EC, Bin, AsnModV2, TransModV2, Form);
3 ->
decode_message(EC, Bin, AsnModV3, TransModV3, Form)
end;
{error, Reason} ->
{error, Reason}
end;
decode_message_dynamic(EC, Bin, _Mods, _Type)
when is_binary(Bin) ->
{error, {bad_encoding_config, EC}};
decode_message_dynamic(_EC, _BadBin, _Mods, _Type) ->
{error, no_binary}.
decode_message(EC, Bin, AsnMod, TransMod, _) ->
case AsnMod:decode('MegacoMessage', Bin) of
{ok, MegaMsg} ->
case EC of
[native] ->
{ok, MegaMsg};
_ ->
{ok, TransMod:tr_message(MegaMsg, decode, EC)}
end;
{error, Reason} ->
{error, Reason}
end.
%%----------------------------------------------------------------------
%% Convert a binary into a partial 'MegacoMessage' record
%% I.e. only version and Mid is fully decoded.
%% Return {ok, MegacoMessageRecord} | {error, Reason}
%%----------------------------------------------------------------------
decode_mini_message(_, Bin, Mod, _) ->
case (catch Mod:decode_message_mId(Bin)) of
{ok, #'MegacoMessage'{mess = Mess} = MegaMsg} ->
Mess2 = Mess#'Message'{messageBody = undefined},
{ok, MegaMsg#'MegacoMessage'{mess = Mess2}};
Error ->
Error
end.
decode_mini_message_dynamic(EC, Bin, [Mod1, Mod2, Mod3], Form) ->
case Mod1:decode_message_version(Bin) of
{ok, PartialMsg} ->
V = (PartialMsg#'MegacoMessage'.mess)#'Message'.version,
case V of
1 ->
decode_mini_message(EC, Bin, Mod1, Form);
2 ->
decode_mini_message(EC, Bin, Mod2, Form);
3 ->
decode_mini_message(EC, Bin, Mod3, Form)
end;
Error ->
Error
end.
|