blob: dd913e19c274b9ec796c485041eff3cbb03c3dae [file] [log] [blame]
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001/*
2// Copyright (c) 2017 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
Patrick Venture31b35d52019-10-20 13:25:16 -070017#include "sensorcommands.hpp"
18
19#include "commandutils.hpp"
20#include "ipmi_to_redfish_hooks.hpp"
21#include "sdrutils.hpp"
22#include "sensorutils.hpp"
23#include "storagecommands.hpp"
24
Patrick Venturef777e1b2019-09-25 17:45:44 -070025#include <algorithm>
Patrick Venture44ec88e2019-09-25 17:48:29 -070026#include <array>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070027#include <boost/algorithm/string.hpp>
28#include <boost/container/flat_map.hpp>
29#include <chrono>
30#include <cmath>
Patrick Venture5c2d26e2019-09-25 17:43:53 -070031#include <cstring>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070032#include <iostream>
James Feist2a265d52019-04-08 11:16:27 -070033#include <ipmid/api.hpp>
Vernon Mauery5480ef62019-03-20 13:43:11 -070034#include <ipmid/utils.hpp>
Patrick Ventureb10ec8b2019-09-25 16:39:14 -070035#include <map>
Patrick Venturefd2a9382019-09-25 17:47:25 -070036#include <memory>
Patrick Venturec4e9de62019-09-25 17:40:54 -070037#include <optional>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070038#include <phosphor-logging/log.hpp>
39#include <sdbusplus/bus.hpp>
Patrick Venturee6154022019-09-25 17:50:25 -070040#include <stdexcept>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070041#include <string>
Patrick Venture38f46f22019-09-25 17:41:26 -070042#include <utility>
Patrick Ventureeb02a5c2019-09-25 17:44:43 -070043#include <variant>
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070044
45namespace ipmi
46{
47using ManagedObjectType =
48 std::map<sdbusplus::message::object_path,
49 std::map<std::string, std::map<std::string, DbusVariant>>>;
50
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070051static constexpr int sensorListUpdatePeriod = 10;
52static constexpr int sensorMapUpdatePeriod = 2;
53
54constexpr size_t maxSDRTotalSize =
55 76; // Largest SDR Record Size (type 01) + SDR Overheader Size
56constexpr static const uint32_t noTimestamp = 0xFFFFFFFF;
57
58static uint16_t sdrReservationID;
59static uint32_t sdrLastAdd = noTimestamp;
60static uint32_t sdrLastRemove = noTimestamp;
61
Richard Marian Thomaiyar01fbcb52018-11-19 22:04:34 +053062SensorSubTree sensorTree;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070063static boost::container::flat_map<std::string, ManagedObjectType> SensorCache;
64
Jason M. Bills17add592018-11-12 14:30:12 -080065// Specify the comparison required to sort and find char* map objects
66struct CmpStr
67{
68 bool operator()(const char *a, const char *b) const
69 {
70 return std::strcmp(a, b) < 0;
71 }
72};
73const static boost::container::flat_map<const char *, SensorUnits, CmpStr>
74 sensorUnits{{{"temperature", SensorUnits::degreesC},
75 {"voltage", SensorUnits::volts},
76 {"current", SensorUnits::amps},
77 {"fan_tach", SensorUnits::rpm},
78 {"power", SensorUnits::watts}}};
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070079
80void registerSensorFunctions() __attribute__((constructor));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070081
82static sdbusplus::bus::match::match sensorAdded(
Vernon Mauery15419dd2019-05-24 09:40:30 -070083 *getSdBus(),
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070084 "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/"
85 "sensors/'",
86 [](sdbusplus::message::message &m) {
87 sensorTree.clear();
88 sdrLastAdd = std::chrono::duration_cast<std::chrono::seconds>(
89 std::chrono::system_clock::now().time_since_epoch())
90 .count();
91 });
92
93static sdbusplus::bus::match::match sensorRemoved(
Vernon Mauery15419dd2019-05-24 09:40:30 -070094 *getSdBus(),
Jason M. Bills3f7c5e42018-10-03 14:00:41 -070095 "type='signal',member='InterfacesRemoved',arg0path='/xyz/openbmc_project/"
96 "sensors/'",
97 [](sdbusplus::message::message &m) {
98 sensorTree.clear();
99 sdrLastRemove = std::chrono::duration_cast<std::chrono::seconds>(
100 std::chrono::system_clock::now().time_since_epoch())
101 .count();
102 });
103
James Feist392786a2019-03-19 13:36:10 -0700104// this keeps track of deassertions for sensor event status command. A
105// deasertion can only happen if an assertion was seen first.
106static boost::container::flat_map<
107 std::string, boost::container::flat_map<std::string, std::optional<bool>>>
108 thresholdDeassertMap;
109
110static sdbusplus::bus::match::match thresholdChanged(
Vernon Mauery15419dd2019-05-24 09:40:30 -0700111 *getSdBus(),
James Feist392786a2019-03-19 13:36:10 -0700112 "type='signal',member='PropertiesChanged',interface='org.freedesktop.DBus."
113 "Properties',arg0namespace='xyz.openbmc_project.Sensor.Threshold'",
114 [](sdbusplus::message::message &m) {
115 boost::container::flat_map<std::string, std::variant<bool, double>>
116 values;
117 m.read(std::string(), values);
118
119 auto findAssert =
120 std::find_if(values.begin(), values.end(), [](const auto &pair) {
121 return pair.first.find("Alarm") != std::string::npos;
122 });
123 if (findAssert != values.end())
124 {
125 auto ptr = std::get_if<bool>(&(findAssert->second));
126 if (ptr == nullptr)
127 {
128 phosphor::logging::log<phosphor::logging::level::ERR>(
129 "thresholdChanged: Assert non bool");
130 return;
131 }
132 if (*ptr)
133 {
134 phosphor::logging::log<phosphor::logging::level::INFO>(
135 "thresholdChanged: Assert",
136 phosphor::logging::entry("SENSOR=%s", m.get_path()));
137 thresholdDeassertMap[m.get_path()][findAssert->first] = *ptr;
138 }
139 else
140 {
141 auto &value =
142 thresholdDeassertMap[m.get_path()][findAssert->first];
143 if (value)
144 {
145 phosphor::logging::log<phosphor::logging::level::INFO>(
146 "thresholdChanged: deassert",
147 phosphor::logging::entry("SENSOR=%s", m.get_path()));
148 value = *ptr;
149 }
150 }
151 }
152 });
153
James Feistaecaef72019-04-26 10:30:32 -0700154static void getSensorMaxMin(const SensorMap &sensorMap, double &max,
155 double &min)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700156{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700157 max = 127;
158 min = -128;
159
James Feistaecaef72019-04-26 10:30:32 -0700160 auto sensorObject = sensorMap.find("xyz.openbmc_project.Sensor.Value");
161 auto critical =
162 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
163 auto warning =
164 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
165
166 if (sensorObject != sensorMap.end())
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700167 {
James Feistaecaef72019-04-26 10:30:32 -0700168 auto maxMap = sensorObject->second.find("MaxValue");
169 auto minMap = sensorObject->second.find("MinValue");
170
171 if (maxMap != sensorObject->second.end())
172 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700173 max = std::visit(VariantToDoubleVisitor(), maxMap->second);
James Feistaecaef72019-04-26 10:30:32 -0700174 }
175 if (minMap != sensorObject->second.end())
176 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700177 min = std::visit(VariantToDoubleVisitor(), minMap->second);
James Feistaecaef72019-04-26 10:30:32 -0700178 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700179 }
James Feistaecaef72019-04-26 10:30:32 -0700180 if (critical != sensorMap.end())
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700181 {
James Feistaecaef72019-04-26 10:30:32 -0700182 auto lower = critical->second.find("CriticalLow");
183 auto upper = critical->second.find("CriticalHigh");
184 if (lower != critical->second.end())
185 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700186 double value = std::visit(VariantToDoubleVisitor(), lower->second);
James Feistaecaef72019-04-26 10:30:32 -0700187 min = std::min(value, min);
188 }
189 if (upper != critical->second.end())
190 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700191 double value = std::visit(VariantToDoubleVisitor(), upper->second);
James Feistaecaef72019-04-26 10:30:32 -0700192 max = std::max(value, max);
193 }
194 }
195 if (warning != sensorMap.end())
196 {
197
198 auto lower = warning->second.find("WarningLow");
199 auto upper = warning->second.find("WarningHigh");
200 if (lower != warning->second.end())
201 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700202 double value = std::visit(VariantToDoubleVisitor(), lower->second);
James Feistaecaef72019-04-26 10:30:32 -0700203 min = std::min(value, min);
204 }
205 if (upper != warning->second.end())
206 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700207 double value = std::visit(VariantToDoubleVisitor(), upper->second);
James Feistaecaef72019-04-26 10:30:32 -0700208 max = std::max(value, max);
209 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700210 }
211}
212
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700213static bool getSensorMap(std::string sensorConnection, std::string sensorPath,
214 SensorMap &sensorMap)
215{
216 static boost::container::flat_map<
217 std::string, std::chrono::time_point<std::chrono::steady_clock>>
218 updateTimeMap;
219
220 auto updateFind = updateTimeMap.find(sensorConnection);
221 auto lastUpdate = std::chrono::time_point<std::chrono::steady_clock>();
222 if (updateFind != updateTimeMap.end())
223 {
224 lastUpdate = updateFind->second;
225 }
226
227 auto now = std::chrono::steady_clock::now();
228
229 if (std::chrono::duration_cast<std::chrono::seconds>(now - lastUpdate)
230 .count() > sensorMapUpdatePeriod)
231 {
232 updateTimeMap[sensorConnection] = now;
233
Vernon Mauery15419dd2019-05-24 09:40:30 -0700234 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
235 auto managedObj = dbus->new_method_call(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700236 sensorConnection.c_str(), "/", "org.freedesktop.DBus.ObjectManager",
237 "GetManagedObjects");
238
239 ManagedObjectType managedObjects;
240 try
241 {
Vernon Mauery15419dd2019-05-24 09:40:30 -0700242 auto reply = dbus->call(managedObj);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700243 reply.read(managedObjects);
244 }
245 catch (sdbusplus::exception_t &)
246 {
247 phosphor::logging::log<phosphor::logging::level::ERR>(
248 "Error getting managed objects from connection",
249 phosphor::logging::entry("CONNECTION=%s",
250 sensorConnection.c_str()));
251 return false;
252 }
253
254 SensorCache[sensorConnection] = managedObjects;
255 }
256 auto connection = SensorCache.find(sensorConnection);
257 if (connection == SensorCache.end())
258 {
259 return false;
260 }
261 auto path = connection->second.find(sensorPath);
262 if (path == connection->second.end())
263 {
264 return false;
265 }
266 sensorMap = path->second;
267
268 return true;
269}
270
271/* sensor commands */
272ipmi_ret_t ipmiSensorWildcardHandler(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
273 ipmi_request_t request,
274 ipmi_response_t response,
275 ipmi_data_len_t dataLen,
276 ipmi_context_t context)
277{
278 *dataLen = 0;
279 printCommand(+netfn, +cmd);
280 return IPMI_CC_INVALID;
281}
282
James Feist7aaf3fe2019-06-25 11:52:11 -0700283namespace meHealth
284{
285constexpr const char *busname = "xyz.openbmc_project.NodeManagerProxy";
286constexpr const char *path = "/xyz/openbmc_project/status/me";
287constexpr const char *interface = "xyz.openbmc_project.SetHealth";
288constexpr const char *method = "SetHealth";
289constexpr const char *critical = "critical";
290constexpr const char *warning = "warning";
291constexpr const char *ok = "ok";
292} // namespace meHealth
293
294static void setMeStatus(uint8_t eventData2, uint8_t eventData3, bool disable)
295{
296 constexpr const std::array<uint8_t, 10> critical = {
297 0x1, 0x2, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xD, 0xE};
298 constexpr const std::array<uint8_t, 5> warning = {0x3, 0xA, 0x13, 0x19,
299 0x1A};
300
301 std::string state;
302 if (std::find(critical.begin(), critical.end(), eventData2) !=
303 critical.end())
304 {
305 state = meHealth::critical;
306 }
307 // special case 0x3 as we only care about a few states
308 else if (eventData2 == 0x3)
309 {
310 if (eventData3 <= 0x2)
311 {
312 state = meHealth::warning;
313 }
314 else
315 {
316 return;
317 }
318 }
319 else if (std::find(warning.begin(), warning.end(), eventData2) !=
320 warning.end())
321 {
322 state = meHealth::warning;
323 }
324 else
325 {
326 return;
327 }
328 if (disable)
329 {
330 state = meHealth::ok;
331 }
332
333 std::shared_ptr<sdbusplus::asio::connection> dbus = getSdBus();
334 auto setHealth =
335 dbus->new_method_call(meHealth::busname, meHealth::path,
336 meHealth::interface, meHealth::method);
337 setHealth.append(std::to_string(static_cast<size_t>(eventData2)), state);
338 try
339 {
340 dbus->call(setHealth);
341 }
342 catch (sdbusplus::exception_t &)
343 {
344 phosphor::logging::log<phosphor::logging::level::ERR>(
345 "Failed to set ME Health");
346 }
347}
348
Jason M. Billsae6bdb12019-04-02 12:00:04 -0700349ipmi::RspType<> ipmiSenPlatformEvent(ipmi::message::Payload &p)
350{
James Feist7aaf3fe2019-06-25 11:52:11 -0700351 constexpr const uint8_t meId = 0x2C;
352 constexpr const uint8_t meSensorNum = 0x17;
353 constexpr const uint8_t disabled = 0x80;
354
Jason M. Billsae6bdb12019-04-02 12:00:04 -0700355 uint8_t generatorID = 0;
356 uint8_t evmRev = 0;
357 uint8_t sensorType = 0;
358 uint8_t sensorNum = 0;
359 uint8_t eventType = 0;
360 uint8_t eventData1 = 0;
361 std::optional<uint8_t> eventData2 = 0;
362 std::optional<uint8_t> eventData3 = 0;
363
364 // todo: This check is supposed to be based on the incoming channel.
365 // e.g. system channel will provide upto 8 bytes including generator
366 // ID, but ipmb channel will provide only up to 7 bytes without the
367 // generator ID.
368 // Support for this check is coming in future patches, so for now just base
369 // it on if the first byte is the EvMRev (0x04).
370 if (p.size() && p.data()[0] == 0x04)
371 {
372 p.unpack(evmRev, sensorType, sensorNum, eventType, eventData1,
373 eventData2, eventData3);
374 // todo: the generator ID for this channel is supposed to come from the
375 // IPMB requesters slave address. Support for this is coming in future
376 // patches, so for now just assume it is coming from the ME (0x2C).
377 generatorID = 0x2C;
378 }
379 else
380 {
381 p.unpack(generatorID, evmRev, sensorType, sensorNum, eventType,
382 eventData1, eventData2, eventData3);
383 }
384 if (!p.fullyUnpacked())
385 {
386 return ipmi::responseReqDataLenInvalid();
387 }
388
Jason M. Bills6dd8f042019-04-11 10:39:02 -0700389 // Send this request to the Redfish hooks to log it as a Redfish message
390 // instead. There is no need to add it to the SEL, so just return success.
391 intel_oem::ipmi::sel::checkRedfishHooks(
392 generatorID, evmRev, sensorType, sensorNum, eventType, eventData1,
393 eventData2.value_or(0xFF), eventData3.value_or(0xFF));
Jason M. Billsae6bdb12019-04-02 12:00:04 -0700394
James Feist7aaf3fe2019-06-25 11:52:11 -0700395 if (generatorID == meId && sensorNum == meSensorNum && eventData2 &&
396 eventData3)
397 {
398 setMeStatus(*eventData2, *eventData3, (eventType & disabled));
399 }
400
Jason M. Billsae6bdb12019-04-02 12:00:04 -0700401 return ipmi::responseSuccess();
402}
403
James Feist0cd014a2019-04-08 15:04:33 -0700404ipmi::RspType<uint8_t, uint8_t, uint8_t, std::optional<uint8_t>>
405 ipmiSenGetSensorReading(uint8_t sensnum)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700406{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700407 std::string connection;
408 std::string path;
409
410 auto status = getSensorConnection(sensnum, connection, path);
411 if (status)
412 {
James Feist0cd014a2019-04-08 15:04:33 -0700413 return ipmi::response(status);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700414 }
415
416 SensorMap sensorMap;
417 if (!getSensorMap(connection, path, sensorMap))
418 {
James Feist0cd014a2019-04-08 15:04:33 -0700419 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700420 }
421 auto sensorObject = sensorMap.find("xyz.openbmc_project.Sensor.Value");
422
423 if (sensorObject == sensorMap.end() ||
424 sensorObject->second.find("Value") == sensorObject->second.end())
425 {
James Feist0cd014a2019-04-08 15:04:33 -0700426 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700427 }
James Feist0cd014a2019-04-08 15:04:33 -0700428 auto &valueVariant = sensorObject->second["Value"];
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700429 double reading = std::visit(VariantToDoubleVisitor(), valueVariant);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700430
Yong Li1f2eb5e2019-05-23 14:07:17 +0800431 double max = 0;
432 double min = 0;
James Feistaecaef72019-04-26 10:30:32 -0700433 getSensorMaxMin(sensorMap, max, min);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700434
435 int16_t mValue = 0;
436 int16_t bValue = 0;
437 int8_t rExp = 0;
438 int8_t bExp = 0;
439 bool bSigned = false;
440
441 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
442 {
James Feist0cd014a2019-04-08 15:04:33 -0700443 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700444 }
445
James Feist0cd014a2019-04-08 15:04:33 -0700446 uint8_t value =
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700447 scaleIPMIValueFromDouble(reading, mValue, rExp, bValue, bExp, bSigned);
James Feist0cd014a2019-04-08 15:04:33 -0700448 uint8_t operation =
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700449 static_cast<uint8_t>(IPMISensorReadingByte2::sensorScanningEnable);
James Feist0cd014a2019-04-08 15:04:33 -0700450 operation |=
James Feist81a95c12019-03-01 15:08:28 -0800451 static_cast<uint8_t>(IPMISensorReadingByte2::eventMessagesEnable);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700452
James Feist0cd014a2019-04-08 15:04:33 -0700453 uint8_t thresholds = 0;
454
455 auto warningObject =
456 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
457 if (warningObject != sensorMap.end())
458 {
459 auto alarmHigh = warningObject->second.find("WarningAlarmHigh");
460 auto alarmLow = warningObject->second.find("WarningAlarmLow");
461 if (alarmHigh != warningObject->second.end())
462 {
463 if (std::get<bool>(alarmHigh->second))
464 {
465 thresholds |= static_cast<uint8_t>(
466 IPMISensorReadingByte3::upperNonCritical);
467 }
468 }
469 if (alarmLow != warningObject->second.end())
470 {
471 if (std::get<bool>(alarmLow->second))
472 {
473 thresholds |= static_cast<uint8_t>(
474 IPMISensorReadingByte3::lowerNonCritical);
475 }
476 }
477 }
478
479 auto criticalObject =
480 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
481 if (criticalObject != sensorMap.end())
482 {
483 auto alarmHigh = criticalObject->second.find("CriticalAlarmHigh");
484 auto alarmLow = criticalObject->second.find("CriticalAlarmLow");
485 if (alarmHigh != criticalObject->second.end())
486 {
487 if (std::get<bool>(alarmHigh->second))
488 {
489 thresholds |=
490 static_cast<uint8_t>(IPMISensorReadingByte3::upperCritical);
491 }
492 }
493 if (alarmLow != criticalObject->second.end())
494 {
495 if (std::get<bool>(alarmLow->second))
496 {
497 thresholds |=
498 static_cast<uint8_t>(IPMISensorReadingByte3::lowerCritical);
499 }
500 }
501 }
502
503 // no discrete as of today so optional byte is never returned
504 return ipmi::responseSuccess(value, operation, thresholds, std::nullopt);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700505}
506
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000507/** @brief implements the Set Sensor threshold command
508 * @param sensorNumber - sensor number
509 * @param lowerNonCriticalThreshMask
510 * @param lowerCriticalThreshMask
511 * @param lowerNonRecovThreshMask
512 * @param upperNonCriticalThreshMask
513 * @param upperCriticalThreshMask
514 * @param upperNonRecovThreshMask
515 * @param reserved
516 * @param lowerNonCritical - lower non-critical threshold
517 * @param lowerCritical - Lower critical threshold
518 * @param lowerNonRecoverable - Lower non recovarable threshold
519 * @param upperNonCritical - Upper non-critical threshold
520 * @param upperCritical - Upper critical
521 * @param upperNonRecoverable - Upper Non-recoverable
522 *
523 * @returns IPMI completion code
524 */
525ipmi::RspType<> ipmiSenSetSensorThresholds(
526 uint8_t sensorNum, bool lowerNonCriticalThreshMask,
527 bool lowerCriticalThreshMask, bool lowerNonRecovThreshMask,
528 bool upperNonCriticalThreshMask, bool upperCriticalThreshMask,
529 bool upperNonRecovThreshMask, uint2_t reserved, uint8_t lowerNonCritical,
530 uint8_t lowerCritical, uint8_t lowerNonRecoverable,
531 uint8_t upperNonCritical, uint8_t upperCritical,
532 uint8_t upperNonRecoverable)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700533{
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000534 constexpr uint8_t thresholdMask = 0xFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700535
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000536 if (reserved)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700537 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000538 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700539 }
540
541 // lower nc and upper nc not suppported on any sensor
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000542 if (lowerNonRecovThreshMask || upperNonRecovThreshMask)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700543 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000544 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700545 }
546
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000547 // if none of the threshold mask are set, nothing to do
548 if (!(lowerNonCriticalThreshMask | lowerCriticalThreshMask |
549 lowerNonRecovThreshMask | upperNonCriticalThreshMask |
550 upperCriticalThreshMask | upperNonRecovThreshMask))
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700551 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000552 return ipmi::responseSuccess();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700553 }
554
555 std::string connection;
556 std::string path;
557
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000558 ipmi::Cc status = getSensorConnection(sensorNum, connection, path);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700559 if (status)
560 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000561 return ipmi::response(status);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700562 }
563 SensorMap sensorMap;
564 if (!getSensorMap(connection, path, sensorMap))
565 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000566 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700567 }
568
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700569 double max = 0;
570 double min = 0;
James Feistaecaef72019-04-26 10:30:32 -0700571 getSensorMaxMin(sensorMap, max, min);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700572
573 int16_t mValue = 0;
574 int16_t bValue = 0;
575 int8_t rExp = 0;
576 int8_t bExp = 0;
577 bool bSigned = false;
578
579 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
580 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000581 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700582 }
583
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700584 // store a vector of property name, value to set, and interface
585 std::vector<std::tuple<std::string, uint8_t, std::string>> thresholdsToSet;
586
587 // define the indexes of the tuple
588 constexpr uint8_t propertyName = 0;
589 constexpr uint8_t thresholdValue = 1;
590 constexpr uint8_t interface = 2;
591 // verifiy all needed fields are present
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000592 if (lowerCriticalThreshMask || upperCriticalThreshMask)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700593 {
594 auto findThreshold =
595 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
596 if (findThreshold == sensorMap.end())
597 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000598 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700599 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000600 if (lowerCriticalThreshMask)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700601 {
602 auto findLower = findThreshold->second.find("CriticalLow");
603 if (findLower == findThreshold->second.end())
604 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000605 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700606 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000607 thresholdsToSet.emplace_back("CriticalLow", lowerCritical,
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700608 findThreshold->first);
609 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000610 if (upperCriticalThreshMask)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700611 {
612 auto findUpper = findThreshold->second.find("CriticalHigh");
613 if (findUpper == findThreshold->second.end())
614 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000615 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700616 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000617 thresholdsToSet.emplace_back("CriticalHigh", upperCritical,
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700618 findThreshold->first);
619 }
620 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000621 if (lowerNonCriticalThreshMask || upperNonCriticalThreshMask)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700622 {
623 auto findThreshold =
624 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
625 if (findThreshold == sensorMap.end())
626 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000627 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700628 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000629 if (lowerNonCriticalThreshMask)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700630 {
631 auto findLower = findThreshold->second.find("WarningLow");
632 if (findLower == findThreshold->second.end())
633 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000634 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700635 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000636 thresholdsToSet.emplace_back("WarningLow", lowerNonCritical,
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700637 findThreshold->first);
638 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000639 if (upperNonCriticalThreshMask)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700640 {
641 auto findUpper = findThreshold->second.find("WarningHigh");
642 if (findUpper == findThreshold->second.end())
643 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000644 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700645 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000646 thresholdsToSet.emplace_back("WarningHigh", upperNonCritical,
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700647 findThreshold->first);
648 }
649 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700650 for (const auto &property : thresholdsToSet)
651 {
652 // from section 36.3 in the IPMI Spec, assume all linear
653 double valueToSet = ((mValue * std::get<thresholdValue>(property)) +
Josh Lehan86236a22019-11-18 17:53:33 -0800654 (bValue * std::pow(10.0, bExp))) *
655 std::pow(10.0, rExp);
Vernon Mauery15419dd2019-05-24 09:40:30 -0700656 setDbusProperty(
657 *getSdBus(), connection, path, std::get<interface>(property),
658 std::get<propertyName>(property), ipmi::Value(valueToSet));
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700659 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000660 return ipmi::responseSuccess();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700661}
662
James Feist902c4c52019-04-16 14:51:31 -0700663IPMIThresholds getIPMIThresholds(const SensorMap &sensorMap)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700664{
James Feist902c4c52019-04-16 14:51:31 -0700665 IPMIThresholds resp;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700666 auto warningInterface =
667 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
668 auto criticalInterface =
669 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
670
671 if ((warningInterface != sensorMap.end()) ||
672 (criticalInterface != sensorMap.end()))
673 {
674 auto sensorPair = sensorMap.find("xyz.openbmc_project.Sensor.Value");
675
676 if (sensorPair == sensorMap.end())
677 {
678 // should not have been able to find a sensor not implementing
679 // the sensor object
James Feist902c4c52019-04-16 14:51:31 -0700680 throw std::runtime_error("Invalid sensor map");
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700681 }
682
Chen,Yugang606dd9f2019-07-01 10:37:30 +0800683 double max = 0;
684 double min = 0;
James Feistaecaef72019-04-26 10:30:32 -0700685 getSensorMaxMin(sensorMap, max, min);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700686
687 int16_t mValue = 0;
688 int16_t bValue = 0;
689 int8_t rExp = 0;
690 int8_t bExp = 0;
691 bool bSigned = false;
692
693 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
694 {
James Feist902c4c52019-04-16 14:51:31 -0700695 throw std::runtime_error("Invalid sensor atrributes");
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700696 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700697 if (warningInterface != sensorMap.end())
698 {
699 auto &warningMap = warningInterface->second;
700
701 auto warningHigh = warningMap.find("WarningHigh");
702 auto warningLow = warningMap.find("WarningLow");
703
704 if (warningHigh != warningMap.end())
705 {
James Feist902c4c52019-04-16 14:51:31 -0700706
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700707 double value =
708 std::visit(VariantToDoubleVisitor(), warningHigh->second);
James Feist902c4c52019-04-16 14:51:31 -0700709 resp.warningHigh = scaleIPMIValueFromDouble(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700710 value, mValue, rExp, bValue, bExp, bSigned);
711 }
712 if (warningLow != warningMap.end())
713 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700714 double value =
715 std::visit(VariantToDoubleVisitor(), warningLow->second);
James Feist902c4c52019-04-16 14:51:31 -0700716 resp.warningLow = scaleIPMIValueFromDouble(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700717 value, mValue, rExp, bValue, bExp, bSigned);
718 }
719 }
720 if (criticalInterface != sensorMap.end())
721 {
722 auto &criticalMap = criticalInterface->second;
723
724 auto criticalHigh = criticalMap.find("CriticalHigh");
725 auto criticalLow = criticalMap.find("CriticalLow");
726
727 if (criticalHigh != criticalMap.end())
728 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700729 double value =
730 std::visit(VariantToDoubleVisitor(), criticalHigh->second);
James Feist902c4c52019-04-16 14:51:31 -0700731 resp.criticalHigh = scaleIPMIValueFromDouble(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700732 value, mValue, rExp, bValue, bExp, bSigned);
733 }
734 if (criticalLow != criticalMap.end())
735 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -0700736 double value =
737 std::visit(VariantToDoubleVisitor(), criticalLow->second);
James Feist902c4c52019-04-16 14:51:31 -0700738 resp.criticalLow = scaleIPMIValueFromDouble(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700739 value, mValue, rExp, bValue, bExp, bSigned);
740 }
741 }
742 }
James Feist902c4c52019-04-16 14:51:31 -0700743 return resp;
744}
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700745
James Feist902c4c52019-04-16 14:51:31 -0700746ipmi::RspType<uint8_t, // readable
747 uint8_t, // lowerNCrit
748 uint8_t, // lowerCrit
749 uint8_t, // lowerNrecoverable
750 uint8_t, // upperNC
751 uint8_t, // upperCrit
752 uint8_t> // upperNRecoverable
753 ipmiSenGetSensorThresholds(uint8_t sensorNumber)
754{
755 std::string connection;
756 std::string path;
757
758 auto status = getSensorConnection(sensorNumber, connection, path);
759 if (status)
760 {
761 return ipmi::response(status);
762 }
763
764 SensorMap sensorMap;
765 if (!getSensorMap(connection, path, sensorMap))
766 {
767 return ipmi::responseResponseError();
768 }
769
770 IPMIThresholds thresholdData;
771 try
772 {
773 thresholdData = getIPMIThresholds(sensorMap);
774 }
775 catch (std::exception &)
776 {
777 return ipmi::responseResponseError();
778 }
779
780 uint8_t readable = 0;
781 uint8_t lowerNC = 0;
782 uint8_t lowerCritical = 0;
783 uint8_t lowerNonRecoverable = 0;
784 uint8_t upperNC = 0;
785 uint8_t upperCritical = 0;
786 uint8_t upperNonRecoverable = 0;
787
788 if (thresholdData.warningHigh)
789 {
790 readable |=
791 1 << static_cast<uint8_t>(IPMIThresholdRespBits::upperNonCritical);
792 upperNC = *thresholdData.warningHigh;
793 }
794 if (thresholdData.warningLow)
795 {
796 readable |=
797 1 << static_cast<uint8_t>(IPMIThresholdRespBits::lowerNonCritical);
798 lowerNC = *thresholdData.warningLow;
799 }
800
801 if (thresholdData.criticalHigh)
802 {
803 readable |=
804 1 << static_cast<uint8_t>(IPMIThresholdRespBits::upperCritical);
805 upperCritical = *thresholdData.criticalHigh;
806 }
807 if (thresholdData.criticalLow)
808 {
809 readable |=
810 1 << static_cast<uint8_t>(IPMIThresholdRespBits::lowerCritical);
811 lowerCritical = *thresholdData.criticalLow;
812 }
813
814 return ipmi::responseSuccess(readable, lowerNC, lowerCritical,
815 lowerNonRecoverable, upperNC, upperCritical,
816 upperNonRecoverable);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700817}
818
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000819/** @brief implements the get Sensor event enable command
820 * @param sensorNumber - sensor number
821 *
822 * @returns IPMI completion code plus response data
823 * - enabled - Sensor Event messages
824 * - assertionEnabledLsb - Assertion event messages
825 * - assertionEnabledMsb - Assertion event messages
826 * - deassertionEnabledLsb - Deassertion event messages
827 * - deassertionEnabledMsb - Deassertion event messages
828 */
829
830ipmi::RspType<uint8_t, // enabled
831 uint8_t, // assertionEnabledLsb
832 uint8_t, // assertionEnabledMsb
833 uint8_t, // deassertionEnabledLsb
834 uint8_t> // deassertionEnabledMsb
835 ipmiSenGetSensorEventEnable(uint8_t sensorNum)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700836{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700837 std::string connection;
838 std::string path;
839
Patrick Venturea41714c2019-09-25 16:59:41 -0700840 uint8_t enabled = 0;
841 uint8_t assertionEnabledLsb = 0;
842 uint8_t assertionEnabledMsb = 0;
843 uint8_t deassertionEnabledLsb = 0;
844 uint8_t deassertionEnabledMsb = 0;
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000845
846 auto status = getSensorConnection(sensorNum, connection, path);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700847 if (status)
848 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000849 return ipmi::response(status);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700850 }
851
852 SensorMap sensorMap;
853 if (!getSensorMap(connection, path, sensorMap))
854 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000855 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700856 }
857
858 auto warningInterface =
859 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
860 auto criticalInterface =
861 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700862 if ((warningInterface != sensorMap.end()) ||
863 (criticalInterface != sensorMap.end()))
864 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000865 enabled = static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700866 IPMISensorEventEnableByte2::sensorScanningEnable);
867 if (warningInterface != sensorMap.end())
868 {
869 auto &warningMap = warningInterface->second;
870
871 auto warningHigh = warningMap.find("WarningHigh");
872 auto warningLow = warningMap.find("WarningLow");
873 if (warningHigh != warningMap.end())
874 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000875 assertionEnabledLsb |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700876 IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000877 deassertionEnabledLsb |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700878 IPMISensorEventEnableThresholds::upperNonCriticalGoingLow);
879 }
880 if (warningLow != warningMap.end())
881 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000882 assertionEnabledLsb |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700883 IPMISensorEventEnableThresholds::lowerNonCriticalGoingLow);
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000884 deassertionEnabledLsb |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700885 IPMISensorEventEnableThresholds::lowerNonCriticalGoingHigh);
886 }
887 }
888 if (criticalInterface != sensorMap.end())
889 {
890 auto &criticalMap = criticalInterface->second;
891
892 auto criticalHigh = criticalMap.find("CriticalHigh");
893 auto criticalLow = criticalMap.find("CriticalLow");
894
895 if (criticalHigh != criticalMap.end())
896 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000897 assertionEnabledMsb |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700898 IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000899 deassertionEnabledMsb |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700900 IPMISensorEventEnableThresholds::upperCriticalGoingLow);
901 }
902 if (criticalLow != criticalMap.end())
903 {
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000904 assertionEnabledLsb |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700905 IPMISensorEventEnableThresholds::lowerCriticalGoingLow);
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000906 deassertionEnabledLsb |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700907 IPMISensorEventEnableThresholds::lowerCriticalGoingHigh);
908 }
909 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700910 }
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +0000911
912 return ipmi::responseSuccess(enabled, assertionEnabledLsb,
913 assertionEnabledMsb, deassertionEnabledLsb,
914 deassertionEnabledMsb);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700915}
916
917ipmi_ret_t ipmiSenGetSensorEventStatus(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
918 ipmi_request_t request,
919 ipmi_response_t response,
920 ipmi_data_len_t dataLen,
921 ipmi_context_t context)
922{
923 if (*dataLen != 1)
924 {
925 *dataLen = 0;
926 return IPMI_CC_REQ_DATA_LEN_INVALID;
927 }
928 *dataLen = 0; // default to 0 in case of an error
929
930 uint8_t sensnum = *(static_cast<uint8_t *>(request));
931
932 std::string connection;
933 std::string path;
934
935 auto status = getSensorConnection(sensnum, connection, path);
936 if (status)
937 {
938 return status;
939 }
940
941 SensorMap sensorMap;
942 if (!getSensorMap(connection, path, sensorMap))
943 {
944 return IPMI_CC_RESPONSE_ERROR;
945 }
946
947 auto warningInterface =
948 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Warning");
949 auto criticalInterface =
950 sensorMap.find("xyz.openbmc_project.Sensor.Threshold.Critical");
951
952 // zero out response buff
953 auto responseClear = static_cast<uint8_t *>(response);
954 std::fill(responseClear, responseClear + sizeof(SensorEventStatusResp), 0);
955 auto resp = static_cast<SensorEventStatusResp *>(response);
956 resp->enabled =
957 static_cast<uint8_t>(IPMISensorEventEnableByte2::sensorScanningEnable);
958
James Feist392786a2019-03-19 13:36:10 -0700959 std::optional<bool> criticalDeassertHigh =
960 thresholdDeassertMap[path]["CriticalAlarmHigh"];
961 std::optional<bool> criticalDeassertLow =
962 thresholdDeassertMap[path]["CriticalAlarmLow"];
963 std::optional<bool> warningDeassertHigh =
964 thresholdDeassertMap[path]["WarningAlarmHigh"];
965 std::optional<bool> warningDeassertLow =
966 thresholdDeassertMap[path]["WarningAlarmLow"];
967
968 if (criticalDeassertHigh && !*criticalDeassertHigh)
969 {
970 resp->deassertionsMSB |= static_cast<uint8_t>(
971 IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
972 }
973 if (criticalDeassertLow && !*criticalDeassertLow)
974 {
975 resp->deassertionsMSB |= static_cast<uint8_t>(
976 IPMISensorEventEnableThresholds::upperCriticalGoingLow);
977 }
978 if (warningDeassertHigh && !*warningDeassertHigh)
979 {
980 resp->deassertionsLSB |= static_cast<uint8_t>(
981 IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
982 }
983 if (warningDeassertLow && !*warningDeassertLow)
984 {
985 resp->deassertionsLSB |= static_cast<uint8_t>(
986 IPMISensorEventEnableThresholds::lowerNonCriticalGoingHigh);
987 }
988
Jason M. Bills3f7c5e42018-10-03 14:00:41 -0700989 if ((warningInterface != sensorMap.end()) ||
990 (criticalInterface != sensorMap.end()))
991 {
992 resp->enabled = static_cast<uint8_t>(
993 IPMISensorEventEnableByte2::eventMessagesEnable);
994 if (warningInterface != sensorMap.end())
995 {
996 auto &warningMap = warningInterface->second;
997
998 auto warningHigh = warningMap.find("WarningAlarmHigh");
999 auto warningLow = warningMap.find("WarningAlarmLow");
1000 auto warningHighAlarm = false;
1001 auto warningLowAlarm = false;
1002
1003 if (warningHigh != warningMap.end())
1004 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001005 warningHighAlarm = std::get<bool>(warningHigh->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001006 }
1007 if (warningLow != warningMap.end())
1008 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001009 warningLowAlarm = std::get<bool>(warningLow->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001010 }
1011 if (warningHighAlarm)
1012 {
1013 resp->assertionsLSB |= static_cast<uint8_t>(
1014 IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
1015 }
1016 if (warningLowAlarm)
1017 {
1018 resp->assertionsLSB |= 1; // lower nc going low
1019 }
1020 }
1021 if (criticalInterface != sensorMap.end())
1022 {
1023 auto &criticalMap = criticalInterface->second;
1024
1025 auto criticalHigh = criticalMap.find("CriticalAlarmHigh");
1026 auto criticalLow = criticalMap.find("CriticalAlarmLow");
1027 auto criticalHighAlarm = false;
1028 auto criticalLowAlarm = false;
1029
1030 if (criticalHigh != criticalMap.end())
1031 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001032 criticalHighAlarm = std::get<bool>(criticalHigh->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001033 }
1034 if (criticalLow != criticalMap.end())
1035 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001036 criticalLowAlarm = std::get<bool>(criticalLow->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001037 }
1038 if (criticalHighAlarm)
1039 {
1040 resp->assertionsMSB |= static_cast<uint8_t>(
1041 IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
1042 }
1043 if (criticalLowAlarm)
1044 {
1045 resp->assertionsLSB |= static_cast<uint8_t>(
1046 IPMISensorEventEnableThresholds::lowerCriticalGoingLow);
1047 }
1048 }
1049 *dataLen = sizeof(SensorEventStatusResp);
1050 }
1051
1052 // no thresholds enabled, don't need assertionMSB
1053 else
1054 {
1055 *dataLen = sizeof(SensorEventStatusResp) - 1;
1056 }
1057
1058 return IPMI_CC_OK;
1059}
1060
1061/* end sensor commands */
1062
1063/* storage commands */
1064
James Feist74c50c62019-08-14 14:18:41 -07001065ipmi::RspType<uint8_t, // sdr version
1066 uint16_t, // record count
1067 uint16_t, // free space
1068 uint32_t, // most recent addition
1069 uint32_t, // most recent erase
1070 uint8_t // operationSupport
1071 >
1072 ipmiStorageGetSDRRepositoryInfo(void)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001073{
James Feist74c50c62019-08-14 14:18:41 -07001074 constexpr const uint16_t unspecifiedFreeSpace = 0xFFFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001075 if (sensorTree.empty() && !getSensorSubtree(sensorTree))
1076 {
James Feist74c50c62019-08-14 14:18:41 -07001077 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001078 }
1079
James Feist74c50c62019-08-14 14:18:41 -07001080 size_t fruCount = 0;
1081 ipmi::Cc ret = ipmi::storage::getFruSdrCount(fruCount);
1082 if (ret != ipmi::ccSuccess)
1083 {
1084 return ipmi::response(ret);
1085 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001086
James Feist74c50c62019-08-14 14:18:41 -07001087 uint16_t recordCount =
1088 sensorTree.size() + fruCount + ipmi::storage::type12Count;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001089
James Feist74c50c62019-08-14 14:18:41 -07001090 uint8_t operationSupport = static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001091 SdrRepositoryInfoOps::overflow); // write not supported
James Feist74c50c62019-08-14 14:18:41 -07001092
1093 operationSupport |=
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001094 static_cast<uint8_t>(SdrRepositoryInfoOps::allocCommandSupported);
James Feist74c50c62019-08-14 14:18:41 -07001095 operationSupport |= static_cast<uint8_t>(
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001096 SdrRepositoryInfoOps::reserveSDRRepositoryCommandSupported);
James Feist74c50c62019-08-14 14:18:41 -07001097 return ipmi::responseSuccess(ipmiSdrVersion, recordCount,
1098 unspecifiedFreeSpace, sdrLastAdd,
1099 sdrLastRemove, operationSupport);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001100}
1101
jayaprakash Mutyala6ae08182019-05-13 18:56:11 +00001102/** @brief implements the get SDR allocation info command
1103 *
1104 * @returns IPMI completion code plus response data
1105 * - allocUnits - Number of possible allocation units
1106 * - allocUnitSize - Allocation unit size in bytes.
1107 * - allocUnitFree - Number of free allocation units
1108 * - allocUnitLargestFree - Largest free block in allocation units
1109 * - maxRecordSize - Maximum record size in allocation units.
1110 */
1111ipmi::RspType<uint16_t, // allocUnits
1112 uint16_t, // allocUnitSize
1113 uint16_t, // allocUnitFree
1114 uint16_t, // allocUnitLargestFree
1115 uint8_t // maxRecordSize
1116 >
1117 ipmiStorageGetSDRAllocationInfo()
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001118{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001119 // 0000h unspecified number of alloc units
jayaprakash Mutyala6ae08182019-05-13 18:56:11 +00001120 constexpr uint16_t allocUnits = 0;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001121
jayaprakash Mutyala6ae08182019-05-13 18:56:11 +00001122 constexpr uint16_t allocUnitFree = 0;
1123 constexpr uint16_t allocUnitLargestFree = 0;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001124 // only allow one block at a time
jayaprakash Mutyala6ae08182019-05-13 18:56:11 +00001125 constexpr uint8_t maxRecordSize = 1;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001126
jayaprakash Mutyala6ae08182019-05-13 18:56:11 +00001127 return ipmi::responseSuccess(allocUnits, maxSDRTotalSize, allocUnitFree,
1128 allocUnitLargestFree, maxRecordSize);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001129}
1130
jayaprakash Mutyala6ae08182019-05-13 18:56:11 +00001131/** @brief implements the reserve SDR command
1132 * @returns IPMI completion code plus response data
1133 * - sdrReservationID
1134 */
1135ipmi::RspType<uint16_t> ipmiStorageReserveSDR()
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001136{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001137 sdrReservationID++;
James Feista80cb902019-02-14 13:05:25 -08001138 if (sdrReservationID == 0)
1139 {
1140 sdrReservationID++;
1141 }
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001142
jayaprakash Mutyala6ae08182019-05-13 18:56:11 +00001143 return ipmi::responseSuccess(sdrReservationID);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001144}
1145
James Feistb49a98a2019-04-16 13:48:09 -07001146ipmi::RspType<uint16_t, // next record ID
1147 std::vector<uint8_t> // payload
1148 >
1149 ipmiStorageGetSDR(uint16_t reservationID, uint16_t recordID, uint8_t offset,
1150 uint8_t bytesToRead)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001151{
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001152 constexpr uint16_t lastRecordIndex = 0xFFFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001153
1154 // reservation required for partial reads with non zero offset into
1155 // record
James Feistb49a98a2019-04-16 13:48:09 -07001156 if ((sdrReservationID == 0 || reservationID != sdrReservationID) && offset)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001157 {
James Feistb49a98a2019-04-16 13:48:09 -07001158 return ipmi::responseInvalidReservationId();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001159 }
1160
1161 if (sensorTree.empty() && !getSensorSubtree(sensorTree))
1162 {
James Feistb49a98a2019-04-16 13:48:09 -07001163 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001164 }
1165
1166 size_t fruCount = 0;
James Feist74c50c62019-08-14 14:18:41 -07001167 ipmi::Cc ret = ipmi::storage::getFruSdrCount(fruCount);
1168 if (ret != ipmi::ccSuccess)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001169 {
James Feistb49a98a2019-04-16 13:48:09 -07001170 return ipmi::response(ret);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001171 }
1172
James Feist74c50c62019-08-14 14:18:41 -07001173 size_t lastRecord =
1174 sensorTree.size() + fruCount + ipmi::storage::type12Count - 1;
James Feistb49a98a2019-04-16 13:48:09 -07001175 if (recordID == lastRecordIndex)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001176 {
James Feistb49a98a2019-04-16 13:48:09 -07001177 recordID = lastRecord;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001178 }
James Feistb49a98a2019-04-16 13:48:09 -07001179 if (recordID > lastRecord)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001180 {
James Feistb49a98a2019-04-16 13:48:09 -07001181 return ipmi::responseInvalidFieldRequest();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001182 }
1183
James Feistb49a98a2019-04-16 13:48:09 -07001184 uint16_t nextRecordId = lastRecord > recordID ? recordID + 1 : 0XFFFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001185
James Feistb49a98a2019-04-16 13:48:09 -07001186 if (recordID >= sensorTree.size())
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001187 {
James Feist74c50c62019-08-14 14:18:41 -07001188 std::vector<uint8_t> recordData;
James Feistb49a98a2019-04-16 13:48:09 -07001189 size_t fruIndex = recordID - sensorTree.size();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001190 if (fruIndex >= fruCount)
1191 {
James Feist74c50c62019-08-14 14:18:41 -07001192 // handle type 12 hardcoded records
1193 size_t type12Index = fruIndex - fruCount;
1194 if (type12Index >= ipmi::storage::type12Count ||
1195 offset > sizeof(Type12Record))
1196 {
1197 return ipmi::responseInvalidFieldRequest();
1198 }
1199 std::vector<uint8_t> record =
1200 ipmi::storage::getType12SDRs(type12Index, recordID);
1201 if (record.size() < (offset + bytesToRead))
1202 {
1203 bytesToRead = record.size() - offset;
1204 }
James Feistb49a98a2019-04-16 13:48:09 -07001205
James Feist74c50c62019-08-14 14:18:41 -07001206 recordData.insert(recordData.end(), record.begin() + offset,
1207 record.begin() + offset + bytesToRead);
1208 }
1209 else
1210 {
1211 // handle fru records
1212 get_sdr::SensorDataFruRecord data;
1213 if (offset > sizeof(data))
1214 {
1215 return ipmi::responseInvalidFieldRequest();
1216 }
1217 ret = ipmi::storage::getFruSdrs(fruIndex, data);
1218 if (ret != IPMI_CC_OK)
1219 {
1220 return ipmi::response(ret);
1221 }
1222 data.header.record_id_msb = recordID << 8;
1223 data.header.record_id_lsb = recordID & 0xFF;
1224 if (sizeof(data) < (offset + bytesToRead))
1225 {
1226 bytesToRead = sizeof(data) - offset;
1227 }
1228
1229 uint8_t *respStart = reinterpret_cast<uint8_t *>(&data) + offset;
1230 recordData.insert(recordData.end(), respStart,
1231 respStart + bytesToRead);
1232 }
James Feistb49a98a2019-04-16 13:48:09 -07001233
1234 return ipmi::responseSuccess(nextRecordId, recordData);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001235 }
1236
1237 std::string connection;
1238 std::string path;
James Feistb49a98a2019-04-16 13:48:09 -07001239 uint16_t sensorIndex = recordID;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001240 for (const auto &sensor : sensorTree)
1241 {
1242 if (sensorIndex-- == 0)
1243 {
1244 if (!sensor.second.size())
1245 {
James Feistb49a98a2019-04-16 13:48:09 -07001246 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001247 }
1248 connection = sensor.second.begin()->first;
1249 path = sensor.first;
1250 break;
1251 }
1252 }
1253
1254 SensorMap sensorMap;
1255 if (!getSensorMap(connection, path, sensorMap))
1256 {
James Feistb49a98a2019-04-16 13:48:09 -07001257 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001258 }
James Feistb49a98a2019-04-16 13:48:09 -07001259 uint8_t sensornumber = (recordID & 0xFF);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001260 get_sdr::SensorDataFullRecord record = {0};
1261
James Feistb49a98a2019-04-16 13:48:09 -07001262 record.header.record_id_msb = recordID << 8;
1263 record.header.record_id_lsb = recordID & 0xFF;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001264 record.header.sdr_version = ipmiSdrVersion;
1265 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
1266 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord) -
1267 sizeof(get_sdr::SensorDataRecordHeader);
1268 record.key.owner_id = 0x20;
1269 record.key.owner_lun = 0x0;
1270 record.key.sensor_number = sensornumber;
1271
James Feist7086a882019-03-13 10:46:00 -07001272 record.body.sensor_capabilities = 0x68; // auto rearm - todo hysteresis
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001273 record.body.sensor_type = getSensorTypeFromPath(path);
1274 std::string type = getSensorTypeStringFromPath(path);
1275 auto typeCstr = type.c_str();
1276 auto findUnits = sensorUnits.find(typeCstr);
1277 if (findUnits != sensorUnits.end())
1278 {
1279 record.body.sensor_units_2_base =
1280 static_cast<uint8_t>(findUnits->second);
1281 } // else default 0x0 unspecified
1282
1283 record.body.event_reading_type = getSensorEventTypeFromPath(path);
1284
1285 auto sensorObject = sensorMap.find("xyz.openbmc_project.Sensor.Value");
1286 if (sensorObject == sensorMap.end())
1287 {
James Feistb49a98a2019-04-16 13:48:09 -07001288 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001289 }
1290
Patrick Venture262276f2019-10-18 13:27:59 -07001291 uint8_t entityId = 0;
1292 uint8_t entityInstance = 0x01;
1293
1294 // follow the association chain to get the parent board's entityid and
1295 // entityInstance
1296 updateIpmiFromAssociation(path, sensorMap, entityId, entityInstance);
1297
1298 record.body.entity_id = entityId;
1299 record.body.entity_instance = entityInstance;
1300
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001301 auto maxObject = sensorObject->second.find("MaxValue");
1302 auto minObject = sensorObject->second.find("MinValue");
Josh Lehanca9dcbd2019-11-18 15:59:59 -08001303
1304 // If min and/or max are left unpopulated,
1305 // then default to what a signed byte would be, namely (-128,127) range.
1306 auto max = static_cast<double>(std::numeric_limits<int8_t>::max());
1307 auto min = static_cast<double>(std::numeric_limits<int8_t>::lowest());
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001308 if (maxObject != sensorObject->second.end())
1309 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001310 max = std::visit(VariantToDoubleVisitor(), maxObject->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001311 }
1312
1313 if (minObject != sensorObject->second.end())
1314 {
Vernon Mauery8166c8d2019-05-23 11:22:30 -07001315 min = std::visit(VariantToDoubleVisitor(), minObject->second);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001316 }
1317
Yong Li1f2eb5e2019-05-23 14:07:17 +08001318 int16_t mValue = 0;
1319 int8_t rExp = 0;
1320 int16_t bValue = 0;
1321 int8_t bExp = 0;
1322 bool bSigned = false;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001323
1324 if (!getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned))
1325 {
James Feistb49a98a2019-04-16 13:48:09 -07001326 return ipmi::responseResponseError();
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001327 }
1328
Josh Lehan7cc9e472019-11-18 16:09:26 -08001329 // The record.body is a struct SensorDataFullRecordBody
1330 // from sensorhandler.hpp in phosphor-ipmi-host.
1331 // The meaning of these bits appears to come from
1332 // table 43.1 of the IPMI spec.
1333 // The above 5 sensor attributes are stuffed in as follows:
1334 // Byte 21 = AA000000 = analog interpretation, 10 signed, 00 unsigned
1335 // Byte 22-24 are for other purposes
1336 // Byte 25 = MMMMMMMM = LSB of M
1337 // Byte 26 = MMTTTTTT = MSB of M (signed), and Tolerance
1338 // Byte 27 = BBBBBBBB = LSB of B
1339 // Byte 28 = BBAAAAAA = MSB of B (signed), and LSB of Accuracy
1340 // Byte 29 = AAAAEE00 = MSB of Accuracy, exponent of Accuracy
1341 // Byte 30 = RRRRBBBB = rExp (signed), bExp (signed)
1342
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001343 // apply M, B, and exponents, M and B are 10 bit values, exponents are 4
1344 record.body.m_lsb = mValue & 0xFF;
1345
Josh Lehan7cc9e472019-11-18 16:09:26 -08001346 uint8_t mBitSign = (mValue < 0) ? 1 : 0;
1347 uint8_t mBitNine = (mValue & 0x0100) >> 8;
1348
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001349 // move the smallest bit of the MSB into place (bit 9)
1350 // the MSbs are bits 7:8 in m_msb_and_tolerance
Josh Lehan7cc9e472019-11-18 16:09:26 -08001351 record.body.m_msb_and_tolerance = (mBitSign << 7) | (mBitNine << 6);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001352
1353 record.body.b_lsb = bValue & 0xFF;
1354
Josh Lehan7cc9e472019-11-18 16:09:26 -08001355 uint8_t bBitSign = (bValue < 0) ? 1 : 0;
1356 uint8_t bBitNine = (bValue & 0x0100) >> 8;
1357
1358 // move the smallest bit of the MSB into place (bit 9)
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001359 // the MSbs are bits 7:8 in b_msb_and_accuracy_lsb
Josh Lehan7cc9e472019-11-18 16:09:26 -08001360 record.body.b_msb_and_accuracy_lsb = (bBitSign << 7) | (bBitNine << 6);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001361
Josh Lehan7cc9e472019-11-18 16:09:26 -08001362 uint8_t rExpSign = (rExp < 0) ? 1 : 0;
1363 uint8_t rExpBits = rExp & 0x07;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001364
Josh Lehan7cc9e472019-11-18 16:09:26 -08001365 uint8_t bExpSign = (bExp < 0) ? 1 : 0;
1366 uint8_t bExpBits = bExp & 0x07;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001367
Josh Lehan7cc9e472019-11-18 16:09:26 -08001368 // move rExp and bExp into place
1369 record.body.r_b_exponents =
1370 (rExpSign << 7) | (rExpBits << 4) | (bExpSign << 3) | bExpBits;
1371
1372 // Set the analog reading byte interpretation accordingly
1373 record.body.sensor_units_1 = (bSigned ? 1 : 0) << 7;
1374
1375 // TODO(): Perhaps care about Tolerance, Accuracy, and so on
1376 // These seem redundant, but derivable from the above 5 attributes
1377 // Original comment said "todo fill out rest of units"
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001378
1379 // populate sensor name from path
1380 std::string name;
1381 size_t nameStart = path.rfind("/");
1382 if (nameStart != std::string::npos)
1383 {
1384 name = path.substr(nameStart + 1, std::string::npos - nameStart);
1385 }
1386
1387 std::replace(name.begin(), name.end(), '_', ' ');
1388 if (name.size() > FULL_RECORD_ID_STR_MAX_LENGTH)
1389 {
James Feist979a2322019-05-15 09:06:54 -07001390 // try to not truncate by replacing common words
1391 constexpr std::array<std::pair<const char *, const char *>, 2>
1392 replaceWords = {std::make_pair("Output", "Out"),
1393 std::make_pair("Input", "In")};
1394 for (const auto &[find, replace] : replaceWords)
1395 {
1396 boost::replace_all(name, find, replace);
1397 }
1398
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001399 name.resize(FULL_RECORD_ID_STR_MAX_LENGTH);
1400 }
1401 record.body.id_string_info = name.size();
1402 std::strncpy(record.body.id_string, name.c_str(),
1403 sizeof(record.body.id_string));
1404
James Feistc4b15bc2019-04-16 15:41:39 -07001405 IPMIThresholds thresholdData;
1406 try
1407 {
1408 thresholdData = getIPMIThresholds(sensorMap);
1409 }
1410 catch (std::exception &)
1411 {
1412 return ipmi::responseResponseError();
1413 }
1414
1415 if (thresholdData.criticalHigh)
1416 {
1417 record.body.upper_critical_threshold = *thresholdData.criticalHigh;
1418 record.body.supported_deassertions[1] |= static_cast<uint8_t>(
1419 IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
1420 record.body.supported_assertions[1] |= static_cast<uint8_t>(
1421 IPMISensorEventEnableThresholds::upperCriticalGoingHigh);
1422 record.body.discrete_reading_setting_mask[0] |=
1423 static_cast<uint8_t>(IPMISensorReadingByte3::upperCritical);
1424 }
1425 if (thresholdData.warningHigh)
1426 {
1427 record.body.upper_noncritical_threshold = *thresholdData.warningHigh;
1428 record.body.supported_deassertions[0] |= static_cast<uint8_t>(
1429 IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
1430 record.body.supported_assertions[0] |= static_cast<uint8_t>(
1431 IPMISensorEventEnableThresholds::upperNonCriticalGoingHigh);
1432 record.body.discrete_reading_setting_mask[0] |=
1433 static_cast<uint8_t>(IPMISensorReadingByte3::upperNonCritical);
1434 }
1435 if (thresholdData.criticalLow)
1436 {
1437 record.body.lower_critical_threshold = *thresholdData.criticalLow;
1438 record.body.supported_deassertions[0] |= static_cast<uint8_t>(
1439 IPMISensorEventEnableThresholds::lowerCriticalGoingLow);
1440 record.body.supported_assertions[0] |= static_cast<uint8_t>(
1441 IPMISensorEventEnableThresholds::lowerCriticalGoingLow);
1442 record.body.discrete_reading_setting_mask[0] |=
1443 static_cast<uint8_t>(IPMISensorReadingByte3::lowerCritical);
1444 }
1445 if (thresholdData.warningLow)
1446 {
1447 record.body.lower_noncritical_threshold = *thresholdData.warningLow;
1448 record.body.supported_deassertions[0] |= static_cast<uint8_t>(
1449 IPMISensorEventEnableThresholds::lowerNonCriticalGoingLow);
1450 record.body.supported_assertions[0] |= static_cast<uint8_t>(
1451 IPMISensorEventEnableThresholds::lowerNonCriticalGoingLow);
1452 record.body.discrete_reading_setting_mask[0] |=
1453 static_cast<uint8_t>(IPMISensorReadingByte3::lowerNonCritical);
1454 }
1455
1456 // everything that is readable is setable
1457 record.body.discrete_reading_setting_mask[1] =
1458 record.body.discrete_reading_setting_mask[0];
1459
James Feistb49a98a2019-04-16 13:48:09 -07001460 if (sizeof(get_sdr::SensorDataFullRecord) < (offset + bytesToRead))
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001461 {
James Feistb49a98a2019-04-16 13:48:09 -07001462 bytesToRead = sizeof(get_sdr::SensorDataFullRecord) - offset;
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001463 }
1464
James Feistb49a98a2019-04-16 13:48:09 -07001465 uint8_t *respStart = reinterpret_cast<uint8_t *>(&record) + offset;
1466 std::vector<uint8_t> recordData(respStart, respStart + bytesToRead);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001467
James Feistb49a98a2019-04-16 13:48:09 -07001468 return ipmi::responseSuccess(nextRecordId, recordData);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001469}
1470/* end storage commands */
1471
1472void registerSensorFunctions()
1473{
1474 // get firmware version information
1475 ipmiPrintAndRegister(NETFUN_SENSOR, IPMI_CMD_WILDCARD, nullptr,
1476 ipmiSensorWildcardHandler, PRIVILEGE_USER);
1477
1478 // <Get Sensor Type>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001479 ipmiPrintAndRegister(NETFUN_SENSOR, ipmi::sensor_event::cmdGetSensorType,
1480 nullptr, ipmiSensorWildcardHandler, PRIVILEGE_USER);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001481
1482 // <Set Sensor Reading and Event Status>
1483 ipmiPrintAndRegister(
Vernon Mauery98bbf692019-09-16 11:14:59 -07001484 NETFUN_SENSOR, ipmi::sensor_event::cmdSetSensorReadingAndEvtSts,
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001485 nullptr, ipmiSensorWildcardHandler, PRIVILEGE_OPERATOR);
1486
Jason M. Billsae6bdb12019-04-02 12:00:04 -07001487 // <Platform Event>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001488 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnSensor,
1489 ipmi::sensor_event::cmdPlatformEvent,
1490 ipmi::Privilege::Operator, ipmiSenPlatformEvent);
Jason M. Billsae6bdb12019-04-02 12:00:04 -07001491
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001492 // <Get Sensor Reading>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001493 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnSensor,
1494 ipmi::sensor_event::cmdGetSensorReading,
1495 ipmi::Privilege::User, ipmiSenGetSensorReading);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001496
1497 // <Get Sensor Threshold>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001498 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnSensor,
1499 ipmi::sensor_event::cmdGetSensorThreshold,
1500 ipmi::Privilege::User, ipmiSenGetSensorThresholds);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001501
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +00001502 // <Set Sensor Threshold>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001503 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnSensor,
1504 ipmi::sensor_event::cmdSetSensorThreshold,
1505 ipmi::Privilege::Operator,
1506 ipmiSenSetSensorThresholds);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001507
1508 // <Get Sensor Event Enable>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001509 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnSensor,
1510 ipmi::sensor_event::cmdGetSensorEventEnable,
jayaprakash Mutyala39fa93f2019-05-13 12:16:30 +00001511 ipmi::Privilege::User, ipmiSenGetSensorEventEnable);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001512
1513 // <Get Sensor Event Status>
1514 ipmiPrintAndRegister(NETFUN_SENSOR,
Vernon Mauery98bbf692019-09-16 11:14:59 -07001515 ipmi::sensor_event::cmdGetSensorEventStatus, nullptr,
1516 ipmiSenGetSensorEventStatus, PRIVILEGE_USER);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001517
1518 // register all storage commands for both Sensor and Storage command
1519 // versions
1520
1521 // <Get SDR Repository Info>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001522 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1523 ipmi::storage::cmdGetSdrRepositoryInfo,
1524 ipmi::Privilege::User,
1525 ipmiStorageGetSDRRepositoryInfo);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001526
1527 // <Get SDR Allocation Info>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001528 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1529 ipmi::storage::cmdGetSdrRepositoryAllocInfo,
1530 ipmi::Privilege::User,
1531 ipmiStorageGetSDRAllocationInfo);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001532
1533 // <Reserve SDR Repo>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001534 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnSensor,
1535 ipmi::sensor_event::cmdReserveDeviceSdrRepository,
jayaprakash Mutyala6ae08182019-05-13 18:56:11 +00001536 ipmi::Privilege::User, ipmiStorageReserveSDR);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001537
Vernon Mauery98bbf692019-09-16 11:14:59 -07001538 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1539 ipmi::storage::cmdReserveSdrRepository,
1540 ipmi::Privilege::User, ipmiStorageReserveSDR);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001541
1542 // <Get Sdr>
Vernon Mauery98bbf692019-09-16 11:14:59 -07001543 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnSensor,
1544 ipmi::sensor_event::cmdGetDeviceSdr,
1545 ipmi::Privilege::User, ipmiStorageGetSDR);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001546
Vernon Mauery98bbf692019-09-16 11:14:59 -07001547 ipmi::registerHandler(ipmi::prioOemBase, ipmi::netFnStorage,
1548 ipmi::storage::cmdGetSdr, ipmi::Privilege::User,
1549 ipmiStorageGetSDR);
Jason M. Bills3f7c5e42018-10-03 14:00:41 -07001550}
1551} // namespace ipmi