blob: 80f7c4bf1fddbd4e0af66e2c545cec44f9adf404 [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "sensorhandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07002
3#include "fruread.hpp"
4#include "ipmid.hpp"
5#include "types.hpp"
6#include "utils.hpp"
7
Patrick Venture46470a32018-09-07 19:26:25 -07008#include <host-ipmid/ipmid-api.h>
Tomd700e762016-09-20 18:24:13 +05309#include <mapper.h>
Emily Shaffer1fabf222017-04-05 08:53:21 -070010#include <math.h>
Chris Austenac4604a2015-10-13 12:43:27 -050011#include <stdio.h>
12#include <string.h>
Chris Austen10ccc0f2015-12-10 18:27:04 -060013#include <systemd/sd-bus.h>
Patrick Venture0b02be92018-08-31 11:55:55 -070014
15#include <bitset>
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050016#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070017#include <phosphor-logging/log.hpp>
18#include <set>
19#include <xyz/openbmc_project/Common/error.hpp>
20#include <xyz/openbmc_project/Sensor/Value/server.hpp>
21
Ratan Guptae0cc8552018-01-22 14:23:04 +053022static constexpr uint8_t fruInventoryDevice = 0x10;
23static constexpr uint8_t IPMIFruInventory = 0x02;
24static constexpr uint8_t BMCSlaveAddress = 0x20;
25
Patrick Venture0b02be92018-08-31 11:55:55 -070026extern int updateSensorRecordFromSSRAESC(const void*);
27extern sd_bus* bus;
Tom Josephbe703f72017-03-09 12:34:35 +053028extern const ipmi::sensor::IdInfoMap sensors;
Ratan Guptae0cc8552018-01-22 14:23:04 +053029extern const FruMap frus;
30
Tom Josephbe703f72017-03-09 12:34:35 +053031using namespace phosphor::logging;
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050032using InternalFailure =
33 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austenac4604a2015-10-13 12:43:27 -050034
Patrick Venture0b02be92018-08-31 11:55:55 -070035void register_netfn_sen_functions() __attribute__((constructor));
Chris Austenac4604a2015-10-13 12:43:27 -050036
Patrick Venture0b02be92018-08-31 11:55:55 -070037struct sensorTypemap_t
38{
Chris Austen0012e9b2015-10-22 01:37:46 -050039 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060040 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050041 char dbusname[32];
Patrick Venture0b02be92018-08-31 11:55:55 -070042};
Chris Austen0012e9b2015-10-22 01:37:46 -050043
Chris Austen0012e9b2015-10-22 01:37:46 -050044sensorTypemap_t g_SensorTypeMap[] = {
45
Chris Austend7cf0e42015-11-07 14:27:12 -060046 {0x01, 0x6F, "Temp"},
47 {0x0C, 0x6F, "DIMM"},
48 {0x0C, 0x6F, "MEMORY_BUFFER"},
49 {0x07, 0x6F, "PROC"},
50 {0x07, 0x6F, "CORE"},
51 {0x07, 0x6F, "CPU"},
52 {0x0F, 0x6F, "BootProgress"},
Patrick Venture0b02be92018-08-31 11:55:55 -070053 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor
54 // type code os 0x09
Chris Austend7cf0e42015-11-07 14:27:12 -060055 {0xC3, 0x6F, "BootCount"},
56 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060057 {0x12, 0x6F, "SYSTEM_EVENT"},
58 {0xC7, 0x03, "SYSTEM"},
59 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060060 {0xC2, 0x6F, "PowerCap"},
Tom Joseph558184e2017-09-01 13:45:05 +053061 {0x0b, 0xCA, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050062 {0xDA, 0x03, "TurboAllowed"},
Tom Joseph558184e2017-09-01 13:45:05 +053063 {0xD8, 0xC8, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060064 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050065};
66
Patrick Venture0b02be92018-08-31 11:55:55 -070067struct sensor_data_t
68{
Chris Austenac4604a2015-10-13 12:43:27 -050069 uint8_t sennum;
Patrick Venture0b02be92018-08-31 11:55:55 -070070} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050071
Patrick Venture0b02be92018-08-31 11:55:55 -070072struct sensorreadingresp_t
73{
Chris Austen10ccc0f2015-12-10 18:27:04 -060074 uint8_t value;
75 uint8_t operation;
76 uint8_t indication[2];
Patrick Venture0b02be92018-08-31 11:55:55 -070077} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050078
Patrick Venture0b02be92018-08-31 11:55:55 -070079int get_bus_for_path(const char* path, char** busname)
80{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070081 return mapper_get_service(bus, path, busname);
82}
Tomd700e762016-09-20 18:24:13 +053083
Emily Shaffer2ae09b92017-04-05 15:09:41 -070084// Use a lookup table to find the interface name of a specific sensor
85// This will be used until an alternative is found. this is the first
86// step for mapping IPMI
Patrick Venture0b02be92018-08-31 11:55:55 -070087int find_openbmc_path(uint8_t num, dbus_interface_t* interface)
88{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070089 int rc;
90
Emily Shaffer2ae09b92017-04-05 15:09:41 -070091 const auto& sensor_it = sensors.find(num);
92 if (sensor_it == sensors.end())
93 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -050094 // The sensor map does not contain the sensor requested
95 return -EINVAL;
Emily Shaffer2ae09b92017-04-05 15:09:41 -070096 }
97
98 const auto& info = sensor_it->second;
99
Patrick Williams8451edf2017-06-13 09:01:06 -0500100 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700101 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700102 if (rc < 0)
103 {
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700104 fprintf(stderr, "Failed to get %s busname: %s\n",
Patrick Venture0b02be92018-08-31 11:55:55 -0700105 info.sensorPath.c_str(), busname);
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700106 goto final;
107 }
108
109 interface->sensortype = info.sensorType;
110 strcpy(interface->bus, busname);
111 strcpy(interface->path, info.sensorPath.c_str());
112 // Take the interface name from the beginning of the DbusInterfaceMap. This
113 // works for the Value interface but may not suffice for more complex
114 // sensors.
115 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Patrick Venture0b02be92018-08-31 11:55:55 -0700116 strcpy(interface->interface,
117 info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700118 interface->sensornumber = num;
119
120final:
121 free(busname);
122 return rc;
123}
124
Tomd700e762016-09-20 18:24:13 +0530125/////////////////////////////////////////////////////////////////////
126//
127// Routines used by ipmi commands wanting to interact on the dbus
128//
129/////////////////////////////////////////////////////////////////////
Patrick Venture0b02be92018-08-31 11:55:55 -0700130int set_sensor_dbus_state_s(uint8_t number, const char* method,
131 const char* value)
132{
Tomd700e762016-09-20 18:24:13 +0530133
134 dbus_interface_t a;
135 int r;
136 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700137 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530138
Patrick Venture0b02be92018-08-31 11:55:55 -0700139 fprintf(ipmidbus,
140 "Attempting to set a dbus Variant Sensor 0x%02x via %s with a "
141 "value of %s\n",
142 number, method, value);
Tomd700e762016-09-20 18:24:13 +0530143
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700144 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530145
Patrick Venture0b02be92018-08-31 11:55:55 -0700146 if (r < 0)
147 {
Tomd700e762016-09-20 18:24:13 +0530148 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
149 return 0;
150 }
151
Patrick Venture0b02be92018-08-31 11:55:55 -0700152 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
153 method);
154 if (r < 0)
155 {
Tomd700e762016-09-20 18:24:13 +0530156 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
157 goto final;
158 }
159
160 r = sd_bus_message_append(m, "v", "s", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700161 if (r < 0)
162 {
Tomd700e762016-09-20 18:24:13 +0530163 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
164 goto final;
165 }
166
Tomd700e762016-09-20 18:24:13 +0530167 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700168 if (r < 0)
169 {
Tomd700e762016-09-20 18:24:13 +0530170 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
171 }
172
173final:
174 sd_bus_error_free(&error);
175 m = sd_bus_message_unref(m);
176
177 return 0;
178}
Patrick Venture0b02be92018-08-31 11:55:55 -0700179int set_sensor_dbus_state_y(uint8_t number, const char* method,
180 const uint8_t value)
181{
Tomd700e762016-09-20 18:24:13 +0530182
183 dbus_interface_t a;
184 int r;
185 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700186 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530187
Patrick Venture0b02be92018-08-31 11:55:55 -0700188 fprintf(ipmidbus,
189 "Attempting to set a dbus Variant Sensor 0x%02x via %s with a "
190 "value of 0x%02x\n",
191 number, method, value);
Tomd700e762016-09-20 18:24:13 +0530192
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700193 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530194
Patrick Venture0b02be92018-08-31 11:55:55 -0700195 if (r < 0)
196 {
Tomd700e762016-09-20 18:24:13 +0530197 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
198 return 0;
199 }
200
Patrick Venture0b02be92018-08-31 11:55:55 -0700201 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
202 method);
203 if (r < 0)
204 {
Tomd700e762016-09-20 18:24:13 +0530205 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
206 goto final;
207 }
208
209 r = sd_bus_message_append(m, "v", "i", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700210 if (r < 0)
211 {
Tomd700e762016-09-20 18:24:13 +0530212 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
213 goto final;
214 }
215
Tomd700e762016-09-20 18:24:13 +0530216 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700217 if (r < 0)
218 {
Tomd700e762016-09-20 18:24:13 +0530219 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
220 }
221
222final:
223 sd_bus_error_free(&error);
224 m = sd_bus_message_unref(m);
225
226 return 0;
227}
228
Patrick Venture0b02be92018-08-31 11:55:55 -0700229uint8_t dbus_to_sensor_type(char* p)
230{
Chris Austenac4604a2015-10-13 12:43:27 -0500231
Patrick Venture0b02be92018-08-31 11:55:55 -0700232 sensorTypemap_t* s = g_SensorTypeMap;
233 char r = 0;
234 while (s->number != 0xFF)
235 {
236 if (!strcmp(s->dbusname, p))
237 {
Tom Joseph558184e2017-09-01 13:45:05 +0530238 r = s->typecode;
Patrick Venture0b02be92018-08-31 11:55:55 -0700239 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500240 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500241 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500242 }
243
Chris Austen0012e9b2015-10-22 01:37:46 -0500244 if (s->number == 0xFF)
245 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500246
Chris Austen0012e9b2015-10-22 01:37:46 -0500247 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500248}
249
Patrick Venture0b02be92018-08-31 11:55:55 -0700250uint8_t get_type_from_interface(dbus_interface_t dbus_if)
251{
Chris Austen0012e9b2015-10-22 01:37:46 -0500252
Patrick Venture0b02be92018-08-31 11:55:55 -0700253 char* p;
Brad Bishop56003452016-10-05 21:49:19 -0400254 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500255
Chris Austen0012e9b2015-10-22 01:37:46 -0500256 // This is where sensors that do not exist in dbus but do
257 // exist in the host code stop. This should indicate it
258 // is not a supported sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700259 if (dbus_if.interface[0] == 0)
260 {
261 return 0;
262 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500263
Emily Shaffer71174412017-04-05 15:10:40 -0700264 // Fetch type from interface itself.
265 if (dbus_if.sensortype != 0)
266 {
267 type = dbus_if.sensortype;
Patrick Venture0b02be92018-08-31 11:55:55 -0700268 }
269 else
270 {
Chris Austen0012e9b2015-10-22 01:37:46 -0500271 // Non InventoryItems
Patrick Venture0b02be92018-08-31 11:55:55 -0700272 p = strrchr(dbus_if.path, '/');
273 type = dbus_to_sensor_type(p + 1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500274 }
275
Brad Bishop56003452016-10-05 21:49:19 -0400276 return type;
Patrick Venture0b02be92018-08-31 11:55:55 -0700277}
Chris Austen0012e9b2015-10-22 01:37:46 -0500278
Emily Shaffer391f3302017-04-03 10:27:08 -0700279// Replaces find_sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700280uint8_t find_type_for_sensor_number(uint8_t num)
281{
Emily Shaffer391f3302017-04-03 10:27:08 -0700282 int r;
283 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700284 r = find_openbmc_path(num, &dbus_if);
Patrick Venture0b02be92018-08-31 11:55:55 -0700285 if (r < 0)
286 {
Emily Shaffer391f3302017-04-03 10:27:08 -0700287 fprintf(stderr, "Could not find sensor %d\n", num);
Lei YU91875f72018-04-03 15:14:49 +0800288 return 0;
Emily Shaffer391f3302017-04-03 10:27:08 -0700289 }
290 return get_type_from_interface(dbus_if);
291}
292
Chris Austen0012e9b2015-10-22 01:37:46 -0500293ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700294 ipmi_request_t request,
295 ipmi_response_t response,
296 ipmi_data_len_t data_len,
297 ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500298{
Patrick Venture0b02be92018-08-31 11:55:55 -0700299 sensor_data_t* reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500300 ipmi_ret_t rc = IPMI_CC_OK;
301
Patrick Venture0b02be92018-08-31 11:55:55 -0700302 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n", reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500303
304 // TODO Not sure what the System-event-sensor is suppose to return
305 // need to ask Hostboot team
Patrick Venture0b02be92018-08-31 11:55:55 -0700306 unsigned char buf[] = {0x00, 0x6F};
Chris Austenac4604a2015-10-13 12:43:27 -0500307
Emily Shaffer391f3302017-04-03 10:27:08 -0700308 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500309
310 // HACK UNTIL Dbus gets updated or we find a better way
Patrick Venture0b02be92018-08-31 11:55:55 -0700311 if (buf[0] == 0)
312 {
Chris Austen800ba712015-12-03 15:31:00 -0600313 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500314 }
315
Chris Austenac4604a2015-10-13 12:43:27 -0500316 *data_len = sizeof(buf);
317 memcpy(response, &buf, *data_len);
318
Chris Austenac4604a2015-10-13 12:43:27 -0500319 return rc;
320}
321
Patrick Venture0b02be92018-08-31 11:55:55 -0700322const std::set<std::string> analogSensorInterfaces = {
Emily Shaffercc941e12017-06-14 13:06:26 -0700323 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700324 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700325};
326
327bool isAnalogSensor(const std::string& interface)
328{
329 return (analogSensorInterfaces.count(interface));
330}
331
Patrick Venture0b02be92018-08-31 11:55:55 -0700332ipmi_ret_t setSensorReading(void* request)
Tom Josephbe703f72017-03-09 12:34:35 +0530333{
Tom Joseph816e92b2017-09-06 19:23:00 +0530334 ipmi::sensor::SetSensorReadingReq cmdData =
Patrick Venture0b02be92018-08-31 11:55:55 -0700335 *(static_cast<ipmi::sensor::SetSensorReadingReq*>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530336
337 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500338 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530339 if (iter == sensors.end())
340 {
341 return IPMI_CC_SENSOR_INVALID;
342 }
343
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500344 try
345 {
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500346 if (ipmi::sensor::Mutability::Write !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700347 (iter->second.mutability & ipmi::sensor::Mutability::Write))
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500348 {
349 log<level::ERR>("Sensor Set operation is not allowed",
350 entry("SENSOR_NUM=%d", cmdData.number));
351 return IPMI_CC_ILLEGAL_COMMAND;
352 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500353 return iter->second.updateFunc(cmdData, iter->second);
354 }
355 catch (InternalFailure& e)
356 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700357 log<level::ERR>("Set sensor failed",
358 entry("SENSOR_NUM=%d", cmdData.number));
359 commit<InternalFailure>();
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500360 }
Tom Joseph82024322017-09-28 20:07:29 +0530361 catch (const std::runtime_error& e)
362 {
363 log<level::ERR>(e.what());
364 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500365
366 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530367}
Chris Austenac4604a2015-10-13 12:43:27 -0500368
Chris Austen0012e9b2015-10-22 01:37:46 -0500369ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700370 ipmi_request_t request, ipmi_response_t response,
371 ipmi_data_len_t data_len, ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500372{
Patrick Venture0b02be92018-08-31 11:55:55 -0700373 sensor_data_t* reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500374
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530375 log<level::DEBUG>("IPMI SET_SENSOR",
376 entry("SENSOR_NUM=0x%02x", reqptr->sennum));
Chris Austenac4604a2015-10-13 12:43:27 -0500377
Tom Josephbe703f72017-03-09 12:34:35 +0530378 /*
379 * This would support the Set Sensor Reading command for the presence
380 * and functional state of Processor, Core & DIMM. For the remaining
381 * sensors the existing support is invoked.
382 */
383 auto ipmiRC = setSensorReading(request);
384
Patrick Venture0b02be92018-08-31 11:55:55 -0700385 if (ipmiRC == IPMI_CC_SENSOR_INVALID)
Tom Josephbe703f72017-03-09 12:34:35 +0530386 {
387 updateSensorRecordFromSSRAESC(reqptr);
388 ipmiRC = IPMI_CC_OK;
389 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500390
Patrick Venture0b02be92018-08-31 11:55:55 -0700391 *data_len = 0;
Tom Josephbe703f72017-03-09 12:34:35 +0530392 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500393}
394
Tom Joseph3ee668f2018-03-02 19:49:17 +0530395ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700396 ipmi_request_t request,
397 ipmi_response_t response,
398 ipmi_data_len_t data_len,
399 ipmi_context_t context)
Tom Joseph3ee668f2018-03-02 19:49:17 +0530400{
Patrick Venture0b02be92018-08-31 11:55:55 -0700401 sensor_data_t* reqptr = (sensor_data_t*)request;
402 sensorreadingresp_t* resp = (sensorreadingresp_t*)response;
403 ipmi::sensor::GetSensorResponse getResponse{};
Tom Joseph3ee668f2018-03-02 19:49:17 +0530404 static constexpr auto scanningEnabledBit = 6;
405
406 const auto iter = sensors.find(reqptr->sennum);
407 if (iter == sensors.end())
408 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -0500409 return IPMI_CC_SENSOR_INVALID;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530410 }
411 if (ipmi::sensor::Mutability::Read !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700412 (iter->second.mutability & ipmi::sensor::Mutability::Read))
Tom Joseph3ee668f2018-03-02 19:49:17 +0530413 {
Jayanth Othayoth6ccf8812018-04-05 22:58:48 -0500414 return IPMI_CC_ILLEGAL_COMMAND;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530415 }
416
417 try
418 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700419 getResponse = iter->second.getFunc(iter->second);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530420 *data_len = getResponse.size();
421 memcpy(resp, getResponse.data(), *data_len);
422 resp->operation = 1 << scanningEnabledBit;
423 return IPMI_CC_OK;
424 }
425 catch (const std::exception& e)
426 {
427 *data_len = getResponse.size();
428 memcpy(resp, getResponse.data(), *data_len);
429 return IPMI_CC_OK;
430 }
431}
432
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530433void getSensorThresholds(uint8_t sensorNum,
434 get_sdr::GetSensorThresholdsResponse* response)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600435{
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530436 constexpr auto warningThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600437 "xyz.openbmc_project.Sensor.Threshold.Warning";
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530438 constexpr auto criticalThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600439 "xyz.openbmc_project.Sensor.Threshold.Critical";
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600440
441 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
442
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530443 const auto iter = sensors.find(sensorNum);
444 const auto info = iter->second;
445
446 auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
447
Patrick Venture0b02be92018-08-31 11:55:55 -0700448 auto warnThresholds = ipmi::getAllDbusProperties(
449 bus, service, info.sensorPath, warningThreshIntf);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530450
James Feist1e121122018-07-31 11:44:09 -0700451 double warnLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(),
452 warnThresholds["WarningLow"]);
453 double warnHigh = mapbox::util::apply_visitor(
454 ipmi::VariantToDoubleVisitor(), warnThresholds["WarningHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530455
456 if (warnLow != 0)
457 {
458 warnLow *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700459 response->lowerNonCritical = static_cast<uint8_t>(
460 (warnLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530461 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700462 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530463 }
464
465 if (warnHigh != 0)
466 {
467 warnHigh *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700468 response->upperNonCritical = static_cast<uint8_t>(
469 (warnHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530470 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700471 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530472 }
473
Patrick Venture0b02be92018-08-31 11:55:55 -0700474 auto critThresholds = ipmi::getAllDbusProperties(
475 bus, service, info.sensorPath, criticalThreshIntf);
James Feist1e121122018-07-31 11:44:09 -0700476 double critLow = mapbox::util::apply_visitor(ipmi::VariantToDoubleVisitor(),
477 critThresholds["CriticalLow"]);
478 double critHigh = mapbox::util::apply_visitor(
479 ipmi::VariantToDoubleVisitor(), critThresholds["CriticalHigh"]);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530480
481 if (critLow != 0)
482 {
483 critLow *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700484 response->lowerCritical = static_cast<uint8_t>(
485 (critLow - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530486 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700487 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530488 }
489
490 if (critHigh != 0)
491 {
492 critHigh *= pow(10, info.scale - info.exponentR);
Patrick Venture0b02be92018-08-31 11:55:55 -0700493 response->upperCritical = static_cast<uint8_t>(
494 (critHigh - info.scaledOffset) / info.coefficientM);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530495 response->validMask |= static_cast<uint8_t>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700496 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530497 }
498}
499
500ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700501 ipmi_request_t request,
502 ipmi_response_t response,
503 ipmi_data_len_t data_len,
504 ipmi_context_t context)
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530505{
506 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
507
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600508 if (*data_len != sizeof(uint8_t))
509 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530510 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600511 return IPMI_CC_REQ_DATA_LEN_INVALID;
512 }
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600513
Patrick Venture0b02be92018-08-31 11:55:55 -0700514 auto sensorNum = *(reinterpret_cast<const uint8_t*>(request));
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530515 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600516
517 const auto iter = sensors.find(sensorNum);
518 if (iter == sensors.end())
519 {
520 return IPMI_CC_SENSOR_INVALID;
521 }
522
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530523 const auto info = iter->second;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600524
Patrick Venture0b02be92018-08-31 11:55:55 -0700525 // Proceed only if the sensor value interface is implemented.
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530526 if (info.propertyInterfaces.find(valueInterface) ==
527 info.propertyInterfaces.end())
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600528 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700529 // return with valid mask as 0
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600530 return IPMI_CC_OK;
531 }
532
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530533 auto responseData =
534 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600535
536 try
537 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530538 getSensorThresholds(sensorNum, responseData);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600539 }
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530540 catch (std::exception& e)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600541 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700542 // Mask if the property is not present
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600543 responseData->validMask = 0;
544 }
545
546 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
547 return IPMI_CC_OK;
548}
549
Chris Austen0012e9b2015-10-22 01:37:46 -0500550ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
551 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500552 ipmi_data_len_t data_len, ipmi_context_t context)
553{
Nan Li70aa8d92016-08-29 00:11:10 +0800554 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500555
Patrick Venture0b02be92018-08-31 11:55:55 -0700556 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n", netfn, cmd);
Chris Austenac4604a2015-10-13 12:43:27 -0500557 *data_len = 0;
558
559 return rc;
560}
561
Emily Shafferd06e0e72017-04-05 09:08:57 -0700562ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
563 ipmi_request_t request,
564 ipmi_response_t response,
565 ipmi_data_len_t data_len,
566 ipmi_context_t context)
567{
568 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
569 if (request == nullptr ||
570 get_sdr_info::request::get_count(request) == false)
571 {
572 // Get Sensor Count
Ratan Guptae0cc8552018-01-22 14:23:04 +0530573 resp->count = sensors.size() + frus.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700574 }
575 else
576 {
577 resp->count = 1;
578 }
579
580 // Multiple LUNs not supported.
581 namespace response = get_sdr_info::response;
582 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
583 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
584 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
585 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
586 response::set_static_population(&(resp->luns_and_dynamic_population));
587
588 *data_len = SDR_INFO_RESP_SIZE;
589
590 return IPMI_CC_OK;
591}
592
Emily Shaffera344afc2017-04-13 15:09:39 -0700593ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
594 ipmi_request_t request,
595 ipmi_response_t response,
596 ipmi_data_len_t data_len,
597 ipmi_context_t context)
598{
599 // A constant reservation ID is okay until we implement add/remove SDR.
600 const uint16_t reservation_id = 1;
601 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700602 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700603
604 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
605 return IPMI_CC_OK;
606}
Chris Austenac4604a2015-10-13 12:43:27 -0500607
Patrick Venture0b02be92018-08-31 11:55:55 -0700608void setUnitFieldsForObject(const ipmi::sensor::Info* info,
609 get_sdr::SensorDataFullRecordBody* body)
Emily Shaffercc941e12017-06-14 13:06:26 -0700610{
Tom Josephdc212b22018-02-16 09:59:57 +0530611 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
612 try
Emily Shaffercc941e12017-06-14 13:06:26 -0700613 {
Tom Josephdc212b22018-02-16 09:59:57 +0530614 auto unit = server::Value::convertUnitFromString(info->unit);
615 // Unit strings defined in
616 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
617 switch (unit)
Emily Shaffercc941e12017-06-14 13:06:26 -0700618 {
Tom Josephdc212b22018-02-16 09:59:57 +0530619 case server::Value::Unit::DegreesC:
620 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
621 break;
622 case server::Value::Unit::RPMS:
Patrick Venture0b02be92018-08-31 11:55:55 -0700623 body->sensor_units_2_base =
624 get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
Tom Josephdc212b22018-02-16 09:59:57 +0530625 get_sdr::body::set_rate_unit(0b100, body); // per minute
626 break;
627 case server::Value::Unit::Volts:
628 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
629 break;
630 case server::Value::Unit::Meters:
631 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
632 break;
633 case server::Value::Unit::Amperes:
634 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
635 break;
636 case server::Value::Unit::Joules:
637 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
638 break;
639 case server::Value::Unit::Watts:
640 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
641 break;
642 default:
643 // Cannot be hit.
644 fprintf(stderr, "Unknown value unit type: = %s\n",
645 info->unit.c_str());
Emily Shaffercc941e12017-06-14 13:06:26 -0700646 }
647 }
Tom Josephdc212b22018-02-16 09:59:57 +0530648 catch (sdbusplus::exception::InvalidEnumString e)
Emily Shaffercc941e12017-06-14 13:06:26 -0700649 {
Tom Josephdc212b22018-02-16 09:59:57 +0530650 log<level::WARNING>("Warning: no unit provided for sensor!");
Emily Shaffercc941e12017-06-14 13:06:26 -0700651 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700652}
653
Patrick Venture0b02be92018-08-31 11:55:55 -0700654ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody* body,
655 const ipmi::sensor::Info* info,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700656 ipmi_data_len_t data_len)
657{
658 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700659 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700660 {
Emily Shafferbbef71c2017-05-08 16:36:17 -0700661
662 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
663
664 /* Unit info */
Tom Josephdc212b22018-02-16 09:59:57 +0530665 setUnitFieldsForObject(info, body);
Emily Shaffer10f49592017-05-10 12:01:10 -0700666
667 get_sdr::body::set_b(info->coefficientB, body);
668 get_sdr::body::set_m(info->coefficientM, body);
669 get_sdr::body::set_b_exp(info->exponentB, body);
Tom Josephdc212b22018-02-16 09:59:57 +0530670 get_sdr::body::set_r_exp(info->exponentR, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700671
Emily Shafferbbef71c2017-05-08 16:36:17 -0700672 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700673 }
674
Tom Joseph96423912018-01-25 00:14:34 +0530675 /* ID string */
676 auto id_string = info->sensorNameFunc(*info);
677
678 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
679 {
680 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
681 }
682 else
683 {
684 get_sdr::body::set_id_strlen(id_string.length(), body);
685 }
686 strncpy(body->id_string, id_string.c_str(),
687 get_sdr::body::get_id_strlen(body));
688
Emily Shafferbbef71c2017-05-08 16:36:17 -0700689 return IPMI_CC_OK;
690};
691
Ratan Guptae0cc8552018-01-22 14:23:04 +0530692ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
693 ipmi_data_len_t data_len)
694{
695 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
696 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700697 get_sdr::SensorDataFruRecord record{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530698 auto dataLength = 0;
699
700 auto fru = frus.begin();
Patrick Venture0b02be92018-08-31 11:55:55 -0700701 uint8_t fruID{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530702 auto recordID = get_sdr::request::get_record_id(req);
703
704 fruID = recordID - FRU_RECORD_ID_START;
705 fru = frus.find(fruID);
706 if (fru == frus.end())
707 {
708 return IPMI_CC_SENSOR_INVALID;
709 }
710
711 /* Header */
712 get_sdr::header::set_record_id(recordID, &(record.header));
713 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
714 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
715 record.header.record_length = sizeof(record.key) + sizeof(record.body);
716
717 /* Key */
718 record.key.fruID = fruID;
719 record.key.accessLun |= IPMI_LOGICAL_FRU;
720 record.key.deviceAddress = BMCSlaveAddress;
721
722 /* Body */
723 record.body.entityID = fru->second[0].entityID;
724 record.body.entityInstance = fru->second[0].entityInstance;
725 record.body.deviceType = fruInventoryDevice;
726 record.body.deviceTypeModifier = IPMIFruInventory;
727
728 /* Device ID string */
Patrick Venture0b02be92018-08-31 11:55:55 -0700729 auto deviceID =
730 fru->second[0].path.substr(fru->second[0].path.find_last_of('/') + 1,
731 fru->second[0].path.length());
Ratan Guptae0cc8552018-01-22 14:23:04 +0530732
733 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
734 {
735 get_sdr::body::set_device_id_strlen(
Patrick Venture0b02be92018-08-31 11:55:55 -0700736 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530737 }
738 else
739 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700740 get_sdr::body::set_device_id_strlen(deviceID.length(), &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530741 }
742
743 strncpy(record.body.deviceID, deviceID.c_str(),
744 get_sdr::body::get_device_id_strlen(&(record.body)));
745
746 if (++fru == frus.end())
747 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700748 get_sdr::response::set_next_record_id(END_OF_RECORD,
749 resp); // last record
Ratan Guptae0cc8552018-01-22 14:23:04 +0530750 }
751 else
752 {
753 get_sdr::response::set_next_record_id(
Patrick Venture0b02be92018-08-31 11:55:55 -0700754 (FRU_RECORD_ID_START + fru->first), resp);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530755 }
756
757 if (req->bytes_to_read > (sizeof(*resp) - req->offset))
758 {
759 dataLength = (sizeof(*resp) - req->offset);
760 }
761 else
762 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700763 dataLength = req->bytes_to_read;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530764 }
765
766 if (dataLength <= 0)
767 {
768 return IPMI_CC_REQ_DATA_LEN_INVALID;
769 }
770
Patrick Venture0b02be92018-08-31 11:55:55 -0700771 memcpy(resp->record_data, reinterpret_cast<uint8_t*>(&record) + req->offset,
772 (dataLength));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530773
774 *data_len = dataLength;
775 *data_len += 2; // additional 2 bytes for next record ID
776
777 return IPMI_CC_OK;
778}
779
Emily Shafferbbef71c2017-05-08 16:36:17 -0700780ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
781 ipmi_request_t request, ipmi_response_t response,
782 ipmi_data_len_t data_len, ipmi_context_t context)
783{
784 ipmi_ret_t ret = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700785 get_sdr::GetSdrReq* req = (get_sdr::GetSdrReq*)request;
786 get_sdr::GetSdrResp* resp = (get_sdr::GetSdrResp*)response;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700787 get_sdr::SensorDataFullRecord record = {0};
788 if (req != NULL)
789 {
790 // Note: we use an iterator so we can provide the next ID at the end of
791 // the call.
792 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +0530793 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700794
795 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +0530796 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700797 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530798 // recordID greater then 255,it means it is a FRU record.
799 // Currently we are supporting two record types either FULL record
800 // or FRU record.
801 if (recordID >= FRU_RECORD_ID_START)
802 {
803 return ipmi_fru_get_sdr(request, response, data_len);
804 }
805 else
806 {
807 sensor = sensors.find(recordID);
808 if (sensor == sensors.end())
809 {
810 return IPMI_CC_SENSOR_INVALID;
811 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700812 }
813 }
814
815 uint8_t sensor_id = sensor->first;
816
817 /* Header */
818 get_sdr::header::set_record_id(sensor_id, &(record.header));
819 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
820 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
821 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
822
823 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +0530824 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -0700825 record.key.sensor_number = sensor_id;
826
827 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +0530828 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700829 record.body.sensor_type = sensor->second.sensorType;
830 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +0530831 record.body.entity_instance = sensor->second.instance;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700832
833 // Set the type-specific details given the DBus interface
834 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
835 data_len);
836
837 if (++sensor == sensors.end())
838 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530839 // we have reached till end of sensor, so assign the next record id
840 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
Patrick Venture0b02be92018-08-31 11:55:55 -0700841 auto next_record_id =
842 (frus.size()) ? frus.begin()->first + FRU_RECORD_ID_START
843 : END_OF_RECORD;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530844
845 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700846 }
847 else
848 {
849 get_sdr::response::set_next_record_id(sensor->first, resp);
850 }
851
852 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
853 memcpy(resp->record_data, (char*)&record + req->offset,
854 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
855 }
856
857 return ret;
858}
859
Chris Austenac4604a2015-10-13 12:43:27 -0500860void register_netfn_sen_functions()
861{
Tom05732372016-09-06 17:21:23 +0530862 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -0700863 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, nullptr,
864 ipmi_sen_wildcard, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500865
Tom05732372016-09-06 17:21:23 +0530866 // <Get Sensor Type>
Patrick Venture0b02be92018-08-31 11:55:55 -0700867 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, nullptr,
868 ipmi_sen_get_sensor_type, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500869
Tom05732372016-09-06 17:21:23 +0530870 // <Set Sensor Reading and Event Status>
Patrick Venture0b02be92018-08-31 11:55:55 -0700871 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, nullptr,
872 ipmi_sen_set_sensor, PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -0500873
Tom05732372016-09-06 17:21:23 +0530874 // <Get Sensor Reading>
Patrick Venture0b02be92018-08-31 11:55:55 -0700875 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, nullptr,
876 ipmi_sen_get_sensor_reading, PRIVILEGE_USER);
Emily Shaffera344afc2017-04-13 15:09:39 -0700877
Tom Joseph5ca50952018-02-22 00:33:38 +0530878 // <Reserve Device SDR Repository>
879 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_DEVICE_SDR_REPO,
Patrick Venture0b02be92018-08-31 11:55:55 -0700880 nullptr, ipmi_sen_reserve_sdr, PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600881
Tom Joseph5ca50952018-02-22 00:33:38 +0530882 // <Get Device SDR Info>
Patrick Venture0b02be92018-08-31 11:55:55 -0700883 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR_INFO, nullptr,
884 ipmi_sen_get_sdr_info, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700885
Tom Joseph5ca50952018-02-22 00:33:38 +0530886 // <Get Device SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -0700887 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR, nullptr,
888 ipmi_sen_get_sdr, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700889
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600890 // <Get Sensor Thresholds>
891 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
892 nullptr, ipmi_sen_get_sensor_thresholds,
893 PRIVILEGE_USER);
894
Chris Austenac4604a2015-10-13 12:43:27 -0500895 return;
896}