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