blob: 9beab512d175e56be19c204e778e43a49b884af3 [file] [log] [blame]
Philipp Maierc5b422e2019-08-30 11:41:02 +02001# -*- 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
25import subprocess
26import sys
27import yaml
28
29# Manual card handler: User is prompted to insert/remove card from the reader.
30class card_handler:
31
32 sl = None
33
34 def __init__(self, sl):
35 self.sl = sl
36
37 def get(self, first = False):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070038 print("Ready for Programming: Insert card now (or CTRL-C to cancel)")
Philipp Maierc5b422e2019-08-30 11:41:02 +020039 self.sl.wait_for_card(newcardonly=not first)
40
41 def error(self):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070042 print("Programming failed: Remove card from reader")
43 print("")
Philipp Maierc5b422e2019-08-30 11:41:02 +020044
45 def done(self):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070046 print("Programming successful: Remove card from reader")
47 print("")
Philipp Maierc5b422e2019-08-30 11:41:02 +020048
49# Automatic card handler: A machine is used to handle the cards.
50class card_handler_auto:
51
52 sl = None
53 cmds = None
54 verbose = True
55
56 def __init__(self, sl, config_file):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070057 print("Card handler Config-file: " + str(config_file))
Philipp Maierc5b422e2019-08-30 11:41:02 +020058 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 Yanitskiy6727f0c2020-01-22 23:38:24 +070065 print("")
66 print("Card handler output:")
67 print("---------------------8<---------------------")
Philipp Maierc5b422e2019-08-30 11:41:02 +020068 stdout = out[0].strip()
69 if len(stdout) > 0:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070070 print("stdout:")
71 print(stdout)
Philipp Maierc5b422e2019-08-30 11:41:02 +020072 stderr = out[1].strip()
73 if len(stderr) > 0:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070074 print("stderr:")
75 print(stderr)
76 print("---------------------8<---------------------")
77 print("")
Philipp Maierc5b422e2019-08-30 11:41:02 +020078
79 def __exec_cmd(self, command):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070080 print("Card handler Commandline: " + str(command))
Philipp Maierc5b422e2019-08-30 11:41:02 +020081
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 Yanitskiy6727f0c2020-01-22 23:38:24 +070090 print("")
91 print("Error: Card handler failure! (rc=" + str(rc) + ")")
Philipp Maierc5b422e2019-08-30 11:41:02 +020092 sys.exit(rc)
93
94 def get(self, first = False):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070095 print("Ready for Programming: Transporting card into the reader-bay...")
Philipp Maierc5b422e2019-08-30 11:41:02 +020096 self.__exec_cmd(self.cmds['get'])
97 self.sl.connect()
98
99 def error(self):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700100 print("Programming failed: Transporting card to the error-bin...")
Philipp Maierc5b422e2019-08-30 11:41:02 +0200101 self.__exec_cmd(self.cmds['error'])
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700102 print("")
Philipp Maierc5b422e2019-08-30 11:41:02 +0200103
104 def done(self):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700105 print("Programming successful: Transporting card into the collector bin...")
Philipp Maierc5b422e2019-08-30 11:41:02 +0200106 self.__exec_cmd(self.cmds['done'])
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700107 print("")