blob: 5b2162a47e7da7d3b7124a8ffe528327d173c18e [file] [log] [blame]
Harald Weltede4c14c2022-07-16 11:53:59 +02001# -*- coding: utf-8 -*-
2
3# without this, pylint will fail when inner classes are used
4# within the 'nested' kwarg of our TlvMeta metaclass on python 3.7 :(
5# pylint: disable=undefined-variable
6
7"""
Harald Weltea0452212022-07-17 21:23:21 +02008DF_PHONEBOOK, DF_MULTIMEDIA as specified in 3GPP TS 31.102 V16.6.0
Harald Weltede4c14c2022-07-16 11:53:59 +02009Needs to be a separate python module to avoid cyclic imports
10"""
11
12#
13# Copyright (C) 2022 Harald Welte <laforge@osmocom.org>
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 2 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program. If not, see <http://www.gnu.org/licenses/>.
27#
28
29from pySim.tlv import *
30from pySim.filesystem import *
31from pySim.construct import *
32from construct import Optional as COptional
33from construct import *
34
Harald Welte6f8a8702022-07-17 21:50:31 +020035# TS 31.102 Section 4.2.8
36class EF_UServiceTable(TransparentEF):
37 def __init__(self, fid, sfid, name, desc, size, table, **kwargs):
38 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size=size, **kwargs)
39 self.table = table
40
41 @staticmethod
42 def _bit_byte_offset_for_service(service: int) -> Tuple[int, int]:
43 i = service - 1
44 byte_offset = i//8
45 bit_offset = (i % 8)
46 return (byte_offset, bit_offset)
47
48 def _decode_bin(self, in_bin):
49 ret = {}
50 for i in range(0, len(in_bin)):
51 byte = in_bin[i]
52 for bitno in range(0, 8):
53 service_nr = i * 8 + bitno + 1
54 ret[service_nr] = {
55 'activated': True if byte & (1 << bitno) else False
56 }
57 if service_nr in self.table:
58 ret[service_nr]['description'] = self.table[service_nr]
59 return ret
60
61 def _encode_bin(self, in_json):
62 # compute the required binary size
63 bin_len = 0
64 for srv in in_json.keys():
65 service_nr = int(srv)
66 (byte_offset, bit_offset) = EF_UServiceTable._bit_byte_offset_for_service(
67 service_nr)
68 if byte_offset >= bin_len:
69 bin_len = byte_offset+1
70 # encode the actual data
71 out = bytearray(b'\x00' * bin_len)
72 for srv in in_json.keys():
73 service_nr = int(srv)
74 (byte_offset, bit_offset) = EF_UServiceTable._bit_byte_offset_for_service(
75 service_nr)
76 if in_json[srv]['activated'] == True:
77 bit = 1
78 else:
79 bit = 0
80 out[byte_offset] |= (bit) << bit_offset
81 return out
82
83 def get_active_services(self, cmd):
84 # obtain list of currently active services
85 (service_data, sw) = cmd.lchan.read_binary_dec()
86 active_services = []
87 for s in service_data.keys():
88 if service_data[s]['activated']:
89 active_services.append(s)
90 return active_services
91
92 def ust_service_check(self, cmd):
93 """Check consistency between services of this file and files present/activated"""
94 num_problems = 0
95 # obtain list of currently active services
96 active_services = self.get_active_services(cmd)
97 # iterate over all the service-constraints we know of
98 files_by_service = self.parent.files_by_service
99 try:
100 for s in sorted(files_by_service.keys()):
101 active_str = 'active' if s in active_services else 'inactive'
102 cmd.poutput("Checking service No %u (%s)" % (s, active_str))
103 for f in files_by_service[s]:
104 should_exist = f.should_exist_for_services(active_services)
105 try:
106 cmd.lchan.select_file(f)
107 sw = None
108 exists = True
109 except SwMatchError as e:
110 sw = str(e)
111 exists = False
112 if exists != should_exist:
113 num_problems += 1
114 if exists:
115 cmd.perror(" ERROR: File %s is selectable but should not!" % f)
116 else:
117 cmd.perror(" ERROR: File %s is not selectable (%s) but should!" % (f, sw))
118 finally:
119 # re-select the EF.UST
120 cmd.lchan.select_file(self)
121 return num_problems
122
123
124
Harald Weltede4c14c2022-07-16 11:53:59 +0200125# TS 31.102 Section 4.4.2.1
126class EF_PBR(LinFixedEF):
127 def __init__(self, fid='4F30', name='EF.PBR', desc='Phone Book Reference', **kwargs):
128 super().__init__(fid, name=name, desc=desc, **kwargs)
129 #self._tlv = FIXME
130
131# TS 31.102 Section 4.4.2.12.2
132class EF_PSC(TransparentEF):
133 _construct = Struct('synce_counter'/Int32ub)
134 def __init__(self, fid='4F22', name='EF.PSC', desc='Phone Book Synchronization Counter', **kwargs):
135 super().__init__(fid, name=name, desc=desc, **kwargs)
136 #self._tlv = FIXME
137
138# TS 31.102 Section 4.4.2.12.3
139class EF_CC(TransparentEF):
140 _construct = Struct('change_counter'/Int16ub)
141 def __init__(self, fid='4F23', name='EF.CC', desc='Change Counter', **kwargs):
142 super().__init__(fid, name=name, desc=desc, **kwargs)
143
144# TS 31.102 Section 4.4.2.12.4
145class EF_PUID(TransparentEF):
146 _construct = Struct('previous_uid'/Int16ub)
147 def __init__(self, fid='4F24', name='EF.PUID', desc='Previous Unique Identifer', **kwargs):
148 super().__init__(fid, name=name, desc=desc, **kwargs)
149
150# TS 31.102 Section 4.4.2
151class DF_PHONEBOOK(CardDF):
152 def __init__(self, fid='5F3A', name='DF.PHONEBOOK', desc='Phonebook', **kwargs):
153 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
154 files = [
155 EF_PBR(),
156 EF_PSC(),
157 EF_CC(),
158 EF_PUID(),
159 # FIXME: Those 4Fxx entries with unspecified FID...
160 ]
161 self.add_files(files)
Harald Weltea0452212022-07-17 21:23:21 +0200162
163
164
165# TS 31.102 Section 4.6.3.1
166class EF_MML(BerTlvEF):
167 def __init__(self, fid='4F47', name='EF.MML', desc='Multimedia Messages List', **kwargs):
168 super().__init__(fid, name=name, desc=desc, **kwargs)
169
170# TS 31.102 Section 4.6.3.2
171class EF_MMDF(BerTlvEF):
172 def __init__(self, fid='4F48', name='EF.MMDF', desc='Multimedia Messages Data File', **kwargs):
173 super().__init__(fid, name=name, desc=desc, **kwargs)
174
175class DF_MULTIMEDIA(CardDF):
176 def __init__(self, fid='5F3B', name='DF.MULTIMEDIA', desc='Multimedia', **kwargs):
177 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
178 files = [
179 EF_MML(),
180 EF_MMDF(),
181 ]
182 self.add_files(files)