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