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