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 | |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 99 | std::string IpmbSensor::getSubTypeUnits(void) |
| 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) { |
| 136 | const int& status = std::get<0>(response); |
| 137 | |
| 138 | if (ec || status) |
| 139 | { |
| 140 | std::cerr |
| 141 | << "Error setting init command for device: " << name |
| 142 | << "\n"; |
| 143 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 144 | }, |
| 145 | "xyz.openbmc_project.Ipmi.Channel.Ipmb", |
| 146 | "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb", |
| 147 | "sendRequest", commandAddress, netfn, lun, *initCommand, initData); |
| 148 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void IpmbSensor::loadDefaults() |
| 152 | { |
| 153 | if (type == IpmbType::meSensor) |
| 154 | { |
| 155 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 156 | netfn = ipmi::sensor::netFn; |
| 157 | command = ipmi::sensor::getSensorReading; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 158 | commandData = {deviceAddress}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 159 | readingFormat = ReadingFormat::byte0; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 160 | } |
| 161 | else if (type == IpmbType::PXE1410CVR) |
| 162 | { |
| 163 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 164 | netfn = ipmi::me_bridge::netFn; |
| 165 | command = ipmi::me_bridge::sendRawPmbus; |
| 166 | initCommand = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 167 | // pmbus read temp |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 168 | commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex, |
| 169 | deviceAddress, 0x00, 0x00, 0x00, 0x00, |
| 170 | 0x01, 0x02, 0x8d}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 171 | // goto page 0 |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 172 | initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex, |
| 173 | deviceAddress, 0x00, 0x00, 0x00, 0x00, |
| 174 | 0x02, 0x00, 0x00, 0x00}; |
Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 175 | readingFormat = ReadingFormat::linearElevenBit; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 176 | } |
| 177 | else if (type == IpmbType::IR38363VR) |
| 178 | { |
| 179 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 180 | netfn = ipmi::me_bridge::netFn; |
| 181 | command = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 182 | // pmbus read temp |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 183 | commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex, |
| 184 | deviceAddress, 00, 0x00, 0x00, 0x00, |
| 185 | 0x01, 0x02, 0x8D}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 186 | readingFormat = ReadingFormat::elevenBitShift; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 187 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 188 | else if (type == IpmbType::ADM1278HSC) |
| 189 | { |
| 190 | commandAddress = meAddress; |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 191 | uint8_t snsNum = 0; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 192 | switch (subType) |
| 193 | { |
| 194 | case IpmbSubType::temp: |
| 195 | case IpmbSubType::curr: |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 196 | if (subType == IpmbSubType::temp) |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 197 | { |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 198 | snsNum = 0x8d; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 199 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 200 | else |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 201 | { |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 202 | snsNum = 0x8c; |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 203 | } |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 204 | netfn = ipmi::me_bridge::netFn; |
| 205 | command = ipmi::me_bridge::sendRawPmbus; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 206 | commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress, |
| 207 | 0x00, 0x00, 0x01, 0x02, snsNum}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 208 | readingFormat = ReadingFormat::elevenBit; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 209 | break; |
| 210 | case IpmbSubType::power: |
| 211 | case IpmbSubType::volt: |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 212 | netfn = ipmi::sensor::netFn; |
| 213 | command = ipmi::sensor::getSensorReading; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 214 | commandData = {deviceAddress}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 215 | readingFormat = ReadingFormat::byte0; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 216 | break; |
| 217 | default: |
| 218 | throw std::runtime_error("Invalid sensor type"); |
| 219 | } |
| 220 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 221 | else if (type == IpmbType::mpsVR) |
| 222 | { |
| 223 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 224 | netfn = ipmi::me_bridge::netFn; |
| 225 | command = ipmi::me_bridge::sendRawPmbus; |
| 226 | initCommand = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 227 | // pmbus read temp |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 228 | commandData = {0x57, 0x01, 0x00, 0x16, hostSMbusIndex, |
| 229 | deviceAddress, 0x00, 0x00, 0x00, 0x00, |
| 230 | 0x01, 0x02, 0x8d}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 231 | // goto page 0 |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 232 | initData = {0x57, 0x01, 0x00, 0x14, hostSMbusIndex, |
| 233 | deviceAddress, 0x00, 0x00, 0x00, 0x00, |
| 234 | 0x02, 0x00, 0x00, 0x00}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 235 | readingFormat = ReadingFormat::byte3; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 236 | } |
| 237 | else |
| 238 | { |
| 239 | throw std::runtime_error("Invalid sensor type"); |
| 240 | } |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 241 | |
| 242 | if (subType == IpmbSubType::util) |
| 243 | { |
| 244 | // Utilization need to be scaled to percent |
| 245 | maxValue = 100; |
| 246 | minValue = 0; |
| 247 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | void IpmbSensor::checkThresholds(void) |
| 251 | { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 252 | thresholds::checkThresholds(this); |
| 253 | } |
| 254 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 255 | bool IpmbSensor::processReading(const std::vector<uint8_t>& data, double& resp) |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 256 | { |
| 257 | |
| 258 | switch (readingFormat) |
| 259 | { |
| 260 | case (ReadingFormat::byte0): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 261 | { |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 262 | if (command == ipmi::sensor::getSensorReading && |
| 263 | !ipmi::sensor::isValid(data)) |
James Feist | cf4238e | 2020-07-28 16:40:03 -0700 | [diff] [blame] | 264 | { |
| 265 | return false; |
| 266 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 267 | resp = data[0]; |
| 268 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 269 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 270 | case (ReadingFormat::byte3): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 271 | { |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 272 | if (data.size() < 4) |
| 273 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 274 | if (!errCount) |
| 275 | { |
| 276 | std::cerr << "Invalid data length returned for " << name |
| 277 | << "\n"; |
| 278 | } |
| 279 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 280 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 281 | resp = data[3]; |
| 282 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 283 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 284 | case (ReadingFormat::elevenBit): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 285 | { |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 286 | if (data.size() < 5) |
| 287 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 288 | if (!errCount) |
| 289 | { |
| 290 | std::cerr << "Invalid data length returned for " << name |
| 291 | << "\n"; |
| 292 | } |
| 293 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 294 | } |
| 295 | |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 296 | int16_t value = ((data[4] << 8) | data[3]); |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 297 | resp = value; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 298 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 299 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 300 | case (ReadingFormat::elevenBitShift): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 301 | { |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 302 | if (data.size() < 5) |
| 303 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 304 | if (!errCount) |
| 305 | { |
| 306 | std::cerr << "Invalid data length returned for " << name |
| 307 | << "\n"; |
| 308 | } |
| 309 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 310 | } |
| 311 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 312 | resp = ((data[4] << 8) | data[3]) >> 3; |
| 313 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 314 | } |
Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 315 | case (ReadingFormat::linearElevenBit): |
| 316 | { |
| 317 | if (data.size() < 5) |
| 318 | { |
| 319 | if (!errCount) |
| 320 | { |
| 321 | std::cerr << "Invalid data length returned for " << name |
| 322 | << "\n"; |
| 323 | } |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | int16_t value = ((data[4] << 8) | data[3]); |
| 328 | constexpr const size_t shift = 16 - 11; // 11bit into 16bit |
| 329 | value <<= shift; |
| 330 | value >>= shift; |
| 331 | resp = value; |
| 332 | return true; |
| 333 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 334 | default: |
| 335 | throw std::runtime_error("Invalid reading type"); |
| 336 | } |
| 337 | } |
| 338 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 339 | void IpmbSensor::read(void) |
| 340 | { |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 341 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 342 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 343 | if (ec == boost::asio::error::operation_aborted) |
| 344 | { |
| 345 | return; // we're being canceled |
| 346 | } |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 347 | if (!readingStateGood()) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 348 | { |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 349 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 350 | read(); |
| 351 | return; |
| 352 | } |
| 353 | dbusConnection->async_method_call( |
| 354 | [this](boost::system::error_code ec, |
| 355 | const IpmbMethodType& response) { |
| 356 | const int& status = std::get<0>(response); |
| 357 | if (ec || status) |
| 358 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 359 | incrementError(); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 360 | read(); |
| 361 | return; |
| 362 | } |
| 363 | const std::vector<uint8_t>& data = std::get<5>(response); |
| 364 | if constexpr (debug) |
| 365 | { |
| 366 | std::cout << name << ": "; |
| 367 | for (size_t d : data) |
| 368 | { |
| 369 | std::cout << d << " "; |
| 370 | } |
| 371 | std::cout << "\n"; |
| 372 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 373 | if (data.empty()) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 374 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 375 | incrementError(); |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 376 | read(); |
| 377 | return; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 378 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 379 | |
| 380 | double value = 0; |
| 381 | |
| 382 | if (!processReading(data, value)) |
| 383 | { |
| 384 | incrementError(); |
| 385 | read(); |
| 386 | return; |
| 387 | } |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 388 | |
| 389 | // rawValue only used in debug logging |
| 390 | // up to 5th byte in data are used to derive value |
| 391 | size_t end = std::min(sizeof(uint64_t), data.size()); |
| 392 | uint64_t rawData = 0; |
| 393 | for (size_t i = 0; i < end; i++) |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 394 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 395 | reinterpret_cast<uint8_t*>(&rawData)[i] = data[i]; |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 396 | } |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 397 | rawValue = static_cast<double>(rawData); |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 398 | |
| 399 | /* Adjust value as per scale and offset */ |
| 400 | value = (value * scaleVal) + offsetVal; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 401 | updateValue(value); |
| 402 | read(); |
| 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 | } |
| 409 | void createSensors( |
| 410 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 411 | boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>>& |
| 412 | sensors, |
| 413 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 414 | { |
| 415 | if (!dbusConnection) |
| 416 | { |
| 417 | std::cerr << "Connection not created\n"; |
| 418 | return; |
| 419 | } |
| 420 | dbusConnection->async_method_call( |
| 421 | [&](boost::system::error_code ec, const ManagedObjectType& resp) { |
| 422 | if (ec) |
| 423 | { |
| 424 | std::cerr << "Error contacting entity manager\n"; |
| 425 | return; |
| 426 | } |
| 427 | for (const auto& pathPair : resp) |
| 428 | { |
| 429 | for (const auto& entry : pathPair.second) |
| 430 | { |
| 431 | if (entry.first != configInterface) |
| 432 | { |
| 433 | continue; |
| 434 | } |
| 435 | std::string name = |
| 436 | loadVariant<std::string>(entry.second, "Name"); |
| 437 | |
| 438 | std::vector<thresholds::Threshold> sensorThresholds; |
| 439 | if (!parseThresholdsFromConfig(pathPair.second, |
| 440 | sensorThresholds)) |
| 441 | { |
| 442 | std::cerr << "error populating thresholds for " << name |
| 443 | << "\n"; |
| 444 | } |
| 445 | uint8_t deviceAddress = |
| 446 | loadVariant<uint8_t>(entry.second, "Address"); |
| 447 | |
| 448 | std::string sensorClass = |
| 449 | loadVariant<std::string>(entry.second, "Class"); |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 450 | |
Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 451 | uint8_t hostSMbusIndex = hostSMbusIndexDefault; |
| 452 | auto findSmType = entry.second.find("HostSMbusIndex"); |
| 453 | if (findSmType != entry.second.end()) |
| 454 | { |
| 455 | hostSMbusIndex = std::visit( |
| 456 | VariantToUnsignedIntVisitor(), findSmType->second); |
| 457 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 458 | |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 459 | float pollRate = pollRateDefault; |
| 460 | auto findPollRate = entry.second.find("PollRate"); |
| 461 | if (findPollRate != entry.second.end()) |
| 462 | { |
| 463 | pollRate = std::visit(VariantToFloatVisitor(), |
| 464 | findPollRate->second); |
| 465 | if (pollRate <= 0.0f) |
| 466 | { |
| 467 | pollRate = pollRateDefault; |
| 468 | } |
| 469 | } |
| 470 | |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 471 | /* Default sensor type is "temperature" */ |
| 472 | std::string sensorTypeName = "temperature"; |
| 473 | auto findType = entry.second.find("SensorType"); |
| 474 | if (findType != entry.second.end()) |
| 475 | { |
| 476 | sensorTypeName = std::visit(VariantToStringVisitor(), |
| 477 | findType->second); |
| 478 | } |
| 479 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 480 | auto& sensor = sensors[name]; |
| 481 | sensor = std::make_unique<IpmbSensor>( |
| 482 | dbusConnection, io, name, pathPair.first, objectServer, |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 483 | std::move(sensorThresholds), deviceAddress, |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 484 | hostSMbusIndex, pollRate, sensorTypeName); |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 485 | |
| 486 | /* Initialize scale and offset value */ |
| 487 | sensor->scaleVal = 1; |
| 488 | sensor->offsetVal = 0; |
| 489 | |
| 490 | auto findScaleVal = entry.second.find("ScaleValue"); |
| 491 | if (findScaleVal != entry.second.end()) |
| 492 | { |
| 493 | sensor->scaleVal = std::visit(VariantToDoubleVisitor(), |
| 494 | findScaleVal->second); |
| 495 | } |
| 496 | |
| 497 | auto findOffsetVal = entry.second.find("OffsetValue"); |
| 498 | if (findOffsetVal != entry.second.end()) |
| 499 | { |
| 500 | sensor->offsetVal = std::visit(VariantToDoubleVisitor(), |
| 501 | findOffsetVal->second); |
| 502 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 503 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 504 | auto findPowerState = entry.second.find("PowerState"); |
| 505 | |
| 506 | if (findPowerState != entry.second.end()) |
| 507 | { |
| 508 | std::string powerState = std::visit( |
| 509 | VariantToStringVisitor(), findPowerState->second); |
| 510 | |
| 511 | setReadState(powerState, sensor->readState); |
| 512 | } |
| 513 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 514 | if (sensorClass == "PxeBridgeTemp") |
| 515 | { |
| 516 | sensor->type = IpmbType::PXE1410CVR; |
| 517 | } |
| 518 | else if (sensorClass == "IRBridgeTemp") |
| 519 | { |
| 520 | sensor->type = IpmbType::IR38363VR; |
| 521 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 522 | else if (sensorClass == "HSCBridge") |
| 523 | { |
| 524 | sensor->type = IpmbType::ADM1278HSC; |
| 525 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 526 | else if (sensorClass == "MpsBridgeTemp") |
| 527 | { |
| 528 | sensor->type = IpmbType::mpsVR; |
| 529 | } |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 530 | else if (sensorClass == "METemp" || |
| 531 | sensorClass == "MESensor") |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 532 | { |
| 533 | sensor->type = IpmbType::meSensor; |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | std::cerr << "Invalid class " << sensorClass << "\n"; |
| 538 | continue; |
| 539 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 540 | |
| 541 | if (sensorTypeName == "voltage") |
| 542 | { |
| 543 | sensor->subType = IpmbSubType::volt; |
| 544 | } |
| 545 | else if (sensorTypeName == "power") |
| 546 | { |
| 547 | sensor->subType = IpmbSubType::power; |
| 548 | } |
| 549 | else if (sensorTypeName == "current") |
| 550 | { |
| 551 | sensor->subType = IpmbSubType::curr; |
| 552 | } |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 553 | else if (sensorTypeName == "utilization") |
| 554 | { |
| 555 | sensor->subType = IpmbSubType::util; |
| 556 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 557 | else |
| 558 | { |
| 559 | sensor->subType = IpmbSubType::temp; |
| 560 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 561 | sensor->init(); |
| 562 | } |
| 563 | } |
| 564 | }, |
| 565 | entityManagerName, "/", "org.freedesktop.DBus.ObjectManager", |
| 566 | "GetManagedObjects"); |
| 567 | } |
| 568 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 569 | void reinitSensors(sdbusplus::message::message& message) |
| 570 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 571 | constexpr const size_t reinitWaitSeconds = 2; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 572 | std::string objectName; |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 573 | boost::container::flat_map<std::string, std::variant<std::string>> values; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 574 | message.read(objectName, values); |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 575 | |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 576 | auto findStatus = values.find(power::property); |
| 577 | if (findStatus != values.end()) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 578 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 579 | bool powerStatus = boost::ends_with( |
Thu Ba Nguyen | 585d3a0 | 2021-07-20 20:25:15 +0700 | [diff] [blame] | 580 | std::get<std::string>(findStatus->second), ".Running"); |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 581 | if (powerStatus) |
| 582 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 583 | if (!initCmdTimer) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 584 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 585 | // this should be impossible |
| 586 | return; |
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 | // we seem to send this command too fast sometimes, wait before |
| 589 | // sending |
| 590 | initCmdTimer->expires_from_now( |
| 591 | boost::posix_time::seconds(reinitWaitSeconds)); |
| 592 | |
| 593 | initCmdTimer->async_wait([](const boost::system::error_code ec) { |
| 594 | if (ec == boost::asio::error::operation_aborted) |
| 595 | { |
| 596 | return; // we're being canceled |
| 597 | } |
| 598 | |
| 599 | for (const auto& sensor : sensors) |
| 600 | { |
| 601 | if (sensor.second) |
| 602 | { |
| 603 | sensor.second->runInitCmd(); |
| 604 | } |
| 605 | } |
| 606 | }); |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 611 | int main() |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 612 | { |
| 613 | |
| 614 | boost::asio::io_service io; |
| 615 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 616 | systemBus->request_name("xyz.openbmc_project.IpmbSensor"); |
| 617 | sdbusplus::asio::object_server objectServer(systemBus); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 618 | |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 619 | initCmdTimer = std::make_unique<boost::asio::deadline_timer>(io); |
| 620 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 621 | io.post([&]() { createSensors(io, objectServer, sensors, systemBus); }); |
| 622 | |
| 623 | boost::asio::deadline_timer configTimer(io); |
| 624 | |
| 625 | std::function<void(sdbusplus::message::message&)> eventHandler = |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 626 | [&](sdbusplus::message::message&) { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 627 | configTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 628 | // create a timer because normally multiple properties change |
| 629 | configTimer.async_wait([&](const boost::system::error_code& ec) { |
| 630 | if (ec == boost::asio::error::operation_aborted) |
| 631 | { |
| 632 | return; // we're being canceled |
| 633 | } |
| 634 | createSensors(io, objectServer, sensors, systemBus); |
| 635 | if (sensors.empty()) |
| 636 | { |
| 637 | std::cout << "Configuration not detected\n"; |
| 638 | } |
| 639 | }); |
| 640 | }; |
| 641 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 642 | sdbusplus::bus::match::match configMatch( |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 643 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 644 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 645 | std::string(inventoryPath) + "',arg0namespace='" + configInterface + |
| 646 | "'", |
| 647 | eventHandler); |
| 648 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 649 | sdbusplus::bus::match::match powerChangeMatch( |
| 650 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 651 | "type='signal',interface='" + std::string(properties::interface) + |
| 652 | "',path='" + std::string(power::path) + "',arg0='" + |
| 653 | std::string(power::interface) + "'", |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 654 | reinitSensors); |
| 655 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 656 | setupManufacturingModeMatch(*systemBus); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 657 | io.run(); |
Zhikui Ren | e76a5a6 | 2021-07-09 15:16:32 -0700 | [diff] [blame] | 658 | return 0; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 659 | } |