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> |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 5 | #include <bitset> |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 6 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 7 | #include <systemd/sd-bus.h> |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 8 | #include "host-ipmid/ipmid-api.h" |
| 9 | #include <phosphor-logging/log.hpp> |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 11 | #include "ipmid.hpp" |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 12 | #include "sensorhandler.h" |
| 13 | #include "types.hpp" |
| 14 | #include "utils.hpp" |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 15 | #include "xyz/openbmc_project/Common/error.hpp" |
| 16 | |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 17 | extern int updateSensorRecordFromSSRAESC(const void *); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 18 | extern sd_bus *bus; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 19 | extern const ipmi::sensor::IdInfoMap sensors; |
| 20 | using namespace phosphor::logging; |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 21 | using InternalFailure = |
| 22 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 23 | |
| 24 | void register_netfn_sen_functions() __attribute__((constructor)); |
| 25 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 26 | struct sensorTypemap_t { |
| 27 | uint8_t number; |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 28 | uint8_t typecode; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 29 | char dbusname[32]; |
| 30 | } ; |
| 31 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 32 | sensorTypemap_t g_SensorTypeMap[] = { |
| 33 | |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 34 | {0x01, 0x6F, "Temp"}, |
| 35 | {0x0C, 0x6F, "DIMM"}, |
| 36 | {0x0C, 0x6F, "MEMORY_BUFFER"}, |
| 37 | {0x07, 0x6F, "PROC"}, |
| 38 | {0x07, 0x6F, "CORE"}, |
| 39 | {0x07, 0x6F, "CPU"}, |
| 40 | {0x0F, 0x6F, "BootProgress"}, |
| 41 | {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor type code os 0x09 |
| 42 | {0xC3, 0x6F, "BootCount"}, |
| 43 | {0x1F, 0x6F, "OperatingSystemStatus"}, |
Chris Austen | 800ba71 | 2015-12-03 15:31:00 -0600 | [diff] [blame] | 44 | {0x12, 0x6F, "SYSTEM_EVENT"}, |
| 45 | {0xC7, 0x03, "SYSTEM"}, |
| 46 | {0xC7, 0x03, "MAIN_PLANAR"}, |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 47 | {0xC2, 0x6F, "PowerCap"}, |
Dhruvaraj S | 0724829 | 2017-03-21 02:14:52 -0500 | [diff] [blame] | 48 | {0xD8, 0x03, "PowerSupplyRedundancy"}, |
Jayanth Othayoth | 0661beb | 2017-03-22 06:00:58 -0500 | [diff] [blame] | 49 | {0xDA, 0x03, "TurboAllowed"}, |
Jayanth Othayoth | d5b2ac0 | 2017-03-27 08:19:18 -0500 | [diff] [blame] | 50 | {0xB4, 0x6F, "PowerSupplyDerating"}, |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 51 | {0xFF, 0x00, ""}, |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 55 | struct sensor_data_t { |
| 56 | uint8_t sennum; |
| 57 | } __attribute__ ((packed)) ; |
| 58 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 59 | struct sensorreadingresp_t { |
| 60 | uint8_t value; |
| 61 | uint8_t operation; |
| 62 | uint8_t indication[2]; |
| 63 | } __attribute__ ((packed)) ; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 64 | |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 65 | // Use a lookup table to find the interface name of a specific sensor |
| 66 | // This will be used until an alternative is found. this is the first |
| 67 | // step for mapping IPMI |
| 68 | int find_interface_property_fru_type(dbus_interface_t *interface, const char *property_name, char *property_value) { |
| 69 | |
| 70 | char *str1; |
| 71 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 72 | sd_bus_message *reply = NULL, *m=NULL; |
| 73 | |
| 74 | |
| 75 | int r; |
| 76 | |
| 77 | r = sd_bus_message_new_method_call(bus,&m,interface->bus,interface->path,"org.freedesktop.DBus.Properties","Get"); |
| 78 | if (r < 0) { |
| 79 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 80 | fprintf(stderr,"Bus: %s Path: %s Interface: %s \n", |
| 81 | interface->bus, interface->path, interface->interface); |
| 82 | goto final; |
| 83 | } |
| 84 | |
| 85 | r = sd_bus_message_append(m, "ss", "org.openbmc.InventoryItem", property_name); |
| 86 | if (r < 0) { |
| 87 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 88 | fprintf(stderr,"Bus: %s Path: %s Interface: %s \n", |
| 89 | interface->bus, interface->path, interface->interface); |
| 90 | goto final; |
| 91 | } |
| 92 | |
| 93 | r = sd_bus_call(bus, m, 0, &error, &reply); |
| 94 | if (r < 0) { |
| 95 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 96 | goto final; |
| 97 | } |
| 98 | |
| 99 | r = sd_bus_message_read(reply, "v", "s", &str1) ; |
| 100 | if (r < 0) { |
| 101 | fprintf(stderr, "Failed to get a response: %s", strerror(-r)); |
| 102 | goto final; |
| 103 | } |
| 104 | |
| 105 | strcpy(property_value, str1); |
| 106 | |
| 107 | final: |
| 108 | |
| 109 | sd_bus_error_free(&error); |
| 110 | m = sd_bus_message_unref(m); |
| 111 | reply = sd_bus_message_unref(reply); |
| 112 | |
| 113 | return r; |
| 114 | } |
| 115 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 116 | int get_bus_for_path(const char *path, char **busname) { |
| 117 | return mapper_get_service(bus, path, busname); |
| 118 | } |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 119 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 120 | 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] | 121 | char *busname = NULL; |
| 122 | const char *iface = "org.openbmc.managers.System"; |
| 123 | const char *objname = "/org/openbmc/managers/System"; |
| 124 | char *str1 = NULL, *str2, *str3; |
| 125 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 126 | sd_bus_message *reply = NULL; |
| 127 | |
| 128 | |
| 129 | int r; |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 130 | r = get_bus_for_path(objname, &busname); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 131 | if (r < 0) { |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 132 | fprintf(stderr, "Failed to get %s busname: %s\n", |
| 133 | objname, strerror(-r)); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 134 | goto final; |
| 135 | } |
| 136 | |
| 137 | r = sd_bus_call_method(bus,busname,objname,iface, "getObjectFromByteId", |
| 138 | &error, &reply, "sy", type, num); |
| 139 | if (r < 0) { |
| 140 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 141 | goto final; |
| 142 | } |
| 143 | |
| 144 | r = sd_bus_message_read(reply, "(ss)", &str2, &str3); |
| 145 | if (r < 0) { |
| 146 | fprintf(stderr, "Failed to get a response: %s", strerror(-r)); |
| 147 | goto final; |
| 148 | } |
| 149 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 150 | r = get_bus_for_path(str2, &str1); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 151 | if (r < 0) { |
Brad Bishop | 819ddd4 | 2016-10-05 21:19:19 -0400 | [diff] [blame] | 152 | fprintf(stderr, "Failed to get %s busname: %s\n", |
| 153 | str2, strerror(-r)); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 154 | goto final; |
| 155 | } |
| 156 | |
| 157 | strncpy(interface->bus, str1, MAX_DBUS_PATH); |
| 158 | strncpy(interface->path, str2, MAX_DBUS_PATH); |
| 159 | strncpy(interface->interface, str3, MAX_DBUS_PATH); |
| 160 | |
| 161 | interface->sensornumber = num; |
Emily Shaffer | 7117441 | 2017-04-05 15:10:40 -0700 | [diff] [blame] | 162 | // Make sure we know that the type hasn't been set, as newer codebase will |
| 163 | // set it automatically from the YAML at this step. |
| 164 | interface->sensortype = 0; |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 165 | |
| 166 | final: |
| 167 | |
| 168 | sd_bus_error_free(&error); |
| 169 | reply = sd_bus_message_unref(reply); |
| 170 | free(busname); |
| 171 | free(str1); |
| 172 | |
| 173 | return r; |
| 174 | } |
| 175 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 176 | // Use a lookup table to find the interface name of a specific sensor |
| 177 | // This will be used until an alternative is found. this is the first |
| 178 | // step for mapping IPMI |
| 179 | int find_openbmc_path(uint8_t num, dbus_interface_t *interface) { |
| 180 | int rc; |
| 181 | |
| 182 | // When the sensor map does not contain the sensor requested, |
| 183 | // fall back to the legacy DBus lookup (deprecated) |
| 184 | const auto& sensor_it = sensors.find(num); |
| 185 | if (sensor_it == sensors.end()) |
| 186 | { |
| 187 | return legacy_dbus_openbmc_path("SENSOR", num, interface); |
| 188 | } |
| 189 | |
| 190 | const auto& info = sensor_it->second; |
| 191 | |
Patrick Williams | 8451edf | 2017-06-13 09:01:06 -0500 | [diff] [blame] | 192 | char* busname = nullptr; |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 193 | rc = get_bus_for_path(info.sensorPath.c_str(), &busname); |
| 194 | if (rc < 0) { |
| 195 | fprintf(stderr, "Failed to get %s busname: %s\n", |
| 196 | info.sensorPath.c_str(), |
| 197 | busname); |
| 198 | goto final; |
| 199 | } |
| 200 | |
| 201 | interface->sensortype = info.sensorType; |
| 202 | strcpy(interface->bus, busname); |
| 203 | strcpy(interface->path, info.sensorPath.c_str()); |
| 204 | // Take the interface name from the beginning of the DbusInterfaceMap. This |
| 205 | // works for the Value interface but may not suffice for more complex |
| 206 | // sensors. |
| 207 | // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103 |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 208 | strcpy(interface->interface, info.propertyInterfaces.begin()->first.c_str()); |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 209 | interface->sensornumber = num; |
| 210 | |
| 211 | final: |
| 212 | free(busname); |
| 213 | return rc; |
| 214 | } |
| 215 | |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 216 | |
| 217 | ///////////////////////////////////////////////////////////////////// |
| 218 | // |
| 219 | // Routines used by ipmi commands wanting to interact on the dbus |
| 220 | // |
| 221 | ///////////////////////////////////////////////////////////////////// |
| 222 | int set_sensor_dbus_state_s(uint8_t number, const char *method, const char *value) { |
| 223 | |
| 224 | |
| 225 | dbus_interface_t a; |
| 226 | int r; |
| 227 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 228 | sd_bus_message *m=NULL; |
| 229 | |
| 230 | fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of %s\n", |
| 231 | number, method, value); |
| 232 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 233 | r = find_openbmc_path(number, &a); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 234 | |
| 235 | if (r < 0) { |
| 236 | fprintf(stderr, "Failed to find Sensor 0x%02x\n", number); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method); |
| 241 | if (r < 0) { |
| 242 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 243 | goto final; |
| 244 | } |
| 245 | |
| 246 | r = sd_bus_message_append(m, "v", "s", value); |
| 247 | if (r < 0) { |
| 248 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 249 | goto final; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | r = sd_bus_call(bus, m, 0, &error, NULL); |
| 254 | if (r < 0) { |
| 255 | fprintf(stderr, "Failed to call the method: %s", strerror(-r)); |
| 256 | } |
| 257 | |
| 258 | final: |
| 259 | sd_bus_error_free(&error); |
| 260 | m = sd_bus_message_unref(m); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | int set_sensor_dbus_state_y(uint8_t number, const char *method, const uint8_t value) { |
| 265 | |
| 266 | |
| 267 | dbus_interface_t a; |
| 268 | int r; |
| 269 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 270 | sd_bus_message *m=NULL; |
| 271 | |
| 272 | fprintf(ipmidbus, "Attempting to set a dbus Variant Sensor 0x%02x via %s with a value of 0x%02x\n", |
| 273 | number, method, value); |
| 274 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 275 | r = find_openbmc_path(number, &a); |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 276 | |
| 277 | if (r < 0) { |
| 278 | fprintf(stderr, "Failed to find Sensor 0x%02x\n", number); |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method); |
| 283 | if (r < 0) { |
| 284 | fprintf(stderr, "Failed to create a method call: %s", strerror(-r)); |
| 285 | goto final; |
| 286 | } |
| 287 | |
| 288 | r = sd_bus_message_append(m, "v", "i", value); |
| 289 | if (r < 0) { |
| 290 | fprintf(stderr, "Failed to create a input parameter: %s", strerror(-r)); |
| 291 | goto final; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | r = sd_bus_call(bus, m, 0, &error, NULL); |
| 296 | if (r < 0) { |
| 297 | fprintf(stderr, "12 Failed to call the method: %s", strerror(-r)); |
| 298 | } |
| 299 | |
| 300 | final: |
| 301 | sd_bus_error_free(&error); |
| 302 | m = sd_bus_message_unref(m); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 307 | uint8_t dbus_to_sensor_type(char *p) { |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 308 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 309 | sensorTypemap_t *s = g_SensorTypeMap; |
| 310 | char r=0; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 311 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 312 | while (s->number != 0xFF) { |
| 313 | if (!strcmp(s->dbusname,p)) { |
| 314 | r = s->number; |
| 315 | break; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 316 | } |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 317 | s++; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Chris Austen | d7cf0e4 | 2015-11-07 14:27:12 -0600 | [diff] [blame] | 320 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 321 | if (s->number == 0xFF) |
| 322 | printf("Failed to find Sensor Type %s\n", p); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 323 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 324 | return r; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 325 | } |
| 326 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 327 | |
| 328 | uint8_t dbus_to_sensor_type_from_dbus(dbus_interface_t *a) { |
| 329 | char fru_type_name[64]; |
| 330 | int r= 0; |
| 331 | |
| 332 | r = find_interface_property_fru_type(a, "fru_type", fru_type_name); |
| 333 | if (r<0) { |
| 334 | fprintf(stderr, "Failed to get a fru type: %s", strerror(-r)); |
| 335 | return -1; |
| 336 | } else { |
| 337 | return dbus_to_sensor_type(fru_type_name); |
| 338 | } |
| 339 | } |
| 340 | |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 341 | uint8_t get_type_from_interface(dbus_interface_t dbus_if) { |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 342 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 343 | char *p; |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 344 | uint8_t type; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 345 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 346 | // This is where sensors that do not exist in dbus but do |
| 347 | // exist in the host code stop. This should indicate it |
| 348 | // is not a supported sensor |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 349 | if (dbus_if.interface[0] == 0) { return 0;} |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 350 | |
Emily Shaffer | 7117441 | 2017-04-05 15:10:40 -0700 | [diff] [blame] | 351 | // Fetch type from interface itself. |
| 352 | if (dbus_if.sensortype != 0) |
| 353 | { |
| 354 | type = dbus_if.sensortype; |
| 355 | } |
| 356 | // Legacy codebase does not populate type during initial handling: |
| 357 | else if (strstr(dbus_if.interface, "InventoryItem")) { |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 358 | // InventoryItems are real frus. So need to get the |
| 359 | // fru_type property |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 360 | type = dbus_to_sensor_type_from_dbus(&dbus_if); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 361 | } else { |
| 362 | // Non InventoryItems |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 363 | p = strrchr (dbus_if.path, '/'); |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 364 | type = dbus_to_sensor_type(p+1); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 365 | } |
| 366 | |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 367 | return type; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 370 | // Replaces find_sensor |
| 371 | uint8_t find_type_for_sensor_number(uint8_t num) { |
| 372 | int r; |
| 373 | dbus_interface_t dbus_if; |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 374 | r = find_openbmc_path(num, &dbus_if); |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 375 | if (r < 0) { |
| 376 | fprintf(stderr, "Could not find sensor %d\n", num); |
| 377 | return r; |
| 378 | } |
| 379 | return get_type_from_interface(dbus_if); |
| 380 | } |
| 381 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 382 | |
| 383 | |
| 384 | |
| 385 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 386 | ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 387 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 388 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 389 | { |
| 390 | sensor_data_t *reqptr = (sensor_data_t*)request; |
| 391 | ipmi_ret_t rc = IPMI_CC_OK; |
| 392 | |
| 393 | printf("IPMI GET_SENSOR_TYPE [0x%02X]\n",reqptr->sennum); |
| 394 | |
| 395 | // TODO Not sure what the System-event-sensor is suppose to return |
| 396 | // need to ask Hostboot team |
| 397 | unsigned char buf[] = {0x00,0x6F}; |
| 398 | |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 399 | buf[0] = find_type_for_sensor_number(reqptr->sennum); |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 400 | |
| 401 | // HACK UNTIL Dbus gets updated or we find a better way |
| 402 | if (buf[0] == 0) { |
Chris Austen | 800ba71 | 2015-12-03 15:31:00 -0600 | [diff] [blame] | 403 | rc = IPMI_CC_SENSOR_INVALID; |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 404 | } |
| 405 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 406 | |
| 407 | *data_len = sizeof(buf); |
| 408 | memcpy(response, &buf, *data_len); |
| 409 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 410 | return rc; |
| 411 | } |
| 412 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 413 | ipmi_ret_t setSensorReading(void *request) |
| 414 | { |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 415 | SetSensorReadingReq cmdData = |
| 416 | *(static_cast<SetSensorReadingReq *>(request)); |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 417 | |
| 418 | // Check if the Sensor Number is present |
Dhruvaraj Subhashchandran | e0af720 | 2017-07-12 06:35:20 -0500 | [diff] [blame] | 419 | const auto iter = sensors.find(cmdData.number); |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 420 | if (iter == sensors.end()) |
| 421 | { |
| 422 | return IPMI_CC_SENSOR_INVALID; |
| 423 | } |
| 424 | |
Dhruvaraj Subhashchandran | 18e9999 | 2017-08-09 09:10:47 -0500 | [diff] [blame] | 425 | try |
| 426 | { |
| 427 | return iter->second.updateFunc(cmdData, iter->second); |
| 428 | } |
| 429 | catch (InternalFailure& e) |
| 430 | { |
| 431 | log<level::ERR>("Set sensor failed", |
| 432 | entry("SENSOR_NUM=%d", cmdData.number)); |
| 433 | commit<InternalFailure>(); |
| 434 | } |
| 435 | |
| 436 | return IPMI_CC_UNSPECIFIED_ERROR; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 437 | } |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 438 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 439 | ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 440 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 441 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 442 | { |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 443 | sensor_data_t *reqptr = (sensor_data_t*)request; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 444 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 445 | printf("IPMI SET_SENSOR [0x%02x]\n",reqptr->sennum); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 446 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 447 | /* |
| 448 | * This would support the Set Sensor Reading command for the presence |
| 449 | * and functional state of Processor, Core & DIMM. For the remaining |
| 450 | * sensors the existing support is invoked. |
| 451 | */ |
| 452 | auto ipmiRC = setSensorReading(request); |
| 453 | |
| 454 | if(ipmiRC == IPMI_CC_SENSOR_INVALID) |
| 455 | { |
| 456 | updateSensorRecordFromSSRAESC(reqptr); |
| 457 | ipmiRC = IPMI_CC_OK; |
| 458 | } |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 459 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 460 | *data_len=0; |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 461 | return ipmiRC; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 462 | } |
| 463 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 464 | |
| 465 | ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 466 | ipmi_request_t request, ipmi_response_t response, |
| 467 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 468 | { |
| 469 | sensor_data_t *reqptr = (sensor_data_t*)request; |
| 470 | ipmi_ret_t rc = IPMI_CC_SENSOR_INVALID; |
| 471 | uint8_t type; |
| 472 | sensorreadingresp_t *resp = (sensorreadingresp_t*) response; |
| 473 | int r; |
| 474 | dbus_interface_t a; |
| 475 | sd_bus *bus = ipmid_get_sd_bus_connection(); |
| 476 | sd_bus_message *reply = NULL; |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 477 | int reading = 0; |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 478 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 479 | printf("IPMI GET_SENSOR_READING [0x%02x]\n",reqptr->sennum); |
| 480 | |
Emily Shaffer | 2ae09b9 | 2017-04-05 15:09:41 -0700 | [diff] [blame] | 481 | r = find_openbmc_path(reqptr->sennum, &a); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 482 | |
Nan Li | 36deb76 | 2016-05-12 10:23:41 +0800 | [diff] [blame] | 483 | if (r < 0) { |
| 484 | fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum); |
| 485 | return IPMI_CC_SENSOR_INVALID; |
| 486 | } |
| 487 | |
Emily Shaffer | 391f330 | 2017-04-03 10:27:08 -0700 | [diff] [blame] | 488 | type = get_type_from_interface(a); |
Brad Bishop | 5600345 | 2016-10-05 21:49:19 -0400 | [diff] [blame] | 489 | if(type == 0) { |
| 490 | fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum); |
| 491 | return IPMI_CC_SENSOR_INVALID; |
| 492 | } |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 493 | |
| 494 | fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface); |
| 495 | |
| 496 | *data_len=0; |
| 497 | |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 498 | int64_t raw_value; |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 499 | ipmi::sensor::Info sensor; |
| 500 | |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 501 | switch(type) { |
| 502 | case 0xC3: |
| 503 | case 0xC2: |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 504 | 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] | 505 | if (r < 0) { |
| 506 | fprintf(stderr, "Failed to call sd_bus_get_property:%d, %s\n", r, strerror(-r)); |
| 507 | fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", |
| 508 | a.bus, a.path, a.interface); |
| 509 | break; |
| 510 | } |
| 511 | |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 512 | r = sd_bus_message_read(reply, "i", &reading); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 513 | if (r < 0) { |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 514 | fprintf(stderr, "Failed to read sensor: %s\n", strerror(-r)); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 515 | break; |
| 516 | } |
| 517 | |
| 518 | printf("Contents of a 0x%02x is 0x%02x\n", type, reading); |
| 519 | |
| 520 | rc = IPMI_CC_OK; |
| 521 | *data_len=sizeof(sensorreadingresp_t); |
| 522 | |
Adriana Kobylak | 9312598 | 2016-03-01 12:48:10 -0600 | [diff] [blame] | 523 | resp->value = (uint8_t)reading; |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 524 | resp->operation = 0; |
| 525 | resp->indication[0] = 0; |
| 526 | resp->indication[1] = 0; |
| 527 | break; |
| 528 | |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 529 | case IPMI_SENSOR_TEMP: |
| 530 | case IPMI_SENSOR_VOLTAGE: |
| 531 | case IPMI_SENSOR_CURRENT: |
| 532 | case IPMI_SENSOR_FAN: |
| 533 | // Get reading for /xyz/openbmc_project/Sensor/Value.interface |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 534 | if(sensors.find(reqptr->sennum) == sensors.end()) |
| 535 | { |
| 536 | fprintf(stderr, "Failed to find config entry for Sensor 0x%02x\n", |
| 537 | reqptr->sennum); |
| 538 | return IPMI_CC_SENSOR_INVALID; |
| 539 | } |
| 540 | |
| 541 | sensor = sensors.at(reqptr->sennum); |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 542 | |
| 543 | // Get value |
| 544 | r = sd_bus_get_property_trivial(bus, |
| 545 | a.bus, |
| 546 | a.path, |
| 547 | a.interface, |
| 548 | "Value", |
| 549 | NULL, |
| 550 | 'x', |
| 551 | &raw_value); |
| 552 | if (r < 0) { |
| 553 | fprintf(stderr, |
| 554 | "Failed to call sd_bus_get_property:%d, %s, 'value'\n", |
| 555 | r, |
| 556 | strerror(-r)); |
| 557 | fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", |
| 558 | a.bus, a.path, a.interface); |
| 559 | break; |
| 560 | } |
| 561 | |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 562 | // Prevent div0 |
| 563 | if (sensor.coefficientM == 0) { |
| 564 | sensor.coefficientM = 1; |
| 565 | }; |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 566 | |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 567 | resp->value = static_cast<uint8_t>( |
| 568 | (raw_value - sensor.scaledOffset) / sensor.coefficientM); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 569 | resp->operation = 1<<6; // scanning enabled |
| 570 | resp->indication[0] = 0; // not a threshold sensor. ignore |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 571 | resp->indication[1] = 0; |
| 572 | rc = IPMI_CC_OK; |
| 573 | *data_len=sizeof(sensorreadingresp_t); |
| 574 | break; |
Tom Joseph | 60cac72 | 2017-08-18 12:06:07 +0530 | [diff] [blame^] | 575 | case IPMI_SENSOR_TPM: |
| 576 | { |
| 577 | auto iter = sensors.find(reqptr->sennum); |
| 578 | if (iter == sensors.end()) |
| 579 | { |
| 580 | return IPMI_CC_SENSOR_INVALID; |
| 581 | } |
Emily Shaffer | 1fabf22 | 2017-04-05 08:53:21 -0700 | [diff] [blame] | 582 | |
Tom Joseph | 60cac72 | 2017-08-18 12:06:07 +0530 | [diff] [blame^] | 583 | auto& interfaceList = iter->second.propertyInterfaces; |
| 584 | if (interfaceList.empty()) |
| 585 | { |
| 586 | log<level::ERR>("Interface List empty for the sensor", |
| 587 | entry("SENSOR_NUMBER=%d", reqptr->sennum)); |
| 588 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 589 | } |
| 590 | |
| 591 | /* For the TPM sensor there is no reading value and sensor scanning |
| 592 | * is disabled. This is a discrete sensor and only the |
| 593 | * corresponding state is asserted. |
| 594 | */ |
| 595 | resp->value = 0; |
| 596 | resp->operation = 0; |
| 597 | resp->indication[0] = 0; |
| 598 | resp->indication[1] = 0; |
| 599 | |
| 600 | try |
| 601 | { |
| 602 | for (const auto& interface : interfaceList) |
| 603 | { |
| 604 | for (const auto& property : interface.second) |
| 605 | { |
| 606 | sdbusplus::bus::bus dbus{ipmid_get_sd_bus_connection()}; |
| 607 | |
| 608 | auto service = ipmi::getService( |
| 609 | dbus, |
| 610 | interface.first, |
| 611 | iter->second.sensorPath); |
| 612 | |
| 613 | auto propValue = ipmi::getDbusProperty( |
| 614 | dbus, service, iter->second.sensorPath, |
| 615 | interface.first, property.first); |
| 616 | |
| 617 | auto tpmStatus = propValue.get<bool>(); |
| 618 | |
| 619 | for (const auto& value : property.second) |
| 620 | { |
| 621 | if (tpmStatus == (value.second.assert).get<bool>()) |
| 622 | { |
| 623 | /* |
| 624 | * The discrete sensors support upto 14 states. |
| 625 | * The assertion states for discrete sensors are |
| 626 | * stored in 2 bytes, 0-7 in Byte 4 of the |
| 627 | * response and 8-14 in Byte 5 of the response. |
| 628 | */ |
| 629 | resp->indication[0] |= 1 << (value.first); |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | rc = IPMI_CC_OK; |
| 637 | *data_len = sizeof(sensorreadingresp_t); |
| 638 | } |
| 639 | catch (InternalFailure& e) |
| 640 | { |
| 641 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 642 | } |
| 643 | break; |
| 644 | } |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 645 | default: |
| 646 | *data_len=0; |
| 647 | rc = IPMI_CC_SENSOR_INVALID; |
| 648 | break; |
| 649 | } |
| 650 | |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 651 | reply = sd_bus_message_unref(reply); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 652 | |
| 653 | return rc; |
| 654 | } |
| 655 | |
Chris Austen | 0012e9b | 2015-10-22 01:37:46 -0500 | [diff] [blame] | 656 | ipmi_ret_t ipmi_sen_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 657 | ipmi_request_t request, ipmi_response_t response, |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 658 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 659 | { |
Nan Li | 70aa8d9 | 2016-08-29 00:11:10 +0800 | [diff] [blame] | 660 | ipmi_ret_t rc = IPMI_CC_INVALID; |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 661 | |
| 662 | printf("IPMI S/E Wildcard Netfn:[0x%X], Cmd:[0x%X]\n",netfn,cmd); |
| 663 | *data_len = 0; |
| 664 | |
| 665 | return rc; |
| 666 | } |
| 667 | |
Emily Shaffer | d06e0e7 | 2017-04-05 09:08:57 -0700 | [diff] [blame] | 668 | ipmi_ret_t ipmi_sen_get_sdr_info(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 669 | ipmi_request_t request, |
| 670 | ipmi_response_t response, |
| 671 | ipmi_data_len_t data_len, |
| 672 | ipmi_context_t context) |
| 673 | { |
| 674 | auto resp = static_cast<get_sdr_info::GetSdrInfoResp*>(response); |
| 675 | if (request == nullptr || |
| 676 | get_sdr_info::request::get_count(request) == false) |
| 677 | { |
| 678 | // Get Sensor Count |
| 679 | resp->count = sensors.size(); |
| 680 | } |
| 681 | else |
| 682 | { |
| 683 | resp->count = 1; |
| 684 | } |
| 685 | |
| 686 | // Multiple LUNs not supported. |
| 687 | namespace response = get_sdr_info::response; |
| 688 | response::set_lun_present(0, &(resp->luns_and_dynamic_population)); |
| 689 | response::set_lun_not_present(1, &(resp->luns_and_dynamic_population)); |
| 690 | response::set_lun_not_present(2, &(resp->luns_and_dynamic_population)); |
| 691 | response::set_lun_not_present(3, &(resp->luns_and_dynamic_population)); |
| 692 | response::set_static_population(&(resp->luns_and_dynamic_population)); |
| 693 | |
| 694 | *data_len = SDR_INFO_RESP_SIZE; |
| 695 | |
| 696 | return IPMI_CC_OK; |
| 697 | } |
| 698 | |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 699 | ipmi_ret_t ipmi_sen_reserve_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 700 | ipmi_request_t request, |
| 701 | ipmi_response_t response, |
| 702 | ipmi_data_len_t data_len, |
| 703 | ipmi_context_t context) |
| 704 | { |
| 705 | // A constant reservation ID is okay until we implement add/remove SDR. |
| 706 | const uint16_t reservation_id = 1; |
| 707 | *(uint16_t*)response = reservation_id; |
Emily Shaffer | 5a5a628 | 2017-08-15 11:17:17 -0700 | [diff] [blame] | 708 | *data_len = sizeof(uint16_t); |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 709 | |
| 710 | printf("Created new IPMI SDR reservation ID %d\n", *(uint16_t*)response); |
| 711 | return IPMI_CC_OK; |
| 712 | } |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 713 | |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 714 | ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody *body, |
| 715 | const ipmi::sensor::Info *info, |
| 716 | ipmi_data_len_t data_len) |
| 717 | { |
| 718 | /* Functional sensor case */ |
Deepak Kodihalli | 1bb0d38 | 2017-08-12 02:01:27 -0500 | [diff] [blame] | 719 | if (info->propertyInterfaces.begin()->first == |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 720 | "xyz.openbmc_project.Sensor.Value") |
| 721 | { |
| 722 | // Get bus |
| 723 | sd_bus *bus = ipmid_get_sd_bus_connection(); |
| 724 | dbus_interface_t iface; |
| 725 | |
| 726 | if (0 > find_openbmc_path(body->entity_id, &iface)) |
| 727 | return IPMI_CC_SENSOR_INVALID; |
| 728 | |
| 729 | body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a % |
| 730 | |
| 731 | /* Unit info */ |
| 732 | char *raw_cstr; |
| 733 | if (0 > sd_bus_get_property_string(bus, iface.bus, iface.path, |
| 734 | iface.interface, "Unit", NULL, |
| 735 | &raw_cstr)) { |
| 736 | fprintf(stderr, "Expected to find Unit interface in bus %s, path %s, but it was missing.\n", |
| 737 | iface.bus, iface.path); |
| 738 | return IPMI_CC_SENSOR_INVALID; |
| 739 | } |
| 740 | |
| 741 | std::string raw_str(raw_cstr); |
| 742 | namespace server = sdbusplus::xyz::openbmc_project::Sensor::server; |
| 743 | server::Value::Unit unit = |
| 744 | server::Value::convertUnitFromString(raw_str); |
| 745 | |
| 746 | // Unit strings defined in |
| 747 | // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml |
| 748 | switch (unit) |
| 749 | { |
| 750 | case server::Value::Unit::DegreesC: |
| 751 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C; |
| 752 | break; |
| 753 | case server::Value::Unit::RPMS: |
| 754 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_REVOLUTIONS; // revolutions |
| 755 | get_sdr::body::set_rate_unit(0b100, body); // per minute |
| 756 | break; |
| 757 | case server::Value::Unit::Volts: |
| 758 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS; |
| 759 | break; |
| 760 | case server::Value::Unit::Meters: |
| 761 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS; |
| 762 | break; |
| 763 | case server::Value::Unit::Amperes: |
| 764 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES; |
| 765 | break; |
| 766 | case server::Value::Unit::Joules: |
| 767 | body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES; |
| 768 | break; |
| 769 | default: |
| 770 | fprintf(stderr, "Unknown value unit type: = %s\n", raw_cstr); |
| 771 | } |
| 772 | |
| 773 | free(raw_cstr); |
| 774 | |
| 775 | /* Modifiers to reading info */ |
Emily Shaffer | 10f4959 | 2017-05-10 12:01:10 -0700 | [diff] [blame] | 776 | // Get scale |
| 777 | int64_t scale; |
| 778 | if (0 > sd_bus_get_property_trivial(bus, |
| 779 | iface.bus, |
| 780 | iface.path, |
| 781 | iface.interface, |
| 782 | "Scale", |
| 783 | NULL, |
| 784 | 'x', |
| 785 | &scale)) { |
| 786 | fprintf(stderr, "Expected to find Scale interface in bus %s, path %s, but it was missing.\n", |
| 787 | iface.bus, iface.path); |
| 788 | return IPMI_CC_SENSOR_INVALID; |
| 789 | } |
| 790 | |
| 791 | get_sdr::body::set_b(info->coefficientB, body); |
| 792 | get_sdr::body::set_m(info->coefficientM, body); |
| 793 | get_sdr::body::set_b_exp(info->exponentB, body); |
| 794 | get_sdr::body::set_r_exp(scale, body); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 795 | |
| 796 | /* ID string */ |
| 797 | std::string id_string = info->sensorPath.substr( |
| 798 | info->sensorPath.find_last_of('/')+1, info->sensorPath.length()); |
| 799 | get_sdr::body::set_id_type(0b00, body); // 00 = unicode |
| 800 | if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH) |
| 801 | { |
| 802 | get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body); |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | get_sdr::body::set_id_strlen(id_string.length(), body); |
| 807 | } |
| 808 | strncpy(body->id_string, id_string.c_str(), |
| 809 | get_sdr::body::get_id_strlen(body)); |
| 810 | } |
| 811 | |
| 812 | return IPMI_CC_OK; |
| 813 | }; |
| 814 | |
| 815 | ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 816 | ipmi_request_t request, ipmi_response_t response, |
| 817 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 818 | { |
| 819 | ipmi_ret_t ret = IPMI_CC_OK; |
| 820 | get_sdr::GetSdrReq *req = (get_sdr::GetSdrReq*)request; |
| 821 | get_sdr::GetSdrResp *resp = (get_sdr::GetSdrResp*)response; |
| 822 | get_sdr::SensorDataFullRecord record = {0}; |
| 823 | if (req != NULL) |
| 824 | { |
| 825 | // Note: we use an iterator so we can provide the next ID at the end of |
| 826 | // the call. |
| 827 | auto sensor = sensors.begin(); |
| 828 | |
| 829 | // At the beginning of a scan, the host side will send us id=0. |
| 830 | if (get_sdr::request::get_record_id(req) != 0) |
| 831 | { |
| 832 | sensor = sensors.find(get_sdr::request::get_record_id(req)); |
| 833 | if(sensor == sensors.end()) { |
| 834 | return IPMI_CC_SENSOR_INVALID; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | uint8_t sensor_id = sensor->first; |
| 839 | |
| 840 | /* Header */ |
| 841 | get_sdr::header::set_record_id(sensor_id, &(record.header)); |
| 842 | record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1 |
| 843 | record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD; |
| 844 | record.header.record_length = sizeof(get_sdr::SensorDataFullRecord); |
| 845 | |
| 846 | /* Key */ |
| 847 | record.key.sensor_number = sensor_id; |
| 848 | |
| 849 | /* Body */ |
| 850 | record.body.entity_id = sensor_id; |
| 851 | record.body.sensor_type = sensor->second.sensorType; |
| 852 | record.body.event_reading_type = sensor->second.sensorReadingType; |
| 853 | |
| 854 | // Set the type-specific details given the DBus interface |
| 855 | ret = populate_record_from_dbus(&(record.body), &(sensor->second), |
| 856 | data_len); |
| 857 | |
| 858 | if (++sensor == sensors.end()) |
| 859 | { |
| 860 | get_sdr::response::set_next_record_id(0xFFFF, resp); // last record |
| 861 | } |
| 862 | else |
| 863 | { |
| 864 | get_sdr::response::set_next_record_id(sensor->first, resp); |
| 865 | } |
| 866 | |
| 867 | *data_len = sizeof(get_sdr::GetSdrResp) - req->offset; |
| 868 | memcpy(resp->record_data, (char*)&record + req->offset, |
| 869 | sizeof(get_sdr::SensorDataFullRecord) - req->offset); |
| 870 | } |
| 871 | |
| 872 | return ret; |
| 873 | } |
| 874 | |
| 875 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 876 | void register_netfn_sen_functions() |
| 877 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 878 | // <Wildcard Command> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 879 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 880 | NETFUN_SENSOR, IPMI_CMD_WILDCARD); |
| 881 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, |
| 882 | nullptr, ipmi_sen_wildcard, |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 883 | PRIVILEGE_USER); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 884 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 885 | // <Get Sensor Type> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 886 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 887 | NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE); |
| 888 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, |
| 889 | nullptr, ipmi_sen_get_sensor_type, |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 890 | PRIVILEGE_USER); |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 891 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 892 | // <Set Sensor Reading and Event Status> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 893 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 894 | NETFUN_SENSOR, IPMI_CMD_SET_SENSOR); |
| 895 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, |
| 896 | nullptr, ipmi_sen_set_sensor, |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 897 | PRIVILEGE_OPERATOR); |
Chris Austen | 8a45e7c | 2015-10-15 00:31:46 -0500 | [diff] [blame] | 898 | |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 899 | // <Get Sensor Reading> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 900 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 901 | NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING); |
| 902 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, |
| 903 | nullptr, ipmi_sen_get_sensor_reading, |
| 904 | PRIVILEGE_USER); |
| 905 | |
| 906 | // <Reserve SDR> |
| 907 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", |
| 908 | NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO); |
| 909 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_RESERVE_SDR_REPO, |
| 910 | nullptr, ipmi_sen_reserve_sdr, |
| 911 | PRIVILEGE_USER); |
Chris Austen | 10ccc0f | 2015-12-10 18:27:04 -0600 | [diff] [blame] | 912 | |
Emily Shaffer | d06e0e7 | 2017-04-05 09:08:57 -0700 | [diff] [blame] | 913 | // <Get SDR Info> |
Emily Shaffer | a344afc | 2017-04-13 15:09:39 -0700 | [diff] [blame] | 914 | printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n", |
| 915 | NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO); |
| 916 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR_INFO, |
| 917 | nullptr, ipmi_sen_get_sdr_info, |
| 918 | PRIVILEGE_USER); |
Emily Shaffer | bbef71c | 2017-05-08 16:36:17 -0700 | [diff] [blame] | 919 | |
| 920 | // <Get SDR> |
| 921 | printf("Registering NetFn:[0x%X], Cmd:[0x%x]\n", |
| 922 | NETFUN_SENSOR, IPMI_CMD_GET_SDR); |
| 923 | ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SDR, |
| 924 | nullptr, ipmi_sen_get_sdr, |
| 925 | PRIVILEGE_USER); |
| 926 | |
Chris Austen | ac4604a | 2015-10-13 12:43:27 -0500 | [diff] [blame] | 927 | return; |
| 928 | } |