blob: 1055a3d0d574a2e41d71f9ad89de69afd49caaf8 [file] [log] [blame]
Christina Quastf2582fc2015-03-06 19:09:35 +01001#!/usr/bin/env python
2
3import argparse
4import sniffer
5import ccid
Christina Quast31b5b4e2015-03-09 17:15:13 +01006import ccid_select
Christina Quastf2582fc2015-03-06 19:09:35 +01007
8import usb.core
9import usb.util
10
11import hashlib
12import os
13import random
14import re
15
16cmd1 = {0x00, 0x10, 0x00, 0x00}
17cmd2 = {0x00, 0x20, 0x00, 0x00, 0x02}
18cmd_poweron = {0x62, 0x62, 0x00, 0x00}
19cmd_poweroff = {0x63, 0x63, 0x00, 0x00}
20cmd_get_slot_stat = {0x65, 0x65, 0x00, 0x00}
21cmd_get_param = {0x00, 0x6C, 0x00, 0x00}
22
23class find_class(object):
24 def __init__(self, class_):
25 self._class = class_
26 def __call__(self, device):
27 # first, let's check the device
28 if device.bDeviceClass == self._class:
29 return True
30 # ok, transverse all devices to find an
31 # interface that matches our class
32 for cfg in device:
33 # find_descriptor: what's it?
34 intf = usb.util.find_descriptor(
35 cfg,
36 bInterfaceClass=self._class
37 )
38 if intf is not None:
39 return True
40
41 return False
42
43
44# main code
45def main():
46 parser = argparse.ArgumentParser()
47 parser.add_argument("-C", "--conf", type=int, choices=[1, 2, 3], help="Set USB config")
48 parser.add_argument("-b", "--read_bin", help="read ICCID, IMSI, etc.", action='store_true')
49 parser.add_argument("-c", "--cmd", help="cmds to send to sim card (Not supported yet)",
50 choices=["cmd1", "cmd2", "cmd_poweron", "cmd_poweroff", "cmd_get_slot_stat", "cmd_get_param"])
51 parser.add_argument("-s", "--sniff", help="Sniff communication!", action='store_true')
Christina Quast31b5b4e2015-03-09 17:15:13 +010052 parser.add_argument("-S", "--select_file", help="Transmit SELECT cmd!", action='store_true')
Christina Quastf2582fc2015-03-06 19:09:35 +010053
54 args = parser.parse_args()
55 print("args: ", args)
56
57# FIXME: why is it a ccid function?
58 if args.conf is not None:
59 devs = usb.core.find(find_all=1, custom_match=find_class(0xb)) # 0xb = Smartcard
60 for dev in devs:
61 dev.set_configuration(args.conf)
Christina Quast31b5b4e2015-03-09 17:15:13 +010062# ret = dev.read(0x83, 64, 100)
Christina Quastf2582fc2015-03-06 19:09:35 +010063
64 if args.read_bin is True:
65 ccid.pySim_read()
66
67 if args.cmd is not None:
68 devs = usb.core.find(find_all=1, custom_match=find_class(0xb)) # 0xb = Smartcard
69 for dev in devs:
70 dev.write(0x1, args.cmd)
71 ret = dev.read(0x82, 64)
Christina Quast31b5b4e2015-03-09 17:15:13 +010072# ret = dev.read(0x83, 64, 100)
Christina Quastf2582fc2015-03-06 19:09:35 +010073 print(ret)
74 if args.sniff is True:
75 sniffer.sniff()
Christina Quast31b5b4e2015-03-09 17:15:13 +010076 if args.select_file is True:
77 ccid_select.select()
78
Christina Quastf2582fc2015-03-06 19:09:35 +010079 return
80
81# (epi, epo) = find_eps(dev)
82 while True:
83 #ep_out.write("Hello")
84 try:
85 ans = dev.read(0x82, 64, 1000)
86 print("".join("%02x " % b for b in ans))
87 except KeyboardInterrupt:
88 print("Bye")
89 sys.exit()
90 except:
91 print("Timeout")
92 # print(ep_in.read(1, 5000));
93
94main()