From c46a4eba43facc98666c3ce0fbb6455823d87aca Mon Sep 17 00:00:00 2001 From: Daniel Willmann Date: Fri, 15 Jun 2018 07:31:50 +0200 Subject: [PATCH] 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 --- csv-format | 16 ++++++++++++++++ pySim-prog.py | 43 ++++++++++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 csv-format diff --git a/csv-format b/csv-format new file mode 100644 index 00000000..808003a5 --- /dev/null +++ b/csv-format @@ -0,0 +1,16 @@ +This file aims to describe the format of the CSV file pySim uses. + +The first line contains the fieldnames which will be used by pySim. This +avoids having a specific order. + +The field names are the following: + +iccid: ICCID of the card. Used to identify the cards (with --read-iccid) +imsi: IMSI of the card +mcc: Mobile Country Code (optional) +mnc: Mobile Network Code (optional) +smsp: MSISDN of the SMSC (optional) +ki: Ki +opc: OPc +acc: Access class of the SIM (optional) +pin_adm: Admin PIN of the SIM. Needed to reprogram various files diff --git a/pySim-prog.py b/pySim-prog.py index 7edadd77..e00c2d01 100755 --- a/pySim-prog.py +++ b/pySim-prog.py @@ -405,17 +405,20 @@ def gen_parameters(opts): def print_parameters(params): - print """Generated card parameters : - > Name : %(name)s - > SMSP : %(smsp)s - > ICCID : %(iccid)s - > MCC/MNC : %(mcc)d/%(mnc)d - > IMSI : %(imsi)s - > Ki : %(ki)s - > OPC : %(opc)s - > ACC : %(acc)s - > ADM1(hex): %(pin_adm)s -""" % params + s = ["Generated card parameters :"] + if 'name' in params: + s.append(" > Name : %(name)s") + if 'smsp' in params: + s.append(" > SMSP : %(smsp)s") + s.append(" > ICCID : %(iccid)s") + s.append(" > MCC/MNC : %(mcc)d/%(mnc)d") + s.append(" > IMSI : %(imsi)s") + s.append(" > Ki : %(ki)s") + s.append(" > OPC : %(opc)s") + if 'acc' in params: + s.append(" > ACC : %(acc)s") + s.append(" > ADM1(hex): %(pin_adm)s") + print("\n".join(s) % params) def write_params_csv(opts, params): @@ -430,10 +433,11 @@ def write_params_csv(opts, params): def _read_params_csv(opts, imsi): import csv - row = ['name', 'iccid', 'mcc', 'mnc', 'imsi', 'smsp', 'ki', 'opc'] f = open(opts.read_csv, 'r') - cr = csv.DictReader(f, row) + cr = csv.DictReader(f) i = 0 + if not 'iccid' in cr.fieldnames: + raise Exception("CSV file in wrong format!") for row in cr: if opts.num is not None and opts.read_imsi is False: if opts.num == i: @@ -450,8 +454,17 @@ def _read_params_csv(opts, imsi): def read_params_csv(opts, imsi): row = _read_params_csv(opts, imsi) if row is not None: - row['mcc'] = int(row['mcc']) - row['mnc'] = int(row['mnc']) + row['mcc'] = int(row.get('mcc', row['imsi'][0:3])) + row['mnc'] = int(row.get('mnc', row['imsi'][3:5])) + pin_adm = None + # We need to escape the pin_adm we get from the csv + if 'pin_adm' in row: + pin_adm = ''.join(['%02x'%(ord(x)) for x in row['pin_adm']]) + # Stay compatible to the odoo csv format + elif 'adm1' in row: + pin_adm = ''.join(['%02x'%(ord(x)) for x in row['adm1']]) + if pin_adm: + row['pin_adm'] = rpad(pin_adm, 16) return row