blob: 2e730121679a0e9825893bb04949e16672d017f5 [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 }
Bruce Mitchell5a86e562021-12-10 12:26:22 -060048 if (units == "Pascals" || units == unitPascals)
49 {
50 return "pressure";
51 }
Ed Tanous6cb732a2021-02-18 15:33:51 -080052 return "";
53}
54
55std::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