Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: Exceptions |
| 4 | """ |
| 5 | |
| 6 | # |
| 7 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 8 | # Copyright (C) 2021 Harald Welte <laforge@osmocom.org> |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 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 | |
Denis 'GNUtoo' Carikli | 8902bcd | 2019-09-12 15:03:23 +0200 | [diff] [blame] | 24 | class NoCardError(Exception): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 25 | """No card was found in the reader.""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 26 | pass |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 27 | |
Denis 'GNUtoo' Carikli | 8902bcd | 2019-09-12 15:03:23 +0200 | [diff] [blame] | 28 | class ProtocolError(Exception): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 29 | """Some kind of protocol level error interfacing with the card.""" |
Denis 'GNUtoo' Carikli | 8902bcd | 2019-09-12 15:03:23 +0200 | [diff] [blame] | 30 | pass |
| 31 | |
| 32 | class ReaderError(Exception): |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 33 | """Some kind of general error with the card reader.""" |
Vadim Yanitskiy | 1381ff1 | 2018-10-27 01:48:15 +0700 | [diff] [blame] | 34 | pass |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 35 | |
| 36 | class SwMatchError(Exception): |
| 37 | """Raised when an operation specifies an expected SW but the actual SW from |
| 38 | the card doesn't match.""" |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 39 | def __init__(self, sw_actual:str, sw_expected:str, rs=None): |
| 40 | """ |
| 41 | Args: |
| 42 | sw_actual : the SW we actually received from the card (4 hex digits) |
| 43 | sw_expected : the SW we expected to receive from the card (4 hex digits) |
| 44 | rs : interpreter class to convert SW to string |
| 45 | """ |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 46 | self.sw_actual = sw_actual |
| 47 | self.sw_expected = sw_expected |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 48 | self.rs = rs |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 49 | def __str__(self): |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 50 | if self.rs: |
Vadim Yanitskiy | 3b51f43 | 2021-03-12 02:18:06 +0100 | [diff] [blame] | 51 | r = self.rs.interpret_sw(self.sw_actual) |
Harald Welte | b2edd14 | 2021-01-08 23:29:35 +0100 | [diff] [blame] | 52 | if r: |
| 53 | return "SW match failed! Expected %s and got %s: %s - %s" % (self.sw_expected, self.sw_actual, r[0], r[1]) |
Harald Welte | e79cc80 | 2021-01-21 14:10:43 +0100 | [diff] [blame] | 54 | return "SW match failed! Expected %s and got %s." % (self.sw_expected, self.sw_actual) |