xref: /openbmc/openpower-occ-control/occ_dbus.cpp (revision b5ca101530dc5fa0992d00ab36edb292519cef94)
1 #include "occ_dbus.hpp"
2 
3 #include "utils.hpp"
4 
5 #include <phosphor-logging/log.hpp>
6 
7 #include <iostream>
8 
9 namespace open_power
10 {
11 namespace occ
12 {
13 namespace dbus
14 {
15 
16 using namespace phosphor::logging;
17 bool OccDBusSensors::setMaxValue(const std::string& path, double value)
18 {
19     if (path.empty())
20     {
21         return false;
22     }
23 
24     if (sensors.find(path) == sensors.end())
25     {
26         sensors.emplace(
27             path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
28     }
29 
30     sensors.at(path)->maxValue(value);
31     return true;
32 }
33 
34 double OccDBusSensors::getMaxValue(const std::string& path) const
35 {
36     if (sensors.find(path) != sensors.end())
37     {
38         return sensors.at(path)->maxValue();
39     }
40 
41     throw std::invalid_argument("Failed to get MaxValue property.");
42 }
43 
44 bool OccDBusSensors::setMinValue(const std::string& path, double value)
45 {
46     if (path.empty())
47     {
48         return false;
49     }
50 
51     if (sensors.find(path) == sensors.end())
52     {
53         sensors.emplace(
54             path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
55     }
56 
57     sensors.at(path)->minValue(value);
58     return true;
59 }
60 
61 double OccDBusSensors::getMinValue(const std::string& path) const
62 {
63     if (sensors.find(path) != sensors.end())
64     {
65         return sensors.at(path)->minValue();
66     }
67 
68     throw std::invalid_argument("Failed to get MinValue property.");
69 }
70 
71 bool OccDBusSensors::setValue(const std::string& path, double value)
72 {
73     if (path.empty())
74     {
75         return false;
76     }
77 
78     if (sensors.find(path) == sensors.end())
79     {
80         sensors.emplace(
81             path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
82     }
83 
84     sensors.at(path)->value(value);
85     return true;
86 }
87 
88 double OccDBusSensors::getValue(const std::string& path) const
89 {
90     if (sensors.find(path) != sensors.end())
91     {
92         return sensors.at(path)->value();
93     }
94 
95     throw std::invalid_argument("Failed to get Value property.");
96 }
97 
98 bool OccDBusSensors::setUnit(const std::string& path, const std::string& value)
99 {
100     if (path.empty())
101     {
102         return false;
103     }
104 
105     if (sensors.find(path) == sensors.end())
106     {
107         sensors.emplace(
108             path, std::make_unique<SensorIntf>(utils::getBus(), path.c_str()));
109     }
110 
111     try
112     {
113         sensors.at(path)->unit(SensorIntf::convertUnitFromString(value));
114     }
115     catch (const std::exception& e)
116     {
117         log<level::ERR>("set Unit propety failed", entry("ERROR=%s", e.what()));
118         return false;
119     }
120 
121     return true;
122 }
123 
124 std::string OccDBusSensors::getUnit(const std::string& path) const
125 {
126     if (sensors.find(path) != sensors.end())
127     {
128         try
129         {
130             return SensorIntf::convertUnitToString(sensors.at(path)->unit());
131         }
132         catch (const std::exception& e)
133         {
134             log<level::ERR>("get Unit propety failed",
135                             entry("ERROR=%s", e.what()));
136         }
137     }
138 
139     throw std::invalid_argument("Failed to get Unit property.");
140 }
141 
142 bool OccDBusSensors::setOperationalStatus(const std::string& path, bool value)
143 {
144     if (path.empty())
145     {
146         return false;
147     }
148 
149     if (operationalStatus.find(path) == operationalStatus.end())
150     {
151         operationalStatus.emplace(path, std::make_unique<OperationalStatusIntf>(
152                                             utils::getBus(), path.c_str()));
153     }
154 
155     operationalStatus.at(path)->functional(value);
156     return true;
157 }
158 
159 bool OccDBusSensors::getOperationalStatus(const std::string& path) const
160 {
161     if (operationalStatus.find(path) != operationalStatus.end())
162     {
163         return operationalStatus.at(path)->functional();
164     }
165 
166     throw std::invalid_argument("Failed to get OperationalStatus property.");
167 }
168 
169 } // namespace dbus
170 } // namespace occ
171 } // namespace open_power
172