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 | |
| 17 | #include "IpmbSensor.hpp" |
| 18 | |
| 19 | #include "Utils.hpp" |
| 20 | #include "VariantVisitors.hpp" |
| 21 | |
| 22 | #include <math.h> |
| 23 | |
| 24 | #include <boost/algorithm/string.hpp> |
| 25 | #include <boost/algorithm/string/predicate.hpp> |
| 26 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 27 | #include <boost/container/flat_map.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 28 | #include <sdbusplus/asio/connection.hpp> |
| 29 | #include <sdbusplus/asio/object_server.hpp> |
| 30 | #include <sdbusplus/bus/match.hpp> |
| 31 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 32 | #include <chrono> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 33 | #include <functional> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 34 | #include <iostream> |
| 35 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 36 | #include <memory> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 37 | #include <numeric> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 38 | #include <string> |
| 39 | #include <tuple> |
| 40 | #include <variant> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 41 | #include <vector> |
| 42 | |
| 43 | constexpr const bool debug = false; |
| 44 | |
| 45 | constexpr const char* configInterface = |
| 46 | "xyz.openbmc_project.Configuration.IpmbSensor"; |
| 47 | static constexpr double ipmbMaxReading = 0xFF; |
| 48 | static constexpr double ipmbMinReading = 0; |
| 49 | |
| 50 | static constexpr uint8_t meAddress = 1; |
| 51 | static constexpr uint8_t lun = 0; |
| 52 | |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 53 | static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/"; |
| 54 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 55 | using IpmbMethodType = |
| 56 | std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>; |
| 57 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 58 | boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>> sensors; |
| 59 | |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 60 | std::unique_ptr<boost::asio::deadline_timer> initCmdTimer; |
| 61 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 62 | IpmbSensor::IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 63 | boost::asio::io_service& io, |
| 64 | const std::string& sensorName, |
| 65 | const std::string& sensorConfiguration, |
| 66 | sdbusplus::asio::object_server& objectServer, |
| 67 | std::vector<thresholds::Threshold>&& thresholdData, |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 68 | uint8_t deviceAddress, std::string& sensorTypeName) : |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 69 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 70 | std::move(thresholdData), sensorConfiguration, |
| 71 | "xyz.openbmc_project.Configuration.ExitAirTemp", ipmbMaxReading, |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 72 | ipmbMinReading, PowerState::on), |
| 73 | deviceAddress(deviceAddress), objectServer(objectServer), |
| 74 | dbusConnection(conn), 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 | |
| 81 | if (thresholds::hasWarningInterface(thresholds)) |
| 82 | { |
| 83 | thresholdInterfaceWarning = objectServer.add_interface( |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 84 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning"); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 85 | } |
| 86 | if (thresholds::hasCriticalInterface(thresholds)) |
| 87 | { |
| 88 | thresholdInterfaceCritical = objectServer.add_interface( |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 89 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical"); |
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(); |
| 97 | objectServer.remove_interface(thresholdInterfaceWarning); |
| 98 | objectServer.remove_interface(thresholdInterfaceCritical); |
| 99 | objectServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 100 | objectServer.remove_interface(association); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void IpmbSensor::init(void) |
| 104 | { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 105 | loadDefaults(); |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 106 | setInitialProperties(dbusConnection); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 107 | if (initCommand) |
| 108 | { |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 109 | runInitCmd(); |
| 110 | } |
| 111 | read(); |
| 112 | } |
| 113 | |
| 114 | void IpmbSensor::runInitCmd() |
| 115 | { |
| 116 | if (initCommand) |
| 117 | { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 118 | dbusConnection->async_method_call( |
| 119 | [this](boost::system::error_code ec, |
| 120 | const IpmbMethodType& response) { |
| 121 | const int& status = std::get<0>(response); |
| 122 | |
| 123 | if (ec || status) |
| 124 | { |
| 125 | std::cerr |
| 126 | << "Error setting init command for device: " << name |
| 127 | << "\n"; |
| 128 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 129 | }, |
| 130 | "xyz.openbmc_project.Ipmi.Channel.Ipmb", |
| 131 | "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb", |
| 132 | "sendRequest", commandAddress, netfn, lun, *initCommand, initData); |
| 133 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void IpmbSensor::loadDefaults() |
| 137 | { |
| 138 | if (type == IpmbType::meSensor) |
| 139 | { |
| 140 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame^] | 141 | netfn = ipmi::sensor::netFn; |
| 142 | command = ipmi::sensor::getSensorReading; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 143 | commandData = {deviceAddress}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 144 | readingFormat = ReadingFormat::byte0; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 145 | } |
| 146 | else if (type == IpmbType::PXE1410CVR) |
| 147 | { |
| 148 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame^] | 149 | netfn = ipmi::me_bridge::netFn; |
| 150 | command = ipmi::me_bridge::sendRawPmbus; |
| 151 | initCommand = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 152 | // pmbus read temp |
| 153 | commandData = {0x57, 0x01, 0x00, 0x16, 0x3, deviceAddress, 0x00, |
| 154 | 0x00, 0x00, 0x00, 0x01, 0x02, 0x8d}; |
| 155 | // goto page 0 |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 156 | initData = {0x57, 0x01, 0x00, 0x14, 0x03, deviceAddress, 0x00, |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 157 | 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00}; |
| 158 | readingFormat = ReadingFormat::byte3; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 159 | } |
| 160 | else if (type == IpmbType::IR38363VR) |
| 161 | { |
| 162 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame^] | 163 | netfn = ipmi::me_bridge::netFn; |
| 164 | command = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 165 | // pmbus read temp |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 166 | commandData = {0x57, 0x01, 0x00, 0x16, 0x03, deviceAddress, 00, |
| 167 | 0x00, 0x00, 0x00, 0x01, 0x02, 0x8D}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 168 | readingFormat = ReadingFormat::elevenBitShift; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 169 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 170 | else if (type == IpmbType::ADM1278HSC) |
| 171 | { |
| 172 | commandAddress = meAddress; |
| 173 | switch (subType) |
| 174 | { |
| 175 | case IpmbSubType::temp: |
| 176 | case IpmbSubType::curr: |
| 177 | uint8_t snsNum; |
| 178 | if (subType == IpmbSubType::temp) |
| 179 | snsNum = 0x8d; |
| 180 | else |
| 181 | snsNum = 0x8c; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame^] | 182 | netfn = ipmi::me_bridge::netFn; |
| 183 | command = ipmi::me_bridge::sendRawPmbus; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 184 | commandData = {0x57, 0x01, 0x00, 0x86, deviceAddress, |
| 185 | 0x00, 0x00, 0x01, 0x02, snsNum}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 186 | readingFormat = ReadingFormat::elevenBit; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 187 | break; |
| 188 | case IpmbSubType::power: |
| 189 | case IpmbSubType::volt: |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame^] | 190 | netfn = ipmi::sensor::netFn; |
| 191 | command = ipmi::sensor::getSensorReading; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 192 | commandData = {deviceAddress}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 193 | readingFormat = ReadingFormat::byte0; |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 194 | break; |
| 195 | default: |
| 196 | throw std::runtime_error("Invalid sensor type"); |
| 197 | } |
| 198 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 199 | else if (type == IpmbType::mpsVR) |
| 200 | { |
| 201 | commandAddress = meAddress; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame^] | 202 | netfn = ipmi::me_bridge::netFn; |
| 203 | command = ipmi::me_bridge::sendRawPmbus; |
| 204 | initCommand = ipmi::me_bridge::sendRawPmbus; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 205 | // pmbus read temp |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 206 | commandData = {0x57, 0x01, 0x00, 0x16, 0x3, deviceAddress, 0x00, |
| 207 | 0x00, 0x00, 0x00, 0x01, 0x02, 0x8d}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 208 | // goto page 0 |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 209 | initData = {0x57, 0x01, 0x00, 0x14, 0x03, deviceAddress, 0x00, |
| 210 | 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00}; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 211 | readingFormat = ReadingFormat::byte3; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 212 | } |
| 213 | else |
| 214 | { |
| 215 | throw std::runtime_error("Invalid sensor type"); |
| 216 | } |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 217 | |
| 218 | if (subType == IpmbSubType::util) |
| 219 | { |
| 220 | // Utilization need to be scaled to percent |
| 221 | maxValue = 100; |
| 222 | minValue = 0; |
| 223 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void IpmbSensor::checkThresholds(void) |
| 227 | { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 228 | thresholds::checkThresholds(this); |
| 229 | } |
| 230 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 231 | bool IpmbSensor::processReading(const std::vector<uint8_t>& data, double& resp) |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 232 | { |
| 233 | |
| 234 | switch (readingFormat) |
| 235 | { |
| 236 | case (ReadingFormat::byte0): |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame^] | 237 | if (command == ipmi::sensor::getSensorReading && |
| 238 | !ipmi::sensor::isValid(data)) |
James Feist | cf4238e | 2020-07-28 16:40:03 -0700 | [diff] [blame] | 239 | { |
| 240 | return false; |
| 241 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 242 | resp = data[0]; |
James Feist | cf4238e | 2020-07-28 16:40:03 -0700 | [diff] [blame] | 243 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 244 | return true; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 245 | case (ReadingFormat::byte3): |
| 246 | if (data.size() < 4) |
| 247 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 248 | if (!errCount) |
| 249 | { |
| 250 | std::cerr << "Invalid data length returned for " << name |
| 251 | << "\n"; |
| 252 | } |
| 253 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 254 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 255 | resp = data[3]; |
| 256 | return true; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 257 | case (ReadingFormat::elevenBit): |
| 258 | if (data.size() < 5) |
| 259 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 260 | if (!errCount) |
| 261 | { |
| 262 | std::cerr << "Invalid data length returned for " << name |
| 263 | << "\n"; |
| 264 | } |
| 265 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 266 | } |
| 267 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 268 | resp = ((data[4] << 8) | data[3]); |
| 269 | return true; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 270 | case (ReadingFormat::elevenBitShift): |
| 271 | if (data.size() < 5) |
| 272 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 273 | if (!errCount) |
| 274 | { |
| 275 | std::cerr << "Invalid data length returned for " << name |
| 276 | << "\n"; |
| 277 | } |
| 278 | return false; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 279 | } |
| 280 | |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 281 | resp = ((data[4] << 8) | data[3]) >> 3; |
| 282 | return true; |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 283 | default: |
| 284 | throw std::runtime_error("Invalid reading type"); |
| 285 | } |
| 286 | } |
| 287 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 288 | void IpmbSensor::read(void) |
| 289 | { |
| 290 | static constexpr size_t pollTime = 1; // in seconds |
| 291 | |
| 292 | waitTimer.expires_from_now(boost::posix_time::seconds(pollTime)); |
| 293 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 294 | if (ec == boost::asio::error::operation_aborted) |
| 295 | { |
| 296 | return; // we're being canceled |
| 297 | } |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 298 | if (!readingStateGood()) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 299 | { |
Adrian Ambrożewicz | 623723b | 2020-07-29 12:53:54 +0200 | [diff] [blame] | 300 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 301 | read(); |
| 302 | return; |
| 303 | } |
| 304 | dbusConnection->async_method_call( |
| 305 | [this](boost::system::error_code ec, |
| 306 | const IpmbMethodType& response) { |
| 307 | const int& status = std::get<0>(response); |
| 308 | if (ec || status) |
| 309 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 310 | incrementError(); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 311 | read(); |
| 312 | return; |
| 313 | } |
| 314 | const std::vector<uint8_t>& data = std::get<5>(response); |
| 315 | if constexpr (debug) |
| 316 | { |
| 317 | std::cout << name << ": "; |
| 318 | for (size_t d : data) |
| 319 | { |
| 320 | std::cout << d << " "; |
| 321 | } |
| 322 | std::cout << "\n"; |
| 323 | } |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 324 | if (data.empty()) |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 325 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 326 | incrementError(); |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 327 | read(); |
| 328 | return; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 329 | } |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 330 | |
| 331 | double value = 0; |
| 332 | |
| 333 | if (!processReading(data, value)) |
| 334 | { |
| 335 | incrementError(); |
| 336 | read(); |
| 337 | return; |
| 338 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 339 | |
| 340 | /* Adjust value as per scale and offset */ |
| 341 | value = (value * scaleVal) + offsetVal; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 342 | updateValue(value); |
| 343 | read(); |
| 344 | }, |
| 345 | "xyz.openbmc_project.Ipmi.Channel.Ipmb", |
| 346 | "/xyz/openbmc_project/Ipmi/Channel/Ipmb", "org.openbmc.Ipmb", |
| 347 | "sendRequest", commandAddress, netfn, lun, command, commandData); |
| 348 | }); |
| 349 | } |
| 350 | void createSensors( |
| 351 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 352 | boost::container::flat_map<std::string, std::unique_ptr<IpmbSensor>>& |
| 353 | sensors, |
| 354 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 355 | { |
| 356 | if (!dbusConnection) |
| 357 | { |
| 358 | std::cerr << "Connection not created\n"; |
| 359 | return; |
| 360 | } |
| 361 | dbusConnection->async_method_call( |
| 362 | [&](boost::system::error_code ec, const ManagedObjectType& resp) { |
| 363 | if (ec) |
| 364 | { |
| 365 | std::cerr << "Error contacting entity manager\n"; |
| 366 | return; |
| 367 | } |
| 368 | for (const auto& pathPair : resp) |
| 369 | { |
| 370 | for (const auto& entry : pathPair.second) |
| 371 | { |
| 372 | if (entry.first != configInterface) |
| 373 | { |
| 374 | continue; |
| 375 | } |
| 376 | std::string name = |
| 377 | loadVariant<std::string>(entry.second, "Name"); |
| 378 | |
| 379 | std::vector<thresholds::Threshold> sensorThresholds; |
| 380 | if (!parseThresholdsFromConfig(pathPair.second, |
| 381 | sensorThresholds)) |
| 382 | { |
| 383 | std::cerr << "error populating thresholds for " << name |
| 384 | << "\n"; |
| 385 | } |
| 386 | uint8_t deviceAddress = |
| 387 | loadVariant<uint8_t>(entry.second, "Address"); |
| 388 | |
| 389 | std::string sensorClass = |
| 390 | loadVariant<std::string>(entry.second, "Class"); |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 391 | |
| 392 | /* Default sensor type is "temperature" */ |
| 393 | std::string sensorTypeName = "temperature"; |
| 394 | auto findType = entry.second.find("SensorType"); |
| 395 | if (findType != entry.second.end()) |
| 396 | { |
| 397 | sensorTypeName = std::visit(VariantToStringVisitor(), |
| 398 | findType->second); |
| 399 | } |
| 400 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 401 | auto& sensor = sensors[name]; |
| 402 | sensor = std::make_unique<IpmbSensor>( |
| 403 | dbusConnection, io, name, pathPair.first, objectServer, |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 404 | std::move(sensorThresholds), deviceAddress, |
| 405 | sensorTypeName); |
| 406 | |
| 407 | /* Initialize scale and offset value */ |
| 408 | sensor->scaleVal = 1; |
| 409 | sensor->offsetVal = 0; |
| 410 | |
| 411 | auto findScaleVal = entry.second.find("ScaleValue"); |
| 412 | if (findScaleVal != entry.second.end()) |
| 413 | { |
| 414 | sensor->scaleVal = std::visit(VariantToDoubleVisitor(), |
| 415 | findScaleVal->second); |
| 416 | } |
| 417 | |
| 418 | auto findOffsetVal = entry.second.find("OffsetValue"); |
| 419 | if (findOffsetVal != entry.second.end()) |
| 420 | { |
| 421 | sensor->offsetVal = std::visit(VariantToDoubleVisitor(), |
| 422 | findOffsetVal->second); |
| 423 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 424 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 425 | auto findPowerState = entry.second.find("PowerState"); |
| 426 | |
| 427 | if (findPowerState != entry.second.end()) |
| 428 | { |
| 429 | std::string powerState = std::visit( |
| 430 | VariantToStringVisitor(), findPowerState->second); |
| 431 | |
| 432 | setReadState(powerState, sensor->readState); |
| 433 | } |
| 434 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 435 | if (sensorClass == "PxeBridgeTemp") |
| 436 | { |
| 437 | sensor->type = IpmbType::PXE1410CVR; |
| 438 | } |
| 439 | else if (sensorClass == "IRBridgeTemp") |
| 440 | { |
| 441 | sensor->type = IpmbType::IR38363VR; |
| 442 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 443 | else if (sensorClass == "HSCBridge") |
| 444 | { |
| 445 | sensor->type = IpmbType::ADM1278HSC; |
| 446 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 447 | else if (sensorClass == "MpsBridgeTemp") |
| 448 | { |
| 449 | sensor->type = IpmbType::mpsVR; |
| 450 | } |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 451 | else if (sensorClass == "METemp" || |
| 452 | sensorClass == "MESensor") |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 453 | { |
| 454 | sensor->type = IpmbType::meSensor; |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | std::cerr << "Invalid class " << sensorClass << "\n"; |
| 459 | continue; |
| 460 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 461 | |
| 462 | if (sensorTypeName == "voltage") |
| 463 | { |
| 464 | sensor->subType = IpmbSubType::volt; |
| 465 | } |
| 466 | else if (sensorTypeName == "power") |
| 467 | { |
| 468 | sensor->subType = IpmbSubType::power; |
| 469 | } |
| 470 | else if (sensorTypeName == "current") |
| 471 | { |
| 472 | sensor->subType = IpmbSubType::curr; |
| 473 | } |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 474 | else if (sensorTypeName == "utilization") |
| 475 | { |
| 476 | sensor->subType = IpmbSubType::util; |
| 477 | } |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 478 | else |
| 479 | { |
| 480 | sensor->subType = IpmbSubType::temp; |
| 481 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 482 | sensor->init(); |
| 483 | } |
| 484 | } |
| 485 | }, |
| 486 | entityManagerName, "/", "org.freedesktop.DBus.ObjectManager", |
| 487 | "GetManagedObjects"); |
| 488 | } |
| 489 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 490 | void reinitSensors(sdbusplus::message::message& message) |
| 491 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 492 | constexpr const size_t reinitWaitSeconds = 2; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 493 | std::string objectName; |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 494 | boost::container::flat_map<std::string, std::variant<std::string>> values; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 495 | message.read(objectName, values); |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 496 | |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 497 | auto findStatus = values.find(power::property); |
| 498 | if (findStatus != values.end()) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 499 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 500 | bool powerStatus = boost::ends_with( |
| 501 | std::get<std::string>(findStatus->second), "Running"); |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 502 | if (powerStatus) |
| 503 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 504 | if (!initCmdTimer) |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 505 | { |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 506 | // this should be impossible |
| 507 | return; |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 508 | } |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 509 | // we seem to send this command too fast sometimes, wait before |
| 510 | // sending |
| 511 | initCmdTimer->expires_from_now( |
| 512 | boost::posix_time::seconds(reinitWaitSeconds)); |
| 513 | |
| 514 | initCmdTimer->async_wait([](const boost::system::error_code ec) { |
| 515 | if (ec == boost::asio::error::operation_aborted) |
| 516 | { |
| 517 | return; // we're being canceled |
| 518 | } |
| 519 | |
| 520 | for (const auto& sensor : sensors) |
| 521 | { |
| 522 | if (sensor.second) |
| 523 | { |
| 524 | sensor.second->runInitCmd(); |
| 525 | } |
| 526 | } |
| 527 | }); |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 532 | int main() |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 533 | { |
| 534 | |
| 535 | boost::asio::io_service io; |
| 536 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 537 | systemBus->request_name("xyz.openbmc_project.IpmbSensor"); |
| 538 | sdbusplus::asio::object_server objectServer(systemBus); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 539 | |
James Feist | 0d4f2bd | 2019-03-05 13:15:40 -0800 | [diff] [blame] | 540 | initCmdTimer = std::make_unique<boost::asio::deadline_timer>(io); |
| 541 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 542 | io.post([&]() { createSensors(io, objectServer, sensors, systemBus); }); |
| 543 | |
| 544 | boost::asio::deadline_timer configTimer(io); |
| 545 | |
| 546 | std::function<void(sdbusplus::message::message&)> eventHandler = |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 547 | [&](sdbusplus::message::message&) { |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 548 | configTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 549 | // create a timer because normally multiple properties change |
| 550 | configTimer.async_wait([&](const boost::system::error_code& ec) { |
| 551 | if (ec == boost::asio::error::operation_aborted) |
| 552 | { |
| 553 | return; // we're being canceled |
| 554 | } |
| 555 | createSensors(io, objectServer, sensors, systemBus); |
| 556 | if (sensors.empty()) |
| 557 | { |
| 558 | std::cout << "Configuration not detected\n"; |
| 559 | } |
| 560 | }); |
| 561 | }; |
| 562 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 563 | sdbusplus::bus::match::match configMatch( |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 564 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 565 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 566 | std::string(inventoryPath) + "',arg0namespace='" + configInterface + |
| 567 | "'", |
| 568 | eventHandler); |
| 569 | |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 570 | sdbusplus::bus::match::match powerChangeMatch( |
| 571 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 572 | "type='signal',interface='" + std::string(properties::interface) + |
| 573 | "',path='" + std::string(power::path) + "',arg0='" + |
| 574 | std::string(power::interface) + "'", |
James Feist | f7e2c5d | 2019-02-13 17:27:51 -0800 | [diff] [blame] | 575 | reinitSensors); |
| 576 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 577 | io.run(); |
| 578 | } |