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 | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 33 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 34 | class CardHandlerBase: |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 35 | """Abstract base class representing a mechanism for card insertion/removal.""" |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 36 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 37 | def __init__(self, sl: LinkBase): |
| 38 | self.sl = sl |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 39 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 40 | def get(self, first: bool = False): |
| 41 | """Method called when pySim needs a new card to be inserted. |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 42 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 43 | Args: |
| 44 | first : set to true when the get method is called the |
| 45 | first time. This is required to prevent blocking |
| 46 | when a card is already inserted into the reader. |
| 47 | The reader API would not recognize that card as |
| 48 | "new card" until it would be removed and re-inserted |
| 49 | again. |
| 50 | """ |
| 51 | print("Ready for Programming: ", end='') |
| 52 | self._get(first) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 53 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 54 | def error(self): |
| 55 | """Method called when pySim failed to program a card. Move card to 'bad' batch.""" |
| 56 | print("Programming failed: ", end='') |
| 57 | self._error() |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 58 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 59 | def done(self): |
| 60 | """Method called when pySim failed to program a card. Move card to 'good' batch.""" |
| 61 | print("Programming successful: ", end='') |
| 62 | self._done() |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 63 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 64 | def _get(self, first: bool = False): |
| 65 | pass |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 66 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 67 | def _error(self): |
| 68 | pass |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 69 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 70 | def _done(self): |
| 71 | pass |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 72 | |
| 73 | |
| 74 | class CardHandler(CardHandlerBase): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 75 | """Manual card handler: User is prompted to insert/remove card from the reader.""" |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 76 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 77 | def _get(self, first: bool = False): |
| 78 | print("Insert card now (or CTRL-C to cancel)") |
| 79 | self.sl.wait_for_card(newcardonly=not first) |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 80 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 81 | def _error(self): |
| 82 | print("Remove card from reader") |
| 83 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 84 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 85 | def _done(self): |
| 86 | print("Remove card from reader") |
| 87 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 88 | |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 89 | |
| 90 | class CardHandlerAuto(CardHandlerBase): |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 91 | """Automatic card handler: A machine is used to handle the cards.""" |
Harald Welte | 913e616 | 2021-10-11 10:55:00 +0200 | [diff] [blame] | 92 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 93 | verbose = True |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 94 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 95 | def __init__(self, sl: LinkBase, config_file: str): |
| 96 | super().__init__(sl) |
| 97 | print("Card handler Config-file: " + str(config_file)) |
| 98 | with open(config_file) as cfg: |
| 99 | self.cmds = yaml.load(cfg, Loader=yaml.FullLoader) |
| 100 | self.verbose = (self.cmds.get('verbose') == True) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 101 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 102 | def __print_outout(self, out): |
| 103 | print("") |
| 104 | print("Card handler output:") |
| 105 | print("---------------------8<---------------------") |
| 106 | stdout = out[0].strip() |
| 107 | if len(stdout) > 0: |
| 108 | print("stdout:") |
| 109 | print(stdout) |
| 110 | stderr = out[1].strip() |
| 111 | if len(stderr) > 0: |
| 112 | print("stderr:") |
| 113 | print(stderr) |
| 114 | print("---------------------8<---------------------") |
| 115 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 116 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 117 | def __exec_cmd(self, command): |
| 118 | print("Card handler Commandline: " + str(command)) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 119 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 120 | proc = subprocess.Popen( |
| 121 | [command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 122 | out = proc.communicate() |
| 123 | rc = proc.returncode |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 124 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 125 | if rc != 0 or self.verbose: |
| 126 | self.__print_outout(out) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 127 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 128 | if rc != 0: |
| 129 | print("") |
| 130 | print("Error: Card handler failure! (rc=" + str(rc) + ")") |
| 131 | sys.exit(rc) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 132 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 133 | def _get(self, first: bool = False): |
| 134 | print("Transporting card into the reader-bay...") |
| 135 | self.__exec_cmd(self.cmds['get']) |
| 136 | if self.sl: |
| 137 | self.sl.connect() |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 138 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 139 | def _error(self): |
| 140 | print("Transporting card to the error-bin...") |
| 141 | self.__exec_cmd(self.cmds['error']) |
| 142 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 143 | |
Harald Welte | c91085e | 2022-02-10 18:05:45 +0100 | [diff] [blame] | 144 | def _done(self): |
| 145 | print("Transporting card into the collector bin...") |
| 146 | self.__exec_cmd(self.cmds['done']) |
| 147 | print("") |