blob: 67ae1b1c4bec446ac6700665b9d01d67f80a0410 [file] [log] [blame]
# Copyright 2016 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import shlex
from cros.factory.device import device_types
_HWMON_PATH = '/sys/class/hwmon'
class HardwareMonitorException(Exception):
pass
class HardwareMonitorDevice(device_types.DeviceComponent):
"""A class representing a single hwmon device."""
def __init__(self, dut, path):
super().__init__(dut)
self._path = path
def GetAttribute(self, name):
# yapf: disable
return self._device.ReadFile(self._device.path.join(self._path, name)) # type: ignore #TODO(b/338318729) Fixit! # pylint: disable=line-too-long
# yapf: enable
def GetPath(self):
return self._path
class HardwareMonitor(device_types.DeviceComponent):
"""Utility class for hardware monitor devices."""
def __init__(self, dut, hwmon_path=_HWMON_PATH):
super().__init__(dut)
self._hwmon_path = hwmon_path
def FindOneDevice(self, attr_name, attr_value):
"""Search hwmon devices that have specified attribute name and value.
Args:
attr_name: An attribute name.
attr_value: An attribute value.
Returns:
The matching hwmon device.
Raises:
HardwareMonitorException not exactly one device match the critiria.
"""
devices = self.FindDevices(attr_name, attr_value)
if len(devices) != 1:
raise HardwareMonitorException('Not exactly one device match given'
' critiria')
return devices[0]
def FindDevices(self, attr_name, attr_value):
"""Search hwmon devices that have specified attribute name and value.
Args:
attr_name: An attribute name.
attr_value: An attribute value.
Returns:
A list of matching hwmon device.
"""
# yapf: disable
search_path = self._device.path.join( # type: ignore #TODO(b/338318729) Fixit! # pylint: disable=line-too-long
# yapf: enable
self._hwmon_path, '*', shlex.quote(attr_name))
output = self._device.CheckOutput(
f'grep {search_path} -l -e {shlex.quote(f"^{attr_value}$")}')
# yapf: disable
return [HardwareMonitorDevice(self._device, self._device.path.dirname(path)) # type: ignore #TODO(b/338318729) Fixit! # pylint: disable=line-too-long
# yapf: enable
for path in output.splitlines()]