blob: 1bd10b7cb7d0f4d270013b601b6f2339093de077 [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
Tomd700e762016-09-20 18:24:13 +053074// Use a lookup table to find the interface name of a specific sensor
75// This will be used until an alternative is found. this is the first
76// step for mapping IPMI
77int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
78
79 char *str1;
80 sd_bus_error error = SD_BUS_ERROR_NULL;
81 sd_bus_message *reply = NULL, *m=NULL;
82
83
84 int r;
85
86 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
87 if (r < 0) {
88 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
89 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
90 interface->bus, interface->path, interface->interface);
91 goto final;
92 }
93
94 r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
95 if (r < 0) {
96 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
97 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
98 interface->bus, interface->path, interface->interface);
99 goto final;
100 }
101
102 r = sd_bus_call(bus, m, 0, &error, &reply);
103 if (r < 0) {
104 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
105 goto final;
106 }
107
108 r = sd_bus_message_read(reply, "v", "s", &str1) ;
109 if (r < 0) {
110 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
111 goto final;
112 }
113
114 strcpy(property_value, str1);
115
116final:
117
118 sd_bus_error_free(&error);
119 m = sd_bus_message_unref(m);
120 reply = sd_bus_message_unref(reply);
121
122 return r;
123}
124
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700125int get_bus_for_path(const char *path, char **busname) {
126 return mapper_get_service(bus, path, busname);
127}
Tomd700e762016-09-20 18:24:13 +0530128
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700129int legacy_dbus_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
Tomd700e762016-09-20 18:24:13 +0530130 char *busname = NULL;
131 const char *iface = "org.openbmc.managers.System";
132 const char *objname = "/org/openbmc/managers/System";
133 char *str1 = NULL, *str2, *str3;
134 sd_bus_error error = SD_BUS_ERROR_NULL;
135 sd_bus_message *reply = NULL;
136
137
138 int r;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700139 r = get_bus_for_path(objname, &busname);
Tomd700e762016-09-20 18:24:13 +0530140 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400141 fprintf(stderr, "Failed to get %s busname: %s\n",
142 objname, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530143 goto final;
144 }
145
146 r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
147 &error, &reply, "sy", type, num);
148 if (r < 0) {
149 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
150 goto final;
151 }
152
153 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
154 if (r < 0) {
155 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
156 goto final;
157 }
158
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700159 r = get_bus_for_path(str2, &str1);
Tomd700e762016-09-20 18:24:13 +0530160 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400161 fprintf(stderr, "Failed to get %s busname: %s\n",
162 str2, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530163 goto final;
164 }
165
166 strncpy(interface->bus, str1, MAX_DBUS_PATH);
167 strncpy(interface->path, str2, MAX_DBUS_PATH);
168 strncpy(interface->interface, str3, MAX_DBUS_PATH);
169
170 interface->sensornumber = num;
Emily Shaffer71174412017-04-05 15:10:40 -0700171 // Make sure we know that the type hasn't been set, as newer codebase will
172 // set it automatically from the YAML at this step.
173 interface->sensortype = 0;
Tomd700e762016-09-20 18:24:13 +0530174
175final:
176
177 sd_bus_error_free(&error);
178 reply = sd_bus_message_unref(reply);
179 free(busname);
180 free(str1);
181
182 return r;
183}
184
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700185// Use a lookup table to find the interface name of a specific sensor
186// This will be used until an alternative is found. this is the first
187// step for mapping IPMI
188int find_openbmc_path(uint8_t num, dbus_interface_t *interface) {
189 int rc;
190
191 // When the sensor map does not contain the sensor requested,
192 // fall back to the legacy DBus lookup (deprecated)
193 const auto& sensor_it = sensors.find(num);
194 if (sensor_it == sensors.end())
195 {
196 return legacy_dbus_openbmc_path("SENSOR", num, interface);
197 }
198
199 const auto& info = sensor_it->second;
200
Patrick Williams8451edf2017-06-13 09:01:06 -0500201 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700202 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
203 if (rc < 0) {
204 fprintf(stderr, "Failed to get %s busname: %s\n",
205 info.sensorPath.c_str(),
206 busname);
207 goto final;
208 }
209
210 interface->sensortype = info.sensorType;
211 strcpy(interface->bus, busname);
212 strcpy(interface->path, info.sensorPath.c_str());
213 // Take the interface name from the beginning of the DbusInterfaceMap. This
214 // works for the Value interface but may not suffice for more complex
215 // sensors.
216 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500217 strcpy(interface->interface, info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700218 interface->sensornumber = num;
219
220final:
221 free(busname);
222 return rc;
223}
224
Tomd700e762016-09-20 18:24:13 +0530225
226/////////////////////////////////////////////////////////////////////
227//
228// Routines used by ipmi commands wanting to interact on the dbus
229//
230/////////////////////////////////////////////////////////////////////
231int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
232
233
234 dbus_interface_t a;
235 int r;
236 sd_bus_error error = SD_BUS_ERROR_NULL;
237 sd_bus_message *m=NULL;
238
239 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
240 number, method, value);
241
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700242 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530243
244 if (r < 0) {
245 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
246 return 0;
247 }
248
249 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
250 if (r < 0) {
251 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
252 goto final;
253 }
254
255 r = sd_bus_message_append(m, "v", "s", value);
256 if (r < 0) {
257 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
258 goto final;
259 }
260
261
262 r = sd_bus_call(bus, m, 0, &error, NULL);
263 if (r < 0) {
264 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
265 }
266
267final:
268 sd_bus_error_free(&error);
269 m = sd_bus_message_unref(m);
270
271 return 0;
272}
273int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
274
275
276 dbus_interface_t a;
277 int r;
278 sd_bus_error error = SD_BUS_ERROR_NULL;
279 sd_bus_message *m=NULL;
280
281 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
282 number, method, value);
283
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700284 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530285
286 if (r < 0) {
287 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
288 return 0;
289 }
290
291 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
292 if (r < 0) {
293 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
294 goto final;
295 }
296
297 r = sd_bus_message_append(m, "v", "i", value);
298 if (r < 0) {
299 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
300 goto final;
301 }
302
303
304 r = sd_bus_call(bus, m, 0, &error, NULL);
305 if (r < 0) {
306 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
307 }
308
309final:
310 sd_bus_error_free(&error);
311 m = sd_bus_message_unref(m);
312
313 return 0;
314}
315
Chris Austen0012e9b2015-10-22 01:37:46 -0500316uint8_t dbus_to_sensor_type(char *p) {
Chris Austenac4604a2015-10-13 12:43:27 -0500317
Chris Austen0012e9b2015-10-22 01:37:46 -0500318 sensorTypemap_t *s = g_SensorTypeMap;
319 char r=0;
Chris Austen0012e9b2015-10-22 01:37:46 -0500320 while (s->number != 0xFF) {
321 if (!strcmp(s->dbusname,p)) {
Tom Joseph558184e2017-09-01 13:45:05 +0530322 r = s->typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -0500323 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500324 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500325 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500326 }
327
Chris Austen0012e9b2015-10-22 01:37:46 -0500328 if (s->number == 0xFF)
329 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500330
Chris Austen0012e9b2015-10-22 01:37:46 -0500331 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500332}
333
Chris Austen0012e9b2015-10-22 01:37:46 -0500334
335uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) {
336 char fru_type_name[64];
337 int r= 0;
338
339 r = find_interface_property_fru_type(a, "fru_type", fru_type_name);
340 if (r<0) {
341 fprintf(stderr, "Failed to get a fru type: %s", strerror(-r));
342 return -1;
343 } else {
344 return dbus_to_sensor_type(fru_type_name);
345 }
346}
347
Emily Shaffer391f3302017-04-03 10:27:08 -0700348uint8_t get_type_from_interface(dbus_interface_t dbus_if) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500349
Chris Austen0012e9b2015-10-22 01:37:46 -0500350 char *p;
Brad Bishop56003452016-10-05 21:49:19 -0400351 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500352
Chris Austen0012e9b2015-10-22 01:37:46 -0500353 // This is where sensors that do not exist in dbus but do
354 // exist in the host code stop. This should indicate it
355 // is not a supported sensor
Emily Shaffer391f3302017-04-03 10:27:08 -0700356 if (dbus_if.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500357
Emily Shaffer71174412017-04-05 15:10:40 -0700358 // Fetch type from interface itself.
359 if (dbus_if.sensortype != 0)
360 {
361 type = dbus_if.sensortype;
362 }
363 // Legacy codebase does not populate type during initial handling:
364 else if (strstr(dbus_if.interface, "InventoryItem")) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500365 // InventoryItems are real frus. So need to get the
366 // fru_type property
Emily Shaffer391f3302017-04-03 10:27:08 -0700367 type = dbus_to_sensor_type_from_dbus(&dbus_if);
Chris Austen0012e9b2015-10-22 01:37:46 -0500368 } else {
369 // Non InventoryItems
Emily Shaffer391f3302017-04-03 10:27:08 -0700370 p = strrchr (dbus_if.path, '/');
Brad Bishop56003452016-10-05 21:49:19 -0400371 type = dbus_to_sensor_type(p+1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500372 }
373
Brad Bishop56003452016-10-05 21:49:19 -0400374 return type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500375 }
376
Emily Shaffer391f3302017-04-03 10:27:08 -0700377// Replaces find_sensor
378uint8_t find_type_for_sensor_number(uint8_t num) {
379 int r;
380 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700381 r = find_openbmc_path(num, &dbus_if);
Emily Shaffer391f3302017-04-03 10:27:08 -0700382 if (r < 0) {
383 fprintf(stderr, "Could not find sensor %d\n", num);
384 return r;
385 }
386 return get_type_from_interface(dbus_if);
387}
388
Chris Austen10ccc0f2015-12-10 18:27:04 -0600389
390
391
392
Chris Austen0012e9b2015-10-22 01:37:46 -0500393ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
394 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500395 ipmi_data_len_t data_len, ipmi_context_t context)
396{
397 sensor_data_t *reqptr = (sensor_data_t*)request;
398 ipmi_ret_t rc = IPMI_CC_OK;
399
400 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
401
402 // TODO Not sure what the System-event-sensor is suppose to return
403 // need to ask Hostboot team
404 unsigned char buf[] = {0x00,0x6F};
405
Emily Shaffer391f3302017-04-03 10:27:08 -0700406 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500407
408 // HACK UNTIL Dbus gets updated or we find a better way
409 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600410 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500411 }
412
Chris Austenac4604a2015-10-13 12:43:27 -0500413
414 *data_len = sizeof(buf);
415 memcpy(response, &buf, *data_len);
416
Chris Austenac4604a2015-10-13 12:43:27 -0500417 return rc;
418}
419
Emily Shaffercc941e12017-06-14 13:06:26 -0700420const std::set<std::string> analogSensorInterfaces =
421{
422 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700423 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700424};
425
426bool isAnalogSensor(const std::string& interface)
427{
428 return (analogSensorInterfaces.count(interface));
429}
430
Tom Josephbe703f72017-03-09 12:34:35 +0530431ipmi_ret_t setSensorReading(void *request)
432{
Tom Joseph816e92b2017-09-06 19:23:00 +0530433 ipmi::sensor::SetSensorReadingReq cmdData =
434 *(static_cast<ipmi::sensor::SetSensorReadingReq *>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530435
436 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500437 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530438 if (iter == sensors.end())
439 {
440 return IPMI_CC_SENSOR_INVALID;
441 }
442
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500443 try
444 {
445 return iter->second.updateFunc(cmdData, iter->second);
446 }
447 catch (InternalFailure& e)
448 {
449 log<level::ERR>("Set sensor failed",
450 entry("SENSOR_NUM=%d", cmdData.number));
451 commit<InternalFailure>();
452 }
Tom Joseph82024322017-09-28 20:07:29 +0530453 catch (const std::runtime_error& e)
454 {
455 log<level::ERR>(e.what());
456 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500457
458 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530459}
Chris Austenac4604a2015-10-13 12:43:27 -0500460
Chris Austen0012e9b2015-10-22 01:37:46 -0500461ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
462 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500463 ipmi_data_len_t data_len, ipmi_context_t context)
464{
Chris Austenac4604a2015-10-13 12:43:27 -0500465 sensor_data_t *reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500466
Chris Austen0012e9b2015-10-22 01:37:46 -0500467 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500468
Tom Josephbe703f72017-03-09 12:34:35 +0530469 /*
470 * This would support the Set Sensor Reading command for the presence
471 * and functional state of Processor, Core & DIMM. For the remaining
472 * sensors the existing support is invoked.
473 */
474 auto ipmiRC = setSensorReading(request);
475
476 if(ipmiRC == IPMI_CC_SENSOR_INVALID)
477 {
478 updateSensorRecordFromSSRAESC(reqptr);
479 ipmiRC = IPMI_CC_OK;
480 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500481
Chris Austenac4604a2015-10-13 12:43:27 -0500482 *data_len=0;
Tom Josephbe703f72017-03-09 12:34:35 +0530483 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500484}
485
Chris Austen10ccc0f2015-12-10 18:27:04 -0600486
487ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
488 ipmi_request_t request, ipmi_response_t response,
489 ipmi_data_len_t data_len, ipmi_context_t context)
490{
491 sensor_data_t *reqptr = (sensor_data_t*)request;
492 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
Tom Joseph40c35b12017-09-07 02:04:42 +0530493 uint8_t type = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600494 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
495 int r;
496 dbus_interface_t a;
497 sd_bus *bus = ipmid_get_sd_bus_connection();
498 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600499 int reading = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600500 char* assertion = NULL;
Tom Josephc6162fb2018-01-25 01:55:20 +0530501 ipmi::sensor::GetSensorResponse getResponse {};
502 static constexpr auto scanningEnabledBit = 6;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600503
Chris Austen10ccc0f2015-12-10 18:27:04 -0600504 printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
505
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700506 r = find_openbmc_path(reqptr->sennum, &a);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600507
Tom Joseph40c35b12017-09-07 02:04:42 +0530508 if (r < 0)
509 {
Nan Li36deb762016-05-12 10:23:41 +0800510 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
Nan Li36deb762016-05-12 10:23:41 +0800511 }
Tom Joseph40c35b12017-09-07 02:04:42 +0530512 else
513 {
514 type = get_type_from_interface(a);
515 if(type == 0) {
516 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
517 return IPMI_CC_SENSOR_INVALID;
518 }
Nan Li36deb762016-05-12 10:23:41 +0800519
Tom Joseph40c35b12017-09-07 02:04:42 +0530520 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path,
521 a.interface);
Brad Bishop56003452016-10-05 21:49:19 -0400522 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600523
Chris Austen10ccc0f2015-12-10 18:27:04 -0600524 *data_len=0;
525
Emily Shaffer10f49592017-05-10 12:01:10 -0700526 int64_t raw_value;
Emily Shaffer1fabf222017-04-05 08:53:21 -0700527 ipmi::sensor::Info sensor;
528
Chris Austen10ccc0f2015-12-10 18:27:04 -0600529 switch(type) {
Chris Austen10ccc0f2015-12-10 18:27:04 -0600530 case 0xC2:
Dhruvaraj Subhashchandrand12ae752017-10-04 00:33:27 -0500531 case 0xC8:
Adriana Kobylak93125982016-03-01 12:48:10 -0600532 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600533 if (r < 0) {
534 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
535 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
536 a.bus, a.path, a.interface);
537 break;
538 }
539
Adriana Kobylak93125982016-03-01 12:48:10 -0600540 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600541 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600542 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600543 break;
544 }
545
546 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
547
548 rc = IPMI_CC_OK;
549 *data_len=sizeof(sensorreadingresp_t);
550
Adriana Kobylak93125982016-03-01 12:48:10 -0600551 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600552 resp->operation = 0;
553 resp->indication[0] = 0;
554 resp->indication[1] = 0;
555 break;
556
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600557 //TODO openbmc/openbmc#2154 Move this sensor to right place.
558 case 0xCA:
559 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "s");
560 if (r < 0) {
561 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
562 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
563 a.bus, a.path, a.interface);
564 break;
565 }
566
567 r = sd_bus_message_read(reply, "s", &assertion);
568 if (r < 0) {
569 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
570 break;
571 }
572
573 rc = IPMI_CC_OK;
574 *data_len=sizeof(sensorreadingresp_t);
575
576 resp->value = 0;
577 resp->operation = 0;
578 if (strcmp(assertion,"Enabled") == 0)
579 {
580 resp->indication[0] = 0x02;
581 }
582 else
583 {
584 resp->indication[0] = 0x1;
585 }
586 resp->indication[1] = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600587 break;
588
Emily Shaffer1fabf222017-04-05 08:53:21 -0700589 case IPMI_SENSOR_TEMP:
590 case IPMI_SENSOR_VOLTAGE:
591 case IPMI_SENSOR_CURRENT:
592 case IPMI_SENSOR_FAN:
593 // Get reading for /xyz/openbmc_project/Sensor/Value.interface
Emily Shaffer10f49592017-05-10 12:01:10 -0700594 if(sensors.find(reqptr->sennum) == sensors.end())
595 {
596 fprintf(stderr, "Failed to find config entry for Sensor 0x%02x\n",
597 reqptr->sennum);
598 return IPMI_CC_SENSOR_INVALID;
599 }
600
601 sensor = sensors.at(reqptr->sennum);
Emily Shaffercc941e12017-06-14 13:06:26 -0700602 if (ipmi::sensor::Mutability::Read !=
603 (sensor.mutability & ipmi::sensor::Mutability::Read))
604 {
605 log<level::ERR>("Sensor was not readable.\n");
606 return IPMI_CC_SENSOR_INVALID;
607 }
608
Emily Shaffer1fabf222017-04-05 08:53:21 -0700609
610 // Get value
611 r = sd_bus_get_property_trivial(bus,
612 a.bus,
613 a.path,
614 a.interface,
615 "Value",
616 NULL,
617 'x',
618 &raw_value);
619 if (r < 0) {
620 fprintf(stderr,
621 "Failed to call sd_bus_get_property:%d, %s, 'value'\n",
622 r,
623 strerror(-r));
624 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
625 a.bus, a.path, a.interface);
626 break;
627 }
628
Emily Shaffer10f49592017-05-10 12:01:10 -0700629 // Prevent div0
630 if (sensor.coefficientM == 0) {
631 sensor.coefficientM = 1;
632 };
Emily Shaffer1fabf222017-04-05 08:53:21 -0700633
Emily Shaffer10f49592017-05-10 12:01:10 -0700634 resp->value = static_cast<uint8_t>(
635 (raw_value - sensor.scaledOffset) / sensor.coefficientM);
Tom Josephc6162fb2018-01-25 01:55:20 +0530636 resp->operation = 1 << scanningEnabledBit; // scanning enabled
Emily Shafferbbef71c2017-05-08 16:36:17 -0700637 resp->indication[0] = 0; // not a threshold sensor. ignore
Emily Shaffer1fabf222017-04-05 08:53:21 -0700638 resp->indication[1] = 0;
639 rc = IPMI_CC_OK;
640 *data_len=sizeof(sensorreadingresp_t);
641 break;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600642 default:
Tom Joseph14c15462017-09-07 02:16:51 +0530643 {
644 const auto iter = sensors.find(reqptr->sennum);
645 if (iter == sensors.end())
646 {
647 return IPMI_CC_SENSOR_INVALID;
648 }
649
650 try
651 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530652 getResponse = iter->second.getFunc(iter->second);
Tom Joseph14c15462017-09-07 02:16:51 +0530653 *data_len = getResponse.size();
654 memcpy(resp, getResponse.data(), *data_len);
Tom Josephc6162fb2018-01-25 01:55:20 +0530655 resp->operation = 1 << scanningEnabledBit;
Tom Joseph14c15462017-09-07 02:16:51 +0530656 return IPMI_CC_OK;
657 }
658 catch (InternalFailure& e)
659 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530660 *data_len = getResponse.size();
661 memcpy(resp, getResponse.data(), *data_len);
662 return IPMI_CC_OK;
Tom Joseph14c15462017-09-07 02:16:51 +0530663 }
Tom Joseph82024322017-09-28 20:07:29 +0530664 catch (const std::runtime_error& e)
665 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530666 *data_len = getResponse.size();
667 memcpy(resp, getResponse.data(), *data_len);
668 return IPMI_CC_OK;
Tom Joseph82024322017-09-28 20:07:29 +0530669 }
Tom Joseph14c15462017-09-07 02:16:51 +0530670 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600671 }
672
vishwa1eaea4f2016-02-26 11:57:40 -0600673 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600674
675 return rc;
676}
677
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600678ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
679 ipmi_request_t request, ipmi_response_t response,
680 ipmi_data_len_t data_len, ipmi_context_t context)
681{
682 constexpr auto warningThresholdInterface =
683 "xyz.openbmc_project.Sensor.Threshold.Warning";
684 constexpr auto criticalThresholdInterface =
685 "xyz.openbmc_project.Sensor.Threshold.Critical";
686 constexpr auto valueInterface =
687 "xyz.openbmc_project.Sensor.Value";
688 constexpr auto sensorRoot = "/xyz/openbmc_project/sensors";
689
690 ipmi::sensor::Thresholds thresholds =
691 {
692 {
693 warningThresholdInterface,
694 {
695 {
696 "WarningLow",
697 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK,
698 ipmi::sensor::ThresholdIndex::NON_CRITICAL_LOW_IDX
699 },
700 {
701 "WarningHigh",
702 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK,
703 ipmi::sensor::ThresholdIndex::NON_CRITICAL_HIGH_IDX
704 }
705 }
706 },
707 {
708 criticalThresholdInterface,
709 {
710 {
711 "CriticalLow",
712 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK,
713 ipmi::sensor::ThresholdIndex::CRITICAL_LOW_IDX
714 },
715 {
716 "CriticalHigh",
717 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK,
718 ipmi::sensor::ThresholdIndex::CRITICAL_HIGH_IDX
719 }
720 }
721 }
722 };
723
724 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
725
726 if (*data_len != sizeof(uint8_t))
727 {
728 return IPMI_CC_REQ_DATA_LEN_INVALID;
729 }
730 auto sensorNum = *(reinterpret_cast<uint8_t*>(request));
731
732 auto responseData =
733 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
734
735 responseData->validMask = 0;
736
737 const auto iter = sensors.find(sensorNum);
738 if (iter == sensors.end())
739 {
740 return IPMI_CC_SENSOR_INVALID;
741 }
742
743 const auto sensorInfo = iter->second;
744
745 //Proceed only if the sensor value interface is implemented.
746 if (sensorInfo.propertyInterfaces.find(valueInterface) ==
747 sensorInfo.propertyInterfaces.end())
748 {
749 //return with valid mask as 0
750 return IPMI_CC_OK;
751 }
752
753 std::string service;
754 try
755 {
756 service = ipmi::getService(bus,
757 sensorInfo.sensorInterface,
758 sensorInfo.sensorPath);
759 }
760 catch (const std::runtime_error& e)
761 {
762 log<level::ERR>(e.what());
763 return IPMI_CC_UNSPECIFIED_ERROR;
764 }
765
766 //prevent divide by 0
767 auto coefficientM =
768 sensorInfo.coefficientM ? sensorInfo.coefficientM : 1;
769
770 try
771 {
772 auto mngObjects = ipmi::getManagedObjects(bus,
773 service,
774 sensorRoot);
775
776 auto senIter = mngObjects.find(sensorInfo.sensorPath);
777 if (senIter == mngObjects.end())
778 {
779 return IPMI_CC_SENSOR_INVALID;
780 }
781
782 for (const auto& threshold : thresholds)
783 {
784 auto thresholdType = senIter->second.find(threshold.first);
785 if (thresholdType != senIter->second.end())
786 {
787 for (const auto& threshLevel : threshold.second)
788 {
789 auto val = thresholdType->
790 second[threshLevel.property].get<int64_t>();
791 if (val != 0)
792 {
793 auto idx = static_cast<uint8_t>(threshLevel.idx);
794 responseData->data[idx] = static_cast<uint8_t>(
795 (val - sensorInfo.scaledOffset) / coefficientM);
796 responseData->validMask |=
797 static_cast<uint8_t>(threshLevel.maskValue);
798 }
799 }
800 }
801 }
802 }
803 catch (InternalFailure& e)
804 {
805 //Not able to get the values, reset the mask.
806 responseData->validMask = 0;
807 }
808
809 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
810 return IPMI_CC_OK;
811}
812
Chris Austen0012e9b2015-10-22 01:37:46 -0500813ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
814 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500815 ipmi_data_len_t data_len, ipmi_context_t context)
816{
Nan Li70aa8d92016-08-29 00:11:10 +0800817 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500818
819 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
820 *data_len = 0;
821
822 return rc;
823}
824
Emily Shafferd06e0e72017-04-05 09:08:57 -0700825ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
826 ipmi_request_t request,
827 ipmi_response_t response,
828 ipmi_data_len_t data_len,
829 ipmi_context_t context)
830{
831 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
832 if (request == nullptr ||
833 get_sdr_info::request::get_count(request) == false)
834 {
835 // Get Sensor Count
Ratan Guptae0cc8552018-01-22 14:23:04 +0530836 resp->count = sensors.size() + frus.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700837 }
838 else
839 {
840 resp->count = 1;
841 }
842
843 // Multiple LUNs not supported.
844 namespace response = get_sdr_info::response;
845 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
846 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
847 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
848 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
849 response::set_static_population(&(resp->luns_and_dynamic_population));
850
851 *data_len = SDR_INFO_RESP_SIZE;
852
853 return IPMI_CC_OK;
854}
855
Emily Shaffera344afc2017-04-13 15:09:39 -0700856ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
857 ipmi_request_t request,
858 ipmi_response_t response,
859 ipmi_data_len_t data_len,
860 ipmi_context_t context)
861{
862 // A constant reservation ID is okay until we implement add/remove SDR.
863 const uint16_t reservation_id = 1;
864 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700865 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700866
867 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
868 return IPMI_CC_OK;
869}
Chris Austenac4604a2015-10-13 12:43:27 -0500870
Emily Shaffercc941e12017-06-14 13:06:26 -0700871void setUnitFieldsForObject(sd_bus *bus,
872 const dbus_interface_t &iface,
873 const ipmi::sensor::Info *info,
874 get_sdr::SensorDataFullRecordBody *body)
875{
876 if (info->propertyInterfaces.begin()->first ==
877 "xyz.openbmc_project.Sensor.Value")
878 {
879 std::string result {};
Emily Shaffer62235612017-06-14 13:06:26 -0700880 if (info->unit.empty())
Emily Shaffercc941e12017-06-14 13:06:26 -0700881 {
Emily Shaffer62235612017-06-14 13:06:26 -0700882 char *raw_cstr = NULL;
883 if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path,
884 iface.interface, "Unit", NULL,
885 &raw_cstr))
886 {
887 log<level::WARNING>("Unit interface missing.",
888 entry("BUS=%s", iface.bus),
889 entry("PATH=%s", iface.path));
890 }
891 else
892 {
893 result = raw_cstr;
894 }
895 free(raw_cstr);
Emily Shaffercc941e12017-06-14 13:06:26 -0700896 }
897 else
898 {
Emily Shaffer62235612017-06-14 13:06:26 -0700899 result = info->unit;
Emily Shaffercc941e12017-06-14 13:06:26 -0700900 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700901
902 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
903 try {
904 auto unit = server::Value::convertUnitFromString(result);
905 // Unit strings defined in
906 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
907 switch (unit)
908 {
909 case server::Value::Unit::DegreesC:
910 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
911 break;
912 case server::Value::Unit::RPMS:
913 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
914 get_sdr::body::set_rate_unit(0b100, body); // per minute
915 break;
916 case server::Value::Unit::Volts:
917 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
918 break;
919 case server::Value::Unit::Meters:
920 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
921 break;
922 case server::Value::Unit::Amperes:
923 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
924 break;
925 case server::Value::Unit::Joules:
926 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
927 break;
928 default:
929 // Cannot be hit.
930 fprintf(stderr, "Unknown value unit type: = %s\n", result.c_str());
931 }
932 }
933 catch (sdbusplus::exception::InvalidEnumString e)
934 {
935 log<level::WARNING>("Warning: no unit provided for sensor!");
936 }
937 }
938}
939
940int64_t getScaleForObject(sd_bus *bus,
941 const dbus_interface_t& iface,
942 const ipmi::sensor::Info *info)
943{
944 int64_t result = 0;
945 if (info->propertyInterfaces.begin()->first ==
946 "xyz.openbmc_project.Sensor.Value")
947 {
Emily Shaffer62235612017-06-14 13:06:26 -0700948 if (info->hasScale)
949 {
950 result = info->scale;
951 }
952 else
953 {
954 if (0 > sd_bus_get_property_trivial(bus,
955 iface.bus,
956 iface.path,
957 iface.interface,
958 "Scale",
959 NULL,
960 'x',
961 &result)) {
962 log<level::WARNING>("Scale interface missing.",
963 entry("BUS=%s", iface.bus),
964 entry("PATH=%s", iface.path));
965 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700966 }
967 }
968
969 return result;
970}
971
Emily Shafferbbef71c2017-05-08 16:36:17 -0700972ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body,
973 const ipmi::sensor::Info *info,
974 ipmi_data_len_t data_len)
975{
976 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700977 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700978 {
979 // Get bus
980 sd_bus *bus = ipmid_get_sd_bus_connection();
981 dbus_interface_t iface;
982
983 if (0 > find_openbmc_path(body->entity_id, &iface))
984 return IPMI_CC_SENSOR_INVALID;
985
986 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
987
988 /* Unit info */
Emily Shaffercc941e12017-06-14 13:06:26 -0700989 setUnitFieldsForObject(bus, iface, info, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700990
991 /* Modifiers to reading info */
Emily Shaffer10f49592017-05-10 12:01:10 -0700992 // Get scale
Emily Shaffercc941e12017-06-14 13:06:26 -0700993 int64_t scale = getScaleForObject(bus, iface, info);
Emily Shaffer10f49592017-05-10 12:01:10 -0700994
995 get_sdr::body::set_b(info->coefficientB, body);
996 get_sdr::body::set_m(info->coefficientM, body);
997 get_sdr::body::set_b_exp(info->exponentB, body);
998 get_sdr::body::set_r_exp(scale, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700999
Emily Shafferbbef71c2017-05-08 16:36:17 -07001000 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -07001001 }
1002
Tom Joseph96423912018-01-25 00:14:34 +05301003 /* ID string */
1004 auto id_string = info->sensorNameFunc(*info);
1005
1006 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
1007 {
1008 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
1009 }
1010 else
1011 {
1012 get_sdr::body::set_id_strlen(id_string.length(), body);
1013 }
1014 strncpy(body->id_string, id_string.c_str(),
1015 get_sdr::body::get_id_strlen(body));
1016
Emily Shafferbbef71c2017-05-08 16:36:17 -07001017 return IPMI_CC_OK;
1018};
1019
Ratan Guptae0cc8552018-01-22 14:23:04 +05301020ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
1021 ipmi_data_len_t data_len)
1022{
1023 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
1024 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
1025 get_sdr::SensorDataFruRecord record {};
1026 auto dataLength = 0;
1027
1028 auto fru = frus.begin();
1029 uint8_t fruID {};
1030 auto recordID = get_sdr::request::get_record_id(req);
1031
1032 fruID = recordID - FRU_RECORD_ID_START;
1033 fru = frus.find(fruID);
1034 if (fru == frus.end())
1035 {
1036 return IPMI_CC_SENSOR_INVALID;
1037 }
1038
1039 /* Header */
1040 get_sdr::header::set_record_id(recordID, &(record.header));
1041 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
1042 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
1043 record.header.record_length = sizeof(record.key) + sizeof(record.body);
1044
1045 /* Key */
1046 record.key.fruID = fruID;
1047 record.key.accessLun |= IPMI_LOGICAL_FRU;
1048 record.key.deviceAddress = BMCSlaveAddress;
1049
1050 /* Body */
1051 record.body.entityID = fru->second[0].entityID;
1052 record.body.entityInstance = fru->second[0].entityInstance;
1053 record.body.deviceType = fruInventoryDevice;
1054 record.body.deviceTypeModifier = IPMIFruInventory;
1055
1056 /* Device ID string */
1057 auto deviceID = fru->second[0].path.substr(
1058 fru->second[0].path.find_last_of('/') + 1,
1059 fru->second[0].path.length());
1060
1061
1062 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
1063 {
1064 get_sdr::body::set_device_id_strlen(
1065 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH,
1066 &(record.body));
1067 }
1068 else
1069 {
1070 get_sdr::body::set_device_id_strlen(deviceID.length(),
1071 &(record.body));
1072 }
1073
1074 strncpy(record.body.deviceID, deviceID.c_str(),
1075 get_sdr::body::get_device_id_strlen(&(record.body)));
1076
1077 if (++fru == frus.end())
1078 {
1079 get_sdr::response::set_next_record_id(END_OF_RECORD, resp); // last record
1080 }
1081 else
1082 {
1083 get_sdr::response::set_next_record_id(
1084 (FRU_RECORD_ID_START + fru->first), resp);
1085 }
1086
1087 if (req->bytes_to_read > (sizeof(*resp) - req->offset))
1088 {
1089 dataLength = (sizeof(*resp) - req->offset);
1090 }
1091 else
1092 {
1093 dataLength = req->bytes_to_read;
1094 }
1095
1096 if (dataLength <= 0)
1097 {
1098 return IPMI_CC_REQ_DATA_LEN_INVALID;
1099 }
1100
1101 memcpy(resp->record_data,
1102 reinterpret_cast<uint8_t*>(&record) + req->offset,
1103 (dataLength));
1104
1105 *data_len = dataLength;
1106 *data_len += 2; // additional 2 bytes for next record ID
1107
1108 return IPMI_CC_OK;
1109}
1110
Emily Shafferbbef71c2017-05-08 16:36:17 -07001111ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1112 ipmi_request_t request, ipmi_response_t response,
1113 ipmi_data_len_t data_len, ipmi_context_t context)
1114{
1115 ipmi_ret_t ret = IPMI_CC_OK;
1116 get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request;
1117 get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response;
1118 get_sdr::SensorDataFullRecord record = {0};
1119 if (req != NULL)
1120 {
1121 // Note: we use an iterator so we can provide the next ID at the end of
1122 // the call.
1123 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +05301124 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001125
1126 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +05301127 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -07001128 {
Ratan Guptae0cc8552018-01-22 14:23:04 +05301129 // recordID greater then 255,it means it is a FRU record.
1130 // Currently we are supporting two record types either FULL record
1131 // or FRU record.
1132 if (recordID >= FRU_RECORD_ID_START)
1133 {
1134 return ipmi_fru_get_sdr(request, response, data_len);
1135 }
1136 else
1137 {
1138 sensor = sensors.find(recordID);
1139 if (sensor == sensors.end())
1140 {
1141 return IPMI_CC_SENSOR_INVALID;
1142 }
Emily Shafferbbef71c2017-05-08 16:36:17 -07001143 }
1144 }
1145
1146 uint8_t sensor_id = sensor->first;
1147
1148 /* Header */
1149 get_sdr::header::set_record_id(sensor_id, &(record.header));
1150 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
1151 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
1152 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
1153
1154 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +05301155 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -07001156 record.key.sensor_number = sensor_id;
1157
1158 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +05301159 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001160 record.body.sensor_type = sensor->second.sensorType;
1161 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +05301162 record.body.entity_instance = sensor->second.instance;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001163
1164 // Set the type-specific details given the DBus interface
1165 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
1166 data_len);
1167
1168 if (++sensor == sensors.end())
1169 {
Ratan Guptae0cc8552018-01-22 14:23:04 +05301170 // we have reached till end of sensor, so assign the next record id
1171 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
1172 auto next_record_id = (frus.size()) ?
1173 frus.begin()->first + FRU_RECORD_ID_START :
1174 END_OF_RECORD;
1175
1176 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001177 }
1178 else
1179 {
1180 get_sdr::response::set_next_record_id(sensor->first, resp);
1181 }
1182
1183 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
1184 memcpy(resp->record_data, (char*)&record + req->offset,
1185 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
1186 }
1187
1188 return ret;
1189}
1190
1191
Chris Austenac4604a2015-10-13 12:43:27 -05001192void register_netfn_sen_functions()
1193{
Tom05732372016-09-06 17:21:23 +05301194 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -07001195 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1196 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
1197 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
1198 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +05301199 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001200
Tom05732372016-09-06 17:21:23 +05301201 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -07001202 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1203 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
1204 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
1205 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +05301206 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001207
Tom05732372016-09-06 17:21:23 +05301208 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -07001209 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1210 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
1211 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
1212 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +05301213 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -05001214
Tom05732372016-09-06 17:21:23 +05301215 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -07001216 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1217 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
1218 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
1219 nullptr, ipmi_sen_get_sensor_reading,
1220 PRIVILEGE_USER);
1221
1222 // <Reserve SDR>
1223 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1224 NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO);
1225 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO,
1226 nullptr, ipmi_sen_reserve_sdr,
1227 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001228
Emily Shafferd06e0e72017-04-05 09:08:57 -07001229 // <Get SDR Info>
Emily Shaffera344afc2017-04-13 15:09:39 -07001230 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
1231 NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
1232 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO,
1233 nullptr, ipmi_sen_get_sdr_info,
1234 PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001235
1236 // <Get SDR>
1237 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
1238 NETFUN_SENSOR, IPMI_CMD_GET_SDR);
1239 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR,
1240 nullptr, ipmi_sen_get_sdr,
1241 PRIVILEGE_USER);
1242
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001243 // <Get Sensor Thresholds>
1244 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1245 nullptr, ipmi_sen_get_sensor_thresholds,
1246 PRIVILEGE_USER);
1247
Chris Austenac4604a2015-10-13 12:43:27 -05001248 return;
1249}