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