1 |
|
%% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com> |
2 |
|
%% |
3 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
|
%% you may not use this file except in compliance with the License. |
5 |
|
%% You may obtain a copy of the License at |
6 |
|
%% |
7 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
8 |
|
%% |
9 |
|
%% Unless required by applicable law or agreed to in writing, software |
10 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
11 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
|
%% See the License for the specific language governing permissions and |
13 |
|
%% limitations under the License. |
14 |
|
|
15 |
|
|
16 |
|
-module(msmp_codec). |
17 |
|
|
18 |
|
|
19 |
|
-feature(maybe_expr, enable). |
20 |
|
|
21 |
|
|
22 |
|
-export([decode/1]). |
23 |
|
-export([encode/1]). |
24 |
|
-include_lib("kernel/include/logger.hrl"). |
25 |
|
|
26 |
|
|
27 |
|
decode(Parser) -> |
28 |
87 |
fun |
29 |
|
(Input) -> |
30 |
114 |
(scran_result:into_map( |
31 |
|
scran_combinator:map_parser( |
32 |
|
scran_bytes:length_encoded( |
33 |
|
scran_combinator:map_result( |
34 |
|
msmp_integer_fixed:decode(3), |
35 |
|
fun |
36 |
|
(Length) -> |
37 |
|
%% adjust length to include the sequence number |
38 |
114 |
Length + 1 |
39 |
|
end)), |
40 |
|
scran_sequence:sequence( |
41 |
|
[scran_result:kv(sequence, msmp_integer_fixed:decode(1)), |
42 |
|
scran_result:kv(packet, Parser)]))))(Input) |
43 |
|
end. |
44 |
|
|
45 |
|
|
46 |
|
encode(Parser) -> |
47 |
14 |
fun |
48 |
|
(Decoded) -> |
49 |
9 |
?LOG_DEBUG(#{decoded => Decoded}), |
50 |
9 |
(narcs_combinator:map_result( |
51 |
|
narcs_sequence:sequence( |
52 |
|
[narcs_combinator:v( |
53 |
|
sequence, |
54 |
|
msmp_integer_fixed:encode(1)), |
55 |
|
narcs_combinator:v( |
56 |
|
packet, |
57 |
|
Parser)]), |
58 |
|
narcs_bytes:length_encoded( |
59 |
|
fun |
60 |
|
(Length) -> |
61 |
|
%% adjust length to exclude the sequence number |
62 |
9 |
(msmp_integer_fixed:encode(3))(Length - 1) |
63 |
|
end)))(Decoded) |
64 |
|
end. |