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