James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 17 | #include <IpmbSensor.hpp> |
| 18 | #include <Utils.hpp> |
| 19 | #include <VariantVisitors.hpp> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 20 | #include <boost/algorithm/string/predicate.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 21 | #include <boost/container/flat_map.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 22 | #include <sdbusplus/asio/connection.hpp> |
| 23 | #include <sdbusplus/asio/object_server.hpp> |
| 24 | #include <sdbusplus/bus/match.hpp> |
| 25 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 26 | #include <chrono> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 27 | #include <cmath> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 28 | #include <functional> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 29 | #include <iostream> |
| 30 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 31 | #include <memory> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 32 | #include <numeric> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 33 | #include <string> |
| 34 | #include <tuple> |
| 35 | #include <variant> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 36 | #include <vector> |
| 37 | |
| 38 | constexpr const bool debug = false; |
| 39 | |
| 40 | constexpr const char* configInterface = |
| 41 | "xyz.openbmc_project.Configuration.IpmbSensor"; |
| 42 | static constexpr double ipmbMaxReading = 0xFF; |
| 43 | static constexpr double ipmbMinReading = 0; |
| 44 | |
| 45 | static constexpr uint8_t meAddress = 1; |
| 46 | static constexpr uint8_t lun = 0; |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 47 | static constexpr uint8_t hostSMbusIndexDefault = 0x03; |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 48 | static constexpr float pollRateDefault = 1; // in seconds |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 49 | |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 50 | static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/"; |
| 51 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 52 | using IpmbMethodType = |
| 53 | std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>; |
| 54 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 55 | boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>> sensors; |
| 56 | |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 57 | std::unique_ptr<boost::asio::deadline_timer> initCmdTimer; |
| 58 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 59 | IpmbSensor::IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 60 | boost::asio::io_service& io, |
| 61 | const std::string& sensorName, |
| 62 | const std::string& sensorConfiguration, |
| 63 | sdbusplus::asio::object_server& objectServer, |
| 64 | std::vector<thresholds::Threshold>&& thresholdData, |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 65 | uint8_t deviceAddress, uint8_t hostSMbusIndex, |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 66 | const float pollRate, std::string& sensorTypeName) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 67 | Sensor(escapeName(sensorName), std::move(thresholdData), |
| 68 | sensorConfiguration, "xyz.openbmc_project.Configuration.ExitAirTemp", |
| 69 | false, false, ipmbMaxReading, ipmbMinReading, conn, PowerState::on), |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 70 | deviceAddress(deviceAddress), hostSMbusIndex(hostSMbusIndex), |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 71 | sensorPollMs(static_cast<int>(pollRate * 1000)), objectServer(objectServer), |
| 72 | waitTimer(io) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 73 | { |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 74 | std::string dbusPath = sensorPathPrefix + sensorTypeName + "/" + name; |
| 75 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 76 | sensorInterface = objectServer.add_interface( |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 77 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 78 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 79 | for (const auto& threshold : thresholds) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 80 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 81 | std::string interface = thresholds::getInterface(threshold.level); |
| 82 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 83 | objectServer.add_interface(dbusPath, interface); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 84 | } |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 85 | association = objectServer.add_interface(dbusPath, association::interface); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | IpmbSensor::~IpmbSensor() |
| 89 | { |
| 90 | waitTimer.cancel(); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 91 | for (const auto& iface : thresholdInterfaces) |
| 92 | { |
| 93 | objectServer.remove_interface(iface); |
| 94 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 95 | objectServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 96 | objectServer.remove_interface(association); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 99 | std::string IpmbSensor::getSubTypeUnits(void) const |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 100 | { |
| 101 | switch (subType) |
| 102 | { |
| 103 | case IpmbSubType::temp: |
| 104 | return sensor_paths::unitDegreesC; |
| 105 | case IpmbSubType::curr: |
| 106 | return sensor_paths::unitAmperes; |
| 107 | case IpmbSubType::power: |
| 108 | return sensor_paths::unitWatts; |
| 109 | case IpmbSubType::volt: |
| 110 | return sensor_paths::unitVolts; |
| 111 | case IpmbSubType::util: |
| 112 | return sensor_paths::unitPercent; |
| 113 | default: |
| 114 | throw std::runtime_error("Invalid sensor type"); |
| 115 | } |
| 116 | } |
| 117 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 118 | void IpmbSensor::init(void) |
| 119 | { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 120 | loadDefaults(); |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 121 | setInitialProperties(getSubTypeUnits()); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 122 | if (initCommand) |
| 123 | { |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 124 | runInitCmd(); |
| 125 | } |
| 126 | read(); |
| 127 | } |
| 128 | |
| 129 | void IpmbSensor::runInitCmd() |
| 130 | { |
| 131 | if (initCommand) |
| 132 | { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 133 | dbusConnection->async_method_call( |
| 134 | [this](boost::system::error_code ec, |
| 135 | const IpmbMethodType& response) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 136 | const int& status = std::get<0>(response); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 137 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 138 | if (ec || (status != 0)) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 139 | { |
| 140 | std::cerr << "Error setting init command for device: " << name |
| 141 | << "\n"; |
| 142 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 143 | }, |
| 144 | "xyz.openbmc_project.Ipmi.Channel.Ipmb", |
| 145 | "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb", |
| 146 | "sendRequest", commandAddress, netfn, lun, *initCommand, initData); |
| 147 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void IpmbSensor::loadDefaults() |
| 151 | { |
| 152 | if (type == IpmbType::meSensor) |
| 153 | { |
| 154 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 155 | netfn = ipmi::sensor::netFn; |
| 156 | command = ipmi::sensor::getSensorReading; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 157 | commandData = {deviceAddress}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 158 | readingFormat = ReadingFormat::byte0; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 159 | } |
| 160 | else if (type == IpmbType::PXE1410CVR) |
| 161 | { |
| 162 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 163 | netfn = ipmi::me_bridge::netFn; |
| 164 | command = ipmi::me_bridge::sendRawPmbus; |
| 165 | initCommand = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 166 | // pmbus read temp |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 167 | commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex, |
| 168 | deviceAddress, 0x00, 0x00, 0x00, 0x00, |
| 169 | 0x01, 0x02, 0x8d}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 170 | // goto page 0 |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 171 | initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex, |
| 172 | deviceAddress, 0x00, 0x00, 0x00, 0x00, |
| 173 | 0x02, 0x00, 0x00, 0x00}; |
Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 174 | readingFormat = ReadingFormat::linearElevenBit; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 175 | } |
| 176 | else if (type == IpmbType::IR38363VR) |
| 177 | { |
| 178 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 179 | netfn = ipmi::me_bridge::netFn; |
| 180 | command = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 181 | // pmbus read temp |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 182 | commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex, |
| 183 | deviceAddress, 00, 0x00, 0x00, 0x00, |
| 184 | 0x01, 0x02, 0x8D}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 185 | readingFormat = ReadingFormat::elevenBitShift; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 186 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 187 | else if (type == IpmbType::ADM1278HSC) |
| 188 | { |
| 189 | commandAddress = meAddress; |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 190 | uint8_t snsNum = 0; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 191 | switch (subType) |
| 192 | { |
| 193 | case IpmbSubType::temp: |
| 194 | case IpmbSubType::curr: |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 195 | if (subType == IpmbSubType::temp) |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 196 | { |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 197 | snsNum = 0x8d; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 198 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 199 | else |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 200 | { |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 201 | snsNum = 0x8c; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 202 | } |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 203 | netfn = ipmi::me_bridge::netFn; |
| 204 | command = ipmi::me_bridge::sendRawPmbus; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 205 | commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress, |
| 206 | 0x00, 0x00, 0x01, 0x02, snsNum}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 207 | readingFormat = ReadingFormat::elevenBit; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 208 | break; |
| 209 | case IpmbSubType::power: |
| 210 | case IpmbSubType::volt: |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 211 | netfn = ipmi::sensor::netFn; |
| 212 | command = ipmi::sensor::getSensorReading; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 213 | commandData = {deviceAddress}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 214 | readingFormat = ReadingFormat::byte0; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 215 | break; |
| 216 | default: |
| 217 | throw std::runtime_error("Invalid sensor type"); |
| 218 | } |
| 219 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 220 | else if (type == IpmbType::mpsVR) |
| 221 | { |
| 222 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 223 | netfn = ipmi::me_bridge::netFn; |
| 224 | command = ipmi::me_bridge::sendRawPmbus; |
| 225 | initCommand = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 226 | // pmbus read temp |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 227 | commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex, |
| 228 | deviceAddress, 0x00, 0x00, 0x00, 0x00, |
| 229 | 0x01, 0x02, 0x8d}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 230 | // goto page 0 |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 231 | initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex, |
| 232 | deviceAddress, 0x00, 0x00, 0x00, 0x00, |
| 233 | 0x02, 0x00, 0x00, 0x00}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 234 | readingFormat = ReadingFormat::byte3; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 235 | } |
| 236 | else |
| 237 | { |
| 238 | throw std::runtime_error("Invalid sensor type"); |
| 239 | } |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 240 | |
| 241 | if (subType == IpmbSubType::util) |
| 242 | { |
| 243 | // Utilization need to be scaled to percent |
| 244 | maxValue = 100; |
| 245 | minValue = 0; |
| 246 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void IpmbSensor::checkThresholds(void) |
| 250 | { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 251 | thresholds::checkThresholds(this); |
| 252 | } |
| 253 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 254 | bool IpmbSensor::processReading(const std::vector<uint8_t>& data, double& resp) |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 255 | { |
| 256 | |
| 257 | switch (readingFormat) |
| 258 | { |
| 259 | case (ReadingFormat::byte0): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 260 | { |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 261 | if (command == ipmi::sensor::getSensorReading && |
| 262 | !ipmi::sensor::isValid(data)) |
James Feist | cf4238e | 2020-07-28 16:40:03 -0700 | [diff] [blame] | 263 | { |
| 264 | return false; |
| 265 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 266 | resp = data[0]; |
| 267 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 268 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 269 | case (ReadingFormat::byte3): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 270 | { |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 271 | if (data.size() < 4) |
| 272 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 273 | if (errCount == 0U) |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 274 | { |
| 275 | std::cerr << "Invalid data length returned for " << name |
| 276 | << "\n"; |
| 277 | } |
| 278 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 279 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 280 | resp = data[3]; |
| 281 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 282 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 283 | case (ReadingFormat::elevenBit): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 284 | { |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 285 | if (data.size() < 5) |
| 286 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 287 | if (errCount == 0U) |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 288 | { |
| 289 | std::cerr << "Invalid data length returned for " << name |
| 290 | << "\n"; |
| 291 | } |
| 292 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 293 | } |
| 294 | |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 295 | int16_t value = ((data[4] << 8) | data[3]); |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 296 | resp = value; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 297 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 298 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 299 | case (ReadingFormat::elevenBitShift): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 300 | { |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 301 | if (data.size() < 5) |
| 302 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 303 | if (errCount == 0U) |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 304 | { |
| 305 | std::cerr << "Invalid data length returned for " << name |
| 306 | << "\n"; |
| 307 | } |
| 308 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 309 | } |
| 310 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 311 | resp = ((data[4] << 8) | data[3]) >> 3; |
| 312 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 313 | } |
Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 314 | case (ReadingFormat::linearElevenBit): |
| 315 | { |
| 316 | if (data.size() < 5) |
| 317 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 318 | if (errCount == 0U) |
Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 319 | { |
| 320 | std::cerr << "Invalid data length returned for " << name |
| 321 | << "\n"; |
| 322 | } |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | int16_t value = ((data[4] << 8) | data[3]); |
| 327 | constexpr const size_t shift = 16 - 11; // 11bit into 16bit |
| 328 | value <<= shift; |
| 329 | value >>= shift; |
| 330 | resp = value; |
| 331 | return true; |
| 332 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 333 | default: |
| 334 | throw std::runtime_error("Invalid reading type"); |
| 335 | } |
| 336 | } |
| 337 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 338 | void IpmbSensor::read(void) |
| 339 | { |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 340 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 341 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 342 | if (ec == boost::asio::error::operation_aborted) |
| 343 | { |
| 344 | return; // we're being canceled |
| 345 | } |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 346 | if (!readingStateGood()) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 347 | { |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 348 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 349 | read(); |
| 350 | return; |
| 351 | } |
| 352 | dbusConnection->async_method_call( |
| 353 | [this](boost::system::error_code ec, |
| 354 | const IpmbMethodType& response) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 355 | const int& status = std::get<0>(response); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 356 | if (ec || (status != 0)) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 357 | { |
| 358 | incrementError(); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 359 | read(); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 360 | return; |
| 361 | } |
| 362 | const std::vector<uint8_t>& data = std::get<5>(response); |
| 363 | if constexpr (debug) |
| 364 | { |
| 365 | std::cout << name << ": "; |
| 366 | for (size_t d : data) |
| 367 | { |
| 368 | std::cout << d << " "; |
| 369 | } |
| 370 | std::cout << "\n"; |
| 371 | } |
| 372 | if (data.empty()) |
| 373 | { |
| 374 | incrementError(); |
| 375 | read(); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | double value = 0; |
| 380 | |
| 381 | if (!processReading(data, value)) |
| 382 | { |
| 383 | incrementError(); |
| 384 | read(); |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | // rawValue only used in debug logging |
| 389 | // up to 5th byte in data are used to derive value |
| 390 | size_t end = std::min(sizeof(uint64_t), data.size()); |
| 391 | uint64_t rawData = 0; |
| 392 | for (size_t i = 0; i < end; i++) |
| 393 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 394 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 395 | reinterpret_cast<uint8_t*>(&rawData)[i] = data[i]; |
| 396 | } |
| 397 | rawValue = static_cast<double>(rawData); |
| 398 | |
| 399 | /* Adjust value as per scale and offset */ |
| 400 | value = (value * scaleVal) + offsetVal; |
| 401 | updateValue(value); |
| 402 | read(); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 403 | }, |
| 404 | "xyz.openbmc_project.Ipmi.Channel.Ipmb", |
| 405 | "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb", |
| 406 | "sendRequest", commandAddress, netfn, lun, command, commandData); |
| 407 | }); |
| 408 | } |
Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 409 | |
| 410 | bool IpmbSensor::sensorClassType(const std::string& sensorClass) |
| 411 | { |
| 412 | if (sensorClass == "PxeBridgeTemp") |
| 413 | { |
| 414 | type = IpmbType::PXE1410CVR; |
| 415 | } |
| 416 | else if (sensorClass == "IRBridgeTemp") |
| 417 | { |
| 418 | type = IpmbType::IR38363VR; |
| 419 | } |
| 420 | else if (sensorClass == "HSCBridge") |
| 421 | { |
| 422 | type = IpmbType::ADM1278HSC; |
| 423 | } |
| 424 | else if (sensorClass == "MpsBridgeTemp") |
| 425 | { |
| 426 | type = IpmbType::mpsVR; |
| 427 | } |
| 428 | else if (sensorClass == "METemp" || sensorClass == "MESensor") |
| 429 | { |
| 430 | type = IpmbType::meSensor; |
| 431 | } |
| 432 | else |
| 433 | { |
| 434 | std::cerr << "Invalid class " << sensorClass << "\n"; |
| 435 | return false; |
| 436 | } |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | void IpmbSensor::sensorSubType(const std::string& sensorTypeName) |
| 441 | { |
| 442 | if (sensorTypeName == "voltage") |
| 443 | { |
| 444 | subType = IpmbSubType::volt; |
| 445 | } |
| 446 | else if (sensorTypeName == "power") |
| 447 | { |
| 448 | subType = IpmbSubType::power; |
| 449 | } |
| 450 | else if (sensorTypeName == "current") |
| 451 | { |
| 452 | subType = IpmbSubType::curr; |
| 453 | } |
| 454 | else if (sensorTypeName == "utilization") |
| 455 | { |
| 456 | subType = IpmbSubType::util; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | subType = IpmbSubType::temp; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | void IpmbSensor::parseConfigValues(const SensorBaseConfigMap& entry) |
| 465 | { |
| 466 | auto findScaleVal = entry.find("ScaleValue"); |
| 467 | if (findScaleVal != entry.end()) |
| 468 | { |
| 469 | scaleVal = std::visit(VariantToDoubleVisitor(), findScaleVal->second); |
| 470 | } |
| 471 | |
| 472 | auto findOffsetVal = entry.find("OffsetValue"); |
| 473 | if (findOffsetVal != entry.end()) |
| 474 | { |
| 475 | offsetVal = std::visit(VariantToDoubleVisitor(), findOffsetVal->second); |
| 476 | } |
| 477 | |
Zev Weiss | a4d2768 | 2022-07-19 15:30:36 -0700 | [diff] [blame] | 478 | readState = getPowerState(entry); |
Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 479 | } |
| 480 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 481 | void createSensors( |
| 482 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 483 | boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>>& |
| 484 | sensors, |
| 485 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 486 | { |
| 487 | if (!dbusConnection) |
| 488 | { |
| 489 | std::cerr << "Connection not created\n"; |
| 490 | return; |
| 491 | } |
| 492 | dbusConnection->async_method_call( |
| 493 | [&](boost::system::error_code ec, const ManagedObjectType& resp) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 494 | if (ec) |
| 495 | { |
| 496 | std::cerr << "Error contacting entity manager\n"; |
| 497 | return; |
| 498 | } |
| 499 | for (const auto& pathPair : resp) |
| 500 | { |
| 501 | for (const auto& entry : pathPair.second) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 502 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 503 | if (entry.first != configInterface) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 504 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 505 | continue; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 506 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 507 | std::string name = |
| 508 | loadVariant<std::string>(entry.second, "Name"); |
| 509 | |
| 510 | std::vector<thresholds::Threshold> sensorThresholds; |
| 511 | if (!parseThresholdsFromConfig(pathPair.second, |
| 512 | sensorThresholds)) |
| 513 | { |
| 514 | std::cerr << "error populating thresholds for " << name |
| 515 | << "\n"; |
| 516 | } |
| 517 | uint8_t deviceAddress = |
| 518 | loadVariant<uint8_t>(entry.second, "Address"); |
| 519 | |
| 520 | std::string sensorClass = |
| 521 | loadVariant<std::string>(entry.second, "Class"); |
| 522 | |
| 523 | uint8_t hostSMbusIndex = hostSMbusIndexDefault; |
| 524 | auto findSmType = entry.second.find("HostSMbusIndex"); |
| 525 | if (findSmType != entry.second.end()) |
| 526 | { |
| 527 | hostSMbusIndex = std::visit(VariantToUnsignedIntVisitor(), |
| 528 | findSmType->second); |
| 529 | } |
| 530 | |
| 531 | float pollRate = pollRateDefault; |
| 532 | auto findPollRate = entry.second.find("PollRate"); |
| 533 | if (findPollRate != entry.second.end()) |
| 534 | { |
| 535 | pollRate = std::visit(VariantToFloatVisitor(), |
| 536 | findPollRate->second); |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 537 | if (pollRate <= 0.0F) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 538 | { |
| 539 | pollRate = pollRateDefault; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* Default sensor type is "temperature" */ |
| 544 | std::string sensorTypeName = "temperature"; |
| 545 | auto findType = entry.second.find("SensorType"); |
| 546 | if (findType != entry.second.end()) |
| 547 | { |
| 548 | sensorTypeName = |
| 549 | std::visit(VariantToStringVisitor(), findType->second); |
| 550 | } |
| 551 | |
| 552 | auto& sensor = sensors[name]; |
| 553 | sensor = std::make_unique<IpmbSensor>( |
| 554 | dbusConnection, io, name, pathPair.first, objectServer, |
| 555 | std::move(sensorThresholds), deviceAddress, hostSMbusIndex, |
| 556 | pollRate, sensorTypeName); |
| 557 | |
| 558 | sensor->parseConfigValues(entry.second); |
| 559 | if (!(sensor->sensorClassType(sensorClass))) |
| 560 | { |
| 561 | continue; |
| 562 | } |
| 563 | sensor->sensorSubType(sensorTypeName); |
| 564 | sensor->init(); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 565 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 566 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 567 | }, |
| 568 | entityManagerName, "/", "org.freedesktop.DBus.ObjectManager", |
| 569 | "GetManagedObjects"); |
| 570 | } |
| 571 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 572 | void reinitSensors(sdbusplus::message::message& message) |
| 573 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 574 | constexpr const size_t reinitWaitSeconds = 2; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 575 | std::string objectName; |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 576 | boost::container::flat_map<std::string, std::variant<std::string>> values; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 577 | message.read(objectName, values); |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 578 | |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 579 | auto findStatus = values.find(power::property); |
| 580 | if (findStatus != values.end()) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 581 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 582 | bool powerStatus = boost::ends_with( |
Thu Ba Nguyen | 585d3a0 | 2021-07-20 20:25:15 +0700 | [diff] [blame] | 583 | std::get<std::string>(findStatus->second), ".Running"); |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 584 | if (powerStatus) |
| 585 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 586 | if (!initCmdTimer) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 587 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 588 | // this should be impossible |
| 589 | return; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 590 | } |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 591 | // we seem to send this command too fast sometimes, wait before |
| 592 | // sending |
| 593 | initCmdTimer->expires_from_now( |
| 594 | boost::posix_time::seconds(reinitWaitSeconds)); |
| 595 | |
| 596 | initCmdTimer->async_wait([](const boost::system::error_code ec) { |
| 597 | if (ec == boost::asio::error::operation_aborted) |
| 598 | { |
| 599 | return; // we're being canceled |
| 600 | } |
| 601 | |
| 602 | for (const auto& sensor : sensors) |
| 603 | { |
| 604 | if (sensor.second) |
| 605 | { |
| 606 | sensor.second->runInitCmd(); |
| 607 | } |
| 608 | } |
| 609 | }); |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | } |
| 613 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 614 | int main() |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 615 | { |
| 616 | |
| 617 | boost::asio::io_service io; |
| 618 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 619 | systemBus->request_name("xyz.openbmc_project.IpmbSensor"); |
| 620 | sdbusplus::asio::object_server objectServer(systemBus); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 621 | |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 622 | initCmdTimer = std::make_unique<boost::asio::deadline_timer>(io); |
| 623 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 624 | io.post([&]() { createSensors(io, objectServer, sensors, systemBus); }); |
| 625 | |
| 626 | boost::asio::deadline_timer configTimer(io); |
| 627 | |
| 628 | std::function<void(sdbusplus::message::message&)> eventHandler = |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 629 | [&](sdbusplus::message::message&) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 630 | configTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 631 | // create a timer because normally multiple properties change |
| 632 | configTimer.async_wait([&](const boost::system::error_code& ec) { |
| 633 | if (ec == boost::asio::error::operation_aborted) |
| 634 | { |
| 635 | return; // we're being canceled |
| 636 | } |
| 637 | createSensors(io, objectServer, sensors, systemBus); |
| 638 | if (sensors.empty()) |
| 639 | { |
| 640 | std::cout << "Configuration not detected\n"; |
| 641 | } |
| 642 | }); |
| 643 | }; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 644 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 645 | sdbusplus::bus::match::match configMatch( |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 646 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 647 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 648 | std::string(inventoryPath) + "',arg0namespace='" + configInterface + |
| 649 | "'", |
| 650 | eventHandler); |
| 651 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 652 | sdbusplus::bus::match::match powerChangeMatch( |
| 653 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 654 | "type='signal',interface='" + std::string(properties::interface) + |
| 655 | "',path='" + std::string(power::path) + "',arg0='" + |
| 656 | std::string(power::interface) + "'", |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 657 | reinitSensors); |
| 658 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 659 | setupManufacturingModeMatch(*systemBus); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 660 | io.run(); |
Zhikui Ren | e76a5a6 | 2021-07-09 15:16:32 -0700 | [diff] [blame] | 661 | return 0; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 662 | } |