Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ pySim: PCSC reader transport link base |
| 5 | """ |
| 6 | |
| 7 | # |
| 8 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
| 9 | # |
| 10 | # This program is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 2 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # This program is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | # |
| 23 | |
| 24 | class LinkBase(object): |
| 25 | |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 26 | def wait_for_card(self, timeout=None, newcardonly=False): |
| 27 | """wait_for_card(): Wait for a card and connect to it |
| 28 | |
| 29 | timeout : Maximum wait time (None=no timeout) |
| 30 | newcardonly : Should we wait for a new card, or an already |
| 31 | inserted one ? |
| 32 | """ |
| 33 | pass |
| 34 | |
| 35 | def connect(self): |
| 36 | """connect(): Connect to a card immediately |
| 37 | """ |
| 38 | pass |
| 39 | |
| 40 | def disconnect(self): |
| 41 | """disconnect(): Disconnect from card |
| 42 | """ |
| 43 | pass |
| 44 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 45 | def reset_card(self): |
| 46 | """reset_card(): Resets the card (power down/up) |
| 47 | """ |
| 48 | pass |
| 49 | |
| 50 | def send_apdu_raw(self, pdu): |
| 51 | """send_apdu_raw(pdu): Sends an APDU with minimal processing |
| 52 | |
| 53 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 54 | return : tuple(data, sw), where |
| 55 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 56 | sw : string (in hex) of status word (ex. "9000") |
| 57 | """ |
| 58 | pass |
| 59 | |
| 60 | def send_apdu(self, pdu): |
| 61 | """send_apdu(pdu): Sends an APDU and auto fetch response data |
| 62 | |
| 63 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 64 | return : tuple(data, sw), where |
| 65 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 66 | sw : string (in hex) of status word (ex. "9000") |
| 67 | """ |
| 68 | data, sw = self.send_apdu_raw(pdu) |
| 69 | |
| 70 | if (sw is not None) and (sw[0:2] == '9f'): |
| 71 | pdu_gr = pdu[0:2] + 'c00000' + sw[2:4] |
| 72 | data, sw = self.send_apdu_raw(pdu_gr) |
| 73 | |
| 74 | return data, sw |
| 75 | |
| 76 | def send_apdu_checksw(self, pdu, sw="9000"): |
| 77 | """send_apdu_checksw(pdu,sw): Sends an APDU and check returned SW |
| 78 | |
| 79 | pdu : string of hexadecimal characters (ex. "A0A40000023F00") |
| 80 | sw : string of 4 hexadecimal characters (ex. "9000") |
| 81 | return : tuple(data, sw), where |
| 82 | data : string (in hex) of returned data (ex. "074F4EFFFF") |
| 83 | sw : string (in hex) of status word (ex. "9000") |
| 84 | """ |
| 85 | rv = self.send_apdu(pdu) |
| 86 | if sw.lower() != rv[1]: |
| 87 | raise RuntimeError("SW match failed ! Expected %s and got %s." % (sw.lower(), rv[1])) |
| 88 | return rv |