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