xref: /openbmc/phosphor-hwmon/hwmon.hpp (revision 358193811e9769851e7f04816c3f5938f5a64f64)
1 #pragma once
2 
3 #include <string>
4 #include <tuple>
5 
6 #include "interface.hpp"
7 
8 namespace hwmon
9 {
10 namespace entry
11 {
12 static constexpr auto cinput = "input";
13 static constexpr auto clabel = "label";
14 static constexpr auto ctarget = "target";
15 static constexpr auto cenable = "enable";
16 static constexpr auto cfault = "fault";
17 
18 static const std::string input = cinput;
19 static const std::string label = clabel;
20 static const std::string target = ctarget;
21 static const std::string enable = cenable;
22 static const std::string fault = cfault;
23 }
24 
25 namespace type
26 {
27 static constexpr auto cfan = "fan";
28 static constexpr auto ctemp = "temp";
29 static constexpr auto cvolt = "in";
30 static constexpr auto ccurr = "curr";
31 static constexpr auto cenergy = "energy";
32 static constexpr auto cpower = "power";
33 static constexpr auto cpwm = "pwm";
34 
35 
36 static const std::string fan = cfan;
37 static const std::string temp = ctemp;
38 static const std::string volt = cvolt;
39 static const std::string curr = ccurr;
40 static const std::string energy = cenergy;
41 static const std::string power = cpower;
42 static const std::string pwm = cpwm;
43 }
44 
45 static constexpr auto typeAttrMap =
46 {
47     // 1 - hwmon class
48     // 2 - unit
49     // 3 - sysfs scaling factor
50     // 4 - namespace
51     std::make_tuple(
52         hwmon::type::ctemp,
53         ValueInterface::Unit::DegreesC,
54         -3,
55         "temperature"),
56     std::make_tuple(
57         hwmon::type::cfan,
58         ValueInterface::Unit::RPMS,
59         0,
60         "fan_tach"),
61     std::make_tuple(
62         hwmon::type::cvolt,
63         ValueInterface::Unit::Volts,
64         -3,
65         "voltage"),
66     std::make_tuple(
67         hwmon::type::ccurr,
68         ValueInterface::Unit::Amperes,
69         -3,
70         "current"),
71     std::make_tuple(
72         hwmon::type::cenergy,
73         ValueInterface::Unit::Joules,
74         -6,
75         "energy"),
76     std::make_tuple(
77         hwmon::type::cpower,
78         ValueInterface::Unit::Watts,
79         -6,
80         "power"),
81 };
82 
83 inline auto getHwmonType(decltype(typeAttrMap)::const_reference attrs)
84 {
85     return std::get<0>(attrs);
86 }
87 
88 inline auto getUnit(decltype(typeAttrMap)::const_reference attrs)
89 {
90     return std::get<1>(attrs);
91 }
92 
93 inline auto getScale(decltype(typeAttrMap)::const_reference attrs)
94 {
95     return std::get<2>(attrs);
96 }
97 
98 inline auto getNamespace(decltype(typeAttrMap)::const_reference attrs)
99 {
100     return std::get<3>(attrs);
101 }
102 
103 using AttributeIterator = decltype(*typeAttrMap.begin());
104 using Attributes
105     = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type;
106 
107 /** @brief Get Attribute tuple for the type
108  *
109  *  Given a type, it tries to find the corresponding tuple
110  *
111  *  @param[in] type the sensor type
112  *  @param[in,out] A pointer to the Attribute tuple
113  */
114 bool getAttributes(const std::string& type, Attributes& attributes);
115 
116 }  //  namespace hwmon
117 
118 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
119