19cf85627SBrandon Kim #include "config.h"
29cf85627SBrandon Kim 
346470a38SPatrick Venture #include "sensorhandler.hpp"
40b02be92SPatrick Venture 
599bf1c41SPatrick Venture #include "entity_map_json.hpp"
60b02be92SPatrick Venture #include "fruread.hpp"
70b02be92SPatrick Venture 
8d700e76aSTom #include <mapper.h>
998a23840SMatthew Barth #include <systemd/sd-bus.h>
100b02be92SPatrick Venture 
110b02be92SPatrick Venture #include <bitset>
12586d35b4SPatrick Venture #include <cmath>
13b51bf9c8SPatrick Venture #include <cstring>
14e08fbffcSVernon Mauery #include <ipmid/api.hpp>
1533250240SVernon Mauery #include <ipmid/types.hpp>
166a98fe7fSVernon Mauery #include <ipmid/utils.hpp>
1718e99992SDhruvaraj Subhashchandran #include <phosphor-logging/elog-errors.hpp>
180b02be92SPatrick Venture #include <phosphor-logging/log.hpp>
194c008028SWilliam A. Kennington III #include <sdbusplus/message/types.hpp>
200b02be92SPatrick Venture #include <set>
210b02be92SPatrick Venture #include <xyz/openbmc_project/Common/error.hpp>
220b02be92SPatrick Venture #include <xyz/openbmc_project/Sensor/Value/server.hpp>
230b02be92SPatrick Venture 
24e0cc8553SRatan Gupta static constexpr uint8_t fruInventoryDevice = 0x10;
25e0cc8553SRatan Gupta static constexpr uint8_t IPMIFruInventory = 0x02;
26e0cc8553SRatan Gupta static constexpr uint8_t BMCSlaveAddress = 0x20;
27e0cc8553SRatan Gupta 
2898a23840SMatthew Barth extern int updateSensorRecordFromSSRAESC(const void*);
29d700e76aSTom extern sd_bus* bus;
30db0cbe64SPatrick Venture 
31db0cbe64SPatrick Venture namespace ipmi
32db0cbe64SPatrick Venture {
33db0cbe64SPatrick Venture namespace sensor
34db0cbe64SPatrick Venture {
35db0cbe64SPatrick Venture extern const IdInfoMap sensors;
36db0cbe64SPatrick Venture } // namespace sensor
37db0cbe64SPatrick Venture } // namespace ipmi
38db0cbe64SPatrick Venture 
39e0cc8553SRatan Gupta extern const FruMap frus;
40e0cc8553SRatan Gupta 
41be703f71STom Joseph using namespace phosphor::logging;
4218e99992SDhruvaraj Subhashchandran using InternalFailure =
4318e99992SDhruvaraj Subhashchandran     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
4498a23840SMatthew Barth 
4598a23840SMatthew Barth void register_netfn_sen_functions() __attribute__((constructor));
4698a23840SMatthew Barth 
470b02be92SPatrick Venture struct sensorTypemap_t
480b02be92SPatrick Venture {
4998a23840SMatthew Barth     uint8_t number;
5098a23840SMatthew Barth     uint8_t typecode;
5198a23840SMatthew Barth     char dbusname[32];
5298a23840SMatthew Barth };
5398a23840SMatthew Barth 
5498a23840SMatthew Barth sensorTypemap_t g_SensorTypeMap[] = {
5598a23840SMatthew Barth 
5698a23840SMatthew Barth     {0x01, 0x6F, "Temp"},
5798a23840SMatthew Barth     {0x0C, 0x6F, "DIMM"},
5898a23840SMatthew Barth     {0x0C, 0x6F, "MEMORY_BUFFER"},
5998a23840SMatthew Barth     {0x07, 0x6F, "PROC"},
6098a23840SMatthew Barth     {0x07, 0x6F, "CORE"},
6198a23840SMatthew Barth     {0x07, 0x6F, "CPU"},
6298a23840SMatthew Barth     {0x0F, 0x6F, "BootProgress"},
630b02be92SPatrick Venture     {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor
640b02be92SPatrick Venture                                // type code os 0x09
6598a23840SMatthew Barth     {0xC3, 0x6F, "BootCount"},
6698a23840SMatthew Barth     {0x1F, 0x6F, "OperatingSystemStatus"},
6798a23840SMatthew Barth     {0x12, 0x6F, "SYSTEM_EVENT"},
6898a23840SMatthew Barth     {0xC7, 0x03, "SYSTEM"},
6998a23840SMatthew Barth     {0xC7, 0x03, "MAIN_PLANAR"},
7098a23840SMatthew Barth     {0xC2, 0x6F, "PowerCap"},
71558184eaSTom Joseph     {0x0b, 0xCA, "PowerSupplyRedundancy"},
720661beb1SJayanth Othayoth     {0xDA, 0x03, "TurboAllowed"},
73558184eaSTom Joseph     {0xD8, 0xC8, "PowerSupplyDerating"},
7498a23840SMatthew Barth     {0xFF, 0x00, ""},
7598a23840SMatthew Barth };
7698a23840SMatthew Barth 
770b02be92SPatrick Venture struct sensor_data_t
780b02be92SPatrick Venture {
7998a23840SMatthew Barth     uint8_t sennum;
8098a23840SMatthew Barth } __attribute__((packed));
8198a23840SMatthew Barth 
8214a47819SLei YU using SDRCacheMap = std::unordered_map<uint8_t, get_sdr::SensorDataFullRecord>;
8314a47819SLei YU SDRCacheMap sdrCacheMap __attribute__((init_priority(101)));
8414a47819SLei YU 
8514a47819SLei YU using SensorThresholdMap =
8614a47819SLei YU     std::unordered_map<uint8_t, get_sdr::GetSensorThresholdsResponse>;
8714a47819SLei YU SensorThresholdMap sensorThresholdMap __attribute__((init_priority(101)));
8814a47819SLei YU 
89*962e68baSLei YU #ifdef FEATURE_SENSORS_CACHE
90be5c6b2aSLei YU std::map<uint8_t, std::unique_ptr<sdbusplus::bus::match::match>>
91be5c6b2aSLei YU     sensorAddedMatches __attribute__((init_priority(101)));
92be5c6b2aSLei YU std::map<uint8_t, std::unique_ptr<sdbusplus::bus::match::match>>
93be5c6b2aSLei YU     sensorUpdatedMatches __attribute__((init_priority(101)));
94be5c6b2aSLei YU 
95be5c6b2aSLei YU void initSensorMatches()
96be5c6b2aSLei YU {
97be5c6b2aSLei YU     using namespace sdbusplus::bus::match::rules;
98be5c6b2aSLei YU     sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
99be5c6b2aSLei YU     for (const auto& s : ipmi::sensor::sensors)
100be5c6b2aSLei YU     {
101be5c6b2aSLei YU         sensorAddedMatches.emplace(
102be5c6b2aSLei YU             s.first,
103be5c6b2aSLei YU             std::make_unique<sdbusplus::bus::match::match>(
104be5c6b2aSLei YU                 bus, interfacesAdded() + argNpath(0, s.second.sensorPath),
105be5c6b2aSLei YU                 [id = s.first, obj = s.second.sensorPath](auto& /*msg*/) {
106be5c6b2aSLei YU                     // TODO
107be5c6b2aSLei YU                 }));
108be5c6b2aSLei YU         sensorUpdatedMatches.emplace(
109be5c6b2aSLei YU             s.first,
110be5c6b2aSLei YU             std::make_unique<sdbusplus::bus::match::match>(
111be5c6b2aSLei YU                 bus,
112be5c6b2aSLei YU                 type::signal() + path(s.second.sensorPath) +
113be5c6b2aSLei YU                     member("PropertiesChanged"s) +
114be5c6b2aSLei YU                     interface("org.freedesktop.DBus.Properties"s),
115be5c6b2aSLei YU                 [id = s.first, obj = s.second.sensorPath](auto& /*msg*/) {
116be5c6b2aSLei YU                     // TODO
117be5c6b2aSLei YU                 }));
118be5c6b2aSLei YU     }
119be5c6b2aSLei YU }
120*962e68baSLei YU #endif
121be5c6b2aSLei YU 
1220b02be92SPatrick Venture int get_bus_for_path(const char* path, char** busname)
1230b02be92SPatrick Venture {
1242ae09b9aSEmily Shaffer     return mapper_get_service(bus, path, busname);
1252ae09b9aSEmily Shaffer }
126d700e76aSTom 
1272ae09b9aSEmily Shaffer // Use a lookup table to find the interface name of a specific sensor
1282ae09b9aSEmily Shaffer // This will be used until an alternative is found.  this is the first
1292ae09b9aSEmily Shaffer // step for mapping IPMI
1300b02be92SPatrick Venture int find_openbmc_path(uint8_t num, dbus_interface_t* interface)
1310b02be92SPatrick Venture {
1322ae09b9aSEmily Shaffer     int rc;
1332ae09b9aSEmily Shaffer 
134db0cbe64SPatrick Venture     const auto& sensor_it = ipmi::sensor::sensors.find(num);
135db0cbe64SPatrick Venture     if (sensor_it == ipmi::sensor::sensors.end())
1362ae09b9aSEmily Shaffer     {
137ba23ff71SAdriana Kobylak         // The sensor map does not contain the sensor requested
138ba23ff71SAdriana Kobylak         return -EINVAL;
1392ae09b9aSEmily Shaffer     }
1402ae09b9aSEmily Shaffer 
1412ae09b9aSEmily Shaffer     const auto& info = sensor_it->second;
1422ae09b9aSEmily Shaffer 
1438451edf5SPatrick Williams     char* busname = nullptr;
1442ae09b9aSEmily Shaffer     rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
1450b02be92SPatrick Venture     if (rc < 0)
1460b02be92SPatrick Venture     {
147b51bf9c8SPatrick Venture         std::fprintf(stderr, "Failed to get %s busname: %s\n",
1480b02be92SPatrick Venture                      info.sensorPath.c_str(), busname);
1492ae09b9aSEmily Shaffer         goto final;
1502ae09b9aSEmily Shaffer     }
1512ae09b9aSEmily Shaffer 
1522ae09b9aSEmily Shaffer     interface->sensortype = info.sensorType;
1532ae09b9aSEmily Shaffer     strcpy(interface->bus, busname);
1542ae09b9aSEmily Shaffer     strcpy(interface->path, info.sensorPath.c_str());
1552ae09b9aSEmily Shaffer     // Take the interface name from the beginning of the DbusInterfaceMap. This
1562ae09b9aSEmily Shaffer     // works for the Value interface but may not suffice for more complex
1572ae09b9aSEmily Shaffer     // sensors.
1582ae09b9aSEmily Shaffer     // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
1590b02be92SPatrick Venture     strcpy(interface->interface,
1600b02be92SPatrick Venture            info.propertyInterfaces.begin()->first.c_str());
1612ae09b9aSEmily Shaffer     interface->sensornumber = num;
1622ae09b9aSEmily Shaffer 
1632ae09b9aSEmily Shaffer final:
1642ae09b9aSEmily Shaffer     free(busname);
1652ae09b9aSEmily Shaffer     return rc;
1662ae09b9aSEmily Shaffer }
1672ae09b9aSEmily Shaffer 
168d700e76aSTom /////////////////////////////////////////////////////////////////////
169d700e76aSTom //
170d700e76aSTom // Routines used by ipmi commands wanting to interact on the dbus
171d700e76aSTom //
172d700e76aSTom /////////////////////////////////////////////////////////////////////
1730b02be92SPatrick Venture int set_sensor_dbus_state_s(uint8_t number, const char* method,
1740b02be92SPatrick Venture                             const char* value)
1750b02be92SPatrick Venture {
176d700e76aSTom 
177d700e76aSTom     dbus_interface_t a;
178d700e76aSTom     int r;
179d700e76aSTom     sd_bus_error error = SD_BUS_ERROR_NULL;
180d700e76aSTom     sd_bus_message* m = NULL;
181d700e76aSTom 
1822ae09b9aSEmily Shaffer     r = find_openbmc_path(number, &a);
183d700e76aSTom 
1840b02be92SPatrick Venture     if (r < 0)
1850b02be92SPatrick Venture     {
186b51bf9c8SPatrick Venture         std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
187d700e76aSTom         return 0;
188d700e76aSTom     }
189d700e76aSTom 
1900b02be92SPatrick Venture     r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
1910b02be92SPatrick Venture                                        method);
1920b02be92SPatrick Venture     if (r < 0)
1930b02be92SPatrick Venture     {
194b51bf9c8SPatrick Venture         std::fprintf(stderr, "Failed to create a method call: %s",
195b51bf9c8SPatrick Venture                      strerror(-r));
196d700e76aSTom         goto final;
197d700e76aSTom     }
198d700e76aSTom 
199d700e76aSTom     r = sd_bus_message_append(m, "v", "s", value);
2000b02be92SPatrick Venture     if (r < 0)
2010b02be92SPatrick Venture     {
202b51bf9c8SPatrick Venture         std::fprintf(stderr, "Failed to create a input parameter: %s",
203b51bf9c8SPatrick Venture                      strerror(-r));
204d700e76aSTom         goto final;
205d700e76aSTom     }
206d700e76aSTom 
207d700e76aSTom     r = sd_bus_call(bus, m, 0, &error, NULL);
2080b02be92SPatrick Venture     if (r < 0)
2090b02be92SPatrick Venture     {
210b51bf9c8SPatrick Venture         std::fprintf(stderr, "Failed to call the method: %s", strerror(-r));
211d700e76aSTom     }
212d700e76aSTom 
213d700e76aSTom final:
214d700e76aSTom     sd_bus_error_free(&error);
215d700e76aSTom     m = sd_bus_message_unref(m);
216d700e76aSTom 
217d700e76aSTom     return 0;
218d700e76aSTom }
2190b02be92SPatrick Venture int set_sensor_dbus_state_y(uint8_t number, const char* method,
2200b02be92SPatrick Venture                             const uint8_t value)
2210b02be92SPatrick Venture {
222d700e76aSTom 
223d700e76aSTom     dbus_interface_t a;
224d700e76aSTom     int r;
225d700e76aSTom     sd_bus_error error = SD_BUS_ERROR_NULL;
226d700e76aSTom     sd_bus_message* m = NULL;
227d700e76aSTom 
2282ae09b9aSEmily Shaffer     r = find_openbmc_path(number, &a);
229d700e76aSTom 
2300b02be92SPatrick Venture     if (r < 0)
2310b02be92SPatrick Venture     {
232b51bf9c8SPatrick Venture         std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
233d700e76aSTom         return 0;
234d700e76aSTom     }
235d700e76aSTom 
2360b02be92SPatrick Venture     r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
2370b02be92SPatrick Venture                                        method);
2380b02be92SPatrick Venture     if (r < 0)
2390b02be92SPatrick Venture     {
240b51bf9c8SPatrick Venture         std::fprintf(stderr, "Failed to create a method call: %s",
241b51bf9c8SPatrick Venture                      strerror(-r));
242d700e76aSTom         goto final;
243d700e76aSTom     }
244d700e76aSTom 
245d700e76aSTom     r = sd_bus_message_append(m, "v", "i", value);
2460b02be92SPatrick Venture     if (r < 0)
2470b02be92SPatrick Venture     {
248b51bf9c8SPatrick Venture         std::fprintf(stderr, "Failed to create a input parameter: %s",
249b51bf9c8SPatrick Venture                      strerror(-r));
250d700e76aSTom         goto final;
251d700e76aSTom     }
252d700e76aSTom 
253d700e76aSTom     r = sd_bus_call(bus, m, 0, &error, NULL);
2540b02be92SPatrick Venture     if (r < 0)
2550b02be92SPatrick Venture     {
256b51bf9c8SPatrick Venture         std::fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
257d700e76aSTom     }
258d700e76aSTom 
259d700e76aSTom final:
260d700e76aSTom     sd_bus_error_free(&error);
261d700e76aSTom     m = sd_bus_message_unref(m);
262d700e76aSTom 
263d700e76aSTom     return 0;
264d700e76aSTom }
265d700e76aSTom 
2660b02be92SPatrick Venture uint8_t dbus_to_sensor_type(char* p)
2670b02be92SPatrick Venture {
26898a23840SMatthew Barth 
26998a23840SMatthew Barth     sensorTypemap_t* s = g_SensorTypeMap;
27098a23840SMatthew Barth     char r = 0;
2710b02be92SPatrick Venture     while (s->number != 0xFF)
2720b02be92SPatrick Venture     {
2730b02be92SPatrick Venture         if (!strcmp(s->dbusname, p))
2740b02be92SPatrick Venture         {
275558184eaSTom Joseph             r = s->typecode;
27698a23840SMatthew Barth             break;
27798a23840SMatthew Barth         }
27898a23840SMatthew Barth         s++;
27998a23840SMatthew Barth     }
28098a23840SMatthew Barth 
28198a23840SMatthew Barth     if (s->number == 0xFF)
28298a23840SMatthew Barth         printf("Failed to find Sensor Type %s\n", p);
28398a23840SMatthew Barth 
28498a23840SMatthew Barth     return r;
28598a23840SMatthew Barth }
28698a23840SMatthew Barth 
2870b02be92SPatrick Venture uint8_t get_type_from_interface(dbus_interface_t dbus_if)
2880b02be92SPatrick Venture {
28998a23840SMatthew Barth 
29056003453SBrad Bishop     uint8_t type;
29198a23840SMatthew Barth 
29298a23840SMatthew Barth     // This is where sensors that do not exist in dbus but do
29398a23840SMatthew Barth     // exist in the host code stop.  This should indicate it
29498a23840SMatthew Barth     // is not a supported sensor
2950b02be92SPatrick Venture     if (dbus_if.interface[0] == 0)
2960b02be92SPatrick Venture     {
2970b02be92SPatrick Venture         return 0;
2980b02be92SPatrick Venture     }
29998a23840SMatthew Barth 
3007117441cSEmily Shaffer     // Fetch type from interface itself.
3017117441cSEmily Shaffer     if (dbus_if.sensortype != 0)
3027117441cSEmily Shaffer     {
3037117441cSEmily Shaffer         type = dbus_if.sensortype;
3040b02be92SPatrick Venture     }
3050b02be92SPatrick Venture     else
3060b02be92SPatrick Venture     {
30798a23840SMatthew Barth         // Non InventoryItems
3084491a46fSPatrick Venture         char* p = strrchr(dbus_if.path, '/');
30956003453SBrad Bishop         type = dbus_to_sensor_type(p + 1);
31098a23840SMatthew Barth     }
31198a23840SMatthew Barth 
31256003453SBrad Bishop     return type;
31398a23840SMatthew Barth }
31498a23840SMatthew Barth 
315391f3303SEmily Shaffer // Replaces find_sensor
3160b02be92SPatrick Venture uint8_t find_type_for_sensor_number(uint8_t num)
3170b02be92SPatrick Venture {
318391f3303SEmily Shaffer     int r;
319391f3303SEmily Shaffer     dbus_interface_t dbus_if;
3202ae09b9aSEmily Shaffer     r = find_openbmc_path(num, &dbus_if);
3210b02be92SPatrick Venture     if (r < 0)
3220b02be92SPatrick Venture     {
323b51bf9c8SPatrick Venture         std::fprintf(stderr, "Could not find sensor %d\n", num);
32491875f77SLei YU         return 0;
325391f3303SEmily Shaffer     }
326391f3303SEmily Shaffer     return get_type_from_interface(dbus_if);
327391f3303SEmily Shaffer }
328391f3303SEmily Shaffer 
329a8be7dc8SDeepak Kumar Sahu /**
330a8be7dc8SDeepak Kumar Sahu  *  @brief implements the get sensor type command.
331a8be7dc8SDeepak Kumar Sahu  *  @param - sensorNumber
332a8be7dc8SDeepak Kumar Sahu  *
333a8be7dc8SDeepak Kumar Sahu  *  @return IPMI completion code plus response data on success.
334a8be7dc8SDeepak Kumar Sahu  *   - sensorType
335a8be7dc8SDeepak Kumar Sahu  *   - eventType
336a8be7dc8SDeepak Kumar Sahu  **/
337a8be7dc8SDeepak Kumar Sahu 
338a8be7dc8SDeepak Kumar Sahu ipmi::RspType<uint8_t, // sensorType
339a8be7dc8SDeepak Kumar Sahu               uint8_t  // eventType
340a8be7dc8SDeepak Kumar Sahu               >
341a8be7dc8SDeepak Kumar Sahu     ipmiGetSensorType(uint8_t sensorNumber)
34298a23840SMatthew Barth {
343a8be7dc8SDeepak Kumar Sahu     uint8_t sensorType = find_type_for_sensor_number(sensorNumber);
34498a23840SMatthew Barth 
345a8be7dc8SDeepak Kumar Sahu     if (sensorType == 0)
3460b02be92SPatrick Venture     {
347a8be7dc8SDeepak Kumar Sahu         return ipmi::responseSensorInvalid();
34898a23840SMatthew Barth     }
34998a23840SMatthew Barth 
350a8be7dc8SDeepak Kumar Sahu     constexpr uint8_t eventType = 0x6F;
351a8be7dc8SDeepak Kumar Sahu     return ipmi::responseSuccess(sensorType, eventType);
35298a23840SMatthew Barth }
35398a23840SMatthew Barth 
3540b02be92SPatrick Venture const std::set<std::string> analogSensorInterfaces = {
355cc941e15SEmily Shaffer     "xyz.openbmc_project.Sensor.Value",
356e9a64056SPatrick Venture     "xyz.openbmc_project.Control.FanPwm",
357cc941e15SEmily Shaffer };
358cc941e15SEmily Shaffer 
359cc941e15SEmily Shaffer bool isAnalogSensor(const std::string& interface)
360cc941e15SEmily Shaffer {
361cc941e15SEmily Shaffer     return (analogSensorInterfaces.count(interface));
362cc941e15SEmily Shaffer }
363cc941e15SEmily Shaffer 
3649da3a750SDeepak Kumar Sahu /**
3659da3a750SDeepak Kumar Sahu @brief This command is used to set sensorReading.
3669da3a750SDeepak Kumar Sahu 
3679da3a750SDeepak Kumar Sahu @param
3689da3a750SDeepak Kumar Sahu     -  sensorNumber
3699da3a750SDeepak Kumar Sahu     -  operation
3709da3a750SDeepak Kumar Sahu     -  reading
3719da3a750SDeepak Kumar Sahu     -  assertOffset0_7
3729da3a750SDeepak Kumar Sahu     -  assertOffset8_14
3739da3a750SDeepak Kumar Sahu     -  deassertOffset0_7
3749da3a750SDeepak Kumar Sahu     -  deassertOffset8_14
3759da3a750SDeepak Kumar Sahu     -  eventData1
3769da3a750SDeepak Kumar Sahu     -  eventData2
3779da3a750SDeepak Kumar Sahu     -  eventData3
3789da3a750SDeepak Kumar Sahu 
3799da3a750SDeepak Kumar Sahu @return completion code on success.
3809da3a750SDeepak Kumar Sahu **/
3819da3a750SDeepak Kumar Sahu 
3829da3a750SDeepak Kumar Sahu ipmi::RspType<> ipmiSetSensorReading(uint8_t sensorNumber, uint8_t operation,
3839da3a750SDeepak Kumar Sahu                                      uint8_t reading, uint8_t assertOffset0_7,
3849da3a750SDeepak Kumar Sahu                                      uint8_t assertOffset8_14,
3859da3a750SDeepak Kumar Sahu                                      uint8_t deassertOffset0_7,
3869da3a750SDeepak Kumar Sahu                                      uint8_t deassertOffset8_14,
3879da3a750SDeepak Kumar Sahu                                      uint8_t eventData1, uint8_t eventData2,
3889da3a750SDeepak Kumar Sahu                                      uint8_t eventData3)
389be703f71STom Joseph {
3909da3a750SDeepak Kumar Sahu     log<level::DEBUG>("IPMI SET_SENSOR",
3919da3a750SDeepak Kumar Sahu                       entry("SENSOR_NUM=0x%02x", sensorNumber));
3929da3a750SDeepak Kumar Sahu 
3930634e989SArun P. Mohanan     if (sensorNumber == 0xFF)
3940634e989SArun P. Mohanan     {
3950634e989SArun P. Mohanan         return ipmi::responseInvalidFieldRequest();
3960634e989SArun P. Mohanan     }
3979da3a750SDeepak Kumar Sahu     ipmi::sensor::SetSensorReadingReq cmdData;
3989da3a750SDeepak Kumar Sahu 
3999da3a750SDeepak Kumar Sahu     cmdData.number = sensorNumber;
4009da3a750SDeepak Kumar Sahu     cmdData.operation = operation;
4019da3a750SDeepak Kumar Sahu     cmdData.reading = reading;
4029da3a750SDeepak Kumar Sahu     cmdData.assertOffset0_7 = assertOffset0_7;
4039da3a750SDeepak Kumar Sahu     cmdData.assertOffset8_14 = assertOffset8_14;
4049da3a750SDeepak Kumar Sahu     cmdData.deassertOffset0_7 = deassertOffset0_7;
4059da3a750SDeepak Kumar Sahu     cmdData.deassertOffset8_14 = deassertOffset8_14;
4069da3a750SDeepak Kumar Sahu     cmdData.eventData1 = eventData1;
4079da3a750SDeepak Kumar Sahu     cmdData.eventData2 = eventData2;
4089da3a750SDeepak Kumar Sahu     cmdData.eventData3 = eventData3;
409be703f71STom Joseph 
410be703f71STom Joseph     // Check if the Sensor Number is present
411db0cbe64SPatrick Venture     const auto iter = ipmi::sensor::sensors.find(sensorNumber);
412db0cbe64SPatrick Venture     if (iter == ipmi::sensor::sensors.end())
413be703f71STom Joseph     {
4149da3a750SDeepak Kumar Sahu         updateSensorRecordFromSSRAESC(&sensorNumber);
4159da3a750SDeepak Kumar Sahu         return ipmi::responseSuccess();
416be703f71STom Joseph     }
417be703f71STom Joseph 
41818e99992SDhruvaraj Subhashchandran     try
41918e99992SDhruvaraj Subhashchandran     {
4200922bde4SJayanth Othayoth         if (ipmi::sensor::Mutability::Write !=
4210922bde4SJayanth Othayoth             (iter->second.mutability & ipmi::sensor::Mutability::Write))
4220922bde4SJayanth Othayoth         {
4230922bde4SJayanth Othayoth             log<level::ERR>("Sensor Set operation is not allowed",
4249da3a750SDeepak Kumar Sahu                             entry("SENSOR_NUM=%d", sensorNumber));
4259da3a750SDeepak Kumar Sahu             return ipmi::responseIllegalCommand();
4260922bde4SJayanth Othayoth         }
4279da3a750SDeepak Kumar Sahu         auto ipmiRC = iter->second.updateFunc(cmdData, iter->second);
4289da3a750SDeepak Kumar Sahu         return ipmi::response(ipmiRC);
429be703f71STom Joseph     }
430a2ad2da8SPatrick Williams     catch (const InternalFailure& e)
43118e99992SDhruvaraj Subhashchandran     {
43218e99992SDhruvaraj Subhashchandran         log<level::ERR>("Set sensor failed",
4339da3a750SDeepak Kumar Sahu                         entry("SENSOR_NUM=%d", sensorNumber));
43418e99992SDhruvaraj Subhashchandran         commit<InternalFailure>();
4359da3a750SDeepak Kumar Sahu         return ipmi::responseUnspecifiedError();
43618e99992SDhruvaraj Subhashchandran     }
4378202432fSTom Joseph     catch (const std::runtime_error& e)
4388202432fSTom Joseph     {
4398202432fSTom Joseph         log<level::ERR>(e.what());
4409da3a750SDeepak Kumar Sahu         return ipmi::responseUnspecifiedError();
4418202432fSTom Joseph     }
44298a23840SMatthew Barth }
44398a23840SMatthew Barth 
4444c3feba5Sjayaprakash Mutyala /** @brief implements the get sensor reading command
4454c3feba5Sjayaprakash Mutyala  *  @param sensorNum - sensor number
4464c3feba5Sjayaprakash Mutyala  *
4474c3feba5Sjayaprakash Mutyala  *  @returns IPMI completion code plus response data
4484c3feba5Sjayaprakash Mutyala  *   - senReading           - sensor reading
4494c3feba5Sjayaprakash Mutyala  *   - reserved
4504c3feba5Sjayaprakash Mutyala  *   - readState            - sensor reading state enabled
4514c3feba5Sjayaprakash Mutyala  *   - senScanState         - sensor scan state disabled
4524c3feba5Sjayaprakash Mutyala  *   - allEventMessageState - all Event message state disabled
4534c3feba5Sjayaprakash Mutyala  *   - assertionStatesLsb   - threshold levels states
4544c3feba5Sjayaprakash Mutyala  *   - assertionStatesMsb   - discrete reading sensor states
4554c3feba5Sjayaprakash Mutyala  */
4564c3feba5Sjayaprakash Mutyala ipmi::RspType<uint8_t, // sensor reading
4573ee668f9STom Joseph 
4584c3feba5Sjayaprakash Mutyala               uint5_t, // reserved
4594c3feba5Sjayaprakash Mutyala               bool,    // reading state
4604cc42556SSui Chen               bool,    // 0 = sensor scanning state disabled
4614cc42556SSui Chen               bool,    // 0 = all event messages disabled
4624c3feba5Sjayaprakash Mutyala 
4634c3feba5Sjayaprakash Mutyala               uint8_t, // threshold levels states
4644c3feba5Sjayaprakash Mutyala               uint8_t  // discrete reading sensor states
4654c3feba5Sjayaprakash Mutyala               >
4664c3feba5Sjayaprakash Mutyala     ipmiSensorGetSensorReading(uint8_t sensorNum)
4674c3feba5Sjayaprakash Mutyala {
4684c3feba5Sjayaprakash Mutyala     if (sensorNum == 0xFF)
4694c3feba5Sjayaprakash Mutyala     {
4704c3feba5Sjayaprakash Mutyala         return ipmi::responseInvalidFieldRequest();
4714c3feba5Sjayaprakash Mutyala     }
4724c3feba5Sjayaprakash Mutyala 
4734c3feba5Sjayaprakash Mutyala     const auto iter = ipmi::sensor::sensors.find(sensorNum);
474db0cbe64SPatrick Venture     if (iter == ipmi::sensor::sensors.end())
47514c15467STom Joseph     {
4764c3feba5Sjayaprakash Mutyala         return ipmi::responseSensorInvalid();
47714c15467STom Joseph     }
47813b87a3eSTom Joseph     if (ipmi::sensor::Mutability::Read !=
47913b87a3eSTom Joseph         (iter->second.mutability & ipmi::sensor::Mutability::Read))
48013b87a3eSTom Joseph     {
4814c3feba5Sjayaprakash Mutyala         return ipmi::responseIllegalCommand();
48213b87a3eSTom Joseph     }
48314c15467STom Joseph 
48414c15467STom Joseph     try
48514c15467STom Joseph     {
4864cc42556SSui Chen         ipmi::sensor::GetSensorResponse getResponse =
4874cc42556SSui Chen             iter->second.getFunc(iter->second);
4884c3feba5Sjayaprakash Mutyala 
4894cc42556SSui Chen         return ipmi::responseSuccess(getResponse.reading, uint5_t(0),
4904cc42556SSui Chen                                      getResponse.readingOrStateUnavailable,
4914cc42556SSui Chen                                      getResponse.scanningEnabled,
4924cc42556SSui Chen                                      getResponse.allEventMessagesEnabled,
4934cc42556SSui Chen                                      getResponse.thresholdLevelsStates,
4944cc42556SSui Chen                                      getResponse.discreteReadingSensorStates);
49514c15467STom Joseph     }
4969cf85627SBrandon Kim #ifdef UPDATE_FUNCTIONAL_ON_FAIL
4979cf85627SBrandon Kim     catch (const SensorFunctionalError& e)
4989cf85627SBrandon Kim     {
4994c3feba5Sjayaprakash Mutyala         return ipmi::responseResponseError();
5009cf85627SBrandon Kim     }
5019cf85627SBrandon Kim #endif
5023ee668f9STom Joseph     catch (const std::exception& e)
50314c15467STom Joseph     {
5044c3feba5Sjayaprakash Mutyala         // Intitilizing with default values
5054c3feba5Sjayaprakash Mutyala         constexpr uint8_t senReading = 0;
5064c3feba5Sjayaprakash Mutyala         constexpr uint5_t reserved{0};
5074c3feba5Sjayaprakash Mutyala         constexpr bool readState = true;
5084c3feba5Sjayaprakash Mutyala         constexpr bool senScanState = false;
5094c3feba5Sjayaprakash Mutyala         constexpr bool allEventMessageState = false;
5104c3feba5Sjayaprakash Mutyala         constexpr uint8_t assertionStatesLsb = 0;
5114c3feba5Sjayaprakash Mutyala         constexpr uint8_t assertionStatesMsb = 0;
5124c3feba5Sjayaprakash Mutyala 
5134c3feba5Sjayaprakash Mutyala         return ipmi::responseSuccess(senReading, reserved, readState,
5144c3feba5Sjayaprakash Mutyala                                      senScanState, allEventMessageState,
5154c3feba5Sjayaprakash Mutyala                                      assertionStatesLsb, assertionStatesMsb);
51614c15467STom Joseph     }
51798a23840SMatthew Barth }
51898a23840SMatthew Barth 
51989a83b67SKonstantin Aladyshev get_sdr::GetSensorThresholdsResponse
52089a83b67SKonstantin Aladyshev     getSensorThresholds(ipmi::Context::ptr& ctx, uint8_t sensorNum)
5210ac0dd23STom Joseph {
522515bc375SWilliam A. Kennington III     get_sdr::GetSensorThresholdsResponse resp{};
5230ac0dd23STom Joseph     constexpr auto warningThreshIntf =
5240ac0dd23STom Joseph         "xyz.openbmc_project.Sensor.Threshold.Warning";
5250ac0dd23STom Joseph     constexpr auto criticalThreshIntf =
5260ac0dd23STom Joseph         "xyz.openbmc_project.Sensor.Threshold.Critical";
5270ac0dd23STom Joseph 
528db0cbe64SPatrick Venture     const auto iter = ipmi::sensor::sensors.find(sensorNum);
5290ac0dd23STom Joseph     const auto info = iter->second;
5300ac0dd23STom Joseph 
53189a83b67SKonstantin Aladyshev     std::string service;
53289a83b67SKonstantin Aladyshev     boost::system::error_code ec;
53389a83b67SKonstantin Aladyshev     ec = ipmi::getService(ctx, info.sensorInterface, info.sensorPath, service);
53489a83b67SKonstantin Aladyshev     if (ec)
53589a83b67SKonstantin Aladyshev     {
53689a83b67SKonstantin Aladyshev         return resp;
53789a83b67SKonstantin Aladyshev     }
5380ac0dd23STom Joseph 
53989a83b67SKonstantin Aladyshev     ipmi::PropertyMap warnThresholds;
54089a83b67SKonstantin Aladyshev     ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
54189a83b67SKonstantin Aladyshev                                     warningThreshIntf, warnThresholds);
54289a83b67SKonstantin Aladyshev     if (!ec)
54389a83b67SKonstantin Aladyshev     {
544f442e119SVernon Mauery         double warnLow = std::visit(ipmi::VariantToDoubleVisitor(),
5451e12112bSJames Feist                                     warnThresholds["WarningLow"]);
546f442e119SVernon Mauery         double warnHigh = std::visit(ipmi::VariantToDoubleVisitor(),
54737544429SJames Feist                                      warnThresholds["WarningHigh"]);
5480ac0dd23STom Joseph 
549e0a41100SKonstantin Aladyshev         if (std::isfinite(warnLow))
5500ac0dd23STom Joseph         {
551586d35b4SPatrick Venture             warnLow *= std::pow(10, info.scale - info.exponentR);
552996c9798Sjayaprakash Mutyala             resp.lowerNonCritical = static_cast<uint8_t>(
5530b02be92SPatrick Venture                 (warnLow - info.scaledOffset) / info.coefficientM);
554996c9798Sjayaprakash Mutyala             resp.validMask |= static_cast<uint8_t>(
5550ac0dd23STom Joseph                 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
5560ac0dd23STom Joseph         }
5570ac0dd23STom Joseph 
558e0a41100SKonstantin Aladyshev         if (std::isfinite(warnHigh))
5590ac0dd23STom Joseph         {
560586d35b4SPatrick Venture             warnHigh *= std::pow(10, info.scale - info.exponentR);
561996c9798Sjayaprakash Mutyala             resp.upperNonCritical = static_cast<uint8_t>(
5620b02be92SPatrick Venture                 (warnHigh - info.scaledOffset) / info.coefficientM);
563996c9798Sjayaprakash Mutyala             resp.validMask |= static_cast<uint8_t>(
5640ac0dd23STom Joseph                 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
5650ac0dd23STom Joseph         }
56689a83b67SKonstantin Aladyshev     }
5670ac0dd23STom Joseph 
56889a83b67SKonstantin Aladyshev     ipmi::PropertyMap critThresholds;
56989a83b67SKonstantin Aladyshev     ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
57089a83b67SKonstantin Aladyshev                                     criticalThreshIntf, critThresholds);
57189a83b67SKonstantin Aladyshev     if (!ec)
57289a83b67SKonstantin Aladyshev     {
573f442e119SVernon Mauery         double critLow = std::visit(ipmi::VariantToDoubleVisitor(),
5741e12112bSJames Feist                                     critThresholds["CriticalLow"]);
575f442e119SVernon Mauery         double critHigh = std::visit(ipmi::VariantToDoubleVisitor(),
57637544429SJames Feist                                      critThresholds["CriticalHigh"]);
5770ac0dd23STom Joseph 
578e0a41100SKonstantin Aladyshev         if (std::isfinite(critLow))
5790ac0dd23STom Joseph         {
580586d35b4SPatrick Venture             critLow *= std::pow(10, info.scale - info.exponentR);
581996c9798Sjayaprakash Mutyala             resp.lowerCritical = static_cast<uint8_t>(
5820b02be92SPatrick Venture                 (critLow - info.scaledOffset) / info.coefficientM);
583996c9798Sjayaprakash Mutyala             resp.validMask |= static_cast<uint8_t>(
5840ac0dd23STom Joseph                 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
5850ac0dd23STom Joseph         }
5860ac0dd23STom Joseph 
587e0a41100SKonstantin Aladyshev         if (std::isfinite(critHigh))
5880ac0dd23STom Joseph         {
589586d35b4SPatrick Venture             critHigh *= std::pow(10, info.scale - info.exponentR);
590996c9798Sjayaprakash Mutyala             resp.upperCritical = static_cast<uint8_t>(
5910b02be92SPatrick Venture                 (critHigh - info.scaledOffset) / info.coefficientM);
592996c9798Sjayaprakash Mutyala             resp.validMask |= static_cast<uint8_t>(
5930ac0dd23STom Joseph                 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
5940ac0dd23STom Joseph         }
59589a83b67SKonstantin Aladyshev     }
596996c9798Sjayaprakash Mutyala 
597996c9798Sjayaprakash Mutyala     return resp;
5980ac0dd23STom Joseph }
5990ac0dd23STom Joseph 
600996c9798Sjayaprakash Mutyala /** @brief implements the get sensor thresholds command
60189a83b67SKonstantin Aladyshev  *  @param ctx - IPMI context pointer
602996c9798Sjayaprakash Mutyala  *  @param sensorNum - sensor number
603996c9798Sjayaprakash Mutyala  *
604996c9798Sjayaprakash Mutyala  *  @returns IPMI completion code plus response data
605996c9798Sjayaprakash Mutyala  *   - validMask - threshold mask
606996c9798Sjayaprakash Mutyala  *   - lower non-critical threshold - IPMI messaging state
607996c9798Sjayaprakash Mutyala  *   - lower critical threshold - link authentication state
608996c9798Sjayaprakash Mutyala  *   - lower non-recoverable threshold - callback state
609996c9798Sjayaprakash Mutyala  *   - upper non-critical threshold
610996c9798Sjayaprakash Mutyala  *   - upper critical
611996c9798Sjayaprakash Mutyala  *   - upper non-recoverable
612996c9798Sjayaprakash Mutyala  */
613996c9798Sjayaprakash Mutyala ipmi::RspType<uint8_t, // validMask
614996c9798Sjayaprakash Mutyala               uint8_t, // lowerNonCritical
615996c9798Sjayaprakash Mutyala               uint8_t, // lowerCritical
616996c9798Sjayaprakash Mutyala               uint8_t, // lowerNonRecoverable
617996c9798Sjayaprakash Mutyala               uint8_t, // upperNonCritical
618996c9798Sjayaprakash Mutyala               uint8_t, // upperCritical
619996c9798Sjayaprakash Mutyala               uint8_t  // upperNonRecoverable
620996c9798Sjayaprakash Mutyala               >
62189a83b67SKonstantin Aladyshev     ipmiSensorGetSensorThresholds(ipmi::Context::ptr& ctx, uint8_t sensorNum)
6225c0beec1SDhruvaraj Subhashchandran {
6230ac0dd23STom Joseph     constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
6245c0beec1SDhruvaraj Subhashchandran 
625db0cbe64SPatrick Venture     const auto iter = ipmi::sensor::sensors.find(sensorNum);
626db0cbe64SPatrick Venture     if (iter == ipmi::sensor::sensors.end())
6275c0beec1SDhruvaraj Subhashchandran     {
628996c9798Sjayaprakash Mutyala         return ipmi::responseSensorInvalid();
6295c0beec1SDhruvaraj Subhashchandran     }
6305c0beec1SDhruvaraj Subhashchandran 
6310ac0dd23STom Joseph     const auto info = iter->second;
6325c0beec1SDhruvaraj Subhashchandran 
6335c0beec1SDhruvaraj Subhashchandran     // Proceed only if the sensor value interface is implemented.
6340ac0dd23STom Joseph     if (info.propertyInterfaces.find(valueInterface) ==
6350ac0dd23STom Joseph         info.propertyInterfaces.end())
6365c0beec1SDhruvaraj Subhashchandran     {
6375c0beec1SDhruvaraj Subhashchandran         // return with valid mask as 0
638996c9798Sjayaprakash Mutyala         return ipmi::responseSuccess();
6395c0beec1SDhruvaraj Subhashchandran     }
6405c0beec1SDhruvaraj Subhashchandran 
64114a47819SLei YU     auto it = sensorThresholdMap.find(sensorNum);
64214a47819SLei YU     if (it == sensorThresholdMap.end())
64314a47819SLei YU     {
64414a47819SLei YU         sensorThresholdMap[sensorNum] = getSensorThresholds(ctx, sensorNum);
64514a47819SLei YU     }
64614a47819SLei YU 
64714a47819SLei YU     const auto& resp = sensorThresholdMap[sensorNum];
6485c0beec1SDhruvaraj Subhashchandran 
649996c9798Sjayaprakash Mutyala     return ipmi::responseSuccess(resp.validMask, resp.lowerNonCritical,
650996c9798Sjayaprakash Mutyala                                  resp.lowerCritical, resp.lowerNonRecoverable,
651996c9798Sjayaprakash Mutyala                                  resp.upperNonCritical, resp.upperCritical,
652996c9798Sjayaprakash Mutyala                                  resp.upperNonRecoverable);
6535c0beec1SDhruvaraj Subhashchandran }
6545c0beec1SDhruvaraj Subhashchandran 
655f93da667SLotus Xu /** @brief implements the Set Sensor threshold command
656f93da667SLotus Xu  *  @param sensorNumber        - sensor number
657f93da667SLotus Xu  *  @param lowerNonCriticalThreshMask
658f93da667SLotus Xu  *  @param lowerCriticalThreshMask
659f93da667SLotus Xu  *  @param lowerNonRecovThreshMask
660f93da667SLotus Xu  *  @param upperNonCriticalThreshMask
661f93da667SLotus Xu  *  @param upperCriticalThreshMask
662f93da667SLotus Xu  *  @param upperNonRecovThreshMask
663f93da667SLotus Xu  *  @param reserved
664f93da667SLotus Xu  *  @param lowerNonCritical    - lower non-critical threshold
665f93da667SLotus Xu  *  @param lowerCritical       - Lower critical threshold
666f93da667SLotus Xu  *  @param lowerNonRecoverable - Lower non recovarable threshold
667f93da667SLotus Xu  *  @param upperNonCritical    - Upper non-critical threshold
668f93da667SLotus Xu  *  @param upperCritical       - Upper critical
669f93da667SLotus Xu  *  @param upperNonRecoverable - Upper Non-recoverable
670f93da667SLotus Xu  *
671f93da667SLotus Xu  *  @returns IPMI completion code
672f93da667SLotus Xu  */
673f93da667SLotus Xu ipmi::RspType<> ipmiSenSetSensorThresholds(
674f93da667SLotus Xu     ipmi::Context::ptr& ctx, uint8_t sensorNum, bool lowerNonCriticalThreshMask,
675f93da667SLotus Xu     bool lowerCriticalThreshMask, bool lowerNonRecovThreshMask,
676f93da667SLotus Xu     bool upperNonCriticalThreshMask, bool upperCriticalThreshMask,
677f93da667SLotus Xu     bool upperNonRecovThreshMask, uint2_t reserved, uint8_t lowerNonCritical,
678f93da667SLotus Xu     uint8_t lowerCritical, uint8_t lowerNonRecoverable,
679f93da667SLotus Xu     uint8_t upperNonCritical, uint8_t upperCritical,
680f93da667SLotus Xu     uint8_t upperNonRecoverable)
681f93da667SLotus Xu {
682f93da667SLotus Xu     if (reserved)
683f93da667SLotus Xu     {
684f93da667SLotus Xu         return ipmi::responseInvalidFieldRequest();
685f93da667SLotus Xu     }
686f93da667SLotus Xu 
687f93da667SLotus Xu     // lower nc and upper nc not suppported on any sensor
688f93da667SLotus Xu     if (lowerNonRecovThreshMask || upperNonRecovThreshMask)
689f93da667SLotus Xu     {
690f93da667SLotus Xu         return ipmi::responseInvalidFieldRequest();
691f93da667SLotus Xu     }
692f93da667SLotus Xu 
693f93da667SLotus Xu     // if none of the threshold mask are set, nothing to do
694f93da667SLotus Xu     if (!(lowerNonCriticalThreshMask | lowerCriticalThreshMask |
695f93da667SLotus Xu           lowerNonRecovThreshMask | upperNonCriticalThreshMask |
696f93da667SLotus Xu           upperCriticalThreshMask | upperNonRecovThreshMask))
697f93da667SLotus Xu     {
698f93da667SLotus Xu         return ipmi::responseSuccess();
699f93da667SLotus Xu     }
700f93da667SLotus Xu 
701f93da667SLotus Xu     constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
702f93da667SLotus Xu 
703f93da667SLotus Xu     const auto iter = ipmi::sensor::sensors.find(sensorNum);
704f93da667SLotus Xu     if (iter == ipmi::sensor::sensors.end())
705f93da667SLotus Xu     {
706f93da667SLotus Xu         return ipmi::responseSensorInvalid();
707f93da667SLotus Xu     }
708f93da667SLotus Xu 
709f93da667SLotus Xu     const auto& info = iter->second;
710f93da667SLotus Xu 
711f93da667SLotus Xu     // Proceed only if the sensor value interface is implemented.
712f93da667SLotus Xu     if (info.propertyInterfaces.find(valueInterface) ==
713f93da667SLotus Xu         info.propertyInterfaces.end())
714f93da667SLotus Xu     {
715f93da667SLotus Xu         // return with valid mask as 0
716f93da667SLotus Xu         return ipmi::responseSuccess();
717f93da667SLotus Xu     }
718f93da667SLotus Xu 
719f93da667SLotus Xu     constexpr auto warningThreshIntf =
720f93da667SLotus Xu         "xyz.openbmc_project.Sensor.Threshold.Warning";
721f93da667SLotus Xu     constexpr auto criticalThreshIntf =
722f93da667SLotus Xu         "xyz.openbmc_project.Sensor.Threshold.Critical";
723f93da667SLotus Xu 
724f93da667SLotus Xu     std::string service;
725f93da667SLotus Xu     boost::system::error_code ec;
726f93da667SLotus Xu     ec = ipmi::getService(ctx, info.sensorInterface, info.sensorPath, service);
727f93da667SLotus Xu     if (ec)
728f93da667SLotus Xu     {
729f93da667SLotus Xu         return ipmi::responseResponseError();
730f93da667SLotus Xu     }
731f93da667SLotus Xu     // store a vector of property name, value to set, and interface
732f93da667SLotus Xu     std::vector<std::tuple<std::string, uint8_t, std::string>> thresholdsToSet;
733f93da667SLotus Xu 
734f93da667SLotus Xu     // define the indexes of the tuple
735f93da667SLotus Xu     constexpr uint8_t propertyName = 0;
736f93da667SLotus Xu     constexpr uint8_t thresholdValue = 1;
737f93da667SLotus Xu     constexpr uint8_t interface = 2;
738f93da667SLotus Xu     // verifiy all needed fields are present
739f93da667SLotus Xu     if (lowerCriticalThreshMask || upperCriticalThreshMask)
740f93da667SLotus Xu     {
741f93da667SLotus Xu 
742f93da667SLotus Xu         ipmi::PropertyMap findThreshold;
743f93da667SLotus Xu         ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
744f93da667SLotus Xu                                         criticalThreshIntf, findThreshold);
745f93da667SLotus Xu 
746f93da667SLotus Xu         if (!ec)
747f93da667SLotus Xu         {
748f93da667SLotus Xu             if (lowerCriticalThreshMask)
749f93da667SLotus Xu             {
750f93da667SLotus Xu                 auto findLower = findThreshold.find("CriticalLow");
751f93da667SLotus Xu                 if (findLower == findThreshold.end())
752f93da667SLotus Xu                 {
753f93da667SLotus Xu                     return ipmi::responseInvalidFieldRequest();
754f93da667SLotus Xu                 }
755f93da667SLotus Xu                 thresholdsToSet.emplace_back("CriticalLow", lowerCritical,
756f93da667SLotus Xu                                              criticalThreshIntf);
757f93da667SLotus Xu             }
758f93da667SLotus Xu             if (upperCriticalThreshMask)
759f93da667SLotus Xu             {
760f93da667SLotus Xu                 auto findUpper = findThreshold.find("CriticalHigh");
761f93da667SLotus Xu                 if (findUpper == findThreshold.end())
762f93da667SLotus Xu                 {
763f93da667SLotus Xu                     return ipmi::responseInvalidFieldRequest();
764f93da667SLotus Xu                 }
765f93da667SLotus Xu                 thresholdsToSet.emplace_back("CriticalHigh", upperCritical,
766f93da667SLotus Xu                                              criticalThreshIntf);
767f93da667SLotus Xu             }
768f93da667SLotus Xu         }
769f93da667SLotus Xu     }
770f93da667SLotus Xu     if (lowerNonCriticalThreshMask || upperNonCriticalThreshMask)
771f93da667SLotus Xu     {
772f93da667SLotus Xu         ipmi::PropertyMap findThreshold;
773f93da667SLotus Xu         ec = ipmi::getAllDbusProperties(ctx, service, info.sensorPath,
774f93da667SLotus Xu                                         warningThreshIntf, findThreshold);
775f93da667SLotus Xu 
776f93da667SLotus Xu         if (!ec)
777f93da667SLotus Xu         {
778f93da667SLotus Xu             if (lowerNonCriticalThreshMask)
779f93da667SLotus Xu             {
780f93da667SLotus Xu                 auto findLower = findThreshold.find("WarningLow");
781f93da667SLotus Xu                 if (findLower == findThreshold.end())
782f93da667SLotus Xu                 {
783f93da667SLotus Xu                     return ipmi::responseInvalidFieldRequest();
784f93da667SLotus Xu                 }
785f93da667SLotus Xu                 thresholdsToSet.emplace_back("WarningLow", lowerNonCritical,
786f93da667SLotus Xu                                              warningThreshIntf);
787f93da667SLotus Xu             }
788f93da667SLotus Xu             if (upperNonCriticalThreshMask)
789f93da667SLotus Xu             {
790f93da667SLotus Xu                 auto findUpper = findThreshold.find("WarningHigh");
791f93da667SLotus Xu                 if (findUpper == findThreshold.end())
792f93da667SLotus Xu                 {
793f93da667SLotus Xu                     return ipmi::responseInvalidFieldRequest();
794f93da667SLotus Xu                 }
795f93da667SLotus Xu                 thresholdsToSet.emplace_back("WarningHigh", upperNonCritical,
796f93da667SLotus Xu                                              warningThreshIntf);
797f93da667SLotus Xu             }
798f93da667SLotus Xu         }
799f93da667SLotus Xu     }
800f93da667SLotus Xu     for (const auto& property : thresholdsToSet)
801f93da667SLotus Xu     {
802f93da667SLotus Xu         // from section 36.3 in the IPMI Spec, assume all linear
803f93da667SLotus Xu         double valueToSet =
804f93da667SLotus Xu             ((info.coefficientM * std::get<thresholdValue>(property)) +
805f93da667SLotus Xu              (info.scaledOffset * std::pow(10.0, info.scale))) *
806f93da667SLotus Xu             std::pow(10.0, info.exponentR);
807f93da667SLotus Xu         ipmi::setDbusProperty(
808f93da667SLotus Xu             ctx, service, info.sensorPath, std::get<interface>(property),
809f93da667SLotus Xu             std::get<propertyName>(property), ipmi::Value(valueToSet));
810f93da667SLotus Xu     }
811f93da667SLotus Xu 
81214a47819SLei YU     // Invalidate the cache
81314a47819SLei YU     sensorThresholdMap.erase(sensorNum);
814f93da667SLotus Xu     return ipmi::responseSuccess();
815f93da667SLotus Xu }
816f93da667SLotus Xu 
817d957823eSjayaprakash Mutyala /** @brief implements the get SDR Info command
818d957823eSjayaprakash Mutyala  *  @param count - Operation
819d957823eSjayaprakash Mutyala  *
820d957823eSjayaprakash Mutyala  *  @returns IPMI completion code plus response data
821d957823eSjayaprakash Mutyala  *   - sdrCount - sensor/SDR count
822d957823eSjayaprakash Mutyala  *   - lunsAndDynamicPopulation - static/Dynamic sensor population flag
823d957823eSjayaprakash Mutyala  */
824d957823eSjayaprakash Mutyala ipmi::RspType<uint8_t, // respcount
825d957823eSjayaprakash Mutyala               uint8_t  // dynamic population flags
826d957823eSjayaprakash Mutyala               >
827d957823eSjayaprakash Mutyala     ipmiSensorGetDeviceSdrInfo(std::optional<uint8_t> count)
828d06e0e7eSEmily Shaffer {
829d957823eSjayaprakash Mutyala     uint8_t sdrCount;
830d957823eSjayaprakash Mutyala     // multiple LUNs not supported.
831d957823eSjayaprakash Mutyala     constexpr uint8_t lunsAndDynamicPopulation = 1;
832d957823eSjayaprakash Mutyala     constexpr uint8_t getSdrCount = 0x01;
833d957823eSjayaprakash Mutyala     constexpr uint8_t getSensorCount = 0x00;
834d957823eSjayaprakash Mutyala 
835d957823eSjayaprakash Mutyala     if (count.value_or(0) == getSdrCount)
836d06e0e7eSEmily Shaffer     {
837d957823eSjayaprakash Mutyala         // Get SDR count. This returns the total number of SDRs in the device.
83887fd2cd1SPatrick Venture         const auto& entityRecords =
83987fd2cd1SPatrick Venture             ipmi::sensor::EntityInfoMapContainer::getContainer()
84087fd2cd1SPatrick Venture                 ->getIpmiEntityRecords();
841db0cbe64SPatrick Venture         sdrCount =
842db0cbe64SPatrick Venture             ipmi::sensor::sensors.size() + frus.size() + entityRecords.size();
843d957823eSjayaprakash Mutyala     }
844d957823eSjayaprakash Mutyala     else if (count.value_or(0) == getSensorCount)
845d957823eSjayaprakash Mutyala     {
846d957823eSjayaprakash Mutyala         // Get Sensor count. This returns the number of sensors
847db0cbe64SPatrick Venture         sdrCount = ipmi::sensor::sensors.size();
848d06e0e7eSEmily Shaffer     }
849d06e0e7eSEmily Shaffer     else
850d06e0e7eSEmily Shaffer     {
851d957823eSjayaprakash Mutyala         return ipmi::responseInvalidCommandOnLun();
852d06e0e7eSEmily Shaffer     }
853d06e0e7eSEmily Shaffer 
854d957823eSjayaprakash Mutyala     return ipmi::responseSuccess(sdrCount, lunsAndDynamicPopulation);
855d06e0e7eSEmily Shaffer }
856d06e0e7eSEmily Shaffer 
857d957823eSjayaprakash Mutyala /** @brief implements the reserve SDR command
858d957823eSjayaprakash Mutyala  *  @returns IPMI completion code plus response data
859d957823eSjayaprakash Mutyala  *   - reservationID - reservation ID
860d957823eSjayaprakash Mutyala  */
861d957823eSjayaprakash Mutyala ipmi::RspType<uint16_t> ipmiSensorReserveSdr()
862a344afc0SEmily Shaffer {
863a344afc0SEmily Shaffer     // A constant reservation ID is okay until we implement add/remove SDR.
864d957823eSjayaprakash Mutyala     constexpr uint16_t reservationID = 1;
865a344afc0SEmily Shaffer 
866d957823eSjayaprakash Mutyala     return ipmi::responseSuccess(reservationID);
867a344afc0SEmily Shaffer }
86898a23840SMatthew Barth 
869dc212b23STom Joseph void setUnitFieldsForObject(const ipmi::sensor::Info* info,
870cc941e15SEmily Shaffer                             get_sdr::SensorDataFullRecordBody* body)
871bbef71c2SEmily Shaffer {
872bbef71c2SEmily Shaffer     namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
873dc212b23STom Joseph     try
874dc212b23STom Joseph     {
875dc212b23STom Joseph         auto unit = server::Value::convertUnitFromString(info->unit);
876bbef71c2SEmily Shaffer         // Unit strings defined in
877bbef71c2SEmily Shaffer         // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
878bbef71c2SEmily Shaffer         switch (unit)
879bbef71c2SEmily Shaffer         {
880bbef71c2SEmily Shaffer             case server::Value::Unit::DegreesC:
881bbef71c2SEmily Shaffer                 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
882bbef71c2SEmily Shaffer                 break;
883bbef71c2SEmily Shaffer             case server::Value::Unit::RPMS:
884812e44c7SKirill Pakhomov                 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_RPM;
885bbef71c2SEmily Shaffer                 break;
886bbef71c2SEmily Shaffer             case server::Value::Unit::Volts:
887bbef71c2SEmily Shaffer                 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
888bbef71c2SEmily Shaffer                 break;
889bbef71c2SEmily Shaffer             case server::Value::Unit::Meters:
890bbef71c2SEmily Shaffer                 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
891bbef71c2SEmily Shaffer                 break;
892bbef71c2SEmily Shaffer             case server::Value::Unit::Amperes:
893bbef71c2SEmily Shaffer                 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
894bbef71c2SEmily Shaffer                 break;
895bbef71c2SEmily Shaffer             case server::Value::Unit::Joules:
896bbef71c2SEmily Shaffer                 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
897bbef71c2SEmily Shaffer                 break;
898dc212b23STom Joseph             case server::Value::Unit::Watts:
899dc212b23STom Joseph                 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
900dc212b23STom Joseph                 break;
901bbef71c2SEmily Shaffer             default:
902cc941e15SEmily Shaffer                 // Cannot be hit.
903b51bf9c8SPatrick Venture                 std::fprintf(stderr, "Unknown value unit type: = %s\n",
904dc212b23STom Joseph                              info->unit.c_str());
905cc941e15SEmily Shaffer         }
906cc941e15SEmily Shaffer     }
90764678b85SPatrick Venture     catch (const sdbusplus::exception::InvalidEnumString& e)
908cc941e15SEmily Shaffer     {
909cc941e15SEmily Shaffer         log<level::WARNING>("Warning: no unit provided for sensor!");
910cc941e15SEmily Shaffer     }
911cc941e15SEmily Shaffer }
912cc941e15SEmily Shaffer 
913cc941e15SEmily Shaffer ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody* body,
914cc941e15SEmily Shaffer                                      const ipmi::sensor::Info* info,
915cc941e15SEmily Shaffer                                      ipmi_data_len_t data_len)
916cc941e15SEmily Shaffer {
917cc941e15SEmily Shaffer     /* Functional sensor case */
918cc941e15SEmily Shaffer     if (isAnalogSensor(info->propertyInterfaces.begin()->first))
919cc941e15SEmily Shaffer     {
920c5324251STony Lee         body->sensor_units_1 = info->sensorUnits1; // default is 0. unsigned, no
921c5324251STony Lee                                                    // rate, no modifier, not a %
922cc941e15SEmily Shaffer         /* Unit info */
923dc212b23STom Joseph         setUnitFieldsForObject(info, body);
92410f4959aSEmily Shaffer 
92510f4959aSEmily Shaffer         get_sdr::body::set_b(info->coefficientB, body);
92610f4959aSEmily Shaffer         get_sdr::body::set_m(info->coefficientM, body);
92710f4959aSEmily Shaffer         get_sdr::body::set_b_exp(info->exponentB, body);
928dc212b23STom Joseph         get_sdr::body::set_r_exp(info->exponentR, body);
929bbef71c2SEmily Shaffer 
930bbef71c2SEmily Shaffer         get_sdr::body::set_id_type(0b00, body); // 00 = unicode
9319642391dSTom Joseph     }
9329642391dSTom Joseph 
9339642391dSTom Joseph     /* ID string */
934be4ffa87SJeremy Kerr     auto id_string = info->sensorName;
935be4ffa87SJeremy Kerr 
936be4ffa87SJeremy Kerr     if (id_string.empty())
937be4ffa87SJeremy Kerr     {
938be4ffa87SJeremy Kerr         id_string = info->sensorNameFunc(*info);
939be4ffa87SJeremy Kerr     }
9409642391dSTom Joseph 
941bbef71c2SEmily Shaffer     if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
942bbef71c2SEmily Shaffer     {
943bbef71c2SEmily Shaffer         get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
944bbef71c2SEmily Shaffer     }
945bbef71c2SEmily Shaffer     else
946bbef71c2SEmily Shaffer     {
947bbef71c2SEmily Shaffer         get_sdr::body::set_id_strlen(id_string.length(), body);
948bbef71c2SEmily Shaffer     }
949bbef71c2SEmily Shaffer     strncpy(body->id_string, id_string.c_str(),
950bbef71c2SEmily Shaffer             get_sdr::body::get_id_strlen(body));
951bbef71c2SEmily Shaffer 
952bbef71c2SEmily Shaffer     return IPMI_CC_OK;
953bbef71c2SEmily Shaffer };
954bbef71c2SEmily Shaffer 
955e0cc8553SRatan Gupta ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
956e0cc8553SRatan Gupta                             ipmi_data_len_t data_len)
957e0cc8553SRatan Gupta {
958e0cc8553SRatan Gupta     auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
959e0cc8553SRatan Gupta     auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
960e0cc8553SRatan Gupta     get_sdr::SensorDataFruRecord record{};
961e0cc8553SRatan Gupta     auto dataLength = 0;
962e0cc8553SRatan Gupta 
963e0cc8553SRatan Gupta     auto fru = frus.begin();
964e0cc8553SRatan Gupta     uint8_t fruID{};
965e0cc8553SRatan Gupta     auto recordID = get_sdr::request::get_record_id(req);
966e0cc8553SRatan Gupta 
967e0cc8553SRatan Gupta     fruID = recordID - FRU_RECORD_ID_START;
968e0cc8553SRatan Gupta     fru = frus.find(fruID);
969e0cc8553SRatan Gupta     if (fru == frus.end())
970e0cc8553SRatan Gupta     {
971e0cc8553SRatan Gupta         return IPMI_CC_SENSOR_INVALID;
972e0cc8553SRatan Gupta     }
973e0cc8553SRatan Gupta 
974e0cc8553SRatan Gupta     /* Header */
975e0cc8553SRatan Gupta     get_sdr::header::set_record_id(recordID, &(record.header));
976e0cc8553SRatan Gupta     record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
977e0cc8553SRatan Gupta     record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
978e0cc8553SRatan Gupta     record.header.record_length = sizeof(record.key) + sizeof(record.body);
979e0cc8553SRatan Gupta 
980e0cc8553SRatan Gupta     /* Key */
981e0cc8553SRatan Gupta     record.key.fruID = fruID;
982e0cc8553SRatan Gupta     record.key.accessLun |= IPMI_LOGICAL_FRU;
983e0cc8553SRatan Gupta     record.key.deviceAddress = BMCSlaveAddress;
984e0cc8553SRatan Gupta 
985e0cc8553SRatan Gupta     /* Body */
986e0cc8553SRatan Gupta     record.body.entityID = fru->second[0].entityID;
987e0cc8553SRatan Gupta     record.body.entityInstance = fru->second[0].entityInstance;
988e0cc8553SRatan Gupta     record.body.deviceType = fruInventoryDevice;
989e0cc8553SRatan Gupta     record.body.deviceTypeModifier = IPMIFruInventory;
990e0cc8553SRatan Gupta 
991e0cc8553SRatan Gupta     /* Device ID string */
9920b02be92SPatrick Venture     auto deviceID =
9930b02be92SPatrick Venture         fru->second[0].path.substr(fru->second[0].path.find_last_of('/') + 1,
994e0cc8553SRatan Gupta                                    fru->second[0].path.length());
995e0cc8553SRatan Gupta 
996e0cc8553SRatan Gupta     if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
997e0cc8553SRatan Gupta     {
998e0cc8553SRatan Gupta         get_sdr::body::set_device_id_strlen(
9990b02be92SPatrick Venture             get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, &(record.body));
1000e0cc8553SRatan Gupta     }
1001e0cc8553SRatan Gupta     else
1002e0cc8553SRatan Gupta     {
10030b02be92SPatrick Venture         get_sdr::body::set_device_id_strlen(deviceID.length(), &(record.body));
1004e0cc8553SRatan Gupta     }
1005e0cc8553SRatan Gupta 
1006e0cc8553SRatan Gupta     strncpy(record.body.deviceID, deviceID.c_str(),
1007e0cc8553SRatan Gupta             get_sdr::body::get_device_id_strlen(&(record.body)));
1008e0cc8553SRatan Gupta 
1009e0cc8553SRatan Gupta     if (++fru == frus.end())
1010e0cc8553SRatan Gupta     {
10119c11894eSJaghathiswari Rankappagounder Natarajan         // we have reached till end of fru, so assign the next record id to
10129c11894eSJaghathiswari Rankappagounder Natarajan         // 512(Max fru ID = 511) + Entity Record ID(may start with 0).
101387fd2cd1SPatrick Venture         const auto& entityRecords =
101487fd2cd1SPatrick Venture             ipmi::sensor::EntityInfoMapContainer::getContainer()
101587fd2cd1SPatrick Venture                 ->getIpmiEntityRecords();
10169c11894eSJaghathiswari Rankappagounder Natarajan         auto next_record_id =
101783a0b848SPatrick Venture             (entityRecords.size())
101883a0b848SPatrick Venture                 ? entityRecords.begin()->first + ENTITY_RECORD_ID_START
10199c11894eSJaghathiswari Rankappagounder Natarajan                 : END_OF_RECORD;
10209c11894eSJaghathiswari Rankappagounder Natarajan         get_sdr::response::set_next_record_id(next_record_id, resp);
10219c11894eSJaghathiswari Rankappagounder Natarajan     }
10229c11894eSJaghathiswari Rankappagounder Natarajan     else
10239c11894eSJaghathiswari Rankappagounder Natarajan     {
10249c11894eSJaghathiswari Rankappagounder Natarajan         get_sdr::response::set_next_record_id(
10259c11894eSJaghathiswari Rankappagounder Natarajan             (FRU_RECORD_ID_START + fru->first), resp);
10269c11894eSJaghathiswari Rankappagounder Natarajan     }
10279c11894eSJaghathiswari Rankappagounder Natarajan 
10289c11894eSJaghathiswari Rankappagounder Natarajan     // Check for invalid offset size
10299c11894eSJaghathiswari Rankappagounder Natarajan     if (req->offset > sizeof(record))
10309c11894eSJaghathiswari Rankappagounder Natarajan     {
10319c11894eSJaghathiswari Rankappagounder Natarajan         return IPMI_CC_PARM_OUT_OF_RANGE;
10329c11894eSJaghathiswari Rankappagounder Natarajan     }
10339c11894eSJaghathiswari Rankappagounder Natarajan 
10349c11894eSJaghathiswari Rankappagounder Natarajan     dataLength = std::min(static_cast<size_t>(req->bytes_to_read),
10359c11894eSJaghathiswari Rankappagounder Natarajan                           sizeof(record) - req->offset);
10369c11894eSJaghathiswari Rankappagounder Natarajan 
10379c11894eSJaghathiswari Rankappagounder Natarajan     std::memcpy(resp->record_data,
10389c11894eSJaghathiswari Rankappagounder Natarajan                 reinterpret_cast<uint8_t*>(&record) + req->offset, dataLength);
10399c11894eSJaghathiswari Rankappagounder Natarajan 
10409c11894eSJaghathiswari Rankappagounder Natarajan     *data_len = dataLength;
10419c11894eSJaghathiswari Rankappagounder Natarajan     *data_len += 2; // additional 2 bytes for next record ID
10429c11894eSJaghathiswari Rankappagounder Natarajan 
10439c11894eSJaghathiswari Rankappagounder Natarajan     return IPMI_CC_OK;
10449c11894eSJaghathiswari Rankappagounder Natarajan }
10459c11894eSJaghathiswari Rankappagounder Natarajan 
10469c11894eSJaghathiswari Rankappagounder Natarajan ipmi_ret_t ipmi_entity_get_sdr(ipmi_request_t request, ipmi_response_t response,
10479c11894eSJaghathiswari Rankappagounder Natarajan                                ipmi_data_len_t data_len)
10489c11894eSJaghathiswari Rankappagounder Natarajan {
10499c11894eSJaghathiswari Rankappagounder Natarajan     auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
10509c11894eSJaghathiswari Rankappagounder Natarajan     auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
10519c11894eSJaghathiswari Rankappagounder Natarajan     get_sdr::SensorDataEntityRecord record{};
10529c11894eSJaghathiswari Rankappagounder Natarajan     auto dataLength = 0;
10539c11894eSJaghathiswari Rankappagounder Natarajan 
105487fd2cd1SPatrick Venture     const auto& entityRecords =
105587fd2cd1SPatrick Venture         ipmi::sensor::EntityInfoMapContainer::getContainer()
105687fd2cd1SPatrick Venture             ->getIpmiEntityRecords();
105783a0b848SPatrick Venture     auto entity = entityRecords.begin();
10589c11894eSJaghathiswari Rankappagounder Natarajan     uint8_t entityRecordID;
10599c11894eSJaghathiswari Rankappagounder Natarajan     auto recordID = get_sdr::request::get_record_id(req);
10609c11894eSJaghathiswari Rankappagounder Natarajan 
10619c11894eSJaghathiswari Rankappagounder Natarajan     entityRecordID = recordID - ENTITY_RECORD_ID_START;
106283a0b848SPatrick Venture     entity = entityRecords.find(entityRecordID);
106383a0b848SPatrick Venture     if (entity == entityRecords.end())
10649c11894eSJaghathiswari Rankappagounder Natarajan     {
10659c11894eSJaghathiswari Rankappagounder Natarajan         return IPMI_CC_SENSOR_INVALID;
10669c11894eSJaghathiswari Rankappagounder Natarajan     }
10679c11894eSJaghathiswari Rankappagounder Natarajan 
10689c11894eSJaghathiswari Rankappagounder Natarajan     /* Header */
10699c11894eSJaghathiswari Rankappagounder Natarajan     get_sdr::header::set_record_id(recordID, &(record.header));
10709c11894eSJaghathiswari Rankappagounder Natarajan     record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
10719c11894eSJaghathiswari Rankappagounder Natarajan     record.header.record_type = get_sdr::SENSOR_DATA_ENTITY_RECORD;
10729c11894eSJaghathiswari Rankappagounder Natarajan     record.header.record_length = sizeof(record.key) + sizeof(record.body);
10739c11894eSJaghathiswari Rankappagounder Natarajan 
10749c11894eSJaghathiswari Rankappagounder Natarajan     /* Key */
10759c11894eSJaghathiswari Rankappagounder Natarajan     record.key.containerEntityId = entity->second.containerEntityId;
10769c11894eSJaghathiswari Rankappagounder Natarajan     record.key.containerEntityInstance = entity->second.containerEntityInstance;
10779c11894eSJaghathiswari Rankappagounder Natarajan     get_sdr::key::set_flags(entity->second.isList, entity->second.isLinked,
10789c11894eSJaghathiswari Rankappagounder Natarajan                             &(record.key));
10799c11894eSJaghathiswari Rankappagounder Natarajan     record.key.entityId1 = entity->second.containedEntities[0].first;
10809c11894eSJaghathiswari Rankappagounder Natarajan     record.key.entityInstance1 = entity->second.containedEntities[0].second;
10819c11894eSJaghathiswari Rankappagounder Natarajan 
10829c11894eSJaghathiswari Rankappagounder Natarajan     /* Body */
10839c11894eSJaghathiswari Rankappagounder Natarajan     record.body.entityId2 = entity->second.containedEntities[1].first;
10849c11894eSJaghathiswari Rankappagounder Natarajan     record.body.entityInstance2 = entity->second.containedEntities[1].second;
10859c11894eSJaghathiswari Rankappagounder Natarajan     record.body.entityId3 = entity->second.containedEntities[2].first;
10869c11894eSJaghathiswari Rankappagounder Natarajan     record.body.entityInstance3 = entity->second.containedEntities[2].second;
10879c11894eSJaghathiswari Rankappagounder Natarajan     record.body.entityId4 = entity->second.containedEntities[3].first;
10889c11894eSJaghathiswari Rankappagounder Natarajan     record.body.entityInstance4 = entity->second.containedEntities[3].second;
10899c11894eSJaghathiswari Rankappagounder Natarajan 
109083a0b848SPatrick Venture     if (++entity == entityRecords.end())
10919c11894eSJaghathiswari Rankappagounder Natarajan     {
10920b02be92SPatrick Venture         get_sdr::response::set_next_record_id(END_OF_RECORD,
10930b02be92SPatrick Venture                                               resp); // last record
1094e0cc8553SRatan Gupta     }
1095e0cc8553SRatan Gupta     else
1096e0cc8553SRatan Gupta     {
1097e0cc8553SRatan Gupta         get_sdr::response::set_next_record_id(
10989c11894eSJaghathiswari Rankappagounder Natarajan             (ENTITY_RECORD_ID_START + entity->first), resp);
1099e0cc8553SRatan Gupta     }
1100e0cc8553SRatan Gupta 
11010fbdbce2SEmily Shaffer     // Check for invalid offset size
11020fbdbce2SEmily Shaffer     if (req->offset > sizeof(record))
1103e0cc8553SRatan Gupta     {
11040fbdbce2SEmily Shaffer         return IPMI_CC_PARM_OUT_OF_RANGE;
1105e0cc8553SRatan Gupta     }
1106e0cc8553SRatan Gupta 
11070fbdbce2SEmily Shaffer     dataLength = std::min(static_cast<size_t>(req->bytes_to_read),
11080fbdbce2SEmily Shaffer                           sizeof(record) - req->offset);
1109e0cc8553SRatan Gupta 
1110b51bf9c8SPatrick Venture     std::memcpy(resp->record_data,
11111cd85963SJason M. Bills                 reinterpret_cast<uint8_t*>(&record) + req->offset, dataLength);
1112e0cc8553SRatan Gupta 
1113e0cc8553SRatan Gupta     *data_len = dataLength;
1114e0cc8553SRatan Gupta     *data_len += 2; // additional 2 bytes for next record ID
1115e0cc8553SRatan Gupta 
1116e0cc8553SRatan Gupta     return IPMI_CC_OK;
1117e0cc8553SRatan Gupta }
1118e0cc8553SRatan Gupta 
1119bbef71c2SEmily Shaffer ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1120bbef71c2SEmily Shaffer                             ipmi_request_t request, ipmi_response_t response,
1121bbef71c2SEmily Shaffer                             ipmi_data_len_t data_len, ipmi_context_t context)
1122bbef71c2SEmily Shaffer {
1123bbef71c2SEmily Shaffer     ipmi_ret_t ret = IPMI_CC_OK;
1124bbef71c2SEmily Shaffer     get_sdr::GetSdrReq* req = (get_sdr::GetSdrReq*)request;
1125bbef71c2SEmily Shaffer     get_sdr::GetSdrResp* resp = (get_sdr::GetSdrResp*)response;
112638426dd5SPatrick Venture 
1127bbef71c2SEmily Shaffer     // Note: we use an iterator so we can provide the next ID at the end of
1128bbef71c2SEmily Shaffer     // the call.
1129db0cbe64SPatrick Venture     auto sensor = ipmi::sensor::sensors.begin();
1130e0cc8553SRatan Gupta     auto recordID = get_sdr::request::get_record_id(req);
1131bbef71c2SEmily Shaffer 
1132bbef71c2SEmily Shaffer     // At the beginning of a scan, the host side will send us id=0.
1133e0cc8553SRatan Gupta     if (recordID != 0)
1134bbef71c2SEmily Shaffer     {
11359c11894eSJaghathiswari Rankappagounder Natarajan         // recordID 0 to 255 means it is a FULL record.
11369c11894eSJaghathiswari Rankappagounder Natarajan         // recordID 256 to 511 means it is a FRU record.
11379c11894eSJaghathiswari Rankappagounder Natarajan         // recordID greater then 511 means it is a Entity Association
11389c11894eSJaghathiswari Rankappagounder Natarajan         // record. Currently we are supporting three record types: FULL
11399c11894eSJaghathiswari Rankappagounder Natarajan         // record, FRU record and Enttiy Association record.
11409c11894eSJaghathiswari Rankappagounder Natarajan         if (recordID >= ENTITY_RECORD_ID_START)
11419c11894eSJaghathiswari Rankappagounder Natarajan         {
11429c11894eSJaghathiswari Rankappagounder Natarajan             return ipmi_entity_get_sdr(request, response, data_len);
11439c11894eSJaghathiswari Rankappagounder Natarajan         }
11449c11894eSJaghathiswari Rankappagounder Natarajan         else if (recordID >= FRU_RECORD_ID_START &&
11459c11894eSJaghathiswari Rankappagounder Natarajan                  recordID < ENTITY_RECORD_ID_START)
1146e0cc8553SRatan Gupta         {
1147e0cc8553SRatan Gupta             return ipmi_fru_get_sdr(request, response, data_len);
1148e0cc8553SRatan Gupta         }
1149e0cc8553SRatan Gupta         else
1150e0cc8553SRatan Gupta         {
1151db0cbe64SPatrick Venture             sensor = ipmi::sensor::sensors.find(recordID);
1152db0cbe64SPatrick Venture             if (sensor == ipmi::sensor::sensors.end())
1153e0cc8553SRatan Gupta             {
1154bbef71c2SEmily Shaffer                 return IPMI_CC_SENSOR_INVALID;
1155bbef71c2SEmily Shaffer             }
1156bbef71c2SEmily Shaffer         }
1157e0cc8553SRatan Gupta     }
1158bbef71c2SEmily Shaffer 
1159bbef71c2SEmily Shaffer     uint8_t sensor_id = sensor->first;
1160bbef71c2SEmily Shaffer 
116114a47819SLei YU     auto it = sdrCacheMap.find(sensor_id);
116214a47819SLei YU     if (it == sdrCacheMap.end())
116314a47819SLei YU     {
1164bbef71c2SEmily Shaffer         /* Header */
116514a47819SLei YU         get_sdr::SensorDataFullRecord record = {0};
1166bbef71c2SEmily Shaffer         get_sdr::header::set_record_id(sensor_id, &(record.header));
1167bbef71c2SEmily Shaffer         record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
1168bbef71c2SEmily Shaffer         record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
1169158ac703SPatrick Venture         record.header.record_length = sizeof(record.key) + sizeof(record.body);
1170bbef71c2SEmily Shaffer 
1171bbef71c2SEmily Shaffer         /* Key */
11729642391dSTom Joseph         get_sdr::key::set_owner_id_bmc(&(record.key));
1173bbef71c2SEmily Shaffer         record.key.sensor_number = sensor_id;
1174bbef71c2SEmily Shaffer 
1175bbef71c2SEmily Shaffer         /* Body */
11769642391dSTom Joseph         record.body.entity_id = sensor->second.entityType;
1177bbef71c2SEmily Shaffer         record.body.sensor_type = sensor->second.sensorType;
1178bbef71c2SEmily Shaffer         record.body.event_reading_type = sensor->second.sensorReadingType;
11799642391dSTom Joseph         record.body.entity_instance = sensor->second.instance;
11800780df10SJaghathiswari Rankappagounder Natarajan         if (ipmi::sensor::Mutability::Write ==
11810780df10SJaghathiswari Rankappagounder Natarajan             (sensor->second.mutability & ipmi::sensor::Mutability::Write))
11820780df10SJaghathiswari Rankappagounder Natarajan         {
11830780df10SJaghathiswari Rankappagounder Natarajan             get_sdr::body::init_settable_state(true, &(record.body));
11840780df10SJaghathiswari Rankappagounder Natarajan         }
1185bbef71c2SEmily Shaffer 
1186bbef71c2SEmily Shaffer         // Set the type-specific details given the DBus interface
118738426dd5SPatrick Venture         populate_record_from_dbus(&(record.body), &(sensor->second), data_len);
118814a47819SLei YU         sdrCacheMap[sensor_id] = std::move(record);
118914a47819SLei YU     }
119014a47819SLei YU 
119114a47819SLei YU     const auto& record = sdrCacheMap[sensor_id];
1192bbef71c2SEmily Shaffer 
1193db0cbe64SPatrick Venture     if (++sensor == ipmi::sensor::sensors.end())
1194bbef71c2SEmily Shaffer     {
1195e0cc8553SRatan Gupta         // we have reached till end of sensor, so assign the next record id
1196e0cc8553SRatan Gupta         // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
119738426dd5SPatrick Venture         auto next_record_id = (frus.size())
119838426dd5SPatrick Venture                                   ? frus.begin()->first + FRU_RECORD_ID_START
11990b02be92SPatrick Venture                                   : END_OF_RECORD;
1200e0cc8553SRatan Gupta 
1201e0cc8553SRatan Gupta         get_sdr::response::set_next_record_id(next_record_id, resp);
1202bbef71c2SEmily Shaffer     }
1203bbef71c2SEmily Shaffer     else
1204bbef71c2SEmily Shaffer     {
1205bbef71c2SEmily Shaffer         get_sdr::response::set_next_record_id(sensor->first, resp);
1206bbef71c2SEmily Shaffer     }
1207bbef71c2SEmily Shaffer 
12086c9ee519SEmily Shaffer     if (req->offset > sizeof(record))
12096c9ee519SEmily Shaffer     {
12106c9ee519SEmily Shaffer         return IPMI_CC_PARM_OUT_OF_RANGE;
12116c9ee519SEmily Shaffer     }
12126c9ee519SEmily Shaffer 
12136c9ee519SEmily Shaffer     // data_len will ultimately be the size of the record, plus
12146c9ee519SEmily Shaffer     // the size of the next record ID:
12156c9ee519SEmily Shaffer     *data_len = std::min(static_cast<size_t>(req->bytes_to_read),
12166c9ee519SEmily Shaffer                          sizeof(record) - req->offset);
12176c9ee519SEmily Shaffer 
12186c9ee519SEmily Shaffer     std::memcpy(resp->record_data,
121914a47819SLei YU                 reinterpret_cast<const uint8_t*>(&record) + req->offset,
122014a47819SLei YU                 *data_len);
12216c9ee519SEmily Shaffer 
12226c9ee519SEmily Shaffer     // data_len should include the LSB and MSB:
12231cd85963SJason M. Bills     *data_len +=
12241cd85963SJason M. Bills         sizeof(resp->next_record_id_lsb) + sizeof(resp->next_record_id_msb);
1225bbef71c2SEmily Shaffer 
1226bbef71c2SEmily Shaffer     return ret;
1227bbef71c2SEmily Shaffer }
1228bbef71c2SEmily Shaffer 
12293342a8e0SJia, Chunhui static bool isFromSystemChannel()
12303342a8e0SJia, Chunhui {
12313342a8e0SJia, Chunhui     // TODO we could not figure out where the request is from based on IPMI
12323342a8e0SJia, Chunhui     // command handler parameters. because of it, we can not differentiate
12333342a8e0SJia, Chunhui     // request from SMS/SMM or IPMB channel
12343342a8e0SJia, Chunhui     return true;
12353342a8e0SJia, Chunhui }
12363342a8e0SJia, Chunhui 
12373342a8e0SJia, Chunhui ipmi_ret_t ipmicmdPlatformEvent(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
12383342a8e0SJia, Chunhui                                 ipmi_request_t request,
12393342a8e0SJia, Chunhui                                 ipmi_response_t response,
12403342a8e0SJia, Chunhui                                 ipmi_data_len_t dataLen, ipmi_context_t context)
12413342a8e0SJia, Chunhui {
12423342a8e0SJia, Chunhui     uint16_t generatorID;
12433342a8e0SJia, Chunhui     size_t count;
12443342a8e0SJia, Chunhui     bool assert = true;
12453342a8e0SJia, Chunhui     std::string sensorPath;
12463342a8e0SJia, Chunhui     size_t paraLen = *dataLen;
12473342a8e0SJia, Chunhui     PlatformEventRequest* req;
12483342a8e0SJia, Chunhui     *dataLen = 0;
12493342a8e0SJia, Chunhui 
12503342a8e0SJia, Chunhui     if ((paraLen < selSystemEventSizeWith1Bytes) ||
12513342a8e0SJia, Chunhui         (paraLen > selSystemEventSizeWith3Bytes))
12523342a8e0SJia, Chunhui     {
12533342a8e0SJia, Chunhui         return IPMI_CC_REQ_DATA_LEN_INVALID;
12543342a8e0SJia, Chunhui     }
12553342a8e0SJia, Chunhui 
12563342a8e0SJia, Chunhui     if (isFromSystemChannel())
12573342a8e0SJia, Chunhui     { // first byte for SYSTEM Interface is Generator ID
12583342a8e0SJia, Chunhui         // +1 to get common struct
12593342a8e0SJia, Chunhui         req = reinterpret_cast<PlatformEventRequest*>((uint8_t*)request + 1);
12603342a8e0SJia, Chunhui         // Capture the generator ID
12613342a8e0SJia, Chunhui         generatorID = *reinterpret_cast<uint8_t*>(request);
12623342a8e0SJia, Chunhui         // Platform Event usually comes from other firmware, like BIOS.
12633342a8e0SJia, Chunhui         // Unlike BMC sensor, it does not have BMC DBUS sensor path.
12643342a8e0SJia, Chunhui         sensorPath = "System";
12653342a8e0SJia, Chunhui     }
12663342a8e0SJia, Chunhui     else
12673342a8e0SJia, Chunhui     {
12683342a8e0SJia, Chunhui         req = reinterpret_cast<PlatformEventRequest*>(request);
12693342a8e0SJia, Chunhui         // TODO GenratorID for IPMB is combination of RqSA and RqLUN
12703342a8e0SJia, Chunhui         generatorID = 0xff;
12713342a8e0SJia, Chunhui         sensorPath = "IPMB";
12723342a8e0SJia, Chunhui     }
12733342a8e0SJia, Chunhui     // Content of event data field depends on sensor class.
12743342a8e0SJia, Chunhui     // When data0 bit[5:4] is non-zero, valid data counts is 3.
12753342a8e0SJia, Chunhui     // When data0 bit[7:6] is non-zero, valid data counts is 2.
12763342a8e0SJia, Chunhui     if (((req->data[0] & byte3EnableMask) != 0 &&
12773342a8e0SJia, Chunhui          paraLen < selSystemEventSizeWith3Bytes) ||
12783342a8e0SJia, Chunhui         ((req->data[0] & byte2EnableMask) != 0 &&
12793342a8e0SJia, Chunhui          paraLen < selSystemEventSizeWith2Bytes))
12803342a8e0SJia, Chunhui     {
12813342a8e0SJia, Chunhui         return IPMI_CC_REQ_DATA_LEN_INVALID;
12823342a8e0SJia, Chunhui     }
12833342a8e0SJia, Chunhui 
12843342a8e0SJia, Chunhui     // Count bytes of Event Data
12853342a8e0SJia, Chunhui     if ((req->data[0] & byte3EnableMask) != 0)
12863342a8e0SJia, Chunhui     {
12873342a8e0SJia, Chunhui         count = 3;
12883342a8e0SJia, Chunhui     }
12893342a8e0SJia, Chunhui     else if ((req->data[0] & byte2EnableMask) != 0)
12903342a8e0SJia, Chunhui     {
12913342a8e0SJia, Chunhui         count = 2;
12923342a8e0SJia, Chunhui     }
12933342a8e0SJia, Chunhui     else
12943342a8e0SJia, Chunhui     {
12953342a8e0SJia, Chunhui         count = 1;
12963342a8e0SJia, Chunhui     }
12973342a8e0SJia, Chunhui     assert = req->eventDirectionType & directionMask ? false : true;
12983342a8e0SJia, Chunhui     std::vector<uint8_t> eventData(req->data, req->data + count);
12993342a8e0SJia, Chunhui 
13003342a8e0SJia, Chunhui     sdbusplus::bus::bus dbus(bus);
13013342a8e0SJia, Chunhui     std::string service =
13023342a8e0SJia, Chunhui         ipmi::getService(dbus, ipmiSELAddInterface, ipmiSELPath);
13033342a8e0SJia, Chunhui     sdbusplus::message::message writeSEL = dbus.new_method_call(
13043342a8e0SJia, Chunhui         service.c_str(), ipmiSELPath, ipmiSELAddInterface, "IpmiSelAdd");
13053342a8e0SJia, Chunhui     writeSEL.append(ipmiSELAddMessage, sensorPath, eventData, assert,
13063342a8e0SJia, Chunhui                     generatorID);
13073342a8e0SJia, Chunhui     try
13083342a8e0SJia, Chunhui     {
13093342a8e0SJia, Chunhui         dbus.call(writeSEL);
13103342a8e0SJia, Chunhui     }
1311a2ad2da8SPatrick Williams     catch (const sdbusplus::exception_t& e)
13123342a8e0SJia, Chunhui     {
13133342a8e0SJia, Chunhui         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
13143342a8e0SJia, Chunhui         return IPMI_CC_UNSPECIFIED_ERROR;
13153342a8e0SJia, Chunhui     }
13163342a8e0SJia, Chunhui     return IPMI_CC_OK;
13173342a8e0SJia, Chunhui }
13183342a8e0SJia, Chunhui 
131998a23840SMatthew Barth void register_netfn_sen_functions()
132098a23840SMatthew Barth {
1321d351a729SWilly Tu     // Handlers with dbus-sdr handler implementation.
1322d351a729SWilly Tu     // Do not register the hander if it dynamic sensors stack is used.
1323a8be7dc8SDeepak Kumar Sahu 
1324d351a729SWilly Tu #ifndef FEATURE_DYNAMIC_SENSORS
1325be5c6b2aSLei YU 
1326*962e68baSLei YU #ifdef FEATURE_SENSORS_CACHE
1327be5c6b2aSLei YU     // Initialize the sensor matches
1328be5c6b2aSLei YU     initSensorMatches();
1329*962e68baSLei YU #endif
1330be5c6b2aSLei YU 
13310573237fSTom     // <Set Sensor Reading and Event Status>
13329da3a750SDeepak Kumar Sahu     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
13339da3a750SDeepak Kumar Sahu                           ipmi::sensor_event::cmdSetSensorReadingAndEvtSts,
13349da3a750SDeepak Kumar Sahu                           ipmi::Privilege::Operator, ipmiSetSensorReading);
13350573237fSTom     // <Get Sensor Reading>
13364c3feba5Sjayaprakash Mutyala     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
13374c3feba5Sjayaprakash Mutyala                           ipmi::sensor_event::cmdGetSensorReading,
13384c3feba5Sjayaprakash Mutyala                           ipmi::Privilege::User, ipmiSensorGetSensorReading);
1339a344afc0SEmily Shaffer 
13405ca50959STom Joseph     // <Reserve Device SDR Repository>
1341d957823eSjayaprakash Mutyala     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1342d957823eSjayaprakash Mutyala                           ipmi::sensor_event::cmdReserveDeviceSdrRepository,
1343d957823eSjayaprakash Mutyala                           ipmi::Privilege::User, ipmiSensorReserveSdr);
134498a23840SMatthew Barth 
13455ca50959STom Joseph     // <Get Device SDR Info>
1346d957823eSjayaprakash Mutyala     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1347d957823eSjayaprakash Mutyala                           ipmi::sensor_event::cmdGetDeviceSdrInfo,
1348d957823eSjayaprakash Mutyala                           ipmi::Privilege::User, ipmiSensorGetDeviceSdrInfo);
1349bbef71c2SEmily Shaffer 
13505c0beec1SDhruvaraj Subhashchandran     // <Get Sensor Thresholds>
1351996c9798Sjayaprakash Mutyala     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1352996c9798Sjayaprakash Mutyala                           ipmi::sensor_event::cmdGetSensorThreshold,
1353996c9798Sjayaprakash Mutyala                           ipmi::Privilege::User, ipmiSensorGetSensorThresholds);
13545c0beec1SDhruvaraj Subhashchandran 
1355f93da667SLotus Xu     // <Set Sensor Thresholds>
1356f93da667SLotus Xu     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1357f93da667SLotus Xu                           ipmi::sensor_event::cmdSetSensorThreshold,
1358f93da667SLotus Xu                           ipmi::Privilege::User, ipmiSenSetSensorThresholds);
1359d351a729SWilly Tu #endif
1360d351a729SWilly Tu 
1361d351a729SWilly Tu     // Common Handers used by both implementation.
1362d351a729SWilly Tu 
1363d351a729SWilly Tu     // <Platform Event Message>
1364d351a729SWilly Tu     ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_PLATFORM_EVENT, nullptr,
1365d351a729SWilly Tu                            ipmicmdPlatformEvent, PRIVILEGE_OPERATOR);
1366d351a729SWilly Tu 
1367d351a729SWilly Tu     // <Get Sensor Type>
1368d351a729SWilly Tu     ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1369d351a729SWilly Tu                           ipmi::sensor_event::cmdGetSensorType,
1370d351a729SWilly Tu                           ipmi::Privilege::User, ipmiGetSensorType);
1371d351a729SWilly Tu 
1372d351a729SWilly Tu     // <Get Device SDR>
1373d351a729SWilly Tu     ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR, nullptr,
1374d351a729SWilly Tu                            ipmi_sen_get_sdr, PRIVILEGE_USER);
137598a23840SMatthew Barth     return;
137698a23840SMatthew Barth }
1377