Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Intel Corporation. |
| 3 | * Copyright (c) 2018-present Facebook. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #pragma once |
| 19 | #include <ipmid/api.h> |
| 20 | |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 21 | #include <phosphor-logging/log.hpp> |
| 22 | |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 23 | #include <cmath> |
| 24 | #include <iostream> |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ipmi |
| 27 | { |
| 28 | |
| 29 | static constexpr int16_t maxInt10 = 0x1FF; |
| 30 | static constexpr int16_t minInt10 = -0x200; |
| 31 | static constexpr int8_t maxInt4 = 7; |
| 32 | static constexpr int8_t minInt4 = -8; |
| 33 | |
| 34 | enum class SensorUnits : uint8_t |
| 35 | { |
| 36 | unspecified = 0x0, |
| 37 | degreesC = 0x1, |
| 38 | volts = 0x4, |
| 39 | amps = 0x5, |
| 40 | watts = 0x6, |
| 41 | rpm = 0x12, |
| 42 | }; |
| 43 | |
| 44 | enum class SensorTypeCodes : uint8_t |
| 45 | { |
| 46 | reserved = 0x0, |
| 47 | temperature = 0x1, |
| 48 | voltage = 0x2, |
| 49 | current = 0x3, |
| 50 | fan = 0x4, |
| 51 | other = 0xB, |
| 52 | }; |
| 53 | |
| 54 | struct CmpStrVersion |
| 55 | { |
| 56 | bool operator()(std::string a, std::string b) const |
| 57 | { |
| 58 | return strverscmp(a.c_str(), b.c_str()) < 0; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | using SensorSubTree = boost::container::flat_map< |
| 63 | std::string, |
| 64 | boost::container::flat_map<std::string, std::vector<std::string>>, |
| 65 | CmpStrVersion>; |
| 66 | |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 67 | inline static bool getSensorSubtree(SensorSubTree& subtree) |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 68 | { |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 69 | sd_bus* bus = NULL; |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 70 | int ret = sd_bus_default_system(&bus); |
| 71 | if (ret < 0) |
| 72 | { |
| 73 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 74 | "Failed to connect to system bus", |
| 75 | phosphor::logging::entry("ERRNO=0x%X", -ret)); |
| 76 | sd_bus_unref(bus); |
| 77 | return false; |
| 78 | } |
Patrick Williams | cd315e0 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 79 | sdbusplus::bus_t dbus(bus); |
Patrick Williams | 010dee0 | 2024-08-16 15:19:44 -0400 | [diff] [blame] | 80 | auto mapperCall = |
| 81 | dbus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 82 | "/xyz/openbmc_project/object_mapper", |
| 83 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 84 | static constexpr const auto depth = 2; |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 85 | static constexpr std::array<const char*, 3> interfaces = { |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 86 | "xyz.openbmc_project.Sensor.Value", |
| 87 | "xyz.openbmc_project.Sensor.Threshold.Warning", |
| 88 | "xyz.openbmc_project.Sensor.Threshold.Critical"}; |
| 89 | mapperCall.append("/xyz/openbmc_project/sensors", depth, interfaces); |
| 90 | |
| 91 | try |
| 92 | { |
| 93 | auto mapperReply = dbus.call(mapperCall); |
| 94 | subtree.clear(); |
| 95 | mapperReply.read(subtree); |
| 96 | } |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 97 | catch (sdbusplus::exception_t& e) |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 98 | { |
| 99 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | // Specify the comparison required to sort and find char* map objects |
| 106 | struct CmpStr |
| 107 | { |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 108 | bool operator()(const char* a, const char* b) const |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 109 | { |
| 110 | return std::strcmp(a, b) < 0; |
| 111 | } |
| 112 | }; |
| 113 | |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 114 | const static boost::container::flat_map<const char*, SensorUnits, CmpStr> |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 115 | sensorUnits{{{"temperature", SensorUnits::degreesC}, |
| 116 | {"voltage", SensorUnits::volts}, |
| 117 | {"current", SensorUnits::amps}, |
| 118 | {"fan_tach", SensorUnits::rpm}, |
| 119 | {"power", SensorUnits::watts}}}; |
| 120 | |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 121 | const static boost::container::flat_map<const char*, SensorTypeCodes, CmpStr> |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 122 | sensorTypes{{{"temperature", SensorTypeCodes::temperature}, |
| 123 | {"voltage", SensorTypeCodes::voltage}, |
| 124 | {"current", SensorTypeCodes::current}, |
| 125 | {"fan_tach", SensorTypeCodes::fan}, |
| 126 | {"fan_pwm", SensorTypeCodes::fan}, |
| 127 | {"power", SensorTypeCodes::other}}}; |
| 128 | |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 129 | inline static std::string getSensorTypeStringFromPath(const std::string& path) |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 130 | { |
| 131 | // get sensor type string from path, path is defined as |
| 132 | // /xyz/openbmc_project/sensors/<type>/label |
| 133 | size_t typeEnd = path.rfind("/"); |
| 134 | if (typeEnd == std::string::npos) |
| 135 | { |
| 136 | return path; |
| 137 | } |
| 138 | size_t typeStart = path.rfind("/", typeEnd - 1); |
| 139 | if (typeStart == std::string::npos) |
| 140 | { |
| 141 | return path; |
| 142 | } |
| 143 | // Start at the character after the '/' |
| 144 | typeStart++; |
| 145 | return path.substr(typeStart, typeEnd - typeStart); |
| 146 | } |
| 147 | |
Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 148 | inline static uint8_t getSensorTypeFromPath(const std::string& path) |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 149 | { |
| 150 | uint8_t sensorType = 0; |
| 151 | std::string type = getSensorTypeStringFromPath(path); |
| 152 | auto findSensor = sensorTypes.find(type.c_str()); |
| 153 | if (findSensor != sensorTypes.end()) |
| 154 | { |
| 155 | sensorType = static_cast<uint8_t>(findSensor->second); |
| 156 | } // else default 0x0 RESERVED |
| 157 | |
| 158 | return sensorType; |
| 159 | } |
| 160 | |
Willy Tu | e39f939 | 2022-06-15 13:24:20 -0700 | [diff] [blame] | 161 | inline static uint8_t getSensorEventTypeFromPath(const std::string&) |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 162 | { |
| 163 | // TODO: Add support for additional reading types as needed |
| 164 | return 0x1; // reading type = threshold |
| 165 | } |
| 166 | |
Patrick Williams | 010dee0 | 2024-08-16 15:19:44 -0400 | [diff] [blame] | 167 | static inline bool getSensorAttributes( |
| 168 | const double max, const double min, int16_t& mValue, int8_t& rExp, |
| 169 | int16_t& bValue, int8_t& bExp, bool& bSigned) |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 170 | { |
| 171 | // computing y = (10^rRexp) * (Mx + (B*(10^Bexp))) |
| 172 | // check for 0, assume always positive |
| 173 | double mDouble; |
| 174 | double bDouble; |
| 175 | if (max <= min) |
| 176 | { |
| 177 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 178 | "getSensorAttributes: Max must be greater than min"); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | mDouble = (max - min) / 0xFF; |
| 183 | |
| 184 | if (min < 0) |
| 185 | { |
| 186 | bSigned = true; |
| 187 | bDouble = floor(0.5 + ((max + min) / 2)); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | bSigned = false; |
| 192 | bDouble = min; |
| 193 | } |
| 194 | |
| 195 | rExp = 0; |
| 196 | |
| 197 | // M too big for 10 bit variable |
| 198 | while (mDouble > maxInt10) |
| 199 | { |
| 200 | if (rExp >= maxInt4) |
| 201 | { |
| 202 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 203 | "rExp Too big, Max and Min range too far", |
| 204 | phosphor::logging::entry("REXP=%d", rExp)); |
| 205 | return false; |
| 206 | } |
| 207 | mDouble /= 10; |
| 208 | rExp++; |
| 209 | } |
| 210 | |
| 211 | // M too small, loop until we lose less than 1 eight bit count of precision |
| 212 | while (((mDouble - floor(mDouble)) / mDouble) > (1.0 / 255)) |
| 213 | { |
| 214 | if (rExp <= minInt4) |
| 215 | { |
| 216 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 217 | "rExp Too Small, Max and Min range too close"); |
| 218 | return false; |
| 219 | } |
| 220 | // check to see if we reached the limit of where we can adjust back the |
| 221 | // B value |
| 222 | if (bDouble / std::pow(10, rExp + minInt4 - 1) > bDouble) |
| 223 | { |
| 224 | if (mDouble < 1.0) |
| 225 | { |
| 226 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 227 | "Could not find mValue and B value with enough " |
| 228 | "precision."); |
| 229 | return false; |
| 230 | } |
| 231 | break; |
| 232 | } |
| 233 | // can't multiply M any more, max precision reached |
| 234 | else if (mDouble * 10 > maxInt10) |
| 235 | { |
| 236 | break; |
| 237 | } |
| 238 | mDouble *= 10; |
| 239 | rExp--; |
| 240 | } |
| 241 | |
| 242 | bDouble /= std::pow(10, rExp); |
| 243 | bExp = 0; |
| 244 | |
| 245 | // B too big for 10 bit variable |
| 246 | while (bDouble > maxInt10 || bDouble < minInt10) |
| 247 | { |
| 248 | if (bExp >= maxInt4) |
| 249 | { |
| 250 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 251 | "bExp Too Big, Max and Min range need to be adjusted"); |
| 252 | return false; |
| 253 | } |
| 254 | bDouble /= 10; |
| 255 | bExp++; |
| 256 | } |
| 257 | |
| 258 | while (((fabs(bDouble) - floor(fabs(bDouble))) / fabs(bDouble)) > |
| 259 | (1.0 / 255)) |
| 260 | { |
| 261 | if (bExp <= minInt4) |
| 262 | { |
| 263 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 264 | "bExp Too Small, Max and Min range need to be adjusted"); |
| 265 | return false; |
| 266 | } |
| 267 | bDouble *= 10; |
| 268 | bExp -= 1; |
| 269 | } |
| 270 | |
| 271 | mValue = static_cast<int16_t>(mDouble) & maxInt10; |
| 272 | bValue = static_cast<int16_t>(bDouble) & maxInt10; |
| 273 | |
| 274 | return true; |
| 275 | } |
| 276 | |
Patrick Williams | 010dee0 | 2024-08-16 15:19:44 -0400 | [diff] [blame] | 277 | static inline uint8_t scaleIPMIValueFromDouble( |
| 278 | const double value, const uint16_t mValue, const int8_t rExp, |
| 279 | const uint16_t bValue, const int8_t bExp, const bool bSigned) |
Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 280 | { |
| 281 | uint32_t scaledValue = |
| 282 | (value - (bValue * std::pow(10, bExp) * std::pow(10, rExp))) / |
| 283 | (mValue * std::pow(10, rExp)); |
| 284 | |
| 285 | if (scaledValue > std::numeric_limits<uint8_t>::max() || |
| 286 | scaledValue < std::numeric_limits<uint8_t>::lowest()) |
| 287 | { |
| 288 | throw std::out_of_range("Value out of range"); |
| 289 | } |
| 290 | if (bSigned) |
| 291 | { |
| 292 | return static_cast<int8_t>(scaledValue); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | return static_cast<uint8_t>(scaledValue); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | static inline uint8_t getScaledIPMIValue(const double value, const double max, |
| 301 | const double min) |
| 302 | { |
| 303 | int16_t mValue = 0; |
| 304 | int8_t rExp = 0; |
| 305 | int16_t bValue = 0; |
| 306 | int8_t bExp = 0; |
| 307 | bool bSigned = 0; |
| 308 | bool result = 0; |
| 309 | |
| 310 | result = getSensorAttributes(max, min, mValue, rExp, bValue, bExp, bSigned); |
| 311 | if (!result) |
| 312 | { |
| 313 | throw std::runtime_error("Illegal sensor attributes"); |
| 314 | } |
| 315 | return scaleIPMIValueFromDouble(value, mValue, rExp, bValue, bExp, bSigned); |
| 316 | } |
| 317 | } // namespace ipmi |