Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 1 | # 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 | |
| 20 | import sispm |
| 21 | from usb.core import USBError |
| 22 | |
Pau Espin Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 23 | from . import log |
| 24 | from .event_loop import MainLoop |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 25 | from .powersupply import PowerSupply |
| 26 | |
| 27 | class 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 Pedrol | 9a4631c | 2018-03-28 19:17:34 +0200 | [diff] [blame] | 54 | MainLoop.sleep(self, 0.1) |
Pau Espin Pedrol | 19c508c | 2018-03-08 20:54:56 +0100 | [diff] [blame] | 55 | 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!') |
| 89 | self.port = int(myport) |
| 90 | device = self._get_device() |
| 91 | if device is None: |
| 92 | raise log.Error('device with with id %s not found!' % mydevid) |
| 93 | dmin = self._retry_usberr(sispm.getminport, device) |
| 94 | dmax = self._retry_usberr(sispm.getmaxport, device) |
| 95 | if dmin > self.port or dmax < self.port: |
| 96 | raise log.Error('Out of range "port" attribute provided in power_supply conf!') |
| 97 | |
| 98 | def is_powered(self): |
| 99 | """Get whether the device is powered on or off.""" |
| 100 | return self._retry_usberr(sispm.getstatus, self._get_device(), self.port) |
| 101 | |
| 102 | def power_set(self, onoff): |
| 103 | """Turn on (onoff=True) or off (onoff=False) the device.""" |
| 104 | if onoff: |
| 105 | self.dbg('switchon') |
| 106 | self._retry_usberr(sispm.switchon, self._get_device(), self.port) |
| 107 | else: |
| 108 | self.dbg('switchoff') |
| 109 | self._retry_usberr(sispm.switchoff, self._get_device(), self.port) |
| 110 | |
| 111 | |
| 112 | # vim: expandtab tabstop=4 shiftwidth=4 |