blob: fadf6f938e6b877f5fb82a0dbbe7794f6f52e795 [file] [log] [blame]
Sylvain Munaut76504e02010-12-07 00:24:32 +01001# -*- coding: utf-8 -*-
2
3""" pySim: PCSC reader transport link
4"""
5
6#
7# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
8# Copyright (C) 2010 Harald Welte <laforge@gnumonks.org>
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
Vadim Yanitskiyfa617ac2020-02-26 22:33:08 +070024from smartcard.CardConnection import CardConnection
Sylvain Munautbdca2522010-12-09 13:31:58 +010025from smartcard.CardRequest import CardRequest
Daniel Willmanndd014ea2020-10-19 10:35:11 +020026from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, CardConnectionException
Sylvain Munaut76504e02010-12-07 00:24:32 +010027from smartcard.System import readers
Sylvain Munaut76504e02010-12-07 00:24:32 +010028
Daniel Willmanndd014ea2020-10-19 10:35:11 +020029from pySim.exceptions import NoCardError, ProtocolError
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010030from pySim.transport import LinkBase
Sylvain Munaut76504e02010-12-07 00:24:32 +010031from pySim.utils import h2i, i2h
32
33
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010034class PcscSimLink(LinkBase):
Sylvain Munaut76504e02010-12-07 00:24:32 +010035
Sylvain Munaut245d5342010-12-08 22:42:07 +010036 def __init__(self, reader_number=0):
Daniel Willmann677d41b2020-10-19 10:34:31 +020037 r = readers()
Sylvain Munautbdca2522010-12-09 13:31:58 +010038 self._reader = r[reader_number]
39 self._con = self._reader.createConnection()
Sylvain Munaut76504e02010-12-07 00:24:32 +010040
41 def __del__(self):
42 self._con.disconnect()
43 return
44
Sylvain Munautbdca2522010-12-09 13:31:58 +010045 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:
Vadim Yanitskiyfa617ac2020-02-26 22:33:08 +070055 # Explicitly select T=0 communication protocol
56 self._con.connect(CardConnection.T0_protocol)
57 except CardConnectionException:
58 raise ProtocolError()
Sylvain Munautbdca2522010-12-09 13:31:58 +010059 except NoCardException:
60 raise NoCardError()
61
Alexander Chemerisd2d660a2017-07-18 16:52:25 +030062 def get_atr(self):
63 return self._con.getATR()
64
Sylvain Munautbdca2522010-12-09 13:31:58 +010065 def disconnect(self):
66 self._con.disconnect()
67
Sylvain Munaut76504e02010-12-07 00:24:32 +010068 def reset_card(self):
Vadim Yanitskiy41c22172020-02-26 22:18:32 +070069 self.disconnect()
70 self.connect()
Sylvain Munaut76504e02010-12-07 00:24:32 +010071 return 1
72
73 def send_apdu_raw(self, pdu):
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010074 """see LinkBase.send_apdu_raw"""
Sylvain Munaut76504e02010-12-07 00:24:32 +010075
Sylvain Munaut76504e02010-12-07 00:24:32 +010076 apdu = h2i(pdu)
77
78 data, sw1, sw2 = self._con.transmit(apdu)
79
80 sw = [sw1, sw2]
81
82 # Return value
83 return i2h(data), i2h(sw)