| 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 |  | 
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "IpmbSensor.hpp" | 
 | 18 |  | 
 | 19 | #include "IpmbSDRSensor.hpp" | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 20 | #include "SensorPaths.hpp" | 
 | 21 | #include "Thresholds.hpp" | 
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 22 | #include "Utils.hpp" | 
 | 23 | #include "VariantVisitors.hpp" | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 24 | #include "sensor.hpp" | 
| Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 25 |  | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 26 | #include <boost/asio/error.hpp> | 
 | 27 | #include <boost/asio/io_context.hpp> | 
 | 28 | #include <boost/asio/post.hpp> | 
 | 29 | #include <boost/asio/steady_timer.hpp> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 30 | #include <boost/container/flat_map.hpp> | 
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 31 | #include <sdbusplus/asio/connection.hpp> | 
 | 32 | #include <sdbusplus/asio/object_server.hpp> | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 33 | #include <sdbusplus/bus.hpp> | 
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 34 | #include <sdbusplus/bus/match.hpp> | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 35 | #include <sdbusplus/message.hpp> | 
 | 36 | #include <sdbusplus/message/native_types.hpp> | 
| James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 37 |  | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 38 | #include <algorithm> | 
 | 39 | #include <array> | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 40 | #include <chrono> | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 41 | #include <cstddef> | 
 | 42 | #include <cstdint> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 43 | #include <functional> | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 44 | #include <iostream> | 
 | 45 | #include <limits> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 46 | #include <memory> | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 47 | #include <stdexcept> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 48 | #include <string> | 
 | 49 | #include <tuple> | 
| Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 50 | #include <utility> | 
| Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 51 | #include <variant> | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 52 | #include <vector> | 
 | 53 |  | 
 | 54 | constexpr const bool debug = false; | 
 | 55 |  | 
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 56 | constexpr const char* sensorType = "IpmbSensor"; | 
| Jayashree Dhanapal | 3746c55 | 2022-03-21 14:45:52 +0530 | [diff] [blame] | 57 | constexpr const char* sdrInterface = "IpmbDevice"; | 
 | 58 |  | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 59 | static constexpr double ipmbMaxReading = 0xFF; | 
 | 60 | static constexpr double ipmbMinReading = 0; | 
 | 61 |  | 
 | 62 | static constexpr uint8_t meAddress = 1; | 
 | 63 | static constexpr uint8_t lun = 0; | 
| Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 64 | static constexpr uint8_t hostSMbusIndexDefault = 0x03; | 
| Jayashree Dhanapal | 6ee6294 | 2021-12-14 15:22:23 +0530 | [diff] [blame] | 65 | static constexpr uint8_t ipmbBusIndexDefault = 0; | 
| Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 66 | static constexpr float pollRateDefault = 1; // in seconds | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 67 |  | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 68 | static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/"; | 
 | 69 |  | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 70 | boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>> sensors; | 
| Jayashree Dhanapal | 3746c55 | 2022-03-21 14:45:52 +0530 | [diff] [blame] | 71 | boost::container::flat_map<uint8_t, std::shared_ptr<IpmbSDRDevice>> sdrsensor; | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 72 |  | 
| Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 73 | std::unique_ptr<boost::asio::steady_timer> initCmdTimer; | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 74 |  | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 75 | IpmbSensor::IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn, | 
| Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 76 |                        boost::asio::io_context& io, | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 77 |                        const std::string& sensorName, | 
 | 78 |                        const std::string& sensorConfiguration, | 
 | 79 |                        sdbusplus::asio::object_server& objectServer, | 
 | 80 |                        std::vector<thresholds::Threshold>&& thresholdData, | 
| Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 81 |                        uint8_t deviceAddress, uint8_t hostSMbusIndex, | 
| Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 82 |                        const float pollRate, std::string& sensorTypeName) : | 
| Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 83 |     Sensor(escapeName(sensorName), std::move(thresholdData), | 
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 84 |            sensorConfiguration, "IpmbSensor", false, false, ipmbMaxReading, | 
 | 85 |            ipmbMinReading, conn, PowerState::on), | 
| Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 86 |     deviceAddress(deviceAddress), hostSMbusIndex(hostSMbusIndex), | 
| Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 87 |     sensorPollMs(static_cast<int>(pollRate * 1000)), objectServer(objectServer), | 
 | 88 |     waitTimer(io) | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 89 | { | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 90 |     std::string dbusPath = sensorPathPrefix + sensorTypeName + "/" + name; | 
 | 91 |  | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 92 |     sensorInterface = objectServer.add_interface( | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 93 |         dbusPath, "xyz.openbmc_project.Sensor.Value"); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 94 |  | 
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 95 |     for (const auto& threshold : thresholds) | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 96 |     { | 
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 97 |         std::string interface = thresholds::getInterface(threshold.level); | 
 | 98 |         thresholdInterfaces[static_cast<size_t>(threshold.level)] = | 
 | 99 |             objectServer.add_interface(dbusPath, interface); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 100 |     } | 
| James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 101 |     association = objectServer.add_interface(dbusPath, association::interface); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 102 | } | 
 | 103 |  | 
 | 104 | IpmbSensor::~IpmbSensor() | 
 | 105 | { | 
 | 106 |     waitTimer.cancel(); | 
| Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 107 |     for (const auto& iface : thresholdInterfaces) | 
 | 108 |     { | 
 | 109 |         objectServer.remove_interface(iface); | 
 | 110 |     } | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 111 |     objectServer.remove_interface(sensorInterface); | 
| James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 112 |     objectServer.remove_interface(association); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 113 | } | 
 | 114 |  | 
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 115 | std::string IpmbSensor::getSubTypeUnits() const | 
| Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 116 | { | 
 | 117 |     switch (subType) | 
 | 118 |     { | 
 | 119 |         case IpmbSubType::temp: | 
 | 120 |             return sensor_paths::unitDegreesC; | 
 | 121 |         case IpmbSubType::curr: | 
 | 122 |             return sensor_paths::unitAmperes; | 
 | 123 |         case IpmbSubType::power: | 
 | 124 |             return sensor_paths::unitWatts; | 
 | 125 |         case IpmbSubType::volt: | 
 | 126 |             return sensor_paths::unitVolts; | 
 | 127 |         case IpmbSubType::util: | 
 | 128 |             return sensor_paths::unitPercent; | 
 | 129 |         default: | 
 | 130 |             throw std::runtime_error("Invalid sensor type"); | 
 | 131 |     } | 
 | 132 | } | 
 | 133 |  | 
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 134 | void IpmbSensor::init() | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 135 | { | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 136 |     loadDefaults(); | 
| Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 137 |     setInitialProperties(getSubTypeUnits()); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 138 |     if (initCommand) | 
 | 139 |     { | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 140 |         runInitCmd(); | 
 | 141 |     } | 
 | 142 |     read(); | 
 | 143 | } | 
 | 144 |  | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 145 | static void initCmdCb(const std::weak_ptr<IpmbSensor>& weakRef, | 
 | 146 |                       const boost::system::error_code& ec, | 
 | 147 |                       const IpmbMethodType& response) | 
 | 148 | { | 
 | 149 |     std::shared_ptr<IpmbSensor> self = weakRef.lock(); | 
 | 150 |     if (!self) | 
 | 151 |     { | 
 | 152 |         return; | 
 | 153 |     } | 
 | 154 |     const int& status = std::get<0>(response); | 
 | 155 |     if (ec || (status != 0)) | 
 | 156 |     { | 
 | 157 |         std::cerr << "Error setting init command for device: " << self->name | 
 | 158 |                   << "\n"; | 
 | 159 |     } | 
 | 160 | } | 
 | 161 |  | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 162 | void IpmbSensor::runInitCmd() | 
 | 163 | { | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 164 |     if (!initCommand) | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 165 |     { | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 166 |         return; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 167 |     } | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 168 |     dbusConnection->async_method_call( | 
 | 169 |         [weakRef{weak_from_this()}](const boost::system::error_code& ec, | 
 | 170 |                                     const IpmbMethodType& response) { | 
 | 171 |         initCmdCb(weakRef, ec, response); | 
| Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 172 |     }, | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 173 |         "xyz.openbmc_project.Ipmi.Channel.Ipmb", | 
 | 174 |         "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb", | 
 | 175 |         "sendRequest", commandAddress, netfn, lun, *initCommand, initData); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 176 | } | 
 | 177 |  | 
 | 178 | void IpmbSensor::loadDefaults() | 
 | 179 | { | 
 | 180 |     if (type == IpmbType::meSensor) | 
 | 181 |     { | 
 | 182 |         commandAddress = meAddress; | 
| Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 183 |         netfn = ipmi::sensor::netFn; | 
 | 184 |         command = ipmi::sensor::getSensorReading; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 185 |         commandData = {deviceAddress}; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 186 |         readingFormat = ReadingFormat::byte0; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 187 |     } | 
 | 188 |     else if (type == IpmbType::PXE1410CVR) | 
 | 189 |     { | 
 | 190 |         commandAddress = meAddress; | 
| Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 191 |         netfn = ipmi::me_bridge::netFn; | 
 | 192 |         command = ipmi::me_bridge::sendRawPmbus; | 
 | 193 |         initCommand = ipmi::me_bridge::sendRawPmbus; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 194 |         // pmbus read temp | 
| Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 195 |         commandData = {0x57,          0x01, 0x00, 0x16, hostSMbusIndex, | 
 | 196 |                        deviceAddress, 0x00, 0x00, 0x00, 0x00, | 
 | 197 |                        0x01,          0x02, 0x8d}; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 198 |         // goto page 0 | 
| Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 199 |         initData = {0x57,          0x01, 0x00, 0x14, hostSMbusIndex, | 
 | 200 |                     deviceAddress, 0x00, 0x00, 0x00, 0x00, | 
 | 201 |                     0x02,          0x00, 0x00, 0x00}; | 
| Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 202 |         readingFormat = ReadingFormat::linearElevenBit; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 203 |     } | 
 | 204 |     else if (type == IpmbType::IR38363VR) | 
 | 205 |     { | 
 | 206 |         commandAddress = meAddress; | 
| Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 207 |         netfn = ipmi::me_bridge::netFn; | 
 | 208 |         command = ipmi::me_bridge::sendRawPmbus; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 209 |         // pmbus read temp | 
| Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 210 |         commandData = {0x57,          0x01, 0x00, 0x16, hostSMbusIndex, | 
 | 211 |                        deviceAddress, 00,   0x00, 0x00, 0x00, | 
 | 212 |                        0x01,          0x02, 0x8D}; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 213 |         readingFormat = ReadingFormat::elevenBitShift; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 214 |     } | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 215 |     else if (type == IpmbType::ADM1278HSC) | 
 | 216 |     { | 
 | 217 |         commandAddress = meAddress; | 
| Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 218 |         uint8_t snsNum = 0; | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 219 |         switch (subType) | 
 | 220 |         { | 
 | 221 |             case IpmbSubType::temp: | 
 | 222 |             case IpmbSubType::curr: | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 223 |                 if (subType == IpmbSubType::temp) | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 224 |                 { | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 225 |                     snsNum = 0x8d; | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 226 |                 } | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 227 |                 else | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 228 |                 { | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 229 |                     snsNum = 0x8c; | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 230 |                 } | 
| Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 231 |                 netfn = ipmi::me_bridge::netFn; | 
 | 232 |                 command = ipmi::me_bridge::sendRawPmbus; | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 233 |                 commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress, | 
 | 234 |                                0x00, 0x00, 0x01, 0x02, snsNum}; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 235 |                 readingFormat = ReadingFormat::elevenBit; | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 236 |                 break; | 
 | 237 |             case IpmbSubType::power: | 
 | 238 |             case IpmbSubType::volt: | 
| Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 239 |                 netfn = ipmi::sensor::netFn; | 
 | 240 |                 command = ipmi::sensor::getSensorReading; | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 241 |                 commandData = {deviceAddress}; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 242 |                 readingFormat = ReadingFormat::byte0; | 
| Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 243 |                 break; | 
 | 244 |             default: | 
 | 245 |                 throw std::runtime_error("Invalid sensor type"); | 
 | 246 |         } | 
 | 247 |     } | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 248 |     else if (type == IpmbType::mpsVR) | 
 | 249 |     { | 
 | 250 |         commandAddress = meAddress; | 
| Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 251 |         netfn = ipmi::me_bridge::netFn; | 
 | 252 |         command = ipmi::me_bridge::sendRawPmbus; | 
 | 253 |         initCommand = ipmi::me_bridge::sendRawPmbus; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 254 |         // pmbus read temp | 
| Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 255 |         commandData = {0x57,          0x01, 0x00, 0x16, hostSMbusIndex, | 
 | 256 |                        deviceAddress, 0x00, 0x00, 0x00, 0x00, | 
 | 257 |                        0x01,          0x02, 0x8d}; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 258 |         // goto page 0 | 
| Anoop S | 832a2c6 | 2020-11-20 19:21:22 +0000 | [diff] [blame] | 259 |         initData = {0x57,          0x01, 0x00, 0x14, hostSMbusIndex, | 
 | 260 |                     deviceAddress, 0x00, 0x00, 0x00, 0x00, | 
 | 261 |                     0x02,          0x00, 0x00, 0x00}; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 262 |         readingFormat = ReadingFormat::byte3; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 263 |     } | 
 | 264 |     else | 
 | 265 |     { | 
 | 266 |         throw std::runtime_error("Invalid sensor type"); | 
 | 267 |     } | 
| Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 268 |  | 
 | 269 |     if (subType == IpmbSubType::util) | 
 | 270 |     { | 
 | 271 |         // Utilization need to be scaled to percent | 
 | 272 |         maxValue = 100; | 
 | 273 |         minValue = 0; | 
 | 274 |     } | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 275 | } | 
 | 276 |  | 
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 277 | void IpmbSensor::checkThresholds() | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 278 | { | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 279 |     thresholds::checkThresholds(this); | 
 | 280 | } | 
 | 281 |  | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 282 | bool IpmbSensor::processReading(const std::vector<uint8_t>& data, double& resp) | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 283 | { | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 284 |     switch (readingFormat) | 
 | 285 |     { | 
 | 286 |         case (ReadingFormat::byte0): | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 287 |         { | 
| Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 288 |             if (command == ipmi::sensor::getSensorReading && | 
 | 289 |                 !ipmi::sensor::isValid(data)) | 
| James Feist | cf4238e | 2020-07-28 16:40:03 -0700 | [diff] [blame] | 290 |             { | 
 | 291 |                 return false; | 
 | 292 |             } | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 293 |             resp = data[0]; | 
 | 294 |             return true; | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 295 |         } | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 296 |         case (ReadingFormat::byte3): | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 297 |         { | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 298 |             if (data.size() < 4) | 
 | 299 |             { | 
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 300 |                 if (errCount == 0U) | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 301 |                 { | 
 | 302 |                     std::cerr << "Invalid data length returned for " << name | 
 | 303 |                               << "\n"; | 
 | 304 |                 } | 
 | 305 |                 return false; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 306 |             } | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 307 |             resp = data[3]; | 
 | 308 |             return true; | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 309 |         } | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 310 |         case (ReadingFormat::elevenBit): | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 311 |         { | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 312 |             if (data.size() < 5) | 
 | 313 |             { | 
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 314 |                 if (errCount == 0U) | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 315 |                 { | 
 | 316 |                     std::cerr << "Invalid data length returned for " << name | 
 | 317 |                               << "\n"; | 
 | 318 |                 } | 
 | 319 |                 return false; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 320 |             } | 
 | 321 |  | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 322 |             int16_t value = ((data[4] << 8) | data[3]); | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 323 |             resp = value; | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 324 |             return true; | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 325 |         } | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 326 |         case (ReadingFormat::elevenBitShift): | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 327 |         { | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 328 |             if (data.size() < 5) | 
 | 329 |             { | 
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 330 |                 if (errCount == 0U) | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 331 |                 { | 
 | 332 |                     std::cerr << "Invalid data length returned for " << name | 
 | 333 |                               << "\n"; | 
 | 334 |                 } | 
 | 335 |                 return false; | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 336 |             } | 
 | 337 |  | 
| James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 338 |             resp = ((data[4] << 8) | data[3]) >> 3; | 
 | 339 |             return true; | 
| James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 340 |         } | 
| Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 341 |         case (ReadingFormat::linearElevenBit): | 
 | 342 |         { | 
 | 343 |             if (data.size() < 5) | 
 | 344 |             { | 
| Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 345 |                 if (errCount == 0U) | 
| Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 346 |                 { | 
 | 347 |                     std::cerr << "Invalid data length returned for " << name | 
 | 348 |                               << "\n"; | 
 | 349 |                 } | 
 | 350 |                 return false; | 
 | 351 |             } | 
 | 352 |  | 
 | 353 |             int16_t value = ((data[4] << 8) | data[3]); | 
 | 354 |             constexpr const size_t shift = 16 - 11; // 11bit into 16bit | 
 | 355 |             value <<= shift; | 
 | 356 |             value >>= shift; | 
 | 357 |             resp = value; | 
 | 358 |             return true; | 
 | 359 |         } | 
| James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 360 |         default: | 
 | 361 |             throw std::runtime_error("Invalid reading type"); | 
 | 362 |     } | 
 | 363 | } | 
 | 364 |  | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 365 | void IpmbSensor::ipmbRequestCompletionCb(const boost::system::error_code& ec, | 
 | 366 |                                          const IpmbMethodType& response) | 
 | 367 | { | 
 | 368 |     const int& status = std::get<0>(response); | 
 | 369 |     if (ec || (status != 0)) | 
 | 370 |     { | 
 | 371 |         incrementError(); | 
 | 372 |         read(); | 
 | 373 |         return; | 
 | 374 |     } | 
 | 375 |     const std::vector<uint8_t>& data = std::get<5>(response); | 
 | 376 |     if constexpr (debug) | 
 | 377 |     { | 
 | 378 |         std::cout << name << ": "; | 
 | 379 |         for (size_t d : data) | 
 | 380 |         { | 
 | 381 |             std::cout << d << " "; | 
 | 382 |         } | 
 | 383 |         std::cout << "\n"; | 
 | 384 |     } | 
 | 385 |     if (data.empty()) | 
 | 386 |     { | 
 | 387 |         incrementError(); | 
 | 388 |         read(); | 
 | 389 |         return; | 
 | 390 |     } | 
 | 391 |  | 
 | 392 |     double value = 0; | 
 | 393 |  | 
 | 394 |     if (!processReading(data, value)) | 
 | 395 |     { | 
 | 396 |         incrementError(); | 
 | 397 |         read(); | 
 | 398 |         return; | 
 | 399 |     } | 
 | 400 |  | 
 | 401 |     // rawValue only used in debug logging | 
 | 402 |     // up to 5th byte in data are used to derive value | 
 | 403 |     size_t end = std::min(sizeof(uint64_t), data.size()); | 
 | 404 |     uint64_t rawData = 0; | 
 | 405 |     for (size_t i = 0; i < end; i++) | 
 | 406 |     { | 
 | 407 |         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | 
 | 408 |         reinterpret_cast<uint8_t*>(&rawData)[i] = data[i]; | 
 | 409 |     } | 
 | 410 |     rawValue = static_cast<double>(rawData); | 
 | 411 |  | 
 | 412 |     /* Adjust value as per scale and offset */ | 
 | 413 |     value = (value * scaleVal) + offsetVal; | 
 | 414 |     updateValue(value); | 
 | 415 |     read(); | 
 | 416 | } | 
 | 417 |  | 
| Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 418 | void IpmbSensor::read() | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 419 | { | 
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 420 |     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 421 |     waitTimer.async_wait( | 
 | 422 |         [weakRef{weak_from_this()}](const boost::system::error_code& ec) { | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 423 |         if (ec == boost::asio::error::operation_aborted) | 
 | 424 |         { | 
 | 425 |             return; // we're being canceled | 
 | 426 |         } | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 427 |         std::shared_ptr<IpmbSensor> self = weakRef.lock(); | 
 | 428 |         if (!self) | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 429 |         { | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 430 |             return; | 
 | 431 |         } | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 432 |         self->sendIpmbRequest(); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 433 |     }); | 
 | 434 | } | 
| Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 435 |  | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 436 | void IpmbSensor::sendIpmbRequest() | 
 | 437 | { | 
 | 438 |     if (!readingStateGood()) | 
 | 439 |     { | 
 | 440 |         updateValue(std::numeric_limits<double>::quiet_NaN()); | 
 | 441 |         read(); | 
 | 442 |         return; | 
 | 443 |     } | 
 | 444 |     dbusConnection->async_method_call( | 
 | 445 |         [weakRef{weak_from_this()}](boost::system::error_code ec, | 
 | 446 |                                     const IpmbMethodType& response) { | 
 | 447 |         std::shared_ptr<IpmbSensor> self = weakRef.lock(); | 
 | 448 |         if (!self) | 
 | 449 |         { | 
 | 450 |             return; | 
 | 451 |         } | 
 | 452 |         self->ipmbRequestCompletionCb(ec, response); | 
| Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 453 |     }, | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 454 |         "xyz.openbmc_project.Ipmi.Channel.Ipmb", | 
 | 455 |         "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb", | 
 | 456 |         "sendRequest", commandAddress, netfn, lun, command, commandData); | 
 | 457 | } | 
 | 458 |  | 
| Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 459 | bool IpmbSensor::sensorClassType(const std::string& sensorClass) | 
 | 460 | { | 
 | 461 |     if (sensorClass == "PxeBridgeTemp") | 
 | 462 |     { | 
 | 463 |         type = IpmbType::PXE1410CVR; | 
 | 464 |     } | 
 | 465 |     else if (sensorClass == "IRBridgeTemp") | 
 | 466 |     { | 
 | 467 |         type = IpmbType::IR38363VR; | 
 | 468 |     } | 
 | 469 |     else if (sensorClass == "HSCBridge") | 
 | 470 |     { | 
 | 471 |         type = IpmbType::ADM1278HSC; | 
 | 472 |     } | 
 | 473 |     else if (sensorClass == "MpsBridgeTemp") | 
 | 474 |     { | 
 | 475 |         type = IpmbType::mpsVR; | 
 | 476 |     } | 
 | 477 |     else if (sensorClass == "METemp" || sensorClass == "MESensor") | 
 | 478 |     { | 
 | 479 |         type = IpmbType::meSensor; | 
 | 480 |     } | 
 | 481 |     else | 
 | 482 |     { | 
 | 483 |         std::cerr << "Invalid class " << sensorClass << "\n"; | 
 | 484 |         return false; | 
 | 485 |     } | 
 | 486 |     return true; | 
 | 487 | } | 
 | 488 |  | 
 | 489 | void IpmbSensor::sensorSubType(const std::string& sensorTypeName) | 
 | 490 | { | 
 | 491 |     if (sensorTypeName == "voltage") | 
 | 492 |     { | 
 | 493 |         subType = IpmbSubType::volt; | 
 | 494 |     } | 
 | 495 |     else if (sensorTypeName == "power") | 
 | 496 |     { | 
 | 497 |         subType = IpmbSubType::power; | 
 | 498 |     } | 
 | 499 |     else if (sensorTypeName == "current") | 
 | 500 |     { | 
 | 501 |         subType = IpmbSubType::curr; | 
 | 502 |     } | 
 | 503 |     else if (sensorTypeName == "utilization") | 
 | 504 |     { | 
 | 505 |         subType = IpmbSubType::util; | 
 | 506 |     } | 
 | 507 |     else | 
 | 508 |     { | 
 | 509 |         subType = IpmbSubType::temp; | 
 | 510 |     } | 
 | 511 | } | 
 | 512 |  | 
 | 513 | void IpmbSensor::parseConfigValues(const SensorBaseConfigMap& entry) | 
 | 514 | { | 
 | 515 |     auto findScaleVal = entry.find("ScaleValue"); | 
 | 516 |     if (findScaleVal != entry.end()) | 
 | 517 |     { | 
 | 518 |         scaleVal = std::visit(VariantToDoubleVisitor(), findScaleVal->second); | 
 | 519 |     } | 
 | 520 |  | 
 | 521 |     auto findOffsetVal = entry.find("OffsetValue"); | 
 | 522 |     if (findOffsetVal != entry.end()) | 
 | 523 |     { | 
 | 524 |         offsetVal = std::visit(VariantToDoubleVisitor(), findOffsetVal->second); | 
 | 525 |     } | 
 | 526 |  | 
| Zev Weiss | a4d2768 | 2022-07-19 15:30:36 -0700 | [diff] [blame] | 527 |     readState = getPowerState(entry); | 
| Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 528 | } | 
 | 529 |  | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 530 | void createSensors( | 
| Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 531 |     boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 532 |     boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>& | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 533 |         sensors, | 
 | 534 |     std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) | 
 | 535 | { | 
 | 536 |     if (!dbusConnection) | 
 | 537 |     { | 
 | 538 |         std::cerr << "Connection not created\n"; | 
 | 539 |         return; | 
 | 540 |     } | 
 | 541 |     dbusConnection->async_method_call( | 
 | 542 |         [&](boost::system::error_code ec, const ManagedObjectType& resp) { | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 543 |         if (ec) | 
 | 544 |         { | 
 | 545 |             std::cerr << "Error contacting entity manager\n"; | 
 | 546 |             return; | 
 | 547 |         } | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 548 |         for (const auto& [path, interfaces] : resp) | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 549 |         { | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 550 |             for (const auto& [intf, cfg] : interfaces) | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 551 |             { | 
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 552 |                 if (intf != configInterfaceName(sensorType)) | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 553 |                 { | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 554 |                     continue; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 555 |                 } | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 556 |                 std::string name = loadVariant<std::string>(cfg, "Name"); | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 557 |  | 
 | 558 |                 std::vector<thresholds::Threshold> sensorThresholds; | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 559 |                 if (!parseThresholdsFromConfig(interfaces, sensorThresholds)) | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 560 |                 { | 
 | 561 |                     std::cerr << "error populating thresholds for " << name | 
 | 562 |                               << "\n"; | 
 | 563 |                 } | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 564 |                 uint8_t deviceAddress = loadVariant<uint8_t>(cfg, "Address"); | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 565 |  | 
| Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 566 |                 std::string sensorClass = loadVariant<std::string>(cfg, | 
 | 567 |                                                                    "Class"); | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 568 |  | 
 | 569 |                 uint8_t hostSMbusIndex = hostSMbusIndexDefault; | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 570 |                 auto findSmType = cfg.find("HostSMbusIndex"); | 
 | 571 |                 if (findSmType != cfg.end()) | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 572 |                 { | 
 | 573 |                     hostSMbusIndex = std::visit(VariantToUnsignedIntVisitor(), | 
 | 574 |                                                 findSmType->second); | 
 | 575 |                 } | 
 | 576 |  | 
| Zev Weiss | 8569bf2 | 2022-10-11 15:37:44 -0700 | [diff] [blame] | 577 |                 float pollRate = getPollRate(cfg, pollRateDefault); | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 578 |  | 
| Jayashree Dhanapal | 6ee6294 | 2021-12-14 15:22:23 +0530 | [diff] [blame] | 579 |                 uint8_t ipmbBusIndex = ipmbBusIndexDefault; | 
 | 580 |                 auto findBusType = cfg.find("Bus"); | 
 | 581 |                 if (findBusType != cfg.end()) | 
 | 582 |                 { | 
 | 583 |                     ipmbBusIndex = std::visit(VariantToUnsignedIntVisitor(), | 
 | 584 |                                               findBusType->second); | 
 | 585 |                     std::cerr << "Ipmb Bus Index for " << name << " is " | 
 | 586 |                               << static_cast<int>(ipmbBusIndex) << "\n"; | 
 | 587 |                 } | 
 | 588 |  | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 589 |                 /* Default sensor type is "temperature" */ | 
 | 590 |                 std::string sensorTypeName = "temperature"; | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 591 |                 auto findType = cfg.find("SensorType"); | 
 | 592 |                 if (findType != cfg.end()) | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 593 |                 { | 
| Patrick Williams | 779c96a | 2023-05-10 07:50:42 -0500 | [diff] [blame] | 594 |                     sensorTypeName = std::visit(VariantToStringVisitor(), | 
 | 595 |                                                 findType->second); | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 596 |                 } | 
 | 597 |  | 
 | 598 |                 auto& sensor = sensors[name]; | 
| Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 599 |                 sensor = nullptr; | 
 | 600 |                 sensor = std::make_shared<IpmbSensor>( | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 601 |                     dbusConnection, io, name, path, objectServer, | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 602 |                     std::move(sensorThresholds), deviceAddress, hostSMbusIndex, | 
 | 603 |                     pollRate, sensorTypeName); | 
 | 604 |  | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 605 |                 sensor->parseConfigValues(cfg); | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 606 |                 if (!(sensor->sensorClassType(sensorClass))) | 
 | 607 |                 { | 
 | 608 |                     continue; | 
 | 609 |                 } | 
 | 610 |                 sensor->sensorSubType(sensorTypeName); | 
 | 611 |                 sensor->init(); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 612 |             } | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 613 |         } | 
| Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 614 |     }, | 
| JeffLin | 2c5a1f2 | 2022-10-05 15:19:09 +0800 | [diff] [blame] | 615 |         entityManagerName, "/xyz/openbmc_project/inventory", | 
 | 616 |         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 617 | } | 
 | 618 |  | 
| Jayashree Dhanapal | 3746c55 | 2022-03-21 14:45:52 +0530 | [diff] [blame] | 619 | void sdrHandler(sdbusplus::message_t& message, | 
 | 620 |                 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) | 
 | 621 | { | 
 | 622 |     std::string objectName; | 
 | 623 |     SensorBaseConfigMap values; | 
 | 624 |     message.read(objectName, values); | 
 | 625 |  | 
 | 626 |     auto findBus = values.find("Bus"); | 
 | 627 |     if (findBus == values.end()) | 
 | 628 |     { | 
 | 629 |         return; | 
 | 630 |     } | 
 | 631 |  | 
 | 632 |     uint8_t busIndex = loadVariant<uint8_t>(values, "Bus"); | 
 | 633 |  | 
 | 634 |     auto& sdrsen = sdrsensor[busIndex]; | 
 | 635 |     sdrsen = nullptr; | 
 | 636 |     sdrsen = std::make_shared<IpmbSDRDevice>(dbusConnection, busIndex); | 
 | 637 |     sdrsen->getSDRRepositoryInfo(); | 
 | 638 | } | 
 | 639 |  | 
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 640 | void reinitSensors(sdbusplus::message_t& message) | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 641 | { | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 642 |     constexpr const size_t reinitWaitSeconds = 2; | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 643 |     std::string objectName; | 
| James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 644 |     boost::container::flat_map<std::string, std::variant<std::string>> values; | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 645 |     message.read(objectName, values); | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 646 |  | 
| James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 647 |     auto findStatus = values.find(power::property); | 
 | 648 |     if (findStatus != values.end()) | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 649 |     { | 
| Zev Weiss | 6c106d6 | 2022-08-17 20:50:00 -0700 | [diff] [blame] | 650 |         bool powerStatus = | 
 | 651 |             std::get<std::string>(findStatus->second).ends_with(".Running"); | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 652 |         if (powerStatus) | 
 | 653 |         { | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 654 |             if (!initCmdTimer) | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 655 |             { | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 656 |                 // this should be impossible | 
 | 657 |                 return; | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 658 |             } | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 659 |             // we seem to send this command too fast sometimes, wait before | 
 | 660 |             // sending | 
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 661 |             initCmdTimer->expires_after( | 
| Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 662 |                 std::chrono::seconds(reinitWaitSeconds)); | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 663 |  | 
 | 664 |             initCmdTimer->async_wait([](const boost::system::error_code ec) { | 
 | 665 |                 if (ec == boost::asio::error::operation_aborted) | 
 | 666 |                 { | 
 | 667 |                     return; // we're being canceled | 
 | 668 |                 } | 
 | 669 |  | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 670 |                 for (const auto& [name, sensor] : sensors) | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 671 |                 { | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 672 |                     if (sensor) | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 673 |                     { | 
| Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 674 |                         sensor->runInitCmd(); | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 675 |                     } | 
 | 676 |                 } | 
 | 677 |             }); | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 678 |         } | 
 | 679 |     } | 
 | 680 | } | 
 | 681 |  | 
| Kumar Thangavel | 5d032bc | 2022-11-30 20:24:47 +0530 | [diff] [blame] | 682 | void interfaceRemoved( | 
 | 683 |     sdbusplus::message_t& message, | 
 | 684 |     boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>& | 
 | 685 |         sensors) | 
 | 686 | { | 
 | 687 |     if (message.is_method_error()) | 
 | 688 |     { | 
 | 689 |         std::cerr << "interfacesRemoved callback method error\n"; | 
 | 690 |         return; | 
 | 691 |     } | 
 | 692 |  | 
 | 693 |     sdbusplus::message::object_path removedPath; | 
 | 694 |     std::vector<std::string> interfaces; | 
 | 695 |  | 
 | 696 |     message.read(removedPath, interfaces); | 
 | 697 |  | 
 | 698 |     // If the xyz.openbmc_project.Confguration.X interface was removed | 
 | 699 |     // for one or more sensors, delete those sensor objects. | 
 | 700 |     auto sensorIt = sensors.begin(); | 
 | 701 |     while (sensorIt != sensors.end()) | 
 | 702 |     { | 
 | 703 |         if ((sensorIt->second->configurationPath == removedPath) && | 
 | 704 |             (std::find(interfaces.begin(), interfaces.end(), | 
 | 705 |                        configInterfaceName(sdrInterface)) != interfaces.end())) | 
 | 706 |         { | 
 | 707 |             sensorIt = sensors.erase(sensorIt); | 
 | 708 |         } | 
 | 709 |         else | 
 | 710 |         { | 
 | 711 |             sensorIt++; | 
 | 712 |         } | 
 | 713 |     } | 
 | 714 | } | 
 | 715 |  | 
| James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 716 | int main() | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 717 | { | 
| Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 718 |     boost::asio::io_context io; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 719 |     auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); | 
| Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 720 |     sdbusplus::asio::object_server objectServer(systemBus, true); | 
 | 721 |     objectServer.add_manager("/xyz/openbmc_project/sensors"); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 722 |     systemBus->request_name("xyz.openbmc_project.IpmbSensor"); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 723 |  | 
| Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 724 |     initCmdTimer = std::make_unique<boost::asio::steady_timer>(io); | 
| James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 725 |  | 
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 726 |     boost::asio::post( | 
 | 727 |         io, [&]() { createSensors(io, objectServer, sensors, systemBus); }); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 728 |  | 
| Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 729 |     boost::asio::steady_timer configTimer(io); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 730 |  | 
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 731 |     std::function<void(sdbusplus::message_t&)> eventHandler = | 
 | 732 |         [&](sdbusplus::message_t&) { | 
| Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 733 |         configTimer.expires_after(std::chrono::seconds(1)); | 
| Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 734 |         // create a timer because normally multiple properties change | 
 | 735 |         configTimer.async_wait([&](const boost::system::error_code& ec) { | 
 | 736 |             if (ec == boost::asio::error::operation_aborted) | 
 | 737 |             { | 
 | 738 |                 return; // we're being canceled | 
 | 739 |             } | 
 | 740 |             createSensors(io, objectServer, sensors, systemBus); | 
 | 741 |             if (sensors.empty()) | 
 | 742 |             { | 
 | 743 |                 std::cout << "Configuration not detected\n"; | 
 | 744 |             } | 
 | 745 |         }); | 
 | 746 |     }; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 747 |  | 
| Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 748 |     std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = | 
 | 749 |         setupPropertiesChangedMatches( | 
| Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 750 |             *systemBus, std::to_array<const char*>({sensorType}), eventHandler); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 751 |  | 
| Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 752 |     sdbusplus::bus::match_t powerChangeMatch( | 
 | 753 |         static_cast<sdbusplus::bus_t&>(*systemBus), | 
| James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 754 |         "type='signal',interface='" + std::string(properties::interface) + | 
 | 755 |             "',path='" + std::string(power::path) + "',arg0='" + | 
 | 756 |             std::string(power::interface) + "'", | 
| James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 757 |         reinitSensors); | 
 | 758 |  | 
| Jayashree Dhanapal | 3746c55 | 2022-03-21 14:45:52 +0530 | [diff] [blame] | 759 |     auto matchSignal = std::make_shared<sdbusplus::bus::match_t>( | 
 | 760 |         static_cast<sdbusplus::bus_t&>(*systemBus), | 
 | 761 |         "type='signal',member='PropertiesChanged',path_namespace='" + | 
 | 762 |             std::string(inventoryPath) + "',arg0namespace='" + | 
 | 763 |             configInterfaceName(sdrInterface) + "'", | 
 | 764 |         [&systemBus](sdbusplus::message_t& msg) { | 
 | 765 |         sdrHandler(msg, systemBus); | 
| Patrick Williams | 597e842 | 2023-10-20 11:19:01 -0500 | [diff] [blame] | 766 |     }); | 
| Jayashree Dhanapal | 3746c55 | 2022-03-21 14:45:52 +0530 | [diff] [blame] | 767 |  | 
| Kumar Thangavel | 5d032bc | 2022-11-30 20:24:47 +0530 | [diff] [blame] | 768 |     // Watch for entity-manager to remove configuration interfaces | 
 | 769 |     // so the corresponding sensors can be removed. | 
 | 770 |     auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>( | 
 | 771 |         static_cast<sdbusplus::bus_t&>(*systemBus), | 
 | 772 |         "type='signal',member='InterfacesRemoved',arg0path='" + | 
 | 773 |             std::string(inventoryPath) + "/'", | 
| Willy Tu | 63c9122 | 2023-07-21 10:17:16 -0700 | [diff] [blame] | 774 |         [](sdbusplus::message_t& msg) { interfaceRemoved(msg, sensors); }); | 
| Kumar Thangavel | 5d032bc | 2022-11-30 20:24:47 +0530 | [diff] [blame] | 775 |  | 
| Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 776 |     setupManufacturingModeMatch(*systemBus); | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 777 |     io.run(); | 
| Zhikui Ren | e76a5a6 | 2021-07-09 15:16:32 -0700 | [diff] [blame] | 778 |     return 0; | 
| James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 779 | } |