xref: /openbmc/phosphor-pid-control/dbus/dbusutil.hpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
1 #pragma once
2 
3 #include <cstdint>
4 #include <map>
5 #include <stdexcept>
6 #include <string>
7 #include <type_traits>
8 #include <unordered_map>
9 #include <utility>
10 #include <vector>
11 
12 namespace pid_control
13 {
14 
15 struct VariantToDoubleVisitor
16 {
17     template <typename T>
operator ()pid_control::VariantToDoubleVisitor18     std::enable_if_t<std::is_arithmetic<T>::value, double> operator()(
19         const T& t) const
20     {
21         return static_cast<double>(t);
22     }
23 
24     template <typename T>
operator ()pid_control::VariantToDoubleVisitor25     std::enable_if_t<!std::is_arithmetic<T>::value, double> operator()(
26         [[maybe_unused]] const T& t) const
27     {
28         throw std::invalid_argument("Cannot translate type to double");
29     }
30 };
31 
32 std::string getSensorUnit(const std::string& type);
33 std::string getSensorPath(const std::string& type, const std::string& id);
34 std::string getMatch(const std::string& path);
35 void scaleSensorReading(const double min, const double max, double& value);
36 bool validType(const std::string& type);
37 
38 bool findSensors(const std::unordered_map<std::string, std::string>& sensors,
39                  const std::string& search,
40                  std::vector<std::pair<std::string, std::string>>& matches);
41 
42 // Set zone number for a zone, 0-based
43 int64_t setZoneIndex(const std::string& name,
44                      std::map<std::string, int64_t>& zones, int64_t index);
45 
46 // Read zone number for a zone.
47 int64_t getZoneIndex(const std::string& name,
48                      std::map<std::string, int64_t>& zones);
49 
50 } // namespace pid_control
51