Harald Welte | 5a89cbe | 2019-11-12 15:24:35 +0100 | [diff] [blame] | 1 | #!/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 | |
| 6 | import plistlib, sys |
| 7 | |
| 8 | INFILE="/etc/libccid_Info.plist" |
| 9 | OUTFILE="/tmp/libccid_Info.plist" |
| 10 | |
| 11 | VENDOR_ID=0x1d50 |
| 12 | PRODUCT_ID=0x6141 |
| 13 | NAME='sysmocom sysmoOCTSIM' |
| 14 | |
| 15 | def 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 | |
| 22 | def 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 | |
| 28 | def 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 | |
| 34 | if len(sys.argv) > 1: |
| 35 | INFILE = sys.argv[1] |
| 36 | if len(sys.argv) > 2: |
| 37 | OUTFILE = sys.argv[2] |
| 38 | |
| 39 | # read the property list |
| 40 | print("Reading libccid config file at '%s'" % (INFILE)) |
| 41 | with open(INFILE, 'rb') as fp: |
| 42 | pl = plistlib.load(fp) |
| 43 | |
| 44 | # consistency check |
| 45 | if 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) |
| 50 | readers = gen_reader_dictlist(pl['ifdProductID'], pl['ifdVendorID'], pl['ifdFriendlyName']) |
| 51 | |
| 52 | if find_reader(readers, VENDOR_ID, PRODUCT_ID): |
| 53 | print("Matching reader already in libccid_Info.plist; no action required", file=sys.stderr) |
| 54 | else: |
| 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!") |