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; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | static constexpr std::array<const char*, 1> SENSOR_TYPES = { |
| 34 | "xyz.openbmc_project.Configuration.ADC"}; |
| 35 | static std::regex INPUT_REGEX(R"(in(\d+)_input)"); |
| 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; |
| 49 | for (const char* type : SENSOR_TYPES) |
| 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; |
| 60 | if (!find_files(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths)) |
| 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 | |
| 73 | std::regex_search(pathStr, match, INPUT_REGEX); |
| 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; |
| 79 | auto oem_name_path = |
| 80 | directory.string() + R"(/of_node/oemname)" + std::to_string(index); |
| 81 | |
| 82 | if (DEBUG) |
| 83 | std::cout << "Checking path " << oem_name_path << "\n"; |
| 84 | std::ifstream nameFile(oem_name_path); |
| 85 | if (!nameFile.good()) |
| 86 | { |
| 87 | std::cerr << "Failure reading " << oem_name_path << "\n"; |
| 88 | continue; |
| 89 | } |
| 90 | std::string oemName; |
| 91 | std::getline(nameFile, oemName); |
| 92 | nameFile.close(); |
| 93 | if (!oemName.size()) |
| 94 | { |
| 95 | // shouldn't have an empty name file |
| 96 | continue; |
| 97 | } |
| 98 | oemName.pop_back(); // remove trailing null |
| 99 | |
| 100 | const SensorData* sensorData = nullptr; |
| 101 | const std::string* interfacePath = nullptr; |
| 102 | for (const std::pair<sdbusplus::message::object_path, SensorData>& |
| 103 | sensor : sensorConfigurations) |
| 104 | { |
| 105 | if (!boost::ends_with(sensor.first.str, oemName)) |
| 106 | { |
| 107 | continue; |
| 108 | } |
| 109 | sensorData = &(sensor.second); |
| 110 | interfacePath = &(sensor.first.str); |
| 111 | break; |
| 112 | } |
| 113 | if (sensorData == nullptr) |
| 114 | { |
| 115 | std::cerr << "failed to find match for " << oemName << "\n"; |
| 116 | continue; |
| 117 | } |
| 118 | const std::pair<std::string, boost::container::flat_map< |
| 119 | std::string, BasicVariantType>>* |
| 120 | baseConfiguration = nullptr; |
| 121 | for (const char* type : SENSOR_TYPES) |
| 122 | { |
| 123 | auto sensorBase = sensorData->find(type); |
| 124 | if (sensorBase != sensorData->end()) |
| 125 | { |
| 126 | baseConfiguration = &(*sensorBase); |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (baseConfiguration == nullptr) |
| 132 | { |
| 133 | std::cerr << "error finding base configuration for" << oemName |
| 134 | << "\n"; |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | auto findSensorName = baseConfiguration->second.find("Name"); |
| 139 | if (findSensorName == baseConfiguration->second.end()) |
| 140 | { |
| 141 | std::cerr << "could not determine configuration name for " |
| 142 | << oemName << "\n"; |
| 143 | continue; |
| 144 | } |
| 145 | std::string sensorName = |
| 146 | sdbusplus::message::variant_ns::get<std::string>( |
| 147 | findSensorName->second); |
| 148 | |
| 149 | // on rescans, only update sensors we were signaled by |
| 150 | auto findSensor = sensors.find(sensorName); |
| 151 | if (!firstScan && findSensor != sensors.end()) |
| 152 | { |
| 153 | bool found = false; |
| 154 | for (auto it = sensorsChanged->begin(); it != sensorsChanged->end(); |
| 155 | it++) |
| 156 | { |
| 157 | if (boost::ends_with(*it, findSensor->second->name)) |
| 158 | { |
| 159 | sensorsChanged->erase(it); |
| 160 | findSensor->second = nullptr; |
| 161 | found = true; |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | if (!found) |
| 166 | { |
| 167 | continue; |
| 168 | } |
| 169 | } |
| 170 | std::vector<thresholds::Threshold> sensorThresholds; |
| 171 | if (!ParseThresholdsFromConfig(*sensorData, sensorThresholds)) |
| 172 | { |
| 173 | std::cerr << "error populating thresholds for " << sensorName |
| 174 | << "\n"; |
| 175 | } |
| 176 | |
James Feist | b9d9b33 | 2018-09-12 12:27:25 -0700 | [diff] [blame] | 177 | auto findScaleFactor = baseConfiguration->second.find("ScaleFactor"); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 178 | float scaleFactor = 1.0; |
| 179 | if (findScaleFactor != baseConfiguration->second.end()) |
| 180 | { |
Yoo, Jae Hyun | 625429b | 2018-10-17 18:19:02 -0700 | [diff] [blame^] | 181 | scaleFactor = variant_ns::visit(VariantToFloatVisitor(), |
| 182 | findScaleFactor->second); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 183 | } |
| 184 | sensors[sensorName] = std::make_unique<ADCSensor>( |
| 185 | path.string(), objectServer, dbusConnection, io, sensorName, |
| 186 | std::move(sensorThresholds), scaleFactor, *interfacePath); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | int main(int argc, char** argv) |
| 191 | { |
| 192 | boost::asio::io_service io; |
| 193 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 194 | systemBus->request_name("xyz.openbmc_project.ADCSensor"); |
| 195 | sdbusplus::asio::object_server objectServer(systemBus); |
| 196 | boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors; |
| 197 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 198 | std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged = |
| 199 | std::make_unique<boost::container::flat_set<std::string>>(); |
| 200 | |
| 201 | io.post([&]() { |
| 202 | createSensors(io, objectServer, sensors, systemBus, nullptr); |
| 203 | }); |
| 204 | |
| 205 | boost::asio::deadline_timer filterTimer(io); |
| 206 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 207 | [&](sdbusplus::message::message& message) { |
| 208 | if (message.is_method_error()) |
| 209 | { |
| 210 | std::cerr << "callback method error\n"; |
| 211 | return; |
| 212 | } |
| 213 | sensorsChanged->insert(message.get_path()); |
| 214 | // this implicitly cancels the timer |
| 215 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 216 | |
| 217 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 218 | if (ec == boost::asio::error::operation_aborted) |
| 219 | { |
| 220 | /* we were canceled*/ |
| 221 | return; |
| 222 | } |
| 223 | else if (ec) |
| 224 | { |
| 225 | std::cerr << "timer error\n"; |
| 226 | return; |
| 227 | } |
| 228 | createSensors(io, objectServer, sensors, systemBus, |
| 229 | sensorsChanged); |
| 230 | }); |
| 231 | }; |
| 232 | |
| 233 | for (const char* type : SENSOR_TYPES) |
| 234 | { |
| 235 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 236 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 237 | "type='signal',member='PropertiesChanged',path_namespace='" + |
| 238 | std::string(INVENTORY_PATH) + "',arg0namespace='" + type + "'", |
| 239 | eventHandler); |
| 240 | matches.emplace_back(std::move(match)); |
| 241 | } |
| 242 | |
| 243 | io.run(); |
| 244 | } |