blob: 618aa54c28ee48f136bf07750c573f61b94cc0ee [file] [log] [blame]
Tomd700e762016-09-20 18:24:13 +05301#include <mapper.h>
Emily Shaffer1fabf222017-04-05 08:53:21 -07002#include <math.h>
Chris Austenac4604a2015-10-13 12:43:27 -05003#include <stdio.h>
4#include <string.h>
Emily Shaffercc941e12017-06-14 13:06:26 -07005#include <set>
Tom Josephbe703f72017-03-09 12:34:35 +05306#include <bitset>
Emily Shafferbbef71c2017-05-08 16:36:17 -07007#include <xyz/openbmc_project/Sensor/Value/server.hpp>
Chris Austen10ccc0f2015-12-10 18:27:04 -06008#include <systemd/sd-bus.h>
Tom Josephbe703f72017-03-09 12:34:35 +05309#include "host-ipmid/ipmid-api.h"
10#include <phosphor-logging/log.hpp>
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050011#include <phosphor-logging/elog-errors.hpp>
Ratan Guptae0cc8552018-01-22 14:23:04 +053012#include "fruread.hpp"
Tomd700e762016-09-20 18:24:13 +053013#include "ipmid.hpp"
Tom Josephbe703f72017-03-09 12:34:35 +053014#include "sensorhandler.h"
15#include "types.hpp"
16#include "utils.hpp"
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050017#include "xyz/openbmc_project/Common/error.hpp"
18
Ratan Guptae0cc8552018-01-22 14:23:04 +053019static constexpr uint8_t fruInventoryDevice = 0x10;
20static constexpr uint8_t IPMIFruInventory = 0x02;
21static constexpr uint8_t BMCSlaveAddress = 0x20;
22
Chris Austen8a45e7c2015-10-15 00:31:46 -050023extern int updateSensorRecordFromSSRAESC(const void *);
Tomd700e762016-09-20 18:24:13 +053024extern sd_bus *bus;
Tom Josephbe703f72017-03-09 12:34:35 +053025extern const ipmi::sensor::IdInfoMap sensors;
Ratan Guptae0cc8552018-01-22 14:23:04 +053026extern const FruMap frus;
27
28
Tom Josephbe703f72017-03-09 12:34:35 +053029using namespace phosphor::logging;
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050030using InternalFailure =
31 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austenac4604a2015-10-13 12:43:27 -050032
33void register_netfn_sen_functions() __attribute__((constructor));
34
Chris Austen0012e9b2015-10-22 01:37:46 -050035struct sensorTypemap_t {
36 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060037 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050038 char dbusname[32];
39} ;
40
Chris Austen0012e9b2015-10-22 01:37:46 -050041sensorTypemap_t g_SensorTypeMap[] = {
42
Chris Austend7cf0e42015-11-07 14:27:12 -060043 {0x01, 0x6F, "Temp"},
44 {0x0C, 0x6F, "DIMM"},
45 {0x0C, 0x6F, "MEMORY_BUFFER"},
46 {0x07, 0x6F, "PROC"},
47 {0x07, 0x6F, "CORE"},
48 {0x07, 0x6F, "CPU"},
49 {0x0F, 0x6F, "BootProgress"},
50 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor type code os 0x09
51 {0xC3, 0x6F, "BootCount"},
52 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060053 {0x12, 0x6F, "SYSTEM_EVENT"},
54 {0xC7, 0x03, "SYSTEM"},
55 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060056 {0xC2, 0x6F, "PowerCap"},
Tom Joseph558184e2017-09-01 13:45:05 +053057 {0x0b, 0xCA, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050058 {0xDA, 0x03, "TurboAllowed"},
Tom Joseph558184e2017-09-01 13:45:05 +053059 {0xD8, 0xC8, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060060 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050061};
62
63
Chris Austenac4604a2015-10-13 12:43:27 -050064struct sensor_data_t {
65 uint8_t sennum;
66} __attribute__ ((packed)) ;
67
Chris Austen10ccc0f2015-12-10 18:27:04 -060068struct sensorreadingresp_t {
69 uint8_t value;
70 uint8_t operation;
71 uint8_t indication[2];
72} __attribute__ ((packed)) ;
Chris Austenac4604a2015-10-13 12:43:27 -050073
Emily Shaffer2ae09b92017-04-05 15:09:41 -070074int get_bus_for_path(const char *path, char **busname) {
75 return mapper_get_service(bus, path, busname);
76}
Tomd700e762016-09-20 18:24:13 +053077
Emily Shaffer2ae09b92017-04-05 15:09:41 -070078int legacy_dbus_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
Tomd700e762016-09-20 18:24:13 +053079 char *busname = NULL;
80 const char *iface = "org.openbmc.managers.System";
81 const char *objname = "/org/openbmc/managers/System";
82 char *str1 = NULL, *str2, *str3;
83 sd_bus_error error = SD_BUS_ERROR_NULL;
84 sd_bus_message *reply = NULL;
85
86
87 int r;
Emily Shaffer2ae09b92017-04-05 15:09:41 -070088 r = get_bus_for_path(objname, &busname);
Tomd700e762016-09-20 18:24:13 +053089 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -040090 fprintf(stderr, "Failed to get %s busname: %s\n",
91 objname, strerror(-r));
Tomd700e762016-09-20 18:24:13 +053092 goto final;
93 }
94
95 r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
96 &error, &reply, "sy", type, num);
97 if (r < 0) {
98 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
99 goto final;
100 }
101
102 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
103 if (r < 0) {
104 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
105 goto final;
106 }
107
Lei YU91875f72018-04-03 15:14:49 +0800108 if (strlen(str2) == 0)
109 {
110 // Path being empty occurs when the sensor id is not in SystemManager
111 r = -EINVAL;
112 goto final;
113 }
114
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700115 r = get_bus_for_path(str2, &str1);
Tomd700e762016-09-20 18:24:13 +0530116 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400117 fprintf(stderr, "Failed to get %s busname: %s\n",
118 str2, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530119 goto final;
120 }
121
122 strncpy(interface->bus, str1, MAX_DBUS_PATH);
123 strncpy(interface->path, str2, MAX_DBUS_PATH);
124 strncpy(interface->interface, str3, MAX_DBUS_PATH);
125
126 interface->sensornumber = num;
Emily Shaffer71174412017-04-05 15:10:40 -0700127 // Make sure we know that the type hasn't been set, as newer codebase will
128 // set it automatically from the YAML at this step.
129 interface->sensortype = 0;
Tomd700e762016-09-20 18:24:13 +0530130
131final:
132
133 sd_bus_error_free(&error);
134 reply = sd_bus_message_unref(reply);
135 free(busname);
136 free(str1);
137
138 return r;
139}
140
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700141// Use a lookup table to find the interface name of a specific sensor
142// This will be used until an alternative is found. this is the first
143// step for mapping IPMI
144int find_openbmc_path(uint8_t num, dbus_interface_t *interface) {
145 int rc;
146
147 // When the sensor map does not contain the sensor requested,
148 // fall back to the legacy DBus lookup (deprecated)
149 const auto& sensor_it = sensors.find(num);
150 if (sensor_it == sensors.end())
151 {
152 return legacy_dbus_openbmc_path("SENSOR", num, interface);
153 }
154
155 const auto& info = sensor_it->second;
156
Patrick Williams8451edf2017-06-13 09:01:06 -0500157 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700158 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
159 if (rc < 0) {
160 fprintf(stderr, "Failed to get %s busname: %s\n",
161 info.sensorPath.c_str(),
162 busname);
163 goto final;
164 }
165
166 interface->sensortype = info.sensorType;
167 strcpy(interface->bus, busname);
168 strcpy(interface->path, info.sensorPath.c_str());
169 // Take the interface name from the beginning of the DbusInterfaceMap. This
170 // works for the Value interface but may not suffice for more complex
171 // sensors.
172 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500173 strcpy(interface->interface, info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700174 interface->sensornumber = num;
175
176final:
177 free(busname);
178 return rc;
179}
180
Tomd700e762016-09-20 18:24:13 +0530181
182/////////////////////////////////////////////////////////////////////
183//
184// Routines used by ipmi commands wanting to interact on the dbus
185//
186/////////////////////////////////////////////////////////////////////
187int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
188
189
190 dbus_interface_t a;
191 int r;
192 sd_bus_error error = SD_BUS_ERROR_NULL;
193 sd_bus_message *m=NULL;
194
195 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
196 number, method, value);
197
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700198 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530199
200 if (r < 0) {
201 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
202 return 0;
203 }
204
205 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
206 if (r < 0) {
207 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
208 goto final;
209 }
210
211 r = sd_bus_message_append(m, "v", "s", value);
212 if (r < 0) {
213 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
214 goto final;
215 }
216
217
218 r = sd_bus_call(bus, m, 0, &error, NULL);
219 if (r < 0) {
220 fprintf(stderr, "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}
229int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
230
231
232 dbus_interface_t a;
233 int r;
234 sd_bus_error error = SD_BUS_ERROR_NULL;
235 sd_bus_message *m=NULL;
236
237 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
238 number, method, value);
239
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700240 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530241
242 if (r < 0) {
243 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
244 return 0;
245 }
246
247 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
248 if (r < 0) {
249 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
250 goto final;
251 }
252
253 r = sd_bus_message_append(m, "v", "i", value);
254 if (r < 0) {
255 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
256 goto final;
257 }
258
259
260 r = sd_bus_call(bus, m, 0, &error, NULL);
261 if (r < 0) {
262 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
263 }
264
265final:
266 sd_bus_error_free(&error);
267 m = sd_bus_message_unref(m);
268
269 return 0;
270}
271
Chris Austen0012e9b2015-10-22 01:37:46 -0500272uint8_t dbus_to_sensor_type(char *p) {
Chris Austenac4604a2015-10-13 12:43:27 -0500273
Chris Austen0012e9b2015-10-22 01:37:46 -0500274 sensorTypemap_t *s = g_SensorTypeMap;
275 char r=0;
Chris Austen0012e9b2015-10-22 01:37:46 -0500276 while (s->number != 0xFF) {
277 if (!strcmp(s->dbusname,p)) {
Tom Joseph558184e2017-09-01 13:45:05 +0530278 r = s->typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -0500279 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500280 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500281 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500282 }
283
Chris Austen0012e9b2015-10-22 01:37:46 -0500284 if (s->number == 0xFF)
285 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500286
Chris Austen0012e9b2015-10-22 01:37:46 -0500287 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500288}
289
Chris Austen0012e9b2015-10-22 01:37:46 -0500290
Emily Shaffer391f3302017-04-03 10:27:08 -0700291uint8_t get_type_from_interface(dbus_interface_t dbus_if) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500292
Chris Austen0012e9b2015-10-22 01:37:46 -0500293 char *p;
Brad Bishop56003452016-10-05 21:49:19 -0400294 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500295
Chris Austen0012e9b2015-10-22 01:37:46 -0500296 // This is where sensors that do not exist in dbus but do
297 // exist in the host code stop. This should indicate it
298 // is not a supported sensor
Emily Shaffer391f3302017-04-03 10:27:08 -0700299 if (dbus_if.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500300
Emily Shaffer71174412017-04-05 15:10:40 -0700301 // Fetch type from interface itself.
302 if (dbus_if.sensortype != 0)
303 {
304 type = dbus_if.sensortype;
Chris Austen0012e9b2015-10-22 01:37:46 -0500305 } else {
306 // Non InventoryItems
Emily Shaffer391f3302017-04-03 10:27:08 -0700307 p = strrchr (dbus_if.path, '/');
Brad Bishop56003452016-10-05 21:49:19 -0400308 type = dbus_to_sensor_type(p+1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500309 }
310
Brad Bishop56003452016-10-05 21:49:19 -0400311 return type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500312 }
313
Emily Shaffer391f3302017-04-03 10:27:08 -0700314// Replaces find_sensor
315uint8_t find_type_for_sensor_number(uint8_t num) {
316 int r;
317 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700318 r = find_openbmc_path(num, &dbus_if);
Emily Shaffer391f3302017-04-03 10:27:08 -0700319 if (r < 0) {
320 fprintf(stderr, "Could not find sensor %d\n", num);
Lei YU91875f72018-04-03 15:14:49 +0800321 return 0;
Emily Shaffer391f3302017-04-03 10:27:08 -0700322 }
323 return get_type_from_interface(dbus_if);
324}
325
Chris Austen10ccc0f2015-12-10 18:27:04 -0600326
327
328
329
Chris Austen0012e9b2015-10-22 01:37:46 -0500330ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
331 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500332 ipmi_data_len_t data_len, ipmi_context_t context)
333{
334 sensor_data_t *reqptr = (sensor_data_t*)request;
335 ipmi_ret_t rc = IPMI_CC_OK;
336
337 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
338
339 // TODO Not sure what the System-event-sensor is suppose to return
340 // need to ask Hostboot team
341 unsigned char buf[] = {0x00,0x6F};
342
Emily Shaffer391f3302017-04-03 10:27:08 -0700343 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500344
345 // HACK UNTIL Dbus gets updated or we find a better way
346 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600347 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500348 }
349
Chris Austenac4604a2015-10-13 12:43:27 -0500350
351 *data_len = sizeof(buf);
352 memcpy(response, &buf, *data_len);
353
Chris Austenac4604a2015-10-13 12:43:27 -0500354 return rc;
355}
356
Emily Shaffercc941e12017-06-14 13:06:26 -0700357const std::set<std::string> analogSensorInterfaces =
358{
359 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700360 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700361};
362
363bool isAnalogSensor(const std::string& interface)
364{
365 return (analogSensorInterfaces.count(interface));
366}
367
Tom Josephbe703f72017-03-09 12:34:35 +0530368ipmi_ret_t setSensorReading(void *request)
369{
Tom Joseph816e92b2017-09-06 19:23:00 +0530370 ipmi::sensor::SetSensorReadingReq cmdData =
371 *(static_cast<ipmi::sensor::SetSensorReadingReq *>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530372
373 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500374 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530375 if (iter == sensors.end())
376 {
377 return IPMI_CC_SENSOR_INVALID;
378 }
379
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500380 try
381 {
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500382 if (ipmi::sensor::Mutability::Write !=
383 (iter->second.mutability & ipmi::sensor::Mutability::Write))
384 {
385 log<level::ERR>("Sensor Set operation is not allowed",
386 entry("SENSOR_NUM=%d", cmdData.number));
387 return IPMI_CC_ILLEGAL_COMMAND;
388 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500389 return iter->second.updateFunc(cmdData, iter->second);
390 }
391 catch (InternalFailure& e)
392 {
393 log<level::ERR>("Set sensor failed",
394 entry("SENSOR_NUM=%d", cmdData.number));
395 commit<InternalFailure>();
396 }
Tom Joseph82024322017-09-28 20:07:29 +0530397 catch (const std::runtime_error& e)
398 {
399 log<level::ERR>(e.what());
400 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500401
402 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530403}
Chris Austenac4604a2015-10-13 12:43:27 -0500404
Chris Austen0012e9b2015-10-22 01:37:46 -0500405ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
406 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500407 ipmi_data_len_t data_len, ipmi_context_t context)
408{
Chris Austenac4604a2015-10-13 12:43:27 -0500409 sensor_data_t *reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500410
Chris Austen0012e9b2015-10-22 01:37:46 -0500411 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500412
Tom Josephbe703f72017-03-09 12:34:35 +0530413 /*
414 * This would support the Set Sensor Reading command for the presence
415 * and functional state of Processor, Core & DIMM. For the remaining
416 * sensors the existing support is invoked.
417 */
418 auto ipmiRC = setSensorReading(request);
419
420 if(ipmiRC == IPMI_CC_SENSOR_INVALID)
421 {
422 updateSensorRecordFromSSRAESC(reqptr);
423 ipmiRC = IPMI_CC_OK;
424 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500425
Chris Austenac4604a2015-10-13 12:43:27 -0500426 *data_len=0;
Tom Josephbe703f72017-03-09 12:34:35 +0530427 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500428}
429
Tom Joseph3ee668f2018-03-02 19:49:17 +0530430ipmi_ret_t legacyGetSensorReading(uint8_t sensorNum,
431 ipmi_response_t response,
432 ipmi_data_len_t data_len)
Chris Austen10ccc0f2015-12-10 18:27:04 -0600433{
Chris Austen10ccc0f2015-12-10 18:27:04 -0600434 int r;
435 dbus_interface_t a;
436 sd_bus *bus = ipmid_get_sd_bus_connection();
Tom Joseph3ee668f2018-03-02 19:49:17 +0530437 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
438 uint8_t type = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600439 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600440 int reading = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600441 char* assertion = NULL;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530442 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
443 *data_len=0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600444
Tom Joseph3ee668f2018-03-02 19:49:17 +0530445 r = find_openbmc_path(sensorNum, &a);
Tom Joseph40c35b12017-09-07 02:04:42 +0530446 if (r < 0)
447 {
Tom Joseph3ee668f2018-03-02 19:49:17 +0530448 sd_journal_print(LOG_ERR, "Failed to find Sensor 0x%02x\n", sensorNum);
449 return IPMI_CC_SENSOR_INVALID;
Nan Li36deb762016-05-12 10:23:41 +0800450 }
Tom Joseph3ee668f2018-03-02 19:49:17 +0530451
452 type = get_type_from_interface(a);
453 if (type == 0)
Tom Joseph40c35b12017-09-07 02:04:42 +0530454 {
Tom Joseph3ee668f2018-03-02 19:49:17 +0530455 sd_journal_print(LOG_ERR, "Failed to find Sensor 0x%02x\n",
456 sensorNum);
457 return IPMI_CC_SENSOR_INVALID;
Brad Bishop56003452016-10-05 21:49:19 -0400458 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600459
Chris Austen10ccc0f2015-12-10 18:27:04 -0600460 switch(type) {
Chris Austen10ccc0f2015-12-10 18:27:04 -0600461 case 0xC2:
Dhruvaraj Subhashchandrand12ae752017-10-04 00:33:27 -0500462 case 0xC8:
Tom Joseph3ee668f2018-03-02 19:49:17 +0530463 r = sd_bus_get_property(bus,a.bus, a.path, a.interface,
464 "value", NULL, &reply, "i");
465 if (r < 0)
466 {
467 sd_journal_print(LOG_ERR, "Failed to call sd_bus_get_property:"
468 " %d, %s\n", r, strerror(-r));
469 sd_journal_print(LOG_ERR, "Bus: %s, Path: %s, Interface: %s\n",
470 a.bus, a.path, a.interface);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600471 break;
472 }
473
Adriana Kobylak93125982016-03-01 12:48:10 -0600474 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600475 if (r < 0) {
Tom Joseph3ee668f2018-03-02 19:49:17 +0530476 sd_journal_print(LOG_ERR, "Failed to read sensor: %s\n",
477 strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600478 break;
479 }
480
Chris Austen10ccc0f2015-12-10 18:27:04 -0600481 rc = IPMI_CC_OK;
482 *data_len=sizeof(sensorreadingresp_t);
483
Adriana Kobylak93125982016-03-01 12:48:10 -0600484 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600485 resp->operation = 0;
486 resp->indication[0] = 0;
487 resp->indication[1] = 0;
488 break;
489
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600490 //TODO openbmc/openbmc#2154 Move this sensor to right place.
491 case 0xCA:
Tom Joseph3ee668f2018-03-02 19:49:17 +0530492 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value",
493 NULL, &reply, "s");
494 if (r < 0)
495 {
496 sd_journal_print(LOG_ERR, "Failed to call sd_bus_get_property:"
497 " %d, %s\n", r, strerror(-r));
498 sd_journal_print(LOG_ERR, "Bus: %s, Path: %s, Interface: %s\n",
499 a.bus, a.path, a.interface);
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600500 break;
501 }
502
503 r = sd_bus_message_read(reply, "s", &assertion);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530504 if (r < 0)
505 {
506 sd_journal_print(LOG_ERR, "Failed to read sensor: %s\n",
507 strerror(-r));
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600508 break;
509 }
510
511 rc = IPMI_CC_OK;
512 *data_len=sizeof(sensorreadingresp_t);
513
514 resp->value = 0;
515 resp->operation = 0;
516 if (strcmp(assertion,"Enabled") == 0)
517 {
518 resp->indication[0] = 0x02;
519 }
520 else
521 {
522 resp->indication[0] = 0x1;
523 }
524 resp->indication[1] = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600525 break;
526
Chris Austen10ccc0f2015-12-10 18:27:04 -0600527 default:
Tom Joseph14c15462017-09-07 02:16:51 +0530528 {
Tom Joseph3ee668f2018-03-02 19:49:17 +0530529 return IPMI_CC_SENSOR_INVALID;
Tom Joseph14c15462017-09-07 02:16:51 +0530530 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600531 }
532
vishwa1eaea4f2016-02-26 11:57:40 -0600533 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600534
535 return rc;
536}
537
Tom Joseph3ee668f2018-03-02 19:49:17 +0530538ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
539 ipmi_request_t request, ipmi_response_t response,
540 ipmi_data_len_t data_len, ipmi_context_t context)
541{
542 sensor_data_t *reqptr = (sensor_data_t*)request;
543 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
544 ipmi::sensor::GetSensorResponse getResponse {};
545 static constexpr auto scanningEnabledBit = 6;
546
547 const auto iter = sensors.find(reqptr->sennum);
548 if (iter == sensors.end())
549 {
550 return legacyGetSensorReading(reqptr->sennum, response, data_len);
551 }
552 if (ipmi::sensor::Mutability::Read !=
553 (iter->second.mutability & ipmi::sensor::Mutability::Read))
554 {
Jayanth Othayoth6ccf8812018-04-05 22:58:48 -0500555 return IPMI_CC_ILLEGAL_COMMAND;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530556 }
557
558 try
559 {
560 getResponse = iter->second.getFunc(iter->second);
561 *data_len = getResponse.size();
562 memcpy(resp, getResponse.data(), *data_len);
563 resp->operation = 1 << scanningEnabledBit;
564 return IPMI_CC_OK;
565 }
566 catch (const std::exception& e)
567 {
568 *data_len = getResponse.size();
569 memcpy(resp, getResponse.data(), *data_len);
570 return IPMI_CC_OK;
571 }
572}
573
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530574void getSensorThresholds(uint8_t sensorNum,
575 get_sdr::GetSensorThresholdsResponse* response)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600576{
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530577 constexpr auto warningThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600578 "xyz.openbmc_project.Sensor.Threshold.Warning";
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530579 constexpr auto criticalThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600580 "xyz.openbmc_project.Sensor.Threshold.Critical";
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600581
582 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
583
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530584 const auto iter = sensors.find(sensorNum);
585 const auto info = iter->second;
586
587 auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
588
589 auto warnThresholds = ipmi::getAllDbusProperties(bus,
590 service,
591 info.sensorPath,
592 warningThreshIntf);
593
594 double warnLow = warnThresholds["WarningLow"].get<int64_t>();
595 double warnHigh = warnThresholds["WarningHigh"].get<int64_t>();
596
597 if (warnLow != 0)
598 {
599 warnLow *= pow(10, info.scale - info.exponentR);
600 response->lowerNonCritical = static_cast<uint8_t>((
601 warnLow - info.scaledOffset) / info.coefficientM);
602 response->validMask |= static_cast<uint8_t>(
603 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
604 }
605
606 if (warnHigh != 0)
607 {
608 warnHigh *= pow(10, info.scale - info.exponentR);
609 response->upperNonCritical = static_cast<uint8_t>((
610 warnHigh - info.scaledOffset) / info.coefficientM);
611 response->validMask |= static_cast<uint8_t>(
612 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
613 }
614
615 auto critThresholds = ipmi::getAllDbusProperties(bus,
616 service,
617 info.sensorPath,
618 criticalThreshIntf);
619 double critLow = critThresholds["CriticalLow"].get<int64_t>();
620 double critHigh = critThresholds["CriticalHigh"].get<int64_t>();
621
622 if (critLow != 0)
623 {
624 critLow *= pow(10, info.scale - info.exponentR);
625 response->lowerCritical = static_cast<uint8_t>((
626 critLow - info.scaledOffset) / info.coefficientM);
627 response->validMask |= static_cast<uint8_t>(
628 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
629 }
630
631 if (critHigh != 0)
632 {
633 critHigh *= pow(10, info.scale - info.exponentR);
634 response->upperCritical = static_cast<uint8_t>((
635 critHigh - info.scaledOffset)/ info.coefficientM);
636 response->validMask |= static_cast<uint8_t>(
637 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
638 }
639}
640
641ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
642 ipmi_request_t request, ipmi_response_t response,
643 ipmi_data_len_t data_len, ipmi_context_t context)
644{
645 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
646
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600647 if (*data_len != sizeof(uint8_t))
648 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530649 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600650 return IPMI_CC_REQ_DATA_LEN_INVALID;
651 }
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600652
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530653 auto sensorNum = *(reinterpret_cast<const uint8_t *>(request));
654 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600655
656 const auto iter = sensors.find(sensorNum);
657 if (iter == sensors.end())
658 {
659 return IPMI_CC_SENSOR_INVALID;
660 }
661
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530662 const auto info = iter->second;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600663
664 //Proceed only if the sensor value interface is implemented.
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530665 if (info.propertyInterfaces.find(valueInterface) ==
666 info.propertyInterfaces.end())
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600667 {
668 //return with valid mask as 0
669 return IPMI_CC_OK;
670 }
671
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530672 auto responseData =
673 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600674
675 try
676 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530677 getSensorThresholds(sensorNum, responseData);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600678 }
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530679 catch (std::exception& e)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600680 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530681 //Mask if the property is not present
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600682 responseData->validMask = 0;
683 }
684
685 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
686 return IPMI_CC_OK;
687}
688
Chris Austen0012e9b2015-10-22 01:37:46 -0500689ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
690 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500691 ipmi_data_len_t data_len, ipmi_context_t context)
692{
Nan Li70aa8d92016-08-29 00:11:10 +0800693 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500694
695 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
696 *data_len = 0;
697
698 return rc;
699}
700
Emily Shafferd06e0e72017-04-05 09:08:57 -0700701ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
702 ipmi_request_t request,
703 ipmi_response_t response,
704 ipmi_data_len_t data_len,
705 ipmi_context_t context)
706{
707 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
708 if (request == nullptr ||
709 get_sdr_info::request::get_count(request) == false)
710 {
711 // Get Sensor Count
Ratan Guptae0cc8552018-01-22 14:23:04 +0530712 resp->count = sensors.size() + frus.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700713 }
714 else
715 {
716 resp->count = 1;
717 }
718
719 // Multiple LUNs not supported.
720 namespace response = get_sdr_info::response;
721 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
722 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
723 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
724 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
725 response::set_static_population(&(resp->luns_and_dynamic_population));
726
727 *data_len = SDR_INFO_RESP_SIZE;
728
729 return IPMI_CC_OK;
730}
731
Emily Shaffera344afc2017-04-13 15:09:39 -0700732ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
733 ipmi_request_t request,
734 ipmi_response_t response,
735 ipmi_data_len_t data_len,
736 ipmi_context_t context)
737{
738 // A constant reservation ID is okay until we implement add/remove SDR.
739 const uint16_t reservation_id = 1;
740 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700741 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700742
743 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
744 return IPMI_CC_OK;
745}
Chris Austenac4604a2015-10-13 12:43:27 -0500746
Tom Josephdc212b22018-02-16 09:59:57 +0530747void setUnitFieldsForObject(const ipmi::sensor::Info *info,
Emily Shaffercc941e12017-06-14 13:06:26 -0700748 get_sdr::SensorDataFullRecordBody *body)
749{
Tom Josephdc212b22018-02-16 09:59:57 +0530750 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
751 try
Emily Shaffercc941e12017-06-14 13:06:26 -0700752 {
Tom Josephdc212b22018-02-16 09:59:57 +0530753 auto unit = server::Value::convertUnitFromString(info->unit);
754 // Unit strings defined in
755 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
756 switch (unit)
Emily Shaffercc941e12017-06-14 13:06:26 -0700757 {
Tom Josephdc212b22018-02-16 09:59:57 +0530758 case server::Value::Unit::DegreesC:
759 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
760 break;
761 case server::Value::Unit::RPMS:
762 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
763 get_sdr::body::set_rate_unit(0b100, body); // per minute
764 break;
765 case server::Value::Unit::Volts:
766 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
767 break;
768 case server::Value::Unit::Meters:
769 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
770 break;
771 case server::Value::Unit::Amperes:
772 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
773 break;
774 case server::Value::Unit::Joules:
775 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
776 break;
777 case server::Value::Unit::Watts:
778 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
779 break;
780 default:
781 // Cannot be hit.
782 fprintf(stderr, "Unknown value unit type: = %s\n",
783 info->unit.c_str());
Emily Shaffercc941e12017-06-14 13:06:26 -0700784 }
785 }
Tom Josephdc212b22018-02-16 09:59:57 +0530786 catch (sdbusplus::exception::InvalidEnumString e)
Emily Shaffercc941e12017-06-14 13:06:26 -0700787 {
Tom Josephdc212b22018-02-16 09:59:57 +0530788 log<level::WARNING>("Warning: no unit provided for sensor!");
Emily Shaffercc941e12017-06-14 13:06:26 -0700789 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700790}
791
Emily Shafferbbef71c2017-05-08 16:36:17 -0700792ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body,
793 const ipmi::sensor::Info *info,
794 ipmi_data_len_t data_len)
795{
796 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700797 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700798 {
Emily Shafferbbef71c2017-05-08 16:36:17 -0700799
800 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
801
802 /* Unit info */
Tom Josephdc212b22018-02-16 09:59:57 +0530803 setUnitFieldsForObject(info, body);
Emily Shaffer10f49592017-05-10 12:01:10 -0700804
805 get_sdr::body::set_b(info->coefficientB, body);
806 get_sdr::body::set_m(info->coefficientM, body);
807 get_sdr::body::set_b_exp(info->exponentB, body);
Tom Josephdc212b22018-02-16 09:59:57 +0530808 get_sdr::body::set_r_exp(info->exponentR, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700809
Emily Shafferbbef71c2017-05-08 16:36:17 -0700810 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700811 }
812
Tom Joseph96423912018-01-25 00:14:34 +0530813 /* ID string */
814 auto id_string = info->sensorNameFunc(*info);
815
816 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
817 {
818 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
819 }
820 else
821 {
822 get_sdr::body::set_id_strlen(id_string.length(), body);
823 }
824 strncpy(body->id_string, id_string.c_str(),
825 get_sdr::body::get_id_strlen(body));
826
Emily Shafferbbef71c2017-05-08 16:36:17 -0700827 return IPMI_CC_OK;
828};
829
Ratan Guptae0cc8552018-01-22 14:23:04 +0530830ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
831 ipmi_data_len_t data_len)
832{
833 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
834 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
835 get_sdr::SensorDataFruRecord record {};
836 auto dataLength = 0;
837
838 auto fru = frus.begin();
839 uint8_t fruID {};
840 auto recordID = get_sdr::request::get_record_id(req);
841
842 fruID = recordID - FRU_RECORD_ID_START;
843 fru = frus.find(fruID);
844 if (fru == frus.end())
845 {
846 return IPMI_CC_SENSOR_INVALID;
847 }
848
849 /* Header */
850 get_sdr::header::set_record_id(recordID, &(record.header));
851 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
852 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
853 record.header.record_length = sizeof(record.key) + sizeof(record.body);
854
855 /* Key */
856 record.key.fruID = fruID;
857 record.key.accessLun |= IPMI_LOGICAL_FRU;
858 record.key.deviceAddress = BMCSlaveAddress;
859
860 /* Body */
861 record.body.entityID = fru->second[0].entityID;
862 record.body.entityInstance = fru->second[0].entityInstance;
863 record.body.deviceType = fruInventoryDevice;
864 record.body.deviceTypeModifier = IPMIFruInventory;
865
866 /* Device ID string */
867 auto deviceID = fru->second[0].path.substr(
868 fru->second[0].path.find_last_of('/') + 1,
869 fru->second[0].path.length());
870
871
872 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
873 {
874 get_sdr::body::set_device_id_strlen(
875 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH,
876 &(record.body));
877 }
878 else
879 {
880 get_sdr::body::set_device_id_strlen(deviceID.length(),
881 &(record.body));
882 }
883
884 strncpy(record.body.deviceID, deviceID.c_str(),
885 get_sdr::body::get_device_id_strlen(&(record.body)));
886
887 if (++fru == frus.end())
888 {
889 get_sdr::response::set_next_record_id(END_OF_RECORD, resp); // last record
890 }
891 else
892 {
893 get_sdr::response::set_next_record_id(
894 (FRU_RECORD_ID_START + fru->first), resp);
895 }
896
897 if (req->bytes_to_read > (sizeof(*resp) - req->offset))
898 {
899 dataLength = (sizeof(*resp) - req->offset);
900 }
901 else
902 {
903 dataLength = req->bytes_to_read;
904 }
905
906 if (dataLength <= 0)
907 {
908 return IPMI_CC_REQ_DATA_LEN_INVALID;
909 }
910
911 memcpy(resp->record_data,
912 reinterpret_cast<uint8_t*>(&record) + req->offset,
913 (dataLength));
914
915 *data_len = dataLength;
916 *data_len += 2; // additional 2 bytes for next record ID
917
918 return IPMI_CC_OK;
919}
920
Emily Shafferbbef71c2017-05-08 16:36:17 -0700921ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
922 ipmi_request_t request, ipmi_response_t response,
923 ipmi_data_len_t data_len, ipmi_context_t context)
924{
925 ipmi_ret_t ret = IPMI_CC_OK;
926 get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request;
927 get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response;
928 get_sdr::SensorDataFullRecord record = {0};
929 if (req != NULL)
930 {
931 // Note: we use an iterator so we can provide the next ID at the end of
932 // the call.
933 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +0530934 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700935
936 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +0530937 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700938 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530939 // recordID greater then 255,it means it is a FRU record.
940 // Currently we are supporting two record types either FULL record
941 // or FRU record.
942 if (recordID >= FRU_RECORD_ID_START)
943 {
944 return ipmi_fru_get_sdr(request, response, data_len);
945 }
946 else
947 {
948 sensor = sensors.find(recordID);
949 if (sensor == sensors.end())
950 {
951 return IPMI_CC_SENSOR_INVALID;
952 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700953 }
954 }
955
956 uint8_t sensor_id = sensor->first;
957
958 /* Header */
959 get_sdr::header::set_record_id(sensor_id, &(record.header));
960 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
961 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
962 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
963
964 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +0530965 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -0700966 record.key.sensor_number = sensor_id;
967
968 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +0530969 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700970 record.body.sensor_type = sensor->second.sensorType;
971 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +0530972 record.body.entity_instance = sensor->second.instance;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700973
974 // Set the type-specific details given the DBus interface
975 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
976 data_len);
977
978 if (++sensor == sensors.end())
979 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530980 // we have reached till end of sensor, so assign the next record id
981 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
982 auto next_record_id = (frus.size()) ?
983 frus.begin()->first + FRU_RECORD_ID_START :
984 END_OF_RECORD;
985
986 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700987 }
988 else
989 {
990 get_sdr::response::set_next_record_id(sensor->first, resp);
991 }
992
993 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
994 memcpy(resp->record_data, (char*)&record + req->offset,
995 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
996 }
997
998 return ret;
999}
1000
1001
Chris Austenac4604a2015-10-13 12:43:27 -05001002void register_netfn_sen_functions()
1003{
Tom05732372016-09-06 17:21:23 +05301004 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -07001005 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1006 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
1007 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
1008 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +05301009 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001010
Tom05732372016-09-06 17:21:23 +05301011 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -07001012 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1013 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
1014 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
1015 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +05301016 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001017
Tom05732372016-09-06 17:21:23 +05301018 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -07001019 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1020 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
1021 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
1022 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +05301023 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -05001024
Tom05732372016-09-06 17:21:23 +05301025 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -07001026 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1027 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
1028 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
1029 nullptr, ipmi_sen_get_sensor_reading,
1030 PRIVILEGE_USER);
1031
Tom Joseph5ca50952018-02-22 00:33:38 +05301032 // <Reserve Device SDR Repository>
1033 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_DEVICE_SDR_REPO,
Emily Shaffera344afc2017-04-13 15:09:39 -07001034 nullptr, ipmi_sen_reserve_sdr,
1035 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001036
Tom Joseph5ca50952018-02-22 00:33:38 +05301037 // <Get Device SDR Info>
1038 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR_INFO,
Emily Shaffera344afc2017-04-13 15:09:39 -07001039 nullptr, ipmi_sen_get_sdr_info,
1040 PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001041
Tom Joseph5ca50952018-02-22 00:33:38 +05301042 // <Get Device SDR>
1043 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR,
Emily Shafferbbef71c2017-05-08 16:36:17 -07001044 nullptr, ipmi_sen_get_sdr,
1045 PRIVILEGE_USER);
1046
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001047 // <Get Sensor Thresholds>
1048 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1049 nullptr, ipmi_sen_get_sensor_thresholds,
1050 PRIVILEGE_USER);
1051
Chris Austenac4604a2015-10-13 12:43:27 -05001052 return;
1053}