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 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 17 | #include "filesystem.hpp" |
| 18 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | #include <ADCSensor.hpp> |
| 20 | #include <Utils.hpp> |
| 21 | #include <VariantVisitors.hpp> |
| 22 | #include <boost/algorithm/string/predicate.hpp> |
| 23 | #include <boost/algorithm/string/replace.hpp> |
| 24 | #include <boost/container/flat_set.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | #include <fstream> |
| 26 | #include <regex> |
| 27 | #include <sdbusplus/asio/connection.hpp> |
| 28 | #include <sdbusplus/asio/object_server.hpp> |
| 29 | |
| 30 | static constexpr bool DEBUG = false; |
| 31 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 32 | namespace fs = std::filesystem; |
Yoo, Jae Hyun | 5093805 | 2018-10-17 18:19:02 -0700 | [diff] [blame] | 33 | namespace variant_ns = sdbusplus::message::variant_ns; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 34 | static constexpr std::array<const char*, 1> sensorTypes = { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | "xyz.openbmc_project.Configuration.ADC"}; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 36 | static std::regex inputRegex(R"(in(\d+)_input)"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | |
James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame^] | 38 | // filter out adc from any other voltage sensor |
| 39 | bool isAdc(const fs::path& parentPath) |
| 40 | { |
| 41 | fs::path namePath = parentPath / "name"; |
| 42 | |
| 43 | std::ifstream nameFile(namePath); |
| 44 | if (!nameFile.good()) |
| 45 | { |
| 46 | std::cerr << "Failure reading " << namePath.string() << "\n"; |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | std::string name; |
| 51 | std::getline(nameFile, name); |
| 52 | |
| 53 | return name == "iio_hwmon"; |
| 54 | } |
| 55 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | void createSensors( |
| 57 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 58 | boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>& |
| 59 | sensors, |
| 60 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 61 | const std::unique_ptr<boost::container::flat_set<std::string>>& |
| 62 | sensorsChanged) |
| 63 | { |
| 64 | bool firstScan = sensorsChanged == nullptr; |
| 65 | // use new data the first time, then refresh |
| 66 | ManagedObjectType sensorConfigurations; |
| 67 | bool useCache = false; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 68 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 69 | { |
| 70 | if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations, |
| 71 | useCache)) |
| 72 | { |
| 73 | std::cerr << "error communicating to entity manager\n"; |
| 74 | return; |
| 75 | } |
| 76 | useCache = true; |
| 77 | } |
| 78 | std::vector<fs::path> paths; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 79 | if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | { |
| 81 | std::cerr << "No temperature sensors in system\n"; |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // iterate through all found adc sensors, and try to match them with |
| 86 | // configuration |
| 87 | for (auto& path : paths) |
| 88 | { |
James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame^] | 89 | if (!isAdc(path.parent_path())) |
| 90 | { |
| 91 | continue; |
| 92 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 93 | std::smatch match; |
| 94 | std::string pathStr = path.string(); |
| 95 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 96 | std::regex_search(pathStr, match, inputRegex); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 97 | std::string indexStr = *(match.begin() + 1); |
| 98 | |
| 99 | auto directory = path.parent_path(); |
| 100 | // convert to 0 based |
| 101 | size_t index = std::stoul(indexStr) - 1; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 102 | |
| 103 | const SensorData* sensorData = nullptr; |
| 104 | const std::string* interfacePath = nullptr; |
James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame^] | 105 | const std::pair<std::string, boost::container::flat_map< |
| 106 | std::string, BasicVariantType>>* |
| 107 | baseConfiguration; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 108 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
| 109 | sensor : sensorConfigurations) |
| 110 | { |
James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame^] | 111 | // clear it out each loop |
| 112 | baseConfiguration = nullptr; |
| 113 | |
| 114 | // find base configuration |
| 115 | for (const char* type : sensorTypes) |
| 116 | { |
| 117 | auto sensorBase = sensor.second.find(type); |
| 118 | if (sensorBase != sensor.second.end()) |
| 119 | { |
| 120 | baseConfiguration = &(*sensorBase); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | if (baseConfiguration == nullptr) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 125 | { |
| 126 | continue; |
| 127 | } |
James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame^] | 128 | auto findIndex = baseConfiguration->second.find("Index"); |
| 129 | if (findIndex == baseConfiguration->second.end()) |
| 130 | { |
| 131 | std::cerr << "Base configuration missing Index" |
| 132 | << baseConfiguration->first << "\n"; |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | unsigned int number = sdbusplus::message::variant_ns::visit( |
| 137 | VariantToUnsignedIntVisitor(), findIndex->second); |
| 138 | |
| 139 | if (number != index) |
| 140 | { |
| 141 | continue; |
| 142 | } |
| 143 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 144 | sensorData = &(sensor.second); |
| 145 | interfacePath = &(sensor.first.str); |
| 146 | break; |
| 147 | } |
| 148 | if (sensorData == nullptr) |
| 149 | { |
James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame^] | 150 | std::cerr << "failed to find match for " << path.string() << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 151 | continue; |
| 152 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 153 | |
| 154 | if (baseConfiguration == nullptr) |
| 155 | { |
James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame^] | 156 | std::cerr << "error finding base configuration for" << path.string() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 157 | << "\n"; |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | auto findSensorName = baseConfiguration->second.find("Name"); |
| 162 | if (findSensorName == baseConfiguration->second.end()) |
| 163 | { |
| 164 | std::cerr << "could not determine configuration name for " |
James Feist | 08aec6f | 2019-01-30 16:17:04 -0800 | [diff] [blame^] | 165 | << path.string() << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 166 | continue; |
| 167 | } |
| 168 | std::string sensorName = |
| 169 | sdbusplus::message::variant_ns::get<std::string>( |
| 170 | findSensorName->second); |
| 171 | |
| 172 | // on rescans, only update sensors we were signaled by |
| 173 | auto findSensor = sensors.find(sensorName); |
| 174 | if (!firstScan && findSensor != sensors.end()) |
| 175 | { |
| 176 | bool found = false; |
| 177 | for (auto it = sensorsChanged->begin(); it != sensorsChanged->end(); |
| 178 | it++) |
| 179 | { |
| 180 | if (boost::ends_with(*it, findSensor->second->name)) |
| 181 | { |
| 182 | sensorsChanged->erase(it); |
| 183 | findSensor->second = nullptr; |
| 184 | found = true; |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | if (!found) |
| 189 | { |
| 190 | continue; |
| 191 | } |
| 192 | } |
| 193 | std::vector<thresholds::Threshold> sensorThresholds; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 194 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 195 | { |
| 196 | std::cerr << "error populating thresholds for " << sensorName |
| 197 | << "\n"; |
| 198 | } |
| 199 | |
James Feist | 757c9db | 2018-09-12 12:27:25 -0700 | [diff] [blame] | 200 | auto findScaleFactor = baseConfiguration->second.find("ScaleFactor"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 201 | float scaleFactor = 1.0; |
| 202 | if (findScaleFactor != baseConfiguration->second.end()) |
| 203 | { |
Yoo, Jae Hyun | 5093805 | 2018-10-17 18:19:02 -0700 | [diff] [blame] | 204 | scaleFactor = variant_ns::visit(VariantToFloatVisitor(), |
| 205 | findScaleFactor->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 206 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 207 | |
| 208 | auto findPowerOn = baseConfiguration->second.find("PowerState"); |
| 209 | PowerState readState = PowerState::always; |
| 210 | if (findPowerOn != baseConfiguration->second.end()) |
| 211 | { |
| 212 | std::string powerState = variant_ns::visit(VariantToStringVisitor(), |
| 213 | findPowerOn->second); |
| 214 | if (powerState == "On") |
| 215 | { |
| 216 | readState = PowerState::on; |
| 217 | }; |
| 218 | } |
| 219 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 220 | sensors[sensorName] = std::make_unique<ADCSensor>( |
| 221 | path.string(), objectServer, dbusConnection, io, sensorName, |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 222 | std::move(sensorThresholds), scaleFactor, readState, |
| 223 | *interfacePath); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | int main(int argc, char** argv) |
| 228 | { |
| 229 | boost::asio::io_service io; |
| 230 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 231 | systemBus->request_name("xyz.openbmc_project.ADCSensor"); |
| 232 | sdbusplus::asio::object_server objectServer(systemBus); |
| 233 | boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors; |
| 234 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 235 | std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged = |
| 236 | std::make_unique<boost::container::flat_set<std::string>>(); |
| 237 | |
| 238 | io.post([&]() { |
| 239 | createSensors(io, objectServer, sensors, systemBus, nullptr); |
| 240 | }); |
| 241 | |
| 242 | boost::asio::deadline_timer filterTimer(io); |
| 243 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 244 | [&](sdbusplus::message::message& message) { |
| 245 | if (message.is_method_error()) |
| 246 | { |
| 247 | std::cerr << "callback method error\n"; |
| 248 | return; |
| 249 | } |
| 250 | sensorsChanged->insert(message.get_path()); |
| 251 | // this implicitly cancels the timer |
| 252 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 253 | |
| 254 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 255 | if (ec == boost::asio::error::operation_aborted) |
| 256 | { |
| 257 | /* we were canceled*/ |
| 258 | return; |
| 259 | } |
| 260 | else if (ec) |
| 261 | { |
| 262 | std::cerr << "timer error\n"; |
| 263 | return; |
| 264 | } |
| 265 | createSensors(io, objectServer, sensors, systemBus, |
| 266 | sensorsChanged); |
| 267 | }); |
| 268 | }; |
| 269 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 270 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 271 | { |
| 272 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 273 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 274 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 275 | std::string(inventoryPath) + "',arg0namespace='" + type + "'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 276 | eventHandler); |
| 277 | matches.emplace_back(std::move(match)); |
| 278 | } |
| 279 | |
| 280 | io.run(); |
| 281 | } |