Advertisement
Guest User

ccid-nfc.py

a guest
Jan 12th, 2012
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.40 KB | None | 0 0
  1. import usb
  2. import time
  3.  
  4. dh = None
  5.  
  6. for bus in usb.busses():
  7.     for dev in bus.devices:
  8.         if (dev.idVendor, dev.idProduct) == (0x072f, 0x2200):
  9.             print "trying reader at usb port {0}:{1}"\
  10.                 .format(bus.dirname, dev.filename)
  11.  
  12.             if False:
  13.                 print "Device:", dev.filename
  14.                 print "  Device class:",dev.deviceClass
  15.                 print "  Device sub class:",dev.deviceSubClass
  16.                 print "  Device protocol:",dev.deviceProtocol
  17.                 print "  Max packet size:",dev.maxPacketSize
  18.                 print "  idVendor: %d (0x%04x)" % (dev.idVendor, dev.idVendor)
  19.                 print "  idProduct: %d (0x%04x)" % (dev.idProduct, dev.idProduct)
  20.                 print "  Device Version:",dev.deviceVersion
  21.                 for config in dev.configurations:
  22.                     print "  Configuration:", config.value
  23.                     print "    Total length:", config.totalLength
  24.                     print "    selfPowered:", config.selfPowered
  25.                     print "    remoteWakeup:", config.remoteWakeup
  26.                     print "    maxPower:", config.maxPower
  27.                     for intf in config.interfaces:
  28.                         print "    Interface:",intf[0].interfaceNumber
  29.                         for alt in intf:
  30.                             print "    Alternate Setting:",alt.alternateSetting
  31.                             print "      Interface class:",alt.interfaceClass
  32.                             print "      Interface sub class:",alt.interfaceSubClass
  33.                             print "      Interface protocol:",alt.interfaceProtocol
  34.                             for ep in alt.endpoints:
  35.                                 print "      Endpoint:",hex(ep.address)
  36.                                 print "        Type:",ep.type
  37.                                 print "        Max packet size:",ep.maxPacketSize
  38.                                 print "        Interval:",ep.interval
  39.  
  40.             configuration = dev.configurations[0]
  41.             interface = configuration.interfaces[0]
  42.             for ep in interface[0].endpoints:
  43.                 if ep.type == usb.ENDPOINT_TYPE_BULK:
  44.                     if ep.address & usb.ENDPOINT_DIR_MASK == usb.ENDPOINT_IN:
  45.                         usb_inp = ep.address
  46.                     else:
  47.                         usb_out = ep.address
  48.  
  49.             dh = dev.open()
  50.             dh.reset()
  51.             #dh.setConfiguration(configuration)
  52.             dh.claimInterface(interface[0])
  53.             dh.setAltInterface(interface[0])
  54.             dh.getDescriptor(usb.DT_CONFIG, 0, 512)
  55.             #try: dh.interruptRead(0x81, 100, 5000)
  56.             #except: pass
  57.  
  58. if dh is not None:
  59.     seq = -1
  60.     #get_parameters = bytearray("\x6C\x00\x00\x00\x00\x00\x00\x00\x00\x00")
  61.     #dh.bulkWrite(usb_out, get_parameters, 100)
  62.  
  63.     if 1:
  64.         # CCID GetSlotStatus
  65.         seq = (seq + 1) % 256
  66.         cmd = bytearray("\x65\x00\x00\x00\x00\x00"+chr(seq)+"\x00\x00\x00")
  67.         print ">>> " + " ".join(["{0:02x}".format(b) for b in cmd])
  68.         dh.bulkWrite(usb_out, cmd, 1000)
  69.         rsp = dh.bulkRead(usb_inp, 300, 1000)
  70.         print "<<< " + " ".join(["{0:02x}".format(b) for b in rsp])
  71.  
  72.     if False:
  73.         # CCID IccPowerOff
  74.         seq = (seq + 1) % 256
  75.         cmd = bytearray("\x63\x00\x00\x00\x00\x00"+chr(seq)+"\x00\x00\x00")
  76.         print ">>> " + " ".join(["{0:02x}".format(b) for b in cmd])
  77.         dh.bulkWrite(usb_out, cmd, 1000)
  78.         rsp = dh.bulkRead(usb_inp, 300, 1000)
  79.         print "<<< " + " ".join(["{0:02x}".format(b) for b in rsp])
  80.  
  81.     if False:
  82.         # CCID IccPowerOn
  83.         seq = (seq + 1) % 256
  84.         cmd = bytearray("\x62\x00\x00\x00\x00\x00"+chr(seq)+"\x00\x00\x00")
  85.         print ">>> " + " ".join(["{0:02x}".format(b) for b in cmd])
  86.         dh.bulkWrite(usb_out, cmd, 1000)
  87.         rsp = dh.bulkRead(usb_inp, 300, 1000)
  88.         print "<<< " + " ".join(["{0:02x}".format(b) for b in rsp])
  89.  
  90.     if False:
  91.         # PN532 GetFirmware
  92.         seq = (seq + 1) % 256
  93.         cmd = bytearray("\x6B\x07\x00\x00\x00\x00"+chr(seq)+"\x00\x00\x00")
  94.         cmd += bytearray("\xFF\x00\x00\x00\x02" + "\xD4\x02")
  95.         print ">>> " + " ".join(["{0:02x}".format(b) for b in cmd])
  96.         dh.bulkWrite(usb_out, cmd, 1000)
  97.         rsp = dh.bulkRead(usb_inp, 300, 1000)
  98.         print "<<< " + " ".join(["{0:02x}".format(b) for b in rsp])
  99.  
  100.     if 1:
  101.         # PN532 InListPassiveTarget (FeliCa)
  102.         seq = (seq + 1) % 256
  103.         cmd = "d44a010100ffff0103".decode("hex")
  104.         cmd = "\xFF\x00\x00\x00"+chr(len(cmd))+cmd
  105.         cmd = "\x6B"+chr(len(cmd))+"\x00\x00\x00\x00"+chr(seq)+"\x00\x00\x00"+cmd
  106.         cmd = bytearray(cmd)
  107.         print ">>> " + " ".join(["{0:02x}".format(b) for b in cmd])
  108.         dh.bulkWrite(usb_out, cmd, 1000)
  109.         rsp = bytearray(dh.bulkRead(usb_inp, 300, 1000))
  110.         print "<<< " + " ".join(["{0:02x}".format(b) for b in rsp])
  111.  
  112.     if False:
  113.         # PN532 RFParameters (RF off)
  114.         seq = (seq + 1) % 256
  115.         cmd = "FF00000004D4320100".decode("hex")
  116.         cmd = "\x6B"+chr(len(cmd))+"\x00\x00\x00\x00"+chr(seq)+"\x00\x00\x00"+cmd
  117.         cmd = bytearray(cmd)
  118.         print ">>> " + " ".join(["{0:02x}".format(b) for b in cmd])
  119.         dh.bulkWrite(usb_out, cmd, 1000)
  120.         rsp = bytearray(dh.bulkRead(usb_inp, 300, 1000))
  121.         print "<<< " + " ".join(["{0:02x}".format(b) for b in rsp])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement