blob: 93cd20292a9a0dfcff88d1441769bc2c95a993a4 [file] [log] [blame]
Harald Welte5a89cbe2019-11-12 15:24:35 +01001#!/usr/bin/python3
2
3# This script checks your libccid configuration file if it contains a matching entry
4# for the sysmoOCTSIM reader. If not, it will generate a modified config file
5
6import plistlib, sys
7
8INFILE="/etc/libccid_Info.plist"
9OUTFILE="/tmp/libccid_Info.plist"
10
11VENDOR_ID=0x1d50
12PRODUCT_ID=0x6141
13NAME='sysmocom sysmoOCTSIM'
14
15def gen_reader_dictlist(prod_id, vend_id, names):
16 readers = []
17 for i in range(0,len(prod_id)):
18 reader = {'vendor_id': vend_id[i], 'product_id': prod_id[i], 'name': names[i]}
19 readers.append(reader)
20 return readers
21
22def find_reader(readers, vend_id, prod_id):
23 for r in readers:
24 if int(r['vendor_id'], 16) == vend_id and int(r['product_id'], 16) == prod_id:
25 return r
26 return None
27
28def plist_add_reader(pl, vend_id, prod_id, name):
29 pl['ifdVendorID'].append(hex(vend_id))
30 pl['ifdProductID'].append(hex(prod_id))
31 pl['ifdFriendlyName'].append(name)
32
33
34if len(sys.argv) > 1:
35 INFILE = sys.argv[1]
36if len(sys.argv) > 2:
37 OUTFILE = sys.argv[2]
38
39# read the property list
40print("Reading libccid config file at '%s'" % (INFILE))
41with open(INFILE, 'rb') as fp:
42 pl = plistlib.load(fp)
43
44# consistency check
45if len(pl['ifdProductID']) != len(pl['ifdVendorID']) or len(pl['ifdProductID']) != len(pl['ifdFriendlyName']):
46 print("input file is corrupt", file=sys.stderr)
47 sys.exit(2)
48
49# convert into a better sorted form (one list of dicts; each dict one reader)
50readers = gen_reader_dictlist(pl['ifdProductID'], pl['ifdVendorID'], pl['ifdFriendlyName'])
51
52if find_reader(readers, VENDOR_ID, PRODUCT_ID):
53 print("Matching reader already in libccid_Info.plist; no action required", file=sys.stderr)
54else:
55 print("Reader not found in config file, it needs to be updated...")
56 plist_add_reader(pl, VENDOR_ID, PRODUCT_ID, NAME)
57 with open(OUTFILE, 'wb') as fp:
58 plistlib.dump(pl, fp)
59 print("Generated new config file stored as '%s'" % (OUTFILE))
60 print("\tWARNING: The generated file doesn't preserve comments!")