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