aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhploetz <hploetz@f711b948-2313-0410-aaa9-d29f33439f0b>2006-08-24 07:02:05 +0000
committerhploetz <hploetz@f711b948-2313-0410-aaa9-d29f33439f0b>2006-08-24 07:02:05 +0000
commit97af2bd2eda9e06e3c7017bbc234c153885cd9a0 (patch)
treee1ba08e826b61fa6cfbf478be3a9f03988ef7f71
parentdce8c9f90fe3d4e5b931af9c4f78af8bbe3f9d81 (diff)
More comfortable interface to brutefid
git-svn-id: svn+ssh://localhost/home/henryk/svn/cyberflex-shell/trunk@112 f711b948-2313-0410-aaa9-d29f33439f0b
-rwxr-xr-xbrutefid.py81
-rw-r--r--cyberflex-shell.e3p2
2 files changed, 78 insertions, 5 deletions
diff --git a/brutefid.py b/brutefid.py
index ab2e16d..a761388 100755
--- a/brutefid.py
+++ b/brutefid.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
-import pycsc, utils, cards, TLV_utils, sys, binascii, time
+import pycsc, utils, cards, TLV_utils, sys, binascii, time, getopt
STATUS_INTERVAL = 10
@@ -11,11 +11,84 @@ top_level = None
start_time = time.time()
loop = 0
+OPTIONS = "r:l"
+LONG_OPTIONS = ["reader=", "list-readers"]
+exit_now = False
+reader = None
+
+def list_readers():
+ for index, name in enumerate(pycsc.listReader()):
+ print "%i: %s" % (index, name)
+
+def connect(reader = None):
+ "Open the connection to a card"
+
+ if reader is None:
+ reader = 0
+
+ if isinstance(reader, int) or reader.isdigit():
+ reader = int(reader)
+ readerName = pycsc.listReader()[reader]
+ else:
+ readerName = reader
+
+ newState = pycsc.getStatusChange(ReaderStates=[
+ {'Reader': readerName, 'CurrentState':pycsc.SCARD_STATE_UNAWARE}
+ ]
+ )
+
+ print "Using reader: %s" % readerName
+ print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
+
+ if not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT:
+ print "Please insert card ..."
+
+ last_was_mute = False
+
+ while not newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT \
+ or newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
+
+ try:
+ newState = pycsc.getStatusChange(ReaderStates=[
+ {'Reader': readerName, 'CurrentState':newState[0]['EventState']}
+ ], Timeout = 100
+ ) ## 100 ms latency from Ctrl-C to abort should be almost unnoticeable by the user
+ except pycsc.PycscException, e:
+ if e.args[0] == 'Command timeout.': pass ## ugly
+ else: raise
+
+ if newState[0]['EventState'] & pycsc.SCARD_STATE_MUTE:
+ if not last_was_mute:
+ print "Card is mute, please retry ..."
+ last_was_mute = True
+ else:
+ last_was_mute = False
+
+ print "Card present: %s" % ((newState[0]['EventState'] & pycsc.SCARD_STATE_PRESENT) and "yes" or "no")
+
+ print "ATR: %s" % utils.hexdump(newState[0]['Atr'], short = True)
+ return pycsc.pycsc(reader = readerName, protocol = pycsc.SCARD_PROTOCOL_ANY)
+
+
if __name__ == "__main__":
- if len(sys.argv) > 1:
- top_level = binascii.unhexlify("".join( sys.argv[1].split() ))
- pycsc_card = pycsc.pycsc(protocol = pycsc.SCARD_PROTOCOL_ANY)
+ (options, arguments) = getopt.gnu_getopt(sys.argv[1:], OPTIONS, LONG_OPTIONS)
+
+ for (option, value) in options:
+ if option in ("-r","--reader"):
+ reader = value
+ if option in ("-l","--list-readers"):
+ list_readers()
+ exit_now = True
+
+ if exit_now:
+ sys.exit()
+ del exit_now
+
+ if len(arguments) > 0:
+ top_level = binascii.unhexlify("".join( ["".join(e.split()) for e in arguments] ))
+
+ pycsc_card = connect(reader)
card = cards.new_card_object(pycsc_card)
cards.generic_card.DEBUG = False
diff --git a/cyberflex-shell.e3p b/cyberflex-shell.e3p
index 92bf4dd..2c5722a 100644
--- a/cyberflex-shell.e3p
+++ b/cyberflex-shell.e3p
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Project SYSTEM "Project-3.7.dtd">
<!-- Project file for project cyberflex-shell -->
-<!-- Saved: 2006-07-18, 13:42:27 -->
+<!-- Saved: 2006-07-24, 17:00:49 -->
<!-- Copyright (C) 2006 Henryk Plötz, henryk@ploetzli.ch -->
<Project version="3.7">
<ProgLanguage mixed="0">Python</ProgLanguage>