Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | """ pySim: PCSC reader transport link |
| 5 | """ |
| 6 | |
| 7 | # |
| 8 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
| 9 | # Copyright (C) 2010 Harald Welte <laforge@gnumonks.org> |
| 10 | # |
| 11 | # This program is free software: you can redistribute it and/or modify |
| 12 | # it under the terms of the GNU General Public License as published by |
| 13 | # the Free Software Foundation, either version 2 of the License, or |
| 14 | # (at your option) any later version. |
| 15 | # |
| 16 | # This program is distributed in the hope that it will be useful, |
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | # GNU General Public License for more details. |
| 20 | # |
| 21 | # You should have received a copy of the GNU General Public License |
| 22 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | # |
| 24 | |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 25 | from smartcard.CardRequest import CardRequest |
| 26 | from smartcard.Exceptions import NoCardException, CardRequestTimeoutException |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 27 | from smartcard.System import readers |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 28 | |
| 29 | from pySim.exceptions import NoCardError |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 30 | from pySim.transport import LinkBase |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 31 | from pySim.utils import h2i, i2h |
| 32 | |
| 33 | |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 34 | class PcscSimLink(LinkBase): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 35 | |
Sylvain Munaut | 245d534 | 2010-12-08 22:42:07 +0100 | [diff] [blame] | 36 | def __init__(self, reader_number=0): |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 37 | r = readers(); |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 38 | self._reader = r[reader_number] |
| 39 | self._con = self._reader.createConnection() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 40 | |
| 41 | def __del__(self): |
| 42 | self._con.disconnect() |
| 43 | return |
| 44 | |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 45 | def wait_for_card(self, timeout=None, newcardonly=False): |
| 46 | cr = CardRequest(readers=[self._reader], timeout=timeout, newcardonly=newcardonly) |
| 47 | try: |
| 48 | cr.waitforcard() |
| 49 | except CardRequestTimeoutException: |
| 50 | raise NoCardError() |
| 51 | self.connect() |
| 52 | |
| 53 | def connect(self): |
| 54 | try: |
| 55 | self._con.connect() |
| 56 | except NoCardException: |
| 57 | raise NoCardError() |
| 58 | |
Alexander Chemeris | d2d660a | 2017-07-18 16:52:25 +0300 | [diff] [blame] | 59 | def get_atr(self): |
| 60 | return self._con.getATR() |
| 61 | |
Sylvain Munaut | bdca252 | 2010-12-09 13:31:58 +0100 | [diff] [blame] | 62 | def disconnect(self): |
| 63 | self._con.disconnect() |
| 64 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 65 | def reset_card(self): |
Vadim Yanitskiy | 41c2217 | 2020-02-26 22:18:32 +0700 | [diff] [blame^] | 66 | self.disconnect() |
| 67 | self.connect() |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 68 | return 1 |
| 69 | |
| 70 | def send_apdu_raw(self, pdu): |
Sylvain Munaut | e7c15cd | 2010-12-07 10:01:55 +0100 | [diff] [blame] | 71 | """see LinkBase.send_apdu_raw""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 72 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 73 | apdu = h2i(pdu) |
| 74 | |
| 75 | data, sw1, sw2 = self._con.transmit(apdu) |
| 76 | |
| 77 | sw = [sw1, sw2] |
| 78 | |
| 79 | # Return value |
| 80 | return i2h(data), i2h(sw) |