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