Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 1 | #include "SensorPaths.hpp" |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 2 | |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 3 | #include <regex> |
| 4 | #include <string> |
| 5 | |
| 6 | namespace sensor_paths |
| 7 | { |
| 8 | |
| 9 | // This is an allowlist of the units a sensor can measure. Should be in sync |
| 10 | // with |
Konstantin Aladyshev | ce6bcdf | 2022-06-09 20:03:46 +0300 | [diff] [blame] | 11 | // phosphor-dbus-interfaces/blob/master/yaml/xyz/openbmc_project/Sensor/Value.interface.yaml#L38 |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 12 | |
| 13 | std::string getPathForUnits(const std::string& units) |
| 14 | { |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 15 | if (units == "DegreesC" || units == unitDegreesC) |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 16 | { |
| 17 | return "temperature"; |
| 18 | } |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 19 | if (units == "RPMS" || units == unitRPMs) |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 20 | { |
| 21 | return "fan_tach"; |
| 22 | } |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 23 | if (units == "Volts" || units == unitVolts) |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 24 | { |
| 25 | return "voltage"; |
| 26 | } |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 27 | if (units == "Meters" || units == unitMeters) |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 28 | { |
| 29 | return "altitude"; |
| 30 | } |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 31 | if (units == "Amperes" || units == unitAmperes) |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 32 | { |
| 33 | return "current"; |
| 34 | } |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 35 | if (units == "Watts" || units == unitWatts) |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 36 | { |
| 37 | return "power"; |
| 38 | } |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 39 | if (units == "Joules" || units == unitJoules) |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 40 | { |
| 41 | return "energy"; |
| 42 | } |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 43 | if (units == "Percent" || units == unitPercent) |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 44 | { |
| 45 | return "Utilization"; |
| 46 | } |
Bruce Mitchell | 5a86e56 | 2021-12-10 12:26:22 -0600 | [diff] [blame] | 47 | if (units == "Pascals" || units == unitPascals) |
| 48 | { |
| 49 | return "pressure"; |
| 50 | } |
Ed Tanous | 6cb732a | 2021-02-18 15:33:51 -0800 | [diff] [blame] | 51 | return ""; |
| 52 | } |
| 53 | |
| 54 | std::string escapePathForDbus(const std::string& name) |
| 55 | { |
| 56 | return std::regex_replace(name, std::regex("[^a-zA-Z0-9_/]+"), "_"); |
| 57 | } |
| 58 | |
| 59 | } // namespace sensor_paths |