blob: 7db8fe0da4ae41d3718e2e5b25b10d70a8223cca [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
Tom Josephb0adbcd2018-01-24 11:51:29 +0530113SensorName nameParentLeaf(const Info& sensorInfo)
114{
115 const auto pos = sensorInfo.sensorPath.find_last_of('/');
116 const auto leaf = sensorInfo.sensorPath.substr(pos + 1);
117
118 const auto remaining = sensorInfo.sensorPath.substr(0, pos);
119
120 const auto parentPos = remaining.find_last_of('/');
121 auto parent = remaining.substr(parentPos + 1);
122
123 parent += "_" + leaf;
124 return parent;
125}
126
Tom Joseph816e92b2017-09-06 19:23:00 +0530127GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
128 const InstancePath& path,
129 const DbusInterface& interface)
130{
131 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
132 GetSensorResponse response {};
133 auto responseData = reinterpret_cast<GetReadingResponse*>(response.data());
134
135 auto service = ipmi::getService(bus, interface, path);
136
137 const auto& interfaceList = sensorInfo.propertyInterfaces;
138
139 for (const auto& interface : interfaceList)
140 {
141 for (const auto& property : interface.second)
142 {
143 auto propValue = ipmi::getDbusProperty(bus,
144 service,
145 path,
146 interface.first,
147 property.first);
148
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500149 for (const auto& value : std::get<OffsetValueMap>(property.second))
Tom Joseph816e92b2017-09-06 19:23:00 +0530150 {
151 if (propValue == value.second.assert)
152 {
153 setOffset(value.first, responseData);
154 break;
155 }
156
157 }
158 }
159 }
160
161 return response;
162}
163
164GetSensorResponse assertion(const Info& sensorInfo)
165{
166 return mapDbusToAssertion(sensorInfo,
167 sensorInfo.sensorPath,
168 sensorInfo.sensorInterface);
169}
170
Tom Josephe4014fc2017-09-06 23:57:36 +0530171GetSensorResponse eventdata2(const Info& sensorInfo)
172{
173 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
174 GetSensorResponse response {};
175 auto responseData = reinterpret_cast<GetReadingResponse*>(response.data());
176
177 auto service = ipmi::getService(bus,
178 sensorInfo.sensorInterface,
179 sensorInfo.sensorPath);
180
181 const auto& interfaceList = sensorInfo.propertyInterfaces;
182
183 for (const auto& interface : interfaceList)
184 {
185 for (const auto& property : interface.second)
186 {
187 auto propValue = ipmi::getDbusProperty(bus,
188 service,
189 sensorInfo.sensorPath,
190 interface.first,
191 property.first);
192
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500193 for (const auto& value : std::get<OffsetValueMap>(property.second))
Tom Josephe4014fc2017-09-06 23:57:36 +0530194 {
195 if (propValue == value.second.assert)
196 {
197 setReading(value.first, responseData);
198 break;
199 }
200 }
201 }
202 }
203
204 return response;
205}
206
Tom Joseph816e92b2017-09-06 19:23:00 +0530207} //namespace get
208
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500209namespace set
210{
211
212IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
213 const std::string& sensorPath,
214 const std::string& command,
215 const std::string& sensorInterface)
216{
217 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
218 using namespace std::string_literals;
219
Tom Josephc6a571a2017-11-28 16:46:22 +0530220 auto dbusService = getService(bus,
221 sensorInterface,
222 sensorPath);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500223
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500224 return bus.new_method_call(dbusService.c_str(),
Tom Josephc6a571a2017-11-28 16:46:22 +0530225 sensorPath.c_str(),
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500226 updateInterface.c_str(),
227 command.c_str());
228}
229
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500230ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData,
231 const Info& sensorInfo,
232 uint8_t data)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500233{
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500234 auto msg = makeDbusMsg(
235 "org.freedesktop.DBus.Properties",
236 sensorInfo.sensorPath,
237 "Set",
238 sensorInfo.sensorInterface);
239
240 const auto& interface = sensorInfo.propertyInterfaces.begin();
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500241 msg.append(interface->first);
242 for (const auto& property : interface->second)
243 {
244 msg.append(property.first);
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500245 const auto& iter = std::get<OffsetValueMap>(property.second).find(data);
246 if (iter == std::get<OffsetValueMap>(property.second).end())
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500247 {
248 log<level::ERR>("Invalid event data");
249 return IPMI_CC_PARM_OUT_OF_RANGE;
250 }
251 msg.append(iter->second.assert);
252 }
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500253 return updateToDbus(msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500254}
255
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500256ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
257 const Info& sensorInfo)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500258{
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500259 auto msg = makeDbusMsg(
260 "org.freedesktop.DBus.Properties",
261 sensorInfo.sensorPath,
262 "Set",
263 sensorInfo.sensorInterface);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500264
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500265 std::bitset<16> assertionSet(getAssertionSet(cmdData).first);
266 std::bitset<16> deassertionSet(getAssertionSet(cmdData).second);
267
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500268 const auto& interface = sensorInfo.propertyInterfaces.begin();
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500269 msg.append(interface->first);
270 for (const auto& property : interface->second)
271 {
272 msg.append(property.first);
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500273 for (const auto& value : std::get<OffsetValueMap>(property.second))
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500274 {
275 if (assertionSet.test(value.first))
276 {
277 msg.append(value.second.assert);
278 }
279 if (deassertionSet.test(value.first))
280 {
281 msg.append(value.second.deassert);
282 }
283 }
284 }
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500285 return updateToDbus(msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500286}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500287
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500288}//namespace set
289
290namespace notify
291{
292
293IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
294 const std::string& sensorPath,
295 const std::string& command,
296 const std::string& sensorInterface)
297{
298 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
299 using namespace std::string_literals;
300
Dhruvaraj Subhashchandranf915f852017-09-14 07:01:48 -0500301 static const auto dbusPath = "/xyz/openbmc_project/inventory"s;
302 std::string dbusService = ipmi::getService(bus, updateInterface, dbusPath);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500303
304 return bus.new_method_call(dbusService.c_str(),
305 dbusPath.c_str(),
306 updateInterface.c_str(),
307 command.c_str());
308}
309
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500310ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
311 const Info& sensorInfo)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500312{
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500313 auto msg = makeDbusMsg(
314 sensorInfo.sensorInterface,
315 sensorInfo.sensorPath,
316 "Notify",
317 sensorInfo.sensorInterface);
318
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500319 std::bitset<16> assertionSet(getAssertionSet(cmdData).first);
320 std::bitset<16> deassertionSet(getAssertionSet(cmdData).second);
321 ipmi::sensor::ObjectMap objects;
322 ipmi::sensor::InterfaceMap interfaces;
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500323 for (const auto& interface : sensorInfo.propertyInterfaces)
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500324 {
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500325 //For a property like functional state the result will be
326 //calculated based on the true value of all conditions.
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500327 for (const auto& property : interface.second)
328 {
329 ipmi::sensor::PropertyMap props;
330 bool valid = false;
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500331 auto result = true;
332 for (const auto& value : std::get<OffsetValueMap>(property.second))
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500333 {
334 if (assertionSet.test(value.first))
335 {
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -0500336 //Skip update if skipOn is ASSERT
337 if (SkipAssertion::ASSERT == value.second.skip)
338 {
339 return IPMI_CC_OK;
340 }
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500341 result = result && value.second.assert.get<bool>();
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500342 valid = true;
343 }
344 else if (deassertionSet.test(value.first))
345 {
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -0500346 //Skip update if skipOn is DEASSERT
347 if (SkipAssertion::DEASSERT == value.second.skip)
348 {
349 return IPMI_CC_OK;
350 }
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500351 result = result && value.second.deassert.get<bool>();
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500352 valid = true;
353 }
354 }
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500355 for (const auto& value :
356 std::get<PreReqOffsetValueMap>(property.second))
357 {
358 if (assertionSet.test(value.first))
359 {
360 result = result && value.second.assert.get<bool>();
361 }
362 else if (deassertionSet.test(value.first))
363 {
364 result = result && value.second.deassert.get<bool>();
365 }
366 }
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500367 if (valid)
368 {
Dhruvaraj Subhashchandrane245e4e2017-10-03 03:58:05 -0500369 props.emplace(property.first, result);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500370 interfaces.emplace(interface.first, std::move(props));
371 }
372 }
373 }
Dhruvaraj Subhashchandrane84841c2017-08-22 07:40:27 -0500374
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500375 objects.emplace(sensorInfo.sensorPath, std::move(interfaces));
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500376 msg.append(std::move(objects));
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500377 return updateToDbus(msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500378}
Tom Joseph816e92b2017-09-06 19:23:00 +0530379
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500380}//namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530381
382namespace inventory
383{
384
385namespace get
386{
387
388GetSensorResponse assertion(const Info& sensorInfo)
389{
390 namespace fs = std::experimental::filesystem;
391
392 fs::path path{ipmi::sensor::inventoryRoot};
393 path += sensorInfo.sensorPath;
394
395 return ipmi::sensor::get::mapDbusToAssertion(
396 sensorInfo,
397 path.string(),
398 sensorInfo.propertyInterfaces.begin()->first);
399}
400
401} //namespace get
402
403} // namespace inventory
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500404}//namespace sensor
405}//namespace ipmi