Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: card handler utilities |
| 4 | """ |
| 5 | |
| 6 | # |
| 7 | # (C) 2019 by Sysmocom s.f.m.c. GmbH |
| 8 | # All Rights Reserved |
| 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 | |
| 24 | |
| 25 | import subprocess |
| 26 | import sys |
| 27 | import yaml |
| 28 | |
| 29 | # Manual card handler: User is prompted to insert/remove card from the reader. |
| 30 | class card_handler: |
| 31 | |
| 32 | sl = None |
| 33 | |
| 34 | def __init__(self, sl): |
| 35 | self.sl = sl |
| 36 | |
| 37 | def get(self, first = False): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 38 | print("Ready for Programming: Insert card now (or CTRL-C to cancel)") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 39 | self.sl.wait_for_card(newcardonly=not first) |
| 40 | |
| 41 | def error(self): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 42 | print("Programming failed: Remove card from reader") |
| 43 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 44 | |
| 45 | def done(self): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 46 | print("Programming successful: Remove card from reader") |
| 47 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 48 | |
| 49 | # Automatic card handler: A machine is used to handle the cards. |
| 50 | class card_handler_auto: |
| 51 | |
| 52 | sl = None |
| 53 | cmds = None |
| 54 | verbose = True |
| 55 | |
| 56 | def __init__(self, sl, config_file): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 57 | print("Card handler Config-file: " + str(config_file)) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 58 | self.sl = sl |
| 59 | with open(config_file) as cfg: |
| 60 | self.cmds = yaml.load(cfg, Loader=yaml.FullLoader) |
| 61 | |
| 62 | self.verbose = (self.cmds.get('verbose') == True) |
| 63 | |
| 64 | def __print_outout(self,out): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 65 | print("") |
| 66 | print("Card handler output:") |
| 67 | print("---------------------8<---------------------") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 68 | stdout = out[0].strip() |
| 69 | if len(stdout) > 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 70 | print("stdout:") |
| 71 | print(stdout) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 72 | stderr = out[1].strip() |
| 73 | if len(stderr) > 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 74 | print("stderr:") |
| 75 | print(stderr) |
| 76 | print("---------------------8<---------------------") |
| 77 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 78 | |
| 79 | def __exec_cmd(self, command): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 80 | print("Card handler Commandline: " + str(command)) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 81 | |
| 82 | proc = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 83 | out = proc.communicate() |
| 84 | rc = proc.returncode |
| 85 | |
| 86 | if rc != 0 or self.verbose: |
| 87 | self.__print_outout(out) |
| 88 | |
| 89 | if rc != 0: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 90 | print("") |
| 91 | print("Error: Card handler failure! (rc=" + str(rc) + ")") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 92 | sys.exit(rc) |
| 93 | |
| 94 | def get(self, first = False): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 95 | print("Ready for Programming: Transporting card into the reader-bay...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 96 | self.__exec_cmd(self.cmds['get']) |
| 97 | self.sl.connect() |
| 98 | |
| 99 | def error(self): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 100 | print("Programming failed: Transporting card to the error-bin...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 101 | self.__exec_cmd(self.cmds['error']) |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 102 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 103 | |
| 104 | def done(self): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 105 | print("Programming successful: Transporting card into the collector bin...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 106 | self.__exec_cmd(self.cmds['done']) |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 107 | print("") |