blob: da6cfd9dc28738841be1c8fc8ebc6f8fdb291ce7 [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>
Tom Josephbe703f72017-03-09 12:34:35 +05305#include <bitset>
Chris Austen10ccc0f2015-12-10 18:27:04 -06006#include <systemd/sd-bus.h>
Tom Josephbe703f72017-03-09 12:34:35 +05307#include "host-ipmid/ipmid-api.h"
8#include <phosphor-logging/log.hpp>
Tomd700e762016-09-20 18:24:13 +05309#include "ipmid.hpp"
Tom Josephbe703f72017-03-09 12:34:35 +053010#include "sensorhandler.h"
11#include "types.hpp"
12#include "utils.hpp"
Chris Austenac4604a2015-10-13 12:43:27 -050013
Chris Austen8a45e7c2015-10-15 00:31:46 -050014extern int updateSensorRecordFromSSRAESC(const void *);
Tomd700e762016-09-20 18:24:13 +053015extern sd_bus *bus;
Tom Josephbe703f72017-03-09 12:34:35 +053016extern const ipmi::sensor::IdInfoMap sensors;
17using namespace phosphor::logging;
Chris Austenac4604a2015-10-13 12:43:27 -050018
19void register_netfn_sen_functions() __attribute__((constructor));
20
Chris Austen0012e9b2015-10-22 01:37:46 -050021struct sensorTypemap_t {
22 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060023 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050024 char dbusname[32];
25} ;
26
Chris Austen0012e9b2015-10-22 01:37:46 -050027sensorTypemap_t g_SensorTypeMap[] = {
28
Chris Austend7cf0e42015-11-07 14:27:12 -060029 {0x01, 0x6F, "Temp"},
30 {0x0C, 0x6F, "DIMM"},
31 {0x0C, 0x6F, "MEMORY_BUFFER"},
32 {0x07, 0x6F, "PROC"},
33 {0x07, 0x6F, "CORE"},
34 {0x07, 0x6F, "CPU"},
35 {0x0F, 0x6F, "BootProgress"},
36 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor type code os 0x09
37 {0xC3, 0x6F, "BootCount"},
38 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060039 {0x12, 0x6F, "SYSTEM_EVENT"},
40 {0xC7, 0x03, "SYSTEM"},
41 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060042 {0xC2, 0x6F, "PowerCap"},
Dhruvaraj S07248292017-03-21 02:14:52 -050043 {0xD8, 0x03, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050044 {0xDA, 0x03, "TurboAllowed"},
Jayanth Othayothd5b2ac02017-03-27 08:19:18 -050045 {0xB4, 0x6F, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060046 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050047};
48
49
Chris Austenac4604a2015-10-13 12:43:27 -050050struct sensor_data_t {
51 uint8_t sennum;
52} __attribute__ ((packed)) ;
53
Chris Austen10ccc0f2015-12-10 18:27:04 -060054struct sensorreadingresp_t {
55 uint8_t value;
56 uint8_t operation;
57 uint8_t indication[2];
58} __attribute__ ((packed)) ;
Chris Austenac4604a2015-10-13 12:43:27 -050059
Tomd700e762016-09-20 18:24:13 +053060// Use a lookup table to find the interface name of a specific sensor
61// This will be used until an alternative is found. this is the first
62// step for mapping IPMI
63int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) {
64
65 char *str1;
66 sd_bus_error error = SD_BUS_ERROR_NULL;
67 sd_bus_message *reply = NULL, *m=NULL;
68
69
70 int r;
71
72 r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get");
73 if (r < 0) {
74 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
75 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
76 interface->bus, interface->path, interface->interface);
77 goto final;
78 }
79
80 r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name);
81 if (r < 0) {
82 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
83 fprintf(stderr,"Bus: %s Path: %s Interface: %s \n",
84 interface->bus, interface->path, interface->interface);
85 goto final;
86 }
87
88 r = sd_bus_call(bus, m, 0, &error, &reply);
89 if (r < 0) {
90 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
91 goto final;
92 }
93
94 r = sd_bus_message_read(reply, "v", "s", &str1) ;
95 if (r < 0) {
96 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
97 goto final;
98 }
99
100 strcpy(property_value, str1);
101
102final:
103
104 sd_bus_error_free(&error);
105 m = sd_bus_message_unref(m);
106 reply = sd_bus_message_unref(reply);
107
108 return r;
109}
110
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700111int get_bus_for_path(const char *path, char **busname) {
112 return mapper_get_service(bus, path, busname);
113}
Tomd700e762016-09-20 18:24:13 +0530114
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700115int legacy_dbus_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) {
Tomd700e762016-09-20 18:24:13 +0530116 char *busname = NULL;
117 const char *iface = "org.openbmc.managers.System";
118 const char *objname = "/org/openbmc/managers/System";
119 char *str1 = NULL, *str2, *str3;
120 sd_bus_error error = SD_BUS_ERROR_NULL;
121 sd_bus_message *reply = NULL;
122
123
124 int r;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700125 r = get_bus_for_path(objname, &busname);
Tomd700e762016-09-20 18:24:13 +0530126 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400127 fprintf(stderr, "Failed to get %s busname: %s\n",
128 objname, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530129 goto final;
130 }
131
132 r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId",
133 &error, &reply, "sy", type, num);
134 if (r < 0) {
135 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
136 goto final;
137 }
138
139 r = sd_bus_message_read(reply, "(ss)", &str2, &str3);
140 if (r < 0) {
141 fprintf(stderr, "Failed to get a response: %s", strerror(-r));
142 goto final;
143 }
144
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700145 r = get_bus_for_path(str2, &str1);
Tomd700e762016-09-20 18:24:13 +0530146 if (r < 0) {
Brad Bishop819ddd42016-10-05 21:19:19 -0400147 fprintf(stderr, "Failed to get %s busname: %s\n",
148 str2, strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530149 goto final;
150 }
151
152 strncpy(interface->bus, str1, MAX_DBUS_PATH);
153 strncpy(interface->path, str2, MAX_DBUS_PATH);
154 strncpy(interface->interface, str3, MAX_DBUS_PATH);
155
156 interface->sensornumber = num;
Emily Shaffer71174412017-04-05 15:10:40 -0700157 // Make sure we know that the type hasn't been set, as newer codebase will
158 // set it automatically from the YAML at this step.
159 interface->sensortype = 0;
Tomd700e762016-09-20 18:24:13 +0530160
161final:
162
163 sd_bus_error_free(&error);
164 reply = sd_bus_message_unref(reply);
165 free(busname);
166 free(str1);
167
168 return r;
169}
170
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700171// Use a lookup table to find the interface name of a specific sensor
172// This will be used until an alternative is found. this is the first
173// step for mapping IPMI
174int find_openbmc_path(uint8_t num, dbus_interface_t *interface) {
175 int rc;
176
177 // When the sensor map does not contain the sensor requested,
178 // fall back to the legacy DBus lookup (deprecated)
179 const auto& sensor_it = sensors.find(num);
180 if (sensor_it == sensors.end())
181 {
182 return legacy_dbus_openbmc_path("SENSOR", num, interface);
183 }
184
185 const auto& info = sensor_it->second;
186
Patrick Williams8451edf2017-06-13 09:01:06 -0500187 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700188 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
189 if (rc < 0) {
190 fprintf(stderr, "Failed to get %s busname: %s\n",
191 info.sensorPath.c_str(),
192 busname);
193 goto final;
194 }
195
196 interface->sensortype = info.sensorType;
197 strcpy(interface->bus, busname);
198 strcpy(interface->path, info.sensorPath.c_str());
199 // Take the interface name from the beginning of the DbusInterfaceMap. This
200 // works for the Value interface but may not suffice for more complex
201 // sensors.
202 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
203 strcpy(interface->interface, info.sensorInterfaces.begin()->first.c_str());
204 interface->sensornumber = num;
205
206final:
207 free(busname);
208 return rc;
209}
210
Tomd700e762016-09-20 18:24:13 +0530211
212/////////////////////////////////////////////////////////////////////
213//
214// Routines used by ipmi commands wanting to interact on the dbus
215//
216/////////////////////////////////////////////////////////////////////
217int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) {
218
219
220 dbus_interface_t a;
221 int r;
222 sd_bus_error error = SD_BUS_ERROR_NULL;
223 sd_bus_message *m=NULL;
224
225 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n",
226 number, method, value);
227
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700228 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530229
230 if (r < 0) {
231 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
232 return 0;
233 }
234
235 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
236 if (r < 0) {
237 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
238 goto final;
239 }
240
241 r = sd_bus_message_append(m, "v", "s", value);
242 if (r < 0) {
243 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
244 goto final;
245 }
246
247
248 r = sd_bus_call(bus, m, 0, &error, NULL);
249 if (r < 0) {
250 fprintf(stderr, "Failed to call the method: %s", strerror(-r));
251 }
252
253final:
254 sd_bus_error_free(&error);
255 m = sd_bus_message_unref(m);
256
257 return 0;
258}
259int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) {
260
261
262 dbus_interface_t a;
263 int r;
264 sd_bus_error error = SD_BUS_ERROR_NULL;
265 sd_bus_message *m=NULL;
266
267 fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n",
268 number, method, value);
269
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700270 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530271
272 if (r < 0) {
273 fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
274 return 0;
275 }
276
277 r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
278 if (r < 0) {
279 fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
280 goto final;
281 }
282
283 r = sd_bus_message_append(m, "v", "i", value);
284 if (r < 0) {
285 fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r));
286 goto final;
287 }
288
289
290 r = sd_bus_call(bus, m, 0, &error, NULL);
291 if (r < 0) {
292 fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
293 }
294
295final:
296 sd_bus_error_free(&error);
297 m = sd_bus_message_unref(m);
298
299 return 0;
300}
301
Chris Austen0012e9b2015-10-22 01:37:46 -0500302uint8_t dbus_to_sensor_type(char *p) {
Chris Austenac4604a2015-10-13 12:43:27 -0500303
Chris Austen0012e9b2015-10-22 01:37:46 -0500304 sensorTypemap_t *s = g_SensorTypeMap;
305 char r=0;
Chris Austenac4604a2015-10-13 12:43:27 -0500306
Chris Austen0012e9b2015-10-22 01:37:46 -0500307 while (s->number != 0xFF) {
308 if (!strcmp(s->dbusname,p)) {
309 r = s->number;
310 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500311 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500312 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500313 }
314
Chris Austend7cf0e42015-11-07 14:27:12 -0600315
Chris Austen0012e9b2015-10-22 01:37:46 -0500316 if (s->number == 0xFF)
317 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500318
Chris Austen0012e9b2015-10-22 01:37:46 -0500319 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500320}
321
Chris Austen0012e9b2015-10-22 01:37:46 -0500322
323uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) {
324 char fru_type_name[64];
325 int r= 0;
326
327 r = find_interface_property_fru_type(a, "fru_type", fru_type_name);
328 if (r<0) {
329 fprintf(stderr, "Failed to get a fru type: %s", strerror(-r));
330 return -1;
331 } else {
332 return dbus_to_sensor_type(fru_type_name);
333 }
334}
335
Emily Shaffer391f3302017-04-03 10:27:08 -0700336uint8_t get_type_from_interface(dbus_interface_t dbus_if) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500337
Chris Austen0012e9b2015-10-22 01:37:46 -0500338 char *p;
Brad Bishop56003452016-10-05 21:49:19 -0400339 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500340
Chris Austen0012e9b2015-10-22 01:37:46 -0500341 // This is where sensors that do not exist in dbus but do
342 // exist in the host code stop. This should indicate it
343 // is not a supported sensor
Emily Shaffer391f3302017-04-03 10:27:08 -0700344 if (dbus_if.interface[0] == 0) { return 0;}
Chris Austen0012e9b2015-10-22 01:37:46 -0500345
Emily Shaffer71174412017-04-05 15:10:40 -0700346 // Fetch type from interface itself.
347 if (dbus_if.sensortype != 0)
348 {
349 type = dbus_if.sensortype;
350 }
351 // Legacy codebase does not populate type during initial handling:
352 else if (strstr(dbus_if.interface, "InventoryItem")) {
Chris Austen0012e9b2015-10-22 01:37:46 -0500353 // InventoryItems are real frus. So need to get the
354 // fru_type property
Emily Shaffer391f3302017-04-03 10:27:08 -0700355 type = dbus_to_sensor_type_from_dbus(&dbus_if);
Chris Austen0012e9b2015-10-22 01:37:46 -0500356 } else {
357 // Non InventoryItems
Emily Shaffer391f3302017-04-03 10:27:08 -0700358 p = strrchr (dbus_if.path, '/');
Brad Bishop56003452016-10-05 21:49:19 -0400359 type = dbus_to_sensor_type(p+1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500360 }
361
Brad Bishop56003452016-10-05 21:49:19 -0400362 return type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500363 }
364
Emily Shaffer391f3302017-04-03 10:27:08 -0700365// Replaces find_sensor
366uint8_t find_type_for_sensor_number(uint8_t num) {
367 int r;
368 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700369 r = find_openbmc_path(num, &dbus_if);
Emily Shaffer391f3302017-04-03 10:27:08 -0700370 if (r < 0) {
371 fprintf(stderr, "Could not find sensor %d\n", num);
372 return r;
373 }
374 return get_type_from_interface(dbus_if);
375}
376
Chris Austen10ccc0f2015-12-10 18:27:04 -0600377
378
379
380
Chris Austen0012e9b2015-10-22 01:37:46 -0500381ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
382 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500383 ipmi_data_len_t data_len, ipmi_context_t context)
384{
385 sensor_data_t *reqptr = (sensor_data_t*)request;
386 ipmi_ret_t rc = IPMI_CC_OK;
387
388 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum);
389
390 // TODO Not sure what the System-event-sensor is suppose to return
391 // need to ask Hostboot team
392 unsigned char buf[] = {0x00,0x6F};
393
Emily Shaffer391f3302017-04-03 10:27:08 -0700394 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500395
396 // HACK UNTIL Dbus gets updated or we find a better way
397 if (buf[0] == 0) {
Chris Austen800ba712015-12-03 15:31:00 -0600398 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500399 }
400
Chris Austenac4604a2015-10-13 12:43:27 -0500401
402 *data_len = sizeof(buf);
403 memcpy(response, &buf, *data_len);
404
Chris Austenac4604a2015-10-13 12:43:27 -0500405 return rc;
406}
407
Tom Josephbe703f72017-03-09 12:34:35 +0530408ipmi_ret_t setSensorReading(void *request)
409{
410 auto cmdData = static_cast<SetSensorReadingReq *>(request);
Chris Austenac4604a2015-10-13 12:43:27 -0500411
Tom Josephbe703f72017-03-09 12:34:35 +0530412 auto assertionStates =
413 (static_cast<uint16_t>(cmdData->assertOffset8_14)) << 8 |
414 cmdData->assertOffset0_7;
415
416 auto deassertionStates =
417 (static_cast<uint16_t>(cmdData->deassertOffset8_14)) << 8 |
418 cmdData->deassertOffset0_7;
419
420 std::bitset<16> assertionSet(assertionStates);
421 std::bitset<16> deassertionSet(deassertionStates);
422
423 // Check if the Sensor Number is present
424 auto iter = sensors.find(cmdData->number);
425 if (iter == sensors.end())
426 {
427 return IPMI_CC_SENSOR_INVALID;
428 }
429
430 auto& interfaceList = iter->second.sensorInterfaces;
431 if (interfaceList.empty())
432 {
433 log<level::ERR>("Interface List empty for the sensor",
434 entry("Sensor Number = %d", cmdData->number));
435 return IPMI_CC_UNSPECIFIED_ERROR;
436 }
437
438 ipmi::sensor::ObjectMap objects;
439 ipmi::sensor::InterfaceMap interfaces;
440 for (const auto& interface : interfaceList)
441 {
442 for (const auto& property : interface.second)
443 {
444 ipmi::sensor::PropertyMap props;
445 bool valid = false;
446 for (const auto& value : property.second)
447 {
448 if (assertionSet.test(value.first))
449 {
450 props.emplace(property.first, value.second.assert);
451 valid = true;
452 }
453 else if (deassertionSet.test(value.first))
454 {
455 props.emplace(property.first, value.second.deassert);
456 valid = true;
457 }
458 }
459 if (valid)
460 {
461 interfaces.emplace(interface.first, std::move(props));
462 }
463 }
464 }
465 objects.emplace(iter->second.sensorPath, std::move(interfaces));
466
Patrick Williams4736b912017-04-18 11:50:57 -0500467 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
Tom Josephbe703f72017-03-09 12:34:35 +0530468 using namespace std::string_literals;
469 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
470 static const auto path = "/xyz/openbmc_project/inventory"s;
471 std::string service;
472
473 try
474 {
475 service = ipmi::getService(bus, intf, path);
476
477 // Update the inventory manager
478 auto pimMsg = bus.new_method_call(service.c_str(),
479 path.c_str(),
480 intf.c_str(),
481 "Notify");
482 pimMsg.append(std::move(objects));
483 auto inventoryMgrResponseMsg = bus.call(pimMsg);
484 if (inventoryMgrResponseMsg.is_method_error())
485 {
486 log<level::ERR>("Error in notify call");
487 return IPMI_CC_UNSPECIFIED_ERROR;
488 }
489 }
490 catch (const std::runtime_error& e)
491 {
492 log<level::ERR>(e.what());
493 return IPMI_CC_UNSPECIFIED_ERROR;
494 }
495
496 return IPMI_CC_OK;
497}
Chris Austenac4604a2015-10-13 12:43:27 -0500498
Chris Austen0012e9b2015-10-22 01:37:46 -0500499ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
500 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500501 ipmi_data_len_t data_len, ipmi_context_t context)
502{
Chris Austenac4604a2015-10-13 12:43:27 -0500503 sensor_data_t *reqptr = (sensor_data_t*)request;
Chris Austenac4604a2015-10-13 12:43:27 -0500504
Chris Austen0012e9b2015-10-22 01:37:46 -0500505 printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500506
Tom Josephbe703f72017-03-09 12:34:35 +0530507 /*
508 * This would support the Set Sensor Reading command for the presence
509 * and functional state of Processor, Core & DIMM. For the remaining
510 * sensors the existing support is invoked.
511 */
512 auto ipmiRC = setSensorReading(request);
513
514 if(ipmiRC == IPMI_CC_SENSOR_INVALID)
515 {
516 updateSensorRecordFromSSRAESC(reqptr);
517 ipmiRC = IPMI_CC_OK;
518 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500519
Chris Austenac4604a2015-10-13 12:43:27 -0500520 *data_len=0;
Tom Josephbe703f72017-03-09 12:34:35 +0530521 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500522}
523
Chris Austen10ccc0f2015-12-10 18:27:04 -0600524
525ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
526 ipmi_request_t request, ipmi_response_t response,
527 ipmi_data_len_t data_len, ipmi_context_t context)
528{
529 sensor_data_t *reqptr = (sensor_data_t*)request;
530 ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID;
531 uint8_t type;
532 sensorreadingresp_t *resp = (sensorreadingresp_t*) response;
533 int r;
534 dbus_interface_t a;
535 sd_bus *bus = ipmid_get_sd_bus_connection();
536 sd_bus_message *reply = NULL;
Adriana Kobylak93125982016-03-01 12:48:10 -0600537 int reading = 0;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600538
539
540 printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum);
541
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700542 r = find_openbmc_path(reqptr->sennum, &a);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600543
Nan Li36deb762016-05-12 10:23:41 +0800544 if (r < 0) {
545 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
546 return IPMI_CC_SENSOR_INVALID;
547 }
548
Emily Shaffer391f3302017-04-03 10:27:08 -0700549 type = get_type_from_interface(a);
Brad Bishop56003452016-10-05 21:49:19 -0400550 if(type == 0) {
551 fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
552 return IPMI_CC_SENSOR_INVALID;
553 }
Chris Austen10ccc0f2015-12-10 18:27:04 -0600554
555 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);
556
557 *data_len=0;
558
Emily Shaffer1fabf222017-04-05 08:53:21 -0700559 int64_t raw_value, scale;
560 ipmi::sensor::Info sensor;
561
Chris Austen10ccc0f2015-12-10 18:27:04 -0600562 switch(type) {
563 case 0xC3:
564 case 0xC2:
Adriana Kobylak93125982016-03-01 12:48:10 -0600565 r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i");
Chris Austen10ccc0f2015-12-10 18:27:04 -0600566 if (r < 0) {
567 fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r));
568 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
569 a.bus, a.path, a.interface);
570 break;
571 }
572
Adriana Kobylak93125982016-03-01 12:48:10 -0600573 r = sd_bus_message_read(reply, "i", &reading);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600574 if (r < 0) {
Adriana Kobylak93125982016-03-01 12:48:10 -0600575 fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r));
Chris Austen10ccc0f2015-12-10 18:27:04 -0600576 break;
577 }
578
579 printf("Contents of a 0x%02x is 0x%02x\n", type, reading);
580
581 rc = IPMI_CC_OK;
582 *data_len=sizeof(sensorreadingresp_t);
583
Adriana Kobylak93125982016-03-01 12:48:10 -0600584 resp->value = (uint8_t)reading;
Chris Austen10ccc0f2015-12-10 18:27:04 -0600585 resp->operation = 0;
586 resp->indication[0] = 0;
587 resp->indication[1] = 0;
588 break;
589
Emily Shaffer1fabf222017-04-05 08:53:21 -0700590 case IPMI_SENSOR_TEMP:
591 case IPMI_SENSOR_VOLTAGE:
592 case IPMI_SENSOR_CURRENT:
593 case IPMI_SENSOR_FAN:
594 // Get reading for /xyz/openbmc_project/Sensor/Value.interface
595
596 // Get value
597 r = sd_bus_get_property_trivial(bus,
598 a.bus,
599 a.path,
600 a.interface,
601 "Value",
602 NULL,
603 'x',
604 &raw_value);
605 if (r < 0) {
606 fprintf(stderr,
607 "Failed to call sd_bus_get_property:%d, %s, 'value'\n",
608 r,
609 strerror(-r));
610 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
611 a.bus, a.path, a.interface);
612 break;
613 }
614
615 // Get scale
616 r = sd_bus_get_property_trivial(bus,
617 a.bus,
618 a.path,
619 a.interface,
620 "Scale",
621 NULL,
622 'x',
623 &scale);
624 if (r < 0) {
625 fprintf(stderr,
626 "Failed to call sd_bus_get_property:%d, %s (scale)\n",
627 r,
628 strerror(-r));
629 fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n",
630 a.bus, a.path, a.interface);
631 break;
632 }
633
634 resp->value = raw_value * pow(10,scale);
635 resp->operation = 0;
636 resp->indication[0] = 0;
637 resp->indication[1] = 0;
638 rc = IPMI_CC_OK;
639 *data_len=sizeof(sensorreadingresp_t);
640 break;
641
Chris Austen10ccc0f2015-12-10 18:27:04 -0600642 default:
643 *data_len=0;
644 rc = IPMI_CC_SENSOR_INVALID;
645 break;
646 }
647
648
vishwa1eaea4f2016-02-26 11:57:40 -0600649 reply = sd_bus_message_unref(reply);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600650
651 return rc;
652}
653
Chris Austen0012e9b2015-10-22 01:37:46 -0500654ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
655 ipmi_request_t request, ipmi_response_t response,
Chris Austenac4604a2015-10-13 12:43:27 -0500656 ipmi_data_len_t data_len, ipmi_context_t context)
657{
Nan Li70aa8d92016-08-29 00:11:10 +0800658 ipmi_ret_t rc = IPMI_CC_INVALID;
Chris Austenac4604a2015-10-13 12:43:27 -0500659
660 printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd);
661 *data_len = 0;
662
663 return rc;
664}
665
Emily Shafferd06e0e72017-04-05 09:08:57 -0700666ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
667 ipmi_request_t request,
668 ipmi_response_t response,
669 ipmi_data_len_t data_len,
670 ipmi_context_t context)
671{
672 auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response);
673 if (request == nullptr ||
674 get_sdr_info::request::get_count(request) == false)
675 {
676 // Get Sensor Count
677 resp->count = sensors.size();
678 }
679 else
680 {
681 resp->count = 1;
682 }
683
684 // Multiple LUNs not supported.
685 namespace response = get_sdr_info::response;
686 response::set_lun_present(0, &(resp->luns_and_dynamic_population));
687 response::set_lun_not_present(1, &(resp->luns_and_dynamic_population));
688 response::set_lun_not_present(2, &(resp->luns_and_dynamic_population));
689 response::set_lun_not_present(3, &(resp->luns_and_dynamic_population));
690 response::set_static_population(&(resp->luns_and_dynamic_population));
691
692 *data_len = SDR_INFO_RESP_SIZE;
693
694 return IPMI_CC_OK;
695}
696
Emily Shaffera344afc2017-04-13 15:09:39 -0700697ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
698 ipmi_request_t request,
699 ipmi_response_t response,
700 ipmi_data_len_t data_len,
701 ipmi_context_t context)
702{
703 // A constant reservation ID is okay until we implement add/remove SDR.
704 const uint16_t reservation_id = 1;
705 *(uint16_t*)response = reservation_id;
706
707 printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response);
708 return IPMI_CC_OK;
709}
Chris Austenac4604a2015-10-13 12:43:27 -0500710
711void register_netfn_sen_functions()
712{
Tom05732372016-09-06 17:21:23 +0530713 // <Wildcard Command>
Emily Shaffera344afc2017-04-13 15:09:39 -0700714 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
715 NETFUN_SENSOR, IPMI_CMD_WILDCARD);
716 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD,
717 nullptr, ipmi_sen_wildcard,
Tom05732372016-09-06 17:21:23 +0530718 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500719
Tom05732372016-09-06 17:21:23 +0530720 // <Get Sensor Type>
Emily Shaffera344afc2017-04-13 15:09:39 -0700721 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
722 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE);
723 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE,
724 nullptr, ipmi_sen_get_sensor_type,
Tom05732372016-09-06 17:21:23 +0530725 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500726
Tom05732372016-09-06 17:21:23 +0530727 // <Set Sensor Reading and Event Status>
Emily Shaffera344afc2017-04-13 15:09:39 -0700728 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
729 NETFUN_SENSOR, IPMI_CMD_SET_SENSOR);
730 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR,
731 nullptr, ipmi_sen_set_sensor,
Tom05732372016-09-06 17:21:23 +0530732 PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -0500733
Tom05732372016-09-06 17:21:23 +0530734 // <Get Sensor Reading>
Emily Shaffera344afc2017-04-13 15:09:39 -0700735 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
736 NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING);
737 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING,
738 nullptr, ipmi_sen_get_sensor_reading,
739 PRIVILEGE_USER);
740
741 // <Reserve SDR>
742 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",
743 NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO);
744 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO,
745 nullptr, ipmi_sen_reserve_sdr,
746 PRIVILEGE_USER);
Chris Austen10ccc0f2015-12-10 18:27:04 -0600747
Emily Shafferd06e0e72017-04-05 09:08:57 -0700748 // <Get SDR Info>
Emily Shaffera344afc2017-04-13 15:09:39 -0700749 printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n",
750 NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO);
751 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO,
752 nullptr, ipmi_sen_get_sdr_info,
753 PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -0500754 return;
755}