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