blob: b0f03b7e8889a2862524d9f7c9318b78fd7f8d47 [file] [log] [blame]
Harald Weltee0f9ef12021-04-10 17:22:35 +02001from construct import *
Harald Welte2db5cfb2021-04-10 19:05:37 +02002from pySim.utils import b2h, h2b, swap_nibbles
Harald Weltee0f9ef12021-04-10 17:22:35 +02003
4"""Utility code related to the integration of the 'construct' declarative parser."""
5
6# (C) 2021 by Harald Welte <laforge@osmocom.org>
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
22class HexAdapter(Adapter):
23 """convert a bytes() type to a string of hex nibbles."""
24 def _decode(self, obj, context, path):
25 return b2h(obj)
26 def _encode(self, obj, context, path):
27 return h2b(obj)
28
Harald Welte2db5cfb2021-04-10 19:05:37 +020029class BcdAdapter(Adapter):
30 """convert a bytes() type to a string of BCD nibbles."""
31 def _decode(self, obj, context, path):
32 return swap_nibbles(b2h(obj))
33 def _encode(self, obj, context, path):
34 return h2b(swap_nibbles(obj))
35
Harald Weltee0f9ef12021-04-10 17:22:35 +020036def filter_dict(d, exclude_prefix='_'):
37 """filter the input dict to ensure no keys starting with 'exclude_prefix' remain."""
38 res = {}
39 for (key, value) in d.items():
40 if key.startswith(exclude_prefix):
41 continue
42 if type(value) is dict:
43 res[key] = filter_dict(value)
44 else:
45 res[key] = value
46 return res
47
48# here we collect some shared / common definitions of data types
49LV = Prefixed(Int8ub, HexAdapter(GreedyBytes))
Robert Falkenberg9d16fbc2021-04-12 11:43:22 +020050
51# Default value for Reserved for Future Use (RFU) bits/bytes
52# See TS 31.101 Sec. "3.4 Coding Conventions"
53__RFU_VALUE = 0
54
55# Field that packs Reserved for Future Use (RFU) bit
56FlagRFU = Default(Flag, __RFU_VALUE)
57
58# Field that packs Reserved for Future Use (RFU) byte
59ByteRFU = Default(Byte, __RFU_VALUE)
60
61# Field that packs all remaining Reserved for Future Use (RFU) bytes
62GreedyBytesRFU = Default(GreedyBytes, b'')
63
64def BitsRFU(n=1):
65 '''
66 Field that packs Reserved for Future Use (RFU) bit(s)
67 as defined in TS 31.101 Sec. "3.4 Coding Conventions"
68
69 Use this for (currently) unused/reserved bits whose contents
70 should be initialized automatically but should not be cleared
71 in the future or when restoring read data (unlike padding).
72
73 Parameters:
74 n (Integer): Number of bits (default: 1)
75 '''
76 return Default(BitsInteger(n), __RFU_VALUE)
77
78def BytesRFU(n=1):
79 '''
80 Field that packs Reserved for Future Use (RFU) byte(s)
81 as defined in TS 31.101 Sec. "3.4 Coding Conventions"
82
83 Use this for (currently) unused/reserved bytes whose contents
84 should be initialized automatically but should not be cleared
85 in the future or when restoring read data (unlike padding).
86
87 Parameters:
88 n (Integer): Number of bytes (default: 1)
89 '''
90 return Default(Bytes(n), __RFU_VALUE)