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