blob: 47c41853935c7bb36590c5b54fbb2da386ce7ae9 [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
Sylvain Munautbdca2522010-12-09 13:31:58 +010025from smartcard.CardRequest import CardRequest
26from smartcard.Exceptions import NoCardException, CardRequestTimeoutException
Sylvain Munaut76504e02010-12-07 00:24:32 +010027from smartcard.System import readers
Sylvain Munaut76504e02010-12-07 00:24:32 +010028
29from pySim.exceptions import NoCardError
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):
Sylvain Munaut76504e02010-12-07 00:24:32 +010037 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:
55 self._con.connect()
56 except NoCardException:
57 raise NoCardError()
58
Alexander Chemerisd2d660a2017-07-18 16:52:25 +030059 def get_atr(self):
60 return self._con.getATR()
61
Sylvain Munautbdca2522010-12-09 13:31:58 +010062 def disconnect(self):
63 self._con.disconnect()
64
Sylvain Munaut76504e02010-12-07 00:24:32 +010065 def reset_card(self):
66 self._con.disconnect()
67 try:
68 self._con.connect()
69 except NoCardException:
70 raise NoCardError()
71 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)