James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2017 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 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 17 | #include <HwmonTempSensor.hpp> |
| 18 | #include <Utils.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | #include <boost/algorithm/string/predicate.hpp> |
| 20 | #include <boost/algorithm/string/replace.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 21 | #include <boost/container/flat_map.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | #include <boost/container/flat_set.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 23 | #include <sdbusplus/asio/connection.hpp> |
| 24 | #include <sdbusplus/asio/object_server.hpp> |
| 25 | #include <sdbusplus/bus/match.hpp> |
| 26 | |
| 27 | #include <array> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 28 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 30 | #include <functional> |
| 31 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | #include <regex> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 33 | #include <stdexcept> |
| 34 | #include <string> |
| 35 | #include <utility> |
| 36 | #include <variant> |
| 37 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 38 | |
Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 39 | static constexpr float pollRateDefault = 0.5; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 41 | static constexpr double maxValuePressure = 120000; // Pascals |
| 42 | static constexpr double minValuePressure = 30000; // Pascals |
| 43 | |
| 44 | static constexpr double maxValueTemperature = 127; // DegreesC |
| 45 | static constexpr double minValueTemperature = -128; // DegreesC |
| 46 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 47 | namespace fs = std::filesystem; |
Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 48 | static auto sensorTypes{ |
Potin Lai | 4786334 | 2021-12-14 18:58:12 +0800 | [diff] [blame^] | 49 | std::to_array<const char*>({"xyz.openbmc_project.Configuration.DPS310", |
| 50 | "xyz.openbmc_project.Configuration.EMC1412", |
Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 51 | "xyz.openbmc_project.Configuration.EMC1413", |
| 52 | "xyz.openbmc_project.Configuration.EMC1414", |
Potin Lai | 4786334 | 2021-12-14 18:58:12 +0800 | [diff] [blame^] | 53 | "xyz.openbmc_project.Configuration.HDC1080", |
| 54 | "xyz.openbmc_project.Configuration.JC42", |
| 55 | "xyz.openbmc_project.Configuration.LM75A", |
| 56 | "xyz.openbmc_project.Configuration.LM95234", |
Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 57 | "xyz.openbmc_project.Configuration.MAX31725", |
| 58 | "xyz.openbmc_project.Configuration.MAX31730", |
| 59 | "xyz.openbmc_project.Configuration.MAX6581", |
| 60 | "xyz.openbmc_project.Configuration.MAX6654", |
| 61 | "xyz.openbmc_project.Configuration.NCT7802", |
| 62 | "xyz.openbmc_project.Configuration.SBTSI", |
Potin Lai | 4786334 | 2021-12-14 18:58:12 +0800 | [diff] [blame^] | 63 | "xyz.openbmc_project.Configuration.SI7020", |
Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 64 | "xyz.openbmc_project.Configuration.TMP112", |
| 65 | "xyz.openbmc_project.Configuration.TMP175", |
| 66 | "xyz.openbmc_project.Configuration.TMP421", |
| 67 | "xyz.openbmc_project.Configuration.TMP441", |
Brandon Kim | 6655823 | 2021-11-09 16:53:08 -0800 | [diff] [blame] | 68 | "xyz.openbmc_project.Configuration.TMP75", |
Potin Lai | 4786334 | 2021-12-14 18:58:12 +0800 | [diff] [blame^] | 69 | "xyz.openbmc_project.Configuration.W83773G"})}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 70 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 71 | static struct SensorParams |
| 72 | getSensorParameters(const std::filesystem::path& path) |
| 73 | { |
| 74 | // offset is to default to 0 and scale to 1, see lore |
| 75 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 76 | struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature, |
| 77 | .maxValue = maxValueTemperature, |
| 78 | .offsetValue = 0.0, |
| 79 | .scaleValue = 1.0, |
Bruce Mitchell | 5a86e56 | 2021-12-10 12:26:22 -0600 | [diff] [blame] | 80 | .units = |
| 81 | sensor_paths::unitDegreesC, |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 82 | .typeName = "temperature"}; |
| 83 | |
| 84 | // For IIO RAW sensors we get a raw_value, an offset, and scale |
| 85 | // to compute the value = (raw_value + offset) * scale |
| 86 | // with a _raw IIO device we need to get the |
| 87 | // offsetValue and scaleValue from the driver |
| 88 | // these are used to compute the reading in |
| 89 | // units that have yet to be scaled for D-Bus. |
| 90 | const std::string pathStr = path.string(); |
| 91 | if (pathStr.ends_with("_raw")) |
| 92 | { |
| 93 | std::string pathOffsetStr = |
| 94 | pathStr.substr(0, pathStr.size() - 4) + "_offset"; |
| 95 | std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0); |
| 96 | // In case there is nothing to read skip this device |
| 97 | // This is not an error condition see lore |
| 98 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 99 | if (tmpOffsetValue) |
| 100 | { |
| 101 | tmpSensorParameters.offsetValue = *tmpOffsetValue; |
| 102 | } |
| 103 | |
| 104 | std::string pathScaleStr = |
| 105 | pathStr.substr(0, pathStr.size() - 4) + "_scale"; |
| 106 | std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0); |
| 107 | // In case there is nothing to read skip this device |
| 108 | // This is not an error condition see lore |
| 109 | // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ |
| 110 | if (tmpScaleValue) |
| 111 | { |
| 112 | tmpSensorParameters.scaleValue = *tmpScaleValue; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Temperatures are read in milli degrees Celsius, we need |
| 117 | // degrees Celsius. Pressures are read in kilopascal, we need |
| 118 | // Pascals. On D-Bus for Open BMC we use the International |
| 119 | // System of Units without prefixes. Links to the kernel |
| 120 | // documentation: |
| 121 | // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface |
| 122 | // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio |
| 123 | if (path.filename() == "in_pressure_input" || |
| 124 | path.filename() == "in_pressure_raw") |
| 125 | { |
| 126 | tmpSensorParameters.minValue = minValuePressure; |
| 127 | tmpSensorParameters.maxValue = maxValuePressure; |
| 128 | // Pressures are read in kilopascal, we need Pascals. |
| 129 | tmpSensorParameters.scaleValue *= 1000.0; |
| 130 | tmpSensorParameters.typeName = "pressure"; |
Bruce Mitchell | 5a86e56 | 2021-12-10 12:26:22 -0600 | [diff] [blame] | 131 | tmpSensorParameters.units = sensor_paths::unitPascals; |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 132 | } |
| 133 | else |
| 134 | { |
| 135 | // Temperatures are read in milli degrees Celsius, |
| 136 | // we need degrees Celsius. |
| 137 | tmpSensorParameters.scaleValue *= 0.001; |
| 138 | } |
| 139 | |
| 140 | return tmpSensorParameters; |
| 141 | } |
| 142 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 143 | void createSensors( |
| 144 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 145 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 146 | sensors, |
| 147 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 148 | const std::shared_ptr<boost::container::flat_set<std::string>>& |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 149 | sensorsChanged) |
| 150 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 151 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 152 | dbusConnection, |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 153 | [&io, &objectServer, &sensors, &dbusConnection, |
| 154 | sensorsChanged](const ManagedObjectType& sensorConfigurations) { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 155 | bool firstScan = sensorsChanged == nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 157 | // IIO _raw devices look like this on sysfs: |
| 158 | // /sys/bus/iio/devices/iio:device0/in_temp_raw |
| 159 | // /sys/bus/iio/devices/iio:device0/in_temp_offset |
| 160 | // /sys/bus/iio/devices/iio:device0/in_temp_scale |
| 161 | // |
| 162 | // Other IIO devices look like this on sysfs: |
| 163 | // /sys/bus/iio/devices/iio:device1/in_temp_input |
| 164 | // /sys/bus/iio/devices/iio:device1/in_pressure_input |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 165 | std::vector<fs::path> paths; |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 166 | fs::path root("/sys/bus/iio/devices"); |
| 167 | findFiles(root, R"(in_temp\d*_(input|raw))", paths); |
| 168 | findFiles(root, R"(in_pressure\d*_(input|raw))", paths); |
| 169 | findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths); |
| 170 | |
| 171 | if (paths.empty()) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 172 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 176 | // iterate through all found temp and pressure sensors, |
| 177 | // and try to match them with configuration |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 178 | for (auto& path : paths) |
| 179 | { |
| 180 | std::smatch match; |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 181 | const std::string pathStr = path.string(); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 182 | auto directory = path.parent_path(); |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 183 | fs::path device; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 184 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 185 | std::string deviceName; |
| 186 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 187 | { |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 188 | device = fs::canonical(directory); |
| 189 | deviceName = device.parent_path().stem(); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 190 | } |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 191 | else |
| 192 | { |
| 193 | device = directory / "device"; |
| 194 | deviceName = fs::canonical(device).stem(); |
| 195 | } |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 196 | auto findHyphen = deviceName.find('-'); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 197 | if (findHyphen == std::string::npos) |
| 198 | { |
| 199 | std::cerr << "found bad device " << deviceName << "\n"; |
| 200 | continue; |
| 201 | } |
| 202 | std::string busStr = deviceName.substr(0, findHyphen); |
| 203 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 204 | |
| 205 | size_t bus = 0; |
| 206 | size_t addr = 0; |
| 207 | try |
| 208 | { |
| 209 | bus = std::stoi(busStr); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 210 | addr = std::stoi(addrStr, nullptr, 16); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 211 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 212 | catch (const std::invalid_argument&) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 213 | { |
| 214 | continue; |
| 215 | } |
| 216 | const SensorData* sensorData = nullptr; |
| 217 | const std::string* interfacePath = nullptr; |
| 218 | const char* sensorType = nullptr; |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 219 | const SensorBaseConfiguration* baseConfiguration = nullptr; |
| 220 | const SensorBaseConfigMap* baseConfigMap = nullptr; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 221 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 222 | auto thisSensorParameters = getSensorParameters(path); |
| 223 | |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 224 | for (const std::pair<sdbusplus::message::object_path, |
| 225 | SensorData>& sensor : sensorConfigurations) |
| 226 | { |
| 227 | sensorData = &(sensor.second); |
| 228 | for (const char* type : sensorTypes) |
| 229 | { |
| 230 | auto sensorBase = sensorData->find(type); |
| 231 | if (sensorBase != sensorData->end()) |
| 232 | { |
| 233 | baseConfiguration = &(*sensorBase); |
| 234 | sensorType = type; |
| 235 | break; |
| 236 | } |
| 237 | } |
| 238 | if (baseConfiguration == nullptr) |
| 239 | { |
| 240 | std::cerr << "error finding base configuration for " |
| 241 | << deviceName << "\n"; |
| 242 | continue; |
| 243 | } |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 244 | baseConfigMap = &baseConfiguration->second; |
| 245 | auto configurationBus = baseConfigMap->find("Bus"); |
| 246 | auto configurationAddress = baseConfigMap->find("Address"); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 247 | |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 248 | if (configurationBus == baseConfigMap->end() || |
| 249 | configurationAddress == baseConfigMap->end()) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 250 | { |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 251 | std::cerr << "error finding bus or address in " |
| 252 | "configuration\n"; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 253 | continue; |
| 254 | } |
| 255 | |
| 256 | if (std::get<uint64_t>(configurationBus->second) != bus || |
| 257 | std::get<uint64_t>(configurationAddress->second) != |
| 258 | addr) |
| 259 | { |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | interfacePath = &(sensor.first.str); |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 264 | break; |
| 265 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 266 | if (interfacePath == nullptr) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 267 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 268 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 269 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 270 | |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 271 | // Temperature has "Name", pressure has "Name1" |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 272 | auto findSensorName = baseConfigMap->find("Name"); |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 273 | if (thisSensorParameters.typeName == "pressure") |
| 274 | { |
| 275 | findSensorName = baseConfigMap->find("Name1"); |
| 276 | } |
| 277 | |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 278 | if (findSensorName == baseConfigMap->end()) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 279 | { |
| 280 | std::cerr << "could not determine configuration name for " |
| 281 | << deviceName << "\n"; |
| 282 | continue; |
| 283 | } |
| 284 | std::string sensorName = |
| 285 | std::get<std::string>(findSensorName->second); |
| 286 | // on rescans, only update sensors we were signaled by |
| 287 | auto findSensor = sensors.find(sensorName); |
| 288 | if (!firstScan && findSensor != sensors.end()) |
| 289 | { |
| 290 | bool found = false; |
Bruce Mitchell | d653b75 | 2021-08-23 13:09:00 -0500 | [diff] [blame] | 291 | auto it = sensorsChanged->begin(); |
| 292 | while (it != sensorsChanged->end()) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 293 | { |
| 294 | if (boost::ends_with(*it, findSensor->second->name)) |
| 295 | { |
Bruce Mitchell | d653b75 | 2021-08-23 13:09:00 -0500 | [diff] [blame] | 296 | it = sensorsChanged->erase(it); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 297 | findSensor->second = nullptr; |
| 298 | found = true; |
| 299 | break; |
| 300 | } |
Bruce Mitchell | d653b75 | 2021-08-23 13:09:00 -0500 | [diff] [blame] | 301 | ++it; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 302 | } |
| 303 | if (!found) |
| 304 | { |
| 305 | continue; |
| 306 | } |
| 307 | } |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 308 | |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 309 | std::vector<thresholds::Threshold> sensorThresholds; |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 310 | int index = 1; |
| 311 | |
| 312 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds, |
| 313 | nullptr, &index)) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 314 | { |
| 315 | std::cerr << "error populating thresholds for " |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 316 | << sensorName << " index 1\n"; |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 317 | } |
Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 318 | |
| 319 | auto findPollRate = baseConfiguration->second.find("PollRate"); |
| 320 | float pollRate = pollRateDefault; |
| 321 | if (findPollRate != baseConfiguration->second.end()) |
| 322 | { |
| 323 | pollRate = std::visit(VariantToFloatVisitor(), |
| 324 | findPollRate->second); |
| 325 | if (pollRate <= 0.0f) |
| 326 | { |
| 327 | pollRate = pollRateDefault; // polling time too short |
| 328 | } |
| 329 | } |
| 330 | |
James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 331 | auto findPowerOn = baseConfiguration->second.find("PowerState"); |
| 332 | PowerState readState = PowerState::always; |
| 333 | if (findPowerOn != baseConfiguration->second.end()) |
| 334 | { |
| 335 | std::string powerState = std::visit( |
| 336 | VariantToStringVisitor(), findPowerOn->second); |
| 337 | setReadState(powerState, readState); |
| 338 | } |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 339 | |
| 340 | auto permitSet = getPermitSet(*baseConfigMap); |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 341 | auto& sensor = sensors[sensorName]; |
| 342 | sensor = nullptr; |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 343 | auto hwmonFile = getFullHwmonFilePath(directory.string(), |
| 344 | "temp1", permitSet); |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 345 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
| 346 | { |
| 347 | hwmonFile = pathStr; |
| 348 | } |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 349 | if (hwmonFile) |
| 350 | { |
| 351 | sensor = std::make_shared<HwmonTempSensor>( |
| 352 | *hwmonFile, sensorType, objectServer, dbusConnection, |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 353 | io, sensorName, std::move(sensorThresholds), |
| 354 | thisSensorParameters, pollRate, *interfacePath, |
| 355 | readState); |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 356 | sensor->setupRead(); |
| 357 | } |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 358 | // Looking for keys like "Name1" for temp2_input, |
| 359 | // "Name2" for temp3_input, etc. |
| 360 | int i = 0; |
| 361 | while (true) |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 362 | { |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 363 | ++i; |
| 364 | auto findKey = |
Jason Ling | 8b8bcc8 | 2020-08-04 19:33:22 -0700 | [diff] [blame] | 365 | baseConfigMap->find("Name" + std::to_string(i)); |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 366 | if (findKey == baseConfigMap->end()) |
| 367 | { |
| 368 | break; |
| 369 | } |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 370 | std::string sensorName = |
| 371 | std::get<std::string>(findKey->second); |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 372 | hwmonFile = getFullHwmonFilePath( |
| 373 | directory.string(), "temp" + std::to_string(i + 1), |
| 374 | permitSet); |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 375 | if (pathStr.starts_with("/sys/bus/iio/devices")) |
| 376 | { |
| 377 | continue; |
| 378 | } |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 379 | if (hwmonFile) |
| 380 | { |
Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 381 | // To look up thresholds for these additional sensors, |
| 382 | // match on the Index property in the threshold data |
| 383 | // where the index comes from the sysfs file we're on, |
| 384 | // i.e. index = 2 for temp2_input. |
| 385 | int index = i + 1; |
| 386 | std::vector<thresholds::Threshold> thresholds; |
| 387 | |
| 388 | if (!parseThresholdsFromConfig(*sensorData, thresholds, |
| 389 | nullptr, &index)) |
| 390 | { |
| 391 | std::cerr << "error populating thresholds for " |
| 392 | << sensorName << " index " << index |
| 393 | << "\n"; |
| 394 | } |
| 395 | |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 396 | auto& sensor = sensors[sensorName]; |
| 397 | sensor = nullptr; |
| 398 | sensor = std::make_shared<HwmonTempSensor>( |
| 399 | *hwmonFile, sensorType, objectServer, |
| 400 | dbusConnection, io, sensorName, |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 401 | std::move(thresholds), thisSensorParameters, |
| 402 | pollRate, *interfacePath, readState); |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 403 | sensor->setupRead(); |
| 404 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 405 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 406 | } |
Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 407 | }); |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 408 | getter->getConfiguration( |
| 409 | std::vector<std::string>(sensorTypes.begin(), sensorTypes.end())); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 412 | void interfaceRemoved( |
| 413 | sdbusplus::message::message& message, |
| 414 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& |
| 415 | sensors) |
| 416 | { |
| 417 | if (message.is_method_error()) |
| 418 | { |
| 419 | std::cerr << "interfacesRemoved callback method error\n"; |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | sdbusplus::message::object_path path; |
| 424 | std::vector<std::string> interfaces; |
| 425 | |
| 426 | message.read(path, interfaces); |
| 427 | |
| 428 | // If the xyz.openbmc_project.Confguration.X interface was removed |
| 429 | // for one or more sensors, delete those sensor objects. |
| 430 | auto sensorIt = sensors.begin(); |
| 431 | while (sensorIt != sensors.end()) |
| 432 | { |
| 433 | if ((sensorIt->second->configurationPath == path) && |
| 434 | (std::find(interfaces.begin(), interfaces.end(), |
| 435 | sensorIt->second->objectType) != interfaces.end())) |
| 436 | { |
| 437 | sensorIt = sensors.erase(sensorIt); |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | sensorIt++; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 446 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 447 | { |
| 448 | boost::asio::io_service io; |
| 449 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 450 | systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); |
| 451 | sdbusplus::asio::object_server objectServer(systemBus); |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 452 | boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 453 | sensors; |
| 454 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 455 | auto sensorsChanged = |
| 456 | std::make_shared<boost::container::flat_set<std::string>>(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 457 | |
| 458 | io.post([&]() { |
| 459 | createSensors(io, objectServer, sensors, systemBus, nullptr); |
| 460 | }); |
| 461 | |
| 462 | boost::asio::deadline_timer filterTimer(io); |
| 463 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 464 | [&](sdbusplus::message::message& message) { |
| 465 | if (message.is_method_error()) |
| 466 | { |
| 467 | std::cerr << "callback method error\n"; |
| 468 | return; |
| 469 | } |
| 470 | sensorsChanged->insert(message.get_path()); |
| 471 | // this implicitly cancels the timer |
| 472 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 473 | |
| 474 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 475 | if (ec == boost::asio::error::operation_aborted) |
| 476 | { |
| 477 | /* we were canceled*/ |
| 478 | return; |
| 479 | } |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 480 | if (ec) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 481 | { |
| 482 | std::cerr << "timer error\n"; |
| 483 | return; |
| 484 | } |
| 485 | createSensors(io, objectServer, sensors, systemBus, |
| 486 | sensorsChanged); |
| 487 | }); |
| 488 | }; |
| 489 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 490 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 491 | { |
| 492 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 493 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 494 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 495 | std::string(inventoryPath) + "',arg0namespace='" + type + "'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 496 | eventHandler); |
| 497 | matches.emplace_back(std::move(match)); |
| 498 | } |
| 499 | |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 500 | setupManufacturingModeMatch(*systemBus); |
Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 501 | |
| 502 | // Watch for entity-manager to remove configuration interfaces |
| 503 | // so the corresponding sensors can be removed. |
| 504 | auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 505 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 506 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 507 | std::string(inventoryPath) + "/'", |
| 508 | [&sensors](sdbusplus::message::message& msg) { |
| 509 | interfaceRemoved(msg, sensors); |
| 510 | }); |
| 511 | |
| 512 | matches.emplace_back(std::move(ifaceRemovedMatch)); |
| 513 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 514 | io.run(); |
| 515 | } |