blob: 7c6a4dcdab32216613cb81e94075c575f8de307a [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include "sensorhandler.h"
2
3#include "fruread.hpp"
4#include "ipmid.hpp"
5#include "types.hpp"
6#include "utils.hpp"
7
Tomd700e762016-09-20 18:24:13 +05308#include <mapper.h>
Emily Shaffer1fabf222017-04-05 08:53:21 -07009#include <math.h>
Chris Austenac4604a2015-10-13 12:43:27 -050010#include <stdio.h>
11#include <string.h>
Chris Austen10ccc0f2015-12-10 18:27:04 -060012#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070013
14#include <bitset>
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050015#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070016#include <phosphor-logging/log.hpp>
17#include <set>
18#include <xyz/openbmc_project/Common/error.hpp>
19#include <xyz/openbmc_project/Sensor/Value/server.hpp>
20
21#include "host-ipmid/ipmid-api.h"
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050022
Ratan Guptae0cc8552018-01-22 14:23:04 +053023static constexpr uint8_t fruInventoryDevice = 0x10;
24static constexpr uint8_t IPMIFruInventory = 0x02;
25static constexpr uint8_t BMCSlaveAddress = 0x20;
26
Patrick Venture0b02be92018-08-31 11:55:55 -070027extern int updateSensorRecordFromSSRAESC(const void*);
28extern sd_bus* bus;
Tom Josephbe703f72017-03-09 12:34:35 +053029extern const ipmi::sensor::IdInfoMap sensors;
Ratan Guptae0cc8552018-01-22 14:23:04 +053030extern const FruMap frus;
31
Tom Josephbe703f72017-03-09 12:34:35 +053032using namespace phosphor::logging;
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050033using InternalFailure =
34 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austenac4604a2015-10-13 12:43:27 -050035
Patrick Venture0b02be92018-08-31 11:55:55 -070036void register_netfn_sen_functions() __attribute__((constructor));
Chris Austenac4604a2015-10-13 12:43:27 -050037
Patrick Venture0b02be92018-08-31 11:55:55 -070038struct sensorTypemap_t
39{
Chris Austen0012e9b2015-10-22 01:37:46 -050040 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060041 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050042 char dbusname[32];
Patrick Venture0b02be92018-08-31 11:55:55 -070043};
Chris Austen0012e9b2015-10-22 01:37:46 -050044
Chris Austen0012e9b2015-10-22 01:37:46 -050045sensorTypemap_t g_SensorTypeMap[] = {
46
Chris Austend7cf0e42015-11-07 14:27:12 -060047 {0x01, 0x6F, "Temp"},
48 {0x0C, 0x6F, "DIMM"},
49 {0x0C, 0x6F, "MEMORY_BUFFER"},
50 {0x07, 0x6F, "PROC"},
51 {0x07, 0x6F, "CORE"},
52 {0x07, 0x6F, "CPU"},
53 {0x0F, 0x6F, "BootProgress"},
Patrick Venture0b02be92018-08-31 11:55:55 -070054 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor
55 // type code os 0x09
Chris Austend7cf0e42015-11-07 14:27:12 -060056 {0xC3, 0x6F, "BootCount"},
57 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060058 {0x12, 0x6F, "SYSTEM_EVENT"},
59 {0xC7, 0x03, "SYSTEM"},
60 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060061 {0xC2, 0x6F, "PowerCap"},
Tom Joseph558184e2017-09-01 13:45:05 +053062 {0x0b, 0xCA, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050063 {0xDA, 0x03, "TurboAllowed"},
Tom Joseph558184e2017-09-01 13:45:05 +053064 {0xD8, 0xC8, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060065 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050066};
67
Patrick Venture0b02be92018-08-31 11:55:55 -070068struct sensor_data_t
69{
Chris Austenac4604a2015-10-13 12:43:27 -050070 uint8_t sennum;
Patrick Venture0b02be92018-08-31 11:55:55 -070071} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050072
Patrick Venture0b02be92018-08-31 11:55:55 -070073struct sensorreadingresp_t
74{
Chris Austen10ccc0f2015-12-10 18:27:04 -060075 uint8_t value;
76 uint8_t operation;
77 uint8_t indication[2];
Patrick Venture0b02be92018-08-31 11:55:55 -070078} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050079
Patrick Venture0b02be92018-08-31 11:55:55 -070080int get_bus_for_path(const char* path, char** busname)
81{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070082 return mapper_get_service(bus, path, busname);
83}
Tomd700e762016-09-20 18:24:13 +053084
Emily Shaffer2ae09b92017-04-05 15:09:41 -070085// Use a lookup table to find the interface name of a specific sensor
86// This will be used until an alternative is found. this is the first
87// step for mapping IPMI
Patrick Venture0b02be92018-08-31 11:55:55 -070088int find_openbmc_path(uint8_t num, dbus_interface_t* interface)
89{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070090 int rc;
91
Emily Shaffer2ae09b92017-04-05 15:09:41 -070092 const auto& sensor_it = sensors.find(num);
93 if (sensor_it == sensors.end())
94 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -050095 // The sensor map does not contain the sensor requested
96 return -EINVAL;
Emily Shaffer2ae09b92017-04-05 15:09:41 -070097 }
98
99 const auto& info = sensor_it->second;
100
Patrick Williams8451edf2017-06-13 09:01:06 -0500101 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700102 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700103 if (rc < 0)
104 {
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700105 fprintf(stderr, "Failed to get %s busname: %s\n",
Patrick Venture0b02be92018-08-31 11:55:55 -0700106 info.sensorPath.c_str(), busname);
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700107 goto final;
108 }
109
110 interface->sensortype = info.sensorType;
111 strcpy(interface->bus, busname);
112 strcpy(interface->path, info.sensorPath.c_str());
113 // Take the interface name from the beginning of the DbusInterfaceMap. This
114 // works for the Value interface but may not suffice for more complex
115 // sensors.
116 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Patrick Venture0b02be92018-08-31 11:55:55 -0700117 strcpy(interface->interface,
118 info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700119 interface->sensornumber = num;
120
121final:
122 free(busname);
123 return rc;
124}
125
Tomd700e762016-09-20 18:24:13 +0530126/////////////////////////////////////////////////////////////////////
127//
128// Routines used by ipmi commands wanting to interact on the dbus
129//
130/////////////////////////////////////////////////////////////////////
Patrick Venture0b02be92018-08-31 11:55:55 -0700131int set_sensor_dbus_state_s(uint8_t number, const char* method,
132 const char* value)
133{
Tomd700e762016-09-20 18:24:13 +0530134
135 dbus_interface_t a;
136 int r;
137 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700138 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530139
Patrick Venture0b02be92018-08-31 11:55:55 -0700140 fprintf(ipmidbus,
141 "Attempting to set a dbus Variant Sensor 0x%02x via %s with a "
142 "value of %s\n",
143 number, method, value);
Tomd700e762016-09-20 18:24:13 +0530144
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700145 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530146
Patrick Venture0b02be92018-08-31 11:55:55 -0700147 if (r < 0)
148 {
Tomd700e762016-09-20 18:24:13 +0530149 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
150 return 0;
151 }
152
Patrick Venture0b02be92018-08-31 11:55:55 -0700153 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
154 method);
155 if (r < 0)
156 {
Tomd700e762016-09-20 18:24:13 +0530157 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
158 goto final;
159 }
160
161 r = sd_bus_message_append(m, "v", "s", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700162 if (r < 0)
163 {
Tomd700e762016-09-20 18:24:13 +0530164 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
165 goto final;
166 }
167
Tomd700e762016-09-20 18:24:13 +0530168 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700169 if (r < 0)
170 {
Tomd700e762016-09-20 18:24:13 +0530171 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
172 }
173
174final:
175 sd_bus_error_free(&error);
176 m = sd_bus_message_unref(m);
177
178 return 0;
179}
Patrick Venture0b02be92018-08-31 11:55:55 -0700180int set_sensor_dbus_state_y(uint8_t number, const char* method,
181 const uint8_t value)
182{
Tomd700e762016-09-20 18:24:13 +0530183
184 dbus_interface_t a;
185 int r;
186 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700187 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530188
Patrick Venture0b02be92018-08-31 11:55:55 -0700189 fprintf(ipmidbus,
190 "Attempting to set a dbus Variant Sensor 0x%02x via %s with a "
191 "value of 0x%02x\n",
192 number, method, value);
Tomd700e762016-09-20 18:24:13 +0530193
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700194 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530195
Patrick Venture0b02be92018-08-31 11:55:55 -0700196 if (r < 0)
197 {
Tomd700e762016-09-20 18:24:13 +0530198 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
199 return 0;
200 }
201
Patrick Venture0b02be92018-08-31 11:55:55 -0700202 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
203 method);
204 if (r < 0)
205 {
Tomd700e762016-09-20 18:24:13 +0530206 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
207 goto final;
208 }
209
210 r = sd_bus_message_append(m, "v", "i", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700211 if (r < 0)
212 {
Tomd700e762016-09-20 18:24:13 +0530213 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
214 goto final;
215 }
216
Tomd700e762016-09-20 18:24:13 +0530217 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700218 if (r < 0)
219 {
Tomd700e762016-09-20 18:24:13 +0530220 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
221 }
222
223final:
224 sd_bus_error_free(&error);
225 m = sd_bus_message_unref(m);
226
227 return 0;
228}
229
Patrick Venture0b02be92018-08-31 11:55:55 -0700230uint8_t dbus_to_sensor_type(char* p)
231{
Chris Austenac4604a2015-10-13 12:43:27 -0500232
Patrick Venture0b02be92018-08-31 11:55:55 -0700233 sensorTypemap_t* s = g_SensorTypeMap;
234 char r = 0;
235 while (s->number != 0xFF)
236 {
237 if (!strcmp(s->dbusname, p))
238 {
Tom Joseph558184e2017-09-01 13:45:05 +0530239 r = s->typecode;
Patrick Venture0b02be92018-08-31 11:55:55 -0700240 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500241 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500242 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500243 }
244
Chris Austen0012e9b2015-10-22 01:37:46 -0500245 if (s->number == 0xFF)
246 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500247
Chris Austen0012e9b2015-10-22 01:37:46 -0500248 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500249}
250
Patrick Venture0b02be92018-08-31 11:55:55 -0700251uint8_t get_type_from_interface(dbus_interface_t dbus_if)
252{
Chris Austen0012e9b2015-10-22 01:37:46 -0500253
Patrick Venture0b02be92018-08-31 11:55:55 -0700254 char* p;
Brad Bishop56003452016-10-05 21:49:19 -0400255 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500256
Chris Austen0012e9b2015-10-22 01:37:46 -0500257 // This is where sensors that do not exist in dbus but do
258 // exist in the host code stop. This should indicate it
259 // is not a supported sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700260 if (dbus_if.interface[0] == 0)
261 {
262 return 0;
263 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500264
Emily Shaffer71174412017-04-05 15:10:40 -0700265 // Fetch type from interface itself.
266 if (dbus_if.sensortype != 0)
267 {
268 type = dbus_if.sensortype;
Patrick Venture0b02be92018-08-31 11:55:55 -0700269 }
270 else
271 {
Chris Austen0012e9b2015-10-22 01:37:46 -0500272 // Non InventoryItems
Patrick Venture0b02be92018-08-31 11:55:55 -0700273 p = strrchr(dbus_if.path, '/');
274 type = dbus_to_sensor_type(p + 1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500275 }
276
Brad Bishop56003452016-10-05 21:49:19 -0400277 return type;
Patrick Venture0b02be92018-08-31 11:55:55 -0700278}
Chris Austen0012e9b2015-10-22 01:37:46 -0500279
Emily Shaffer391f3302017-04-03 10:27:08 -0700280// Replaces find_sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700281uint8_t find_type_for_sensor_number(uint8_t num)
282{
Emily Shaffer391f3302017-04-03 10:27:08 -0700283 int r;
284 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700285 r = find_openbmc_path(num, &dbus_if);
Patrick Venture0b02be92018-08-31 11:55:55 -0700286 if (r < 0)
287 {
Emily Shaffer391f3302017-04-03 10:27:08 -0700288 fprintf(stderr, "Could not find sensor %d\n", num);
Lei YU91875f72018-04-03 15:14:49 +0800289 return 0;
Emily Shaffer391f3302017-04-03 10:27:08 -0700290 }
291 return get_type_from_interface(dbus_if);
292}
293
Chris Austen0012e9b2015-10-22 01:37:46 -0500294ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700295 ipmi_request_t request,
296 ipmi_response_t response,
297 ipmi_data_len_t data_len,
298 ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500299{
Patrick Venture0b02be92018-08-31 11:55:55 -0700300 sensor_data_t* reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500301 ipmi_ret_t rc = IPMI_CC_OK;
302
Patrick Venture0b02be92018-08-31 11:55:55 -0700303 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n", reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500304
305 // TODO Not sure what the System-event-sensor is suppose to return
306 // need to ask Hostboot team
Patrick Venture0b02be92018-08-31 11:55:55 -0700307 unsigned char buf[] = {0x00, 0x6F};
Chris Austenac4604a2015-10-13 12:43:27 -0500308
Emily Shaffer391f3302017-04-03 10:27:08 -0700309 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500310
311 // HACK UNTIL Dbus gets updated or we find a better way
Patrick Venture0b02be92018-08-31 11:55:55 -0700312 if (buf[0] == 0)
313 {
Chris Austen800ba712015-12-03 15:31:00 -0600314 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500315 }
316
Chris Austenac4604a2015-10-13 12:43:27 -0500317 *data_len = sizeof(buf);
318 memcpy(response, &buf, *data_len);
319
Chris Austenac4604a2015-10-13 12:43:27 -0500320 return rc;
321}
322
Patrick Venture0b02be92018-08-31 11:55:55 -0700323const std::set<std::string> analogSensorInterfaces = {
Emily Shaffercc941e12017-06-14 13:06:26 -0700324 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700325 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700326};
327
328bool isAnalogSensor(const std::string& interface)
329{
330 return (analogSensorInterfaces.count(interface));
331}
332
Patrick Venture0b02be92018-08-31 11:55:55 -0700333ipmi_ret_t setSensorReading(void* request)
Tom Josephbe703f72017-03-09 12:34:35 +0530334{
Tom Joseph816e92b2017-09-06 19:23:00 +0530335 ipmi::sensor::SetSensorReadingReq cmdData =
Patrick Venture0b02be92018-08-31 11:55:55 -0700336 *(static_cast<ipmi::sensor::SetSensorReadingReq*>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530337
338 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500339 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530340 if (iter == sensors.end())
341 {
342 return IPMI_CC_SENSOR_INVALID;
343 }
344
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500345 try
346 {
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500347 if (ipmi::sensor::Mutability::Write !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700348 (iter->second.mutability & ipmi::sensor::Mutability::Write))
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500349 {
350 log<level::ERR>("Sensor Set operation is not allowed",
351 entry("SENSOR_NUM=%d", cmdData.number));
352 return IPMI_CC_ILLEGAL_COMMAND;
353 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500354 return iter->second.updateFunc(cmdData, iter->second);
355 }
356 catch (InternalFailure& e)
357 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700358 log<level::ERR>("Set sensor failed",
359 entry("SENSOR_NUM=%d", cmdData.number));
360 commit<InternalFailure>();
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500361 }
Tom Joseph82024322017-09-28 20:07:29 +0530362 catch (const std::runtime_error& e)
363 {
364 log<level::ERR>(e.what());
365 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500366
367 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530368}
Chris Austenac4604a2015-10-13 12:43:27 -0500369
Chris Austen0012e9b2015-10-22 01:37:46 -0500370ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700371 ipmi_request_t request, ipmi_response_t response,
372 ipmi_data_len_t data_len, ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500373{
Patrick Venture0b02be92018-08-31 11:55:55 -0700374 sensor_data_t* reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500375
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530376 log<level::DEBUG>("IPMI SET_SENSOR",
377 entry("SENSOR_NUM=0x%02x", reqptr->sennum));
Chris Austenac4604a2015-10-13 12:43:27 -0500378
Tom Josephbe703f72017-03-09 12:34:35 +0530379 /*
380 * This would support the Set Sensor Reading command for the presence
381 * and functional state of Processor, Core & DIMM. For the remaining
382 * sensors the existing support is invoked.
383 */
384 auto ipmiRC = setSensorReading(request);
385
Patrick Venture0b02be92018-08-31 11:55:55 -0700386 if (ipmiRC == IPMI_CC_SENSOR_INVALID)
Tom Josephbe703f72017-03-09 12:34:35 +0530387 {
388 updateSensorRecordFromSSRAESC(reqptr);
389 ipmiRC = IPMI_CC_OK;
390 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500391
Patrick Venture0b02be92018-08-31 11:55:55 -0700392 *data_len = 0;
Tom Josephbe703f72017-03-09 12:34:35 +0530393 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500394}
395
Tom Joseph3ee668f2018-03-02 19:49:17 +0530396ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700397 ipmi_request_t request,
398 ipmi_response_t response,
399 ipmi_data_len_t data_len,
400 ipmi_context_t context)
Tom Joseph3ee668f2018-03-02 19:49:17 +0530401{
Patrick Venture0b02be92018-08-31 11:55:55 -0700402 sensor_data_t* reqptr = (sensor_data_t*)request;
403 sensorreadingresp_t* resp = (sensorreadingresp_t*)response;
404 ipmi::sensor::GetSensorResponse getResponse{};
Tom Joseph3ee668f2018-03-02 19:49:17 +0530405 static constexpr auto scanningEnabledBit = 6;
406
407 const auto iter = sensors.find(reqptr->sennum);
408 if (iter == sensors.end())
409 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -0500410 return IPMI_CC_SENSOR_INVALID;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530411 }
412 if (ipmi::sensor::Mutability::Read !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700413 (iter->second.mutability & ipmi::sensor::Mutability::Read))
Tom Joseph3ee668f2018-03-02 19:49:17 +0530414 {
Jayanth Othayoth6ccf8812018-04-05 22:58:48 -0500415 return IPMI_CC_ILLEGAL_COMMAND;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530416 }
417
418 try
419 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700420 getResponse = iter->second.getFunc(iter->second);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530421 *data_len = getResponse.size();
422 memcpy(resp, getResponse.data(), *data_len);
423 resp->operation = 1 << scanningEnabledBit;
424 return IPMI_CC_OK;
425 }
426 catch (const std::exception& e)
427 {
428 *data_len = getResponse.size();
429 memcpy(resp, getResponse.data(), *data_len);
430 return IPMI_CC_OK;
431 }
432}
433
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530434void getSensorThresholds(uint8_t sensorNum,
435 get_sdr::GetSensorThresholdsResponse* response)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600436{
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530437 constexpr auto warningThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600438 "xyz.openbmc_project.Sensor.Threshold.Warning";
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530439 constexpr auto criticalThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600440 "xyz.openbmc_project.Sensor.Threshold.Critical";
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600441
442 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
443
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530444 const auto iter = sensors.find(sensorNum);
445 const auto info = iter->second;
446
447 auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
448
Patrick Venture0b02be92018-08-31 11:55:55 -0700449 auto warnThresholds = ipmi::getAllDbusProperties(
450 bus, service, info.sensorPath, warningThreshIntf);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530451
James Feist1e121122018-07-31 11:44:09 -0700452 double warnLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(),
453 warnThresholds["WarningLow"]);
454 double warnHigh = mapbox::util::apply_visitor(
455 ipmi::VariantToDoubleVisitor(), warnThresholds["WarningHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530456
457 if (warnLow != 0)
458 {
459 warnLow *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700460 response->lowerNonCritical = static_cast<uint8_t>(
461 (warnLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530462 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700463 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530464 }
465
466 if (warnHigh != 0)
467 {
468 warnHigh *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700469 response->upperNonCritical = static_cast<uint8_t>(
470 (warnHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530471 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700472 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530473 }
474
Patrick Venture0b02be92018-08-31 11:55:55 -0700475 auto critThresholds = ipmi::getAllDbusProperties(
476 bus, service, info.sensorPath, criticalThreshIntf);
James Feist1e121122018-07-31 11:44:09 -0700477 double critLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(),
478 critThresholds["CriticalLow"]);
479 double critHigh = mapbox::util::apply_visitor(
480 ipmi::VariantToDoubleVisitor(), critThresholds["CriticalHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530481
482 if (critLow != 0)
483 {
484 critLow *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700485 response->lowerCritical = static_cast<uint8_t>(
486 (critLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530487 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700488 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530489 }
490
491 if (critHigh != 0)
492 {
493 critHigh *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700494 response->upperCritical = static_cast<uint8_t>(
495 (critHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530496 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700497 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530498 }
499}
500
501ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700502 ipmi_request_t request,
503 ipmi_response_t response,
504 ipmi_data_len_t data_len,
505 ipmi_context_t context)
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530506{
507 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
508
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600509 if (*data_len != sizeof(uint8_t))
510 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530511 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600512 return IPMI_CC_REQ_DATA_LEN_INVALID;
513 }
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600514
Patrick Venture0b02be92018-08-31 11:55:55 -0700515 auto sensorNum = *(reinterpret_cast<const uint8_t*>(request));
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530516 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600517
518 const auto iter = sensors.find(sensorNum);
519 if (iter == sensors.end())
520 {
521 return IPMI_CC_SENSOR_INVALID;
522 }
523
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530524 const auto info = iter->second;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600525
Patrick Venture0b02be92018-08-31 11:55:55 -0700526 // Proceed only if the sensor value interface is implemented.
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530527 if (info.propertyInterfaces.find(valueInterface) ==
528 info.propertyInterfaces.end())
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600529 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700530 // return with valid mask as 0
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600531 return IPMI_CC_OK;
532 }
533
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530534 auto responseData =
535 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600536
537 try
538 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530539 getSensorThresholds(sensorNum, responseData);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600540 }
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530541 catch (std::exception& e)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600542 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700543 // Mask if the property is not present
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600544 responseData->validMask = 0;
545 }
546
547 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
548 return IPMI_CC_OK;
549}
550
Chris Austen0012e9b2015-10-22 01:37:46 -0500551ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
552 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500553 ipmi_data_len_t data_len, ipmi_context_t context)
554{
Nan Li70aa8d92016-08-29 00:11:10 +0800555 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500556
Patrick Venture0b02be92018-08-31 11:55:55 -0700557 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n", netfn, cmd);
Chris Austenac4604a2015-10-13 12:43:27 -0500558 *data_len = 0;
559
560 return rc;
561}
562
Emily Shafferd06e0e72017-04-05 09:08:57 -0700563ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
564 ipmi_request_t request,
565 ipmi_response_t response,
566 ipmi_data_len_t data_len,
567 ipmi_context_t context)
568{
569 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
570 if (request == nullptr ||
571 get_sdr_info::request::get_count(request) == false)
572 {
573 // Get Sensor Count
Ratan Guptae0cc8552018-01-22 14:23:04 +0530574 resp->count = sensors.size() + frus.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700575 }
576 else
577 {
578 resp->count = 1;
579 }
580
581 // Multiple LUNs not supported.
582 namespace response = get_sdr_info::response;
583 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
584 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
585 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
586 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
587 response::set_static_population(&(resp->luns_and_dynamic_population));
588
589 *data_len = SDR_INFO_RESP_SIZE;
590
591 return IPMI_CC_OK;
592}
593
Emily Shaffera344afc2017-04-13 15:09:39 -0700594ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
595 ipmi_request_t request,
596 ipmi_response_t response,
597 ipmi_data_len_t data_len,
598 ipmi_context_t context)
599{
600 // A constant reservation ID is okay until we implement add/remove SDR.
601 const uint16_t reservation_id = 1;
602 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700603 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700604
605 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
606 return IPMI_CC_OK;
607}
Chris Austenac4604a2015-10-13 12:43:27 -0500608
Patrick Venture0b02be92018-08-31 11:55:55 -0700609void setUnitFieldsForObject(const ipmi::sensor::Info* info,
610 get_sdr::SensorDataFullRecordBody* body)
Emily Shaffercc941e12017-06-14 13:06:26 -0700611{
Tom Josephdc212b22018-02-16 09:59:57 +0530612 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
613 try
Emily Shaffercc941e12017-06-14 13:06:26 -0700614 {
Tom Josephdc212b22018-02-16 09:59:57 +0530615 auto unit = server::Value::convertUnitFromString(info->unit);
616 // Unit strings defined in
617 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
618 switch (unit)
Emily Shaffercc941e12017-06-14 13:06:26 -0700619 {
Tom Josephdc212b22018-02-16 09:59:57 +0530620 case server::Value::Unit::DegreesC:
621 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
622 break;
623 case server::Value::Unit::RPMS:
Patrick Venture0b02be92018-08-31 11:55:55 -0700624 body->sensor_units_2_base =
625 get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
Tom Josephdc212b22018-02-16 09:59:57 +0530626 get_sdr::body::set_rate_unit(0b100, body); // per minute
627 break;
628 case server::Value::Unit::Volts:
629 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
630 break;
631 case server::Value::Unit::Meters:
632 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
633 break;
634 case server::Value::Unit::Amperes:
635 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
636 break;
637 case server::Value::Unit::Joules:
638 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
639 break;
640 case server::Value::Unit::Watts:
641 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
642 break;
643 default:
644 // Cannot be hit.
645 fprintf(stderr, "Unknown value unit type: = %s\n",
646 info->unit.c_str());
Emily Shaffercc941e12017-06-14 13:06:26 -0700647 }
648 }
Tom Josephdc212b22018-02-16 09:59:57 +0530649 catch (sdbusplus::exception::InvalidEnumString e)
Emily Shaffercc941e12017-06-14 13:06:26 -0700650 {
Tom Josephdc212b22018-02-16 09:59:57 +0530651 log<level::WARNING>("Warning: no unit provided for sensor!");
Emily Shaffercc941e12017-06-14 13:06:26 -0700652 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700653}
654
Patrick Venture0b02be92018-08-31 11:55:55 -0700655ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody* body,
656 const ipmi::sensor::Info* info,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700657 ipmi_data_len_t data_len)
658{
659 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700660 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700661 {
Emily Shafferbbef71c2017-05-08 16:36:17 -0700662
663 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
664
665 /* Unit info */
Tom Josephdc212b22018-02-16 09:59:57 +0530666 setUnitFieldsForObject(info, body);
Emily Shaffer10f49592017-05-10 12:01:10 -0700667
668 get_sdr::body::set_b(info->coefficientB, body);
669 get_sdr::body::set_m(info->coefficientM, body);
670 get_sdr::body::set_b_exp(info->exponentB, body);
Tom Josephdc212b22018-02-16 09:59:57 +0530671 get_sdr::body::set_r_exp(info->exponentR, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700672
Emily Shafferbbef71c2017-05-08 16:36:17 -0700673 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700674 }
675
Tom Joseph96423912018-01-25 00:14:34 +0530676 /* ID string */
677 auto id_string = info->sensorNameFunc(*info);
678
679 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
680 {
681 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
682 }
683 else
684 {
685 get_sdr::body::set_id_strlen(id_string.length(), body);
686 }
687 strncpy(body->id_string, id_string.c_str(),
688 get_sdr::body::get_id_strlen(body));
689
Emily Shafferbbef71c2017-05-08 16:36:17 -0700690 return IPMI_CC_OK;
691};
692
Ratan Guptae0cc8552018-01-22 14:23:04 +0530693ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
694 ipmi_data_len_t data_len)
695{
696 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
697 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700698 get_sdr::SensorDataFruRecord record{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530699 auto dataLength = 0;
700
701 auto fru = frus.begin();
Patrick Venture0b02be92018-08-31 11:55:55 -0700702 uint8_t fruID{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530703 auto recordID = get_sdr::request::get_record_id(req);
704
705 fruID = recordID - FRU_RECORD_ID_START;
706 fru = frus.find(fruID);
707 if (fru == frus.end())
708 {
709 return IPMI_CC_SENSOR_INVALID;
710 }
711
712 /* Header */
713 get_sdr::header::set_record_id(recordID, &(record.header));
714 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
715 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
716 record.header.record_length = sizeof(record.key) + sizeof(record.body);
717
718 /* Key */
719 record.key.fruID = fruID;
720 record.key.accessLun |= IPMI_LOGICAL_FRU;
721 record.key.deviceAddress = BMCSlaveAddress;
722
723 /* Body */
724 record.body.entityID = fru->second[0].entityID;
725 record.body.entityInstance = fru->second[0].entityInstance;
726 record.body.deviceType = fruInventoryDevice;
727 record.body.deviceTypeModifier = IPMIFruInventory;
728
729 /* Device ID string */
Patrick Venture0b02be92018-08-31 11:55:55 -0700730 auto deviceID =
731 fru->second[0].path.substr(fru->second[0].path.find_last_of('/') + 1,
732 fru->second[0].path.length());
Ratan Guptae0cc8552018-01-22 14:23:04 +0530733
734 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
735 {
736 get_sdr::body::set_device_id_strlen(
Patrick Venture0b02be92018-08-31 11:55:55 -0700737 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530738 }
739 else
740 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700741 get_sdr::body::set_device_id_strlen(deviceID.length(), &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530742 }
743
744 strncpy(record.body.deviceID, deviceID.c_str(),
745 get_sdr::body::get_device_id_strlen(&(record.body)));
746
747 if (++fru == frus.end())
748 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700749 get_sdr::response::set_next_record_id(END_OF_RECORD,
750 resp); // last record
Ratan Guptae0cc8552018-01-22 14:23:04 +0530751 }
752 else
753 {
754 get_sdr::response::set_next_record_id(
Patrick Venture0b02be92018-08-31 11:55:55 -0700755 (FRU_RECORD_ID_START + fru->first), resp);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530756 }
757
758 if (req->bytes_to_read > (sizeof(*resp) - req->offset))
759 {
760 dataLength = (sizeof(*resp) - req->offset);
761 }
762 else
763 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700764 dataLength = req->bytes_to_read;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530765 }
766
767 if (dataLength <= 0)
768 {
769 return IPMI_CC_REQ_DATA_LEN_INVALID;
770 }
771
Patrick Venture0b02be92018-08-31 11:55:55 -0700772 memcpy(resp->record_data, reinterpret_cast<uint8_t*>(&record) + req->offset,
773 (dataLength));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530774
775 *data_len = dataLength;
776 *data_len += 2; // additional 2 bytes for next record ID
777
778 return IPMI_CC_OK;
779}
780
Emily Shafferbbef71c2017-05-08 16:36:17 -0700781ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
782 ipmi_request_t request, ipmi_response_t response,
783 ipmi_data_len_t data_len, ipmi_context_t context)
784{
785 ipmi_ret_t ret = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700786 get_sdr::GetSdrReq* req = (get_sdr::GetSdrReq*)request;
787 get_sdr::GetSdrResp* resp = (get_sdr::GetSdrResp*)response;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700788 get_sdr::SensorDataFullRecord record = {0};
789 if (req != NULL)
790 {
791 // Note: we use an iterator so we can provide the next ID at the end of
792 // the call.
793 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +0530794 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700795
796 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +0530797 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700798 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530799 // recordID greater then 255,it means it is a FRU record.
800 // Currently we are supporting two record types either FULL record
801 // or FRU record.
802 if (recordID >= FRU_RECORD_ID_START)
803 {
804 return ipmi_fru_get_sdr(request, response, data_len);
805 }
806 else
807 {
808 sensor = sensors.find(recordID);
809 if (sensor == sensors.end())
810 {
811 return IPMI_CC_SENSOR_INVALID;
812 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700813 }
814 }
815
816 uint8_t sensor_id = sensor->first;
817
818 /* Header */
819 get_sdr::header::set_record_id(sensor_id, &(record.header));
820 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
821 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
822 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
823
824 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +0530825 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -0700826 record.key.sensor_number = sensor_id;
827
828 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +0530829 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700830 record.body.sensor_type = sensor->second.sensorType;
831 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +0530832 record.body.entity_instance = sensor->second.instance;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700833
834 // Set the type-specific details given the DBus interface
835 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
836 data_len);
837
838 if (++sensor == sensors.end())
839 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530840 // we have reached till end of sensor, so assign the next record id
841 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
Patrick Venture0b02be92018-08-31 11:55:55 -0700842 auto next_record_id =
843 (frus.size()) ? frus.begin()->first + FRU_RECORD_ID_START
844 : END_OF_RECORD;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530845
846 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700847 }
848 else
849 {
850 get_sdr::response::set_next_record_id(sensor->first, resp);
851 }
852
853 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
854 memcpy(resp->record_data, (char*)&record + req->offset,
855 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
856 }
857
858 return ret;
859}
860
Chris Austenac4604a2015-10-13 12:43:27 -0500861void register_netfn_sen_functions()
862{
Tom05732372016-09-06 17:21:23 +0530863 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -0700864 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, nullptr,
865 ipmi_sen_wildcard, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500866
Tom05732372016-09-06 17:21:23 +0530867 // <Get Sensor Type>
Patrick Venture0b02be92018-08-31 11:55:55 -0700868 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, nullptr,
869 ipmi_sen_get_sensor_type, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500870
Tom05732372016-09-06 17:21:23 +0530871 // <Set Sensor Reading and Event Status>
Patrick Venture0b02be92018-08-31 11:55:55 -0700872 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, nullptr,
873 ipmi_sen_set_sensor, PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -0500874
Tom05732372016-09-06 17:21:23 +0530875 // <Get Sensor Reading>
Patrick Venture0b02be92018-08-31 11:55:55 -0700876 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, nullptr,
877 ipmi_sen_get_sensor_reading, PRIVILEGE_USER);
Emily Shaffera344afc2017-04-13 15:09:39 -0700878
Tom Joseph5ca50952018-02-22 00:33:38 +0530879 // <Reserve Device SDR Repository>
880 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_DEVICE_SDR_REPO,
Patrick Venture0b02be92018-08-31 11:55:55 -0700881 nullptr, ipmi_sen_reserve_sdr, PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600882
Tom Joseph5ca50952018-02-22 00:33:38 +0530883 // <Get Device SDR Info>
Patrick Venture0b02be92018-08-31 11:55:55 -0700884 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR_INFO, nullptr,
885 ipmi_sen_get_sdr_info, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700886
Tom Joseph5ca50952018-02-22 00:33:38 +0530887 // <Get Device SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -0700888 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR, nullptr,
889 ipmi_sen_get_sdr, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700890
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600891 // <Get Sensor Thresholds>
892 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
893 nullptr, ipmi_sen_get_sensor_thresholds,
894 PRIVILEGE_USER);
895
Chris Austenac4604a2015-10-13 12:43:27 -0500896 return;
897}