16714a25aSJames Feist /* 26714a25aSJames Feist // Copyright (c) 2017 Intel Corporation 36714a25aSJames Feist // 46714a25aSJames Feist // Licensed under the Apache License, Version 2.0 (the "License"); 56714a25aSJames Feist // you may not use this file except in compliance with the License. 66714a25aSJames Feist // You may obtain a copy of the License at 76714a25aSJames Feist // 86714a25aSJames Feist // http://www.apache.org/licenses/LICENSE-2.0 96714a25aSJames Feist // 106714a25aSJames Feist // Unless required by applicable law or agreed to in writing, software 116714a25aSJames Feist // distributed under the License is distributed on an "AS IS" BASIS, 126714a25aSJames Feist // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136714a25aSJames Feist // See the License for the specific language governing permissions and 146714a25aSJames Feist // limitations under the License. 156714a25aSJames Feist */ 166714a25aSJames Feist 17e73bd0a1SAndrew Jeffery #include "DeviceMgmt.hpp" 18e73bd0a1SAndrew Jeffery #include "HwmonTempSensor.hpp" 19e73bd0a1SAndrew Jeffery #include "Utils.hpp" 20e73bd0a1SAndrew Jeffery 216714a25aSJames Feist #include <boost/algorithm/string/replace.hpp> 2296e97db7SPatrick Venture #include <boost/container/flat_map.hpp> 236714a25aSJames Feist #include <boost/container/flat_set.hpp> 2438fb5983SJames Feist #include <sdbusplus/asio/connection.hpp> 2538fb5983SJames Feist #include <sdbusplus/asio/object_server.hpp> 2638fb5983SJames Feist #include <sdbusplus/bus/match.hpp> 2738fb5983SJames Feist 2838fb5983SJames Feist #include <array> 297dd6443bSJae Hyun Yoo #include <charconv> 3024f02f24SJames Feist #include <filesystem> 316714a25aSJames Feist #include <fstream> 3296e97db7SPatrick Venture #include <functional> 3396e97db7SPatrick Venture #include <memory> 346714a25aSJames Feist #include <regex> 3596e97db7SPatrick Venture #include <stdexcept> 3696e97db7SPatrick Venture #include <string> 3796e97db7SPatrick Venture #include <utility> 3896e97db7SPatrick Venture #include <variant> 3996e97db7SPatrick Venture #include <vector> 406714a25aSJames Feist 4187bc67f7SJeff Lin static constexpr float pollRateDefault = 0.5; 426714a25aSJames Feist 43544e7dc5SBruce Mitchell static constexpr double maxValuePressure = 120000; // Pascals 44544e7dc5SBruce Mitchell static constexpr double minValuePressure = 30000; // Pascals 45544e7dc5SBruce Mitchell 463ec41c53SBruce Mitchell static constexpr double maxValueRelativeHumidity = 100; // PercentRH 473ec41c53SBruce Mitchell static constexpr double minValueRelativeHumidity = 0; // PercentRH 483ec41c53SBruce Mitchell 49544e7dc5SBruce Mitchell static constexpr double maxValueTemperature = 127; // DegreesC 50544e7dc5SBruce Mitchell static constexpr double minValueTemperature = -128; // DegreesC 51544e7dc5SBruce Mitchell 52cf3bce6eSJames Feist namespace fs = std::filesystem; 53d29f8aa0SZev Weiss 54d29f8aa0SZev Weiss static const I2CDeviceTypeMap sensorTypes{ 5576d3676bSPatrick Rudolph {"ADM1021", I2CDeviceType{"adm1021", true}}, 56d29f8aa0SZev Weiss {"DPS310", I2CDeviceType{"dps310", false}}, 57d29f8aa0SZev Weiss {"EMC1412", I2CDeviceType{"emc1412", true}}, 58d29f8aa0SZev Weiss {"EMC1413", I2CDeviceType{"emc1413", true}}, 59d29f8aa0SZev Weiss {"EMC1414", I2CDeviceType{"emc1414", true}}, 60d29f8aa0SZev Weiss {"HDC1080", I2CDeviceType{"hdc1080", false}}, 61d29f8aa0SZev Weiss {"JC42", I2CDeviceType{"jc42", true}}, 62d29f8aa0SZev Weiss {"LM75A", I2CDeviceType{"lm75a", true}}, 63d29f8aa0SZev Weiss {"LM95234", I2CDeviceType{"lm95234", true}}, 64d29f8aa0SZev Weiss {"MAX31725", I2CDeviceType{"max31725", true}}, 65d29f8aa0SZev Weiss {"MAX31730", I2CDeviceType{"max31730", true}}, 66d29f8aa0SZev Weiss {"MAX6581", I2CDeviceType{"max6581", true}}, 67d29f8aa0SZev Weiss {"MAX6654", I2CDeviceType{"max6654", true}}, 68f3862ee3SPatrick Rudolph {"MAX6639", I2CDeviceType{"max6639", true}}, 6928b88233SPotin Lai {"MCP9600", I2CDeviceType{"mcp9600", false}}, 70d29f8aa0SZev Weiss {"NCT6779", I2CDeviceType{"nct6779", true}}, 71d29f8aa0SZev Weiss {"NCT7802", I2CDeviceType{"nct7802", true}}, 72*fb011cc4SPotin Lai {"PT5161L", I2CDeviceType{"pt5161l", true}}, 73d29f8aa0SZev Weiss {"SBTSI", I2CDeviceType{"sbtsi", true}}, 74d29f8aa0SZev Weiss {"SI7020", I2CDeviceType{"si7020", false}}, 759b0a6f67STim Lee {"TMP100", I2CDeviceType{"tmp100", true}}, 76d29f8aa0SZev Weiss {"TMP112", I2CDeviceType{"tmp112", true}}, 77d29f8aa0SZev Weiss {"TMP175", I2CDeviceType{"tmp175", true}}, 78d29f8aa0SZev Weiss {"TMP421", I2CDeviceType{"tmp421", true}}, 79d29f8aa0SZev Weiss {"TMP441", I2CDeviceType{"tmp441", true}}, 80f4c3fad0SKhang Kieu {"TMP461", I2CDeviceType{"tmp461", true}}, 819a7db6afSHieu Huynh {"TMP464", I2CDeviceType{"tmp464", true}}, 82d29f8aa0SZev Weiss {"TMP75", I2CDeviceType{"tmp75", true}}, 83d29f8aa0SZev Weiss {"W83773G", I2CDeviceType{"w83773g", true}}, 84d29f8aa0SZev Weiss }; 856714a25aSJames Feist 86544e7dc5SBruce Mitchell static struct SensorParams 87544e7dc5SBruce Mitchell getSensorParameters(const std::filesystem::path& path) 88544e7dc5SBruce Mitchell { 89544e7dc5SBruce Mitchell // offset is to default to 0 and scale to 1, see lore 90544e7dc5SBruce Mitchell // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ 91779c96a2SPatrick Williams struct SensorParams tmpSensorParameters = { 92779c96a2SPatrick Williams .minValue = minValueTemperature, 93544e7dc5SBruce Mitchell .maxValue = maxValueTemperature, 94544e7dc5SBruce Mitchell .offsetValue = 0.0, 95544e7dc5SBruce Mitchell .scaleValue = 1.0, 96779c96a2SPatrick Williams .units = sensor_paths::unitDegreesC, 97544e7dc5SBruce Mitchell .typeName = "temperature"}; 98544e7dc5SBruce Mitchell 99544e7dc5SBruce Mitchell // For IIO RAW sensors we get a raw_value, an offset, and scale 100544e7dc5SBruce Mitchell // to compute the value = (raw_value + offset) * scale 101544e7dc5SBruce Mitchell // with a _raw IIO device we need to get the 102544e7dc5SBruce Mitchell // offsetValue and scaleValue from the driver 103544e7dc5SBruce Mitchell // these are used to compute the reading in 104544e7dc5SBruce Mitchell // units that have yet to be scaled for D-Bus. 105544e7dc5SBruce Mitchell const std::string pathStr = path.string(); 106544e7dc5SBruce Mitchell if (pathStr.ends_with("_raw")) 107544e7dc5SBruce Mitchell { 108779c96a2SPatrick Williams std::string pathOffsetStr = pathStr.substr(0, pathStr.size() - 4) + 109779c96a2SPatrick Williams "_offset"; 110544e7dc5SBruce Mitchell std::optional<double> tmpOffsetValue = readFile(pathOffsetStr, 1.0); 111544e7dc5SBruce Mitchell // In case there is nothing to read skip this device 112544e7dc5SBruce Mitchell // This is not an error condition see lore 113544e7dc5SBruce Mitchell // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ 114544e7dc5SBruce Mitchell if (tmpOffsetValue) 115544e7dc5SBruce Mitchell { 116544e7dc5SBruce Mitchell tmpSensorParameters.offsetValue = *tmpOffsetValue; 117544e7dc5SBruce Mitchell } 118544e7dc5SBruce Mitchell 119779c96a2SPatrick Williams std::string pathScaleStr = pathStr.substr(0, pathStr.size() - 4) + 120779c96a2SPatrick Williams "_scale"; 121544e7dc5SBruce Mitchell std::optional<double> tmpScaleValue = readFile(pathScaleStr, 1.0); 122544e7dc5SBruce Mitchell // In case there is nothing to read skip this device 123544e7dc5SBruce Mitchell // This is not an error condition see lore 124544e7dc5SBruce Mitchell // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ 125544e7dc5SBruce Mitchell if (tmpScaleValue) 126544e7dc5SBruce Mitchell { 127544e7dc5SBruce Mitchell tmpSensorParameters.scaleValue = *tmpScaleValue; 128544e7dc5SBruce Mitchell } 129544e7dc5SBruce Mitchell } 130544e7dc5SBruce Mitchell 131544e7dc5SBruce Mitchell // Temperatures are read in milli degrees Celsius, we need 132544e7dc5SBruce Mitchell // degrees Celsius. Pressures are read in kilopascal, we need 133544e7dc5SBruce Mitchell // Pascals. On D-Bus for Open BMC we use the International 134544e7dc5SBruce Mitchell // System of Units without prefixes. Links to the kernel 135544e7dc5SBruce Mitchell // documentation: 136544e7dc5SBruce Mitchell // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface 137544e7dc5SBruce Mitchell // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio 138544e7dc5SBruce Mitchell if (path.filename() == "in_pressure_input" || 139544e7dc5SBruce Mitchell path.filename() == "in_pressure_raw") 140544e7dc5SBruce Mitchell { 141544e7dc5SBruce Mitchell tmpSensorParameters.minValue = minValuePressure; 142544e7dc5SBruce Mitchell tmpSensorParameters.maxValue = maxValuePressure; 143544e7dc5SBruce Mitchell // Pressures are read in kilopascal, we need Pascals. 144544e7dc5SBruce Mitchell tmpSensorParameters.scaleValue *= 1000.0; 145544e7dc5SBruce Mitchell tmpSensorParameters.typeName = "pressure"; 1465a86e562SBruce Mitchell tmpSensorParameters.units = sensor_paths::unitPascals; 147544e7dc5SBruce Mitchell } 1483ec41c53SBruce Mitchell else if (path.filename() == "in_humidityrelative_input" || 1493ec41c53SBruce Mitchell path.filename() == "in_humidityrelative_raw") 1503ec41c53SBruce Mitchell { 1513ec41c53SBruce Mitchell tmpSensorParameters.minValue = minValueRelativeHumidity; 1523ec41c53SBruce Mitchell tmpSensorParameters.maxValue = maxValueRelativeHumidity; 1533ec41c53SBruce Mitchell // Relative Humidity are read in milli-percent, we need percent. 1543ec41c53SBruce Mitchell tmpSensorParameters.scaleValue *= 0.001; 1553ec41c53SBruce Mitchell tmpSensorParameters.typeName = "humidity"; 1563ec41c53SBruce Mitchell tmpSensorParameters.units = "PercentRH"; 1573ec41c53SBruce Mitchell } 158544e7dc5SBruce Mitchell else 159544e7dc5SBruce Mitchell { 160544e7dc5SBruce Mitchell // Temperatures are read in milli degrees Celsius, 161544e7dc5SBruce Mitchell // we need degrees Celsius. 162544e7dc5SBruce Mitchell tmpSensorParameters.scaleValue *= 0.001; 163544e7dc5SBruce Mitchell } 164544e7dc5SBruce Mitchell 165544e7dc5SBruce Mitchell return tmpSensorParameters; 166544e7dc5SBruce Mitchell } 167544e7dc5SBruce Mitchell 1689f3a74edSJayashree Dhanapal struct SensorConfigKey 1699f3a74edSJayashree Dhanapal { 1709f3a74edSJayashree Dhanapal uint64_t bus; 1719f3a74edSJayashree Dhanapal uint64_t addr; 1729f3a74edSJayashree Dhanapal bool operator<(const SensorConfigKey& other) const 1739f3a74edSJayashree Dhanapal { 1749f3a74edSJayashree Dhanapal if (bus != other.bus) 1759f3a74edSJayashree Dhanapal { 1769f3a74edSJayashree Dhanapal return bus < other.bus; 1779f3a74edSJayashree Dhanapal } 1789f3a74edSJayashree Dhanapal return addr < other.addr; 1799f3a74edSJayashree Dhanapal } 1809f3a74edSJayashree Dhanapal }; 1819f3a74edSJayashree Dhanapal 1829f3a74edSJayashree Dhanapal struct SensorConfig 1839f3a74edSJayashree Dhanapal { 1849f3a74edSJayashree Dhanapal std::string sensorPath; 1859f3a74edSJayashree Dhanapal SensorData sensorData; 1869f3a74edSJayashree Dhanapal std::string interface; 1879f3a74edSJayashree Dhanapal SensorBaseConfigMap config; 1889f3a74edSJayashree Dhanapal std::vector<std::string> name; 1899f3a74edSJayashree Dhanapal }; 1909f3a74edSJayashree Dhanapal 1919f3a74edSJayashree Dhanapal using SensorConfigMap = 1929f3a74edSJayashree Dhanapal boost::container::flat_map<SensorConfigKey, SensorConfig>; 1939f3a74edSJayashree Dhanapal 1949f3a74edSJayashree Dhanapal static SensorConfigMap 1959f3a74edSJayashree Dhanapal buildSensorConfigMap(const ManagedObjectType& sensorConfigs) 1969f3a74edSJayashree Dhanapal { 1979f3a74edSJayashree Dhanapal SensorConfigMap configMap; 198a1808330SZev Weiss for (const auto& [path, cfgData] : sensorConfigs) 1999f3a74edSJayashree Dhanapal { 200a1808330SZev Weiss for (const auto& [intf, cfg] : cfgData) 2019f3a74edSJayashree Dhanapal { 2029f3a74edSJayashree Dhanapal auto busCfg = cfg.find("Bus"); 2039f3a74edSJayashree Dhanapal auto addrCfg = cfg.find("Address"); 2049f3a74edSJayashree Dhanapal if ((busCfg == cfg.end()) || (addrCfg == cfg.end())) 2059f3a74edSJayashree Dhanapal { 2069f3a74edSJayashree Dhanapal continue; 2079f3a74edSJayashree Dhanapal } 2089f3a74edSJayashree Dhanapal 2092049bd26SEd Tanous if ((std::get_if<uint64_t>(&busCfg->second) == nullptr) || 2102049bd26SEd Tanous (std::get_if<uint64_t>(&addrCfg->second) == nullptr)) 2119f3a74edSJayashree Dhanapal { 212a1808330SZev Weiss std::cerr << path.str << " Bus or Address invalid\n"; 2139f3a74edSJayashree Dhanapal continue; 2149f3a74edSJayashree Dhanapal } 2159f3a74edSJayashree Dhanapal 2169f3a74edSJayashree Dhanapal std::vector<std::string> hwmonNames; 2179f3a74edSJayashree Dhanapal auto nameCfg = cfg.find("Name"); 2189f3a74edSJayashree Dhanapal if (nameCfg != cfg.end()) 2199f3a74edSJayashree Dhanapal { 2209f3a74edSJayashree Dhanapal hwmonNames.push_back(std::get<std::string>(nameCfg->second)); 2219f3a74edSJayashree Dhanapal size_t i = 1; 2229f3a74edSJayashree Dhanapal while (true) 2239f3a74edSJayashree Dhanapal { 2249f3a74edSJayashree Dhanapal auto sensorNameCfg = cfg.find("Name" + std::to_string(i)); 2259f3a74edSJayashree Dhanapal if (sensorNameCfg == cfg.end()) 2269f3a74edSJayashree Dhanapal { 2279f3a74edSJayashree Dhanapal break; 2289f3a74edSJayashree Dhanapal } 2299f3a74edSJayashree Dhanapal hwmonNames.push_back( 2309f3a74edSJayashree Dhanapal std::get<std::string>(sensorNameCfg->second)); 2319f3a74edSJayashree Dhanapal i++; 2329f3a74edSJayashree Dhanapal } 2339f3a74edSJayashree Dhanapal } 2349f3a74edSJayashree Dhanapal 2359f3a74edSJayashree Dhanapal SensorConfigKey key = {std::get<uint64_t>(busCfg->second), 2369f3a74edSJayashree Dhanapal std::get<uint64_t>(addrCfg->second)}; 237a1808330SZev Weiss SensorConfig val = {path.str, cfgData, intf, cfg, hwmonNames}; 2389f3a74edSJayashree Dhanapal 2399f3a74edSJayashree Dhanapal auto [it, inserted] = configMap.emplace(key, std::move(val)); 2409f3a74edSJayashree Dhanapal if (!inserted) 2419f3a74edSJayashree Dhanapal { 242a1808330SZev Weiss std::cerr << path.str << ": ignoring duplicate entry for {" 243a1808330SZev Weiss << key.bus << ", 0x" << std::hex << key.addr 244a1808330SZev Weiss << std::dec << "}\n"; 2459f3a74edSJayashree Dhanapal } 2469f3a74edSJayashree Dhanapal } 2479f3a74edSJayashree Dhanapal } 2489f3a74edSJayashree Dhanapal return configMap; 2499f3a74edSJayashree Dhanapal } 2509f3a74edSJayashree Dhanapal 2516714a25aSJames Feist void createSensors( 2521f978631SEd Tanous boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, 253f3fd1915SYong Li boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& 2546714a25aSJames Feist sensors, 2556714a25aSJames Feist std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, 2565591cf08SJames Feist const std::shared_ptr<boost::container::flat_set<std::string>>& 257a1456c4aSZev Weiss sensorsChanged, 258a1456c4aSZev Weiss bool activateOnly) 2596714a25aSJames Feist { 260df515159SJames Feist auto getter = std::make_shared<GetSensorConfiguration>( 261df515159SJames Feist dbusConnection, 262a1456c4aSZev Weiss [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged, 263a1456c4aSZev Weiss activateOnly](const ManagedObjectType& sensorConfigurations) { 2646714a25aSJames Feist bool firstScan = sensorsChanged == nullptr; 265df515159SJames Feist 266bb67932aSEd Tanous SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations); 2679f3a74edSJayashree Dhanapal 268c564eda5SMatt Simmering auto devices = instantiateDevices(sensorConfigurations, sensors, 269c564eda5SMatt Simmering sensorTypes); 270a1456c4aSZev Weiss 271544e7dc5SBruce Mitchell // IIO _raw devices look like this on sysfs: 272544e7dc5SBruce Mitchell // /sys/bus/iio/devices/iio:device0/in_temp_raw 273544e7dc5SBruce Mitchell // /sys/bus/iio/devices/iio:device0/in_temp_offset 274544e7dc5SBruce Mitchell // /sys/bus/iio/devices/iio:device0/in_temp_scale 275544e7dc5SBruce Mitchell // 276544e7dc5SBruce Mitchell // Other IIO devices look like this on sysfs: 277544e7dc5SBruce Mitchell // /sys/bus/iio/devices/iio:device1/in_temp_input 278544e7dc5SBruce Mitchell // /sys/bus/iio/devices/iio:device1/in_pressure_input 2796714a25aSJames Feist std::vector<fs::path> paths; 280544e7dc5SBruce Mitchell fs::path root("/sys/bus/iio/devices"); 281544e7dc5SBruce Mitchell findFiles(root, R"(in_temp\d*_(input|raw))", paths); 282544e7dc5SBruce Mitchell findFiles(root, R"(in_pressure\d*_(input|raw))", paths); 2833ec41c53SBruce Mitchell findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths); 284544e7dc5SBruce Mitchell findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths); 285544e7dc5SBruce Mitchell 286544e7dc5SBruce Mitchell // iterate through all found temp and pressure sensors, 287544e7dc5SBruce Mitchell // and try to match them with configuration 2886714a25aSJames Feist for (auto& path : paths) 2896714a25aSJames Feist { 2906714a25aSJames Feist std::smatch match; 291544e7dc5SBruce Mitchell const std::string pathStr = path.string(); 2926714a25aSJames Feist auto directory = path.parent_path(); 293544e7dc5SBruce Mitchell fs::path device; 2946714a25aSJames Feist 295544e7dc5SBruce Mitchell std::string deviceName; 296ae463966SZhikui Ren std::error_code ec; 297544e7dc5SBruce Mitchell if (pathStr.starts_with("/sys/bus/iio/devices")) 2986714a25aSJames Feist { 299ae463966SZhikui Ren device = fs::canonical(directory, ec); 300ae463966SZhikui Ren if (ec) 301ae463966SZhikui Ren { 302ae463966SZhikui Ren std::cerr << "Fail to find device in path [" << pathStr 303ae463966SZhikui Ren << "]\n"; 304ae463966SZhikui Ren continue; 305ae463966SZhikui Ren } 306544e7dc5SBruce Mitchell deviceName = device.parent_path().stem(); 30737266ca9SJames Feist } 308544e7dc5SBruce Mitchell else 309544e7dc5SBruce Mitchell { 310ae463966SZhikui Ren device = fs::canonical(directory / "device", ec); 311ae463966SZhikui Ren if (ec) 312ae463966SZhikui Ren { 313ae463966SZhikui Ren std::cerr << "Fail to find device in path [" << pathStr 314ae463966SZhikui Ren << "]\n"; 315ae463966SZhikui Ren continue; 316ae463966SZhikui Ren } 317ae463966SZhikui Ren deviceName = device.stem(); 318544e7dc5SBruce Mitchell } 31937266ca9SJames Feist 320278e177fSTom Tung uint64_t bus = 0; 321278e177fSTom Tung uint64_t addr = 0; 32203d333e0SAkshit Shah if (!getDeviceBusAddr(deviceName, bus, addr)) 32337266ca9SJames Feist { 3246714a25aSJames Feist continue; 3256714a25aSJames Feist } 32637266ca9SJames Feist 327544e7dc5SBruce Mitchell auto thisSensorParameters = getSensorParameters(path); 3289f3a74edSJayashree Dhanapal auto findSensorCfg = configMap.find({bus, addr}); 3299f3a74edSJayashree Dhanapal if (findSensorCfg == configMap.end()) 33037266ca9SJames Feist { 33137266ca9SJames Feist continue; 33237266ca9SJames Feist } 33337266ca9SJames Feist 334bb67932aSEd Tanous const std::string& interfacePath = findSensorCfg->second.sensorPath; 3357627c860SZev Weiss auto findI2CDev = devices.find(interfacePath); 3367627c860SZev Weiss 337a1456c4aSZev Weiss std::shared_ptr<I2CDevice> i2cDev; 3387627c860SZev Weiss if (findI2CDev != devices.end()) 339a1456c4aSZev Weiss { 3409bbeff75SJoseph Fu // If we're only looking to activate newly-instantiated i2c 3419bbeff75SJoseph Fu // devices and this sensor's underlying device was already there 3429bbeff75SJoseph Fu // before this call, there's nothing more to do here. 3439bbeff75SJoseph Fu if (activateOnly && !findI2CDev->second.second) 3449bbeff75SJoseph Fu { 3459bbeff75SJoseph Fu continue; 3469bbeff75SJoseph Fu } 3477627c860SZev Weiss i2cDev = findI2CDev->second.first; 348a1456c4aSZev Weiss } 349a1456c4aSZev Weiss 3509f3a74edSJayashree Dhanapal const SensorData& sensorData = findSensorCfg->second.sensorData; 3510489ec20SMatt Spinler std::string sensorType = findSensorCfg->second.interface; 3520489ec20SMatt Spinler auto pos = sensorType.find_last_of('.'); 3530489ec20SMatt Spinler if (pos != std::string::npos) 3540489ec20SMatt Spinler { 3550489ec20SMatt Spinler sensorType = sensorType.substr(pos + 1); 3560489ec20SMatt Spinler } 3579f3a74edSJayashree Dhanapal const SensorBaseConfigMap& baseConfigMap = 3589f3a74edSJayashree Dhanapal findSensorCfg->second.config; 359bb67932aSEd Tanous std::vector<std::string>& hwmonName = findSensorCfg->second.name; 3606714a25aSJames Feist 361544e7dc5SBruce Mitchell // Temperature has "Name", pressure has "Name1" 3629f3a74edSJayashree Dhanapal auto findSensorName = baseConfigMap.find("Name"); 363fefdbe7fSPotin Lai int index = 1; 3643ec41c53SBruce Mitchell if (thisSensorParameters.typeName == "pressure" || 3653ec41c53SBruce Mitchell thisSensorParameters.typeName == "humidity") 366544e7dc5SBruce Mitchell { 3679f3a74edSJayashree Dhanapal findSensorName = baseConfigMap.find("Name1"); 368fefdbe7fSPotin Lai index = 2; 369544e7dc5SBruce Mitchell } 370544e7dc5SBruce Mitchell 3719f3a74edSJayashree Dhanapal if (findSensorName == baseConfigMap.end()) 3726714a25aSJames Feist { 3736714a25aSJames Feist std::cerr << "could not determine configuration name for " 37437266ca9SJames Feist << deviceName << "\n"; 3756714a25aSJames Feist continue; 3766714a25aSJames Feist } 377df515159SJames Feist std::string sensorName = 378df515159SJames Feist std::get<std::string>(findSensorName->second); 3796714a25aSJames Feist // on rescans, only update sensors we were signaled by 3806714a25aSJames Feist auto findSensor = sensors.find(sensorName); 3816714a25aSJames Feist if (!firstScan && findSensor != sensors.end()) 3826714a25aSJames Feist { 3836714a25aSJames Feist bool found = false; 384d653b75cSBruce Mitchell auto it = sensorsChanged->begin(); 385d653b75cSBruce Mitchell while (it != sensorsChanged->end()) 3866714a25aSJames Feist { 3876c106d66SZev Weiss if (it->ends_with(findSensor->second->name)) 3886714a25aSJames Feist { 389d653b75cSBruce Mitchell it = sensorsChanged->erase(it); 3906714a25aSJames Feist findSensor->second = nullptr; 3916714a25aSJames Feist found = true; 3926714a25aSJames Feist break; 3936714a25aSJames Feist } 394d653b75cSBruce Mitchell ++it; 3956714a25aSJames Feist } 3966714a25aSJames Feist if (!found) 3976714a25aSJames Feist { 3986714a25aSJames Feist continue; 3996714a25aSJames Feist } 4006714a25aSJames Feist } 4015636d52bSMatt Spinler 4026714a25aSJames Feist std::vector<thresholds::Threshold> sensorThresholds; 4035636d52bSMatt Spinler 4049f3a74edSJayashree Dhanapal if (!parseThresholdsFromConfig(sensorData, sensorThresholds, 4055636d52bSMatt Spinler nullptr, &index)) 4066714a25aSJames Feist { 407bb67932aSEd Tanous std::cerr << "error populating thresholds for " << sensorName 408bb67932aSEd Tanous << " index " << index << "\n"; 4096714a25aSJames Feist } 41087bc67f7SJeff Lin 4118569bf2aSZev Weiss float pollRate = getPollRate(baseConfigMap, pollRateDefault); 412a4d2768cSZev Weiss PowerState readState = getPowerState(baseConfigMap); 413100c20bfSJason Ling 4149f3a74edSJayashree Dhanapal auto permitSet = getPermitSet(baseConfigMap); 4158b3f7d40SAlex Qiu auto& sensor = sensors[sensorName]; 416a1456c4aSZev Weiss if (!activateOnly) 417a1456c4aSZev Weiss { 4188b3f7d40SAlex Qiu sensor = nullptr; 419a1456c4aSZev Weiss } 420779c96a2SPatrick Williams auto hwmonFile = getFullHwmonFilePath(directory.string(), "temp1", 421779c96a2SPatrick Williams permitSet); 422544e7dc5SBruce Mitchell if (pathStr.starts_with("/sys/bus/iio/devices")) 423544e7dc5SBruce Mitchell { 424544e7dc5SBruce Mitchell hwmonFile = pathStr; 425544e7dc5SBruce Mitchell } 426100c20bfSJason Ling if (hwmonFile) 427100c20bfSJason Ling { 428a1456c4aSZev Weiss if (sensor != nullptr) 429a1456c4aSZev Weiss { 430a1456c4aSZev Weiss sensor->activate(*hwmonFile, i2cDev); 431a1456c4aSZev Weiss } 432a1456c4aSZev Weiss else 433a1456c4aSZev Weiss { 434f3fd1915SYong Li sensor = std::make_shared<HwmonTempSensor>( 435a1456c4aSZev Weiss *hwmonFile, sensorType, objectServer, dbusConnection, 436a1456c4aSZev Weiss io, sensorName, std::move(sensorThresholds), 437a1456c4aSZev Weiss thisSensorParameters, pollRate, interfacePath, 438a1456c4aSZev Weiss readState, i2cDev); 439f3fd1915SYong Li sensor->setupRead(); 4409eb0cc3dSWilly Tu } 441a1456c4aSZev Weiss } 4429f3a74edSJayashree Dhanapal hwmonName.erase( 4439f3a74edSJayashree Dhanapal remove(hwmonName.begin(), hwmonName.end(), sensorName), 4449f3a74edSJayashree Dhanapal hwmonName.end()); 4459eb0cc3dSWilly Tu 4468b3f7d40SAlex Qiu // Looking for keys like "Name1" for temp2_input, 4478b3f7d40SAlex Qiu // "Name2" for temp3_input, etc. 4488b3f7d40SAlex Qiu int i = 0; 4498b3f7d40SAlex Qiu while (true) 45037266ca9SJames Feist { 4518b3f7d40SAlex Qiu ++i; 452bb67932aSEd Tanous auto findKey = baseConfigMap.find("Name" + std::to_string(i)); 4539f3a74edSJayashree Dhanapal if (findKey == baseConfigMap.end()) 4548b3f7d40SAlex Qiu { 4558b3f7d40SAlex Qiu break; 45637266ca9SJames Feist } 457bb67932aSEd Tanous std::string sensorName = std::get<std::string>(findKey->second); 458bb67932aSEd Tanous hwmonFile = getFullHwmonFilePath(directory.string(), 459bb67932aSEd Tanous "temp" + std::to_string(i + 1), 460100c20bfSJason Ling permitSet); 461544e7dc5SBruce Mitchell if (pathStr.starts_with("/sys/bus/iio/devices")) 462544e7dc5SBruce Mitchell { 463544e7dc5SBruce Mitchell continue; 464544e7dc5SBruce Mitchell } 465100c20bfSJason Ling if (hwmonFile) 466100c20bfSJason Ling { 4675636d52bSMatt Spinler // To look up thresholds for these additional sensors, 4685636d52bSMatt Spinler // match on the Index property in the threshold data 4695636d52bSMatt Spinler // where the index comes from the sysfs file we're on, 4705636d52bSMatt Spinler // i.e. index = 2 for temp2_input. 4715636d52bSMatt Spinler int index = i + 1; 4725636d52bSMatt Spinler std::vector<thresholds::Threshold> thresholds; 4735636d52bSMatt Spinler 4749f3a74edSJayashree Dhanapal if (!parseThresholdsFromConfig(sensorData, thresholds, 4755636d52bSMatt Spinler nullptr, &index)) 4765636d52bSMatt Spinler { 4775636d52bSMatt Spinler std::cerr << "error populating thresholds for " 478bb67932aSEd Tanous << sensorName << " index " << index << "\n"; 4795636d52bSMatt Spinler } 4805636d52bSMatt Spinler 4818b3f7d40SAlex Qiu auto& sensor = sensors[sensorName]; 482a1456c4aSZev Weiss if (!activateOnly) 483a1456c4aSZev Weiss { 4848b3f7d40SAlex Qiu sensor = nullptr; 485a1456c4aSZev Weiss } 486a1456c4aSZev Weiss 487a1456c4aSZev Weiss if (sensor != nullptr) 488a1456c4aSZev Weiss { 489a1456c4aSZev Weiss sensor->activate(*hwmonFile, i2cDev); 490a1456c4aSZev Weiss } 491a1456c4aSZev Weiss else 492a1456c4aSZev Weiss { 493f3fd1915SYong Li sensor = std::make_shared<HwmonTempSensor>( 494a1456c4aSZev Weiss *hwmonFile, sensorType, objectServer, 495a1456c4aSZev Weiss dbusConnection, io, sensorName, 496a1456c4aSZev Weiss std::move(thresholds), thisSensorParameters, 497a1456c4aSZev Weiss pollRate, interfacePath, readState, i2cDev); 498f3fd1915SYong Li sensor->setupRead(); 4998b3f7d40SAlex Qiu } 500a1456c4aSZev Weiss } 5019eb0cc3dSWilly Tu 5029eb0cc3dSWilly Tu hwmonName.erase( 5039eb0cc3dSWilly Tu remove(hwmonName.begin(), hwmonName.end(), sensorName), 5049eb0cc3dSWilly Tu hwmonName.end()); 5056714a25aSJames Feist } 50631ec7dbbSMatt Spinler if (hwmonName.empty()) 50731ec7dbbSMatt Spinler { 5089f3a74edSJayashree Dhanapal configMap.erase(findSensorCfg); 5099f3a74edSJayashree Dhanapal } 51031ec7dbbSMatt Spinler } 5118a17c303SEd Tanous }); 512d29f8aa0SZev Weiss std::vector<std::string> types(sensorTypes.size()); 513d29f8aa0SZev Weiss for (const auto& [type, dt] : sensorTypes) 514d29f8aa0SZev Weiss { 515d29f8aa0SZev Weiss types.push_back(type); 516d29f8aa0SZev Weiss } 517d29f8aa0SZev Weiss getter->getConfiguration(types); 5186714a25aSJames Feist } 5196714a25aSJames Feist 52020bf2c1cSMatt Spinler void interfaceRemoved( 52192f8f515SPatrick Williams sdbusplus::message_t& message, 52220bf2c1cSMatt Spinler boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& 52320bf2c1cSMatt Spinler sensors) 52420bf2c1cSMatt Spinler { 52520bf2c1cSMatt Spinler if (message.is_method_error()) 52620bf2c1cSMatt Spinler { 52720bf2c1cSMatt Spinler std::cerr << "interfacesRemoved callback method error\n"; 52820bf2c1cSMatt Spinler return; 52920bf2c1cSMatt Spinler } 53020bf2c1cSMatt Spinler 53120bf2c1cSMatt Spinler sdbusplus::message::object_path path; 53220bf2c1cSMatt Spinler std::vector<std::string> interfaces; 53320bf2c1cSMatt Spinler 53420bf2c1cSMatt Spinler message.read(path, interfaces); 53520bf2c1cSMatt Spinler 53620bf2c1cSMatt Spinler // If the xyz.openbmc_project.Confguration.X interface was removed 53720bf2c1cSMatt Spinler // for one or more sensors, delete those sensor objects. 53820bf2c1cSMatt Spinler auto sensorIt = sensors.begin(); 53920bf2c1cSMatt Spinler while (sensorIt != sensors.end()) 54020bf2c1cSMatt Spinler { 5416289f5a2SPatrick Rudolph if (sensorIt->second && (sensorIt->second->configurationPath == path) && 54220bf2c1cSMatt Spinler (std::find(interfaces.begin(), interfaces.end(), 54355832f37SMatt Spinler sensorIt->second->configInterface) != interfaces.end())) 54420bf2c1cSMatt Spinler { 54520bf2c1cSMatt Spinler sensorIt = sensors.erase(sensorIt); 54620bf2c1cSMatt Spinler } 54720bf2c1cSMatt Spinler else 54820bf2c1cSMatt Spinler { 54920bf2c1cSMatt Spinler sensorIt++; 55020bf2c1cSMatt Spinler } 55120bf2c1cSMatt Spinler } 55220bf2c1cSMatt Spinler } 55320bf2c1cSMatt Spinler 554a1456c4aSZev Weiss static void powerStateChanged( 555a1456c4aSZev Weiss PowerState type, bool newState, 556a1456c4aSZev Weiss boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>& 557a1456c4aSZev Weiss sensors, 5581f978631SEd Tanous boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, 559a1456c4aSZev Weiss std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) 560a1456c4aSZev Weiss { 561a1456c4aSZev Weiss if (newState) 562a1456c4aSZev Weiss { 563a1456c4aSZev Weiss createSensors(io, objectServer, sensors, dbusConnection, nullptr, true); 564a1456c4aSZev Weiss } 565a1456c4aSZev Weiss else 566a1456c4aSZev Weiss { 567a1456c4aSZev Weiss for (auto& [path, sensor] : sensors) 568a1456c4aSZev Weiss { 569a1456c4aSZev Weiss if (sensor != nullptr && sensor->readState == type) 570a1456c4aSZev Weiss { 571a1456c4aSZev Weiss sensor->deactivate(); 572a1456c4aSZev Weiss } 573a1456c4aSZev Weiss } 574a1456c4aSZev Weiss } 575a1456c4aSZev Weiss } 576a1456c4aSZev Weiss 577b6c0b914SJames Feist int main() 5786714a25aSJames Feist { 5791f978631SEd Tanous boost::asio::io_context io; 5806714a25aSJames Feist auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); 58114ed5e99SEd Tanous sdbusplus::asio::object_server objectServer(systemBus, true); 58214ed5e99SEd Tanous objectServer.add_manager("/xyz/openbmc_project/sensors"); 5836714a25aSJames Feist systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); 58414ed5e99SEd Tanous 585f3fd1915SYong Li boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>> 5866714a25aSJames Feist sensors; 5875591cf08SJames Feist auto sensorsChanged = 5885591cf08SJames Feist std::make_shared<boost::container::flat_set<std::string>>(); 5896714a25aSJames Feist 590a1456c4aSZev Weiss auto powerCallBack = [&sensors, &io, &objectServer, 591a1456c4aSZev Weiss &systemBus](PowerState type, bool state) { 592a1456c4aSZev Weiss powerStateChanged(type, state, sensors, io, objectServer, systemBus); 593a1456c4aSZev Weiss }; 594a1456c4aSZev Weiss setupPowerMatchCallback(systemBus, powerCallBack); 595a1456c4aSZev Weiss 59683db50caSEd Tanous boost::asio::post(io, [&]() { 597a1456c4aSZev Weiss createSensors(io, objectServer, sensors, systemBus, nullptr, false); 5986714a25aSJames Feist }); 5996714a25aSJames Feist 6009b4a20e9SEd Tanous boost::asio::steady_timer filterTimer(io); 60192f8f515SPatrick Williams std::function<void(sdbusplus::message_t&)> eventHandler = 60292f8f515SPatrick Williams [&](sdbusplus::message_t& message) { 6036714a25aSJames Feist if (message.is_method_error()) 6046714a25aSJames Feist { 6056714a25aSJames Feist std::cerr << "callback method error\n"; 6066714a25aSJames Feist return; 6076714a25aSJames Feist } 6086714a25aSJames Feist sensorsChanged->insert(message.get_path()); 6096714a25aSJames Feist // this implicitly cancels the timer 61083db50caSEd Tanous filterTimer.expires_after(std::chrono::seconds(1)); 6116714a25aSJames Feist 6126714a25aSJames Feist filterTimer.async_wait([&](const boost::system::error_code& ec) { 6136714a25aSJames Feist if (ec == boost::asio::error::operation_aborted) 6146714a25aSJames Feist { 6156714a25aSJames Feist /* we were canceled*/ 6166714a25aSJames Feist return; 6176714a25aSJames Feist } 6188a57ec09SEd Tanous if (ec) 6196714a25aSJames Feist { 6206714a25aSJames Feist std::cerr << "timer error\n"; 6216714a25aSJames Feist return; 6226714a25aSJames Feist } 623a1456c4aSZev Weiss createSensors(io, objectServer, sensors, systemBus, sensorsChanged, 624a1456c4aSZev Weiss false); 6256714a25aSJames Feist }); 6266714a25aSJames Feist }; 6276714a25aSJames Feist 628214d9717SZev Weiss std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = 629214d9717SZev Weiss setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); 6301263c3daSBruce Lee setupManufacturingModeMatch(*systemBus); 63120bf2c1cSMatt Spinler 63220bf2c1cSMatt Spinler // Watch for entity-manager to remove configuration interfaces 63320bf2c1cSMatt Spinler // so the corresponding sensors can be removed. 63492f8f515SPatrick Williams auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match_t>( 63592f8f515SPatrick Williams static_cast<sdbusplus::bus_t&>(*systemBus), 63620bf2c1cSMatt Spinler "type='signal',member='InterfacesRemoved',arg0path='" + 63720bf2c1cSMatt Spinler std::string(inventoryPath) + "/'", 63892f8f515SPatrick Williams [&sensors](sdbusplus::message_t& msg) { 63920bf2c1cSMatt Spinler interfaceRemoved(msg, sensors); 64020bf2c1cSMatt Spinler }); 64120bf2c1cSMatt Spinler 64220bf2c1cSMatt Spinler matches.emplace_back(std::move(ifaceRemovedMatch)); 64320bf2c1cSMatt Spinler 6446714a25aSJames Feist io.run(); 6456714a25aSJames Feist } 646