Skip to content

Commit c46a4eb

Browse files
committed
pySim-prog: Use CSV format with headers
This way we can have optional fields like pin_adm in the file Also require iccid as identifier for the SIM card Set defaults for optional card parameters. Change-Id: I0d317ea51d0cf582b82157eec6cdec074001a236
1 parent 7d38d74 commit c46a4eb

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

csv-format

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
This file aims to describe the format of the CSV file pySim uses.
2+
3+
The first line contains the fieldnames which will be used by pySim. This
4+
avoids having a specific order.
5+
6+
The field names are the following:
7+
8+
iccid: ICCID of the card. Used to identify the cards (with --read-iccid)
9+
imsi: IMSI of the card
10+
mcc: Mobile Country Code (optional)
11+
mnc: Mobile Network Code (optional)
12+
smsp: MSISDN of the SMSC (optional)
13+
ki: Ki
14+
opc: OPc
15+
acc: Access class of the SIM (optional)
16+
pin_adm: Admin PIN of the SIM. Needed to reprogram various files

pySim-prog.py

+28-15
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,20 @@ def gen_parameters(opts):
405405

406406
def print_parameters(params):
407407

408-
print """Generated card parameters :
409-
> Name : %(name)s
410-
> SMSP : %(smsp)s
411-
> ICCID : %(iccid)s
412-
> MCC/MNC : %(mcc)d/%(mnc)d
413-
> IMSI : %(imsi)s
414-
> Ki : %(ki)s
415-
> OPC : %(opc)s
416-
> ACC : %(acc)s
417-
> ADM1(hex): %(pin_adm)s
418-
""" % params
408+
s = ["Generated card parameters :"]
409+
if 'name' in params:
410+
s.append(" > Name : %(name)s")
411+
if 'smsp' in params:
412+
s.append(" > SMSP : %(smsp)s")
413+
s.append(" > ICCID : %(iccid)s")
414+
s.append(" > MCC/MNC : %(mcc)d/%(mnc)d")
415+
s.append(" > IMSI : %(imsi)s")
416+
s.append(" > Ki : %(ki)s")
417+
s.append(" > OPC : %(opc)s")
418+
if 'acc' in params:
419+
s.append(" > ACC : %(acc)s")
420+
s.append(" > ADM1(hex): %(pin_adm)s")
421+
print("\n".join(s) % params)
419422

420423

421424
def write_params_csv(opts, params):
@@ -430,10 +433,11 @@ def write_params_csv(opts, params):
430433

431434
def _read_params_csv(opts, imsi):
432435
import csv
433-
row = ['name', 'iccid', 'mcc', 'mnc', 'imsi', 'smsp', 'ki', 'opc']
434436
f = open(opts.read_csv, 'r')
435-
cr = csv.DictReader(f, row)
437+
cr = csv.DictReader(f)
436438
i = 0
439+
if not 'iccid' in cr.fieldnames:
440+
raise Exception("CSV file in wrong format!")
437441
for row in cr:
438442
if opts.num is not None and opts.read_imsi is False:
439443
if opts.num == i:
@@ -450,8 +454,17 @@ def _read_params_csv(opts, imsi):
450454
def read_params_csv(opts, imsi):
451455
row = _read_params_csv(opts, imsi)
452456
if row is not None:
453-
row['mcc'] = int(row['mcc'])
454-
row['mnc'] = int(row['mnc'])
457+
row['mcc'] = int(row.get('mcc', row['imsi'][0:3]))
458+
row['mnc'] = int(row.get('mnc', row['imsi'][3:5]))
459+
pin_adm = None
460+
# We need to escape the pin_adm we get from the csv
461+
if 'pin_adm' in row:
462+
pin_adm = ''.join(['%02x'%(ord(x)) for x in row['pin_adm']])
463+
# Stay compatible to the odoo csv format
464+
elif 'adm1' in row:
465+
pin_adm = ''.join(['%02x'%(ord(x)) for x in row['adm1']])
466+
if pin_adm:
467+
row['pin_adm'] = rpad(pin_adm, 16)
455468
return row
456469

457470

0 commit comments

Comments
 (0)