Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 3 | """ pySim: card handler utilities. A 'card handler' is some method |
| 4 | by which cards can be inserted/removed into the card reader. For |
| 5 | normal smart card readers, this has to be done manually. However, |
| 6 | there are also automatic card feeders. |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 7 | """ |
| 8 | |
| 9 | # |
| 10 | # (C) 2019 by Sysmocom s.f.m.c. GmbH |
| 11 | # All Rights Reserved |
| 12 | # |
| 13 | # This program is free software: you can redistribute it and/or modify |
| 14 | # it under the terms of the GNU General Public License as published by |
| 15 | # the Free Software Foundation, either version 2 of the License, or |
| 16 | # (at your option) any later version. |
| 17 | # |
| 18 | # This program is distributed in the hope that it will be useful, |
| 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | # GNU General Public License for more details. |
| 22 | # |
| 23 | # You should have received a copy of the GNU General Public License |
| 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | # |
| 26 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 27 | from pySim.transport import LinkBase |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 28 | |
| 29 | import subprocess |
| 30 | import sys |
| 31 | import yaml |
| 32 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 33 | class CardHandlerBase: |
| 34 | """Abstract base class representing a mechanism for card insertion/removal.""" |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 35 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 36 | def __init__(self, sl:LinkBase): |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 37 | self.sl = sl |
| 38 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 39 | def get(self, first:bool = False): |
| 40 | """Method called when pySim needs a new card to be inserted. |
| 41 | |
| 42 | Args: |
| 43 | first : set to true when the get method is called the |
| 44 | first time. This is required to prevent blocking |
| 45 | when a card is already inserted into the reader. |
| 46 | The reader API would not recognize that card as |
| 47 | "new card" until it would be removed and re-inserted |
| 48 | again. |
| 49 | """ |
| 50 | print("Ready for Programming: ", end='') |
| 51 | self._get(first) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 52 | |
| 53 | def error(self): |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 54 | """Method called when pySim failed to program a card. Move card to 'bad' batch.""" |
| 55 | print("Programming failed: ", end='') |
| 56 | self._error() |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 57 | |
| 58 | def done(self): |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 59 | """Method called when pySim failed to program a card. Move card to 'good' batch.""" |
| 60 | print("Programming successful: ", end='') |
| 61 | self._done() |
| 62 | |
| 63 | def _get(self, first:bool = False): |
| 64 | pass |
| 65 | |
| 66 | def _error(self): |
| 67 | pass |
| 68 | |
| 69 | def _done(self): |
| 70 | pass |
| 71 | |
| 72 | |
| 73 | class CardHandler(CardHandlerBase): |
| 74 | """Manual card handler: User is prompted to insert/remove card from the reader.""" |
| 75 | |
| 76 | def _get(self, first:bool = False): |
| 77 | print("Insert card now (or CTRL-C to cancel)") |
| 78 | self.sl.wait_for_card(newcardonly=not first) |
| 79 | |
| 80 | def _error(self): |
| 81 | print("Remove card from reader") |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 82 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 83 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 84 | def _done(self): |
| 85 | print("Remove card from reader") |
| 86 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 87 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 88 | |
| 89 | class CardHandlerAuto(CardHandlerBase): |
| 90 | """Automatic card handler: A machine is used to handle the cards.""" |
| 91 | |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 92 | verbose = True |
| 93 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 94 | def __init__(self, sl:LinkBase, config_file:str): |
| 95 | super().__init__(sl) |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 96 | print("Card handler Config-file: " + str(config_file)) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 97 | with open(config_file) as cfg: |
| 98 | self.cmds = yaml.load(cfg, Loader=yaml.FullLoader) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 99 | self.verbose = (self.cmds.get('verbose') == True) |
| 100 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 101 | def __print_outout(self, out): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 102 | print("") |
| 103 | print("Card handler output:") |
| 104 | print("---------------------8<---------------------") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 105 | stdout = out[0].strip() |
| 106 | if len(stdout) > 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 107 | print("stdout:") |
| 108 | print(stdout) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 109 | stderr = out[1].strip() |
| 110 | if len(stderr) > 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 111 | print("stderr:") |
| 112 | print(stderr) |
| 113 | print("---------------------8<---------------------") |
| 114 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 115 | |
| 116 | def __exec_cmd(self, command): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 117 | print("Card handler Commandline: " + str(command)) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 118 | |
| 119 | proc = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 120 | out = proc.communicate() |
| 121 | rc = proc.returncode |
| 122 | |
| 123 | if rc != 0 or self.verbose: |
| 124 | self.__print_outout(out) |
| 125 | |
| 126 | if rc != 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 127 | print("") |
| 128 | print("Error: Card handler failure! (rc=" + str(rc) + ")") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 129 | sys.exit(rc) |
| 130 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 131 | def _get(self, first:bool = False): |
| 132 | print("Transporting card into the reader-bay...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 133 | self.__exec_cmd(self.cmds['get']) |
Philipp Maier | 48e1b90 | 2021-09-17 13:22:32 +0200 | [diff] [blame] | 134 | if self.sl: |
| 135 | self.sl.connect() |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 136 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 137 | def _error(self): |
| 138 | print("Transporting card to the error-bin...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 139 | self.__exec_cmd(self.cmds['error']) |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 140 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 141 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 142 | def _done(self): |
| 143 | print("Transporting card into the collector bin...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 144 | self.__exec_cmd(self.cmds['done']) |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 145 | print("") |