aboutsummaryrefslogtreecommitdiff
path: root/devlib/instrument/hwmon.py
blob: 5a9d8afa98c453d4402d311f1c830b7801a7cd37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#    Copyright 2015 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import division
import re

from devlib.instrument import Instrument, Measurement, INSTANTANEOUS
from devlib.exception import TargetError


class HwmonInstrument(Instrument):

    name = 'hwmon'
    mode = INSTANTANEOUS

    # sensor kind --> (meaure, standard unit conversion)
    measure_map = {
        'temp': ('temperature', lambda x: x / 1000),
        'in': ('voltage', lambda x: x / 1000),
        'curr': ('current', lambda x: x / 1000),
        'power': ('power', lambda x: x / 1000000),
        'energy': ('energy', lambda x: x / 1000000),
    }

    def __init__(self, target):
        if not hasattr(target, 'hwmon'):
            raise TargetError('Target does not support HWMON')
        super(HwmonInstrument, self).__init__(target)

        self.logger.debug('Discovering available HWMON sensors...')
        for ts in self.target.hwmon.sensors:
            try:
                ts.get_file('input')
                measure = self.measure_map.get(ts.kind)[0]
                if measure:
                    self.logger.debug('\tAdding sensor {}'.format(ts.name))
                    self.add_channel(_guess_site(ts), measure, sensor=ts)
                else:
                    self.logger.debug('\tSkipping sensor {} (unknown kind "{}")'.format(ts.name, ts.kind))
            except ValueError:
                message = 'Skipping sensor {} because it does not have an input file'
                self.logger.debug(message.format(ts.name))
                continue

    def take_measurement(self):
        result = []
        for chan in self.active_channels:
            convert = self.measure_map[chan.sensor.kind][1]
            value = convert(chan.sensor.get('input'))
            result.append(Measurement(value, chan))
        return result


def _guess_site(sensor):
    """
    HWMON does not specify a standard for labeling its sensors, or for
    device/item split (the implication is that each hwmon device a separate chip
    with possibly several sensors on it, but not everyone adheres to that, e.g.,
    with some mobile devices splitting a chip's sensors across multiple hwmon
    devices.  This function processes name/label of the senors to attempt to
    identify the best "candidate" for the site to which the sensor belongs.
    """
    if sensor.name == sensor.label:
        # If no label has been specified for the sensor (in which case, it
        # defaults to the sensor's name), assume that the "site" of the sensor
        # is identified by the HWMON device
        text = sensor.device.name
    else:
        # If a label has been specified, assume multiple sensors controlled by
        # the same device and the label identifies the site.
        text = sensor.label
    # strip out sensor kind suffix, if any, as that does not indicate a site
    for kind in ['volt', 'in', 'curr', 'power', 'energy',
                 'temp', 'voltage', 'temperature', 'current']:
        if kind in text.lower():
            regex = re.compile(r'_*{}\d*_*'.format(kind), re.I)
            text = regex.sub('', text)
    return text.strip()