blob: 7b5086971a6f1a005ff144f3b52d578be816ef34 [file] [log] [blame]
Sylvain Munaut76504e02010-12-07 00:24:32 +01001#!/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
Vadim Yanitskiyfa617ac2020-02-26 22:33:08 +070025from smartcard.CardConnection import CardConnection
Sylvain Munautbdca2522010-12-09 13:31:58 +010026from smartcard.CardRequest import CardRequest
Daniel Willmanndd014ea2020-10-19 10:35:11 +020027from smartcard.Exceptions import NoCardException, CardRequestTimeoutException, CardConnectionException
Sylvain Munaut76504e02010-12-07 00:24:32 +010028from smartcard.System import readers
Sylvain Munaut76504e02010-12-07 00:24:32 +010029
Daniel Willmanndd014ea2020-10-19 10:35:11 +020030from pySim.exceptions import NoCardError, ProtocolError
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010031from pySim.transport import LinkBase
Sylvain Munaut76504e02010-12-07 00:24:32 +010032from pySim.utils import h2i, i2h
33
34
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010035class PcscSimLink(LinkBase):
Sylvain Munaut76504e02010-12-07 00:24:32 +010036
Sylvain Munaut245d5342010-12-08 22:42:07 +010037 def __init__(self, reader_number=0):
Daniel Willmann677d41b2020-10-19 10:34:31 +020038 r = readers()
Sylvain Munautbdca2522010-12-09 13:31:58 +010039 self._reader = r[reader_number]
40 self._con = self._reader.createConnection()
Sylvain Munaut76504e02010-12-07 00:24:32 +010041
42 def __del__(self):
43 self._con.disconnect()
44 return
45
Sylvain Munautbdca2522010-12-09 13:31:58 +010046 def wait_for_card(self, timeout=None, newcardonly=False):
47 cr = CardRequest(readers=[self._reader], timeout=timeout, newcardonly=newcardonly)
48 try:
49 cr.waitforcard()
50 except CardRequestTimeoutException:
51 raise NoCardError()
52 self.connect()
53
54 def connect(self):
55 try:
Vadim Yanitskiyfa617ac2020-02-26 22:33:08 +070056 # Explicitly select T=0 communication protocol
57 self._con.connect(CardConnection.T0_protocol)
58 except CardConnectionException:
59 raise ProtocolError()
Sylvain Munautbdca2522010-12-09 13:31:58 +010060 except NoCardException:
61 raise NoCardError()
62
Alexander Chemerisd2d660a2017-07-18 16:52:25 +030063 def get_atr(self):
64 return self._con.getATR()
65
Sylvain Munautbdca2522010-12-09 13:31:58 +010066 def disconnect(self):
67 self._con.disconnect()
68
Sylvain Munaut76504e02010-12-07 00:24:32 +010069 def reset_card(self):
Vadim Yanitskiy41c22172020-02-26 22:18:32 +070070 self.disconnect()
71 self.connect()
Sylvain Munaut76504e02010-12-07 00:24:32 +010072 return 1
73
74 def send_apdu_raw(self, pdu):
Sylvain Munaute7c15cd2010-12-07 10:01:55 +010075 """see LinkBase.send_apdu_raw"""
Sylvain Munaut76504e02010-12-07 00:24:32 +010076
Sylvain Munaut76504e02010-12-07 00:24:32 +010077 apdu = h2i(pdu)
78
79 data, sw1, sw2 = self._con.transmit(apdu)
80
81 sw = [sw1, sw2]
82
83 # Return value
84 return i2h(data), i2h(sw)