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