blob: 2bd022bf07ce86b7baafcaf071aae0775e41e91c [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
526 switch(type) {
Chris Austen10ccc0f2015-12-10 18:27:04 -0600527 case 0xC2:
Dhruvaraj Subhashchandrand12ae752017-10-04 00:33:27 -0500528 case 0xC8:
Adriana Kobylak93125982016-03-01 12:48:10 -0600529 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600530 if (r < 0) {
531 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
532 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
533 a.bus, a.path, a.interface);
534 break;
535 }
536
Adriana Kobylak93125982016-03-01 12:48:10 -0600537 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600538 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600539 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600540 break;
541 }
542
543 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
544
545 rc = IPMI_CC_OK;
546 *data_len=sizeof(sensorreadingresp_t);
547
Adriana Kobylak93125982016-03-01 12:48:10 -0600548 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600549 resp->operation = 0;
550 resp->indication[0] = 0;
551 resp->indication[1] = 0;
552 break;
553
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600554 //TODO openbmc/openbmc#2154 Move this sensor to right place.
555 case 0xCA:
556 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "s");
557 if (r < 0) {
558 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
559 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
560 a.bus, a.path, a.interface);
561 break;
562 }
563
564 r = sd_bus_message_read(reply, "s", &assertion);
565 if (r < 0) {
566 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
567 break;
568 }
569
570 rc = IPMI_CC_OK;
571 *data_len=sizeof(sensorreadingresp_t);
572
573 resp->value = 0;
574 resp->operation = 0;
575 if (strcmp(assertion,"Enabled") == 0)
576 {
577 resp->indication[0] = 0x02;
578 }
579 else
580 {
581 resp->indication[0] = 0x1;
582 }
583 resp->indication[1] = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600584 break;
585
Chris Austen10ccc0f2015-12-10 18:27:04 -0600586 default:
Tom Joseph14c15462017-09-07 02:16:51 +0530587 {
588 const auto iter = sensors.find(reqptr->sennum);
589 if (iter == sensors.end())
590 {
591 return IPMI_CC_SENSOR_INVALID;
592 }
Tom Joseph13b87a32018-02-16 09:37:43 +0530593 if (ipmi::sensor::Mutability::Read !=
594 (iter->second.mutability & ipmi::sensor::Mutability::Read))
595 {
596 return IPMI_CC_SENSOR_INVALID;
597 }
Tom Joseph14c15462017-09-07 02:16:51 +0530598
599 try
600 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530601 getResponse = iter->second.getFunc(iter->second);
Tom Joseph14c15462017-09-07 02:16:51 +0530602 *data_len = getResponse.size();
603 memcpy(resp, getResponse.data(), *data_len);
Tom Josephc6162fb2018-01-25 01:55:20 +0530604 resp->operation = 1 << scanningEnabledBit;
Tom Joseph14c15462017-09-07 02:16:51 +0530605 return IPMI_CC_OK;
606 }
607 catch (InternalFailure& e)
608 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530609 *data_len = getResponse.size();
610 memcpy(resp, getResponse.data(), *data_len);
611 return IPMI_CC_OK;
Tom Joseph14c15462017-09-07 02:16:51 +0530612 }
Tom Joseph82024322017-09-28 20:07:29 +0530613 catch (const std::runtime_error& e)
614 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530615 *data_len = getResponse.size();
616 memcpy(resp, getResponse.data(), *data_len);
617 return IPMI_CC_OK;
Tom Joseph82024322017-09-28 20:07:29 +0530618 }
Tom Joseph14c15462017-09-07 02:16:51 +0530619 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600620 }
621
vishwa1eaea4f2016-02-26 11:57:40 -0600622 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600623
624 return rc;
625}
626
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530627void getSensorThresholds(uint8_t sensorNum,
628 get_sdr::GetSensorThresholdsResponse* response)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600629{
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530630 constexpr auto warningThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600631 "xyz.openbmc_project.Sensor.Threshold.Warning";
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530632 constexpr auto criticalThreshIntf =
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600633 "xyz.openbmc_project.Sensor.Threshold.Critical";
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600634
635 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
636
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530637 const auto iter = sensors.find(sensorNum);
638 const auto info = iter->second;
639
640 auto service = ipmi::getService(bus, info.sensorInterface, info.sensorPath);
641
642 auto warnThresholds = ipmi::getAllDbusProperties(bus,
643 service,
644 info.sensorPath,
645 warningThreshIntf);
646
647 double warnLow = warnThresholds["WarningLow"].get<int64_t>();
648 double warnHigh = warnThresholds["WarningHigh"].get<int64_t>();
649
650 if (warnLow != 0)
651 {
652 warnLow *= pow(10, info.scale - info.exponentR);
653 response->lowerNonCritical = static_cast<uint8_t>((
654 warnLow - info.scaledOffset) / info.coefficientM);
655 response->validMask |= static_cast<uint8_t>(
656 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK);
657 }
658
659 if (warnHigh != 0)
660 {
661 warnHigh *= pow(10, info.scale - info.exponentR);
662 response->upperNonCritical = static_cast<uint8_t>((
663 warnHigh - info.scaledOffset) / info.coefficientM);
664 response->validMask |= static_cast<uint8_t>(
665 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK);
666 }
667
668 auto critThresholds = ipmi::getAllDbusProperties(bus,
669 service,
670 info.sensorPath,
671 criticalThreshIntf);
672 double critLow = critThresholds["CriticalLow"].get<int64_t>();
673 double critHigh = critThresholds["CriticalHigh"].get<int64_t>();
674
675 if (critLow != 0)
676 {
677 critLow *= pow(10, info.scale - info.exponentR);
678 response->lowerCritical = static_cast<uint8_t>((
679 critLow - info.scaledOffset) / info.coefficientM);
680 response->validMask |= static_cast<uint8_t>(
681 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK);
682 }
683
684 if (critHigh != 0)
685 {
686 critHigh *= pow(10, info.scale - info.exponentR);
687 response->upperCritical = static_cast<uint8_t>((
688 critHigh - info.scaledOffset)/ info.coefficientM);
689 response->validMask |= static_cast<uint8_t>(
690 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK);
691 }
692}
693
694ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
695 ipmi_request_t request, ipmi_response_t response,
696 ipmi_data_len_t data_len, ipmi_context_t context)
697{
698 constexpr auto valueInterface = "xyz.openbmc_project.Sensor.Value";
699
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600700 if (*data_len != sizeof(uint8_t))
701 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530702 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600703 return IPMI_CC_REQ_DATA_LEN_INVALID;
704 }
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600705
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530706 auto sensorNum = *(reinterpret_cast<const uint8_t *>(request));
707 *data_len = 0;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600708
709 const auto iter = sensors.find(sensorNum);
710 if (iter == sensors.end())
711 {
712 return IPMI_CC_SENSOR_INVALID;
713 }
714
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530715 const auto info = iter->second;
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600716
717 //Proceed only if the sensor value interface is implemented.
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530718 if (info.propertyInterfaces.find(valueInterface) ==
719 info.propertyInterfaces.end())
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600720 {
721 //return with valid mask as 0
722 return IPMI_CC_OK;
723 }
724
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530725 auto responseData =
726 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600727
728 try
729 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530730 getSensorThresholds(sensorNum, responseData);
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600731 }
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530732 catch (std::exception& e)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600733 {
Tom Joseph0ac0dd22018-02-16 09:14:45 +0530734 //Mask if the property is not present
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600735 responseData->validMask = 0;
736 }
737
738 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
739 return IPMI_CC_OK;
740}
741
Chris Austen0012e9b2015-10-22 01:37:46 -0500742ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
743 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500744 ipmi_data_len_t data_len, ipmi_context_t context)
745{
Nan Li70aa8d92016-08-29 00:11:10 +0800746 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500747
748 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
749 *data_len = 0;
750
751 return rc;
752}
753
Emily Shafferd06e0e72017-04-05 09:08:57 -0700754ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
755 ipmi_request_t request,
756 ipmi_response_t response,
757 ipmi_data_len_t data_len,
758 ipmi_context_t context)
759{
760 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
761 if (request == nullptr ||
762 get_sdr_info::request::get_count(request) == false)
763 {
764 // Get Sensor Count
Ratan Guptae0cc8552018-01-22 14:23:04 +0530765 resp->count = sensors.size() + frus.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700766 }
767 else
768 {
769 resp->count = 1;
770 }
771
772 // Multiple LUNs not supported.
773 namespace response = get_sdr_info::response;
774 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
775 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
776 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
777 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
778 response::set_static_population(&(resp->luns_and_dynamic_population));
779
780 *data_len = SDR_INFO_RESP_SIZE;
781
782 return IPMI_CC_OK;
783}
784
Emily Shaffera344afc2017-04-13 15:09:39 -0700785ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
786 ipmi_request_t request,
787 ipmi_response_t response,
788 ipmi_data_len_t data_len,
789 ipmi_context_t context)
790{
791 // A constant reservation ID is okay until we implement add/remove SDR.
792 const uint16_t reservation_id = 1;
793 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700794 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700795
796 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
797 return IPMI_CC_OK;
798}
Chris Austenac4604a2015-10-13 12:43:27 -0500799
Tom Josephdc212b22018-02-16 09:59:57 +0530800void setUnitFieldsForObject(const ipmi::sensor::Info *info,
Emily Shaffercc941e12017-06-14 13:06:26 -0700801 get_sdr::SensorDataFullRecordBody *body)
802{
Tom Josephdc212b22018-02-16 09:59:57 +0530803 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
804 try
Emily Shaffercc941e12017-06-14 13:06:26 -0700805 {
Tom Josephdc212b22018-02-16 09:59:57 +0530806 auto unit = server::Value::convertUnitFromString(info->unit);
807 // Unit strings defined in
808 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
809 switch (unit)
Emily Shaffercc941e12017-06-14 13:06:26 -0700810 {
Tom Josephdc212b22018-02-16 09:59:57 +0530811 case server::Value::Unit::DegreesC:
812 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
813 break;
814 case server::Value::Unit::RPMS:
815 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
816 get_sdr::body::set_rate_unit(0b100, body); // per minute
817 break;
818 case server::Value::Unit::Volts:
819 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
820 break;
821 case server::Value::Unit::Meters:
822 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
823 break;
824 case server::Value::Unit::Amperes:
825 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
826 break;
827 case server::Value::Unit::Joules:
828 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
829 break;
830 case server::Value::Unit::Watts:
831 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
832 break;
833 default:
834 // Cannot be hit.
835 fprintf(stderr, "Unknown value unit type: = %s\n",
836 info->unit.c_str());
Emily Shaffercc941e12017-06-14 13:06:26 -0700837 }
838 }
Tom Josephdc212b22018-02-16 09:59:57 +0530839 catch (sdbusplus::exception::InvalidEnumString e)
Emily Shaffercc941e12017-06-14 13:06:26 -0700840 {
Tom Josephdc212b22018-02-16 09:59:57 +0530841 log<level::WARNING>("Warning: no unit provided for sensor!");
Emily Shaffercc941e12017-06-14 13:06:26 -0700842 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700843}
844
Emily Shafferbbef71c2017-05-08 16:36:17 -0700845ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body,
846 const ipmi::sensor::Info *info,
847 ipmi_data_len_t data_len)
848{
849 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700850 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700851 {
Emily Shafferbbef71c2017-05-08 16:36:17 -0700852
853 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
854
855 /* Unit info */
Tom Josephdc212b22018-02-16 09:59:57 +0530856 setUnitFieldsForObject(info, body);
Emily Shaffer10f49592017-05-10 12:01:10 -0700857
858 get_sdr::body::set_b(info->coefficientB, body);
859 get_sdr::body::set_m(info->coefficientM, body);
860 get_sdr::body::set_b_exp(info->exponentB, body);
Tom Josephdc212b22018-02-16 09:59:57 +0530861 get_sdr::body::set_r_exp(info->exponentR, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700862
Emily Shafferbbef71c2017-05-08 16:36:17 -0700863 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700864 }
865
Tom Joseph96423912018-01-25 00:14:34 +0530866 /* ID string */
867 auto id_string = info->sensorNameFunc(*info);
868
869 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
870 {
871 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
872 }
873 else
874 {
875 get_sdr::body::set_id_strlen(id_string.length(), body);
876 }
877 strncpy(body->id_string, id_string.c_str(),
878 get_sdr::body::get_id_strlen(body));
879
Emily Shafferbbef71c2017-05-08 16:36:17 -0700880 return IPMI_CC_OK;
881};
882
Ratan Guptae0cc8552018-01-22 14:23:04 +0530883ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
884 ipmi_data_len_t data_len)
885{
886 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
887 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
888 get_sdr::SensorDataFruRecord record {};
889 auto dataLength = 0;
890
891 auto fru = frus.begin();
892 uint8_t fruID {};
893 auto recordID = get_sdr::request::get_record_id(req);
894
895 fruID = recordID - FRU_RECORD_ID_START;
896 fru = frus.find(fruID);
897 if (fru == frus.end())
898 {
899 return IPMI_CC_SENSOR_INVALID;
900 }
901
902 /* Header */
903 get_sdr::header::set_record_id(recordID, &(record.header));
904 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
905 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
906 record.header.record_length = sizeof(record.key) + sizeof(record.body);
907
908 /* Key */
909 record.key.fruID = fruID;
910 record.key.accessLun |= IPMI_LOGICAL_FRU;
911 record.key.deviceAddress = BMCSlaveAddress;
912
913 /* Body */
914 record.body.entityID = fru->second[0].entityID;
915 record.body.entityInstance = fru->second[0].entityInstance;
916 record.body.deviceType = fruInventoryDevice;
917 record.body.deviceTypeModifier = IPMIFruInventory;
918
919 /* Device ID string */
920 auto deviceID = fru->second[0].path.substr(
921 fru->second[0].path.find_last_of('/') + 1,
922 fru->second[0].path.length());
923
924
925 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
926 {
927 get_sdr::body::set_device_id_strlen(
928 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH,
929 &(record.body));
930 }
931 else
932 {
933 get_sdr::body::set_device_id_strlen(deviceID.length(),
934 &(record.body));
935 }
936
937 strncpy(record.body.deviceID, deviceID.c_str(),
938 get_sdr::body::get_device_id_strlen(&(record.body)));
939
940 if (++fru == frus.end())
941 {
942 get_sdr::response::set_next_record_id(END_OF_RECORD, resp); // last record
943 }
944 else
945 {
946 get_sdr::response::set_next_record_id(
947 (FRU_RECORD_ID_START + fru->first), resp);
948 }
949
950 if (req->bytes_to_read > (sizeof(*resp) - req->offset))
951 {
952 dataLength = (sizeof(*resp) - req->offset);
953 }
954 else
955 {
956 dataLength = req->bytes_to_read;
957 }
958
959 if (dataLength <= 0)
960 {
961 return IPMI_CC_REQ_DATA_LEN_INVALID;
962 }
963
964 memcpy(resp->record_data,
965 reinterpret_cast<uint8_t*>(&record) + req->offset,
966 (dataLength));
967
968 *data_len = dataLength;
969 *data_len += 2; // additional 2 bytes for next record ID
970
971 return IPMI_CC_OK;
972}
973
Emily Shafferbbef71c2017-05-08 16:36:17 -0700974ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
975 ipmi_request_t request, ipmi_response_t response,
976 ipmi_data_len_t data_len, ipmi_context_t context)
977{
978 ipmi_ret_t ret = IPMI_CC_OK;
979 get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request;
980 get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response;
981 get_sdr::SensorDataFullRecord record = {0};
982 if (req != NULL)
983 {
984 // Note: we use an iterator so we can provide the next ID at the end of
985 // the call.
986 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +0530987 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700988
989 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +0530990 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700991 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530992 // recordID greater then 255,it means it is a FRU record.
993 // Currently we are supporting two record types either FULL record
994 // or FRU record.
995 if (recordID >= FRU_RECORD_ID_START)
996 {
997 return ipmi_fru_get_sdr(request, response, data_len);
998 }
999 else
1000 {
1001 sensor = sensors.find(recordID);
1002 if (sensor == sensors.end())
1003 {
1004 return IPMI_CC_SENSOR_INVALID;
1005 }
Emily Shafferbbef71c2017-05-08 16:36:17 -07001006 }
1007 }
1008
1009 uint8_t sensor_id = sensor->first;
1010
1011 /* Header */
1012 get_sdr::header::set_record_id(sensor_id, &(record.header));
1013 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
1014 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
1015 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
1016
1017 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +05301018 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -07001019 record.key.sensor_number = sensor_id;
1020
1021 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +05301022 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001023 record.body.sensor_type = sensor->second.sensorType;
1024 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +05301025 record.body.entity_instance = sensor->second.instance;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001026
1027 // Set the type-specific details given the DBus interface
1028 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
1029 data_len);
1030
1031 if (++sensor == sensors.end())
1032 {
Ratan Guptae0cc8552018-01-22 14:23:04 +05301033 // we have reached till end of sensor, so assign the next record id
1034 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
1035 auto next_record_id = (frus.size()) ?
1036 frus.begin()->first + FRU_RECORD_ID_START :
1037 END_OF_RECORD;
1038
1039 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001040 }
1041 else
1042 {
1043 get_sdr::response::set_next_record_id(sensor->first, resp);
1044 }
1045
1046 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
1047 memcpy(resp->record_data, (char*)&record + req->offset,
1048 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
1049 }
1050
1051 return ret;
1052}
1053
1054
Chris Austenac4604a2015-10-13 12:43:27 -05001055void register_netfn_sen_functions()
1056{
Tom05732372016-09-06 17:21:23 +05301057 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -07001058 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1059 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
1060 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
1061 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +05301062 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001063
Tom05732372016-09-06 17:21:23 +05301064 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -07001065 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1066 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
1067 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
1068 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +05301069 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001070
Tom05732372016-09-06 17:21:23 +05301071 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -07001072 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1073 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
1074 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
1075 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +05301076 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -05001077
Tom05732372016-09-06 17:21:23 +05301078 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -07001079 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1080 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
1081 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
1082 nullptr, ipmi_sen_get_sensor_reading,
1083 PRIVILEGE_USER);
1084
1085 // <Reserve SDR>
1086 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1087 NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO);
1088 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO,
1089 nullptr, ipmi_sen_reserve_sdr,
1090 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001091
Emily Shafferd06e0e72017-04-05 09:08:57 -07001092 // <Get SDR Info>
Emily Shaffera344afc2017-04-13 15:09:39 -07001093 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
1094 NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
1095 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO,
1096 nullptr, ipmi_sen_get_sdr_info,
1097 PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001098
1099 // <Get SDR>
1100 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
1101 NETFUN_SENSOR, IPMI_CMD_GET_SDR);
1102 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR,
1103 nullptr, ipmi_sen_get_sdr,
1104 PRIVILEGE_USER);
1105
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001106 // <Get Sensor Thresholds>
1107 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1108 nullptr, ipmi_sen_get_sensor_thresholds,
1109 PRIVILEGE_USER);
1110
Chris Austenac4604a2015-10-13 12:43:27 -05001111 return;
1112}