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 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame^] | 17 | #include "HwmonTempSensor.hpp" |
| 18 | #include "Utils.hpp" |
| 19 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | #include <boost/algorithm/string/predicate.hpp> |
| 21 | #include <boost/algorithm/string/replace.hpp> |
| 22 | #include <boost/container/flat_set.hpp> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 23 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 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 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 31 | namespace fs = std::filesystem; |
Patrick Venture | 3546adb | 2019-08-14 18:35:29 -0700 | [diff] [blame] | 32 | static constexpr std::array<const char*, 7> sensorTypes = { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | "xyz.openbmc_project.Configuration.TMP75", |
John Wang | 55ab2af | 2019-04-18 14:22:31 +0800 | [diff] [blame] | 34 | "xyz.openbmc_project.Configuration.TMP421", |
Patrick Venture | 7fa475d | 2019-08-10 08:49:01 -0700 | [diff] [blame] | 35 | "xyz.openbmc_project.Configuration.TMP441", |
John Wang | 55ab2af | 2019-04-18 14:22:31 +0800 | [diff] [blame] | 36 | "xyz.openbmc_project.Configuration.TMP112", |
Patrick Venture | 3546adb | 2019-08-14 18:35:29 -0700 | [diff] [blame] | 37 | "xyz.openbmc_project.Configuration.TMP175", |
Patrick Venture | bd1a9d5 | 2019-08-14 18:10:26 -0700 | [diff] [blame] | 38 | "xyz.openbmc_project.Configuration.EMC1413", |
| 39 | "xyz.openbmc_project.Configuration.MAX31725"}; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 | |
| 41 | void createSensors( |
| 42 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, |
| 43 | boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>& |
| 44 | sensors, |
| 45 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 46 | const std::unique_ptr<boost::container::flat_set<std::string>>& |
| 47 | sensorsChanged) |
| 48 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 49 | auto getter = std::make_shared<GetSensorConfiguration>( |
| 50 | dbusConnection, |
| 51 | std::move([&io, &objectServer, &sensors, &dbusConnection, |
| 52 | &sensorsChanged]( |
| 53 | const ManagedObjectType& sensorConfigurations) { |
| 54 | bool firstScan = sensorsChanged == nullptr; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 56 | std::vector<fs::path> paths; |
| 57 | if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", |
| 58 | paths)) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 59 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 60 | std::cerr << "No temperature sensors in system\n"; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | boost::container::flat_set<std::string> directories; |
| 65 | |
| 66 | // iterate through all found temp sensors, and try to match them |
| 67 | // with configuration |
| 68 | for (auto& path : paths) |
| 69 | { |
| 70 | std::smatch match; |
| 71 | const std::string& pathStr = path.string(); |
| 72 | auto directory = path.parent_path(); |
| 73 | |
| 74 | auto ret = directories.insert(directory.string()); |
| 75 | if (!ret.second) |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 76 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 77 | continue; // already searched this path |
| 78 | } |
| 79 | |
| 80 | fs::path device = directory / "device"; |
| 81 | std::string deviceName = fs::canonical(device).stem(); |
| 82 | auto findHyphen = deviceName.find("-"); |
| 83 | if (findHyphen == std::string::npos) |
| 84 | { |
| 85 | std::cerr << "found bad device " << deviceName << "\n"; |
| 86 | continue; |
| 87 | } |
| 88 | std::string busStr = deviceName.substr(0, findHyphen); |
| 89 | std::string addrStr = deviceName.substr(findHyphen + 1); |
| 90 | |
| 91 | size_t bus = 0; |
| 92 | size_t addr = 0; |
| 93 | try |
| 94 | { |
| 95 | bus = std::stoi(busStr); |
| 96 | addr = std::stoi(addrStr, 0, 16); |
| 97 | } |
| 98 | catch (std::invalid_argument&) |
| 99 | { |
| 100 | continue; |
| 101 | } |
| 102 | const SensorData* sensorData = nullptr; |
| 103 | const std::string* interfacePath = nullptr; |
| 104 | const char* sensorType = nullptr; |
| 105 | const std::pair< |
| 106 | std::string, |
| 107 | boost::container::flat_map<std::string, BasicVariantType>>* |
| 108 | baseConfiguration = nullptr; |
| 109 | |
| 110 | for (const std::pair<sdbusplus::message::object_path, |
| 111 | SensorData>& sensor : sensorConfigurations) |
| 112 | { |
| 113 | sensorData = &(sensor.second); |
| 114 | for (const char* type : sensorTypes) |
| 115 | { |
| 116 | auto sensorBase = sensorData->find(type); |
| 117 | if (sensorBase != sensorData->end()) |
| 118 | { |
| 119 | baseConfiguration = &(*sensorBase); |
| 120 | sensorType = type; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | if (baseConfiguration == nullptr) |
| 125 | { |
| 126 | std::cerr << "error finding base configuration for " |
| 127 | << deviceName << "\n"; |
| 128 | continue; |
| 129 | } |
| 130 | auto configurationBus = |
| 131 | baseConfiguration->second.find("Bus"); |
| 132 | auto configurationAddress = |
| 133 | baseConfiguration->second.find("Address"); |
| 134 | |
| 135 | if (configurationBus == baseConfiguration->second.end() || |
| 136 | configurationAddress == baseConfiguration->second.end()) |
| 137 | { |
| 138 | std::cerr |
| 139 | << "error finding bus or address in configuration"; |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | if (std::get<uint64_t>(configurationBus->second) != bus || |
| 144 | std::get<uint64_t>(configurationAddress->second) != |
| 145 | addr) |
| 146 | { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | interfacePath = &(sensor.first.str); |
James Feist | 37266ca | 2018-10-15 15:56:28 -0700 | [diff] [blame] | 151 | break; |
| 152 | } |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 153 | if (interfacePath == nullptr) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 154 | { |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 155 | std::cerr << "failed to find match for " << deviceName |
| 156 | << "\n"; |
| 157 | continue; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 158 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 159 | |
James Feist | df51515 | 2019-09-18 16:40:40 -0700 | [diff] [blame] | 160 | auto findSensorName = baseConfiguration->second.find("Name"); |
| 161 | if (findSensorName == baseConfiguration->second.end()) |
| 162 | { |
| 163 | std::cerr << "could not determine configuration name for " |
| 164 | << deviceName << "\n"; |
| 165 | continue; |
| 166 | } |
| 167 | std::string sensorName = |
| 168 | std::get<std::string>(findSensorName->second); |
| 169 | // on rescans, only update sensors we were signaled by |
| 170 | auto findSensor = sensors.find(sensorName); |
| 171 | if (!firstScan && findSensor != sensors.end()) |
| 172 | { |
| 173 | bool found = false; |
| 174 | for (auto it = sensorsChanged->begin(); |
| 175 | it != sensorsChanged->end(); it++) |
| 176 | { |
| 177 | if (boost::ends_with(*it, findSensor->second->name)) |
| 178 | { |
| 179 | sensorsChanged->erase(it); |
| 180 | findSensor->second = nullptr; |
| 181 | found = true; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | if (!found) |
| 186 | { |
| 187 | continue; |
| 188 | } |
| 189 | } |
| 190 | std::vector<thresholds::Threshold> sensorThresholds; |
| 191 | if (!parseThresholdsFromConfig(*sensorData, sensorThresholds)) |
| 192 | { |
| 193 | std::cerr << "error populating thresholds for " |
| 194 | << sensorName << "\n"; |
| 195 | } |
| 196 | auto& sensor1 = sensors[sensorName]; |
| 197 | sensor1 = nullptr; |
| 198 | sensor1 = std::make_unique<HwmonTempSensor>( |
| 199 | directory.string() + "/temp1_input", sensorType, |
| 200 | objectServer, dbusConnection, io, sensorName, |
| 201 | std::move(sensorThresholds), *interfacePath); |
| 202 | auto findSecondName = baseConfiguration->second.find("Name1"); |
| 203 | if (findSecondName == baseConfiguration->second.end()) |
| 204 | { |
| 205 | continue; |
| 206 | } |
| 207 | sensorName = std::get<std::string>(findSecondName->second); |
| 208 | auto& sensor2 = sensors[sensorName]; |
| 209 | sensor2 = nullptr; |
| 210 | sensor2 = std::make_unique<HwmonTempSensor>( |
| 211 | directory.string() + "/temp2_input", sensorType, |
| 212 | objectServer, dbusConnection, io, sensorName, |
| 213 | std::vector<thresholds::Threshold>(), *interfacePath); |
| 214 | } |
| 215 | })); |
| 216 | getter->getConfiguration( |
| 217 | std::vector<std::string>(sensorTypes.begin(), sensorTypes.end())); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 218 | } |
| 219 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 220 | int main() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 221 | { |
| 222 | boost::asio::io_service io; |
| 223 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 224 | systemBus->request_name("xyz.openbmc_project.HwmonTempSensor"); |
| 225 | sdbusplus::asio::object_server objectServer(systemBus); |
| 226 | boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>> |
| 227 | sensors; |
| 228 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; |
| 229 | std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged = |
| 230 | std::make_unique<boost::container::flat_set<std::string>>(); |
| 231 | |
| 232 | io.post([&]() { |
| 233 | createSensors(io, objectServer, sensors, systemBus, nullptr); |
| 234 | }); |
| 235 | |
| 236 | boost::asio::deadline_timer filterTimer(io); |
| 237 | std::function<void(sdbusplus::message::message&)> eventHandler = |
| 238 | [&](sdbusplus::message::message& message) { |
| 239 | if (message.is_method_error()) |
| 240 | { |
| 241 | std::cerr << "callback method error\n"; |
| 242 | return; |
| 243 | } |
| 244 | sensorsChanged->insert(message.get_path()); |
| 245 | // this implicitly cancels the timer |
| 246 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); |
| 247 | |
| 248 | filterTimer.async_wait([&](const boost::system::error_code& ec) { |
| 249 | if (ec == boost::asio::error::operation_aborted) |
| 250 | { |
| 251 | /* we were canceled*/ |
| 252 | return; |
| 253 | } |
| 254 | else if (ec) |
| 255 | { |
| 256 | std::cerr << "timer error\n"; |
| 257 | return; |
| 258 | } |
| 259 | createSensors(io, objectServer, sensors, systemBus, |
| 260 | sensorsChanged); |
| 261 | }); |
| 262 | }; |
| 263 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 264 | for (const char* type : sensorTypes) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 265 | { |
| 266 | auto match = std::make_unique<sdbusplus::bus::match::match>( |
| 267 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
| 268 | "type='signal',member='PropertiesChanged',path_namespace='" + |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 269 | std::string(inventoryPath) + "',arg0namespace='" + type + "'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 270 | eventHandler); |
| 271 | matches.emplace_back(std::move(match)); |
| 272 | } |
| 273 | |
| 274 | io.run(); |
| 275 | } |