/* // Copyright (c) 2017 Intel Corporation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static constexpr float pollRateDefault = 0.5; static constexpr double maxValuePressure = 120000; // Pascals static constexpr double minValuePressure = 30000; // Pascals static constexpr double maxValueRelativeHumidity = 100; // PercentRH static constexpr double minValueRelativeHumidity = 0; // PercentRH static constexpr double maxValueTemperature = 127; // DegreesC static constexpr double minValueTemperature = -128; // DegreesC namespace fs = std::filesystem; static auto sensorTypes{ std::to_array({"xyz.openbmc_project.Configuration.DPS310", "xyz.openbmc_project.Configuration.EMC1412", "xyz.openbmc_project.Configuration.EMC1413", "xyz.openbmc_project.Configuration.EMC1414", "xyz.openbmc_project.Configuration.HDC1080", "xyz.openbmc_project.Configuration.JC42", "xyz.openbmc_project.Configuration.LM75A", "xyz.openbmc_project.Configuration.LM95234", "xyz.openbmc_project.Configuration.MAX31725", "xyz.openbmc_project.Configuration.MAX31730", "xyz.openbmc_project.Configuration.MAX6581", "xyz.openbmc_project.Configuration.MAX6654", "xyz.openbmc_project.Configuration.NCT7802", "xyz.openbmc_project.Configuration.NCT6779", "xyz.openbmc_project.Configuration.SBTSI", "xyz.openbmc_project.Configuration.SI7020", "xyz.openbmc_project.Configuration.TMP112", "xyz.openbmc_project.Configuration.TMP175", "xyz.openbmc_project.Configuration.TMP421", "xyz.openbmc_project.Configuration.TMP441", "xyz.openbmc_project.Configuration.TMP75", "xyz.openbmc_project.Configuration.W83773G"})}; static struct SensorParams getSensorParameters(const std::filesystem::path& path) { // offset is to default to 0 and scale to 1, see lore // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ struct SensorParams tmpSensorParameters = {.minValue = minValueTemperature, .maxValue = maxValueTemperature, .offsetValue = 0.0, .scaleValue = 1.0, .units = sensor_paths::unitDegreesC, .typeName = "temperature"}; // For IIO RAW sensors we get a raw_value, an offset, and scale // to compute the value = (raw_value + offset) * scale // with a _raw IIO device we need to get the // offsetValue and scaleValue from the driver // these are used to compute the reading in // units that have yet to be scaled for D-Bus. const std::string pathStr = path.string(); if (pathStr.ends_with("_raw")) { std::string pathOffsetStr = pathStr.substr(0, pathStr.size() - 4) + "_offset"; std::optional tmpOffsetValue = readFile(pathOffsetStr, 1.0); // In case there is nothing to read skip this device // This is not an error condition see lore // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ if (tmpOffsetValue) { tmpSensorParameters.offsetValue = *tmpOffsetValue; } std::string pathScaleStr = pathStr.substr(0, pathStr.size() - 4) + "_scale"; std::optional tmpScaleValue = readFile(pathScaleStr, 1.0); // In case there is nothing to read skip this device // This is not an error condition see lore // https://lore.kernel.org/linux-iio/5c79425f-6e88-36b6-cdfe-4080738d039f@metafoo.de/ if (tmpScaleValue) { tmpSensorParameters.scaleValue = *tmpScaleValue; } } // Temperatures are read in milli degrees Celsius, we need // degrees Celsius. Pressures are read in kilopascal, we need // Pascals. On D-Bus for Open BMC we use the International // System of Units without prefixes. Links to the kernel // documentation: // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio if (path.filename() == "in_pressure_input" || path.filename() == "in_pressure_raw") { tmpSensorParameters.minValue = minValuePressure; tmpSensorParameters.maxValue = maxValuePressure; // Pressures are read in kilopascal, we need Pascals. tmpSensorParameters.scaleValue *= 1000.0; tmpSensorParameters.typeName = "pressure"; tmpSensorParameters.units = sensor_paths::unitPascals; } else if (path.filename() == "in_humidityrelative_input" || path.filename() == "in_humidityrelative_raw") { tmpSensorParameters.minValue = minValueRelativeHumidity; tmpSensorParameters.maxValue = maxValueRelativeHumidity; // Relative Humidity are read in milli-percent, we need percent. tmpSensorParameters.scaleValue *= 0.001; tmpSensorParameters.typeName = "humidity"; tmpSensorParameters.units = "PercentRH"; } else { // Temperatures are read in milli degrees Celsius, // we need degrees Celsius. tmpSensorParameters.scaleValue *= 0.001; } return tmpSensorParameters; } struct SensorConfigKey { uint64_t bus; uint64_t addr; bool operator<(const SensorConfigKey& other) const { if (bus != other.bus) { return bus < other.bus; } return addr < other.addr; } }; struct SensorConfig { std::string sensorPath; SensorData sensorData; std::string interface; SensorBaseConfigMap config; std::vector name; }; using SensorConfigMap = boost::container::flat_map; static SensorConfigMap buildSensorConfigMap(const ManagedObjectType& sensorConfigs) { SensorConfigMap configMap; for (const std::pair& sensor : sensorConfigs) { for (const std::pair& cfgmap : sensor.second) { const SensorBaseConfigMap& cfg = cfgmap.second; auto busCfg = cfg.find("Bus"); auto addrCfg = cfg.find("Address"); if ((busCfg == cfg.end()) || (addrCfg == cfg.end())) { continue; } if ((std::get_if(&busCfg->second) == nullptr) || (std::get_if(&addrCfg->second) == nullptr)) { std::cerr << sensor.first.str << " Bus or Address invalid\n"; continue; } std::vector hwmonNames; auto nameCfg = cfg.find("Name"); if (nameCfg != cfg.end()) { hwmonNames.push_back(std::get(nameCfg->second)); size_t i = 1; while (true) { auto sensorNameCfg = cfg.find("Name" + std::to_string(i)); if (sensorNameCfg == cfg.end()) { break; } hwmonNames.push_back( std::get(sensorNameCfg->second)); i++; } } SensorConfigKey key = {std::get(busCfg->second), std::get(addrCfg->second)}; SensorConfig val = {sensor.first.str, sensor.second, cfgmap.first, cfg, hwmonNames}; auto [it, inserted] = configMap.emplace(key, std::move(val)); if (!inserted) { std::cerr << sensor.first.str << ": ignoring duplicate entry for {" << key.bus << ", 0x" << std::hex << key.addr << std::dec << "}\n"; } } } return configMap; } void createSensors( boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, boost::container::flat_map>& sensors, std::shared_ptr& dbusConnection, const std::shared_ptr>& sensorsChanged) { auto getter = std::make_shared( dbusConnection, [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged](const ManagedObjectType& sensorConfigurations) { bool firstScan = sensorsChanged == nullptr; SensorConfigMap configMap = buildSensorConfigMap(sensorConfigurations); // IIO _raw devices look like this on sysfs: // /sys/bus/iio/devices/iio:device0/in_temp_raw // /sys/bus/iio/devices/iio:device0/in_temp_offset // /sys/bus/iio/devices/iio:device0/in_temp_scale // // Other IIO devices look like this on sysfs: // /sys/bus/iio/devices/iio:device1/in_temp_input // /sys/bus/iio/devices/iio:device1/in_pressure_input std::vector paths; fs::path root("/sys/bus/iio/devices"); findFiles(root, R"(in_temp\d*_(input|raw))", paths); findFiles(root, R"(in_pressure\d*_(input|raw))", paths); findFiles(root, R"(in_humidityrelative\d*_(input|raw))", paths); findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths); if (paths.empty()) { return; } // iterate through all found temp and pressure sensors, // and try to match them with configuration for (auto& path : paths) { std::smatch match; const std::string pathStr = path.string(); auto directory = path.parent_path(); fs::path device; std::string deviceName; if (pathStr.starts_with("/sys/bus/iio/devices")) { device = fs::canonical(directory); deviceName = device.parent_path().stem(); } else { device = directory / "device"; deviceName = fs::canonical(device).stem(); } auto findHyphen = deviceName.find('-'); if (findHyphen == std::string::npos) { std::cerr << "found bad device " << deviceName << "\n"; continue; } std::string busStr = deviceName.substr(0, findHyphen); std::string addrStr = deviceName.substr(findHyphen + 1); uint64_t bus = 0; uint64_t addr = 0; std::from_chars_result res{}; res = std::from_chars(busStr.data(), busStr.data() + busStr.size(), bus); if (res.ec != std::errc{}) { continue; } res = std::from_chars(addrStr.data(), addrStr.data() + addrStr.size(), addr, 16); if (res.ec != std::errc{}) { continue; } auto thisSensorParameters = getSensorParameters(path); auto findSensorCfg = configMap.find({bus, addr}); if (findSensorCfg == configMap.end()) { continue; } const std::string& interfacePath = findSensorCfg->second.sensorPath; const SensorData& sensorData = findSensorCfg->second.sensorData; const std::string& sensorType = findSensorCfg->second.interface; const SensorBaseConfigMap& baseConfigMap = findSensorCfg->second.config; std::vector& hwmonName = findSensorCfg->second.name; // Temperature has "Name", pressure has "Name1" auto findSensorName = baseConfigMap.find("Name"); int index = 1; if (thisSensorParameters.typeName == "pressure" || thisSensorParameters.typeName == "humidity") { findSensorName = baseConfigMap.find("Name1"); index = 2; } if (findSensorName == baseConfigMap.end()) { std::cerr << "could not determine configuration name for " << deviceName << "\n"; continue; } std::string sensorName = std::get(findSensorName->second); // on rescans, only update sensors we were signaled by auto findSensor = sensors.find(sensorName); if (!firstScan && findSensor != sensors.end()) { bool found = false; auto it = sensorsChanged->begin(); while (it != sensorsChanged->end()) { if (boost::ends_with(*it, findSensor->second->name)) { it = sensorsChanged->erase(it); findSensor->second = nullptr; found = true; break; } ++it; } if (!found) { continue; } } std::vector sensorThresholds; if (!parseThresholdsFromConfig(sensorData, sensorThresholds, nullptr, &index)) { std::cerr << "error populating thresholds for " << sensorName << " index " << index << "\n"; } auto findPollRate = baseConfigMap.find("PollRate"); float pollRate = pollRateDefault; if (findPollRate != baseConfigMap.end()) { pollRate = std::visit(VariantToFloatVisitor(), findPollRate->second); if (pollRate <= 0.0F) { pollRate = pollRateDefault; // polling time too short } } PowerState readState = getPowerState(baseConfigMap); auto permitSet = getPermitSet(baseConfigMap); auto& sensor = sensors[sensorName]; sensor = nullptr; auto hwmonFile = getFullHwmonFilePath(directory.string(), "temp1", permitSet); if (pathStr.starts_with("/sys/bus/iio/devices")) { hwmonFile = pathStr; } if (hwmonFile) { sensor = std::make_shared( *hwmonFile, sensorType, objectServer, dbusConnection, io, sensorName, std::move(sensorThresholds), thisSensorParameters, pollRate, interfacePath, readState); sensor->setupRead(); } hwmonName.erase( remove(hwmonName.begin(), hwmonName.end(), sensorName), hwmonName.end()); // Looking for keys like "Name1" for temp2_input, // "Name2" for temp3_input, etc. int i = 0; while (true) { ++i; auto findKey = baseConfigMap.find("Name" + std::to_string(i)); if (findKey == baseConfigMap.end()) { break; } std::string sensorName = std::get(findKey->second); hwmonFile = getFullHwmonFilePath(directory.string(), "temp" + std::to_string(i + 1), permitSet); if (pathStr.starts_with("/sys/bus/iio/devices")) { continue; } if (hwmonFile) { // To look up thresholds for these additional sensors, // match on the Index property in the threshold data // where the index comes from the sysfs file we're on, // i.e. index = 2 for temp2_input. int index = i + 1; std::vector thresholds; if (!parseThresholdsFromConfig(sensorData, thresholds, nullptr, &index)) { std::cerr << "error populating thresholds for " << sensorName << " index " << index << "\n"; } auto& sensor = sensors[sensorName]; sensor = nullptr; sensor = std::make_shared( *hwmonFile, sensorType, objectServer, dbusConnection, io, sensorName, std::move(thresholds), thisSensorParameters, pollRate, interfacePath, readState); sensor->setupRead(); } hwmonName.erase( remove(hwmonName.begin(), hwmonName.end(), sensorName), hwmonName.end()); } if (hwmonName.empty()) { configMap.erase(findSensorCfg); } } }); getter->getConfiguration( std::vector(sensorTypes.begin(), sensorTypes.end())); } void interfaceRemoved( sdbusplus::message_t& message, boost::container::flat_map>& sensors) { if (message.is_method_error()) { std::cerr << "interfacesRemoved callback method error\n"; return; } sdbusplus::message::object_path path; std::vector interfaces; message.read(path, interfaces); // If the xyz.openbmc_project.Confguration.X interface was removed // for one or more sensors, delete those sensor objects. auto sensorIt = sensors.begin(); while (sensorIt != sensors.end()) { if ((sensorIt->second->configurationPath == path) && (std::find(interfaces.begin(), interfaces.end(), sensorIt->second->objectType) != interfaces.end())) { sensorIt = sensors.erase(sensorIt); } else { sensorIt++; } } } int main() { boost::asio::io_service io; auto systemBus = std::make_shared(io); systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); sdbusplus::asio::object_server objectServer(systemBus); boost::container::flat_map> sensors; auto sensorsChanged = std::make_shared>(); io.post([&]() { createSensors(io, objectServer, sensors, systemBus, nullptr); }); boost::asio::deadline_timer filterTimer(io); std::function eventHandler = [&](sdbusplus::message_t& message) { if (message.is_method_error()) { std::cerr << "callback method error\n"; return; } sensorsChanged->insert(message.get_path()); // this implicitly cancels the timer filterTimer.expires_from_now(boost::posix_time::seconds(1)); filterTimer.async_wait([&](const boost::system::error_code& ec) { if (ec == boost::asio::error::operation_aborted) { /* we were canceled*/ return; } if (ec) { std::cerr << "timer error\n"; return; } createSensors(io, objectServer, sensors, systemBus, sensorsChanged); }); }; std::vector> matches = setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler); setupManufacturingModeMatch(*systemBus); // Watch for entity-manager to remove configuration interfaces // so the corresponding sensors can be removed. auto ifaceRemovedMatch = std::make_unique( static_cast(*systemBus), "type='signal',member='InterfacesRemoved',arg0path='" + std::string(inventoryPath) + "/'", [&sensors](sdbusplus::message_t& msg) { interfaceRemoved(msg, sensors); }); matches.emplace_back(std::move(ifaceRemovedMatch)); io.run(); }