blob: 575ccaed04048034c03e43ef00f713c6f16be6ae [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
Alexander Couzens6c5c3f82023-07-28 05:13:06 +0200123 def ust_update(self, cmd, activate=[], deactivate=[]):
124 service_data, sw = cmd.lchan.read_binary()
125 service_data = h2b(service_data)
Harald Welte6f8a8702022-07-17 21:50:31 +0200126
Alexander Couzens6c5c3f82023-07-28 05:13:06 +0200127 for service in activate:
128 nbyte, nbit = EF_UServiceTable._bit_byte_offset_for_service(service)
129 if nbyte > len(service_data):
130 missing = nbyte - service_data
131 service_data.extend(missing * "00")
132 service_data[nbyte] |= (1 << nbit)
133
134 for service in deactivate:
135 nbyte, nbit = EF_UServiceTable._bit_byte_offset_for_service(service)
136 if nbyte > len(service_data):
137 missing = nbyte - service_data
138 service_data.extend(missing * "00")
139 service_data[nbyte] &= ~(1 << nbit)
140
141 service_data = b2h(service_data)
142 cmd.lchan.update_binary(service_data)
Harald Welte6f8a8702022-07-17 21:50:31 +0200143
Harald Weltede4c14c2022-07-16 11:53:59 +0200144# TS 31.102 Section 4.4.2.1
145class EF_PBR(LinFixedEF):
Harald Welte865eea62023-01-27 19:26:12 +0100146 # TODO: a80ac0034f3a02c5034f0904aa0acb034f3d07c2034f4a06
Harald Weltede4c14c2022-07-16 11:53:59 +0200147 def __init__(self, fid='4F30', name='EF.PBR', desc='Phone Book Reference', **kwargs):
148 super().__init__(fid, name=name, desc=desc, **kwargs)
149 #self._tlv = FIXME
150
151# TS 31.102 Section 4.4.2.12.2
152class EF_PSC(TransparentEF):
153 _construct = Struct('synce_counter'/Int32ub)
154 def __init__(self, fid='4F22', name='EF.PSC', desc='Phone Book Synchronization Counter', **kwargs):
155 super().__init__(fid, name=name, desc=desc, **kwargs)
156 #self._tlv = FIXME
157
158# TS 31.102 Section 4.4.2.12.3
159class EF_CC(TransparentEF):
160 _construct = Struct('change_counter'/Int16ub)
161 def __init__(self, fid='4F23', name='EF.CC', desc='Change Counter', **kwargs):
162 super().__init__(fid, name=name, desc=desc, **kwargs)
163
164# TS 31.102 Section 4.4.2.12.4
165class EF_PUID(TransparentEF):
166 _construct = Struct('previous_uid'/Int16ub)
167 def __init__(self, fid='4F24', name='EF.PUID', desc='Previous Unique Identifer', **kwargs):
168 super().__init__(fid, name=name, desc=desc, **kwargs)
169
170# TS 31.102 Section 4.4.2
171class DF_PHONEBOOK(CardDF):
172 def __init__(self, fid='5F3A', name='DF.PHONEBOOK', desc='Phonebook', **kwargs):
173 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
174 files = [
175 EF_PBR(),
176 EF_PSC(),
177 EF_CC(),
178 EF_PUID(),
179 # FIXME: Those 4Fxx entries with unspecified FID...
180 ]
181 self.add_files(files)
Harald Weltea0452212022-07-17 21:23:21 +0200182
183
184
185# TS 31.102 Section 4.6.3.1
186class EF_MML(BerTlvEF):
187 def __init__(self, fid='4F47', name='EF.MML', desc='Multimedia Messages List', **kwargs):
188 super().__init__(fid, name=name, desc=desc, **kwargs)
189
190# TS 31.102 Section 4.6.3.2
191class EF_MMDF(BerTlvEF):
192 def __init__(self, fid='4F48', name='EF.MMDF', desc='Multimedia Messages Data File', **kwargs):
193 super().__init__(fid, name=name, desc=desc, **kwargs)
194
195class DF_MULTIMEDIA(CardDF):
196 def __init__(self, fid='5F3B', name='DF.MULTIMEDIA', desc='Multimedia', **kwargs):
197 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
198 files = [
199 EF_MML(),
200 EF_MMDF(),
201 ]
202 self.add_files(files)
Harald Welte650f6122022-07-17 21:42:50 +0200203
204
205# TS 31.102 Section 4.6.4.1
206EF_MST_map = {
207 1: 'MCPTT UE configuration data',
208 2: 'MCPTT User profile data',
209 3: 'MCS Group configuration data',
210 4: 'MCPTT Service configuration data',
211 5: 'MCS UE initial configuration data',
212 6: 'MCData UE configuration data',
213 7: 'MCData user profile data',
214 8: 'MCData service configuration data',
215 9: 'MCVideo UE configuration data',
216 10: 'MCVideo user profile data',
217 11: 'MCVideo service configuration data',
218 }
219
220# TS 31.102 Section 4.6.4.2
221class EF_MCS_CONFIG(BerTlvEF):
222 class McpttUeConfigurationData(BER_TLV_IE, tag=0x80):
223 pass
224 class McpttUserProfileData(BER_TLV_IE, tag=0x81):
225 pass
226 class McsGroupConfigurationData(BER_TLV_IE, tag=0x82):
227 pass
228 class McpttServiceConfigurationData(BER_TLV_IE, tag=0x83):
229 pass
230 class McsUeInitialConfigurationData(BER_TLV_IE, tag=0x84):
231 pass
232 class McdataUeConfigurationData(BER_TLV_IE, tag=0x85):
233 pass
234 class McdataUserProfileData(BER_TLV_IE, tag=0x86):
235 pass
236 class McdataServiceConfigurationData(BER_TLV_IE, tag=0x87):
237 pass
238 class McvideoUeConfigurationData(BER_TLV_IE, tag=0x88):
239 pass
240 class McvideoUserProfileData(BER_TLV_IE, tag=0x89):
241 pass
242 class McvideoServiceConfigurationData(BER_TLV_IE, tag=0x8a):
243 pass
244 class McsConfigDataCollection(TLV_IE_Collection, nested=[McpttUeConfigurationData,
245 McpttUserProfileData, McsGroupConfigurationData,
246 McpttServiceConfigurationData, McsUeInitialConfigurationData,
247 McdataUeConfigurationData, McdataUserProfileData,
248 McdataServiceConfigurationData, McvideoUeConfigurationData,
249 McvideoUserProfileData, McvideoServiceConfigurationData]):
250 pass
251 def __init__(self, fid='4F02', sfid=0x02, name='EF.MCS_CONFIG', desc='MCS configuration data', **kwargs):
252 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, **kwargs)
253 self._tlv = EF_MCS_CONFIG.McsConfigDataCollection
254
255# TS 31.102 Section 4.6.4.1
256class EF_MST(EF_UServiceTable):
Harald Welte13edf302022-07-21 15:19:23 +0200257 def __init__(self, fid='4F01', sfid=0x01, name='EF.MST', desc='MCS Service Table', size=(2,2),
Harald Welte650f6122022-07-17 21:42:50 +0200258 table=EF_MST_map, **kwargs):
259 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size=size, table=table)
260
261class DF_MCS(CardDF):
262 def __init__(self, fid='5F3D', name='DF.MCS', desc='Mission Critical Services', **kwargs):
263 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
264 files = [
265 EF_MST(),
266 EF_MCS_CONFIG(),
267 ]
268 self.add_files(files)
Harald Welte228ae8e2022-07-17 22:01:04 +0200269
270
271# TS 31.102 Section 4.6.5.2
272EF_VST_map = {
273 1: 'MCPTT UE configuration data',
274 2: 'MCPTT User profile data',
275 3: 'MCS Group configuration data',
276 4: 'MCPTT Service configuration data',
277 5: 'MCS UE initial configuration data',
278 6: 'MCData UE configuration data',
279 7: 'MCData user profile data',
280 8: 'MCData service configuration data',
281 9: 'MCVideo UE configuration data',
282 10: 'MCVideo user profile data',
283 11: 'MCVideo service configuration data',
284 }
285
286# TS 31.102 Section 4.6.5.2
287class EF_VST(EF_UServiceTable):
Harald Welte13edf302022-07-21 15:19:23 +0200288 def __init__(self, fid='4F01', sfid=0x01, name='EF.VST', desc='V2X Service Table', size=(2,2),
Harald Welte228ae8e2022-07-17 22:01:04 +0200289 table=EF_VST_map, **kwargs):
290 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size=size, table=table)
291
292# TS 31.102 Section 4.6.5.3
293class EF_V2X_CONFIG(BerTlvEF):
294 class V2xConfigurationData(BER_TLV_IE, tag=0x80):
295 pass
296 class V2xConfigDataCollection(TLV_IE_Collection, nested=[V2xConfigurationData]):
297 pass
298 def __init__(self, fid='4F02', sfid=0x02, name='EF.V2X_CONFIG', desc='V2X configuration data', **kwargs):
299 super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, **kwargs)
300 self._tlv = EF_V2X_CONFIG.V2xConfigDataCollection
301
302# TS 31.102 Section 4.6.5
303class DF_V2X(CardDF):
304 def __init__(self, fid='5F3E', name='DF.V2X', desc='Vehicle to X', **kwargs):
305 super().__init__(fid=fid, name=name, desc=desc, **kwargs)
306 files = [
307 EF_VST(),
308 EF_V2X_CONFIG(),
309 ]
310 self.add_files(files)