xref: /openbmc/dbus-sensors/src/SensorPaths.cpp (revision 77141acb)
1 #include "SensorPaths.hpp"
2 
3 #include <cstring>
4 #include <regex>
5 #include <string>
6 
7 namespace sensor_paths
8 {
9 
10 // This is an allowlist of the units a sensor can measure. Should be in sync
11 // with
12 // phosphor-dbus-interfaces/blob/master/yaml/xyz/openbmc_project/Sensor/Value.interface.yaml#L38
13 
14 std::string getPathForUnits(const std::string& units)
15 {
16     if (units == "DegreesC" || units == unitDegreesC)
17     {
18         return "temperature";
19     }
20     if (units == "RPMS" || units == unitRPMs)
21     {
22         return "fan_tach";
23     }
24     if (units == "Volts" || units == unitVolts)
25     {
26         return "voltage";
27     }
28     if (units == "Meters" || units == unitMeters)
29     {
30         return "altitude";
31     }
32     if (units == "Amperes" || units == unitAmperes)
33     {
34         return "current";
35     }
36     if (units == "Watts" || units == unitWatts)
37     {
38         return "power";
39     }
40     if (units == "Joules" || units == unitJoules)
41     {
42         return "energy";
43     }
44     if (units == "Percent" || units == unitPercent)
45     {
46         return "Utilization";
47     }
48     if (units == "Pascals" || units == unitPascals)
49     {
50         return "pressure";
51     }
52     return "";
53 }
54 
55 std::string escapePathForDbus(const std::string& name)
56 {
57     return std::regex_replace(name, std::regex("[^a-zA-Z0-9_/]+"), "_");
58 }
59 
60 } // namespace sensor_paths
61