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); |
| 158 | }, |
| 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 | { |
| 270 | |
| 271 | switch (readingFormat) |
| 272 | { |
| 273 | case (ReadingFormat::byte0): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 274 | { |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 275 | if (command == ipmi::sensor::getSensorReading && |
| 276 | !ipmi::sensor::isValid(data)) |
James Feist | cf4238e | 2020-07-28 16:40:03 -0700 | [diff] [blame] | 277 | { |
| 278 | return false; |
| 279 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 280 | resp = data[0]; |
| 281 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 282 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 283 | case (ReadingFormat::byte3): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 284 | { |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 285 | if (data.size() < 4) |
| 286 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 287 | if (errCount == 0U) |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 288 | { |
| 289 | std::cerr << "Invalid data length returned for " << name |
| 290 | << "\n"; |
| 291 | } |
| 292 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 293 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 294 | resp = data[3]; |
| 295 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 296 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 297 | case (ReadingFormat::elevenBit): |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 298 | { |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 299 | if (data.size() < 5) |
| 300 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 301 | if (errCount == 0U) |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 302 | { |
| 303 | std::cerr << "Invalid data length returned for " << name |
| 304 | << "\n"; |
| 305 | } |
| 306 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 307 | } |
| 308 | |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 309 | int16_t value = ((data[4] << 8) | data[3]); |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 310 | resp = value; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 311 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 312 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 313 | case (ReadingFormat::elevenBitShift): |
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 | if (data.size() < 5) |
| 316 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 317 | if (errCount == 0U) |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 318 | { |
| 319 | std::cerr << "Invalid data length returned for " << name |
| 320 | << "\n"; |
| 321 | } |
| 322 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 323 | } |
| 324 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 325 | resp = ((data[4] << 8) | data[3]) >> 3; |
| 326 | return true; |
James Feist | e4a970d | 2020-08-19 11:21:58 -0700 | [diff] [blame] | 327 | } |
Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 328 | case (ReadingFormat::linearElevenBit): |
| 329 | { |
| 330 | if (data.size() < 5) |
| 331 | { |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 332 | if (errCount == 0U) |
Jayashree-D | 3732257 | 2021-03-19 17:40:56 +0530 | [diff] [blame] | 333 | { |
| 334 | std::cerr << "Invalid data length returned for " << name |
| 335 | << "\n"; |
| 336 | } |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | int16_t value = ((data[4] << 8) | data[3]); |
| 341 | constexpr const size_t shift = 16 - 11; // 11bit into 16bit |
| 342 | value <<= shift; |
| 343 | value >>= shift; |
| 344 | resp = value; |
| 345 | return true; |
| 346 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 347 | default: |
| 348 | throw std::runtime_error("Invalid reading type"); |
| 349 | } |
| 350 | } |
| 351 | |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 352 | void IpmbSensor::ipmbRequestCompletionCb(const boost::system::error_code& ec, |
| 353 | const IpmbMethodType& response) |
| 354 | { |
| 355 | const int& status = std::get<0>(response); |
| 356 | if (ec || (status != 0)) |
| 357 | { |
| 358 | incrementError(); |
| 359 | read(); |
| 360 | return; |
| 361 | } |
| 362 | const std::vector<uint8_t>& data = std::get<5>(response); |
| 363 | if constexpr (debug) |
| 364 | { |
| 365 | std::cout << name << ": "; |
| 366 | for (size_t d : data) |
| 367 | { |
| 368 | std::cout << d << " "; |
| 369 | } |
| 370 | std::cout << "\n"; |
| 371 | } |
| 372 | if (data.empty()) |
| 373 | { |
| 374 | incrementError(); |
| 375 | read(); |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | double value = 0; |
| 380 | |
| 381 | if (!processReading(data, value)) |
| 382 | { |
| 383 | incrementError(); |
| 384 | read(); |
| 385 | return; |
| 386 | } |
| 387 | |
| 388 | // rawValue only used in debug logging |
| 389 | // up to 5th byte in data are used to derive value |
| 390 | size_t end = std::min(sizeof(uint64_t), data.size()); |
| 391 | uint64_t rawData = 0; |
| 392 | for (size_t i = 0; i < end; i++) |
| 393 | { |
| 394 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 395 | reinterpret_cast<uint8_t*>(&rawData)[i] = data[i]; |
| 396 | } |
| 397 | rawValue = static_cast<double>(rawData); |
| 398 | |
| 399 | /* Adjust value as per scale and offset */ |
| 400 | value = (value * scaleVal) + offsetVal; |
| 401 | updateValue(value); |
| 402 | read(); |
| 403 | } |
| 404 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 405 | void IpmbSensor::read(void) |
| 406 | { |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 407 | waitTimer.expires_from_now(std::chrono::milliseconds(sensorPollMs)); |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 408 | waitTimer.async_wait( |
| 409 | [weakRef{weak_from_this()}](const boost::system::error_code& ec) { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 410 | if (ec == boost::asio::error::operation_aborted) |
| 411 | { |
| 412 | return; // we're being canceled |
| 413 | } |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 414 | std::shared_ptr<IpmbSensor> self = weakRef.lock(); |
| 415 | if (!self) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 416 | { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 417 | return; |
| 418 | } |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 419 | self->sendIpmbRequest(); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 420 | }); |
| 421 | } |
Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 422 | |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 423 | void IpmbSensor::sendIpmbRequest() |
| 424 | { |
| 425 | if (!readingStateGood()) |
| 426 | { |
| 427 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 428 | read(); |
| 429 | return; |
| 430 | } |
| 431 | dbusConnection->async_method_call( |
| 432 | [weakRef{weak_from_this()}](boost::system::error_code ec, |
| 433 | const IpmbMethodType& response) { |
| 434 | std::shared_ptr<IpmbSensor> self = weakRef.lock(); |
| 435 | if (!self) |
| 436 | { |
| 437 | return; |
| 438 | } |
| 439 | self->ipmbRequestCompletionCb(ec, response); |
| 440 | }, |
| 441 | "xyz.openbmc_project.Ipmi.Channel.Ipmb", |
| 442 | "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb", |
| 443 | "sendRequest", commandAddress, netfn, lun, command, commandData); |
| 444 | } |
| 445 | |
Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 446 | bool IpmbSensor::sensorClassType(const std::string& sensorClass) |
| 447 | { |
| 448 | if (sensorClass == "PxeBridgeTemp") |
| 449 | { |
| 450 | type = IpmbType::PXE1410CVR; |
| 451 | } |
| 452 | else if (sensorClass == "IRBridgeTemp") |
| 453 | { |
| 454 | type = IpmbType::IR38363VR; |
| 455 | } |
| 456 | else if (sensorClass == "HSCBridge") |
| 457 | { |
| 458 | type = IpmbType::ADM1278HSC; |
| 459 | } |
| 460 | else if (sensorClass == "MpsBridgeTemp") |
| 461 | { |
| 462 | type = IpmbType::mpsVR; |
| 463 | } |
| 464 | else if (sensorClass == "METemp" || sensorClass == "MESensor") |
| 465 | { |
| 466 | type = IpmbType::meSensor; |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | std::cerr << "Invalid class " << sensorClass << "\n"; |
| 471 | return false; |
| 472 | } |
| 473 | return true; |
| 474 | } |
| 475 | |
| 476 | void IpmbSensor::sensorSubType(const std::string& sensorTypeName) |
| 477 | { |
| 478 | if (sensorTypeName == "voltage") |
| 479 | { |
| 480 | subType = IpmbSubType::volt; |
| 481 | } |
| 482 | else if (sensorTypeName == "power") |
| 483 | { |
| 484 | subType = IpmbSubType::power; |
| 485 | } |
| 486 | else if (sensorTypeName == "current") |
| 487 | { |
| 488 | subType = IpmbSubType::curr; |
| 489 | } |
| 490 | else if (sensorTypeName == "utilization") |
| 491 | { |
| 492 | subType = IpmbSubType::util; |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | subType = IpmbSubType::temp; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void IpmbSensor::parseConfigValues(const SensorBaseConfigMap& entry) |
| 501 | { |
| 502 | auto findScaleVal = entry.find("ScaleValue"); |
| 503 | if (findScaleVal != entry.end()) |
| 504 | { |
| 505 | scaleVal = std::visit(VariantToDoubleVisitor(), findScaleVal->second); |
| 506 | } |
| 507 | |
| 508 | auto findOffsetVal = entry.find("OffsetValue"); |
| 509 | if (findOffsetVal != entry.end()) |
| 510 | { |
| 511 | offsetVal = std::visit(VariantToDoubleVisitor(), findOffsetVal->second); |
| 512 | } |
| 513 | |
Zev Weiss | a4d2768 | 2022-07-19 15:30:36 -0700 | [diff] [blame] | 514 | readState = getPowerState(entry); |
Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 515 | } |
| 516 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 517 | void createSensors( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame^] | 518 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 519 | boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>& |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 520 | sensors, |
| 521 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 522 | { |
| 523 | if (!dbusConnection) |
| 524 | { |
| 525 | std::cerr << "Connection not created\n"; |
| 526 | return; |
| 527 | } |
| 528 | dbusConnection->async_method_call( |
| 529 | [&](boost::system::error_code ec, const ManagedObjectType& resp) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 530 | if (ec) |
| 531 | { |
| 532 | std::cerr << "Error contacting entity manager\n"; |
| 533 | return; |
| 534 | } |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 535 | for (const auto& [path, interfaces] : resp) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 536 | { |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 537 | for (const auto& [intf, cfg] : interfaces) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 538 | { |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 539 | if (intf != configInterfaceName(sensorType)) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 540 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 541 | continue; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 542 | } |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 543 | std::string name = loadVariant<std::string>(cfg, "Name"); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 544 | |
| 545 | std::vector<thresholds::Threshold> sensorThresholds; |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 546 | if (!parseThresholdsFromConfig(interfaces, sensorThresholds)) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 547 | { |
| 548 | std::cerr << "error populating thresholds for " << name |
| 549 | << "\n"; |
| 550 | } |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 551 | uint8_t deviceAddress = loadVariant<uint8_t>(cfg, "Address"); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 552 | |
| 553 | std::string sensorClass = |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 554 | loadVariant<std::string>(cfg, "Class"); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 555 | |
| 556 | uint8_t hostSMbusIndex = hostSMbusIndexDefault; |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 557 | auto findSmType = cfg.find("HostSMbusIndex"); |
| 558 | if (findSmType != cfg.end()) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 559 | { |
| 560 | hostSMbusIndex = std::visit(VariantToUnsignedIntVisitor(), |
| 561 | findSmType->second); |
| 562 | } |
| 563 | |
Zev Weiss | 8569bf2 | 2022-10-11 15:37:44 -0700 | [diff] [blame] | 564 | float pollRate = getPollRate(cfg, pollRateDefault); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 565 | |
Jayashree Dhanapal | 6ee6294 | 2021-12-14 15:22:23 +0530 | [diff] [blame] | 566 | uint8_t ipmbBusIndex = ipmbBusIndexDefault; |
| 567 | auto findBusType = cfg.find("Bus"); |
| 568 | if (findBusType != cfg.end()) |
| 569 | { |
| 570 | ipmbBusIndex = std::visit(VariantToUnsignedIntVisitor(), |
| 571 | findBusType->second); |
| 572 | std::cerr << "Ipmb Bus Index for " << name << " is " |
| 573 | << static_cast<int>(ipmbBusIndex) << "\n"; |
| 574 | } |
| 575 | |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 576 | /* Default sensor type is "temperature" */ |
| 577 | std::string sensorTypeName = "temperature"; |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 578 | auto findType = cfg.find("SensorType"); |
| 579 | if (findType != cfg.end()) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 580 | { |
| 581 | sensorTypeName = |
| 582 | std::visit(VariantToStringVisitor(), findType->second); |
| 583 | } |
| 584 | |
| 585 | auto& sensor = sensors[name]; |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 586 | sensor = nullptr; |
| 587 | sensor = std::make_shared<IpmbSensor>( |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 588 | dbusConnection, io, name, path, objectServer, |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 589 | std::move(sensorThresholds), deviceAddress, hostSMbusIndex, |
| 590 | pollRate, sensorTypeName); |
| 591 | |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 592 | sensor->parseConfigValues(cfg); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 593 | if (!(sensor->sensorClassType(sensorClass))) |
| 594 | { |
| 595 | continue; |
| 596 | } |
| 597 | sensor->sensorSubType(sensorTypeName); |
| 598 | sensor->init(); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 599 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 600 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 601 | }, |
JeffLin | 2c5a1f2 | 2022-10-05 15:19:09 +0800 | [diff] [blame] | 602 | entityManagerName, "/xyz/openbmc_project/inventory", |
| 603 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Jayashree Dhanapal | 3746c55 | 2022-03-21 14:45:52 +0530 | [diff] [blame] | 606 | void sdrHandler(sdbusplus::message_t& message, |
| 607 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 608 | { |
| 609 | std::string objectName; |
| 610 | SensorBaseConfigMap values; |
| 611 | message.read(objectName, values); |
| 612 | |
| 613 | auto findBus = values.find("Bus"); |
| 614 | if (findBus == values.end()) |
| 615 | { |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | uint8_t busIndex = loadVariant<uint8_t>(values, "Bus"); |
| 620 | |
| 621 | auto& sdrsen = sdrsensor[busIndex]; |
| 622 | sdrsen = nullptr; |
| 623 | sdrsen = std::make_shared<IpmbSDRDevice>(dbusConnection, busIndex); |
| 624 | sdrsen->getSDRRepositoryInfo(); |
| 625 | } |
| 626 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 627 | void reinitSensors(sdbusplus::message_t& message) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 628 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 629 | constexpr const size_t reinitWaitSeconds = 2; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 630 | std::string objectName; |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 631 | boost::container::flat_map<std::string, std::variant<std::string>> values; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 632 | message.read(objectName, values); |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 633 | |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 634 | auto findStatus = values.find(power::property); |
| 635 | if (findStatus != values.end()) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 636 | { |
Zev Weiss | 6c106d6 | 2022-08-17 20:50:00 -0700 | [diff] [blame] | 637 | bool powerStatus = |
| 638 | std::get<std::string>(findStatus->second).ends_with(".Running"); |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 639 | if (powerStatus) |
| 640 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 641 | if (!initCmdTimer) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 642 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 643 | // this should be impossible |
| 644 | return; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 645 | } |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 646 | // we seem to send this command too fast sometimes, wait before |
| 647 | // sending |
| 648 | initCmdTimer->expires_from_now( |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 649 | std::chrono::seconds(reinitWaitSeconds)); |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 650 | |
| 651 | initCmdTimer->async_wait([](const boost::system::error_code ec) { |
| 652 | if (ec == boost::asio::error::operation_aborted) |
| 653 | { |
| 654 | return; // we're being canceled |
| 655 | } |
| 656 | |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 657 | for (const auto& [name, sensor] : sensors) |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 658 | { |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 659 | if (sensor) |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 660 | { |
Zev Weiss | 8ba551b | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 661 | sensor->runInitCmd(); |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | }); |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 669 | int main() |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 670 | { |
| 671 | |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame^] | 672 | boost::asio::io_context io; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 673 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 674 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 675 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 676 | systemBus->request_name("xyz.openbmc_project.IpmbSensor"); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 677 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 678 | initCmdTimer = std::make_unique<boost::asio::steady_timer>(io); |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 679 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 680 | io.post([&]() { createSensors(io, objectServer, sensors, systemBus); }); |
| 681 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 682 | boost::asio::steady_timer configTimer(io); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 683 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 684 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 685 | [&](sdbusplus::message_t&) { |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 686 | configTimer.expires_from_now(std::chrono::seconds(1)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 687 | // create a timer because normally multiple properties change |
| 688 | configTimer.async_wait([&](const boost::system::error_code& ec) { |
| 689 | if (ec == boost::asio::error::operation_aborted) |
| 690 | { |
| 691 | return; // we're being canceled |
| 692 | } |
| 693 | createSensors(io, objectServer, sensors, systemBus); |
| 694 | if (sensors.empty()) |
| 695 | { |
| 696 | std::cout << "Configuration not detected\n"; |
| 697 | } |
| 698 | }); |
| 699 | }; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 700 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 701 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 702 | setupPropertiesChangedMatches( |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 703 | *systemBus, std::to_array<const char*>({sensorType}), eventHandler); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 704 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 705 | sdbusplus::bus::match_t powerChangeMatch( |
| 706 | static_cast<sdbusplus::bus_t&>(*systemBus), |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 707 | "type='signal',interface='" + std::string(properties::interface) + |
| 708 | "',path='" + std::string(power::path) + "',arg0='" + |
| 709 | std::string(power::interface) + "'", |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 710 | reinitSensors); |
| 711 | |
Jayashree Dhanapal | 3746c55 | 2022-03-21 14:45:52 +0530 | [diff] [blame] | 712 | auto matchSignal = std::make_shared<sdbusplus::bus::match_t>( |
| 713 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| 714 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 715 | std::string(inventoryPath) + "',arg0namespace='" + |
| 716 | configInterfaceName(sdrInterface) + "'", |
| 717 | [&systemBus](sdbusplus::message_t& msg) { |
| 718 | sdrHandler(msg, systemBus); |
| 719 | }); |
| 720 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 721 | setupManufacturingModeMatch(*systemBus); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 722 | io.run(); |
Zhikui Ren | e76a5a6 | 2021-07-09 15:16:32 -0700 | [diff] [blame] | 723 | return 0; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 724 | } |