blob: d1b244fa7c0a7002c0d1676412cebd87d6d8caba [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
Pau Espin Pedrole1a58bd2020-04-10 20:46:07 +020023from ..core import log
24from ..core.event_loop import MainLoop
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010025from .powersupply import PowerSupply
26
27class PowerSupplySispm(PowerSupply):
28 """PowerSupply implementation using pysispm.
29
30 The device object from sismpm is not cached into an attribute of the class
31 instance because it is actually a libusb object keeping the device assigned
32 to it until it destroyed, meaning it will block other users to use the whole
33 device until the object is released. Instead, we pick the object in the
34 smallest scope possible, and we re-try if we receive a "Resource Busy" error
35 because we know it will be available in short time.
36 """
37
38 def _retry_usberr(self, func, *args):
39 """Run function until it runs successfully, retry on spurious errors.
40
41 Sometimes when operating the usb device, libusb reports the following spurious exception:
42 [Errno 16] Resource busy -> This can appear if another instance is using the device.
43 [Errno 110] Operation timed out
44
45 Retrying after that it's usually enough.
46 """
47 while True:
48 try:
49 ret = func(*args)
50 return ret
51 except USBError as e:
52 if e.errno == 16 or e.errno==110:
53 self.log('skip usb error, retry', repr(e))
Pau Espin Pedrol9a4631c2018-03-28 19:17:34 +020054 MainLoop.sleep(self, 0.1)
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010055 continue
56 raise e
57
58 def _get_device(self):
59 """Get the sispm device object.
60
61 It should be kept alive as short as possible as it blocks other users
62 from using the device until the object is released.
63 """
64 mydevid = self.conf.get('device')
65 devices = self._retry_usberr(sispm.connect)
66 for d in devices:
67 did = self._retry_usberr(sispm.getid, d)
68 self.dbg('detected device:', did)
69 if did == mydevid:
70 self.dbg('found matching device: %s' % did)
71 return d
72 return None
73
74
75########################
76# PUBLIC - INTERNAL API
77########################
78 def __init__(self, conf):
79 super().__init__(conf, 'sispm')
80 mydevid = conf.get('device')
81 if mydevid is None:
82 raise log.Error('No "device" attribute provided in supply conf!')
83 self.set_name('sispm-'+mydevid)
84 myport = conf.get('port')
85 if myport is None:
86 raise log.Error('No "port" attribute provided in power_supply conf!')
87 if not int(myport):
88 raise log.Error('Wrong non numeric "port" attribute provided in power_supply conf!')
Pau Espin Pedrol7f4807a2018-05-28 12:21:35 +020089 self.set_name('sispm-'+mydevid+'-'+myport)
Pau Espin Pedrol19c508c2018-03-08 20:54:56 +010090 self.port = int(myport)
91 device = self._get_device()
92 if device is None:
93 raise log.Error('device with with id %s not found!' % mydevid)
94 dmin = self._retry_usberr(sispm.getminport, device)
95 dmax = self._retry_usberr(sispm.getmaxport, device)
96 if dmin > self.port or dmax < self.port:
97 raise log.Error('Out of range "port" attribute provided in power_supply conf!')
98
99 def is_powered(self):
100 """Get whether the device is powered on or off."""
101 return self._retry_usberr(sispm.getstatus, self._get_device(), self.port)
102
103 def power_set(self, onoff):
104 """Turn on (onoff=True) or off (onoff=False) the device."""
105 if onoff:
106 self.dbg('switchon')
107 self._retry_usberr(sispm.switchon, self._get_device(), self.port)
108 else:
109 self.dbg('switchoff')
110 self._retry_usberr(sispm.switchoff, self._get_device(), self.port)
111
112
113# vim: expandtab tabstop=4 shiftwidth=4