blob: 91604b49d81fdc73646b4738fb314c440a144a00 [file] [log] [blame]
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +01001# osmo_gsm_tester: class defining a Power Supply object
2#
3# Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import sispm
21from usb.core import USBError
22
23from . import log, event_loop
24from .powersupply import PowerSupply
25
26class PowerSupplySispm(PowerSupply):
27 """PowerSupply implementation using pysispm.
28
29 The device object from sismpm is not cached into an attribute of the class
30 instance because it is actually a libusb object keeping the device assigned
31 to it until it destroyed, meaning it will block other users to use the whole
32 device until the object is released. Instead, we pick the object in the
33 smallest scope possible, and we re-try if we receive a "Resource Busy" error
34 because we know it will be available in short time.
35 """
36
37 def _retry_usberr(self, func, *args):
38 """Run function until it runs successfully, retry on spurious errors.
39
40 Sometimes when operating the usb device, libusb reports the following spurious exception:
41 [Errno 16] Resource busy -> This can appear if another instance is using the device.
42 [Errno 110] Operation timed out
43
44 Retrying after that it's usually enough.
45 """
46 while True:
47 try:
48 ret = func(*args)
49 return ret
50 except USBError as e:
51 if e.errno == 16 or e.errno==110:
52 self.log('skip usb error, retry', repr(e))
53 event_loop.sleep(self, 0.1)
54 continue
55 raise e
56
57 def _get_device(self):
58 """Get the sispm device object.
59
60 It should be kept alive as short as possible as it blocks other users
61 from using the device until the object is released.
62 """
63 mydevid = self.conf.get('device')
64 devices = self._retry_usberr(sispm.connect)
65 for d in devices:
66 did = self._retry_usberr(sispm.getid, d)
67 self.dbg('detected device:', did)
68 if did == mydevid:
69 self.dbg('found matching device: %s' % did)
70 return d
71 return None
72
73
74########################
75# PUBLIC - INTERNAL API
76########################
77 def __init__(self, conf):
78 super().__init__(conf, 'sispm')
79 mydevid = conf.get('device')
80 if mydevid is None:
81 raise log.Error('No "device" attribute provided in supply conf!')
82 self.set_name('sispm-'+mydevid)
83 myport = conf.get('port')
84 if myport is None:
85 raise log.Error('No "port" attribute provided in power_supply conf!')
86 if not int(myport):
87 raise log.Error('Wrong non numeric "port" attribute provided in power_supply conf!')
88 self.port = int(myport)
89 device = self._get_device()
90 if device is None:
91 raise log.Error('device with with id %s not found!' % mydevid)
92 dmin = self._retry_usberr(sispm.getminport, device)
93 dmax = self._retry_usberr(sispm.getmaxport, device)
94 if dmin > self.port or dmax < self.port:
95 raise log.Error('Out of range "port" attribute provided in power_supply conf!')
96
97 def is_powered(self):
98 """Get whether the device is powered on or off."""
99 return self._retry_usberr(sispm.getstatus, self._get_device(), self.port)
100
101 def power_set(self, onoff):
102 """Turn on (onoff=True) or off (onoff=False) the device."""
103 if onoff:
104 self.dbg('switchon')
105 self._retry_usberr(sispm.switchon, self._get_device(), self.port)
106 else:
107 self.dbg('switchoff')
108 self._retry_usberr(sispm.switchoff, self._get_device(), self.port)
109
110
111# vim: expandtab tabstop=4 shiftwidth=4