James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 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 | 0771659 | 2018-10-14 11:46:40 -0700 | [diff] [blame] | 17 | #include "conf.hpp" |
James Feist | 0c8223b | 2019-05-08 15:33:33 -0700 | [diff] [blame] | 18 | #include "util.hpp" |
Patrick Venture | 0771659 | 2018-10-14 11:46:40 -0700 | [diff] [blame] | 19 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 20 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 21 | #include <sdbusplus/bus.hpp> |
| 22 | #include <sdbusplus/bus/match.hpp> |
| 23 | #include <sdbusplus/exception.hpp> |
| 24 | |
| 25 | #include <algorithm> |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 26 | #include <chrono> |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 27 | #include <functional> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 28 | #include <iostream> |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 29 | #include <list> |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 30 | #include <regex> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 31 | #include <set> |
| 32 | #include <unordered_map> |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 33 | #include <variant> |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 34 | |
| 35 | static constexpr bool DEBUG = false; // enable to print found configuration |
| 36 | |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 37 | extern std::map<std::string, struct conf::SensorConfig> sensorConfig; |
| 38 | extern std::map<int64_t, conf::PIDConf> zoneConfig; |
| 39 | extern std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 40 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 41 | constexpr const char* pidConfigurationInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 42 | "xyz.openbmc_project.Configuration.Pid"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 43 | constexpr const char* objectManagerInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 44 | "org.freedesktop.DBus.ObjectManager"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 45 | constexpr const char* pidZoneConfigurationInterface = |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 46 | "xyz.openbmc_project.Configuration.Pid.Zone"; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 47 | constexpr const char* stepwiseConfigurationInterface = |
| 48 | "xyz.openbmc_project.Configuration.Stepwise"; |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 49 | constexpr const char* thermalControlIface = |
| 50 | "xyz.openbmc_project.Control.ThermalMode"; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 51 | constexpr const char* sensorInterface = "xyz.openbmc_project.Sensor.Value"; |
| 52 | constexpr const char* pwmInterface = "xyz.openbmc_project.Control.FanPwm"; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 53 | |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 54 | using Association = std::tuple<std::string, std::string, std::string>; |
| 55 | using Associations = std::vector<Association>; |
| 56 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 57 | namespace thresholds |
| 58 | { |
| 59 | constexpr const char* warningInterface = |
| 60 | "xyz.openbmc_project.Sensor.Threshold.Warning"; |
| 61 | constexpr const char* criticalInterface = |
| 62 | "xyz.openbmc_project.Sensor.Threshold.Critical"; |
| 63 | const std::array<const char*, 4> types = {"CriticalLow", "CriticalHigh", |
| 64 | "WarningLow", "WarningHigh"}; |
| 65 | |
| 66 | } // namespace thresholds |
| 67 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 68 | namespace dbus_configuration |
| 69 | { |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 70 | using DbusVariantType = |
| 71 | std::variant<uint64_t, int64_t, double, std::string, |
| 72 | std::vector<std::string>, std::vector<double>>; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 73 | using SensorInterfaceType = std::pair<std::string, std::string>; |
| 74 | |
| 75 | inline std::string getSensorNameFromPath(const std::string& dbusPath) |
| 76 | { |
| 77 | return dbusPath.substr(dbusPath.find_last_of("/") + 1); |
| 78 | } |
| 79 | |
| 80 | inline std::string sensorNameToDbusName(const std::string& sensorName) |
| 81 | { |
| 82 | std::string retString = sensorName; |
| 83 | std::replace(retString.begin(), retString.end(), ' ', '_'); |
| 84 | return retString; |
| 85 | } |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 86 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 87 | bool findSensors(const std::unordered_map<std::string, std::string>& sensors, |
| 88 | const std::string& search, |
| 89 | std::vector<std::pair<std::string, std::string>>& matches) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 90 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 91 | std::smatch match; |
Jae Hyun Yoo | 1a704dc | 2020-06-04 15:00:10 -0700 | [diff] [blame] | 92 | std::regex reg(search + '$'); |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 93 | for (const auto& sensor : sensors) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 94 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 95 | if (std::regex_search(sensor.first, match, reg)) |
| 96 | { |
| 97 | matches.push_back(sensor); |
| 98 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 99 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 100 | return matches.size() > 0; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // this function prints the configuration into a form similar to the cpp |
| 104 | // generated code to help in verification, should be turned off during normal |
| 105 | // use |
| 106 | void debugPrint(void) |
| 107 | { |
| 108 | // print sensor config |
| 109 | std::cout << "sensor config:\n"; |
| 110 | std::cout << "{\n"; |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 111 | for (const auto& pair : sensorConfig) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 112 | { |
| 113 | |
| 114 | std::cout << "\t{" << pair.first << ",\n\t\t{"; |
| 115 | std::cout << pair.second.type << ", "; |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 116 | std::cout << pair.second.readPath << ", "; |
| 117 | std::cout << pair.second.writePath << ", "; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 118 | std::cout << pair.second.min << ", "; |
| 119 | std::cout << pair.second.max << ", "; |
| 120 | std::cout << pair.second.timeout << "},\n\t},\n"; |
| 121 | } |
| 122 | std::cout << "}\n\n"; |
| 123 | std::cout << "ZoneDetailsConfig\n"; |
| 124 | std::cout << "{\n"; |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 125 | for (const auto& zone : zoneDetailsConfig) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 126 | { |
| 127 | std::cout << "\t{" << zone.first << ",\n"; |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 128 | std::cout << "\t\t{" << zone.second.minThermalOutput << ", "; |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame] | 129 | std::cout << zone.second.failsafePercent << "}\n\t},\n"; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 130 | } |
| 131 | std::cout << "}\n\n"; |
| 132 | std::cout << "ZoneConfig\n"; |
| 133 | std::cout << "{\n"; |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 134 | for (const auto& zone : zoneConfig) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 135 | { |
| 136 | std::cout << "\t{" << zone.first << "\n"; |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 137 | for (const auto& pidconf : zone.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 138 | { |
| 139 | std::cout << "\t\t{" << pidconf.first << ",\n"; |
| 140 | std::cout << "\t\t\t{" << pidconf.second.type << ",\n"; |
| 141 | std::cout << "\t\t\t{"; |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 142 | for (const auto& input : pidconf.second.inputs) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 143 | { |
| 144 | std::cout << "\n\t\t\t" << input << ",\n"; |
| 145 | } |
| 146 | std::cout << "\t\t\t}\n"; |
| 147 | std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n"; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 148 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n"; |
Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 149 | std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff |
| 150 | << ",\n"; |
| 151 | std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff |
| 152 | << ",\n"; |
| 153 | std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset |
| 154 | << ",\n"; |
| 155 | std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain |
| 156 | << ",\n"; |
| 157 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min |
| 158 | << "," << pidconf.second.pidInfo.integralLimit.max |
| 159 | << "},\n"; |
| 160 | std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << "," |
| 161 | << pidconf.second.pidInfo.outLim.max << "},\n"; |
| 162 | std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n"; |
| 163 | std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n"; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 164 | std::cout << "\t\t\t}\n\t\t}\n"; |
| 165 | } |
| 166 | std::cout << "\t},\n"; |
| 167 | } |
| 168 | std::cout << "}\n\n"; |
| 169 | } |
| 170 | |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 171 | size_t getZoneIndex(const std::string& name, std::vector<std::string>& zones) |
| 172 | { |
| 173 | auto it = std::find(zones.begin(), zones.end(), name); |
| 174 | if (it == zones.end()) |
| 175 | { |
| 176 | zones.emplace_back(name); |
| 177 | it = zones.end() - 1; |
| 178 | } |
| 179 | |
| 180 | return it - zones.begin(); |
| 181 | } |
| 182 | |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 183 | std::vector<std::string> getSelectedProfiles(sdbusplus::bus::bus& bus) |
| 184 | { |
| 185 | std::vector<std::string> ret; |
| 186 | auto mapper = |
| 187 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 188 | "/xyz/openbmc_project/object_mapper", |
| 189 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
| 190 | mapper.append("/", 0, std::array<const char*, 1>{thermalControlIface}); |
| 191 | std::unordered_map< |
| 192 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 193 | respData; |
| 194 | |
| 195 | try |
| 196 | { |
| 197 | auto resp = bus.call(mapper); |
| 198 | resp.read(respData); |
| 199 | } |
| 200 | catch (sdbusplus::exception_t&) |
| 201 | { |
| 202 | // can't do anything without mapper call data |
| 203 | throw std::runtime_error("ObjectMapper Call Failure"); |
| 204 | } |
| 205 | if (respData.empty()) |
| 206 | { |
| 207 | // if the user has profiles but doesn't expose the interface to select |
| 208 | // one, just go ahead without using profiles |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | // assumption is that we should only have a small handful of selected |
| 213 | // profiles at a time (probably only 1), so calling each individually should |
| 214 | // not incur a large cost |
| 215 | for (const auto& objectPair : respData) |
| 216 | { |
| 217 | const std::string& path = objectPair.first; |
| 218 | for (const auto& ownerPair : objectPair.second) |
| 219 | { |
| 220 | const std::string& busName = ownerPair.first; |
| 221 | auto getProfile = |
| 222 | bus.new_method_call(busName.c_str(), path.c_str(), |
| 223 | "org.freedesktop.DBus.Properties", "Get"); |
| 224 | getProfile.append(thermalControlIface, "Current"); |
| 225 | std::variant<std::string> variantResp; |
| 226 | try |
| 227 | { |
| 228 | auto resp = bus.call(getProfile); |
| 229 | resp.read(variantResp); |
| 230 | } |
| 231 | catch (sdbusplus::exception_t&) |
| 232 | { |
| 233 | throw std::runtime_error("Failure getting profile"); |
| 234 | } |
| 235 | std::string mode = std::get<std::string>(variantResp); |
| 236 | ret.emplace_back(std::move(mode)); |
| 237 | } |
| 238 | } |
| 239 | if constexpr (DEBUG) |
| 240 | { |
| 241 | std::cout << "Profiles selected: "; |
| 242 | for (const auto& profile : ret) |
| 243 | { |
| 244 | std::cout << profile << " "; |
| 245 | } |
| 246 | std::cout << "\n"; |
| 247 | } |
| 248 | return ret; |
| 249 | } |
| 250 | |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 251 | int eventHandler(sd_bus_message* m, void* context, sd_bus_error*) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 252 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 253 | |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 254 | if (context == nullptr || m == nullptr) |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 255 | { |
| 256 | throw std::runtime_error("Invalid match"); |
| 257 | } |
James Feist | 991ebd8 | 2020-07-21 11:14:52 -0700 | [diff] [blame] | 258 | |
| 259 | // we skip associations because the mapper populates these, not the sensors |
| 260 | const std::array<const char*, 1> skipList = { |
| 261 | "xyz.openbmc_project.Association"}; |
| 262 | |
| 263 | sdbusplus::message::message message(m); |
| 264 | if (std::string(message.get_member()) == "InterfacesAdded") |
| 265 | { |
| 266 | sdbusplus::message::object_path path; |
| 267 | std::unordered_map< |
| 268 | std::string, |
| 269 | std::unordered_map<std::string, std::variant<Associations, bool>>> |
| 270 | data; |
| 271 | |
| 272 | message.read(path, data); |
| 273 | |
| 274 | for (const char* skip : skipList) |
| 275 | { |
| 276 | auto find = data.find(skip); |
| 277 | if (find != data.end()) |
| 278 | { |
| 279 | data.erase(find); |
| 280 | if (data.empty()) |
| 281 | { |
| 282 | return 1; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 288 | boost::asio::steady_timer* timer = |
| 289 | static_cast<boost::asio::steady_timer*>(context); |
| 290 | |
| 291 | // do a brief sleep as we tend to get a bunch of these events at |
| 292 | // once |
| 293 | timer->expires_after(std::chrono::seconds(2)); |
| 294 | timer->async_wait([](const boost::system::error_code ec) { |
| 295 | if (ec == boost::asio::error::operation_aborted) |
| 296 | { |
| 297 | /* another timer started*/ |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | std::cout << "New configuration detected, reloading\n."; |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 302 | tryRestartControlLoops(); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 303 | }); |
| 304 | |
| 305 | return 1; |
| 306 | } |
| 307 | |
| 308 | void createMatches(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer) |
| 309 | { |
| 310 | // this is a list because the matches can't be moved |
| 311 | static std::list<sdbusplus::bus::match::match> matches; |
| 312 | |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 313 | const std::array<std::string, 4> interfaces = { |
| 314 | thermalControlIface, pidConfigurationInterface, |
| 315 | pidZoneConfigurationInterface, stepwiseConfigurationInterface}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 316 | |
| 317 | // this list only needs to be created once |
| 318 | if (!matches.empty()) |
| 319 | { |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | // we restart when the configuration changes or there are new sensors |
| 324 | for (const auto& interface : interfaces) |
| 325 | { |
| 326 | matches.emplace_back( |
| 327 | bus, |
| 328 | "type='signal',member='PropertiesChanged',arg0namespace='" + |
| 329 | interface + "'", |
| 330 | eventHandler, &timer); |
| 331 | } |
| 332 | matches.emplace_back( |
| 333 | bus, |
| 334 | "type='signal',member='InterfacesAdded',arg0path='/xyz/openbmc_project/" |
| 335 | "sensors/'", |
| 336 | eventHandler, &timer); |
| 337 | } |
| 338 | |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 339 | /** |
| 340 | * retrieve an attribute from the pid configuration map |
| 341 | * @param[in] base - the PID configuration map, keys are the attributes and |
| 342 | * value is the variant associated with that attribute. |
| 343 | * @param attributeName - the name of the attribute |
| 344 | * @return a variant holding the value associated with a key |
| 345 | * @throw runtime_error : attributeName is not in base |
| 346 | */ |
| 347 | inline DbusVariantType getPIDAttribute( |
| 348 | const std::unordered_map<std::string, DbusVariantType>& base, |
| 349 | const std::string& attributeName) |
| 350 | { |
| 351 | auto search = base.find(attributeName); |
| 352 | if (search == base.end()) |
| 353 | { |
| 354 | throw std::runtime_error("missing attribute " + attributeName); |
| 355 | } |
| 356 | return search->second; |
| 357 | } |
| 358 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 359 | void populatePidInfo( |
| 360 | sdbusplus::bus::bus& bus, |
| 361 | const std::unordered_map<std::string, DbusVariantType>& base, |
| 362 | struct conf::ControllerInfo& info, const std::string* thresholdProperty) |
| 363 | { |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 364 | info.type = std::get<std::string>(getPIDAttribute(base, "Class")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 365 | if (info.type == "fan") |
| 366 | { |
| 367 | info.setpoint = 0; |
| 368 | } |
| 369 | else |
| 370 | { |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 371 | info.setpoint = std::visit(VariantToDoubleVisitor(), |
| 372 | getPIDAttribute(base, "SetPoint")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | if (thresholdProperty != nullptr) |
| 376 | { |
| 377 | std::string interface; |
| 378 | if (*thresholdProperty == "WarningHigh" || |
| 379 | *thresholdProperty == "WarningLow") |
| 380 | { |
| 381 | interface = thresholds::warningInterface; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | interface = thresholds::criticalInterface; |
| 386 | } |
| 387 | const std::string& path = sensorConfig[info.inputs.front()].readPath; |
| 388 | |
| 389 | DbusHelper helper; |
| 390 | std::string service = helper.getService(bus, interface, path); |
| 391 | double reading = 0; |
| 392 | try |
| 393 | { |
| 394 | helper.getProperty(bus, service, path, interface, |
| 395 | *thresholdProperty, reading); |
| 396 | } |
| 397 | catch (const sdbusplus::exception::SdBusError& ex) |
| 398 | { |
| 399 | // unsupported threshold, leaving reading at 0 |
| 400 | } |
| 401 | |
| 402 | info.setpoint += reading; |
| 403 | } |
| 404 | |
| 405 | info.pidInfo.ts = 1.0; // currently unused |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 406 | info.pidInfo.proportionalCoeff = std::visit( |
| 407 | VariantToDoubleVisitor(), getPIDAttribute(base, "PCoefficient")); |
| 408 | info.pidInfo.integralCoeff = std::visit( |
| 409 | VariantToDoubleVisitor(), getPIDAttribute(base, "ICoefficient")); |
| 410 | info.pidInfo.feedFwdOffset = std::visit( |
| 411 | VariantToDoubleVisitor(), getPIDAttribute(base, "FFOffCoefficient")); |
| 412 | info.pidInfo.feedFwdGain = std::visit( |
| 413 | VariantToDoubleVisitor(), getPIDAttribute(base, "FFGainCoefficient")); |
| 414 | info.pidInfo.integralLimit.max = std::visit( |
| 415 | VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMax")); |
| 416 | info.pidInfo.integralLimit.min = std::visit( |
| 417 | VariantToDoubleVisitor(), getPIDAttribute(base, "ILimitMin")); |
| 418 | info.pidInfo.outLim.max = std::visit(VariantToDoubleVisitor(), |
| 419 | getPIDAttribute(base, "OutLimitMax")); |
| 420 | info.pidInfo.outLim.min = std::visit(VariantToDoubleVisitor(), |
| 421 | getPIDAttribute(base, "OutLimitMin")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 422 | info.pidInfo.slewNeg = |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 423 | std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewNeg")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 424 | info.pidInfo.slewPos = |
Jason Ling | 6fc301f | 2020-07-23 12:39:57 -0700 | [diff] [blame] | 425 | std::visit(VariantToDoubleVisitor(), getPIDAttribute(base, "SlewPos")); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 426 | double negativeHysteresis = 0; |
| 427 | double positiveHysteresis = 0; |
| 428 | |
| 429 | auto findNeg = base.find("NegativeHysteresis"); |
| 430 | auto findPos = base.find("PositiveHysteresis"); |
| 431 | |
| 432 | if (findNeg != base.end()) |
| 433 | { |
| 434 | negativeHysteresis = |
| 435 | std::visit(VariantToDoubleVisitor(), findNeg->second); |
| 436 | } |
| 437 | |
| 438 | if (findPos != base.end()) |
| 439 | { |
| 440 | positiveHysteresis = |
| 441 | std::visit(VariantToDoubleVisitor(), findPos->second); |
| 442 | } |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 443 | info.pidInfo.negativeHysteresis = negativeHysteresis; |
| 444 | info.pidInfo.positiveHysteresis = positiveHysteresis; |
| 445 | } |
| 446 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 447 | bool init(sdbusplus::bus::bus& bus, boost::asio::steady_timer& timer) |
| 448 | { |
| 449 | |
| 450 | sensorConfig.clear(); |
| 451 | zoneConfig.clear(); |
| 452 | zoneDetailsConfig.clear(); |
| 453 | |
| 454 | createMatches(bus, timer); |
| 455 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 456 | using DbusVariantType = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 457 | std::variant<uint64_t, int64_t, double, std::string, |
| 458 | std::vector<std::string>, std::vector<double>>; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 459 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 460 | using ManagedObjectType = std::unordered_map< |
| 461 | sdbusplus::message::object_path, |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 462 | std::unordered_map<std::string, |
| 463 | std::unordered_map<std::string, DbusVariantType>>>; |
James Feist | 64f072a | 2018-08-10 16:39:24 -0700 | [diff] [blame] | 464 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 465 | auto mapper = |
| 466 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 467 | "/xyz/openbmc_project/object_mapper", |
| 468 | "xyz.openbmc_project.ObjectMapper", "GetSubTree"); |
James Feist | 26e8c6a | 2018-10-25 10:38:26 -0700 | [diff] [blame] | 469 | mapper.append("/", 0, |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 470 | std::array<const char*, 6>{objectManagerInterface, |
| 471 | pidConfigurationInterface, |
| 472 | pidZoneConfigurationInterface, |
| 473 | stepwiseConfigurationInterface, |
| 474 | sensorInterface, pwmInterface}); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 475 | std::unordered_map< |
| 476 | std::string, std::unordered_map<std::string, std::vector<std::string>>> |
| 477 | respData; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 478 | try |
| 479 | { |
| 480 | auto resp = bus.call(mapper); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 481 | resp.read(respData); |
| 482 | } |
| 483 | catch (sdbusplus::exception_t&) |
| 484 | { |
| 485 | // can't do anything without mapper call data |
| 486 | throw std::runtime_error("ObjectMapper Call Failure"); |
| 487 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 488 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 489 | if (respData.empty()) |
| 490 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 491 | // can't do anything without mapper call data |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 492 | throw std::runtime_error("No configuration data available from Mapper"); |
| 493 | } |
| 494 | // create a map of pair of <has pid configuration, ObjectManager path> |
| 495 | std::unordered_map<std::string, std::pair<bool, std::string>> owners; |
| 496 | // and a map of <path, interface> for sensors |
| 497 | std::unordered_map<std::string, std::string> sensors; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 498 | for (const auto& objectPair : respData) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 499 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 500 | for (const auto& ownerPair : objectPair.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 501 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 502 | auto& owner = owners[ownerPair.first]; |
| 503 | for (const std::string& interface : ownerPair.second) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 504 | { |
| 505 | |
| 506 | if (interface == objectManagerInterface) |
| 507 | { |
| 508 | owner.second = objectPair.first; |
| 509 | } |
| 510 | if (interface == pidConfigurationInterface || |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 511 | interface == pidZoneConfigurationInterface || |
| 512 | interface == stepwiseConfigurationInterface) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 513 | { |
| 514 | owner.first = true; |
| 515 | } |
| 516 | if (interface == sensorInterface || interface == pwmInterface) |
| 517 | { |
| 518 | // we're not interested in pwm sensors, just pwm control |
| 519 | if (interface == sensorInterface && |
| 520 | objectPair.first.find("pwm") != std::string::npos) |
| 521 | { |
| 522 | continue; |
| 523 | } |
| 524 | sensors[objectPair.first] = interface; |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | ManagedObjectType configurations; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 530 | for (const auto& owner : owners) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 531 | { |
| 532 | // skip if no pid configuration (means probably a sensor) |
| 533 | if (!owner.second.first) |
| 534 | { |
| 535 | continue; |
| 536 | } |
| 537 | auto endpoint = bus.new_method_call( |
| 538 | owner.first.c_str(), owner.second.second.c_str(), |
| 539 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 540 | ManagedObjectType configuration; |
| 541 | try |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 542 | { |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 543 | auto responce = bus.call(endpoint); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 544 | responce.read(configuration); |
| 545 | } |
| 546 | catch (sdbusplus::exception_t&) |
| 547 | { |
| 548 | // this shouldn't happen, probably means daemon crashed |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 549 | throw std::runtime_error("Error getting managed objects from " + |
| 550 | owner.first); |
| 551 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 552 | |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 553 | for (auto& pathPair : configuration) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 554 | { |
| 555 | if (pathPair.second.find(pidConfigurationInterface) != |
| 556 | pathPair.second.end() || |
| 557 | pathPair.second.find(pidZoneConfigurationInterface) != |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 558 | pathPair.second.end() || |
| 559 | pathPair.second.find(stepwiseConfigurationInterface) != |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 560 | pathPair.second.end()) |
| 561 | { |
| 562 | configurations.emplace(pathPair); |
| 563 | } |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | // remove controllers from config that aren't in the current profile(s) |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 568 | std::vector<std::string> selectedProfiles = getSelectedProfiles(bus); |
| 569 | if (selectedProfiles.size()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 570 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 571 | for (auto pathIt = configurations.begin(); |
| 572 | pathIt != configurations.end();) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 573 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 574 | for (auto confIt = pathIt->second.begin(); |
| 575 | confIt != pathIt->second.end();) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 576 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 577 | auto profilesFind = confIt->second.find("Profiles"); |
| 578 | if (profilesFind == confIt->second.end()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 579 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 580 | confIt++; |
| 581 | continue; // if no profiles selected, apply always |
| 582 | } |
| 583 | auto profiles = |
| 584 | std::get<std::vector<std::string>>(profilesFind->second); |
| 585 | if (profiles.empty()) |
| 586 | { |
| 587 | confIt++; |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | bool found = false; |
| 592 | for (const std::string& profile : profiles) |
| 593 | { |
| 594 | if (std::find(selectedProfiles.begin(), |
| 595 | selectedProfiles.end(), |
| 596 | profile) != selectedProfiles.end()) |
| 597 | { |
| 598 | found = true; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | if (found) |
| 603 | { |
| 604 | confIt++; |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 605 | } |
| 606 | else |
| 607 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 608 | confIt = pathIt->second.erase(confIt); |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 609 | } |
| 610 | } |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 611 | if (pathIt->second.empty()) |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 612 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 613 | pathIt = configurations.erase(pathIt); |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 614 | } |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 615 | else |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 616 | { |
James Feist | 3987c8b | 2019-05-13 10:43:17 -0700 | [diff] [blame] | 617 | pathIt++; |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 618 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 619 | } |
| 620 | } |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 621 | |
| 622 | // on dbus having an index field is a bit strange, so randomly |
| 623 | // assign index based on name property |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 624 | std::vector<std::string> foundZones; |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 625 | for (const auto& configuration : configurations) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 626 | { |
| 627 | auto findZone = |
| 628 | configuration.second.find(pidZoneConfigurationInterface); |
| 629 | if (findZone != configuration.second.end()) |
| 630 | { |
Patrick Venture | e2ec0f6 | 2018-09-04 12:30:27 -0700 | [diff] [blame] | 631 | const auto& zone = findZone->second; |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 632 | |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 633 | const std::string& name = std::get<std::string>(zone.at("Name")); |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 634 | size_t index = getZoneIndex(name, foundZones); |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 635 | |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 636 | auto& details = zoneDetailsConfig[index]; |
James Feist | 3484bed | 2019-02-25 13:28:18 -0800 | [diff] [blame] | 637 | details.minThermalOutput = std::visit(VariantToDoubleVisitor(), |
| 638 | zone.at("MinThermalOutput")); |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame] | 639 | details.failsafePercent = std::visit(VariantToDoubleVisitor(), |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 640 | zone.at("FailSafePercent")); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 641 | } |
| 642 | auto findBase = configuration.second.find(pidConfigurationInterface); |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 643 | // loop through all the PID configurations and fill out a sensor config |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 644 | if (findBase != configuration.second.end()) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 645 | { |
James Feist | 8c3c51e | 2018-08-08 16:31:43 -0700 | [diff] [blame] | 646 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 647 | const auto& base = |
| 648 | configuration.second.at(pidConfigurationInterface); |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 649 | const std::string pidName = std::get<std::string>(base.at("Name")); |
| 650 | const std::string pidClass = |
| 651 | std::get<std::string>(base.at("Class")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 652 | const std::vector<std::string>& zones = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 653 | std::get<std::vector<std::string>>(base.at("Zones")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 654 | for (const std::string& zone : zones) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 655 | { |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 656 | size_t index = getZoneIndex(zone, foundZones); |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 657 | conf::PIDConf& conf = zoneConfig[index]; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 658 | std::vector<std::string> inputSensorNames( |
| 659 | std::get<std::vector<std::string>>(base.at("Inputs"))); |
| 660 | std::vector<std::string> outputSensorNames; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 661 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 662 | // assumption: all fan pids must have at least one output |
| 663 | if (pidClass == "fan") |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 664 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 665 | outputSensorNames = std::get<std::vector<std::string>>( |
| 666 | getPIDAttribute(base, "Outputs")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 667 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 668 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 669 | std::vector<SensorInterfaceType> inputSensorInterfaces; |
| 670 | std::vector<SensorInterfaceType> outputSensorInterfaces; |
| 671 | /* populate an interface list for different sensor direction |
| 672 | * types (input,output) |
| 673 | */ |
| 674 | /* take the Inputs from the configuration and generate |
| 675 | * a list of dbus descriptors (path, interface). |
| 676 | * Mapping can be many-to-one since an element of Inputs can be |
| 677 | * a regex |
| 678 | */ |
| 679 | for (const std::string& sensorName : inputSensorNames) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 680 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 681 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 682 | inputSensorInterfaces); |
| 683 | } |
| 684 | for (const std::string& sensorName : outputSensorNames) |
| 685 | { |
| 686 | findSensors(sensors, sensorNameToDbusName(sensorName), |
| 687 | outputSensorInterfaces); |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 688 | } |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 689 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 690 | inputSensorNames.clear(); |
| 691 | for (const SensorInterfaceType& inputSensorInterface : |
| 692 | inputSensorInterfaces) |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 693 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 694 | const std::string& dbusInterface = |
| 695 | inputSensorInterface.second; |
| 696 | const std::string& inputSensorPath = |
| 697 | inputSensorInterface.first; |
| 698 | std::string inputSensorName = |
| 699 | getSensorNameFromPath(inputSensorPath); |
| 700 | auto& config = sensorConfig[inputSensorName]; |
| 701 | inputSensorNames.push_back(inputSensorName); |
| 702 | config.type = pidClass; |
| 703 | config.readPath = inputSensorInterface.first; |
| 704 | // todo: maybe un-hardcode this if we run into slower |
| 705 | // timeouts with sensors |
| 706 | if (config.type == "temp") |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 707 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 708 | config.timeout = 0; |
| 709 | config.ignoreDbusMinMax = true; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 710 | } |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 711 | if (dbusInterface != sensorInterface) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 712 | { |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 713 | /* all expected inputs in the configuration are expected |
| 714 | * to be sensor interfaces |
| 715 | */ |
| 716 | throw std::runtime_error( |
| 717 | "sensor at dbus path [" + inputSensorPath + |
| 718 | "] has an interface [" + dbusInterface + |
| 719 | "] that does not match the expected interface of " + |
| 720 | sensorInterface); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 723 | |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 724 | /* fan pids need to pair up tach sensors with their pwm |
| 725 | * counterparts |
| 726 | */ |
| 727 | if (pidClass == "fan") |
| 728 | { |
| 729 | /* If a PID is a fan there should be either |
| 730 | * (1) one output(pwm) per input(tach) |
| 731 | * OR |
| 732 | * (2) one putput(pwm) for all inputs(tach) |
| 733 | * everything else indicates a bad configuration. |
| 734 | */ |
| 735 | bool singlePwm = false; |
| 736 | if (outputSensorInterfaces.size() == 1) |
| 737 | { |
| 738 | /* one pwm, set write paths for all fan sensors to it */ |
| 739 | singlePwm = true; |
| 740 | } |
| 741 | else if (inputSensorInterfaces.size() == |
| 742 | outputSensorInterfaces.size()) |
| 743 | { |
| 744 | /* one to one mapping, each fan sensor gets its own pwm |
| 745 | * control */ |
| 746 | singlePwm = false; |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | throw std::runtime_error( |
| 751 | "fan PID has invalid number of Outputs"); |
| 752 | } |
| 753 | std::string fanSensorName; |
| 754 | std::string pwmPath; |
| 755 | std::string pwmInterface; |
| 756 | if (singlePwm) |
| 757 | { |
| 758 | /* if just a single output(pwm) is provided then use |
| 759 | * that pwm control path for all the fan sensor write |
| 760 | * path configs |
| 761 | */ |
| 762 | pwmPath = outputSensorInterfaces.at(0).first; |
| 763 | pwmInterface = outputSensorInterfaces.at(0).second; |
| 764 | } |
| 765 | for (uint32_t idx = 0; idx < inputSensorInterfaces.size(); |
| 766 | idx++) |
| 767 | { |
| 768 | if (!singlePwm) |
| 769 | { |
| 770 | pwmPath = outputSensorInterfaces.at(idx).first; |
| 771 | pwmInterface = |
| 772 | outputSensorInterfaces.at(idx).second; |
| 773 | } |
| 774 | if (pwmInterface != pwmInterface) |
| 775 | { |
| 776 | throw std::runtime_error( |
| 777 | "fan pwm control at dbus path [" + pwmPath + |
| 778 | "] has an interface [" + pwmInterface + |
| 779 | "] that does not match the expected interface " |
| 780 | "of " + |
| 781 | pwmInterface); |
| 782 | } |
| 783 | const std::string& fanPath = |
| 784 | inputSensorInterfaces.at(idx).first; |
| 785 | fanSensorName = getSensorNameFromPath(fanPath); |
| 786 | auto& fanConfig = sensorConfig[fanSensorName]; |
| 787 | fanConfig.writePath = pwmPath; |
| 788 | // todo: un-hardcode this if there are fans with |
| 789 | // different ranges |
| 790 | fanConfig.max = 255; |
| 791 | fanConfig.min = 0; |
| 792 | } |
| 793 | } |
James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 794 | // if the sensors aren't available in the current state, don't |
| 795 | // add them to the configuration. |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 796 | if (inputSensorNames.empty()) |
James Feist | 11d243d | 2019-06-24 16:18:40 -0700 | [diff] [blame] | 797 | { |
| 798 | continue; |
| 799 | } |
| 800 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 801 | std::string offsetType; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 802 | |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 803 | // SetPointOffset is a threshold value to pull from the sensor |
| 804 | // to apply an offset. For upper thresholds this means the |
| 805 | // setpoint is usually negative. |
| 806 | auto findSetpointOffset = base.find("SetPointOffset"); |
| 807 | if (findSetpointOffset != base.end()) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 808 | { |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 809 | offsetType = |
| 810 | std::get<std::string>(findSetpointOffset->second); |
| 811 | if (std::find(thresholds::types.begin(), |
| 812 | thresholds::types.end(), |
| 813 | offsetType) == thresholds::types.end()) |
| 814 | { |
| 815 | throw std::runtime_error("Unsupported type: " + |
| 816 | offsetType); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | if (offsetType.empty()) |
| 821 | { |
| 822 | struct conf::ControllerInfo& info = |
| 823 | conf[std::get<std::string>(base.at("Name"))]; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 824 | info.inputs = std::move(inputSensorNames); |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 825 | populatePidInfo(bus, base, info, nullptr); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 826 | } |
| 827 | else |
| 828 | { |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 829 | // we have to split up the inputs, as in practice t-control |
| 830 | // values will differ, making setpoints differ |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 831 | for (const std::string& input : inputSensorNames) |
James Feist | 5ec2027 | 2019-07-10 11:59:57 -0700 | [diff] [blame] | 832 | { |
| 833 | struct conf::ControllerInfo& info = conf[input]; |
| 834 | info.inputs.emplace_back(input); |
| 835 | populatePidInfo(bus, base, info, &offsetType); |
| 836 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 837 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | auto findStepwise = |
| 841 | configuration.second.find(stepwiseConfigurationInterface); |
| 842 | if (findStepwise != configuration.second.end()) |
| 843 | { |
| 844 | const auto& base = findStepwise->second; |
| 845 | const std::vector<std::string>& zones = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 846 | std::get<std::vector<std::string>>(base.at("Zones")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 847 | for (const std::string& zone : zones) |
| 848 | { |
James Feist | ffd418b | 2018-11-15 14:46:36 -0800 | [diff] [blame] | 849 | size_t index = getZoneIndex(zone, foundZones); |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 850 | conf::PIDConf& conf = zoneConfig[index]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 851 | |
| 852 | std::vector<std::string> inputs; |
| 853 | std::vector<std::string> sensorNames = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 854 | std::get<std::vector<std::string>>(base.at("Inputs")); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 855 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 856 | bool sensorFound = false; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 857 | for (const std::string& sensorName : sensorNames) |
| 858 | { |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 859 | std::vector<std::pair<std::string, std::string>> |
| 860 | sensorPathIfacePairs; |
Jason Ling | f3b04fd | 2020-07-24 09:33:04 -0700 | [diff] [blame] | 861 | if (!findSensors(sensors, sensorNameToDbusName(sensorName), |
| 862 | sensorPathIfacePairs)) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 863 | { |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 864 | break; |
| 865 | } |
| 866 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 867 | for (const auto& sensorPathIfacePair : sensorPathIfacePairs) |
| 868 | { |
| 869 | size_t idx = |
| 870 | sensorPathIfacePair.first.find_last_of("/") + 1; |
| 871 | std::string shortName = |
| 872 | sensorPathIfacePair.first.substr(idx); |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 873 | |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 874 | inputs.push_back(shortName); |
| 875 | auto& config = sensorConfig[shortName]; |
Patrick Venture | 69c5106 | 2019-02-11 09:46:03 -0800 | [diff] [blame] | 876 | config.readPath = sensorPathIfacePair.first; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 877 | config.type = "temp"; |
James Feist | 3660b38 | 2019-11-11 16:29:19 -0800 | [diff] [blame] | 878 | config.ignoreDbusMinMax = true; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 879 | // todo: maybe un-hardcode this if we run into slower |
| 880 | // timeouts with sensors |
| 881 | |
James Feist | 2642cb5 | 2019-02-25 13:00:16 -0800 | [diff] [blame] | 882 | config.timeout = 0; |
James Feist | 1738e2a | 2019-02-04 15:57:03 -0800 | [diff] [blame] | 883 | sensorFound = true; |
| 884 | } |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 885 | } |
| 886 | if (!sensorFound) |
| 887 | { |
| 888 | continue; |
| 889 | } |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 890 | struct conf::ControllerInfo& info = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 891 | conf[std::get<std::string>(base.at("Name"))]; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 892 | info.inputs = std::move(inputs); |
| 893 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 894 | info.type = "stepwise"; |
| 895 | info.stepwiseInfo.ts = 1.0; // currently unused |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 896 | info.stepwiseInfo.positiveHysteresis = 0.0; |
| 897 | info.stepwiseInfo.negativeHysteresis = 0.0; |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 898 | std::string subtype = std::get<std::string>(base.at("Class")); |
| 899 | |
| 900 | info.stepwiseInfo.isCeiling = (subtype == "Ceiling"); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 901 | auto findPosHyst = base.find("PositiveHysteresis"); |
| 902 | auto findNegHyst = base.find("NegativeHysteresis"); |
| 903 | if (findPosHyst != base.end()) |
| 904 | { |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 905 | info.stepwiseInfo.positiveHysteresis = std::visit( |
James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 906 | VariantToDoubleVisitor(), findPosHyst->second); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 907 | } |
| 908 | if (findNegHyst != base.end()) |
| 909 | { |
James Feist | 5782ab8 | 2019-04-02 08:38:48 -0700 | [diff] [blame] | 910 | info.stepwiseInfo.negativeHysteresis = std::visit( |
James Feist | 208abce | 2018-12-06 09:59:10 -0800 | [diff] [blame] | 911 | VariantToDoubleVisitor(), findNegHyst->second); |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 912 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 913 | std::vector<double> readings = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 914 | std::get<std::vector<double>>(base.at("Reading")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 915 | if (readings.size() > ec::maxStepwisePoints) |
| 916 | { |
| 917 | throw std::invalid_argument("Too many stepwise points."); |
| 918 | } |
| 919 | if (readings.empty()) |
| 920 | { |
| 921 | throw std::invalid_argument( |
| 922 | "Must have one stepwise point."); |
| 923 | } |
| 924 | std::copy(readings.begin(), readings.end(), |
| 925 | info.stepwiseInfo.reading); |
| 926 | if (readings.size() < ec::maxStepwisePoints) |
| 927 | { |
| 928 | info.stepwiseInfo.reading[readings.size()] = |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 929 | std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 930 | } |
| 931 | std::vector<double> outputs = |
James Feist | 1f802f5 | 2019-02-08 13:51:43 -0800 | [diff] [blame] | 932 | std::get<std::vector<double>>(base.at("Output")); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 933 | if (readings.size() != outputs.size()) |
| 934 | { |
| 935 | throw std::invalid_argument( |
| 936 | "Outputs size must match readings"); |
| 937 | } |
| 938 | std::copy(outputs.begin(), outputs.end(), |
| 939 | info.stepwiseInfo.output); |
| 940 | if (outputs.size() < ec::maxStepwisePoints) |
| 941 | { |
| 942 | info.stepwiseInfo.output[outputs.size()] = |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 943 | std::numeric_limits<double>::quiet_NaN(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 944 | } |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | } |
James Feist | f0096a0 | 2019-02-21 11:25:22 -0800 | [diff] [blame] | 948 | if constexpr (DEBUG) |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 949 | { |
| 950 | debugPrint(); |
| 951 | } |
James Feist | c959c42 | 2018-11-01 12:33:40 -0700 | [diff] [blame] | 952 | if (zoneConfig.empty() || zoneDetailsConfig.empty()) |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 953 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 954 | std::cerr |
| 955 | << "No fan zones, application pausing until new configuration\n"; |
| 956 | return false; |
James Feist | 50fdfe3 | 2018-09-24 15:51:09 -0700 | [diff] [blame] | 957 | } |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 958 | return true; |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 959 | } |
| 960 | } // namespace dbus_configuration |