blob: 231822b7d5461e10a2e72b71edac88d1c3fe5e47 [file] [log] [blame]
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -05001#include <bitset>
Tom Joseph816e92b2017-09-06 19:23:00 +05302#include <experimental/filesystem>
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -05003#include <phosphor-logging/elog-errors.hpp>
4#include <phosphor-logging/log.hpp>
5#include "xyz/openbmc_project/Common/error.hpp"
6#include "types.hpp"
Tom Joseph816e92b2017-09-06 19:23:00 +05307#include "sensorhandler.h"
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -05008#include "sensordatahandler.hpp"
Tom Joseph816e92b2017-09-06 19:23:00 +05309#include "utils.hpp"
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050010
11namespace ipmi
12{
13namespace sensor
14{
15
16using namespace phosphor::logging;
17using InternalFailure =
18 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
19
20static constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
21static constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
22static constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
23
24/** @brief get the D-Bus service and service path
25 * @param[in] bus - The Dbus bus object
26 * @param[in] interface - interface to the service
27 * @param[in] path - interested path in the list of objects
28 * @return pair of service path and service
29 */
30ServicePath getServiceAndPath(sdbusplus::bus::bus& bus,
31 const std::string& interface,
32 const std::string& path)
33{
34 auto depth = 0;
35 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
36 MAPPER_PATH,
37 MAPPER_INTERFACE,
38 "GetSubTree");
39 mapperCall.append("/");
40 mapperCall.append(depth);
41 mapperCall.append(std::vector<Interface>({interface}));
42
43 auto mapperResponseMsg = bus.call(mapperCall);
44 if (mapperResponseMsg.is_method_error())
45 {
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050046 log<level::ERR>("Mapper GetSubTree failed",
47 entry("PATH=%s", path),
48 entry("INTERFACE=%s", interface));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050049 elog<InternalFailure>();
50 }
51
52 MapperResponseType mapperResponse;
53 mapperResponseMsg.read(mapperResponse);
54 if (mapperResponse.empty())
55 {
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050056 log<level::ERR>("Invalid mapper response",
57 entry("PATH=%s", path),
58 entry("INTERFACE=%s", interface));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050059 elog<InternalFailure>();
60 }
61
62 if (path.empty())
63 {
64 //Get the first one if the path is not in list.
65 return std::make_pair(mapperResponse.begin()->first,
66 mapperResponse.begin()->second.begin()->first);
67 }
68 const auto& iter = mapperResponse.find(path);
69 if (iter == mapperResponse.end())
70 {
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050071 log<level::ERR>("Coudn't find d-bus path",
72 entry("PATH=%s", path),
73 entry("INTERFACE=%s", interface));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050074 elog<InternalFailure>();
75 }
76 return std::make_pair(iter->first, iter->second.begin()->first);
77}
78
79AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData)
80{
81 Assertion assertionStates =
82 (static_cast<Assertion>(cmdData.assertOffset8_14)) << 8 |
83 cmdData.assertOffset0_7;
84 Deassertion deassertionStates =
85 (static_cast<Deassertion>(cmdData.deassertOffset8_14)) << 8 |
86 cmdData.deassertOffset0_7;
87 return std::make_pair(assertionStates, deassertionStates);
88}
89
90ipmi_ret_t updateToDbus(IpmiUpdateData& msg)
91{
92 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
93 try
94 {
95 auto serviceResponseMsg = bus.call(msg);
96 if (serviceResponseMsg.is_method_error())
97 {
98 log<level::ERR>("Error in D-Bus call");
99 return IPMI_CC_UNSPECIFIED_ERROR;
100 }
101 }
102 catch (InternalFailure& e)
103 {
104 commit<InternalFailure>();
105 return IPMI_CC_UNSPECIFIED_ERROR;
106 }
107 return IPMI_CC_OK;
108}
109
Tom Joseph816e92b2017-09-06 19:23:00 +0530110namespace get
111{
112
113GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
114 const InstancePath& path,
115 const DbusInterface& interface)
116{
117 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
118 GetSensorResponse response {};
119 auto responseData = reinterpret_cast<GetReadingResponse*>(response.data());
120
121 auto service = ipmi::getService(bus, interface, path);
122
123 const auto& interfaceList = sensorInfo.propertyInterfaces;
124
125 for (const auto& interface : interfaceList)
126 {
127 for (const auto& property : interface.second)
128 {
129 auto propValue = ipmi::getDbusProperty(bus,
130 service,
131 path,
132 interface.first,
133 property.first);
134
135 for (const auto& value : property.second)
136 {
137 if (propValue == value.second.assert)
138 {
139 setOffset(value.first, responseData);
140 break;
141 }
142
143 }
144 }
145 }
146
147 return response;
148}
149
150GetSensorResponse assertion(const Info& sensorInfo)
151{
152 return mapDbusToAssertion(sensorInfo,
153 sensorInfo.sensorPath,
154 sensorInfo.sensorInterface);
155}
156
157} //namespace get
158
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500159namespace set
160{
161
162IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
163 const std::string& sensorPath,
164 const std::string& command,
165 const std::string& sensorInterface)
166{
167 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
168 using namespace std::string_literals;
169
170 std::string dbusService;
171 std::string dbusPath;
172
173 std::tie(dbusPath, dbusService) = getServiceAndPath(bus,
174 sensorInterface,
175 sensorPath);
176 return bus.new_method_call(dbusService.c_str(),
177 dbusPath.c_str(),
178 updateInterface.c_str(),
179 command.c_str());
180}
181
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500182ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData,
183 const Info& sensorInfo,
184 uint8_t data)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500185{
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500186 auto msg = makeDbusMsg(
187 "org.freedesktop.DBus.Properties",
188 sensorInfo.sensorPath,
189 "Set",
190 sensorInfo.sensorInterface);
191
192 const auto& interface = sensorInfo.propertyInterfaces.begin();
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500193 msg.append(interface->first);
194 for (const auto& property : interface->second)
195 {
196 msg.append(property.first);
197 const auto& iter = property.second.find(data);
198 if (iter == property.second.end())
199 {
200 log<level::ERR>("Invalid event data");
201 return IPMI_CC_PARM_OUT_OF_RANGE;
202 }
203 msg.append(iter->second.assert);
204 }
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500205 return updateToDbus(msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500206}
207
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500208ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
209 const Info& sensorInfo)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500210{
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500211 auto msg = makeDbusMsg(
212 "org.freedesktop.DBus.Properties",
213 sensorInfo.sensorPath,
214 "Set",
215 sensorInfo.sensorInterface);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500216
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500217 std::bitset<16> assertionSet(getAssertionSet(cmdData).first);
218 std::bitset<16> deassertionSet(getAssertionSet(cmdData).second);
219
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500220 const auto& interface = sensorInfo.propertyInterfaces.begin();
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500221 msg.append(interface->first);
222 for (const auto& property : interface->second)
223 {
224 msg.append(property.first);
225 for (const auto& value : property.second)
226 {
227 if (assertionSet.test(value.first))
228 {
229 msg.append(value.second.assert);
230 }
231 if (deassertionSet.test(value.first))
232 {
233 msg.append(value.second.deassert);
234 }
235 }
236 }
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500237 return updateToDbus(msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500238}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500239
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500240}//namespace set
241
242namespace notify
243{
244
245IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
246 const std::string& sensorPath,
247 const std::string& command,
248 const std::string& sensorInterface)
249{
250 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
251 using namespace std::string_literals;
252
253 std::string dbusService;
254 std::string dbusPath;
255
256 std::tie(dbusPath, dbusService) = getServiceAndPath(bus,
257 updateInterface);
258
259 return bus.new_method_call(dbusService.c_str(),
260 dbusPath.c_str(),
261 updateInterface.c_str(),
262 command.c_str());
263}
264
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500265ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
266 const Info& sensorInfo)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500267{
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500268 auto msg = makeDbusMsg(
269 sensorInfo.sensorInterface,
270 sensorInfo.sensorPath,
271 "Notify",
272 sensorInfo.sensorInterface);
273
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500274 std::bitset<16> assertionSet(getAssertionSet(cmdData).first);
275 std::bitset<16> deassertionSet(getAssertionSet(cmdData).second);
276 ipmi::sensor::ObjectMap objects;
277 ipmi::sensor::InterfaceMap interfaces;
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500278 for (const auto& interface : sensorInfo.propertyInterfaces)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500279 {
280 for (const auto& property : interface.second)
281 {
282 ipmi::sensor::PropertyMap props;
283 bool valid = false;
284 for (const auto& value : property.second)
285 {
286 if (assertionSet.test(value.first))
287 {
288 props.emplace(property.first, value.second.assert);
289 valid = true;
290 }
291 else if (deassertionSet.test(value.first))
292 {
293 props.emplace(property.first, value.second.deassert);
294 valid = true;
295 }
296 }
297 if (valid)
298 {
299 interfaces.emplace(interface.first, std::move(props));
300 }
301 }
302 }
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500303 objects.emplace(sensorInfo.sensorPath, std::move(interfaces));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500304 msg.append(std::move(objects));
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500305 return updateToDbus(msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500306}
Tom Joseph816e92b2017-09-06 19:23:00 +0530307
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500308}//namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530309
310namespace inventory
311{
312
313namespace get
314{
315
316GetSensorResponse assertion(const Info& sensorInfo)
317{
318 namespace fs = std::experimental::filesystem;
319
320 fs::path path{ipmi::sensor::inventoryRoot};
321 path += sensorInfo.sensorPath;
322
323 return ipmi::sensor::get::mapDbusToAssertion(
324 sensorInfo,
325 path.string(),
326 sensorInfo.propertyInterfaces.begin()->first);
327}
328
329} //namespace get
330
331} // namespace inventory
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500332}//namespace sensor
333}//namespace ipmi