blob: 036eb22b615e841dd37e3425bea7a126737388c8 [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>
Tomd700e762016-09-20 18:24:13 +053012#include "ipmid.hpp"
Tom Josephbe703f72017-03-09 12:34:35 +053013#include "sensorhandler.h"
14#include "types.hpp"
15#include "utils.hpp"
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050016#include "xyz/openbmc_project/Common/error.hpp"
17
Chris Austen8a45e7c2015-10-15 00:31:46 -050018extern int updateSensorRecordFromSSRAESC(const void *);
Tomd700e762016-09-20 18:24:13 +053019extern sd_bus *bus;
Tom Josephbe703f72017-03-09 12:34:35 +053020extern const ipmi::sensor::IdInfoMap sensors;
21using namespace phosphor::logging;
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050022using InternalFailure =
23 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austenac4604a2015-10-13 12:43:27 -050024
25void register_netfn_sen_functions() __attribute__((constructor));
26
Chris Austen0012e9b2015-10-22 01:37:46 -050027struct sensorTypemap_t {
28 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060029 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050030 char dbusname[32];
31} ;
32
Chris Austen0012e9b2015-10-22 01:37:46 -050033sensorTypemap_t g_SensorTypeMap[] = {
34
Chris Austend7cf0e42015-11-07 14:27:12 -060035 {0x01, 0x6F, "Temp"},
36 {0x0C, 0x6F, "DIMM"},
37 {0x0C, 0x6F, "MEMORY_BUFFER"},
38 {0x07, 0x6F, "PROC"},
39 {0x07, 0x6F, "CORE"},
40 {0x07, 0x6F, "CPU"},
41 {0x0F, 0x6F, "BootProgress"},
42 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor type code os 0x09
43 {0xC3, 0x6F, "BootCount"},
44 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060045 {0x12, 0x6F, "SYSTEM_EVENT"},
46 {0xC7, 0x03, "SYSTEM"},
47 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060048 {0xC2, 0x6F, "PowerCap"},
Tom Joseph558184e2017-09-01 13:45:05 +053049 {0x0b, 0xCA, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050050 {0xDA, 0x03, "TurboAllowed"},
Tom Joseph558184e2017-09-01 13:45:05 +053051 {0xD8, 0xC8, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060052 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050053};
54
55
Chris Austenac4604a2015-10-13 12:43:27 -050056struct sensor_data_t {
57 uint8_t sennum;
58} __attribute__ ((packed)) ;
59
Chris Austen10ccc0f2015-12-10 18:27:04 -060060struct sensorreadingresp_t {
61 uint8_t value;
62 uint8_t operation;
63 uint8_t indication[2];
64} __attribute__ ((packed)) ;
Chris Austenac4604a2015-10-13 12:43:27 -050065
Tomd700e762016-09-20 18:24:13 +053066// Use a lookup table to find the interface name of a specific sensor
67// This will be used until an alternative is found. this is the first
68// step for mapping IPMI
69int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
70
71 char *str1;
72 sd_bus_error error = SD_BUS_ERROR_NULL;
73 sd_bus_message *reply = NULL, *m=NULL;
74
75
76 int r;
77
78 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
79 if (r < 0) {
80 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
81 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
82 interface->bus, interface->path, interface->interface);
83 goto final;
84 }
85
86 r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
87 if (r < 0) {
88 fprintf(stderr, "Failed to create a input parameter: %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_call(bus, m, 0, &error, &reply);
95 if (r < 0) {
96 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
97 goto final;
98 }
99
100 r = sd_bus_message_read(reply, "v", "s", &str1) ;
101 if (r < 0) {
102 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
103 goto final;
104 }
105
106 strcpy(property_value, str1);
107
108final:
109
110 sd_bus_error_free(&error);
111 m = sd_bus_message_unref(m);
112 reply = sd_bus_message_unref(reply);
113
114 return r;
115}
116
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700117int get_bus_for_path(const char *path, char **busname) {
118 return mapper_get_service(bus, path, busname);
119}
Tomd700e762016-09-20 18:24:13 +0530120
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700121int legacy_dbus_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
Tomd700e762016-09-20 18:24:13 +0530122 char *busname = NULL;
123 const char *iface = "org.openbmc.managers.System";
124 const char *objname = "/org/openbmc/managers/System";
125 char *str1 = NULL, *str2, *str3;
126 sd_bus_error error = SD_BUS_ERROR_NULL;
127 sd_bus_message *reply = NULL;
128
129
130 int r;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700131 r = get_bus_for_path(objname, &busname);
Tomd700e762016-09-20 18:24:13 +0530132 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400133 fprintf(stderr, "Failed to get %s busname: %s\n",
134 objname, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530135 goto final;
136 }
137
138 r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
139 &error, &reply, "sy", type, num);
140 if (r < 0) {
141 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
142 goto final;
143 }
144
145 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
146 if (r < 0) {
147 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
148 goto final;
149 }
150
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700151 r = get_bus_for_path(str2, &str1);
Tomd700e762016-09-20 18:24:13 +0530152 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400153 fprintf(stderr, "Failed to get %s busname: %s\n",
154 str2, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530155 goto final;
156 }
157
158 strncpy(interface->bus, str1, MAX_DBUS_PATH);
159 strncpy(interface->path, str2, MAX_DBUS_PATH);
160 strncpy(interface->interface, str3, MAX_DBUS_PATH);
161
162 interface->sensornumber = num;
Emily Shaffer71174412017-04-05 15:10:40 -0700163 // Make sure we know that the type hasn't been set, as newer codebase will
164 // set it automatically from the YAML at this step.
165 interface->sensortype = 0;
Tomd700e762016-09-20 18:24:13 +0530166
167final:
168
169 sd_bus_error_free(&error);
170 reply = sd_bus_message_unref(reply);
171 free(busname);
172 free(str1);
173
174 return r;
175}
176
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700177// Use a lookup table to find the interface name of a specific sensor
178// This will be used until an alternative is found. this is the first
179// step for mapping IPMI
180int find_openbmc_path(uint8_t num, dbus_interface_t *interface) {
181 int rc;
182
183 // When the sensor map does not contain the sensor requested,
184 // fall back to the legacy DBus lookup (deprecated)
185 const auto& sensor_it = sensors.find(num);
186 if (sensor_it == sensors.end())
187 {
188 return legacy_dbus_openbmc_path("SENSOR", num, interface);
189 }
190
191 const auto& info = sensor_it->second;
192
Patrick Williams8451edf2017-06-13 09:01:06 -0500193 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700194 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
195 if (rc < 0) {
196 fprintf(stderr, "Failed to get %s busname: %s\n",
197 info.sensorPath.c_str(),
198 busname);
199 goto final;
200 }
201
202 interface->sensortype = info.sensorType;
203 strcpy(interface->bus, busname);
204 strcpy(interface->path, info.sensorPath.c_str());
205 // Take the interface name from the beginning of the DbusInterfaceMap. This
206 // works for the Value interface but may not suffice for more complex
207 // sensors.
208 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500209 strcpy(interface->interface, info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700210 interface->sensornumber = num;
211
212final:
213 free(busname);
214 return rc;
215}
216
Tomd700e762016-09-20 18:24:13 +0530217
218/////////////////////////////////////////////////////////////////////
219//
220// Routines used by ipmi commands wanting to interact on the dbus
221//
222/////////////////////////////////////////////////////////////////////
223int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
224
225
226 dbus_interface_t a;
227 int r;
228 sd_bus_error error = SD_BUS_ERROR_NULL;
229 sd_bus_message *m=NULL;
230
231 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
232 number, method, value);
233
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700234 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530235
236 if (r < 0) {
237 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
238 return 0;
239 }
240
241 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
242 if (r < 0) {
243 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
244 goto final;
245 }
246
247 r = sd_bus_message_append(m, "v", "s", value);
248 if (r < 0) {
249 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
250 goto final;
251 }
252
253
254 r = sd_bus_call(bus, m, 0, &error, NULL);
255 if (r < 0) {
256 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
257 }
258
259final:
260 sd_bus_error_free(&error);
261 m = sd_bus_message_unref(m);
262
263 return 0;
264}
265int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
266
267
268 dbus_interface_t a;
269 int r;
270 sd_bus_error error = SD_BUS_ERROR_NULL;
271 sd_bus_message *m=NULL;
272
273 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
274 number, method, value);
275
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700276 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530277
278 if (r < 0) {
279 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
280 return 0;
281 }
282
283 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
284 if (r < 0) {
285 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
286 goto final;
287 }
288
289 r = sd_bus_message_append(m, "v", "i", value);
290 if (r < 0) {
291 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
292 goto final;
293 }
294
295
296 r = sd_bus_call(bus, m, 0, &error, NULL);
297 if (r < 0) {
298 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
299 }
300
301final:
302 sd_bus_error_free(&error);
303 m = sd_bus_message_unref(m);
304
305 return 0;
306}
307
Chris Austen0012e9b2015-10-22 01:37:46 -0500308uint8_t dbus_to_sensor_type(char *p) {
Chris Austenac4604a2015-10-13 12:43:27 -0500309
Chris Austen0012e9b2015-10-22 01:37:46 -0500310 sensorTypemap_t *s = g_SensorTypeMap;
311 char r=0;
Chris Austen0012e9b2015-10-22 01:37:46 -0500312 while (s->number != 0xFF) {
313 if (!strcmp(s->dbusname,p)) {
Tom Joseph558184e2017-09-01 13:45:05 +0530314 r = s->typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -0500315 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500316 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500317 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500318 }
319
Chris Austen0012e9b2015-10-22 01:37:46 -0500320 if (s->number == 0xFF)
321 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500322
Chris Austen0012e9b2015-10-22 01:37:46 -0500323 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500324}
325
Chris Austen0012e9b2015-10-22 01:37:46 -0500326
327uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) {
328 char fru_type_name[64];
329 int r= 0;
330
331 r = find_interface_property_fru_type(a, "fru_type", fru_type_name);
332 if (r<0) {
333 fprintf(stderr, "Failed to get a fru type: %s", strerror(-r));
334 return -1;
335 } else {
336 return dbus_to_sensor_type(fru_type_name);
337 }
338}
339
Emily Shaffer391f3302017-04-03 10:27:08 -0700340uint8_t get_type_from_interface(dbus_interface_t dbus_if) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500341
Chris Austen0012e9b2015-10-22 01:37:46 -0500342 char *p;
Brad Bishop56003452016-10-05 21:49:19 -0400343 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500344
Chris Austen0012e9b2015-10-22 01:37:46 -0500345 // This is where sensors that do not exist in dbus but do
346 // exist in the host code stop. This should indicate it
347 // is not a supported sensor
Emily Shaffer391f3302017-04-03 10:27:08 -0700348 if (dbus_if.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500349
Emily Shaffer71174412017-04-05 15:10:40 -0700350 // Fetch type from interface itself.
351 if (dbus_if.sensortype != 0)
352 {
353 type = dbus_if.sensortype;
354 }
355 // Legacy codebase does not populate type during initial handling:
356 else if (strstr(dbus_if.interface, "InventoryItem")) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500357 // InventoryItems are real frus. So need to get the
358 // fru_type property
Emily Shaffer391f3302017-04-03 10:27:08 -0700359 type = dbus_to_sensor_type_from_dbus(&dbus_if);
Chris Austen0012e9b2015-10-22 01:37:46 -0500360 } else {
361 // Non InventoryItems
Emily Shaffer391f3302017-04-03 10:27:08 -0700362 p = strrchr (dbus_if.path, '/');
Brad Bishop56003452016-10-05 21:49:19 -0400363 type = dbus_to_sensor_type(p+1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500364 }
365
Brad Bishop56003452016-10-05 21:49:19 -0400366 return type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500367 }
368
Emily Shaffer391f3302017-04-03 10:27:08 -0700369// Replaces find_sensor
370uint8_t find_type_for_sensor_number(uint8_t num) {
371 int r;
372 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700373 r = find_openbmc_path(num, &dbus_if);
Emily Shaffer391f3302017-04-03 10:27:08 -0700374 if (r < 0) {
375 fprintf(stderr, "Could not find sensor %d\n", num);
376 return r;
377 }
378 return get_type_from_interface(dbus_if);
379}
380
Chris Austen10ccc0f2015-12-10 18:27:04 -0600381
382
383
384
Chris Austen0012e9b2015-10-22 01:37:46 -0500385ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
386 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500387 ipmi_data_len_t data_len, ipmi_context_t context)
388{
389 sensor_data_t *reqptr = (sensor_data_t*)request;
390 ipmi_ret_t rc = IPMI_CC_OK;
391
392 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
393
394 // TODO Not sure what the System-event-sensor is suppose to return
395 // need to ask Hostboot team
396 unsigned char buf[] = {0x00,0x6F};
397
Emily Shaffer391f3302017-04-03 10:27:08 -0700398 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500399
400 // HACK UNTIL Dbus gets updated or we find a better way
401 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600402 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500403 }
404
Chris Austenac4604a2015-10-13 12:43:27 -0500405
406 *data_len = sizeof(buf);
407 memcpy(response, &buf, *data_len);
408
Chris Austenac4604a2015-10-13 12:43:27 -0500409 return rc;
410}
411
Emily Shaffercc941e12017-06-14 13:06:26 -0700412const std::set<std::string> analogSensorInterfaces =
413{
414 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700415 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700416};
417
418bool isAnalogSensor(const std::string& interface)
419{
420 return (analogSensorInterfaces.count(interface));
421}
422
Tom Josephbe703f72017-03-09 12:34:35 +0530423ipmi_ret_t setSensorReading(void *request)
424{
Tom Joseph816e92b2017-09-06 19:23:00 +0530425 ipmi::sensor::SetSensorReadingReq cmdData =
426 *(static_cast<ipmi::sensor::SetSensorReadingReq *>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530427
428 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500429 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530430 if (iter == sensors.end())
431 {
432 return IPMI_CC_SENSOR_INVALID;
433 }
434
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500435 try
436 {
437 return iter->second.updateFunc(cmdData, iter->second);
438 }
439 catch (InternalFailure& e)
440 {
441 log<level::ERR>("Set sensor failed",
442 entry("SENSOR_NUM=%d", cmdData.number));
443 commit<InternalFailure>();
444 }
Tom Joseph82024322017-09-28 20:07:29 +0530445 catch (const std::runtime_error& e)
446 {
447 log<level::ERR>(e.what());
448 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500449
450 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530451}
Chris Austenac4604a2015-10-13 12:43:27 -0500452
Chris Austen0012e9b2015-10-22 01:37:46 -0500453ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
454 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500455 ipmi_data_len_t data_len, ipmi_context_t context)
456{
Chris Austenac4604a2015-10-13 12:43:27 -0500457 sensor_data_t *reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500458
Chris Austen0012e9b2015-10-22 01:37:46 -0500459 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500460
Tom Josephbe703f72017-03-09 12:34:35 +0530461 /*
462 * This would support the Set Sensor Reading command for the presence
463 * and functional state of Processor, Core & DIMM. For the remaining
464 * sensors the existing support is invoked.
465 */
466 auto ipmiRC = setSensorReading(request);
467
468 if(ipmiRC == IPMI_CC_SENSOR_INVALID)
469 {
470 updateSensorRecordFromSSRAESC(reqptr);
471 ipmiRC = IPMI_CC_OK;
472 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500473
Chris Austenac4604a2015-10-13 12:43:27 -0500474 *data_len=0;
Tom Josephbe703f72017-03-09 12:34:35 +0530475 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500476}
477
Chris Austen10ccc0f2015-12-10 18:27:04 -0600478
479ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
480 ipmi_request_t request, ipmi_response_t response,
481 ipmi_data_len_t data_len, ipmi_context_t context)
482{
483 sensor_data_t *reqptr = (sensor_data_t*)request;
484 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
Tom Joseph40c35b12017-09-07 02:04:42 +0530485 uint8_t type = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600486 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
487 int r;
488 dbus_interface_t a;
489 sd_bus *bus = ipmid_get_sd_bus_connection();
490 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600491 int reading = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600492 char* assertion = NULL;
Tom Josephc6162fb2018-01-25 01:55:20 +0530493 ipmi::sensor::GetSensorResponse getResponse {};
494 static constexpr auto scanningEnabledBit = 6;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600495
Chris Austen10ccc0f2015-12-10 18:27:04 -0600496 printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
497
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700498 r = find_openbmc_path(reqptr->sennum, &a);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600499
Tom Joseph40c35b12017-09-07 02:04:42 +0530500 if (r < 0)
501 {
Nan Li36deb762016-05-12 10:23:41 +0800502 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
Nan Li36deb762016-05-12 10:23:41 +0800503 }
Tom Joseph40c35b12017-09-07 02:04:42 +0530504 else
505 {
506 type = get_type_from_interface(a);
507 if(type == 0) {
508 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
509 return IPMI_CC_SENSOR_INVALID;
510 }
Nan Li36deb762016-05-12 10:23:41 +0800511
Tom Joseph40c35b12017-09-07 02:04:42 +0530512 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path,
513 a.interface);
Brad Bishop56003452016-10-05 21:49:19 -0400514 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600515
Chris Austen10ccc0f2015-12-10 18:27:04 -0600516 *data_len=0;
517
Emily Shaffer10f49592017-05-10 12:01:10 -0700518 int64_t raw_value;
Emily Shaffer1fabf222017-04-05 08:53:21 -0700519 ipmi::sensor::Info sensor;
520
Chris Austen10ccc0f2015-12-10 18:27:04 -0600521 switch(type) {
Chris Austen10ccc0f2015-12-10 18:27:04 -0600522 case 0xC2:
Dhruvaraj Subhashchandrand12ae752017-10-04 00:33:27 -0500523 case 0xC8:
Adriana Kobylak93125982016-03-01 12:48:10 -0600524 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600525 if (r < 0) {
526 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
527 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
528 a.bus, a.path, a.interface);
529 break;
530 }
531
Adriana Kobylak93125982016-03-01 12:48:10 -0600532 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600533 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600534 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600535 break;
536 }
537
538 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
539
540 rc = IPMI_CC_OK;
541 *data_len=sizeof(sensorreadingresp_t);
542
Adriana Kobylak93125982016-03-01 12:48:10 -0600543 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600544 resp->operation = 0;
545 resp->indication[0] = 0;
546 resp->indication[1] = 0;
547 break;
548
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600549 //TODO openbmc/openbmc#2154 Move this sensor to right place.
550 case 0xCA:
551 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "s");
552 if (r < 0) {
553 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
554 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
555 a.bus, a.path, a.interface);
556 break;
557 }
558
559 r = sd_bus_message_read(reply, "s", &assertion);
560 if (r < 0) {
561 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
562 break;
563 }
564
565 rc = IPMI_CC_OK;
566 *data_len=sizeof(sensorreadingresp_t);
567
568 resp->value = 0;
569 resp->operation = 0;
570 if (strcmp(assertion,"Enabled") == 0)
571 {
572 resp->indication[0] = 0x02;
573 }
574 else
575 {
576 resp->indication[0] = 0x1;
577 }
578 resp->indication[1] = 0;
Dhruvaraj Subhashchandran6244f932017-11-23 01:08:46 -0600579 break;
580
Emily Shaffer1fabf222017-04-05 08:53:21 -0700581 case IPMI_SENSOR_TEMP:
582 case IPMI_SENSOR_VOLTAGE:
583 case IPMI_SENSOR_CURRENT:
584 case IPMI_SENSOR_FAN:
585 // Get reading for /xyz/openbmc_project/Sensor/Value.interface
Emily Shaffer10f49592017-05-10 12:01:10 -0700586 if(sensors.find(reqptr->sennum) == sensors.end())
587 {
588 fprintf(stderr, "Failed to find config entry for Sensor 0x%02x\n",
589 reqptr->sennum);
590 return IPMI_CC_SENSOR_INVALID;
591 }
592
593 sensor = sensors.at(reqptr->sennum);
Emily Shaffercc941e12017-06-14 13:06:26 -0700594 if (ipmi::sensor::Mutability::Read !=
595 (sensor.mutability & ipmi::sensor::Mutability::Read))
596 {
597 log<level::ERR>("Sensor was not readable.\n");
598 return IPMI_CC_SENSOR_INVALID;
599 }
600
Emily Shaffer1fabf222017-04-05 08:53:21 -0700601
602 // Get value
603 r = sd_bus_get_property_trivial(bus,
604 a.bus,
605 a.path,
606 a.interface,
607 "Value",
608 NULL,
609 'x',
610 &raw_value);
611 if (r < 0) {
612 fprintf(stderr,
613 "Failed to call sd_bus_get_property:%d, %s, 'value'\n",
614 r,
615 strerror(-r));
616 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
617 a.bus, a.path, a.interface);
618 break;
619 }
620
Emily Shaffer10f49592017-05-10 12:01:10 -0700621 // Prevent div0
622 if (sensor.coefficientM == 0) {
623 sensor.coefficientM = 1;
624 };
Emily Shaffer1fabf222017-04-05 08:53:21 -0700625
Emily Shaffer10f49592017-05-10 12:01:10 -0700626 resp->value = static_cast<uint8_t>(
627 (raw_value - sensor.scaledOffset) / sensor.coefficientM);
Tom Josephc6162fb2018-01-25 01:55:20 +0530628 resp->operation = 1 << scanningEnabledBit; // scanning enabled
Emily Shafferbbef71c2017-05-08 16:36:17 -0700629 resp->indication[0] = 0; // not a threshold sensor. ignore
Emily Shaffer1fabf222017-04-05 08:53:21 -0700630 resp->indication[1] = 0;
631 rc = IPMI_CC_OK;
632 *data_len=sizeof(sensorreadingresp_t);
633 break;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600634 default:
Tom Joseph14c15462017-09-07 02:16:51 +0530635 {
636 const auto iter = sensors.find(reqptr->sennum);
637 if (iter == sensors.end())
638 {
639 return IPMI_CC_SENSOR_INVALID;
640 }
641
642 try
643 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530644 getResponse = iter->second.getFunc(iter->second);
Tom Joseph14c15462017-09-07 02:16:51 +0530645 *data_len = getResponse.size();
646 memcpy(resp, getResponse.data(), *data_len);
Tom Josephc6162fb2018-01-25 01:55:20 +0530647 resp->operation = 1 << scanningEnabledBit;
Tom Joseph14c15462017-09-07 02:16:51 +0530648 return IPMI_CC_OK;
649 }
650 catch (InternalFailure& e)
651 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530652 *data_len = getResponse.size();
653 memcpy(resp, getResponse.data(), *data_len);
654 return IPMI_CC_OK;
Tom Joseph14c15462017-09-07 02:16:51 +0530655 }
Tom Joseph82024322017-09-28 20:07:29 +0530656 catch (const std::runtime_error& e)
657 {
Tom Josephc6162fb2018-01-25 01:55:20 +0530658 *data_len = getResponse.size();
659 memcpy(resp, getResponse.data(), *data_len);
660 return IPMI_CC_OK;
Tom Joseph82024322017-09-28 20:07:29 +0530661 }
Tom Joseph14c15462017-09-07 02:16:51 +0530662 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600663 }
664
vishwa1eaea4f2016-02-26 11:57:40 -0600665 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600666
667 return rc;
668}
669
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600670ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
671 ipmi_request_t request, ipmi_response_t response,
672 ipmi_data_len_t data_len, ipmi_context_t context)
673{
674 constexpr auto warningThresholdInterface =
675 "xyz.openbmc_project.Sensor.Threshold.Warning";
676 constexpr auto criticalThresholdInterface =
677 "xyz.openbmc_project.Sensor.Threshold.Critical";
678 constexpr auto valueInterface =
679 "xyz.openbmc_project.Sensor.Value";
680 constexpr auto sensorRoot = "/xyz/openbmc_project/sensors";
681
682 ipmi::sensor::Thresholds thresholds =
683 {
684 {
685 warningThresholdInterface,
686 {
687 {
688 "WarningLow",
689 ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK,
690 ipmi::sensor::ThresholdIndex::NON_CRITICAL_LOW_IDX
691 },
692 {
693 "WarningHigh",
694 ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK,
695 ipmi::sensor::ThresholdIndex::NON_CRITICAL_HIGH_IDX
696 }
697 }
698 },
699 {
700 criticalThresholdInterface,
701 {
702 {
703 "CriticalLow",
704 ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK,
705 ipmi::sensor::ThresholdIndex::CRITICAL_LOW_IDX
706 },
707 {
708 "CriticalHigh",
709 ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK,
710 ipmi::sensor::ThresholdIndex::CRITICAL_HIGH_IDX
711 }
712 }
713 }
714 };
715
716 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
717
718 if (*data_len != sizeof(uint8_t))
719 {
720 return IPMI_CC_REQ_DATA_LEN_INVALID;
721 }
722 auto sensorNum = *(reinterpret_cast<uint8_t*>(request));
723
724 auto responseData =
725 reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response);
726
727 responseData->validMask = 0;
728
729 const auto iter = sensors.find(sensorNum);
730 if (iter == sensors.end())
731 {
732 return IPMI_CC_SENSOR_INVALID;
733 }
734
735 const auto sensorInfo = iter->second;
736
737 //Proceed only if the sensor value interface is implemented.
738 if (sensorInfo.propertyInterfaces.find(valueInterface) ==
739 sensorInfo.propertyInterfaces.end())
740 {
741 //return with valid mask as 0
742 return IPMI_CC_OK;
743 }
744
745 std::string service;
746 try
747 {
748 service = ipmi::getService(bus,
749 sensorInfo.sensorInterface,
750 sensorInfo.sensorPath);
751 }
752 catch (const std::runtime_error& e)
753 {
754 log<level::ERR>(e.what());
755 return IPMI_CC_UNSPECIFIED_ERROR;
756 }
757
758 //prevent divide by 0
759 auto coefficientM =
760 sensorInfo.coefficientM ? sensorInfo.coefficientM : 1;
761
762 try
763 {
764 auto mngObjects = ipmi::getManagedObjects(bus,
765 service,
766 sensorRoot);
767
768 auto senIter = mngObjects.find(sensorInfo.sensorPath);
769 if (senIter == mngObjects.end())
770 {
771 return IPMI_CC_SENSOR_INVALID;
772 }
773
774 for (const auto& threshold : thresholds)
775 {
776 auto thresholdType = senIter->second.find(threshold.first);
777 if (thresholdType != senIter->second.end())
778 {
779 for (const auto& threshLevel : threshold.second)
780 {
781 auto val = thresholdType->
782 second[threshLevel.property].get<int64_t>();
783 if (val != 0)
784 {
785 auto idx = static_cast<uint8_t>(threshLevel.idx);
786 responseData->data[idx] = static_cast<uint8_t>(
787 (val - sensorInfo.scaledOffset) / coefficientM);
788 responseData->validMask |=
789 static_cast<uint8_t>(threshLevel.maskValue);
790 }
791 }
792 }
793 }
794 }
795 catch (InternalFailure& e)
796 {
797 //Not able to get the values, reset the mask.
798 responseData->validMask = 0;
799 }
800
801 *data_len = sizeof(get_sdr::GetSensorThresholdsResponse);
802 return IPMI_CC_OK;
803}
804
Chris Austen0012e9b2015-10-22 01:37:46 -0500805ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
806 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500807 ipmi_data_len_t data_len, ipmi_context_t context)
808{
Nan Li70aa8d92016-08-29 00:11:10 +0800809 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500810
811 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
812 *data_len = 0;
813
814 return rc;
815}
816
Emily Shafferd06e0e72017-04-05 09:08:57 -0700817ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
818 ipmi_request_t request,
819 ipmi_response_t response,
820 ipmi_data_len_t data_len,
821 ipmi_context_t context)
822{
823 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
824 if (request == nullptr ||
825 get_sdr_info::request::get_count(request) == false)
826 {
827 // Get Sensor Count
828 resp->count = sensors.size();
829 }
830 else
831 {
832 resp->count = 1;
833 }
834
835 // Multiple LUNs not supported.
836 namespace response = get_sdr_info::response;
837 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
838 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
839 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
840 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
841 response::set_static_population(&(resp->luns_and_dynamic_population));
842
843 *data_len = SDR_INFO_RESP_SIZE;
844
845 return IPMI_CC_OK;
846}
847
Emily Shaffera344afc2017-04-13 15:09:39 -0700848ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
849 ipmi_request_t request,
850 ipmi_response_t response,
851 ipmi_data_len_t data_len,
852 ipmi_context_t context)
853{
854 // A constant reservation ID is okay until we implement add/remove SDR.
855 const uint16_t reservation_id = 1;
856 *(uint16_t*)response = reservation_id;
Emily Shaffer5a5a6282017-08-15 11:17:17 -0700857 *data_len = sizeof(uint16_t);
Emily Shaffera344afc2017-04-13 15:09:39 -0700858
859 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
860 return IPMI_CC_OK;
861}
Chris Austenac4604a2015-10-13 12:43:27 -0500862
Emily Shaffercc941e12017-06-14 13:06:26 -0700863void setUnitFieldsForObject(sd_bus *bus,
864 const dbus_interface_t &iface,
865 const ipmi::sensor::Info *info,
866 get_sdr::SensorDataFullRecordBody *body)
867{
868 if (info->propertyInterfaces.begin()->first ==
869 "xyz.openbmc_project.Sensor.Value")
870 {
871 std::string result {};
Emily Shaffer62235612017-06-14 13:06:26 -0700872 if (info->unit.empty())
Emily Shaffercc941e12017-06-14 13:06:26 -0700873 {
Emily Shaffer62235612017-06-14 13:06:26 -0700874 char *raw_cstr = NULL;
875 if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path,
876 iface.interface, "Unit", NULL,
877 &raw_cstr))
878 {
879 log<level::WARNING>("Unit interface missing.",
880 entry("BUS=%s", iface.bus),
881 entry("PATH=%s", iface.path));
882 }
883 else
884 {
885 result = raw_cstr;
886 }
887 free(raw_cstr);
Emily Shaffercc941e12017-06-14 13:06:26 -0700888 }
889 else
890 {
Emily Shaffer62235612017-06-14 13:06:26 -0700891 result = info->unit;
Emily Shaffercc941e12017-06-14 13:06:26 -0700892 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700893
894 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
895 try {
896 auto unit = server::Value::convertUnitFromString(result);
897 // Unit strings defined in
898 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
899 switch (unit)
900 {
901 case server::Value::Unit::DegreesC:
902 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
903 break;
904 case server::Value::Unit::RPMS:
905 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions
906 get_sdr::body::set_rate_unit(0b100, body); // per minute
907 break;
908 case server::Value::Unit::Volts:
909 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
910 break;
911 case server::Value::Unit::Meters:
912 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
913 break;
914 case server::Value::Unit::Amperes:
915 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
916 break;
917 case server::Value::Unit::Joules:
918 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
919 break;
920 default:
921 // Cannot be hit.
922 fprintf(stderr, "Unknown value unit type: = %s\n", result.c_str());
923 }
924 }
925 catch (sdbusplus::exception::InvalidEnumString e)
926 {
927 log<level::WARNING>("Warning: no unit provided for sensor!");
928 }
929 }
930}
931
932int64_t getScaleForObject(sd_bus *bus,
933 const dbus_interface_t& iface,
934 const ipmi::sensor::Info *info)
935{
936 int64_t result = 0;
937 if (info->propertyInterfaces.begin()->first ==
938 "xyz.openbmc_project.Sensor.Value")
939 {
Emily Shaffer62235612017-06-14 13:06:26 -0700940 if (info->hasScale)
941 {
942 result = info->scale;
943 }
944 else
945 {
946 if (0 > sd_bus_get_property_trivial(bus,
947 iface.bus,
948 iface.path,
949 iface.interface,
950 "Scale",
951 NULL,
952 'x',
953 &result)) {
954 log<level::WARNING>("Scale interface missing.",
955 entry("BUS=%s", iface.bus),
956 entry("PATH=%s", iface.path));
957 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700958 }
959 }
960
961 return result;
962}
963
Emily Shafferbbef71c2017-05-08 16:36:17 -0700964ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body,
965 const ipmi::sensor::Info *info,
966 ipmi_data_len_t data_len)
967{
968 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700969 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700970 {
971 // Get bus
972 sd_bus *bus = ipmid_get_sd_bus_connection();
973 dbus_interface_t iface;
974
975 if (0 > find_openbmc_path(body->entity_id, &iface))
976 return IPMI_CC_SENSOR_INVALID;
977
978 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
979
980 /* Unit info */
Emily Shaffercc941e12017-06-14 13:06:26 -0700981 setUnitFieldsForObject(bus, iface, info, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700982
983 /* Modifiers to reading info */
Emily Shaffer10f49592017-05-10 12:01:10 -0700984 // Get scale
Emily Shaffercc941e12017-06-14 13:06:26 -0700985 int64_t scale = getScaleForObject(bus, iface, info);
Emily Shaffer10f49592017-05-10 12:01:10 -0700986
987 get_sdr::body::set_b(info->coefficientB, body);
988 get_sdr::body::set_m(info->coefficientM, body);
989 get_sdr::body::set_b_exp(info->exponentB, body);
990 get_sdr::body::set_r_exp(scale, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700991
Emily Shafferbbef71c2017-05-08 16:36:17 -0700992 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700993 }
994
Tom Joseph96423912018-01-25 00:14:34 +0530995 /* ID string */
996 auto id_string = info->sensorNameFunc(*info);
997
998 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
999 {
1000 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
1001 }
1002 else
1003 {
1004 get_sdr::body::set_id_strlen(id_string.length(), body);
1005 }
1006 strncpy(body->id_string, id_string.c_str(),
1007 get_sdr::body::get_id_strlen(body));
1008
Emily Shafferbbef71c2017-05-08 16:36:17 -07001009 return IPMI_CC_OK;
1010};
1011
1012ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
1013 ipmi_request_t request, ipmi_response_t response,
1014 ipmi_data_len_t data_len, ipmi_context_t context)
1015{
1016 ipmi_ret_t ret = IPMI_CC_OK;
1017 get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request;
1018 get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response;
1019 get_sdr::SensorDataFullRecord record = {0};
1020 if (req != NULL)
1021 {
1022 // Note: we use an iterator so we can provide the next ID at the end of
1023 // the call.
1024 auto sensor = sensors.begin();
1025
1026 // At the beginning of a scan, the host side will send us id=0.
1027 if (get_sdr::request::get_record_id(req) != 0)
1028 {
1029 sensor = sensors.find(get_sdr::request::get_record_id(req));
1030 if(sensor == sensors.end()) {
1031 return IPMI_CC_SENSOR_INVALID;
1032 }
1033 }
1034
1035 uint8_t sensor_id = sensor->first;
1036
1037 /* Header */
1038 get_sdr::header::set_record_id(sensor_id, &(record.header));
1039 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
1040 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
1041 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
1042
1043 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +05301044 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -07001045 record.key.sensor_number = sensor_id;
1046
1047 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +05301048 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001049 record.body.sensor_type = sensor->second.sensorType;
1050 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +05301051 record.body.entity_instance = sensor->second.instance;
Emily Shafferbbef71c2017-05-08 16:36:17 -07001052
1053 // Set the type-specific details given the DBus interface
1054 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
1055 data_len);
1056
1057 if (++sensor == sensors.end())
1058 {
1059 get_sdr::response::set_next_record_id(0xFFFF, resp); // last record
1060 }
1061 else
1062 {
1063 get_sdr::response::set_next_record_id(sensor->first, resp);
1064 }
1065
1066 *data_len = sizeof(get_sdr::GetSdrResp) - req->offset;
1067 memcpy(resp->record_data, (char*)&record + req->offset,
1068 sizeof(get_sdr::SensorDataFullRecord) - req->offset);
1069 }
1070
1071 return ret;
1072}
1073
1074
Chris Austenac4604a2015-10-13 12:43:27 -05001075void register_netfn_sen_functions()
1076{
Tom05732372016-09-06 17:21:23 +05301077 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -07001078 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1079 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
1080 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
1081 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +05301082 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001083
Tom05732372016-09-06 17:21:23 +05301084 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -07001085 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1086 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
1087 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
1088 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +05301089 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001090
Tom05732372016-09-06 17:21:23 +05301091 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -07001092 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1093 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
1094 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
1095 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +05301096 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -05001097
Tom05732372016-09-06 17:21:23 +05301098 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -07001099 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1100 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
1101 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
1102 nullptr, ipmi_sen_get_sensor_reading,
1103 PRIVILEGE_USER);
1104
1105 // <Reserve SDR>
1106 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
1107 NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO);
1108 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO,
1109 nullptr, ipmi_sen_reserve_sdr,
1110 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001111
Emily Shafferd06e0e72017-04-05 09:08:57 -07001112 // <Get SDR Info>
Emily Shaffera344afc2017-04-13 15:09:39 -07001113 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
1114 NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
1115 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO,
1116 nullptr, ipmi_sen_get_sdr_info,
1117 PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001118
1119 // <Get SDR>
1120 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
1121 NETFUN_SENSOR, IPMI_CMD_GET_SDR);
1122 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR,
1123 nullptr, ipmi_sen_get_sdr,
1124 PRIVILEGE_USER);
1125
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001126 // <Get Sensor Thresholds>
1127 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1128 nullptr, ipmi_sen_get_sensor_thresholds,
1129 PRIVILEGE_USER);
1130
Chris Austenac4604a2015-10-13 12:43:27 -05001131 return;
1132}