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