blob: b0cea9cb0cc8a0116b90c4bd80e591e99018fccf [file] [log] [blame]
Zev Weiss6b6891c2021-04-22 02:46:21 -05001#include <SensorPaths.hpp>
2
Ed Tanous6cb732a2021-02-18 15:33:51 -08003#include <cstring>
4#include <regex>
5#include <string>
6
7namespace 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/xyz/openbmc_project/Sensor/Value.interface.yaml#L35
13
14std::string getPathForUnits(const std::string& units)
15{
Zev Weiss6b6891c2021-04-22 02:46:21 -050016 if (units == "DegreesC" || units == unitDegreesC)
Ed Tanous6cb732a2021-02-18 15:33:51 -080017 {
18 return "temperature";
19 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050020 if (units == "RPMS" || units == unitRPMs)
Ed Tanous6cb732a2021-02-18 15:33:51 -080021 {
22 return "fan_tach";
23 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050024 if (units == "Volts" || units == unitVolts)
Ed Tanous6cb732a2021-02-18 15:33:51 -080025 {
26 return "voltage";
27 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050028 if (units == "Meters" || units == unitMeters)
Ed Tanous6cb732a2021-02-18 15:33:51 -080029 {
30 return "altitude";
31 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050032 if (units == "Amperes" || units == unitAmperes)
Ed Tanous6cb732a2021-02-18 15:33:51 -080033 {
34 return "current";
35 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050036 if (units == "Watts" || units == unitWatts)
Ed Tanous6cb732a2021-02-18 15:33:51 -080037 {
38 return "power";
39 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050040 if (units == "Joules" || units == unitJoules)
Ed Tanous6cb732a2021-02-18 15:33:51 -080041 {
42 return "energy";
43 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050044 if (units == "Percent" || units == unitPercent)
Ed Tanous6cb732a2021-02-18 15:33:51 -080045 {
46 return "Utilization";
47 }
48 return "";
49}
50
51std::string escapePathForDbus(const std::string& name)
52{
53 return std::regex_replace(name, std::regex("[^a-zA-Z0-9_/]+"), "_");
54}
55
56} // namespace sensor_paths