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