blob: e52ab69ba5aad0c177b6b341e2aea8431870bbb7 [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
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530678void getSensorThresholds(uint8_t sensorNum,
679 get_sdr::GetSensorThresholdsResponse* response)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600680{
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530681 constexpr auto warningThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600682 "xyz.openbmc_project.Sensor.Threshold.Warning";
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530683 constexpr auto criticalThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600684 "xyz.openbmc_project.Sensor.Threshold.Critical";
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600685
686 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
687
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530688 const auto iter = sensors.find(sensorNum);
689 const auto info = iter->second;
690
691 auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
692
693 auto warnThresholds = ipmi::getAllDbusProperties(bus,
694 service,
695 info.sensorPath,
696 warningThreshIntf);
697
698 double warnLow = warnThresholds["WarningLow"].get<int64_t>();
699 double warnHigh = warnThresholds["WarningHigh"].get<int64_t>();
700
701 if (warnLow != 0)
702 {
703 warnLow *= pow(10, info.scale - info.exponentR);
704 response->lowerNonCritical = static_cast<uint8_t>((
705 warnLow - info.scaledOffset) / info.coefficientM);
706 response->validMask |= static_cast<uint8_t>(
707 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
708 }
709
710 if (warnHigh != 0)
711 {
712 warnHigh *= pow(10, info.scale - info.exponentR);
713 response->upperNonCritical = static_cast<uint8_t>((
714 warnHigh - info.scaledOffset) / info.coefficientM);
715 response->validMask |= static_cast<uint8_t>(
716 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
717 }
718
719 auto critThresholds = ipmi::getAllDbusProperties(bus,
720 service,
721 info.sensorPath,
722 criticalThreshIntf);
723 double critLow = critThresholds["CriticalLow"].get<int64_t>();
724 double critHigh = critThresholds["CriticalHigh"].get<int64_t>();
725
726 if (critLow != 0)
727 {
728 critLow *= pow(10, info.scale - info.exponentR);
729 response->lowerCritical = static_cast<uint8_t>((
730 critLow - info.scaledOffset) / info.coefficientM);
731 response->validMask |= static_cast<uint8_t>(
732 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
733 }
734
735 if (critHigh != 0)
736 {
737 critHigh *= pow(10, info.scale - info.exponentR);
738 response->upperCritical = static_cast<uint8_t>((
739 critHigh - info.scaledOffset)/ info.coefficientM);
740 response->validMask |= static_cast<uint8_t>(
741 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
742 }
743}
744
745ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
746 ipmi_request_t request, ipmi_response_t response,
747 ipmi_data_len_t data_len, ipmi_context_t context)
748{
749 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
750
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600751 if (*data_len != sizeof(uint8_t))
752 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530753 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600754 return IPMI_CC_REQ_DATA_LEN_INVALID;
755 }
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600756
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530757 auto sensorNum = *(reinterpret_cast<const uint8_t *>(request));
758 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600759
760 const auto iter = sensors.find(sensorNum);
761 if (iter == sensors.end())
762 {
763 return IPMI_CC_SENSOR_INVALID;
764 }
765
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530766 const auto info = iter->second;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600767
768 //Proceed only if the sensor value interface is implemented.
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530769 if (info.propertyInterfaces.find(valueInterface) ==
770 info.propertyInterfaces.end())
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600771 {
772 //return with valid mask as 0
773 return IPMI_CC_OK;
774 }
775
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530776 auto responseData =
777 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600778
779 try
780 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530781 getSensorThresholds(sensorNum, responseData);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600782 }
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530783 catch (std::exception& e)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600784 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530785 //Mask if the property is not present
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600786 responseData->validMask = 0;
787 }
788
789 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
790 return IPMI_CC_OK;
791}
792
Chris Austen0012e9b2015-10-22 01:37:46 -0500793ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
794 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500795 ipmi_data_len_t data_len, ipmi_context_t context)
796{
Nan Li70aa8d92016-08-29 00:11:10 +0800797 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500798
799 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
800 *data_len = 0;
801
802 return rc;
803}
804
Emily Shafferd06e0e72017-04-05 09:08:57 -0700805ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
806 ipmi_request_t request,
807 ipmi_response_t response,
808 ipmi_data_len_t data_len,
809 ipmi_context_t context)
810{
811 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
812 if (request == nullptr ||
813 get_sdr_info::request::get_count(request) == false)
814 {
815 // Get Sensor Count
Ratan Guptae0cc8552018-01-22 14:23:04 +0530816 resp->count = sensors.size() + frus.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700817 }
818 else
819 {
820 resp->count = 1;
821 }
822
823 // Multiple LUNs not supported.
824 namespace response = get_sdr_info::response;
825 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
826 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
827 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
828 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
829 response::set_static_population(&(resp->luns_and_dynamic_population));
830
831 *data_len = SDR_INFO_RESP_SIZE;
832
833 return IPMI_CC_OK;
834}
835
Emily Shaffera344afc2017-04-13 15:09:39 -0700836ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
837 ipmi_request_t request,
838 ipmi_response_t response,
839 ipmi_data_len_t data_len,
840 ipmi_context_t context)
841{
842 // A constant reservation ID is okay until we implement add/remove SDR.
843 const uint16_t reservation_id = 1;
844 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700845 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700846
847 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
848 return IPMI_CC_OK;
849}
Chris Austenac4604a2015-10-13 12:43:27 -0500850
Emily Shaffercc941e12017-06-14 13:06:26 -0700851void setUnitFieldsForObject(sd_bus *bus,
852 const dbus_interface_t &iface,
853 const ipmi::sensor::Info *info,
854 get_sdr::SensorDataFullRecordBody *body)
855{
856 if (info->propertyInterfaces.begin()->first ==
857 "xyz.openbmc_project.Sensor.Value")
858 {
859 std::string result {};
Emily Shaffer62235612017-06-14 13:06:26 -0700860 if (info->unit.empty())
Emily Shaffercc941e12017-06-14 13:06:26 -0700861 {
Emily Shaffer62235612017-06-14 13:06:26 -0700862 char *raw_cstr = NULL;
863 if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path,
864 iface.interface, "Unit", NULL,
865 &raw_cstr))
866 {
867 log<level::WARNING>("Unit interface missing.",
868 entry("BUS=%s", iface.bus),
869 entry("PATH=%s", iface.path));
870 }
871 else
872 {
873 result = raw_cstr;
874 }
875 free(raw_cstr);
Emily Shaffercc941e12017-06-14 13:06:26 -0700876 }
877 else
878 {
Emily Shaffer62235612017-06-14 13:06:26 -0700879 result = info->unit;
Emily Shaffercc941e12017-06-14 13:06:26 -0700880 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700881
882 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
883 try {
884 auto unit = server::Value::convertUnitFromString(result);
885 // Unit strings defined in
886 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
887 switch (unit)
888 {
889 case server::Value::Unit::DegreesC:
890 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
891 break;
892 case server::Value::Unit::RPMS:
893 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
894 get_sdr::body::set_rate_unit(0b100, body); // per minute
895 break;
896 case server::Value::Unit::Volts:
897 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
898 break;
899 case server::Value::Unit::Meters:
900 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
901 break;
902 case server::Value::Unit::Amperes:
903 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
904 break;
905 case server::Value::Unit::Joules:
906 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
907 break;
908 default:
909 // Cannot be hit.
910 fprintf(stderr, "Unknown value unit type: = %s\n", result.c_str());
911 }
912 }
913 catch (sdbusplus::exception::InvalidEnumString e)
914 {
915 log<level::WARNING>("Warning: no unit provided for sensor!");
916 }
917 }
918}
919
920int64_t getScaleForObject(sd_bus *bus,
921 const dbus_interface_t& iface,
922 const ipmi::sensor::Info *info)
923{
924 int64_t result = 0;
925 if (info->propertyInterfaces.begin()->first ==
926 "xyz.openbmc_project.Sensor.Value")
927 {
Emily Shaffer62235612017-06-14 13:06:26 -0700928 if (info->hasScale)
929 {
930 result = info->scale;
931 }
932 else
933 {
934 if (0 > sd_bus_get_property_trivial(bus,
935 iface.bus,
936 iface.path,
937 iface.interface,
938 "Scale",
939 NULL,
940 'x',
941 &result)) {
942 log<level::WARNING>("Scale interface missing.",
943 entry("BUS=%s", iface.bus),
944 entry("PATH=%s", iface.path));
945 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700946 }
947 }
948
949 return result;
950}
951
Emily Shafferbbef71c2017-05-08 16:36:17 -0700952ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body,
953 const ipmi::sensor::Info *info,
954 ipmi_data_len_t data_len)
955{
956 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700957 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700958 {
959 // Get bus
960 sd_bus *bus = ipmid_get_sd_bus_connection();
961 dbus_interface_t iface;
962
963 if (0 > find_openbmc_path(body->entity_id, &iface))
964 return IPMI_CC_SENSOR_INVALID;
965
966 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
967
968 /* Unit info */
Emily Shaffercc941e12017-06-14 13:06:26 -0700969 setUnitFieldsForObject(bus, iface, info, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700970
971 /* Modifiers to reading info */
Emily Shaffer10f49592017-05-10 12:01:10 -0700972 // Get scale
Emily Shaffercc941e12017-06-14 13:06:26 -0700973 int64_t scale = getScaleForObject(bus, iface, info);
Emily Shaffer10f49592017-05-10 12:01:10 -0700974
975 get_sdr::body::set_b(info->coefficientB, body);
976 get_sdr::body::set_m(info->coefficientM, body);
977 get_sdr::body::set_b_exp(info->exponentB, body);
978 get_sdr::body::set_r_exp(scale, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700979
Emily Shafferbbef71c2017-05-08 16:36:17 -0700980 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700981 }
982
Tom Joseph96423912018-01-25 00:14:34 +0530983 /* ID string */
984 auto id_string = info->sensorNameFunc(*info);
985
986 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
987 {
988 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
989 }
990 else
991 {
992 get_sdr::body::set_id_strlen(id_string.length(), body);
993 }
994 strncpy(body->id_string, id_string.c_str(),
995 get_sdr::body::get_id_strlen(body));
996
Emily Shafferbbef71c2017-05-08 16:36:17 -0700997 return IPMI_CC_OK;
998};
999
Ratan Guptae0cc8552018-01-22 14:23:04 +05301000ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
1001 ipmi_data_len_t data_len)
1002{
1003 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
1004 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
1005 get_sdr::SensorDataFruRecord record {};
1006 auto dataLength = 0;
1007
1008 auto fru = frus.begin();
1009 uint8_t fruID {};
1010 auto recordID = get_sdr::request::get_record_id(req);
1011
1012 fruID = recordID - FRU_RECORD_ID_START;
1013 fru = frus.find(fruID);
1014 if (fru == frus.end())
1015 {
1016 return IPMI_CC_SENSOR_INVALID;
1017 }
1018
1019 /* Header */
1020 get_sdr::header::set_record_id(recordID, &(record.header));
1021 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
1022 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
1023 record.header.record_length = sizeof(record.key) + sizeof(record.body);
1024
1025 /* Key */
1026 record.key.fruID = fruID;
1027 record.key.accessLun |= IPMI_LOGICAL_FRU;
1028 record.key.deviceAddress = BMCSlaveAddress;
1029
1030 /* Body */
1031 record.body.entityID = fru->second[0].entityID;
1032 record.body.entityInstance = fru->second[0].entityInstance;
1033 record.body.deviceType = fruInventoryDevice;
1034 record.body.deviceTypeModifier = IPMIFruInventory;
1035
1036 /* Device ID string */
1037 auto deviceID = fru->second[0].path.substr(
1038 fru->second[0].path.find_last_of('/') + 1,
1039 fru->second[0].path.length());
1040
1041
1042 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
1043 {
1044 get_sdr::body::set_device_id_strlen(
1045 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH,
1046 &(record.body));
1047 }
1048 else
1049 {
1050 get_sdr::body::set_device_id_strlen(deviceID.length(),
1051 &(record.body));
1052 }
1053
1054 strncpy(record.body.deviceID, deviceID.c_str(),
1055 get_sdr::body::get_device_id_strlen(&(record.body)));
1056
1057 if (++fru == frus.end())
1058 {
1059 get_sdr::response::set_next_record_id(END_OF_RECORD, resp); // last record
1060 }
1061 else
1062 {
1063 get_sdr::response::set_next_record_id(
1064 (FRU_RECORD_ID_START + fru->first), resp);
1065 }
1066
1067 if (req->bytes_to_read > (sizeof(*resp) - req->offset))
1068 {
1069 dataLength = (sizeof(*resp) - req->offset);
1070 }
1071 else
1072 {
1073 dataLength = req->bytes_to_read;
1074 }
1075
1076 if (dataLength <= 0)
1077 {
1078 return IPMI_CC_REQ_DATA_LEN_INVALID;
1079 }
1080
1081 memcpy(resp->record_data,
1082 reinterpret_cast<uint8_t*>(&record) + req->offset,
1083 (dataLength));
1084
1085 *data_len = dataLength;
1086 *data_len += 2; // additional 2 bytes for next record ID
1087
1088 return IPMI_CC_OK;
1089}
1090
Emily Shafferbbef71c2017-05-08 16:36:17 -07001091ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1092 ipmi_request_t request, ipmi_response_t response,
1093 ipmi_data_len_t data_len, ipmi_context_t context)
1094{
1095 ipmi_ret_t ret = IPMI_CC_OK;
1096 get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request;
1097 get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response;
1098 get_sdr::SensorDataFullRecord record = {0};
1099 if (req != NULL)
1100 {
1101 // Note: we use an iterator so we can provide the next ID at the end of
1102 // the call.
1103 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +05301104 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001105
1106 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +05301107 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -07001108 {
Ratan Guptae0cc8552018-01-22 14:23:04 +05301109 // recordID greater then 255,it means it is a FRU record.
1110 // Currently we are supporting two record types either FULL record
1111 // or FRU record.
1112 if (recordID >= FRU_RECORD_ID_START)
1113 {
1114 return ipmi_fru_get_sdr(request, response, data_len);
1115 }
1116 else
1117 {
1118 sensor = sensors.find(recordID);
1119 if (sensor == sensors.end())
1120 {
1121 return IPMI_CC_SENSOR_INVALID;
1122 }
Emily Shafferbbef71c2017-05-08 16:36:17 -07001123 }
1124 }
1125
1126 uint8_t sensor_id = sensor->first;
1127
1128 /* Header */
1129 get_sdr::header::set_record_id(sensor_id, &(record.header));
1130 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
1131 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
1132 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
1133
1134 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +05301135 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -07001136 record.key.sensor_number = sensor_id;
1137
1138 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +05301139 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001140 record.body.sensor_type = sensor->second.sensorType;
1141 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +05301142 record.body.entity_instance = sensor->second.instance;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001143
1144 // Set the type-specific details given the DBus interface
1145 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
1146 data_len);
1147
1148 if (++sensor == sensors.end())
1149 {
Ratan Guptae0cc8552018-01-22 14:23:04 +05301150 // we have reached till end of sensor, so assign the next record id
1151 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
1152 auto next_record_id = (frus.size()) ?
1153 frus.begin()->first + FRU_RECORD_ID_START :
1154 END_OF_RECORD;
1155
1156 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001157 }
1158 else
1159 {
1160 get_sdr::response::set_next_record_id(sensor->first, resp);
1161 }
1162
1163 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
1164 memcpy(resp->record_data, (char*)&record + req->offset,
1165 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
1166 }
1167
1168 return ret;
1169}
1170
1171
Chris Austenac4604a2015-10-13 12:43:27 -05001172void register_netfn_sen_functions()
1173{
Tom05732372016-09-06 17:21:23 +05301174 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -07001175 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1176 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
1177 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
1178 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +05301179 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001180
Tom05732372016-09-06 17:21:23 +05301181 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -07001182 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1183 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
1184 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
1185 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +05301186 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001187
Tom05732372016-09-06 17:21:23 +05301188 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -07001189 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1190 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
1191 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
1192 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +05301193 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -05001194
Tom05732372016-09-06 17:21:23 +05301195 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -07001196 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1197 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
1198 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
1199 nullptr, ipmi_sen_get_sensor_reading,
1200 PRIVILEGE_USER);
1201
1202 // <Reserve SDR>
1203 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1204 NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO);
1205 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO,
1206 nullptr, ipmi_sen_reserve_sdr,
1207 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001208
Emily Shafferd06e0e72017-04-05 09:08:57 -07001209 // <Get SDR Info>
Emily Shaffera344afc2017-04-13 15:09:39 -07001210 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
1211 NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
1212 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO,
1213 nullptr, ipmi_sen_get_sdr_info,
1214 PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001215
1216 // <Get SDR>
1217 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
1218 NETFUN_SENSOR, IPMI_CMD_GET_SDR);
1219 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR,
1220 nullptr, ipmi_sen_get_sdr,
1221 PRIVILEGE_USER);
1222
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001223 // <Get Sensor Thresholds>
1224 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1225 nullptr, ipmi_sen_get_sensor_thresholds,
1226 PRIVILEGE_USER);
1227
Chris Austenac4604a2015-10-13 12:43:27 -05001228 return;
1229}