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