blob: a0098c72ec1b6d8e0f287b6750d3ddbae6412cfb [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 Welte650f6122022-07-17 21:42:50 +02008DF_PHONEBOOK, DF_MULTIMEDIA, DF_MCS 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):
Harald Welte865eea62023-01-27 19:26:12 +0100127 # TODO: a80ac0034f3a02c5034f0904aa0acb034f3d07c2034f4a06
Harald Weltede4c14c2022-07-16 11:53:59 +0200128 def __init__(self, fid='4F30', name='EF.PBR', desc='Phone Book Reference', **kwargs):
129 super().__init__(fid, name=name, desc=desc, **kwargs)
130 #self._tlv = FIXME
131
132# TS 31.102 Section 4.4.2.12.2
133class EF_PSC(TransparentEF):
134 _construct = Struct('synce_counter'/Int32ub)
135 def __init__(self, fid='4F22', name='EF.PSC', desc='Phone Book Synchronization Counter', **kwargs):
136 super().__init__(fid, name=name, desc=desc, **kwargs)
137 #self._tlv = FIXME
138
139# TS 31.102 Section 4.4.2.12.3
140class EF_CC(TransparentEF):
141 _construct = Struct('change_counter'/Int16ub)
142 def __init__(self, fid='4F23', name='EF.CC', desc='Change Counter', **kwargs):
143 super().__init__(fid, name=name, desc=desc, **kwargs)
144
145# TS 31.102 Section 4.4.2.12.4
146class EF_PUID(TransparentEF):
147 _construct = Struct('previous_uid'/Int16ub)
148 def __init__(self, fid='4F24', name='EF.PUID', desc='Previous Unique Identifer', **kwargs):
149 super().__init__(fid, name=name, desc=desc, **kwargs)
150
151# TS 31.102 Section 4.4.2
152class DF_PHONEBOOK(CardDF):
153 def __init__(self, fid='5F3A', name='DF.PHONEBOOK', desc='Phonebook', **kwargs):
154 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
155 files = [
156 EF_PBR(),
157 EF_PSC(),
158 EF_CC(),
159 EF_PUID(),
160 # FIXME: Those 4Fxx entries with unspecified FID...
161 ]
162 self.add_files(files)
Harald Weltea0452212022-07-17 21:23:21 +0200163
164
165
166# TS 31.102 Section 4.6.3.1
167class EF_MML(BerTlvEF):
168 def __init__(self, fid='4F47', name='EF.MML', desc='Multimedia Messages List', **kwargs):
169 super().__init__(fid, name=name, desc=desc, **kwargs)
170
171# TS 31.102 Section 4.6.3.2
172class EF_MMDF(BerTlvEF):
173 def __init__(self, fid='4F48', name='EF.MMDF', desc='Multimedia Messages Data File', **kwargs):
174 super().__init__(fid, name=name, desc=desc, **kwargs)
175
176class DF_MULTIMEDIA(CardDF):
177 def __init__(self, fid='5F3B', name='DF.MULTIMEDIA', desc='Multimedia', **kwargs):
178 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
179 files = [
180 EF_MML(),
181 EF_MMDF(),
182 ]
183 self.add_files(files)
Harald Welte650f6122022-07-17 21:42:50 +0200184
185
186# TS 31.102 Section 4.6.4.1
187EF_MST_map = {
188 1: 'MCPTT UE configuration data',
189 2: 'MCPTT User profile data',
190 3: 'MCS Group configuration data',
191 4: 'MCPTT Service configuration data',
192 5: 'MCS UE initial configuration data',
193 6: 'MCData UE configuration data',
194 7: 'MCData user profile data',
195 8: 'MCData service configuration data',
196 9: 'MCVideo UE configuration data',
197 10: 'MCVideo user profile data',
198 11: 'MCVideo service configuration data',
199 }
200
201# TS 31.102 Section 4.6.4.2
202class EF_MCS_CONFIG(BerTlvEF):
203 class McpttUeConfigurationData(BER_TLV_IE, tag=0x80):
204 pass
205 class McpttUserProfileData(BER_TLV_IE, tag=0x81):
206 pass
207 class McsGroupConfigurationData(BER_TLV_IE, tag=0x82):
208 pass
209 class McpttServiceConfigurationData(BER_TLV_IE, tag=0x83):
210 pass
211 class McsUeInitialConfigurationData(BER_TLV_IE, tag=0x84):
212 pass
213 class McdataUeConfigurationData(BER_TLV_IE, tag=0x85):
214 pass
215 class McdataUserProfileData(BER_TLV_IE, tag=0x86):
216 pass
217 class McdataServiceConfigurationData(BER_TLV_IE, tag=0x87):
218 pass
219 class McvideoUeConfigurationData(BER_TLV_IE, tag=0x88):
220 pass
221 class McvideoUserProfileData(BER_TLV_IE, tag=0x89):
222 pass
223 class McvideoServiceConfigurationData(BER_TLV_IE, tag=0x8a):
224 pass
225 class McsConfigDataCollection(TLV_IE_Collection, nested=[McpttUeConfigurationData,
226 McpttUserProfileData, McsGroupConfigurationData,
227 McpttServiceConfigurationData, McsUeInitialConfigurationData,
228 McdataUeConfigurationData, McdataUserProfileData,
229 McdataServiceConfigurationData, McvideoUeConfigurationData,
230 McvideoUserProfileData, McvideoServiceConfigurationData]):
231 pass
232 def __init__(self, fid='4F02', sfid=0x02, name='EF.MCS_CONFIG', desc='MCS configuration data', **kwargs):
233 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, **kwargs)
234 self._tlv = EF_MCS_CONFIG.McsConfigDataCollection
235
236# TS 31.102 Section 4.6.4.1
237class EF_MST(EF_UServiceTable):
Harald Welte13edf302022-07-21 15:19:23 +0200238 def __init__(self, fid='4F01', sfid=0x01, name='EF.MST', desc='MCS Service Table', size=(2,2),
Harald Welte650f6122022-07-17 21:42:50 +0200239 table=EF_MST_map, **kwargs):
240 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size=size, table=table)
241
242class DF_MCS(CardDF):
243 def __init__(self, fid='5F3D', name='DF.MCS', desc='Mission Critical Services', **kwargs):
244 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
245 files = [
246 EF_MST(),
247 EF_MCS_CONFIG(),
248 ]
249 self.add_files(files)
Harald Welte228ae8e2022-07-17 22:01:04 +0200250
251
252# TS 31.102 Section 4.6.5.2
253EF_VST_map = {
254 1: 'MCPTT UE configuration data',
255 2: 'MCPTT User profile data',
256 3: 'MCS Group configuration data',
257 4: 'MCPTT Service configuration data',
258 5: 'MCS UE initial configuration data',
259 6: 'MCData UE configuration data',
260 7: 'MCData user profile data',
261 8: 'MCData service configuration data',
262 9: 'MCVideo UE configuration data',
263 10: 'MCVideo user profile data',
264 11: 'MCVideo service configuration data',
265 }
266
267# TS 31.102 Section 4.6.5.2
268class EF_VST(EF_UServiceTable):
Harald Welte13edf302022-07-21 15:19:23 +0200269 def __init__(self, fid='4F01', sfid=0x01, name='EF.VST', desc='V2X Service Table', size=(2,2),
Harald Welte228ae8e2022-07-17 22:01:04 +0200270 table=EF_VST_map, **kwargs):
271 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size=size, table=table)
272
273# TS 31.102 Section 4.6.5.3
274class EF_V2X_CONFIG(BerTlvEF):
275 class V2xConfigurationData(BER_TLV_IE, tag=0x80):
276 pass
277 class V2xConfigDataCollection(TLV_IE_Collection, nested=[V2xConfigurationData]):
278 pass
279 def __init__(self, fid='4F02', sfid=0x02, name='EF.V2X_CONFIG', desc='V2X configuration data', **kwargs):
280 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, **kwargs)
281 self._tlv = EF_V2X_CONFIG.V2xConfigDataCollection
282
283# TS 31.102 Section 4.6.5
284class DF_V2X(CardDF):
285 def __init__(self, fid='5F3E', name='DF.V2X', desc='Vehicle to X', **kwargs):
286 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
287 files = [
288 EF_VST(),
289 EF_V2X_CONFIG(),
290 ]
291 self.add_files(files)