xref: /openbmc/dbus-sensors/src/SensorPaths.cpp (revision d85c89518604abc63f9f0121f334277234271dfb)
1 #include "SensorPaths.hpp"
2 
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
11 // phosphor-dbus-interfaces/blob/master/yaml/xyz/openbmc_project/Sensor/Value.interface.yaml#L38
12 
getPathForUnits(const std::string & units)13 std::string getPathForUnits(const std::string& units)
14 {
15     for (const auto& unit : unitsMap)
16     {
17         if (unit.dbusUnitName == units || unit.dbusPathName == units)
18         {
19             return std::string(unit.emUnitName);
20         }
21     }
22     return "";
23 }
24 
escapePathForDbus(const std::string & name)25 std::string escapePathForDbus(const std::string& name)
26 {
27     return std::regex_replace(name, std::regex("[^a-zA-Z0-9_/]+"), "_");
28 }
29 
convertToFullUnits(const std::string & units)30 std::string convertToFullUnits(const std::string& units)
31 {
32     for (const auto& unit : unitsMap)
33     {
34         if (unit.dbusUnitName == units)
35         {
36             return std::string(unit.dbusPathName);
37         }
38     }
39     return "";
40 }
41 
42 } // namespace sensor_paths
43