| 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 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 156 | struct SensorConfigKey | 
 | 157 | { | 
 | 158 |     uint64_t bus; | 
 | 159 |     uint64_t addr; | 
 | 160 |     bool operator<(const SensorConfigKey& other) const | 
 | 161 |     { | 
 | 162 |         if (bus != other.bus) | 
 | 163 |         { | 
 | 164 |             return bus < other.bus; | 
 | 165 |         } | 
 | 166 |         return addr < other.addr; | 
 | 167 |     } | 
 | 168 | }; | 
 | 169 |  | 
 | 170 | struct SensorConfig | 
 | 171 | { | 
 | 172 |     std::string sensorPath; | 
 | 173 |     SensorData sensorData; | 
 | 174 |     std::string interface; | 
 | 175 |     SensorBaseConfigMap config; | 
 | 176 |     std::vector<std::string> name; | 
 | 177 | }; | 
 | 178 |  | 
 | 179 | using SensorConfigMap = | 
 | 180 |     boost::container::flat_map<SensorConfigKey, SensorConfig>; | 
 | 181 |  | 
 | 182 | static SensorConfigMap | 
 | 183 |     buildSensorConfigMap(const ManagedObjectType& sensorConfigs) | 
 | 184 | { | 
 | 185 |     SensorConfigMap configMap; | 
 | 186 |     for (const std::pair<sdbusplus::message::object_path, SensorData>& sensor : | 
 | 187 |          sensorConfigs) | 
 | 188 |     { | 
 | 189 |         for (const std::pair<std::string, SensorBaseConfigMap>& cfgmap : | 
 | 190 |              sensor.second) | 
 | 191 |         { | 
 | 192 |             const SensorBaseConfigMap& cfg = cfgmap.second; | 
 | 193 |             auto busCfg = cfg.find("Bus"); | 
 | 194 |             auto addrCfg = cfg.find("Address"); | 
 | 195 |             if ((busCfg == cfg.end()) || (addrCfg == cfg.end())) | 
 | 196 |             { | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 197 |                 continue; | 
 | 198 |             } | 
 | 199 |  | 
 | 200 |             if ((!std::get_if<uint64_t>(&busCfg->second)) || | 
 | 201 |                 (!std::get_if<uint64_t>(&addrCfg->second))) | 
 | 202 |             { | 
 | 203 |                 std::cerr << sensor.first.str << " Bus or Address invalid\n"; | 
 | 204 |                 continue; | 
 | 205 |             } | 
 | 206 |  | 
 | 207 |             std::vector<std::string> hwmonNames; | 
 | 208 |             auto nameCfg = cfg.find("Name"); | 
 | 209 |             if (nameCfg != cfg.end()) | 
 | 210 |             { | 
 | 211 |                 hwmonNames.push_back(std::get<std::string>(nameCfg->second)); | 
 | 212 |                 size_t i = 1; | 
 | 213 |                 while (true) | 
 | 214 |                 { | 
 | 215 |                     auto sensorNameCfg = cfg.find("Name" + std::to_string(i)); | 
 | 216 |                     if (sensorNameCfg == cfg.end()) | 
 | 217 |                     { | 
 | 218 |                         break; | 
 | 219 |                     } | 
 | 220 |                     hwmonNames.push_back( | 
 | 221 |                         std::get<std::string>(sensorNameCfg->second)); | 
 | 222 |                     i++; | 
 | 223 |                 } | 
 | 224 |             } | 
 | 225 |  | 
 | 226 |             SensorConfigKey key = {std::get<uint64_t>(busCfg->second), | 
 | 227 |                                    std::get<uint64_t>(addrCfg->second)}; | 
 | 228 |             SensorConfig val = {sensor.first.str, sensor.second, cfgmap.first, | 
 | 229 |                                 cfg, hwmonNames}; | 
 | 230 |  | 
 | 231 |             auto [it, inserted] = configMap.emplace(key, std::move(val)); | 
 | 232 |             if (!inserted) | 
 | 233 |             { | 
 | 234 |                 std::cerr << sensor.first.str | 
 | 235 |                           << ": ignoring duplicate entry for {" << key.bus | 
 | 236 |                           << ", 0x" << std::hex << key.addr << std::dec | 
 | 237 |                           << "}\n"; | 
 | 238 |             } | 
 | 239 |         } | 
 | 240 |     } | 
 | 241 |     return configMap; | 
 | 242 | } | 
 | 243 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 244 | void createSensors( | 
 | 245 |     boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, | 
| Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 246 |     boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 247 |         sensors, | 
 | 248 |     std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, | 
| James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 249 |     const std::shared_ptr<boost::container::flat_set<std::string>>& | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 250 |         sensorsChanged) | 
 | 251 | { | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 252 |     auto getter = std::make_shared<GetSensorConfiguration>( | 
 | 253 |         dbusConnection, | 
| Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 254 |         [&io, &objectServer, &sensors, &dbusConnection, | 
 | 255 |          sensorsChanged](const ManagedObjectType& sensorConfigurations) { | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 256 |             bool firstScan = sensorsChanged == nullptr; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 257 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 258 |             SensorConfigMap configMap = | 
 | 259 |                 buildSensorConfigMap(sensorConfigurations); | 
 | 260 |  | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 261 |             // IIO _raw devices look like this on sysfs: | 
 | 262 |             //     /sys/bus/iio/devices/iio:device0/in_temp_raw | 
 | 263 |             //     /sys/bus/iio/devices/iio:device0/in_temp_offset | 
 | 264 |             //     /sys/bus/iio/devices/iio:device0/in_temp_scale | 
 | 265 |             // | 
 | 266 |             // Other IIO devices look like this on sysfs: | 
 | 267 |             //     /sys/bus/iio/devices/iio:device1/in_temp_input | 
 | 268 |             //     /sys/bus/iio/devices/iio:device1/in_pressure_input | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 269 |             std::vector<fs::path> paths; | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 270 |             fs::path root("/sys/bus/iio/devices"); | 
 | 271 |             findFiles(root, R"(in_temp\d*_(input|raw))", paths); | 
 | 272 |             findFiles(root, R"(in_pressure\d*_(input|raw))", paths); | 
| Bruce Mitchell | 3ec41c5 | 2021-12-10 16:05:17 -0600 | [diff] [blame] | 273 |             findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths); | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 274 |             findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths); | 
 | 275 |  | 
 | 276 |             if (paths.empty()) | 
| James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 277 |             { | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 278 |                 return; | 
 | 279 |             } | 
 | 280 |  | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 281 |             // iterate through all found temp and pressure sensors, | 
 | 282 |             // and try to match them with configuration | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 283 |             for (auto& path : paths) | 
 | 284 |             { | 
 | 285 |                 std::smatch match; | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 286 |                 const std::string pathStr = path.string(); | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 287 |                 auto directory = path.parent_path(); | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 288 |                 fs::path device; | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 289 |  | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 290 |                 std::string deviceName; | 
 | 291 |                 if (pathStr.starts_with("/sys/bus/iio/devices")) | 
| James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 292 |                 { | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 293 |                     device = fs::canonical(directory); | 
 | 294 |                     deviceName = device.parent_path().stem(); | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 295 |                 } | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 296 |                 else | 
 | 297 |                 { | 
 | 298 |                     device = directory / "device"; | 
 | 299 |                     deviceName = fs::canonical(device).stem(); | 
 | 300 |                 } | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 301 |                 auto findHyphen = deviceName.find('-'); | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 302 |                 if (findHyphen == std::string::npos) | 
 | 303 |                 { | 
 | 304 |                     std::cerr << "found bad device " << deviceName << "\n"; | 
 | 305 |                     continue; | 
 | 306 |                 } | 
 | 307 |                 std::string busStr = deviceName.substr(0, findHyphen); | 
 | 308 |                 std::string addrStr = deviceName.substr(findHyphen + 1); | 
 | 309 |  | 
 | 310 |                 size_t bus = 0; | 
 | 311 |                 size_t addr = 0; | 
 | 312 |                 try | 
 | 313 |                 { | 
 | 314 |                     bus = std::stoi(busStr); | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 315 |                     addr = std::stoi(addrStr, nullptr, 16); | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 316 |                 } | 
| Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 317 |                 catch (const std::invalid_argument&) | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 318 |                 { | 
 | 319 |                     continue; | 
 | 320 |                 } | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 321 |  | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 322 |                 auto thisSensorParameters = getSensorParameters(path); | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 323 |                 auto findSensorCfg = configMap.find({bus, addr}); | 
 | 324 |                 if (findSensorCfg == configMap.end()) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 325 |                 { | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 326 |                     continue; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 327 |                 } | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 328 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 329 |                 const std::string& interfacePath = | 
 | 330 |                     findSensorCfg->second.sensorPath; | 
 | 331 |                 const SensorData& sensorData = findSensorCfg->second.sensorData; | 
 | 332 |                 const std::string& sensorType = findSensorCfg->second.interface; | 
 | 333 |                 const SensorBaseConfigMap& baseConfigMap = | 
 | 334 |                     findSensorCfg->second.config; | 
 | 335 |                 std::vector<std::string>& hwmonName = | 
 | 336 |                     findSensorCfg->second.name; | 
 | 337 |  | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 338 |                 // Temperature has "Name", pressure has "Name1" | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 339 |                 auto findSensorName = baseConfigMap.find("Name"); | 
| Bruce Mitchell | 3ec41c5 | 2021-12-10 16:05:17 -0600 | [diff] [blame] | 340 |                 if (thisSensorParameters.typeName == "pressure" || | 
 | 341 |                     thisSensorParameters.typeName == "humidity") | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 342 |                 { | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 343 |                     findSensorName = baseConfigMap.find("Name1"); | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 344 |                 } | 
 | 345 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 346 |                 if (findSensorName == baseConfigMap.end()) | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 347 |                 { | 
 | 348 |                     std::cerr << "could not determine configuration name for " | 
 | 349 |                               << deviceName << "\n"; | 
 | 350 |                     continue; | 
 | 351 |                 } | 
 | 352 |                 std::string sensorName = | 
 | 353 |                     std::get<std::string>(findSensorName->second); | 
 | 354 |                 // on rescans, only update sensors we were signaled by | 
 | 355 |                 auto findSensor = sensors.find(sensorName); | 
 | 356 |                 if (!firstScan && findSensor != sensors.end()) | 
 | 357 |                 { | 
 | 358 |                     bool found = false; | 
| Bruce Mitchell | d653b75 | 2021-08-23 13:09:00 -0500 | [diff] [blame] | 359 |                     auto it = sensorsChanged->begin(); | 
 | 360 |                     while (it != sensorsChanged->end()) | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 361 |                     { | 
 | 362 |                         if (boost::ends_with(*it, findSensor->second->name)) | 
 | 363 |                         { | 
| Bruce Mitchell | d653b75 | 2021-08-23 13:09:00 -0500 | [diff] [blame] | 364 |                             it = sensorsChanged->erase(it); | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 365 |                             findSensor->second = nullptr; | 
 | 366 |                             found = true; | 
 | 367 |                             break; | 
 | 368 |                         } | 
| Bruce Mitchell | d653b75 | 2021-08-23 13:09:00 -0500 | [diff] [blame] | 369 |                         ++it; | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 370 |                     } | 
 | 371 |                     if (!found) | 
 | 372 |                     { | 
 | 373 |                         continue; | 
 | 374 |                     } | 
 | 375 |                 } | 
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 376 |  | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 377 |                 std::vector<thresholds::Threshold> sensorThresholds; | 
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 378 |                 int index = 1; | 
 | 379 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 380 |                 if (!parseThresholdsFromConfig(sensorData, sensorThresholds, | 
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 381 |                                                nullptr, &index)) | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 382 |                 { | 
 | 383 |                     std::cerr << "error populating thresholds for " | 
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 384 |                               << sensorName << " index 1\n"; | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 385 |                 } | 
| Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 386 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 387 |                 auto findPollRate = baseConfigMap.find("PollRate"); | 
| Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 388 |                 float pollRate = pollRateDefault; | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 389 |                 if (findPollRate != baseConfigMap.end()) | 
| Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 390 |                 { | 
 | 391 |                     pollRate = std::visit(VariantToFloatVisitor(), | 
 | 392 |                                           findPollRate->second); | 
 | 393 |                     if (pollRate <= 0.0f) | 
 | 394 |                     { | 
 | 395 |                         pollRate = pollRateDefault; // polling time too short | 
 | 396 |                     } | 
 | 397 |                 } | 
 | 398 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 399 |                 auto findPowerOn = baseConfigMap.find("PowerState"); | 
| James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 400 |                 PowerState readState = PowerState::always; | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 401 |                 if (findPowerOn != baseConfigMap.end()) | 
| James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 402 |                 { | 
 | 403 |                     std::string powerState = std::visit( | 
 | 404 |                         VariantToStringVisitor(), findPowerOn->second); | 
 | 405 |                     setReadState(powerState, readState); | 
 | 406 |                 } | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 407 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 408 |                 auto permitSet = getPermitSet(baseConfigMap); | 
| Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 409 |                 auto& sensor = sensors[sensorName]; | 
 | 410 |                 sensor = nullptr; | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 411 |                 auto hwmonFile = getFullHwmonFilePath(directory.string(), | 
 | 412 |                                                       "temp1", permitSet); | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 413 |                 if (pathStr.starts_with("/sys/bus/iio/devices")) | 
 | 414 |                 { | 
 | 415 |                     hwmonFile = pathStr; | 
 | 416 |                 } | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 417 |                 if (hwmonFile) | 
 | 418 |                 { | 
 | 419 |                     sensor = std::make_shared<HwmonTempSensor>( | 
 | 420 |                         *hwmonFile, sensorType, objectServer, dbusConnection, | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 421 |                         io, sensorName, std::move(sensorThresholds), | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 422 |                         thisSensorParameters, pollRate, interfacePath, | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 423 |                         readState); | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 424 |                     sensor->setupRead(); | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 425 |                     hwmonName.erase( | 
 | 426 |                         remove(hwmonName.begin(), hwmonName.end(), sensorName), | 
 | 427 |                         hwmonName.end()); | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 428 |                 } | 
| Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 429 |                 // Looking for keys like "Name1" for temp2_input, | 
 | 430 |                 // "Name2" for temp3_input, etc. | 
 | 431 |                 int i = 0; | 
 | 432 |                 while (true) | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 433 |                 { | 
| Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 434 |                     ++i; | 
 | 435 |                     auto findKey = | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 436 |                         baseConfigMap.find("Name" + std::to_string(i)); | 
 | 437 |                     if (findKey == baseConfigMap.end()) | 
| Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 438 |                     { | 
 | 439 |                         break; | 
 | 440 |                     } | 
| Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 441 |                     std::string sensorName = | 
 | 442 |                         std::get<std::string>(findKey->second); | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 443 |                     hwmonFile = getFullHwmonFilePath( | 
 | 444 |                         directory.string(), "temp" + std::to_string(i + 1), | 
 | 445 |                         permitSet); | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 446 |                     if (pathStr.starts_with("/sys/bus/iio/devices")) | 
 | 447 |                     { | 
 | 448 |                         continue; | 
 | 449 |                     } | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 450 |                     if (hwmonFile) | 
 | 451 |                     { | 
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 452 |                         // To look up thresholds for these additional sensors, | 
 | 453 |                         // match on the Index property in the threshold data | 
 | 454 |                         // where the index comes from the sysfs file we're on, | 
 | 455 |                         // i.e. index = 2 for temp2_input. | 
 | 456 |                         int index = i + 1; | 
 | 457 |                         std::vector<thresholds::Threshold> thresholds; | 
 | 458 |  | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 459 |                         if (!parseThresholdsFromConfig(sensorData, thresholds, | 
| Matt Spinler | 5636d52 | 2021-03-17 14:52:18 -0500 | [diff] [blame] | 460 |                                                        nullptr, &index)) | 
 | 461 |                         { | 
 | 462 |                             std::cerr << "error populating thresholds for " | 
 | 463 |                                       << sensorName << " index " << index | 
 | 464 |                                       << "\n"; | 
 | 465 |                         } | 
 | 466 |  | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 467 |                         auto& sensor = sensors[sensorName]; | 
 | 468 |                         sensor = nullptr; | 
 | 469 |                         sensor = std::make_shared<HwmonTempSensor>( | 
 | 470 |                             *hwmonFile, sensorType, objectServer, | 
 | 471 |                             dbusConnection, io, sensorName, | 
| Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 472 |                             std::move(thresholds), thisSensorParameters, | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 473 |                             pollRate, interfacePath, readState); | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 474 |                         sensor->setupRead(); | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 475 |                         hwmonName.erase(remove(hwmonName.begin(), | 
 | 476 |                                                hwmonName.end(), sensorName), | 
 | 477 |                                         hwmonName.end()); | 
| Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 478 |                     } | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 479 |                 } | 
| Jayashree Dhanapal | 9f3a74e | 2022-01-06 12:05:06 +0530 | [diff] [blame] | 480 |                 if (hwmonName.empty()) | 
 | 481 |                 { | 
 | 482 |                     configMap.erase(findSensorCfg); | 
 | 483 |                 } | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 484 |             } | 
| Ed Tanous | 8a17c30 | 2021-09-02 15:07:11 -0700 | [diff] [blame] | 485 |         }); | 
| James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 486 |     getter->getConfiguration( | 
 | 487 |         std::vector<std::string>(sensorTypes.begin(), sensorTypes.end())); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 488 | } | 
 | 489 |  | 
| Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 490 | void interfaceRemoved( | 
 | 491 |     sdbusplus::message::message& message, | 
 | 492 |     boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& | 
 | 493 |         sensors) | 
 | 494 | { | 
 | 495 |     if (message.is_method_error()) | 
 | 496 |     { | 
 | 497 |         std::cerr << "interfacesRemoved callback method error\n"; | 
 | 498 |         return; | 
 | 499 |     } | 
 | 500 |  | 
 | 501 |     sdbusplus::message::object_path path; | 
 | 502 |     std::vector<std::string> interfaces; | 
 | 503 |  | 
 | 504 |     message.read(path, interfaces); | 
 | 505 |  | 
 | 506 |     // If the xyz.openbmc_project.Confguration.X interface was removed | 
 | 507 |     // for one or more sensors, delete those sensor objects. | 
 | 508 |     auto sensorIt = sensors.begin(); | 
 | 509 |     while (sensorIt != sensors.end()) | 
 | 510 |     { | 
 | 511 |         if ((sensorIt->second->configurationPath == path) && | 
 | 512 |             (std::find(interfaces.begin(), interfaces.end(), | 
 | 513 |                        sensorIt->second->objectType) != interfaces.end())) | 
 | 514 |         { | 
 | 515 |             sensorIt = sensors.erase(sensorIt); | 
 | 516 |         } | 
 | 517 |         else | 
 | 518 |         { | 
 | 519 |             sensorIt++; | 
 | 520 |         } | 
 | 521 |     } | 
 | 522 | } | 
 | 523 |  | 
| James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 524 | int main() | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 525 | { | 
 | 526 |     boost::asio::io_service io; | 
 | 527 |     auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); | 
 | 528 |     systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); | 
 | 529 |     sdbusplus::asio::object_server objectServer(systemBus); | 
| Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 530 |     boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>> | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 531 |         sensors; | 
 | 532 |     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; | 
| James Feist | 5591cf08 | 2020-07-15 16:44:54 -0700 | [diff] [blame] | 533 |     auto sensorsChanged = | 
 | 534 |         std::make_shared<boost::container::flat_set<std::string>>(); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 535 |  | 
 | 536 |     io.post([&]() { | 
 | 537 |         createSensors(io, objectServer, sensors, systemBus, nullptr); | 
 | 538 |     }); | 
 | 539 |  | 
 | 540 |     boost::asio::deadline_timer filterTimer(io); | 
 | 541 |     std::function<void(sdbusplus::message::message&)> eventHandler = | 
 | 542 |         [&](sdbusplus::message::message& message) { | 
 | 543 |             if (message.is_method_error()) | 
 | 544 |             { | 
 | 545 |                 std::cerr << "callback method error\n"; | 
 | 546 |                 return; | 
 | 547 |             } | 
 | 548 |             sensorsChanged->insert(message.get_path()); | 
 | 549 |             // this implicitly cancels the timer | 
 | 550 |             filterTimer.expires_from_now(boost::posix_time::seconds(1)); | 
 | 551 |  | 
 | 552 |             filterTimer.async_wait([&](const boost::system::error_code& ec) { | 
 | 553 |                 if (ec == boost::asio::error::operation_aborted) | 
 | 554 |                 { | 
 | 555 |                     /* we were canceled*/ | 
 | 556 |                     return; | 
 | 557 |                 } | 
| Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 558 |                 if (ec) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 559 |                 { | 
 | 560 |                     std::cerr << "timer error\n"; | 
 | 561 |                     return; | 
 | 562 |                 } | 
 | 563 |                 createSensors(io, objectServer, sensors, systemBus, | 
 | 564 |                               sensorsChanged); | 
 | 565 |             }); | 
 | 566 |         }; | 
 | 567 |  | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 568 |     for (const char* type : sensorTypes) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 569 |     { | 
 | 570 |         auto match = std::make_unique<sdbusplus::bus::match::match>( | 
 | 571 |             static_cast<sdbusplus::bus::bus&>(*systemBus), | 
 | 572 |             "type='signal',member='PropertiesChanged',path_namespace='" + | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 573 |                 std::string(inventoryPath) + "',arg0namespace='" + type + "'", | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 574 |             eventHandler); | 
 | 575 |         matches.emplace_back(std::move(match)); | 
 | 576 |     } | 
 | 577 |  | 
| Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 578 |     setupManufacturingModeMatch(*systemBus); | 
| Matt Spinler | 20bf2c1 | 2021-09-14 11:07:07 -0500 | [diff] [blame] | 579 |  | 
 | 580 |     // Watch for entity-manager to remove configuration interfaces | 
 | 581 |     // so the corresponding sensors can be removed. | 
 | 582 |     auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match::match>( | 
 | 583 |         static_cast<sdbusplus::bus::bus&>(*systemBus), | 
 | 584 |         "type='signal',member='InterfacesRemoved',arg0path='" + | 
 | 585 |             std::string(inventoryPath) + "/'", | 
 | 586 |         [&sensors](sdbusplus::message::message& msg) { | 
 | 587 |             interfaceRemoved(msg, sensors); | 
 | 588 |         }); | 
 | 589 |  | 
 | 590 |     matches.emplace_back(std::move(ifaceRemovedMatch)); | 
 | 591 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 592 |     io.run(); | 
 | 593 | } |