blob: bb338336ddb253aca84a4e5719b6a94902f8169f [file] [log] [blame]
Philipp Maierc5b422e2019-08-30 11:41:02 +02001#!/usr/bin/env python2
2# -*- coding: utf-8 -*-
3
4""" pySim: card handler utilities
5"""
6
7#
8# (C) 2019 by Sysmocom s.f.m.c. GmbH
9# All Rights Reserved
10#
11# This program is free software: you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation, either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program. If not, see <http://www.gnu.org/licenses/>.
23#
24
25
26import subprocess
27import sys
28import yaml
29
30# Manual card handler: User is prompted to insert/remove card from the reader.
31class card_handler:
32
33 sl = None
34
35 def __init__(self, sl):
36 self.sl = sl
37
38 def get(self, first = False):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070039 print("Ready for Programming: Insert card now (or CTRL-C to cancel)")
Philipp Maierc5b422e2019-08-30 11:41:02 +020040 self.sl.wait_for_card(newcardonly=not first)
41
42 def error(self):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070043 print("Programming failed: Remove card from reader")
44 print("")
Philipp Maierc5b422e2019-08-30 11:41:02 +020045
46 def done(self):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070047 print("Programming successful: Remove card from reader")
48 print("")
Philipp Maierc5b422e2019-08-30 11:41:02 +020049
50# Automatic card handler: A machine is used to handle the cards.
51class card_handler_auto:
52
53 sl = None
54 cmds = None
55 verbose = True
56
57 def __init__(self, sl, config_file):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070058 print("Card handler Config-file: " + str(config_file))
Philipp Maierc5b422e2019-08-30 11:41:02 +020059 self.sl = sl
60 with open(config_file) as cfg:
61 self.cmds = yaml.load(cfg, Loader=yaml.FullLoader)
62
63 self.verbose = (self.cmds.get('verbose') == True)
64
65 def __print_outout(self,out):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070066 print("")
67 print("Card handler output:")
68 print("---------------------8<---------------------")
Philipp Maierc5b422e2019-08-30 11:41:02 +020069 stdout = out[0].strip()
70 if len(stdout) > 0:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070071 print("stdout:")
72 print(stdout)
Philipp Maierc5b422e2019-08-30 11:41:02 +020073 stderr = out[1].strip()
74 if len(stderr) > 0:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070075 print("stderr:")
76 print(stderr)
77 print("---------------------8<---------------------")
78 print("")
Philipp Maierc5b422e2019-08-30 11:41:02 +020079
80 def __exec_cmd(self, command):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070081 print("Card handler Commandline: " + str(command))
Philipp Maierc5b422e2019-08-30 11:41:02 +020082
83 proc = subprocess.Popen([command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
84 out = proc.communicate()
85 rc = proc.returncode
86
87 if rc != 0 or self.verbose:
88 self.__print_outout(out)
89
90 if rc != 0:
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070091 print("")
92 print("Error: Card handler failure! (rc=" + str(rc) + ")")
Philipp Maierc5b422e2019-08-30 11:41:02 +020093 sys.exit(rc)
94
95 def get(self, first = False):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +070096 print("Ready for Programming: Transporting card into the reader-bay...")
Philipp Maierc5b422e2019-08-30 11:41:02 +020097 self.__exec_cmd(self.cmds['get'])
98 self.sl.connect()
99
100 def error(self):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700101 print("Programming failed: Transporting card to the error-bin...")
Philipp Maierc5b422e2019-08-30 11:41:02 +0200102 self.__exec_cmd(self.cmds['error'])
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700103 print("")
Philipp Maierc5b422e2019-08-30 11:41:02 +0200104
105 def done(self):
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700106 print("Programming successful: Transporting card into the collector bin...")
Philipp Maierc5b422e2019-08-30 11:41:02 +0200107 self.__exec_cmd(self.cmds['done'])
Vadim Yanitskiy6727f0c2020-01-22 23:38:24 +0700108 print("")