Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 1 | #include <mapper.h> |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 2 | #include <math.h> |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <string.h> |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 5 | #include <set> |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 6 | #include <bitset> |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 8 | #include <systemd/sd-bus.h> |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 9 | #include "host-ipmid/ipmid-api.h" |
| 10 | #include <phosphor-logging/log.hpp> |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 12 | #include "fruread.hpp" |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 13 | #include "ipmid.hpp" |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 14 | #include "sensorhandler.h" |
| 15 | #include "types.hpp" |
| 16 | #include "utils.hpp" |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 17 | #include "xyz/openbmc_project/Common/error.hpp" |
| 18 | |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 19 | static constexpr uint8_t fruInventoryDevice = 0x10; |
| 20 | static constexpr uint8_t IPMIFruInventory = 0x02; |
| 21 | static constexpr uint8_t BMCSlaveAddress = 0x20; |
| 22 | |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 23 | extern int updateSensorRecordFromSSRAESC(const void *); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 24 | extern sd_bus *bus; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 25 | extern const ipmi::sensor::IdInfoMap sensors; |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 26 | extern const FruMap frus; |
| 27 | |
| 28 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 29 | using namespace phosphor::logging; |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 30 | using InternalFailure = |
| 31 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 32 | |
| 33 | void register_netfn_sen_functions() __attribute__((constructor)); |
| 34 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 35 | struct sensorTypemap_t { |
| 36 | uint8_t number; |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 37 | uint8_t typecode; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 38 | char dbusname[32]; |
| 39 | } ; |
| 40 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 41 | sensorTypemap_t g_SensorTypeMap[] = { |
| 42 | |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 43 | {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 Austen | 800ba71 | 2015-12-03 15:31:00 -0600 | [diff] [blame] | 53 | {0x12, 0x6F, "SYSTEM_EVENT"}, |
| 54 | {0xC7, 0x03, "SYSTEM"}, |
| 55 | {0xC7, 0x03, "MAIN_PLANAR"}, |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 56 | {0xC2, 0x6F, "PowerCap"}, |
Tom Joseph | 558184e | 2017-09-01 13:45:05 +0530 | [diff] [blame] | 57 | {0x0b, 0xCA, "PowerSupplyRedundancy"}, |
Jayanth Othayoth | 0661beb | 2017-03-22 06:00:58 -0500 | [diff] [blame] | 58 | {0xDA, 0x03, "TurboAllowed"}, |
Tom Joseph | 558184e | 2017-09-01 13:45:05 +0530 | [diff] [blame] | 59 | {0xD8, 0xC8, "PowerSupplyDerating"}, |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 60 | {0xFF, 0x00, ""}, |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 64 | struct sensor_data_t { |
| 65 | uint8_t sennum; |
| 66 | } __attribute__ ((packed)) ; |
| 67 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 68 | struct sensorreadingresp_t { |
| 69 | uint8_t value; |
| 70 | uint8_t operation; |
| 71 | uint8_t indication[2]; |
| 72 | } __attribute__ ((packed)) ; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 73 | |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 74 | // 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 |
| 77 | int 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 | |
| 116 | final: |
| 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 Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 125 | int get_bus_for_path(const char *path, char **busname) { |
| 126 | return mapper_get_service(bus, path, busname); |
| 127 | } |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 128 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 129 | int legacy_dbus_openbmc_path(const char *type, const uint8_t num, dbus_interface_t *interface) { |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 130 | 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 Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 139 | r = get_bus_for_path(objname, &busname); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 140 | if (r < 0) { |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 141 | fprintf(stderr, "Failed to get %s busname: %s\n", |
| 142 | objname, strerror(-r)); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 143 | 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 Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 159 | r = get_bus_for_path(str2, &str1); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 160 | if (r < 0) { |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 161 | fprintf(stderr, "Failed to get %s busname: %s\n", |
| 162 | str2, strerror(-r)); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 163 | 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 Shaffer | 7117441 | 2017-04-05 15:10:40 -0700 | [diff] [blame] | 171 | // 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; |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 174 | |
| 175 | final: |
| 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 Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 185 | // 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 |
| 188 | int 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 Williams | 8451edf | 2017-06-13 09:01:06 -0500 | [diff] [blame] | 201 | char* busname = nullptr; |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 202 | 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 Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 217 | strcpy(interface->interface, info.propertyInterfaces.begin()->first.c_str()); |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 218 | interface->sensornumber = num; |
| 219 | |
| 220 | final: |
| 221 | free(busname); |
| 222 | return rc; |
| 223 | } |
| 224 | |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 225 | |
| 226 | ///////////////////////////////////////////////////////////////////// |
| 227 | // |
| 228 | // Routines used by ipmi commands wanting to interact on the dbus |
| 229 | // |
| 230 | ///////////////////////////////////////////////////////////////////// |
| 231 | int 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 Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 242 | r = find_openbmc_path(number, &a); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 243 | |
| 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 | |
| 267 | final: |
| 268 | sd_bus_error_free(&error); |
| 269 | m = sd_bus_message_unref(m); |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | int 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 Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 284 | r = find_openbmc_path(number, &a); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 285 | |
| 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 | |
| 309 | final: |
| 310 | sd_bus_error_free(&error); |
| 311 | m = sd_bus_message_unref(m); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 316 | uint8_t dbus_to_sensor_type(char *p) { |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 317 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 318 | sensorTypemap_t *s = g_SensorTypeMap; |
| 319 | char r=0; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 320 | while (s->number != 0xFF) { |
| 321 | if (!strcmp(s->dbusname,p)) { |
Tom Joseph | 558184e | 2017-09-01 13:45:05 +0530 | [diff] [blame] | 322 | r = s->typecode; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 323 | break; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 324 | } |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 325 | s++; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 328 | if (s->number == 0xFF) |
| 329 | printf("Failed to find Sensor Type %s\n", p); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 330 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 331 | return r; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 332 | } |
| 333 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 334 | |
| 335 | uint8_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 Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 348 | uint8_t get_type_from_interface(dbus_interface_t dbus_if) { |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 349 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 350 | char *p; |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 351 | uint8_t type; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 352 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 353 | // 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 Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 356 | if (dbus_if.interface[0] == 0) { return 0;} |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 357 | |
Emily Shaffer | 7117441 | 2017-04-05 15:10:40 -0700 | [diff] [blame] | 358 | // 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 Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 365 | // InventoryItems are real frus. So need to get the |
| 366 | // fru_type property |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 367 | type = dbus_to_sensor_type_from_dbus(&dbus_if); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 368 | } else { |
| 369 | // Non InventoryItems |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 370 | p = strrchr (dbus_if.path, '/'); |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 371 | type = dbus_to_sensor_type(p+1); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 372 | } |
| 373 | |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 374 | return type; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 375 | } |
| 376 | |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 377 | // Replaces find_sensor |
| 378 | uint8_t find_type_for_sensor_number(uint8_t num) { |
| 379 | int r; |
| 380 | dbus_interface_t dbus_if; |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 381 | r = find_openbmc_path(num, &dbus_if); |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 382 | 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 Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 389 | |
| 390 | |
| 391 | |
| 392 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 393 | ipmi_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 Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 395 | 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 Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 406 | buf[0] = find_type_for_sensor_number(reqptr->sennum); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 407 | |
| 408 | // HACK UNTIL Dbus gets updated or we find a better way |
| 409 | if (buf[0] == 0) { |
Chris Austen | 800ba71 | 2015-12-03 15:31:00 -0600 | [diff] [blame] | 410 | rc = IPMI_CC_SENSOR_INVALID; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 411 | } |
| 412 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 413 | |
| 414 | *data_len = sizeof(buf); |
| 415 | memcpy(response, &buf, *data_len); |
| 416 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 417 | return rc; |
| 418 | } |
| 419 | |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 420 | const std::set<std::string> analogSensorInterfaces = |
| 421 | { |
| 422 | "xyz.openbmc_project.Sensor.Value", |
Patrick Venture | e9a6405 | 2017-08-18 19:17:27 -0700 | [diff] [blame] | 423 | "xyz.openbmc_project.Control.FanPwm", |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
| 426 | bool isAnalogSensor(const std::string& interface) |
| 427 | { |
| 428 | return (analogSensorInterfaces.count(interface)); |
| 429 | } |
| 430 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 431 | ipmi_ret_t setSensorReading(void *request) |
| 432 | { |
Tom Joseph | 816e92b | 2017-09-06 19:23:00 +0530 | [diff] [blame] | 433 | ipmi::sensor::SetSensorReadingReq cmdData = |
| 434 | *(static_cast<ipmi::sensor::SetSensorReadingReq *>(request)); |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 435 | |
| 436 | // Check if the Sensor Number is present |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 437 | const auto iter = sensors.find(cmdData.number); |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 438 | if (iter == sensors.end()) |
| 439 | { |
| 440 | return IPMI_CC_SENSOR_INVALID; |
| 441 | } |
| 442 | |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 443 | 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 Joseph | 8202432 | 2017-09-28 20:07:29 +0530 | [diff] [blame] | 453 | catch (const std::runtime_error& e) |
| 454 | { |
| 455 | log<level::ERR>(e.what()); |
| 456 | } |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 457 | |
| 458 | return IPMI_CC_UNSPECIFIED_ERROR; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 459 | } |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 460 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 461 | ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 462 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 463 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 464 | { |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 465 | sensor_data_t *reqptr = (sensor_data_t*)request; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 466 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 467 | printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 468 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 469 | /* |
| 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 Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 481 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 482 | *data_len=0; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 483 | return ipmiRC; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 484 | } |
| 485 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 486 | |
| 487 | ipmi_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 Joseph | 40c35b1 | 2017-09-07 02:04:42 +0530 | [diff] [blame] | 493 | uint8_t type = 0; |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 494 | 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 Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 499 | int reading = 0; |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 500 | char* assertion = NULL; |
Tom Joseph | c6162fb | 2018-01-25 01:55:20 +0530 | [diff] [blame] | 501 | ipmi::sensor::GetSensorResponse getResponse {}; |
| 502 | static constexpr auto scanningEnabledBit = 6; |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 503 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 504 | printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum); |
| 505 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 506 | r = find_openbmc_path(reqptr->sennum, &a); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 507 | |
Tom Joseph | 40c35b1 | 2017-09-07 02:04:42 +0530 | [diff] [blame] | 508 | if (r < 0) |
| 509 | { |
Nan Li | 36deb76 | 2016-05-12 10:23:41 +0800 | [diff] [blame] | 510 | fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum); |
Nan Li | 36deb76 | 2016-05-12 10:23:41 +0800 | [diff] [blame] | 511 | } |
Tom Joseph | 40c35b1 | 2017-09-07 02:04:42 +0530 | [diff] [blame] | 512 | 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 Li | 36deb76 | 2016-05-12 10:23:41 +0800 | [diff] [blame] | 519 | |
Tom Joseph | 40c35b1 | 2017-09-07 02:04:42 +0530 | [diff] [blame] | 520 | fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, |
| 521 | a.interface); |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 522 | } |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 523 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 524 | *data_len=0; |
| 525 | |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 526 | int64_t raw_value; |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 527 | ipmi::sensor::Info sensor; |
| 528 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 529 | switch(type) { |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 530 | case 0xC2: |
Dhruvaraj Subhashchandran | d12ae75 | 2017-10-04 00:33:27 -0500 | [diff] [blame] | 531 | case 0xC8: |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 532 | r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "i"); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 533 | if (r < 0) { |
| 534 | fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r)); |
| 535 | fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", |
| 536 | a.bus, a.path, a.interface); |
| 537 | break; |
| 538 | } |
| 539 | |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 540 | r = sd_bus_message_read(reply, "i", &reading); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 541 | if (r < 0) { |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 542 | fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r)); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 543 | break; |
| 544 | } |
| 545 | |
| 546 | printf("Contents of a 0x%02x is 0x%02x\n", type, reading); |
| 547 | |
| 548 | rc = IPMI_CC_OK; |
| 549 | *data_len=sizeof(sensorreadingresp_t); |
| 550 | |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 551 | resp->value = (uint8_t)reading; |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 552 | resp->operation = 0; |
| 553 | resp->indication[0] = 0; |
| 554 | resp->indication[1] = 0; |
| 555 | break; |
| 556 | |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 557 | //TODO openbmc/openbmc#2154 Move this sensor to right place. |
| 558 | case 0xCA: |
| 559 | r = sd_bus_get_property(bus,a.bus, a.path, a.interface, "value", NULL, &reply, "s"); |
| 560 | if (r < 0) { |
| 561 | fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r)); |
| 562 | fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", |
| 563 | a.bus, a.path, a.interface); |
| 564 | break; |
| 565 | } |
| 566 | |
| 567 | r = sd_bus_message_read(reply, "s", &assertion); |
| 568 | if (r < 0) { |
| 569 | fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r)); |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | rc = IPMI_CC_OK; |
| 574 | *data_len=sizeof(sensorreadingresp_t); |
| 575 | |
| 576 | resp->value = 0; |
| 577 | resp->operation = 0; |
| 578 | if (strcmp(assertion,"Enabled") == 0) |
| 579 | { |
| 580 | resp->indication[0] = 0x02; |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | resp->indication[0] = 0x1; |
| 585 | } |
| 586 | resp->indication[1] = 0; |
Dhruvaraj Subhashchandran | 6244f93 | 2017-11-23 01:08:46 -0600 | [diff] [blame] | 587 | break; |
| 588 | |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 589 | case IPMI_SENSOR_TEMP: |
| 590 | case IPMI_SENSOR_VOLTAGE: |
| 591 | case IPMI_SENSOR_CURRENT: |
| 592 | case IPMI_SENSOR_FAN: |
| 593 | // Get reading for /xyz/openbmc_project/Sensor/Value.interface |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 594 | if(sensors.find(reqptr->sennum) == sensors.end()) |
| 595 | { |
| 596 | fprintf(stderr, "Failed to find config entry for Sensor 0x%02x\n", |
| 597 | reqptr->sennum); |
| 598 | return IPMI_CC_SENSOR_INVALID; |
| 599 | } |
| 600 | |
| 601 | sensor = sensors.at(reqptr->sennum); |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 602 | if (ipmi::sensor::Mutability::Read != |
| 603 | (sensor.mutability & ipmi::sensor::Mutability::Read)) |
| 604 | { |
| 605 | log<level::ERR>("Sensor was not readable.\n"); |
| 606 | return IPMI_CC_SENSOR_INVALID; |
| 607 | } |
| 608 | |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 609 | |
| 610 | // Get value |
| 611 | r = sd_bus_get_property_trivial(bus, |
| 612 | a.bus, |
| 613 | a.path, |
| 614 | a.interface, |
| 615 | "Value", |
| 616 | NULL, |
| 617 | 'x', |
| 618 | &raw_value); |
| 619 | if (r < 0) { |
| 620 | fprintf(stderr, |
| 621 | "Failed to call sd_bus_get_property:%d, %s, 'value'\n", |
| 622 | r, |
| 623 | strerror(-r)); |
| 624 | fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", |
| 625 | a.bus, a.path, a.interface); |
| 626 | break; |
| 627 | } |
| 628 | |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 629 | // Prevent div0 |
| 630 | if (sensor.coefficientM == 0) { |
| 631 | sensor.coefficientM = 1; |
| 632 | }; |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 633 | |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 634 | resp->value = static_cast<uint8_t>( |
| 635 | (raw_value - sensor.scaledOffset) / sensor.coefficientM); |
Tom Joseph | c6162fb | 2018-01-25 01:55:20 +0530 | [diff] [blame] | 636 | resp->operation = 1 << scanningEnabledBit; // scanning enabled |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 637 | resp->indication[0] = 0; // not a threshold sensor. ignore |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 638 | resp->indication[1] = 0; |
| 639 | rc = IPMI_CC_OK; |
| 640 | *data_len=sizeof(sensorreadingresp_t); |
| 641 | break; |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 642 | default: |
Tom Joseph | 14c1546 | 2017-09-07 02:16:51 +0530 | [diff] [blame] | 643 | { |
| 644 | const auto iter = sensors.find(reqptr->sennum); |
| 645 | if (iter == sensors.end()) |
| 646 | { |
| 647 | return IPMI_CC_SENSOR_INVALID; |
| 648 | } |
| 649 | |
| 650 | try |
| 651 | { |
Tom Joseph | c6162fb | 2018-01-25 01:55:20 +0530 | [diff] [blame] | 652 | getResponse = iter->second.getFunc(iter->second); |
Tom Joseph | 14c1546 | 2017-09-07 02:16:51 +0530 | [diff] [blame] | 653 | *data_len = getResponse.size(); |
| 654 | memcpy(resp, getResponse.data(), *data_len); |
Tom Joseph | c6162fb | 2018-01-25 01:55:20 +0530 | [diff] [blame] | 655 | resp->operation = 1 << scanningEnabledBit; |
Tom Joseph | 14c1546 | 2017-09-07 02:16:51 +0530 | [diff] [blame] | 656 | return IPMI_CC_OK; |
| 657 | } |
| 658 | catch (InternalFailure& e) |
| 659 | { |
Tom Joseph | c6162fb | 2018-01-25 01:55:20 +0530 | [diff] [blame] | 660 | *data_len = getResponse.size(); |
| 661 | memcpy(resp, getResponse.data(), *data_len); |
| 662 | return IPMI_CC_OK; |
Tom Joseph | 14c1546 | 2017-09-07 02:16:51 +0530 | [diff] [blame] | 663 | } |
Tom Joseph | 8202432 | 2017-09-28 20:07:29 +0530 | [diff] [blame] | 664 | catch (const std::runtime_error& e) |
| 665 | { |
Tom Joseph | c6162fb | 2018-01-25 01:55:20 +0530 | [diff] [blame] | 666 | *data_len = getResponse.size(); |
| 667 | memcpy(resp, getResponse.data(), *data_len); |
| 668 | return IPMI_CC_OK; |
Tom Joseph | 8202432 | 2017-09-28 20:07:29 +0530 | [diff] [blame] | 669 | } |
Tom Joseph | 14c1546 | 2017-09-07 02:16:51 +0530 | [diff] [blame] | 670 | } |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 671 | } |
| 672 | |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 673 | reply = sd_bus_message_unref(reply); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 674 | |
| 675 | return rc; |
| 676 | } |
| 677 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 678 | ipmi_ret_t ipmi_sen_get_sensor_thresholds(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 679 | ipmi_request_t request, ipmi_response_t response, |
| 680 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 681 | { |
| 682 | constexpr auto warningThresholdInterface = |
| 683 | "xyz.openbmc_project.Sensor.Threshold.Warning"; |
| 684 | constexpr auto criticalThresholdInterface = |
| 685 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 686 | constexpr auto valueInterface = |
| 687 | "xyz.openbmc_project.Sensor.Value"; |
| 688 | constexpr auto sensorRoot = "/xyz/openbmc_project/sensors"; |
| 689 | |
| 690 | ipmi::sensor::Thresholds thresholds = |
| 691 | { |
| 692 | { |
| 693 | warningThresholdInterface, |
| 694 | { |
| 695 | { |
| 696 | "WarningLow", |
| 697 | ipmi::sensor::ThresholdMask::NON_CRITICAL_LOW_MASK, |
| 698 | ipmi::sensor::ThresholdIndex::NON_CRITICAL_LOW_IDX |
| 699 | }, |
| 700 | { |
| 701 | "WarningHigh", |
| 702 | ipmi::sensor::ThresholdMask::NON_CRITICAL_HIGH_MASK, |
| 703 | ipmi::sensor::ThresholdIndex::NON_CRITICAL_HIGH_IDX |
| 704 | } |
| 705 | } |
| 706 | }, |
| 707 | { |
| 708 | criticalThresholdInterface, |
| 709 | { |
| 710 | { |
| 711 | "CriticalLow", |
| 712 | ipmi::sensor::ThresholdMask::CRITICAL_LOW_MASK, |
| 713 | ipmi::sensor::ThresholdIndex::CRITICAL_LOW_IDX |
| 714 | }, |
| 715 | { |
| 716 | "CriticalHigh", |
| 717 | ipmi::sensor::ThresholdMask::CRITICAL_HIGH_MASK, |
| 718 | ipmi::sensor::ThresholdIndex::CRITICAL_HIGH_IDX |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | }; |
| 723 | |
| 724 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 725 | |
| 726 | if (*data_len != sizeof(uint8_t)) |
| 727 | { |
| 728 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 729 | } |
| 730 | auto sensorNum = *(reinterpret_cast<uint8_t*>(request)); |
| 731 | |
| 732 | auto responseData = |
| 733 | reinterpret_cast<get_sdr::GetSensorThresholdsResponse*>(response); |
| 734 | |
| 735 | responseData->validMask = 0; |
| 736 | |
| 737 | const auto iter = sensors.find(sensorNum); |
| 738 | if (iter == sensors.end()) |
| 739 | { |
| 740 | return IPMI_CC_SENSOR_INVALID; |
| 741 | } |
| 742 | |
| 743 | const auto sensorInfo = iter->second; |
| 744 | |
| 745 | //Proceed only if the sensor value interface is implemented. |
| 746 | if (sensorInfo.propertyInterfaces.find(valueInterface) == |
| 747 | sensorInfo.propertyInterfaces.end()) |
| 748 | { |
| 749 | //return with valid mask as 0 |
| 750 | return IPMI_CC_OK; |
| 751 | } |
| 752 | |
| 753 | std::string service; |
| 754 | try |
| 755 | { |
| 756 | service = ipmi::getService(bus, |
| 757 | sensorInfo.sensorInterface, |
| 758 | sensorInfo.sensorPath); |
| 759 | } |
| 760 | catch (const std::runtime_error& e) |
| 761 | { |
| 762 | log<level::ERR>(e.what()); |
| 763 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 764 | } |
| 765 | |
| 766 | //prevent divide by 0 |
| 767 | auto coefficientM = |
| 768 | sensorInfo.coefficientM ? sensorInfo.coefficientM : 1; |
| 769 | |
| 770 | try |
| 771 | { |
| 772 | auto mngObjects = ipmi::getManagedObjects(bus, |
| 773 | service, |
| 774 | sensorRoot); |
| 775 | |
| 776 | auto senIter = mngObjects.find(sensorInfo.sensorPath); |
| 777 | if (senIter == mngObjects.end()) |
| 778 | { |
| 779 | return IPMI_CC_SENSOR_INVALID; |
| 780 | } |
| 781 | |
| 782 | for (const auto& threshold : thresholds) |
| 783 | { |
| 784 | auto thresholdType = senIter->second.find(threshold.first); |
| 785 | if (thresholdType != senIter->second.end()) |
| 786 | { |
| 787 | for (const auto& threshLevel : threshold.second) |
| 788 | { |
| 789 | auto val = thresholdType-> |
| 790 | second[threshLevel.property].get<int64_t>(); |
| 791 | if (val != 0) |
| 792 | { |
| 793 | auto idx = static_cast<uint8_t>(threshLevel.idx); |
| 794 | responseData->data[idx] = static_cast<uint8_t>( |
| 795 | (val - sensorInfo.scaledOffset) / coefficientM); |
| 796 | responseData->validMask |= |
| 797 | static_cast<uint8_t>(threshLevel.maskValue); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | catch (InternalFailure& e) |
| 804 | { |
| 805 | //Not able to get the values, reset the mask. |
| 806 | responseData->validMask = 0; |
| 807 | } |
| 808 | |
| 809 | *data_len = sizeof(get_sdr::GetSensorThresholdsResponse); |
| 810 | return IPMI_CC_OK; |
| 811 | } |
| 812 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 813 | ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 814 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 815 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 816 | { |
Nan Li | 70aa8d9 | 2016-08-29 00:11:10 +0800 | [diff] [blame] | 817 | ipmi_ret_t rc = IPMI_CC_INVALID; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 818 | |
| 819 | printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd); |
| 820 | *data_len = 0; |
| 821 | |
| 822 | return rc; |
| 823 | } |
| 824 | |
Emily Shaffer | d06e0e7 | 2017-04-05 09:08:57 -0700 | [diff] [blame] | 825 | ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 826 | ipmi_request_t request, |
| 827 | ipmi_response_t response, |
| 828 | ipmi_data_len_t data_len, |
| 829 | ipmi_context_t context) |
| 830 | { |
| 831 | auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response); |
| 832 | if (request == nullptr || |
| 833 | get_sdr_info::request::get_count(request) == false) |
| 834 | { |
| 835 | // Get Sensor Count |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 836 | resp->count = sensors.size() + frus.size(); |
Emily Shaffer | d06e0e7 | 2017-04-05 09:08:57 -0700 | [diff] [blame] | 837 | } |
| 838 | else |
| 839 | { |
| 840 | resp->count = 1; |
| 841 | } |
| 842 | |
| 843 | // Multiple LUNs not supported. |
| 844 | namespace response = get_sdr_info::response; |
| 845 | response::set_lun_present(0, &(resp->luns_and_dynamic_population)); |
| 846 | response::set_lun_not_present(1, &(resp->luns_and_dynamic_population)); |
| 847 | response::set_lun_not_present(2, &(resp->luns_and_dynamic_population)); |
| 848 | response::set_lun_not_present(3, &(resp->luns_and_dynamic_population)); |
| 849 | response::set_static_population(&(resp->luns_and_dynamic_population)); |
| 850 | |
| 851 | *data_len = SDR_INFO_RESP_SIZE; |
| 852 | |
| 853 | return IPMI_CC_OK; |
| 854 | } |
| 855 | |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 856 | ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 857 | ipmi_request_t request, |
| 858 | ipmi_response_t response, |
| 859 | ipmi_data_len_t data_len, |
| 860 | ipmi_context_t context) |
| 861 | { |
| 862 | // A constant reservation ID is okay until we implement add/remove SDR. |
| 863 | const uint16_t reservation_id = 1; |
| 864 | *(uint16_t*)response = reservation_id; |
Emily Shaffer | 5a5a628 | 2017-08-15 11:17:17 -0700 | [diff] [blame] | 865 | *data_len = sizeof(uint16_t); |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 866 | |
| 867 | printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response); |
| 868 | return IPMI_CC_OK; |
| 869 | } |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 870 | |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 871 | void setUnitFieldsForObject(sd_bus *bus, |
| 872 | const dbus_interface_t &iface, |
| 873 | const ipmi::sensor::Info *info, |
| 874 | get_sdr::SensorDataFullRecordBody *body) |
| 875 | { |
| 876 | if (info->propertyInterfaces.begin()->first == |
| 877 | "xyz.openbmc_project.Sensor.Value") |
| 878 | { |
| 879 | std::string result {}; |
Emily Shaffer | 6223561 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 880 | if (info->unit.empty()) |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 881 | { |
Emily Shaffer | 6223561 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 882 | char *raw_cstr = NULL; |
| 883 | if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path, |
| 884 | iface.interface, "Unit", NULL, |
| 885 | &raw_cstr)) |
| 886 | { |
| 887 | log<level::WARNING>("Unit interface missing.", |
| 888 | entry("BUS=%s", iface.bus), |
| 889 | entry("PATH=%s", iface.path)); |
| 890 | } |
| 891 | else |
| 892 | { |
| 893 | result = raw_cstr; |
| 894 | } |
| 895 | free(raw_cstr); |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 896 | } |
| 897 | else |
| 898 | { |
Emily Shaffer | 6223561 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 899 | result = info->unit; |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 900 | } |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 901 | |
| 902 | namespace server = sdbusplus::xyz::openbmc_project::Sensor::server; |
| 903 | try { |
| 904 | auto unit = server::Value::convertUnitFromString(result); |
| 905 | // Unit strings defined in |
| 906 | // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml |
| 907 | switch (unit) |
| 908 | { |
| 909 | case server::Value::Unit::DegreesC: |
| 910 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C; |
| 911 | break; |
| 912 | case server::Value::Unit::RPMS: |
| 913 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions |
| 914 | get_sdr::body::set_rate_unit(0b100, body); // per minute |
| 915 | break; |
| 916 | case server::Value::Unit::Volts: |
| 917 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS; |
| 918 | break; |
| 919 | case server::Value::Unit::Meters: |
| 920 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS; |
| 921 | break; |
| 922 | case server::Value::Unit::Amperes: |
| 923 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES; |
| 924 | break; |
| 925 | case server::Value::Unit::Joules: |
| 926 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES; |
| 927 | break; |
| 928 | default: |
| 929 | // Cannot be hit. |
| 930 | fprintf(stderr, "Unknown value unit type: = %s\n", result.c_str()); |
| 931 | } |
| 932 | } |
| 933 | catch (sdbusplus::exception::InvalidEnumString e) |
| 934 | { |
| 935 | log<level::WARNING>("Warning: no unit provided for sensor!"); |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | int64_t getScaleForObject(sd_bus *bus, |
| 941 | const dbus_interface_t& iface, |
| 942 | const ipmi::sensor::Info *info) |
| 943 | { |
| 944 | int64_t result = 0; |
| 945 | if (info->propertyInterfaces.begin()->first == |
| 946 | "xyz.openbmc_project.Sensor.Value") |
| 947 | { |
Emily Shaffer | 6223561 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 948 | if (info->hasScale) |
| 949 | { |
| 950 | result = info->scale; |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | if (0 > sd_bus_get_property_trivial(bus, |
| 955 | iface.bus, |
| 956 | iface.path, |
| 957 | iface.interface, |
| 958 | "Scale", |
| 959 | NULL, |
| 960 | 'x', |
| 961 | &result)) { |
| 962 | log<level::WARNING>("Scale interface missing.", |
| 963 | entry("BUS=%s", iface.bus), |
| 964 | entry("PATH=%s", iface.path)); |
| 965 | } |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | return result; |
| 970 | } |
| 971 | |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 972 | ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body, |
| 973 | const ipmi::sensor::Info *info, |
| 974 | ipmi_data_len_t data_len) |
| 975 | { |
| 976 | /* Functional sensor case */ |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 977 | if (isAnalogSensor(info->propertyInterfaces.begin()->first)) |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 978 | { |
| 979 | // Get bus |
| 980 | sd_bus *bus = ipmid_get_sd_bus_connection(); |
| 981 | dbus_interface_t iface; |
| 982 | |
| 983 | if (0 > find_openbmc_path(body->entity_id, &iface)) |
| 984 | return IPMI_CC_SENSOR_INVALID; |
| 985 | |
| 986 | body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a % |
| 987 | |
| 988 | /* Unit info */ |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 989 | setUnitFieldsForObject(bus, iface, info, body); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 990 | |
| 991 | /* Modifiers to reading info */ |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 992 | // Get scale |
Emily Shaffer | cc941e1 | 2017-06-14 13:06:26 -0700 | [diff] [blame] | 993 | int64_t scale = getScaleForObject(bus, iface, info); |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 994 | |
| 995 | get_sdr::body::set_b(info->coefficientB, body); |
| 996 | get_sdr::body::set_m(info->coefficientM, body); |
| 997 | get_sdr::body::set_b_exp(info->exponentB, body); |
| 998 | get_sdr::body::set_r_exp(scale, body); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 999 | |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1000 | get_sdr::body::set_id_type(0b00, body); // 00 = unicode |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Tom Joseph | 9642391 | 2018-01-25 00:14:34 +0530 | [diff] [blame] | 1003 | /* ID string */ |
| 1004 | auto id_string = info->sensorNameFunc(*info); |
| 1005 | |
| 1006 | if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH) |
| 1007 | { |
| 1008 | get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body); |
| 1009 | } |
| 1010 | else |
| 1011 | { |
| 1012 | get_sdr::body::set_id_strlen(id_string.length(), body); |
| 1013 | } |
| 1014 | strncpy(body->id_string, id_string.c_str(), |
| 1015 | get_sdr::body::get_id_strlen(body)); |
| 1016 | |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1017 | return IPMI_CC_OK; |
| 1018 | }; |
| 1019 | |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1020 | ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response, |
| 1021 | ipmi_data_len_t data_len) |
| 1022 | { |
| 1023 | auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request); |
| 1024 | auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response); |
| 1025 | get_sdr::SensorDataFruRecord record {}; |
| 1026 | auto dataLength = 0; |
| 1027 | |
| 1028 | auto fru = frus.begin(); |
| 1029 | uint8_t fruID {}; |
| 1030 | auto recordID = get_sdr::request::get_record_id(req); |
| 1031 | |
| 1032 | fruID = recordID - FRU_RECORD_ID_START; |
| 1033 | fru = frus.find(fruID); |
| 1034 | if (fru == frus.end()) |
| 1035 | { |
| 1036 | return IPMI_CC_SENSOR_INVALID; |
| 1037 | } |
| 1038 | |
| 1039 | /* Header */ |
| 1040 | get_sdr::header::set_record_id(recordID, &(record.header)); |
| 1041 | record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1 |
| 1042 | record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD; |
| 1043 | record.header.record_length = sizeof(record.key) + sizeof(record.body); |
| 1044 | |
| 1045 | /* Key */ |
| 1046 | record.key.fruID = fruID; |
| 1047 | record.key.accessLun |= IPMI_LOGICAL_FRU; |
| 1048 | record.key.deviceAddress = BMCSlaveAddress; |
| 1049 | |
| 1050 | /* Body */ |
| 1051 | record.body.entityID = fru->second[0].entityID; |
| 1052 | record.body.entityInstance = fru->second[0].entityInstance; |
| 1053 | record.body.deviceType = fruInventoryDevice; |
| 1054 | record.body.deviceTypeModifier = IPMIFruInventory; |
| 1055 | |
| 1056 | /* Device ID string */ |
| 1057 | auto deviceID = fru->second[0].path.substr( |
| 1058 | fru->second[0].path.find_last_of('/') + 1, |
| 1059 | fru->second[0].path.length()); |
| 1060 | |
| 1061 | |
| 1062 | if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH) |
| 1063 | { |
| 1064 | get_sdr::body::set_device_id_strlen( |
| 1065 | get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, |
| 1066 | &(record.body)); |
| 1067 | } |
| 1068 | else |
| 1069 | { |
| 1070 | get_sdr::body::set_device_id_strlen(deviceID.length(), |
| 1071 | &(record.body)); |
| 1072 | } |
| 1073 | |
| 1074 | strncpy(record.body.deviceID, deviceID.c_str(), |
| 1075 | get_sdr::body::get_device_id_strlen(&(record.body))); |
| 1076 | |
| 1077 | if (++fru == frus.end()) |
| 1078 | { |
| 1079 | get_sdr::response::set_next_record_id(END_OF_RECORD, resp); // last record |
| 1080 | } |
| 1081 | else |
| 1082 | { |
| 1083 | get_sdr::response::set_next_record_id( |
| 1084 | (FRU_RECORD_ID_START + fru->first), resp); |
| 1085 | } |
| 1086 | |
| 1087 | if (req->bytes_to_read > (sizeof(*resp) - req->offset)) |
| 1088 | { |
| 1089 | dataLength = (sizeof(*resp) - req->offset); |
| 1090 | } |
| 1091 | else |
| 1092 | { |
| 1093 | dataLength = req->bytes_to_read; |
| 1094 | } |
| 1095 | |
| 1096 | if (dataLength <= 0) |
| 1097 | { |
| 1098 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 1099 | } |
| 1100 | |
| 1101 | memcpy(resp->record_data, |
| 1102 | reinterpret_cast<uint8_t*>(&record) + req->offset, |
| 1103 | (dataLength)); |
| 1104 | |
| 1105 | *data_len = dataLength; |
| 1106 | *data_len += 2; // additional 2 bytes for next record ID |
| 1107 | |
| 1108 | return IPMI_CC_OK; |
| 1109 | } |
| 1110 | |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1111 | ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 1112 | ipmi_request_t request, ipmi_response_t response, |
| 1113 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 1114 | { |
| 1115 | ipmi_ret_t ret = IPMI_CC_OK; |
| 1116 | get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request; |
| 1117 | get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response; |
| 1118 | get_sdr::SensorDataFullRecord record = {0}; |
| 1119 | if (req != NULL) |
| 1120 | { |
| 1121 | // Note: we use an iterator so we can provide the next ID at the end of |
| 1122 | // the call. |
| 1123 | auto sensor = sensors.begin(); |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1124 | auto recordID = get_sdr::request::get_record_id(req); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1125 | |
| 1126 | // At the beginning of a scan, the host side will send us id=0. |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1127 | if (recordID != 0) |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1128 | { |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1129 | // recordID greater then 255,it means it is a FRU record. |
| 1130 | // Currently we are supporting two record types either FULL record |
| 1131 | // or FRU record. |
| 1132 | if (recordID >= FRU_RECORD_ID_START) |
| 1133 | { |
| 1134 | return ipmi_fru_get_sdr(request, response, data_len); |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| 1138 | sensor = sensors.find(recordID); |
| 1139 | if (sensor == sensors.end()) |
| 1140 | { |
| 1141 | return IPMI_CC_SENSOR_INVALID; |
| 1142 | } |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | uint8_t sensor_id = sensor->first; |
| 1147 | |
| 1148 | /* Header */ |
| 1149 | get_sdr::header::set_record_id(sensor_id, &(record.header)); |
| 1150 | record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1 |
| 1151 | record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD; |
| 1152 | record.header.record_length = sizeof(get_sdr::SensorDataFullRecord); |
| 1153 | |
| 1154 | /* Key */ |
Tom Joseph | 9642391 | 2018-01-25 00:14:34 +0530 | [diff] [blame] | 1155 | get_sdr::key::set_owner_id_bmc(&(record.key)); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1156 | record.key.sensor_number = sensor_id; |
| 1157 | |
| 1158 | /* Body */ |
Tom Joseph | 9642391 | 2018-01-25 00:14:34 +0530 | [diff] [blame] | 1159 | record.body.entity_id = sensor->second.entityType; |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1160 | record.body.sensor_type = sensor->second.sensorType; |
| 1161 | record.body.event_reading_type = sensor->second.sensorReadingType; |
Tom Joseph | 9642391 | 2018-01-25 00:14:34 +0530 | [diff] [blame] | 1162 | record.body.entity_instance = sensor->second.instance; |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1163 | |
| 1164 | // Set the type-specific details given the DBus interface |
| 1165 | ret = populate_record_from_dbus(&(record.body), &(sensor->second), |
| 1166 | data_len); |
| 1167 | |
| 1168 | if (++sensor == sensors.end()) |
| 1169 | { |
Ratan Gupta | e0cc855 | 2018-01-22 14:23:04 +0530 | [diff] [blame] | 1170 | // we have reached till end of sensor, so assign the next record id |
| 1171 | // to 256(Max Sensor ID = 255) + FRU ID(may start with 0). |
| 1172 | auto next_record_id = (frus.size()) ? |
| 1173 | frus.begin()->first + FRU_RECORD_ID_START : |
| 1174 | END_OF_RECORD; |
| 1175 | |
| 1176 | get_sdr::response::set_next_record_id(next_record_id, resp); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1177 | } |
| 1178 | else |
| 1179 | { |
| 1180 | get_sdr::response::set_next_record_id(sensor->first, resp); |
| 1181 | } |
| 1182 | |
| 1183 | *data_len = sizeof(get_sdr::GetSdrResp) - req->offset; |
| 1184 | memcpy(resp->record_data, (char*)&record + req->offset, |
| 1185 | sizeof(get_sdr::SensorDataFullRecord) - req->offset); |
| 1186 | } |
| 1187 | |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
| 1191 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1192 | void register_netfn_sen_functions() |
| 1193 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1194 | // <Wildcard Command> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 1195 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 1196 | NETFUN_SENSOR, IPMI_CMD_WILDCARD); |
| 1197 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, |
| 1198 | nullptr, ipmi_sen_wildcard, |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1199 | PRIVILEGE_USER); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1200 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1201 | // <Get Sensor Type> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 1202 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 1203 | NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE); |
| 1204 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, |
| 1205 | nullptr, ipmi_sen_get_sensor_type, |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1206 | PRIVILEGE_USER); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1207 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1208 | // <Set Sensor Reading and Event Status> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 1209 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 1210 | NETFUN_SENSOR, IPMI_CMD_SET_SENSOR); |
| 1211 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, |
| 1212 | nullptr, ipmi_sen_set_sensor, |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1213 | PRIVILEGE_OPERATOR); |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 1214 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1215 | // <Get Sensor Reading> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 1216 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 1217 | NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING); |
| 1218 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, |
| 1219 | nullptr, ipmi_sen_get_sensor_reading, |
| 1220 | PRIVILEGE_USER); |
| 1221 | |
| 1222 | // <Reserve SDR> |
| 1223 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 1224 | NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO); |
| 1225 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO, |
| 1226 | nullptr, ipmi_sen_reserve_sdr, |
| 1227 | PRIVILEGE_USER); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 1228 | |
Emily Shaffer | d06e0e7 | 2017-04-05 09:08:57 -0700 | [diff] [blame] | 1229 | // <Get SDR Info> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 1230 | printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n", |
| 1231 | NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO); |
| 1232 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO, |
| 1233 | nullptr, ipmi_sen_get_sdr_info, |
| 1234 | PRIVILEGE_USER); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 1235 | |
| 1236 | // <Get SDR> |
| 1237 | printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n", |
| 1238 | NETFUN_SENSOR, IPMI_CMD_GET_SDR); |
| 1239 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR, |
| 1240 | nullptr, ipmi_sen_get_sdr, |
| 1241 | PRIVILEGE_USER); |
| 1242 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 1243 | // <Get Sensor Thresholds> |
| 1244 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS, |
| 1245 | nullptr, ipmi_sen_get_sensor_thresholds, |
| 1246 | PRIVILEGE_USER); |
| 1247 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 1248 | return; |
| 1249 | } |